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