blob: 187ecfab8362a76ba271e98d933d03c4b931b861 [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>
Anton Blanchard1217d342014-08-20 08:55:19 +100023#include "pseries.h"
Badari Pulavarty57b53922008-04-18 13:33:50 -070024
Anton Blancharda5d86252014-06-04 17:50:47 +100025unsigned long pseries_memory_block_size(void)
Nathan Fontenotc540ada2011-01-20 10:45:20 -060026{
27 struct device_node *np;
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100028 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
29 struct resource r;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060030
31 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
32 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100033 const __be64 *size;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060034
35 size = of_get_property(np, "ibm,lmb-size", NULL);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100036 if (size)
37 memblock_size = be64_to_cpup(size);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060038 of_node_put(np);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100039 } else if (machine_is(pseries)) {
40 /* This fallback really only applies to pseries */
Nathan Fontenotc540ada2011-01-20 10:45:20 -060041 unsigned int memzero_size = 0;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060042
43 np = of_find_node_by_path("/memory@0");
44 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100045 if (!of_address_to_resource(np, 0, &r))
46 memzero_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060047 of_node_put(np);
48 }
49
50 if (memzero_size) {
51 /* We now know the size of memory@0, use this to find
52 * the first memoryblock and get its size.
53 */
54 char buf[64];
55
56 sprintf(buf, "/memory@%x", memzero_size);
57 np = of_find_node_by_path(buf);
58 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100059 if (!of_address_to_resource(np, 0, &r))
60 memblock_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060061 of_node_put(np);
62 }
63 }
64 }
Nathan Fontenotc540ada2011-01-20 10:45:20 -060065 return memblock_size;
66}
67
David Rientjes4edd7ce2013-04-29 15:08:22 -070068#ifdef CONFIG_MEMORY_HOTREMOVE
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060069static int pseries_remove_memory(u64 start, u64 size)
Badari Pulavarty57b53922008-04-18 13:33:50 -070070{
Yasuaki Ishimatsu1633dbb2012-10-10 15:53:53 -070071 int ret;
Nathan Fontenot92ecd172008-07-03 13:20:58 +100072
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060073 /* Remove htab bolted mappings for this section of memory */
74 start = (unsigned long)__va(start);
75 ret = remove_section_mapping(start, start + size);
Benjamin Herrenschmidtb4a26be2010-04-06 15:03:40 +000076
77 /* Ensure all vmalloc mappings are flushed in case they also
78 * hit that section of memory
79 */
80 vm_unmap_aliases();
81
Badari Pulavarty57b53922008-04-18 13:33:50 -070082 return ret;
83}
84
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060085static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
86{
87 unsigned long block_sz, start_pfn;
88 int sections_per_block;
89 int i, nid;
90
91 start_pfn = base >> PAGE_SHIFT;
92
Li Zhong42dbfc82014-04-10 16:25:31 +080093 lock_device_hotplug();
94
95 if (!pfn_valid(start_pfn))
96 goto out;
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060097
Anton Blancharda5d86252014-06-04 17:50:47 +100098 block_sz = pseries_memory_block_size();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060099 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
100 nid = memory_add_physaddr_to_nid(base);
101
102 for (i = 0; i < sections_per_block; i++) {
103 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
104 base += MIN_MEMORY_BLOCK_SIZE;
105 }
106
Li Zhong42dbfc82014-04-10 16:25:31 +0800107out:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600108 /* Update memory regions for memory remove */
109 memblock_remove(base, memblock_size);
Li Zhong42dbfc82014-04-10 16:25:31 +0800110 unlock_device_hotplug();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600111 return 0;
112}
113
114static int pseries_remove_mem_node(struct device_node *np)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000115{
116 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500117 const __be32 *regs;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000118 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000119 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000120 int ret = -EINVAL;
121
122 /*
123 * Check to see if we are actually removing memory
124 */
125 type = of_get_property(np, "device_type", NULL);
126 if (type == NULL || strcmp(type, "memory") != 0)
127 return 0;
128
129 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000130 * Find the bae address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000131 */
132 regs = of_get_property(np, "reg", NULL);
133 if (!regs)
134 return ret;
135
Thomas Falconc9ac4082014-08-19 15:44:57 -0500136 base = be64_to_cpu(*(unsigned long *)regs);
137 lmb_size = be32_to_cpu(regs[3]);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000138
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600139 pseries_remove_memblock(base, lmb_size);
140 return 0;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000141}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700142#else
143static inline int pseries_remove_memblock(unsigned long base,
144 unsigned int memblock_size)
145{
146 return -EOPNOTSUPP;
147}
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600148static inline int pseries_remove_mem_node(struct device_node *np)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700149{
Gavin Shanf1b39292014-08-11 19:16:19 +1000150 return 0;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700151}
152#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000153
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600154static int pseries_add_mem_node(struct device_node *np)
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700155{
156 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500157 const __be32 *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000158 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000159 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700160 int ret = -EINVAL;
161
162 /*
163 * Check to see if we are actually adding memory
164 */
165 type = of_get_property(np, "device_type", NULL);
166 if (type == NULL || strcmp(type, "memory") != 0)
167 return 0;
168
169 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000170 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700171 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700172 regs = of_get_property(np, "reg", NULL);
173 if (!regs)
174 return ret;
175
Thomas Falconc9ac4082014-08-19 15:44:57 -0500176 base = be64_to_cpu(*(unsigned long *)regs);
177 lmb_size = be32_to_cpu(regs[3]);
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700178
179 /*
180 * Update memory region to represent the memory add
181 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000182 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000183 return (ret < 0) ? -EINVAL : 0;
184}
185
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000186static int pseries_update_drconf_memory(struct of_prop_reconfig *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000187{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000188 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600189 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000190 u32 entries;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500191 __be32 *p;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000192 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000193
Anton Blancharda5d86252014-06-04 17:50:47 +1000194 memblock_size = pseries_memory_block_size();
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600195 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000196 return -EINVAL;
197
Thomas Falconc9ac4082014-08-19 15:44:57 -0500198 p = (__be32 *) pr->old_prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000199 if (!p)
200 return -EINVAL;
201
202 /* The first int of the property is the number of lmb's described
203 * by the property. This is followed by an array of of_drconf_cell
204 * entries. Get the niumber of entries and skip to the array of
205 * of_drconf_cell's.
206 */
Thomas Falconc9ac4082014-08-19 15:44:57 -0500207 entries = be32_to_cpu(*p++);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000208 old_drmem = (struct of_drconf_cell *)p;
209
Thomas Falconc9ac4082014-08-19 15:44:57 -0500210 p = (__be32 *)pr->prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000211 p++;
212 new_drmem = (struct of_drconf_cell *)p;
213
214 for (i = 0; i < entries; i++) {
Thomas Falconc9ac4082014-08-19 15:44:57 -0500215 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
216 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
217 rc = pseries_remove_memblock(
218 be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000219 memblock_size);
220 break;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500221 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
222 DRCONF_MEM_ASSIGNED)) &&
223 (be32_to_cpu(new_drmem[i].flags) &
224 DRCONF_MEM_ASSIGNED)) {
225 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000226 memblock_size);
227 rc = (rc < 0) ? -EINVAL : 0;
228 break;
229 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000230 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000231 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700232}
233
Badari Pulavarty57b53922008-04-18 13:33:50 -0700234static int pseries_memory_notifier(struct notifier_block *nb,
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000235 unsigned long action, void *node)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700236{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000237 struct of_prop_reconfig *pr;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000238 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700239
240 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000241 case OF_RECONFIG_ATTACH_NODE:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600242 err = pseries_add_mem_node(node);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700243 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000244 case OF_RECONFIG_DETACH_NODE:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600245 err = pseries_remove_mem_node(node);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700246 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000247 case OF_RECONFIG_UPDATE_PROPERTY:
248 pr = (struct of_prop_reconfig *)node;
249 if (!strcmp(pr->prop->name, "ibm,dynamic-memory"))
250 err = pseries_update_drconf_memory(pr);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700251 break;
252 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000253 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700254}
255
256static struct notifier_block pseries_mem_nb = {
257 .notifier_call = pseries_memory_notifier,
258};
259
260static int __init pseries_memory_hotplug_init(void)
261{
262 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000263 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700264
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600265#ifdef CONFIG_MEMORY_HOTREMOVE
266 ppc_md.remove_memory = pseries_remove_memory;
267#endif
268
Badari Pulavarty57b53922008-04-18 13:33:50 -0700269 return 0;
270}
271machine_device_initcall(pseries, pseries_memory_hotplug_init);