memory-hotplug: don't calculate vm_total_pages twice when rebuilding zonelists in...
[deliverable/linux.git] / drivers / base / memory.c
CommitLineData
3947be19
DH
1/*
2 * drivers/base/memory.c - basic Memory class support
3 *
4 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
5 * Dave Hansen <haveblue@us.ibm.com>
6 *
7 * This file provides the necessary infrastructure to represent
8 * a SPARSEMEM-memory-model system's physical memory in /sysfs.
9 * All arch-independent code that assumes MEMORY_HOTPLUG requires
10 * SPARSEMEM should be contained here, or in mm/memory_hotplug.c.
11 */
12
13#include <linux/sysdev.h>
14#include <linux/module.h>
15#include <linux/init.h>
3947be19 16#include <linux/topology.h>
c59ede7b 17#include <linux/capability.h>
3947be19
DH
18#include <linux/device.h>
19#include <linux/memory.h>
20#include <linux/kobject.h>
21#include <linux/memory_hotplug.h>
22#include <linux/mm.h>
da19cbcf 23#include <linux/mutex.h>
3947be19
DH
24#include <asm/atomic.h>
25#include <asm/uaccess.h>
26
27#define MEMORY_CLASS_NAME "memory"
28
29static struct sysdev_class memory_sysdev_class = {
af5ca3f4 30 .name = MEMORY_CLASS_NAME,
3947be19 31};
3947be19 32
312c004d 33static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
3947be19
DH
34{
35 return MEMORY_CLASS_NAME;
36}
37
9ec0fd4e 38static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
3947be19
DH
39{
40 int retval = 0;
41
42 return retval;
43}
44
312c004d
KS
45static struct kset_uevent_ops memory_uevent_ops = {
46 .name = memory_uevent_name,
47 .uevent = memory_uevent,
3947be19
DH
48};
49
e041c683 50static BLOCKING_NOTIFIER_HEAD(memory_chain);
3947be19 51
98a38ebd 52int register_memory_notifier(struct notifier_block *nb)
3947be19 53{
e041c683 54 return blocking_notifier_chain_register(&memory_chain, nb);
3947be19 55}
3c82c30c 56EXPORT_SYMBOL(register_memory_notifier);
3947be19 57
98a38ebd 58void unregister_memory_notifier(struct notifier_block *nb)
3947be19 59{
e041c683 60 blocking_notifier_chain_unregister(&memory_chain, nb);
3947be19 61}
3c82c30c 62EXPORT_SYMBOL(unregister_memory_notifier);
3947be19
DH
63
64/*
65 * register_memory - Setup a sysfs device for a memory block
66 */
00a41db5
BP
67static
68int register_memory(struct memory_block *memory, struct mem_section *section)
3947be19
DH
69{
70 int error;
71
72 memory->sysdev.cls = &memory_sysdev_class;
73 memory->sysdev.id = __section_nr(section);
74
75 error = sysdev_register(&memory->sysdev);
3947be19
DH
76 return error;
77}
78
79static void
00a41db5 80unregister_memory(struct memory_block *memory, struct mem_section *section)
3947be19
DH
81{
82 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
83 BUG_ON(memory->sysdev.id != __section_nr(section));
84
00a41db5
BP
85 /* drop the ref. we got in remove_memory_block() */
86 kobject_put(&memory->sysdev.kobj);
3947be19 87 sysdev_unregister(&memory->sysdev);
3947be19
DH
88}
89
90/*
91 * use this as the physical section index that this memsection
92 * uses.
93 */
94
4a0b2b4d
AK
95static ssize_t show_mem_phys_index(struct sys_device *dev,
96 struct sysdev_attribute *attr, char *buf)
3947be19
DH
97{
98 struct memory_block *mem =
99 container_of(dev, struct memory_block, sysdev);
100 return sprintf(buf, "%08lx\n", mem->phys_index);
101}
102
103/*
104 * online, offline, going offline, etc.
105 */
4a0b2b4d
AK
106static ssize_t show_mem_state(struct sys_device *dev,
107 struct sysdev_attribute *attr, char *buf)
3947be19
DH
108{
109 struct memory_block *mem =
110 container_of(dev, struct memory_block, sysdev);
111 ssize_t len = 0;
112
113 /*
114 * We can probably put these states in a nice little array
115 * so that they're not open-coded
116 */
117 switch (mem->state) {
118 case MEM_ONLINE:
119 len = sprintf(buf, "online\n");
120 break;
121 case MEM_OFFLINE:
122 len = sprintf(buf, "offline\n");
123 break;
124 case MEM_GOING_OFFLINE:
125 len = sprintf(buf, "going-offline\n");
126 break;
127 default:
128 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
129 mem->state);
130 WARN_ON(1);
131 break;
132 }
133
134 return len;
135}
136
7b78d335 137int memory_notify(unsigned long val, void *v)
3947be19 138{
e041c683 139 return blocking_notifier_call_chain(&memory_chain, val, v);
3947be19
DH
140}
141
142/*
143 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
144 * OK to have direct references to sparsemem variables in here.
145 */
146static int
147memory_block_action(struct memory_block *mem, unsigned long action)
148{
149 int i;
150 unsigned long psection;
151 unsigned long start_pfn, start_paddr;
152 struct page *first_page;
153 int ret;
154 int old_state = mem->state;
155
156 psection = mem->phys_index;
157 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
158
159 /*
160 * The probe routines leave the pages reserved, just
161 * as the bootmem code does. Make sure they're still
162 * that way.
163 */
164 if (action == MEM_ONLINE) {
165 for (i = 0; i < PAGES_PER_SECTION; i++) {
166 if (PageReserved(first_page+i))
167 continue;
168
169 printk(KERN_WARNING "section number %ld page number %d "
170 "not reserved, was it already online? \n",
171 psection, i);
172 return -EBUSY;
173 }
174 }
175
176 switch (action) {
177 case MEM_ONLINE:
178 start_pfn = page_to_pfn(first_page);
179 ret = online_pages(start_pfn, PAGES_PER_SECTION);
180 break;
181 case MEM_OFFLINE:
182 mem->state = MEM_GOING_OFFLINE;
3947be19
DH
183 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
184 ret = remove_memory(start_paddr,
185 PAGES_PER_SECTION << PAGE_SHIFT);
186 if (ret) {
187 mem->state = old_state;
188 break;
189 }
3947be19
DH
190 break;
191 default:
192 printk(KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
2b3a302a 193 __func__, mem, action, action);
3947be19
DH
194 WARN_ON(1);
195 ret = -EINVAL;
196 }
3947be19
DH
197
198 return ret;
199}
200
201static int memory_block_change_state(struct memory_block *mem,
202 unsigned long to_state, unsigned long from_state_req)
203{
204 int ret = 0;
da19cbcf 205 mutex_lock(&mem->state_mutex);
3947be19
DH
206
207 if (mem->state != from_state_req) {
208 ret = -EINVAL;
209 goto out;
210 }
211
212 ret = memory_block_action(mem, to_state);
213 if (!ret)
214 mem->state = to_state;
215
216out:
da19cbcf 217 mutex_unlock(&mem->state_mutex);
3947be19
DH
218 return ret;
219}
220
221static ssize_t
4a0b2b4d
AK
222store_mem_state(struct sys_device *dev,
223 struct sysdev_attribute *attr, const char *buf, size_t count)
3947be19
DH
224{
225 struct memory_block *mem;
226 unsigned int phys_section_nr;
227 int ret = -EINVAL;
228
229 mem = container_of(dev, struct memory_block, sysdev);
230 phys_section_nr = mem->phys_index;
231
540557b9 232 if (!present_section_nr(phys_section_nr))
3947be19
DH
233 goto out;
234
235 if (!strncmp(buf, "online", min((int)count, 6)))
236 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
237 else if(!strncmp(buf, "offline", min((int)count, 7)))
238 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
239out:
240 if (ret)
241 return ret;
242 return count;
243}
244
245/*
246 * phys_device is a bad name for this. What I really want
247 * is a way to differentiate between memory ranges that
248 * are part of physical devices that constitute
249 * a complete removable unit or fru.
250 * i.e. do these ranges belong to the same physical device,
251 * s.t. if I offline all of these sections I can then
252 * remove the physical device?
253 */
4a0b2b4d
AK
254static ssize_t show_phys_device(struct sys_device *dev,
255 struct sysdev_attribute *attr, char *buf)
3947be19
DH
256{
257 struct memory_block *mem =
258 container_of(dev, struct memory_block, sysdev);
259 return sprintf(buf, "%d\n", mem->phys_device);
260}
261
262static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
263static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
264static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
265
266#define mem_create_simple_file(mem, attr_name) \
267 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
268#define mem_remove_simple_file(mem, attr_name) \
269 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
270
271/*
272 * Block size attribute stuff
273 */
274static ssize_t
275print_block_size(struct class *class, char *buf)
276{
277 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
278}
279
280static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
281
282static int block_size_init(void)
283{
28ec24e2
AM
284 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
285 &class_attr_block_size_bytes.attr);
3947be19
DH
286}
287
288/*
289 * Some architectures will have custom drivers to do this, and
290 * will not need to do it from userspace. The fake hot-add code
291 * as well as ppc64 will do all of their discovery in userspace
292 * and will require this interface.
293 */
294#ifdef CONFIG_ARCH_MEMORY_PROBE
295static ssize_t
be7ee9b2 296memory_probe_store(struct class *class, const char *buf, size_t count)
3947be19
DH
297{
298 u64 phys_addr;
bc02af93 299 int nid;
3947be19
DH
300 int ret;
301
302 phys_addr = simple_strtoull(buf, NULL, 0);
303
bc02af93
YG
304 nid = memory_add_physaddr_to_nid(phys_addr);
305 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
3947be19
DH
306
307 if (ret)
308 count = ret;
309
310 return count;
311}
312static CLASS_ATTR(probe, 0700, NULL, memory_probe_store);
313
314static int memory_probe_init(void)
315{
28ec24e2
AM
316 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
317 &class_attr_probe.attr);
3947be19
DH
318}
319#else
28ec24e2
AM
320static inline int memory_probe_init(void)
321{
322 return 0;
323}
3947be19
DH
324#endif
325
326/*
327 * Note that phys_device is optional. It is here to allow for
328 * differentiation between which *physical* devices each
329 * section belongs to...
330 */
331
332static int add_memory_block(unsigned long node_id, struct mem_section *section,
333 unsigned long state, int phys_device)
334{
0b0acbec 335 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
3947be19
DH
336 int ret = 0;
337
338 if (!mem)
339 return -ENOMEM;
340
3947be19
DH
341 mem->phys_index = __section_nr(section);
342 mem->state = state;
da19cbcf 343 mutex_init(&mem->state_mutex);
3947be19
DH
344 mem->phys_device = phys_device;
345
00a41db5 346 ret = register_memory(mem, section);
3947be19
DH
347 if (!ret)
348 ret = mem_create_simple_file(mem, phys_index);
349 if (!ret)
350 ret = mem_create_simple_file(mem, state);
351 if (!ret)
352 ret = mem_create_simple_file(mem, phys_device);
353
354 return ret;
355}
356
357/*
358 * For now, we have a linear search to go find the appropriate
359 * memory_block corresponding to a particular phys_index. If
360 * this gets to be a real problem, we can always use a radix
361 * tree or something here.
362 *
363 * This could be made generic for all sysdev classes.
364 */
365static struct memory_block *find_memory_block(struct mem_section *section)
366{
367 struct kobject *kobj;
368 struct sys_device *sysdev;
369 struct memory_block *mem;
370 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
371
372 /*
373 * This only works because we know that section == sysdev->id
374 * slightly redundant with sysdev_register()
375 */
376 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
377
378 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
379 if (!kobj)
380 return NULL;
381
382 sysdev = container_of(kobj, struct sys_device, kobj);
383 mem = container_of(sysdev, struct memory_block, sysdev);
384
385 return mem;
386}
387
388int remove_memory_block(unsigned long node_id, struct mem_section *section,
389 int phys_device)
390{
391 struct memory_block *mem;
392
393 mem = find_memory_block(section);
394 mem_remove_simple_file(mem, phys_index);
395 mem_remove_simple_file(mem, state);
396 mem_remove_simple_file(mem, phys_device);
00a41db5 397 unregister_memory(mem, section);
3947be19
DH
398
399 return 0;
400}
401
402/*
403 * need an interface for the VM to add new memory regions,
404 * but without onlining it.
405 */
406int register_new_memory(struct mem_section *section)
407{
408 return add_memory_block(0, section, MEM_OFFLINE, 0);
409}
410
411int unregister_memory_section(struct mem_section *section)
412{
540557b9 413 if (!present_section(section))
3947be19
DH
414 return -EINVAL;
415
416 return remove_memory_block(0, section, 0);
417}
418
419/*
420 * Initialize the sysfs support for memory devices...
421 */
422int __init memory_dev_init(void)
423{
424 unsigned int i;
425 int ret;
28ec24e2 426 int err;
3947be19 427
312c004d 428 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
3947be19 429 ret = sysdev_class_register(&memory_sysdev_class);
28ec24e2
AM
430 if (ret)
431 goto out;
3947be19
DH
432
433 /*
434 * Create entries for memory sections that were found
435 * during boot and have been initialized
436 */
437 for (i = 0; i < NR_MEM_SECTIONS; i++) {
540557b9 438 if (!present_section_nr(i))
3947be19 439 continue;
28ec24e2
AM
440 err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
441 if (!ret)
442 ret = err;
3947be19
DH
443 }
444
28ec24e2
AM
445 err = memory_probe_init();
446 if (!ret)
447 ret = err;
448 err = block_size_init();
449 if (!ret)
450 ret = err;
451out:
452 if (ret)
2b3a302a 453 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
3947be19
DH
454 return ret;
455}
This page took 0.50859 seconds and 5 git commands to generate.