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>
27 static bool rtas_hp_event
;
29 unsigned long pseries_memory_block_size(void)
31 struct device_node
*np
;
32 unsigned int memblock_size
= MIN_MEMORY_BLOCK_SIZE
;
35 np
= of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
39 size
= of_get_property(np
, "ibm,lmb-size", NULL
);
41 memblock_size
= be64_to_cpup(size
);
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 struct property
*dlpar_clone_drconf_property(struct device_node
*dn
)
104 struct property
*prop
, *new_prop
;
105 struct of_drconf_cell
*lmbs
;
109 prop
= of_find_property(dn
, "ibm,dynamic-memory", NULL
);
113 new_prop
= dlpar_clone_property(prop
, prop
->length
);
117 /* Convert the property to cpu endian-ness */
119 *p
= be32_to_cpu(*p
);
122 lmbs
= (struct of_drconf_cell
*)p
;
124 for (i
= 0; i
< num_lmbs
; i
++) {
125 lmbs
[i
].base_addr
= be64_to_cpu(lmbs
[i
].base_addr
);
126 lmbs
[i
].drc_index
= be32_to_cpu(lmbs
[i
].drc_index
);
127 lmbs
[i
].aa_index
= be32_to_cpu(lmbs
[i
].aa_index
);
128 lmbs
[i
].flags
= be32_to_cpu(lmbs
[i
].flags
);
134 static void dlpar_update_drconf_property(struct device_node
*dn
,
135 struct property
*prop
)
137 struct of_drconf_cell
*lmbs
;
141 /* Convert the property back to BE */
144 *p
= cpu_to_be32(*p
);
147 lmbs
= (struct of_drconf_cell
*)p
;
148 for (i
= 0; i
< num_lmbs
; i
++) {
149 lmbs
[i
].base_addr
= cpu_to_be64(lmbs
[i
].base_addr
);
150 lmbs
[i
].drc_index
= cpu_to_be32(lmbs
[i
].drc_index
);
151 lmbs
[i
].aa_index
= cpu_to_be32(lmbs
[i
].aa_index
);
152 lmbs
[i
].flags
= cpu_to_be32(lmbs
[i
].flags
);
155 rtas_hp_event
= true;
156 of_update_property(dn
, prop
);
157 rtas_hp_event
= false;
160 static int dlpar_update_device_tree_lmb(struct of_drconf_cell
*lmb
)
162 struct device_node
*dn
;
163 struct property
*prop
;
164 struct of_drconf_cell
*lmbs
;
168 dn
= of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
172 prop
= dlpar_clone_drconf_property(dn
);
180 lmbs
= (struct of_drconf_cell
*)p
;
182 for (i
= 0; i
< num_lmbs
; i
++) {
183 if (lmbs
[i
].drc_index
== lmb
->drc_index
) {
184 lmbs
[i
].flags
= lmb
->flags
;
185 lmbs
[i
].aa_index
= lmb
->aa_index
;
187 dlpar_update_drconf_property(dn
, prop
);
196 static u32
find_aa_index(struct device_node
*dr_node
,
197 struct property
*ala_prop
, const u32
*lmb_assoc
)
201 int aa_arrays
, aa_array_entries
, aa_array_sz
;
205 * The ibm,associativity-lookup-arrays property is defined to be
206 * a 32-bit value specifying the number of associativity arrays
207 * followed by a 32-bitvalue specifying the number of entries per
208 * array, followed by the associativity arrays.
210 assoc_arrays
= ala_prop
->value
;
212 aa_arrays
= be32_to_cpu(assoc_arrays
[0]);
213 aa_array_entries
= be32_to_cpu(assoc_arrays
[1]);
214 aa_array_sz
= aa_array_entries
* sizeof(u32
);
217 for (i
= 0; i
< aa_arrays
; i
++) {
218 index
= (i
* aa_array_entries
) + 2;
220 if (memcmp(&assoc_arrays
[index
], &lmb_assoc
[1], aa_array_sz
))
227 if (aa_index
== -1) {
228 struct property
*new_prop
;
231 new_prop_size
= ala_prop
->length
+ aa_array_sz
;
232 new_prop
= dlpar_clone_property(ala_prop
, new_prop_size
);
236 assoc_arrays
= new_prop
->value
;
238 /* increment the number of entries in the lookup array */
239 assoc_arrays
[0] = cpu_to_be32(aa_arrays
+ 1);
241 /* copy the new associativity into the lookup array */
242 index
= aa_arrays
* aa_array_entries
+ 2;
243 memcpy(&assoc_arrays
[index
], &lmb_assoc
[1], aa_array_sz
);
245 of_update_property(dr_node
, new_prop
);
248 * The associativity lookup array index for this lmb is
249 * number of entries - 1 since we added its associativity
250 * to the end of the lookup array.
252 aa_index
= be32_to_cpu(assoc_arrays
[0]) - 1;
258 static u32
lookup_lmb_associativity_index(struct of_drconf_cell
*lmb
)
260 struct device_node
*parent
, *lmb_node
, *dr_node
;
261 struct property
*ala_prop
;
262 const u32
*lmb_assoc
;
265 parent
= of_find_node_by_path("/");
269 lmb_node
= dlpar_configure_connector(cpu_to_be32(lmb
->drc_index
),
275 lmb_assoc
= of_get_property(lmb_node
, "ibm,associativity", NULL
);
277 dlpar_free_cc_nodes(lmb_node
);
281 dr_node
= of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
283 dlpar_free_cc_nodes(lmb_node
);
287 ala_prop
= of_find_property(dr_node
, "ibm,associativity-lookup-arrays",
290 of_node_put(dr_node
);
291 dlpar_free_cc_nodes(lmb_node
);
295 aa_index
= find_aa_index(dr_node
, ala_prop
, lmb_assoc
);
297 dlpar_free_cc_nodes(lmb_node
);
301 static int dlpar_add_device_tree_lmb(struct of_drconf_cell
*lmb
)
305 lmb
->flags
|= DRCONF_MEM_ASSIGNED
;
307 aa_index
= lookup_lmb_associativity_index(lmb
);
309 pr_err("Couldn't find associativity index for drc index %x\n",
314 lmb
->aa_index
= aa_index
;
315 return dlpar_update_device_tree_lmb(lmb
);
318 static int dlpar_remove_device_tree_lmb(struct of_drconf_cell
*lmb
)
320 lmb
->flags
&= ~DRCONF_MEM_ASSIGNED
;
321 lmb
->aa_index
= 0xffffffff;
322 return dlpar_update_device_tree_lmb(lmb
);
325 static struct memory_block
*lmb_to_memblock(struct of_drconf_cell
*lmb
)
327 unsigned long section_nr
;
328 struct mem_section
*mem_sect
;
329 struct memory_block
*mem_block
;
331 section_nr
= pfn_to_section_nr(PFN_DOWN(lmb
->base_addr
));
332 mem_sect
= __nr_to_section(section_nr
);
334 mem_block
= find_memory_block(mem_sect
);
338 #ifdef CONFIG_MEMORY_HOTREMOVE
339 static int pseries_remove_memblock(unsigned long base
, unsigned int memblock_size
)
341 unsigned long block_sz
, start_pfn
;
342 int sections_per_block
;
345 start_pfn
= base
>> PAGE_SHIFT
;
347 lock_device_hotplug();
349 if (!pfn_valid(start_pfn
))
352 block_sz
= pseries_memory_block_size();
353 sections_per_block
= block_sz
/ MIN_MEMORY_BLOCK_SIZE
;
354 nid
= memory_add_physaddr_to_nid(base
);
356 for (i
= 0; i
< sections_per_block
; i
++) {
357 remove_memory(nid
, base
, MIN_MEMORY_BLOCK_SIZE
);
358 base
+= MIN_MEMORY_BLOCK_SIZE
;
362 /* Update memory regions for memory remove */
363 memblock_remove(base
, memblock_size
);
364 unlock_device_hotplug();
368 static int pseries_remove_mem_node(struct device_node
*np
)
373 unsigned int lmb_size
;
377 * Check to see if we are actually removing memory
379 type
= of_get_property(np
, "device_type", NULL
);
380 if (type
== NULL
|| strcmp(type
, "memory") != 0)
384 * Find the base address and size of the memblock
386 regs
= of_get_property(np
, "reg", NULL
);
390 base
= be64_to_cpu(*(unsigned long *)regs
);
391 lmb_size
= be32_to_cpu(regs
[3]);
393 pseries_remove_memblock(base
, lmb_size
);
397 static bool lmb_is_removable(struct of_drconf_cell
*lmb
)
399 int i
, scns_per_block
;
401 unsigned long pfn
, block_sz
;
404 if (!(lmb
->flags
& DRCONF_MEM_ASSIGNED
))
407 block_sz
= memory_block_size_bytes();
408 scns_per_block
= block_sz
/ MIN_MEMORY_BLOCK_SIZE
;
409 phys_addr
= lmb
->base_addr
;
411 for (i
= 0; i
< scns_per_block
; i
++) {
412 pfn
= PFN_DOWN(phys_addr
);
413 if (!pfn_present(pfn
))
416 rc
&= is_mem_section_removable(pfn
, PAGES_PER_SECTION
);
417 phys_addr
+= MIN_MEMORY_BLOCK_SIZE
;
420 return rc
? true : false;
423 static int dlpar_add_lmb(struct of_drconf_cell
*);
425 static int dlpar_remove_lmb(struct of_drconf_cell
*lmb
)
427 struct memory_block
*mem_block
;
428 unsigned long block_sz
;
431 if (!lmb_is_removable(lmb
))
434 mem_block
= lmb_to_memblock(lmb
);
438 rc
= device_offline(&mem_block
->dev
);
439 put_device(&mem_block
->dev
);
443 block_sz
= pseries_memory_block_size();
444 nid
= memory_add_physaddr_to_nid(lmb
->base_addr
);
446 remove_memory(nid
, lmb
->base_addr
, block_sz
);
448 /* Update memory regions for memory remove */
449 memblock_remove(lmb
->base_addr
, block_sz
);
451 dlpar_remove_device_tree_lmb(lmb
);
455 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove
,
456 struct property
*prop
)
458 struct of_drconf_cell
*lmbs
;
459 int lmbs_removed
= 0;
460 int lmbs_available
= 0;
464 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove
);
466 if (lmbs_to_remove
== 0)
471 lmbs
= (struct of_drconf_cell
*)p
;
473 /* Validate that there are enough LMBs to satisfy the request */
474 for (i
= 0; i
< num_lmbs
; i
++) {
475 if (lmb_is_removable(&lmbs
[i
]))
479 if (lmbs_available
< lmbs_to_remove
) {
480 pr_info("Not enough LMBs available (%d of %d) to satisfy request\n",
481 lmbs_available
, lmbs_to_remove
);
485 for (i
= 0; i
< num_lmbs
&& lmbs_removed
< lmbs_to_remove
; i
++) {
486 rc
= dlpar_remove_lmb(&lmbs
[i
]);
492 /* Mark this lmb so we can add it later if all of the
493 * requested LMBs cannot be removed.
495 lmbs
[i
].reserved
= 1;
498 if (lmbs_removed
!= lmbs_to_remove
) {
499 pr_err("Memory hot-remove failed, adding LMB's back\n");
501 for (i
= 0; i
< num_lmbs
; i
++) {
502 if (!lmbs
[i
].reserved
)
505 rc
= dlpar_add_lmb(&lmbs
[i
]);
507 pr_err("Failed to add LMB back, drc index %x\n",
510 lmbs
[i
].reserved
= 0;
515 for (i
= 0; i
< num_lmbs
; i
++) {
516 if (!lmbs
[i
].reserved
)
519 dlpar_release_drc(lmbs
[i
].drc_index
);
520 pr_info("Memory at %llx was hot-removed\n",
523 lmbs
[i
].reserved
= 0;
531 static int dlpar_memory_remove_by_index(u32 drc_index
, struct property
*prop
)
533 struct of_drconf_cell
*lmbs
;
538 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index
);
542 lmbs
= (struct of_drconf_cell
*)p
;
545 for (i
= 0; i
< num_lmbs
; i
++) {
546 if (lmbs
[i
].drc_index
== drc_index
) {
548 rc
= dlpar_remove_lmb(&lmbs
[i
]);
550 dlpar_release_drc(lmbs
[i
].drc_index
);
560 pr_info("Failed to hot-remove memory at %llx\n",
563 pr_info("Memory at %llx was hot-removed\n", lmbs
[i
].base_addr
);
568 static int dlpar_memory_readd_by_index(u32 drc_index
, struct property
*prop
)
570 struct of_drconf_cell
*lmbs
;
575 pr_info("Attempting to update LMB, drc index %x\n", drc_index
);
579 lmbs
= (struct of_drconf_cell
*)p
;
582 for (i
= 0; i
< num_lmbs
; i
++) {
583 if (lmbs
[i
].drc_index
== drc_index
) {
585 rc
= dlpar_remove_lmb(&lmbs
[i
]);
587 rc
= dlpar_add_lmb(&lmbs
[i
]);
589 dlpar_release_drc(lmbs
[i
].drc_index
);
599 pr_info("Failed to update memory at %llx\n",
602 pr_info("Memory at %llx was updated\n", lmbs
[i
].base_addr
);
607 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove
, u32 drc_index
,
608 struct property
*prop
)
610 struct of_drconf_cell
*lmbs
;
612 int i
, rc
, start_lmb_found
;
613 int lmbs_available
= 0, start_index
= 0, end_index
;
615 pr_info("Attempting to hot-remove %u LMB(s) at %x\n",
616 lmbs_to_remove
, drc_index
);
618 if (lmbs_to_remove
== 0)
623 lmbs
= (struct of_drconf_cell
*)p
;
626 /* Navigate to drc_index */
627 while (start_index
< num_lmbs
) {
628 if (lmbs
[start_index
].drc_index
== drc_index
) {
636 if (!start_lmb_found
)
639 end_index
= start_index
+ lmbs_to_remove
;
641 /* Validate that there are enough LMBs to satisfy the request */
642 for (i
= start_index
; i
< end_index
; i
++) {
643 if (lmbs
[i
].flags
& DRCONF_MEM_RESERVED
)
649 if (lmbs_available
< lmbs_to_remove
)
652 for (i
= start_index
; i
< end_index
; i
++) {
653 if (!(lmbs
[i
].flags
& DRCONF_MEM_ASSIGNED
))
656 rc
= dlpar_remove_lmb(&lmbs
[i
]);
660 lmbs
[i
].reserved
= 1;
664 pr_err("Memory indexed-count-remove failed, adding any removed LMBs\n");
666 for (i
= start_index
; i
< end_index
; i
++) {
667 if (!lmbs
[i
].reserved
)
670 rc
= dlpar_add_lmb(&lmbs
[i
]);
672 pr_err("Failed to add LMB, drc index %x\n",
673 be32_to_cpu(lmbs
[i
].drc_index
));
675 lmbs
[i
].reserved
= 0;
679 for (i
= start_index
; i
< end_index
; i
++) {
680 if (!lmbs
[i
].reserved
)
683 dlpar_release_drc(lmbs
[i
].drc_index
);
684 pr_info("Memory at %llx (drc index %x) was hot-removed\n",
685 lmbs
[i
].base_addr
, lmbs
[i
].drc_index
);
687 lmbs
[i
].reserved
= 0;
695 static inline int pseries_remove_memblock(unsigned long base
,
696 unsigned int memblock_size
)
700 static inline int pseries_remove_mem_node(struct device_node
*np
)
704 static inline int dlpar_memory_remove(struct pseries_hp_errorlog
*hp_elog
)
708 static int dlpar_remove_lmb(struct of_drconf_cell
*lmb
)
712 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove
,
713 struct property
*prop
)
717 static int dlpar_memory_remove_by_index(u32 drc_index
, struct property
*prop
)
721 static int dlpar_memory_readd_by_index(u32 drc_index
, struct property
*prop
)
726 static int dlpar_memory_remove_by_ic(u32 lmbs_to_remove
, u32 drc_index
,
727 struct property
*prop
)
731 #endif /* CONFIG_MEMORY_HOTREMOVE */
733 static int dlpar_online_lmb(struct of_drconf_cell
*lmb
)
735 struct memory_block
*mem_block
;
738 mem_block
= lmb_to_memblock(lmb
);
742 rc
= device_online(&mem_block
->dev
);
743 put_device(&mem_block
->dev
);
747 static int dlpar_add_lmb(struct of_drconf_cell
*lmb
)
749 unsigned long block_sz
;
752 if (lmb
->flags
& DRCONF_MEM_ASSIGNED
)
755 rc
= dlpar_add_device_tree_lmb(lmb
);
757 pr_err("Couldn't update device tree for drc index %x\n",
759 dlpar_release_drc(lmb
->drc_index
);
763 block_sz
= memory_block_size_bytes();
765 /* Find the node id for this address */
766 nid
= memory_add_physaddr_to_nid(lmb
->base_addr
);
769 rc
= add_memory(nid
, lmb
->base_addr
, block_sz
);
771 dlpar_remove_device_tree_lmb(lmb
);
775 rc
= dlpar_online_lmb(lmb
);
777 remove_memory(nid
, lmb
->base_addr
, block_sz
);
778 dlpar_remove_device_tree_lmb(lmb
);
780 lmb
->flags
|= DRCONF_MEM_ASSIGNED
;
786 static int dlpar_memory_add_by_count(u32 lmbs_to_add
, struct property
*prop
)
788 struct of_drconf_cell
*lmbs
;
790 int lmbs_available
= 0;
794 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add
);
796 if (lmbs_to_add
== 0)
801 lmbs
= (struct of_drconf_cell
*)p
;
803 /* Validate that there are enough LMBs to satisfy the request */
804 for (i
= 0; i
< num_lmbs
; i
++) {
805 if (!(lmbs
[i
].flags
& DRCONF_MEM_ASSIGNED
))
809 if (lmbs_available
< lmbs_to_add
)
812 for (i
= 0; i
< num_lmbs
&& lmbs_to_add
!= lmbs_added
; i
++) {
813 rc
= dlpar_acquire_drc(lmbs
[i
].drc_index
);
817 rc
= dlpar_add_lmb(&lmbs
[i
]);
819 dlpar_release_drc(lmbs
[i
].drc_index
);
825 /* Mark this lmb so we can remove it later if all of the
826 * requested LMBs cannot be added.
828 lmbs
[i
].reserved
= 1;
831 if (lmbs_added
!= lmbs_to_add
) {
832 pr_err("Memory hot-add failed, removing any added LMBs\n");
834 for (i
= 0; i
< num_lmbs
; i
++) {
835 if (!lmbs
[i
].reserved
)
838 rc
= dlpar_remove_lmb(&lmbs
[i
]);
840 pr_err("Failed to remove LMB, drc index %x\n",
841 be32_to_cpu(lmbs
[i
].drc_index
));
843 dlpar_release_drc(lmbs
[i
].drc_index
);
847 for (i
= 0; i
< num_lmbs
; i
++) {
848 if (!lmbs
[i
].reserved
)
851 pr_info("Memory at %llx (drc index %x) was hot-added\n",
852 lmbs
[i
].base_addr
, lmbs
[i
].drc_index
);
853 lmbs
[i
].reserved
= 0;
860 static int dlpar_memory_add_by_index(u32 drc_index
, struct property
*prop
)
862 struct of_drconf_cell
*lmbs
;
867 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index
);
871 lmbs
= (struct of_drconf_cell
*)p
;
874 for (i
= 0; i
< num_lmbs
; i
++) {
875 if (lmbs
[i
].drc_index
== drc_index
) {
877 rc
= dlpar_acquire_drc(lmbs
[i
].drc_index
);
879 rc
= dlpar_add_lmb(&lmbs
[i
]);
881 dlpar_release_drc(lmbs
[i
].drc_index
);
892 pr_info("Failed to hot-add memory, drc index %x\n", drc_index
);
894 pr_info("Memory at %llx (drc index %x) was hot-added\n",
895 lmbs
[i
].base_addr
, drc_index
);
900 static int dlpar_memory_add_by_ic(u32 lmbs_to_add
, u32 drc_index
,
901 struct property
*prop
)
903 struct of_drconf_cell
*lmbs
;
905 int i
, rc
, start_lmb_found
;
906 int lmbs_available
= 0, start_index
= 0, end_index
;
908 pr_info("Attempting to hot-add %u LMB(s) at index %x\n",
909 lmbs_to_add
, drc_index
);
911 if (lmbs_to_add
== 0)
916 lmbs
= (struct of_drconf_cell
*)p
;
919 /* Navigate to drc_index */
920 while (start_index
< num_lmbs
) {
921 if (lmbs
[start_index
].drc_index
== drc_index
) {
929 if (!start_lmb_found
)
932 end_index
= start_index
+ lmbs_to_add
;
934 /* Validate that the LMBs in this range are not reserved */
935 for (i
= start_index
; i
< end_index
; i
++) {
936 if (lmbs
[i
].flags
& DRCONF_MEM_RESERVED
)
942 if (lmbs_available
< lmbs_to_add
)
945 for (i
= start_index
; i
< end_index
; i
++) {
946 if (lmbs
[i
].flags
& DRCONF_MEM_ASSIGNED
)
949 rc
= dlpar_acquire_drc(lmbs
[i
].drc_index
);
953 rc
= dlpar_add_lmb(&lmbs
[i
]);
955 dlpar_release_drc(lmbs
[i
].drc_index
);
959 lmbs
[i
].reserved
= 1;
963 pr_err("Memory indexed-count-add failed, removing any added LMBs\n");
965 for (i
= start_index
; i
< end_index
; i
++) {
966 if (!lmbs
[i
].reserved
)
969 rc
= dlpar_remove_lmb(&lmbs
[i
]);
971 pr_err("Failed to remove LMB, drc index %x\n",
972 be32_to_cpu(lmbs
[i
].drc_index
));
974 dlpar_release_drc(lmbs
[i
].drc_index
);
978 for (i
= start_index
; i
< end_index
; i
++) {
979 if (!lmbs
[i
].reserved
)
982 pr_info("Memory at %llx (drc index %x) was hot-added\n",
983 lmbs
[i
].base_addr
, lmbs
[i
].drc_index
);
984 lmbs
[i
].reserved
= 0;
991 int dlpar_memory(struct pseries_hp_errorlog
*hp_elog
)
993 struct device_node
*dn
;
994 struct property
*prop
;
995 u32 count
, drc_index
;
998 lock_device_hotplug();
1000 dn
= of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
1003 goto dlpar_memory_out
;
1006 prop
= dlpar_clone_drconf_property(dn
);
1009 goto dlpar_memory_out
;
1012 switch (hp_elog
->action
) {
1013 case PSERIES_HP_ELOG_ACTION_ADD
:
1014 if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_COUNT
) {
1015 count
= hp_elog
->_drc_u
.drc_count
;
1016 rc
= dlpar_memory_add_by_count(count
, prop
);
1017 } else if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_INDEX
) {
1018 drc_index
= hp_elog
->_drc_u
.drc_index
;
1019 rc
= dlpar_memory_add_by_index(drc_index
, prop
);
1020 } else if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_IC
) {
1021 count
= hp_elog
->_drc_u
.ic
.count
;
1022 drc_index
= hp_elog
->_drc_u
.ic
.index
;
1023 rc
= dlpar_memory_add_by_ic(count
, drc_index
, prop
);
1029 case PSERIES_HP_ELOG_ACTION_REMOVE
:
1030 if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_COUNT
) {
1031 count
= hp_elog
->_drc_u
.drc_count
;
1032 rc
= dlpar_memory_remove_by_count(count
, prop
);
1033 } else if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_INDEX
) {
1034 drc_index
= hp_elog
->_drc_u
.drc_index
;
1035 rc
= dlpar_memory_remove_by_index(drc_index
, prop
);
1036 } else if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_IC
) {
1037 count
= hp_elog
->_drc_u
.ic
.count
;
1038 drc_index
= hp_elog
->_drc_u
.ic
.index
;
1039 rc
= dlpar_memory_remove_by_ic(count
, drc_index
, prop
);
1045 case PSERIES_HP_ELOG_ACTION_READD
:
1046 drc_index
= hp_elog
->_drc_u
.drc_index
;
1047 rc
= dlpar_memory_readd_by_index(drc_index
, prop
);
1050 pr_err("Invalid action (%d) specified\n", hp_elog
->action
);
1055 dlpar_free_property(prop
);
1059 unlock_device_hotplug();
1063 static int pseries_add_mem_node(struct device_node
*np
)
1068 unsigned int lmb_size
;
1072 * Check to see if we are actually adding memory
1074 type
= of_get_property(np
, "device_type", NULL
);
1075 if (type
== NULL
|| strcmp(type
, "memory") != 0)
1079 * Find the base and size of the memblock
1081 regs
= of_get_property(np
, "reg", NULL
);
1085 base
= be64_to_cpu(*(unsigned long *)regs
);
1086 lmb_size
= be32_to_cpu(regs
[3]);
1089 * Update memory region to represent the memory add
1091 ret
= memblock_add(base
, lmb_size
);
1092 return (ret
< 0) ? -EINVAL
: 0;
1095 static int pseries_update_drconf_memory(struct of_reconfig_data
*pr
)
1097 struct of_drconf_cell
*new_drmem
, *old_drmem
;
1098 unsigned long memblock_size
;
1101 int i
, rc
= -EINVAL
;
1106 memblock_size
= pseries_memory_block_size();
1110 p
= (__be32
*) pr
->old_prop
->value
;
1114 /* The first int of the property is the number of lmb's described
1115 * by the property. This is followed by an array of of_drconf_cell
1116 * entries. Get the number of entries and skip to the array of
1119 entries
= be32_to_cpu(*p
++);
1120 old_drmem
= (struct of_drconf_cell
*)p
;
1122 p
= (__be32
*)pr
->prop
->value
;
1124 new_drmem
= (struct of_drconf_cell
*)p
;
1126 for (i
= 0; i
< entries
; i
++) {
1127 if ((be32_to_cpu(old_drmem
[i
].flags
) & DRCONF_MEM_ASSIGNED
) &&
1128 (!(be32_to_cpu(new_drmem
[i
].flags
) & DRCONF_MEM_ASSIGNED
))) {
1129 rc
= pseries_remove_memblock(
1130 be64_to_cpu(old_drmem
[i
].base_addr
),
1133 } else if ((!(be32_to_cpu(old_drmem
[i
].flags
) &
1134 DRCONF_MEM_ASSIGNED
)) &&
1135 (be32_to_cpu(new_drmem
[i
].flags
) &
1136 DRCONF_MEM_ASSIGNED
)) {
1137 rc
= memblock_add(be64_to_cpu(old_drmem
[i
].base_addr
),
1139 rc
= (rc
< 0) ? -EINVAL
: 0;
1146 static int pseries_memory_notifier(struct notifier_block
*nb
,
1147 unsigned long action
, void *data
)
1149 struct of_reconfig_data
*rd
= data
;
1153 case OF_RECONFIG_ATTACH_NODE
:
1154 err
= pseries_add_mem_node(rd
->dn
);
1156 case OF_RECONFIG_DETACH_NODE
:
1157 err
= pseries_remove_mem_node(rd
->dn
);
1159 case OF_RECONFIG_UPDATE_PROPERTY
:
1160 if (!strcmp(rd
->prop
->name
, "ibm,dynamic-memory"))
1161 err
= pseries_update_drconf_memory(rd
);
1164 return notifier_from_errno(err
);
1167 static struct notifier_block pseries_mem_nb
= {
1168 .notifier_call
= pseries_memory_notifier
,
1171 static int __init
pseries_memory_hotplug_init(void)
1173 if (firmware_has_feature(FW_FEATURE_LPAR
))
1174 of_reconfig_notifier_register(&pseries_mem_nb
);
1178 machine_device_initcall(pseries
, pseries_memory_hotplug_init
);