hotplug-memory.c 21.3 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_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
491
{
492 493 494
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
495 496 497 498 499 500 501

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

	if (lmbs_to_remove == 0)
		return -EINVAL;

502 503
	rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
	if (rc)
504 505 506
		return -EINVAL;

	/* Validate that there are enough LMBs to satisfy the request */
507 508
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_RESERVED)
509 510 511 512 513 514 515 516
			break;

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_remove)
		return -EINVAL;

517 518
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
519 520
			continue;

521
		rc = dlpar_remove_lmb(lmb);
522 523 524
		if (rc)
			break;

525
		drmem_mark_lmb_reserved(lmb);
526 527 528 529 530
	}

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

531 532 533

		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
534 535
				continue;

536
			rc = dlpar_add_lmb(lmb);
537 538
			if (rc)
				pr_err("Failed to add LMB, drc index %x\n",
539
				       lmb->drc_index);
540

541
			drmem_remove_lmb_reservation(lmb);
542 543 544
		}
		rc = -EINVAL;
	} else {
545 546
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
547 548
				continue;

549
			dlpar_release_drc(lmb->drc_index);
550
			pr_info("Memory at %llx (drc index %x) was hot-removed\n",
551
				lmb->base_addr, lmb->drc_index);
552

553
			drmem_remove_lmb_reservation(lmb);
554 555 556 557 558 559
		}
	}

	return rc;
}

560 561 562 563 564 565
#else
static inline int pseries_remove_memblock(unsigned long base,
					  unsigned int memblock_size)
{
	return -EOPNOTSUPP;
}
566
static inline int pseries_remove_mem_node(struct device_node *np)
567
{
568
	return 0;
569
}
570 571 572 573
static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
{
	return -EOPNOTSUPP;
}
574
static int dlpar_remove_lmb(struct drmem_lmb *lmb)
575 576 577
{
	return -EOPNOTSUPP;
}
578
static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
579 580 581
{
	return -EOPNOTSUPP;
}
582
static int dlpar_memory_remove_by_index(u32 drc_index)
583 584 585
{
	return -EOPNOTSUPP;
}
586

587
static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
588 589 590
{
	return -EOPNOTSUPP;
}
591
#endif /* CONFIG_MEMORY_HOTREMOVE */
592

593
static int dlpar_add_lmb(struct drmem_lmb *lmb)
594 595
{
	unsigned long block_sz;
596
	int rc;
597

598 599 600
	if (lmb->flags & DRCONF_MEM_ASSIGNED)
		return -EINVAL;

601
	rc = update_lmb_associativity_index(lmb);
602 603 604 605 606
	if (rc) {
		dlpar_release_drc(lmb->drc_index);
		return rc;
	}

607
	lmb_set_nid(lmb);
608 609 610
	block_sz = memory_block_size_bytes();

	/* Add the memory */
611
	rc = __add_memory(lmb->nid, lmb->base_addr, block_sz);
612
	if (rc) {
613
		invalidate_lmb_associativity_index(lmb);
614 615 616 617 618
		return rc;
	}

	rc = dlpar_online_lmb(lmb);
	if (rc) {
619
		__remove_memory(lmb->nid, lmb->base_addr, block_sz);
620
		invalidate_lmb_associativity_index(lmb);
621
		lmb_clear_nid(lmb);
622
	} else {
623
		lmb->flags |= DRCONF_MEM_ASSIGNED;
624
	}
625 626 627 628

	return rc;
}

629
static int dlpar_memory_add_by_count(u32 lmbs_to_add)
630
{
631
	struct drmem_lmb *lmb;
632 633
	int lmbs_available = 0;
	int lmbs_added = 0;
634
	int rc;
635 636 637 638 639 640 641

	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 */
642 643
	for_each_drmem_lmb(lmb) {
		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
644
			lmbs_available++;
645 646 647

		if (lmbs_available == lmbs_to_add)
			break;
648 649 650 651 652
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

653 654
	for_each_drmem_lmb(lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
655 656
			continue;

657
		rc = dlpar_acquire_drc(lmb->drc_index);
658 659 660
		if (rc)
			continue;

661
		rc = dlpar_add_lmb(lmb);
662
		if (rc) {
663
			dlpar_release_drc(lmb->drc_index);
664 665 666
			continue;
		}

667 668 669
		/* Mark this lmb so we can remove it later if all of the
		 * requested LMBs cannot be added.
		 */
670 671 672 673 674
		drmem_mark_lmb_reserved(lmb);

		lmbs_added++;
		if (lmbs_added == lmbs_to_add)
			break;
675 676 677
	}

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

680 681
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
682 683
				continue;

684
			rc = dlpar_remove_lmb(lmb);
685 686
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
687
				       lmb->drc_index);
688
			else
689 690 691
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
692
		}
