hotplug-memory.c 20.5 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 353
{
	if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
		return false;

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

366
static int dlpar_add_lmb(struct drmem_lmb *);
367

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

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

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

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

	block_sz = pseries_memory_block_size();

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

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

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

398 399 400
	return 0;
}

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

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

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

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

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

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

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

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

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

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

455
			drmem_remove_lmb_reservation(lmb);
456 457 458 459
		}

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

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

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

	return rc;
}

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

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

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

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

	if (!lmb_found)
		rc = -EINVAL;

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

	return rc;
}

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

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

	if (lmbs_to_remove == 0)
		return -EINVAL;

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

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

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_remove)
		return -EINVAL;

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

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

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

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

549 550 551

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

554
			rc = dlpar_add_lmb(lmb);
555 556
			if (rc)
				pr_err("Failed to add LMB, drc index %x\n",
557
				       lmb->drc_index);
558

559
			drmem_remove_lmb_reservation(lmb);
560 561 562
		}
		rc = -EINVAL;
	} else {
563 564
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
565 566
				continue;

567
			dlpar_release_drc(lmb->drc_index);
568
			pr_info("Memory at %llx (drc index %x) was hot-removed\n",
569
				lmb->base_addr, lmb->drc_index);
570

571
			drmem_remove_lmb_reservation(lmb);
572 573 574 575 576 577
		}
	}

	return rc;
}

578 579
#else
static inline int pseries_remove_memblock(unsigned long base,
580
					  unsigned long memblock_size)
581 582 583
{
	return -EOPNOTSUPP;
}
584
static inline int pseries_remove_mem_node(struct device_node *np)
585
{
586
	return 0;
587
}
588 589 590 591
static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
{
	return -EOPNOTSUPP;
}
592
static int dlpar_remove_lmb(struct drmem_lmb *lmb)
593 594 595
{
	return -EOPNOTSUPP;
}
596
static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
597 598 599
{
	return -EOPNOTSUPP;
}
600
static int dlpar_memory_remove_by_index(u32 drc_index)
601 602 603
{
	return -EOPNOTSUPP;
}
604

605
static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
606 607 608
{
	return -EOPNOTSUPP;
}
609
#endif /* CONFIG_MEMORY_HOTREMOVE */
610

611
static int dlpar_add_lmb(struct drmem_lmb *lmb)
612 613
{
	unsigned long block_sz;
614
	int nid, rc;
615

616 617 618
	if (lmb->flags & DRCONF_MEM_ASSIGNED)
		return -EINVAL;

619
	rc = update_lmb_associativity_index(lmb);
620 621 622 623 624
	if (rc) {
		dlpar_release_drc(lmb->drc_index);
		return rc;
	}

625 626
	block_sz = memory_block_size_bytes();

627 628 629 630
	/* 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;
631

632
	/* Add the memory */
633
	rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
634
	if (rc) {
635
		invalidate_lmb_associativity_index(lmb);
636 637 638 639 640
		return rc;
	}

	rc = dlpar_online_lmb(lmb);
	if (rc) {
641
		__remove_memory(nid, lmb->base_addr, block_sz);
642
		invalidate_lmb_associativity_index(lmb);
643
	} else {
644
		lmb->flags |= DRCONF_MEM_ASSIGNED;
645
	}
646 647 648 649

	return rc;
}

650
static int dlpar_memory_add_by_count(u32 lmbs_to_add)
651
{
652
	struct drmem_lmb *lmb;
653 654
	int lmbs_available = 0;
	int lmbs_added = 0;
655
	int rc;
656 657 658 659 660 661 662

	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 */
663 664
	for_each_drmem_lmb(lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
665
			lmbs_available++;
666 667 668

		if (lmbs_available == lmbs_to_add)
			break;
669 670 671 672 673
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

674 675
	for_each_drmem_lmb(lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
676 677
			continue;

678
		rc = dlpar_acquire_drc(lmb->drc_index);
679 680 681
		if (rc)
			continue;

682
		rc = dlpar_add_lmb(lmb);
683
		if (rc) {
684
			dlpar_release_drc(lmb->drc_index);
685 686 687
			continue;
		}

688 689 690
		/* Mark this lmb so we can remove it later if all of the
		 * requested LMBs cannot be added.
		 */
691 692 693 694 695
		drmem_mark_lmb_reserved(lmb);

		lmbs_added++;
		if (lmbs_added == lmbs_to_add)
			break;
696 697 698
	}

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

701 702
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
703 704
				continue;

705
			rc = dlpar_remove_lmb(lmb);
706 707
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
708
				       lmb->drc_index);
709
			else
710 711 712
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
713
		}
