GNU Linux-libre 4.9.330-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 "pseries.h"
26
27 static bool rtas_hp_event;
28
29 unsigned long pseries_memory_block_size(void)
30 {
31         struct device_node *np;
32         u64 memblock_size = MIN_MEMORY_BLOCK_SIZE;
33         struct resource r;
34
35         np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
36         if (np) {
37                 const __be64 *size;
38
39                 size = of_get_property(np, "ibm,lmb-size", NULL);
40                 if (size)
41                         memblock_size = be64_to_cpup(size);
42                 of_node_put(np);
43         } else  if (machine_is(pseries)) {
44                 /* This fallback really only applies to pseries */
45                 unsigned int memzero_size = 0;
46
47                 np = of_find_node_by_path("/memory@0");
48                 if (np) {
49                         if (!of_address_to_resource(np, 0, &r))
50                                 memzero_size = resource_size(&r);
51                         of_node_put(np);
52                 }
53
54                 if (memzero_size) {
55                         /* We now know the size of memory@0, use this to find
56                          * the first memoryblock and get its size.
57                          */
58                         char buf[64];
59
60                         sprintf(buf, "/memory@%x", memzero_size);
61                         np = of_find_node_by_path(buf);
62                         if (np) {
63                                 if (!of_address_to_resource(np, 0, &r))
64                                         memblock_size = resource_size(&r);
65                                 of_node_put(np);
66                         }
67                 }
68         }
69         return memblock_size;
70 }
71
72 static void dlpar_free_property(struct property *prop)
73 {
74         kfree(prop->name);
75         kfree(prop->value);
76         kfree(prop);
77 }
78
79 static struct property *dlpar_clone_property(struct property *prop,
80                                              u32 prop_size)
81 {
82         struct property *new_prop;
83
84         new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
85         if (!new_prop)
86                 return NULL;
87
88         new_prop->name = kstrdup(prop->name, GFP_KERNEL);
89         new_prop->value = kzalloc(prop_size, GFP_KERNEL);
90         if (!new_prop->name || !new_prop->value) {
91                 dlpar_free_property(new_prop);
92                 return NULL;
93         }
94
95         memcpy(new_prop->value, prop->value, prop->length);
96         new_prop->length = prop_size;
97
98         of_property_set_flag(new_prop, OF_DYNAMIC);
99         return new_prop;
100 }
101
102 static struct property *dlpar_clone_drconf_property(struct device_node *dn)
103 {
104         struct property *prop, *new_prop;
105         struct of_drconf_cell *lmbs;
106         u32 num_lmbs, *p;
107         int i;
108
109         prop = of_find_property(dn, "ibm,dynamic-memory", NULL);
110         if (!prop)
111                 return NULL;
112
113         new_prop = dlpar_clone_property(prop, prop->length);
114         if (!new_prop)
115                 return NULL;
116
117         /* Convert the property to cpu endian-ness */
118         p = new_prop->value;
119         *p = be32_to_cpu(*p);
120
121         num_lmbs = *p++;
122         lmbs = (struct of_drconf_cell *)p;
123
124         for (i = 0; i < num_lmbs; i++) {
125                 lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr);
126                 lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index);
127                 lmbs[i].aa_index = be32_to_cpu(lmbs[i].aa_index);
128                 lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
129         }
130
131         return new_prop;
132 }
133
134 static void dlpar_update_drconf_property(struct device_node *dn,
135                                          struct property *prop)
136 {
137         struct of_drconf_cell *lmbs;
138         u32 num_lmbs, *p;
139         int i;
140
141         /* Convert the property back to BE */
142         p = prop->value;
143         num_lmbs = *p;
144         *p = cpu_to_be32(*p);
145         p++;
146
147         lmbs = (struct of_drconf_cell *)p;
148         for (i = 0; i < num_lmbs; i++) {
149                 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
150                 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
151                 lmbs[i].aa_index = cpu_to_be32(lmbs[i].aa_index);
152                 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
153         }
154
155         rtas_hp_event = true;
156         of_update_property(dn, prop);
157         rtas_hp_event = false;
158 }
159
160 static int dlpar_update_device_tree_lmb(struct of_drconf_cell *lmb)
161 {
162         struct device_node *dn;
163         struct property *prop;
164         struct of_drconf_cell *lmbs;
165         u32 *p, num_lmbs;
166         int i;
167
168         dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
169         if (!dn)
170                 return -ENODEV;
171
172         prop = dlpar_clone_drconf_property(dn);
173         if (!prop) {
174                 of_node_put(dn);
175                 return -ENODEV;
176         }
177
178         p = prop->value;
179         num_lmbs = *p++;
180         lmbs = (struct of_drconf_cell *)p;
181
182         for (i = 0; i < num_lmbs; i++) {
183                 if (lmbs[i].drc_index == lmb->drc_index) {
184                         lmbs[i].flags = lmb->flags;
185                         lmbs[i].aa_index = lmb->aa_index;
186
187                         dlpar_update_drconf_property(dn, prop);
188                         break;
189                 }
190         }
191
192         of_node_put(dn);
193         return 0;
194 }
195
196 static u32 find_aa_index(struct device_node *dr_node,
197                          struct property *ala_prop, const u32 *lmb_assoc)
198 {
199         u32 *assoc_arrays;
200         u32 aa_index;
201         int aa_arrays, aa_array_entries, aa_array_sz;
202         int i, index;
203
204         /*
205          * The ibm,associativity-lookup-arrays property is defined to be
206          * a 32-bit value specifying the number of associativity arrays
207          * followed by a 32-bitvalue specifying the number of entries per
208          * array, followed by the associativity arrays.
209          */
210         assoc_arrays = ala_prop->value;
211
212         aa_arrays = be32_to_cpu(assoc_arrays[0]);
213         aa_array_entries = be32_to_cpu(assoc_arrays[1]);
214         aa_array_sz = aa_array_entries * sizeof(u32);
215
216         aa_index = -1;
217         for (i = 0; i < aa_arrays; i++) {
218                 index = (i * aa_array_entries) + 2;
219
220                 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
221                         continue;
222
223                 aa_index = i;
224                 break;
225         }
226
227         if (aa_index == -1) {
228                 struct property *new_prop;
229                 u32 new_prop_size;
230
231                 new_prop_size = ala_prop->length + aa_array_sz;
232                 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
233                 if (!new_prop)
234                         return -1;
235
236                 assoc_arrays = new_prop->value;
237
238                 /* increment the number of entries in the lookup array */
239                 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
240
241                 /* copy the new associativity into the lookup array */
242                 index = aa_arrays * aa_array_entries + 2;
243                 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
244
245                 of_update_property(dr_node, new_prop);
246
247                 /*
248                  * The associativity lookup array index for this lmb is
249                  * number of entries - 1 since we added its associativity
250                  * to the end of the lookup array.
251                  */
252                 aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
253         }
254
255         return aa_index;
256 }
257
258 static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb)
259 {
260         struct device_node *parent, *lmb_node, *dr_node;
261         struct property *ala_prop;
262         const u32 *lmb_assoc;
263         u32 aa_index;
264
265         parent = of_find_node_by_path("/");
266         if (!parent)
267                 return -ENODEV;
268
269         lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
270                                              parent);
271         of_node_put(parent);
272         if (!lmb_node)
273                 return -EINVAL;
274
275         lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
276         if (!lmb_assoc) {
277                 dlpar_free_cc_nodes(lmb_node);
278                 return -ENODEV;
279         }
280
281         dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
282         if (!dr_node) {
283                 dlpar_free_cc_nodes(lmb_node);
284                 return -ENODEV;
285         }
286
287         ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
288                                     NULL);
289         if (!ala_prop) {
290                 of_node_put(dr_node);
291                 dlpar_free_cc_nodes(lmb_node);
292                 return -ENODEV;
293         }
294
295         aa_index = find_aa_index(dr_node, ala_prop, lmb_assoc);
296
297         of_node_put(dr_node);
298         dlpar_free_cc_nodes(lmb_node);
299         return aa_index;
300 }
301
302 static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb)
303 {
304         int aa_index;
305
306         lmb->flags |= DRCONF_MEM_ASSIGNED;
307
308         aa_index = lookup_lmb_associativity_index(lmb);
309         if (aa_index < 0) {
310                 pr_err("Couldn't find associativity index for drc index %x\n",
311                        lmb->drc_index);
312                 return aa_index;
313         }
314
315         lmb->aa_index = aa_index;
316         return dlpar_update_device_tree_lmb(lmb);
317 }
318
319 static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb)
320 {
321         lmb->flags &= ~DRCONF_MEM_ASSIGNED;
322         lmb->aa_index = 0xffffffff;
323         return dlpar_update_device_tree_lmb(lmb);
324 }
325
326 #ifdef CONFIG_MEMORY_HOTREMOVE
327 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
328 {
329         unsigned long block_sz, start_pfn;
330         int sections_per_block;
331         int i, nid;
332
333         start_pfn = base >> PAGE_SHIFT;
334
335         lock_device_hotplug();
336
337         if (!pfn_valid(start_pfn))
338                 goto out;
339
340         block_sz = pseries_memory_block_size();
341         sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
342         nid = memory_add_physaddr_to_nid(base);
343
344         for (i = 0; i < sections_per_block; i++) {
345                 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
346                 base += MIN_MEMORY_BLOCK_SIZE;
347         }
348
349 out:
350         /* Update memory regions for memory remove */
351         memblock_remove(base, memblock_size);
352         unlock_device_hotplug();
353         return 0;
354 }
355
356 static int pseries_remove_mem_node(struct device_node *np)
357 {
358         const char *type;
359         const __be32 *regs;
360         unsigned long base;
361         unsigned int lmb_size;
362         int ret = -EINVAL;
363
364         /*
365          * Check to see if we are actually removing memory
366          */
367         type = of_get_property(np, "device_type", NULL);
368         if (type == NULL || strcmp(type, "memory") != 0)
369                 return 0;
370
371         /*
372          * Find the base address and size of the memblock
373          */
374         regs = of_get_property(np, "reg", NULL);
375         if (!regs)
376                 return ret;
377
378         base = be64_to_cpu(*(unsigned long *)regs);
379         lmb_size = be32_to_cpu(regs[3]);
380
381         pseries_remove_memblock(base, lmb_size);
382         return 0;
383 }
384
385 static bool lmb_is_removable(struct of_drconf_cell *lmb)
386 {
387         int i, scns_per_block;
388         int rc = 1;
389         unsigned long pfn, block_sz;
390         u64 phys_addr;
391
392         if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
393                 return false;
394
395         block_sz = memory_block_size_bytes();
396         scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
397         phys_addr = lmb->base_addr;
398
399         for (i = 0; i < scns_per_block; i++) {
400                 pfn = PFN_DOWN(phys_addr);
401                 if (!pfn_present(pfn)) {
402                         phys_addr += MIN_MEMORY_BLOCK_SIZE;
403                         continue;
404                 }
405
406                 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
407                 phys_addr += MIN_MEMORY_BLOCK_SIZE;
408         }
409
410         return rc ? true : false;
411 }
412
413 static int dlpar_add_lmb(struct of_drconf_cell *);
414
415 static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
416 {
417         unsigned long section_nr;
418         struct mem_section *mem_sect;
419         struct memory_block *mem_block;
420
421         section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
422         mem_sect = __nr_to_section(section_nr);
423
424         mem_block = find_memory_block(mem_sect);
425         return mem_block;
426 }
427
428 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
429 {
430         struct memory_block *mem_block;
431         unsigned long block_sz;
432         int nid, rc;
433
434         if (!lmb_is_removable(lmb))
435                 return -EINVAL;
436
437         mem_block = lmb_to_memblock(lmb);
438         if (!mem_block)
439                 return -EINVAL;
440
441         rc = device_offline(&mem_block->dev);
442         put_device(&mem_block->dev);
443         if (rc)
444                 return rc;
445
446         block_sz = pseries_memory_block_size();
447         nid = memory_add_physaddr_to_nid(lmb->base_addr);
448
449         remove_memory(nid, lmb->base_addr, block_sz);
450
451         /* Update memory regions for memory remove */
452         memblock_remove(lmb->base_addr, block_sz);
453
454         dlpar_release_drc(lmb->drc_index);
455         dlpar_remove_device_tree_lmb(lmb);
456
457         return 0;
458 }
459
460 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
461                                         struct property *prop)
462 {
463         struct of_drconf_cell *lmbs;
464         int lmbs_removed = 0;
465         int lmbs_available = 0;
466         u32 num_lmbs, *p;
467         int i, rc;
468
469         pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
470
471         if (lmbs_to_remove == 0)
472                 return -EINVAL;
473
474         p = prop->value;
475         num_lmbs = *p++;
476         lmbs = (struct of_drconf_cell *)p;
477
478         /* Validate that there are enough LMBs to satisfy the request */
479         for (i = 0; i < num_lmbs; i++) {
480                 if (lmbs[i].flags & DRCONF_MEM_ASSIGNED)
481                         lmbs_available++;
482         }
483
484         if (lmbs_available < lmbs_to_remove)
485                 return -EINVAL;
486
487         for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
488                 rc = dlpar_remove_lmb(&lmbs[i]);
489                 if (rc)
490                         continue;
491
492                 lmbs_removed++;
493
494                 /* Mark this lmb so we can add it later if all of the
495                  * requested LMBs cannot be removed.
496                  */
497                 lmbs[i].reserved = 1;
498         }
499
500         if (lmbs_removed != lmbs_to_remove) {
501                 pr_err("Memory hot-remove failed, adding LMB's back\n");
502
503                 for (i = 0; i < num_lmbs; i++) {
504                         if (!lmbs[i].reserved)
505                                 continue;
506
507                         rc = dlpar_add_lmb(&lmbs[i]);
508                         if (rc)
509                                 pr_err("Failed to add LMB back, drc index %x\n",
510                                        lmbs[i].drc_index);
511
512                         lmbs[i].reserved = 0;
513                 }
514
515                 rc = -EINVAL;
516         } else {
517                 for (i = 0; i < num_lmbs; i++) {
518                         if (!lmbs[i].reserved)
519                                 continue;
520
521                         pr_info("Memory at %llx was hot-removed\n",
522                                 lmbs[i].base_addr);
523
524                         lmbs[i].reserved = 0;
525                 }
526                 rc = 0;
527         }
528
529         return rc;
530 }
531
532 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
533 {
534         struct of_drconf_cell *lmbs;
535         u32 num_lmbs, *p;
536         int lmb_found;
537         int i, rc;
538
539         pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
540
541         p = prop->value;
542         num_lmbs = *p++;
543         lmbs = (struct of_drconf_cell *)p;
544
545         lmb_found = 0;
546         for (i = 0; i < num_lmbs; i++) {
547                 if (lmbs[i].drc_index == drc_index) {
548                         lmb_found = 1;
549                         rc = dlpar_remove_lmb(&lmbs[i]);
550                         break;
551                 }
552         }
553
554         if (!lmb_found)
555                 rc = -EINVAL;
556
557         if (rc)
558                 pr_info("Failed to hot-remove memory at %llx\n",
559                         lmbs[i].base_addr);
560         else
561                 pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
562
563         return rc;
564 }
565
566 #else
567 static inline int pseries_remove_memblock(unsigned long base,
568                                           unsigned int memblock_size)
569 {
570         return -EOPNOTSUPP;
571 }
572 static inline int pseries_remove_mem_node(struct device_node *np)
573 {
574         return 0;
575 }
576 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
577 {
578         return -EOPNOTSUPP;
579 }
580 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
581 {
582         return -EOPNOTSUPP;
583 }
584 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
585                                         struct property *prop)
586 {
587         return -EOPNOTSUPP;
588 }
589 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
590 {
591         return -EOPNOTSUPP;
592 }
593
594 #endif /* CONFIG_MEMORY_HOTREMOVE */
595
596 static int dlpar_add_lmb(struct of_drconf_cell *lmb)
597 {
598         unsigned long block_sz;
599         int nid, rc;
600
601         if (lmb->flags & DRCONF_MEM_ASSIGNED)
602                 return -EINVAL;
603
604         rc = dlpar_acquire_drc(lmb->drc_index);
605         if (rc)
606                 return rc;
607
608         rc = dlpar_add_device_tree_lmb(lmb);
609         if (rc) {
610                 pr_err("Couldn't update device tree for drc index %x\n",
611                        lmb->drc_index);
612                 dlpar_release_drc(lmb->drc_index);
613                 return rc;
614         }
615
616         block_sz = memory_block_size_bytes();
617
618         /* Find the node id for this address */
619         nid = memory_add_physaddr_to_nid(lmb->base_addr);
620
621         /* Add the memory */
622         rc = __add_memory(nid, lmb->base_addr, block_sz);
623         if (rc) {
624                 dlpar_remove_device_tree_lmb(lmb);
625                 dlpar_release_drc(lmb->drc_index);
626         } else {
627                 lmb->flags |= DRCONF_MEM_ASSIGNED;
628         }
629
630         return rc;
631 }
632
633 static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
634 {
635         struct of_drconf_cell *lmbs;
636         u32 num_lmbs, *p;
637         int lmbs_available = 0;
638         int lmbs_added = 0;
639         int i, rc;
640
641         pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
642
643         if (lmbs_to_add == 0)
644                 return -EINVAL;
645
646         p = prop->value;
647         num_lmbs = *p++;
648         lmbs = (struct of_drconf_cell *)p;
649
650         /* Validate that there are enough LMBs to satisfy the request */
651         for (i = 0; i < num_lmbs; i++) {
652                 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
653                         lmbs_available++;
654         }
655
656         if (lmbs_available < lmbs_to_add)
657                 return -EINVAL;
658
659         for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
660                 rc = dlpar_add_lmb(&lmbs[i]);
661                 if (rc)
662                         continue;
663
664                 lmbs_added++;
665
666                 /* Mark this lmb so we can remove it later if all of the
667                  * requested LMBs cannot be added.
668                  */
669                 lmbs[i].reserved = 1;
670         }
671
672         if (lmbs_added != lmbs_to_add) {
673                 pr_err("Memory hot-add failed, removing any added LMBs\n");
674
675                 for (i = 0; i < num_lmbs; i++) {
676                         if (!lmbs[i].reserved)
677                                 continue;
678
679                         rc = dlpar_remove_lmb(&lmbs[i]);
680                         if (rc)
681                                 pr_err("Failed to remove LMB, drc index %x\n",
682                                        be32_to_cpu(lmbs[i].drc_index));
683                 }
684                 rc = -EINVAL;
685         } else {
686                 for (i = 0; i < num_lmbs; i++) {
687                         if (!lmbs[i].reserved)
688                                 continue;
689
690                         pr_info("Memory at %llx (drc index %x) was hot-added\n",
691                                 lmbs[i].base_addr, lmbs[i].drc_index);
692                         lmbs[i].reserved = 0;
693                 }
694         }
695
696         return rc;
697 }
698
699 static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
700 {
701         struct of_drconf_cell *lmbs;
702         u32 num_lmbs, *p;
703         int i, lmb_found;
704         int rc;
705
706         pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
707
708         p = prop->value;
709         num_lmbs = *p++;
710         lmbs = (struct of_drconf_cell *)p;
711
712         lmb_found = 0;
713         for (i = 0; i < num_lmbs; i++) {
714                 if (lmbs[i].drc_index == drc_index) {
715                         lmb_found = 1;
716                         rc = dlpar_add_lmb(&lmbs[i]);
717                         break;
718                 }
719         }
720
721         if (!lmb_found)
722                 rc = -EINVAL;
723
724         if (rc)
725                 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
726         else
727                 pr_info("Memory at %llx (drc index %x) was hot-added\n",
728                         lmbs[i].base_addr, drc_index);
729
730         return rc;
731 }
732
733 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
734 {
735         struct device_node *dn;
736         struct property *prop;
737         u32 count, drc_index;
738         int rc;
739
740         count = hp_elog->_drc_u.drc_count;
741         drc_index = hp_elog->_drc_u.drc_index;
742
743         lock_device_hotplug();
744
745         dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
746         if (!dn) {
747                 rc = -EINVAL;
748                 goto dlpar_memory_out;
749         }
750
751         prop = dlpar_clone_drconf_property(dn);
752         if (!prop) {
753                 rc = -EINVAL;
754                 goto dlpar_memory_out;
755         }
756
757         switch (hp_elog->action) {
758         case PSERIES_HP_ELOG_ACTION_ADD:
759                 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
760                         rc = dlpar_memory_add_by_count(count, prop);
761                 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
762                         rc = dlpar_memory_add_by_index(drc_index, prop);
763                 else
764                         rc = -EINVAL;
765                 break;
766         case PSERIES_HP_ELOG_ACTION_REMOVE:
767                 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
768                         rc = dlpar_memory_remove_by_count(count, prop);
769                 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
770                         rc = dlpar_memory_remove_by_index(drc_index, prop);
771                 else
772                         rc = -EINVAL;
773                 break;
774         default:
775                 pr_err("Invalid action (%d) specified\n", hp_elog->action);
776                 rc = -EINVAL;
777                 break;
778         }
779
780         dlpar_free_property(prop);
781
782 dlpar_memory_out:
783         of_node_put(dn);
784         unlock_device_hotplug();
785         return rc;
786 }
787
788 static int pseries_add_mem_node(struct device_node *np)
789 {
790         const char *type;
791         const __be32 *regs;
792         unsigned long base;
793         unsigned int lmb_size;
794         int ret = -EINVAL;
795
796         /*
797          * Check to see if we are actually adding memory
798          */
799         type = of_get_property(np, "device_type", NULL);
800         if (type == NULL || strcmp(type, "memory") != 0)
801                 return 0;
802
803         /*
804          * Find the base and size of the memblock
805          */
806         regs = of_get_property(np, "reg", NULL);
807         if (!regs)
808                 return ret;
809
810         base = be64_to_cpu(*(unsigned long *)regs);
811         lmb_size = be32_to_cpu(regs[3]);
812
813         /*
814          * Update memory region to represent the memory add
815          */
816         ret = memblock_add(base, lmb_size);
817         return (ret < 0) ? -EINVAL : 0;
818 }
819
820 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
821 {
822         struct of_drconf_cell *new_drmem, *old_drmem;
823         unsigned long memblock_size;
824         u32 entries;
825         __be32 *p;
826         int i, rc = -EINVAL;
827
828         if (rtas_hp_event)
829                 return 0;
830
831         memblock_size = pseries_memory_block_size();
832         if (!memblock_size)
833                 return -EINVAL;
834
835         p = (__be32 *) pr->old_prop->value;
836         if (!p)
837                 return -EINVAL;
838
839         /* The first int of the property is the number of lmb's described
840          * by the property. This is followed by an array of of_drconf_cell
841          * entries. Get the number of entries and skip to the array of
842          * of_drconf_cell's.
843          */
844         entries = be32_to_cpu(*p++);
845         old_drmem = (struct of_drconf_cell *)p;
846
847         p = (__be32 *)pr->prop->value;
848         p++;
849         new_drmem = (struct of_drconf_cell *)p;
850
851         for (i = 0; i < entries; i++) {
852                 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
853                     (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
854                         rc = pseries_remove_memblock(
855                                 be64_to_cpu(old_drmem[i].base_addr),
856                                                      memblock_size);
857                         break;
858                 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
859                             DRCONF_MEM_ASSIGNED)) &&
860                             (be32_to_cpu(new_drmem[i].flags) &
861                             DRCONF_MEM_ASSIGNED)) {
862                         rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
863                                           memblock_size);
864                         rc = (rc < 0) ? -EINVAL : 0;
865                         break;
866                 }
867         }
868         return rc;
869 }
870
871 static int pseries_memory_notifier(struct notifier_block *nb,
872                                    unsigned long action, void *data)
873 {
874         struct of_reconfig_data *rd = data;
875         int err = 0;
876
877         switch (action) {
878         case OF_RECONFIG_ATTACH_NODE:
879                 err = pseries_add_mem_node(rd->dn);
880                 break;
881         case OF_RECONFIG_DETACH_NODE:
882                 err = pseries_remove_mem_node(rd->dn);
883                 break;
884         case OF_RECONFIG_UPDATE_PROPERTY:
885                 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
886                         err = pseries_update_drconf_memory(rd);
887                 break;
888         }
889         return notifier_from_errno(err);
890 }
891
892 static struct notifier_block pseries_mem_nb = {
893         .notifier_call = pseries_memory_notifier,
894 };
895
896 static int __init pseries_memory_hotplug_init(void)
897 {
898         if (firmware_has_feature(FW_FEATURE_LPAR))
899                 of_reconfig_notifier_register(&pseries_mem_nb);
900
901         return 0;
902 }
903 machine_device_initcall(pseries, pseries_memory_hotplug_init);