1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * pseries Memory Hotplug infrastructure.
5 * Copyright (C) 2008 Badari Pulavarty, IBM Corporation
8 #define pr_fmt(fmt) "pseries-hotplug-mem: " fmt
11 #include <linux/of_address.h>
12 #include <linux/memblock.h>
13 #include <linux/memory.h>
14 #include <linux/memory_hotplug.h>
15 #include <linux/slab.h>
17 #include <asm/firmware.h>
18 #include <asm/machdep.h>
20 #include <asm/sparsemem.h>
21 #include <asm/fadump.h>
22 #include <asm/drmem.h>
25 unsigned long pseries_memory_block_size(void)
27 struct device_node *np;
28 u64 memblock_size = MIN_MEMORY_BLOCK_SIZE;
31 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
37 size_cells = of_n_size_cells(np);
39 prop = of_get_property(np, "ibm,lmb-size", &len);
40 if (prop && len >= size_cells * sizeof(__be32))
41 memblock_size = of_read_number(prop, size_cells);
44 } else if (machine_is(pseries)) {
45 /* This fallback really only applies to pseries */
46 unsigned int memzero_size = 0;
48 np = of_find_node_by_path("/memory@0");
50 if (!of_address_to_resource(np, 0, &r))
51 memzero_size = resource_size(&r);
56 /* We now know the size of memory@0, use this to find
57 * the first memoryblock and get its size.
61 sprintf(buf, "/memory@%x", memzero_size);
62 np = of_find_node_by_path(buf);
64 if (!of_address_to_resource(np, 0, &r))
65 memblock_size = resource_size(&r);
73 static void dlpar_free_property(struct property *prop)
80 static struct property *dlpar_clone_property(struct property *prop,
83 struct property *new_prop;
85 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
89 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
90 new_prop->value = kzalloc(prop_size, GFP_KERNEL);
91 if (!new_prop->name || !new_prop->value) {
92 dlpar_free_property(new_prop);
96 memcpy(new_prop->value, prop->value, prop->length);
97 new_prop->length = prop_size;
99 of_property_set_flag(new_prop, OF_DYNAMIC);
103 static bool find_aa_index(struct device_node *dr_node,
104 struct property *ala_prop,
105 const u32 *lmb_assoc, u32 *aa_index)
107 u32 *assoc_arrays, new_prop_size;
108 struct property *new_prop;
109 int aa_arrays, aa_array_entries, aa_array_sz;
113 * The ibm,associativity-lookup-arrays property is defined to be
114 * a 32-bit value specifying the number of associativity arrays
115 * followed by a 32-bitvalue specifying the number of entries per
116 * array, followed by the associativity arrays.
118 assoc_arrays = ala_prop->value;
120 aa_arrays = be32_to_cpu(assoc_arrays[0]);
121 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
122 aa_array_sz = aa_array_entries * sizeof(u32);
124 for (i = 0; i < aa_arrays; i++) {
125 index = (i * aa_array_entries) + 2;
127 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
134 new_prop_size = ala_prop->length + aa_array_sz;
135 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
139 assoc_arrays = new_prop->value;
141 /* increment the number of entries in the lookup array */
142 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
144 /* copy the new associativity into the lookup array */
145 index = aa_arrays * aa_array_entries + 2;
146 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
148 of_update_property(dr_node, new_prop);
151 * The associativity lookup array index for this lmb is
152 * number of entries - 1 since we added its associativity
153 * to the end of the lookup array.
155 *aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
159 static int update_lmb_associativity_index(struct drmem_lmb *lmb)
161 struct device_node *parent, *lmb_node, *dr_node;
162 struct property *ala_prop;
163 const u32 *lmb_assoc;
167 parent = of_find_node_by_path("/");
171 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
177 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
179 dlpar_free_cc_nodes(lmb_node);
183 update_numa_distance(lmb_node);
185 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
187 dlpar_free_cc_nodes(lmb_node);
191 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
194 of_node_put(dr_node);
195 dlpar_free_cc_nodes(lmb_node);
199 found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
201 of_node_put(dr_node);
202 dlpar_free_cc_nodes(lmb_node);
205 pr_err("Could not find LMB associativity\n");
209 lmb->aa_index = aa_index;
213 static struct memory_block *lmb_to_memblock(struct drmem_lmb *lmb)
215 unsigned long section_nr;
216 struct memory_block *mem_block;
218 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
220 mem_block = find_memory_block(section_nr);
224 static int get_lmb_range(u32 drc_index, int n_lmbs,
225 struct drmem_lmb **start_lmb,
226 struct drmem_lmb **end_lmb)
228 struct drmem_lmb *lmb, *start, *end;
229 struct drmem_lmb *limit;
232 for_each_drmem_lmb(lmb) {
233 if (lmb->drc_index == drc_index) {
242 end = &start[n_lmbs];
244 limit = &drmem_info->lmbs[drmem_info->n_lmbs];
253 static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
255 struct memory_block *mem_block;
258 mem_block = lmb_to_memblock(lmb);
262 if (online && mem_block->dev.offline)
263 rc = device_online(&mem_block->dev);
264 else if (!online && !mem_block->dev.offline)
265 rc = device_offline(&mem_block->dev);
269 put_device(&mem_block->dev);
274 static int dlpar_online_lmb(struct drmem_lmb *lmb)
276 return dlpar_change_lmb_state(lmb, true);
279 #ifdef CONFIG_MEMORY_HOTREMOVE
280 static int dlpar_offline_lmb(struct drmem_lmb *lmb)
282 return dlpar_change_lmb_state(lmb, false);
285 static int pseries_remove_memblock(unsigned long base, unsigned long memblock_size)
287 unsigned long block_sz, start_pfn;
288 int sections_per_block;
291 start_pfn = base >> PAGE_SHIFT;
293 lock_device_hotplug();
295 if (!pfn_valid(start_pfn))
298 block_sz = pseries_memory_block_size();
299 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
301 for (i = 0; i < sections_per_block; i++) {
302 __remove_memory(base, MIN_MEMORY_BLOCK_SIZE);
303 base += MIN_MEMORY_BLOCK_SIZE;
307 /* Update memory regions for memory remove */
308 memblock_remove(base, memblock_size);
309 unlock_device_hotplug();
313 static int pseries_remove_mem_node(struct device_node *np)
317 unsigned long lmb_size;
319 int addr_cells, size_cells;
322 * Check to see if we are actually removing memory
324 if (!of_node_is_type(np, "memory"))
328 * Find the base address and size of the memblock
330 prop = of_get_property(np, "reg", NULL);
334 addr_cells = of_n_addr_cells(np);
335 size_cells = of_n_size_cells(np);
338 * "reg" property represents (addr,size) tuple.
340 base = of_read_number(prop, addr_cells);
342 lmb_size = of_read_number(prop, size_cells);
344 pseries_remove_memblock(base, lmb_size);
348 static bool lmb_is_removable(struct drmem_lmb *lmb)
350 if ((lmb->flags & DRCONF_MEM_RESERVED) ||
351 !(lmb->flags & DRCONF_MEM_ASSIGNED))
354 #ifdef CONFIG_FA_DUMP
356 * Don't hot-remove memory that falls in fadump boot memory area
357 * and memory that is reserved for capturing old kernel memory.
359 if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
362 /* device_offline() will determine if we can actually remove this lmb */
366 static int dlpar_add_lmb(struct drmem_lmb *);
368 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
370 struct memory_block *mem_block;
371 unsigned long block_sz;
374 if (!lmb_is_removable(lmb))
377 mem_block = lmb_to_memblock(lmb);
378 if (mem_block == NULL)
381 rc = dlpar_offline_lmb(lmb);
383 put_device(&mem_block->dev);
387 block_sz = pseries_memory_block_size();
389 __remove_memory(lmb->base_addr, block_sz);
390 put_device(&mem_block->dev);
392 /* Update memory regions for memory remove */
393 memblock_remove(lmb->base_addr, block_sz);
395 invalidate_lmb_associativity_index(lmb);
396 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
401 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
403 struct drmem_lmb *lmb;
404 int lmbs_reserved = 0;
405 int lmbs_available = 0;
408 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
410 if (lmbs_to_remove == 0)
413 /* Validate that there are enough LMBs to satisfy the request */
414 for_each_drmem_lmb(lmb) {
415 if (lmb_is_removable(lmb))
418 if (lmbs_available == lmbs_to_remove)
422 if (lmbs_available < lmbs_to_remove) {
423 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
424 lmbs_available, lmbs_to_remove);
428 for_each_drmem_lmb(lmb) {
429 rc = dlpar_remove_lmb(lmb);
433 /* Mark this lmb so we can add it later if all of the
434 * requested LMBs cannot be removed.
436 drmem_mark_lmb_reserved(lmb);
439 if (lmbs_reserved == lmbs_to_remove)
443 if (lmbs_reserved != lmbs_to_remove) {
444 pr_err("Memory hot-remove failed, adding LMB's back\n");
446 for_each_drmem_lmb(lmb) {
447 if (!drmem_lmb_reserved(lmb))
450 rc = dlpar_add_lmb(lmb);
452 pr_err("Failed to add LMB back, drc index %x\n",
455 drmem_remove_lmb_reservation(lmb);
458 if (lmbs_reserved == 0)
464 for_each_drmem_lmb(lmb) {
465 if (!drmem_lmb_reserved(lmb))
468 dlpar_release_drc(lmb->drc_index);
469 pr_info("Memory at %llx was hot-removed\n",
472 drmem_remove_lmb_reservation(lmb);
475 if (lmbs_reserved == 0)
484 static int dlpar_memory_remove_by_index(u32 drc_index)
486 struct drmem_lmb *lmb;
490 pr_debug("Attempting to hot-remove LMB, drc index %x\n", drc_index);
493 for_each_drmem_lmb(lmb) {
494 if (lmb->drc_index == drc_index) {
496 rc = dlpar_remove_lmb(lmb);
498 dlpar_release_drc(lmb->drc_index);
508 pr_debug("Failed to hot-remove memory at %llx\n",
511 pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
516 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
518 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
521 pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
522 lmbs_to_remove, drc_index);
524 if (lmbs_to_remove == 0)
527 rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
532 * Validate that all LMBs in range are not reserved. Note that it
533 * is ok if they are !ASSIGNED since our goal here is to remove the
534 * LMB range, regardless of whether some LMBs were already removed
535 * by any other reason.
537 * This is a contrast to what is done in remove_by_count() where we
538 * check for both RESERVED and !ASSIGNED (via lmb_is_removable()),
539 * because we want to remove a fixed amount of LMBs in that function.
541 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
542 if (lmb->flags & DRCONF_MEM_RESERVED) {
543 pr_err("Memory at %llx (drc index %x) is reserved\n",
544 lmb->base_addr, lmb->drc_index);
549 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
551 * dlpar_remove_lmb() will error out if the LMB is already
552 * !ASSIGNED, but this case is a no-op for us.
554 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
557 rc = dlpar_remove_lmb(lmb);
561 drmem_mark_lmb_reserved(lmb);
565 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
568 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
569 if (!drmem_lmb_reserved(lmb))
573 * Setting the isolation state of an UNISOLATED/CONFIGURED
574 * device to UNISOLATE is a no-op, but the hypervisor can
575 * use it as a hint that the LMB removal failed.
577 dlpar_unisolate_drc(lmb->drc_index);
579 rc = dlpar_add_lmb(lmb);
581 pr_err("Failed to add LMB, drc index %x\n",
584 drmem_remove_lmb_reservation(lmb);
588 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
589 if (!drmem_lmb_reserved(lmb))
592 dlpar_release_drc(lmb->drc_index);
593 pr_info("Memory at %llx (drc index %x) was hot-removed\n",
594 lmb->base_addr, lmb->drc_index);
596 drmem_remove_lmb_reservation(lmb);
604 static inline int pseries_remove_memblock(unsigned long base,
605 unsigned long memblock_size)
609 static inline int pseries_remove_mem_node(struct device_node *np)
613 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
617 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
621 static int dlpar_memory_remove_by_index(u32 drc_index)
626 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
630 #endif /* CONFIG_MEMORY_HOTREMOVE */
632 static int dlpar_add_lmb(struct drmem_lmb *lmb)
634 unsigned long block_sz;
637 if (lmb->flags & DRCONF_MEM_ASSIGNED)
640 rc = update_lmb_associativity_index(lmb);
642 dlpar_release_drc(lmb->drc_index);
646 block_sz = memory_block_size_bytes();
648 /* Find the node id for this LMB. Fake one if necessary. */
649 nid = of_drconf_to_nid_single(lmb);
650 if (nid < 0 || !node_possible(nid))
651 nid = first_online_node;
654 rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
656 invalidate_lmb_associativity_index(lmb);
660 rc = dlpar_online_lmb(lmb);
662 __remove_memory(lmb->base_addr, block_sz);
663 invalidate_lmb_associativity_index(lmb);
665 lmb->flags |= DRCONF_MEM_ASSIGNED;
671 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
673 struct drmem_lmb *lmb;
674 int lmbs_available = 0;
675 int lmbs_reserved = 0;
678 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
680 if (lmbs_to_add == 0)
683 /* Validate that there are enough LMBs to satisfy the request */
684 for_each_drmem_lmb(lmb) {
685 if (lmb->flags & DRCONF_MEM_RESERVED)
688 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
691 if (lmbs_available == lmbs_to_add)
695 if (lmbs_available < lmbs_to_add)
698 for_each_drmem_lmb(lmb) {
699 if (lmb->flags & DRCONF_MEM_ASSIGNED)
702 rc = dlpar_acquire_drc(lmb->drc_index);
706 rc = dlpar_add_lmb(lmb);
708 dlpar_release_drc(lmb->drc_index);
712 /* Mark this lmb so we can remove it later if all of the
713 * requested LMBs cannot be added.
715 drmem_mark_lmb_reserved(lmb);
717 if (lmbs_reserved == lmbs_to_add)
721 if (lmbs_reserved != lmbs_to_add) {
722 pr_err("Memory hot-add failed, removing any added LMBs\n");
724 for_each_drmem_lmb(lmb) {
725 if (!drmem_lmb_reserved(lmb))
728 rc = dlpar_remove_lmb(lmb);
730 pr_err("Failed to remove LMB, drc index %x\n",
733 dlpar_release_drc(lmb->drc_index);
735 drmem_remove_lmb_reservation(lmb);
738 if (lmbs_reserved == 0)
743 for_each_drmem_lmb(lmb) {
744 if (!drmem_lmb_reserved(lmb))
747 pr_debug("Memory at %llx (drc index %x) was hot-added\n",
748 lmb->base_addr, lmb->drc_index);
749 drmem_remove_lmb_reservation(lmb);
752 if (lmbs_reserved == 0)
761 static int dlpar_memory_add_by_index(u32 drc_index)
763 struct drmem_lmb *lmb;
766 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
769 for_each_drmem_lmb(lmb) {
770 if (lmb->drc_index == drc_index) {
772 rc = dlpar_acquire_drc(lmb->drc_index);
774 rc = dlpar_add_lmb(lmb);
776 dlpar_release_drc(lmb->drc_index);
787 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
789 pr_info("Memory at %llx (drc index %x) was hot-added\n",
790 lmb->base_addr, drc_index);
795 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
797 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
800 pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
801 lmbs_to_add, drc_index);
803 if (lmbs_to_add == 0)
806 rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
810 /* Validate that the LMBs in this range are not reserved */
811 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
812 /* Fail immediately if the whole range can't be hot-added */
813 if (lmb->flags & DRCONF_MEM_RESERVED) {
814 pr_err("Memory at %llx (drc index %x) is reserved\n",
815 lmb->base_addr, lmb->drc_index);
820 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
821 if (lmb->flags & DRCONF_MEM_ASSIGNED)
824 rc = dlpar_acquire_drc(lmb->drc_index);
828 rc = dlpar_add_lmb(lmb);
830 dlpar_release_drc(lmb->drc_index);
834 drmem_mark_lmb_reserved(lmb);
838 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
840 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
841 if (!drmem_lmb_reserved(lmb))
844 rc = dlpar_remove_lmb(lmb);
846 pr_err("Failed to remove LMB, drc index %x\n",
849 dlpar_release_drc(lmb->drc_index);
851 drmem_remove_lmb_reservation(lmb);
855 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
856 if (!drmem_lmb_reserved(lmb))
859 pr_info("Memory at %llx (drc index %x) was hot-added\n",
860 lmb->base_addr, lmb->drc_index);
861 drmem_remove_lmb_reservation(lmb);
868 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
870 u32 count, drc_index;
873 lock_device_hotplug();
875 switch (hp_elog->action) {
876 case PSERIES_HP_ELOG_ACTION_ADD:
877 switch (hp_elog->id_type) {
878 case PSERIES_HP_ELOG_ID_DRC_COUNT:
879 count = hp_elog->_drc_u.drc_count;
880 rc = dlpar_memory_add_by_count(count);
882 case PSERIES_HP_ELOG_ID_DRC_INDEX:
883 drc_index = hp_elog->_drc_u.drc_index;
884 rc = dlpar_memory_add_by_index(drc_index);
886 case PSERIES_HP_ELOG_ID_DRC_IC:
887 count = hp_elog->_drc_u.ic.count;
888 drc_index = hp_elog->_drc_u.ic.index;
889 rc = dlpar_memory_add_by_ic(count, drc_index);
897 case PSERIES_HP_ELOG_ACTION_REMOVE:
898 switch (hp_elog->id_type) {
899 case PSERIES_HP_ELOG_ID_DRC_COUNT:
900 count = hp_elog->_drc_u.drc_count;
901 rc = dlpar_memory_remove_by_count(count);
903 case PSERIES_HP_ELOG_ID_DRC_INDEX:
904 drc_index = hp_elog->_drc_u.drc_index;
905 rc = dlpar_memory_remove_by_index(drc_index);
907 case PSERIES_HP_ELOG_ID_DRC_IC:
908 count = hp_elog->_drc_u.ic.count;
909 drc_index = hp_elog->_drc_u.ic.index;
910 rc = dlpar_memory_remove_by_ic(count, drc_index);
919 pr_err("Invalid action (%d) specified\n", hp_elog->action);
925 rc = drmem_update_dt();
927 unlock_device_hotplug();
931 static int pseries_add_mem_node(struct device_node *np)
935 unsigned long lmb_size;
937 int addr_cells, size_cells;
940 * Check to see if we are actually adding memory
942 if (!of_node_is_type(np, "memory"))
946 * Find the base and size of the memblock
948 prop = of_get_property(np, "reg", NULL);
952 addr_cells = of_n_addr_cells(np);
953 size_cells = of_n_size_cells(np);
955 * "reg" property represents (addr,size) tuple.
957 base = of_read_number(prop, addr_cells);
959 lmb_size = of_read_number(prop, size_cells);
962 * Update memory region to represent the memory add
964 ret = memblock_add(base, lmb_size);
965 return (ret < 0) ? -EINVAL : 0;
968 static int pseries_memory_notifier(struct notifier_block *nb,
969 unsigned long action, void *data)
971 struct of_reconfig_data *rd = data;
975 case OF_RECONFIG_ATTACH_NODE:
976 err = pseries_add_mem_node(rd->dn);
978 case OF_RECONFIG_DETACH_NODE:
979 err = pseries_remove_mem_node(rd->dn);
981 case OF_RECONFIG_UPDATE_PROPERTY:
982 if (!strcmp(rd->dn->name,
983 "ibm,dynamic-reconfiguration-memory"))
984 drmem_update_lmbs(rd->prop);
986 return notifier_from_errno(err);
989 static struct notifier_block pseries_mem_nb = {
990 .notifier_call = pseries_memory_notifier,
993 static int __init pseries_memory_hotplug_init(void)
995 if (firmware_has_feature(FW_FEATURE_LPAR))
996 of_reconfig_notifier_register(&pseries_mem_nb);
1000 machine_device_initcall(pseries, pseries_memory_hotplug_init);