2 * pseries Memory Hotplug infrastructure.
4 * Copyright (C) 2008 Badari Pulavarty, IBM Corporation
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define pr_fmt(fmt) "pseries-hotplug-mem: " fmt
15 #include <linux/of_address.h>
16 #include <linux/memblock.h>
17 #include <linux/memory.h>
18 #include <linux/memory_hotplug.h>
19 #include <linux/slab.h>
21 #include <asm/firmware.h>
22 #include <asm/machdep.h>
24 #include <asm/sparsemem.h>
25 #include <asm/fadump.h>
28 static bool rtas_hp_event;
30 unsigned long pseries_memory_block_size(void)
32 struct device_node *np;
33 unsigned int memblock_size = MIN_MEMORY_BLOCK_SIZE;
36 np = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
40 size = of_get_property(np, "ibm,lmb-size", NULL);
42 memblock_size = be64_to_cpup(size);
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 struct property *dlpar_clone_drconf_property(struct device_node *dn)
105 struct property *prop, *new_prop;
106 struct of_drconf_cell *lmbs;
110 prop = of_find_property(dn, "ibm,dynamic-memory", NULL);
114 new_prop = dlpar_clone_property(prop, prop->length);
118 /* Convert the property to cpu endian-ness */
120 *p = be32_to_cpu(*p);
123 lmbs = (struct of_drconf_cell *)p;
125 for (i = 0; i < num_lmbs; i++) {
126 lmbs[i].base_addr = be64_to_cpu(lmbs[i].base_addr);
127 lmbs[i].drc_index = be32_to_cpu(lmbs[i].drc_index);
128 lmbs[i].aa_index = be32_to_cpu(lmbs[i].aa_index);
129 lmbs[i].flags = be32_to_cpu(lmbs[i].flags);
135 static void dlpar_update_drconf_property(struct device_node *dn,
136 struct property *prop)
138 struct of_drconf_cell *lmbs;
142 /* Convert the property back to BE */
145 *p = cpu_to_be32(*p);
148 lmbs = (struct of_drconf_cell *)p;
149 for (i = 0; i < num_lmbs; i++) {
150 lmbs[i].base_addr = cpu_to_be64(lmbs[i].base_addr);
151 lmbs[i].drc_index = cpu_to_be32(lmbs[i].drc_index);
152 lmbs[i].aa_index = cpu_to_be32(lmbs[i].aa_index);
153 lmbs[i].flags = cpu_to_be32(lmbs[i].flags);
156 rtas_hp_event = true;
157 of_update_property(dn, prop);
158 rtas_hp_event = false;
161 static int dlpar_update_device_tree_lmb(struct of_drconf_cell *lmb)
163 struct device_node *dn;
164 struct property *prop;
165 struct of_drconf_cell *lmbs;
169 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
173 prop = dlpar_clone_drconf_property(dn);
181 lmbs = (struct of_drconf_cell *)p;
183 for (i = 0; i < num_lmbs; i++) {
184 if (lmbs[i].drc_index == lmb->drc_index) {
185 lmbs[i].flags = lmb->flags;
186 lmbs[i].aa_index = lmb->aa_index;
188 dlpar_update_drconf_property(dn, prop);
197 static u32 find_aa_index(struct device_node *dr_node,
198 struct property *ala_prop, const u32 *lmb_assoc)
202 int aa_arrays, aa_array_entries, aa_array_sz;
206 * The ibm,associativity-lookup-arrays property is defined to be
207 * a 32-bit value specifying the number of associativity arrays
208 * followed by a 32-bitvalue specifying the number of entries per
209 * array, followed by the associativity arrays.
211 assoc_arrays = ala_prop->value;
213 aa_arrays = be32_to_cpu(assoc_arrays[0]);
214 aa_array_entries = be32_to_cpu(assoc_arrays[1]);
215 aa_array_sz = aa_array_entries * sizeof(u32);
218 for (i = 0; i < aa_arrays; i++) {
219 index = (i * aa_array_entries) + 2;
221 if (memcmp(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz))
228 if (aa_index == -1) {
229 struct property *new_prop;
232 new_prop_size = ala_prop->length + aa_array_sz;
233 new_prop = dlpar_clone_property(ala_prop, new_prop_size);
237 assoc_arrays = new_prop->value;
239 /* increment the number of entries in the lookup array */
240 assoc_arrays[0] = cpu_to_be32(aa_arrays + 1);
242 /* copy the new associativity into the lookup array */
243 index = aa_arrays * aa_array_entries + 2;
244 memcpy(&assoc_arrays[index], &lmb_assoc[1], aa_array_sz);
246 of_update_property(dr_node, new_prop);
249 * The associativity lookup array index for this lmb is
250 * number of entries - 1 since we added its associativity
251 * to the end of the lookup array.
253 aa_index = be32_to_cpu(assoc_arrays[0]) - 1;
259 static u32 lookup_lmb_associativity_index(struct of_drconf_cell *lmb)
261 struct device_node *parent, *lmb_node, *dr_node;
262 struct property *ala_prop;
263 const u32 *lmb_assoc;
266 parent = of_find_node_by_path("/");
270 lmb_node = dlpar_configure_connector(cpu_to_be32(lmb->drc_index),
276 lmb_assoc = of_get_property(lmb_node, "ibm,associativity", NULL);
278 dlpar_free_cc_nodes(lmb_node);
282 dr_node = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
284 dlpar_free_cc_nodes(lmb_node);
288 ala_prop = of_find_property(dr_node, "ibm,associativity-lookup-arrays",
291 of_node_put(dr_node);
292 dlpar_free_cc_nodes(lmb_node);
296 aa_index = find_aa_index(dr_node, ala_prop, lmb_assoc);
298 dlpar_free_cc_nodes(lmb_node);
302 static int dlpar_add_device_tree_lmb(struct of_drconf_cell *lmb)
306 lmb->flags |= DRCONF_MEM_ASSIGNED;
308 aa_index = lookup_lmb_associativity_index(lmb);
310 pr_err("Couldn't find associativity index for drc index %x\n",
315 lmb->aa_index = aa_index;
316 return dlpar_update_device_tree_lmb(lmb);
319 static int dlpar_remove_device_tree_lmb(struct of_drconf_cell *lmb)
321 lmb->flags &= ~DRCONF_MEM_ASSIGNED;
322 lmb->aa_index = 0xffffffff;
323 return dlpar_update_device_tree_lmb(lmb);
326 static struct memory_block *lmb_to_memblock(struct of_drconf_cell *lmb)
328 unsigned long section_nr;
329 struct mem_section *mem_sect;
330 struct memory_block *mem_block;
332 section_nr = pfn_to_section_nr(PFN_DOWN(lmb->base_addr));
333 mem_sect = __nr_to_section(section_nr);
335 mem_block = find_memory_block(mem_sect);
339 #ifdef CONFIG_MEMORY_HOTREMOVE
340 static int pseries_remove_memblock(unsigned long base, unsigned int memblock_size)
342 unsigned long block_sz, start_pfn;
343 int sections_per_block;
346 start_pfn = base >> PAGE_SHIFT;
348 lock_device_hotplug();
350 if (!pfn_valid(start_pfn))
353 block_sz = pseries_memory_block_size();
354 sections_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
355 nid = memory_add_physaddr_to_nid(base);
357 for (i = 0; i < sections_per_block; i++) {
358 remove_memory(nid, base, MIN_MEMORY_BLOCK_SIZE);
359 base += MIN_MEMORY_BLOCK_SIZE;
363 /* Update memory regions for memory remove */
364 memblock_remove(base, memblock_size);
365 unlock_device_hotplug();
369 static int pseries_remove_mem_node(struct device_node *np)
374 unsigned int lmb_size;
378 * Check to see if we are actually removing memory
380 type = of_get_property(np, "device_type", NULL);
381 if (type == NULL || strcmp(type, "memory") != 0)
385 * Find the base address and size of the memblock
387 regs = of_get_property(np, "reg", NULL);
391 base = be64_to_cpu(*(unsigned long *)regs);
392 lmb_size = be32_to_cpu(regs[3]);
394 pseries_remove_memblock(base, lmb_size);
398 static bool lmb_is_removable(struct of_drconf_cell *lmb)
400 int i, scns_per_block;
402 unsigned long pfn, block_sz;
405 if (!(lmb->flags & DRCONF_MEM_ASSIGNED))
408 block_sz = memory_block_size_bytes();
409 scns_per_block = block_sz / MIN_MEMORY_BLOCK_SIZE;
410 phys_addr = lmb->base_addr;
412 #ifdef CONFIG_FA_DUMP
413 /* Don't hot-remove memory that falls in fadump boot memory area */
414 if (is_fadump_boot_memory_area(phys_addr, block_sz))
418 for (i = 0; i < scns_per_block; i++) {
419 pfn = PFN_DOWN(phys_addr);
420 if (!pfn_present(pfn))
423 rc &= is_mem_section_removable(pfn, PAGES_PER_SECTION);
424 phys_addr += MIN_MEMORY_BLOCK_SIZE;
427 return rc ? true : false;
430 static int dlpar_add_lmb(struct of_drconf_cell *);
432 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
434 struct memory_block *mem_block;
435 unsigned long block_sz;
438 if (!lmb_is_removable(lmb))
441 mem_block = lmb_to_memblock(lmb);
445 rc = device_offline(&mem_block->dev);
446 put_device(&mem_block->dev);
450 block_sz = pseries_memory_block_size();
451 nid = memory_add_physaddr_to_nid(lmb->base_addr);
453 remove_memory(nid, lmb->base_addr, block_sz);
455 /* Update memory regions for memory remove */
456 memblock_remove(lmb->base_addr, block_sz);
458 dlpar_remove_device_tree_lmb(lmb);
462 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
463 struct property *prop)
465 struct of_drconf_cell *lmbs;
466 int lmbs_removed = 0;
467 int lmbs_available = 0;
471 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove);
473 if (lmbs_to_remove == 0)
478 lmbs = (struct of_drconf_cell *)p;
480 /* Validate that there are enough LMBs to satisfy the request */
481 for (i = 0; i < num_lmbs; i++) {
482 if (lmb_is_removable(&lmbs[i]))
486 if (lmbs_available < lmbs_to_remove) {
487 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
488 lmbs_available, lmbs_to_remove);
492 for (i = 0; i < num_lmbs && lmbs_removed < lmbs_to_remove; i++) {
493 rc = dlpar_remove_lmb(&lmbs[i]);
499 /* Mark this lmb so we can add it later if all of the
500 * requested LMBs cannot be removed.
502 lmbs[i].reserved = 1;
505 if (lmbs_removed != lmbs_to_remove) {
506 pr_err("Memory hot-remove failed, adding LMB's back\n");
508 for (i = 0; i < num_lmbs; i++) {
509 if (!lmbs[i].reserved)
512 rc = dlpar_add_lmb(&lmbs[i]);
514 pr_err("Failed to add LMB back, drc index %x\n",
517 lmbs[i].reserved = 0;
522 for (i = 0; i < num_lmbs; i++) {
523 if (!lmbs[i].reserved)
526 dlpar_release_drc(lmbs[i].drc_index);
527 pr_info("Memory at %llx was hot-removed\n",
530 lmbs[i].reserved = 0;
538 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
540 struct of_drconf_cell *lmbs;
545 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index);
549 lmbs = (struct of_drconf_cell *)p;
552 for (i = 0; i < num_lmbs; i++) {
553 if (lmbs[i].drc_index == drc_index) {
555 rc = dlpar_remove_lmb(&lmbs[i]);
557 dlpar_release_drc(lmbs[i].drc_index);
567 pr_info("Failed to hot-remove memory at %llx\n",
570 pr_info("Memory at %llx was hot-removed\n", lmbs[i].base_addr);
575 static int dlpar_memory_readd_by_index(u32 drc_index, struct property *prop)
577 struct of_drconf_cell *lmbs;
582 pr_info("Attempting to update LMB, drc index %x\n", drc_index);
586 lmbs = (struct of_drconf_cell *)p;
589 for (i = 0; i < num_lmbs; i++) {
590 if (lmbs[i].drc_index == drc_index) {
592 rc = dlpar_remove_lmb(&lmbs[i]);
594 rc = dlpar_add_lmb(&lmbs[i]);
596 dlpar_release_drc(lmbs[i].drc_index);
606 pr_info("Failed to update memory at %llx\n",
609 pr_info("Memory at %llx was updated\n", lmbs[i].base_addr);
614 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index,
615 struct property *prop)
617 struct of_drconf_cell *lmbs;
619 int i, rc, start_lmb_found;
620 int lmbs_available = 0, start_index = 0, end_index;
622 pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
623 lmbs_to_remove, drc_index);
625 if (lmbs_to_remove == 0)
630 lmbs = (struct of_drconf_cell *)p;
633 /* Navigate to drc_index */
634 while (start_index < num_lmbs) {
635 if (lmbs[start_index].drc_index == drc_index) {
643 if (!start_lmb_found)
646 end_index = start_index + lmbs_to_remove;
648 /* Validate that there are enough LMBs to satisfy the request */
649 for (i = start_index; i < end_index; i++) {
650 if (lmbs[i].flags & DRCONF_MEM_RESERVED)
656 if (lmbs_available < lmbs_to_remove)
659 for (i = start_index; i < end_index; i++) {
660 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
663 rc = dlpar_remove_lmb(&lmbs[i]);
667 lmbs[i].reserved = 1;
671 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
673 for (i = start_index; i < end_index; i++) {
674 if (!lmbs[i].reserved)
677 rc = dlpar_add_lmb(&lmbs[i]);
679 pr_err("Failed to add LMB, drc index %x\n",
680 be32_to_cpu(lmbs[i].drc_index));
682 lmbs[i].reserved = 0;
686 for (i = start_index; i < end_index; i++) {
687 if (!lmbs[i].reserved)
690 dlpar_release_drc(lmbs[i].drc_index);
691 pr_info("Memory at %llx (drc index %x) was hot-removed\n",
692 lmbs[i].base_addr, lmbs[i].drc_index);
694 lmbs[i].reserved = 0;
702 static inline int pseries_remove_memblock(unsigned long base,
703 unsigned int memblock_size)
707 static inline int pseries_remove_mem_node(struct device_node *np)
711 static inline int dlpar_memory_remove(struct pseries_hp_errorlog *hp_elog)
715 static int dlpar_remove_lmb(struct of_drconf_cell *lmb)
719 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove,
720 struct property *prop)
724 static int dlpar_memory_remove_by_index(u32 drc_index, struct property *prop)
728 static int dlpar_memory_readd_by_index(u32 drc_index, struct property *prop)
733 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove, u32 drc_index,
734 struct property *prop)
738 #endif /* CONFIG_MEMORY_HOTREMOVE */
740 static int dlpar_online_lmb(struct of_drconf_cell *lmb)
742 struct memory_block *mem_block;
745 mem_block = lmb_to_memblock(lmb);
749 rc = device_online(&mem_block->dev);
750 put_device(&mem_block->dev);
754 static int dlpar_add_lmb(struct of_drconf_cell *lmb)
756 unsigned long block_sz;
759 if (lmb->flags & DRCONF_MEM_ASSIGNED)
762 rc = dlpar_add_device_tree_lmb(lmb);
764 pr_err("Couldn't update device tree for drc index %x\n",
766 dlpar_release_drc(lmb->drc_index);
770 block_sz = memory_block_size_bytes();
772 /* Find the node id for this address */
773 nid = memory_add_physaddr_to_nid(lmb->base_addr);
776 rc = add_memory(nid, lmb->base_addr, block_sz);
778 dlpar_remove_device_tree_lmb(lmb);
782 rc = dlpar_online_lmb(lmb);
784 remove_memory(nid, lmb->base_addr, block_sz);
785 dlpar_remove_device_tree_lmb(lmb);
787 lmb->flags |= DRCONF_MEM_ASSIGNED;
793 static int dlpar_memory_add_by_count(u32 lmbs_to_add, struct property *prop)
795 struct of_drconf_cell *lmbs;
797 int lmbs_available = 0;
801 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add);
803 if (lmbs_to_add == 0)
808 lmbs = (struct of_drconf_cell *)p;
810 /* Validate that there are enough LMBs to satisfy the request */
811 for (i = 0; i < num_lmbs; i++) {
812 if (!(lmbs[i].flags & DRCONF_MEM_ASSIGNED))
816 if (lmbs_available < lmbs_to_add)
819 for (i = 0; i < num_lmbs && lmbs_to_add != lmbs_added; i++) {
820 rc = dlpar_acquire_drc(lmbs[i].drc_index);
824 rc = dlpar_add_lmb(&lmbs[i]);
826 dlpar_release_drc(lmbs[i].drc_index);
832 /* Mark this lmb so we can remove it later if all of the
833 * requested LMBs cannot be added.
835 lmbs[i].reserved = 1;
838 if (lmbs_added != lmbs_to_add) {
839 pr_err("Memory hot-add failed, removing any added LMBs\n");
841 for (i = 0; i < num_lmbs; i++) {
842 if (!lmbs[i].reserved)
845 rc = dlpar_remove_lmb(&lmbs[i]);
847 pr_err("Failed to remove LMB, drc index %x\n",
848 be32_to_cpu(lmbs[i].drc_index));
850 dlpar_release_drc(lmbs[i].drc_index);
854 for (i = 0; i < num_lmbs; i++) {
855 if (!lmbs[i].reserved)
858 pr_info("Memory at %llx (drc index %x) was hot-added\n",
859 lmbs[i].base_addr, lmbs[i].drc_index);
860 lmbs[i].reserved = 0;
867 static int dlpar_memory_add_by_index(u32 drc_index, struct property *prop)
869 struct of_drconf_cell *lmbs;
874 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index);
878 lmbs = (struct of_drconf_cell *)p;
881 for (i = 0; i < num_lmbs; i++) {
882 if (lmbs[i].drc_index == drc_index) {
884 rc = dlpar_acquire_drc(lmbs[i].drc_index);
886 rc = dlpar_add_lmb(&lmbs[i]);
888 dlpar_release_drc(lmbs[i].drc_index);
899 pr_info("Failed to hot-add memory, drc index %x\n", drc_index);
901 pr_info("Memory at %llx (drc index %x) was hot-added\n",
902 lmbs[i].base_addr, drc_index);
907 static int dlpar_memory_add_by_ic(u32 lmbs_to_add, u32 drc_index,
908 struct property *prop)
910 struct of_drconf_cell *lmbs;
912 int i, rc, start_lmb_found;
913 int lmbs_available = 0, start_index = 0, end_index;
915 pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
916 lmbs_to_add, drc_index);
918 if (lmbs_to_add == 0)
923 lmbs = (struct of_drconf_cell *)p;
926 /* Navigate to drc_index */
927 while (start_index < num_lmbs) {
928 if (lmbs[start_index].drc_index == drc_index) {
936 if (!start_lmb_found)
939 end_index = start_index + lmbs_to_add;
941 /* Validate that the LMBs in this range are not reserved */
942 for (i = start_index; i < end_index; i++) {
943 if (lmbs[i].flags & DRCONF_MEM_RESERVED)
949 if (lmbs_available < lmbs_to_add)
952 for (i = start_index; i < end_index; i++) {
953 if (lmbs[i].flags & DRCONF_MEM_ASSIGNED)
956 rc = dlpar_acquire_drc(lmbs[i].drc_index);
960 rc = dlpar_add_lmb(&lmbs[i]);
962 dlpar_release_drc(lmbs[i].drc_index);
966 lmbs[i].reserved = 1;
970 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
972 for (i = start_index; i < end_index; i++) {
973 if (!lmbs[i].reserved)
976 rc = dlpar_remove_lmb(&lmbs[i]);
978 pr_err("Failed to remove LMB, drc index %x\n",
979 be32_to_cpu(lmbs[i].drc_index));
981 dlpar_release_drc(lmbs[i].drc_index);
985 for (i = start_index; i < end_index; i++) {
986 if (!lmbs[i].reserved)
989 pr_info("Memory at %llx (drc index %x) was hot-added\n",
990 lmbs[i].base_addr, lmbs[i].drc_index);
991 lmbs[i].reserved = 0;
998 int dlpar_memory(struct pseries_hp_errorlog *hp_elog)
1000 struct device_node *dn;
1001 struct property *prop;
1002 u32 count, drc_index;
1005 lock_device_hotplug();
1007 dn = of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1010 goto dlpar_memory_out;
1013 prop = dlpar_clone_drconf_property(dn);
1016 goto dlpar_memory_out;
1019 switch (hp_elog->action) {
1020 case PSERIES_HP_ELOG_ACTION_ADD:
1021 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) {
1022 count = hp_elog->_drc_u.drc_count;
1023 rc = dlpar_memory_add_by_count(count, prop);
1024 } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
1025 drc_index = hp_elog->_drc_u.drc_index;
1026 rc = dlpar_memory_add_by_index(drc_index, prop);
1027 } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_IC) {
1028 count = hp_elog->_drc_u.ic.count;
1029 drc_index = hp_elog->_drc_u.ic.index;
1030 rc = dlpar_memory_add_by_ic(count, drc_index, prop);
1036 case PSERIES_HP_ELOG_ACTION_REMOVE:
1037 if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_COUNT) {
1038 count = hp_elog->_drc_u.drc_count;
1039 rc = dlpar_memory_remove_by_count(count, prop);
1040 } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_INDEX) {
1041 drc_index = hp_elog->_drc_u.drc_index;
1042 rc = dlpar_memory_remove_by_index(drc_index, prop);
1043 } else if (hp_elog->id_type == PSERIES_HP_ELOG_ID_DRC_IC) {
1044 count = hp_elog->_drc_u.ic.count;
1045 drc_index = hp_elog->_drc_u.ic.index;
1046 rc = dlpar_memory_remove_by_ic(count, drc_index, prop);
1052 case PSERIES_HP_ELOG_ACTION_READD:
1053 drc_index = hp_elog->_drc_u.drc_index;
1054 rc = dlpar_memory_readd_by_index(drc_index, prop);
1057 pr_err("Invalid action (%d) specified\n", hp_elog->action);
1062 dlpar_free_property(prop);
1066 unlock_device_hotplug();
1070 static int pseries_add_mem_node(struct device_node *np)
1075 unsigned int lmb_size;
1079 * Check to see if we are actually adding memory
1081 type = of_get_property(np, "device_type", NULL);
1082 if (type == NULL || strcmp(type, "memory") != 0)
1086 * Find the base and size of the memblock
1088 regs = of_get_property(np, "reg", NULL);
1092 base = be64_to_cpu(*(unsigned long *)regs);
1093 lmb_size = be32_to_cpu(regs[3]);
1096 * Update memory region to represent the memory add
1098 ret = memblock_add(base, lmb_size);
1099 return (ret < 0) ? -EINVAL : 0;
1102 static int pseries_update_drconf_memory(struct of_reconfig_data *pr)
1104 struct of_drconf_cell *new_drmem, *old_drmem;
1105 unsigned long memblock_size;
1108 int i, rc = -EINVAL;
1113 memblock_size = pseries_memory_block_size();
1117 p = (__be32 *) pr->old_prop->value;
1121 /* The first int of the property is the number of lmb's described
1122 * by the property. This is followed by an array of of_drconf_cell
1123 * entries. Get the number of entries and skip to the array of
1126 entries = be32_to_cpu(*p++);
1127 old_drmem = (struct of_drconf_cell *)p;
1129 p = (__be32 *)pr->prop->value;
1131 new_drmem = (struct of_drconf_cell *)p;
1133 for (i = 0; i < entries; i++) {
1134 if ((be32_to_cpu(old_drmem[i].flags) & DRCONF_MEM_ASSIGNED) &&
1135 (!(be32_to_cpu(new_drmem[i].flags) & DRCONF_MEM_ASSIGNED))) {
1136 rc = pseries_remove_memblock(
1137 be64_to_cpu(old_drmem[i].base_addr),
1140 } else if ((!(be32_to_cpu(old_drmem[i].flags) &
1141 DRCONF_MEM_ASSIGNED)) &&
1142 (be32_to_cpu(new_drmem[i].flags) &
1143 DRCONF_MEM_ASSIGNED)) {
1144 rc = memblock_add(be64_to_cpu(old_drmem[i].base_addr),
1146 rc = (rc < 0) ? -EINVAL : 0;
1153 static int pseries_memory_notifier(struct notifier_block *nb,
1154 unsigned long action, void *data)
1156 struct of_reconfig_data *rd = data;
1160 case OF_RECONFIG_ATTACH_NODE:
1161 err = pseries_add_mem_node(rd->dn);
1163 case OF_RECONFIG_DETACH_NODE:
1164 err = pseries_remove_mem_node(rd->dn);
1166 case OF_RECONFIG_UPDATE_PROPERTY:
1167 if (!strcmp(rd->prop->name, "ibm,dynamic-memory"))
1168 err = pseries_update_drconf_memory(rd);
1171 return notifier_from_errno(err);
1174 static struct notifier_block pseries_mem_nb = {
1175 .notifier_call = pseries_memory_notifier,
1178 static int __init pseries_memory_hotplug_init(void)
1180 if (firmware_has_feature(FW_FEATURE_LPAR))
1181 of_reconfig_notifier_register(&pseries_mem_nb);
1185 machine_device_initcall(pseries, pseries_memory_hotplug_init);