hotplug-memory.c 22.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-or-later
2 3 4 5 6 7
/*
 * pseries Memory Hotplug infrastructure.
 *
 * Copyright (C) 2008 Badari Pulavarty, IBM Corporation
 */

8 9
#define pr_fmt(fmt)	"pseries-hotplug-mem: " fmt

10
#include <linux/of.h>
R
Rob Herring 已提交
11
#include <linux/of_address.h>
Y
Yinghai Lu 已提交
12
#include <linux/memblock.h>
13
#include <linux/memory.h>
14
#include <linux/memory_hotplug.h>
15
#include <linux/slab.h>
16

17 18
#include <asm/firmware.h>
#include <asm/machdep.h>
R
Rob Herring 已提交
19
#include <asm/prom.h>
20
#include <asm/sparsemem.h>
21
#include <asm/fadump.h>
22
#include <asm/drmem.h>
23
#include "pseries.h"
24

25 26
static bool rtas_hp_event;

27
unsigned long pseries_memory_block_size(void)
28 29
{
	struct device_node *np;
30 31
	unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
	struct resource r;
32 33 34

	np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
	if (np) {
35
		const __be64 *size;
36 37

		size = of_get_property(np, "ibm,lmb-size", NULL);
38 39
		if (size)
			memblock_size = be64_to_cpup(size);
40
		of_node_put(np);
41 42
	} else  if (machine_is(pseries)) {
		/* This fallback really only applies to pseries */
43 44 45 46
		unsigned int memzero_size = 0;

		np = of_find_node_by_path("/memory@0");
		if (np) {
47 48
			if (!of_address_to_resource(np, 0, &r))
				memzero_size = resource_size(&r);
49 50 51 52 53 54 55 56 57 58 59 60
			of_node_put(np);
		}

		if (memzero_size) {
			/* We now know the size of memory@0, use this to find
			 * the first memoryblock and get its size.
			 */
			char buf[64];

			sprintf(buf, "/memory@%x", memzero_size);
			np = of_find_node_by_path(buf);
			if (np) {
61 62
				if (!of_address_to_resource(np, 0, &r))
					memblock_size = resource_size(&r);
63 64 65 66 67 68 69
				of_node_put(np);
			}
		}
	}
	return memblock_size;
}

70
static void dlpar_free_property(struct property *prop)
71 72 73 74 75 76
{
	kfree(prop->name);
	kfree(prop->value);
	kfree(prop);
}

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static struct property *dlpar_clone_property(struct property *prop,
					     u32 prop_size)
{
	struct property *new_prop;

	new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
	if (!new_prop)
		return NULL;

	new_prop->name = kstrdup(prop->name, GFP_KERNEL);
	new_prop->value = kzalloc(prop_size, GFP_KERNEL);
	if (!new_prop->name || !new_prop->value) {
		dlpar_free_property(new_prop);
		return NULL;
	}

	memcpy(new_prop->value, prop->value, prop->length);
	new_prop->length = prop_size;

	of_property_set_flag(new_prop, OF_DYNAMIC);
	return new_prop;
}

100 101 102
static bool find_aa_index(struct device_node *dr_node,
			 struct property *ala_prop,
			 const u32 *lmb_assoc, u32 *aa_index)
103
{
104 105
	u32 *assoc_arrays, new_prop_size;
	struct property *new_prop;
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	int aa_arrays, aa_array_entries, aa_array_sz;
	int i, index;

	/*
	 * The ibm,associativity-lookup-arrays property is defined to be
	 * a 32-bit value specifying the number of associativity arrays
	 * followed by a 32-bitvalue specifying the number of entries per
	 * array, followed by the associativity arrays.
	 */
	assoc_arrays = ala_prop->value;

	aa_arrays = be32_to_cpu(assoc_arrays[0]);
	aa_array_entries = be32_to_cpu(assoc_arrays[1]);
	aa_array_sz = aa_array_entries * sizeof(u32);

	for (i = 0; i < aa_arrays; i++) {
		index = (i * aa_array_entries) + 2;

		if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
			continue;

127 128
		*aa_index = i;
		return true;
129 130
	}

131 132 133 134
	new_prop_size = ala_prop->length + aa_array_sz;
	new_prop = dlpar_clone_property(ala_prop, new_prop_size);
	if (!new_prop)
		return false;
135

136
	assoc_arrays = new_prop->value;
137

138 139
	/* increment the number of entries in the lookup array */
	assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
140

141 142 143
	/* copy the new associativity into the lookup array */
	index = aa_arrays * aa_array_entries + 2;
	memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
144

145
	of_update_property(dr_node, new_prop);
146

147 148 149 150 151 152 153
	/*
	 * The associativity lookup array index for this lmb is
	 * number of entries - 1 since we added its associativity
	 * to the end of the lookup array.
	 */
	*aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
	return true;
154 155
}

