1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * pseries Memory Hotplug infrastructure.
4  *
5  * Copyright (C) 2008 Badari Pulavarty, IBM Corporation
6  */
7 
8 #define pr_fmt(fmt)	"pseries-hotplug-mem: " fmt
9 
10 #include <linux/of.h>
11 #include <linux/of_address.h>
12 #include <linux/memblock.h>
13 #include <linux/memory.h>
14 #include <linux/memory_hotplug.h>
15 #include <linux/slab.h>
16 
17 #include <asm/firmware.h>
18 #include <asm/machdep.h>
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 static bool rtas_hp_event;
26 
27 unsigned long pseries_memory_block_size(void)
28 {
29 	struct device_node *np;
30 	unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
31 	struct resource r;
32 
33 	np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
34 	if (np) {
35 		const __be64 *size;
36 
37 		size = of_get_property(np, "ibm,lmb-size", NULL);
38 		if (size)
39 			memblock_size = be64_to_cpup(size);
40 		of_node_put(np);
41 	} else  if (machine_is(pseries)) {
42 		/* This fallback really only applies to pseries */
43 		unsigned int memzero_size = 0;
44 
45 		np = of_find_node_by_path("/memory@0");
46 		if (np) {
47 			if (!of_address_to_resource(np, 0, &r))
48 				memzero_size = resource_size(&r);
49 			of_node_put(np);
50 		}
51 
52 		if (memzero_size) {
53 			/* We now know the size of memory@0, use this to find
54 			 * the first memoryblock and get its size.
55 			 */
56 			char buf[64];
57 
58 			sprintf(buf, "/memory@%x", memzero_size);
59 			np = of_find_node_by_path(buf);
60 			if (np) {
61 				if (!of_address_to_resource(np, 0, &r))
62 					memblock_size = resource_size(&r);
63 				of_node_put(np);
64 			}
65 		}
66 	}
67 	return memblock_size;
68 }
69 
70 static void dlpar_free_property(struct property *prop)
71 {
72 	kfree(prop->name);
73 	kfree(prop->value);
74 	kfree(prop);
75 }
76 
77 static struct property *dlpar_clone_property(struct property *prop,
78 					     u32 prop_size)
79 {
80 	struct property *new_prop;
81 
82 	new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
83 	if (!new_prop)
84 		return NULL;
85 
86 	new_prop->name = kstrdup(prop->name, GFP_KERNEL);
87 	new_prop->value = kzalloc(prop_size, GFP_KERNEL);
88 	if (!new_prop->name || !new_prop->value) {
89 		dlpar_free_property(new_prop);
90 		return NULL;
91 	}
92 
93 	memcpy(new_prop->value, prop->value, prop->length);
94 	new_prop->length = prop_size;
95 
96 	of_property_set_flag(new_prop, OF_DYNAMIC);
97 	return new_prop;
98 }
99 
100 static bool find_aa_index(struct device_node *dr_node,
101 			 struct property *ala_prop,
102 			 const u32 *lmb_assoc, u32 *aa_index)
103 {
104 	u32 *assoc_arrays, new_prop_size;
105 	struct property *new_prop;
106 	int aa_arrays, aa_array_entries, aa_array_sz;
107 	int i, index;
108 
109 	/*
110 	 * The ibm,associativity-lookup-arrays property is defined to be
111 	 * a 32-bit value specifying the number of associativity arrays
112 	 * followed by a 32-bitvalue specifying the number of entries per
113 	 * array, followed by the associativity arrays.
114 	 */
115 	assoc_arrays = ala_prop->value;
116 
117 	aa_arrays = be32_to_cpu(assoc_arrays[0]);
118 	aa_array_entries = be32_to_cpu(assoc_arrays[1]);
119 	aa_array_sz = aa_array_entries * sizeof(u32);
120 
121 	for (i = 0; i < aa_arrays; i++) {
122 		index = (i * aa_array_entries) + 2;
123 
124 		if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
125 			continue;
126 
127 		*aa_index = i;
128 		return true;
129 	}
130 
131 	new_prop_size = ala_prop->length + aa_array_sz;
132 	new_prop = dlpar_clone_property(ala_prop, new_prop_size);
133 	if (!new_prop)
134 		return false;
135 
136 	assoc_arrays = new_prop->value;
137 
138 	/* increment the number of entries in the lookup array */
139 	assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
140 
141 	/* copy the new associativity into the lookup array */
142 	index = aa_arrays * aa_array_entries + 2;
143 	memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
144 
145 	of_update_property(dr_node, new_prop);
146 
147 	/*
148 	 * The associativity lookup array index for this lmb is
149 	 * number of entries - 1 since we added its associativity
150 	 * to the end of the lookup array.
151 	 */
152 	*aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
153 	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 	const u32 *lmb_assoc;
161 	u32 aa_index;
162 	bool found;
163 
164 	parent = of_find_node_by_path("/");
165 	if (!parent)
166 		return -ENODEV;
167 
168 	lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
169 					     parent);
170 	of_node_put(parent);
171 	if (!lmb_node)
172 		return -EINVAL;
173 
174 	lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
175 	if (!lmb_assoc) {
176 		dlpar_free_cc_nodes(lmb_node);
177 		return -ENODEV;
178 	}
179 
180 	dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
181 	if (!dr_node) {
182 		dlpar_free_cc_nodes(lmb_node);
183 		return -ENODEV;
184 	}
185 
186 	ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
187 				    NULL);
188 	if (!ala_prop) {
189 		of_node_put(dr_node);
190 		dlpar_free_cc_nodes(lmb_node);
191 		return -ENODEV;
192 	}
193 
194 	found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
195 
196 	of_node_put(dr_node);
197 	dlpar_free_cc_nodes(lmb_node);
198 
199 	if (!found) {
200 		pr_err("Could not find LMB associativity\n");
201 		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 	unsigned long section_nr;
211 	struct mem_section *mem_sect;
212 	struct memory_block *mem_block;
213 
214 	section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
215 	mem_sect = __nr_to_section(section_nr);
216 
217 	mem_block = find_memory_block(mem_sect);
218 	return mem_block;
219 }
220 
221 static int get_lmb_range(u32 drc_index, int n_lmbs,
222 			 struct drmem_lmb **start_lmb,
223 			 struct drmem_lmb **end_lmb)
224 {
225 	struct drmem_lmb *lmb, *start, *end;
226 	struct drmem_lmb *last_lmb;
227 
228 	start = NULL;
229 	for_each_drmem_lmb(lmb) {
230 		if (lmb->drc_index == drc_index) {
231 			start = lmb;
232 			break;
233 		}
234 	}
235 
236 	if (!start)
237 		return -EINVAL;
238 
239 	end = &start[n_lmbs - 1];
240 
241 	last_lmb = &drmem_info->lmbs[drmem_info->n_lmbs - 1];
242 	if (end > last_lmb)
243 		return -EINVAL;
244 
245 	*start_lmb = start;
246 	*end_lmb = end;
247 	return 0;
248 }
249 
250 static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
251 {
252 	struct memory_block *mem_block;
253 	int rc;
254 
255 	mem_block = lmb_to_memblock(lmb);
256 	if (!mem_block)
257 		return -EINVAL;
258 
259 	if (online && mem_block->dev.offline)
260 		rc = device_online(&mem_block->dev);
261 	else if (!online && !mem_block->dev.offline)
262 		rc = device_offline(&mem_block->dev);
263 	else
264 		rc = 0;
265 
266 	put_device(&mem_block->dev);
267 
268 	return rc;
269 }
270 
271 static int dlpar_online_lmb(struct drmem_lmb *lmb)
272 {
273 	return dlpar_change_lmb_state(lmb, true);
274 }
275 
276 #ifdef CONFIG_MEMORY_HOTREMOVE
277 static int dlpar_offline_lmb(struct drmem_lmb *lmb)
278 {
279 	return dlpar_change_lmb_state(lmb, false);
280 }
281 
282 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
283 {
284 	unsigned long block_sz, start_pfn;
285 	int sections_per_block;
286 	int i, nid;
287 
288 	start_pfn = base >> PAGE_SHIFT;
289 
290 	lock_device_hotplug();
291 
292 	if (!pfn_valid(start_pfn))
293 		goto out;
294 
295 	block_sz = pseries_memory_block_size();
296 	sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
297 	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 */
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 	int ret = -EINVAL;
317 
318 	/*
319 	 * Check to see if we are actually removing memory
320 	 */
321 	if (!of_node_is_type(np, "memory"))
322 		return 0;
323 
324 	/*
325 	 * Find the base address and size of the memblock
326 	 */
327 	regs = of_get_property(np, "reg", NULL);
328 	if (!regs)
329 		return ret;
330 
331 	base = be64_to_cpu(*(unsigned long *)regs);
332 	lmb_size = be32_to_cpu(regs[3]);
333 
334 	pseries_remove_memblock(base, lmb_size);
335 	return 0;
336 }
337 
338 static bool lmb_is_removable(struct drmem_lmb *lmb)
339 {
340 	int i, scns_per_block;
341 	int rc = 1;
342 	unsigned long pfn, block_sz;
343 	u64 phys_addr;
344 
345 	if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
346 		return false;
347 
348 	block_sz = memory_block_size_bytes();
349 	scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
350 	phys_addr = lmb->base_addr;
351 
352 #ifdef CONFIG_FA_DUMP
353 	/*
354 	 * Don't hot-remove memory that falls in fadump boot memory area
355 	 * and memory that is reserved for capturing old kernel memory.
356 	 */
357 	if (is_fadump_memory_area(phys_addr, block_sz))
358 		return false;
359 #endif
360 
361 	for (i = 0; i < scns_per_block; i++) {
362 		pfn = PFN_DOWN(phys_addr);
363 		if (!pfn_present(pfn))
364 			continue;
365 
366 		rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
367 		phys_addr += MIN_MEMORY_BLOCK_SIZE;
368 	}
369 
370 	return rc ? true : false;
371 }
372 
373 static int dlpar_add_lmb(struct drmem_lmb *);
374 
375 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
376 {
377 	unsigned long block_sz;
378 	int rc;
379 
380 	if (!lmb_is_removable(lmb))
381 		return -EINVAL;
382 
383 	rc = dlpar_offline_lmb(lmb);
384 	if (rc)
385 		return rc;
386 
387 	block_sz = pseries_memory_block_size();
388 
389 	__remove_memory(lmb->nid, lmb->base_addr, block_sz);
390 
391 	/* Update memory regions for memory remove */
392 	memblock_remove(lmb->base_addr, block_sz);
393 
394 	invalidate_lmb_associativity_index(lmb);
395 	lmb_clear_nid(lmb);
396 	lmb->flags &= ~DRCONF_MEM_ASSIGNED;
397 
398 	return 0;
399 }
400 
401 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
402 {
403 	struct drmem_lmb *lmb;
404 	int lmbs_removed = 0;
405 	int lmbs_available = 0;
406 	int rc;
407 
408 	pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
409 
410 	if (lmbs_to_remove == 0)
411 		return -EINVAL;
412 
413 	/* Validate that there are enough LMBs to satisfy the request */
414 	for_each_drmem_lmb(lmb) {
415 		if (lmb_is_removable(lmb))
416 			lmbs_available++;
417 
418 		if (lmbs_available == lmbs_to_remove)
419 			break;
420 	}
421 
422 	if (lmbs_available < lmbs_to_remove) {
423 		pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
424 			lmbs_available, lmbs_to_remove);
425 		return -EINVAL;
426 	}
427 
428 	for_each_drmem_lmb(lmb) {
429 		rc = dlpar_remove_lmb(lmb);
430 		if (rc)
431 			continue;
432 
433 		/* Mark this lmb so we can add it later if all of the
434 		 * requested LMBs cannot be removed.
435 		 */
436 		drmem_mark_lmb_reserved(lmb);
437 
438 		lmbs_removed++;
439 		if (lmbs_removed == lmbs_to_remove)
440 			break;
441 	}
442 
443 	if (lmbs_removed != lmbs_to_remove) {
444 		pr_err("Memory hot-remove failed, adding LMB's back\n");
445 
446 		for_each_drmem_lmb(lmb) {
447 			if (!drmem_lmb_reserved(lmb))
448 				continue;
449 
450 			rc = dlpar_add_lmb(lmb);
451 			if (rc)
452 				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 		rc = -EINVAL;
459 	} else {
460 		for_each_drmem_lmb(lmb) {
461 			if (!drmem_lmb_reserved(lmb))
462 				continue;
463 
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 		rc = 0;
471 	}
472 
473 	return rc;
474 }
475 
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 	pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
483 
484 	lmb_found = 0;
485 	for_each_drmem_lmb(lmb) {
486 		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 			break;
493 		}
494 	}
495 
496 	if (!lmb_found)
497 		rc = -EINVAL;
498 
499 	if (rc)
500 		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 	return rc;
506 }
507 
508 static int dlpar_memory_readd_by_index(u32 drc_index)
509 {
510 	struct drmem_lmb *lmb;
511 	int lmb_found;
512 	int rc;
513 
514 	pr_info("Attempting to update LMB, drc index %x\n", drc_index);
515 
516 	lmb_found = 0;
517 	for_each_drmem_lmb(lmb) {
518 		if (lmb->drc_index == drc_index) {
519 			lmb_found = 1;
520 			rc = dlpar_remove_lmb(lmb);
521 			if (!rc) {
522 				rc = dlpar_add_lmb(lmb);
523 				if (rc)
524 					dlpar_release_drc(lmb->drc_index);
525 			}
526 			break;
527 		}
528 	}
529 
530 	if (!lmb_found)
531 		rc = -EINVAL;
532 
533 	if (rc)
534 		pr_info("Failed to update memory at %llx\n",
535 			lmb->base_addr);
536 	else
537 		pr_info("Memory at %llx was updated\n", lmb->base_addr);
538 
539 	return rc;
540 }
541 
542 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
543 {
544 	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
545 	int lmbs_available = 0;
546 	int rc;
547 
548 	pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
549 		lmbs_to_remove, drc_index);
550 
551 	if (lmbs_to_remove == 0)
552 		return -EINVAL;
553 
554 	rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
555 	if (rc)
556 		return -EINVAL;
557 
558 	/* Validate that there are enough LMBs to satisfy the request */
559 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
560 		if (lmb->flags & DRCONF_MEM_RESERVED)
561 			break;
562 
563 		lmbs_available++;
564 	}
565 
566 	if (lmbs_available < lmbs_to_remove)
567 		return -EINVAL;
568 
569 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
570 		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
571 			continue;
572 
573 		rc = dlpar_remove_lmb(lmb);
574 		if (rc)
575 			break;
576 
577 		drmem_mark_lmb_reserved(lmb);
578 	}
579 
580 	if (rc) {
581 		pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
582 
583 
584 		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
585 			if (!drmem_lmb_reserved(lmb))
586 				continue;
587 
588 			rc = dlpar_add_lmb(lmb);
589 			if (rc)
590 				pr_err("Failed to add LMB, drc index %x\n",
591 				       lmb->drc_index);
592 
593 			drmem_remove_lmb_reservation(lmb);
594 		}
595 		rc = -EINVAL;
596 	} else {
597 		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
598 			if (!drmem_lmb_reserved(lmb))
599 				continue;
600 
601 			dlpar_release_drc(lmb->drc_index);
602 			pr_info("Memory at %llx (drc index %x) was hot-removed\n",
603 				lmb->base_addr, lmb->drc_index);
604 
605 			drmem_remove_lmb_reservation(lmb);
606 		}
607 	}
608 
609 	return rc;
610 }
611 
612 #else
613 static inline int pseries_remove_memblock(unsigned long base,
614 					  unsigned int memblock_size)
615 {
616 	return -EOPNOTSUPP;
617 }
618 static inline int pseries_remove_mem_node(struct device_node *np)
619 {
620 	return 0;
621 }
622 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
623 {
624 	return -EOPNOTSUPP;
625 }
626 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
627 {
628 	return -EOPNOTSUPP;
629 }
630 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
631 {
632 	return -EOPNOTSUPP;
633 }
634 static int dlpar_memory_remove_by_index(u32 drc_index)
635 {
636 	return -EOPNOTSUPP;
637 }
638 static int dlpar_memory_readd_by_index(u32 drc_index)
639 {
640 	return -EOPNOTSUPP;
641 }
642 
643 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
644 {
645 	return -EOPNOTSUPP;
646 }
647 #endif /* CONFIG_MEMORY_HOTREMOVE */
648 
649 static int dlpar_add_lmb(struct drmem_lmb *lmb)
650 {
651 	unsigned long block_sz;
652 	int rc;
653 
654 	if (lmb->flags & DRCONF_MEM_ASSIGNED)
655 		return -EINVAL;
656 
657 	rc = update_lmb_associativity_index(lmb);
658 	if (rc) {
659 		dlpar_release_drc(lmb->drc_index);
660 		return rc;
661 	}
662 
663 	lmb_set_nid(lmb);
664 	block_sz = memory_block_size_bytes();
665 
666 	/* Add the memory */
667 	rc = __add_memory(lmb->nid, lmb->base_addr, block_sz);
668 	if (rc) {
669 		invalidate_lmb_associativity_index(lmb);
670 		return rc;
671 	}
672 
673 	rc = dlpar_online_lmb(lmb);
674 	if (rc) {
675 		__remove_memory(lmb->nid, lmb->base_addr, block_sz);
676 		invalidate_lmb_associativity_index(lmb);
677 		lmb_clear_nid(lmb);
678 	} else {
679 		lmb->flags |= DRCONF_MEM_ASSIGNED;
680 	}
681 
682 	return rc;
683 }
684 
685 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
686 {
687 	struct drmem_lmb *lmb;
688 	int lmbs_available = 0;
689 	int lmbs_added = 0;
690 	int rc;
691 
692 	pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
693 
694 	if (lmbs_to_add == 0)
695 		return -EINVAL;
696 
697 	/* Validate that there are enough LMBs to satisfy the request */
698 	for_each_drmem_lmb(lmb) {
699 		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
700 			lmbs_available++;
701 
702 		if (lmbs_available == lmbs_to_add)
703 			break;
704 	}
705 
706 	if (lmbs_available < lmbs_to_add)
707 		return -EINVAL;
708 
709 	for_each_drmem_lmb(lmb) {
710 		if (lmb->flags & DRCONF_MEM_ASSIGNED)
711 			continue;
712 
713 		rc = dlpar_acquire_drc(lmb->drc_index);
714 		if (rc)
715 			continue;
716 
717 		rc = dlpar_add_lmb(lmb);
718 		if (rc) {
719 			dlpar_release_drc(lmb->drc_index);
720 			continue;
721 		}
722 
723 		/* Mark this lmb so we can remove it later if all of the
724 		 * requested LMBs cannot be added.
725 		 */
726 		drmem_mark_lmb_reserved(lmb);
727 
728 		lmbs_added++;
729 		if (lmbs_added == lmbs_to_add)
730 			break;
731 	}
732 
733 	if (lmbs_added != lmbs_to_add) {
734 		pr_err("Memory hot-add failed, removing any added LMBs\n");
735 
736 		for_each_drmem_lmb(lmb) {
737 			if (!drmem_lmb_reserved(lmb))
738 				continue;
739 
740 			rc = dlpar_remove_lmb(lmb);
741 			if (rc)
742 				pr_err("Failed to remove LMB, drc index %x\n",
743 				       lmb->drc_index);
744 			else
745 				dlpar_release_drc(lmb->drc_index);
746 
747 			drmem_remove_lmb_reservation(lmb);
748 		}
749 		rc = -EINVAL;
750 	} else {
751 		for_each_drmem_lmb(lmb) {
752 			if (!drmem_lmb_reserved(lmb))
753 				continue;
754 
755 			pr_info("Memory at %llx (drc index %x) was hot-added\n",
756 				lmb->base_addr, lmb->drc_index);
757 			drmem_remove_lmb_reservation(lmb);
758 		}
759 		rc = 0;
760 	}
761 
762 	return rc;
763 }
764 
765 static int dlpar_memory_add_by_index(u32 drc_index)
766 {
767 	struct drmem_lmb *lmb;
768 	int rc, lmb_found;
769 
770 	pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
771 
772 	lmb_found = 0;
773 	for_each_drmem_lmb(lmb) {
774 		if (lmb->drc_index == drc_index) {
775 			lmb_found = 1;
776 			rc = dlpar_acquire_drc(lmb->drc_index);
777 			if (!rc) {
778 				rc = dlpar_add_lmb(lmb);
779 				if (rc)
780 					dlpar_release_drc(lmb->drc_index);
781 			}
782 
783 			break;
784 		}
785 	}
786 
787 	if (!lmb_found)
788 		rc = -EINVAL;
789 
790 	if (rc)
791 		pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
792 	else
793 		pr_info("Memory at %llx (drc index %x) was hot-added\n",
794 			lmb->base_addr, drc_index);
795 
796 	return rc;
797 }
798 
799 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
800 {
801 	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
802 	int lmbs_available = 0;
803 	int rc;
804 
805 	pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
806 		lmbs_to_add, drc_index);
807 
808 	if (lmbs_to_add == 0)
809 		return -EINVAL;
810 
811 	rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
812 	if (rc)
813 		return -EINVAL;
814 
815 	/* Validate that the LMBs in this range are not reserved */
816 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
817 		if (lmb->flags & DRCONF_MEM_RESERVED)
818 			break;
819 
820 		lmbs_available++;
821 	}
822 
823 	if (lmbs_available < lmbs_to_add)
824 		return -EINVAL;
825 
826 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
827 		if (lmb->flags & DRCONF_MEM_ASSIGNED)
828 			continue;
829 
830 		rc = dlpar_acquire_drc(lmb->drc_index);
831 		if (rc)
832 			break;
833 
834 		rc = dlpar_add_lmb(lmb);
835 		if (rc) {
836 			dlpar_release_drc(lmb->drc_index);
837 			break;
838 		}
839 
840 		drmem_mark_lmb_reserved(lmb);
841 	}
842 
843 	if (rc) {
844 		pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
845 
846 		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
847 			if (!drmem_lmb_reserved(lmb))
848 				continue;
849 
850 			rc = dlpar_remove_lmb(lmb);
851 			if (rc)
852 				pr_err("Failed to remove LMB, drc index %x\n",
853 				       lmb->drc_index);
854 			else
855 				dlpar_release_drc(lmb->drc_index);
856 
857 			drmem_remove_lmb_reservation(lmb);
858 		}
859 		rc = -EINVAL;
860 	} else {
861 		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
862 			if (!drmem_lmb_reserved(lmb))
863 				continue;
864 
865 			pr_info("Memory at %llx (drc index %x) was hot-added\n",
866 				lmb->base_addr, lmb->drc_index);
867 			drmem_remove_lmb_reservation(lmb);
868 		}
869 	}
870 
871 	return rc;
872 }
873 
874 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
875 {
876 	u32 count, drc_index;
877 	int rc;
878 
879 	lock_device_hotplug();
880 
881 	switch (hp_elog->action) {
882 	case PSERIES_HP_ELOG_ACTION_ADD:
883 		if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) {
884 			count = hp_elog->_drc_u.drc_count;
885 			rc = dlpar_memory_add_by_count(count);
886 		} else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
887 			drc_index = hp_elog->_drc_u.drc_index;
888 			rc = dlpar_memory_add_by_index(drc_index);
889 		} else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_IC) {
890 			count = hp_elog->_drc_u.ic.count;
891 			drc_index = hp_elog->_drc_u.ic.index;
892 			rc = dlpar_memory_add_by_ic(count, drc_index);
893 		} else {
894 			rc = -EINVAL;
895 		}
896 
897 		break;
898 	case PSERIES_HP_ELOG_ACTION_REMOVE:
899 		if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) {
900 			count = hp_elog->_drc_u.drc_count;
901 			rc = dlpar_memory_remove_by_count(count);
902 		} else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
903 			drc_index = hp_elog->_drc_u.drc_index;
904 			rc = dlpar_memory_remove_by_index(drc_index);
905 		} else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_IC) {
906 			count = hp_elog->_drc_u.ic.count;
907 			drc_index = hp_elog->_drc_u.ic.index;
908 			rc = dlpar_memory_remove_by_ic(count, drc_index);
909 		} else {
910 			rc = -EINVAL;
911 		}
912 
913 		break;
914 	case PSERIES_HP_ELOG_ACTION_READD:
915 		drc_index = hp_elog->_drc_u.drc_index;
916 		rc = dlpar_memory_readd_by_index(drc_index);
917 		break;
918 	default:
919 		pr_err("Invalid action (%d) specified\n", hp_elog->action);
920 		rc = -EINVAL;
921 		break;
922 	}
923 
924 	if (!rc) {
925 		rtas_hp_event = true;
926 		rc = drmem_update_dt();
927 		rtas_hp_event = false;
928 	}
929 
930 	unlock_device_hotplug();
931 	return rc;
932 }
933 
934 static int pseries_add_mem_node(struct device_node *np)
935 {
936 	const __be32 *regs;
937 	unsigned long base;
938 	unsigned int lmb_size;
939 	int ret = -EINVAL;
940 
941 	/*
942 	 * Check to see if we are actually adding memory
943 	 */
944 	if (!of_node_is_type(np, "memory"))
945 		return 0;
946 
947 	/*
948 	 * Find the base and size of the memblock
949 	 */
950 	regs = of_get_property(np, "reg", NULL);
951 	if (!regs)
952 		return ret;
953 
954 	base = be64_to_cpu(*(unsigned long *)regs);
955 	lmb_size = be32_to_cpu(regs[3]);
956 
957 	/*
958 	 * Update memory region to represent the memory add
959 	 */
960 	ret = memblock_add(base, lmb_size);
961 	return (ret < 0) ? -EINVAL : 0;
962 }
963 
964 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
965 {
966 	struct of_drconf_cell_v1 *new_drmem, *old_drmem;
967 	unsigned long memblock_size;
968 	u32 entries;
969 	__be32 *p;
970 	int i, rc = -EINVAL;
971 
972 	if (rtas_hp_event)
973 		return 0;
974 
975 	memblock_size = pseries_memory_block_size();
976 	if (!memblock_size)
977 		return -EINVAL;
978 
979 	p = (__be32 *) pr->old_prop->value;
980 	if (!p)
981 		return -EINVAL;
982 
983 	/* The first int of the property is the number of lmb's described
984 	 * by the property. This is followed by an array of of_drconf_cell
985 	 * entries. Get the number of entries and skip to the array of
986 	 * of_drconf_cell's.
987 	 */
988 	entries = be32_to_cpu(*p++);
989 	old_drmem = (struct of_drconf_cell_v1 *)p;
990 
991 	p = (__be32 *)pr->prop->value;
992 	p++;
993 	new_drmem = (struct of_drconf_cell_v1 *)p;
994 
995 	for (i = 0; i < entries; i++) {
996 		if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
997 		    (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
998 			rc = pseries_remove_memblock(
999 				be64_to_cpu(old_drmem[i].base_addr),
1000 						     memblock_size);
1001 			break;
1002 		} else if ((!(be32_to_cpu(old_drmem[i].flags) &
1003 			    DRCONF_MEM_ASSIGNED)) &&
1004 			    (be32_to_cpu(new_drmem[i].flags) &
1005 			    DRCONF_MEM_ASSIGNED)) {
1006 			rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
1007 					  memblock_size);
1008 			rc = (rc < 0) ? -EINVAL : 0;
1009 			break;
1010 		}
1011 	}
1012 	return rc;
1013 }
1014 
1015 static int pseries_memory_notifier(struct notifier_block *nb,
1016 				   unsigned long action, void *data)
1017 {
1018 	struct of_reconfig_data *rd = data;
1019 	int err = 0;
1020 
1021 	switch (action) {
1022 	case OF_RECONFIG_ATTACH_NODE:
1023 		err = pseries_add_mem_node(rd->dn);
1024 		break;
1025 	case OF_RECONFIG_DETACH_NODE:
1026 		err = pseries_remove_mem_node(rd->dn);
1027 		break;
1028 	case OF_RECONFIG_UPDATE_PROPERTY:
1029 		if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
1030 			err = pseries_update_drconf_memory(rd);
1031 		break;
1032 	}
1033 	return notifier_from_errno(err);
1034 }
1035 
1036 static struct notifier_block pseries_mem_nb = {
1037 	.notifier_call = pseries_memory_notifier,
1038 };
1039 
1040 static int __init pseries_memory_hotplug_init(void)
1041 {
1042 	if (firmware_has_feature(FW_FEATURE_LPAR))
1043 		of_reconfig_notifier_register(&pseries_mem_nb);
1044 
1045 	return 0;
1046 }
1047 machine_device_initcall(pseries, pseries_memory_hotplug_init);
1048