GNU Linux-libre 5.4.274-gnu1
[releases.git] / arch / powerpc / platforms / pseries / hotplug-memory.c
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         u64 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 *limit;
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];
240
241         limit = &drmem_info->lmbs[drmem_info->n_lmbs];
242         if (end > limit)
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 long 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 *prop;
314         unsigned long base;
315         unsigned long lmb_size;
316         int ret = -EINVAL;
317         int addr_cells, size_cells;
318
319         /*
320          * Check to see if we are actually removing memory
321          */
322         if (!of_node_is_type(np, "memory"))
323                 return 0;
324
325         /*
326          * Find the base address and size of the memblock
327          */
328         prop = of_get_property(np, "reg", NULL);
329         if (!prop)
330                 return ret;
331
332         addr_cells = of_n_addr_cells(np);
333         size_cells = of_n_size_cells(np);
334
335         /*
336          * "reg" property represents (addr,size) tuple.
337          */
338         base = of_read_number(prop, addr_cells);
339         prop += addr_cells;
340         lmb_size = of_read_number(prop, size_cells);
341
342         pseries_remove_memblock(base, lmb_size);
343         return 0;
344 }
345
346 static bool lmb_is_removable(struct drmem_lmb *lmb)
347 {
348         int i, scns_per_block;
349         int rc = 1;
350         unsigned long pfn, block_sz;
351         u64 phys_addr;
352
353         if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
354                 return false;
355
356         block_sz = memory_block_size_bytes();
357         scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
358         phys_addr = lmb->base_addr;
359
360 #ifdef CONFIG_FA_DUMP
361         /*
362          * Don't hot-remove memory that falls in fadump boot memory area
363          * and memory that is reserved for capturing old kernel memory.
364          */
365         if (is_fadump_memory_area(phys_addr, block_sz))
366                 return false;
367 #endif
368
369         for (i = 0; i < scns_per_block; i++) {
370                 pfn = PFN_DOWN(phys_addr);
371                 if (!pfn_present(pfn)) {
372                         phys_addr += MIN_MEMORY_BLOCK_SIZE;
373                         continue;
374                 }
375
376                 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
377                 phys_addr += MIN_MEMORY_BLOCK_SIZE;
378         }
379
380         return rc ? true : false;
381 }
382
383 static int dlpar_add_lmb(struct drmem_lmb *);
384
385 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
386 {
387         struct memory_block *mem_block;
388         unsigned long block_sz;
389         int rc;
390
391         if (!lmb_is_removable(lmb))
392                 return -EINVAL;
393
394         mem_block = lmb_to_memblock(lmb);
395         if (mem_block == NULL)
396                 return -EINVAL;
397
398         rc = dlpar_offline_lmb(lmb);
399         if (rc) {
400                 put_device(&mem_block->dev);
401                 return rc;
402         }
403
404         block_sz = pseries_memory_block_size();
405
406         __remove_memory(mem_block->nid, lmb->base_addr, block_sz);
407         put_device(&mem_block->dev);
408
409         /* Update memory regions for memory remove */
410         memblock_remove(lmb->base_addr, block_sz);
411
412         invalidate_lmb_associativity_index(lmb);
413         lmb->flags &= ~DRCONF_MEM_ASSIGNED;
414
415         return 0;
416 }
417
418 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
419 {
420         struct drmem_lmb *lmb;
421         int lmbs_removed = 0;
422         int lmbs_available = 0;
423         int rc;
424
425         pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
426
427         if (lmbs_to_remove == 0)
428                 return -EINVAL;
429
430         /* Validate that there are enough LMBs to satisfy the request */
431         for_each_drmem_lmb(lmb) {
432                 if (lmb_is_removable(lmb))
433                         lmbs_available++;
434
435                 if (lmbs_available == lmbs_to_remove)
436                         break;
437         }
438
439         if (lmbs_available < lmbs_to_remove) {
440                 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
441                         lmbs_available, lmbs_to_remove);
442                 return -EINVAL;
443         }
444
445         for_each_drmem_lmb(lmb) {
446                 rc = dlpar_remove_lmb(lmb);
447                 if (rc)
448                         continue;
449
450                 /* Mark this lmb so we can add it later if all of the
451                  * requested LMBs cannot be removed.
452                  */
453                 drmem_mark_lmb_reserved(lmb);
454
455                 lmbs_removed++;
456                 if (lmbs_removed == lmbs_to_remove)
457                         break;
458         }
459
460         if (lmbs_removed != lmbs_to_remove) {
461                 pr_err("Memory hot-remove failed, adding LMB's back\n");
462
463                 for_each_drmem_lmb(lmb) {
464                         if (!drmem_lmb_reserved(lmb))
465                                 continue;
466
467                         rc = dlpar_add_lmb(lmb);
468                         if (rc)
469                                 pr_err("Failed to add LMB back, drc index %x\n",
470                                        lmb->drc_index);
471
472                         drmem_remove_lmb_reservation(lmb);
473                 }
474
475                 rc = -EINVAL;
476         } else {
477                 for_each_drmem_lmb(lmb) {
478                         if (!drmem_lmb_reserved(lmb))
479                                 continue;
480
481                         dlpar_release_drc(lmb->drc_index);
482                         pr_info("Memory at %llx was hot-removed\n",
483                                 lmb->base_addr);
484
485                         drmem_remove_lmb_reservation(lmb);
486                 }
487                 rc = 0;
488         }
489
490         return rc;
491 }
492
493 static int dlpar_memory_remove_by_index(u32 drc_index)
494 {
495         struct drmem_lmb *lmb;
496         int lmb_found;
497         int rc;
498
499         pr_debug("Attempting to hot-remove LMB, drc index %x\n", drc_index);
500
501         lmb_found = 0;
502         for_each_drmem_lmb(lmb) {
503                 if (lmb->drc_index == drc_index) {
504                         lmb_found = 1;
505                         rc = dlpar_remove_lmb(lmb);
506                         if (!rc)
507                                 dlpar_release_drc(lmb->drc_index);
508
509                         break;
510                 }
511         }
512
513         if (!lmb_found) {
514                 pr_debug("Failed to look up LMB for drc index %x\n", drc_index);
515                 rc = -EINVAL;
516         } else if (rc) {
517                 pr_debug("Failed to hot-remove memory at %llx\n",
518                          lmb->base_addr);
519         } else {
520                 pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
521         }
522
523         return rc;
524 }
525
526 static int dlpar_memory_readd_by_index(u32 drc_index)
527 {
528         struct drmem_lmb *lmb;
529         int lmb_found;
530         int rc;
531
532         pr_info("Attempting to update LMB, drc index %x\n", drc_index);
533
534         lmb_found = 0;
535         for_each_drmem_lmb(lmb) {
536                 if (lmb->drc_index == drc_index) {
537                         lmb_found = 1;
538                         rc = dlpar_remove_lmb(lmb);
539                         if (!rc) {
540                                 rc = dlpar_add_lmb(lmb);
541                                 if (rc)
542                                         dlpar_release_drc(lmb->drc_index);
543                         }
544                         break;
545                 }
546         }
547
548         if (!lmb_found)
549                 rc = -EINVAL;
550
551         if (rc)
552                 pr_info("Failed to update memory at %llx\n",
553                         lmb->base_addr);
554         else
555                 pr_info("Memory at %llx was updated\n", lmb->base_addr);
556
557         return rc;
558 }
559
560 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
561 {
562         struct drmem_lmb *lmb, *start_lmb, *end_lmb;
563         int lmbs_available = 0;
564         int rc;
565
566         pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
567                 lmbs_to_remove, drc_index);
568
569         if (lmbs_to_remove == 0)
570                 return -EINVAL;
571
572         rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
573         if (rc)
574                 return -EINVAL;
575
576         /* Validate that there are enough LMBs to satisfy the request */
577         for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
578                 if (lmb->flags & DRCONF_MEM_RESERVED)
579                         break;
580
581                 lmbs_available++;
582         }
583
584         if (lmbs_available < lmbs_to_remove)
585                 return -EINVAL;
586
587         for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
588                 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
589                         continue;
590
591                 rc = dlpar_remove_lmb(lmb);
592                 if (rc)
593                         break;
594
595                 drmem_mark_lmb_reserved(lmb);
596         }
597
598         if (rc) {
599                 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
600
601
602                 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
603                         if (!drmem_lmb_reserved(lmb))
604                                 continue;
605
606                         rc = dlpar_add_lmb(lmb);
607                         if (rc)
608                                 pr_err("Failed to add LMB, drc index %x\n",
609                                        lmb->drc_index);
610
611                         drmem_remove_lmb_reservation(lmb);
612                 }
613                 rc = -EINVAL;
614         } else {
615                 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
616                         if (!drmem_lmb_reserved(lmb))
617                                 continue;
618
619                         dlpar_release_drc(lmb->drc_index);
620                         pr_info("Memory at %llx (drc index %x) was hot-removed\n",
621                                 lmb->base_addr, lmb->drc_index);
622
623                         drmem_remove_lmb_reservation(lmb);
624                 }
625         }
626
627         return rc;
628 }
629
630 #else
631 static inline int pseries_remove_memblock(unsigned long base,
632                                           unsigned long memblock_size)
633 {
634         return -EOPNOTSUPP;
635 }
636 static inline int pseries_remove_mem_node(struct device_node *np)
637 {
638         return 0;
639 }
640 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
641 {
642         return -EOPNOTSUPP;
643 }
644 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
645 {
646         return -EOPNOTSUPP;
647 }
648 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
649 {
650         return -EOPNOTSUPP;
651 }
652 static int dlpar_memory_remove_by_index(u32 drc_index)
653 {
654         return -EOPNOTSUPP;
655 }
656 static int dlpar_memory_readd_by_index(u32 drc_index)
657 {
658         return -EOPNOTSUPP;
659 }
660
661 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
662 {
663         return -EOPNOTSUPP;
664 }
665 #endif /* CONFIG_MEMORY_HOTREMOVE */
666
667 static int dlpar_add_lmb(struct drmem_lmb *lmb)
668 {
669         unsigned long block_sz;
670         int nid, rc;
671
672         if (lmb->flags & DRCONF_MEM_ASSIGNED)
673                 return -EINVAL;
674
675         rc = update_lmb_associativity_index(lmb);
676         if (rc) {
677                 dlpar_release_drc(lmb->drc_index);
678                 return rc;
679         }
680
681         block_sz = memory_block_size_bytes();
682
683         /* Find the node id for this address. */
684         nid = memory_add_physaddr_to_nid(lmb->base_addr);
685
686         /* Add the memory */
687         rc = __add_memory(nid, lmb->base_addr, block_sz);
688         if (rc) {
689                 invalidate_lmb_associativity_index(lmb);
690                 return rc;
691         }
692
693         rc = dlpar_online_lmb(lmb);
694         if (rc) {
695                 __remove_memory(nid, lmb->base_addr, block_sz);
696                 invalidate_lmb_associativity_index(lmb);
697         } else {
698                 lmb->flags |= DRCONF_MEM_ASSIGNED;
699         }
700
701         return rc;
702 }
703
704 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
705 {
706         struct drmem_lmb *lmb;
707         int lmbs_available = 0;
708         int lmbs_added = 0;
709         int rc;
710
711         pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
712
713         if (lmbs_to_add == 0)
714                 return -EINVAL;
715
716         /* Validate that there are enough LMBs to satisfy the request */
717         for_each_drmem_lmb(lmb) {
718                 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
719                         lmbs_available++;
720
721                 if (lmbs_available == lmbs_to_add)
722                         break;
723         }
724
725         if (lmbs_available < lmbs_to_add)
726                 return -EINVAL;
727
728         for_each_drmem_lmb(lmb) {
729                 if (lmb->flags & DRCONF_MEM_ASSIGNED)
730                         continue;
731
732                 rc = dlpar_acquire_drc(lmb->drc_index);
733                 if (rc)
734                         continue;
735
736                 rc = dlpar_add_lmb(lmb);
737                 if (rc) {
738                         dlpar_release_drc(lmb->drc_index);
739                         continue;
740                 }
741
742                 /* Mark this lmb so we can remove it later if all of the
743                  * requested LMBs cannot be added.
744                  */
745                 drmem_mark_lmb_reserved(lmb);
746
747                 lmbs_added++;
748                 if (lmbs_added == lmbs_to_add)
749                         break;
750         }
751
752         if (lmbs_added != lmbs_to_add) {
753                 pr_err("Memory hot-add failed, removing any added LMBs\n");
754
755                 for_each_drmem_lmb(lmb) {
756                         if (!drmem_lmb_reserved(lmb))
757                                 continue;
758
759                         rc = dlpar_remove_lmb(lmb);
760                         if (rc)
761                                 pr_err("Failed to remove LMB, drc index %x\n",
762                                        lmb->drc_index);
763                         else
764                                 dlpar_release_drc(lmb->drc_index);
765
766                         drmem_remove_lmb_reservation(lmb);
767                 }
768                 rc = -EINVAL;
769         } else {
770                 for_each_drmem_lmb(lmb) {
771                         if (!drmem_lmb_reserved(lmb))
772                                 continue;
773
774                         pr_debug("Memory at %llx (drc index %x) was hot-added\n",
775                                  lmb->base_addr, lmb->drc_index);
776                         drmem_remove_lmb_reservation(lmb);
777                 }
778                 rc = 0;
779         }
780
781         return rc;
782 }
783
784 static int dlpar_memory_add_by_index(u32 drc_index)
785 {
786         struct drmem_lmb *lmb;
787         int rc, lmb_found;
788
789         pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
790
791         lmb_found = 0;
792         for_each_drmem_lmb(lmb) {
793                 if (lmb->drc_index == drc_index) {
794                         lmb_found = 1;
795                         rc = dlpar_acquire_drc(lmb->drc_index);
796                         if (!rc) {
797                                 rc = dlpar_add_lmb(lmb);
798                                 if (rc)
799                                         dlpar_release_drc(lmb->drc_index);
800                         }
801
802                         break;
803                 }
804         }
805
806         if (!lmb_found)
807                 rc = -EINVAL;
808
809         if (rc)
810                 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
811         else
812                 pr_info("Memory at %llx (drc index %x) was hot-added\n",
813                         lmb->base_addr, drc_index);
814
815         return rc;
816 }
817
818 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
819 {
820         struct drmem_lmb *lmb, *start_lmb, *end_lmb;
821         int lmbs_available = 0;
822         int rc;
823
824         pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
825                 lmbs_to_add, drc_index);
826
827         if (lmbs_to_add == 0)
828                 return -EINVAL;
829
830         rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
831         if (rc)
832                 return -EINVAL;
833
834         /* Validate that the LMBs in this range are not reserved */
835         for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
836                 if (lmb->flags & DRCONF_MEM_RESERVED)
837                         break;
838
839                 lmbs_available++;
840         }
841
842         if (lmbs_available < lmbs_to_add)
843                 return -EINVAL;
844
845         for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
846                 if (lmb->flags & DRCONF_MEM_ASSIGNED)
847                         continue;
848
849                 rc = dlpar_acquire_drc(lmb->drc_index);
850                 if (rc)
851                         break;
852
853                 rc = dlpar_add_lmb(lmb);
854                 if (rc) {
855                         dlpar_release_drc(lmb->drc_index);
856                         break;
857                 }
858
859                 drmem_mark_lmb_reserved(lmb);
860         }
861
862         if (rc) {
863                 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
864
865                 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
866                         if (!drmem_lmb_reserved(lmb))
867                                 continue;
868
869                         rc = dlpar_remove_lmb(lmb);
870                         if (rc)
871                                 pr_err("Failed to remove LMB, drc index %x\n",
872                                        lmb->drc_index);
873                         else
874                                 dlpar_release_drc(lmb->drc_index);
875
876                         drmem_remove_lmb_reservation(lmb);
877                 }
878                 rc = -EINVAL;
879         } else {
880                 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
881                         if (!drmem_lmb_reserved(lmb))
882                                 continue;
883
884                         pr_info("Memory at %llx (drc index %x) was hot-added\n",
885                                 lmb->base_addr, lmb->drc_index);
886                         drmem_remove_lmb_reservation(lmb);
887                 }
888         }
889
890         return rc;
891 }
892
893 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
894 {
895         u32 count, drc_index;
896         int rc;
897
898         lock_device_hotplug();
899
900         switch (hp_elog->action) {
901         case PSERIES_HP_ELOG_ACTION_ADD:
902                 switch (hp_elog->id_type) {
903                 case PSERIES_HP_ELOG_ID_DRC_COUNT:
904                         count = hp_elog->_drc_u.drc_count;
905                         rc = dlpar_memory_add_by_count(count);
906                         break;
907                 case PSERIES_HP_ELOG_ID_DRC_INDEX:
908                         drc_index = hp_elog->_drc_u.drc_index;
909                         rc = dlpar_memory_add_by_index(drc_index);
910                         break;
911                 case PSERIES_HP_ELOG_ID_DRC_IC:
912                         count = hp_elog->_drc_u.ic.count;
913                         drc_index = hp_elog->_drc_u.ic.index;
914                         rc = dlpar_memory_add_by_ic(count, drc_index);
915                         break;
916                 default:
917                         rc = -EINVAL;
918                         break;
919                 }
920
921                 break;
922         case PSERIES_HP_ELOG_ACTION_REMOVE:
923                 switch (hp_elog->id_type) {
924                 case PSERIES_HP_ELOG_ID_DRC_COUNT:
925                         count = hp_elog->_drc_u.drc_count;
926                         rc = dlpar_memory_remove_by_count(count);
927                         break;
928                 case PSERIES_HP_ELOG_ID_DRC_INDEX:
929                         drc_index = hp_elog->_drc_u.drc_index;
930                         rc = dlpar_memory_remove_by_index(drc_index);
931                         break;
932                 case PSERIES_HP_ELOG_ID_DRC_IC:
933                         count = hp_elog->_drc_u.ic.count;
934                         drc_index = hp_elog->_drc_u.ic.index;
935                         rc = dlpar_memory_remove_by_ic(count, drc_index);
936                         break;
937                 default:
938                         rc = -EINVAL;
939                         break;
940                 }
941
942                 break;
943         case PSERIES_HP_ELOG_ACTION_READD:
944                 drc_index = hp_elog->_drc_u.drc_index;
945                 rc = dlpar_memory_readd_by_index(drc_index);
946                 break;
947         default:
948                 pr_err("Invalid action (%d) specified\n", hp_elog->action);
949                 rc = -EINVAL;
950                 break;
951         }
952
953         if (!rc) {
954                 rtas_hp_event = true;
955                 rc = drmem_update_dt();
956                 rtas_hp_event = false;
957         }
958
959         unlock_device_hotplug();
960         return rc;
961 }
962
963 static int pseries_add_mem_node(struct device_node *np)
964 {
965         const __be32 *prop;
966         unsigned long base;
967         unsigned long lmb_size;
968         int ret = -EINVAL;
969         int addr_cells, size_cells;
970
971         /*
972          * Check to see if we are actually adding memory
973          */
974         if (!of_node_is_type(np, "memory"))
975                 return 0;
976
977         /*
978          * Find the base and size of the memblock
979          */
980         prop = of_get_property(np, "reg", NULL);
981         if (!prop)
982                 return ret;
983
984         addr_cells = of_n_addr_cells(np);
985         size_cells = of_n_size_cells(np);
986         /*
987          * "reg" property represents (addr,size) tuple.
988          */
989         base = of_read_number(prop, addr_cells);
990         prop += addr_cells;
991         lmb_size = of_read_number(prop, size_cells);
992
993         /*
994          * Update memory region to represent the memory add
995          */
996         ret = memblock_add(base, lmb_size);
997         return (ret < 0) ? -EINVAL : 0;
998 }
999
1000 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
1001 {
1002         struct of_drconf_cell_v1 *new_drmem, *old_drmem;
1003         unsigned long memblock_size;
1004         u32 entries;
1005         __be32 *p;
1006         int i, rc = -EINVAL;
1007
1008         if (rtas_hp_event)
1009                 return 0;
1010
1011         memblock_size = pseries_memory_block_size();
1012         if (!memblock_size)
1013                 return -EINVAL;
1014
1015         if (!pr->old_prop)
1016                 return 0;
1017
1018         p = (__be32 *) pr->old_prop->value;
1019         if (!p)
1020                 return -EINVAL;
1021
1022         /* The first int of the property is the number of lmb's described
1023          * by the property. This is followed by an array of of_drconf_cell
1024          * entries. Get the number of entries and skip to the array of
1025          * of_drconf_cell's.
1026          */
1027         entries = be32_to_cpu(*p++);
1028         old_drmem = (struct of_drconf_cell_v1 *)p;
1029
1030         p = (__be32 *)pr->prop->value;
1031         p++;
1032         new_drmem = (struct of_drconf_cell_v1 *)p;
1033
1034         for (i = 0; i < entries; i++) {
1035                 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
1036                     (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
1037                         rc = pseries_remove_memblock(
1038                                 be64_to_cpu(old_drmem[i].base_addr),
1039                                                      memblock_size);
1040                         break;
1041                 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
1042                             DRCONF_MEM_ASSIGNED)) &&
1043                             (be32_to_cpu(new_drmem[i].flags) &
1044                             DRCONF_MEM_ASSIGNED)) {
1045                         rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
1046                                           memblock_size);
1047                         rc = (rc < 0) ? -EINVAL : 0;
1048                         break;
1049                 }
1050         }
1051         return rc;
1052 }
1053
1054 static int pseries_memory_notifier(struct notifier_block *nb,
1055                                    unsigned long action, void *data)
1056 {
1057         struct of_reconfig_data *rd = data;
1058         int err = 0;
1059
1060         switch (action) {
1061         case OF_RECONFIG_ATTACH_NODE:
1062                 err = pseries_add_mem_node(rd->dn);
1063                 break;
1064         case OF_RECONFIG_DETACH_NODE:
1065                 err = pseries_remove_mem_node(rd->dn);
1066                 break;
1067         case OF_RECONFIG_UPDATE_PROPERTY:
1068                 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
1069                         err = pseries_update_drconf_memory(rd);
1070                 break;
1071         }
1072         return notifier_from_errno(err);
1073 }
1074
1075 static struct notifier_block pseries_mem_nb = {
1076         .notifier_call = pseries_memory_notifier,
1077 };
1078
1079 static int __init pseries_memory_hotplug_init(void)
1080 {
1081         if (firmware_has_feature(FW_FEATURE_LPAR))
1082                 of_reconfig_notifier_register(&pseries_mem_nb);
1083
1084         return 0;
1085 }
1086 machine_device_initcall(pseries, pseries_memory_hotplug_init);