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
pseries_memory_block_size(void)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
dlpar_free_property(struct property * prop)73 static void dlpar_free_property(struct property *prop)
74 {
75 kfree(prop->name);
76 kfree(prop->value);
77 kfree(prop);
78 }
79
dlpar_clone_property(struct property * prop,u32 prop_size)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
find_aa_index(struct device_node * dr_node,struct property * ala_prop,const u32 * lmb_assoc,u32 * aa_index)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
update_lmb_associativity_index(struct drmem_lmb * lmb)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 update_numa_distance(lmb_node);
184
185 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
186 if (!dr_node) {
187 dlpar_free_cc_nodes(lmb_node);
188 return -ENODEV;
189 }
190
191 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
192 NULL);
193 if (!ala_prop) {
194 of_node_put(dr_node);
195 dlpar_free_cc_nodes(lmb_node);
196 return -ENODEV;
197 }
198
199 found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
200
201 of_node_put(dr_node);
202 dlpar_free_cc_nodes(lmb_node);
203
204 if (!found) {
205 pr_err("Could not find LMB associativity\n");
206 return -1;
207 }
208
209 lmb->aa_index = aa_index;
210 return 0;
211 }
212
lmb_to_memblock(struct drmem_lmb * lmb)213 static struct memory_block *lmb_to_memblock(struct drmem_lmb *lmb)
214 {
215 unsigned long section_nr;
216 struct mem_section *mem_sect;
217 struct memory_block *mem_block;
218
219 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
220 mem_sect = __nr_to_section(section_nr);
221
222 mem_block = find_memory_block(mem_sect);
223 return mem_block;
224 }
225
get_lmb_range(u32 drc_index,int n_lmbs,struct drmem_lmb ** start_lmb,struct drmem_lmb ** end_lmb)226 static int get_lmb_range(u32 drc_index, int n_lmbs,
227 struct drmem_lmb **start_lmb,
228 struct drmem_lmb **end_lmb)
229 {
230 struct drmem_lmb *lmb, *start, *end;
231 struct drmem_lmb *limit;
232
233 start = NULL;
234 for_each_drmem_lmb(lmb) {
235 if (lmb->drc_index == drc_index) {
236 start = lmb;
237 break;
238 }
239 }
240
241 if (!start)
242 return -EINVAL;
243
244 end = &start[n_lmbs];
245
246 limit = &drmem_info->lmbs[drmem_info->n_lmbs];
247 if (end > limit)
248 return -EINVAL;
249
250 *start_lmb = start;
251 *end_lmb = end;
252 return 0;
253 }
254
dlpar_change_lmb_state(struct drmem_lmb * lmb,bool online)255 static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
256 {
257 struct memory_block *mem_block;
258 int rc;
259
260 mem_block = lmb_to_memblock(lmb);
261 if (!mem_block)
262 return -EINVAL;
263
264 if (online && mem_block->dev.offline)
265 rc = device_online(&mem_block->dev);
266 else if (!online && !mem_block->dev.offline)
267 rc = device_offline(&mem_block->dev);
268 else
269 rc = 0;
270
271 put_device(&mem_block->dev);
272
273 return rc;
274 }
275
dlpar_online_lmb(struct drmem_lmb * lmb)276 static int dlpar_online_lmb(struct drmem_lmb *lmb)
277 {
278 return dlpar_change_lmb_state(lmb, true);
279 }
280
281 #ifdef CONFIG_MEMORY_HOTREMOVE
dlpar_offline_lmb(struct drmem_lmb * lmb)282 static int dlpar_offline_lmb(struct drmem_lmb *lmb)
283 {
284 return dlpar_change_lmb_state(lmb, false);
285 }
286
pseries_remove_memblock(unsigned long base,unsigned long memblock_size)287 static int pseries_remove_memblock(unsigned long base, unsigned long memblock_size)
288 {
289 unsigned long block_sz, start_pfn;
290 int sections_per_block;
291 int i, nid;
292
293 start_pfn = base >> PAGE_SHIFT;
294
295 lock_device_hotplug();
296
297 if (!pfn_valid(start_pfn))
298 goto out;
299
300 block_sz = pseries_memory_block_size();
301 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
302 nid = memory_add_physaddr_to_nid(base);
303
304 for (i = 0; i < sections_per_block; i++) {
305 __remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
306 base += MIN_MEMORY_BLOCK_SIZE;
307 }
308
309 out:
310 /* Update memory regions for memory remove */
311 memblock_remove(base, memblock_size);
312 unlock_device_hotplug();
313 return 0;
314 }
315
pseries_remove_mem_node(struct device_node * np)316 static int pseries_remove_mem_node(struct device_node *np)
317 {
318 const __be32 *prop;
319 unsigned long base;
320 unsigned long lmb_size;
321 int ret = -EINVAL;
322 int addr_cells, size_cells;
323
324 /*
325 * Check to see if we are actually removing memory
326 */
327 if (!of_node_is_type(np, "memory"))
328 return 0;
329
330 /*
331 * Find the base address and size of the memblock
332 */
333 prop = of_get_property(np, "reg", NULL);
334 if (!prop)
335 return ret;
336
337 addr_cells = of_n_addr_cells(np);
338 size_cells = of_n_size_cells(np);
339
340 /*
341 * "reg" property represents (addr,size) tuple.
342 */
343 base = of_read_number(prop, addr_cells);
344 prop += addr_cells;
345 lmb_size = of_read_number(prop, size_cells);
346
347 pseries_remove_memblock(base, lmb_size);
348 return 0;
349 }
350
lmb_is_removable(struct drmem_lmb * lmb)351 static bool lmb_is_removable(struct drmem_lmb *lmb)
352 {
353 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
354 return false;
355
356 #ifdef CONFIG_FA_DUMP
357 /*
358 * Don't hot-remove memory that falls in fadump boot memory area
359 * and memory that is reserved for capturing old kernel memory.
360 */
361 if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
362 return false;
363 #endif
364 /* device_offline() will determine if we can actually remove this lmb */
365 return true;
366 }
367
368 static int dlpar_add_lmb(struct drmem_lmb *);
369
dlpar_remove_lmb(struct drmem_lmb * lmb)370 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
371 {
372 struct memory_block *mem_block;
373 unsigned long block_sz;
374 int rc;
375
376 if (!lmb_is_removable(lmb))
377 return -EINVAL;
378
379 mem_block = lmb_to_memblock(lmb);
380 if (mem_block == NULL)
381 return -EINVAL;
382
383 rc = dlpar_offline_lmb(lmb);
384 if (rc) {
385 put_device(&mem_block->dev);
386 return rc;
387 }
388
389 block_sz = pseries_memory_block_size();
390
391 __remove_memory(mem_block->nid, lmb->base_addr, block_sz);
392 put_device(&mem_block->dev);
393
394 /* Update memory regions for memory remove */
395 memblock_remove(lmb->base_addr, block_sz);
396
397 invalidate_lmb_associativity_index(lmb);
398 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
399
400 return 0;
401 }
402
dlpar_memory_remove_by_count(u32 lmbs_to_remove)403 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
404 {
405 struct drmem_lmb *lmb;
406 int lmbs_removed = 0;
407 int lmbs_available = 0;
408 int rc;
409
410 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
411
412 if (lmbs_to_remove == 0)
413 return -EINVAL;
414
415 /* Validate that there are enough LMBs to satisfy the request */
416 for_each_drmem_lmb(lmb) {
417 if (lmb_is_removable(lmb))
418 lmbs_available++;
419
420 if (lmbs_available == lmbs_to_remove)
421 break;
422 }
423
424 if (lmbs_available < lmbs_to_remove) {
425 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
426 lmbs_available, lmbs_to_remove);
427 return -EINVAL;
428 }
429
430 for_each_drmem_lmb(lmb) {
431 rc = dlpar_remove_lmb(lmb);
432 if (rc)
433 continue;
434
435 /* Mark this lmb so we can add it later if all of the
436 * requested LMBs cannot be removed.
437 */
438 drmem_mark_lmb_reserved(lmb);
439
440 lmbs_removed++;
441 if (lmbs_removed == lmbs_to_remove)
442 break;
443 }
444
445 if (lmbs_removed != lmbs_to_remove) {
446 pr_err("Memory hot-remove failed, adding LMB's back\n");
447
448 for_each_drmem_lmb(lmb) {
449 if (!drmem_lmb_reserved(lmb))
450 continue;
451
452 rc = dlpar_add_lmb(lmb);
453 if (rc)
454 pr_err("Failed to add LMB back, drc index %x\n",
455 lmb->drc_index);
456
457 drmem_remove_lmb_reservation(lmb);
458 }
459
460 rc = -EINVAL;
461 } else {
462 for_each_drmem_lmb(lmb) {
463 if (!drmem_lmb_reserved(lmb))
464 continue;
465
466 dlpar_release_drc(lmb->drc_index);
467 pr_info("Memory at %llx was hot-removed\n",
468 lmb->base_addr);
469
470 drmem_remove_lmb_reservation(lmb);
471 }
472 rc = 0;
473 }
474
475 return rc;
476 }
477
dlpar_memory_remove_by_index(u32 drc_index)478 static int dlpar_memory_remove_by_index(u32 drc_index)
479 {
480 struct drmem_lmb *lmb;
481 int lmb_found;
482 int rc;
483
484 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
485
486 lmb_found = 0;
487 for_each_drmem_lmb(lmb) {
488 if (lmb->drc_index == drc_index) {
489 lmb_found = 1;
490 rc = dlpar_remove_lmb(lmb);
491 if (!rc)
492 dlpar_release_drc(lmb->drc_index);
493
494 break;
495 }
496 }
497
498 if (!lmb_found)
499 rc = -EINVAL;
500
501 if (rc)
502 pr_info("Failed to hot-remove memory at %llx\n",
503 lmb->base_addr);
504 else
505 pr_info("Memory at %llx was hot-removed\n", lmb->base_addr);
506
507 return rc;
508 }
509
dlpar_memory_remove_by_ic(u32 lmbs_to_remove,u32 drc_index)510 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
511 {
512 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
513 int lmbs_available = 0;
514 int rc;
515
516 pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
517 lmbs_to_remove, drc_index);
518
519 if (lmbs_to_remove == 0)
520 return -EINVAL;
521
522 rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
523 if (rc)
524 return -EINVAL;
525
526 /* Validate that there are enough LMBs to satisfy the request */
527 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
528 if (lmb->flags & DRCONF_MEM_RESERVED)
529 break;
530
531 lmbs_available++;
532 }
533
534 if (lmbs_available < lmbs_to_remove)
535 return -EINVAL;
536
537 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
538 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
539 continue;
540
541 rc = dlpar_remove_lmb(lmb);
542 if (rc)
543 break;
544
545 drmem_mark_lmb_reserved(lmb);
546 }
547
548 if (rc) {
549 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
550
551
552 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
553 if (!drmem_lmb_reserved(lmb))
554 continue;
555
556 rc = dlpar_add_lmb(lmb);
557 if (rc)
558 pr_err("Failed to add LMB, drc index %x\n",
559 lmb->drc_index);
560
561 drmem_remove_lmb_reservation(lmb);
562 }
563 rc = -EINVAL;
564 } else {
565 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
566 if (!drmem_lmb_reserved(lmb))
567 continue;
568
569 dlpar_release_drc(lmb->drc_index);
570 pr_info("Memory at %llx (drc index %x) was hot-removed\n",
571 lmb->base_addr, lmb->drc_index);
572
573 drmem_remove_lmb_reservation(lmb);
574 }
575 }
576
577 return rc;
578 }
579
580 #else
pseries_remove_memblock(unsigned long base,unsigned long memblock_size)581 static inline int pseries_remove_memblock(unsigned long base,
582 unsigned long memblock_size)
583 {
584 return -EOPNOTSUPP;
585 }
pseries_remove_mem_node(struct device_node * np)586 static inline int pseries_remove_mem_node(struct device_node *np)
587 {
588 return 0;
589 }
dlpar_memory_remove(struct pseries_hp_errorlog * hp_elog)590 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
591 {
592 return -EOPNOTSUPP;
593 }
dlpar_remove_lmb(struct drmem_lmb * lmb)594 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
595 {
596 return -EOPNOTSUPP;
597 }
dlpar_memory_remove_by_count(u32 lmbs_to_remove)598 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
599 {
600 return -EOPNOTSUPP;
601 }
dlpar_memory_remove_by_index(u32 drc_index)602 static int dlpar_memory_remove_by_index(u32 drc_index)
603 {
604 return -EOPNOTSUPP;
605 }
606
dlpar_memory_remove_by_ic(u32 lmbs_to_remove,u32 drc_index)607 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
608 {
609 return -EOPNOTSUPP;
610 }
611 #endif /* CONFIG_MEMORY_HOTREMOVE */
612
dlpar_add_lmb(struct drmem_lmb * lmb)613 static int dlpar_add_lmb(struct drmem_lmb *lmb)
614 {
615 unsigned long block_sz;
616 int nid, rc;
617
618 if (lmb->flags & DRCONF_MEM_ASSIGNED)
619 return -EINVAL;
620
621 rc = update_lmb_associativity_index(lmb);
622 if (rc) {
623 dlpar_release_drc(lmb->drc_index);
624 return rc;
625 }
626
627 block_sz = memory_block_size_bytes();
628
629 /* Find the node id for this LMB. Fake one if necessary. */
630 nid = of_drconf_to_nid_single(lmb);
631 if (nid < 0 || !node_possible(nid))
632 nid = first_online_node;
633
634 /* Add the memory */
635 rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
636 if (rc) {
637 invalidate_lmb_associativity_index(lmb);
638 return rc;
639 }
640
641 rc = dlpar_online_lmb(lmb);
642 if (rc) {
643 __remove_memory(nid, lmb->base_addr, block_sz);
644 invalidate_lmb_associativity_index(lmb);
645 } else {
646 lmb->flags |= DRCONF_MEM_ASSIGNED;
647 }
648
649 return rc;
650 }
651
dlpar_memory_add_by_count(u32 lmbs_to_add)652 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
653 {
654 struct drmem_lmb *lmb;
655 int lmbs_available = 0;
656 int lmbs_added = 0;
657 int rc;
658
659 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
660
661 if (lmbs_to_add == 0)
662 return -EINVAL;
663
664 /* Validate that there are enough LMBs to satisfy the request */
665 for_each_drmem_lmb(lmb) {
666 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
667 lmbs_available++;
668
669 if (lmbs_available == lmbs_to_add)
670 break;
671 }
672
673 if (lmbs_available < lmbs_to_add)
674 return -EINVAL;
675
676 for_each_drmem_lmb(lmb) {
677 if (lmb->flags & DRCONF_MEM_ASSIGNED)
678 continue;
679
680 rc = dlpar_acquire_drc(lmb->drc_index);
681 if (rc)
682 continue;
683
684 rc = dlpar_add_lmb(lmb);
685 if (rc) {
686 dlpar_release_drc(lmb->drc_index);
687 continue;
688 }
689
690 /* Mark this lmb so we can remove it later if all of the
691 * requested LMBs cannot be added.
692 */
693 drmem_mark_lmb_reserved(lmb);
694
695 lmbs_added++;
696 if (lmbs_added == lmbs_to_add)
697 break;
698 }
699
700 if (lmbs_added != lmbs_to_add) {
701 pr_err("Memory hot-add failed, removing any added LMBs\n");
702
703 for_each_drmem_lmb(lmb) {
704 if (!drmem_lmb_reserved(lmb))
705 continue;
706
707 rc = dlpar_remove_lmb(lmb);
708 if (rc)
709 pr_err("Failed to remove LMB, drc index %x\n",
710 lmb->drc_index);
711 else
712 dlpar_release_drc(lmb->drc_index);
713
714 drmem_remove_lmb_reservation(lmb);
715 }
716 rc = -EINVAL;
717 } else {
718 for_each_drmem_lmb(lmb) {
719 if (!drmem_lmb_reserved(lmb))
720 continue;
721
722 pr_info("Memory at %llx (drc index %x) was hot-added\n",
723 lmb->base_addr, lmb->drc_index);
724 drmem_remove_lmb_reservation(lmb);
725 }
726 rc = 0;
727 }
728
729 return rc;
730 }
731
dlpar_memory_add_by_index(u32 drc_index)732 static int dlpar_memory_add_by_index(u32 drc_index)
733 {
734 struct drmem_lmb *lmb;
735 int rc, lmb_found;
736
737 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
738
739 lmb_found = 0;
740 for_each_drmem_lmb(lmb) {
741 if (lmb->drc_index == drc_index) {
742 lmb_found = 1;
743 rc = dlpar_acquire_drc(lmb->drc_index);
744 if (!rc) {
745 rc = dlpar_add_lmb(lmb);
746 if (rc)
747 dlpar_release_drc(lmb->drc_index);
748 }
749
750 break;
751 }
752 }
753
754 if (!lmb_found)
755 rc = -EINVAL;
756
757 if (rc)
758 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
759 else
760 pr_info("Memory at %llx (drc index %x) was hot-added\n",
761 lmb->base_addr, drc_index);
762
763 return rc;
764 }
765
dlpar_memory_add_by_ic(u32 lmbs_to_add,u32 drc_index)766 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
767 {
768 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
769 int lmbs_available = 0;
770 int rc;
771
772 pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
773 lmbs_to_add, drc_index);
774
775 if (lmbs_to_add == 0)
776 return -EINVAL;
777
778 rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
779 if (rc)
780 return -EINVAL;
781
782 /* Validate that the LMBs in this range are not reserved */
783 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
784 if (lmb->flags & DRCONF_MEM_RESERVED)
785 break;
786
787 lmbs_available++;
788 }
789
790 if (lmbs_available < lmbs_to_add)
791 return -EINVAL;
792
793 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
794 if (lmb->flags & DRCONF_MEM_ASSIGNED)
795 continue;
796
797 rc = dlpar_acquire_drc(lmb->drc_index);
798 if (rc)
799 break;
800
801 rc = dlpar_add_lmb(lmb);
802 if (rc) {
803 dlpar_release_drc(lmb->drc_index);
804 break;
805 }
806
807 drmem_mark_lmb_reserved(lmb);
808 }
809
810 if (rc) {
811 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
812
813 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
814 if (!drmem_lmb_reserved(lmb))
815 continue;
816
817 rc = dlpar_remove_lmb(lmb);
818 if (rc)
819 pr_err("Failed to remove LMB, drc index %x\n",
820 lmb->drc_index);
821 else
822 dlpar_release_drc(lmb->drc_index);
823
824 drmem_remove_lmb_reservation(lmb);
825 }
826 rc = -EINVAL;
827 } else {
828 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
829 if (!drmem_lmb_reserved(lmb))
830 continue;
831
832 pr_info("Memory at %llx (drc index %x) was hot-added\n",
833 lmb->base_addr, lmb->drc_index);
834 drmem_remove_lmb_reservation(lmb);
835 }
836 }
837
838 return rc;
839 }
840
dlpar_memory(struct pseries_hp_errorlog * hp_elog)841 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
842 {
843 u32 count, drc_index;
844 int rc;
845
846 lock_device_hotplug();
847
848 switch (hp_elog->action) {
849 case PSERIES_HP_ELOG_ACTION_ADD:
850 switch (hp_elog->id_type) {
851 case PSERIES_HP_ELOG_ID_DRC_COUNT:
852 count = hp_elog->_drc_u.drc_count;
853 rc = dlpar_memory_add_by_count(count);
854 break;
855 case PSERIES_HP_ELOG_ID_DRC_INDEX:
856 drc_index = hp_elog->_drc_u.drc_index;
857 rc = dlpar_memory_add_by_index(drc_index);
858 break;
859 case PSERIES_HP_ELOG_ID_DRC_IC:
860 count = hp_elog->_drc_u.ic.count;
861 drc_index = hp_elog->_drc_u.ic.index;
862 rc = dlpar_memory_add_by_ic(count, drc_index);
863 break;
864 default:
865 rc = -EINVAL;
866 break;
867 }
868
869 break;
870 case PSERIES_HP_ELOG_ACTION_REMOVE:
871 switch (hp_elog->id_type) {
872 case PSERIES_HP_ELOG_ID_DRC_COUNT:
873 count = hp_elog->_drc_u.drc_count;
874 rc = dlpar_memory_remove_by_count(count);
875 break;
876 case PSERIES_HP_ELOG_ID_DRC_INDEX:
877 drc_index = hp_elog->_drc_u.drc_index;
878 rc = dlpar_memory_remove_by_index(drc_index);
879 break;
880 case PSERIES_HP_ELOG_ID_DRC_IC:
881 count = hp_elog->_drc_u.ic.count;
882 drc_index = hp_elog->_drc_u.ic.index;
883 rc = dlpar_memory_remove_by_ic(count, drc_index);
884 break;
885 default:
886 rc = -EINVAL;
887 break;
888 }
889
890 break;
891 default:
892 pr_err("Invalid action (%d) specified\n", hp_elog->action);
893 rc = -EINVAL;
894 break;
895 }
896
897 if (!rc)
898 rc = drmem_update_dt();
899
900 unlock_device_hotplug();
901 return rc;
902 }
903
pseries_add_mem_node(struct device_node * np)904 static int pseries_add_mem_node(struct device_node *np)
905 {
906 const __be32 *prop;
907 unsigned long base;
908 unsigned long lmb_size;
909 int ret = -EINVAL;
910 int addr_cells, size_cells;
911
912 /*
913 * Check to see if we are actually adding memory
914 */
915 if (!of_node_is_type(np, "memory"))
916 return 0;
917
918 /*
919 * Find the base and size of the memblock
920 */
921 prop = of_get_property(np, "reg", NULL);
922 if (!prop)
923 return ret;
924
925 addr_cells = of_n_addr_cells(np);
926 size_cells = of_n_size_cells(np);
927 /*
928 * "reg" property represents (addr,size) tuple.
929 */
930 base = of_read_number(prop, addr_cells);
931 prop += addr_cells;
932 lmb_size = of_read_number(prop, size_cells);
933
934 /*
935 * Update memory region to represent the memory add
936 */
937 ret = memblock_add(base, lmb_size);
938 return (ret < 0) ? -EINVAL : 0;
939 }
940
pseries_memory_notifier(struct notifier_block * nb,unsigned long action,void * data)941 static int pseries_memory_notifier(struct notifier_block *nb,
942 unsigned long action, void *data)
943 {
944 struct of_reconfig_data *rd = data;
945 int err = 0;
946
947 switch (action) {
948 case OF_RECONFIG_ATTACH_NODE:
949 err = pseries_add_mem_node(rd->dn);
950 break;
951 case OF_RECONFIG_DETACH_NODE:
952 err = pseries_remove_mem_node(rd->dn);
953 break;
954 }
955 return notifier_from_errno(err);
956 }
957
958 static struct notifier_block pseries_mem_nb = {
959 .notifier_call = pseries_memory_notifier,
960 };
961
pseries_memory_hotplug_init(void)962 static int __init pseries_memory_hotplug_init(void)
963 {
964 if (firmware_has_feature(FW_FEATURE_LPAR))
965 of_reconfig_notifier_register(&pseries_mem_nb);
966
967 return 0;
968 }
969 machine_device_initcall(pseries, pseries_memory_hotplug_init);
970