hotplug-memory.c 20.7 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
	int lmbs_reserved = 0;
406
	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
		drmem_mark_lmb_reserved(lmb);

439 440
		lmbs_reserved++;
		if (lmbs_reserved == lmbs_to_remove)
441
			break;
442 443
	}

444
	if (lmbs_reserved != lmbs_to_remove) {
445 446
		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

			lmbs_reserved--;
			if (lmbs_reserved == 0)
				break;
461 462 463 464
		}

		rc = -EINVAL;
	} else {
465 466
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
467 468
				continue;

469
			dlpar_release_drc(lmb->drc_index);
470
			pr_info("Memory at %llx was hot-removed\n",
471
				lmb->base_addr);
472

473
			drmem_remove_lmb_reservation(lmb);
474 475 476 477

			lmbs_reserved--;
			if (lmbs_reserved == 0)
				break;
478 479 480 481 482 483 484
		}
		rc = 0;
	}

	return rc;
}

485
static int dlpar_memory_remove_by_index(u32 drc_index)
486
{
487
	struct drmem_lmb *lmb;
488
	int lmb_found;
489
	int rc;
490

491
	pr_debug("Attempting to hot-remove LMB, drc index %x\n", drc_index);
492 493

	lmb_found = 0;
494 495
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
496
			lmb_found = 1;
497
			rc = dlpar_remove_lmb(lmb);
498
			if (!rc)
499
				dlpar_release_drc(lmb->drc_index);
500

501 502 503 504 505 506 507 508
			break;
		}
	}

	if (!lmb_found)
		rc = -EINVAL;

	if (rc)
509 510
		pr_debug("Failed to hot-remove memory at %llx\n",
			 lmb->base_addr);
511
	else
512
		pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
513 514 515 516

	return rc;
}

517
static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
518
{
519 520 521
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
522 523 524 525 526 527 528

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

	if (lmbs_to_remove == 0)
		return -EINVAL;

529 530
	rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
	if (rc)
531 532 533
		return -EINVAL;

	/* Validate that there are enough LMBs to satisfy the request */
534 535
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_RESERVED)
536 537 538 539 540 541 542 543
			break;

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_remove)
		return -EINVAL;

544 545
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
546 547
			continue;

548
		rc = dlpar_remove_lmb(lmb);
549 550 551
		if (rc)
			break;

552
		drmem_mark_lmb_reserved(lmb);
553 554 555 556 557
	}

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

558 559 560

		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
561 562
				continue;

563 564 565 566 567 568 569
			/*
			 * 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);

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
#else
static inline int pseries_remove_memblock(unsigned long base,
596
					  unsigned long memblock_size)
597 598 599
{
	return -EOPNOTSUPP;
}
600
static inline int pseries_remove_mem_node(struct device_node *np)
601
{
602
	return 0;
603
}
604
static int dlpar_remove_lmb(struct drmem_lmb *lmb)
605 606 607
{
	return -EOPNOTSUPP;
}
608
static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
609 610 611
{
	return -EOPNOTSUPP;
}
612
static int dlpar_memory_remove_by_index(u32 drc_index)
613 614 615
{
	return -EOPNOTSUPP;
}
616

617
static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
618 619 620
{
	return -EOPNOTSUPP;
}
621
#endif /* CONFIG_MEMORY_HOTREMOVE */
622

623
static int dlpar_add_lmb(struct drmem_lmb *lmb)
624 625
{
	unsigned long block_sz;
626
	int nid, rc;
627

628 629 630
	if (lmb->flags & DRCONF_MEM_ASSIGNED)
		return -EINVAL;

631
	rc = update_lmb_associativity_index(lmb);
632 633 634 635 636
	if (rc) {
		dlpar_release_drc(lmb->drc_index);
		return rc;
	}

637 638
	block_sz = memory_block_size_bytes();

639 640 641 642
	/* 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;
643

644
	/* Add the memory */
645
	rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
646
	if (rc) {
647
		invalidate_lmb_associativity_index(lmb);
648 649 650 651 652
		return rc;
	}

	rc = dlpar_online_lmb(lmb);
	if (rc) {
653
		__remove_memory(nid, lmb->base_addr, block_sz);
654
		invalidate_lmb_associativity_index(lmb);
655
	} else {
656
		lmb->flags |= DRCONF_MEM_ASSIGNED;
657
	}
658 659 660 661

	return rc;
}

662
static int dlpar_memory_add_by_count(u32 lmbs_to_add)
663
{
664
	struct drmem_lmb *lmb;
665 666
	int lmbs_available = 0;
	int lmbs_added = 0;
667
	int rc;
668 669 670 671 672 673 674

	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 */
675 676
	for_each_drmem_lmb(lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
677
			lmbs_available++;
678 679 680

		if (lmbs_available == lmbs_to_add)
			break;
681 682 683 684 685
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

686 687
	for_each_drmem_lmb(lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
688 689
			continue;

690
		rc = dlpar_acquire_drc(lmb->drc_index);
691 692 693
		if (rc)
			continue;

694
		rc = dlpar_add_lmb(lmb);
695
		if (rc) {
696
			dlpar_release_drc(lmb->drc_index);
697 698 699
			continue;
		}

700 701 702
		/* Mark this lmb so we can remove it later if all of the
		 * requested LMBs cannot be added.
		 */
703 704 705 706 707
		drmem_mark_lmb_reserved(lmb);

		lmbs_added++;
		if (lmbs_added == lmbs_to_add)
			break;
708 709 710
	}

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

713 714
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
715 716
				continue;

717
			rc = dlpar_remove_lmb(lmb);
718 719
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
720
				       lmb->drc_index);
721
			else
722 723 724
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
725
		}
