blob: 7891f7c97267402f19569cf8a1b015fc83bb2378 [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}
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 */
Badari Pulavarty00a41db2008-02-11 09:23:18 -080065static
66int register_memory(struct memory_block *memory, struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -070067{
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);
Dave Hansen3947be12005-10-29 18:16:54 -070074 return error;
75}
76
77static void
Badari Pulavarty00a41db2008-02-11 09:23:18 -080078unregister_memory(struct memory_block *memory, struct mem_section *section)
Dave Hansen3947be12005-10-29 18:16:54 -070079{
80 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
81 BUG_ON(memory->sysdev.id != __section_nr(section));
82
Badari Pulavarty00a41db2008-02-11 09:23:18 -080083 /* drop the ref. we got in remove_memory_block() */
84 kobject_put(&memory->sysdev.kobj);
Dave Hansen3947be12005-10-29 18:16:54 -070085 sysdev_unregister(&memory->sysdev);
Dave Hansen3947be12005-10-29 18:16:54 -070086}
87
88/*
89 * use this as the physical section index that this memsection
90 * uses.
91 */
92
93static ssize_t show_mem_phys_index(struct sys_device *dev, char *buf)
94{
95 struct memory_block *mem =
96 container_of(dev, struct memory_block, sysdev);
97 return sprintf(buf, "%08lx\n", mem->phys_index);
98}
99
100/*
101 * online, offline, going offline, etc.
102 */
103static ssize_t show_mem_state(struct sys_device *dev, char *buf)
104{
105 struct memory_block *mem =
106 container_of(dev, struct memory_block, sysdev);
107 ssize_t len = 0;
108
109 /*
110 * We can probably put these states in a nice little array
111 * so that they're not open-coded
112 */
113 switch (mem->state) {
114 case MEM_ONLINE:
115 len = sprintf(buf, "online\n");
116 break;
117 case MEM_OFFLINE:
118 len = sprintf(buf, "offline\n");
119 break;
120 case MEM_GOING_OFFLINE:
121 len = sprintf(buf, "going-offline\n");
122 break;
123 default:
124 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
125 mem->state);
126 WARN_ON(1);
127 break;
128 }
129
130 return len;
131}
132
Yasunori Goto7b78d332007-10-21 16:41:36 -0700133int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700134{
Alan Sterne041c682006-03-27 01:16:30 -0800135 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700136}
137
138/*
139 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
140 * OK to have direct references to sparsemem variables in here.
141 */
142static int
143memory_block_action(struct memory_block *mem, unsigned long action)
144{
145 int i;
146 unsigned long psection;
147 unsigned long start_pfn, start_paddr;
148 struct page *first_page;
149 int ret;
150 int old_state = mem->state;
151
152 psection = mem->phys_index;
153 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
154
155 /*
156 * The probe routines leave the pages reserved, just
157 * as the bootmem code does. Make sure they're still
158 * that way.
159 */
160 if (action == MEM_ONLINE) {
161 for (i = 0; i < PAGES_PER_SECTION; i++) {
162 if (PageReserved(first_page+i))
163 continue;
164
165 printk(KERN_WARNING "section number %ld page number %d "
166 "not reserved, was it already online? \n",
167 psection, i);
168 return -EBUSY;
169 }
170 }
171
172 switch (action) {
173 case MEM_ONLINE:
174 start_pfn = page_to_pfn(first_page);
175 ret = online_pages(start_pfn, PAGES_PER_SECTION);
176 break;
177 case MEM_OFFLINE:
178 mem->state = MEM_GOING_OFFLINE;
Dave Hansen3947be12005-10-29 18:16:54 -0700179 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
180 ret = remove_memory(start_paddr,
181 PAGES_PER_SECTION << PAGE_SHIFT);
182 if (ret) {
183 mem->state = old_state;
184 break;
185 }
Dave Hansen3947be12005-10-29 18:16:54 -0700186 break;
187 default:
188 printk(KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
189 __FUNCTION__, mem, action, action);
190 WARN_ON(1);
191 ret = -EINVAL;
192 }
Dave Hansen3947be12005-10-29 18:16:54 -0700193
194 return ret;
195}
196
197static int memory_block_change_state(struct memory_block *mem,
198 unsigned long to_state, unsigned long from_state_req)
199{
200 int ret = 0;
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800201 mutex_lock(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700202
203 if (mem->state != from_state_req) {
204 ret = -EINVAL;
205 goto out;
206 }
207
208 ret = memory_block_action(mem, to_state);
209 if (!ret)
210 mem->state = to_state;
211
212out:
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800213 mutex_unlock(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700214 return ret;
215}
216
217static ssize_t
218store_mem_state(struct sys_device *dev, const char *buf, size_t count)
219{
220 struct memory_block *mem;
221 unsigned int phys_section_nr;
222 int ret = -EINVAL;
223
224 mem = container_of(dev, struct memory_block, sysdev);
225 phys_section_nr = mem->phys_index;
226
Andy Whitcroft540557b2007-10-16 01:24:11 -0700227 if (!present_section_nr(phys_section_nr))
Dave Hansen3947be12005-10-29 18:16:54 -0700228 goto out;
229
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);
234out:
235 if (ret)
236 return ret;
237 return count;
238}
239
240/*
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?
248 */
249static ssize_t show_phys_device(struct sys_device *dev, char *buf)
250{
251 struct memory_block *mem =
252 container_of(dev, struct memory_block, sysdev);
253 return sprintf(buf, "%d\n", mem->phys_device);
254}
255
256static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
257static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
258static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
259
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)
264
265/*
266 * Block size attribute stuff
267 */
268static ssize_t
269print_block_size(struct class *class, char *buf)
270{
271 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
272}
273
274static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
275
276static int block_size_init(void)
277{
Andrew Morton28ec24e2006-12-06 20:37:29 -0800278 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
279 &class_attr_block_size_bytes.attr);
Dave Hansen3947be12005-10-29 18:16:54 -0700280}
281
282/*
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.
287 */
288#ifdef CONFIG_ARCH_MEMORY_PROBE
289static ssize_t
Al Virobe7ee9b2006-02-01 06:06:16 -0500290memory_probe_store(struct class *class, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700291{
292 u64 phys_addr;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700293 int nid;
Dave Hansen3947be12005-10-29 18:16:54 -0700294 int ret;
295
296 phys_addr = simple_strtoull(buf, NULL, 0);
297
Yasunori Gotobc02af92006-06-27 02:53:30 -0700298 nid = memory_add_physaddr_to_nid(phys_addr);
299 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700300
301 if (ret)
302 count = ret;
303
304 return count;
305}
306static CLASS_ATTR(probe, 0700, NULL, memory_probe_store);
307
308static int memory_probe_init(void)
309{
Andrew Morton28ec24e2006-12-06 20:37:29 -0800310 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
311 &class_attr_probe.attr);
Dave Hansen3947be12005-10-29 18:16:54 -0700312}
313#else
Andrew Morton28ec24e2006-12-06 20:37:29 -0800314static inline int memory_probe_init(void)
315{
316 return 0;
317}
Dave Hansen3947be12005-10-29 18:16:54 -0700318#endif
319
320/*
321 * Note that phys_device is optional. It is here to allow for
322 * differentiation between which *physical* devices each
323 * section belongs to...
324 */
325
326static int add_memory_block(unsigned long node_id, struct mem_section *section,
327 unsigned long state, int phys_device)
328{
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700329 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Dave Hansen3947be12005-10-29 18:16:54 -0700330 int ret = 0;
331
332 if (!mem)
333 return -ENOMEM;
334
Dave Hansen3947be12005-10-29 18:16:54 -0700335 mem->phys_index = __section_nr(section);
336 mem->state = state;
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800337 mutex_init(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700338 mem->phys_device = phys_device;
339
Badari Pulavarty00a41db2008-02-11 09:23:18 -0800340 ret = register_memory(mem, section);
Dave Hansen3947be12005-10-29 18:16:54 -0700341 if (!ret)
342 ret = mem_create_simple_file(mem, phys_index);
343 if (!ret)
344 ret = mem_create_simple_file(mem, state);
345 if (!ret)
346 ret = mem_create_simple_file(mem, phys_device);
347
348 return ret;
349}
350
351/*
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.
356 *
357 * This could be made generic for all sysdev classes.
358 */
359static struct memory_block *find_memory_block(struct mem_section *section)
360{
361 struct kobject *kobj;
362 struct sys_device *sysdev;
363 struct memory_block *mem;
364 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
365
366 /*
367 * This only works because we know that section == sysdev->id
368 * slightly redundant with sysdev_register()
369 */
370 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
371
372 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
373 if (!kobj)
374 return NULL;
375
376 sysdev = container_of(kobj, struct sys_device, kobj);
377 mem = container_of(sysdev, struct memory_block, sysdev);
378
379 return mem;
380}
381
382int remove_memory_block(unsigned long node_id, struct mem_section *section,
383 int phys_device)
384{
385 struct memory_block *mem;
386
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);
Badari Pulavarty00a41db2008-02-11 09:23:18 -0800391 unregister_memory(mem, section);
Dave Hansen3947be12005-10-29 18:16:54 -0700392
393 return 0;
394}
395
396/*
397 * need an interface for the VM to add new memory regions,
398 * but without onlining it.
399 */
400int register_new_memory(struct mem_section *section)
401{
402 return add_memory_block(0, section, MEM_OFFLINE, 0);
403}
404
405int unregister_memory_section(struct mem_section *section)
406{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700407 if (!present_section(section))
Dave Hansen3947be12005-10-29 18:16:54 -0700408 return -EINVAL;
409
410 return remove_memory_block(0, section, 0);
411}
412
413/*
414 * Initialize the sysfs support for memory devices...
415 */
416int __init memory_dev_init(void)
417{
418 unsigned int i;
419 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800420 int err;
Dave Hansen3947be12005-10-29 18:16:54 -0700421
Kay Sievers312c0042005-11-16 09:00:00 +0100422 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
Dave Hansen3947be12005-10-29 18:16:54 -0700423 ret = sysdev_class_register(&memory_sysdev_class);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800424 if (ret)
425 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700426
427 /*
428 * Create entries for memory sections that were found
429 * during boot and have been initialized
430 */
431 for (i = 0; i < NR_MEM_SECTIONS; i++) {
Andy Whitcroft540557b2007-10-16 01:24:11 -0700432 if (!present_section_nr(i))
Dave Hansen3947be12005-10-29 18:16:54 -0700433 continue;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800434 err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
435 if (!ret)
436 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700437 }
438
Andrew Morton28ec24e2006-12-06 20:37:29 -0800439 err = memory_probe_init();
440 if (!ret)
441 ret = err;
442 err = block_size_init();
443 if (!ret)
444 ret = err;
445out:
446 if (ret)
447 printk(KERN_ERR "%s() failed: %d\n", __FUNCTION__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700448 return ret;
449}