156
static int update_lmb_associativity_index(struct drmem_lmb *lmb)
157 158
{
	struct device_node *parent, *lmb_node, *dr_node;
159
	struct property *ala_prop;
160 161
	const u32 *lmb_assoc;
	u32 aa_index;
162
	bool found;
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

	parent = of_find_node_by_path("/");
	if (!parent)
		return -ENODEV;

	lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
					     parent);
	of_node_put(parent);
	if (!lmb_node)
		return -EINVAL;

	lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
	if (!lmb_assoc) {
		dlpar_free_cc_nodes(lmb_node);
		return -ENODEV;
	}

	dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
	if (!dr_node) {
		dlpar_free_cc_nodes(lmb_node);
		return -ENODEV;
	}

186 187 188 189
	ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
				    NULL);
	if (!ala_prop) {
		of_node_put(dr_node);
190 191 192 193
		dlpar_free_cc_nodes(lmb_node);
		return -ENODEV;
	}

194
	found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
195

196
	of_node_put(dr_node);
197 198
	dlpar_free_cc_nodes(lmb_node);

199
	if (!found) {
200 201
		pr_err("Could not find LMB associativity\n");
		return -1;
202 203 204
	}

	lmb->aa_index = aa_index;
205
	return 0;
206 207
}

208
static struct memory_block *lmb_to_memblock(struct drmem_lmb *lmb)
209 210 211 212 213 214 215 216 217 218 219 220
{
	unsigned long section_nr;
	struct mem_section *mem_sect;
	struct memory_block *mem_block;

	section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
	mem_sect = __nr_to_section(section_nr);

	mem_block = find_memory_block(mem_sect);
	return mem_block;
}

221 222 223 224 225
static int get_lmb_range(u32 drc_index, int n_lmbs,
			 struct drmem_lmb **start_lmb,
			 struct drmem_lmb **end_lmb)
{
	struct drmem_lmb *lmb, *start, *end;
226
	struct drmem_lmb *limit;
227 228 229 230 231 232 233 234 235 236 237 238

	start = NULL;
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
			start = lmb;
			break;
		}
	}

	if (!start)
		return -EINVAL;

239
	end = &start[n_lmbs];
240

241 242
	limit = &drmem_info->lmbs[drmem_info->n_lmbs];
	if (end > limit)
243 244 245 246 247 248 249 250
		return -EINVAL;

	*start_lmb = start;
	*end_lmb = end;
	return 0;
}

static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
{
	struct memory_block *mem_block;
	int rc;

	mem_block = lmb_to_memblock(lmb);
	if (!mem_block)
		return -EINVAL;

	if (online && mem_block->dev.offline)
		rc = device_online(&mem_block->dev);
	else if (!online && !mem_block->dev.offline)
		rc = device_offline(&mem_block->dev);
	else
		rc = 0;

	put_device(&mem_block->dev);

	return rc;
}

271
static int dlpar_online_lmb(struct drmem_lmb *lmb)
272 273 274 275
{
	return dlpar_change_lmb_state(lmb, true);
}

276
#ifdef CONFIG_MEMORY_HOTREMOVE
277
static int dlpar_offline_lmb(struct drmem_lmb *lmb)
278 279 280 281
{
	return dlpar_change_lmb_state(lmb, false);
}