726 727
		rc = -EINVAL;
	} else {
728 729
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
730 731
				continue;

732 733
			pr_debug("Memory at %llx (drc index %x) was hot-added\n",
				 lmb->base_addr, lmb->drc_index);
734
			drmem_remove_lmb_reservation(lmb);
735
		}
736
		rc = 0;
737 738 739 740 741
	}

	return rc;
}

742
static int dlpar_memory_add_by_index(u32 drc_index)
743
{
744 745
	struct drmem_lmb *lmb;
	int rc, lmb_found;
746 747 748 749

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

	lmb_found = 0;
750 751
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
752
			lmb_found = 1;
753
			rc = dlpar_acquire_drc(lmb->drc_index);
754
			if (!rc) {
755
				rc = dlpar_add_lmb(lmb);
756
				if (rc)
757
					dlpar_release_drc(lmb->drc_index);
758 759
			}

760 761 762 763 764 765 766 767 768 769 770
			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",
771
			lmb->base_addr, drc_index);
772 773 774 775

	return rc;
}

776
static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
777
{
778 779 780
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
781 782 783 784 785 786 787

	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;

788 789
	rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
	if (rc)
790 791 792
		return -EINVAL;

	/* Validate that the LMBs in this range are not reserved */
793 794
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_RESERVED)
795 796 797 798 799 800 801 802
			break;

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

803 804
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
805 806
			continue;

807
		rc = dlpar_acquire_drc(lmb->drc_index);
808 809 810
		if (rc)
			break;

811
		rc = dlpar_add_lmb(lmb);
812
		if (rc) {
813
			dlpar_release_drc(lmb->drc_index);
814 815 816
			break;
		}

817
		drmem_mark_lmb_reserved(lmb);
818 819 820 821 822
	}

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

823 824
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
825 826
				continue;

827
			rc = dlpar_remove_lmb(lmb);
828 829
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
830
				       lmb->drc_index);
831
			else
832 833 834
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
835 836 837
		}
		rc = -EINVAL;
	} else {
838 839
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
840 841 842
				continue;

			pr_info("Memory at %llx (drc index %x) was hot-added\n",
843 844
				lmb->base_addr, lmb->drc_index);
			drmem_remove_lmb_reservation(lmb);
845 846 847 848 849 850
		}
	}

	return rc;
}

851 852
int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
{
853 854 855
	u32 count, drc_index;
	int rc;

856 857 858
	lock_device_hotplug();

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

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

900
		break;
901 902 903 904 905 906
	default:
		pr_err("Invalid action (%d) specified\n", hp_elog->action);
		rc = -EINVAL;
		break;
	}

907
	if (!rc)
908 909
		rc = drmem_update_dt();

910 911 912 913
	unlock_device_hotplug();
	return rc;
}

914
static int pseries_add_mem_node(struct device_node *np)
915
{
916
	const __be32 *prop;
917
	unsigned long base;
918
	unsigned long lmb_size;
919
	int ret = -EINVAL;
920
	int addr_cells, size_cells;
921 922 923 924

	/*
	 * Check to see if we are actually adding memory
	 */
925
	if (!of_node_is_type(np, "memory"))
926 927 928
		return 0;

	/*
Y
Yinghai Lu 已提交
929
	 * Find the base and size of the memblock
930
	 */
931 932
	prop = of_get_property(np, "reg", NULL);
	if (!prop)
933 934
		return ret;

935 936 937 938 939 940 941 942
	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);
943 944 945 946

	/*
	 * Update memory region to represent the memory add
	 */
947
	ret = memblock_add(base, lmb_size);
948 949 950
	return (ret < 0) ? -EINVAL : 0;
}

951
static int pseries_memory_notifier(struct notifier_block *nb,
952
				   unsigned long action, void *data)
953
{
954
	struct of_reconfig_data *rd = data;
955
	int err = 0;
956 957

	switch (action) {
958
	case OF_RECONFIG_ATTACH_NODE:
959
		err = pseries_add_mem_node(rd->dn);
960
		break;
961
	case OF_RECONFIG_DETACH_NODE:
962
		err = pseries_remove_mem_node(rd->dn);
963 964
		break;
	}
965
	return notifier_from_errno(err);
966 967 968 969 970 971 972 973 974
}

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))
975
		of_reconfig_notifier_register(&pseries_mem_nb);
976 977 978 979

	return 0;
}
machine_device_initcall(pseries, pseries_memory_hotplug_init);