hotplug-memory.c 20.6 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
unsigned long pseries_memory_block_size(void)
26 27
{
	struct device_node *np;
28
	u64 memblock_size = MIN_MEMORY_BLOCK_SIZE;
29
	struct resource r;
30 31 32

	np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
	if (np) {
33 34 35
		int len;
		int size_cells;
		const __be32 *prop;
36

37 38 39 40 41
		size_cells = of_n_size_cells(np);

		prop = of_get_property(np, "ibm,lmb-size", &len);
		if (prop && len >= size_cells * sizeof(__be32))
			memblock_size = of_read_number(prop, size_cells);
42
		of_node_put(np);
43

44 45
	} else  if (machine_is(pseries)) {
		/* This fallback really only applies to pseries */
46 47 48 49
		unsigned int memzero_size = 0;

		np = of_find_node_by_path("/memory@0");
		if (np) {
50 51
			if (!of_address_to_resource(np, 0, &r))
				memzero_size = resource_size(&r);
52 53 54 55 56 57 58 59 60 61 62 63
			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) {
64 65
				if (!of_address_to_resource(np, 0, &r))
					memblock_size = resource_size(&r);
66 67 68 69 70 71 72
				of_node_put(np);
			}
		}
	}
	return memblock_size;
}

73
static void dlpar_free_property(struct property *prop)
74 75 76 77 78 79
{
	kfree(prop->name);
	kfree(prop->value);
	kfree(prop);
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
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;
}

103 104 105
static bool find_aa_index(struct device_node *dr_node,
			 struct property *ala_prop,
			 const u32 *lmb_assoc, u32 *aa_index)
106
{
107 108
	u32 *assoc_arrays, new_prop_size;
	struct property *new_prop;
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
	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;

130 131
		*aa_index = i;
		return true;
132 133
	}

134 135 136 137
	new_prop_size = ala_prop->length + aa_array_sz;
	new_prop = dlpar_clone_property(ala_prop, new_prop_size);
	if (!new_prop)
		return false;
138

139
	assoc_arrays = new_prop->value;
140

141 142
	/* increment the number of entries in the lookup array */
	assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
143

144 145 146
	/* 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);
147

148
	of_update_property(dr_node, new_prop);
149

150 151 152 153 154 155 156
	/*
	 * 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;
157 158
}

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

	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;
	}

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

197
	found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
198

199
	of_node_put(dr_node);
200 201
	dlpar_free_cc_nodes(lmb_node);

202
	if (!found) {
203 204
		pr_err("Could not find LMB associativity\n");
		return -1;
205 206 207
	}

	lmb->aa_index = aa_index;
208
	return 0;
209 210
}

211
static struct memory_block *lmb_to_memblock(struct drmem_lmb *lmb)
212 213 214 215 216 217 218 219 220 221 222 223
{
	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;
}

224 225 226 227 228
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;
229
	struct drmem_lmb *limit;
230 231 232 233 234 235 236 237 238 239 240 241

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

	if (!start)
		return -EINVAL;

242
	end = &start[n_lmbs];
243

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

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

static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
{
	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;
}

274
static int dlpar_online_lmb(struct drmem_lmb *lmb)
275 276 277 278
{
	return dlpar_change_lmb_state(lmb, true);
}

279
#ifdef CONFIG_MEMORY_HOTREMOVE
280
static int dlpar_offline_lmb(struct drmem_lmb *lmb)
281 282 283 284
{
	return dlpar_change_lmb_state(lmb, false);
}

285
static int pseries_remove_memblock(unsigned long base, unsigned long memblock_size)
286 287 288 289
{
	unsigned long block_sz, start_pfn;
	int sections_per_block;
	int i, nid;
290

291
	start_pfn = base >> PAGE_SHIFT;
292

293 294 295 296
	lock_device_hotplug();

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

298
	block_sz = pseries_memory_block_size();
299 300
	sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
	nid = memory_add_physaddr_to_nid(base);
301

302
	for (i = 0; i < sections_per_block; i++) {
303
		__remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
304
		base += MIN_MEMORY_BLOCK_SIZE;
305
	}
306

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

314
static int pseries_remove_mem_node(struct device_node *np)
315
{
316
	const __be32 *prop;
317
	unsigned long base;
318
	unsigned long lmb_size;
319
	int ret = -EINVAL;
320
	int addr_cells, size_cells;
321 322 323 324

	/*
	 * Check to see if we are actually removing memory
	 */