282 283 284 285 286
static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
{
	unsigned long block_sz, start_pfn;
	int sections_per_block;
	int i, nid;
287

288
	start_pfn = base >> PAGE_SHIFT;
289

290 291 292 293
	lock_device_hotplug();

	if (!pfn_valid(start_pfn))
		goto out;
294

295
	block_sz = pseries_memory_block_size();
296 297
	sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
	nid = memory_add_physaddr_to_nid(base);
298

299
	for (i = 0; i < sections_per_block; i++) {
300
		__remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
301
		base += MIN_MEMORY_BLOCK_SIZE;
302
	}
303

304
out:
305
	/* Update memory regions for memory remove */
Y
Yinghai Lu 已提交
306
	memblock_remove(base, memblock_size);
307
	unlock_device_hotplug();
308
	return 0;
309 310
}

311
static int pseries_remove_mem_node(struct device_node *np)
312
{
313
	const __be32 *regs;
314
	unsigned long base;
315
	unsigned int lmb_size;
316 317 318 319 320
	int ret = -EINVAL;

	/*
	 * Check to see if we are actually removing memory
	 */
321
	if (!of_node_is_type(np, "memory"))
322 323 324
		return 0;

	/*
325
	 * Find the base address and size of the memblock
326 327 328 329 330
	 */
	regs = of_get_property(np, "reg", NULL);
	if (!regs)
		return ret;

331 332
	base = be64_to_cpu(*(unsigned long *)regs);
	lmb_size = be32_to_cpu(regs[3]);
333

334 335
	pseries_remove_memblock(base, lmb_size);
	return 0;
336
}
337

338
static bool lmb_is_removable(struct drmem_lmb *lmb)
339 340 341 342
{
	if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
		return false;

343
#ifdef CONFIG_FA_DUMP
344 345 346 347
	/*
	 * Don't hot-remove memory that falls in fadump boot memory area
	 * and memory that is reserved for capturing old kernel memory.
	 */
348
	if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
349 350
		return false;
#endif
351 352
	/* device_offline() will determine if we can actually remove this lmb */
	return true;
353 354
}

355
static int dlpar_add_lmb(struct drmem_lmb *);
356

357
static int dlpar_remove_lmb(struct drmem_lmb *lmb)
358 359
{
	unsigned long block_sz;
360
	int rc;
361 362 363 364

	if (!lmb_is_removable(lmb))
		return -EINVAL;

365
	rc = dlpar_offline_lmb(lmb);
366 367 368 369 370
	if (rc)
		return rc;

	block_sz = pseries_memory_block_size();

371
	__remove_memory(lmb->nid, lmb->base_addr, block_sz);
372 373 374 375

	/* Update memory regions for memory remove */
	memblock_remove(lmb->base_addr, block_sz);

376
	invalidate_lmb_associativity_index(lmb);
377
	lmb_clear_nid(lmb);
378 379
	lmb->flags &= ~DRCONF_MEM_ASSIGNED;

380 381 382
	return 0;
}

383
static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
384
{
385
	struct drmem_lmb *lmb;
386 387
	int lmbs_removed = 0;
	int lmbs_available = 0;
388
	int rc;
389 390 391 392 393 394 395

	pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);

	if (lmbs_to_remove == 0)
		return -EINVAL;

	/* Validate that there are enough LMBs to satisfy the request */
396 397
	for_each_drmem_lmb(lmb) {
		if (lmb_is_removable(lmb))
398
			lmbs_available++;
399 400 401

		if (lmbs_available == lmbs_to_remove)
			break;
402 403
	}

404 405 406
	if (lmbs_available < lmbs_to_remove) {
		pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
			lmbs_available, lmbs_to_remove);
407
		return -EINVAL;
408
	}
409

410 411
	for_each_drmem_lmb(lmb) {
		rc = dlpar_remove_lmb(lmb);
412 413 414 415 416 417
		if (rc)
			continue;

		/* Mark this lmb so we can add it later if all of the
		 * requested LMBs cannot be removed.
		 */
418 419 420 421 422
		drmem_mark_lmb_reserved(lmb);

		lmbs_removed++;
		if (lmbs_removed == lmbs_to_remove)
			break;
423 424 425 426 427
	}

	if (lmbs_removed != lmbs_to_remove) {
		pr_err("Memory hot-remove failed, adding LMB's back\n");

428 429
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
430 431
				continue;

432
			rc = dlpar_add_lmb(lmb);
433 434
			if (rc)
				pr_err("Failed to add LMB back, drc index %x\n",
435
				       lmb->drc_index);
436

437
			drmem_remove_lmb_reservation(lmb);
438 439 440 441
		}

		rc = -EINVAL;
	} else {
442 443
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
444 445
				continue;

446
			dlpar_release_drc(lmb->drc_index);
447
			pr_info("Memory at %llx was hot-removed\n",
448
				lmb->base_addr);
449

450
			drmem_remove_lmb_reservation(lmb);
451 452 453 454 455 456 457
		}
		rc = 0;
	}

	return rc;
}

