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 mem_section *mem_sect;
217 struct memory_block *mem_block;
219 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
220 mem_sect = __nr_to_section(section_nr);
222 mem_block = find_memory_block(mem_sect);
226 static int get_lmb_range(u32 drc_index, int n_lmbs,
227 struct drmem_lmb **start_lmb,
228 struct drmem_lmb **end_lmb)
230 struct drmem_lmb *lmb, *start, *end;
231 struct drmem_lmb *limit;
234 for_each_drmem_lmb(lmb) {
235 if (lmb->drc_index == drc_index) {
244 end = &start[n_lmbs];
246 limit = &drmem_info->lmbs[drmem_info->n_lmbs];
255 static int dlpar_change_lmb_state(struct drmem_lmb *lmb, bool online)
257 struct memory_block *mem_block;
260 mem_block = lmb_to_memblock(lmb);
264 if (online && mem_block->dev.offline)
265 rc = device_online(&mem_block->dev);
266 else if (!online && !mem_block->dev.offline)
267 rc = device_offline(&mem_block->dev);
271 put_device(&mem_block->dev);
276 static int dlpar_online_lmb(struct drmem_lmb *lmb)
278 return dlpar_change_lmb_state(lmb, true);
281 #ifdef CONFIG_MEMORY_HOTREMOVE
282 static int dlpar_offline_lmb(struct drmem_lmb *lmb)
284 return dlpar_change_lmb_state(lmb, false);
287 static int pseries_remove_memblock(unsigned long base, unsigned long memblock_size)
289 unsigned long block_sz, start_pfn;
290 int sections_per_block;
293 start_pfn = base >> PAGE_SHIFT;
295 lock_device_hotplug();
297 if (!pfn_valid(start_pfn))
300 block_sz = pseries_memory_block_size();
301 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
302 nid = memory_add_physaddr_to_nid(base);
304 for (i = 0; i < sections_per_block; i++) {
305 __remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
306 base += MIN_MEMORY_BLOCK_SIZE;
310 /* Update memory regions for memory remove */
311 memblock_remove(base, memblock_size);
312 unlock_device_hotplug();
316 static int pseries_remove_mem_node(struct device_node *np)
320 unsigned long lmb_size;
322 int addr_cells, size_cells;
325 * Check to see if we are actually removing memory
327 if (!of_node_is_type(np, "memory"))
331 * Find the base address and size of the memblock
333 prop = of_get_property(np, "reg", NULL);
337 addr_cells = of_n_addr_cells(np);
338 size_cells = of_n_size_cells(np);
341 * "reg" property represents (addr,size) tuple.
343 base = of_read_number(prop, addr_cells);
345 lmb_size = of_read_number(prop, size_cells);
347 pseries_remove_memblock(base, lmb_size);
351 static bool lmb_is_removable(struct drmem_lmb *lmb)
353 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
356 #ifdef CONFIG_FA_DUMP
358 * Don't hot-remove memory that falls in fadump boot memory area
359 * and memory that is reserved for capturing old kernel memory.
361 if (is_fadump_memory_area(lmb->base_addr, memory_block_size_bytes()))
364 /* device_offline() will determine if we can actually remove this lmb */
368 static int dlpar_add_lmb(struct drmem_lmb *);
370 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
372 struct memory_block *mem_block;
373 unsigned long block_sz;
376 if (!lmb_is_removable(lmb))
379 mem_block = lmb_to_memblock(lmb);
380 if (mem_block == NULL)
383 rc = dlpar_offline_lmb(lmb);
385 put_device(&mem_block->dev);
389 block_sz = pseries_memory_block_size();
391 __remove_memory(mem_block->nid, lmb->base_addr, block_sz);
392 put_device(&mem_block->dev);
394 /* Update memory regions for memory remove */
395 memblock_remove(lmb->base_addr, block_sz);
397 invalidate_lmb_associativity_index(lmb);
398 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
403 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
405 struct drmem_lmb *lmb;
406 int lmbs_removed = 0;
407 int lmbs_available = 0;
410 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
412 if (lmbs_to_remove == 0)
415 /* Validate that there are enough LMBs to satisfy the request */
416 for_each_drmem_lmb(lmb) {
417 if (lmb_is_removable(lmb))
420 if (lmbs_available == lmbs_to_remove)
424 if (lmbs_available < lmbs_to_remove) {
425 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
426 lmbs_available, lmbs_to_remove);
430 for_each_drmem_lmb(lmb) {
431 rc = dlpar_remove_lmb(lmb);
435 /* Mark this lmb so we can add it later if all of the
436 * requested LMBs cannot be removed.
438 drmem_mark_lmb_reserved(lmb);
441 if (lmbs_removed == lmbs_to_remove)
445 if (lmbs_removed != lmbs_to_remove) {
446 pr_err("Memory hot-remove failed, adding LMB's back\n");
448 for_each_drmem_lmb(lmb) {
449 if (!drmem_lmb_reserved(lmb))
452 rc = dlpar_add_lmb(lmb);
454 pr_err("Failed to add LMB back, drc index %x\n",
457 drmem_remove_lmb_reservation(lmb);
462 for_each_drmem_lmb(lmb) {
463 if (!drmem_lmb_reserved(lmb))
466 dlpar_release_drc(lmb->drc_index);
467 pr_info("Memory at %llx was hot-removed\n",
470 drmem_remove_lmb_reservation(lmb);
478 static int dlpar_memory_remove_by_index(u32 drc_index)
480 struct drmem_lmb *lmb;
484 pr_debug("Attempting to hot-remove LMB, drc index %x\n", drc_index);
487 for_each_drmem_lmb(lmb) {
488 if (lmb->drc_index == drc_index) {
490 rc = dlpar_remove_lmb(lmb);
492 dlpar_release_drc(lmb->drc_index);
499 pr_debug("Failed to look up LMB for drc index %x\n", drc_index);
502 pr_debug("Failed to hot-remove memory at %llx\n",
505 pr_debug("Memory at %llx was hot-removed\n", lmb->base_addr);
511 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
513 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
514 int lmbs_available = 0;
517 pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
518 lmbs_to_remove, drc_index);
520 if (lmbs_to_remove == 0)
523 rc = get_lmb_range(drc_index, lmbs_to_remove, &start_lmb, &end_lmb);
527 /* Validate that there are enough LMBs to satisfy the request */
528 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
529 if (lmb->flags & DRCONF_MEM_RESERVED)
535 if (lmbs_available < lmbs_to_remove)
538 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
539 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
542 rc = dlpar_remove_lmb(lmb);
546 drmem_mark_lmb_reserved(lmb);
550 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
553 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
554 if (!drmem_lmb_reserved(lmb))
557 rc = dlpar_add_lmb(lmb);
559 pr_err("Failed to add LMB, drc index %x\n",
562 drmem_remove_lmb_reservation(lmb);
566 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
567 if (!drmem_lmb_reserved(lmb))
570 dlpar_release_drc(lmb->drc_index);
571 pr_info("Memory at %llx (drc index %x) was hot-removed\n",
572 lmb->base_addr, lmb->drc_index);
574 drmem_remove_lmb_reservation(lmb);
582 static inline int pseries_remove_memblock(unsigned long base,
583 unsigned long memblock_size)
587 static inline int pseries_remove_mem_node(struct device_node *np)
591 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
595 static int dlpar_remove_lmb(struct drmem_lmb *lmb)
599 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove)
603 static int dlpar_memory_remove_by_index(u32 drc_index)
608 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index)
612 #endif /* CONFIG_MEMORY_HOTREMOVE */
614 static int dlpar_add_lmb(struct drmem_lmb *lmb)
616 unsigned long block_sz;
619 if (lmb->flags & DRCONF_MEM_ASSIGNED)
622 rc = update_lmb_associativity_index(lmb);
624 dlpar_release_drc(lmb->drc_index);
628 block_sz = memory_block_size_bytes();
630 /* Find the node id for this LMB. Fake one if necessary. */
631 nid = of_drconf_to_nid_single(lmb);
632 if (nid < 0 || !node_possible(nid))
633 nid = first_online_node;
636 rc = __add_memory(nid, lmb->base_addr, block_sz, MHP_NONE);
638 invalidate_lmb_associativity_index(lmb);
642 rc = dlpar_online_lmb(lmb);
644 __remove_memory(nid, lmb->base_addr, block_sz);
645 invalidate_lmb_associativity_index(lmb);
647 lmb->flags |= DRCONF_MEM_ASSIGNED;
653 static int dlpar_memory_add_by_count(u32 lmbs_to_add)
655 struct drmem_lmb *lmb;
656 int lmbs_available = 0;
660 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
662 if (lmbs_to_add == 0)
665 /* Validate that there are enough LMBs to satisfy the request */
666 for_each_drmem_lmb(lmb) {
667 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
670 if (lmbs_available == lmbs_to_add)
674 if (lmbs_available < lmbs_to_add)
677 for_each_drmem_lmb(lmb) {
678 if (lmb->flags & DRCONF_MEM_ASSIGNED)
681 rc = dlpar_acquire_drc(lmb->drc_index);
685 rc = dlpar_add_lmb(lmb);
687 dlpar_release_drc(lmb->drc_index);
691 /* Mark this lmb so we can remove it later if all of the
692 * requested LMBs cannot be added.
694 drmem_mark_lmb_reserved(lmb);
697 if (lmbs_added == lmbs_to_add)
701 if (lmbs_added != lmbs_to_add) {
702 pr_err("Memory hot-add failed, removing any added LMBs\n");
704 for_each_drmem_lmb(lmb) {
705 if (!drmem_lmb_reserved(lmb))
708 rc = dlpar_remove_lmb(lmb);
710 pr_err("Failed to remove LMB, drc index %x\n",
713 dlpar_release_drc(lmb->drc_index);
715 drmem_remove_lmb_reservation(lmb);
719 for_each_drmem_lmb(lmb) {
720 if (!drmem_lmb_reserved(lmb))
723 pr_debug("Memory at %llx (drc index %x) was hot-added\n",
724 lmb->base_addr, lmb->drc_index);
725 drmem_remove_lmb_reservation(lmb);
733 static int dlpar_memory_add_by_index(u32 drc_index)
735 struct drmem_lmb *lmb;
738 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
741 for_each_drmem_lmb(lmb) {
742 if (lmb->drc_index == drc_index) {
744 rc = dlpar_acquire_drc(lmb->drc_index);
746 rc = dlpar_add_lmb(lmb);
748 dlpar_release_drc(lmb->drc_index);
759 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
761 pr_info("Memory at %llx (drc index %x) was hot-added\n",
762 lmb->base_addr, drc_index);
767 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index)
769 struct drmem_lmb *lmb, *start_lmb, *end_lmb;
770 int lmbs_available = 0;
773 pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
774 lmbs_to_add, drc_index);
776 if (lmbs_to_add == 0)
779 rc = get_lmb_range(drc_index, lmbs_to_add, &start_lmb, &end_lmb);
783 /* Validate that the LMBs in this range are not reserved */
784 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
785 if (lmb->flags & DRCONF_MEM_RESERVED)
791 if (lmbs_available < lmbs_to_add)
794 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
795 if (lmb->flags & DRCONF_MEM_ASSIGNED)
798 rc = dlpar_acquire_drc(lmb->drc_index);
802 rc = dlpar_add_lmb(lmb);
804 dlpar_release_drc(lmb->drc_index);
808 drmem_mark_lmb_reserved(lmb);
812 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
814 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
815 if (!drmem_lmb_reserved(lmb))
818 rc = dlpar_remove_lmb(lmb);
820 pr_err("Failed to remove LMB, drc index %x\n",
823 dlpar_release_drc(lmb->drc_index);
825 drmem_remove_lmb_reservation(lmb);
829 for_each_drmem_lmb_in_range(lmb, start_lmb, end_lmb) {
830 if (!drmem_lmb_reserved(lmb))
833 pr_info("Memory at %llx (drc index %x) was hot-added\n",
834 lmb->base_addr, lmb->drc_index);
835 drmem_remove_lmb_reservation(lmb);
842 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
844 u32 count, drc_index;
847 lock_device_hotplug();
849 switch (hp_elog->action) {
850 case PSERIES_HP_ELOG_ACTION_ADD:
851 switch (hp_elog->id_type) {
852 case PSERIES_HP_ELOG_ID_DRC_COUNT:
853 count = hp_elog->_drc_u.drc_count;
854 rc = dlpar_memory_add_by_count(count);
856 case PSERIES_HP_ELOG_ID_DRC_INDEX:
857 drc_index = hp_elog->_drc_u.drc_index;
858 rc = dlpar_memory_add_by_index(drc_index);
860 case PSERIES_HP_ELOG_ID_DRC_IC:
861 count = hp_elog->_drc_u.ic.count;
862 drc_index = hp_elog->_drc_u.ic.index;
863 rc = dlpar_memory_add_by_ic(count, drc_index);
871 case PSERIES_HP_ELOG_ACTION_REMOVE:
872 switch (hp_elog->id_type) {
873 case PSERIES_HP_ELOG_ID_DRC_COUNT:
874 count = hp_elog->_drc_u.drc_count;
875 rc = dlpar_memory_remove_by_count(count);
877 case PSERIES_HP_ELOG_ID_DRC_INDEX:
878 drc_index = hp_elog->_drc_u.drc_index;
879 rc = dlpar_memory_remove_by_index(drc_index);
881 case PSERIES_HP_ELOG_ID_DRC_IC:
882 count = hp_elog->_drc_u.ic.count;
883 drc_index = hp_elog->_drc_u.ic.index;
884 rc = dlpar_memory_remove_by_ic(count, drc_index);
893 pr_err("Invalid action (%d) specified\n", hp_elog->action);
899 rc = drmem_update_dt();
901 unlock_device_hotplug();
905 static int pseries_add_mem_node(struct device_node *np)
909 unsigned long lmb_size;
911 int addr_cells, size_cells;
914 * Check to see if we are actually adding memory
916 if (!of_node_is_type(np, "memory"))
920 * Find the base and size of the memblock
922 prop = of_get_property(np, "reg", NULL);
926 addr_cells = of_n_addr_cells(np);
927 size_cells = of_n_size_cells(np);
929 * "reg" property represents (addr,size) tuple.
931 base = of_read_number(prop, addr_cells);
933 lmb_size = of_read_number(prop, size_cells);
936 * Update memory region to represent the memory add
938 ret = memblock_add(base, lmb_size);
939 return (ret < 0) ? -EINVAL : 0;
942 static int pseries_memory_notifier(struct notifier_block *nb,
943 unsigned long action, void *data)
945 struct of_reconfig_data *rd = data;
949 case OF_RECONFIG_ATTACH_NODE:
950 err = pseries_add_mem_node(rd->dn);
952 case OF_RECONFIG_DETACH_NODE:
953 err = pseries_remove_mem_node(rd->dn);
956 return notifier_from_errno(err);
959 static struct notifier_block pseries_mem_nb = {
960 .notifier_call = pseries_memory_notifier,
963 static int __init pseries_memory_hotplug_init(void)
965 if (firmware_has_feature(FW_FEATURE_LPAR))
966 of_reconfig_notifier_register(&pseries_mem_nb);
970 machine_device_initcall(pseries, pseries_memory_hotplug_init);