Skip to content

Commit f1e5858

Browse files
atishp04palmer-dabbelt
authored andcommitted
RISC-V: Support cpu hotplug
This patch enable support for cpu hotplug in RISC-V. It uses SBI HSM extension to online/offline any hart. As a result, the harts are returned to firmware once they are offline. If the harts are brought online afterwards, they re-enter Linux kernel as if a secondary hart booted for the first time. All booting requirements are honored during this process. Tested both on QEMU and HighFive Unleashed board with. Test result follows. --------------------------------------------------- Offline cpu 2 --------------------------------------------------- $ echo 0 > /sys/devices/system/cpu/cpu2/online [ 32.828684] CPU2: off $ cat /proc/cpuinfo processor : 0 hart : 0 isa : rv64imafdcsu mmu : sv48 processor : 1 hart : 1 isa : rv64imafdcsu mmu : sv48 processor : 3 hart : 3 isa : rv64imafdcsu mmu : sv48 processor : 4 hart : 4 isa : rv64imafdcsu mmu : sv48 processor : 5 hart : 5 isa : rv64imafdcsu mmu : sv48 processor : 6 hart : 6 isa : rv64imafdcsu mmu : sv48 processor : 7 hart : 7 isa : rv64imafdcsu mmu : sv48 --------------------------------------------------- online cpu 2 --------------------------------------------------- $ echo 1 > /sys/devices/system/cpu/cpu2/online $ cat /proc/cpuinfo processor : 0 hart : 0 isa : rv64imafdcsu mmu : sv48 processor : 1 hart : 1 isa : rv64imafdcsu mmu : sv48 processor : 2 hart : 2 isa : rv64imafdcsu mmu : sv48 processor : 3 hart : 3 isa : rv64imafdcsu mmu : sv48 processor : 4 hart : 4 isa : rv64imafdcsu mmu : sv48 processor : 5 hart : 5 isa : rv64imafdcsu mmu : sv48 processor : 6 hart : 6 isa : rv64imafdcsu mmu : sv48 processor : 7 hart : 7 isa : rv64imafdcsu mmu : sv48 Signed-off-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]>
1 parent cfafe26 commit f1e5858

File tree

7 files changed

+180
-2
lines changed

7 files changed

+180
-2
lines changed

arch/riscv/Kconfig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ config RISCV
2020
select CLONE_BACKWARDS
2121
select COMMON_CLK
2222
select GENERIC_CLOCKEVENTS
23-
select GENERIC_CPU_DEVICES
2423
select GENERIC_IRQ_SHOW
2524
select GENERIC_PCI_IOMAP
2625
select GENERIC_SCHED_CLOCK
@@ -254,6 +253,17 @@ config NR_CPUS
254253
depends on SMP
255254
default "8"
256255

256+
config HOTPLUG_CPU
257+
bool "Support for hot-pluggable CPUs"
258+
depends on SMP
259+
select GENERIC_IRQ_MIGRATION
260+
help
261+
262+
Say Y here to experiment with turning CPUs off and on. CPUs
263+
can be controlled through /sys/devices/system/cpu.
264+
265+
Say N if you want to disable CPU hotplug.
266+
257267
choice
258268
prompt "CPU Tuning"
259269
default TUNE_GENERIC

arch/riscv/include/asm/cpu_ops.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,24 @@
1818
* is a mechanism for doing so, tests whether it is
1919
* possible to boot the given HART.
2020
* @cpu_start: Boots a cpu into the kernel.
21+
* @cpu_disable: Prepares a cpu to die. May fail for some
22+
* mechanism-specific reason, which will cause the hot
23+
* unplug to be aborted. Called from the cpu to be killed.
24+
* @cpu_stop: Makes a cpu leave the kernel. Must not fail. Called from
25+
* the cpu being stopped.
26+
* @cpu_is_stopped: Ensures a cpu has left the kernel. Called from another
27+
* cpu.
2128
*/
2229
struct cpu_operations {
2330
const char *name;
2431
int (*cpu_prepare)(unsigned int cpu);
2532
int (*cpu_start)(unsigned int cpu,
2633
struct task_struct *tidle);
34+
#ifdef CONFIG_HOTPLUG_CPU
35+
int (*cpu_disable)(unsigned int cpu);
36+
void (*cpu_stop)(void);
37+
int (*cpu_is_stopped)(unsigned int cpu);
38+
#endif
2739
};
2840

