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