458
static int dlpar_memory_remove_by_index(u32 drc_index)
459
{
460
	struct drmem_lmb *lmb;
461
	int lmb_found;
462
	int rc;
463 464 465 466

	pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);

	lmb_found = 0;
467 468
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
469
			lmb_found = 1;
470
			rc = dlpar_remove_lmb(lmb);
471
			if (!rc)
472
				dlpar_release_drc(lmb->drc_index);
473

474 475 476 477 478 479 480 481 482
			break;
		}
	}

	if (!lmb_found)
		rc = -EINVAL;

	if (rc)
		pr_info("Failed to hot-remove memory at %llx\n",
483
			lmb->base_addr);
484
	else
485
		pr_info("Memory at %llx was hot-removed\n", lmb->base_addr);
486 487 488 489

	return rc;
}

490
static int dlpar_memory_readd_by_index(u32 drc_index)
491
{
492
	struct drmem_lmb *lmb;
493
	int lmb_found;
494
	int rc;
495 496 497 498

	pr_info("Attempting to update LMB, drc index %x\n", drc_index);

	lmb_found = 0;
499 500
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
501
			lmb_found = 1;
502
			rc = dlpar_remove_lmb(lmb);
503
			if (!rc) {
504
				rc = dlpar_add_lmb(lmb);
505
				if (rc)
506
					dlpar_release_drc(lmb->drc_index);
507 508 509 510 511 512 513 514 515 516
			}
			break;
		}
	}

	if (!lmb_found)
		rc = -EINVAL;

	if (rc)
		pr_info("Failed to update memory at %llx\n",
517
			lmb->base_addr);
518
	else
519
		pr_info("Memory at %llx was updated\n", lmb->base_addr);
520 521 522

	return rc;
}
523

524
static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
525
{
526 527 528
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
529 530 531 532 533 534 535

	pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
		lmbs_to_remove, drc_index);

	if (lmbs_to_remove == 0)
		return -EINVAL;

536 537
	rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
	if (rc)
538 539 540
		return -EINVAL;

	/* Validate that there are enough LMBs to satisfy the request */
541 542
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_RESERVED)
543 544 545 546 547 548 549 550
			break;

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_remove)
		return -EINVAL;

551 552
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
553 554
			continue;

555
		rc = dlpar_remove_lmb(lmb);
556 557 558
		if (rc)
			break;

559
		drmem_mark_lmb_reserved(lmb);
560 561 562 563 564
	}

	if (rc) {
		pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");

565 566 567

		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
568 569
				continue;

570
			rc = dlpar_add_lmb(lmb);
571 572
			if (rc)
				pr_err("Failed to add LMB, drc index %x\n",
573
				       lmb->drc_index);
574

575
			drmem_remove_lmb_reservation(lmb);
576 577 578
		}
		rc = -EINVAL;
	} else {
579 580
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
581 582
				continue;

583
			dlpar_release_drc(lmb->drc_index);
584
			pr_info("Memory at %llx (drc index %x) was hot-removed\n",
585
				lmb->base_addr, lmb->drc_index);
586

587
			drmem_remove_lmb_reservation(lmb);
588 589 590 591 592 593
		}
	}

	return rc;
}

594 595 596 597 598 599
#else
static inline int pseries_remove_memblock(unsigned long base,
					  unsigned int memblock_size)
{
	return -EOPNOTSUPP;
}
600
static inline int pseries_remove_mem_node(struct device_node *np)
601
{
602
	return 0;
603
}
604 605 606 607
static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
{
	return -EOPNOTSUPP;
}
608
static int dlpar_remove_lmb(struct drmem_lmb *lmb)
609 610 611
{
	return -EOPNOTSUPP;
}
612
static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
613 614 615
{
	return -EOPNOTSUPP;
}
616
static int dlpar_memory_remove_by_index(u32 drc_index)
617 618 619
{
	return -EOPNOTSUPP;
}
620
static int dlpar_memory_readd_by_index(u32 drc_index)
621 622 623
{
	return -EOPNOTSUPP;
}
624

