Skip to content

Commit cfafe26

Browse files
atishp04palmer-dabbelt
authored andcommitted
RISC-V: Add supported for ordered booting method using HSM
Currently, all harts have to jump Linux in RISC-V. This complicates the multi-stage boot process as every transient stage also has to ensure all harts enter to that stage and jump to Linux afterwards. It also obstructs a clean Kexec implementation. SBI HSM extension provides alternate solutions where only a single hart need to boot and enter Linux. The booting hart can bring up secondary harts one by one afterwards. Add SBI HSM based cpu_ops that implements an ordered booting method in RISC-V. This change is also backward compatible with older firmware not implementing HSM extension. If a latest kernel is used with older firmware, it will continue to use the default spinning booting method. Signed-off-by: Atish Patra <[email protected]> Reviewed-by: Anup Patel <[email protected]> Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent db5a794 commit cfafe26

File tree

6 files changed

+121
-3
lines changed

6 files changed

+121
-3
lines changed

arch/riscv/kernel/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@ obj-$(CONFIG_PERF_EVENTS) += perf_event.o
4646
obj-$(CONFIG_PERF_EVENTS) += perf_callchain.o
4747
obj-$(CONFIG_HAVE_PERF_REGS) += perf_regs.o
4848
obj-$(CONFIG_RISCV_SBI) += sbi.o
49+
ifeq ($(CONFIG_RISCV_SBI), y)
50+
obj-$(CONFIG_SMP) += cpu_ops_sbi.o
51+
endif
4952

5053
clean:

arch/riscv/kernel/cpu_ops.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init;
1818
void *__cpu_up_stack_pointer[NR_CPUS];
1919
void *__cpu_up_task_pointer[NR_CPUS];
2020

21+
extern const struct cpu_operations cpu_ops_sbi;
2122
extern const struct cpu_operations cpu_ops_spinwait;
2223

2324
void cpu_update_secondary_bootdata(unsigned int cpuid,
@@ -34,5 +35,12 @@ void cpu_update_secondary_bootdata(unsigned int cpuid,
3435

3536
void __init cpu_set_ops(int cpuid)
3637
{
37-
cpu_ops[cpuid] = &cpu_ops_spinwait;
38+
#if IS_ENABLED(CONFIG_RISCV_SBI)
39+
if (sbi_probe_extension(SBI_EXT_HSM) > 0) {
40+
if (!cpuid)
41+
pr_info("SBI v0.2 HSM extension detected\n");
42+
cpu_ops[cpuid] = &cpu_ops_sbi;
43+
} else
44+
#endif
45+
cpu_ops[cpuid] = &cpu_ops_spinwait;
3846
}

arch/riscv/kernel/cpu_ops_sbi.c

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/*
3+
* HSM extension and cpu_ops implementation.
4+
*
5+
* Copyright (c) 2020 Western Digital Corporation or its affiliates.
6+
*/
7+
8+
#include <linux/init.h>
9+
#include <linux/mm.h>
10+
#include <asm/cpu_ops.h>
11+
#include <asm/sbi.h>
12+
#include <asm/smp.h>
13+
14+
extern char secondary_start_sbi[];
15+
const struct cpu_operations cpu_ops_sbi;
16+
17+
static int sbi_hsm_hart_start(unsigned long hartid, unsigned long saddr,
18+
unsigned long priv)
19+
{
20+
struct sbiret ret;
21+
22+
ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_START,
23+
hartid, saddr, priv, 0, 0, 0);
24+
if (ret.error)
25+
return sbi_err_map_linux_errno(ret.error);
26+
else
27+
return 0;
28+
}
29+
30+
#ifdef CONFIG_HOTPLUG_CPU
31+
static int sbi_hsm_hart_stop(void)
32+
{
33+
struct sbiret ret;
34+
35+
ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_STOP, 0, 0, 0, 0, 0, 0);
36+
37+
if (ret.error)
38+
return sbi_err_map_linux_errno(ret.error);
39+
else
40+
return 0;
41+
}
42+
43+
static int sbi_hsm_hart_get_status(unsigned long hartid)
44+
{
45+
struct sbiret ret;
46+
47+
ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_STATUS,
48+
hartid, 0, 0, 0, 0, 0);
49+
if (ret.error)
50+
return sbi_err_map_linux_errno(ret.error);
51+
else
52+
return ret.value;
53+
}
54+
#endif
55+
56+
static int sbi_cpu_start(unsigned int cpuid, struct task_struct *tidle)
57+
{
58+
int rc;
59+
unsigned long boot_addr = __pa_symbol(secondary_start_sbi);
60+
int hartid = cpuid_to_hartid_map(cpuid);
61+
62+
cpu_update_secondary_bootdata(cpuid, tidle);
63+
rc = sbi_hsm_hart_start(hartid, boot_addr, 0);
64+
65+
return rc;
66+
}
67+
68+
static int sbi_cpu_prepare(unsigned int cpuid)
69+
{
70+
if (!cpu_ops_sbi.cpu_start) {
71+
pr_err("cpu start method not defined for CPU [%d]\n", cpuid);
72+
return -ENODEV;
73+
}
74+
return 0;
75+
}
76+
77+
const struct cpu_operations cpu_ops_sbi = {
78+
.name = "sbi",
79+
.cpu_prepare = sbi_cpu_prepare,
80+
.cpu_start = sbi_cpu_start,
81+
};

arch/riscv/kernel/head.S

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,37 @@ relocate:
9999
ret
100100
#endif /* CONFIG_MMU */
101101
#ifdef CONFIG_SMP
102+
.global secondary_start_sbi
103+
secondary_start_sbi:
104+
/* Mask all interrupts */
105+
csrw CSR_IE, zero
106+
csrw CSR_IP, zero
107+
108+
/* Load the global pointer */
109+
.option push
110+
.option norelax
111+
la gp, __global_pointer$
112+
.option pop
113+
114+
/*
115+
* Disable FPU to detect illegal usage of
116+
* floating point in kernel space
117+
*/
118+
li t0, SR_FS
119+
csrc CSR_STATUS, t0
120+
102121
/* Set trap vector to spin forever to help debug */
103122
la a3, .Lsecondary_park
104123
csrw CSR_TVEC, a3
105124

106125
slli a3, a0, LGREG
126+
la a4, __cpu_up_stack_pointer
127+
la a5, __cpu_up_task_pointer
128+
add a4, a3, a4
129+
add a5, a3, a5
130+
REG_L sp, (a4)
131+
REG_L tp, (a5)
132+
107133
.global secondary_start_common
108134
secondary_start_common:
109135

arch/riscv/kernel/smpboot.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ void __init smp_cpus_done(unsigned int max_cpus)
143143
/*
144144
* C entry point for a secondary processor.
145145
*/
146-
asmlinkage __visible void __init smp_callin(void)
146+
asmlinkage __visible void smp_callin(void)
147147
{
148148
struct mm_struct *mm = &init_mm;
149149

arch/riscv/kernel/traps.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ int is_valid_bugaddr(unsigned long pc)
148148
}
149149
#endif /* CONFIG_GENERIC_BUG */
150150

151-
void __init trap_init(void)
151+
void trap_init(void)
152152
{
153153
/*
154154
* Set sup0 scratch register to 0, indicating to exception vector

0 commit comments

Comments
 (0)