blob: 937e8258981db78be0b81303d63a8e7ebc30d692 [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}
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
95static ssize_t show_mem_phys_index(struct sys_device *dev, char *buf)
96{
97 struct memory_block *mem =
98 container_of(dev, struct memory_block, sysdev);
99 return sprintf(buf, "%08lx\n", mem->phys_index);
100}
101
102/*
103 * online, offline, going offline, etc.
104 */
105static ssize_t show_mem_state(struct sys_device *dev, char *buf)
106{
107 struct memory_block *mem =
108 container_of(dev, struct memory_block, sysdev);
109 ssize_t len = 0;
110
111 /*
112 * We can probably put these states in a nice little array
113 * so that they're not open-coded
114 */
115 switch (mem->state) {
116 case MEM_ONLINE:
117 len = sprintf(buf, "online\n");
118 break;
119 case MEM_OFFLINE:
120 len = sprintf(buf, "offline\n");
121 break;
122 case MEM_GOING_OFFLINE:
123 len = sprintf(buf, "going-offline\n");
124 break;
125 default:
126 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
127 mem->state);
128 WARN_ON(1);
129 break;
130 }
131
132 return len;
133}
134
Yasunori Goto7b78d332007-10-21 16:41:36 -0700135int memory_notify(unsigned long val, void *v)
Dave Hansen3947be12005-10-29 18:16:54 -0700136{
Alan Sterne041c682006-03-27 01:16:30 -0800137 return blocking_notifier_call_chain(&memory_chain, val, v);
Dave Hansen3947be12005-10-29 18:16:54 -0700138}
139
140/*
141 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
142 * OK to have direct references to sparsemem variables in here.
143 */
144static int
145memory_block_action(struct memory_block *mem, unsigned long action)
146{
147 int i;
148 unsigned long psection;
149 unsigned long start_pfn, start_paddr;
150 struct page *first_page;
151 int ret;
152 int old_state = mem->state;
153
154 psection = mem->phys_index;
155 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
156
157 /*
158 * The probe routines leave the pages reserved, just
159 * as the bootmem code does. Make sure they're still
160 * that way.
161 */
162 if (action == MEM_ONLINE) {
163 for (i = 0; i < PAGES_PER_SECTION; i++) {
164 if (PageReserved(first_page+i))
165 continue;
166
167 printk(KERN_WARNING "section number %ld page number %d "
168 "not reserved, was it already online? \n",
169 psection, i);
170 return -EBUSY;
171 }
172 }
173
174 switch (action) {
175 case MEM_ONLINE:
176 start_pfn = page_to_pfn(first_page);
177 ret = online_pages(start_pfn, PAGES_PER_SECTION);
178 break;
179 case MEM_OFFLINE:
180 mem->state = MEM_GOING_OFFLINE;
Dave Hansen3947be12005-10-29 18:16:54 -0700181 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
182 ret = remove_memory(start_paddr,
183 PAGES_PER_SECTION << PAGE_SHIFT);
184 if (ret) {
185 mem->state = old_state;
186 break;
187 }
Dave Hansen3947be12005-10-29 18:16:54 -0700188 break;
189 default:
190 printk(KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800191 __func__, mem, action, action);
Dave Hansen3947be12005-10-29 18:16:54 -0700192 WARN_ON(1);
193 ret = -EINVAL;
194 }
Dave Hansen3947be12005-10-29 18:16:54 -0700195
196 return ret;
197}
198
199static int memory_block_change_state(struct memory_block *mem,
200 unsigned long to_state, unsigned long from_state_req)
201{
202 int ret = 0;
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800203 mutex_lock(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700204
205 if (mem->state != from_state_req) {
206 ret = -EINVAL;
207 goto out;
208 }
209
210 ret = memory_block_action(mem, to_state);
211 if (!ret)
212 mem->state = to_state;
213
214out:
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800215 mutex_unlock(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700216 return ret;
217}
218
219static ssize_t
220store_mem_state(struct sys_device *dev, const char *buf, size_t count)
221{
222 struct memory_block *mem;
223 unsigned int phys_section_nr;
224 int ret = -EINVAL;
225
226 mem = container_of(dev, struct memory_block, sysdev);
227 phys_section_nr = mem->phys_index;
228
Andy Whitcroft540557b2007-10-16 01:24:11 -0700229 if (!present_section_nr(phys_section_nr))
Dave Hansen3947be12005-10-29 18:16:54 -0700230 goto out;
231
232 if (!strncmp(buf, "online", min((int)count, 6)))
233 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
234 else if(!strncmp(buf, "offline", min((int)count, 7)))
235 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
236out:
237 if (ret)
238 return ret;
239 return count;
240}
241
242/*
243 * phys_device is a bad name for this. What I really want
244 * is a way to differentiate between memory ranges that
245 * are part of physical devices that constitute
246 * a complete removable unit or fru.
247 * i.e. do these ranges belong to the same physical device,
248 * s.t. if I offline all of these sections I can then
249 * remove the physical device?
250 */
251static ssize_t show_phys_device(struct sys_device *dev, char *buf)
252{
253 struct memory_block *mem =
254 container_of(dev, struct memory_block, sysdev);
255 return sprintf(buf, "%d\n", mem->phys_device);
256}
257
258static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
259static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
260static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
261
262#define mem_create_simple_file(mem, attr_name) \
263 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
264#define mem_remove_simple_file(mem, attr_name) \
265 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
266
267/*
268 * Block size attribute stuff
269 */
270static ssize_t
271print_block_size(struct class *class, char *buf)
272{
273 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
274}
275
276static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
277
278static int block_size_init(void)
279{
Andrew Morton28ec24e2006-12-06 20:37:29 -0800280 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
281 &class_attr_block_size_bytes.attr);
Dave Hansen3947be12005-10-29 18:16:54 -0700282}
283
284/*
285 * Some architectures will have custom drivers to do this, and
286 * will not need to do it from userspace. The fake hot-add code
287 * as well as ppc64 will do all of their discovery in userspace
288 * and will require this interface.
289 */
290#ifdef CONFIG_ARCH_MEMORY_PROBE
291static ssize_t
Al Virobe7ee9b2006-02-01 06:06:16 -0500292memory_probe_store(struct class *class, const char *buf, size_t count)
Dave Hansen3947be12005-10-29 18:16:54 -0700293{
294 u64 phys_addr;
Yasunori Gotobc02af92006-06-27 02:53:30 -0700295 int nid;
Dave Hansen3947be12005-10-29 18:16:54 -0700296 int ret;
297
298 phys_addr = simple_strtoull(buf, NULL, 0);
299
Yasunori Gotobc02af92006-06-27 02:53:30 -0700300 nid = memory_add_physaddr_to_nid(phys_addr);
301 ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
Dave Hansen3947be12005-10-29 18:16:54 -0700302
303 if (ret)
304 count = ret;
305
306 return count;
307}
308static CLASS_ATTR(probe, 0700, NULL, memory_probe_store);
309
310static int memory_probe_init(void)
311{
Andrew Morton28ec24e2006-12-06 20:37:29 -0800312 return sysfs_create_file(&memory_sysdev_class.kset.kobj,
313 &class_attr_probe.attr);
Dave Hansen3947be12005-10-29 18:16:54 -0700314}
315#else
Andrew Morton28ec24e2006-12-06 20:37:29 -0800316static inline int memory_probe_init(void)
317{
318 return 0;
319}
Dave Hansen3947be12005-10-29 18:16:54 -0700320#endif
321
322/*
323 * Note that phys_device is optional. It is here to allow for
324 * differentiation between which *physical* devices each
325 * section belongs to...
326 */
327
328static int add_memory_block(unsigned long node_id, struct mem_section *section,
329 unsigned long state, int phys_device)
330{
Dave Hansen0b0acbe2005-10-29 18:16:55 -0700331 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
Dave Hansen3947be12005-10-29 18:16:54 -0700332 int ret = 0;
333
334 if (!mem)
335 return -ENOMEM;
336
Dave Hansen3947be12005-10-29 18:16:54 -0700337 mem->phys_index = __section_nr(section);
338 mem->state = state;
Daniel Walkerda19cbc2008-02-04 23:35:47 -0800339 mutex_init(&mem->state_mutex);
Dave Hansen3947be12005-10-29 18:16:54 -0700340 mem->phys_device = phys_device;
341
Badari Pulavarty00a41db2008-02-11 09:23:18 -0800342 ret = register_memory(mem, section);
Dave Hansen3947be12005-10-29 18:16:54 -0700343 if (!ret)
344 ret = mem_create_simple_file(mem, phys_index);
345 if (!ret)
346 ret = mem_create_simple_file(mem, state);
347 if (!ret)
348 ret = mem_create_simple_file(mem, phys_device);
349
350 return ret;
351}
352
353/*
354 * For now, we have a linear search to go find the appropriate
355 * memory_block corresponding to a particular phys_index. If
356 * this gets to be a real problem, we can always use a radix
357 * tree or something here.
358 *
359 * This could be made generic for all sysdev classes.
360 */
361static struct memory_block *find_memory_block(struct mem_section *section)
362{
363 struct kobject *kobj;
364 struct sys_device *sysdev;
365 struct memory_block *mem;
366 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
367
368 /*
369 * This only works because we know that section == sysdev->id
370 * slightly redundant with sysdev_register()
371 */
372 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
373
374 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
375 if (!kobj)
376 return NULL;
377
378 sysdev = container_of(kobj, struct sys_device, kobj);
379 mem = container_of(sysdev, struct memory_block, sysdev);
380
381 return mem;
382}
383
384int remove_memory_block(unsigned long node_id, struct mem_section *section,
385 int phys_device)
386{
387 struct memory_block *mem;
388
389 mem = find_memory_block(section);
390 mem_remove_simple_file(mem, phys_index);
391 mem_remove_simple_file(mem, state);
392 mem_remove_simple_file(mem, phys_device);
Badari Pulavarty00a41db2008-02-11 09:23:18 -0800393 unregister_memory(mem, section);
Dave Hansen3947be12005-10-29 18:16:54 -0700394
395 return 0;
396}
397
398/*
399 * need an interface for the VM to add new memory regions,
400 * but without onlining it.
401 */
402int register_new_memory(struct mem_section *section)
403{
404 return add_memory_block(0, section, MEM_OFFLINE, 0);
405}
406
407int unregister_memory_section(struct mem_section *section)
408{
Andy Whitcroft540557b2007-10-16 01:24:11 -0700409 if (!present_section(section))
Dave Hansen3947be12005-10-29 18:16:54 -0700410 return -EINVAL;
411
412 return remove_memory_block(0, section, 0);
413}
414
415/*
416 * Initialize the sysfs support for memory devices...
417 */
418int __init memory_dev_init(void)
419{
420 unsigned int i;
421 int ret;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800422 int err;
Dave Hansen3947be12005-10-29 18:16:54 -0700423
Kay Sievers312c0042005-11-16 09:00:00 +0100424 memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops;
Dave Hansen3947be12005-10-29 18:16:54 -0700425 ret = sysdev_class_register(&memory_sysdev_class);
Andrew Morton28ec24e2006-12-06 20:37:29 -0800426 if (ret)
427 goto out;
Dave Hansen3947be12005-10-29 18:16:54 -0700428
429 /*
430 * Create entries for memory sections that were found
431 * during boot and have been initialized
432 */
433 for (i = 0; i < NR_MEM_SECTIONS; i++) {
Andy Whitcroft540557b2007-10-16 01:24:11 -0700434 if (!present_section_nr(i))
Dave Hansen3947be12005-10-29 18:16:54 -0700435 continue;
Andrew Morton28ec24e2006-12-06 20:37:29 -0800436 err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
437 if (!ret)
438 ret = err;
Dave Hansen3947be12005-10-29 18:16:54 -0700439 }
440
Andrew Morton28ec24e2006-12-06 20:37:29 -0800441 err = memory_probe_init();
442 if (!ret)
443 ret = err;
444 err = block_size_init();
445 if (!ret)
446 ret = err;
447out:
448 if (ret)
Harvey Harrison2b3a3022008-03-04 16:41:05 -0800449 printk(KERN_ERR "%s() failed: %d\n", __func__, ret);
Dave Hansen3947be12005-10-29 18:16:54 -0700450 return ret;
451}