powerpc/pseries: Implement memory hotplug add in the kernel
[firefly-linux-kernel-4.4.55.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         unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
33         struct resource r;
34
35         np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
36         if (np) {
37                 const __be64 *size;
38
39                 size = of_get_property(np, "ibm,lmb-size", NULL);
40                 if (size)
41                         memblock_size = be64_to_cpup(size);
42                 of_node_put(np);
43         } else  if (machine_is(pseries)) {
44                 /* This fallback really only applies to pseries */
45                 unsigned int memzero_size = 0;
46
47                 np = of_find_node_by_path("/memory@0");
48                 if (np) {
49                         if (!of_address_to_resource(np, 0, &r))
50                                 memzero_size = resource_size(&r);
51                         of_node_put(np);
52                 }
53
54                 if (memzero_size) {
55                         /* We now know the size of memory@0, use this to find
56                          * the first memoryblock and get its size.
57                          */
58                         char buf[64];
59
60                         sprintf(buf, "/memory@%x", memzero_size);
61                         np = of_find_node_by_path(buf);
62                         if (np) {
63                                 if (!of_address_to_resource(np, 0, &r))
64                                         memblock_size = resource_size(&r);
65                                 of_node_put(np);
66                         }
67                 }
68         }
69         return memblock_size;
70 }
71
72 static void dlpar_free_drconf_property(struct property *prop)
73 {
74         kfree(prop->name);
75         kfree(prop->value);
76         kfree(prop);
77 }
78
79 static struct property *dlpar_clone_drconf_property(struct device_node *dn)
80 {
81         struct property *prop, *new_prop;
82         struct of_drconf_cell *lmbs;
83         u32 num_lmbs, *p;
84         int i;
85
86         prop = of_find_property(dn, "ibm,dynamic-memory", NULL);
87         if (!prop)
88                 return NULL;
89
90         new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
91         if (!new_prop)
92                 return NULL;
93
94         new_prop->name = kstrdup(prop->name, GFP_KERNEL);
95         new_prop->value = kmalloc(prop->length, GFP_KERNEL);
96         if (!new_prop->name || !new_prop->value) {
97                 dlpar_free_drconf_property(new_prop);
98                 return NULL;
99         }
100
101         memcpy(new_prop->value, prop->value, prop->length);
102         new_prop->length = prop->length;
103
104         /* Convert the property to cpu endian-ness */
105         p = new_prop->value;
106         *p = be32_to_cpu(*p);
107
108         num_lmbs = *p++;
109         lmbs = (struct of_drconf_cell *)p;
110
111         for (i = 0; i < num_lmbs; i++) {
112                 lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr);
113                 lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index);
114                 lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
115         }
116
117         return new_prop;
118 }
119
120 static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
121 {
122         unsigned long section_nr;
123         struct mem_section *mem_sect;
124         struct memory_block *mem_block;
125
126         section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
127         mem_sect = __nr_to_section(section_nr);
128
129         mem_block = find_memory_block(mem_sect);
130         return mem_block;
131 }
132
133 #ifdef CONFIG_MEMORY_HOTREMOVE
134 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
135 {
136         unsigned long block_sz, start_pfn;
137         int sections_per_block;
138         int i, nid;
139
140         start_pfn = base >> PAGE_SHIFT;
141
142         lock_device_hotplug();
143
144         if (!pfn_valid(start_pfn))
145                 goto out;
146
147         block_sz = pseries_memory_block_size();
148         sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
149         nid = memory_add_physaddr_to_nid(base);
150
151         for (i = 0; i < sections_per_block; i++) {
152                 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
153                 base += MIN_MEMORY_BLOCK_SIZE;
154         }
155
156 out:
157         /* Update memory regions for memory remove */
158         memblock_remove(base, memblock_size);
159         unlock_device_hotplug();
160         return 0;
161 }
162
163 static int pseries_remove_mem_node(struct device_node *np)
164 {
165         const char *type;
166         const __be32 *regs;
167         unsigned long base;
168         unsigned int lmb_size;
169         int ret = -EINVAL;
170
171         /*
172          * Check to see if we are actually removing memory
173          */
174         type = of_get_property(np, "device_type", NULL);
175         if (type == NULL || strcmp(type, "memory") != 0)
176                 return 0;
177
178         /*
179          * Find the base address and size of the memblock
180          */
181         regs = of_get_property(np, "reg", NULL);
182         if (!regs)
183                 return ret;
184
185         base = be64_to_cpu(*(unsigned long *)regs);
186         lmb_size = be32_to_cpu(regs[3]);
187
188         pseries_remove_memblock(base, lmb_size);
189         return 0;
190 }
191 #else
192 static inline int pseries_remove_memblock(unsigned long base,
193                                           unsigned int memblock_size)
194 {
195         return -EOPNOTSUPP;
196 }
197 static inline int pseries_remove_mem_node(struct device_node *np)
198 {
199         return 0;
200 }
201 #endif /* CONFIG_MEMORY_HOTREMOVE */
202
203 static int dlpar_add_lmb(struct of_drconf_cell *lmb)
204 {
205         struct memory_block *mem_block;
206         unsigned long block_sz;
207         int nid, rc;
208
209         if (lmb->flags & DRCONF_MEM_ASSIGNED)
210                 return -EINVAL;
211
212         block_sz = memory_block_size_bytes();
213
214         rc = dlpar_acquire_drc(lmb->drc_index);
215         if (rc)
216                 return rc;
217
218         /* Find the node id for this address */
219         nid = memory_add_physaddr_to_nid(lmb->base_addr);
220
221         /* Add the memory */
222         rc = add_memory(nid, lmb->base_addr, block_sz);
223         if (rc) {
224                 dlpar_release_drc(lmb->drc_index);
225                 return rc;
226         }
227
228         /* Register this block of memory */
229         rc = memblock_add(lmb->base_addr, block_sz);
230         if (rc) {
231                 remove_memory(nid, lmb->base_addr, block_sz);
232                 dlpar_release_drc(lmb->drc_index);
233                 return rc;
234         }
235
236         mem_block = lmb_to_memblock(lmb);
237         if (!mem_block) {
238                 remove_memory(nid, lmb->base_addr, block_sz);
239                 dlpar_release_drc(lmb->drc_index);
240                 return -EINVAL;
241         }
242
243         rc = device_online(&mem_block->dev);
244         put_device(&mem_block->dev);
245         if (rc) {
246                 remove_memory(nid, lmb->base_addr, block_sz);
247                 dlpar_release_drc(lmb->drc_index);
248                 return rc;
249         }
250
251         lmb->flags |= DRCONF_MEM_ASSIGNED;
252         return 0;
253 }
254
255 static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
256 {
257         struct of_drconf_cell *lmbs;
258         u32 num_lmbs, *p;
259         int lmbs_available = 0;
260         int lmbs_added = 0;
261         int i, rc;
262
263         pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
264
265         if (lmbs_to_add == 0)
266                 return -EINVAL;
267
268         p = prop->value;
269         num_lmbs = *p++;
270         lmbs = (struct of_drconf_cell *)p;
271
272         /* Validate that there are enough LMBs to satisfy the request */
273         for (i = 0; i < num_lmbs; i++) {
274                 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
275                         lmbs_available++;
276         }
277
278         if (lmbs_available < lmbs_to_add)
279                 return -EINVAL;
280
281         for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
282                 rc = dlpar_add_lmb(&lmbs[i]);
283                 if (rc)
284                         continue;
285
286                 lmbs_added++;
287
288                 /* Mark this lmb so we can remove it later if all of the
289                  * requested LMBs cannot be added.
290                  */
291                 lmbs[i].reserved = 1;
292         }
293
294         if (lmbs_added != lmbs_to_add) {
295                 /* TODO: remove added lmbs */
296                 rc = -EINVAL;
297         } else {
298                 for (i = 0; i < num_lmbs; i++) {
299                         if (!lmbs[i].reserved)
300                                 continue;
301
302                         pr_info("Memory at %llx (drc index %x) was hot-added\n",
303                                 lmbs[i].base_addr, lmbs[i].drc_index);
304                         lmbs[i].reserved = 0;
305                 }
306         }
307
308         return rc;
309 }
310
311 static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
312 {
313         struct of_drconf_cell *lmbs;
314         u32 num_lmbs, *p;
315         int i, lmb_found;
316         int rc;
317
318         pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
319
320         p = prop->value;
321         num_lmbs = *p++;
322         lmbs = (struct of_drconf_cell *)p;
323
324         lmb_found = 0;
325         for (i = 0; i < num_lmbs; i++) {
326                 if (lmbs[i].drc_index == drc_index) {
327                         lmb_found = 1;
328                         rc = dlpar_add_lmb(&lmbs[i]);
329                         break;
330                 }
331         }
332
333         if (!lmb_found)
334                 rc = -EINVAL;
335
336         if (rc)
337                 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
338         else
339                 pr_info("Memory at %llx (drc index %x) was hot-added\n",
340                         lmbs[i].base_addr, drc_index);
341
342         return rc;
343 }
344
345 static void dlpar_update_drconf_property(struct device_node *dn,
346                                          struct property *prop)
347 {
348         struct of_drconf_cell *lmbs;
349         u32 num_lmbs, *p;
350         int i;
351
352         /* Convert the property back to BE */
353         p = prop->value;
354         num_lmbs = *p;
355         *p = cpu_to_be32(*p);
356         p++;
357
358         lmbs = (struct of_drconf_cell *)p;
359         for (i = 0; i < num_lmbs; i++) {
360                 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
361                 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
362                 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
363         }
364
365         rtas_hp_event = true;
366         of_update_property(dn, prop);
367         rtas_hp_event = false;
368 }
369
370 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
371 {
372         struct device_node *dn;
373         struct property *prop;
374         u32 count, drc_index;
375         int rc;
376
377         count = hp_elog->_drc_u.drc_count;
378         drc_index = hp_elog->_drc_u.drc_index;
379
380         lock_device_hotplug();
381
382         dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
383         if (!dn)
384                 return -EINVAL;
385
386         prop = dlpar_clone_drconf_property(dn);
387         if (!prop) {
388                 of_node_put(dn);
389                 return -EINVAL;
390         }
391
392         switch (hp_elog->action) {
393         case PSERIES_HP_ELOG_ACTION_ADD:
394                 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
395                         rc = dlpar_memory_add_by_count(count, prop);
396                 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
397                         rc = dlpar_memory_add_by_index(drc_index, prop);
398                 else
399                         rc = -EINVAL;
400                 break;
401         default:
402                 pr_err("Invalid action (%d) specified\n", hp_elog->action);
403                 rc = -EINVAL;
404                 break;
405         }
406
407         if (rc)
408                 dlpar_free_drconf_property(prop);
409         else
410                 dlpar_update_drconf_property(dn, prop);
411
412         of_node_put(dn);
413         unlock_device_hotplug();
414         return rc;
415 }
416
417 static int pseries_add_mem_node(struct device_node *np)
418 {
419         const char *type;
420         const __be32 *regs;
421         unsigned long base;
422         unsigned int lmb_size;
423         int ret = -EINVAL;
424
425         /*
426          * Check to see if we are actually adding memory
427          */
428         type = of_get_property(np, "device_type", NULL);
429         if (type == NULL || strcmp(type, "memory") != 0)
430                 return 0;
431
432         /*
433          * Find the base and size of the memblock
434          */
435         regs = of_get_property(np, "reg", NULL);
436         if (!regs)
437                 return ret;
438
439         base = be64_to_cpu(*(unsigned long *)regs);
440         lmb_size = be32_to_cpu(regs[3]);
441
442         /*
443          * Update memory region to represent the memory add
444          */
445         ret = memblock_add(base, lmb_size);
446         return (ret < 0) ? -EINVAL : 0;
447 }
448
449 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
450 {
451         struct of_drconf_cell *new_drmem, *old_drmem;
452         unsigned long memblock_size;
453         u32 entries;
454         __be32 *p;
455         int i, rc = -EINVAL;
456
457         if (rtas_hp_event)
458                 return 0;
459
460         memblock_size = pseries_memory_block_size();
461         if (!memblock_size)
462                 return -EINVAL;
463
464         p = (__be32 *) pr->old_prop->value;
465         if (!p)
466                 return -EINVAL;
467
468         /* The first int of the property is the number of lmb's described
469          * by the property. This is followed by an array of of_drconf_cell
470          * entries. Get the number of entries and skip to the array of
471          * of_drconf_cell's.
472          */
473         entries = be32_to_cpu(*p++);
474         old_drmem = (struct of_drconf_cell *)p;
475
476         p = (__be32 *)pr->prop->value;
477         p++;
478         new_drmem = (struct of_drconf_cell *)p;
479
480         for (i = 0; i < entries; i++) {
481                 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
482                     (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
483                         rc = pseries_remove_memblock(
484                                 be64_to_cpu(old_drmem[i].base_addr),
485                                                      memblock_size);
486                         break;
487                 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
488                             DRCONF_MEM_ASSIGNED)) &&
489                             (be32_to_cpu(new_drmem[i].flags) &
490                             DRCONF_MEM_ASSIGNED)) {
491                         rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
492                                           memblock_size);
493                         rc = (rc < 0) ? -EINVAL : 0;
494                         break;
495                 }
496         }
497         return rc;
498 }
499
500 static int pseries_memory_notifier(struct notifier_block *nb,
501                                    unsigned long action, void *data)
502 {
503         struct of_reconfig_data *rd = data;
504         int err = 0;
505
506         switch (action) {
507         case OF_RECONFIG_ATTACH_NODE:
508                 err = pseries_add_mem_node(rd->dn);
509                 break;
510         case OF_RECONFIG_DETACH_NODE:
511                 err = pseries_remove_mem_node(rd->dn);
512                 break;
513         case OF_RECONFIG_UPDATE_PROPERTY:
514                 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
515                         err = pseries_update_drconf_memory(rd);
516                 break;
517         }
518         return notifier_from_errno(err);
519 }
520
521 static struct notifier_block pseries_mem_nb = {
522         .notifier_call = pseries_memory_notifier,
523 };
524
525 static int __init pseries_memory_hotplug_init(void)
526 {
527         if (firmware_has_feature(FW_FEATURE_LPAR))
528                 of_reconfig_notifier_register(&pseries_mem_nb);
529
530         return 0;
531 }
532 machine_device_initcall(pseries, pseries_memory_hotplug_init);