xref: /linux/arch/powerpc/platforms/pseries/hotplug-memory.c (revision c67454615cf95160cb806f7a471158a901eb261d)
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 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 	np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
32 	if (np) {
33 		int len;
34 		int size_cells;
35 		const __be32 *prop;
36 
37 		size_cells = of_n_size_cells(np);
38 
39 		prop = of_get_property(np, "ibm,lmb-size", &len);
40 		if (prop && len >= size_cells * sizeof(__be32))
41 			memblock_size = of_read_number(prop, size_cells);
42 		of_node_put(np);
43 
44 	} else  if (machine_is(pseries)) {
45 		/* This fallback really only applies to pseries */
46 		unsigned int memzero_size = 0;
47 
48 		np = of_find_node_by_path("/memory@0");
49 		if (np) {
50 			if (!of_address_to_resource(np, 0, &r))
51 				memzero_size = resource_size(&r);
52 			of_node_put(np);
53 		}
54 
55 		if (memzero_size) {
56 			/* We now know the size of memory@0, use this to find
57 			 * the first memoryblock and get its size.
58 			 */
59 			char buf[64];
60 
61 			sprintf(buf, "/memory@%x", memzero_size);
62 			np = of_find_node_by_path(buf);
63 			if (np) {
64 				if (!of_address_to_resource(np, 0, &r))
65 					memblock_size = resource_size(&r);
66 				of_node_put(np);
67 			}
68 		}
69 	}
70 	return memblock_size;
71 }
72 
73 static void dlpar_free_property(struct property *prop)
74 {
75 	kfree(prop->name);
76 	kfree(prop->value);
77 	kfree(prop);
78 }
79 
80 static struct property *dlpar_clone_property(struct property *prop,
81 					     u32 prop_size)
82 {
83 	struct property *new_prop;
84 
85 	new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
86 	if (!new_prop)
87 		return NULL;
88 
89 	new_prop->name = kstrdup(prop->name, GFP_KERNEL);
90 	new_prop->value = kzalloc(prop_size, GFP_KERNEL);
91 	if (!new_prop->name || !new_prop->value) {
92 		dlpar_free_property(new_prop);
93 		return NULL;
94 	}
95 
96 	memcpy(new_prop->value, prop->value, prop->length);
97 	new_prop->length = prop_size;
98 
99 	of_property_set_flag(new_prop, OF_DYNAMIC);
100 	return new_prop;
101 }
102 
103 static bool find_aa_index(struct device_node *dr_node,
104 			 struct property *ala_prop,
105 			 const u32 *lmb_assoc, u32 *aa_index)
106 {
107 	u32 *assoc_arrays, new_prop_size;
108 	struct property *new_prop;
109 	int aa_arrays, aa_array_entries, aa_array_sz;
110 	int i, index;
111 
112 	/*
113 	 * The ibm,associativity-lookup-arrays property is defined to be
114 	 * a 32-bit value specifying the number of associativity arrays
115 	 * followed by a 32-bitvalue specifying the number of entries per
116 	 * array, followed by the associativity arrays.
117 	 */
118 	assoc_arrays = ala_prop->value;
119 
120 	aa_arrays = be32_to_cpu(assoc_arrays[0]);
121 	aa_array_entries = be32_to_cpu(assoc_arrays[1]);
122 	aa_array_sz = aa_array_entries * sizeof(u32);
123 
124 	for (i = 0; i < aa_arrays; i++) {
125 		index = (i * aa_array_entries) + 2;
126 
127 		if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
128 			continue;
129 
130 		*aa_index = i;
131 		return true;
132 	}
133 
134 	new_prop_size = ala_prop->length + aa_array_sz;
135 	new_prop = dlpar_clone_property(ala_prop, new_prop_size);
136 	if (!new_prop)
137 		return false;
138 
139 	assoc_arrays = new_prop->value;
140 
141 	/* increment the number of entries in the lookup array */
142 	assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
143 
144 	/* copy the new associativity into the lookup array */
145 	index = aa_arrays * aa_array_entries + 2;
146 	memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
147 
148 	of_update_property(dr_node, new_prop);
149 
150 	/*
151 	 * The associativity lookup array index for this lmb is
152 	 * number of entries - 1 since we added its associativity
153 	 * to the end of the lookup array.
154 	 */
155 	*aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
156 	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 	const u32 *lmb_assoc;
164 	u32 aa_index;
165 	bool found;
166 
167 	parent = of_find_node_by_path("/");
168 	if (!parent)
169 		return -ENODEV;
170 
171 	lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
172 					     parent);
173 	of_node_put(parent);
174 	if (!lmb_node)
175 		return -EINVAL;
176 
177 	lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
178 	if (!lmb_assoc) {
179 		dlpar_free_cc_nodes(lmb_node);
180 		return -ENODEV;
181 	}
182 
183 	dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
184 	if (!dr_node) {
185 		dlpar_free_cc_nodes(lmb_node);
186 		return -ENODEV;
187 	}
188 
189 	ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
190 				    NULL);
191 	if (!ala_prop) {
192 		of_node_put(dr_node);
193 		dlpar_free_cc_nodes(lmb_node);
194 		return -ENODEV;
195 	}
196 
197 	found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
198 
199 	of_node_put(dr_node);
200 	dlpar_free_cc_nodes(lmb_node);
201 
202 	if (!found) {
203 		pr_err("Could not find LMB associativity\n");
204 		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 	unsigned long section_nr;
214 	struct mem_section *mem_sect;
215 	struct memory_block *mem_block;
216 
217 	section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
218 	mem_sect = __nr_to_section(section_nr);
219 
220 	mem_block = find_memory_block(mem_sect);
221 	return mem_block;
222 }
223 
224 static int get_lmb_range(u32 drc_index, int n_lmbs,
225 			 struct drmem_lmb **start_lmb,
226 			 struct drmem_lmb **end_lmb)
227 {
228 	struct drmem_lmb *lmb, *start, *end;
229 	struct drmem_lmb *limit;
230 
231 	start = NULL;
232 	for_each_drmem_lmb(lmb) {
233 		if (lmb->drc_index == drc_index) {
234 			start = lmb;
235 			break;
236 		}
237 	}
238 
239 	if (!start)
240 		return -EINVAL;
241 
242 	end = &start[n_lmbs];
243 
244 	limit = &drmem_info->lmbs[drmem_info->n_lmbs];
245 	if (end > limit)
246 		return -EINVAL;
247 
248 	*start_lmb = start;
249 	*end_lmb = end;
250 	return 0;
251 }
252 
253 static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
254 {
255 	struct memory_block *mem_block;
256 	int rc;
257 
258 	mem_block = lmb_to_memblock(lmb);
259 	if (!mem_block)
260 		return -EINVAL;
261 
262 	if (online && mem_block->dev.offline)
263 		rc = device_online(&mem_block->dev);
264 	else if (!online && !mem_block->dev.offline)
265 		rc = device_offline(&mem_block->dev);
266 	else
267 		rc = 0;
268 
269 	put_device(&mem_block->dev);
270 
271 	return rc;
272 }
273 
274 static int dlpar_online_lmb(struct drmem_lmb *lmb)
275 {
276 	return dlpar_change_lmb_state(lmb, true);
277 }
278 
279 #ifdef CONFIG_MEMORY_HOTREMOVE
280 static int dlpar_offline_lmb(struct drmem_lmb *lmb)
281 {
282 	return dlpar_change_lmb_state(lmb, false);
283 }
284 
285 static int pseries_remove_memblock(unsigned long base, unsigned long memblock_size)
286 {
287 	unsigned long block_sz, start_pfn;
288 	int sections_per_block;
289 	int i, nid;
290 
291 	start_pfn = base >> PAGE_SHIFT;
292 
293 	lock_device_hotplug();
294 
295 	if (!pfn_valid(start_pfn))
296 		goto out;
297 
298 	block_sz = pseries_memory_block_size();
299 	sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
300 	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 */
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 	 * Check to see if we are actually removing memory
324 	 */
325 	if (!of_node_is_type(np, "memory"))
326 		return 0;
327 
328 	/*
329 	 * Find the base address and size of the memblock
330 	 */
331 	prop = of_get_property(np, "reg", NULL);
332 	if (!prop)
333 		return ret;
334 
335 	addr_cells = of_n_addr_cells(np);
336 	size_cells = of_n_size_cells(np);
337 
338 	/*
339 	 * "reg" property represents (addr,size) tuple.
340 	 */
341 	base = of_read_number(prop, addr_cells);
342 	prop += addr_cells;
343 	lmb_size = of_read_number(prop, size_cells);
344 
345 	pseries_remove_memblock(base, lmb_size);
346 	return 0;
347 }
348 
349 static bool lmb_is_removable(struct drmem_lmb *lmb)
350 {
351 	if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
352 		return false;
353 
354 #ifdef CONFIG_FA_DUMP
355 	/*
356 	 * Don't hot-remove memory that falls in fadump boot memory area
357 	 * and memory that is reserved for capturing old kernel memory.
358 	 */
359 	if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
360 		return false;
361 #endif
362 	/* device_offline() will determine if we can actually remove this lmb */
363 	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 	if (!lmb_is_removable(lmb))
375 		return -EINVAL;
376 
377 	mem_block = lmb_to_memblock(lmb);
378 	if (mem_block == NULL)
379 		return -EINVAL;
380 
381 	rc = dlpar_offline_lmb(lmb);
382 	if (rc) {
383 		put_device(&mem_block->dev);
384 		return rc;
385 	}
386 
387 	block_sz = pseries_memory_block_size();
388 
389 	__remove_memory(mem_block->nid, lmb->base_addr, block_sz);
390 	put_device(&mem_block->dev);
391 
392 	/* Update memory regions for memory remove */
393 	memblock_remove(lmb->base_addr, block_sz);
394 
395 	invalidate_lmb_associativity_index(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_debug("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_debug("Failed to hot-remove memory at %llx\n",
501 			 lmb->base_addr);
502 	else
503 		pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
504 
505 	return rc;
506 }
507 
508 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
509 {
510 	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
511 	int lmbs_available = 0;
512 	int rc;
513 
514 	pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
515 		lmbs_to_remove, drc_index);
516 
517 	if (lmbs_to_remove == 0)
518 		return -EINVAL;
519 
520 	rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
521 	if (rc)
522 		return -EINVAL;
523 
524 	/* Validate that there are enough LMBs to satisfy the request */
525 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
526 		if (lmb->flags & DRCONF_MEM_RESERVED)
527 			break;
528 
529 		lmbs_available++;
530 	}
531 
532 	if (lmbs_available < lmbs_to_remove)
533 		return -EINVAL;
534 
535 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
536 		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
537 			continue;
538 
539 		rc = dlpar_remove_lmb(lmb);
540 		if (rc)
541 			break;
542 
543 		drmem_mark_lmb_reserved(lmb);
544 	}
545 
546 	if (rc) {
547 		pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
548 
549 
550 		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
551 			if (!drmem_lmb_reserved(lmb))
552 				continue;
553 
554 			rc = dlpar_add_lmb(lmb);
555 			if (rc)
556 				pr_err("Failed to add LMB, drc index %x\n",
557 				       lmb->drc_index);
558 
559 			drmem_remove_lmb_reservation(lmb);
560 		}
561 		rc = -EINVAL;
562 	} else {
563 		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
564 			if (!drmem_lmb_reserved(lmb))
565 				continue;
566 
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 	return rc;
576 }
577 
578 #else
579 static inline int pseries_remove_memblock(unsigned long base,
580 					  unsigned long memblock_size)
581 {
582 	return -EOPNOTSUPP;
583 }
584 static inline int pseries_remove_mem_node(struct device_node *np)
585 {
586 	return 0;
587 }
588 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
589 {
590 	return -EOPNOTSUPP;
591 }
592 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
593 {
594 	return -EOPNOTSUPP;
595 }
596 static int dlpar_memory_remove_by_index(u32 drc_index)
597 {
598 	return -EOPNOTSUPP;
599 }
600 
601 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
602 {
603 	return -EOPNOTSUPP;
604 }
605 #endif /* CONFIG_MEMORY_HOTREMOVE */
606 
607 static int dlpar_add_lmb(struct drmem_lmb *lmb)
608 {
609 	unsigned long block_sz;
610 	int nid, rc;
611 
612 	if (lmb->flags & DRCONF_MEM_ASSIGNED)
613 		return -EINVAL;
614 
615 	rc = update_lmb_associativity_index(lmb);
616 	if (rc) {
617 		dlpar_release_drc(lmb->drc_index);
618 		return rc;
619 	}
620 
621 	block_sz = memory_block_size_bytes();
622 
623 	/* Find the node id for this LMB.  Fake one if necessary. */
624 	nid = of_drconf_to_nid_single(lmb);
625 	if (nid < 0 || !node_possible(nid))
626 		nid = first_online_node;
627 
628 	/* Add the memory */
629 	rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
630 	if (rc) {
631 		invalidate_lmb_associativity_index(lmb);
632 		return rc;
633 	}
634 
635 	rc = dlpar_online_lmb(lmb);
636 	if (rc) {
637 		__remove_memory(nid, lmb->base_addr, block_sz);
638 		invalidate_lmb_associativity_index(lmb);
639 	} else {
640 		lmb->flags |= DRCONF_MEM_ASSIGNED;
641 	}
642 
643 	return rc;
644 }
645 
646 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
647 {
648 	struct drmem_lmb *lmb;
649 	int lmbs_available = 0;
650 	int lmbs_added = 0;
651 	int rc;
652 
653 	pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
654 
655 	if (lmbs_to_add == 0)
656 		return -EINVAL;
657 
658 	/* Validate that there are enough LMBs to satisfy the request */
659 	for_each_drmem_lmb(lmb) {
660 		if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
661 			lmbs_available++;
662 
663 		if (lmbs_available == lmbs_to_add)
664 			break;
665 	}
666 
667 	if (lmbs_available < lmbs_to_add)
668 		return -EINVAL;
669 
670 	for_each_drmem_lmb(lmb) {
671 		if (lmb->flags & DRCONF_MEM_ASSIGNED)
672 			continue;
673 
674 		rc = dlpar_acquire_drc(lmb->drc_index);
675 		if (rc)
676 			continue;
677 
678 		rc = dlpar_add_lmb(lmb);
679 		if (rc) {
680 			dlpar_release_drc(lmb->drc_index);
681 			continue;
682 		}
683 
684 		/* Mark this lmb so we can remove it later if all of the
685 		 * requested LMBs cannot be added.
686 		 */
687 		drmem_mark_lmb_reserved(lmb);
688 
689 		lmbs_added++;
690 		if (lmbs_added == lmbs_to_add)
691 			break;
692 	}
693 
694 	if (lmbs_added != lmbs_to_add) {
695 		pr_err("Memory hot-add failed, removing any added LMBs\n");
696 
697 		for_each_drmem_lmb(lmb) {
698 			if (!drmem_lmb_reserved(lmb))
699 				continue;
700 
701 			rc = dlpar_remove_lmb(lmb);
702 			if (rc)
703 				pr_err("Failed to remove LMB, drc index %x\n",
704 				       lmb->drc_index);
705 			else
706 				dlpar_release_drc(lmb->drc_index);
707 
708 			drmem_remove_lmb_reservation(lmb);
709 		}
710 		rc = -EINVAL;
711 	} else {
712 		for_each_drmem_lmb(lmb) {
713 			if (!drmem_lmb_reserved(lmb))
714 				continue;
715 
716 			pr_debug("Memory at %llx (drc index %x) was hot-added\n",
717 				 lmb->base_addr, lmb->drc_index);
718 			drmem_remove_lmb_reservation(lmb);
719 		}
720 		rc = 0;
721 	}
722 
723 	return rc;
724 }
725 
726 static int dlpar_memory_add_by_index(u32 drc_index)
727 {
728 	struct drmem_lmb *lmb;
729 	int rc, lmb_found;
730 
731 	pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
732 
733 	lmb_found = 0;
734 	for_each_drmem_lmb(lmb) {
735 		if (lmb->drc_index == drc_index) {
736 			lmb_found = 1;
737 			rc = dlpar_acquire_drc(lmb->drc_index);
738 			if (!rc) {
739 				rc = dlpar_add_lmb(lmb);
740 				if (rc)
741 					dlpar_release_drc(lmb->drc_index);
742 			}
743 
744 			break;
745 		}
746 	}
747 
748 	if (!lmb_found)
749 		rc = -EINVAL;
750 
751 	if (rc)
752 		pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
753 	else
754 		pr_info("Memory at %llx (drc index %x) was hot-added\n",
755 			lmb->base_addr, drc_index);
756 
757 	return rc;
758 }
759 
760 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
761 {
762 	struct drmem_lmb *lmb, *start_lmb, *end_lmb;
763 	int lmbs_available = 0;
764 	int rc;
765 
766 	pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
767 		lmbs_to_add, drc_index);
768 
769 	if (lmbs_to_add == 0)
770 		return -EINVAL;
771 
772 	rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
773 	if (rc)
774 		return -EINVAL;
775 
776 	/* Validate that the LMBs in this range are not reserved */
777 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
778 		if (lmb->flags & DRCONF_MEM_RESERVED)
779 			break;
780 
781 		lmbs_available++;
782 	}
783 
784 	if (lmbs_available < lmbs_to_add)
785 		return -EINVAL;
786 
787 	for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
788 		if (lmb->flags & DRCONF_MEM_ASSIGNED)
789 			continue;
790 
791 		rc = dlpar_acquire_drc(lmb->drc_index);
792 		if (rc)
793 			break;
794 
795 		rc = dlpar_add_lmb(lmb);
796 		if (rc) {
797 			dlpar_release_drc(lmb->drc_index);
798 			break;
799 		}
800 
801 		drmem_mark_lmb_reserved(lmb);
802 	}
803 
804 	if (rc) {
805 		pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
806 
807 		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
808 			if (!drmem_lmb_reserved(lmb))
809 				continue;
810 
811 			rc = dlpar_remove_lmb(lmb);
812 			if (rc)
813 				pr_err("Failed to remove LMB, drc index %x\n",
814 				       lmb->drc_index);
815 			else
816 				dlpar_release_drc(lmb->drc_index);
817 
818 			drmem_remove_lmb_reservation(lmb);
819 		}
820 		rc = -EINVAL;
821 	} else {
822 		for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
823 			if (!drmem_lmb_reserved(lmb))
824 				continue;
825 
826 			pr_info("Memory at %llx (drc index %x) was hot-added\n",
827 				lmb->base_addr, lmb->drc_index);
828 			drmem_remove_lmb_reservation(lmb);
829 		}
830 	}
831 
832 	return rc;
833 }
834 
835 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
836 {
837 	u32 count, drc_index;
838 	int rc;
839 
840 	lock_device_hotplug();
841 
842 	switch (hp_elog->action) {
843 	case PSERIES_HP_ELOG_ACTION_ADD:
844 		switch (hp_elog->id_type) {
845 		case PSERIES_HP_ELOG_ID_DRC_COUNT:
846 			count = hp_elog->_drc_u.drc_count;
847 			rc = dlpar_memory_add_by_count(count);
848 			break;
849 		case PSERIES_HP_ELOG_ID_DRC_INDEX:
850 			drc_index = hp_elog->_drc_u.drc_index;
851 			rc = dlpar_memory_add_by_index(drc_index);
852 			break;
853 		case PSERIES_HP_ELOG_ID_DRC_IC:
854 			count = hp_elog->_drc_u.ic.count;
855 			drc_index = hp_elog->_drc_u.ic.index;
856 			rc = dlpar_memory_add_by_ic(count, drc_index);
857 			break;
858 		default:
859 			rc = -EINVAL;
860 			break;
861 		}
862 
863 		break;
864 	case PSERIES_HP_ELOG_ACTION_REMOVE:
865 		switch (hp_elog->id_type) {
866 		case PSERIES_HP_ELOG_ID_DRC_COUNT:
867 			count = hp_elog->_drc_u.drc_count;
868 			rc = dlpar_memory_remove_by_count(count);
869 			break;
870 		case PSERIES_HP_ELOG_ID_DRC_INDEX:
871 			drc_index = hp_elog->_drc_u.drc_index;
872 			rc = dlpar_memory_remove_by_index(drc_index);
873 			break;
874 		case PSERIES_HP_ELOG_ID_DRC_IC:
875 			count = hp_elog->_drc_u.ic.count;
876 			drc_index = hp_elog->_drc_u.ic.index;
877 			rc = dlpar_memory_remove_by_ic(count, drc_index);
878 			break;
879 		default:
880 			rc = -EINVAL;
881 			break;
882 		}
883 
884 		break;
885 	default:
886 		pr_err("Invalid action (%d) specified\n", hp_elog->action);
887 		rc = -EINVAL;
888 		break;
889 	}
890 
891 	if (!rc)
892 		rc = drmem_update_dt();
893 
894 	unlock_device_hotplug();
895 	return rc;
896 }
897 
898 static int pseries_add_mem_node(struct device_node *np)
899 {
900 	const __be32 *prop;
901 	unsigned long base;
902 	unsigned long lmb_size;
903 	int ret = -EINVAL;
904 	int addr_cells, size_cells;
905 
906 	/*
907 	 * Check to see if we are actually adding memory
908 	 */
909 	if (!of_node_is_type(np, "memory"))
910 		return 0;
911 
912 	/*
913 	 * Find the base and size of the memblock
914 	 */
915 	prop = of_get_property(np, "reg", NULL);
916 	if (!prop)
917 		return ret;
918 
919 	addr_cells = of_n_addr_cells(np);
920 	size_cells = of_n_size_cells(np);
921 	/*
922 	 * "reg" property represents (addr,size) tuple.
923 	 */
924 	base = of_read_number(prop, addr_cells);
925 	prop += addr_cells;
926 	lmb_size = of_read_number(prop, size_cells);
927 
928 	/*
929 	 * Update memory region to represent the memory add
930 	 */
931 	ret = memblock_add(base, lmb_size);
932 	return (ret < 0) ? -EINVAL : 0;
933 }
934 
935 static int pseries_memory_notifier(struct notifier_block *nb,
936 				   unsigned long action, void *data)
937 {
938 	struct of_reconfig_data *rd = data;
939 	int err = 0;
940 
941 	switch (action) {
942 	case OF_RECONFIG_ATTACH_NODE:
943 		err = pseries_add_mem_node(rd->dn);
944 		break;
945 	case OF_RECONFIG_DETACH_NODE:
946 		err = pseries_remove_mem_node(rd->dn);
947 		break;
948 	}
949 	return notifier_from_errno(err);
950 }
951 
952 static struct notifier_block pseries_mem_nb = {
953 	.notifier_call = pseries_memory_notifier,
954 };
955 
956 static int __init pseries_memory_hotplug_init(void)
957 {
958 	if (firmware_has_feature(FW_FEATURE_LPAR))
959 		of_reconfig_notifier_register(&pseries_mem_nb);
960 
961 	return 0;
962 }
963 machine_device_initcall(pseries, pseries_memory_hotplug_init);
964