325
	if (!of_node_is_type(np, "memory"))
326 327 328
		return 0;

	/*
329
	 * Find the base address and size of the memblock
330
	 */
331 332
	prop = of_get_property(np, "reg", NULL);
	if (!prop)
333 334
		return ret;

335 336 337 338 339 340 341 342 343
	addr_cells = of_n_addr_cells(np);
	size_cells = of_n_size_cells(np);

	/*
	 * "reg" property represents (addr,size) tuple.
	 */
	base = of_read_number(prop, addr_cells);
	prop += addr_cells;
	lmb_size = of_read_number(prop, size_cells);
344

345 346
	pseries_remove_memblock(base, lmb_size);
	return 0;
347
}
348

349
static bool lmb_is_removable(struct drmem_lmb *lmb)
350
{
351 352
	if ((lmb->flags & DRCONF_MEM_RESERVED) ||
		!(lmb->flags & DRCONF_MEM_ASSIGNED))
353 354
		return false;

355
#ifdef CONFIG_FA_DUMP
356 357 358 359
	/*
	 * Don't hot-remove memory that falls in fadump boot memory area
	 * and memory that is reserved for capturing old kernel memory.
	 */
360
	if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
361 362
		return false;
#endif
363 364
	/* device_offline() will determine if we can actually remove this lmb */
	return true;
365 366
}

367
static int dlpar_add_lmb(struct drmem_lmb *);
368

369
static int dlpar_remove_lmb(struct drmem_lmb *lmb)
370
{
371
	struct memory_block *mem_block;
372
	unsigned long block_sz;
373
	int rc;
374 375 376 377

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

378 379 380 381
	mem_block = lmb_to_memblock(lmb);
	if (mem_block == NULL)
		return -EINVAL;

382
	rc = dlpar_offline_lmb(lmb);
383 384
	if (rc) {
		put_device(&mem_block->dev);
385
		return rc;
386
	}
387 388 389

	block_sz = pseries_memory_block_size();

390 391
	__remove_memory(mem_block->nid, lmb->base_addr, block_sz);
	put_device(&mem_block->dev);
392 393 394 395

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

396 397 398
	invalidate_lmb_associativity_index(lmb);
	lmb->flags &= ~DRCONF_MEM_ASSIGNED;

399 400 401
	return 0;
}

402
static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
403
{
404
	struct drmem_lmb *lmb;
405 406
	int lmbs_removed = 0;
	int lmbs_available = 0;
407
	int rc;
408 409 410 411 412 413 414

	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 */
415 416
	for_each_drmem_lmb(lmb) {
		if (lmb_is_removable(lmb))
417
			lmbs_available++;
418 419 420

		if (lmbs_available == lmbs_to_remove)
			break;
421 422
	}

423 424 425
	if (lmbs_available < lmbs_to_remove) {
		pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
			lmbs_available, lmbs_to_remove);
426
		return -EINVAL;
427
	}
428

429 430
	for_each_drmem_lmb(lmb) {
		rc = dlpar_remove_lmb(lmb);
431 432 433 434 435 436
		if (rc)
			continue;

		/* Mark this lmb so we can add it later if all of the
		 * requested LMBs cannot be removed.
		 */
437 438 439 440 441
		drmem_mark_lmb_reserved(lmb);

		lmbs_removed++;
		if (lmbs_removed == lmbs_to_remove)
			break;
442 443 444 445 446
	}

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

447 448
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
449 450
				continue;

451
			rc = dlpar_add_lmb(lmb);
452 453
			if (rc)
				pr_err("Failed to add LMB back, drc index %x\n",
454
				       lmb->drc_index);
455

456
			drmem_remove_lmb_reservation(lmb);
457 458 459 460
		}

		rc = -EINVAL;
	} else {
461 462
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
463 464
				continue;

465
			dlpar_release_drc(lmb->drc_index);
466
			pr_info("Memory at %llx was hot-removed\n",
467
				lmb->base_addr);
468

469
			drmem_remove_lmb_reservation(lmb);
470 471 472 473 474 475 476
		}
		rc = 0;
	}

	return rc;
}

