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 u64 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_property(struct property *prop)
79 static struct property *dlpar_clone_property(struct property *prop,
82 struct property *new_prop;
84 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
88 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
89 new_prop->value = kzalloc(prop_size, GFP_KERNEL);
90 if (!new_prop->name || !new_prop->value) {
91 dlpar_free_property(new_prop);
95 memcpy(new_prop->value, prop->value, prop->length);
96 new_prop->length = prop_size;
98 of_property_set_flag(new_prop, OF_DYNAMIC);
102 static struct property *dlpar_clone_drconf_property(struct device_node *dn)
104 struct property *prop, *new_prop;
105 struct of_drconf_cell *lmbs;
109 prop = of_find_property(dn, "ibm,dynamic-memory", NULL);
113 new_prop = dlpar_clone_property(prop, prop->length);
117 /* Convert the property to cpu endian-ness */
119 *p = be32_to_cpu(*p);
122 lmbs = (struct of_drconf_cell *)p;
124 for (i = 0; i < num_lmbs; i++) {
125 lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr);
126 lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index);
127 lmbs[i].aa_index = be32_to_cpu(lmbs[i].aa_index);
128 lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
134 static void dlpar_update_drconf_property(struct device_node *dn,
135 struct property *prop)
137 struct of_drconf_cell *lmbs;
141 /* Convert the property back to BE */
144 *p = cpu_to_be32(*p);
147 lmbs = (struct of_drconf_cell *)p;
148 for (i = 0; i < num_lmbs; i++) {
149 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
150 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
151 lmbs[i].aa_index = cpu_to_be32(lmbs[i].aa_index);
152 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
155 rtas_hp_event = true;
156 of_update_property(dn, prop);
157 rtas_hp_event = false;
160 static int dlpar_update_device_tree_lmb(struct of_drconf_cell *lmb)
162 struct device_node *dn;
163 struct property *prop;
164 struct of_drconf_cell *lmbs;
168 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
172 prop = dlpar_clone_drconf_property(dn);
180 lmbs = (struct of_drconf_cell *)p;
182 for (i = 0; i < num_lmbs; i++) {
183 if (lmbs[i].drc_index == lmb->drc_index) {
184 lmbs[i].flags = lmb->flags;
185 lmbs[i].aa_index = lmb->aa_index;
187 dlpar_update_drconf_property(dn, prop);
196 static u32 find_aa_index(struct device_node *dr_node,
197 struct property *ala_prop, const u32 *lmb_assoc)
201 int aa_arrays, aa_array_entries, aa_array_sz;
205 * The ibm,associativity-lookup-arrays property is defined to be
206 * a 32-bit value specifying the number of associativity arrays
207 * followed by a 32-bitvalue specifying the number of entries per
208 * array, followed by the associativity arrays.
210 assoc_arrays = ala_prop->value;
212 aa_arrays = be32_to_cpu(assoc_arrays[0]);
213 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
214 aa_array_sz = aa_array_entries * sizeof(u32);
217 for (i = 0; i < aa_arrays; i++) {
218 index = (i * aa_array_entries) + 2;
220 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
227 if (aa_index == -1) {
228 struct property *new_prop;
231 new_prop_size = ala_prop->length + aa_array_sz;
232 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
236 assoc_arrays = new_prop->value;
238 /* increment the number of entries in the lookup array */
239 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
241 /* copy the new associativity into the lookup array */
242 index = aa_arrays * aa_array_entries + 2;
243 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
245 of_update_property(dr_node, new_prop);
248 * The associativity lookup array index for this lmb is
249 * number of entries - 1 since we added its associativity
250 * to the end of the lookup array.
252 aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
258 static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb)
260 struct device_node *parent, *lmb_node, *dr_node;
261 struct property *ala_prop;
262 const u32 *lmb_assoc;
265 parent = of_find_node_by_path("/");
269 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
275 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
277 dlpar_free_cc_nodes(lmb_node);
281 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
283 dlpar_free_cc_nodes(lmb_node);
287 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
290 of_node_put(dr_node);
291 dlpar_free_cc_nodes(lmb_node);
295 aa_index = find_aa_index(dr_node, ala_prop, lmb_assoc);
297 of_node_put(dr_node);
298 dlpar_free_cc_nodes(lmb_node);
302 static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb)
306 lmb->flags |= DRCONF_MEM_ASSIGNED;
308 aa_index = lookup_lmb_associativity_index(lmb);
310 pr_err("Couldn't find associativity index for drc index %x\n",
315 lmb->aa_index = aa_index;
316 return dlpar_update_device_tree_lmb(lmb);
319 static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb)
321 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
322 lmb->aa_index = 0xffffffff;
323 return dlpar_update_device_tree_lmb(lmb);
326 #ifdef CONFIG_MEMORY_HOTREMOVE
327 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
329 unsigned long block_sz, start_pfn;
330 int sections_per_block;
333 start_pfn = base >> PAGE_SHIFT;
335 lock_device_hotplug();
337 if (!pfn_valid(start_pfn))
340 block_sz = pseries_memory_block_size();
341 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
342 nid = memory_add_physaddr_to_nid(base);
344 for (i = 0; i < sections_per_block; i++) {
345 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
346 base += MIN_MEMORY_BLOCK_SIZE;
350 /* Update memory regions for memory remove */
351 memblock_remove(base, memblock_size);
352 unlock_device_hotplug();
356 static int pseries_remove_mem_node(struct device_node *np)
361 unsigned int lmb_size;
365 * Check to see if we are actually removing memory
367 type = of_get_property(np, "device_type", NULL);
368 if (type == NULL || strcmp(type, "memory") != 0)
372 * Find the base address and size of the memblock
374 regs = of_get_property(np, "reg", NULL);
378 base = be64_to_cpu(*(unsigned long *)regs);
379 lmb_size = be32_to_cpu(regs[3]);
381 pseries_remove_memblock(base, lmb_size);
385 static bool lmb_is_removable(struct of_drconf_cell *lmb)
387 int i, scns_per_block;
389 unsigned long pfn, block_sz;
392 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
395 block_sz = memory_block_size_bytes();
396 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
397 phys_addr = lmb->base_addr;
399 for (i = 0; i < scns_per_block; i++) {
400 pfn = PFN_DOWN(phys_addr);
401 if (!pfn_present(pfn)) {
402 phys_addr += MIN_MEMORY_BLOCK_SIZE;
406 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
407 phys_addr += MIN_MEMORY_BLOCK_SIZE;
410 return rc ? true : false;
413 static int dlpar_add_lmb(struct of_drconf_cell *);
415 static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
417 unsigned long section_nr;
418 struct mem_section *mem_sect;
419 struct memory_block *mem_block;
421 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
422 mem_sect = __nr_to_section(section_nr);
424 mem_block = find_memory_block(mem_sect);
428 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
430 struct memory_block *mem_block;
431 unsigned long block_sz;
434 if (!lmb_is_removable(lmb))
437 mem_block = lmb_to_memblock(lmb);
441 rc = device_offline(&mem_block->dev);
442 put_device(&mem_block->dev);
446 block_sz = pseries_memory_block_size();
447 nid = memory_add_physaddr_to_nid(lmb->base_addr);
449 remove_memory(nid, lmb->base_addr, block_sz);
451 /* Update memory regions for memory remove */
452 memblock_remove(lmb->base_addr, block_sz);
454 dlpar_release_drc(lmb->drc_index);
455 dlpar_remove_device_tree_lmb(lmb);
460 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
461 struct property *prop)
463 struct of_drconf_cell *lmbs;
464 int lmbs_removed = 0;
465 int lmbs_available = 0;
469 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
471 if (lmbs_to_remove == 0)
476 lmbs = (struct of_drconf_cell *)p;
478 /* Validate that there are enough LMBs to satisfy the request */
479 for (i = 0; i < num_lmbs; i++) {
480 if (lmbs[i].flags & DRCONF_MEM_ASSIGNED)
484 if (lmbs_available < lmbs_to_remove)
487 for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
488 rc = dlpar_remove_lmb(&lmbs[i]);
494 /* Mark this lmb so we can add it later if all of the
495 * requested LMBs cannot be removed.
497 lmbs[i].reserved = 1;
500 if (lmbs_removed != lmbs_to_remove) {
501 pr_err("Memory hot-remove failed, adding LMB's back\n");
503 for (i = 0; i < num_lmbs; i++) {
504 if (!lmbs[i].reserved)
507 rc = dlpar_add_lmb(&lmbs[i]);
509 pr_err("Failed to add LMB back, drc index %x\n",
512 lmbs[i].reserved = 0;
517 for (i = 0; i < num_lmbs; i++) {
518 if (!lmbs[i].reserved)
521 pr_info("Memory at %llx was hot-removed\n",
524 lmbs[i].reserved = 0;
532 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
534 struct of_drconf_cell *lmbs;
539 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
543 lmbs = (struct of_drconf_cell *)p;
546 for (i = 0; i < num_lmbs; i++) {
547 if (lmbs[i].drc_index == drc_index) {
549 rc = dlpar_remove_lmb(&lmbs[i]);
558 pr_info("Failed to hot-remove memory at %llx\n",
561 pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
567 static inline int pseries_remove_memblock(unsigned long base,
568 unsigned int memblock_size)
572 static inline int pseries_remove_mem_node(struct device_node *np)
576 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
580 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
584 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
585 struct property *prop)
589 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
594 #endif /* CONFIG_MEMORY_HOTREMOVE */
596 static int dlpar_add_lmb(struct of_drconf_cell *lmb)
598 unsigned long block_sz;
601 if (lmb->flags & DRCONF_MEM_ASSIGNED)
604 rc = dlpar_acquire_drc(lmb->drc_index);
608 rc = dlpar_add_device_tree_lmb(lmb);
610 pr_err("Couldn't update device tree for drc index %x\n",
612 dlpar_release_drc(lmb->drc_index);
616 block_sz = memory_block_size_bytes();
618 /* Find the node id for this address */
619 nid = memory_add_physaddr_to_nid(lmb->base_addr);
622 rc = __add_memory(nid, lmb->base_addr, block_sz);
624 dlpar_remove_device_tree_lmb(lmb);
625 dlpar_release_drc(lmb->drc_index);
627 lmb->flags |= DRCONF_MEM_ASSIGNED;
633 static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
635 struct of_drconf_cell *lmbs;
637 int lmbs_available = 0;
641 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
643 if (lmbs_to_add == 0)
648 lmbs = (struct of_drconf_cell *)p;
650 /* Validate that there are enough LMBs to satisfy the request */
651 for (i = 0; i < num_lmbs; i++) {
652 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
656 if (lmbs_available < lmbs_to_add)
659 for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
660 rc = dlpar_add_lmb(&lmbs[i]);
666 /* Mark this lmb so we can remove it later if all of the
667 * requested LMBs cannot be added.
669 lmbs[i].reserved = 1;
672 if (lmbs_added != lmbs_to_add) {
673 pr_err("Memory hot-add failed, removing any added LMBs\n");
675 for (i = 0; i < num_lmbs; i++) {
676 if (!lmbs[i].reserved)
679 rc = dlpar_remove_lmb(&lmbs[i]);
681 pr_err("Failed to remove LMB, drc index %x\n",
682 be32_to_cpu(lmbs[i].drc_index));
686 for (i = 0; i < num_lmbs; i++) {
687 if (!lmbs[i].reserved)
690 pr_info("Memory at %llx (drc index %x) was hot-added\n",
691 lmbs[i].base_addr, lmbs[i].drc_index);
692 lmbs[i].reserved = 0;
699 static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
701 struct of_drconf_cell *lmbs;
706 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
710 lmbs = (struct of_drconf_cell *)p;
713 for (i = 0; i < num_lmbs; i++) {
714 if (lmbs[i].drc_index == drc_index) {
716 rc = dlpar_add_lmb(&lmbs[i]);
725 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
727 pr_info("Memory at %llx (drc index %x) was hot-added\n",
728 lmbs[i].base_addr, drc_index);
733 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
735 struct device_node *dn;
736 struct property *prop;
737 u32 count, drc_index;
740 count = hp_elog->_drc_u.drc_count;
741 drc_index = hp_elog->_drc_u.drc_index;
743 lock_device_hotplug();
745 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
748 goto dlpar_memory_out;
751 prop = dlpar_clone_drconf_property(dn);
754 goto dlpar_memory_out;
757 switch (hp_elog->action) {
758 case PSERIES_HP_ELOG_ACTION_ADD:
759 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
760 rc = dlpar_memory_add_by_count(count, prop);
761 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
762 rc = dlpar_memory_add_by_index(drc_index, prop);
766 case PSERIES_HP_ELOG_ACTION_REMOVE:
767 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
768 rc = dlpar_memory_remove_by_count(count, prop);
769 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
770 rc = dlpar_memory_remove_by_index(drc_index, prop);
775 pr_err("Invalid action (%d) specified\n", hp_elog->action);
780 dlpar_free_property(prop);
784 unlock_device_hotplug();
788 static int pseries_add_mem_node(struct device_node *np)
793 unsigned int lmb_size;
797 * Check to see if we are actually adding memory
799 type = of_get_property(np, "device_type", NULL);
800 if (type == NULL || strcmp(type, "memory") != 0)
804 * Find the base and size of the memblock
806 regs = of_get_property(np, "reg", NULL);
810 base = be64_to_cpu(*(unsigned long *)regs);
811 lmb_size = be32_to_cpu(regs[3]);
814 * Update memory region to represent the memory add
816 ret = memblock_add(base, lmb_size);
817 return (ret < 0) ? -EINVAL : 0;
820 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
822 struct of_drconf_cell *new_drmem, *old_drmem;
823 unsigned long memblock_size;
831 memblock_size = pseries_memory_block_size();
835 p = (__be32 *) pr->old_prop->value;
839 /* The first int of the property is the number of lmb's described
840 * by the property. This is followed by an array of of_drconf_cell
841 * entries. Get the number of entries and skip to the array of
844 entries = be32_to_cpu(*p++);
845 old_drmem = (struct of_drconf_cell *)p;
847 p = (__be32 *)pr->prop->value;
849 new_drmem = (struct of_drconf_cell *)p;
851 for (i = 0; i < entries; i++) {
852 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
853 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
854 rc = pseries_remove_memblock(
855 be64_to_cpu(old_drmem[i].base_addr),
858 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
859 DRCONF_MEM_ASSIGNED)) &&
860 (be32_to_cpu(new_drmem[i].flags) &
861 DRCONF_MEM_ASSIGNED)) {
862 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
864 rc = (rc < 0) ? -EINVAL : 0;
871 static int pseries_memory_notifier(struct notifier_block *nb,
872 unsigned long action, void *data)
874 struct of_reconfig_data *rd = data;
878 case OF_RECONFIG_ATTACH_NODE:
879 err = pseries_add_mem_node(rd->dn);
881 case OF_RECONFIG_DETACH_NODE:
882 err = pseries_remove_mem_node(rd->dn);
884 case OF_RECONFIG_UPDATE_PROPERTY:
885 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
886 err = pseries_update_drconf_memory(rd);
889 return notifier_from_errno(err);
892 static struct notifier_block pseries_mem_nb = {
893 .notifier_call = pseries_memory_notifier,
896 static int __init pseries_memory_hotplug_init(void)
898 if (firmware_has_feature(FW_FEATURE_LPAR))
899 of_reconfig_notifier_register(&pseries_mem_nb);
903 machine_device_initcall(pseries, pseries_memory_hotplug_init);