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.
13 #include <linux/of_address.h>
14 #include <linux/memblock.h>
15 #include <linux/vmalloc.h>
16 #include <linux/memory.h>
17 #include <linux/memory_hotplug.h>
19 #include <asm/firmware.h>
20 #include <asm/machdep.h>
22 #include <asm/sparsemem.h>
24 static unsigned long get_memblock_size(void)
26 struct device_node *np;
27 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
30 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
34 size = of_get_property(np, "ibm,lmb-size", NULL);
36 memblock_size = be64_to_cpup(size);
38 } else if (machine_is(pseries)) {
39 /* This fallback really only applies to pseries */
40 unsigned int memzero_size = 0;
42 np = of_find_node_by_path("/memory@0");
44 if (!of_address_to_resource(np, 0, &r))
45 memzero_size = resource_size(&r);
50 /* We now know the size of memory@0, use this to find
51 * the first memoryblock and get its size.
55 sprintf(buf, "/memory@%x", memzero_size);
56 np = of_find_node_by_path(buf);
58 if (!of_address_to_resource(np, 0, &r))
59 memblock_size = resource_size(&r);
67 /* WARNING: This is going to override the generic definition whenever
68 * pseries is built-in regardless of what platform is active at boot
69 * time. This is fine for now as this is the only "option" and it
70 * should work everywhere. If not, we'll have to turn this into a
73 unsigned long memory_block_size_bytes(void)
75 return get_memblock_size();
78 #ifdef CONFIG_MEMORY_HOTREMOVE
79 static int pseries_remove_memory(u64 start, u64 size)
83 /* Remove htab bolted mappings for this section of memory */
84 start = (unsigned long)__va(start);
85 ret = remove_section_mapping(start, start + size);
87 /* Ensure all vmalloc mappings are flushed in case they also
88 * hit that section of memory
95 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
97 unsigned long block_sz, start_pfn;
98 int sections_per_block;
101 start_pfn = base >> PAGE_SHIFT;
103 lock_device_hotplug();
105 if (!pfn_valid(start_pfn))
108 block_sz = memory_block_size_bytes();
109 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
110 nid = memory_add_physaddr_to_nid(base);
112 for (i = 0; i < sections_per_block; i++) {
113 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
114 base += MIN_MEMORY_BLOCK_SIZE;
118 /* Update memory regions for memory remove */
119 memblock_remove(base, memblock_size);
120 unlock_device_hotplug();
124 static int pseries_remove_mem_node(struct device_node *np)
127 const unsigned int *regs;
129 unsigned int lmb_size;
133 * Check to see if we are actually removing memory
135 type = of_get_property(np, "device_type", NULL);
136 if (type == NULL || strcmp(type, "memory") != 0)
140 * Find the bae address and size of the memblock
142 regs = of_get_property(np, "reg", NULL);
146 base = *(unsigned long *)regs;
149 pseries_remove_memblock(base, lmb_size);
153 static inline int pseries_remove_memblock(unsigned long base,
154 unsigned int memblock_size)
158 static inline int pseries_remove_mem_node(struct device_node *np)
162 #endif /* CONFIG_MEMORY_HOTREMOVE */
164 static int pseries_add_mem_node(struct device_node *np)
167 const unsigned int *regs;
169 unsigned int lmb_size;
173 * Check to see if we are actually adding memory
175 type = of_get_property(np, "device_type", NULL);
176 if (type == NULL || strcmp(type, "memory") != 0)
180 * Find the base and size of the memblock
182 regs = of_get_property(np, "reg", NULL);
186 base = *(unsigned long *)regs;
190 * Update memory region to represent the memory add
192 ret = memblock_add(base, lmb_size);
193 return (ret < 0) ? -EINVAL : 0;
196 static int pseries_update_drconf_memory(struct of_prop_reconfig *pr)
198 struct of_drconf_cell *new_drmem, *old_drmem;
199 unsigned long memblock_size;
204 memblock_size = get_memblock_size();
208 p = (u32 *)of_get_property(pr->dn, "ibm,dynamic-memory", NULL);
212 /* The first int of the property is the number of lmb's described
213 * by the property. This is followed by an array of of_drconf_cell
214 * entries. Get the niumber of entries and skip to the array of
218 old_drmem = (struct of_drconf_cell *)p;
220 p = (u32 *)pr->prop->value;
222 new_drmem = (struct of_drconf_cell *)p;
224 for (i = 0; i < entries; i++) {
225 if ((old_drmem[i].flags & DRCONF_MEM_ASSIGNED) &&
226 (!(new_drmem[i].flags & DRCONF_MEM_ASSIGNED))) {
227 rc = pseries_remove_memblock(old_drmem[i].base_addr,
230 } else if ((!(old_drmem[i].flags & DRCONF_MEM_ASSIGNED)) &&
231 (new_drmem[i].flags & DRCONF_MEM_ASSIGNED)) {
232 rc = memblock_add(old_drmem[i].base_addr,
234 rc = (rc < 0) ? -EINVAL : 0;
242 static int pseries_memory_notifier(struct notifier_block *nb,
243 unsigned long action, void *node)
245 struct of_prop_reconfig *pr;
249 case OF_RECONFIG_ATTACH_NODE:
250 err = pseries_add_mem_node(node);
252 case OF_RECONFIG_DETACH_NODE:
253 err = pseries_remove_mem_node(node);
255 case OF_RECONFIG_UPDATE_PROPERTY:
256 pr = (struct of_prop_reconfig *)node;
257 if (!strcmp(pr->prop->name, "ibm,dynamic-memory"))
258 err = pseries_update_drconf_memory(pr);
261 return notifier_from_errno(err);
264 static struct notifier_block pseries_mem_nb = {
265 .notifier_call = pseries_memory_notifier,
268 static int __init pseries_memory_hotplug_init(void)
270 if (firmware_has_feature(FW_FEATURE_LPAR))
271 of_reconfig_notifier_register(&pseries_mem_nb);
273 #ifdef CONFIG_MEMORY_HOTREMOVE
274 ppc_md.remove_memory = pseries_remove_memory;
279 machine_device_initcall(pseries, pseries_memory_hotplug_init);