477
static int dlpar_memory_remove_by_index(u32 drc_index)
478
{
479
	struct drmem_lmb *lmb;
480
	int lmb_found;
481
	int rc;
482

483
	pr_debug("Attempting to hot-remove LMB, drc index %x\n", drc_index);
484 485

	lmb_found = 0;
486 487
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
488
			lmb_found = 1;
489
			rc = dlpar_remove_lmb(lmb);
490
			if (!rc)
491
				dlpar_release_drc(lmb->drc_index);
492

493 494 495 496 497 498 499 500
			break;
		}
	}

	if (!lmb_found)
		rc = -EINVAL;

	if (rc)
501 502
		pr_debug("Failed to hot-remove memory at %llx\n",
			 lmb->base_addr);
503
	else
504
		pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
505 506 507 508

	return rc;
}

509
static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
510
{
511 512 513
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
514 515 516 517 518 519 520

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

	if (lmbs_to_remove == 0)
		return -EINVAL;

521 522
	rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
	if (rc)
523 524 525
		return -EINVAL;

	/* Validate that there are enough LMBs to satisfy the request */
526 527
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_RESERVED)
528 529 530 531 532 533 534 535
			break;

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_remove)
		return -EINVAL;

536 537
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
538 539
			continue;

540
		rc = dlpar_remove_lmb(lmb);
541 542 543
		if (rc)
			break;

544
		drmem_mark_lmb_reserved(lmb);
545 546 547 548 549
	}

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

550 551 552

		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
553 554
				continue;

555 556 557 558 559 560 561
			/*
			 * Setting the isolation state of an UNISOLATED/CONFIGURED
			 * device to UNISOLATE is a no-op, but the hypervisor can
			 * use it as a hint that the LMB removal failed.
			 */
			dlpar_unisolate_drc(lmb->drc_index);

562
			rc = dlpar_add_lmb(lmb);
563 564
			if (rc)
				pr_err("Failed to add LMB, drc index %x\n",
565
				       lmb->drc_index);
566

567
			drmem_remove_lmb_reservation(lmb);
568 569 570
		}
		rc = -EINVAL;
	} else {
571 572
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
573 574
				continue;

575
			dlpar_release_drc(lmb->drc_index);
576
			pr_info("Memory at %llx (drc index %x) was hot-removed\n",
577
				lmb->base_addr, lmb->drc_index);
578

579
			drmem_remove_lmb_reservation(lmb);
580 581 582 583 584 585
		}
	}

	return rc;
}

586 587
#else
static inline int pseries_remove_memblock(unsigned long base,
588
					  unsigned long memblock_size)
589 590 591
{
	return -EOPNOTSUPP;
}
592
static inline int pseries_remove_mem_node(struct device_node *np)
593
{
594
	return 0;
595
}
596
static int dlpar_remove_lmb(struct drmem_lmb *lmb)
597 598 599
{
	return -EOPNOTSUPP;
}
600
static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
601 602 603
{
	return -EOPNOTSUPP;
}
604
static int dlpar_memory_remove_by_index(u32 drc_index)
605 606 607
{
	return -EOPNOTSUPP;
}
608

609
static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
610 611 612
{
	return -EOPNOTSUPP;
}
613
#endif /* CONFIG_MEMORY_HOTREMOVE */
614

615
static int dlpar_add_lmb(struct drmem_lmb *lmb)
616 617
{
	unsigned long block_sz;
618
	int nid, rc;
619

620 621 622
	if (lmb->flags & DRCONF_MEM_ASSIGNED)
		return -EINVAL;

623
	rc = update_lmb_associativity_index(lmb);
624 625 626 627 628
	if (rc) {
		dlpar_release_drc(lmb->drc_index);
		return rc;
	}

629 630
	block_sz = memory_block_size_bytes();

631 632 633 634
	/* Find the node id for this LMB.  Fake one if necessary. */
	nid = of_drconf_to_nid_single(lmb);
	if (nid < 0 || !node_possible(nid))
		nid = first_online_node;
635

636
	/* Add the memory */
637
	rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
638
	if (rc) {
639
		invalidate_lmb_associativity_index(lmb);
640 641 642 643 644
		return rc;
	}

	rc = dlpar_online_lmb(lmb);
	if (rc) {
645
		__remove_memory(nid, lmb->base_addr, block_sz);
646
		invalidate_lmb_associativity_index(lmb);
647
	} else {
648
		lmb->flags |= DRCONF_MEM_ASSIGNED;
649
	}
650 651 652 653

	return rc;
}