625
static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
626 627 628
{
	return -EOPNOTSUPP;
}
629
#endif /* CONFIG_MEMORY_HOTREMOVE */
630

631
static int dlpar_add_lmb(struct drmem_lmb *lmb)
632 633
{
	unsigned long block_sz;
634
	int rc;
635

636 637 638
	if (lmb->flags & DRCONF_MEM_ASSIGNED)
		return -EINVAL;

639
	rc = update_lmb_associativity_index(lmb);
640 641 642 643 644
	if (rc) {
		dlpar_release_drc(lmb->drc_index);
		return rc;
	}

645
	lmb_set_nid(lmb);
646 647 648
	block_sz = memory_block_size_bytes();

	/* Add the memory */
649
	rc = __add_memory(lmb->nid, lmb->base_addr, block_sz);
650
	if (rc) {
651
		invalidate_lmb_associativity_index(lmb);
652 653 654 655 656
		return rc;
	}

	rc = dlpar_online_lmb(lmb);
	if (rc) {
657
		__remove_memory(lmb->nid, lmb->base_addr, block_sz);
658
		invalidate_lmb_associativity_index(lmb);
659
		lmb_clear_nid(lmb);
660
	} else {
661
		lmb->flags |= DRCONF_MEM_ASSIGNED;
662
	}
663 664 665 666

	return rc;
}

667
static int dlpar_memory_add_by_count(u32 lmbs_to_add)
668
{
669
	struct drmem_lmb *lmb;
670 671
	int lmbs_available = 0;
	int lmbs_added = 0;
672
	int rc;
673 674 675 676 677 678 679

	pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);

	if (lmbs_to_add == 0)
		return -EINVAL;

	/* Validate that there are enough LMBs to satisfy the request */
680 681
	for_each_drmem_lmb(lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
682
			lmbs_available++;
683 684 685

		if (lmbs_available == lmbs_to_add)
			break;
686 687 688 689 690
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

691 692
	for_each_drmem_lmb(lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
693 694
			continue;

695
		rc = dlpar_acquire_drc(lmb->drc_index);
696 697 698
		if (rc)
			continue;

699
		rc = dlpar_add_lmb(lmb);
700
		if (rc) {
701
			dlpar_release_drc(lmb->drc_index);
702 703 704
			continue;
		}

705 706 707
		/* Mark this lmb so we can remove it later if all of the
		 * requested LMBs cannot be added.
		 */
708 709 710 711 712
		drmem_mark_lmb_reserved(lmb);

		lmbs_added++;
		if (lmbs_added == lmbs_to_add)
			break;
713 714 715
	}

	if (lmbs_added != lmbs_to_add) {
716 717
		pr_err("Memory hot-add failed, removing any added LMBs\n");

718 719
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
720 721
				continue;

722
			rc = dlpar_remove_lmb(lmb);
723 724
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
725
				       lmb->drc_index);
726
			else
727 728 729
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
730
		}
731 732
		rc = -EINVAL;
	} else {
733 734
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
735 736 737
				continue;

			pr_info("Memory at %llx (drc index %x) was hot-added\n",
738 739
				lmb->base_addr, lmb->drc_index);
			drmem_remove_lmb_reservation(lmb);
740
		}
741
		rc = 0;
742 743 744 745 746
	}

	return rc;
}

747
static int dlpar_memory_add_by_index(u32 drc_index)
748
{
749 750
	struct drmem_lmb *lmb;
	int rc, lmb_found;
751 752 753 754

	pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);

	lmb_found = 0;
755 756
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
757
			lmb_found = 1;
758
			rc = dlpar_acquire_drc(lmb->drc_index);
759
			if (!rc) {
760
				rc = dlpar_add_lmb(lmb);
761
				if (rc)
762
					dlpar_release_drc(lmb->drc_index);
763 764
			}

