211d0bf7f5d94738e70c1da5f6ca529209f80365
[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
20 #include <asm/firmware.h>
21 #include <asm/machdep.h>
22 #include <asm/prom.h>
23 #include <asm/sparsemem.h>
24 #include "pseries.h"
25
26 unsigned long pseries_memory_block_size(void)
27 {
28         struct device_node *np;
29         unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
30         struct resource r;
31
32         np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
33         if (np) {
34                 const __be64 *size;
35
36                 size = of_get_property(np, "ibm,lmb-size", NULL);
37                 if (size)
38                         memblock_size = be64_to_cpup(size);
39                 of_node_put(np);
40         } else  if (machine_is(pseries)) {
41                 /* This fallback really only applies to pseries */
42                 unsigned int memzero_size = 0;
43
44                 np = of_find_node_by_path("/memory@0");
45                 if (np) {
46                         if (!of_address_to_resource(np, 0, &r))
47                                 memzero_size = resource_size(&r);
48                         of_node_put(np);
49                 }
50
51                 if (memzero_size) {
52                         /* We now know the size of memory@0, use this to find
53                          * the first memoryblock and get its size.
54                          */
55                         char buf[64];
56
57                         sprintf(buf, "/memory@%x", memzero_size);
58                         np = of_find_node_by_path(buf);
59                         if (np) {
60                                 if (!of_address_to_resource(np, 0, &r))
61                                         memblock_size = resource_size(&r);
62                                 of_node_put(np);
63                         }
64                 }
65         }
66         return memblock_size;
67 }
68
69 #ifdef CONFIG_MEMORY_HOTREMOVE
70 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
71 {
72         unsigned long block_sz, start_pfn;
73         int sections_per_block;
74         int i, nid;
75
76         start_pfn = base >> PAGE_SHIFT;
77
78         lock_device_hotplug();
79
80         if (!pfn_valid(start_pfn))
81                 goto out;
82
83         block_sz = pseries_memory_block_size();
84         sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
85         nid = memory_add_physaddr_to_nid(base);
86
87         for (i = 0; i < sections_per_block; i++) {
88                 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
89                 base += MIN_MEMORY_BLOCK_SIZE;
90         }
91
92 out:
93         /* Update memory regions for memory remove */
94         memblock_remove(base, memblock_size);
95         unlock_device_hotplug();
96         return 0;
97 }
98
99 static int pseries_remove_mem_node(struct device_node *np)
100 {
101         const char *type;
102         const __be32 *regs;
103         unsigned long base;
104         unsigned int lmb_size;
105         int ret = -EINVAL;
106
107         /*
108          * Check to see if we are actually removing memory
109          */
110         type = of_get_property(np, "device_type", NULL);
111         if (type == NULL || strcmp(type, "memory") != 0)
112                 return 0;
113
114         /*
115          * Find the base address and size of the memblock
116          */
117         regs = of_get_property(np, "reg", NULL);
118         if (!regs)
119                 return ret;
120
121         base = be64_to_cpu(*(unsigned long *)regs);
122         lmb_size = be32_to_cpu(regs[3]);
123
124         pseries_remove_memblock(base, lmb_size);
125         return 0;
126 }
127 #else
128 static inline int pseries_remove_memblock(unsigned long base,
129                                           unsigned int memblock_size)
130 {
131         return -EOPNOTSUPP;
132 }
133 static inline int pseries_remove_mem_node(struct device_node *np)
134 {
135         return 0;
136 }
137 #endif /* CONFIG_MEMORY_HOTREMOVE */
138
139 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
140 {
141         int rc = 0;
142
143         lock_device_hotplug();
144
145         switch (hp_elog->action) {
146         default:
147                 pr_err("Invalid action (%d) specified\n", hp_elog->action);
148                 rc = -EINVAL;
149                 break;
150         }
151
152         unlock_device_hotplug();
153         return rc;
154 }
155
156 static int pseries_add_mem_node(struct device_node *np)
157 {
158         const char *type;
159         const __be32 *regs;
160         unsigned long base;
161         unsigned int lmb_size;
162         int ret = -EINVAL;
163
164         /*
165          * Check to see if we are actually adding memory
166          */
167         type = of_get_property(np, "device_type", NULL);
168         if (type == NULL || strcmp(type, "memory") != 0)
169                 return 0;
170
171         /*
172          * Find the base and size of the memblock
173          */
174         regs = of_get_property(np, "reg", NULL);
175         if (!regs)
176                 return ret;
177
178         base = be64_to_cpu(*(unsigned long *)regs);
179         lmb_size = be32_to_cpu(regs[3]);
180
181         /*
182          * Update memory region to represent the memory add
183          */
184         ret = memblock_add(base, lmb_size);
185         return (ret < 0) ? -EINVAL : 0;
186 }
187
188 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
189 {
190         struct of_drconf_cell *new_drmem, *old_drmem;
191         unsigned long memblock_size;
192         u32 entries;
193         __be32 *p;
194         int i, rc = -EINVAL;
195
196         memblock_size = pseries_memory_block_size();
197         if (!memblock_size)
198                 return -EINVAL;
199
200         p = (__be32 *) pr->old_prop->value;
201         if (!p)
202                 return -EINVAL;
203
204         /* The first int of the property is the number of lmb's described
205          * by the property. This is followed by an array of of_drconf_cell
206          * entries. Get the number of entries and skip to the array of
207          * of_drconf_cell's.
208          */
209         entries = be32_to_cpu(*p++);
210         old_drmem = (struct of_drconf_cell *)p;
211
212         p = (__be32 *)pr->prop->value;
213         p++;
214         new_drmem = (struct of_drconf_cell *)p;
215
216         for (i = 0; i < entries; i++) {
217                 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
218                     (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
219                         rc = pseries_remove_memblock(
220                                 be64_to_cpu(old_drmem[i].base_addr),
221                                                      memblock_size);
222                         break;
223                 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
224                             DRCONF_MEM_ASSIGNED)) &&
225                             (be32_to_cpu(new_drmem[i].flags) &
226                             DRCONF_MEM_ASSIGNED)) {
227                         rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
228                                           memblock_size);
229                         rc = (rc < 0) ? -EINVAL : 0;
230                         break;
231                 }
232         }
233         return rc;
234 }
235
236 static int pseries_memory_notifier(struct notifier_block *nb,
237                                    unsigned long action, void *data)
238 {
239         struct of_reconfig_data *rd = data;
240         int err = 0;
241
242         switch (action) {
243         case OF_RECONFIG_ATTACH_NODE:
244                 err = pseries_add_mem_node(rd->dn);
245                 break;
246         case OF_RECONFIG_DETACH_NODE:
247                 err = pseries_remove_mem_node(rd->dn);
248                 break;
249         case OF_RECONFIG_UPDATE_PROPERTY:
250                 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
251                         err = pseries_update_drconf_memory(rd);
252                 break;
253         }
254         return notifier_from_errno(err);
255 }
256
257 static struct notifier_block pseries_mem_nb = {
258         .notifier_call = pseries_memory_notifier,
259 };
260
261 static int __init pseries_memory_hotplug_init(void)
262 {
263         if (firmware_has_feature(FW_FEATURE_LPAR))
264                 of_reconfig_notifier_register(&pseries_mem_nb);
265
266         return 0;
267 }
268 machine_device_initcall(pseries, pseries_memory_hotplug_init);