2 * drivers/base/memory.c - basic Memory class support
4 * Written by Matt Tolentino <matthew.e.tolentino@intel.com>
5 * Dave Hansen <haveblue@us.ibm.com>
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.
13 #include <linux/sysdev.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/topology.h>
17 #include <linux/capability.h>
18 #include <linux/device.h>
19 #include <linux/memory.h>
20 #include <linux/kobject.h>
21 #include <linux/memory_hotplug.h>
23 #include <linux/mutex.h>
24 #include <linux/stat.h>
26 #include <asm/atomic.h>
27 #include <asm/uaccess.h>
29 #define MEMORY_CLASS_NAME "memory"
31 static struct sysdev_class memory_sysdev_class = {
32 .name = MEMORY_CLASS_NAME,
35 static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
37 return MEMORY_CLASS_NAME;
40 static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
47 static struct kset_uevent_ops memory_uevent_ops = {
48 .name = memory_uevent_name,
49 .uevent = memory_uevent,
52 static BLOCKING_NOTIFIER_HEAD(memory_chain);
54 int register_memory_notifier(struct notifier_block *nb)
56 return blocking_notifier_chain_register(&memory_chain, nb);
58 EXPORT_SYMBOL(register_memory_notifier);
60 void unregister_memory_notifier(struct notifier_block *nb)
62 blocking_notifier_chain_unregister(&memory_chain, nb);
64 EXPORT_SYMBOL(unregister_memory_notifier);
67 * register_memory - Setup a sysfs device for a memory block
70 int register_memory(struct memory_block *memory, struct mem_section *section)
74 memory->sysdev.cls = &memory_sysdev_class;
75 memory->sysdev.id = __section_nr(section);
77 error = sysdev_register(&memory->sysdev);
82 unregister_memory(struct memory_block *memory, struct mem_section *section)
84 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
85 BUG_ON(memory->sysdev.id != __section_nr(section));
87 /* drop the ref. we got in remove_memory_block() */
88 kobject_put(&memory->sysdev.kobj);
89 sysdev_unregister(&memory->sysdev);
93 * use this as the physical section index that this memsection
97 static ssize_t show_mem_phys_index(struct sys_device *dev,
98 struct sysdev_attribute *attr, char *buf)
100 struct memory_block *mem =
101 container_of(dev, struct memory_block, sysdev);
102 return sprintf(buf, "%08lx\n", mem->phys_index);
106 * Show whether the section of memory is likely to be hot-removable
108 static ssize_t show_mem_removable(struct sys_device *dev,
109 struct sysdev_attribute *attr, char *buf)
111 unsigned long start_pfn;
113 struct memory_block *mem =
114 container_of(dev, struct memory_block, sysdev);
116 start_pfn = section_nr_to_pfn(mem->phys_index);
117 ret = is_mem_section_removable(start_pfn, PAGES_PER_SECTION);
118 return sprintf(buf, "%d\n", ret);
122 * online, offline, going offline, etc.
124 static ssize_t show_mem_state(struct sys_device *dev,
125 struct sysdev_attribute *attr, char *buf)
127 struct memory_block *mem =
128 container_of(dev, struct memory_block, sysdev);
132 * We can probably put these states in a nice little array
133 * so that they're not open-coded
135 switch (mem->state) {
137 len = sprintf(buf, "online\n");
140 len = sprintf(buf, "offline\n");
142 case MEM_GOING_OFFLINE:
143 len = sprintf(buf, "going-offline\n");
146 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
155 int memory_notify(unsigned long val, void *v)
157 return blocking_notifier_call_chain(&memory_chain, val, v);
161 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
162 * OK to have direct references to sparsemem variables in here.
165 memory_block_action(struct memory_block *mem, unsigned long action)
168 unsigned long psection;
169 unsigned long start_pfn, start_paddr;
170 struct page *first_page;
172 int old_state = mem->state;
174 psection = mem->phys_index;
175 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
178 * The probe routines leave the pages reserved, just
179 * as the bootmem code does. Make sure they're still
182 if (action == MEM_ONLINE) {
183 for (i = 0; i < PAGES_PER_SECTION; i++) {
184 if (PageReserved(first_page+i))
187 printk(KERN_WARNING "section number %ld page number %d "
188 "not reserved, was it already online? \n",
196 start_pfn = page_to_pfn(first_page);
197 ret = online_pages(start_pfn, PAGES_PER_SECTION);
200 mem->state = MEM_GOING_OFFLINE;
201 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
202 ret = remove_memory(start_paddr,
203 PAGES_PER_SECTION << PAGE_SHIFT);
205 mem->state = old_state;
210 WARN(1, KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
211 __func__, mem, action, action);
218 static int memory_block_change_state(struct memory_block *mem,
219 unsigned long to_state, unsigned long from_state_req)
222 mutex_lock(&mem->state_mutex);
224 if (mem->state != from_state_req) {
229 ret = memory_block_action(mem, to_state);
231 mem->state = to_state;
234 mutex_unlock(&mem->state_mutex);
239 store_mem_state(struct sys_device *dev,
240 struct sysdev_attribute *attr, const char *buf, size_t count)
242 struct memory_block *mem;
243 unsigned int phys_section_nr;
246 mem = container_of(dev, struct memory_block, sysdev);
247 phys_section_nr = mem->phys_index;
249 if (!present_section_nr(phys_section_nr))
252 if (!strncmp(buf, "online", min((int)count, 6)))
253 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
254 else if(!strncmp(buf, "offline", min((int)count, 7)))
255 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
263 * phys_device is a bad name for this. What I really want
264 * is a way to differentiate between memory ranges that
265 * are part of physical devices that constitute
266 * a complete removable unit or fru.
267 * i.e. do these ranges belong to the same physical device,
268 * s.t. if I offline all of these sections I can then
269 * remove the physical device?
271 static ssize_t show_phys_device(struct sys_device *dev,
272 struct sysdev_attribute *attr, char *buf)
274 struct memory_block *mem =
275 container_of(dev, struct memory_block, sysdev);
276 return sprintf(buf, "%d\n", mem->phys_device);
279 static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
280 static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
281 static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
282 static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL);
284 #define mem_create_simple_file(mem, attr_name) \
285 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
286 #define mem_remove_simple_file(mem, attr_name) \
287 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
290 * Block size attribute stuff
293 print_block_size(struct class *class, char *buf)
295 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
298 static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
300 static int block_size_init(void)
302 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
303 &class_attr_block_size_bytes.attr);
307 * Some architectures will have custom drivers to do this, and
308 * will not need to do it from userspace. The fake hot-add code
309 * as well as ppc64 will do all of their discovery in userspace
310 * and will require this interface.
312 #ifdef CONFIG_ARCH_MEMORY_PROBE
314 memory_probe_store(struct class *class, const char *buf, size_t count)
320 phys_addr = simple_strtoull(buf, NULL, 0);
322 nid = memory_add_physaddr_to_nid(phys_addr);
323 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
330 static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store);
332 static int memory_probe_init(void)
334 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
335 &class_attr_probe.attr);
338 static inline int memory_probe_init(void)
345 * Note that phys_device is optional. It is here to allow for
346 * differentiation between which *physical* devices each
347 * section belongs to...
350 static int add_memory_block(unsigned long node_id, struct mem_section *section,
351 unsigned long state, int phys_device)
353 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
359 mem->phys_index = __section_nr(section);
361 mutex_init(&mem->state_mutex);
362 mem->phys_device = phys_device;
364 ret = register_memory(mem, section);
366 ret = mem_create_simple_file(mem, phys_index);
368 ret = mem_create_simple_file(mem, state);
370 ret = mem_create_simple_file(mem, phys_device);
372 ret = mem_create_simple_file(mem, removable);
378 * For now, we have a linear search to go find the appropriate
379 * memory_block corresponding to a particular phys_index. If
380 * this gets to be a real problem, we can always use a radix
381 * tree or something here.
383 * This could be made generic for all sysdev classes.
385 static struct memory_block *find_memory_block(struct mem_section *section)
387 struct kobject *kobj;
388 struct sys_device *sysdev;
389 struct memory_block *mem;
390 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
393 * This only works because we know that section == sysdev->id
394 * slightly redundant with sysdev_register()
396 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
398 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
402 sysdev = container_of(kobj, struct sys_device, kobj);
403 mem = container_of(sysdev, struct memory_block, sysdev);
408 int remove_memory_block(unsigned long node_id, struct mem_section *section,
411 struct memory_block *mem;
413 mem = find_memory_block(section);
414 mem_remove_simple_file(mem, phys_index);
415 mem_remove_simple_file(mem, state);
416 mem_remove_simple_file(mem, phys_device);
417 mem_remove_simple_file(mem, removable);
418 unregister_memory(mem, section);
424 * need an interface for the VM to add new memory regions,
425 * but without onlining it.
427 int register_new_memory(struct mem_section *section)
429 return add_memory_block(0, section, MEM_OFFLINE, 0);
432 int unregister_memory_section(struct mem_section *section)
434 if (!present_section(section))
437 return remove_memory_block(0, section, 0);
441 * Initialize the sysfs support for memory devices...
443 int __init memory_dev_init(void)
449 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
450 ret = sysdev_class_register(&memory_sysdev_class);
455 * Create entries for memory sections that were found
456 * during boot and have been initialized
458 for (i = 0; i < NR_MEM_SECTIONS; i++) {
459 if (!present_section_nr(i))
461 err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
466 err = memory_probe_init();
469 err = block_size_init();
474 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);