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>
19 #include <asm/sparsemem.h>
20 #include <asm/fadump.h>
21 #include <asm/drmem.h>
24 unsigned long pseries_memory_block_size(void)
26 struct device_node *np;
27 u64 memblock_size = MIN_MEMORY_BLOCK_SIZE;
30 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
36 size_cells = of_n_size_cells(np);
38 prop = of_get_property(np, "ibm,lmb-size", &len);
39 if (prop && len >= size_cells * sizeof(__be32))
40 memblock_size = of_read_number(prop, size_cells);
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 bool find_aa_index(struct device_node *dr_node,
103 struct property *ala_prop,
104 const u32 *lmb_assoc, u32 *aa_index)
106 u32 *assoc_arrays, new_prop_size;
107 struct property *new_prop;
108 int aa_arrays, aa_array_entries, aa_array_sz;
112 * The ibm,associativity-lookup-arrays property is defined to be
113 * a 32-bit value specifying the number of associativity arrays
114 * followed by a 32-bitvalue specifying the number of entries per
115 * array, followed by the associativity arrays.
117 assoc_arrays = ala_prop->value;
119 aa_arrays = be32_to_cpu(assoc_arrays[0]);
120 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
121 aa_array_sz = aa_array_entries * sizeof(u32);
123 for (i = 0; i < aa_arrays; i++) {
124 index = (i * aa_array_entries) + 2;
126 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
133 new_prop_size = ala_prop->length + aa_array_sz;
134 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
138 assoc_arrays = new_prop->value;
140 /* increment the number of entries in the lookup array */
141 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
143 /* copy the new associativity into the lookup array */
144 index = aa_arrays * aa_array_entries + 2;
145 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
147 of_update_property(dr_node, new_prop);
150 * The associativity lookup array index for this lmb is
151 * number of entries - 1 since we added its associativity
152 * to the end of the lookup array.
154 *aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
158 static int update_lmb_associativity_index(struct drmem_lmb *lmb)
160 struct device_node *parent, *lmb_node, *dr_node;
161 struct property *ala_prop;
162 const u32 *lmb_assoc;
166 parent = of_find_node_by_path("/");
170 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
176 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
178 dlpar_free_cc_nodes(lmb_node);
182 update_numa_distance(lmb_node);
184 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
186 dlpar_free_cc_nodes(lmb_node);
190 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
193 of_node_put(dr_node);
194 dlpar_free_cc_nodes(lmb_node);
198 found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
200 of_node_put(dr_node);
201 dlpar_free_cc_nodes(lmb_node);
204 pr_err("Could not find LMB associativity\n");
208 lmb->aa_index = aa_index;
212 static struct memory_block *lmb_to_memblock(struct drmem_lmb *lmb)
214 unsigned long section_nr;
215 struct memory_block *mem_block;
217 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
219 mem_block = find_memory_block(section_nr);
223 static int get_lmb_range(u32 drc_index, int n_lmbs,
224 struct drmem_lmb **start_lmb,
225 struct drmem_lmb **end_lmb)
227 struct drmem_lmb *lmb, *start, *end;
228 struct drmem_lmb *limit;
231 for_each_drmem_lmb(lmb) {
232 if (lmb->drc_index == drc_index) {
241 end = &start[n_lmbs];
243 limit = &drmem_info->lmbs[drmem_info->n_lmbs];
252 static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
254 struct memory_block *mem_block;
257 mem_block = lmb_to_memblock(lmb);
261 if (online && mem_block->dev.offline)
262 rc = device_online(&mem_block->dev);
263 else if (!online && !mem_block->dev.offline)
264 rc = device_offline(&mem_block->dev);
268 put_device(&mem_block->dev);
273 static int dlpar_online_lmb(struct drmem_lmb *lmb)
275 return dlpar_change_lmb_state(lmb, true);
278 #ifdef CONFIG_MEMORY_HOTREMOVE
279 static int dlpar_offline_lmb(struct drmem_lmb *lmb)
281 return dlpar_change_lmb_state(lmb, false);
284 static int pseries_remove_memblock(unsigned long base, unsigned long memblock_size)
286 unsigned long block_sz, start_pfn;
287 int sections_per_block;
290 start_pfn = base >> PAGE_SHIFT;
292 lock_device_hotplug();
294 if (!pfn_valid(start_pfn))
297 block_sz = pseries_memory_block_size();
298 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
300 for (i = 0; i < sections_per_block; i++) {
301 __remove_memory(base, MIN_MEMORY_BLOCK_SIZE);
302 base += MIN_MEMORY_BLOCK_SIZE;
306 /* Update memory regions for memory remove */
307 memblock_remove(base, memblock_size);
308 unlock_device_hotplug();
312 static int pseries_remove_mem_node(struct device_node *np)
316 unsigned long lmb_size;
318 int addr_cells, size_cells;
321 * Check to see if we are actually removing memory
323 if (!of_node_is_type(np, "memory"))
327 * Find the base address and size of the memblock
329 prop = of_get_property(np, "reg", NULL);
333 addr_cells = of_n_addr_cells(np);
334 size_cells = of_n_size_cells(np);
337 * "reg" property represents (addr,size) tuple.
339 base = of_read_number(prop, addr_cells);
341 lmb_size = of_read_number(prop, size_cells);
343 pseries_remove_memblock(base, lmb_size);
347 static bool lmb_is_removable(struct drmem_lmb *lmb)
349 if ((lmb->flags & DRCONF_MEM_RESERVED) ||
350 !(lmb->flags & DRCONF_MEM_ASSIGNED))
353 #ifdef CONFIG_FA_DUMP
355 * Don't hot-remove memory that falls in fadump boot memory area
356 * and memory that is reserved for capturing old kernel memory.
358 if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
361 /* device_offline() will determine if we can actually remove this lmb */
365 static int dlpar_add_lmb(struct drmem_lmb *);
367 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
369 struct memory_block *mem_block;
370 unsigned long block_sz;
373 if (!lmb_is_removable(lmb))
376 mem_block = lmb_to_memblock(lmb);
377 if (mem_block == NULL)
380 rc = dlpar_offline_lmb(lmb);
382 put_device(&mem_block->dev);
386 block_sz = pseries_memory_block_size();
388 __remove_memory(lmb->base_addr, block_sz);
389 put_device(&mem_block->dev);
391 /* Update memory regions for memory remove */
392 memblock_remove(lmb->base_addr, block_sz);
394 invalidate_lmb_associativity_index(lmb);
395 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
400 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
402 struct drmem_lmb *lmb;
403 int lmbs_reserved = 0;
404 int lmbs_available = 0;
407 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
409 if (lmbs_to_remove == 0)
412 /* Validate that there are enough LMBs to satisfy the request */
413 for_each_drmem_lmb(lmb) {
414 if (lmb_is_removable(lmb))
417 if (lmbs_available == lmbs_to_remove)
421 if (lmbs_available < lmbs_to_remove) {
422 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
423 lmbs_available, lmbs_to_remove);
427 for_each_drmem_lmb(lmb) {
428 rc = dlpar_remove_lmb(lmb);
432 /* Mark this lmb so we can add it later if all of the
433 * requested LMBs cannot be removed.
435 drmem_mark_lmb_reserved(lmb);
438 if (lmbs_reserved == lmbs_to_remove)
442 if (lmbs_reserved != lmbs_to_remove) {
443 pr_err("Memory hot-remove failed, adding LMB's back\n");
445 for_each_drmem_lmb(lmb) {
446 if (!drmem_lmb_reserved(lmb))
449 rc = dlpar_add_lmb(lmb);
451 pr_err("Failed to add LMB back, drc index %x\n",
454 drmem_remove_lmb_reservation(lmb);
457 if (lmbs_reserved == 0)
463 for_each_drmem_lmb(lmb) {
464 if (!drmem_lmb_reserved(lmb))
467 dlpar_release_drc(lmb->drc_index);
468 pr_info("Memory at %llx was hot-removed\n",
471 drmem_remove_lmb_reservation(lmb);
474 if (lmbs_reserved == 0)
483 static int dlpar_memory_remove_by_index(u32 drc_index)
485 struct drmem_lmb *lmb;
489 pr_debug("Attempting to hot-remove LMB, drc index %x\n", drc_index);
492 for_each_drmem_lmb(lmb) {
493 if (lmb->drc_index == drc_index) {
495 rc = dlpar_remove_lmb(lmb);
497 dlpar_release_drc(lmb->drc_index);
507 pr_debug("Failed to hot-remove memory at %llx\n",
510 pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
515 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
517 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
520 pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
521 lmbs_to_remove, drc_index);
523 if (lmbs_to_remove == 0)
526 rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
531 * Validate that all LMBs in range are not reserved. Note that it
532 * is ok if they are !ASSIGNED since our goal here is to remove the
533 * LMB range, regardless of whether some LMBs were already removed
534 * by any other reason.
536 * This is a contrast to what is done in remove_by_count() where we
537 * check for both RESERVED and !ASSIGNED (via lmb_is_removable()),
538 * because we want to remove a fixed amount of LMBs in that function.
540 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
541 if (lmb->flags & DRCONF_MEM_RESERVED) {
542 pr_err("Memory at %llx (drc index %x) is reserved\n",
543 lmb->base_addr, lmb->drc_index);
548 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
550 * dlpar_remove_lmb() will error out if the LMB is already
551 * !ASSIGNED, but this case is a no-op for us.
553 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
556 rc = dlpar_remove_lmb(lmb);
560 drmem_mark_lmb_reserved(lmb);
564 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
567 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
568 if (!drmem_lmb_reserved(lmb))
572 * Setting the isolation state of an UNISOLATED/CONFIGURED
573 * device to UNISOLATE is a no-op, but the hypervisor can
574 * use it as a hint that the LMB removal failed.
576 dlpar_unisolate_drc(lmb->drc_index);
578 rc = dlpar_add_lmb(lmb);
580 pr_err("Failed to add LMB, drc index %x\n",
583 drmem_remove_lmb_reservation(lmb);
587 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
588 if (!drmem_lmb_reserved(lmb))
591 dlpar_release_drc(lmb->drc_index);
592 pr_info("Memory at %llx (drc index %x) was hot-removed\n",
593 lmb->base_addr, lmb->drc_index);
595 drmem_remove_lmb_reservation(lmb);
603 static inline int pseries_remove_memblock(unsigned long base,
604 unsigned long memblock_size)
608 static inline int pseries_remove_mem_node(struct device_node *np)
612 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
616 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
620 static int dlpar_memory_remove_by_index(u32 drc_index)
625 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
629 #endif /* CONFIG_MEMORY_HOTREMOVE */
631 static int dlpar_add_lmb(struct drmem_lmb *lmb)
633 unsigned long block_sz;
636 if (lmb->flags & DRCONF_MEM_ASSIGNED)
639 rc = update_lmb_associativity_index(lmb);
641 dlpar_release_drc(lmb->drc_index);
645 block_sz = memory_block_size_bytes();
647 /* Find the node id for this LMB. Fake one if necessary. */
648 nid = of_drconf_to_nid_single(lmb);
649 if (nid < 0 || !node_possible(nid))
650 nid = first_online_node;
653 rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
655 invalidate_lmb_associativity_index(lmb);
659 rc = dlpar_online_lmb(lmb);
661 __remove_memory(lmb->base_addr, block_sz);
662 invalidate_lmb_associativity_index(lmb);
664 lmb->flags |= DRCONF_MEM_ASSIGNED;
670 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
672 struct drmem_lmb *lmb;
673 int lmbs_available = 0;
674 int lmbs_reserved = 0;
677 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
679 if (lmbs_to_add == 0)
682 /* Validate that there are enough LMBs to satisfy the request */
683 for_each_drmem_lmb(lmb) {
684 if (lmb->flags & DRCONF_MEM_RESERVED)
687 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
690 if (lmbs_available == lmbs_to_add)
694 if (lmbs_available < lmbs_to_add)
697 for_each_drmem_lmb(lmb) {
698 if (lmb->flags & DRCONF_MEM_ASSIGNED)
701 rc = dlpar_acquire_drc(lmb->drc_index);
705 rc = dlpar_add_lmb(lmb);
707 dlpar_release_drc(lmb->drc_index);
711 /* Mark this lmb so we can remove it later if all of the
712 * requested LMBs cannot be added.
714 drmem_mark_lmb_reserved(lmb);
716 if (lmbs_reserved == lmbs_to_add)
720 if (lmbs_reserved != lmbs_to_add) {
721 pr_err("Memory hot-add failed, removing any added LMBs\n");
723 for_each_drmem_lmb(lmb) {
724 if (!drmem_lmb_reserved(lmb))
727 rc = dlpar_remove_lmb(lmb);
729 pr_err("Failed to remove LMB, drc index %x\n",
732 dlpar_release_drc(lmb->drc_index);
734 drmem_remove_lmb_reservation(lmb);
737 if (lmbs_reserved == 0)
742 for_each_drmem_lmb(lmb) {
743 if (!drmem_lmb_reserved(lmb))
746 pr_debug("Memory at %llx (drc index %x) was hot-added\n",
747 lmb->base_addr, lmb->drc_index);
748 drmem_remove_lmb_reservation(lmb);
751 if (lmbs_reserved == 0)
760 static int dlpar_memory_add_by_index(u32 drc_index)
762 struct drmem_lmb *lmb;
765 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
768 for_each_drmem_lmb(lmb) {
769 if (lmb->drc_index == drc_index) {
771 rc = dlpar_acquire_drc(lmb->drc_index);
773 rc = dlpar_add_lmb(lmb);
775 dlpar_release_drc(lmb->drc_index);
786 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
788 pr_info("Memory at %llx (drc index %x) was hot-added\n",
789 lmb->base_addr, drc_index);
794 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
796 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
799 pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
800 lmbs_to_add, drc_index);
802 if (lmbs_to_add == 0)
805 rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
809 /* Validate that the LMBs in this range are not reserved */
810 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
811 /* Fail immediately if the whole range can't be hot-added */
812 if (lmb->flags & DRCONF_MEM_RESERVED) {
813 pr_err("Memory at %llx (drc index %x) is reserved\n",
814 lmb->base_addr, lmb->drc_index);
819 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
820 if (lmb->flags & DRCONF_MEM_ASSIGNED)
823 rc = dlpar_acquire_drc(lmb->drc_index);
827 rc = dlpar_add_lmb(lmb);
829 dlpar_release_drc(lmb->drc_index);
833 drmem_mark_lmb_reserved(lmb);
837 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
839 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
840 if (!drmem_lmb_reserved(lmb))
843 rc = dlpar_remove_lmb(lmb);
845 pr_err("Failed to remove LMB, drc index %x\n",
848 dlpar_release_drc(lmb->drc_index);
850 drmem_remove_lmb_reservation(lmb);
854 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
855 if (!drmem_lmb_reserved(lmb))
858 pr_info("Memory at %llx (drc index %x) was hot-added\n",
859 lmb->base_addr, lmb->drc_index);
860 drmem_remove_lmb_reservation(lmb);
867 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
869 u32 count, drc_index;
872 lock_device_hotplug();
874 switch (hp_elog->action) {
875 case PSERIES_HP_ELOG_ACTION_ADD:
876 switch (hp_elog->id_type) {
877 case PSERIES_HP_ELOG_ID_DRC_COUNT:
878 count = hp_elog->_drc_u.drc_count;
879 rc = dlpar_memory_add_by_count(count);
881 case PSERIES_HP_ELOG_ID_DRC_INDEX:
882 drc_index = hp_elog->_drc_u.drc_index;
883 rc = dlpar_memory_add_by_index(drc_index);
885 case PSERIES_HP_ELOG_ID_DRC_IC:
886 count = hp_elog->_drc_u.ic.count;
887 drc_index = hp_elog->_drc_u.ic.index;
888 rc = dlpar_memory_add_by_ic(count, drc_index);
896 case PSERIES_HP_ELOG_ACTION_REMOVE:
897 switch (hp_elog->id_type) {
898 case PSERIES_HP_ELOG_ID_DRC_COUNT:
899 count = hp_elog->_drc_u.drc_count;
900 rc = dlpar_memory_remove_by_count(count);
902 case PSERIES_HP_ELOG_ID_DRC_INDEX:
903 drc_index = hp_elog->_drc_u.drc_index;
904 rc = dlpar_memory_remove_by_index(drc_index);
906 case PSERIES_HP_ELOG_ID_DRC_IC:
907 count = hp_elog->_drc_u.ic.count;
908 drc_index = hp_elog->_drc_u.ic.index;
909 rc = dlpar_memory_remove_by_ic(count, drc_index);
918 pr_err("Invalid action (%d) specified\n", hp_elog->action);
924 rc = drmem_update_dt();
926 unlock_device_hotplug();
930 static int pseries_add_mem_node(struct device_node *np)
934 unsigned long lmb_size;
936 int addr_cells, size_cells;
939 * Check to see if we are actually adding memory
941 if (!of_node_is_type(np, "memory"))
945 * Find the base and size of the memblock
947 prop = of_get_property(np, "reg", NULL);
951 addr_cells = of_n_addr_cells(np);
952 size_cells = of_n_size_cells(np);
954 * "reg" property represents (addr,size) tuple.
956 base = of_read_number(prop, addr_cells);
958 lmb_size = of_read_number(prop, size_cells);
961 * Update memory region to represent the memory add
963 ret = memblock_add(base, lmb_size);
964 return (ret < 0) ? -EINVAL : 0;
967 static int pseries_memory_notifier(struct notifier_block *nb,
968 unsigned long action, void *data)
970 struct of_reconfig_data *rd = data;
974 case OF_RECONFIG_ATTACH_NODE:
975 err = pseries_add_mem_node(rd->dn);
977 case OF_RECONFIG_DETACH_NODE:
978 err = pseries_remove_mem_node(rd->dn);
980 case OF_RECONFIG_UPDATE_PROPERTY:
981 if (!strcmp(rd->dn->name,
982 "ibm,dynamic-reconfiguration-memory"))
983 drmem_update_lmbs(rd->prop);
985 return notifier_from_errno(err);
988 static struct notifier_block pseries_mem_nb = {
989 .notifier_call = pseries_memory_notifier,
992 static int __init pseries_memory_hotplug_init(void)
994 if (firmware_has_feature(FW_FEATURE_LPAR))
995 of_reconfig_notifier_register(&pseries_mem_nb);
999 machine_device_initcall(pseries, pseries_memory_hotplug_init);