[PATCH] USB Storage: add unusual_devs entry for NIKON Coolpix 2000
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / base / memory.c
1 /*
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>
16 #include <linux/sched.h> /* capable() */
17 #include <linux/topology.h>
18 #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>
23 #include <asm/atomic.h>
24 #include <asm/uaccess.h>
25
26 #define MEMORY_CLASS_NAME "memory"
27
28 static struct sysdev_class memory_sysdev_class = {
29 set_kset_name(MEMORY_CLASS_NAME),
30 };
31
32 static char *memory_hotplug_name(struct kset *kset, struct kobject *kobj)
33 {
34 return MEMORY_CLASS_NAME;
35 }
36
37 static int memory_hotplug(struct kset *kset, struct kobject *kobj, char **envp,
38 int num_envp, char *buffer, int buffer_size)
39 {
40 int retval = 0;
41
42 return retval;
43 }
44
45 static struct kset_hotplug_ops memory_hotplug_ops = {
46 .name = memory_hotplug_name,
47 .hotplug = memory_hotplug,
48 };
49
50 static struct notifier_block *memory_chain;
51
52 static int register_memory_notifier(struct notifier_block *nb)
53 {
54 return notifier_chain_register(&memory_chain, nb);
55 }
56
57 static void unregister_memory_notifier(struct notifier_block *nb)
58 {
59 notifier_chain_unregister(&memory_chain, nb);
60 }
61
62 /*
63 * register_memory - Setup a sysfs device for a memory block
64 */
65 static int
66 register_memory(struct memory_block *memory, struct mem_section *section,
67 struct node *root)
68 {
69 int error;
70
71 memory->sysdev.cls = &memory_sysdev_class;
72 memory->sysdev.id = __section_nr(section);
73
74 error = sysdev_register(&memory->sysdev);
75
76 if (root && !error)
77 error = sysfs_create_link(&root->sysdev.kobj,
78 &memory->sysdev.kobj,
79 kobject_name(&memory->sysdev.kobj));
80
81 return error;
82 }
83
84 static void
85 unregister_memory(struct memory_block *memory, struct mem_section *section,
86 struct node *root)
87 {
88 BUG_ON(memory->sysdev.cls != &memory_sysdev_class);
89 BUG_ON(memory->sysdev.id != __section_nr(section));
90
91 sysdev_unregister(&memory->sysdev);
92 if (root)
93 sysfs_remove_link(&root->sysdev.kobj,
94 kobject_name(&memory->sysdev.kobj));
95 }
96
97 /*
98 * use this as the physical section index that this memsection
99 * uses.
100 */
101
102 static ssize_t show_mem_phys_index(struct sys_device *dev, char *buf)
103 {
104 struct memory_block *mem =
105 container_of(dev, struct memory_block, sysdev);
106 return sprintf(buf, "%08lx\n", mem->phys_index);
107 }
108
109 /*
110 * online, offline, going offline, etc.
111 */
112 static ssize_t show_mem_state(struct sys_device *dev, char *buf)
113 {
114 struct memory_block *mem =
115 container_of(dev, struct memory_block, sysdev);
116 ssize_t len = 0;
117
118 /*
119 * We can probably put these states in a nice little array
120 * so that they're not open-coded
121 */
122 switch (mem->state) {
123 case MEM_ONLINE:
124 len = sprintf(buf, "online\n");
125 break;
126 case MEM_OFFLINE:
127 len = sprintf(buf, "offline\n");
128 break;
129 case MEM_GOING_OFFLINE:
130 len = sprintf(buf, "going-offline\n");
131 break;
132 default:
133 len = sprintf(buf, "ERROR-UNKNOWN-%ld\n",
134 mem->state);
135 WARN_ON(1);
136 break;
137 }
138
139 return len;
140 }
141
142 static inline int memory_notify(unsigned long val, void *v)
143 {
144 return notifier_call_chain(&memory_chain, val, v);
145 }
146
147 /*
148 * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is
149 * OK to have direct references to sparsemem variables in here.
150 */
151 static int
152 memory_block_action(struct memory_block *mem, unsigned long action)
153 {
154 int i;
155 unsigned long psection;
156 unsigned long start_pfn, start_paddr;
157 struct page *first_page;
158 int ret;
159 int old_state = mem->state;
160
161 psection = mem->phys_index;
162 first_page = pfn_to_page(psection << PFN_SECTION_SHIFT);
163
164 /*
165 * The probe routines leave the pages reserved, just
166 * as the bootmem code does. Make sure they're still
167 * that way.
168 */
169 if (action == MEM_ONLINE) {
170 for (i = 0; i < PAGES_PER_SECTION; i++) {
171 if (PageReserved(first_page+i))
172 continue;
173
174 printk(KERN_WARNING "section number %ld page number %d "
175 "not reserved, was it already online? \n",
176 psection, i);
177 return -EBUSY;
178 }
179 }
180
181 switch (action) {
182 case MEM_ONLINE:
183 start_pfn = page_to_pfn(first_page);
184 ret = online_pages(start_pfn, PAGES_PER_SECTION);
185 break;
186 case MEM_OFFLINE:
187 mem->state = MEM_GOING_OFFLINE;
188 memory_notify(MEM_GOING_OFFLINE, NULL);
189 start_paddr = page_to_pfn(first_page) << PAGE_SHIFT;
190 ret = remove_memory(start_paddr,
191 PAGES_PER_SECTION << PAGE_SHIFT);
192 if (ret) {
193 mem->state = old_state;
194 break;
195 }
196 memory_notify(MEM_MAPPING_INVALID, NULL);
197 break;
198 default:
199 printk(KERN_WARNING "%s(%p, %ld) unknown action: %ld\n",
200 __FUNCTION__, mem, action, action);
201 WARN_ON(1);
202 ret = -EINVAL;
203 }
204 /*
205 * For now, only notify on successful memory operations
206 */
207 if (!ret)
208 memory_notify(action, NULL);
209
210 return ret;
211 }
212
213 static int memory_block_change_state(struct memory_block *mem,
214 unsigned long to_state, unsigned long from_state_req)
215 {
216 int ret = 0;
217 down(&mem->state_sem);
218
219 if (mem->state != from_state_req) {
220 ret = -EINVAL;
221 goto out;
222 }
223
224 ret = memory_block_action(mem, to_state);
225 if (!ret)
226 mem->state = to_state;
227
228 out:
229 up(&mem->state_sem);
230 return ret;
231 }
232
233 static ssize_t
234 store_mem_state(struct sys_device *dev, const char *buf, size_t count)
235 {
236 struct memory_block *mem;
237 unsigned int phys_section_nr;
238 int ret = -EINVAL;
239
240 mem = container_of(dev, struct memory_block, sysdev);
241 phys_section_nr = mem->phys_index;
242
243 if (!valid_section_nr(phys_section_nr))
244 goto out;
245
246 if (!strncmp(buf, "online", min((int)count, 6)))
247 ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE);
248 else if(!strncmp(buf, "offline", min((int)count, 7)))
249 ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE);
250 out:
251 if (ret)
252 return ret;
253 return count;
254 }
255
256 /*
257 * phys_device is a bad name for this. What I really want
258 * is a way to differentiate between memory ranges that
259 * are part of physical devices that constitute
260 * a complete removable unit or fru.
261 * i.e. do these ranges belong to the same physical device,
262 * s.t. if I offline all of these sections I can then
263 * remove the physical device?
264 */
265 static ssize_t show_phys_device(struct sys_device *dev, char *buf)
266 {
267 struct memory_block *mem =
268 container_of(dev, struct memory_block, sysdev);
269 return sprintf(buf, "%d\n", mem->phys_device);
270 }
271
272 static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL);
273 static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state);
274 static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL);
275
276 #define mem_create_simple_file(mem, attr_name) \
277 sysdev_create_file(&mem->sysdev, &attr_##attr_name)
278 #define mem_remove_simple_file(mem, attr_name) \
279 sysdev_remove_file(&mem->sysdev, &attr_##attr_name)
280
281 /*
282 * Block size attribute stuff
283 */
284 static ssize_t
285 print_block_size(struct class *class, char *buf)
286 {
287 return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE);
288 }
289
290 static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL);
291
292 static int block_size_init(void)
293 {
294 sysfs_create_file(&memory_sysdev_class.kset.kobj,
295 &class_attr_block_size_bytes.attr);
296 return 0;
297 }
298
299 /*
300 * Some architectures will have custom drivers to do this, and
301 * will not need to do it from userspace. The fake hot-add code
302 * as well as ppc64 will do all of their discovery in userspace
303 * and will require this interface.
304 */
305 #ifdef CONFIG_ARCH_MEMORY_PROBE
306 static ssize_t
307 memory_probe_store(struct class *class, const char __user *buf, size_t count)
308 {
309 u64 phys_addr;
310 int ret;
311
312 phys_addr = simple_strtoull(buf, NULL, 0);
313
314 ret = add_memory(phys_addr, PAGES_PER_SECTION << PAGE_SHIFT);
315
316 if (ret)
317 count = ret;
318
319 return count;
320 }
321 static CLASS_ATTR(probe, 0700, NULL, memory_probe_store);
322
323 static int memory_probe_init(void)
324 {
325 sysfs_create_file(&memory_sysdev_class.kset.kobj,
326 &class_attr_probe.attr);
327 return 0;
328 }
329 #else
330 #define memory_probe_init(...) do {} while (0)
331 #endif
332
333 /*
334 * Note that phys_device is optional. It is here to allow for
335 * differentiation between which *physical* devices each
336 * section belongs to...
337 */
338
339 static int add_memory_block(unsigned long node_id, struct mem_section *section,
340 unsigned long state, int phys_device)
341 {
342 struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL);
343 int ret = 0;
344
345 if (!mem)
346 return -ENOMEM;
347
348 mem->phys_index = __section_nr(section);
349 mem->state = state;
350 init_MUTEX(&mem->state_sem);
351 mem->phys_device = phys_device;
352
353 ret = register_memory(mem, section, NULL);
354 if (!ret)
355 ret = mem_create_simple_file(mem, phys_index);
356 if (!ret)
357 ret = mem_create_simple_file(mem, state);
358 if (!ret)
359 ret = mem_create_simple_file(mem, phys_device);
360
361 return ret;
362 }
363
364 /*
365 * For now, we have a linear search to go find the appropriate
366 * memory_block corresponding to a particular phys_index. If
367 * this gets to be a real problem, we can always use a radix
368 * tree or something here.
369 *
370 * This could be made generic for all sysdev classes.
371 */
372 static struct memory_block *find_memory_block(struct mem_section *section)
373 {
374 struct kobject *kobj;
375 struct sys_device *sysdev;
376 struct memory_block *mem;
377 char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1];
378
379 /*
380 * This only works because we know that section == sysdev->id
381 * slightly redundant with sysdev_register()
382 */
383 sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section));
384
385 kobj = kset_find_obj(&memory_sysdev_class.kset, name);
386 if (!kobj)
387 return NULL;
388
389 sysdev = container_of(kobj, struct sys_device, kobj);
390 mem = container_of(sysdev, struct memory_block, sysdev);
391
392 return mem;
393 }
394
395 int remove_memory_block(unsigned long node_id, struct mem_section *section,
396 int phys_device)
397 {
398 struct memory_block *mem;
399
400 mem = find_memory_block(section);
401 mem_remove_simple_file(mem, phys_index);
402 mem_remove_simple_file(mem, state);
403 mem_remove_simple_file(mem, phys_device);
404 unregister_memory(mem, section, NULL);
405
406 return 0;
407 }
408
409 /*
410 * need an interface for the VM to add new memory regions,
411 * but without onlining it.
412 */
413 int register_new_memory(struct mem_section *section)
414 {
415 return add_memory_block(0, section, MEM_OFFLINE, 0);
416 }
417
418 int unregister_memory_section(struct mem_section *section)
419 {
420 if (!valid_section(section))
421 return -EINVAL;
422
423 return remove_memory_block(0, section, 0);
424 }
425
426 /*
427 * Initialize the sysfs support for memory devices...
428 */
429 int __init memory_dev_init(void)
430 {
431 unsigned int i;
432 int ret;
433
434 memory_sysdev_class.kset.hotplug_ops = &memory_hotplug_ops;
435 ret = sysdev_class_register(&memory_sysdev_class);
436
437 /*
438 * Create entries for memory sections that were found
439 * during boot and have been initialized
440 */
441 for (i = 0; i < NR_MEM_SECTIONS; i++) {
442 if (!valid_section_nr(i))
443 continue;
444 add_memory_block(0, __nr_to_section(i), MEM_ONLINE, 0);
445 }
446
447 memory_probe_init();
448 block_size_init();
449
450 return ret;
451 }