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