2 * pseries Memory Hotplug infrastructure.
4 * Copyright (C) 2008 Badari Pulavarty, IBM Corporation
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.
12 #define pr_fmt(fmt) "pseries-hotplug-mem: " fmt
15 #include <linux/of_address.h>
16 #include <linux/memblock.h>
17 #include <linux/memory.h>
18 #include <linux/memory_hotplug.h>
20 #include <asm/firmware.h>
21 #include <asm/machdep.h>
23 #include <asm/sparsemem.h>
26 unsigned long pseries_memory_block_size(void)
28 struct device_node *np;
29 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
32 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
36 size = of_get_property(np, "ibm,lmb-size", NULL);
38 memblock_size = be64_to_cpup(size);
40 } else if (machine_is(pseries)) {
41 /* This fallback really only applies to pseries */
42 unsigned int memzero_size = 0;
44 np = of_find_node_by_path("/memory@0");
46 if (!of_address_to_resource(np, 0, &r))
47 memzero_size = resource_size(&r);
52 /* We now know the size of memory@0, use this to find
53 * the first memoryblock and get its size.
57 sprintf(buf, "/memory@%x", memzero_size);
58 np = of_find_node_by_path(buf);
60 if (!of_address_to_resource(np, 0, &r))
61 memblock_size = resource_size(&r);
69 #ifdef CONFIG_MEMORY_HOTREMOVE
70 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
72 unsigned long block_sz, start_pfn;
73 int sections_per_block;
76 start_pfn = base >> PAGE_SHIFT;
78 lock_device_hotplug();
80 if (!pfn_valid(start_pfn))
83 block_sz = pseries_memory_block_size();
84 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
85 nid = memory_add_physaddr_to_nid(base);
87 for (i = 0; i < sections_per_block; i++) {
88 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
89 base += MIN_MEMORY_BLOCK_SIZE;
93 /* Update memory regions for memory remove */
94 memblock_remove(base, memblock_size);
95 unlock_device_hotplug();
99 static int pseries_remove_mem_node(struct device_node *np)
104 unsigned int lmb_size;
108 * Check to see if we are actually removing memory
110 type = of_get_property(np, "device_type", NULL);
111 if (type == NULL || strcmp(type, "memory") != 0)
115 * Find the base address and size of the memblock
117 regs = of_get_property(np, "reg", NULL);
121 base = be64_to_cpu(*(unsigned long *)regs);
122 lmb_size = be32_to_cpu(regs[3]);
124 pseries_remove_memblock(base, lmb_size);
128 static inline int pseries_remove_memblock(unsigned long base,
129 unsigned int memblock_size)
133 static inline int pseries_remove_mem_node(struct device_node *np)
137 #endif /* CONFIG_MEMORY_HOTREMOVE */
139 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
143 lock_device_hotplug();
145 switch (hp_elog->action) {
147 pr_err("Invalid action (%d) specified\n", hp_elog->action);
152 unlock_device_hotplug();
156 static int pseries_add_mem_node(struct device_node *np)
161 unsigned int lmb_size;
165 * Check to see if we are actually adding memory
167 type = of_get_property(np, "device_type", NULL);
168 if (type == NULL || strcmp(type, "memory") != 0)
172 * Find the base and size of the memblock
174 regs = of_get_property(np, "reg", NULL);
178 base = be64_to_cpu(*(unsigned long *)regs);
179 lmb_size = be32_to_cpu(regs[3]);
182 * Update memory region to represent the memory add
184 ret = memblock_add(base, lmb_size);
185 return (ret < 0) ? -EINVAL : 0;
188 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
190 struct of_drconf_cell *new_drmem, *old_drmem;
191 unsigned long memblock_size;
196 memblock_size = pseries_memory_block_size();
200 p = (__be32 *) pr->old_prop->value;
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
206 * entries. Get the number of entries and skip to the array of
209 entries = be32_to_cpu(*p++);
210 old_drmem = (struct of_drconf_cell *)p;
212 p = (__be32 *)pr->prop->value;
214 new_drmem = (struct of_drconf_cell *)p;
216 for (i = 0; i < entries; i++) {
217 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),
223 } 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),
229 rc = (rc < 0) ? -EINVAL : 0;
236 static int pseries_memory_notifier(struct notifier_block *nb,
237 unsigned long action, void *data)
239 struct of_reconfig_data *rd = data;
243 case OF_RECONFIG_ATTACH_NODE:
244 err = pseries_add_mem_node(rd->dn);
246 case OF_RECONFIG_DETACH_NODE:
247 err = pseries_remove_mem_node(rd->dn);
249 case OF_RECONFIG_UPDATE_PROPERTY:
250 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
251 err = pseries_update_drconf_memory(rd);
254 return notifier_from_errno(err);
257 static struct notifier_block pseries_mem_nb = {
258 .notifier_call = pseries_memory_notifier,
261 static int __init pseries_memory_hotplug_init(void)
263 if (firmware_has_feature(FW_FEATURE_LPAR))
264 of_reconfig_notifier_register(&pseries_mem_nb);
268 machine_device_initcall(pseries, pseries_memory_hotplug_init);