714 715
		rc = -EINVAL;
	} else {
716 717
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
718 719 720
				continue;

			pr_info("Memory at %llx (drc index %x) was hot-added\n",
721 722
				lmb->base_addr, lmb->drc_index);
			drmem_remove_lmb_reservation(lmb);
723
		}
724
		rc = 0;
725 726 727 728 729
	}

	return rc;
}

730
static int dlpar_memory_add_by_index(u32 drc_index)
731
{
732 733
	struct drmem_lmb *lmb;
	int rc, lmb_found;
734 735 736 737

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

	lmb_found = 0;
738 739
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
740
			lmb_found = 1;
741
			rc = dlpar_acquire_drc(lmb->drc_index);
742
			if (!rc) {
743
				rc = dlpar_add_lmb(lmb);
744
				if (rc)
745
					dlpar_release_drc(lmb->drc_index);
746 747
			}

748 749 750 751 752 753 754 755 756 757 758
			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",
759
			lmb->base_addr, drc_index);
760 761 762 763

	return rc;
}

764
static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
765
{
766 767 768
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
769 770 771 772 773 774 775

	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;

776 777
	rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
	if (rc)
778 779 780
		return -EINVAL;

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

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

791 792
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
793 794
			continue;

795
		rc = dlpar_acquire_drc(lmb->drc_index);
796 797 798
		if (rc)
			break;

799
		rc = dlpar_add_lmb(lmb);
800
		if (rc) {
801
			dlpar_release_drc(lmb->drc_index);
802 803 804
			break;
		}

805
		drmem_mark_lmb_reserved(lmb);
806 807 808 809 810
	}

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

811 812
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
813 814
				continue;

815
			rc = dlpar_remove_lmb(lmb);
816 817
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
818
				       lmb->drc_index);
819
			else
820 821 822
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
823 824 825
		}
		rc = -EINVAL;
	} else {
826 827
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
828 829 830
				continue;

			pr_info("Memory at %llx (drc index %x) was hot-added\n",
831 832
				lmb->base_addr, lmb->drc_index);
			drmem_remove_lmb_reservation(lmb);
833 834 835 836 837 838
		}
	}

	return rc;
}

839 840
int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
{
841 842 843
	u32 count, drc_index;
	int rc;

844 845 846
	lock_device_hotplug();

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

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

888
		break;
889 890 891 892 893 894
	default:
		pr_err("Invalid action (%d) specified\n", hp_elog->action);
		rc = -EINVAL;
		break;
	}

895
	if (!rc)
896 897
		rc = drmem_update_dt();

898 899 900 901
	unlock_device_hotplug();
	return rc;
}

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

	/*
	 * Check to see if we are actually adding memory
	 */
913
	if (!of_node_is_type(np, "memory"))
914 915 916
		return 0;

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

923 924 925 926 927 928 929 930
	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);
931 932 933 934

	/*
	 * Update memory region to represent the memory add
	 */
935
	ret = memblock_add(base, lmb_size);
936 937 938
	return (ret < 0) ? -EINVAL : 0;
}

939
static int pseries_memory_notifier(struct notifier_block *nb,
940
				   unsigned long action, void *data)
941
{
942
	struct of_reconfig_data *rd = data;
943
	int err = 0;
944 945

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

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))
963
		of_reconfig_notifier_register(&pseries_mem_nb);
964 965 966 967

	return 0;
}
machine_device_initcall(pseries, pseries_memory_hotplug_init);