blob: 9590dbb756f2e1cafc6eb02f216b075f5db6087f [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>
17
Badari Pulavarty57b53922008-04-18 13:33:50 -070018#include <asm/firmware.h>
19#include <asm/machdep.h>
Rob Herring26a20562013-09-26 07:40:04 -050020#include <asm/prom.h>
Michael Neuling0b2f8282009-02-08 14:49:39 +000021#include <asm/sparsemem.h>
Badari Pulavarty57b53922008-04-18 13:33:50 -070022
Nathan Fontenotc540ada2011-01-20 10:45:20 -060023static unsigned long get_memblock_size(void)
24{
25 struct device_node *np;
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100026 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
27 struct resource r;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060028
29 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
30 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100031 const __be64 *size;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060032
33 size = of_get_property(np, "ibm,lmb-size", NULL);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100034 if (size)
35 memblock_size = be64_to_cpup(size);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060036 of_node_put(np);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100037 } else if (machine_is(pseries)) {
38 /* This fallback really only applies to pseries */
Nathan Fontenotc540ada2011-01-20 10:45:20 -060039 unsigned int memzero_size = 0;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060040
41 np = of_find_node_by_path("/memory@0");
42 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100043 if (!of_address_to_resource(np, 0, &r))
44 memzero_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060045 of_node_put(np);
46 }
47
48 if (memzero_size) {
49 /* We now know the size of memory@0, use this to find
50 * the first memoryblock and get its size.
51 */
52 char buf[64];
53
54 sprintf(buf, "/memory@%x", memzero_size);
55 np = of_find_node_by_path(buf);
56 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100057 if (!of_address_to_resource(np, 0, &r))
58 memblock_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060059 of_node_put(np);
60 }
61 }
62 }
Nathan Fontenotc540ada2011-01-20 10:45:20 -060063 return memblock_size;
64}
65
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100066/* WARNING: This is going to override the generic definition whenever
67 * pseries is built-in regardless of what platform is active at boot
68 * time. This is fine for now as this is the only "option" and it
69 * should work everywhere. If not, we'll have to turn this into a
70 * ppc_md. callback
71 */
Nathan Fontenotc540ada2011-01-20 10:45:20 -060072unsigned long memory_block_size_bytes(void)
73{
74 return get_memblock_size();
75}
76
David Rientjes4edd7ce2013-04-29 15:08:22 -070077#ifdef CONFIG_MEMORY_HOTREMOVE
Yinghai Lu95f72d12010-07-12 14:36:09 +100078static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
Badari Pulavarty57b53922008-04-18 13:33:50 -070079{
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +100080 unsigned long start, start_pfn;
Badari Pulavarty57b53922008-04-18 13:33:50 -070081 struct zone *zone;
Yasuaki Ishimatsu1633dbb2012-10-10 15:53:53 -070082 int ret;
83 unsigned long section;
84 unsigned long sections_to_remove;
Nathan Fontenot92ecd172008-07-03 13:20:58 +100085
Nathan Fontenot9fd3f882008-10-01 09:44:02 +000086 start_pfn = base >> PAGE_SHIFT;
Nathan Fontenot04badfd2008-10-13 08:42:00 +000087
88 if (!pfn_valid(start_pfn)) {
Yinghai Lu95f72d12010-07-12 14:36:09 +100089 memblock_remove(base, memblock_size);
Nathan Fontenot04badfd2008-10-13 08:42:00 +000090 return 0;
91 }
92
Badari Pulavarty57b53922008-04-18 13:33:50 -070093 zone = page_zone(pfn_to_page(start_pfn));
94
95 /*
96 * Remove section mappings and sysfs entries for the
97 * section of the memory we are removing.
98 *
99 * NOTE: Ideally, this should be done in generic code like
100 * remove_memory(). But remove_memory() gets called by writing
101 * to sysfs "state" file and we can't remove sysfs entries
102 * while writing to it. So we have to defer it to here.
103 */
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700104 sections_to_remove = (memblock_size >> PAGE_SHIFT) / PAGES_PER_SECTION;
Yasuaki Ishimatsu1633dbb2012-10-10 15:53:53 -0700105 for (section = 0; section < sections_to_remove; section++) {
106 unsigned long pfn = start_pfn + section * PAGES_PER_SECTION;
Andrew Morton158544b2012-10-10 15:53:52 -0700107 ret = __remove_pages(zone, pfn, PAGES_PER_SECTION);
Yasuaki Ishimatsud760afd2012-10-08 16:34:14 -0700108 if (ret)
109 return ret;
110 }
Badari Pulavarty57b53922008-04-18 13:33:50 -0700111
112 /*
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700113 * Update memory regions for memory remove
114 */
Yinghai Lu95f72d12010-07-12 14:36:09 +1000115 memblock_remove(base, memblock_size);
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700116
117 /*
Badari Pulavarty57b53922008-04-18 13:33:50 -0700118 * Remove htab bolted mappings for this section of memory
119 */
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000120 start = (unsigned long)__va(base);
Yinghai Lu95f72d12010-07-12 14:36:09 +1000121 ret = remove_section_mapping(start, start + memblock_size);
Benjamin Herrenschmidtb4a26be2010-04-06 15:03:40 +0000122
123 /* Ensure all vmalloc mappings are flushed in case they also
124 * hit that section of memory
125 */
126 vm_unmap_aliases();
127
Badari Pulavarty57b53922008-04-18 13:33:50 -0700128 return ret;
129}
130
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000131static int pseries_remove_memory(struct device_node *np)
132{
133 const char *type;
134 const unsigned int *regs;
135 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000136 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000137 int ret = -EINVAL;
138
139 /*
140 * Check to see if we are actually removing memory
141 */
142 type = of_get_property(np, "device_type", NULL);
143 if (type == NULL || strcmp(type, "memory") != 0)
144 return 0;
145
146 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000147 * Find the bae address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000148 */
149 regs = of_get_property(np, "reg", NULL);
150 if (!regs)
151 return ret;
152
153 base = *(unsigned long *)regs;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000154 lmb_size = regs[3];
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000155
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000156 ret = pseries_remove_memblock(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000157 return ret;
158}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700159#else
160static inline int pseries_remove_memblock(unsigned long base,
161 unsigned int memblock_size)
162{
163 return -EOPNOTSUPP;
164}
165static inline int pseries_remove_memory(struct device_node *np)
166{
167 return -EOPNOTSUPP;
168}
169#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000170
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700171static int pseries_add_memory(struct device_node *np)
172{
173 const char *type;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700174 const unsigned int *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000175 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000176 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700177 int ret = -EINVAL;
178
179 /*
180 * Check to see if we are actually adding memory
181 */
182 type = of_get_property(np, "device_type", NULL);
183 if (type == NULL || strcmp(type, "memory") != 0)
184 return 0;
185
186 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000187 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700188 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700189 regs = of_get_property(np, "reg", NULL);
190 if (!regs)
191 return ret;
192
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000193 base = *(unsigned long *)regs;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000194 lmb_size = regs[3];
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700195
196 /*
197 * Update memory region to represent the memory add
198 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000199 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000200 return (ret < 0) ? -EINVAL : 0;
201}
202
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000203static int pseries_update_drconf_memory(struct of_prop_reconfig *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000204{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000205 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600206 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000207 u32 entries;
208 u32 *p;
209 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000210
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600211 memblock_size = get_memblock_size();
212 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000213 return -EINVAL;
214
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000215 p = (u32 *)of_get_property(pr->dn, "ibm,dynamic-memory", NULL);
216 if (!p)
217 return -EINVAL;
218
219 /* The first int of the property is the number of lmb's described
220 * by the property. This is followed by an array of of_drconf_cell
221 * entries. Get the niumber of entries and skip to the array of
222 * of_drconf_cell's.
223 */
224 entries = *p++;
225 old_drmem = (struct of_drconf_cell *)p;
226
227 p = (u32 *)pr->prop->value;
228 p++;
229 new_drmem = (struct of_drconf_cell *)p;
230
231 for (i = 0; i < entries; i++) {
232 if ((old_drmem[i].flags & DRCONF_MEM_ASSIGNED) &&
233 (!(new_drmem[i].flags & DRCONF_MEM_ASSIGNED))) {
234 rc = pseries_remove_memblock(old_drmem[i].base_addr,
235 memblock_size);
236 break;
237 } else if ((!(old_drmem[i].flags & DRCONF_MEM_ASSIGNED)) &&
238 (new_drmem[i].flags & DRCONF_MEM_ASSIGNED)) {
239 rc = memblock_add(old_drmem[i].base_addr,
240 memblock_size);
241 rc = (rc < 0) ? -EINVAL : 0;
242 break;
243 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000244 }
245
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000246 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700247}
248
Badari Pulavarty57b53922008-04-18 13:33:50 -0700249static int pseries_memory_notifier(struct notifier_block *nb,
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000250 unsigned long action, void *node)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700251{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000252 struct of_prop_reconfig *pr;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000253 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700254
255 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000256 case OF_RECONFIG_ATTACH_NODE:
Akinobu Mitade2780a2011-06-21 03:35:56 +0000257 err = pseries_add_memory(node);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700258 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000259 case OF_RECONFIG_DETACH_NODE:
Akinobu Mitade2780a2011-06-21 03:35:56 +0000260 err = pseries_remove_memory(node);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700261 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000262 case OF_RECONFIG_UPDATE_PROPERTY:
263 pr = (struct of_prop_reconfig *)node;
264 if (!strcmp(pr->prop->name, "ibm,dynamic-memory"))
265 err = pseries_update_drconf_memory(pr);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700266 break;
267 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000268 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700269}
270
271static struct notifier_block pseries_mem_nb = {
272 .notifier_call = pseries_memory_notifier,
273};
274
275static int __init pseries_memory_hotplug_init(void)
276{
277 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000278 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700279
280 return 0;
281}
282machine_device_initcall(pseries, pseries_memory_hotplug_init);