765 766 767 768 769 770 771 772 773 774 775
			break;
		}
	}

	if (!lmb_found)
		rc = -EINVAL;

	if (rc)
		pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
	else
		pr_info("Memory at %llx (drc index %x) was hot-added\n",
776
			lmb->base_addr, drc_index);
777 778 779 780

	return rc;
}

781
static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
782
{
783 784 785
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
786 787 788 789 790 791 792

	pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
		lmbs_to_add, drc_index);

	if (lmbs_to_add == 0)
		return -EINVAL;

793 794
	rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
	if (rc)
795 796 797
		return -EINVAL;

	/* Validate that the LMBs in this range are not reserved */
798 799
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_RESERVED)
800 801 802 803 804 805 806 807
			break;

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

808 809
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
810 811
			continue;

812
		rc = dlpar_acquire_drc(lmb->drc_index);
813 814 815
		if (rc)
			break;

816
		rc = dlpar_add_lmb(lmb);
817
		if (rc) {
818
			dlpar_release_drc(lmb->drc_index);
819 820 821
			break;
		}

822
		drmem_mark_lmb_reserved(lmb);
823 824 825 826 827
	}

	if (rc) {
		pr_err("Memory indexed-count-add failed, removing any added LMBs\n");

828 829
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
830 831
				continue;

832
			rc = dlpar_remove_lmb(lmb);
833 834
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
835
				       lmb->drc_index);
836
			else
837 838 839
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
840 841 842
		}
		rc = -EINVAL;
	} else {
843 844
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
845 846 847
				continue;

			pr_info("Memory at %llx (drc index %x) was hot-added\n",
848 849
				lmb->base_addr, lmb->drc_index);
			drmem_remove_lmb_reservation(lmb);
850 851 852 853 854 855
		}
	}

	return rc;
}

856 857
int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
{
858 859 860
	u32 count, drc_index;
	int rc;

861 862 863
	lock_device_hotplug();

	switch (hp_elog->action) {
864
	case PSERIES_HP_ELOG_ACTION_ADD:
865 866
		switch (hp_elog->id_type) {
		case PSERIES_HP_ELOG_ID_DRC_COUNT:
867
			count = hp_elog->_drc_u.drc_count;
868
			rc = dlpar_memory_add_by_count(count);
869 870
			break;
		case PSERIES_HP_ELOG_ID_DRC_INDEX:
871
			drc_index = hp_elog->_drc_u.drc_index;
872
			rc = dlpar_memory_add_by_index(drc_index);
873 874
			break;
		case PSERIES_HP_ELOG_ID_DRC_IC:
875 876
			count = hp_elog->_drc_u.ic.count;
			drc_index = hp_elog->_drc_u.ic.index;
877
			rc = dlpar_memory_add_by_ic(count, drc_index);
878 879
			break;
		default:
880
			rc = -EINVAL;
881
			break;
882 883
		}

884
		break;
885
	case PSERIES_HP_ELOG_ACTION_REMOVE:
886 887
		switch (hp_elog->id_type) {
		case PSERIES_HP_ELOG_ID_DRC_COUNT:
888
			count = hp_elog->_drc_u.drc_count;
889
			rc = dlpar_memory_remove_by_count(count);
890 891
			break;
		case PSERIES_HP_ELOG_ID_DRC_INDEX:
892
			drc_index = hp_elog->_drc_u.drc_index;
893
			rc = dlpar_memory_remove_by_index(drc_index);
894 895
			break;
		case PSERIES_HP_ELOG_ID_DRC_IC:
896 897
			count = hp_elog->_drc_u.ic.count;
			drc_index = hp_elog->_drc_u.ic.index;
898
			rc = dlpar_memory_remove_by_ic(count, drc_index);
899 900
			break;
		default:
901
			rc = -EINVAL;
902
			break;
903 904
		}

905
		break;
906
	case PSERIES_HP_ELOG_ACTION_READD:
907
		drc_index = hp_elog->_drc_u.drc_index;
908
		rc = dlpar_memory_readd_by_index(drc_index);
909
		break;
910 911 912 913 914 915
	default:
		pr_err("Invalid action (%d) specified\n", hp_elog->action);
		rc = -EINVAL;
		break;
	}

916 917 918 919 920 921
	if (!rc) {
		rtas_hp_event = true;
		rc = drmem_update_dt();
		rtas_hp_event = false;
	}

922 923 924 925
	unlock_device_hotplug();
	return rc;
}