2941
extern const struct cpu_operations *cpu_ops[NR_CPUS];

arch/riscv/include/asm/smp.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out);
4343
*/
4444
#define raw_smp_processor_id() (current_thread_info()->cpu)
4545

46+
#if defined CONFIG_HOTPLUG_CPU
47+
int __cpu_disable(void);
48+
void __cpu_die(unsigned int cpu);
49+
void cpu_stop(void);
50+
#else
51+
#endif /* CONFIG_HOTPLUG_CPU */
52+
4653
#else
4754

4855
static inline void show_ipi_stats(struct seq_file *p, int prec)
@@ -69,4 +76,14 @@ static inline void riscv_cpuid_to_hartid_mask(const struct cpumask *in,
6976
}
7077

7178
#endif /* CONFIG_SMP */
79+
80+
#if defined(CONFIG_HOTPLUG_CPU) && (CONFIG_SMP)
81+
bool cpu_has_hotplug(unsigned int cpu);
82+
#else
83+
static inline bool cpu_has_hotplug(unsigned int cpu)
84+
{
85+
return false;
86+
}
87+
#endif
88+
7289
#endif /* _ASM_RISCV_SMP_H */

arch/riscv/kernel/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,6 @@ obj-$(CONFIG_RISCV_SBI) += sbi.o
4949
ifeq ($(CONFIG_RISCV_SBI), y)
5050
obj-$(CONFIG_SMP) += cpu_ops_sbi.o
5151
endif
52+
obj-$(CONFIG_HOTPLUG_CPU) += cpu-hotplug.o
5253

5354
clean:

arch/riscv/kernel/cpu-hotplug.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2020 Western Digital Corporation or its affiliates.
4+
*/
5+
6+
#include <linux/kernel.h>
7+
#include <linux/mm.h>
8+
#include <linux/sched.h>
9+
#include <linux/err.h>
10+
#include <linux/irq.h>
11+
#include <linux/cpu.h>
12+
#include <linux/sched/hotplug.h>
13+
#include <asm/irq.h>
14+
#include <asm/cpu_ops.h>
15+
#include <asm/sbi.h>
16+
17+
void cpu_stop(void);
18+
void arch_cpu_idle_dead(void)
19+
{
20+
cpu_stop();
21+
}
22+
23+
bool cpu_has_hotplug(unsigned int cpu)
24+
{
25+
if (cpu_ops[cpu]->cpu_stop)
26+
return true;
27+
28+
return false;
29+
}
30+
31+
/*
32+
* __cpu_disable runs on the processor to be shutdown.
33+
*/
34+
int __cpu_disable(void)
35+
{
36+
int ret = 0;
37+
unsigned int cpu = smp_processor_id();
38+
39+
if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_stop)
40+
return -EOPNOTSUPP;
41+
42+
if (cpu_ops[cpu]->cpu_disable)
43+
ret = cpu_ops[cpu]->cpu_disable(cpu);
44+
45+
if (ret)
46+
return ret;
47+
48+
remove_cpu_topology(cpu);
49+
set_cpu_online(cpu, false);
50+
irq_migrate_all_off_this_cpu();
51+
52+
return ret;
53+
}
54+
55+
/*
56+
* Called on the thread which is asking for a CPU to be shutdown.
57+
*/
58+
void __cpu_die(unsigned int cpu)
59+
{
60+
int ret = 0;
61+
62+
if (!cpu_wait_death(cpu, 5)) {
63+
pr_err("CPU %u: didn't die\n", cpu);
64+
return;
65+
}
66+
pr_notice("CPU%u: off\n", cpu);
67+
68+
/* Verify from the firmware if the cpu is really stopped*/
69+
if (cpu_ops[cpu]->cpu_is_stopped)
70+
ret = cpu_ops[cpu]->cpu_is_stopped(cpu);
71+
if (ret)
72+
pr_warn("CPU%d may not have stopped: %d\n", cpu, ret);
73+
}
74+
75+
/*
76+
* Called from the idle thread for the CPU which has been shutdown.
77+
*/
78+
void cpu_stop(void)
79+
{
80+
idle_task_exit();
81+
82+
(void)cpu_report_death();
83+
84+
cpu_ops[smp_processor_id()]->cpu_stop();
85+
/* It should never reach here */
86+
BUG();
87+
}

