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/memory.h>
16 #include <linux/memory_hotplug.h>
18 #include <asm/firmware.h>
19 #include <asm/machdep.h>
21 #include <asm/sparsemem.h>
24 unsigned long pseries_memory_block_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 #ifdef CONFIG_MEMORY_HOTREMOVE
68 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
70 unsigned long block_sz, start_pfn;
71 int sections_per_block;
74 start_pfn = base >> PAGE_SHIFT;
76 lock_device_hotplug();
78 if (!pfn_valid(start_pfn))
81 block_sz = pseries_memory_block_size();
82 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
83 nid = memory_add_physaddr_to_nid(base);
85 for (i = 0; i < sections_per_block; i++) {
86 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
87 base += MIN_MEMORY_BLOCK_SIZE;
91 /* Update memory regions for memory remove */
92 memblock_remove(base, memblock_size);
93 unlock_device_hotplug();
97 static int pseries_remove_mem_node(struct device_node *np)
102 unsigned int lmb_size;
106 * Check to see if we are actually removing memory
108 type = of_get_property(np, "device_type", NULL);
109 if (type == NULL || strcmp(type, "memory") != 0)
113 * Find the base address and size of the memblock
115 regs = of_get_property(np, "reg", NULL);
119 base = be64_to_cpu(*(unsigned long *)regs);
120 lmb_size = be32_to_cpu(regs[3]);
122 pseries_remove_memblock(base, lmb_size);
126 static inline int pseries_remove_memblock(unsigned long base,
127 unsigned int memblock_size)
131 static inline int pseries_remove_mem_node(struct device_node *np)
135 #endif /* CONFIG_MEMORY_HOTREMOVE */
137 static int pseries_add_mem_node(struct device_node *np)
142 unsigned int lmb_size;
146 * Check to see if we are actually adding memory
148 type = of_get_property(np, "device_type", NULL);
149 if (type == NULL || strcmp(type, "memory") != 0)
153 * Find the base and size of the memblock
155 regs = of_get_property(np, "reg", NULL);
159 base = be64_to_cpu(*(unsigned long *)regs);
160 lmb_size = be32_to_cpu(regs[3]);
163 * Update memory region to represent the memory add
165 ret = memblock_add(base, lmb_size);
166 return (ret < 0) ? -EINVAL : 0;
169 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
171 struct of_drconf_cell *new_drmem, *old_drmem;
172 unsigned long memblock_size;
177 memblock_size = pseries_memory_block_size();
181 p = (__be32 *) pr->old_prop->value;
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
187 * entries. Get the number of entries and skip to the array of
190 entries = be32_to_cpu(*p++);
191 old_drmem = (struct of_drconf_cell *)p;
193 p = (__be32 *)pr->prop->value;
195 new_drmem = (struct of_drconf_cell *)p;
197 for (i = 0; i < entries; i++) {
198 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),
204 } 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),
210 rc = (rc < 0) ? -EINVAL : 0;
217 static int pseries_memory_notifier(struct notifier_block *nb,
218 unsigned long action, void *data)
220 struct of_reconfig_data *rd = data;
224 case OF_RECONFIG_ATTACH_NODE:
225 err = pseries_add_mem_node(rd->dn);
227 case OF_RECONFIG_DETACH_NODE:
228 err = pseries_remove_mem_node(rd->dn);
230 case OF_RECONFIG_UPDATE_PROPERTY:
231 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
232 err = pseries_update_drconf_memory(rd);
235 return notifier_from_errno(err);
238 static struct notifier_block pseries_mem_nb = {
239 .notifier_call = pseries_memory_notifier,
242 static int __init pseries_memory_hotplug_init(void)
244 if (firmware_has_feature(FW_FEATURE_LPAR))
245 of_reconfig_notifier_register(&pseries_mem_nb);
249 machine_device_initcall(pseries, pseries_memory_hotplug_init);