693 694
		rc = -EINVAL;
	} else {
695 696
		for_each_drmem_lmb(lmb) {
			if (!drmem_lmb_reserved(lmb))
697 698 699
				continue;

			pr_info("Memory at %llx (drc index %x) was hot-added\n",
700 701
				lmb->base_addr, lmb->drc_index);
			drmem_remove_lmb_reservation(lmb);
702
		}
703
		rc = 0;
704 705 706 707 708
	}

	return rc;
}

709
static int dlpar_memory_add_by_index(u32 drc_index)
710
{
711 712
	struct drmem_lmb *lmb;
	int rc, lmb_found;
713 714 715 716

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

	lmb_found = 0;
717 718
	for_each_drmem_lmb(lmb) {
		if (lmb->drc_index == drc_index) {
719
			lmb_found = 1;
720
			rc = dlpar_acquire_drc(lmb->drc_index);
721
			if (!rc) {
722
				rc = dlpar_add_lmb(lmb);
723
				if (rc)
724
					dlpar_release_drc(lmb->drc_index);
725 726
			}

727 728 729 730 731 732 733 734 735 736 737
			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",
738
			lmb->base_addr, drc_index);
739 740 741 742

	return rc;
}

743
static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
744
{
745 746 747
	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
	int lmbs_available = 0;
	int rc;
748 749 750 751 752 753 754

	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;

755 756
	rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
	if (rc)
757 758 759
		return -EINVAL;

	/* Validate that the LMBs in this range are not reserved */
760 761
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_RESERVED)
762 763 764 765 766 767 768 769
			break;

		lmbs_available++;
	}

	if (lmbs_available < lmbs_to_add)
		return -EINVAL;

770 771
	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
		if (lmb->flags & DRCONF_MEM_ASSIGNED)
772 773
			continue;

774
		rc = dlpar_acquire_drc(lmb->drc_index);
775 776 777
		if (rc)
			break;

778
		rc = dlpar_add_lmb(lmb);
779
		if (rc) {
780
			dlpar_release_drc(lmb->drc_index);
781 782 783
			break;
		}

784
		drmem_mark_lmb_reserved(lmb);
785 786 787 788 789
	}

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

790 791
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
792 793
				continue;

794
			rc = dlpar_remove_lmb(lmb);
795 796
			if (rc)
				pr_err("Failed to remove LMB, drc index %x\n",
797
				       lmb->drc_index);
798
			else
799 800 801
				dlpar_release_drc(lmb->drc_index);

			drmem_remove_lmb_reservation(lmb);
802 803 804
		}
		rc = -EINVAL;
	} else {
805 806
		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
			if (!drmem_lmb_reserved(lmb))
807 808 809
				continue;

			pr_info("Memory at %llx (drc index %x) was hot-added\n",
810 811
				lmb->base_addr, lmb->drc_index);
			drmem_remove_lmb_reservation(lmb);
812 813 814 815 816 817
		}
	}

	return rc;
}

