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;
300 nid = memory_add_physaddr_to_nid(base);
302 for (i = 0; i < sections_per_block; i++) {
303 __remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
304 base += MIN_MEMORY_BLOCK_SIZE;
308 /* Update memory regions for memory remove */
309 memblock_remove(base, memblock_size);
310 unlock_device_hotplug();
314 static int pseries_remove_mem_node(struct device_node *np)
318 unsigned long lmb_size;
320 int addr_cells, size_cells;
323 * Check to see if we are actually removing memory
325 if (!of_node_is_type(np, "memory"))
329 * Find the base address and size of the memblock
331 prop = of_get_property(np, "reg", NULL);
335 addr_cells = of_n_addr_cells(np);
336 size_cells = of_n_size_cells(np);
339 * "reg" property represents (addr,size) tuple.
341 base = of_read_number(prop, addr_cells);
343 lmb_size = of_read_number(prop, size_cells);
345 pseries_remove_memblock(base, lmb_size);
349 static bool lmb_is_removable(struct drmem_lmb *lmb)
351 if ((lmb->flags & DRCONF_MEM_RESERVED) ||
352 !(lmb->flags & DRCONF_MEM_ASSIGNED))
355 #ifdef CONFIG_FA_DUMP
357 * Don't hot-remove memory that falls in fadump boot memory area
358 * and memory that is reserved for capturing old kernel memory.
360 if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
363 /* device_offline() will determine if we can actually remove this lmb */
367 static int dlpar_add_lmb(struct drmem_lmb *);
369 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
371 struct memory_block *mem_block;
372 unsigned long block_sz;
375 if (!lmb_is_removable(lmb))
378 mem_block = lmb_to_memblock(lmb);
379 if (mem_block == NULL)
382 rc = dlpar_offline_lmb(lmb);
384 put_device(&mem_block->dev);
388 block_sz = pseries_memory_block_size();
390 __remove_memory(mem_block->nid, lmb->base_addr, block_sz);
391 put_device(&mem_block->dev);
393 /* Update memory regions for memory remove */
394 memblock_remove(lmb->base_addr, block_sz);
396 invalidate_lmb_associativity_index(lmb);
397 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
402 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
404 struct drmem_lmb *lmb;
405 int lmbs_reserved = 0;
406 int lmbs_available = 0;
409 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
411 if (lmbs_to_remove == 0)
414 /* Validate that there are enough LMBs to satisfy the request */
415 for_each_drmem_lmb(lmb) {
416 if (lmb_is_removable(lmb))
419 if (lmbs_available == lmbs_to_remove)
423 if (lmbs_available < lmbs_to_remove) {
424 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
425 lmbs_available, lmbs_to_remove);
429 for_each_drmem_lmb(lmb) {
430 rc = dlpar_remove_lmb(lmb);
434 /* Mark this lmb so we can add it later if all of the
435 * requested LMBs cannot be removed.
437 drmem_mark_lmb_reserved(lmb);
440 if (lmbs_reserved == lmbs_to_remove)
444 if (lmbs_reserved != lmbs_to_remove) {
445 pr_err("Memory hot-remove failed, adding LMB's back\n");
447 for_each_drmem_lmb(lmb) {
448 if (!drmem_lmb_reserved(lmb))
451 rc = dlpar_add_lmb(lmb);
453 pr_err("Failed to add LMB back, drc index %x\n",
456 drmem_remove_lmb_reservation(lmb);
459 if (lmbs_reserved == 0)
465 for_each_drmem_lmb(lmb) {
466 if (!drmem_lmb_reserved(lmb))
469 dlpar_release_drc(lmb->drc_index);
470 pr_info("Memory at %llx was hot-removed\n",
473 drmem_remove_lmb_reservation(lmb);
476 if (lmbs_reserved == 0)
485 static int dlpar_memory_remove_by_index(u32 drc_index)
487 struct drmem_lmb *lmb;
491 pr_debug("Attempting to hot-remove LMB, drc index %x\n", drc_index);
494 for_each_drmem_lmb(lmb) {
495 if (lmb->drc_index == drc_index) {
497 rc = dlpar_remove_lmb(lmb);
499 dlpar_release_drc(lmb->drc_index);
509 pr_debug("Failed to hot-remove memory at %llx\n",
512 pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
517 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
519 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
522 pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
523 lmbs_to_remove, drc_index);
525 if (lmbs_to_remove == 0)
528 rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
533 * Validate that all LMBs in range are not reserved. Note that it
534 * is ok if they are !ASSIGNED since our goal here is to remove the
535 * LMB range, regardless of whether some LMBs were already removed
536 * by any other reason.
538 * This is a contrast to what is done in remove_by_count() where we
539 * check for both RESERVED and !ASSIGNED (via lmb_is_removable()),
540 * because we want to remove a fixed amount of LMBs in that function.
542 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
543 if (lmb->flags & DRCONF_MEM_RESERVED) {
544 pr_err("Memory at %llx (drc index %x) is reserved\n",
545 lmb->base_addr, lmb->drc_index);
550 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
552 * dlpar_remove_lmb() will error out if the LMB is already
553 * !ASSIGNED, but this case is a no-op for us.
555 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
558 rc = dlpar_remove_lmb(lmb);
562 drmem_mark_lmb_reserved(lmb);
566 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
569 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
570 if (!drmem_lmb_reserved(lmb))
574 * Setting the isolation state of an UNISOLATED/CONFIGURED
575 * device to UNISOLATE is a no-op, but the hypervisor can
576 * use it as a hint that the LMB removal failed.
578 dlpar_unisolate_drc(lmb->drc_index);
580 rc = dlpar_add_lmb(lmb);
582 pr_err("Failed to add LMB, drc index %x\n",
585 drmem_remove_lmb_reservation(lmb);
589 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
590 if (!drmem_lmb_reserved(lmb))
593 dlpar_release_drc(lmb->drc_index);
594 pr_info("Memory at %llx (drc index %x) was hot-removed\n",
595 lmb->base_addr, lmb->drc_index);
597 drmem_remove_lmb_reservation(lmb);
605 static inline int pseries_remove_memblock(unsigned long base,
606 unsigned long memblock_size)
610 static inline int pseries_remove_mem_node(struct device_node *np)
614 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
618 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
622 static int dlpar_memory_remove_by_index(u32 drc_index)
627 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
631 #endif /* CONFIG_MEMORY_HOTREMOVE */
633 static int dlpar_add_lmb(struct drmem_lmb *lmb)
635 unsigned long block_sz;
638 if (lmb->flags & DRCONF_MEM_ASSIGNED)
641 rc = update_lmb_associativity_index(lmb);
643 dlpar_release_drc(lmb->drc_index);
647 block_sz = memory_block_size_bytes();
649 /* Find the node id for this LMB. Fake one if necessary. */
650 nid = of_drconf_to_nid_single(lmb);
651 if (nid < 0 || !node_possible(nid))
652 nid = first_online_node;
655 rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
657 invalidate_lmb_associativity_index(lmb);
661 rc = dlpar_online_lmb(lmb);
663 __remove_memory(nid, lmb->base_addr, block_sz);
664 invalidate_lmb_associativity_index(lmb);
666 lmb->flags |= DRCONF_MEM_ASSIGNED;
672 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
674 struct drmem_lmb *lmb;
675 int lmbs_available = 0;
676 int lmbs_reserved = 0;
679 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
681 if (lmbs_to_add == 0)
684 /* Validate that there are enough LMBs to satisfy the request */
685 for_each_drmem_lmb(lmb) {
686 if (lmb->flags & DRCONF_MEM_RESERVED)
689 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
692 if (lmbs_available == lmbs_to_add)
696 if (lmbs_available < lmbs_to_add)
699 for_each_drmem_lmb(lmb) {
700 if (lmb->flags & DRCONF_MEM_ASSIGNED)
703 rc = dlpar_acquire_drc(lmb->drc_index);
707 rc = dlpar_add_lmb(lmb);
709 dlpar_release_drc(lmb->drc_index);
713 /* Mark this lmb so we can remove it later if all of the
714 * requested LMBs cannot be added.
716 drmem_mark_lmb_reserved(lmb);
718 if (lmbs_reserved == lmbs_to_add)
722 if (lmbs_reserved != lmbs_to_add) {
723 pr_err("Memory hot-add failed, removing any added LMBs\n");
725 for_each_drmem_lmb(lmb) {
726 if (!drmem_lmb_reserved(lmb))
729 rc = dlpar_remove_lmb(lmb);
731 pr_err("Failed to remove LMB, drc index %x\n",
734 dlpar_release_drc(lmb->drc_index);
736 drmem_remove_lmb_reservation(lmb);
739 if (lmbs_reserved == 0)
744 for_each_drmem_lmb(lmb) {
745 if (!drmem_lmb_reserved(lmb))
748 pr_debug("Memory at %llx (drc index %x) was hot-added\n",
749 lmb->base_addr, lmb->drc_index);
750 drmem_remove_lmb_reservation(lmb);
753 if (lmbs_reserved == 0)
762 static int dlpar_memory_add_by_index(u32 drc_index)
764 struct drmem_lmb *lmb;
767 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
770 for_each_drmem_lmb(lmb) {
771 if (lmb->drc_index == drc_index) {
773 rc = dlpar_acquire_drc(lmb->drc_index);
775 rc = dlpar_add_lmb(lmb);
777 dlpar_release_drc(lmb->drc_index);
788 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
790 pr_info("Memory at %llx (drc index %x) was hot-added\n",
791 lmb->base_addr, drc_index);
796 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
798 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
801 pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
802 lmbs_to_add, drc_index);
804 if (lmbs_to_add == 0)
807 rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
811 /* Validate that the LMBs in this range are not reserved */
812 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
813 /* Fail immediately if the whole range can't be hot-added */
814 if (lmb->flags & DRCONF_MEM_RESERVED) {
815 pr_err("Memory at %llx (drc index %x) is reserved\n",
816 lmb->base_addr, lmb->drc_index);
821 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
822 if (lmb->flags & DRCONF_MEM_ASSIGNED)
825 rc = dlpar_acquire_drc(lmb->drc_index);
829 rc = dlpar_add_lmb(lmb);
831 dlpar_release_drc(lmb->drc_index);
835 drmem_mark_lmb_reserved(lmb);
839 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
841 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
842 if (!drmem_lmb_reserved(lmb))
845 rc = dlpar_remove_lmb(lmb);
847 pr_err("Failed to remove LMB, drc index %x\n",
850 dlpar_release_drc(lmb->drc_index);
852 drmem_remove_lmb_reservation(lmb);
856 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
857 if (!drmem_lmb_reserved(lmb))
860 pr_info("Memory at %llx (drc index %x) was hot-added\n",
861 lmb->base_addr, lmb->drc_index);
862 drmem_remove_lmb_reservation(lmb);
869 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
871 u32 count, drc_index;
874 lock_device_hotplug();
876 switch (hp_elog->action) {
877 case PSERIES_HP_ELOG_ACTION_ADD:
878 switch (hp_elog->id_type) {
879 case PSERIES_HP_ELOG_ID_DRC_COUNT:
880 count = hp_elog->_drc_u.drc_count;
881 rc = dlpar_memory_add_by_count(count);
883 case PSERIES_HP_ELOG_ID_DRC_INDEX:
884 drc_index = hp_elog->_drc_u.drc_index;
885 rc = dlpar_memory_add_by_index(drc_index);
887 case PSERIES_HP_ELOG_ID_DRC_IC:
888 count = hp_elog->_drc_u.ic.count;
889 drc_index = hp_elog->_drc_u.ic.index;
890 rc = dlpar_memory_add_by_ic(count, drc_index);
898 case PSERIES_HP_ELOG_ACTION_REMOVE:
899 switch (hp_elog->id_type) {
900 case PSERIES_HP_ELOG_ID_DRC_COUNT:
901 count = hp_elog->_drc_u.drc_count;
902 rc = dlpar_memory_remove_by_count(count);
904 case PSERIES_HP_ELOG_ID_DRC_INDEX:
905 drc_index = hp_elog->_drc_u.drc_index;
906 rc = dlpar_memory_remove_by_index(drc_index);
908 case PSERIES_HP_ELOG_ID_DRC_IC:
909 count = hp_elog->_drc_u.ic.count;
910 drc_index = hp_elog->_drc_u.ic.index;
911 rc = dlpar_memory_remove_by_ic(count, drc_index);
920 pr_err("Invalid action (%d) specified\n", hp_elog->action);
926 rc = drmem_update_dt();
928 unlock_device_hotplug();
932 static int pseries_add_mem_node(struct device_node *np)
936 unsigned long lmb_size;
938 int addr_cells, size_cells;
941 * Check to see if we are actually adding memory
943 if (!of_node_is_type(np, "memory"))
947 * Find the base and size of the memblock
949 prop = of_get_property(np, "reg", NULL);
953 addr_cells = of_n_addr_cells(np);
954 size_cells = of_n_size_cells(np);
956 * "reg" property represents (addr,size) tuple.
958 base = of_read_number(prop, addr_cells);
960 lmb_size = of_read_number(prop, size_cells);
963 * Update memory region to represent the memory add
965 ret = memblock_add(base, lmb_size);
966 return (ret < 0) ? -EINVAL : 0;
969 static int pseries_memory_notifier(struct notifier_block *nb,
970 unsigned long action, void *data)
972 struct of_reconfig_data *rd = data;
976 case OF_RECONFIG_ATTACH_NODE:
977 err = pseries_add_mem_node(rd->dn);
979 case OF_RECONFIG_DETACH_NODE:
980 err = pseries_remove_mem_node(rd->dn);
982 case OF_RECONFIG_UPDATE_PROPERTY:
983 if (!strcmp(rd->dn->name,
984 "ibm,dynamic-reconfiguration-memory"))
985 drmem_update_lmbs(rd->prop);
987 return notifier_from_errno(err);
990 static struct notifier_block pseries_mem_nb = {
991 .notifier_call = pseries_memory_notifier,
994 static int __init pseries_memory_hotplug_init(void)
996 if (firmware_has_feature(FW_FEATURE_LPAR))
997 of_reconfig_notifier_register(&pseries_mem_nb);
1001 machine_device_initcall(pseries, pseries_memory_hotplug_init);