blob: 211d0bf7f5d94738e70c1da5f6ca529209f80365 [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
Nathan Fontenot999e2da2015-02-10 13:47:02 -060012#define pr_fmt(fmt) "pseries-hotplug-mem: " fmt
13
Badari Pulavarty57b53922008-04-18 13:33:50 -070014#include <linux/of.h>
Rob Herring26a20562013-09-26 07:40:04 -050015#include <linux/of_address.h>
Yinghai Lu95f72d12010-07-12 14:36:09 +100016#include <linux/memblock.h>
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100017#include <linux/memory.h>
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060018#include <linux/memory_hotplug.h>
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100019
Badari Pulavarty57b53922008-04-18 13:33:50 -070020#include <asm/firmware.h>
21#include <asm/machdep.h>
Rob Herring26a20562013-09-26 07:40:04 -050022#include <asm/prom.h>
Michael Neuling0b2f8282009-02-08 14:49:39 +000023#include <asm/sparsemem.h>
Anton Blanchard1217d342014-08-20 08:55:19 +100024#include "pseries.h"
Badari Pulavarty57b53922008-04-18 13:33:50 -070025
Anton Blancharda5d86252014-06-04 17:50:47 +100026unsigned long pseries_memory_block_size(void)
Nathan Fontenotc540ada2011-01-20 10:45:20 -060027{
28 struct device_node *np;
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100029 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
30 struct resource r;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060031
32 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
33 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100034 const __be64 *size;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060035
36 size = of_get_property(np, "ibm,lmb-size", NULL);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100037 if (size)
38 memblock_size = be64_to_cpup(size);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060039 of_node_put(np);
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100040 } else if (machine_is(pseries)) {
41 /* This fallback really only applies to pseries */
Nathan Fontenotc540ada2011-01-20 10:45:20 -060042 unsigned int memzero_size = 0;
Nathan Fontenotc540ada2011-01-20 10:45:20 -060043
44 np = of_find_node_by_path("/memory@0");
45 if (np) {
Benjamin Herrenschmidt770e1ac2011-06-14 10:57:51 +100046 if (!of_address_to_resource(np, 0, &r))
47 memzero_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060048 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 Herrenschmidt770e1ac2011-06-14 10:57:51 +100060 if (!of_address_to_resource(np, 0, &r))
61 memblock_size = resource_size(&r);
Nathan Fontenotc540ada2011-01-20 10:45:20 -060062 of_node_put(np);
63 }
64 }
65 }
Nathan Fontenotc540ada2011-01-20 10:45:20 -060066 return memblock_size;
67}
68
David Rientjes4edd7ce2013-04-29 15:08:22 -070069#ifdef CONFIG_MEMORY_HOTREMOVE
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060070static 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 Zhong42dbfc82014-04-10 16:25:31 +080078 lock_device_hotplug();
79
80 if (!pfn_valid(start_pfn))
81 goto out;
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060082
Anton Blancharda5d86252014-06-04 17:50:47 +100083 block_sz = pseries_memory_block_size();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060084 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 Zhong42dbfc82014-04-10 16:25:31 +080092out:
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060093 /* Update memory regions for memory remove */
94 memblock_remove(base, memblock_size);
Li Zhong42dbfc82014-04-10 16:25:31 +080095 unlock_device_hotplug();
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -060096 return 0;
97}
98
99static int pseries_remove_mem_node(struct device_node *np)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000100{
101 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500102 const __be32 *regs;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000103 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000104 unsigned int lmb_size;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000105 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 Zhong4646d132014-08-07 13:11:58 +0800115 * Find the base address and size of the memblock
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000116 */
117 regs = of_get_property(np, "reg", NULL);
118 if (!regs)
119 return ret;
120
Thomas Falconc9ac4082014-08-19 15:44:57 -0500121 base = be64_to_cpu(*(unsigned long *)regs);
122 lmb_size = be32_to_cpu(regs[3]);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000123
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600124 pseries_remove_memblock(base, lmb_size);
125 return 0;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000126}
David Rientjes4edd7ce2013-04-29 15:08:22 -0700127#else
128static inline int pseries_remove_memblock(unsigned long base,
129 unsigned int memblock_size)
130{
131 return -EOPNOTSUPP;
132}
Nathan Fontenot9ac8cde2014-01-27 10:54:06 -0600133static inline int pseries_remove_mem_node(struct device_node *np)
David Rientjes4edd7ce2013-04-29 15:08:22 -0700134{
Gavin Shanf1b39292014-08-11 19:16:19 +1000135 return 0;
David Rientjes4edd7ce2013-04-29 15:08:22 -0700136}
137#endif /* CONFIG_MEMORY_HOTREMOVE */
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000138
Nathan Fontenot999e2da2015-02-10 13:47:02 -0600139int 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 Fontenot9ac8cde2014-01-27 10:54:06 -0600156static int pseries_add_mem_node(struct device_node *np)
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700157{
158 const char *type;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500159 const __be32 *regs;
Nathan Fontenot92ecd172008-07-03 13:20:58 +1000160 unsigned long base;
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000161 unsigned int lmb_size;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700162 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 Lu95f72d12010-07-12 14:36:09 +1000172 * Find the base and size of the memblock
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700173 */
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700174 regs = of_get_property(np, "reg", NULL);
175 if (!regs)
176 return ret;
177
Thomas Falconc9ac4082014-08-19 15:44:57 -0500178 base = be64_to_cpu(*(unsigned long *)regs);
179 lmb_size = be32_to_cpu(regs[3]);
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700180
181 /*
182 * Update memory region to represent the memory add
183 */
Benjamin Herrenschmidt3fdfd992010-07-23 10:35:52 +1000184 ret = memblock_add(base, lmb_size);
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000185 return (ret < 0) ? -EINVAL : 0;
186}
187
Grant Likelyf5242e52014-11-24 17:58:01 +0000188static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000189{
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000190 struct of_drconf_cell *new_drmem, *old_drmem;
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600191 unsigned long memblock_size;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000192 u32 entries;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500193 __be32 *p;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000194 int i, rc = -EINVAL;
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000195
Anton Blancharda5d86252014-06-04 17:50:47 +1000196 memblock_size = pseries_memory_block_size();
Nathan Fontenotc540ada2011-01-20 10:45:20 -0600197 if (!memblock_size)
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000198 return -EINVAL;
199
Thomas Falconc9ac4082014-08-19 15:44:57 -0500200 p = (__be32 *) pr->old_prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000201 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 Zhong4646d132014-08-07 13:11:58 +0800206 * entries. Get the number of entries and skip to the array of
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000207 * of_drconf_cell's.
208 */
Thomas Falconc9ac4082014-08-19 15:44:57 -0500209 entries = be32_to_cpu(*p++);
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000210 old_drmem = (struct of_drconf_cell *)p;
211
Thomas Falconc9ac4082014-08-19 15:44:57 -0500212 p = (__be32 *)pr->prop->value;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000213 p++;
214 new_drmem = (struct of_drconf_cell *)p;
215
216 for (i = 0; i < entries; i++) {
Thomas Falconc9ac4082014-08-19 15:44:57 -0500217 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 Fontenot1cf3d8b2012-10-02 16:57:57 +0000221 memblock_size);
222 break;
Thomas Falconc9ac4082014-08-19 15:44:57 -0500223 } 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 Fontenot1cf3d8b2012-10-02 16:57:57 +0000228 memblock_size);
229 rc = (rc < 0) ? -EINVAL : 0;
230 break;
231 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000232 }
Nathan Fontenot3c3f67e2008-07-03 13:22:39 +1000233 return rc;
Badari Pulavarty98d5c212008-04-18 13:33:52 -0700234}
235
Badari Pulavarty57b53922008-04-18 13:33:50 -0700236static int pseries_memory_notifier(struct notifier_block *nb,
Grant Likelyf5242e52014-11-24 17:58:01 +0000237 unsigned long action, void *data)
Badari Pulavarty57b53922008-04-18 13:33:50 -0700238{
Grant Likelyf5242e52014-11-24 17:58:01 +0000239 struct of_reconfig_data *rd = data;
Akinobu Mitade2780a2011-06-21 03:35:56 +0000240 int err = 0;
Badari Pulavarty57b53922008-04-18 13:33:50 -0700241
242 switch (action) {
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000243 case OF_RECONFIG_ATTACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000244 err = pseries_add_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700245 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000246 case OF_RECONFIG_DETACH_NODE:
Grant Likelyf5242e52014-11-24 17:58:01 +0000247 err = pseries_remove_mem_node(rd->dn);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700248 break;
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000249 case OF_RECONFIG_UPDATE_PROPERTY:
Grant Likelyf5242e52014-11-24 17:58:01 +0000250 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
251 err = pseries_update_drconf_memory(rd);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700252 break;
253 }
Akinobu Mitade2780a2011-06-21 03:35:56 +0000254 return notifier_from_errno(err);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700255}
256
257static struct notifier_block pseries_mem_nb = {
258 .notifier_call = pseries_memory_notifier,
259};
260
261static int __init pseries_memory_hotplug_init(void)
262{
263 if (firmware_has_feature(FW_FEATURE_LPAR))
Nathan Fontenot1cf3d8b2012-10-02 16:57:57 +0000264 of_reconfig_notifier_register(&pseries_mem_nb);
Badari Pulavarty57b53922008-04-18 13:33:50 -0700265
Badari Pulavarty57b53922008-04-18 13:33:50 -0700266 return 0;
267}
268machine_device_initcall(pseries, pseries_memory_hotplug_init);