Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * pseries Memory Hotplug infrastructure. |
| 3 | * |
| 4 | * Copyright (C) 2008 Badari Pulavarty, IBM Corporation |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/of.h> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 13 | #include <linux/of_address.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 14 | #include <linux/memblock.h> |
Benjamin Herrenschmidt | b4a26be | 2010-04-06 15:03:40 +0000 | [diff] [blame] | 15 | #include <linux/vmalloc.h> |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 16 | #include <linux/memory.h> |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 17 | #include <linux/memory_hotplug.h> |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 18 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 19 | #include <asm/firmware.h> |
| 20 | #include <asm/machdep.h> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 21 | #include <asm/prom.h> |
Michael Neuling | 0b2f828 | 2009-02-08 14:49:39 +0000 | [diff] [blame] | 22 | #include <asm/sparsemem.h> |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 23 | |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 24 | static unsigned long get_memblock_size(void) |
| 25 | { |
| 26 | struct device_node *np; |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 27 | unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE; |
| 28 | struct resource r; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 29 | |
| 30 | np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 31 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 32 | const __be64 *size; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 33 | |
| 34 | size = of_get_property(np, "ibm,lmb-size", NULL); |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 35 | if (size) |
| 36 | memblock_size = be64_to_cpup(size); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 37 | of_node_put(np); |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 38 | } else if (machine_is(pseries)) { |
| 39 | /* This fallback really only applies to pseries */ |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 40 | unsigned int memzero_size = 0; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 41 | |
| 42 | np = of_find_node_by_path("/memory@0"); |
| 43 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 44 | if (!of_address_to_resource(np, 0, &r)) |
| 45 | memzero_size = resource_size(&r); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 46 | of_node_put(np); |
| 47 | } |
| 48 | |
| 49 | if (memzero_size) { |
| 50 | /* We now know the size of memory@0, use this to find |
| 51 | * the first memoryblock and get its size. |
| 52 | */ |
| 53 | char buf[64]; |
| 54 | |
| 55 | sprintf(buf, "/memory@%x", memzero_size); |
| 56 | np = of_find_node_by_path(buf); |
| 57 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 58 | if (!of_address_to_resource(np, 0, &r)) |
| 59 | memblock_size = resource_size(&r); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 60 | of_node_put(np); |
| 61 | } |
| 62 | } |
| 63 | } |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 64 | return memblock_size; |
| 65 | } |
| 66 | |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 67 | /* WARNING: This is going to override the generic definition whenever |
| 68 | * pseries is built-in regardless of what platform is active at boot |
| 69 | * time. This is fine for now as this is the only "option" and it |
| 70 | * should work everywhere. If not, we'll have to turn this into a |
| 71 | * ppc_md. callback |
| 72 | */ |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 73 | unsigned long memory_block_size_bytes(void) |
| 74 | { |
| 75 | return get_memblock_size(); |
| 76 | } |
| 77 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 78 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 79 | static int pseries_remove_memory(u64 start, u64 size) |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 80 | { |
Yasuaki Ishimatsu | 1633dbb | 2012-10-10 15:53:53 -0700 | [diff] [blame] | 81 | int ret; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 82 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 83 | /* Remove htab bolted mappings for this section of memory */ |
| 84 | start = (unsigned long)__va(start); |
| 85 | ret = remove_section_mapping(start, start + size); |
Benjamin Herrenschmidt | b4a26be | 2010-04-06 15:03:40 +0000 | [diff] [blame] | 86 | |
| 87 | /* Ensure all vmalloc mappings are flushed in case they also |
| 88 | * hit that section of memory |
| 89 | */ |
| 90 | vm_unmap_aliases(); |
| 91 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 92 | return ret; |
| 93 | } |
| 94 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 95 | static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size) |
| 96 | { |
| 97 | unsigned long block_sz, start_pfn; |
| 98 | int sections_per_block; |
| 99 | int i, nid; |
| 100 | |
| 101 | start_pfn = base >> PAGE_SHIFT; |
| 102 | |
| 103 | if (!pfn_valid(start_pfn)) { |
| 104 | memblock_remove(base, memblock_size); |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | block_sz = memory_block_size_bytes(); |
| 109 | sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; |
| 110 | nid = memory_add_physaddr_to_nid(base); |
| 111 | |
| 112 | for (i = 0; i < sections_per_block; i++) { |
| 113 | remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE); |
| 114 | base += MIN_MEMORY_BLOCK_SIZE; |
| 115 | } |
| 116 | |
| 117 | /* Update memory regions for memory remove */ |
| 118 | memblock_remove(base, memblock_size); |
| 119 | return 0; |
| 120 | } |
| 121 | |
| 122 | static int pseries_remove_mem_node(struct device_node *np) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 123 | { |
| 124 | const char *type; |
| 125 | const unsigned int *regs; |
| 126 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 127 | unsigned int lmb_size; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 128 | int ret = -EINVAL; |
| 129 | |
| 130 | /* |
| 131 | * Check to see if we are actually removing memory |
| 132 | */ |
| 133 | type = of_get_property(np, "device_type", NULL); |
| 134 | if (type == NULL || strcmp(type, "memory") != 0) |
| 135 | return 0; |
| 136 | |
| 137 | /* |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 138 | * Find the bae address and size of the memblock |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 139 | */ |
| 140 | regs = of_get_property(np, "reg", NULL); |
| 141 | if (!regs) |
| 142 | return ret; |
| 143 | |
| 144 | base = *(unsigned long *)regs; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 145 | lmb_size = regs[3]; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 146 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 147 | pseries_remove_memblock(base, lmb_size); |
| 148 | return 0; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 149 | } |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 150 | #else |
| 151 | static inline int pseries_remove_memblock(unsigned long base, |
| 152 | unsigned int memblock_size) |
| 153 | { |
| 154 | return -EOPNOTSUPP; |
| 155 | } |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 156 | static inline int pseries_remove_mem_node(struct device_node *np) |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 157 | { |
| 158 | return -EOPNOTSUPP; |
| 159 | } |
| 160 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 161 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 162 | static int pseries_add_mem_node(struct device_node *np) |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 163 | { |
| 164 | const char *type; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 165 | const unsigned int *regs; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 166 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 167 | unsigned int lmb_size; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 168 | int ret = -EINVAL; |
| 169 | |
| 170 | /* |
| 171 | * Check to see if we are actually adding memory |
| 172 | */ |
| 173 | type = of_get_property(np, "device_type", NULL); |
| 174 | if (type == NULL || strcmp(type, "memory") != 0) |
| 175 | return 0; |
| 176 | |
| 177 | /* |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 178 | * Find the base and size of the memblock |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 179 | */ |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 180 | regs = of_get_property(np, "reg", NULL); |
| 181 | if (!regs) |
| 182 | return ret; |
| 183 | |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 184 | base = *(unsigned long *)regs; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 185 | lmb_size = regs[3]; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * Update memory region to represent the memory add |
| 189 | */ |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 190 | ret = memblock_add(base, lmb_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 191 | return (ret < 0) ? -EINVAL : 0; |
| 192 | } |
| 193 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 194 | static int pseries_update_drconf_memory(struct of_prop_reconfig *pr) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 195 | { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 196 | struct of_drconf_cell *new_drmem, *old_drmem; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 197 | unsigned long memblock_size; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 198 | u32 entries; |
| 199 | u32 *p; |
| 200 | int i, rc = -EINVAL; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 201 | |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 202 | memblock_size = get_memblock_size(); |
| 203 | if (!memblock_size) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 204 | return -EINVAL; |
| 205 | |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 206 | p = (u32 *)of_get_property(pr->dn, "ibm,dynamic-memory", NULL); |
| 207 | if (!p) |
| 208 | return -EINVAL; |
| 209 | |
| 210 | /* The first int of the property is the number of lmb's described |
| 211 | * by the property. This is followed by an array of of_drconf_cell |
| 212 | * entries. Get the niumber of entries and skip to the array of |
| 213 | * of_drconf_cell's. |
| 214 | */ |
| 215 | entries = *p++; |
| 216 | old_drmem = (struct of_drconf_cell *)p; |
| 217 | |
| 218 | p = (u32 *)pr->prop->value; |
| 219 | p++; |
| 220 | new_drmem = (struct of_drconf_cell *)p; |
| 221 | |
| 222 | for (i = 0; i < entries; i++) { |
| 223 | if ((old_drmem[i].flags & DRCONF_MEM_ASSIGNED) && |
| 224 | (!(new_drmem[i].flags & DRCONF_MEM_ASSIGNED))) { |
| 225 | rc = pseries_remove_memblock(old_drmem[i].base_addr, |
| 226 | memblock_size); |
| 227 | break; |
| 228 | } else if ((!(old_drmem[i].flags & DRCONF_MEM_ASSIGNED)) && |
| 229 | (new_drmem[i].flags & DRCONF_MEM_ASSIGNED)) { |
| 230 | rc = memblock_add(old_drmem[i].base_addr, |
| 231 | memblock_size); |
| 232 | rc = (rc < 0) ? -EINVAL : 0; |
| 233 | break; |
| 234 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 235 | } |
| 236 | |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 237 | return rc; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 238 | } |
| 239 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 240 | static int pseries_memory_notifier(struct notifier_block *nb, |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 241 | unsigned long action, void *node) |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 242 | { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 243 | struct of_prop_reconfig *pr; |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 244 | int err = 0; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 245 | |
| 246 | switch (action) { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 247 | case OF_RECONFIG_ATTACH_NODE: |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 248 | err = pseries_add_mem_node(node); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 249 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 250 | case OF_RECONFIG_DETACH_NODE: |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 251 | err = pseries_remove_mem_node(node); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 252 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 253 | case OF_RECONFIG_UPDATE_PROPERTY: |
| 254 | pr = (struct of_prop_reconfig *)node; |
| 255 | if (!strcmp(pr->prop->name, "ibm,dynamic-memory")) |
| 256 | err = pseries_update_drconf_memory(pr); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 257 | break; |
| 258 | } |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 259 | return notifier_from_errno(err); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static struct notifier_block pseries_mem_nb = { |
| 263 | .notifier_call = pseries_memory_notifier, |
| 264 | }; |
| 265 | |
| 266 | static int __init pseries_memory_hotplug_init(void) |
| 267 | { |
| 268 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 269 | of_reconfig_notifier_register(&pseries_mem_nb); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 270 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame^] | 271 | #ifdef CONFIG_MEMORY_HOTREMOVE |
| 272 | ppc_md.remove_memory = pseries_remove_memory; |
| 273 | #endif |
| 274 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 275 | return 0; |
| 276 | } |
| 277 | machine_device_initcall(pseries, pseries_memory_hotplug_init); |