blob: 7f75c94af822c40322d8a4a751e983ad0e2bcdae [file] [log] [blame]
Badari Pulavarty57b53922008-04-18 13:33:50 -07001/*
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
12#include <linux/of.h>
Rob Herring26a20562013-09-26 07:40:04 -050013#include <linux/of_address.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100014#include <linux/memblock.h>
Benjamin Herrenschmidtb4a26be2010-04-06 15:03:40 +000015#include <linux/vmalloc.h>
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100016#include <linux/memory.h>
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060017#include <linux/memory_hotplug.h>
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100018
Badari Pulavarty57b53922008-04-18 13:33:50 -070019#include <asm/firmware.h>
20#include <asm/machdep.h>
Rob Herring26a20562013-09-26 07:40:04 -050021#include <asm/prom.h>
Michael Neuling0b2f8282009-02-08 14:49:39 +000022#include <asm/sparsemem.h>
Badari Pulavarty57b53922008-04-18 13:33:50 -070023
Nathan Fontenotc540ada2011-01-20 10:45:20 -060024static unsigned long get_memblock_size(void)
25{
26 struct device_node *np;
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100027 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
28 struct resource r;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060029
30 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
31 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100032 const __be64 *size;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060033
34 size = of_get_property(np, "ibm,lmb-size", NULL);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100035 if (size)
36 memblock_size = be64_to_cpup(size);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060037 of_node_put(np);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100038 } else if (machine_is(pseries)) {
39 /* This fallback really only applies to pseries */
Nathan Fontenotc540ada2011-01-20 10:45:20 -060040 unsigned int memzero_size = 0;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060041
42 np = of_find_node_by_path("/memory@0");
43 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100044 if (!of_address_to_resource(np, 0, &r))
45 memzero_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060046 of_node_put(np);
47 }
48
49 if (memzero_size) {
50 /* We now know the size of memory@0, use this to find
51 * the first memoryblock and get its size.
52 */
53 char buf[64];
54
55 sprintf(buf, "/memory@%x", memzero_size);
56 np = of_find_node_by_path(buf);
57 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100058 if (!of_address_to_resource(np, 0, &r))
59 memblock_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060060 of_node_put(np);
61 }
62 }
63 }
Nathan Fontenotc540ada2011-01-20 10:45:20 -060064 return memblock_size;
65}
66
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100067/* WARNING: This is going to override the generic definition whenever
68 * pseries is built-in regardless of what platform is active at boot
69 * time. This is fine for now as this is the only "option" and it
70 * should work everywhere. If not, we'll have to turn this into a
71 * ppc_md. callback
72 */
Nathan Fontenotc540ada2011-01-20 10:45:20 -060073unsigned long memory_block_size_bytes(void)
74{
75 return get_memblock_size();
76}
77
David Rientjes4edd7ce2013-04-29 15:08:22 -070078#ifdef CONFIG_MEMORY_HOTREMOVE
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060079static int pseries_remove_memory(u64 start, u64 size)
Badari Pulavarty57b53922008-04-18 13:33:50 -070080{
Yasuaki Ishimatsu1633dbb2012-10-10 15:53:53 -070081 int ret;
Nathan Fontenot92ecd172008-07-03 13:20:58 +100082
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060083 /* Remove htab bolted mappings for this section of memory */
84 start = (unsigned long)__va(start);
85 ret = remove_section_mapping(start, start + size);
Benjamin Herrenschmidtb4a26be2010-04-06 15:03:40 +000086
87 /* Ensure all vmalloc mappings are flushed in case they also
88 * hit that section of memory
89 */
90 vm_unmap_aliases();
91
Badari Pulavarty57b53922008-04-18 13:33:50 -070092 return ret;
93}
94
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060095static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
96{
97 unsigned long block_sz, start_pfn;
98 int sections_per_block;
99 int i, nid;
100
101 start_pfn = base >> PAGE_SHIFT;
102
Li Zhong42dbfc82014-04-10 16:25:31 +0800103 lock_device_hotplug();
104
105 if (!pfn_valid(start_pfn))
106 goto out;
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600107
108 block_sz = memory_block_size_bytes();
109 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
110 nid = memory_add_physaddr_to_nid(base);
111
112 for (i = 0; i < sections_per_block; i++) {
113 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
114 base += MIN_MEMORY_BLOCK_SIZE;
115 }
116
Li Zhong42dbfc82014-04-10 16:25:31 +0800117out:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600118 /* Update memory regions for memory remove */
119 memblock_remove(base, memblock_size);
Li Zhong42dbfc82014-04-10 16:25:31 +0800120 unlock_device_hotplug();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600121 return 0;
122}
123
124static int pseries_remove_mem_node(struct device_node *np)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000125{
126 const char *type;
127 const unsigned int *regs;
128 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000129 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000130 int ret = -EINVAL;
131
132 /*
133 * Check to see if we are actually removing memory
134 */
135 type = of_get_property(np, "device_type", NULL);
136 if (type == NULL || strcmp(type, "memory") != 0)
137 return 0;
138
139 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000140 * Find the bae address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000141 */
142 regs = of_get_property(np, "reg", NULL);
143 if (!regs)
144 return ret;
145
146 base = *(unsigned long *)regs;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000147 lmb_size = regs[3];
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000148
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600149 pseries_remove_memblock(base, lmb_size);
150 return 0;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000151}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700152#else
153static inline int pseries_remove_memblock(unsigned long base,
154 unsigned int memblock_size)
155{
156 return -EOPNOTSUPP;
157}
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600158static inline int pseries_remove_mem_node(struct device_node *np)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700159{
160 return -EOPNOTSUPP;
161}
162#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000163
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600164static int pseries_add_mem_node(struct device_node *np)
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700165{
166 const char *type;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700167 const unsigned int *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000168 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000169 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700170 int ret = -EINVAL;
171
172 /*
173 * Check to see if we are actually adding memory
174 */
175 type = of_get_property(np, "device_type", NULL);
176 if (type == NULL || strcmp(type, "memory") != 0)
177 return 0;
178
179 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000180 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700181 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700182 regs = of_get_property(np, "reg", NULL);
183 if (!regs)
184 return ret;
185
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000186 base = *(unsigned long *)regs;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000187 lmb_size = regs[3];
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700188
189 /*
190 * Update memory region to represent the memory add
191 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000192 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000193 return (ret < 0) ? -EINVAL : 0;
194}
195
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000196static int pseries_update_drconf_memory(struct of_prop_reconfig *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000197{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000198 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600199 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000200 u32 entries;
201 u32 *p;
202 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000203
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600204 memblock_size = get_memblock_size();
205 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000206 return -EINVAL;
207
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000208 p = (u32 *)of_get_property(pr->dn, "ibm,dynamic-memory", NULL);
209 if (!p)
210 return -EINVAL;
211
212 /* The first int of the property is the number of lmb's described
213 * by the property. This is followed by an array of of_drconf_cell
214 * entries. Get the niumber of entries and skip to the array of
215 * of_drconf_cell's.
216 */
217 entries = *p++;
218 old_drmem = (struct of_drconf_cell *)p;
219
220 p = (u32 *)pr->prop->value;
221 p++;
222 new_drmem = (struct of_drconf_cell *)p;
223
224 for (i = 0; i < entries; i++) {
225 if ((old_drmem[i].flags & DRCONF_MEM_ASSIGNED) &&
226 (!(new_drmem[i].flags & DRCONF_MEM_ASSIGNED))) {
227 rc = pseries_remove_memblock(old_drmem[i].base_addr,
228 memblock_size);
229 break;
230 } else if ((!(old_drmem[i].flags & DRCONF_MEM_ASSIGNED)) &&
231 (new_drmem[i].flags & DRCONF_MEM_ASSIGNED)) {
232 rc = memblock_add(old_drmem[i].base_addr,
233 memblock_size);
234 rc = (rc < 0) ? -EINVAL : 0;
235 break;
236 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000237 }
238
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000239 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700240}
241
Badari Pulavarty57b53922008-04-18 13:33:50 -0700242static int pseries_memory_notifier(struct notifier_block *nb,
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000243 unsigned long action, void *node)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700244{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000245 struct of_prop_reconfig *pr;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000246 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700247
248 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000249 case OF_RECONFIG_ATTACH_NODE:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600250 err = pseries_add_mem_node(node);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700251 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000252 case OF_RECONFIG_DETACH_NODE:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600253 err = pseries_remove_mem_node(node);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700254 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000255 case OF_RECONFIG_UPDATE_PROPERTY:
256 pr = (struct of_prop_reconfig *)node;
257 if (!strcmp(pr->prop->name, "ibm,dynamic-memory"))
258 err = pseries_update_drconf_memory(pr);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700259 break;
260 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000261 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700262}
263
264static struct notifier_block pseries_mem_nb = {
265 .notifier_call = pseries_memory_notifier,
266};
267
268static int __init pseries_memory_hotplug_init(void)
269{
270 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000271 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700272
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600273#ifdef CONFIG_MEMORY_HOTREMOVE
274 ppc_md.remove_memory = pseries_remove_memory;
275#endif
276
Badari Pulavarty57b53922008-04-18 13:33:50 -0700277 return 0;
278}
279machine_device_initcall(pseries, pseries_memory_hotplug_init);