blob: 1f3801a8184da7f8b3503f28861c506ab496b760 [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 Viro9ec0fd42007-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}
56
Andy Whitcroft98a38eb2006-01-06 00:10:35 -080057void unregister_memory_notifier(struct notifier_block *nb)
Dave Hansen3947be12005-10-29 18:16:54 -070058{
Alan Sterne041c682006-03-27 01:16:30 -080059 blocking_notifier_chain_unregister(&memory_chain, nb);
Dave Hansen3947be12005-10-29 18:16:54 -070060}
61
62/*
63 * register_memory - Setup a sysfs device for a memory block
64 */
Andy Whitcroft900b2b42006-01-06 00:10:35 -080065int register_memory(struct memory_block *memory, struct mem_section *section,
Dave Hansen3947be12005-10-29 18:16:54 -070066 struct node *root)
67{
68 int error;
69
70 memory->sysdev.cls = &memory_sysdev_class;
71 memory->sysdev.id = __section_nr(section);
72
73 error = sysdev_register(&memory->sysdev);
74
75 if (root && !error)
76 error = sysfs_create_link(&root->sysdev.kobj,
77 &memory->sysdev.kobj,
78 kobject_name(&memory->sysdev.kobj));
79
80 return error;
81}
82
83static void
84unregister_memory(struct memory_block *memory, struct mem_section *section,
85 struct node *root)
86{
87 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
88 BUG_ON(memory->sysdev.id != __section_nr(section));
89
90 sysdev_unregister(&memory->sysdev);
91 if (root)
92 sysfs_remove_link(&root->sysdev.kobj,
93 kobject_name(&memory->sysdev.kobj));
94}
95
96/*
97 * use this as the physical section index that this memsection
98 * uses.
99 */
100
101static ssize_t show_mem_phys_index(struct sys_device *dev, char *buf)
102{
103 struct memory_block *mem =
104 container_of(dev, struct memory_block, sysdev);
105 return sprintf(buf, "%08lx\n", mem->phys_index);
106}
107
108/*
109 * online, offline, going offline, etc.
110 */
111static ssize_t show_mem_state(struct sys_device *dev, char *buf)
112{
113 struct memory_block *mem =
114 container_of(dev, struct memory_block, sysdev);
115 ssize_t len = 0;
116
117 /*
118 * We can probably put these states in a nice little array
119 * so that they're not open-coded
120 */
121 switch (mem->state) {
122 case MEM_ONLINE:
123 len = sprintf(buf, "online\n");
124 break;
125 case MEM_OFFLINE:
126 len = sprintf(buf, "offline\n");
127 break;
128 case MEM_GOING_OFFLINE:
129 len = sprintf(buf, "going-offline\n");
130 break;
131 default:
132 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
133 mem->state);
134 WARN_ON(1);
135 break;
136 }
137
138 return len;
139}
140
Yasunori Goto7b78d332007-10-21 16:41:36 -0700141int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700142{
Alan Sterne041c682006-03-27 01:16:30 -0800143 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700144}
145
146/*
147 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
148 * OK to have direct references to sparsemem variables in here.
149 */
150static int
151memory_block_action(struct memory_block *mem, unsigned long action)
152{
153 int i;
154 unsigned long psection;
155 unsigned long start_pfn, start_paddr;
156 struct page *first_page;
157 int ret;
158 int old_state = mem->state;
159
160 psection = mem->phys_index;
161 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
162
163 /*
164 * The probe routines leave the pages reserved, just
165 * as the bootmem code does. Make sure they're still
166 * that way.
167 */
168 if (action == MEM_ONLINE) {
169 for (i = 0; i < PAGES_PER_SECTION; i++) {
170 if (PageReserved(first_page+i))
171 continue;
172
173 printk(KERN_WARNING "section number %ld page number %d "
174 "not reserved, was it already online? \n",
175 psection, i);
176 return -EBUSY;
177 }
178 }
179
180 switch (action) {
181 case MEM_ONLINE:
182 start_pfn = page_to_pfn(first_page);
183 ret = online_pages(start_pfn, PAGES_PER_SECTION);
184 break;
185 case MEM_OFFLINE:
186 mem->state = MEM_GOING_OFFLINE;
Dave Hansen3947be12005-10-29 18:16:54 -0700187 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
188 ret = remove_memory(start_paddr,
189 PAGES_PER_SECTION << PAGE_SHIFT);
190 if (ret) {
191 mem->state = old_state;
192 break;
193 }
Dave Hansen3947be12005-10-29 18:16:54 -0700194 break;
195 default:
196 printk(KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
197 __FUNCTION__, mem, action, action);
198 WARN_ON(1);
199 ret = -EINVAL;
200 }
Dave Hansen3947be12005-10-29 18:16:54 -0700201
202 return ret;
203}
204
205static int memory_block_change_state(struct memory_block *mem,
206 unsigned long to_state, unsigned long from_state_req)
207{
208 int ret = 0;
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800209 mutex_lock(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700210
211 if (mem->state != from_state_req) {
212 ret = -EINVAL;
213 goto out;
214 }
215
216 ret = memory_block_action(mem, to_state);
217 if (!ret)
218 mem->state = to_state;
219
220out:
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800221 mutex_unlock(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700222 return ret;
223}
224
225static ssize_t
226store_mem_state(struct sys_device *dev, const char *buf, size_t count)
227{
228 struct memory_block *mem;
229 unsigned int phys_section_nr;
230 int ret = -EINVAL;
231
232 mem = container_of(dev, struct memory_block, sysdev);
233 phys_section_nr = mem->phys_index;
234
Andy Whitcroft540557b2007-10-16 01:24:11 -0700235 if (!present_section_nr(phys_section_nr))
Dave Hansen3947be12005-10-29 18:16:54 -0700236 goto out;
237
238 if (!strncmp(buf, "online", min((int)count, 6)))
239 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
240 else if(!strncmp(buf, "offline", min((int)count, 7)))
241 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
242out:
243 if (ret)
244 return ret;
245 return count;
246}
247
248/*
249 * phys_device is a bad name for this. What I really want
250 * is a way to differentiate between memory ranges that
251 * are part of physical devices that constitute
252 * a complete removable unit or fru.
253 * i.e. do these ranges belong to the same physical device,
254 * s.t. if I offline all of these sections I can then
255 * remove the physical device?
256 */
257static ssize_t show_phys_device(struct sys_device *dev, char *buf)
258{
259 struct memory_block *mem =
260 container_of(dev, struct memory_block, sysdev);
261 return sprintf(buf, "%d\n", mem->phys_device);
262}
263
264static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
265static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
266static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
267
268#define mem_create_simple_file(mem, attr_name) \
269 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
270#define mem_remove_simple_file(mem, attr_name) \
271 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
272
273/*
274 * Block size attribute stuff
275 */
276static ssize_t
277print_block_size(struct class *class, char *buf)
278{
279 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
280}
281
282static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
283
284static int block_size_init(void)
285{
Andrew Morton28ec24e2006-12-06 20:37:29 -0800286 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
287 &class_attr_block_size_bytes.attr);
Dave Hansen3947be12005-10-29 18:16:54 -0700288}
289
290/*
291 * Some architectures will have custom drivers to do this, and
292 * will not need to do it from userspace. The fake hot-add code
293 * as well as ppc64 will do all of their discovery in userspace
294 * and will require this interface.
295 */
296#ifdef CONFIG_ARCH_MEMORY_PROBE
297static ssize_t
Al Virobe7ee9b2006-02-01 06:06:16 -0500298memory_probe_store(struct class *class, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700299{
300 u64 phys_addr;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700301 int nid;
Dave Hansen3947be12005-10-29 18:16:54 -0700302 int ret;
303
304 phys_addr = simple_strtoull(buf, NULL, 0);
305
Yasunori Gotobc02af92006-06-27 02:53:30 -0700306 nid = memory_add_physaddr_to_nid(phys_addr);
307 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700308
309 if (ret)
310 count = ret;
311
312 return count;
313}
314static CLASS_ATTR(probe, 0700, NULL, memory_probe_store);
315
316static int memory_probe_init(void)
317{
Andrew Morton28ec24e2006-12-06 20:37:29 -0800318 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
319 &class_attr_probe.attr);
Dave Hansen3947be12005-10-29 18:16:54 -0700320}
321#else
Andrew Morton28ec24e2006-12-06 20:37:29 -0800322static inline int memory_probe_init(void)
323{
324 return 0;
325}
Dave Hansen3947be12005-10-29 18:16:54 -0700326#endif
327
328/*
329 * Note that phys_device is optional. It is here to allow for
330 * differentiation between which *physical* devices each
331 * section belongs to...
332 */
333
334static int add_memory_block(unsigned long node_id, struct mem_section *section,
335 unsigned long state, int phys_device)
336{
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700337 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Dave Hansen3947be12005-10-29 18:16:54 -0700338 int ret = 0;
339
340 if (!mem)
341 return -ENOMEM;
342
Dave Hansen3947be12005-10-29 18:16:54 -0700343 mem->phys_index = __section_nr(section);
344 mem->state = state;
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800345 mutex_init(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700346 mem->phys_device = phys_device;
347
348 ret = register_memory(mem, section, NULL);
349 if (!ret)
350 ret = mem_create_simple_file(mem, phys_index);
351 if (!ret)
352 ret = mem_create_simple_file(mem, state);
353 if (!ret)
354 ret = mem_create_simple_file(mem, phys_device);
355
356 return ret;
357}
358
359/*
360 * For now, we have a linear search to go find the appropriate
361 * memory_block corresponding to a particular phys_index. If
362 * this gets to be a real problem, we can always use a radix
363 * tree or something here.
364 *
365 * This could be made generic for all sysdev classes.
366 */
367static struct memory_block *find_memory_block(struct mem_section *section)
368{
369 struct kobject *kobj;
370 struct sys_device *sysdev;
371 struct memory_block *mem;
372 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
373
374 /*
375 * This only works because we know that section == sysdev->id
376 * slightly redundant with sysdev_register()
377 */
378 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
379
380 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
381 if (!kobj)
382 return NULL;
383
384 sysdev = container_of(kobj, struct sys_device, kobj);
385 mem = container_of(sysdev, struct memory_block, sysdev);
386
387 return mem;
388}
389
390int remove_memory_block(unsigned long node_id, struct mem_section *section,
391 int phys_device)
392{
393 struct memory_block *mem;
394
395 mem = find_memory_block(section);
396 mem_remove_simple_file(mem, phys_index);
397 mem_remove_simple_file(mem, state);
398 mem_remove_simple_file(mem, phys_device);
399 unregister_memory(mem, section, NULL);
400
401 return 0;
402}
403
404/*
405 * need an interface for the VM to add new memory regions,
406 * but without onlining it.
407 */
408int register_new_memory(struct mem_section *section)
409{
410 return add_memory_block(0, section, MEM_OFFLINE, 0);
411}
412
413int unregister_memory_section(struct mem_section *section)
414{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700415 if (!present_section(section))
Dave Hansen3947be12005-10-29 18:16:54 -0700416 return -EINVAL;
417
418 return remove_memory_block(0, section, 0);
419}
420
421/*
422 * Initialize the sysfs support for memory devices...
423 */
424int __init memory_dev_init(void)
425{
426 unsigned int i;
427 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800428 int err;
Dave Hansen3947be12005-10-29 18:16:54 -0700429
Kay Sievers312c0042005-11-16 09:00:00 +0100430 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
Dave Hansen3947be12005-10-29 18:16:54 -0700431 ret = sysdev_class_register(&memory_sysdev_class);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800432 if (ret)
433 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700434
435 /*
436 * Create entries for memory sections that were found
437 * during boot and have been initialized
438 */
439 for (i = 0; i < NR_MEM_SECTIONS; i++) {
Andy Whitcroft540557b2007-10-16 01:24:11 -0700440 if (!present_section_nr(i))
Dave Hansen3947be12005-10-29 18:16:54 -0700441 continue;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800442 err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
443 if (!ret)
444 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700445 }
446
Andrew Morton28ec24e2006-12-06 20:37:29 -0800447 err = memory_probe_init();
448 if (!ret)
449 ret = err;
450 err = block_size_init();
451 if (!ret)
452 ret = err;
453out:
454 if (ret)
455 printk(KERN_ERR "%s() failed: %d\n", __FUNCTION__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700456 return ret;
457}