654
static int dlpar_memory_add_by_count(u32 lmbs_to_add)
655
{
656
	struct drmem_lmb *lmb;
657 658
	int lmbs_available = 0;
	int lmbs_added = 0;
659
	int rc;
660 661 662 663 664 665 666

	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 */
667 668
	for_each_drmem_lmb(lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
669
			lmbs_available++;
670 671 672

		if (lmbs_available == lmbs_to_add)
			break;
673 674 675 676 677
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

678 679
	for_each_drmem_lmb(lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
680 681
			continue;

682
		rc = dlpar_acquire_drc(lmb->drc_index);
683 684 685
		if (rc)
			continue;

686
		rc = dlpar_add_lmb(lmb);
687
		if (rc) {
688
			dlpar_release_drc(lmb->drc_index);
689 690 691
			continue;
		}

692 693 694
		/* Mark this lmb so we can remove it later if all of the
		 * requested LMBs cannot be added.
		 */
695 696 697 698 699
		drmem_mark_lmb_reserved(lmb);

		lmbs_added++;
		if (lmbs_added == lmbs_to_add)
			break;
700 701 702
	}

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

705 706
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
707 708
				continue;

709
			rc = dlpar_remove_lmb(lmb);
710 711
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
712
				       lmb->drc_index);
713
			else
714 715 716
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
717
		}
718 719
		rc = -EINVAL;
	} else {
720 721
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
722 723
				continue;

724 725
			pr_debug("Memory at %llx (drc index %x) was hot-added\n",
				 lmb->base_addr, lmb->drc_index);
726
			drmem_remove_lmb_reservation(lmb);
727
		}
728
		rc = 0;
729 730 731 732 733
	}

	return rc;
}

734
static int dlpar_memory_add_by_index(u32 drc_index)
735
{
736 737
	struct drmem_lmb *lmb;
	int rc, lmb_found;
738 739 740 741

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

	lmb_found = 0;
742 743
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
744
			lmb_found = 1;
745
			rc = dlpar_acquire_drc(lmb->drc_index);
746
			if (!rc) {
747
				rc = dlpar_add_lmb(lmb);
748
				if (rc)
749
					dlpar_release_drc(lmb->drc_index);
750 751
			}

752 753 754 755 756 757 758 759 760 761 762
			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",
763
			lmb->base_addr, drc_index);
764 765 766 767

	return rc;
}

768
static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
769
{
770 771 772
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
773 774 775 776 777 778 779

	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;

780 781
	rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
	if (rc)
782 783 784
		return -EINVAL;

	/* Validate that the LMBs in this range are not reserved */
785 786
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_RESERVED)
787 788 789 790 791 792 793 794
			break;

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

795 796
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
797 798
			continue;

799
		rc = dlpar_acquire_drc(lmb->drc_index);
800 801 802
		if (rc)
			break;

803
		rc = dlpar_add_lmb(lmb);
804
		if (rc) {
805
			dlpar_release_drc(lmb->drc_index);
806 807 808
			break;
		}

809
		drmem_mark_lmb_reserved(lmb);
810 811 812 813 814
	}

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

815 816
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
817 818
				continue;

819
			rc = dlpar_remove_lmb(lmb);
820 821
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
822
				       lmb->drc_index);
823
			else
824 825 826
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
827 828 829
		}
		rc = -EINVAL;
	} else {
830 831
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
832 833 834
				continue;

			pr_info("Memory at %llx (drc index %x) was hot-added\n",
835 836
				lmb->base_addr, lmb->drc_index);
			drmem_remove_lmb_reservation(lmb);
837 838 839 840 841 842
		}
	}

	return rc;
}

