blob: 3ad49a00029ff91c39f00bb430de302f7ab409b7 [file] [log] [blame]
Dave Hansen3947be12005-10-29 18:16:54 -07001/*
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>
Dave Hansen3947be12005-10-29 18:16:54 -070016#include <linux/topology.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080017#include <linux/capability.h>
Dave Hansen3947be12005-10-29 18:16:54 -070018#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>
Daniel Walkerda19cbc2008-02-04 23:35:47 -080023#include <linux/mutex.h>
Dave Hansen3947be12005-10-29 18:16:54 -070024#include <asm/atomic.h>
25#include <asm/uaccess.h>
26
27#define MEMORY_CLASS_NAME "memory"
28
29static struct sysdev_class memory_sysdev_class = {
Kay Sieversaf5ca3f2007-12-20 02:09:39 +010030 .name = MEMORY_CLASS_NAME,
Dave Hansen3947be12005-10-29 18:16:54 -070031};
Dave Hansen3947be12005-10-29 18:16:54 -070032
Kay Sievers312c0042005-11-16 09:00:00 +010033static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj)
Dave Hansen3947be12005-10-29 18:16:54 -070034{
35 return MEMORY_CLASS_NAME;
36}
37
Al Viro9ec0fd4e2007-10-14 06:53:45 +010038static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env)
Dave Hansen3947be12005-10-29 18:16:54 -070039{
40 int retval = 0;
41
42 return retval;
43}
44
Kay Sievers312c0042005-11-16 09:00:00 +010045static struct kset_uevent_ops memory_uevent_ops = {
46 .name = memory_uevent_name,
47 .uevent = memory_uevent,
Dave Hansen3947be12005-10-29 18:16:54 -070048};
49
Alan Sterne041c682006-03-27 01:16:30 -080050static BLOCKING_NOTIFIER_HEAD(memory_chain);
Dave Hansen3947be12005-10-29 18:16:54 -070051
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080052int register_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070053{
Alan Sterne041c682006-03-27 01:16:30 -080054 return blocking_notifier_chain_register(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070055}
Hannes Hering3c82c302008-05-07 14:43:01 +020056EXPORT_SYMBOL(register_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070057
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080058void unregister_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070059{
Alan Sterne041c682006-03-27 01:16:30 -080060 blocking_notifier_chain_unregister(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070061}
Hannes Hering3c82c302008-05-07 14:43:01 +020062EXPORT_SYMBOL(unregister_memory_notifier);
Dave Hansen3947be12005-10-29 18:16:54 -070063
64/*
65 * register_memory - Setup a sysfs device for a memory block
66 */
Badari Pulavarty00a41db2008-02-11 09:23:18 -080067static
68int register_memory(struct memory_block *memory, struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -070069{
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);
Dave Hansen3947be12005-10-29 18:16:54 -070076 return error;
77}
78
79static void
Badari Pulavarty00a41db2008-02-11 09:23:18 -080080unregister_memory(struct memory_block *memory, struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -070081{
82 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
83 BUG_ON(memory->sysdev.id != __section_nr(section));
84
Badari Pulavarty00a41db2008-02-11 09:23:18 -080085 /* drop the ref. we got in remove_memory_block() */
86 kobject_put(&memory->sysdev.kobj);
Dave Hansen3947be12005-10-29 18:16:54 -070087 sysdev_unregister(&memory->sysdev);
Dave Hansen3947be12005-10-29 18:16:54 -070088}
89
90/*
91 * use this as the physical section index that this memsection
92 * uses.
93 */
94
Andi Kleen4a0b2b42008-07-01 18:48:41 +020095static ssize_t show_mem_phys_index(struct sys_device *dev,
96 struct sysdev_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -070097{
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/*
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700104 * Show whether the section of memory is likely to be hot-removable
105 */
106static ssize_t show_mem_removable(struct sys_device *dev, char *buf)
107{
108 unsigned long start_pfn;
109 int ret;
110 struct memory_block *mem =
111 container_of(dev, struct memory_block, sysdev);
112
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);
116}
117
118/*
Dave Hansen3947be12005-10-29 18:16:54 -0700119 * online, offline, going offline, etc.
120 */
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200121static ssize_t show_mem_state(struct sys_device *dev,
122 struct sysdev_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700123{
124 struct memory_block *mem =
125 container_of(dev, struct memory_block, sysdev);
126 ssize_t len = 0;
127
128 /*
129 * We can probably put these states in a nice little array
130 * so that they're not open-coded
131 */
132 switch (mem->state) {
133 case MEM_ONLINE:
134 len = sprintf(buf, "online\n");
135 break;
136 case MEM_OFFLINE:
137 len = sprintf(buf, "offline\n");
138 break;
139 case MEM_GOING_OFFLINE:
140 len = sprintf(buf, "going-offline\n");
141 break;
142 default:
143 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
144 mem->state);
145 WARN_ON(1);
146 break;
147 }
148
149 return len;
150}
151
Yasunori Goto7b78d332007-10-21 16:41:36 -0700152int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700153{
Alan Sterne041c682006-03-27 01:16:30 -0800154 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700155}
156
157/*
158 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
159 * OK to have direct references to sparsemem variables in here.
160 */
161static int
162memory_block_action(struct memory_block *mem, unsigned long action)
163{
164 int i;
165 unsigned long psection;
166 unsigned long start_pfn, start_paddr;
167 struct page *first_page;
168 int ret;
169 int old_state = mem->state;
170
171 psection = mem->phys_index;
172 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
173
174 /*
175 * The probe routines leave the pages reserved, just
176 * as the bootmem code does. Make sure they're still
177 * that way.
178 */
179 if (action == MEM_ONLINE) {
180 for (i = 0; i < PAGES_PER_SECTION; i++) {
181 if (PageReserved(first_page+i))
182 continue;
183
184 printk(KERN_WARNING "section number %ld page number %d "
185 "not reserved, was it already online? \n",
186 psection, i);
187 return -EBUSY;
188 }
189 }
190
191 switch (action) {
192 case MEM_ONLINE:
193 start_pfn = page_to_pfn(first_page);
194 ret = online_pages(start_pfn, PAGES_PER_SECTION);
195 break;
196 case MEM_OFFLINE:
197 mem->state = MEM_GOING_OFFLINE;
Dave Hansen3947be12005-10-29 18:16:54 -0700198 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
199 ret = remove_memory(start_paddr,
200 PAGES_PER_SECTION << PAGE_SHIFT);
201 if (ret) {
202 mem->state = old_state;
203 break;
204 }
Dave Hansen3947be12005-10-29 18:16:54 -0700205 break;
206 default:
Arjan van de Venf810a5c2008-07-25 19:45:39 -0700207 WARN(1, KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800208 __func__, mem, action, action);
Dave Hansen3947be12005-10-29 18:16:54 -0700209 ret = -EINVAL;
210 }
Dave Hansen3947be12005-10-29 18:16:54 -0700211
212 return ret;
213}
214
215static int memory_block_change_state(struct memory_block *mem,
216 unsigned long to_state, unsigned long from_state_req)
217{
218 int ret = 0;
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800219 mutex_lock(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700220
221 if (mem->state != from_state_req) {
222 ret = -EINVAL;
223 goto out;
224 }
225
226 ret = memory_block_action(mem, to_state);
227 if (!ret)
228 mem->state = to_state;
229
230out:
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800231 mutex_unlock(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700232 return ret;
233}
234
235static ssize_t
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200236store_mem_state(struct sys_device *dev,
237 struct sysdev_attribute *attr, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700238{
239 struct memory_block *mem;
240 unsigned int phys_section_nr;
241 int ret = -EINVAL;
242
243 mem = container_of(dev, struct memory_block, sysdev);
244 phys_section_nr = mem->phys_index;
245
Andy Whitcroft540557b2007-10-16 01:24:11 -0700246 if (!present_section_nr(phys_section_nr))
Dave Hansen3947be12005-10-29 18:16:54 -0700247 goto out;
248
249 if (!strncmp(buf, "online", min((int)count, 6)))
250 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
251 else if(!strncmp(buf, "offline", min((int)count, 7)))
252 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
253out:
254 if (ret)
255 return ret;
256 return count;
257}
258
259/*
260 * phys_device is a bad name for this. What I really want
261 * is a way to differentiate between memory ranges that
262 * are part of physical devices that constitute
263 * a complete removable unit or fru.
264 * i.e. do these ranges belong to the same physical device,
265 * s.t. if I offline all of these sections I can then
266 * remove the physical device?
267 */
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200268static ssize_t show_phys_device(struct sys_device *dev,
269 struct sysdev_attribute *attr, char *buf)
Dave Hansen3947be12005-10-29 18:16:54 -0700270{
271 struct memory_block *mem =
272 container_of(dev, struct memory_block, sysdev);
273 return sprintf(buf, "%d\n", mem->phys_device);
274}
275
276static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
277static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
278static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700279static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL);
Dave Hansen3947be12005-10-29 18:16:54 -0700280
281#define mem_create_simple_file(mem, attr_name) \
282 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
283#define mem_remove_simple_file(mem, attr_name) \
284 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
285
286/*
287 * Block size attribute stuff
288 */
289static ssize_t
290print_block_size(struct class *class, char *buf)
291{
292 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
293}
294
295static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
296
297static int block_size_init(void)
298{
Andrew Morton28ec24e2006-12-06 20:37:29 -0800299 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
300 &class_attr_block_size_bytes.attr);
Dave Hansen3947be12005-10-29 18:16:54 -0700301}
302
303/*
304 * Some architectures will have custom drivers to do this, and
305 * will not need to do it from userspace. The fake hot-add code
306 * as well as ppc64 will do all of their discovery in userspace
307 * and will require this interface.
308 */
309#ifdef CONFIG_ARCH_MEMORY_PROBE
310static ssize_t
Al Virobe7ee9b2006-02-01 06:06:16 -0500311memory_probe_store(struct class *class, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700312{
313 u64 phys_addr;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700314 int nid;
Dave Hansen3947be12005-10-29 18:16:54 -0700315 int ret;
316
317 phys_addr = simple_strtoull(buf, NULL, 0);
318
Yasunori Gotobc02af92006-06-27 02:53:30 -0700319 nid = memory_add_physaddr_to_nid(phys_addr);
320 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700321
322 if (ret)
323 count = ret;
324
325 return count;
326}
327static CLASS_ATTR(probe, 0700, NULL, memory_probe_store);
328
329static int memory_probe_init(void)
330{
Andrew Morton28ec24e2006-12-06 20:37:29 -0800331 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
332 &class_attr_probe.attr);
Dave Hansen3947be12005-10-29 18:16:54 -0700333}
334#else
Andrew Morton28ec24e2006-12-06 20:37:29 -0800335static inline int memory_probe_init(void)
336{
337 return 0;
338}
Dave Hansen3947be12005-10-29 18:16:54 -0700339#endif
340
341/*
342 * Note that phys_device is optional. It is here to allow for
343 * differentiation between which *physical* devices each
344 * section belongs to...
345 */
346
347static int add_memory_block(unsigned long node_id, struct mem_section *section,
348 unsigned long state, int phys_device)
349{
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700350 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Dave Hansen3947be12005-10-29 18:16:54 -0700351 int ret = 0;
352
353 if (!mem)
354 return -ENOMEM;
355
Dave Hansen3947be12005-10-29 18:16:54 -0700356 mem->phys_index = __section_nr(section);
357 mem->state = state;
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800358 mutex_init(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700359 mem->phys_device = phys_device;
360
Badari Pulavarty00a41db2008-02-11 09:23:18 -0800361 ret = register_memory(mem, section);
Dave Hansen3947be12005-10-29 18:16:54 -0700362 if (!ret)
363 ret = mem_create_simple_file(mem, phys_index);
364 if (!ret)
365 ret = mem_create_simple_file(mem, state);
366 if (!ret)
367 ret = mem_create_simple_file(mem, phys_device);
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700368 if (!ret)
369 ret = mem_create_simple_file(mem, removable);
Dave Hansen3947be12005-10-29 18:16:54 -0700370
371 return ret;
372}
373
374/*
375 * For now, we have a linear search to go find the appropriate
376 * memory_block corresponding to a particular phys_index. If
377 * this gets to be a real problem, we can always use a radix
378 * tree or something here.
379 *
380 * This could be made generic for all sysdev classes.
381 */
382static struct memory_block *find_memory_block(struct mem_section *section)
383{
384 struct kobject *kobj;
385 struct sys_device *sysdev;
386 struct memory_block *mem;
387 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
388
389 /*
390 * This only works because we know that section == sysdev->id
391 * slightly redundant with sysdev_register()
392 */
393 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
394
395 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
396 if (!kobj)
397 return NULL;
398
399 sysdev = container_of(kobj, struct sys_device, kobj);
400 mem = container_of(sysdev, struct memory_block, sysdev);
401
402 return mem;
403}
404
405int remove_memory_block(unsigned long node_id, struct mem_section *section,
406 int phys_device)
407{
408 struct memory_block *mem;
409
410 mem = find_memory_block(section);
411 mem_remove_simple_file(mem, phys_index);
412 mem_remove_simple_file(mem, state);
413 mem_remove_simple_file(mem, phys_device);
Badari Pulavarty5c755e92008-07-23 21:28:19 -0700414 mem_remove_simple_file(mem, removable);
Badari Pulavarty00a41db2008-02-11 09:23:18 -0800415 unregister_memory(mem, section);
Dave Hansen3947be12005-10-29 18:16:54 -0700416
417 return 0;
418}
419
420/*
421 * need an interface for the VM to add new memory regions,
422 * but without onlining it.
423 */
424int register_new_memory(struct mem_section *section)
425{
426 return add_memory_block(0, section, MEM_OFFLINE, 0);
427}
428
429int unregister_memory_section(struct mem_section *section)
430{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700431 if (!present_section(section))
Dave Hansen3947be12005-10-29 18:16:54 -0700432 return -EINVAL;
433
434 return remove_memory_block(0, section, 0);
435}
436
437/*
438 * Initialize the sysfs support for memory devices...
439 */
440int __init memory_dev_init(void)
441{
442 unsigned int i;
443 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800444 int err;
Dave Hansen3947be12005-10-29 18:16:54 -0700445
Kay Sievers312c0042005-11-16 09:00:00 +0100446 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
Dave Hansen3947be12005-10-29 18:16:54 -0700447 ret = sysdev_class_register(&memory_sysdev_class);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800448 if (ret)
449 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700450
451 /*
452 * Create entries for memory sections that were found
453 * during boot and have been initialized
454 */
455 for (i = 0; i < NR_MEM_SECTIONS; i++) {
Andy Whitcroft540557b2007-10-16 01:24:11 -0700456 if (!present_section_nr(i))
Dave Hansen3947be12005-10-29 18:16:54 -0700457 continue;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800458 err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
459 if (!ret)
460 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700461 }
462
Andrew Morton28ec24e2006-12-06 20:37:29 -0800463 err = memory_probe_init();
464 if (!ret)
465 ret = err;
466 err = block_size_init();
467 if (!ret)
468 ret = err;
469out:
470 if (ret)
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800471 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700472 return ret;
473}