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