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 | } |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 191 | #else |
| 192 | static inline int pseries_remove_memblock(unsigned long base, |
| 193 | unsigned int memblock_size) |
| 194 | { |
| 195 | return -EOPNOTSUPP; |
| 196 | } |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 197 | static inline int pseries_remove_mem_node(struct device_node *np) |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 198 | { |
Gavin Shan | f1b3929 | 2014-08-11 19:16:19 +1000 | [diff] [blame] | 199 | return 0; |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 200 | } |
| 201 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 202 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame^] | 203 | static int dlpar_add_lmb(struct of_drconf_cell *lmb) |
| 204 | { |
| 205 | struct memory_block *mem_block; |
| 206 | unsigned long block_sz; |
| 207 | int nid, rc; |
| 208 | |
| 209 | if (lmb->flags & DRCONF_MEM_ASSIGNED) |
| 210 | return -EINVAL; |
| 211 | |
| 212 | block_sz = memory_block_size_bytes(); |
| 213 | |
| 214 | rc = dlpar_acquire_drc(lmb->drc_index); |
| 215 | if (rc) |
| 216 | return rc; |
| 217 | |
| 218 | /* Find the node id for this address */ |
| 219 | nid = memory_add_physaddr_to_nid(lmb->base_addr); |
| 220 | |
| 221 | /* Add the memory */ |
| 222 | rc = add_memory(nid, lmb->base_addr, block_sz); |
| 223 | if (rc) { |
| 224 | dlpar_release_drc(lmb->drc_index); |
| 225 | return rc; |
| 226 | } |
| 227 | |
| 228 | /* Register this block of memory */ |
| 229 | rc = memblock_add(lmb->base_addr, block_sz); |
| 230 | if (rc) { |
| 231 | remove_memory(nid, lmb->base_addr, block_sz); |
| 232 | dlpar_release_drc(lmb->drc_index); |
| 233 | return rc; |
| 234 | } |
| 235 | |
| 236 | mem_block = lmb_to_memblock(lmb); |
| 237 | if (!mem_block) { |
| 238 | remove_memory(nid, lmb->base_addr, block_sz); |
| 239 | dlpar_release_drc(lmb->drc_index); |
| 240 | return -EINVAL; |
| 241 | } |
| 242 | |
| 243 | rc = device_online(&mem_block->dev); |
| 244 | put_device(&mem_block->dev); |
| 245 | if (rc) { |
| 246 | remove_memory(nid, lmb->base_addr, block_sz); |
| 247 | dlpar_release_drc(lmb->drc_index); |
| 248 | return rc; |
| 249 | } |
| 250 | |
| 251 | lmb->flags |= DRCONF_MEM_ASSIGNED; |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop) |
| 256 | { |
| 257 | struct of_drconf_cell *lmbs; |
| 258 | u32 num_lmbs, *p; |
| 259 | int lmbs_available = 0; |
| 260 | int lmbs_added = 0; |
| 261 | int i, rc; |
| 262 | |
| 263 | pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add); |
| 264 | |
| 265 | if (lmbs_to_add == 0) |
| 266 | return -EINVAL; |
| 267 | |
| 268 | p = prop->value; |
| 269 | num_lmbs = *p++; |
| 270 | lmbs = (struct of_drconf_cell *)p; |
| 271 | |
| 272 | /* Validate that there are enough LMBs to satisfy the request */ |
| 273 | for (i = 0; i < num_lmbs; i++) { |
| 274 | if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED)) |
| 275 | lmbs_available++; |
| 276 | } |
| 277 | |
| 278 | if (lmbs_available < lmbs_to_add) |
| 279 | return -EINVAL; |
| 280 | |
| 281 | for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) { |
| 282 | rc = dlpar_add_lmb(&lmbs[i]); |
| 283 | if (rc) |
| 284 | continue; |
| 285 | |
| 286 | lmbs_added++; |
| 287 | |
| 288 | /* Mark this lmb so we can remove it later if all of the |
| 289 | * requested LMBs cannot be added. |
| 290 | */ |
| 291 | lmbs[i].reserved = 1; |
| 292 | } |
| 293 | |
| 294 | if (lmbs_added != lmbs_to_add) { |
| 295 | /* TODO: remove added lmbs */ |
| 296 | rc = -EINVAL; |
| 297 | } else { |
| 298 | for (i = 0; i < num_lmbs; i++) { |
| 299 | if (!lmbs[i].reserved) |
| 300 | continue; |
| 301 | |
| 302 | pr_info("Memory at %llx (drc index %x) was hot-added\n", |
| 303 | lmbs[i].base_addr, lmbs[i].drc_index); |
| 304 | lmbs[i].reserved = 0; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | return rc; |
| 309 | } |
| 310 | |
| 311 | static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop) |
| 312 | { |
| 313 | struct of_drconf_cell *lmbs; |
| 314 | u32 num_lmbs, *p; |
| 315 | int i, lmb_found; |
| 316 | int rc; |
| 317 | |
| 318 | pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index); |
| 319 | |
| 320 | p = prop->value; |
| 321 | num_lmbs = *p++; |
| 322 | lmbs = (struct of_drconf_cell *)p; |
| 323 | |
| 324 | lmb_found = 0; |
| 325 | for (i = 0; i < num_lmbs; i++) { |
| 326 | if (lmbs[i].drc_index == drc_index) { |
| 327 | lmb_found = 1; |
| 328 | rc = dlpar_add_lmb(&lmbs[i]); |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if (!lmb_found) |
| 334 | rc = -EINVAL; |
| 335 | |
| 336 | if (rc) |
| 337 | pr_info("Failed to hot-add memory, drc index %x\n", drc_index); |
| 338 | else |
| 339 | pr_info("Memory at %llx (drc index %x) was hot-added\n", |
| 340 | lmbs[i].base_addr, drc_index); |
| 341 | |
| 342 | return rc; |
| 343 | } |
| 344 | |
| 345 | static void dlpar_update_drconf_property(struct device_node *dn, |
| 346 | struct property *prop) |
| 347 | { |
| 348 | struct of_drconf_cell *lmbs; |
| 349 | u32 num_lmbs, *p; |
| 350 | int i; |
| 351 | |
| 352 | /* Convert the property back to BE */ |
| 353 | p = prop->value; |
| 354 | num_lmbs = *p; |
| 355 | *p = cpu_to_be32(*p); |
| 356 | p++; |
| 357 | |
| 358 | lmbs = (struct of_drconf_cell *)p; |
| 359 | for (i = 0; i < num_lmbs; i++) { |
| 360 | lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr); |
| 361 | lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index); |
| 362 | lmbs[i].flags = cpu_to_be32(lmbs[i].flags); |
| 363 | } |
| 364 | |
| 365 | rtas_hp_event = true; |
| 366 | of_update_property(dn, prop); |
| 367 | rtas_hp_event = false; |
| 368 | } |
| 369 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 370 | int dlpar_memory(struct pseries_hp_errorlog *hp_elog) |
| 371 | { |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame^] | 372 | struct device_node *dn; |
| 373 | struct property *prop; |
| 374 | u32 count, drc_index; |
| 375 | int rc; |
| 376 | |
| 377 | count = hp_elog->_drc_u.drc_count; |
| 378 | drc_index = hp_elog->_drc_u.drc_index; |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 379 | |
| 380 | lock_device_hotplug(); |
| 381 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame^] | 382 | dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 383 | if (!dn) |
| 384 | return -EINVAL; |
| 385 | |
| 386 | prop = dlpar_clone_drconf_property(dn); |
| 387 | if (!prop) { |
| 388 | of_node_put(dn); |
| 389 | return -EINVAL; |
| 390 | } |
| 391 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 392 | switch (hp_elog->action) { |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame^] | 393 | case PSERIES_HP_ELOG_ACTION_ADD: |
| 394 | if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) |
| 395 | rc = dlpar_memory_add_by_count(count, prop); |
| 396 | else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) |
| 397 | rc = dlpar_memory_add_by_index(drc_index, prop); |
| 398 | else |
| 399 | rc = -EINVAL; |
| 400 | break; |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 401 | default: |
| 402 | pr_err("Invalid action (%d) specified\n", hp_elog->action); |
| 403 | rc = -EINVAL; |
| 404 | break; |
| 405 | } |
| 406 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame^] | 407 | if (rc) |
| 408 | dlpar_free_drconf_property(prop); |
| 409 | else |
| 410 | dlpar_update_drconf_property(dn, prop); |
| 411 | |
| 412 | of_node_put(dn); |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame] | 413 | unlock_device_hotplug(); |
| 414 | return rc; |
| 415 | } |
| 416 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 417 | static int pseries_add_mem_node(struct device_node *np) |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 418 | { |
| 419 | const char *type; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 420 | const __be32 *regs; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 421 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 422 | unsigned int lmb_size; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 423 | int ret = -EINVAL; |
| 424 | |
| 425 | /* |
| 426 | * Check to see if we are actually adding memory |
| 427 | */ |
| 428 | type = of_get_property(np, "device_type", NULL); |
| 429 | if (type == NULL || strcmp(type, "memory") != 0) |
| 430 | return 0; |
| 431 | |
| 432 | /* |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 433 | * Find the base and size of the memblock |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 434 | */ |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 435 | regs = of_get_property(np, "reg", NULL); |
| 436 | if (!regs) |
| 437 | return ret; |
| 438 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 439 | base = be64_to_cpu(*(unsigned long *)regs); |
| 440 | lmb_size = be32_to_cpu(regs[3]); |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * Update memory region to represent the memory add |
| 444 | */ |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 445 | ret = memblock_add(base, lmb_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 446 | return (ret < 0) ? -EINVAL : 0; |
| 447 | } |
| 448 | |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 449 | static int pseries_update_drconf_memory(struct of_reconfig_data *pr) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 450 | { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 451 | struct of_drconf_cell *new_drmem, *old_drmem; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 452 | unsigned long memblock_size; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 453 | u32 entries; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 454 | __be32 *p; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 455 | int i, rc = -EINVAL; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 456 | |
Nathan Fontenot | 5f97b2a | 2015-02-10 13:48:25 -0600 | [diff] [blame^] | 457 | if (rtas_hp_event) |
| 458 | return 0; |
| 459 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 460 | memblock_size = pseries_memory_block_size(); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 461 | if (!memblock_size) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 462 | return -EINVAL; |
| 463 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 464 | p = (__be32 *) pr->old_prop->value; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 465 | if (!p) |
| 466 | return -EINVAL; |
| 467 | |
| 468 | /* The first int of the property is the number of lmb's described |
| 469 | * 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] | 470 | * entries. Get the number of entries and skip to the array of |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 471 | * of_drconf_cell's. |
| 472 | */ |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 473 | entries = be32_to_cpu(*p++); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 474 | old_drmem = (struct of_drconf_cell *)p; |
| 475 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 476 | p = (__be32 *)pr->prop->value; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 477 | p++; |
| 478 | new_drmem = (struct of_drconf_cell *)p; |
| 479 | |
| 480 | for (i = 0; i < entries; i++) { |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 481 | if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) && |
| 482 | (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) { |
| 483 | rc = pseries_remove_memblock( |
| 484 | be64_to_cpu(old_drmem[i].base_addr), |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 485 | memblock_size); |
| 486 | break; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 487 | } else if ((!(be32_to_cpu(old_drmem[i].flags) & |
| 488 | DRCONF_MEM_ASSIGNED)) && |
| 489 | (be32_to_cpu(new_drmem[i].flags) & |
| 490 | DRCONF_MEM_ASSIGNED)) { |
| 491 | rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr), |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 492 | memblock_size); |
| 493 | rc = (rc < 0) ? -EINVAL : 0; |
| 494 | break; |
| 495 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 496 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 497 | return rc; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 498 | } |
| 499 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 500 | static int pseries_memory_notifier(struct notifier_block *nb, |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 501 | unsigned long action, void *data) |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 502 | { |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 503 | struct of_reconfig_data *rd = data; |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 504 | int err = 0; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 505 | |
| 506 | switch (action) { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 507 | case OF_RECONFIG_ATTACH_NODE: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 508 | err = pseries_add_mem_node(rd->dn); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 509 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 510 | case OF_RECONFIG_DETACH_NODE: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 511 | err = pseries_remove_mem_node(rd->dn); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 512 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 513 | case OF_RECONFIG_UPDATE_PROPERTY: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 514 | if (!strcmp(rd->prop->name, "ibm,dynamic-memory")) |
| 515 | err = pseries_update_drconf_memory(rd); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 516 | break; |
| 517 | } |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 518 | return notifier_from_errno(err); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static struct notifier_block pseries_mem_nb = { |
| 522 | .notifier_call = pseries_memory_notifier, |
| 523 | }; |
| 524 | |
| 525 | static int __init pseries_memory_hotplug_init(void) |
| 526 | { |
| 527 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 528 | of_reconfig_notifier_register(&pseries_mem_nb); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 529 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 530 | return 0; |
| 531 | } |
| 532 | machine_device_initcall(pseries, pseries_memory_hotplug_init); |