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>
25 unsigned long pseries_memory_block_size(void)
27 struct device_node *np;
28 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
31 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
35 size = of_get_property(np, "ibm,lmb-size", NULL);
37 memblock_size = be64_to_cpup(size);
39 } else if (machine_is(pseries)) {
40 /* This fallback really only applies to pseries */
41 unsigned int memzero_size = 0;
43 np = of_find_node_by_path("/memory@0");
45 if (!of_address_to_resource(np, 0, &r))
46 memzero_size = resource_size(&r);
51 /* We now know the size of memory@0, use this to find
52 * the first memoryblock and get its size.
56 sprintf(buf, "/memory@%x", memzero_size);
57 np = of_find_node_by_path(buf);
59 if (!of_address_to_resource(np, 0, &r))
60 memblock_size = resource_size(&r);
68 #ifdef CONFIG_MEMORY_HOTREMOVE
69 static int pseries_remove_memory(u64 start, u64 size)
73 /* Remove htab bolted mappings for this section of memory */
74 start = (unsigned long)__va(start);
75 ret = remove_section_mapping(start, start + size);
77 /* Ensure all vmalloc mappings are flushed in case they also
78 * hit that section of memory
85 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
87 unsigned long block_sz, start_pfn;
88 int sections_per_block;
91 start_pfn = base >> PAGE_SHIFT;
93 lock_device_hotplug();
95 if (!pfn_valid(start_pfn))
98 block_sz = pseries_memory_block_size();
99 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
100 nid = memory_add_physaddr_to_nid(base);
102 for (i = 0; i < sections_per_block; i++) {
103 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
104 base += MIN_MEMORY_BLOCK_SIZE;
108 /* Update memory regions for memory remove */
109 memblock_remove(base, memblock_size);
110 unlock_device_hotplug();
114 static int pseries_remove_mem_node(struct device_node *np)
119 unsigned int lmb_size;
123 * Check to see if we are actually removing memory
125 type = of_get_property(np, "device_type", NULL);
126 if (type == NULL || strcmp(type, "memory") != 0)
130 * Find the base address and size of the memblock
132 regs = of_get_property(np, "reg", NULL);
136 base = be64_to_cpu(*(unsigned long *)regs);
137 lmb_size = be32_to_cpu(regs[3]);
139 pseries_remove_memblock(base, lmb_size);
143 static inline int pseries_remove_memblock(unsigned long base,
144 unsigned int memblock_size)
148 static inline int pseries_remove_mem_node(struct device_node *np)
152 #endif /* CONFIG_MEMORY_HOTREMOVE */
154 static int pseries_add_mem_node(struct device_node *np)
159 unsigned int lmb_size;
163 * Check to see if we are actually adding memory
165 type = of_get_property(np, "device_type", NULL);
166 if (type == NULL || strcmp(type, "memory") != 0)
170 * Find the base and size of the memblock
172 regs = of_get_property(np, "reg", NULL);
176 base = be64_to_cpu(*(unsigned long *)regs);
177 lmb_size = be32_to_cpu(regs[3]);
180 * Update memory region to represent the memory add
182 ret = memblock_add(base, lmb_size);
183 return (ret < 0) ? -EINVAL : 0;
186 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
188 struct of_drconf_cell *new_drmem, *old_drmem;
189 unsigned long memblock_size;
194 memblock_size = pseries_memory_block_size();
198 p = (__be32 *) pr->old_prop->value;
202 /* The first int of the property is the number of lmb's described
203 * by the property. This is followed by an array of of_drconf_cell
204 * entries. Get the number of entries and skip to the array of
207 entries = be32_to_cpu(*p++);
208 old_drmem = (struct of_drconf_cell *)p;
210 p = (__be32 *)pr->prop->value;
212 new_drmem = (struct of_drconf_cell *)p;
214 for (i = 0; i < entries; i++) {
215 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
216 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
217 rc = pseries_remove_memblock(
218 be64_to_cpu(old_drmem[i].base_addr),
221 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
222 DRCONF_MEM_ASSIGNED)) &&
223 (be32_to_cpu(new_drmem[i].flags) &
224 DRCONF_MEM_ASSIGNED)) {
225 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
227 rc = (rc < 0) ? -EINVAL : 0;
234 static int pseries_memory_notifier(struct notifier_block *nb,
235 unsigned long action, void *data)
237 struct of_reconfig_data *rd = data;
241 case OF_RECONFIG_ATTACH_NODE:
242 err = pseries_add_mem_node(rd->dn);
244 case OF_RECONFIG_DETACH_NODE:
245 err = pseries_remove_mem_node(rd->dn);
247 case OF_RECONFIG_UPDATE_PROPERTY:
248 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
249 err = pseries_update_drconf_memory(rd);
252 return notifier_from_errno(err);
255 static struct notifier_block pseries_mem_nb = {
256 .notifier_call = pseries_memory_notifier,
259 static int __init pseries_memory_hotplug_init(void)
261 if (firmware_has_feature(FW_FEATURE_LPAR))
262 of_reconfig_notifier_register(&pseries_mem_nb);
264 #ifdef CONFIG_MEMORY_HOTREMOVE
265 ppc_md.remove_memory = pseries_remove_memory;
270 machine_device_initcall(pseries, pseries_memory_hotplug_init);