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_drconf_property(struct property
*prop
)
79 static struct property
*dlpar_clone_drconf_property(struct device_node
*dn
)
81 struct property
*prop
, *new_prop
;
82 struct of_drconf_cell
*lmbs
;
86 prop
= of_find_property(dn
, "ibm,dynamic-memory", NULL
);
90 new_prop
= kzalloc(sizeof(*new_prop
), GFP_KERNEL
);
94 new_prop
->name
= kstrdup(prop
->name
, GFP_KERNEL
);
95 new_prop
->value
= kmemdup(prop
->value
, prop
->length
, GFP_KERNEL
);
96 if (!new_prop
->name
|| !new_prop
->value
) {
97 dlpar_free_drconf_property(new_prop
);
101 new_prop
->length
= prop
->length
;
103 /* Convert the property to cpu endian-ness */
105 *p
= be32_to_cpu(*p
);
108 lmbs
= (struct of_drconf_cell
*)p
;
110 for (i
= 0; i
< num_lmbs
; i
++) {
111 lmbs
[i
].base_addr
= be64_to_cpu(lmbs
[i
].base_addr
);
112 lmbs
[i
].drc_index
= be32_to_cpu(lmbs
[i
].drc_index
);
113 lmbs
[i
].flags
= be32_to_cpu(lmbs
[i
].flags
);
119 static struct memory_block
*lmb_to_memblock(struct of_drconf_cell
*lmb
)
121 unsigned long section_nr
;
122 struct mem_section
*mem_sect
;
123 struct memory_block
*mem_block
;
125 section_nr
= pfn_to_section_nr(PFN_DOWN(lmb
->base_addr
));
126 mem_sect
= __nr_to_section(section_nr
);
128 mem_block
= find_memory_block(mem_sect
);
132 #ifdef CONFIG_MEMORY_HOTREMOVE
133 static int pseries_remove_memblock(unsigned long base
, unsigned int memblock_size
)
135 unsigned long block_sz
, start_pfn
;
136 int sections_per_block
;
139 start_pfn
= base
>> PAGE_SHIFT
;
141 lock_device_hotplug();
143 if (!pfn_valid(start_pfn
))
146 block_sz
= pseries_memory_block_size();
147 sections_per_block
= block_sz
/ MIN_MEMORY_BLOCK_SIZE
;
148 nid
= memory_add_physaddr_to_nid(base
);
150 for (i
= 0; i
< sections_per_block
; i
++) {
151 remove_memory(nid
, base
, MIN_MEMORY_BLOCK_SIZE
);
152 base
+= MIN_MEMORY_BLOCK_SIZE
;
156 /* Update memory regions for memory remove */
157 memblock_remove(base
, memblock_size
);
158 unlock_device_hotplug();
162 static int pseries_remove_mem_node(struct device_node
*np
)
167 unsigned int lmb_size
;
171 * Check to see if we are actually removing memory
173 type
= of_get_property(np
, "device_type", NULL
);
174 if (type
== NULL
|| strcmp(type
, "memory") != 0)
178 * Find the base address and size of the memblock
180 regs
= of_get_property(np
, "reg", NULL
);
184 base
= be64_to_cpu(*(unsigned long *)regs
);
185 lmb_size
= be32_to_cpu(regs
[3]);
187 pseries_remove_memblock(base
, lmb_size
);
191 static bool lmb_is_removable(struct of_drconf_cell
*lmb
)
193 int i
, scns_per_block
;
195 unsigned long pfn
, block_sz
;
198 if (!(lmb
->flags
& DRCONF_MEM_ASSIGNED
))
201 block_sz
= memory_block_size_bytes();
202 scns_per_block
= block_sz
/ MIN_MEMORY_BLOCK_SIZE
;
203 phys_addr
= lmb
->base_addr
;
205 for (i
= 0; i
< scns_per_block
; i
++) {
206 pfn
= PFN_DOWN(phys_addr
);
207 if (!pfn_present(pfn
))
210 rc
&= is_mem_section_removable(pfn
, PAGES_PER_SECTION
);
211 phys_addr
+= MIN_MEMORY_BLOCK_SIZE
;
214 return rc
? true : false;
217 static int dlpar_add_lmb(struct of_drconf_cell
*);
219 static int dlpar_remove_lmb(struct of_drconf_cell
*lmb
)
221 struct memory_block
*mem_block
;
222 unsigned long block_sz
;
225 if (!lmb_is_removable(lmb
))
228 mem_block
= lmb_to_memblock(lmb
);
232 rc
= device_offline(&mem_block
->dev
);
233 put_device(&mem_block
->dev
);
237 block_sz
= pseries_memory_block_size();
238 nid
= memory_add_physaddr_to_nid(lmb
->base_addr
);
240 remove_memory(nid
, lmb
->base_addr
, block_sz
);
242 /* Update memory regions for memory remove */
243 memblock_remove(lmb
->base_addr
, block_sz
);
245 dlpar_release_drc(lmb
->drc_index
);
247 lmb
->flags
&= ~DRCONF_MEM_ASSIGNED
;
251 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove
,
252 struct property
*prop
)
254 struct of_drconf_cell
*lmbs
;
255 int lmbs_removed
= 0;
256 int lmbs_available
= 0;
260 pr_info("Attempting to hot-remove %d LMB(s)\n", lmbs_to_remove
);
262 if (lmbs_to_remove
== 0)
267 lmbs
= (struct of_drconf_cell
*)p
;
269 /* Validate that there are enough LMBs to satisfy the request */
270 for (i
= 0; i
< num_lmbs
; i
++) {
271 if (lmbs
[i
].flags
& DRCONF_MEM_ASSIGNED
)
275 if (lmbs_available
< lmbs_to_remove
)
278 for (i
= 0; i
< num_lmbs
&& lmbs_removed
< lmbs_to_remove
; i
++) {
279 rc
= dlpar_remove_lmb(&lmbs
[i
]);
285 /* Mark this lmb so we can add it later if all of the
286 * requested LMBs cannot be removed.
288 lmbs
[i
].reserved
= 1;
291 if (lmbs_removed
!= lmbs_to_remove
) {
292 pr_err("Memory hot-remove failed, adding LMB's back\n");
294 for (i
= 0; i
< num_lmbs
; i
++) {
295 if (!lmbs
[i
].reserved
)
298 rc
= dlpar_add_lmb(&lmbs
[i
]);
300 pr_err("Failed to add LMB back, drc index %x\n",
303 lmbs
[i
].reserved
= 0;
308 for (i
= 0; i
< num_lmbs
; i
++) {
309 if (!lmbs
[i
].reserved
)
312 pr_info("Memory at %llx was hot-removed\n",
315 lmbs
[i
].reserved
= 0;
323 static int dlpar_memory_remove_by_index(u32 drc_index
, struct property
*prop
)
325 struct of_drconf_cell
*lmbs
;
330 pr_info("Attempting to hot-remove LMB, drc index %x\n", drc_index
);
334 lmbs
= (struct of_drconf_cell
*)p
;
337 for (i
= 0; i
< num_lmbs
; i
++) {
338 if (lmbs
[i
].drc_index
== drc_index
) {
340 rc
= dlpar_remove_lmb(&lmbs
[i
]);
349 pr_info("Failed to hot-remove memory at %llx\n",
352 pr_info("Memory at %llx was hot-removed\n", lmbs
[i
].base_addr
);
358 static inline int pseries_remove_memblock(unsigned long base
,
359 unsigned int memblock_size
)
363 static inline int pseries_remove_mem_node(struct device_node
*np
)
367 static inline int dlpar_memory_remove(struct pseries_hp_errorlog
*hp_elog
)
371 static int dlpar_remove_lmb(struct of_drconf_cell
*lmb
)
375 static int dlpar_memory_remove_by_count(u32 lmbs_to_remove
,
376 struct property
*prop
)
380 static int dlpar_memory_remove_by_index(u32 drc_index
, struct property
*prop
)
385 #endif /* CONFIG_MEMORY_HOTREMOVE */
387 static int dlpar_add_lmb(struct of_drconf_cell
*lmb
)
389 struct memory_block
*mem_block
;
390 unsigned long block_sz
;
393 if (lmb
->flags
& DRCONF_MEM_ASSIGNED
)
396 block_sz
= memory_block_size_bytes();
398 rc
= dlpar_acquire_drc(lmb
->drc_index
);
402 /* Find the node id for this address */
403 nid
= memory_add_physaddr_to_nid(lmb
->base_addr
);
406 rc
= add_memory(nid
, lmb
->base_addr
, block_sz
);
408 dlpar_release_drc(lmb
->drc_index
);
412 /* Register this block of memory */
413 rc
= memblock_add(lmb
->base_addr
, block_sz
);
415 remove_memory(nid
, lmb
->base_addr
, block_sz
);
416 dlpar_release_drc(lmb
->drc_index
);
420 mem_block
= lmb_to_memblock(lmb
);
422 remove_memory(nid
, lmb
->base_addr
, block_sz
);
423 dlpar_release_drc(lmb
->drc_index
);
427 rc
= device_online(&mem_block
->dev
);
428 put_device(&mem_block
->dev
);
430 remove_memory(nid
, lmb
->base_addr
, block_sz
);
431 dlpar_release_drc(lmb
->drc_index
);
435 lmb
->flags
|= DRCONF_MEM_ASSIGNED
;
439 static int dlpar_memory_add_by_count(u32 lmbs_to_add
, struct property
*prop
)
441 struct of_drconf_cell
*lmbs
;
443 int lmbs_available
= 0;
447 pr_info("Attempting to hot-add %d LMB(s)\n", lmbs_to_add
);
449 if (lmbs_to_add
== 0)
454 lmbs
= (struct of_drconf_cell
*)p
;
456 /* Validate that there are enough LMBs to satisfy the request */
457 for (i
= 0; i
< num_lmbs
; i
++) {
458 if (!(lmbs
[i
].flags
& DRCONF_MEM_ASSIGNED
))
462 if (lmbs_available
< lmbs_to_add
)
465 for (i
= 0; i
< num_lmbs
&& lmbs_to_add
!= lmbs_added
; i
++) {
466 rc
= dlpar_add_lmb(&lmbs
[i
]);
472 /* Mark this lmb so we can remove it later if all of the
473 * requested LMBs cannot be added.
475 lmbs
[i
].reserved
= 1;
478 if (lmbs_added
!= lmbs_to_add
) {
479 pr_err("Memory hot-add failed, removing any added LMBs\n");
481 for (i
= 0; i
< num_lmbs
; i
++) {
482 if (!lmbs
[i
].reserved
)
485 rc
= dlpar_remove_lmb(&lmbs
[i
]);
487 pr_err("Failed to remove LMB, drc index %x\n",
488 be32_to_cpu(lmbs
[i
].drc_index
));
492 for (i
= 0; i
< num_lmbs
; i
++) {
493 if (!lmbs
[i
].reserved
)
496 pr_info("Memory at %llx (drc index %x) was hot-added\n",
497 lmbs
[i
].base_addr
, lmbs
[i
].drc_index
);
498 lmbs
[i
].reserved
= 0;
505 static int dlpar_memory_add_by_index(u32 drc_index
, struct property
*prop
)
507 struct of_drconf_cell
*lmbs
;
512 pr_info("Attempting to hot-add LMB, drc index %x\n", drc_index
);
516 lmbs
= (struct of_drconf_cell
*)p
;
519 for (i
= 0; i
< num_lmbs
; i
++) {
520 if (lmbs
[i
].drc_index
== drc_index
) {
522 rc
= dlpar_add_lmb(&lmbs
[i
]);
531 pr_info("Failed to hot-add memory, drc index %x\n", drc_index
);
533 pr_info("Memory at %llx (drc index %x) was hot-added\n",
534 lmbs
[i
].base_addr
, drc_index
);
539 static void dlpar_update_drconf_property(struct device_node
*dn
,
540 struct property
*prop
)
542 struct of_drconf_cell
*lmbs
;
546 /* Convert the property back to BE */
549 *p
= cpu_to_be32(*p
);
552 lmbs
= (struct of_drconf_cell
*)p
;
553 for (i
= 0; i
< num_lmbs
; i
++) {
554 lmbs
[i
].base_addr
= cpu_to_be64(lmbs
[i
].base_addr
);
555 lmbs
[i
].drc_index
= cpu_to_be32(lmbs
[i
].drc_index
);
556 lmbs
[i
].flags
= cpu_to_be32(lmbs
[i
].flags
);
559 rtas_hp_event
= true;
560 of_update_property(dn
, prop
);
561 rtas_hp_event
= false;
564 int dlpar_memory(struct pseries_hp_errorlog
*hp_elog
)
566 struct device_node
*dn
;
567 struct property
*prop
;
568 u32 count
, drc_index
;
571 count
= hp_elog
->_drc_u
.drc_count
;
572 drc_index
= hp_elog
->_drc_u
.drc_index
;
574 lock_device_hotplug();
576 dn
= of_find_node_by_path("/ibm,dynamic-reconfiguration-memory");
579 goto dlpar_memory_out
;
582 prop
= dlpar_clone_drconf_property(dn
);
585 goto dlpar_memory_out
;
588 switch (hp_elog
->action
) {
589 case PSERIES_HP_ELOG_ACTION_ADD
:
590 if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_COUNT
)
591 rc
= dlpar_memory_add_by_count(count
, prop
);
592 else if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_INDEX
)
593 rc
= dlpar_memory_add_by_index(drc_index
, prop
);
597 case PSERIES_HP_ELOG_ACTION_REMOVE
:
598 if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_COUNT
)
599 rc
= dlpar_memory_remove_by_count(count
, prop
);
600 else if (hp_elog
->id_type
== PSERIES_HP_ELOG_ID_DRC_INDEX
)
601 rc
= dlpar_memory_remove_by_index(drc_index
, prop
);
606 pr_err("Invalid action (%d) specified\n", hp_elog
->action
);
612 dlpar_free_drconf_property(prop
);
614 dlpar_update_drconf_property(dn
, prop
);
618 unlock_device_hotplug();
622 static int pseries_add_mem_node(struct device_node
*np
)
627 unsigned int lmb_size
;
631 * Check to see if we are actually adding memory
633 type
= of_get_property(np
, "device_type", NULL
);
634 if (type
== NULL
|| strcmp(type
, "memory") != 0)
638 * Find the base and size of the memblock
640 regs
= of_get_property(np
, "reg", NULL
);
644 base
= be64_to_cpu(*(unsigned long *)regs
);
645 lmb_size
= be32_to_cpu(regs
[3]);
648 * Update memory region to represent the memory add
650 ret
= memblock_add(base
, lmb_size
);
651 return (ret
< 0) ? -EINVAL
: 0;
654 static int pseries_update_drconf_memory(struct of_reconfig_data
*pr
)
656 struct of_drconf_cell
*new_drmem
, *old_drmem
;
657 unsigned long memblock_size
;
665 memblock_size
= pseries_memory_block_size();
669 p
= (__be32
*) pr
->old_prop
->value
;
673 /* The first int of the property is the number of lmb's described
674 * by the property. This is followed by an array of of_drconf_cell
675 * entries. Get the number of entries and skip to the array of
678 entries
= be32_to_cpu(*p
++);
679 old_drmem
= (struct of_drconf_cell
*)p
;
681 p
= (__be32
*)pr
->prop
->value
;
683 new_drmem
= (struct of_drconf_cell
*)p
;
685 for (i
= 0; i
< entries
; i
++) {
686 if ((be32_to_cpu(old_drmem
[i
].flags
) & DRCONF_MEM_ASSIGNED
) &&
687 (!(be32_to_cpu(new_drmem
[i
].flags
) & DRCONF_MEM_ASSIGNED
))) {
688 rc
= pseries_remove_memblock(
689 be64_to_cpu(old_drmem
[i
].base_addr
),
692 } else if ((!(be32_to_cpu(old_drmem
[i
].flags
) &
693 DRCONF_MEM_ASSIGNED
)) &&
694 (be32_to_cpu(new_drmem
[i
].flags
) &
695 DRCONF_MEM_ASSIGNED
)) {
696 rc
= memblock_add(be64_to_cpu(old_drmem
[i
].base_addr
),
698 rc
= (rc
< 0) ? -EINVAL
: 0;
705 static int pseries_memory_notifier(struct notifier_block
*nb
,
706 unsigned long action
, void *data
)
708 struct of_reconfig_data
*rd
= data
;
712 case OF_RECONFIG_ATTACH_NODE
:
713 err
= pseries_add_mem_node(rd
->dn
);
715 case OF_RECONFIG_DETACH_NODE
:
716 err
= pseries_remove_mem_node(rd
->dn
);
718 case OF_RECONFIG_UPDATE_PROPERTY
:
719 if (!strcmp(rd
->prop
->name
, "ibm,dynamic-memory"))
720 err
= pseries_update_drconf_memory(rd
);
723 return notifier_from_errno(err
);
726 static struct notifier_block pseries_mem_nb
= {
727 .notifier_call
= pseries_memory_notifier
,
730 static int __init
pseries_memory_hotplug_init(void)
732 if (firmware_has_feature(FW_FEATURE_LPAR
))
733 of_reconfig_notifier_register(&pseries_mem_nb
);
737 machine_device_initcall(pseries
, pseries_memory_hotplug_init
);