926
static int pseries_add_mem_node(struct device_node *np)
927
{
928
	const __be32 *regs;
929
	unsigned long base;
930
	unsigned int lmb_size;
931 932 933 934 935
	int ret = -EINVAL;

	/*
	 * Check to see if we are actually adding memory
	 */
936
	if (!of_node_is_type(np, "memory"))
937 938 939
		return 0;

	/*
Y
Yinghai Lu 已提交
940
	 * Find the base and size of the memblock
941 942 943 944 945
	 */
	regs = of_get_property(np, "reg", NULL);
	if (!regs)
		return ret;

946 947
	base = be64_to_cpu(*(unsigned long *)regs);
	lmb_size = be32_to_cpu(regs[3]);
948 949 950 951

	/*
	 * Update memory region to represent the memory add
	 */
952
	ret = memblock_add(base, lmb_size);
953 954 955
	return (ret < 0) ? -EINVAL : 0;
}

956
static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
957
{
958
	struct of_drconf_cell_v1 *new_drmem, *old_drmem;
959
	unsigned long memblock_size;
960
	u32 entries;
961
	__be32 *p;
962
	int i, rc = -EINVAL;
963

964 965 966
	if (rtas_hp_event)
		return 0;

967
	memblock_size = pseries_memory_block_size();
968
	if (!memblock_size)
969 970
		return -EINVAL;

971 972 973
	if (!pr->old_prop)
		return 0;

974
	p = (__be32 *) pr->old_prop->value;
975 976 977 978 979
	if (!p)
		return -EINVAL;

	/* The first int of the property is the number of lmb's described
	 * by the property. This is followed by an array of of_drconf_cell
980
	 * entries. Get the number of entries and skip to the array of
981 982
	 * of_drconf_cell's.
	 */
983
	entries = be32_to_cpu(*p++);
984
	old_drmem = (struct of_drconf_cell_v1 *)p;
985

986
	p = (__be32 *)pr->prop->value;
987
	p++;
988
	new_drmem = (struct of_drconf_cell_v1 *)p;
989 990

	for (i = 0; i < entries; i++) {
991 992 993 994
		if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
		    (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
			rc = pseries_remove_memblock(
				be64_to_cpu(old_drmem[i].base_addr),
995 996
						     memblock_size);
			break;
997 998 999 1000 1001
		} else if ((!(be32_to_cpu(old_drmem[i].flags) &
			    DRCONF_MEM_ASSIGNED)) &&
			    (be32_to_cpu(new_drmem[i].flags) &
			    DRCONF_MEM_ASSIGNED)) {
			rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
1002 1003 1004 1005
					  memblock_size);
			rc = (rc < 0) ? -EINVAL : 0;
			break;
		}
1006 1007
	}
	return rc;
1008 1009
}

1010
static int pseries_memory_notifier(struct notifier_block *nb,
1011
				   unsigned long action, void *data)
1012
{
1013
	struct of_reconfig_data *rd = data;
1014
	int err = 0;
1015 1016

	switch (action) {
1017
	case OF_RECONFIG_ATTACH_NODE:
1018
		err = pseries_add_mem_node(rd->dn);
1019
		break;
1020
	case OF_RECONFIG_DETACH_NODE:
1021
		err = pseries_remove_mem_node(rd->dn);
1022
		break;
1023
	case OF_RECONFIG_UPDATE_PROPERTY:
1024 1025
		if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
			err = pseries_update_drconf_memory(rd);
1026 1027
		break;
	}
1028
	return notifier_from_errno(err);
1029 1030 1031 1032 1033 1034 1035 1036 1037
}

static struct notifier_block pseries_mem_nb = {
	.notifier_call = pseries_memory_notifier,
};

static int __init pseries_memory_hotplug_init(void)
{
	if (firmware_has_feature(FW_FEATURE_LPAR))
1038
		of_reconfig_notifier_register(&pseries_mem_nb);
1039 1040 1041 1042

	return 0;
}
machine_device_initcall(pseries, pseries_memory_hotplug_init);