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 static void dlpar_free_property(struct property *prop)
31 static struct property *dlpar_clone_property(struct property *prop,
34 struct property *new_prop;
36 new_prop = kzalloc(sizeof(*new_prop), GFP_KERNEL);
40 new_prop->name = kstrdup(prop->name, GFP_KERNEL);
41 new_prop->value = kzalloc(prop_size, GFP_KERNEL);
42 if (!new_prop->name || !new_prop->value) {
43 dlpar_free_property(new_prop);
47 memcpy(new_prop->value, prop->value, prop->length);
48 new_prop->length = prop_size;
50 of_property_set_flag(new_prop, OF_DYNAMIC);
54 static bool find_aa_index(struct device_node *dr_node,
55 struct property *ala_prop,
56 const u32 *lmb_assoc, u32 *aa_index)
60 struct property *new_prop;
61 int aa_arrays, aa_array_entries, aa_array_sz;
65 * The ibm,associativity-lookup-arrays property is defined to be
66 * a 32-bit value specifying the number of associativity arrays
67 * followed by a 32-bitvalue specifying the number of entries per
68 * array, followed by the associativity arrays.
70 assoc_arrays = ala_prop->value;
72 aa_arrays = be32_to_cpu(assoc_arrays[0]);
73 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
74 aa_array_sz = aa_array_entries * sizeof(u32);
76 for (i = 0; i < aa_arrays; i++) {
77 index = (i * aa_array_entries) + 2;
79 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
86 new_prop_size = ala_prop->length + aa_array_sz;
87 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
91 assoc_arrays = new_prop->value;
93 /* increment the number of entries in the lookup array */
94 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
96 /* copy the new associativity into the lookup array */
97 index = aa_arrays * aa_array_entries + 2;
98 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
100 of_update_property(dr_node, new_prop);
103 * The associativity lookup array index for this lmb is
104 * number of entries - 1 since we added its associativity
105 * to the end of the lookup array.
107 *aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
111 static int update_lmb_associativity_index(struct drmem_lmb *lmb)
113 struct device_node *parent, *lmb_node, *dr_node;
114 struct property *ala_prop;
115 const u32 *lmb_assoc;
119 parent = of_find_node_by_path("/");
123 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
129 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
131 dlpar_free_cc_nodes(lmb_node);
135 update_numa_distance(lmb_node);
137 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
139 dlpar_free_cc_nodes(lmb_node);
143 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
146 of_node_put(dr_node);
147 dlpar_free_cc_nodes(lmb_node);
151 found = find_aa_index(dr_node, ala_prop, lmb_assoc, &aa_index);
153 of_node_put(dr_node);
154 dlpar_free_cc_nodes(lmb_node);
157 pr_err("Could not find LMB associativity\n");
161 lmb->aa_index = aa_index;
165 static struct memory_block *lmb_to_memblock(struct drmem_lmb *lmb)
167 unsigned long section_nr;
168 struct memory_block *mem_block;
170 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
172 mem_block = find_memory_block(section_nr);
176 static int get_lmb_range(u32 drc_index, int n_lmbs,
177 struct drmem_lmb **start_lmb,
178 struct drmem_lmb **end_lmb)
180 struct drmem_lmb *lmb, *start, *end;
181 struct drmem_lmb *limit;
184 for_each_drmem_lmb(lmb) {
185 if (lmb->drc_index == drc_index) {
194 end = &start[n_lmbs];
196 limit = &drmem_info->lmbs[drmem_info->n_lmbs];
205 static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
207 struct memory_block *mem_block;
210 mem_block = lmb_to_memblock(lmb);
214 if (online && mem_block->dev.offline)
215 rc = device_online(&mem_block->dev);
216 else if (!online && !mem_block->dev.offline)
217 rc = device_offline(&mem_block->dev);
221 put_device(&mem_block->dev);
226 static int dlpar_online_lmb(struct drmem_lmb *lmb)
228 return dlpar_change_lmb_state(lmb, true);
231 #ifdef CONFIG_MEMORY_HOTREMOVE
232 static int dlpar_offline_lmb(struct drmem_lmb *lmb)
234 return dlpar_change_lmb_state(lmb, false);
237 static int pseries_remove_memblock(unsigned long base, unsigned long memblock_size)
239 unsigned long start_pfn;
240 int sections_per_block;
243 start_pfn = base >> PAGE_SHIFT;
245 lock_device_hotplug();
247 if (!pfn_valid(start_pfn))
250 sections_per_block = memory_block_size / MIN_MEMORY_BLOCK_SIZE;
252 for (i = 0; i < sections_per_block; i++) {
253 __remove_memory(base, MIN_MEMORY_BLOCK_SIZE);
254 base += MIN_MEMORY_BLOCK_SIZE;
258 /* Update memory regions for memory remove */
259 memblock_remove(base, memblock_size);
260 unlock_device_hotplug();
264 static int pseries_remove_mem_node(struct device_node *np)
270 * Check to see if we are actually removing memory
272 if (!of_node_is_type(np, "memory"))
276 * Find the base address and size of the memblock
278 ret = of_address_to_resource(np, 0, &res);
282 pseries_remove_memblock(res.start, resource_size(&res));
286 static bool lmb_is_removable(struct drmem_lmb *lmb)
288 if ((lmb->flags & DRCONF_MEM_RESERVED) ||
289 !(lmb->flags & DRCONF_MEM_ASSIGNED))
292 #ifdef CONFIG_FA_DUMP
294 * Don't hot-remove memory that falls in fadump boot memory area
295 * and memory that is reserved for capturing old kernel memory.
297 if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
300 /* device_offline() will determine if we can actually remove this lmb */
304 static int dlpar_add_lmb(struct drmem_lmb *);
306 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
308 struct memory_block *mem_block;
311 if (!lmb_is_removable(lmb))
314 mem_block = lmb_to_memblock(lmb);
315 if (mem_block == NULL)
318 rc = dlpar_offline_lmb(lmb);
320 put_device(&mem_block->dev);
324 __remove_memory(lmb->base_addr, memory_block_size);
325 put_device(&mem_block->dev);
327 /* Update memory regions for memory remove */
328 memblock_remove(lmb->base_addr, memory_block_size);
330 invalidate_lmb_associativity_index(lmb);
331 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
336 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
338 struct drmem_lmb *lmb;
339 int lmbs_reserved = 0;
340 int lmbs_available = 0;
343 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
345 if (lmbs_to_remove == 0)
348 /* Validate that there are enough LMBs to satisfy the request */
349 for_each_drmem_lmb(lmb) {
350 if (lmb_is_removable(lmb))
353 if (lmbs_available == lmbs_to_remove)
357 if (lmbs_available < lmbs_to_remove) {
358 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
359 lmbs_available, lmbs_to_remove);
363 for_each_drmem_lmb(lmb) {
364 rc = dlpar_remove_lmb(lmb);
368 /* Mark this lmb so we can add it later if all of the
369 * requested LMBs cannot be removed.
371 drmem_mark_lmb_reserved(lmb);
374 if (lmbs_reserved == lmbs_to_remove)
378 if (lmbs_reserved != lmbs_to_remove) {
379 pr_err("Memory hot-remove failed, adding LMB's back\n");
381 for_each_drmem_lmb(lmb) {
382 if (!drmem_lmb_reserved(lmb))
385 rc = dlpar_add_lmb(lmb);
387 pr_err("Failed to add LMB back, drc index %x\n",
390 drmem_remove_lmb_reservation(lmb);
393 if (lmbs_reserved == 0)
399 for_each_drmem_lmb(lmb) {
400 if (!drmem_lmb_reserved(lmb))
403 dlpar_release_drc(lmb->drc_index);
404 pr_info("Memory at %llx was hot-removed\n",
407 drmem_remove_lmb_reservation(lmb);
410 if (lmbs_reserved == 0)
419 static int dlpar_memory_remove_by_index(u32 drc_index)
421 struct drmem_lmb *lmb;
425 pr_debug("Attempting to hot-remove LMB, drc index %x\n", drc_index);
428 for_each_drmem_lmb(lmb) {
429 if (lmb->drc_index == drc_index) {
431 rc = dlpar_remove_lmb(lmb);
433 dlpar_release_drc(lmb->drc_index);
443 pr_debug("Failed to hot-remove memory at %llx\n",
446 pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
451 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
453 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
456 pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
457 lmbs_to_remove, drc_index);
459 if (lmbs_to_remove == 0)
462 rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
467 * Validate that all LMBs in range are not reserved. Note that it
468 * is ok if they are !ASSIGNED since our goal here is to remove the
469 * LMB range, regardless of whether some LMBs were already removed
470 * by any other reason.
472 * This is a contrast to what is done in remove_by_count() where we
473 * check for both RESERVED and !ASSIGNED (via lmb_is_removable()),
474 * because we want to remove a fixed amount of LMBs in that function.
476 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
477 if (lmb->flags & DRCONF_MEM_RESERVED) {
478 pr_err("Memory at %llx (drc index %x) is reserved\n",
479 lmb->base_addr, lmb->drc_index);
484 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
486 * dlpar_remove_lmb() will error out if the LMB is already
487 * !ASSIGNED, but this case is a no-op for us.
489 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
492 rc = dlpar_remove_lmb(lmb);
496 drmem_mark_lmb_reserved(lmb);
500 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
503 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
504 if (!drmem_lmb_reserved(lmb))
508 * Setting the isolation state of an UNISOLATED/CONFIGURED
509 * device to UNISOLATE is a no-op, but the hypervisor can
510 * use it as a hint that the LMB removal failed.
512 dlpar_unisolate_drc(lmb->drc_index);
514 rc = dlpar_add_lmb(lmb);
516 pr_err("Failed to add LMB, drc index %x\n",
519 drmem_remove_lmb_reservation(lmb);
523 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
524 if (!drmem_lmb_reserved(lmb))
527 dlpar_release_drc(lmb->drc_index);
528 pr_info("Memory at %llx (drc index %x) was hot-removed\n",
529 lmb->base_addr, lmb->drc_index);
531 drmem_remove_lmb_reservation(lmb);
539 static inline int pseries_remove_memblock(unsigned long base,
540 unsigned long memblock_size)
544 static inline int pseries_remove_mem_node(struct device_node *np)
548 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
552 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
556 static int dlpar_memory_remove_by_index(u32 drc_index)
561 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
565 #endif /* CONFIG_MEMORY_HOTREMOVE */
567 static int dlpar_add_lmb(struct drmem_lmb *lmb)
569 unsigned long block_sz;
572 if (lmb->flags & DRCONF_MEM_ASSIGNED)
575 rc = update_lmb_associativity_index(lmb);
577 dlpar_release_drc(lmb->drc_index);
581 block_sz = memory_block_size_bytes();
583 /* Find the node id for this LMB. Fake one if necessary. */
584 nid = of_drconf_to_nid_single(lmb);
585 if (nid < 0 || !node_possible(nid))
586 nid = first_online_node;
589 rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_MEMMAP_ON_MEMORY);
591 invalidate_lmb_associativity_index(lmb);
595 rc = dlpar_online_lmb(lmb);
597 __remove_memory(lmb->base_addr, block_sz);
598 invalidate_lmb_associativity_index(lmb);
600 lmb->flags |= DRCONF_MEM_ASSIGNED;
606 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
608 struct drmem_lmb *lmb;
609 int lmbs_available = 0;
610 int lmbs_reserved = 0;
613 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
615 if (lmbs_to_add == 0)
618 /* Validate that there are enough LMBs to satisfy the request */
619 for_each_drmem_lmb(lmb) {
620 if (lmb->flags & DRCONF_MEM_RESERVED)
623 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
626 if (lmbs_available == lmbs_to_add)
630 if (lmbs_available < lmbs_to_add)
633 for_each_drmem_lmb(lmb) {
634 if (lmb->flags & DRCONF_MEM_ASSIGNED)
637 rc = dlpar_acquire_drc(lmb->drc_index);
641 rc = dlpar_add_lmb(lmb);
643 dlpar_release_drc(lmb->drc_index);
647 /* Mark this lmb so we can remove it later if all of the
648 * requested LMBs cannot be added.
650 drmem_mark_lmb_reserved(lmb);
652 if (lmbs_reserved == lmbs_to_add)
656 if (lmbs_reserved != lmbs_to_add) {
657 pr_err("Memory hot-add failed, removing any added LMBs\n");
659 for_each_drmem_lmb(lmb) {
660 if (!drmem_lmb_reserved(lmb))
663 rc = dlpar_remove_lmb(lmb);
665 pr_err("Failed to remove LMB, drc index %x\n",
668 dlpar_release_drc(lmb->drc_index);
670 drmem_remove_lmb_reservation(lmb);
673 if (lmbs_reserved == 0)
678 for_each_drmem_lmb(lmb) {
679 if (!drmem_lmb_reserved(lmb))
682 pr_debug("Memory at %llx (drc index %x) was hot-added\n",
683 lmb->base_addr, lmb->drc_index);
684 drmem_remove_lmb_reservation(lmb);
687 if (lmbs_reserved == 0)
696 static int dlpar_memory_add_by_index(u32 drc_index)
698 struct drmem_lmb *lmb;
701 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
704 for_each_drmem_lmb(lmb) {
705 if (lmb->drc_index == drc_index) {
707 rc = dlpar_acquire_drc(lmb->drc_index);
709 rc = dlpar_add_lmb(lmb);
711 dlpar_release_drc(lmb->drc_index);
722 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
724 pr_info("Memory at %llx (drc index %x) was hot-added\n",
725 lmb->base_addr, drc_index);
730 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
732 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
735 pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
736 lmbs_to_add, drc_index);
738 if (lmbs_to_add == 0)
741 rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
745 /* Validate that the LMBs in this range are not reserved */
746 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
747 /* Fail immediately if the whole range can't be hot-added */
748 if (lmb->flags & DRCONF_MEM_RESERVED) {
749 pr_err("Memory at %llx (drc index %x) is reserved\n",
750 lmb->base_addr, lmb->drc_index);
755 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
756 if (lmb->flags & DRCONF_MEM_ASSIGNED)
759 rc = dlpar_acquire_drc(lmb->drc_index);
763 rc = dlpar_add_lmb(lmb);
765 dlpar_release_drc(lmb->drc_index);
769 drmem_mark_lmb_reserved(lmb);
773 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
775 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
776 if (!drmem_lmb_reserved(lmb))
779 rc = dlpar_remove_lmb(lmb);
781 pr_err("Failed to remove LMB, drc index %x\n",
784 dlpar_release_drc(lmb->drc_index);
786 drmem_remove_lmb_reservation(lmb);
790 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
791 if (!drmem_lmb_reserved(lmb))
794 pr_info("Memory at %llx (drc index %x) was hot-added\n",
795 lmb->base_addr, lmb->drc_index);
796 drmem_remove_lmb_reservation(lmb);
803 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
805 u32 count, drc_index;
808 lock_device_hotplug();
810 switch (hp_elog->action) {
811 case PSERIES_HP_ELOG_ACTION_ADD:
812 switch (hp_elog->id_type) {
813 case PSERIES_HP_ELOG_ID_DRC_COUNT:
814 count = hp_elog->_drc_u.drc_count;
815 rc = dlpar_memory_add_by_count(count);
817 case PSERIES_HP_ELOG_ID_DRC_INDEX:
818 drc_index = hp_elog->_drc_u.drc_index;
819 rc = dlpar_memory_add_by_index(drc_index);
821 case PSERIES_HP_ELOG_ID_DRC_IC:
822 count = hp_elog->_drc_u.ic.count;
823 drc_index = hp_elog->_drc_u.ic.index;
824 rc = dlpar_memory_add_by_ic(count, drc_index);
832 case PSERIES_HP_ELOG_ACTION_REMOVE:
833 switch (hp_elog->id_type) {
834 case PSERIES_HP_ELOG_ID_DRC_COUNT:
835 count = hp_elog->_drc_u.drc_count;
836 rc = dlpar_memory_remove_by_count(count);
838 case PSERIES_HP_ELOG_ID_DRC_INDEX:
839 drc_index = hp_elog->_drc_u.drc_index;
840 rc = dlpar_memory_remove_by_index(drc_index);
842 case PSERIES_HP_ELOG_ID_DRC_IC:
843 count = hp_elog->_drc_u.ic.count;
844 drc_index = hp_elog->_drc_u.ic.index;
845 rc = dlpar_memory_remove_by_ic(count, drc_index);
854 pr_err("Invalid action (%d) specified\n", hp_elog->action);
860 rc = drmem_update_dt();
862 unlock_device_hotplug();
866 static int pseries_add_mem_node(struct device_node *np)
872 * Check to see if we are actually adding memory
874 if (!of_node_is_type(np, "memory"))
878 * Find the base and size of the memblock
880 ret = of_address_to_resource(np, 0, &res);
885 * Update memory region to represent the memory add
887 ret = memblock_add(res.start, resource_size(&res));
888 return (ret < 0) ? -EINVAL : 0;
891 static int pseries_memory_notifier(struct notifier_block *nb,
892 unsigned long action, void *data)
894 struct of_reconfig_data *rd = data;
898 case OF_RECONFIG_ATTACH_NODE:
899 err = pseries_add_mem_node(rd->dn);
901 case OF_RECONFIG_DETACH_NODE:
902 err = pseries_remove_mem_node(rd->dn);
904 case OF_RECONFIG_UPDATE_PROPERTY:
905 if (!strcmp(rd->dn->name,
906 "ibm,dynamic-reconfiguration-memory"))
907 drmem_update_lmbs(rd->prop);
909 return notifier_from_errno(err);
912 static struct notifier_block pseries_mem_nb = {
913 .notifier_call = pseries_memory_notifier,
916 static int __init pseries_memory_hotplug_init(void)
918 if (firmware_has_feature(FW_FEATURE_LPAR))
919 of_reconfig_notifier_register(&pseries_mem_nb);
923 machine_device_initcall(pseries, pseries_memory_hotplug_init);