Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 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> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 16 | #include <linux/topology.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 17 | #include <linux/capability.h> |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 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> |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Shaohua Li | 9f1b16a | 2008-10-18 20:27:12 -0700 | [diff] [blame] | 24 | #include <linux/stat.h> |
| 25 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 26 | #include <asm/atomic.h> |
| 27 | #include <asm/uaccess.h> |
| 28 | |
| 29 | #define MEMORY_CLASS_NAME "memory" |
| 30 | |
| 31 | static struct sysdev_class memory_sysdev_class = { |
Kay Sievers | af5ca3f | 2007-12-20 02:09:39 +0100 | [diff] [blame] | 32 | .name = MEMORY_CLASS_NAME, |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 33 | }; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 34 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 35 | static const char *memory_uevent_name(struct kset *kset, struct kobject *kobj) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 36 | { |
| 37 | return MEMORY_CLASS_NAME; |
| 38 | } |
| 39 | |
Al Viro | 9ec0fd4e | 2007-10-14 06:53:45 +0100 | [diff] [blame] | 40 | static int memory_uevent(struct kset *kset, struct kobject *obj, struct kobj_uevent_env *env) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 41 | { |
| 42 | int retval = 0; |
| 43 | |
| 44 | return retval; |
| 45 | } |
| 46 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 47 | static struct kset_uevent_ops memory_uevent_ops = { |
| 48 | .name = memory_uevent_name, |
| 49 | .uevent = memory_uevent, |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 52 | static BLOCKING_NOTIFIER_HEAD(memory_chain); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 53 | |
Andy Whitcroft | 98a38eb | 2006-01-06 00:10:35 -0800 | [diff] [blame] | 54 | int register_memory_notifier(struct notifier_block *nb) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 55 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 56 | return blocking_notifier_chain_register(&memory_chain, nb); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 57 | } |
Hannes Hering | 3c82c30 | 2008-05-07 14:43:01 +0200 | [diff] [blame] | 58 | EXPORT_SYMBOL(register_memory_notifier); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 59 | |
Andy Whitcroft | 98a38eb | 2006-01-06 00:10:35 -0800 | [diff] [blame] | 60 | void unregister_memory_notifier(struct notifier_block *nb) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 61 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 62 | blocking_notifier_chain_unregister(&memory_chain, nb); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 63 | } |
Hannes Hering | 3c82c30 | 2008-05-07 14:43:01 +0200 | [diff] [blame] | 64 | EXPORT_SYMBOL(unregister_memory_notifier); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * register_memory - Setup a sysfs device for a memory block |
| 68 | */ |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 69 | static |
| 70 | int register_memory(struct memory_block *memory, struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 71 | { |
| 72 | int error; |
| 73 | |
| 74 | memory->sysdev.cls = &memory_sysdev_class; |
| 75 | memory->sysdev.id = __section_nr(section); |
| 76 | |
| 77 | error = sysdev_register(&memory->sysdev); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 78 | return error; |
| 79 | } |
| 80 | |
| 81 | static void |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 82 | unregister_memory(struct memory_block *memory, struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 83 | { |
| 84 | BUG_ON(memory->sysdev.cls != &memory_sysdev_class); |
| 85 | BUG_ON(memory->sysdev.id != __section_nr(section)); |
| 86 | |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 87 | /* drop the ref. we got in remove_memory_block() */ |
| 88 | kobject_put(&memory->sysdev.kobj); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 89 | sysdev_unregister(&memory->sysdev); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | /* |
| 93 | * use this as the physical section index that this memsection |
| 94 | * uses. |
| 95 | */ |
| 96 | |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 97 | static ssize_t show_mem_phys_index(struct sys_device *dev, |
| 98 | struct sysdev_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 99 | { |
| 100 | struct memory_block *mem = |
| 101 | container_of(dev, struct memory_block, sysdev); |
| 102 | return sprintf(buf, "%08lx\n", mem->phys_index); |
| 103 | } |
| 104 | |
| 105 | /* |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 106 | * Show whether the section of memory is likely to be hot-removable |
| 107 | */ |
Stephen Rothwell | 1f07be1 | 2008-07-28 11:05:04 +1000 | [diff] [blame] | 108 | static ssize_t show_mem_removable(struct sys_device *dev, |
| 109 | struct sysdev_attribute *attr, char *buf) |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 110 | { |
| 111 | unsigned long start_pfn; |
| 112 | int ret; |
| 113 | struct memory_block *mem = |
| 114 | container_of(dev, struct memory_block, sysdev); |
| 115 | |
| 116 | start_pfn = section_nr_to_pfn(mem->phys_index); |
| 117 | ret = is_mem_section_removable(start_pfn, PAGES_PER_SECTION); |
| 118 | return sprintf(buf, "%d\n", ret); |
| 119 | } |
| 120 | |
| 121 | /* |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 122 | * online, offline, going offline, etc. |
| 123 | */ |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 124 | static ssize_t show_mem_state(struct sys_device *dev, |
| 125 | struct sysdev_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 126 | { |
| 127 | struct memory_block *mem = |
| 128 | container_of(dev, struct memory_block, sysdev); |
| 129 | ssize_t len = 0; |
| 130 | |
| 131 | /* |
| 132 | * We can probably put these states in a nice little array |
| 133 | * so that they're not open-coded |
| 134 | */ |
| 135 | switch (mem->state) { |
| 136 | case MEM_ONLINE: |
| 137 | len = sprintf(buf, "online\n"); |
| 138 | break; |
| 139 | case MEM_OFFLINE: |
| 140 | len = sprintf(buf, "offline\n"); |
| 141 | break; |
| 142 | case MEM_GOING_OFFLINE: |
| 143 | len = sprintf(buf, "going-offline\n"); |
| 144 | break; |
| 145 | default: |
| 146 | len = sprintf(buf, "ERROR-UNKNOWN-%ld\n", |
| 147 | mem->state); |
| 148 | WARN_ON(1); |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | return len; |
| 153 | } |
| 154 | |
Yasunori Goto | 7b78d33 | 2007-10-21 16:41:36 -0700 | [diff] [blame] | 155 | int memory_notify(unsigned long val, void *v) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 156 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 157 | return blocking_notifier_call_chain(&memory_chain, val, v); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* |
| 161 | * MEMORY_HOTPLUG depends on SPARSEMEM in mm/Kconfig, so it is |
| 162 | * OK to have direct references to sparsemem variables in here. |
| 163 | */ |
| 164 | static int |
| 165 | memory_block_action(struct memory_block *mem, unsigned long action) |
| 166 | { |
| 167 | int i; |
| 168 | unsigned long psection; |
| 169 | unsigned long start_pfn, start_paddr; |
| 170 | struct page *first_page; |
| 171 | int ret; |
| 172 | int old_state = mem->state; |
| 173 | |
| 174 | psection = mem->phys_index; |
| 175 | first_page = pfn_to_page(psection << PFN_SECTION_SHIFT); |
| 176 | |
| 177 | /* |
| 178 | * The probe routines leave the pages reserved, just |
| 179 | * as the bootmem code does. Make sure they're still |
| 180 | * that way. |
| 181 | */ |
| 182 | if (action == MEM_ONLINE) { |
| 183 | for (i = 0; i < PAGES_PER_SECTION; i++) { |
| 184 | if (PageReserved(first_page+i)) |
| 185 | continue; |
| 186 | |
| 187 | printk(KERN_WARNING "section number %ld page number %d " |
| 188 | "not reserved, was it already online? \n", |
| 189 | psection, i); |
| 190 | return -EBUSY; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | switch (action) { |
| 195 | case MEM_ONLINE: |
| 196 | start_pfn = page_to_pfn(first_page); |
| 197 | ret = online_pages(start_pfn, PAGES_PER_SECTION); |
| 198 | break; |
| 199 | case MEM_OFFLINE: |
| 200 | mem->state = MEM_GOING_OFFLINE; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 201 | start_paddr = page_to_pfn(first_page) << PAGE_SHIFT; |
| 202 | ret = remove_memory(start_paddr, |
| 203 | PAGES_PER_SECTION << PAGE_SHIFT); |
| 204 | if (ret) { |
| 205 | mem->state = old_state; |
| 206 | break; |
| 207 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 208 | break; |
| 209 | default: |
Arjan van de Ven | f810a5c | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 210 | WARN(1, KERN_WARNING "%s(%p, %ld) unknown action: %ld\n", |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 211 | __func__, mem, action, action); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 212 | ret = -EINVAL; |
| 213 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 214 | |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static int memory_block_change_state(struct memory_block *mem, |
| 219 | unsigned long to_state, unsigned long from_state_req) |
| 220 | { |
| 221 | int ret = 0; |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 222 | mutex_lock(&mem->state_mutex); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 223 | |
| 224 | if (mem->state != from_state_req) { |
| 225 | ret = -EINVAL; |
| 226 | goto out; |
| 227 | } |
| 228 | |
| 229 | ret = memory_block_action(mem, to_state); |
| 230 | if (!ret) |
| 231 | mem->state = to_state; |
| 232 | |
| 233 | out: |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 234 | mutex_unlock(&mem->state_mutex); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 235 | return ret; |
| 236 | } |
| 237 | |
| 238 | static ssize_t |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 239 | store_mem_state(struct sys_device *dev, |
| 240 | struct sysdev_attribute *attr, const char *buf, size_t count) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 241 | { |
| 242 | struct memory_block *mem; |
| 243 | unsigned int phys_section_nr; |
| 244 | int ret = -EINVAL; |
| 245 | |
| 246 | mem = container_of(dev, struct memory_block, sysdev); |
| 247 | phys_section_nr = mem->phys_index; |
| 248 | |
Andy Whitcroft | 540557b | 2007-10-16 01:24:11 -0700 | [diff] [blame] | 249 | if (!present_section_nr(phys_section_nr)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 250 | goto out; |
| 251 | |
| 252 | if (!strncmp(buf, "online", min((int)count, 6))) |
| 253 | ret = memory_block_change_state(mem, MEM_ONLINE, MEM_OFFLINE); |
| 254 | else if(!strncmp(buf, "offline", min((int)count, 7))) |
| 255 | ret = memory_block_change_state(mem, MEM_OFFLINE, MEM_ONLINE); |
| 256 | out: |
| 257 | if (ret) |
| 258 | return ret; |
| 259 | return count; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * phys_device is a bad name for this. What I really want |
| 264 | * is a way to differentiate between memory ranges that |
| 265 | * are part of physical devices that constitute |
| 266 | * a complete removable unit or fru. |
| 267 | * i.e. do these ranges belong to the same physical device, |
| 268 | * s.t. if I offline all of these sections I can then |
| 269 | * remove the physical device? |
| 270 | */ |
Andi Kleen | 4a0b2b4 | 2008-07-01 18:48:41 +0200 | [diff] [blame] | 271 | static ssize_t show_phys_device(struct sys_device *dev, |
| 272 | struct sysdev_attribute *attr, char *buf) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 273 | { |
| 274 | struct memory_block *mem = |
| 275 | container_of(dev, struct memory_block, sysdev); |
| 276 | return sprintf(buf, "%d\n", mem->phys_device); |
| 277 | } |
| 278 | |
| 279 | static SYSDEV_ATTR(phys_index, 0444, show_mem_phys_index, NULL); |
| 280 | static SYSDEV_ATTR(state, 0644, show_mem_state, store_mem_state); |
| 281 | static SYSDEV_ATTR(phys_device, 0444, show_phys_device, NULL); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 282 | static SYSDEV_ATTR(removable, 0444, show_mem_removable, NULL); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 283 | |
| 284 | #define mem_create_simple_file(mem, attr_name) \ |
| 285 | sysdev_create_file(&mem->sysdev, &attr_##attr_name) |
| 286 | #define mem_remove_simple_file(mem, attr_name) \ |
| 287 | sysdev_remove_file(&mem->sysdev, &attr_##attr_name) |
| 288 | |
| 289 | /* |
| 290 | * Block size attribute stuff |
| 291 | */ |
| 292 | static ssize_t |
| 293 | print_block_size(struct class *class, char *buf) |
| 294 | { |
| 295 | return sprintf(buf, "%lx\n", (unsigned long)PAGES_PER_SECTION * PAGE_SIZE); |
| 296 | } |
| 297 | |
| 298 | static CLASS_ATTR(block_size_bytes, 0444, print_block_size, NULL); |
| 299 | |
| 300 | static int block_size_init(void) |
| 301 | { |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 302 | return sysfs_create_file(&memory_sysdev_class.kset.kobj, |
| 303 | &class_attr_block_size_bytes.attr); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Some architectures will have custom drivers to do this, and |
| 308 | * will not need to do it from userspace. The fake hot-add code |
| 309 | * as well as ppc64 will do all of their discovery in userspace |
| 310 | * and will require this interface. |
| 311 | */ |
| 312 | #ifdef CONFIG_ARCH_MEMORY_PROBE |
| 313 | static ssize_t |
Al Viro | be7ee9b | 2006-02-01 06:06:16 -0500 | [diff] [blame] | 314 | memory_probe_store(struct class *class, const char *buf, size_t count) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 315 | { |
| 316 | u64 phys_addr; |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 317 | int nid; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 318 | int ret; |
| 319 | |
| 320 | phys_addr = simple_strtoull(buf, NULL, 0); |
| 321 | |
Yasunori Goto | bc02af9 | 2006-06-27 02:53:30 -0700 | [diff] [blame] | 322 | nid = memory_add_physaddr_to_nid(phys_addr); |
| 323 | ret = add_memory(nid, phys_addr, PAGES_PER_SECTION << PAGE_SHIFT); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 324 | |
| 325 | if (ret) |
| 326 | count = ret; |
| 327 | |
| 328 | return count; |
| 329 | } |
Shaohua Li | 9f1b16a | 2008-10-18 20:27:12 -0700 | [diff] [blame] | 330 | static CLASS_ATTR(probe, S_IWUSR, NULL, memory_probe_store); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 331 | |
| 332 | static int memory_probe_init(void) |
| 333 | { |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 334 | return sysfs_create_file(&memory_sysdev_class.kset.kobj, |
| 335 | &class_attr_probe.attr); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 336 | } |
| 337 | #else |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 338 | static inline int memory_probe_init(void) |
| 339 | { |
| 340 | return 0; |
| 341 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 342 | #endif |
| 343 | |
| 344 | /* |
| 345 | * Note that phys_device is optional. It is here to allow for |
| 346 | * differentiation between which *physical* devices each |
| 347 | * section belongs to... |
| 348 | */ |
| 349 | |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 350 | static int add_memory_block(int nid, struct mem_section *section, |
| 351 | unsigned long state, int phys_device, |
| 352 | enum mem_add_context context) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 353 | { |
Dave Hansen | 0b0acbe | 2005-10-29 18:16:55 -0700 | [diff] [blame] | 354 | struct memory_block *mem = kzalloc(sizeof(*mem), GFP_KERNEL); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 355 | int ret = 0; |
| 356 | |
| 357 | if (!mem) |
| 358 | return -ENOMEM; |
| 359 | |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 360 | mem->phys_index = __section_nr(section); |
| 361 | mem->state = state; |
Daniel Walker | da19cbc | 2008-02-04 23:35:47 -0800 | [diff] [blame] | 362 | mutex_init(&mem->state_mutex); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 363 | mem->phys_device = phys_device; |
| 364 | |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 365 | ret = register_memory(mem, section); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 366 | if (!ret) |
| 367 | ret = mem_create_simple_file(mem, phys_index); |
| 368 | if (!ret) |
| 369 | ret = mem_create_simple_file(mem, state); |
| 370 | if (!ret) |
| 371 | ret = mem_create_simple_file(mem, phys_device); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 372 | if (!ret) |
| 373 | ret = mem_create_simple_file(mem, removable); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 374 | if (!ret) { |
| 375 | if (context == HOTPLUG) |
| 376 | ret = register_mem_sect_under_node(mem, nid); |
| 377 | } |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 378 | |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * For now, we have a linear search to go find the appropriate |
| 384 | * memory_block corresponding to a particular phys_index. If |
| 385 | * this gets to be a real problem, we can always use a radix |
| 386 | * tree or something here. |
| 387 | * |
| 388 | * This could be made generic for all sysdev classes. |
| 389 | */ |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 390 | struct memory_block *find_memory_block(struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 391 | { |
| 392 | struct kobject *kobj; |
| 393 | struct sys_device *sysdev; |
| 394 | struct memory_block *mem; |
| 395 | char name[sizeof(MEMORY_CLASS_NAME) + 9 + 1]; |
| 396 | |
| 397 | /* |
| 398 | * This only works because we know that section == sysdev->id |
| 399 | * slightly redundant with sysdev_register() |
| 400 | */ |
| 401 | sprintf(&name[0], "%s%d", MEMORY_CLASS_NAME, __section_nr(section)); |
| 402 | |
| 403 | kobj = kset_find_obj(&memory_sysdev_class.kset, name); |
| 404 | if (!kobj) |
| 405 | return NULL; |
| 406 | |
| 407 | sysdev = container_of(kobj, struct sys_device, kobj); |
| 408 | mem = container_of(sysdev, struct memory_block, sysdev); |
| 409 | |
| 410 | return mem; |
| 411 | } |
| 412 | |
| 413 | int remove_memory_block(unsigned long node_id, struct mem_section *section, |
| 414 | int phys_device) |
| 415 | { |
| 416 | struct memory_block *mem; |
| 417 | |
| 418 | mem = find_memory_block(section); |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 419 | unregister_mem_sect_under_nodes(mem); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 420 | mem_remove_simple_file(mem, phys_index); |
| 421 | mem_remove_simple_file(mem, state); |
| 422 | mem_remove_simple_file(mem, phys_device); |
Badari Pulavarty | 5c755e9 | 2008-07-23 21:28:19 -0700 | [diff] [blame] | 423 | mem_remove_simple_file(mem, removable); |
Badari Pulavarty | 00a41db | 2008-02-11 09:23:18 -0800 | [diff] [blame] | 424 | unregister_memory(mem, section); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * need an interface for the VM to add new memory regions, |
| 431 | * but without onlining it. |
| 432 | */ |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 433 | int register_new_memory(int nid, struct mem_section *section) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 434 | { |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 435 | return add_memory_block(nid, section, MEM_OFFLINE, 0, HOTPLUG); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | int unregister_memory_section(struct mem_section *section) |
| 439 | { |
Andy Whitcroft | 540557b | 2007-10-16 01:24:11 -0700 | [diff] [blame] | 440 | if (!present_section(section)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 441 | return -EINVAL; |
| 442 | |
| 443 | return remove_memory_block(0, section, 0); |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Initialize the sysfs support for memory devices... |
| 448 | */ |
| 449 | int __init memory_dev_init(void) |
| 450 | { |
| 451 | unsigned int i; |
| 452 | int ret; |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 453 | int err; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 454 | |
Kay Sievers | 312c004 | 2005-11-16 09:00:00 +0100 | [diff] [blame] | 455 | memory_sysdev_class.kset.uevent_ops = &memory_uevent_ops; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 456 | ret = sysdev_class_register(&memory_sysdev_class); |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 457 | if (ret) |
| 458 | goto out; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 459 | |
| 460 | /* |
| 461 | * Create entries for memory sections that were found |
| 462 | * during boot and have been initialized |
| 463 | */ |
| 464 | for (i = 0; i < NR_MEM_SECTIONS; i++) { |
Andy Whitcroft | 540557b | 2007-10-16 01:24:11 -0700 | [diff] [blame] | 465 | if (!present_section_nr(i)) |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 466 | continue; |
Gary Hade | c04fc58 | 2009-01-06 14:39:14 -0800 | [diff] [blame] | 467 | err = add_memory_block(0, __nr_to_section(i), MEM_ONLINE, |
| 468 | 0, BOOT); |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 469 | if (!ret) |
| 470 | ret = err; |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Andrew Morton | 28ec24e | 2006-12-06 20:37:29 -0800 | [diff] [blame] | 473 | err = memory_probe_init(); |
| 474 | if (!ret) |
| 475 | ret = err; |
| 476 | err = block_size_init(); |
| 477 | if (!ret) |
| 478 | ret = err; |
| 479 | out: |
| 480 | if (ret) |
Harvey Harrison | 2b3a302 | 2008-03-04 16:41:05 -0800 | [diff] [blame] | 481 | printk(KERN_ERR "%s() failed: %d\n", __func__, ret); |
Dave Hansen | 3947be1 | 2005-10-29 18:16:54 -0700 | [diff] [blame] | 482 | return ret; |
| 483 | } |