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