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>
19 #include <linux/slab.h>
21 #include <asm/firmware.h>
22 #include <asm/machdep.h>
24 #include <asm/sparsemem.h>
27 static bool rtas_hp_event;
29 unsigned long pseries_memory_block_size(void)
31 struct device_node *np;
32 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
35 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
39 size = of_get_property(np, "ibm,lmb-size", NULL);
41 memblock_size = be64_to_cpup(size);
43 } else if (machine_is(pseries)) {
44 /* This fallback really only applies to pseries */
45 unsigned int memzero_size = 0;
47 np = of_find_node_by_path("/memory@0");
49 if (!of_address_to_resource(np, 0, &r))
50 memzero_size = resource_size(&r);
55 /* We now know the size of memory@0, use this to find
56 * the first memoryblock and get its size.
60 sprintf(buf, "/memory@%x", memzero_size);
61 np = of_find_node_by_path(buf);
63 if (!of_address_to_resource(np, 0, &r))
64 memblock_size = resource_size(&r);
72 static void dlpar_free_drconf_property(struct property *prop)
79 static struct property *dlpar_clone_drconf_property(struct device_node *dn)
81 struct property *prop, *new_prop;
82 struct of_drconf_cell *lmbs;
86 prop = of_find_property(dn, "ibm,dynamic-memory", NULL);
90 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
94 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
95 new_prop->value = kmemdup(prop->value, prop->length, GFP_KERNEL);
96 if (!new_prop->name || !new_prop->value) {
97 dlpar_free_drconf_property(new_prop);
101 new_prop->length = prop->length;
103 /* Convert the property to cpu endian-ness */
105 *p = be32_to_cpu(*p);
108 lmbs = (struct of_drconf_cell *)p;
110 for (i = 0; i < num_lmbs; i++) {
111 lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr);
112 lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index);
113 lmbs[i].aa_index = be32_to_cpu(lmbs[i].aa_index);
114 lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
120 static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
122 unsigned long section_nr;
123 struct mem_section *mem_sect;
124 struct memory_block *mem_block;
126 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
127 mem_sect = __nr_to_section(section_nr);
129 mem_block = find_memory_block(mem_sect);
133 #ifdef CONFIG_MEMORY_HOTREMOVE
134 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
136 unsigned long block_sz, start_pfn;
137 int sections_per_block;
140 start_pfn = base >> PAGE_SHIFT;
142 lock_device_hotplug();
144 if (!pfn_valid(start_pfn))
147 block_sz = pseries_memory_block_size();
148 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
149 nid = memory_add_physaddr_to_nid(base);
151 for (i = 0; i < sections_per_block; i++) {
152 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
153 base += MIN_MEMORY_BLOCK_SIZE;
157 /* Update memory regions for memory remove */
158 memblock_remove(base, memblock_size);
159 unlock_device_hotplug();
163 static int pseries_remove_mem_node(struct device_node *np)
168 unsigned int lmb_size;
172 * Check to see if we are actually removing memory
174 type = of_get_property(np, "device_type", NULL);
175 if (type == NULL || strcmp(type, "memory") != 0)
179 * Find the base address and size of the memblock
181 regs = of_get_property(np, "reg", NULL);
185 base = be64_to_cpu(*(unsigned long *)regs);
186 lmb_size = be32_to_cpu(regs[3]);
188 pseries_remove_memblock(base, lmb_size);
192 static bool lmb_is_removable(struct of_drconf_cell *lmb)
194 int i, scns_per_block;
196 unsigned long pfn, block_sz;
199 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
202 block_sz = memory_block_size_bytes();
203 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
204 phys_addr = lmb->base_addr;
206 for (i = 0; i < scns_per_block; i++) {
207 pfn = PFN_DOWN(phys_addr);
208 if (!pfn_present(pfn)) {
209 phys_addr += MIN_MEMORY_BLOCK_SIZE;
213 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
214 phys_addr += MIN_MEMORY_BLOCK_SIZE;
217 return rc ? true : false;
220 static int dlpar_add_lmb(struct of_drconf_cell *);
222 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
224 struct memory_block *mem_block;
225 unsigned long block_sz;
228 if (!lmb_is_removable(lmb))
231 mem_block = lmb_to_memblock(lmb);
235 rc = device_offline(&mem_block->dev);
236 put_device(&mem_block->dev);
240 block_sz = pseries_memory_block_size();
241 nid = memory_add_physaddr_to_nid(lmb->base_addr);
243 remove_memory(nid, lmb->base_addr, block_sz);
245 /* Update memory regions for memory remove */
246 memblock_remove(lmb->base_addr, block_sz);
248 dlpar_release_drc(lmb->drc_index);
250 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
254 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
255 struct property *prop)
257 struct of_drconf_cell *lmbs;
258 int lmbs_removed = 0;
259 int lmbs_available = 0;
263 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
265 if (lmbs_to_remove == 0)
270 lmbs = (struct of_drconf_cell *)p;
272 /* Validate that there are enough LMBs to satisfy the request */
273 for (i = 0; i < num_lmbs; i++) {
274 if (lmbs[i].flags & DRCONF_MEM_ASSIGNED)
278 if (lmbs_available < lmbs_to_remove)
281 for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
282 rc = dlpar_remove_lmb(&lmbs[i]);
288 /* Mark this lmb so we can add it later if all of the
289 * requested LMBs cannot be removed.
291 lmbs[i].reserved = 1;
294 if (lmbs_removed != lmbs_to_remove) {
295 pr_err("Memory hot-remove failed, adding LMB's back\n");
297 for (i = 0; i < num_lmbs; i++) {
298 if (!lmbs[i].reserved)
301 rc = dlpar_add_lmb(&lmbs[i]);
303 pr_err("Failed to add LMB back, drc index %x\n",
306 lmbs[i].reserved = 0;
311 for (i = 0; i < num_lmbs; i++) {
312 if (!lmbs[i].reserved)
315 pr_info("Memory at %llx was hot-removed\n",
318 lmbs[i].reserved = 0;
326 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
328 struct of_drconf_cell *lmbs;
333 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
337 lmbs = (struct of_drconf_cell *)p;
340 for (i = 0; i < num_lmbs; i++) {
341 if (lmbs[i].drc_index == drc_index) {
343 rc = dlpar_remove_lmb(&lmbs[i]);
352 pr_info("Failed to hot-remove memory at %llx\n",
355 pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
361 static inline int pseries_remove_memblock(unsigned long base,
362 unsigned int memblock_size)
366 static inline int pseries_remove_mem_node(struct device_node *np)
370 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
374 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
378 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
379 struct property *prop)
383 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
388 #endif /* CONFIG_MEMORY_HOTREMOVE */
390 static int dlpar_add_lmb(struct of_drconf_cell *lmb)
392 struct memory_block *mem_block;
393 unsigned long block_sz;
396 if (lmb->flags & DRCONF_MEM_ASSIGNED)
399 block_sz = memory_block_size_bytes();
401 rc = dlpar_acquire_drc(lmb->drc_index);
405 /* Find the node id for this address */
406 nid = memory_add_physaddr_to_nid(lmb->base_addr);
409 rc = add_memory(nid, lmb->base_addr, block_sz);
411 dlpar_release_drc(lmb->drc_index);
415 /* Register this block of memory */
416 rc = memblock_add(lmb->base_addr, block_sz);
418 remove_memory(nid, lmb->base_addr, block_sz);
419 dlpar_release_drc(lmb->drc_index);
423 mem_block = lmb_to_memblock(lmb);
425 remove_memory(nid, lmb->base_addr, block_sz);
426 dlpar_release_drc(lmb->drc_index);
430 rc = device_online(&mem_block->dev);
431 put_device(&mem_block->dev);
433 remove_memory(nid, lmb->base_addr, block_sz);
434 dlpar_release_drc(lmb->drc_index);
438 lmb->flags |= DRCONF_MEM_ASSIGNED;
442 static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
444 struct of_drconf_cell *lmbs;
446 int lmbs_available = 0;
450 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
452 if (lmbs_to_add == 0)
457 lmbs = (struct of_drconf_cell *)p;
459 /* Validate that there are enough LMBs to satisfy the request */
460 for (i = 0; i < num_lmbs; i++) {
461 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
465 if (lmbs_available < lmbs_to_add)
468 for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
469 rc = dlpar_add_lmb(&lmbs[i]);
475 /* Mark this lmb so we can remove it later if all of the
476 * requested LMBs cannot be added.
478 lmbs[i].reserved = 1;
481 if (lmbs_added != lmbs_to_add) {
482 pr_err("Memory hot-add failed, removing any added LMBs\n");
484 for (i = 0; i < num_lmbs; i++) {
485 if (!lmbs[i].reserved)
488 rc = dlpar_remove_lmb(&lmbs[i]);
490 pr_err("Failed to remove LMB, drc index %x\n",
491 be32_to_cpu(lmbs[i].drc_index));
495 for (i = 0; i < num_lmbs; i++) {
496 if (!lmbs[i].reserved)
499 pr_info("Memory at %llx (drc index %x) was hot-added\n",
500 lmbs[i].base_addr, lmbs[i].drc_index);
501 lmbs[i].reserved = 0;
508 static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
510 struct of_drconf_cell *lmbs;
515 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
519 lmbs = (struct of_drconf_cell *)p;
522 for (i = 0; i < num_lmbs; i++) {
523 if (lmbs[i].drc_index == drc_index) {
525 rc = dlpar_add_lmb(&lmbs[i]);
534 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
536 pr_info("Memory at %llx (drc index %x) was hot-added\n",
537 lmbs[i].base_addr, drc_index);
542 static void dlpar_update_drconf_property(struct device_node *dn,
543 struct property *prop)
545 struct of_drconf_cell *lmbs;
549 /* Convert the property back to BE */
552 *p = cpu_to_be32(*p);
555 lmbs = (struct of_drconf_cell *)p;
556 for (i = 0; i < num_lmbs; i++) {
557 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
558 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
559 lmbs[i].aa_index = cpu_to_be32(lmbs[i].aa_index);
560 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
563 rtas_hp_event = true;
564 of_update_property(dn, prop);
565 rtas_hp_event = false;
568 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
570 struct device_node *dn;
571 struct property *prop;
572 u32 count, drc_index;
575 count = hp_elog->_drc_u.drc_count;
576 drc_index = hp_elog->_drc_u.drc_index;
578 lock_device_hotplug();
580 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
583 goto dlpar_memory_out;
586 prop = dlpar_clone_drconf_property(dn);
589 goto dlpar_memory_out;
592 switch (hp_elog->action) {
593 case PSERIES_HP_ELOG_ACTION_ADD:
594 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
595 rc = dlpar_memory_add_by_count(count, prop);
596 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
597 rc = dlpar_memory_add_by_index(drc_index, prop);
601 case PSERIES_HP_ELOG_ACTION_REMOVE:
602 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
603 rc = dlpar_memory_remove_by_count(count, prop);
604 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
605 rc = dlpar_memory_remove_by_index(drc_index, prop);
610 pr_err("Invalid action (%d) specified\n", hp_elog->action);
616 dlpar_free_drconf_property(prop);
618 dlpar_update_drconf_property(dn, prop);
622 unlock_device_hotplug();
626 static int pseries_add_mem_node(struct device_node *np)
631 unsigned int lmb_size;
635 * Check to see if we are actually adding memory
637 type = of_get_property(np, "device_type", NULL);
638 if (type == NULL || strcmp(type, "memory") != 0)
642 * Find the base and size of the memblock
644 regs = of_get_property(np, "reg", NULL);
648 base = be64_to_cpu(*(unsigned long *)regs);
649 lmb_size = be32_to_cpu(regs[3]);
652 * Update memory region to represent the memory add
654 ret = memblock_add(base, lmb_size);
655 return (ret < 0) ? -EINVAL : 0;
658 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
660 struct of_drconf_cell *new_drmem, *old_drmem;
661 unsigned long memblock_size;
669 memblock_size = pseries_memory_block_size();
673 p = (__be32 *) pr->old_prop->value;
677 /* The first int of the property is the number of lmb's described
678 * by the property. This is followed by an array of of_drconf_cell
679 * entries. Get the number of entries and skip to the array of
682 entries = be32_to_cpu(*p++);
683 old_drmem = (struct of_drconf_cell *)p;
685 p = (__be32 *)pr->prop->value;
687 new_drmem = (struct of_drconf_cell *)p;
689 for (i = 0; i < entries; i++) {
690 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
691 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
692 rc = pseries_remove_memblock(
693 be64_to_cpu(old_drmem[i].base_addr),
696 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
697 DRCONF_MEM_ASSIGNED)) &&
698 (be32_to_cpu(new_drmem[i].flags) &
699 DRCONF_MEM_ASSIGNED)) {
700 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
702 rc = (rc < 0) ? -EINVAL : 0;
709 static int pseries_memory_notifier(struct notifier_block *nb,
710 unsigned long action, void *data)
712 struct of_reconfig_data *rd = data;
716 case OF_RECONFIG_ATTACH_NODE:
717 err = pseries_add_mem_node(rd->dn);
719 case OF_RECONFIG_DETACH_NODE:
720 err = pseries_remove_mem_node(rd->dn);
722 case OF_RECONFIG_UPDATE_PROPERTY:
723 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
724 err = pseries_update_drconf_memory(rd);
727 return notifier_from_errno(err);
730 static struct notifier_block pseries_mem_nb = {
731 .notifier_call = pseries_memory_notifier,
734 static int __init pseries_memory_hotplug_init(void)
736 if (firmware_has_feature(FW_FEATURE_LPAR))
737 of_reconfig_notifier_register(&pseries_mem_nb);
741 machine_device_initcall(pseries, pseries_memory_hotplug_init);