blob: fa41f0da5b6f4687952388f04d1ff114c93520f9 [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 Herrenschmidt770e1ac2011-06-14 10:57:51 +100015#include <linux/memory.h>
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060016#include <linux/memory_hotplug.h>
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100017
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>
Anton Blanchard1217d342014-08-20 08:55:19 +100022#include "pseries.h"
Badari Pulavarty57b53922008-04-18 13:33:50 -070023
Anton Blancharda5d86252014-06-04 17:50:47 +100024unsigned long pseries_memory_block_size(void)
Nathan Fontenotc540ada2011-01-20 10:45:20 -060025{
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
David Rientjes4edd7ce2013-04-29 15:08:22 -070067#ifdef CONFIG_MEMORY_HOTREMOVE
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060068static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
69{
70 unsigned long block_sz, start_pfn;
71 int sections_per_block;
72 int i, nid;
73
74 start_pfn = base >> PAGE_SHIFT;
75
Li Zhong42dbfc82014-04-10 16:25:31 +080076 lock_device_hotplug();
77
78 if (!pfn_valid(start_pfn))
79 goto out;
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060080
Anton Blancharda5d86252014-06-04 17:50:47 +100081 block_sz = pseries_memory_block_size();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060082 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
83 nid = memory_add_physaddr_to_nid(base);
84
85 for (i = 0; i < sections_per_block; i++) {
86 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
87 base += MIN_MEMORY_BLOCK_SIZE;
88 }
89
Li Zhong42dbfc82014-04-10 16:25:31 +080090out:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060091 /* Update memory regions for memory remove */
92 memblock_remove(base, memblock_size);
Li Zhong42dbfc82014-04-10 16:25:31 +080093 unlock_device_hotplug();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060094 return 0;
95}
96
97static int pseries_remove_mem_node(struct device_node *np)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +100098{
99 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500100 const __be32 *regs;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000101 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000102 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000103 int ret = -EINVAL;
104
105 /*
106 * Check to see if we are actually removing memory
107 */
108 type = of_get_property(np, "device_type", NULL);
109 if (type == NULL || strcmp(type, "memory") != 0)
110 return 0;
111
112 /*
Li Zhong4646d132014-08-07 13:11:58 +0800113 * Find the base address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000114 */
115 regs = of_get_property(np, "reg", NULL);
116 if (!regs)
117 return ret;
118
Thomas Falconc9ac4082014-08-19 15:44:57 -0500119 base = be64_to_cpu(*(unsigned long *)regs);
120 lmb_size = be32_to_cpu(regs[3]);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000121
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600122 pseries_remove_memblock(base, lmb_size);
123 return 0;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000124}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700125#else
126static inline int pseries_remove_memblock(unsigned long base,
127 unsigned int memblock_size)
128{
129 return -EOPNOTSUPP;
130}
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600131static inline int pseries_remove_mem_node(struct device_node *np)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700132{
Gavin Shanf1b39292014-08-11 19:16:19 +1000133 return 0;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700134}
135#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000136
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600137static int pseries_add_mem_node(struct device_node *np)
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700138{
139 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500140 const __be32 *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000141 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000142 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700143 int ret = -EINVAL;
144
145 /*
146 * Check to see if we are actually adding memory
147 */
148 type = of_get_property(np, "device_type", NULL);
149 if (type == NULL || strcmp(type, "memory") != 0)
150 return 0;
151
152 /*
Yinghai Lu95f72d12010-07-12 14:36:09 +1000153 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700154 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700155 regs = of_get_property(np, "reg", NULL);
156 if (!regs)
157 return ret;
158
Thomas Falconc9ac4082014-08-19 15:44:57 -0500159 base = be64_to_cpu(*(unsigned long *)regs);
160 lmb_size = be32_to_cpu(regs[3]);
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700161
162 /*
163 * Update memory region to represent the memory add
164 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000165 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000166 return (ret < 0) ? -EINVAL : 0;
167}
168
Grant Likelyf5242e52014-11-24 17:58:01 +0000169static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000170{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000171 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600172 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000173 u32 entries;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500174 __be32 *p;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000175 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000176
Anton Blancharda5d86252014-06-04 17:50:47 +1000177 memblock_size = pseries_memory_block_size();
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600178 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000179 return -EINVAL;
180
Thomas Falconc9ac4082014-08-19 15:44:57 -0500181 p = (__be32 *) pr->old_prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000182 if (!p)
183 return -EINVAL;
184
185 /* The first int of the property is the number of lmb's described
186 * by the property. This is followed by an array of of_drconf_cell
Li Zhong4646d132014-08-07 13:11:58 +0800187 * entries. Get the number of entries and skip to the array of
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000188 * of_drconf_cell's.
189 */
Thomas Falconc9ac4082014-08-19 15:44:57 -0500190 entries = be32_to_cpu(*p++);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000191 old_drmem = (struct of_drconf_cell *)p;
192
Thomas Falconc9ac4082014-08-19 15:44:57 -0500193 p = (__be32 *)pr->prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000194 p++;
195 new_drmem = (struct of_drconf_cell *)p;
196
197 for (i = 0; i < entries; i++) {
Thomas Falconc9ac4082014-08-19 15:44:57 -0500198 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
199 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
200 rc = pseries_remove_memblock(
201 be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000202 memblock_size);
203 break;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500204 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
205 DRCONF_MEM_ASSIGNED)) &&
206 (be32_to_cpu(new_drmem[i].flags) &
207 DRCONF_MEM_ASSIGNED)) {
208 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000209 memblock_size);
210 rc = (rc < 0) ? -EINVAL : 0;
211 break;
212 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000213 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000214 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700215}
216
Badari Pulavarty57b53922008-04-18 13:33:50 -0700217static int pseries_memory_notifier(struct notifier_block *nb,
Grant Likelyf5242e52014-11-24 17:58:01 +0000218 unsigned long action, void *data)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700219{
Grant Likelyf5242e52014-11-24 17:58:01 +0000220 struct of_reconfig_data *rd = data;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000221 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700222
223 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000224 case OF_RECONFIG_ATTACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000225 err = pseries_add_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700226 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000227 case OF_RECONFIG_DETACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000228 err = pseries_remove_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700229 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000230 case OF_RECONFIG_UPDATE_PROPERTY:
Grant Likelyf5242e52014-11-24 17:58:01 +0000231 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
232 err = pseries_update_drconf_memory(rd);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700233 break;
234 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000235 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700236}
237
238static struct notifier_block pseries_mem_nb = {
239 .notifier_call = pseries_memory_notifier,
240};
241
242static int __init pseries_memory_hotplug_init(void)
243{
244 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000245 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700246
Badari Pulavarty57b53922008-04-18 13:33:50 -0700247 return 0;
248}
249machine_device_initcall(pseries, pseries_memory_hotplug_init);