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
);
56 EXPORT_SYMBOL(register_memory_notifier
);
58 void unregister_memory_notifier(struct notifier_block
*nb
)
60 blocking_notifier_chain_unregister(&memory_chain
, nb
);
62 EXPORT_SYMBOL(unregister_memory_notifier
);
65 * register_memory - Setup a sysfs device for a memory block
68 int register_memory(struct memory_block
*memory
, struct mem_section
*section
)
72 memory
->sysdev
.cls
= &memory_sysdev_class
;
73 memory
->sysdev
.id
= __section_nr(section
);
75 error
= sysdev_register(&memory
->sysdev
);
80 unregister_memory(struct memory_block
*memory
, struct mem_section
*section
)
82 BUG_ON(memory
->sysdev
.cls
!= &memory_sysdev_class
);
83 BUG_ON(memory
->sysdev
.id
!= __section_nr(section
));
85 /* drop the ref. we got in remove_memory_block() */
86 kobject_put(&memory
->sysdev
.kobj
);
87 sysdev_unregister(&memory
->sysdev
);
91 * use this as the physical section index that this memsection
95 static ssize_t
show_mem_phys_index(struct sys_device
*dev
,
96 struct sysdev_attribute
*attr
, char *buf
)
98 struct memory_block
*mem
=
99 container_of(dev
, struct memory_block
, sysdev
);
100 return sprintf(buf
, "%08lx\n", mem
->phys_index
);
104 * Show whether the section of memory is likely to be hot-removable
106 static ssize_t
show_mem_removable(struct sys_device
*dev
, char *buf
)
108 unsigned long start_pfn
;
110 struct memory_block
*mem
=
111 container_of(dev
, struct memory_block
, sysdev
);
113 start_pfn
= section_nr_to_pfn(mem
->phys_index
);
114 ret
= is_mem_section_removable(start_pfn
, PAGES_PER_SECTION
);
115 return sprintf(buf
, "%d\n", ret
);
119 * online, offline, going offline, etc.
121 static ssize_t
show_mem_state(struct sys_device
*dev
,
122 struct sysdev_attribute
*attr
, char *buf
)
124 struct memory_block
*mem
=
125 container_of(dev
, struct memory_block
, sysdev
);
129 * We can probably put these states in a nice little array
130 * so that they're not open-coded
132 switch (mem
->state
) {
134 len
= sprintf(buf
, "online\n");
137 len
= sprintf(buf
, "offline\n");
139 case MEM_GOING_OFFLINE
:
140 len
= sprintf(buf
, "going-offline\n");
143 len
= sprintf(buf
, "ERROR-UNKNOWN-%ld\n",
152 int memory_notify(unsigned long val
, void *v
)
154 return blocking_notifier_call_chain(&memory_chain
, val
, v
);
158 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
159 * OK to have direct references to sparsemem variables in here.
162 memory_block_action(struct memory_block
*mem
, unsigned long action
)
165 unsigned long psection
;
166 unsigned long start_pfn
, start_paddr
;
167 struct page
*first_page
;
169 int old_state
= mem
->state
;
171 psection
= mem
->phys_index
;
172 first_page
= pfn_to_page(psection
<< PFN_SECTION_SHIFT
);
175 * The probe routines leave the pages reserved, just
176 * as the bootmem code does. Make sure they're still
179 if (action
== MEM_ONLINE
) {
180 for (i
= 0; i
< PAGES_PER_SECTION
; i
++) {
181 if (PageReserved(first_page
+i
))
184 printk(KERN_WARNING
"section number %ld page number %d "
185 "not reserved, was it already online? \n",
193 start_pfn
= page_to_pfn(first_page
);
194 ret
= online_pages(start_pfn
, PAGES_PER_SECTION
);
197 mem
->state
= MEM_GOING_OFFLINE
;
198 start_paddr
= page_to_pfn(first_page
) << PAGE_SHIFT
;
199 ret
= remove_memory(start_paddr
,
200 PAGES_PER_SECTION
<< PAGE_SHIFT
);
202 mem
->state
= old_state
;
207 printk(KERN_WARNING
"%s(%p, %ld) unknown action: %ld\n",
208 __func__
, mem
, action
, action
);
216 static int memory_block_change_state(struct memory_block
*mem
,
217 unsigned long to_state
, unsigned long from_state_req
)
220 mutex_lock(&mem
->state_mutex
);
222 if (mem
->state
!= from_state_req
) {
227 ret
= memory_block_action(mem
, to_state
);
229 mem
->state
= to_state
;
232 mutex_unlock(&mem
->state_mutex
);
237 store_mem_state(struct sys_device
*dev
,
238 struct sysdev_attribute
*attr
, const char *buf
, size_t count
)
240 struct memory_block
*mem
;
241 unsigned int phys_section_nr
;
244 mem
= container_of(dev
, struct memory_block
, sysdev
);
245 phys_section_nr
= mem
->phys_index
;
247 if (!present_section_nr(phys_section_nr
))
250 if (!strncmp(buf
, "online", min((int)count
, 6)))
251 ret
= memory_block_change_state(mem
, MEM_ONLINE
, MEM_OFFLINE
);
252 else if(!strncmp(buf
, "offline", min((int)count
, 7)))
253 ret
= memory_block_change_state(mem
, MEM_OFFLINE
, MEM_ONLINE
);
261 * phys_device is a bad name for this. What I really want
262 * is a way to differentiate between memory ranges that
263 * are part of physical devices that constitute
264 * a complete removable unit or fru.
265 * i.e. do these ranges belong to the same physical device,
266 * s.t. if I offline all of these sections I can then
267 * remove the physical device?
269 static ssize_t
show_phys_device(struct sys_device
*dev
,
270 struct sysdev_attribute
*attr
, char *buf
)
272 struct memory_block
*mem
=
273 container_of(dev
, struct memory_block
, sysdev
);
274 return sprintf(buf
, "%d\n", mem
->phys_device
);
277 static SYSDEV_ATTR(phys_index
, 0444, show_mem_phys_index
, NULL
);
278 static SYSDEV_ATTR(state
, 0644, show_mem_state
, store_mem_state
);
279 static SYSDEV_ATTR(phys_device
, 0444, show_phys_device
, NULL
);
280 static SYSDEV_ATTR(removable
, 0444, show_mem_removable
, NULL
);
282 #define mem_create_simple_file(mem, attr_name) \
283 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
284 #define mem_remove_simple_file(mem, attr_name) \
285 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
288 * Block size attribute stuff
291 print_block_size(struct class *class, char *buf
)
293 return sprintf(buf
, "%lx\n", (unsigned long)PAGES_PER_SECTION
* PAGE_SIZE
);
296 static CLASS_ATTR(block_size_bytes
, 0444, print_block_size
, NULL
);
298 static int block_size_init(void)
300 return sysfs_create_file(&memory_sysdev_class
.kset
.kobj
,
301 &class_attr_block_size_bytes
.attr
);
305 * Some architectures will have custom drivers to do this, and
306 * will not need to do it from userspace. The fake hot-add code
307 * as well as ppc64 will do all of their discovery in userspace
308 * and will require this interface.
310 #ifdef CONFIG_ARCH_MEMORY_PROBE
312 memory_probe_store(struct class *class, const char *buf
, size_t count
)
318 phys_addr
= simple_strtoull(buf
, NULL
, 0);
320 nid
= memory_add_physaddr_to_nid(phys_addr
);
321 ret
= add_memory(nid
, phys_addr
, PAGES_PER_SECTION
<< PAGE_SHIFT
);
328 static CLASS_ATTR(probe
, 0700, NULL
, memory_probe_store
);
330 static int memory_probe_init(void)
332 return sysfs_create_file(&memory_sysdev_class
.kset
.kobj
,
333 &class_attr_probe
.attr
);
336 static inline int memory_probe_init(void)
343 * Note that phys_device is optional. It is here to allow for
344 * differentiation between which *physical* devices each
345 * section belongs to...
348 static int add_memory_block(unsigned long node_id
, struct mem_section
*section
,
349 unsigned long state
, int phys_device
)
351 struct memory_block
*mem
= kzalloc(sizeof(*mem
), GFP_KERNEL
);
357 mem
->phys_index
= __section_nr(section
);
359 mutex_init(&mem
->state_mutex
);
360 mem
->phys_device
= phys_device
;
362 ret
= register_memory(mem
, section
);
364 ret
= mem_create_simple_file(mem
, phys_index
);
366 ret
= mem_create_simple_file(mem
, state
);
368 ret
= mem_create_simple_file(mem
, phys_device
);
370 ret
= mem_create_simple_file(mem
, removable
);
376 * For now, we have a linear search to go find the appropriate
377 * memory_block corresponding to a particular phys_index. If
378 * this gets to be a real problem, we can always use a radix
379 * tree or something here.
381 * This could be made generic for all sysdev classes.
383 static struct memory_block
*find_memory_block(struct mem_section
*section
)
385 struct kobject
*kobj
;
386 struct sys_device
*sysdev
;
387 struct memory_block
*mem
;
388 char name
[sizeof(MEMORY_CLASS_NAME
) + 9 + 1];
391 * This only works because we know that section == sysdev->id
392 * slightly redundant with sysdev_register()
394 sprintf(&name
[0], "%s%d", MEMORY_CLASS_NAME
, __section_nr(section
));
396 kobj
= kset_find_obj(&memory_sysdev_class
.kset
, name
);
400 sysdev
= container_of(kobj
, struct sys_device
, kobj
);
401 mem
= container_of(sysdev
, struct memory_block
, sysdev
);
406 int remove_memory_block(unsigned long node_id
, struct mem_section
*section
,
409 struct memory_block
*mem
;
411 mem
= find_memory_block(section
);
412 mem_remove_simple_file(mem
, phys_index
);
413 mem_remove_simple_file(mem
, state
);
414 mem_remove_simple_file(mem
, phys_device
);
415 mem_remove_simple_file(mem
, removable
);
416 unregister_memory(mem
, section
);
422 * need an interface for the VM to add new memory regions,
423 * but without onlining it.
425 int register_new_memory(struct mem_section
*section
)
427 return add_memory_block(0, section
, MEM_OFFLINE
, 0);
430 int unregister_memory_section(struct mem_section
*section
)
432 if (!present_section(section
))
435 return remove_memory_block(0, section
, 0);
439 * Initialize the sysfs support for memory devices...
441 int __init
memory_dev_init(void)
447 memory_sysdev_class
.kset
.uevent_ops
= &memory_uevent_ops
;
448 ret
= sysdev_class_register(&memory_sysdev_class
);
453 * Create entries for memory sections that were found
454 * during boot and have been initialized
456 for (i
= 0; i
< NR_MEM_SECTIONS
; i
++) {
457 if (!present_section_nr(i
))
459 err
= add_memory_block(0, __nr_to_section(i
), MEM_ONLINE
, 0);
464 err
= memory_probe_init();
467 err
= block_size_init();
472 printk(KERN_ERR
"%s() failed: %d\n", __func__
, ret
);