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 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 12 | #define pr_fmt(fmt) "pseries-hotplug-mem: " fmt |
| 13 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 14 | #include <linux/of.h> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 15 | #include <linux/of_address.h> |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 16 | #include <linux/memblock.h> |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 17 | #include <linux/memory.h> |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 18 | #include <linux/memory_hotplug.h> |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 19 | #include <linux/slab.h> |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 20 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 21 | #include <asm/firmware.h> |
| 22 | #include <asm/machdep.h> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 23 | #include <asm/prom.h> |
Michael Neuling | 0b2f828 | 2009-02-08 14:49:39 +0000 | [diff] [blame] | 24 | #include <asm/sparsemem.h> |
Anton Blanchard | 1217d34 | 2014-08-20 08:55:19 +1000 | [diff] [blame] | 25 | #include "pseries.h" |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 26 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 27 | static bool rtas_hp_event; |
| 28 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 29 | unsigned long pseries_memory_block_size(void) |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 30 | { |
| 31 | struct device_node *np; |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 32 | unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE; |
| 33 | struct resource r; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 34 | |
| 35 | np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 36 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 37 | const __be64 *size; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 38 | |
| 39 | size = of_get_property(np, "ibm,lmb-size", NULL); |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 40 | if (size) |
| 41 | memblock_size = be64_to_cpup(size); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 42 | of_node_put(np); |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 43 | } else if (machine_is(pseries)) { |
| 44 | /* This fallback really only applies to pseries */ |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 45 | unsigned int memzero_size = 0; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 46 | |
| 47 | np = of_find_node_by_path("/memory@0"); |
| 48 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 49 | if (!of_address_to_resource(np, 0, &r)) |
| 50 | memzero_size = resource_size(&r); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 51 | of_node_put(np); |
| 52 | } |
| 53 | |
| 54 | if (memzero_size) { |
| 55 | /* We now know the size of memory@0, use this to find |
| 56 | * the first memoryblock and get its size. |
| 57 | */ |
| 58 | char buf[64]; |
| 59 | |
| 60 | sprintf(buf, "/memory@%x", memzero_size); |
| 61 | np = of_find_node_by_path(buf); |
| 62 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 63 | if (!of_address_to_resource(np, 0, &r)) |
| 64 | memblock_size = resource_size(&r); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 65 | of_node_put(np); |
| 66 | } |
| 67 | } |
| 68 | } |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 69 | return memblock_size; |
| 70 | } |
| 71 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 72 | static void dlpar_free_drconf_property(struct property *prop) |
| 73 | { |
| 74 | kfree(prop->name); |
| 75 | kfree(prop->value); |
| 76 | kfree(prop); |
| 77 | } |
| 78 | |
| 79 | static struct property *dlpar_clone_drconf_property(struct device_node *dn) |
| 80 | { |
| 81 | struct property *prop, *new_prop; |
| 82 | struct of_drconf_cell *lmbs; |
| 83 | u32 num_lmbs, *p; |
| 84 | int i; |
| 85 | |
| 86 | prop = of_find_property(dn, "ibm,dynamic-memory", NULL); |
| 87 | if (!prop) |
| 88 | return NULL; |
| 89 | |
| 90 | new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL); |
| 91 | if (!new_prop) |
| 92 | return NULL; |
| 93 | |
| 94 | new_prop->name = kstrdup(prop->name, GFP_KERNEL); |
Andrzej Hajda | 2e16acc | 2015-08-07 09:59:12 +0200 | [diff] [blame] | 95 | new_prop->value = kmemdup(prop->value, prop->length, GFP_KERNEL); |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 96 | if (!new_prop->name || !new_prop->value) { |
| 97 | dlpar_free_drconf_property(new_prop); |
| 98 | return NULL; |
| 99 | } |
| 100 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 101 | new_prop->length = prop->length; |
| 102 | |
| 103 | /* Convert the property to cpu endian-ness */ |
| 104 | p = new_prop->value; |
| 105 | *p = be32_to_cpu(*p); |
| 106 | |
| 107 | num_lmbs = *p++; |
| 108 | lmbs = (struct of_drconf_cell *)p; |
| 109 | |
| 110 | for (i = 0; i < num_lmbs; i++) { |
| 111 | lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr); |
| 112 | lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index); |
| 113 | lmbs[i].flags = be32_to_cpu(lmbs[i].flags); |
| 114 | } |
| 115 | |
| 116 | return new_prop; |
| 117 | } |
| 118 | |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 119 | static void dlpar_update_drconf_property(struct device_node *dn, |
| 120 | struct property *prop) |
| 121 | { |
| 122 | struct of_drconf_cell *lmbs; |
| 123 | u32 num_lmbs, *p; |
| 124 | int i; |
| 125 | |
| 126 | /* Convert the property back to BE */ |
| 127 | p = prop->value; |
| 128 | num_lmbs = *p; |
| 129 | *p = cpu_to_be32(*p); |
| 130 | p++; |
| 131 | |
| 132 | lmbs = (struct of_drconf_cell *)p; |
| 133 | for (i = 0; i < num_lmbs; i++) { |
| 134 | lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr); |
| 135 | lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index); |
| 136 | lmbs[i].flags = cpu_to_be32(lmbs[i].flags); |
| 137 | } |
| 138 | |
| 139 | rtas_hp_event = true; |
| 140 | of_update_property(dn, prop); |
| 141 | rtas_hp_event = false; |
| 142 | } |
| 143 | |
| 144 | static int dlpar_update_device_tree_lmb(struct of_drconf_cell *lmb) |
| 145 | { |
| 146 | struct device_node *dn; |
| 147 | struct property *prop; |
| 148 | struct of_drconf_cell *lmbs; |
| 149 | u32 *p, num_lmbs; |
| 150 | int i; |
| 151 | |
| 152 | dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 153 | if (!dn) |
| 154 | return -ENODEV; |
| 155 | |
| 156 | prop = dlpar_clone_drconf_property(dn); |
| 157 | if (!prop) { |
| 158 | of_node_put(dn); |
| 159 | return -ENODEV; |
| 160 | } |
| 161 | |
| 162 | p = prop->value; |
| 163 | num_lmbs = *p++; |
| 164 | lmbs = (struct of_drconf_cell *)p; |
| 165 | |
| 166 | for (i = 0; i < num_lmbs; i++) { |
| 167 | if (lmbs[i].drc_index == lmb->drc_index) { |
| 168 | lmbs[i].flags = lmb->flags; |
| 169 | lmbs[i].aa_index = lmb->aa_index; |
| 170 | |
| 171 | dlpar_update_drconf_property(dn, prop); |
| 172 | break; |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | of_node_put(dn); |
| 177 | return 0; |
| 178 | } |
| 179 | |
| 180 | static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb) |
| 181 | { |
| 182 | struct device_node *parent, *lmb_node, *dr_node; |
| 183 | const u32 *lmb_assoc; |
| 184 | const u32 *assoc_arrays; |
| 185 | u32 aa_index; |
| 186 | int aa_arrays, aa_array_entries, aa_array_sz; |
| 187 | int i; |
| 188 | |
| 189 | parent = of_find_node_by_path("/"); |
| 190 | if (!parent) |
| 191 | return -ENODEV; |
| 192 | |
| 193 | lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index), |
| 194 | parent); |
| 195 | of_node_put(parent); |
| 196 | if (!lmb_node) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL); |
| 200 | if (!lmb_assoc) { |
| 201 | dlpar_free_cc_nodes(lmb_node); |
| 202 | return -ENODEV; |
| 203 | } |
| 204 | |
| 205 | dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 206 | if (!dr_node) { |
| 207 | dlpar_free_cc_nodes(lmb_node); |
| 208 | return -ENODEV; |
| 209 | } |
| 210 | |
| 211 | assoc_arrays = of_get_property(dr_node, |
| 212 | "ibm,associativity-lookup-arrays", |
| 213 | NULL); |
| 214 | of_node_put(dr_node); |
| 215 | if (!assoc_arrays) { |
| 216 | dlpar_free_cc_nodes(lmb_node); |
| 217 | return -ENODEV; |
| 218 | } |
| 219 | |
| 220 | /* The ibm,associativity-lookup-arrays property is defined to be |
| 221 | * a 32-bit value specifying the number of associativity arrays |
| 222 | * followed by a 32-bitvalue specifying the number of entries per |
| 223 | * array, followed by the associativity arrays. |
| 224 | */ |
| 225 | aa_arrays = be32_to_cpu(assoc_arrays[0]); |
| 226 | aa_array_entries = be32_to_cpu(assoc_arrays[1]); |
| 227 | aa_array_sz = aa_array_entries * sizeof(u32); |
| 228 | |
| 229 | aa_index = -1; |
| 230 | for (i = 0; i < aa_arrays; i++) { |
| 231 | int indx = (i * aa_array_entries) + 2; |
| 232 | |
| 233 | if (memcmp(&assoc_arrays[indx], &lmb_assoc[1], aa_array_sz)) |
| 234 | continue; |
| 235 | |
| 236 | aa_index = i; |
| 237 | break; |
| 238 | } |
| 239 | |
| 240 | dlpar_free_cc_nodes(lmb_node); |
| 241 | return aa_index; |
| 242 | } |
| 243 | |
| 244 | static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb) |
| 245 | { |
| 246 | int aa_index; |
| 247 | |
| 248 | lmb->flags |= DRCONF_MEM_ASSIGNED; |
| 249 | |
| 250 | aa_index = lookup_lmb_associativity_index(lmb); |
| 251 | if (aa_index < 0) { |
| 252 | pr_err("Couldn't find associativity index for drc index %x\n", |
| 253 | lmb->drc_index); |
| 254 | return aa_index; |
| 255 | } |
| 256 | |
| 257 | lmb->aa_index = aa_index; |
| 258 | return dlpar_update_device_tree_lmb(lmb); |
| 259 | } |
| 260 | |
| 261 | static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb) |
| 262 | { |
| 263 | lmb->flags &= ~DRCONF_MEM_ASSIGNED; |
| 264 | lmb->aa_index = 0xffffffff; |
| 265 | return dlpar_update_device_tree_lmb(lmb); |
| 266 | } |
| 267 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 268 | static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb) |
| 269 | { |
| 270 | unsigned long section_nr; |
| 271 | struct mem_section *mem_sect; |
| 272 | struct memory_block *mem_block; |
| 273 | |
| 274 | section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr)); |
| 275 | mem_sect = __nr_to_section(section_nr); |
| 276 | |
| 277 | mem_block = find_memory_block(mem_sect); |
| 278 | return mem_block; |
| 279 | } |
| 280 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 281 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 282 | static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size) |
| 283 | { |
| 284 | unsigned long block_sz, start_pfn; |
| 285 | int sections_per_block; |
| 286 | int i, nid; |
| 287 | |
| 288 | start_pfn = base >> PAGE_SHIFT; |
| 289 | |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 290 | lock_device_hotplug(); |
| 291 | |
| 292 | if (!pfn_valid(start_pfn)) |
| 293 | goto out; |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 294 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 295 | block_sz = pseries_memory_block_size(); |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 296 | sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; |
| 297 | nid = memory_add_physaddr_to_nid(base); |
| 298 | |
| 299 | for (i = 0; i < sections_per_block; i++) { |
| 300 | remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE); |
| 301 | base += MIN_MEMORY_BLOCK_SIZE; |
| 302 | } |
| 303 | |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 304 | out: |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 305 | /* Update memory regions for memory remove */ |
| 306 | memblock_remove(base, memblock_size); |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 307 | unlock_device_hotplug(); |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 308 | return 0; |
| 309 | } |
| 310 | |
| 311 | static int pseries_remove_mem_node(struct device_node *np) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 312 | { |
| 313 | const char *type; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 314 | const __be32 *regs; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 315 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 316 | unsigned int lmb_size; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 317 | int ret = -EINVAL; |
| 318 | |
| 319 | /* |
| 320 | * Check to see if we are actually removing memory |
| 321 | */ |
| 322 | type = of_get_property(np, "device_type", NULL); |
| 323 | if (type == NULL || strcmp(type, "memory") != 0) |
| 324 | return 0; |
| 325 | |
| 326 | /* |
Li Zhong | 4646d13 | 2014-08-07 13:11:58 +0800 | [diff] [blame] | 327 | * Find the base address and size of the memblock |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 328 | */ |
| 329 | regs = of_get_property(np, "reg", NULL); |
| 330 | if (!regs) |
| 331 | return ret; |
| 332 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 333 | base = be64_to_cpu(*(unsigned long *)regs); |
| 334 | lmb_size = be32_to_cpu(regs[3]); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 335 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 336 | pseries_remove_memblock(base, lmb_size); |
| 337 | return 0; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 338 | } |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 339 | |
| 340 | static bool lmb_is_removable(struct of_drconf_cell *lmb) |
| 341 | { |
| 342 | int i, scns_per_block; |
| 343 | int rc = 1; |
| 344 | unsigned long pfn, block_sz; |
| 345 | u64 phys_addr; |
| 346 | |
| 347 | if (!(lmb->flags & DRCONF_MEM_ASSIGNED)) |
| 348 | return false; |
| 349 | |
| 350 | block_sz = memory_block_size_bytes(); |
| 351 | scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; |
| 352 | phys_addr = lmb->base_addr; |
| 353 | |
| 354 | for (i = 0; i < scns_per_block; i++) { |
| 355 | pfn = PFN_DOWN(phys_addr); |
| 356 | if (!pfn_present(pfn)) |
| 357 | continue; |
| 358 | |
| 359 | rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION); |
| 360 | phys_addr += MIN_MEMORY_BLOCK_SIZE; |
| 361 | } |
| 362 | |
| 363 | return rc ? true : false; |
| 364 | } |
| 365 | |
| 366 | static int dlpar_add_lmb(struct of_drconf_cell *); |
| 367 | |
| 368 | static int dlpar_remove_lmb(struct of_drconf_cell *lmb) |
| 369 | { |
| 370 | struct memory_block *mem_block; |
| 371 | unsigned long block_sz; |
| 372 | int nid, rc; |
| 373 | |
| 374 | if (!lmb_is_removable(lmb)) |
| 375 | return -EINVAL; |
| 376 | |
| 377 | mem_block = lmb_to_memblock(lmb); |
| 378 | if (!mem_block) |
| 379 | return -EINVAL; |
| 380 | |
| 381 | rc = device_offline(&mem_block->dev); |
| 382 | put_device(&mem_block->dev); |
| 383 | if (rc) |
| 384 | return rc; |
| 385 | |
| 386 | block_sz = pseries_memory_block_size(); |
| 387 | nid = memory_add_physaddr_to_nid(lmb->base_addr); |
| 388 | |
| 389 | remove_memory(nid, lmb->base_addr, block_sz); |
| 390 | |
| 391 | /* Update memory regions for memory remove */ |
| 392 | memblock_remove(lmb->base_addr, block_sz); |
| 393 | |
| 394 | dlpar_release_drc(lmb->drc_index); |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 395 | dlpar_remove_device_tree_lmb(lmb); |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 396 | |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 397 | return 0; |
| 398 | } |
| 399 | |
| 400 | static int dlpar_memory_remove_by_count(u32 lmbs_to_remove, |
| 401 | struct property *prop) |
| 402 | { |
| 403 | struct of_drconf_cell *lmbs; |
| 404 | int lmbs_removed = 0; |
| 405 | int lmbs_available = 0; |
| 406 | u32 num_lmbs, *p; |
| 407 | int i, rc; |
| 408 | |
| 409 | pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove); |
| 410 | |
| 411 | if (lmbs_to_remove == 0) |
| 412 | return -EINVAL; |
| 413 | |
| 414 | p = prop->value; |
| 415 | num_lmbs = *p++; |
| 416 | lmbs = (struct of_drconf_cell *)p; |
| 417 | |
| 418 | /* Validate that there are enough LMBs to satisfy the request */ |
| 419 | for (i = 0; i < num_lmbs; i++) { |
| 420 | if (lmbs[i].flags & DRCONF_MEM_ASSIGNED) |
| 421 | lmbs_available++; |
| 422 | } |
| 423 | |
| 424 | if (lmbs_available < lmbs_to_remove) |
| 425 | return -EINVAL; |
| 426 | |
| 427 | for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) { |
| 428 | rc = dlpar_remove_lmb(&lmbs[i]); |
| 429 | if (rc) |
| 430 | continue; |
| 431 | |
| 432 | lmbs_removed++; |
| 433 | |
| 434 | /* Mark this lmb so we can add it later if all of the |
| 435 | * requested LMBs cannot be removed. |
| 436 | */ |
| 437 | lmbs[i].reserved = 1; |
| 438 | } |
| 439 | |
| 440 | if (lmbs_removed != lmbs_to_remove) { |
| 441 | pr_err("Memory hot-remove failed, adding LMB's back\n"); |
| 442 | |
| 443 | for (i = 0; i < num_lmbs; i++) { |
| 444 | if (!lmbs[i].reserved) |
| 445 | continue; |
| 446 | |
| 447 | rc = dlpar_add_lmb(&lmbs[i]); |
| 448 | if (rc) |
| 449 | pr_err("Failed to add LMB back, drc index %x\n", |
| 450 | lmbs[i].drc_index); |
| 451 | |
| 452 | lmbs[i].reserved = 0; |
| 453 | } |
| 454 | |
| 455 | rc = -EINVAL; |
| 456 | } else { |
| 457 | for (i = 0; i < num_lmbs; i++) { |
| 458 | if (!lmbs[i].reserved) |
| 459 | continue; |
| 460 | |
| 461 | pr_info("Memory at %llx was hot-removed\n", |
| 462 | lmbs[i].base_addr); |
| 463 | |
| 464 | lmbs[i].reserved = 0; |
| 465 | } |
| 466 | rc = 0; |
| 467 | } |
| 468 | |
| 469 | return rc; |
| 470 | } |
| 471 | |
| 472 | static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop) |
| 473 | { |
| 474 | struct of_drconf_cell *lmbs; |
| 475 | u32 num_lmbs, *p; |
| 476 | int lmb_found; |
| 477 | int i, rc; |
| 478 | |
| 479 | pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index); |
| 480 | |
| 481 | p = prop->value; |
| 482 | num_lmbs = *p++; |
| 483 | lmbs = (struct of_drconf_cell *)p; |
| 484 | |
| 485 | lmb_found = 0; |
| 486 | for (i = 0; i < num_lmbs; i++) { |
| 487 | if (lmbs[i].drc_index == drc_index) { |
| 488 | lmb_found = 1; |
| 489 | rc = dlpar_remove_lmb(&lmbs[i]); |
| 490 | break; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if (!lmb_found) |
| 495 | rc = -EINVAL; |
| 496 | |
| 497 | if (rc) |
| 498 | pr_info("Failed to hot-remove memory at %llx\n", |
| 499 | lmbs[i].base_addr); |
| 500 | else |
| 501 | pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr); |
| 502 | |
| 503 | return rc; |
| 504 | } |
| 505 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 506 | #else |
| 507 | static inline int pseries_remove_memblock(unsigned long base, |
| 508 | unsigned int memblock_size) |
| 509 | { |
| 510 | return -EOPNOTSUPP; |
| 511 | } |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 512 | static inline int pseries_remove_mem_node(struct device_node *np) |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 513 | { |
Gavin Shan | f1b3929 | 2014-08-11 19:16:19 +1000 | [diff] [blame] | 514 | return 0; |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 515 | } |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 516 | static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog) |
| 517 | { |
| 518 | return -EOPNOTSUPP; |
| 519 | } |
Alexey Kardashevskiy | 16e00f5 | 2015-04-14 17:01:56 +1000 | [diff] [blame] | 520 | static int dlpar_remove_lmb(struct of_drconf_cell *lmb) |
| 521 | { |
| 522 | return -EOPNOTSUPP; |
| 523 | } |
| 524 | static int dlpar_memory_remove_by_count(u32 lmbs_to_remove, |
| 525 | struct property *prop) |
| 526 | { |
| 527 | return -EOPNOTSUPP; |
| 528 | } |
| 529 | static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop) |
| 530 | { |
| 531 | return -EOPNOTSUPP; |
| 532 | } |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 533 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 534 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 535 | |
Nathan Fontenot | 4a4bdfe | 2016-02-10 11:10:44 -0600 | [diff] [blame] | 536 | static int dlpar_add_lmb_memory(struct of_drconf_cell *lmb) |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 537 | { |
| 538 | struct memory_block *mem_block; |
| 539 | unsigned long block_sz; |
| 540 | int nid, rc; |
| 541 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 542 | block_sz = memory_block_size_bytes(); |
| 543 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 544 | /* Find the node id for this address */ |
| 545 | nid = memory_add_physaddr_to_nid(lmb->base_addr); |
| 546 | |
| 547 | /* Add the memory */ |
| 548 | rc = add_memory(nid, lmb->base_addr, block_sz); |
Nathan Fontenot | 4a4bdfe | 2016-02-10 11:10:44 -0600 | [diff] [blame] | 549 | if (rc) |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 550 | return rc; |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 551 | |
| 552 | /* Register this block of memory */ |
| 553 | rc = memblock_add(lmb->base_addr, block_sz); |
| 554 | if (rc) { |
| 555 | remove_memory(nid, lmb->base_addr, block_sz); |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 556 | return rc; |
| 557 | } |
| 558 | |
| 559 | mem_block = lmb_to_memblock(lmb); |
| 560 | if (!mem_block) { |
| 561 | remove_memory(nid, lmb->base_addr, block_sz); |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 562 | return -EINVAL; |
| 563 | } |
| 564 | |
| 565 | rc = device_online(&mem_block->dev); |
| 566 | put_device(&mem_block->dev); |
| 567 | if (rc) { |
| 568 | remove_memory(nid, lmb->base_addr, block_sz); |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 569 | return rc; |
| 570 | } |
| 571 | |
| 572 | lmb->flags |= DRCONF_MEM_ASSIGNED; |
| 573 | return 0; |
| 574 | } |
| 575 | |
Nathan Fontenot | 4a4bdfe | 2016-02-10 11:10:44 -0600 | [diff] [blame] | 576 | static int dlpar_add_lmb(struct of_drconf_cell *lmb) |
| 577 | { |
| 578 | int rc; |
| 579 | |
| 580 | if (lmb->flags & DRCONF_MEM_ASSIGNED) |
| 581 | return -EINVAL; |
| 582 | |
| 583 | rc = dlpar_acquire_drc(lmb->drc_index); |
| 584 | if (rc) |
| 585 | return rc; |
| 586 | |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 587 | rc = dlpar_add_device_tree_lmb(lmb); |
| 588 | if (rc) { |
| 589 | pr_err("Couldn't update device tree for drc index %x\n", |
| 590 | lmb->drc_index); |
Nathan Fontenot | 4a4bdfe | 2016-02-10 11:10:44 -0600 | [diff] [blame] | 591 | dlpar_release_drc(lmb->drc_index); |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 592 | return rc; |
| 593 | } |
| 594 | |
| 595 | rc = dlpar_add_lmb_memory(lmb); |
| 596 | if (rc) { |
| 597 | dlpar_remove_device_tree_lmb(lmb); |
| 598 | dlpar_release_drc(lmb->drc_index); |
| 599 | } |
Nathan Fontenot | 4a4bdfe | 2016-02-10 11:10:44 -0600 | [diff] [blame] | 600 | |
| 601 | return rc; |
| 602 | } |
| 603 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 604 | static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop) |
| 605 | { |
| 606 | struct of_drconf_cell *lmbs; |
| 607 | u32 num_lmbs, *p; |
| 608 | int lmbs_available = 0; |
| 609 | int lmbs_added = 0; |
| 610 | int i, rc; |
| 611 | |
| 612 | pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add); |
| 613 | |
| 614 | if (lmbs_to_add == 0) |
| 615 | return -EINVAL; |
| 616 | |
| 617 | p = prop->value; |
| 618 | num_lmbs = *p++; |
| 619 | lmbs = (struct of_drconf_cell *)p; |
| 620 | |
| 621 | /* Validate that there are enough LMBs to satisfy the request */ |
| 622 | for (i = 0; i < num_lmbs; i++) { |
| 623 | if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED)) |
| 624 | lmbs_available++; |
| 625 | } |
| 626 | |
| 627 | if (lmbs_available < lmbs_to_add) |
| 628 | return -EINVAL; |
| 629 | |
| 630 | for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) { |
| 631 | rc = dlpar_add_lmb(&lmbs[i]); |
| 632 | if (rc) |
| 633 | continue; |
| 634 | |
| 635 | lmbs_added++; |
| 636 | |
| 637 | /* Mark this lmb so we can remove it later if all of the |
| 638 | * requested LMBs cannot be added. |
| 639 | */ |
| 640 | lmbs[i].reserved = 1; |
| 641 | } |
| 642 | |
| 643 | if (lmbs_added != lmbs_to_add) { |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 644 | pr_err("Memory hot-add failed, removing any added LMBs\n"); |
| 645 | |
| 646 | for (i = 0; i < num_lmbs; i++) { |
| 647 | if (!lmbs[i].reserved) |
| 648 | continue; |
| 649 | |
| 650 | rc = dlpar_remove_lmb(&lmbs[i]); |
| 651 | if (rc) |
| 652 | pr_err("Failed to remove LMB, drc index %x\n", |
| 653 | be32_to_cpu(lmbs[i].drc_index)); |
| 654 | } |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 655 | rc = -EINVAL; |
| 656 | } else { |
| 657 | for (i = 0; i < num_lmbs; i++) { |
| 658 | if (!lmbs[i].reserved) |
| 659 | continue; |
| 660 | |
| 661 | pr_info("Memory at %llx (drc index %x) was hot-added\n", |
| 662 | lmbs[i].base_addr, lmbs[i].drc_index); |
| 663 | lmbs[i].reserved = 0; |
| 664 | } |
| 665 | } |
| 666 | |
| 667 | return rc; |
| 668 | } |
| 669 | |
| 670 | static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop) |
| 671 | { |
| 672 | struct of_drconf_cell *lmbs; |
| 673 | u32 num_lmbs, *p; |
| 674 | int i, lmb_found; |
| 675 | int rc; |
| 676 | |
| 677 | pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index); |
| 678 | |
| 679 | p = prop->value; |
| 680 | num_lmbs = *p++; |
| 681 | lmbs = (struct of_drconf_cell *)p; |
| 682 | |
| 683 | lmb_found = 0; |
| 684 | for (i = 0; i < num_lmbs; i++) { |
| 685 | if (lmbs[i].drc_index == drc_index) { |
| 686 | lmb_found = 1; |
| 687 | rc = dlpar_add_lmb(&lmbs[i]); |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | |
| 692 | if (!lmb_found) |
| 693 | rc = -EINVAL; |
| 694 | |
| 695 | if (rc) |
| 696 | pr_info("Failed to hot-add memory, drc index %x\n", drc_index); |
| 697 | else |
| 698 | pr_info("Memory at %llx (drc index %x) was hot-added\n", |
| 699 | lmbs[i].base_addr, drc_index); |
| 700 | |
| 701 | return rc; |
| 702 | } |
| 703 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 704 | int dlpar_memory(struct pseries_hp_errorlog *hp_elog) |
| 705 | { |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 706 | struct device_node *dn; |
| 707 | struct property *prop; |
| 708 | u32 count, drc_index; |
| 709 | int rc; |
| 710 | |
| 711 | count = hp_elog->_drc_u.drc_count; |
| 712 | drc_index = hp_elog->_drc_u.drc_index; |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 713 | |
| 714 | lock_device_hotplug(); |
| 715 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 716 | dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
Nathan Fontenot | b0a478e | 2015-04-07 09:53:46 -0500 | [diff] [blame] | 717 | if (!dn) { |
| 718 | rc = -EINVAL; |
| 719 | goto dlpar_memory_out; |
| 720 | } |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 721 | |
| 722 | prop = dlpar_clone_drconf_property(dn); |
| 723 | if (!prop) { |
Nathan Fontenot | b0a478e | 2015-04-07 09:53:46 -0500 | [diff] [blame] | 724 | rc = -EINVAL; |
| 725 | goto dlpar_memory_out; |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 726 | } |
| 727 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 728 | switch (hp_elog->action) { |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 729 | case PSERIES_HP_ELOG_ACTION_ADD: |
| 730 | if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) |
| 731 | rc = dlpar_memory_add_by_count(count, prop); |
| 732 | else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) |
| 733 | rc = dlpar_memory_add_by_index(drc_index, prop); |
| 734 | else |
| 735 | rc = -EINVAL; |
| 736 | break; |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 737 | case PSERIES_HP_ELOG_ACTION_REMOVE: |
| 738 | if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) |
| 739 | rc = dlpar_memory_remove_by_count(count, prop); |
| 740 | else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) |
| 741 | rc = dlpar_memory_remove_by_index(drc_index, prop); |
| 742 | else |
| 743 | rc = -EINVAL; |
| 744 | break; |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 745 | default: |
| 746 | pr_err("Invalid action (%d) specified\n", hp_elog->action); |
| 747 | rc = -EINVAL; |
| 748 | break; |
| 749 | } |
| 750 | |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 751 | dlpar_free_drconf_property(prop); |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 752 | |
Nathan Fontenot | b0a478e | 2015-04-07 09:53:46 -0500 | [diff] [blame] | 753 | dlpar_memory_out: |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 754 | of_node_put(dn); |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 755 | unlock_device_hotplug(); |
| 756 | return rc; |
| 757 | } |
| 758 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 759 | static int pseries_add_mem_node(struct device_node *np) |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 760 | { |
| 761 | const char *type; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 762 | const __be32 *regs; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 763 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 764 | unsigned int lmb_size; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 765 | int ret = -EINVAL; |
| 766 | |
| 767 | /* |
| 768 | * Check to see if we are actually adding memory |
| 769 | */ |
| 770 | type = of_get_property(np, "device_type", NULL); |
| 771 | if (type == NULL || strcmp(type, "memory") != 0) |
| 772 | return 0; |
| 773 | |
| 774 | /* |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 775 | * Find the base and size of the memblock |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 776 | */ |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 777 | regs = of_get_property(np, "reg", NULL); |
| 778 | if (!regs) |
| 779 | return ret; |
| 780 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 781 | base = be64_to_cpu(*(unsigned long *)regs); |
| 782 | lmb_size = be32_to_cpu(regs[3]); |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 783 | |
| 784 | /* |
| 785 | * Update memory region to represent the memory add |
| 786 | */ |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 787 | ret = memblock_add(base, lmb_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 788 | return (ret < 0) ? -EINVAL : 0; |
| 789 | } |
| 790 | |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 791 | static int pseries_update_drconf_memory(struct of_reconfig_data *pr) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 792 | { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 793 | struct of_drconf_cell *new_drmem, *old_drmem; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 794 | unsigned long memblock_size; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 795 | u32 entries; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 796 | __be32 *p; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 797 | int i, rc = -EINVAL; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 798 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 799 | if (rtas_hp_event) |
| 800 | return 0; |
| 801 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 802 | memblock_size = pseries_memory_block_size(); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 803 | if (!memblock_size) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 804 | return -EINVAL; |
| 805 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 806 | p = (__be32 *) pr->old_prop->value; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 807 | if (!p) |
| 808 | return -EINVAL; |
| 809 | |
| 810 | /* The first int of the property is the number of lmb's described |
| 811 | * by the property. This is followed by an array of of_drconf_cell |
Li Zhong | 4646d13 | 2014-08-07 13:11:58 +0800 | [diff] [blame] | 812 | * entries. Get the number of entries and skip to the array of |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 813 | * of_drconf_cell's. |
| 814 | */ |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 815 | entries = be32_to_cpu(*p++); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 816 | old_drmem = (struct of_drconf_cell *)p; |
| 817 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 818 | p = (__be32 *)pr->prop->value; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 819 | p++; |
| 820 | new_drmem = (struct of_drconf_cell *)p; |
| 821 | |
| 822 | for (i = 0; i < entries; i++) { |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 823 | if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) && |
| 824 | (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) { |
| 825 | rc = pseries_remove_memblock( |
| 826 | be64_to_cpu(old_drmem[i].base_addr), |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 827 | memblock_size); |
| 828 | break; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 829 | } else if ((!(be32_to_cpu(old_drmem[i].flags) & |
| 830 | DRCONF_MEM_ASSIGNED)) && |
| 831 | (be32_to_cpu(new_drmem[i].flags) & |
| 832 | DRCONF_MEM_ASSIGNED)) { |
| 833 | rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr), |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 834 | memblock_size); |
| 835 | rc = (rc < 0) ? -EINVAL : 0; |
| 836 | break; |
| 837 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 838 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 839 | return rc; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 842 | static int pseries_memory_notifier(struct notifier_block *nb, |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 843 | unsigned long action, void *data) |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 844 | { |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 845 | struct of_reconfig_data *rd = data; |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 846 | int err = 0; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 847 | |
| 848 | switch (action) { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 849 | case OF_RECONFIG_ATTACH_NODE: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 850 | err = pseries_add_mem_node(rd->dn); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 851 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 852 | case OF_RECONFIG_DETACH_NODE: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 853 | err = pseries_remove_mem_node(rd->dn); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 854 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 855 | case OF_RECONFIG_UPDATE_PROPERTY: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 856 | if (!strcmp(rd->prop->name, "ibm,dynamic-memory")) |
| 857 | err = pseries_update_drconf_memory(rd); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 858 | break; |
| 859 | } |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 860 | return notifier_from_errno(err); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static struct notifier_block pseries_mem_nb = { |
| 864 | .notifier_call = pseries_memory_notifier, |
| 865 | }; |
| 866 | |
| 867 | static int __init pseries_memory_hotplug_init(void) |
| 868 | { |
| 869 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 870 | of_reconfig_notifier_register(&pseries_mem_nb); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 871 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 872 | return 0; |
| 873 | } |
| 874 | machine_device_initcall(pseries, pseries_memory_hotplug_init); |