arch/riscv/kernel/cpu_ops_sbi.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,42 @@ static int sbi_cpu_prepare(unsigned int cpuid)
7474
return 0;
7575
}
7676

77+
#ifdef CONFIG_HOTPLUG_CPU
78+
static int sbi_cpu_disable(unsigned int cpuid)
79+
{
80+
if (!cpu_ops_sbi.cpu_stop)
81+
return -EOPNOTSUPP;
82+
return 0;
83+
}
84+
85+
static void sbi_cpu_stop(void)
86+
{
87+
int ret;
88+
89+
ret = sbi_hsm_hart_stop();
90+
pr_crit("Unable to stop the cpu %u (%d)\n", smp_processor_id(), ret);
91+
}
92+
93+
static int sbi_cpu_is_stopped(unsigned int cpuid)
94+
{
95+
int rc;
96+
int hartid = cpuid_to_hartid_map(cpuid);
97+
98+
rc = sbi_hsm_hart_get_status(hartid);
99+
100+
if (rc == SBI_HSM_HART_STATUS_STOPPED)
101+
return 0;
102+
return rc;
103+
}
104+
#endif
105+
77106
const struct cpu_operations cpu_ops_sbi = {
78107
.name = "sbi",
79108
.cpu_prepare = sbi_cpu_prepare,
80109
.cpu_start = sbi_cpu_start,
110+
#ifdef CONFIG_HOTPLUG_CPU
111+
.cpu_disable = sbi_cpu_disable,
112+
.cpu_stop = sbi_cpu_stop,
113+
.cpu_is_stopped = sbi_cpu_is_stopped,
114+
#endif
81115
};

arch/riscv/kernel/setup.c

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
#include <linux/of_platform.h>
1717
#include <linux/sched/task.h>
1818
#include <linux/swiotlb.h>
19+
#include <linux/smp.h>
1920

2021
#include <asm/clint.h>
22+
#include <asm/cpu_ops.h>
2123
#include <asm/setup.h>
2224
#include <asm/sections.h>
2325
#include <asm/pgtable.h>
24-
#include <asm/smp.h>
2526
#include <asm/sbi.h>
2627
#include <asm/tlbflush.h>
2728
#include <asm/thread_info.h>
@@ -47,6 +48,7 @@ struct screen_info screen_info = {
4748
*/
4849
atomic_t hart_lottery __section(.sdata);
4950
unsigned long boot_cpu_hartid;
51+
static DEFINE_PER_CPU(struct cpu, cpu_devices);
5052

5153
void __init parse_dtb(void)
5254
{
@@ -94,3 +96,18 @@ void __init setup_arch(char **cmdline_p)
9496

9597
riscv_fill_hwcap();
9698
}
99+
100+
static int __init topology_init(void)
101+
{
102+
int i;
103+
104+
for_each_possible_cpu(i) {
105+
struct cpu *cpu = &per_cpu(cpu_devices, i);
106+
107+
cpu->hotpluggable = cpu_has_hotplug(i);
108+
register_cpu(cpu, i);
109+
}
110+
111+
return 0;
112+
}
113+
subsys_initcall(topology_init);

0 commit comments

Comments
 (0)