818 819
int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
{
820 821 822
	u32 count, drc_index;
	int rc;

823 824 825
	lock_device_hotplug();

	switch (hp_elog->action) {
826
	case PSERIES_HP_ELOG_ACTION_ADD:
827 828
		switch (hp_elog->id_type) {
		case PSERIES_HP_ELOG_ID_DRC_COUNT:
829
			count = hp_elog->_drc_u.drc_count;
830
			rc = dlpar_memory_add_by_count(count);
831 832
			break;
		case PSERIES_HP_ELOG_ID_DRC_INDEX:
833
			drc_index = hp_elog->_drc_u.drc_index;
834
			rc = dlpar_memory_add_by_index(drc_index);
835 836
			break;
		case PSERIES_HP_ELOG_ID_DRC_IC:
837 838
			count = hp_elog->_drc_u.ic.count;
			drc_index = hp_elog->_drc_u.ic.index;
839
			rc = dlpar_memory_add_by_ic(count, drc_index);
840 841
			break;
		default:
842
			rc = -EINVAL;
843
			break;
844 845
		}

846
		break;
847
	case PSERIES_HP_ELOG_ACTION_REMOVE:
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_remove_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_remove_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_remove_by_ic(count, drc_index);
861 862
			break;
		default:
863
			rc = -EINVAL;
864
			break;
865 866
		}

867
		break;
868 869 870 871 872 873
	default:
		pr_err("Invalid action (%d) specified\n", hp_elog->action);
		rc = -EINVAL;
		break;
	}

874 875 876 877 878 879
	if (!rc) {
		rtas_hp_event = true;
		rc = drmem_update_dt();
		rtas_hp_event = false;
	}

880 881 882 883
	unlock_device_hotplug();
	return rc;
}

884
static int pseries_add_mem_node(struct device_node *np)
885
{
886
	const __be32 *regs;
887
	unsigned long base;
888
	unsigned int lmb_size;
889 890 891 892 893
	int ret = -EINVAL;

	/*
	 * Check to see if we are actually adding memory
	 */
894
	if (!of_node_is_type(np, "memory"))
895 896 897
		return 0;

	/*
Y
Yinghai Lu 已提交
898
	 * Find the base and size of the memblock
899 900 901 902 903
	 */
	regs = of_get_property(np, "reg", NULL);
	if (!regs)
		return ret;

904 905
	base = be64_to_cpu(*(unsigned long *)regs);
	lmb_size = be32_to_cpu(regs[3]);
906 907 908 909

	/*
	 * Update memory region to represent the memory add
	 */
910
	ret = memblock_add(base, lmb_size);
911 912 913
	return (ret < 0) ? -EINVAL : 0;
}

914
static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
915
{
916
	struct of_drconf_cell_v1 *new_drmem, *old_drmem;
917
	unsigned long memblock_size;
918
	u32 entries;
919
	__be32 *p;
920
	int i, rc = -EINVAL;
921

922 923 924
	if (rtas_hp_event)
		return 0;

925
	memblock_size = pseries_memory_block_size();
926
	if (!memblock_size)
927 928
		return -EINVAL;

929 930 931
	if (!pr->old_prop)
		return 0;

932
	p = (__be32 *) pr->old_prop->value;
933 934 935 936 937
	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
938
	 * entries. Get the number of entries and skip to the array of
939 940
	 * of_drconf_cell's.
	 */
941
	entries = be32_to_cpu(*p++);
942
	old_drmem = (struct of_drconf_cell_v1 *)p;
943

944
	p = (__be32 *)pr->prop->value;
945
	p++;
946
	new_drmem = (struct of_drconf_cell_v1 *)p;
947 948

	for (i = 0; i < entries; i++) {
949 950 951 952
		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),
953 954
						     memblock_size);
			break;
955 956 957 958 959
		} 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),
960 961 962 963
					  memblock_size);
			rc = (rc < 0) ? -EINVAL : 0;
			break;
		}
964 965
	}
	return rc;
966 967
}

968
static int pseries_memory_notifier(struct notifier_block *nb,
969
				   unsigned long action, void *data)
970
{
971
	struct of_reconfig_data *rd = data;
972
	int err = 0;
973 974

	switch (action) {
975
	case OF_RECONFIG_ATTACH_NODE:
976
		err = pseries_add_mem_node(rd->dn);
977
		break;
978
	case OF_RECONFIG_DETACH_NODE:
979
		err = pseries_remove_mem_node(rd->dn);
980
		break;
981
	case OF_RECONFIG_UPDATE_PROPERTY:
982 983
		if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
			err = pseries_update_drconf_memory(rd);
984 985
		break;
	}
986
	return notifier_from_errno(err);
987 988 989 990 991 992 993 994 995
}

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))
996
		of_reconfig_notifier_register(&pseries_mem_nb);
997 998 999 1000

	return 0;
}
machine_device_initcall(pseries, pseries_memory_hotplug_init);