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> |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 19 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 20 | #include <asm/firmware.h> |
| 21 | #include <asm/machdep.h> |
Rob Herring | 26a2056 | 2013-09-26 07:40:04 -0500 | [diff] [blame] | 22 | #include <asm/prom.h> |
Michael Neuling | 0b2f828 | 2009-02-08 14:49:39 +0000 | [diff] [blame] | 23 | #include <asm/sparsemem.h> |
Anton Blanchard | 1217d34 | 2014-08-20 08:55:19 +1000 | [diff] [blame] | 24 | #include "pseries.h" |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 25 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 26 | unsigned long pseries_memory_block_size(void) |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 27 | { |
| 28 | struct device_node *np; |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 29 | unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE; |
| 30 | struct resource r; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 31 | |
| 32 | np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory"); |
| 33 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 34 | const __be64 *size; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 35 | |
| 36 | size = of_get_property(np, "ibm,lmb-size", NULL); |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 37 | if (size) |
| 38 | memblock_size = be64_to_cpup(size); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 39 | of_node_put(np); |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 40 | } else if (machine_is(pseries)) { |
| 41 | /* This fallback really only applies to pseries */ |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 42 | unsigned int memzero_size = 0; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 43 | |
| 44 | np = of_find_node_by_path("/memory@0"); |
| 45 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 46 | if (!of_address_to_resource(np, 0, &r)) |
| 47 | memzero_size = resource_size(&r); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 48 | of_node_put(np); |
| 49 | } |
| 50 | |
| 51 | if (memzero_size) { |
| 52 | /* We now know the size of memory@0, use this to find |
| 53 | * the first memoryblock and get its size. |
| 54 | */ |
| 55 | char buf[64]; |
| 56 | |
| 57 | sprintf(buf, "/memory@%x", memzero_size); |
| 58 | np = of_find_node_by_path(buf); |
| 59 | if (np) { |
Benjamin Herrenschmidt | 770e1ac | 2011-06-14 10:57:51 +1000 | [diff] [blame] | 60 | if (!of_address_to_resource(np, 0, &r)) |
| 61 | memblock_size = resource_size(&r); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 62 | of_node_put(np); |
| 63 | } |
| 64 | } |
| 65 | } |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 66 | return memblock_size; |
| 67 | } |
| 68 | |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 69 | #ifdef CONFIG_MEMORY_HOTREMOVE |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 70 | static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size) |
| 71 | { |
| 72 | unsigned long block_sz, start_pfn; |
| 73 | int sections_per_block; |
| 74 | int i, nid; |
| 75 | |
| 76 | start_pfn = base >> PAGE_SHIFT; |
| 77 | |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 78 | lock_device_hotplug(); |
| 79 | |
| 80 | if (!pfn_valid(start_pfn)) |
| 81 | goto out; |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 82 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 83 | block_sz = pseries_memory_block_size(); |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 84 | sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE; |
| 85 | nid = memory_add_physaddr_to_nid(base); |
| 86 | |
| 87 | for (i = 0; i < sections_per_block; i++) { |
| 88 | remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE); |
| 89 | base += MIN_MEMORY_BLOCK_SIZE; |
| 90 | } |
| 91 | |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 92 | out: |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 93 | /* Update memory regions for memory remove */ |
| 94 | memblock_remove(base, memblock_size); |
Li Zhong | 42dbfc8 | 2014-04-10 16:25:31 +0800 | [diff] [blame] | 95 | unlock_device_hotplug(); |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static int pseries_remove_mem_node(struct device_node *np) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 100 | { |
| 101 | const char *type; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 102 | const __be32 *regs; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 103 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 104 | unsigned int lmb_size; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 105 | int ret = -EINVAL; |
| 106 | |
| 107 | /* |
| 108 | * Check to see if we are actually removing memory |
| 109 | */ |
| 110 | type = of_get_property(np, "device_type", NULL); |
| 111 | if (type == NULL || strcmp(type, "memory") != 0) |
| 112 | return 0; |
| 113 | |
| 114 | /* |
Li Zhong | 4646d13 | 2014-08-07 13:11:58 +0800 | [diff] [blame] | 115 | * Find the base address and size of the memblock |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 116 | */ |
| 117 | regs = of_get_property(np, "reg", NULL); |
| 118 | if (!regs) |
| 119 | return ret; |
| 120 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 121 | base = be64_to_cpu(*(unsigned long *)regs); |
| 122 | lmb_size = be32_to_cpu(regs[3]); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 123 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 124 | pseries_remove_memblock(base, lmb_size); |
| 125 | return 0; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 126 | } |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 127 | #else |
| 128 | static inline int pseries_remove_memblock(unsigned long base, |
| 129 | unsigned int memblock_size) |
| 130 | { |
| 131 | return -EOPNOTSUPP; |
| 132 | } |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 133 | static inline int pseries_remove_mem_node(struct device_node *np) |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 134 | { |
Gavin Shan | f1b3929 | 2014-08-11 19:16:19 +1000 | [diff] [blame] | 135 | return 0; |
David Rientjes | 4edd7ce | 2013-04-29 15:08:22 -0700 | [diff] [blame] | 136 | } |
| 137 | #endif /* CONFIG_MEMORY_HOTREMOVE */ |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 138 | |
Nathan Fontenot | 999e2da | 2015-02-10 13:47:02 -0600 | [diff] [blame^] | 139 | int dlpar_memory(struct pseries_hp_errorlog *hp_elog) |
| 140 | { |
| 141 | int rc = 0; |
| 142 | |
| 143 | lock_device_hotplug(); |
| 144 | |
| 145 | switch (hp_elog->action) { |
| 146 | default: |
| 147 | pr_err("Invalid action (%d) specified\n", hp_elog->action); |
| 148 | rc = -EINVAL; |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | unlock_device_hotplug(); |
| 153 | return rc; |
| 154 | } |
| 155 | |
Nathan Fontenot | 9ac8cde | 2014-01-27 10:54:06 -0600 | [diff] [blame] | 156 | static int pseries_add_mem_node(struct device_node *np) |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 157 | { |
| 158 | const char *type; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 159 | const __be32 *regs; |
Nathan Fontenot | 92ecd17 | 2008-07-03 13:20:58 +1000 | [diff] [blame] | 160 | unsigned long base; |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 161 | unsigned int lmb_size; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 162 | int ret = -EINVAL; |
| 163 | |
| 164 | /* |
| 165 | * Check to see if we are actually adding memory |
| 166 | */ |
| 167 | type = of_get_property(np, "device_type", NULL); |
| 168 | if (type == NULL || strcmp(type, "memory") != 0) |
| 169 | return 0; |
| 170 | |
| 171 | /* |
Yinghai Lu | 95f72d1 | 2010-07-12 14:36:09 +1000 | [diff] [blame] | 172 | * Find the base and size of the memblock |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 173 | */ |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 174 | regs = of_get_property(np, "reg", NULL); |
| 175 | if (!regs) |
| 176 | return ret; |
| 177 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 178 | base = be64_to_cpu(*(unsigned long *)regs); |
| 179 | lmb_size = be32_to_cpu(regs[3]); |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 180 | |
| 181 | /* |
| 182 | * Update memory region to represent the memory add |
| 183 | */ |
Benjamin Herrenschmidt | 3fdfd99 | 2010-07-23 10:35:52 +1000 | [diff] [blame] | 184 | ret = memblock_add(base, lmb_size); |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 185 | return (ret < 0) ? -EINVAL : 0; |
| 186 | } |
| 187 | |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 188 | static int pseries_update_drconf_memory(struct of_reconfig_data *pr) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 189 | { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 190 | struct of_drconf_cell *new_drmem, *old_drmem; |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 191 | unsigned long memblock_size; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 192 | u32 entries; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 193 | __be32 *p; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 194 | int i, rc = -EINVAL; |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 195 | |
Anton Blanchard | a5d8625 | 2014-06-04 17:50:47 +1000 | [diff] [blame] | 196 | memblock_size = pseries_memory_block_size(); |
Nathan Fontenot | c540ada | 2011-01-20 10:45:20 -0600 | [diff] [blame] | 197 | if (!memblock_size) |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 198 | return -EINVAL; |
| 199 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 200 | p = (__be32 *) pr->old_prop->value; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 201 | if (!p) |
| 202 | return -EINVAL; |
| 203 | |
| 204 | /* The first int of the property is the number of lmb's described |
| 205 | * 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] | 206 | * entries. Get the number of entries and skip to the array of |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 207 | * of_drconf_cell's. |
| 208 | */ |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 209 | entries = be32_to_cpu(*p++); |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 210 | old_drmem = (struct of_drconf_cell *)p; |
| 211 | |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 212 | p = (__be32 *)pr->prop->value; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 213 | p++; |
| 214 | new_drmem = (struct of_drconf_cell *)p; |
| 215 | |
| 216 | for (i = 0; i < entries; i++) { |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 217 | if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) && |
| 218 | (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) { |
| 219 | rc = pseries_remove_memblock( |
| 220 | be64_to_cpu(old_drmem[i].base_addr), |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 221 | memblock_size); |
| 222 | break; |
Thomas Falcon | c9ac408 | 2014-08-19 15:44:57 -0500 | [diff] [blame] | 223 | } else if ((!(be32_to_cpu(old_drmem[i].flags) & |
| 224 | DRCONF_MEM_ASSIGNED)) && |
| 225 | (be32_to_cpu(new_drmem[i].flags) & |
| 226 | DRCONF_MEM_ASSIGNED)) { |
| 227 | rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr), |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 228 | memblock_size); |
| 229 | rc = (rc < 0) ? -EINVAL : 0; |
| 230 | break; |
| 231 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 232 | } |
Nathan Fontenot | 3c3f67e | 2008-07-03 13:22:39 +1000 | [diff] [blame] | 233 | return rc; |
Badari Pulavarty | 98d5c21 | 2008-04-18 13:33:52 -0700 | [diff] [blame] | 234 | } |
| 235 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 236 | static int pseries_memory_notifier(struct notifier_block *nb, |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 237 | unsigned long action, void *data) |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 238 | { |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 239 | struct of_reconfig_data *rd = data; |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 240 | int err = 0; |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 241 | |
| 242 | switch (action) { |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 243 | case OF_RECONFIG_ATTACH_NODE: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 244 | err = pseries_add_mem_node(rd->dn); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 245 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 246 | case OF_RECONFIG_DETACH_NODE: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 247 | err = pseries_remove_mem_node(rd->dn); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 248 | break; |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 249 | case OF_RECONFIG_UPDATE_PROPERTY: |
Grant Likely | f5242e5 | 2014-11-24 17:58:01 +0000 | [diff] [blame] | 250 | if (!strcmp(rd->prop->name, "ibm,dynamic-memory")) |
| 251 | err = pseries_update_drconf_memory(rd); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 252 | break; |
| 253 | } |
Akinobu Mita | de2780a | 2011-06-21 03:35:56 +0000 | [diff] [blame] | 254 | return notifier_from_errno(err); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | static struct notifier_block pseries_mem_nb = { |
| 258 | .notifier_call = pseries_memory_notifier, |
| 259 | }; |
| 260 | |
| 261 | static int __init pseries_memory_hotplug_init(void) |
| 262 | { |
| 263 | if (firmware_has_feature(FW_FEATURE_LPAR)) |
Nathan Fontenot | 1cf3d8b | 2012-10-02 16:57:57 +0000 | [diff] [blame] | 264 | of_reconfig_notifier_register(&pseries_mem_nb); |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 265 | |
Badari Pulavarty | 57b5392 | 2008-04-18 13:33:50 -0700 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | machine_device_initcall(pseries, pseries_memory_hotplug_init); |