843 844
int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
{
845 846 847
	u32 count, drc_index;
	int rc;

848 849 850
	lock_device_hotplug();

	switch (hp_elog->action) {
851
	case PSERIES_HP_ELOG_ACTION_ADD:
852 853
		switch (hp_elog->id_type) {
		case PSERIES_HP_ELOG_ID_DRC_COUNT:
854
			count = hp_elog->_drc_u.drc_count;
855
			rc = dlpar_memory_add_by_count(count);
856 857
			break;
		case PSERIES_HP_ELOG_ID_DRC_INDEX:
858
			drc_index = hp_elog->_drc_u.drc_index;
859
			rc = dlpar_memory_add_by_index(drc_index);
860 861
			break;
		case PSERIES_HP_ELOG_ID_DRC_IC:
862 863
			count = hp_elog->_drc_u.ic.count;
			drc_index = hp_elog->_drc_u.ic.index;
864
			rc = dlpar_memory_add_by_ic(count, drc_index);
865 866
			break;
		default:
867
			rc = -EINVAL;
868
			break;
869 870
		}

871
		break;
872
	case PSERIES_HP_ELOG_ACTION_REMOVE:
873 874
		switch (hp_elog->id_type) {
		case PSERIES_HP_ELOG_ID_DRC_COUNT:
875
			count = hp_elog->_drc_u.drc_count;
876
			rc = dlpar_memory_remove_by_count(count);
877 878
			break;
		case PSERIES_HP_ELOG_ID_DRC_INDEX:
879
			drc_index = hp_elog->_drc_u.drc_index;
880
			rc = dlpar_memory_remove_by_index(drc_index);
881 882
			break;
		case PSERIES_HP_ELOG_ID_DRC_IC:
883 884
			count = hp_elog->_drc_u.ic.count;
			drc_index = hp_elog->_drc_u.ic.index;
885
			rc = dlpar_memory_remove_by_ic(count, drc_index);
886 887
			break;
		default:
888
			rc = -EINVAL;
889
			break;
890 891
		}

892
		break;
893 894 895 896 897 898
	default:
		pr_err("Invalid action (%d) specified\n", hp_elog->action);
		rc = -EINVAL;
		break;
	}

899
	if (!rc)
900 901
		rc = drmem_update_dt();

902 903 904 905
	unlock_device_hotplug();
	return rc;
}

906
static int pseries_add_mem_node(struct device_node *np)
907
{
908
	const __be32 *prop;
909
	unsigned long base;
910
	unsigned long lmb_size;
911
	int ret = -EINVAL;
912
	int addr_cells, size_cells;
913 914 915 916

	/*
	 * Check to see if we are actually adding memory
	 */
917
	if (!of_node_is_type(np, "memory"))
918 919 920
		return 0;

	/*
Y
Yinghai Lu 已提交
921
	 * Find the base and size of the memblock
922
	 */
923 924
	prop = of_get_property(np, "reg", NULL);
	if (!prop)
925 926
		return ret;

927 928 929 930 931 932 933 934
	addr_cells = of_n_addr_cells(np);
	size_cells = of_n_size_cells(np);
	/*
	 * "reg" property represents (addr,size) tuple.
	 */
	base = of_read_number(prop, addr_cells);
	prop += addr_cells;
	lmb_size = of_read_number(prop, size_cells);
935 936 937 938

	/*
	 * Update memory region to represent the memory add
	 */
939
	ret = memblock_add(base, lmb_size);
940 941 942
	return (ret < 0) ? -EINVAL : 0;
}

943
static int pseries_memory_notifier(struct notifier_block *nb,
944
				   unsigned long action, void *data)
945
{
946
	struct of_reconfig_data *rd = data;
947
	int err = 0;
948 949

	switch (action) {
950
	case OF_RECONFIG_ATTACH_NODE:
951
		err = pseries_add_mem_node(rd->dn);
952
		break;
953
	case OF_RECONFIG_DETACH_NODE:
954
		err = pseries_remove_mem_node(rd->dn);
955 956
		break;
	}
957
	return notifier_from_errno(err);
958 959 960 961 962 963 964 965 966
}

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))
967
		of_reconfig_notifier_register(&pseries_mem_nb);
968 969 970 971

	return 0;
}
machine_device_initcall(pseries, pseries_memory_hotplug_init);