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 | |
Nathan Fontenot | c05a5a4 | 2016-06-20 09:01:38 -0500 | [diff] [blame] | 194 | static u32 find_aa_index(struct device_node *dr_node, |
| 195 | struct property *ala_prop, const u32 *lmb_assoc) |
| 196 | { |
| 197 | u32 *assoc_arrays; |
| 198 | u32 aa_index; |
| 199 | int aa_arrays, aa_array_entries, aa_array_sz; |
| 200 | int i, index; |
| 201 | |
| 202 | /* |
| 203 | * The ibm,associativity-lookup-arrays property is defined to be |
| 204 | * a 32-bit value specifying the number of associativity arrays |
| 205 | * followed by a 32-bitvalue specifying the number of entries per |
| 206 | * array, followed by the associativity arrays. |
| 207 | */ |
| 208 | assoc_arrays = ala_prop->value; |
| 209 | |
| 210 | aa_arrays = be32_to_cpu(assoc_arrays[0]); |
| 211 | aa_array_entries = be32_to_cpu(assoc_arrays[1]); |
| 212 | aa_array_sz = aa_array_entries * sizeof(u32); |
| 213 | |
| 214 | aa_index = -1; |
| 215 | for (i = 0; i < aa_arrays; i++) { |
| 216 | index = (i * aa_array_entries) + 2; |
| 217 | |
| 218 | if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz)) |
| 219 | continue; |
| 220 | |
| 221 | aa_index = i; |
| 222 | break; |
| 223 | } |
| 224 | |
| 225 | if (aa_index == -1) { |
| 226 | struct property *new_prop; |
| 227 | u32 new_prop_size; |
| 228 | |
| 229 | new_prop_size = ala_prop->length + aa_array_sz; |
| 230 | new_prop = dlpar_clone_property(ala_prop, new_prop_size); |
| 231 | if (!new_prop) |
| 232 | return -1; |
| 233 | |
| 234 | assoc_arrays = new_prop->value; |
| 235 | |
| 236 | /* increment the number of entries in the lookup array */ |
| 237 | assoc_arrays[0] = cpu_to_be32(aa_arrays + 1); |
| 238 | |
| 239 | /* copy the new associativity into the lookup array */ |
| 240 | index = aa_arrays * aa_array_entries + 2; |
| 241 | memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz); |
| 242 | |
| 243 | of_update_property(dr_node, new_prop); |
| 244 | |
| 245 | /* |
| 246 | * The associativity lookup array index for this lmb is |
| 247 | * number of entries - 1 since we added its associativity |
| 248 | * to the end of the lookup array. |
| 249 | */ |
| 250 | aa_index = be32_to_cpu(assoc_arrays[0]) - 1; |
| 251 | } |
| 252 | |
| 253 | return aa_index; |
| 254 | } |
| 255 | |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 256 | static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb) |
| 257 | { |
| 258 | struct device_node *parent, *lmb_node, *dr_node; |
Nathan Fontenot | c05a5a4 | 2016-06-20 09:01:38 -0500 | [diff] [blame] | 259 | struct property *ala_prop; |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 260 | const u32 *lmb_assoc; |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 261 | u32 aa_index; |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 262 | |
| 263 | parent = of_find_node_by_path("/"); |
| 264 | if (!parent) |
| 265 | return -ENODEV; |
| 266 | |
| 267 | lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index), |
| 268 | parent); |
| 269 | of_node_put(parent); |
| 270 | if (!lmb_node) |
| 271 | return -EINVAL; |
| 272 | |
| 273 | lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL); |
| 274 | if (!lmb_assoc) { |
| 275 | dlpar_free_cc_nodes(lmb_node); |
| 276 | return -ENODEV; |
| 277 | } |
| 278 | |
| 279 | dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 280 | if (!dr_node) { |
| 281 | dlpar_free_cc_nodes(lmb_node); |
| 282 | return -ENODEV; |
| 283 | } |
| 284 | |
Nathan Fontenot | c05a5a4 | 2016-06-20 09:01:38 -0500 | [diff] [blame] | 285 | ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays", |
| 286 | NULL); |
| 287 | if (!ala_prop) { |
| 288 | of_node_put(dr_node); |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 289 | dlpar_free_cc_nodes(lmb_node); |
| 290 | return -ENODEV; |
| 291 | } |
| 292 | |
Nathan Fontenot | c05a5a4 | 2016-06-20 09:01:38 -0500 | [diff] [blame] | 293 | aa_index = find_aa_index(dr_node, ala_prop, lmb_assoc); |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 294 | |
| 295 | dlpar_free_cc_nodes(lmb_node); |
| 296 | return aa_index; |
| 297 | } |
| 298 | |
| 299 | static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb) |
| 300 | { |
| 301 | int aa_index; |
| 302 | |
| 303 | lmb->flags |= DRCONF_MEM_ASSIGNED; |
| 304 | |
| 305 | aa_index = lookup_lmb_associativity_index(lmb); |
| 306 | if (aa_index < 0) { |
| 307 | pr_err("Couldn't find associativity index for drc index %x\n", |
| 308 | lmb->drc_index); |
| 309 | return aa_index; |
| 310 | } |
| 311 | |
| 312 | lmb->aa_index = aa_index; |
| 313 | return dlpar_update_device_tree_lmb(lmb); |
| 314 | } |
| 315 | |
| 316 | static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb) |
| 317 | { |
| 318 | lmb->flags &= ~DRCONF_MEM_ASSIGNED; |
| 319 | lmb->aa_index = 0xffffffff; |
| 320 | return dlpar_update_device_tree_lmb(lmb); |
| 321 | } |
| 322 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 323 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 324 | static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size) |
| 325 | { |
| 326 | unsigned long block_sz, start_pfn; |
| 327 | int sections_per_block; |
| 328 | int i, nid; |
| 329 | |
| 330 | start_pfn = base >> PAGE_SHIFT; |
| 331 | |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 332 | lock_device_hotplug(); |
| 333 | |
| 334 | if (!pfn_valid(start_pfn)) |
| 335 | goto out; |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 336 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 337 | block_sz = pseries_memory_block_size(); |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 338 | sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; |
| 339 | nid = memory_add_physaddr_to_nid(base); |
| 340 | |
| 341 | for (i = 0; i < sections_per_block; i++) { |
| 342 | remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE); |
| 343 | base += MIN_MEMORY_BLOCK_SIZE; |
| 344 | } |
| 345 | |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 346 | out: |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 347 | /* Update memory regions for memory remove */ |
| 348 | memblock_remove(base, memblock_size); |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 349 | unlock_device_hotplug(); |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | static int pseries_remove_mem_node(struct device_node *np) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 354 | { |
| 355 | const char *type; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 356 | const __be32 *regs; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 357 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 358 | unsigned int lmb_size; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 359 | int ret = -EINVAL; |
| 360 | |
| 361 | /* |
| 362 | * Check to see if we are actually removing memory |
| 363 | */ |
| 364 | type = of_get_property(np, "device_type", NULL); |
| 365 | if (type == NULL || strcmp(type, "memory") != 0) |
| 366 | return 0; |
| 367 | |
| 368 | /* |
Li Zhong | 4646d13 | 2014-08-07 13:11:58 +0800 | [diff] [blame] | 369 | * Find the base address and size of the memblock |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 370 | */ |
| 371 | regs = of_get_property(np, "reg", NULL); |
| 372 | if (!regs) |
| 373 | return ret; |
| 374 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 375 | base = be64_to_cpu(*(unsigned long *)regs); |
| 376 | lmb_size = be32_to_cpu(regs[3]); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 377 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 378 | pseries_remove_memblock(base, lmb_size); |
| 379 | return 0; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 380 | } |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 381 | |
| 382 | static bool lmb_is_removable(struct of_drconf_cell *lmb) |
| 383 | { |
| 384 | int i, scns_per_block; |
| 385 | int rc = 1; |
| 386 | unsigned long pfn, block_sz; |
| 387 | u64 phys_addr; |
| 388 | |
| 389 | if (!(lmb->flags & DRCONF_MEM_ASSIGNED)) |
| 390 | return false; |
| 391 | |
| 392 | block_sz = memory_block_size_bytes(); |
| 393 | scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; |
| 394 | phys_addr = lmb->base_addr; |
| 395 | |
| 396 | for (i = 0; i < scns_per_block; i++) { |
| 397 | pfn = PFN_DOWN(phys_addr); |
| 398 | if (!pfn_present(pfn)) |
| 399 | continue; |
| 400 | |
| 401 | rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION); |
| 402 | phys_addr += MIN_MEMORY_BLOCK_SIZE; |
| 403 | } |
| 404 | |
| 405 | return rc ? true : false; |
| 406 | } |
| 407 | |
| 408 | static int dlpar_add_lmb(struct of_drconf_cell *); |
| 409 | |
Alastair D'Silva | 9dc5128 | 2016-08-01 16:32:51 +1000 | [diff] [blame] | 410 | static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb) |
| 411 | { |
| 412 | unsigned long section_nr; |
| 413 | struct mem_section *mem_sect; |
| 414 | struct memory_block *mem_block; |
| 415 | |
| 416 | section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr)); |
| 417 | mem_sect = __nr_to_section(section_nr); |
| 418 | |
| 419 | mem_block = find_memory_block(mem_sect); |
| 420 | return mem_block; |
| 421 | } |
| 422 | |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 423 | static int dlpar_remove_lmb(struct of_drconf_cell *lmb) |
| 424 | { |
| 425 | struct memory_block *mem_block; |
| 426 | unsigned long block_sz; |
| 427 | int nid, rc; |
| 428 | |
| 429 | if (!lmb_is_removable(lmb)) |
| 430 | return -EINVAL; |
| 431 | |
| 432 | mem_block = lmb_to_memblock(lmb); |
| 433 | if (!mem_block) |
| 434 | return -EINVAL; |
| 435 | |
| 436 | rc = device_offline(&mem_block->dev); |
| 437 | put_device(&mem_block->dev); |
| 438 | if (rc) |
| 439 | return rc; |
| 440 | |
| 441 | block_sz = pseries_memory_block_size(); |
| 442 | nid = memory_add_physaddr_to_nid(lmb->base_addr); |
| 443 | |
| 444 | remove_memory(nid, lmb->base_addr, block_sz); |
| 445 | |
| 446 | /* Update memory regions for memory remove */ |
| 447 | memblock_remove(lmb->base_addr, block_sz); |
| 448 | |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 449 | dlpar_remove_device_tree_lmb(lmb); |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
| 453 | static int dlpar_memory_remove_by_count(u32 lmbs_to_remove, |
| 454 | struct property *prop) |
| 455 | { |
| 456 | struct of_drconf_cell *lmbs; |
| 457 | int lmbs_removed = 0; |
| 458 | int lmbs_available = 0; |
| 459 | u32 num_lmbs, *p; |
| 460 | int i, rc; |
| 461 | |
| 462 | pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove); |
| 463 | |
| 464 | if (lmbs_to_remove == 0) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | p = prop->value; |
| 468 | num_lmbs = *p++; |
| 469 | lmbs = (struct of_drconf_cell *)p; |
| 470 | |
| 471 | /* Validate that there are enough LMBs to satisfy the request */ |
| 472 | for (i = 0; i < num_lmbs; i++) { |
Nathan Fontenot | 2db029e | 2016-11-28 11:50:45 -0500 | [diff] [blame] | 473 | if (lmb_is_removable(&lmbs[i])) |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 474 | lmbs_available++; |
| 475 | } |
| 476 | |
Nathan Fontenot | 2db029e | 2016-11-28 11:50:45 -0500 | [diff] [blame] | 477 | if (lmbs_available < lmbs_to_remove) { |
| 478 | pr_info("Not enough LMBs available (%d of %d) to satisfy request\n", |
| 479 | lmbs_available, lmbs_to_remove); |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 480 | return -EINVAL; |
Nathan Fontenot | 2db029e | 2016-11-28 11:50:45 -0500 | [diff] [blame] | 481 | } |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 482 | |
| 483 | for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) { |
| 484 | rc = dlpar_remove_lmb(&lmbs[i]); |
| 485 | if (rc) |
| 486 | continue; |
| 487 | |
| 488 | lmbs_removed++; |
| 489 | |
| 490 | /* Mark this lmb so we can add it later if all of the |
| 491 | * requested LMBs cannot be removed. |
| 492 | */ |
| 493 | lmbs[i].reserved = 1; |
| 494 | } |
| 495 | |
| 496 | if (lmbs_removed != lmbs_to_remove) { |
| 497 | pr_err("Memory hot-remove failed, adding LMB's back\n"); |
| 498 | |
| 499 | for (i = 0; i < num_lmbs; i++) { |
| 500 | if (!lmbs[i].reserved) |
| 501 | continue; |
| 502 | |
| 503 | rc = dlpar_add_lmb(&lmbs[i]); |
| 504 | if (rc) |
| 505 | pr_err("Failed to add LMB back, drc index %x\n", |
| 506 | lmbs[i].drc_index); |
| 507 | |
| 508 | lmbs[i].reserved = 0; |
| 509 | } |
| 510 | |
| 511 | rc = -EINVAL; |
| 512 | } else { |
| 513 | for (i = 0; i < num_lmbs; i++) { |
| 514 | if (!lmbs[i].reserved) |
| 515 | continue; |
| 516 | |
John Allen | c21f515 | 2017-01-06 13:25:53 -0600 | [diff] [blame] | 517 | dlpar_release_drc(lmbs[i].drc_index); |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 518 | pr_info("Memory at %llx was hot-removed\n", |
| 519 | lmbs[i].base_addr); |
| 520 | |
| 521 | lmbs[i].reserved = 0; |
| 522 | } |
| 523 | rc = 0; |
| 524 | } |
| 525 | |
| 526 | return rc; |
| 527 | } |
| 528 | |
| 529 | static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop) |
| 530 | { |
| 531 | struct of_drconf_cell *lmbs; |
| 532 | u32 num_lmbs, *p; |
| 533 | int lmb_found; |
| 534 | int i, rc; |
| 535 | |
| 536 | pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index); |
| 537 | |
| 538 | p = prop->value; |
| 539 | num_lmbs = *p++; |
| 540 | lmbs = (struct of_drconf_cell *)p; |
| 541 | |
| 542 | lmb_found = 0; |
| 543 | for (i = 0; i < num_lmbs; i++) { |
| 544 | if (lmbs[i].drc_index == drc_index) { |
| 545 | lmb_found = 1; |
| 546 | rc = dlpar_remove_lmb(&lmbs[i]); |
John Allen | c21f515 | 2017-01-06 13:25:53 -0600 | [diff] [blame] | 547 | if (!rc) |
| 548 | dlpar_release_drc(lmbs[i].drc_index); |
| 549 | |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | if (!lmb_found) |
| 555 | rc = -EINVAL; |
| 556 | |
| 557 | if (rc) |
| 558 | pr_info("Failed to hot-remove memory at %llx\n", |
| 559 | lmbs[i].base_addr); |
| 560 | else |
| 561 | pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr); |
| 562 | |
| 563 | return rc; |
| 564 | } |
| 565 | |
John Allen | e70d597 | 2017-01-06 13:27:26 -0600 | [diff] [blame] | 566 | static int dlpar_memory_readd_by_index(u32 drc_index, struct property *prop) |
| 567 | { |
| 568 | struct of_drconf_cell *lmbs; |
| 569 | u32 num_lmbs, *p; |
| 570 | int lmb_found; |
| 571 | int i, rc; |
| 572 | |
| 573 | pr_info("Attempting to update LMB, drc index %x\n", drc_index); |
| 574 | |
| 575 | p = prop->value; |
| 576 | num_lmbs = *p++; |
| 577 | lmbs = (struct of_drconf_cell *)p; |
| 578 | |
| 579 | lmb_found = 0; |
| 580 | for (i = 0; i < num_lmbs; i++) { |
| 581 | if (lmbs[i].drc_index == drc_index) { |
| 582 | lmb_found = 1; |
| 583 | rc = dlpar_remove_lmb(&lmbs[i]); |
| 584 | if (!rc) { |
| 585 | rc = dlpar_add_lmb(&lmbs[i]); |
| 586 | if (rc) |
| 587 | dlpar_release_drc(lmbs[i].drc_index); |
| 588 | } |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | if (!lmb_found) |
| 594 | rc = -EINVAL; |
| 595 | |
| 596 | if (rc) |
| 597 | pr_info("Failed to update memory at %llx\n", |
| 598 | lmbs[i].base_addr); |
| 599 | else |
| 600 | pr_info("Memory at %llx was updated\n", lmbs[i].base_addr); |
| 601 | |
| 602 | return rc; |
| 603 | } |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 604 | #else |
| 605 | static inline int pseries_remove_memblock(unsigned long base, |
| 606 | unsigned int memblock_size) |
| 607 | { |
| 608 | return -EOPNOTSUPP; |
| 609 | } |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 610 | static inline int pseries_remove_mem_node(struct device_node *np) |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 611 | { |
Gavin Shan | f1b3929 | 2014-08-11 19:16:19 +1000 | [diff] [blame] | 612 | return 0; |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 613 | } |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 614 | static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog) |
| 615 | { |
| 616 | return -EOPNOTSUPP; |
| 617 | } |
Alexey Kardashevskiy | 16e00f5 | 2015-04-14 17:01:56 +1000 | [diff] [blame] | 618 | static int dlpar_remove_lmb(struct of_drconf_cell *lmb) |
| 619 | { |
| 620 | return -EOPNOTSUPP; |
| 621 | } |
| 622 | static int dlpar_memory_remove_by_count(u32 lmbs_to_remove, |
| 623 | struct property *prop) |
| 624 | { |
| 625 | return -EOPNOTSUPP; |
| 626 | } |
| 627 | static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop) |
| 628 | { |
| 629 | return -EOPNOTSUPP; |
| 630 | } |
Michael Ellerman | f84775c | 2017-02-16 11:27:00 +1100 | [diff] [blame] | 631 | static int dlpar_memory_readd_by_index(u32 drc_index, struct property *prop) |
| 632 | { |
| 633 | return -EOPNOTSUPP; |
| 634 | } |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 635 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 636 | |
Nathan Fontenot | fdb4f6e | 2016-06-29 12:20:30 -0500 | [diff] [blame] | 637 | static int dlpar_add_lmb(struct of_drconf_cell *lmb) |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 638 | { |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 639 | unsigned long block_sz; |
| 640 | int nid, rc; |
| 641 | |
Nathan Fontenot | 4a4bdfe | 2016-02-10 11:10:44 -0600 | [diff] [blame] | 642 | if (lmb->flags & DRCONF_MEM_ASSIGNED) |
| 643 | return -EINVAL; |
| 644 | |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 645 | rc = dlpar_add_device_tree_lmb(lmb); |
| 646 | if (rc) { |
| 647 | pr_err("Couldn't update device tree for drc index %x\n", |
| 648 | lmb->drc_index); |
Nathan Fontenot | 4a4bdfe | 2016-02-10 11:10:44 -0600 | [diff] [blame] | 649 | dlpar_release_drc(lmb->drc_index); |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 650 | return rc; |
| 651 | } |
| 652 | |
Nathan Fontenot | fdb4f6e | 2016-06-29 12:20:30 -0500 | [diff] [blame] | 653 | block_sz = memory_block_size_bytes(); |
| 654 | |
| 655 | /* Find the node id for this address */ |
| 656 | nid = memory_add_physaddr_to_nid(lmb->base_addr); |
| 657 | |
| 658 | /* Add the memory */ |
| 659 | rc = add_memory(nid, lmb->base_addr, block_sz); |
John Allen | c21f515 | 2017-01-06 13:25:53 -0600 | [diff] [blame] | 660 | if (rc) |
Nathan Fontenot | bdf5fc6 | 2016-02-10 11:12:13 -0600 | [diff] [blame] | 661 | dlpar_remove_device_tree_lmb(lmb); |
John Allen | c21f515 | 2017-01-06 13:25:53 -0600 | [diff] [blame] | 662 | else |
Nathan Fontenot | fdb4f6e | 2016-06-29 12:20:30 -0500 | [diff] [blame] | 663 | lmb->flags |= DRCONF_MEM_ASSIGNED; |
Nathan Fontenot | 4a4bdfe | 2016-02-10 11:10:44 -0600 | [diff] [blame] | 664 | |
| 665 | return rc; |
| 666 | } |
| 667 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 668 | static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop) |
| 669 | { |
| 670 | struct of_drconf_cell *lmbs; |
| 671 | u32 num_lmbs, *p; |
| 672 | int lmbs_available = 0; |
| 673 | int lmbs_added = 0; |
| 674 | int i, rc; |
| 675 | |
| 676 | pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add); |
| 677 | |
| 678 | if (lmbs_to_add == 0) |
| 679 | return -EINVAL; |
| 680 | |
| 681 | p = prop->value; |
| 682 | num_lmbs = *p++; |
| 683 | lmbs = (struct of_drconf_cell *)p; |
| 684 | |
| 685 | /* Validate that there are enough LMBs to satisfy the request */ |
| 686 | for (i = 0; i < num_lmbs; i++) { |
| 687 | if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED)) |
| 688 | lmbs_available++; |
| 689 | } |
| 690 | |
| 691 | if (lmbs_available < lmbs_to_add) |
| 692 | return -EINVAL; |
| 693 | |
| 694 | for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) { |
John Allen | c21f515 | 2017-01-06 13:25:53 -0600 | [diff] [blame] | 695 | rc = dlpar_acquire_drc(lmbs[i].drc_index); |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 696 | if (rc) |
| 697 | continue; |
| 698 | |
John Allen | c21f515 | 2017-01-06 13:25:53 -0600 | [diff] [blame] | 699 | rc = dlpar_add_lmb(&lmbs[i]); |
| 700 | if (rc) { |
| 701 | dlpar_release_drc(lmbs[i].drc_index); |
| 702 | continue; |
| 703 | } |
| 704 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 705 | lmbs_added++; |
| 706 | |
| 707 | /* Mark this lmb so we can remove it later if all of the |
| 708 | * requested LMBs cannot be added. |
| 709 | */ |
| 710 | lmbs[i].reserved = 1; |
| 711 | } |
| 712 | |
| 713 | if (lmbs_added != lmbs_to_add) { |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 714 | pr_err("Memory hot-add failed, removing any added LMBs\n"); |
| 715 | |
| 716 | for (i = 0; i < num_lmbs; i++) { |
| 717 | if (!lmbs[i].reserved) |
| 718 | continue; |
| 719 | |
| 720 | rc = dlpar_remove_lmb(&lmbs[i]); |
| 721 | if (rc) |
| 722 | pr_err("Failed to remove LMB, drc index %x\n", |
| 723 | be32_to_cpu(lmbs[i].drc_index)); |
John Allen | c21f515 | 2017-01-06 13:25:53 -0600 | [diff] [blame] | 724 | else |
| 725 | dlpar_release_drc(lmbs[i].drc_index); |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 726 | } |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 727 | rc = -EINVAL; |
| 728 | } else { |
| 729 | for (i = 0; i < num_lmbs; i++) { |
| 730 | if (!lmbs[i].reserved) |
| 731 | continue; |
| 732 | |
| 733 | pr_info("Memory at %llx (drc index %x) was hot-added\n", |
| 734 | lmbs[i].base_addr, lmbs[i].drc_index); |
| 735 | lmbs[i].reserved = 0; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | return rc; |
| 740 | } |
| 741 | |
| 742 | static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop) |
| 743 | { |
| 744 | struct of_drconf_cell *lmbs; |
| 745 | u32 num_lmbs, *p; |
| 746 | int i, lmb_found; |
| 747 | int rc; |
| 748 | |
| 749 | pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index); |
| 750 | |
| 751 | p = prop->value; |
| 752 | num_lmbs = *p++; |
| 753 | lmbs = (struct of_drconf_cell *)p; |
| 754 | |
| 755 | lmb_found = 0; |
| 756 | for (i = 0; i < num_lmbs; i++) { |
| 757 | if (lmbs[i].drc_index == drc_index) { |
| 758 | lmb_found = 1; |
John Allen | c21f515 | 2017-01-06 13:25:53 -0600 | [diff] [blame] | 759 | rc = dlpar_acquire_drc(lmbs[i].drc_index); |
| 760 | if (!rc) { |
| 761 | rc = dlpar_add_lmb(&lmbs[i]); |
| 762 | if (rc) |
| 763 | dlpar_release_drc(lmbs[i].drc_index); |
| 764 | } |
| 765 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 766 | break; |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | if (!lmb_found) |
| 771 | rc = -EINVAL; |
| 772 | |
| 773 | if (rc) |
| 774 | pr_info("Failed to hot-add memory, drc index %x\n", drc_index); |
| 775 | else |
| 776 | pr_info("Memory at %llx (drc index %x) was hot-added\n", |
| 777 | lmbs[i].base_addr, drc_index); |
| 778 | |
| 779 | return rc; |
| 780 | } |
| 781 | |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 782 | static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index, |
| 783 | struct property *prop) |
| 784 | { |
| 785 | struct of_drconf_cell *lmbs; |
| 786 | u32 num_lmbs, *p; |
| 787 | int i, rc, start_lmb_found; |
| 788 | int lmbs_available = 0, start_index = 0, end_index; |
| 789 | |
| 790 | pr_info("Attempting to hot-add %u LMB(s) at index %x\n", |
| 791 | lmbs_to_add, drc_index); |
| 792 | |
| 793 | if (lmbs_to_add == 0) |
| 794 | return -EINVAL; |
| 795 | |
| 796 | p = prop->value; |
| 797 | num_lmbs = *p++; |
| 798 | lmbs = (struct of_drconf_cell *)p; |
| 799 | start_lmb_found = 0; |
| 800 | |
| 801 | /* Navigate to drc_index */ |
| 802 | while (start_index < num_lmbs) { |
| 803 | if (lmbs[start_index].drc_index == drc_index) { |
| 804 | start_lmb_found = 1; |
| 805 | break; |
| 806 | } |
| 807 | |
| 808 | start_index++; |
| 809 | } |
| 810 | |
| 811 | if (!start_lmb_found) |
| 812 | return -EINVAL; |
| 813 | |
| 814 | end_index = start_index + lmbs_to_add; |
| 815 | |
| 816 | /* Validate that the LMBs in this range are not reserved */ |
| 817 | for (i = start_index; i < end_index; i++) { |
| 818 | if (lmbs[i].flags & DRCONF_MEM_RESERVED) |
| 819 | break; |
| 820 | |
| 821 | lmbs_available++; |
| 822 | } |
| 823 | |
| 824 | if (lmbs_available < lmbs_to_add) |
| 825 | return -EINVAL; |
| 826 | |
| 827 | for (i = start_index; i < end_index; i++) { |
| 828 | if (lmbs[i].flags & DRCONF_MEM_ASSIGNED) |
| 829 | continue; |
| 830 | |
| 831 | rc = dlpar_acquire_drc(lmbs[i].drc_index); |
| 832 | if (rc) |
| 833 | break; |
| 834 | |
| 835 | rc = dlpar_add_lmb(&lmbs[i]); |
| 836 | if (rc) { |
| 837 | dlpar_release_drc(lmbs[i].drc_index); |
| 838 | break; |
| 839 | } |
| 840 | |
| 841 | lmbs[i].reserved = 1; |
| 842 | } |
| 843 | |
| 844 | if (rc) { |
| 845 | pr_err("Memory indexed-count-add failed, removing any added LMBs\n"); |
| 846 | |
| 847 | for (i = start_index; i < end_index; i++) { |
| 848 | if (!lmbs[i].reserved) |
| 849 | continue; |
| 850 | |
| 851 | rc = dlpar_remove_lmb(&lmbs[i]); |
| 852 | if (rc) |
| 853 | pr_err("Failed to remove LMB, drc index %x\n", |
| 854 | be32_to_cpu(lmbs[i].drc_index)); |
| 855 | else |
| 856 | dlpar_release_drc(lmbs[i].drc_index); |
| 857 | } |
| 858 | rc = -EINVAL; |
| 859 | } else { |
| 860 | for (i = start_index; i < end_index; i++) { |
| 861 | if (!lmbs[i].reserved) |
| 862 | continue; |
| 863 | |
| 864 | pr_info("Memory at %llx (drc index %x) was hot-added\n", |
| 865 | lmbs[i].base_addr, lmbs[i].drc_index); |
| 866 | lmbs[i].reserved = 0; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | return rc; |
| 871 | } |
| 872 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 873 | int dlpar_memory(struct pseries_hp_errorlog *hp_elog) |
| 874 | { |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 875 | struct device_node *dn; |
| 876 | struct property *prop; |
| 877 | u32 count, drc_index; |
| 878 | int rc; |
| 879 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 880 | lock_device_hotplug(); |
| 881 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 882 | dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
Nathan Fontenot | b0a478e | 2015-04-07 09:53:46 -0500 | [diff] [blame] | 883 | if (!dn) { |
| 884 | rc = -EINVAL; |
| 885 | goto dlpar_memory_out; |
| 886 | } |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 887 | |
| 888 | prop = dlpar_clone_drconf_property(dn); |
| 889 | if (!prop) { |
Nathan Fontenot | b0a478e | 2015-04-07 09:53:46 -0500 | [diff] [blame] | 890 | rc = -EINVAL; |
| 891 | goto dlpar_memory_out; |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 892 | } |
| 893 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 894 | switch (hp_elog->action) { |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 895 | case PSERIES_HP_ELOG_ACTION_ADD: |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 896 | if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) { |
| 897 | count = hp_elog->_drc_u.drc_count; |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 898 | rc = dlpar_memory_add_by_count(count, prop); |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 899 | } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) { |
| 900 | drc_index = hp_elog->_drc_u.drc_index; |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 901 | rc = dlpar_memory_add_by_index(drc_index, prop); |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 902 | } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_IC) { |
| 903 | count = hp_elog->_drc_u.ic.count; |
| 904 | drc_index = hp_elog->_drc_u.ic.index; |
| 905 | rc = dlpar_memory_add_by_ic(count, drc_index, prop); |
| 906 | } else { |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 907 | rc = -EINVAL; |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 908 | } |
| 909 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 910 | break; |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 911 | case PSERIES_HP_ELOG_ACTION_REMOVE: |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 912 | if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) { |
| 913 | count = hp_elog->_drc_u.drc_count; |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 914 | rc = dlpar_memory_remove_by_count(count, prop); |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 915 | } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) { |
| 916 | drc_index = hp_elog->_drc_u.drc_index; |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 917 | rc = dlpar_memory_remove_by_index(drc_index, prop); |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 918 | } else { |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 919 | rc = -EINVAL; |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 920 | } |
| 921 | |
Nathan Fontenot | 51925fb | 2015-02-10 13:49:22 -0600 | [diff] [blame] | 922 | break; |
John Allen | e70d597 | 2017-01-06 13:27:26 -0600 | [diff] [blame] | 923 | case PSERIES_HP_ELOG_ACTION_READD: |
Sahil Mehta | 333f7b7 | 2017-02-15 13:45:56 -0500 | [diff] [blame^] | 924 | drc_index = hp_elog->_drc_u.drc_index; |
John Allen | e70d597 | 2017-01-06 13:27:26 -0600 | [diff] [blame] | 925 | rc = dlpar_memory_readd_by_index(drc_index, prop); |
| 926 | break; |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 927 | default: |
| 928 | pr_err("Invalid action (%d) specified\n", hp_elog->action); |
| 929 | rc = -EINVAL; |
| 930 | break; |
| 931 | } |
| 932 | |
Nathan Fontenot | c2101c9 | 2016-06-20 09:00:39 -0500 | [diff] [blame] | 933 | dlpar_free_property(prop); |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 934 | |
Nathan Fontenot | b0a478e | 2015-04-07 09:53:46 -0500 | [diff] [blame] | 935 | dlpar_memory_out: |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 936 | of_node_put(dn); |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 937 | unlock_device_hotplug(); |
| 938 | return rc; |
| 939 | } |
| 940 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 941 | static int pseries_add_mem_node(struct device_node *np) |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 942 | { |
| 943 | const char *type; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 944 | const __be32 *regs; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 945 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 946 | unsigned int lmb_size; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 947 | int ret = -EINVAL; |
| 948 | |
| 949 | /* |
| 950 | * Check to see if we are actually adding memory |
| 951 | */ |
| 952 | type = of_get_property(np, "device_type", NULL); |
| 953 | if (type == NULL || strcmp(type, "memory") != 0) |
| 954 | return 0; |
| 955 | |
| 956 | /* |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 957 | * Find the base and size of the memblock |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 958 | */ |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 959 | regs = of_get_property(np, "reg", NULL); |
| 960 | if (!regs) |
| 961 | return ret; |
| 962 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 963 | base = be64_to_cpu(*(unsigned long *)regs); |
| 964 | lmb_size = be32_to_cpu(regs[3]); |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 965 | |
| 966 | /* |
| 967 | * Update memory region to represent the memory add |
| 968 | */ |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 969 | ret = memblock_add(base, lmb_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 970 | return (ret < 0) ? -EINVAL : 0; |
| 971 | } |
| 972 | |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 973 | static int pseries_update_drconf_memory(struct of_reconfig_data *pr) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 974 | { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 975 | struct of_drconf_cell *new_drmem, *old_drmem; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 976 | unsigned long memblock_size; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 977 | u32 entries; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 978 | __be32 *p; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 979 | int i, rc = -EINVAL; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 980 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame] | 981 | if (rtas_hp_event) |
| 982 | return 0; |
| 983 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 984 | memblock_size = pseries_memory_block_size(); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 985 | if (!memblock_size) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 986 | return -EINVAL; |
| 987 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 988 | p = (__be32 *) pr->old_prop->value; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 989 | if (!p) |
| 990 | return -EINVAL; |
| 991 | |
| 992 | /* The first int of the property is the number of lmb's described |
| 993 | * 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] | 994 | * entries. Get the number of entries and skip to the array of |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 995 | * of_drconf_cell's. |
| 996 | */ |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 997 | entries = be32_to_cpu(*p++); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 998 | old_drmem = (struct of_drconf_cell *)p; |
| 999 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 1000 | p = (__be32 *)pr->prop->value; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1001 | p++; |
| 1002 | new_drmem = (struct of_drconf_cell *)p; |
| 1003 | |
| 1004 | for (i = 0; i < entries; i++) { |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 1005 | if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) && |
| 1006 | (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) { |
| 1007 | rc = pseries_remove_memblock( |
| 1008 | be64_to_cpu(old_drmem[i].base_addr), |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1009 | memblock_size); |
| 1010 | break; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 1011 | } else if ((!(be32_to_cpu(old_drmem[i].flags) & |
| 1012 | DRCONF_MEM_ASSIGNED)) && |
| 1013 | (be32_to_cpu(new_drmem[i].flags) & |
| 1014 | DRCONF_MEM_ASSIGNED)) { |
| 1015 | rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr), |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1016 | memblock_size); |
| 1017 | rc = (rc < 0) ? -EINVAL : 0; |
| 1018 | break; |
| 1019 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 1020 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 1021 | return rc; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 1022 | } |
| 1023 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1024 | static int pseries_memory_notifier(struct notifier_block *nb, |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 1025 | unsigned long action, void *data) |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1026 | { |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 1027 | struct of_reconfig_data *rd = data; |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 1028 | int err = 0; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1029 | |
| 1030 | switch (action) { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1031 | case OF_RECONFIG_ATTACH_NODE: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 1032 | err = pseries_add_mem_node(rd->dn); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1033 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1034 | case OF_RECONFIG_DETACH_NODE: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 1035 | err = pseries_remove_mem_node(rd->dn); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1036 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1037 | case OF_RECONFIG_UPDATE_PROPERTY: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 1038 | if (!strcmp(rd->prop->name, "ibm,dynamic-memory")) |
| 1039 | err = pseries_update_drconf_memory(rd); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1040 | break; |
| 1041 | } |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 1042 | return notifier_from_errno(err); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static struct notifier_block pseries_mem_nb = { |
| 1046 | .notifier_call = pseries_memory_notifier, |
| 1047 | }; |
| 1048 | |
| 1049 | static int __init pseries_memory_hotplug_init(void) |
| 1050 | { |
| 1051 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 1052 | of_reconfig_notifier_register(&pseries_mem_nb); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1053 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 1054 | return 0; |
| 1055 | } |
| 1056 | machine_device_initcall(pseries, pseries_memory_hotplug_init); |