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 <asm/atomic.h>
25 #include <asm/uaccess.h>
27 #define MEMORY_CLASS_NAME "memory"
29 static struct sysdev_class memory_sysdev_class = {
30 .name = MEMORY_CLASS_NAME,
33 static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
35 return MEMORY_CLASS_NAME;
38 static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
45 static struct kset_uevent_ops memory_uevent_ops = {
46 .name = memory_uevent_name,
47 .uevent = memory_uevent,
50 static BLOCKING_NOTIFIER_HEAD(memory_chain);
52 int register_memory_notifier(struct notifier_block *nb)
54 return blocking_notifier_chain_register(&memory_chain, nb);
57 void unregister_memory_notifier(struct notifier_block *nb)
59 blocking_notifier_chain_unregister(&memory_chain, nb);
63 * register_memory - Setup a sysfs device for a memory block
66 int register_memory(struct memory_block *memory, struct mem_section *section)
70 memory->sysdev.cls = &memory_sysdev_class;
71 memory->sysdev.id = __section_nr(section);
73 error = sysdev_register(&memory->sysdev);
78 unregister_memory(struct memory_block *memory, struct mem_section *section)
80 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
81 BUG_ON(memory->sysdev.id != __section_nr(section));
83 /* drop the ref. we got in remove_memory_block() */
84 kobject_put(&memory->sysdev.kobj);
85 sysdev_unregister(&memory->sysdev);
89 * use this as the physical section index that this memsection
93 static ssize_t show_mem_phys_index(struct sys_device *dev, char *buf)
95 struct memory_block *mem =
96 container_of(dev, struct memory_block, sysdev);
97 return sprintf(buf, "%08lx\n", mem->phys_index);
101 * online, offline, going offline, etc.
103 static ssize_t show_mem_state(struct sys_device *dev, char *buf)
105 struct memory_block *mem =
106 container_of(dev, struct memory_block, sysdev);
110 * We can probably put these states in a nice little array
111 * so that they're not open-coded
113 switch (mem->state) {
115 len = sprintf(buf, "online\n");
118 len = sprintf(buf, "offline\n");
120 case MEM_GOING_OFFLINE:
121 len = sprintf(buf, "going-offline\n");
124 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
133 int memory_notify(unsigned long val, void *v)
135 return blocking_notifier_call_chain(&memory_chain, val, v);
139 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
140 * OK to have direct references to sparsemem variables in here.
143 memory_block_action(struct memory_block *mem, unsigned long action)
146 unsigned long psection;
147 unsigned long start_pfn, start_paddr;
148 struct page *first_page;
150 int old_state = mem->state;
152 psection = mem->phys_index;
153 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
156 * The probe routines leave the pages reserved, just
157 * as the bootmem code does. Make sure they're still
160 if (action == MEM_ONLINE) {
161 for (i = 0; i < PAGES_PER_SECTION; i++) {
162 if (PageReserved(first_page+i))
165 printk(KERN_WARNING "section number %ld page number %d "
166 "not reserved, was it already online? \n",
174 start_pfn = page_to_pfn(first_page);
175 ret = online_pages(start_pfn, PAGES_PER_SECTION);
178 mem->state = MEM_GOING_OFFLINE;
179 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
180 ret = remove_memory(start_paddr,
181 PAGES_PER_SECTION << PAGE_SHIFT);
183 mem->state = old_state;
188 printk(KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
189 __func__, mem, action, action);
197 static int memory_block_change_state(struct memory_block *mem,
198 unsigned long to_state, unsigned long from_state_req)
201 mutex_lock(&mem->state_mutex);
203 if (mem->state != from_state_req) {
208 ret = memory_block_action(mem, to_state);
210 mem->state = to_state;
213 mutex_unlock(&mem->state_mutex);
218 store_mem_state(struct sys_device *dev, const char *buf, size_t count)
220 struct memory_block *mem;
221 unsigned int phys_section_nr;
224 mem = container_of(dev, struct memory_block, sysdev);
225 phys_section_nr = mem->phys_index;
227 if (!present_section_nr(phys_section_nr))
230 if (!strncmp(buf, "online", min((int)count, 6)))
231 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
232 else if(!strncmp(buf, "offline", min((int)count, 7)))
233 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
241 * phys_device is a bad name for this. What I really want
242 * is a way to differentiate between memory ranges that
243 * are part of physical devices that constitute
244 * a complete removable unit or fru.
245 * i.e. do these ranges belong to the same physical device,
246 * s.t. if I offline all of these sections I can then
247 * remove the physical device?
249 static ssize_t show_phys_device(struct sys_device *dev, char *buf)
251 struct memory_block *mem =
252 container_of(dev, struct memory_block, sysdev);
253 return sprintf(buf, "%d\n", mem->phys_device);
256 static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
257 static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
258 static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
260 #define mem_create_simple_file(mem, attr_name) \
261 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
262 #define mem_remove_simple_file(mem, attr_name) \
263 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
266 * Block size attribute stuff
269 print_block_size(struct class *class, char *buf)
271 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
274 static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
276 static int block_size_init(void)
278 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
279 &class_attr_block_size_bytes.attr);
283 * Some architectures will have custom drivers to do this, and
284 * will not need to do it from userspace. The fake hot-add code
285 * as well as ppc64 will do all of their discovery in userspace
286 * and will require this interface.
288 #ifdef CONFIG_ARCH_MEMORY_PROBE
290 memory_probe_store(struct class *class, const char *buf, size_t count)
296 phys_addr = simple_strtoull(buf, NULL, 0);
298 nid = memory_add_physaddr_to_nid(phys_addr);
299 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
306 static CLASS_ATTR(probe, 0700, NULL, memory_probe_store);
308 static int memory_probe_init(void)
310 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
311 &class_attr_probe.attr);
314 static inline int memory_probe_init(void)
321 * Note that phys_device is optional. It is here to allow for
322 * differentiation between which *physical* devices each
323 * section belongs to...
326 static int add_memory_block(unsigned long node_id, struct mem_section *section,
327 unsigned long state, int phys_device)
329 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
335 mem->phys_index = __section_nr(section);
337 mutex_init(&mem->state_mutex);
338 mem->phys_device = phys_device;
340 ret = register_memory(mem, section);
342 ret = mem_create_simple_file(mem, phys_index);
344 ret = mem_create_simple_file(mem, state);
346 ret = mem_create_simple_file(mem, phys_device);
352 * For now, we have a linear search to go find the appropriate
353 * memory_block corresponding to a particular phys_index. If
354 * this gets to be a real problem, we can always use a radix
355 * tree or something here.
357 * This could be made generic for all sysdev classes.
359 static struct memory_block *find_memory_block(struct mem_section *section)
361 struct kobject *kobj;
362 struct sys_device *sysdev;
363 struct memory_block *mem;
364 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
367 * This only works because we know that section == sysdev->id
368 * slightly redundant with sysdev_register()
370 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
372 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
376 sysdev = container_of(kobj, struct sys_device, kobj);
377 mem = container_of(sysdev, struct memory_block, sysdev);
382 int remove_memory_block(unsigned long node_id, struct mem_section *section,
385 struct memory_block *mem;
387 mem = find_memory_block(section);
388 mem_remove_simple_file(mem, phys_index);
389 mem_remove_simple_file(mem, state);
390 mem_remove_simple_file(mem, phys_device);
391 unregister_memory(mem, section);
397 * need an interface for the VM to add new memory regions,
398 * but without onlining it.
400 int register_new_memory(struct mem_section *section)
402 return add_memory_block(0, section, MEM_OFFLINE, 0);
405 int unregister_memory_section(struct mem_section *section)
407 if (!present_section(section))
410 return remove_memory_block(0, section, 0);
414 * Initialize the sysfs support for memory devices...
416 int __init memory_dev_init(void)
422 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
423 ret = sysdev_class_register(&memory_sysdev_class);
428 * Create entries for memory sections that were found
429 * during boot and have been initialized
431 for (i = 0; i < NR_MEM_SECTIONS; i++) {
432 if (!present_section_nr(i))
434 err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
439 err = memory_probe_init();
442 err = block_size_init();
447 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);