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_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].flags = be32_to_cpu(lmbs[i].flags);
133 static void dlpar_update_drconf_property(struct device_node *dn,
134 struct property *prop)
136 struct of_drconf_cell *lmbs;
140 /* Convert the property back to BE */
143 *p = cpu_to_be32(*p);
146 lmbs = (struct of_drconf_cell *)p;
147 for (i = 0; i < num_lmbs; i++) {
148 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
149 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
150 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
153 rtas_hp_event = true;
154 of_update_property(dn, prop);
155 rtas_hp_event = false;
158 static int dlpar_update_device_tree_lmb(struct of_drconf_cell *lmb)
160 struct device_node *dn;
161 struct property *prop;
162 struct of_drconf_cell *lmbs;
166 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
170 prop = dlpar_clone_drconf_property(dn);
178 lmbs = (struct of_drconf_cell *)p;
180 for (i = 0; i < num_lmbs; i++) {
181 if (lmbs[i].drc_index == lmb->drc_index) {
182 lmbs[i].flags = lmb->flags;
183 lmbs[i].aa_index = lmb->aa_index;
185 dlpar_update_drconf_property(dn, prop);
194 static u32 find_aa_index(struct device_node *dr_node,
195 struct property *ala_prop, const u32 *lmb_assoc)
199 int aa_arrays, aa_array_entries, aa_array_sz;
203 * The ibm,associativity-lookup-arrays property is defined to be
204 * a 32-bit value specifying the number of associativity arrays
205 * followed by a 32-bitvalue specifying the number of entries per
206 * array, followed by the associativity arrays.
208 assoc_arrays = ala_prop->value;
210 aa_arrays = be32_to_cpu(assoc_arrays[0]);
211 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
212 aa_array_sz = aa_array_entries * sizeof(u32);
215 for (i = 0; i < aa_arrays; i++) {
216 index = (i * aa_array_entries) + 2;
218 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
225 if (aa_index == -1) {
226 struct property *new_prop;
229 new_prop_size = ala_prop->length + aa_array_sz;
230 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
234 assoc_arrays = new_prop->value;
236 /* increment the number of entries in the lookup array */
237 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
239 /* copy the new associativity into the lookup array */
240 index = aa_arrays * aa_array_entries + 2;
241 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
243 of_update_property(dr_node, new_prop);
246 * The associativity lookup array index for this lmb is
247 * number of entries - 1 since we added its associativity
248 * to the end of the lookup array.
250 aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
256 static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb)
258 struct device_node *parent, *lmb_node, *dr_node;
259 struct property *ala_prop;
260 const u32 *lmb_assoc;
263 parent = of_find_node_by_path("/");
267 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
273 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
275 dlpar_free_cc_nodes(lmb_node);
279 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
281 dlpar_free_cc_nodes(lmb_node);
285 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
288 of_node_put(dr_node);
289 dlpar_free_cc_nodes(lmb_node);
293 aa_index = find_aa_index(dr_node, ala_prop, lmb_assoc);
295 dlpar_free_cc_nodes(lmb_node);
299 static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb)
303 lmb->flags |= DRCONF_MEM_ASSIGNED;
305 aa_index = lookup_lmb_associativity_index(lmb);
307 pr_err("Couldn't find associativity index for drc index %x\n",
312 lmb->aa_index = aa_index;
313 return dlpar_update_device_tree_lmb(lmb);
316 static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb)
318 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
319 lmb->aa_index = 0xffffffff;
320 return dlpar_update_device_tree_lmb(lmb);
323 #ifdef CONFIG_MEMORY_HOTREMOVE
324 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
326 unsigned long block_sz, start_pfn;
327 int sections_per_block;
330 start_pfn = base >> PAGE_SHIFT;
332 lock_device_hotplug();
334 if (!pfn_valid(start_pfn))
337 block_sz = pseries_memory_block_size();
338 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
339 nid = memory_add_physaddr_to_nid(base);
341 for (i = 0; i < sections_per_block; i++) {
342 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
343 base += MIN_MEMORY_BLOCK_SIZE;
347 /* Update memory regions for memory remove */
348 memblock_remove(base, memblock_size);
349 unlock_device_hotplug();
353 static int pseries_remove_mem_node(struct device_node *np)
358 unsigned int lmb_size;
362 * Check to see if we are actually removing memory
364 type = of_get_property(np, "device_type", NULL);
365 if (type == NULL || strcmp(type, "memory") != 0)
369 * Find the base address and size of the memblock
371 regs = of_get_property(np, "reg", NULL);
375 base = be64_to_cpu(*(unsigned long *)regs);
376 lmb_size = be32_to_cpu(regs[3]);
378 pseries_remove_memblock(base, lmb_size);
382 static bool lmb_is_removable(struct of_drconf_cell *lmb)
384 int i, scns_per_block;
386 unsigned long pfn, block_sz;
389 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
392 block_sz = memory_block_size_bytes();
393 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
394 phys_addr = lmb->base_addr;
396 for (i = 0; i < scns_per_block; i++) {
397 pfn = PFN_DOWN(phys_addr);
398 if (!pfn_present(pfn))
401 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
402 phys_addr += MIN_MEMORY_BLOCK_SIZE;
405 return rc ? true : false;
408 static int dlpar_add_lmb(struct of_drconf_cell *);
410 static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
412 unsigned long section_nr;
413 struct mem_section *mem_sect;
414 struct memory_block *mem_block;
416 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
417 mem_sect = __nr_to_section(section_nr);
419 mem_block = find_memory_block(mem_sect);
423 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
425 struct memory_block *mem_block;
426 unsigned long block_sz;
429 if (!lmb_is_removable(lmb))
432 mem_block = lmb_to_memblock(lmb);
436 rc = device_offline(&mem_block->dev);
437 put_device(&mem_block->dev);
441 block_sz = pseries_memory_block_size();
442 nid = memory_add_physaddr_to_nid(lmb->base_addr);
444 remove_memory(nid, lmb->base_addr, block_sz);
446 /* Update memory regions for memory remove */
447 memblock_remove(lmb->base_addr, block_sz);
449 dlpar_remove_device_tree_lmb(lmb);
453 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
454 struct property *prop)
456 struct of_drconf_cell *lmbs;
457 int lmbs_removed = 0;
458 int lmbs_available = 0;
462 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
464 if (lmbs_to_remove == 0)
469 lmbs = (struct of_drconf_cell *)p;
471 /* Validate that there are enough LMBs to satisfy the request */
472 for (i = 0; i < num_lmbs; i++) {
473 if (lmb_is_removable(&lmbs[i]))
477 if (lmbs_available < lmbs_to_remove) {
478 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
479 lmbs_available, lmbs_to_remove);
483 for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
484 rc = dlpar_remove_lmb(&lmbs[i]);
490 /* Mark this lmb so we can add it later if all of the
491 * requested LMBs cannot be removed.
493 lmbs[i].reserved = 1;
496 if (lmbs_removed != lmbs_to_remove) {
497 pr_err("Memory hot-remove failed, adding LMB's back\n");
499 for (i = 0; i < num_lmbs; i++) {
500 if (!lmbs[i].reserved)
503 rc = dlpar_add_lmb(&lmbs[i]);
505 pr_err("Failed to add LMB back, drc index %x\n",
508 lmbs[i].reserved = 0;
513 for (i = 0; i < num_lmbs; i++) {
514 if (!lmbs[i].reserved)
517 dlpar_release_drc(lmbs[i].drc_index);
518 pr_info("Memory at %llx was hot-removed\n",
521 lmbs[i].reserved = 0;
529 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
531 struct of_drconf_cell *lmbs;
536 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
540 lmbs = (struct of_drconf_cell *)p;
543 for (i = 0; i < num_lmbs; i++) {
544 if (lmbs[i].drc_index == drc_index) {
546 rc = dlpar_remove_lmb(&lmbs[i]);
548 dlpar_release_drc(lmbs[i].drc_index);
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);
566 static int dlpar_memory_readd_by_index(u32 drc_index, struct property *prop)
568 struct of_drconf_cell *lmbs;
573 pr_info("Attempting to update LMB, drc index %x\n", drc_index);
577 lmbs = (struct of_drconf_cell *)p;
580 for (i = 0; i < num_lmbs; i++) {
581 if (lmbs[i].drc_index == drc_index) {
583 rc = dlpar_remove_lmb(&lmbs[i]);
585 rc = dlpar_add_lmb(&lmbs[i]);
587 dlpar_release_drc(lmbs[i].drc_index);
597 pr_info("Failed to update memory at %llx\n",
600 pr_info("Memory at %llx was updated\n", lmbs[i].base_addr);
605 static inline int pseries_remove_memblock(unsigned long base,
606 unsigned int memblock_size)
610 static inline int pseries_remove_mem_node(struct device_node *np)
614 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
618 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
622 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
623 struct property *prop)
627 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
632 #endif /* CONFIG_MEMORY_HOTREMOVE */
634 static int dlpar_add_lmb(struct of_drconf_cell *lmb)
636 unsigned long block_sz;
639 if (lmb->flags & DRCONF_MEM_ASSIGNED)
642 rc = dlpar_add_device_tree_lmb(lmb);
644 pr_err("Couldn't update device tree for drc index %x\n",
646 dlpar_release_drc(lmb->drc_index);
650 block_sz = memory_block_size_bytes();
652 /* Find the node id for this address */
653 nid = memory_add_physaddr_to_nid(lmb->base_addr);
656 rc = add_memory(nid, lmb->base_addr, block_sz);
658 dlpar_remove_device_tree_lmb(lmb);
660 lmb->flags |= DRCONF_MEM_ASSIGNED;
665 static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
667 struct of_drconf_cell *lmbs;
669 int lmbs_available = 0;
673 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
675 if (lmbs_to_add == 0)
680 lmbs = (struct of_drconf_cell *)p;
682 /* Validate that there are enough LMBs to satisfy the request */
683 for (i = 0; i < num_lmbs; i++) {
684 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
688 if (lmbs_available < lmbs_to_add)
691 for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
692 rc = dlpar_acquire_drc(lmbs[i].drc_index);
696 rc = dlpar_add_lmb(&lmbs[i]);
698 dlpar_release_drc(lmbs[i].drc_index);
704 /* Mark this lmb so we can remove it later if all of the
705 * requested LMBs cannot be added.
707 lmbs[i].reserved = 1;
710 if (lmbs_added != lmbs_to_add) {
711 pr_err("Memory hot-add failed, removing any added LMBs\n");
713 for (i = 0; i < num_lmbs; i++) {
714 if (!lmbs[i].reserved)
717 rc = dlpar_remove_lmb(&lmbs[i]);
719 pr_err("Failed to remove LMB, drc index %x\n",
720 be32_to_cpu(lmbs[i].drc_index));
722 dlpar_release_drc(lmbs[i].drc_index);
726 for (i = 0; i < num_lmbs; i++) {
727 if (!lmbs[i].reserved)
730 pr_info("Memory at %llx (drc index %x) was hot-added\n",
731 lmbs[i].base_addr, lmbs[i].drc_index);
732 lmbs[i].reserved = 0;
739 static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
741 struct of_drconf_cell *lmbs;
746 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
750 lmbs = (struct of_drconf_cell *)p;
753 for (i = 0; i < num_lmbs; i++) {
754 if (lmbs[i].drc_index == drc_index) {
756 rc = dlpar_acquire_drc(lmbs[i].drc_index);
758 rc = dlpar_add_lmb(&lmbs[i]);
760 dlpar_release_drc(lmbs[i].drc_index);
771 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
773 pr_info("Memory at %llx (drc index %x) was hot-added\n",
774 lmbs[i].base_addr, drc_index);
779 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
781 struct device_node *dn;
782 struct property *prop;
783 u32 count, drc_index;
786 count = hp_elog->_drc_u.drc_count;
787 drc_index = hp_elog->_drc_u.drc_index;
789 lock_device_hotplug();
791 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
794 goto dlpar_memory_out;
797 prop = dlpar_clone_drconf_property(dn);
800 goto dlpar_memory_out;
803 switch (hp_elog->action) {
804 case PSERIES_HP_ELOG_ACTION_ADD:
805 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
806 rc = dlpar_memory_add_by_count(count, prop);
807 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
808 rc = dlpar_memory_add_by_index(drc_index, prop);
812 case PSERIES_HP_ELOG_ACTION_REMOVE:
813 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT)
814 rc = dlpar_memory_remove_by_count(count, prop);
815 else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX)
816 rc = dlpar_memory_remove_by_index(drc_index, prop);
820 case PSERIES_HP_ELOG_ACTION_READD:
821 rc = dlpar_memory_readd_by_index(drc_index, prop);
824 pr_err("Invalid action (%d) specified\n", hp_elog->action);
829 dlpar_free_property(prop);
833 unlock_device_hotplug();
837 static int pseries_add_mem_node(struct device_node *np)
842 unsigned int lmb_size;
846 * Check to see if we are actually adding memory
848 type = of_get_property(np, "device_type", NULL);
849 if (type == NULL || strcmp(type, "memory") != 0)
853 * Find the base and size of the memblock
855 regs = of_get_property(np, "reg", NULL);
859 base = be64_to_cpu(*(unsigned long *)regs);
860 lmb_size = be32_to_cpu(regs[3]);
863 * Update memory region to represent the memory add
865 ret = memblock_add(base, lmb_size);
866 return (ret < 0) ? -EINVAL : 0;
869 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
871 struct of_drconf_cell *new_drmem, *old_drmem;
872 unsigned long memblock_size;
880 memblock_size = pseries_memory_block_size();
884 p = (__be32 *) pr->old_prop->value;
888 /* The first int of the property is the number of lmb's described
889 * by the property. This is followed by an array of of_drconf_cell
890 * entries. Get the number of entries and skip to the array of
893 entries = be32_to_cpu(*p++);
894 old_drmem = (struct of_drconf_cell *)p;
896 p = (__be32 *)pr->prop->value;
898 new_drmem = (struct of_drconf_cell *)p;
900 for (i = 0; i < entries; i++) {
901 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
902 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
903 rc = pseries_remove_memblock(
904 be64_to_cpu(old_drmem[i].base_addr),
907 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
908 DRCONF_MEM_ASSIGNED)) &&
909 (be32_to_cpu(new_drmem[i].flags) &
910 DRCONF_MEM_ASSIGNED)) {
911 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
913 rc = (rc < 0) ? -EINVAL : 0;
920 static int pseries_memory_notifier(struct notifier_block *nb,
921 unsigned long action, void *data)
923 struct of_reconfig_data *rd = data;
927 case OF_RECONFIG_ATTACH_NODE:
928 err = pseries_add_mem_node(rd->dn);
930 case OF_RECONFIG_DETACH_NODE:
931 err = pseries_remove_mem_node(rd->dn);
933 case OF_RECONFIG_UPDATE_PROPERTY:
934 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
935 err = pseries_update_drconf_memory(rd);
938 return notifier_from_errno(err);
941 static struct notifier_block pseries_mem_nb = {
942 .notifier_call = pseries_memory_notifier,
945 static int __init pseries_memory_hotplug_init(void)
947 if (firmware_has_feature(FW_FEATURE_LPAR))
948 of_reconfig_notifier_register(&pseries_mem_nb);
952 machine_device_initcall(pseries, pseries_memory_hotplug_init);