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