Skip to contentSkip to conversation

Add support for SBI v0.2 and CPU hotplug

PATCH Superseded Atish Patra 5 participants 25 messages Feb 12, 2020 – Feb 20, 2020

Subsystems (4)

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v8 cover Add support for SBI v0.2 and CPU hotplug

The Supervisor Binary Interface(SBI) specification[1] now defines a base extension that provides extendability to add future extensions while maintaining backward compatibility with previous versions. The new version is defined as 0.2 and older version is marked as 0.1.

This series adds support v0.2 and a unified calling convention implementation between 0.1 and 0.2. It also add other SBI v0.2 functionality defined in [2]. The base support for SBI v0.2 is already available in OpenSBI v0.5. It also adds SBI HSM extension and cpu-hotplug support for RISC-V which requires additional patches[3] in OpenSBI.

[1] https://github.com/riscv/riscv-sbi-doc/blob/master/riscv-sbi.adoc
[2] https://github.com/riscv/riscv-sbi-doc/pull/27
[3] http://lists.infradead.org/pipermail/opensbi/2020-January/001050.html

The patches are also available in following github repositery.

OpenSBI : https://github.com/atishp04/opensbi/tree/sbi_hsm_v1
Linux Kernel: https://github.com/atishp04/linux/tree/sbi_v0.2_v8

Changes from v7->v8:
1. Refactored to code to have modular cpu_ops calls.
2. Refactored HSM extension from sbi.c to cpu_ops_sbi.c.
3. Fix plic driver to handle cpu hotplug.

Changes from v6->v7:
1. Rebased on v5.5
2. Fixed few compilation issues for !CONFIG_SMP and !CONFIG_RISCV_SBI
3. Added SBI HSM extension
4. Add CPU hotplug support

Changes from v5->v6
1. Fixed few compilation issues around config.
2. Fixed hart mask generation issues for RFENCE & IPI extensions.

Changes from v4->v5
1. Fixed few minor comments related to static & inline.
2. Make sure that every patch is boot tested individually.

Changes from v3->v4.
1. Rebased on for-next.
2. Fixed issuses with checkpatch --strict.
3. Unfied all IPI/fence related functions.
4. Added Hfence related SBI calls.

Changes from v2->v3.
1. Moved v0.1 extensions to a new config.
2. Added support for relacement extensions of v0.1 extensions.

Changes from v1->v2
1. Removed the legacy calling convention.
2. Moved all SBI related calls to sbi.c.
3. Moved all SBI related macros to uapi.

Atish Patra (11):
RISC-V: Mark existing SBI as 0.1 SBI.
RISC-V: Add basic support for SBI v0.2
RISC-V: Add SBI v0.2 extension definitions
RISC-V: Introduce a new config for SBI v0.1
RISC-V: Implement new SBI v0.2 extensions
RISC-V: Move relocate and few other functions out of __init
RISC-V: Add cpu_ops and modify default booting method
RISC-V: Add SBI HSM extension
RISC-V: Add supported for ordered booting method using HSM irqchip/sifive-plic: Initialize the plic handler when cpu comes online
RISC-V: Support cpu hotplug

arch/riscv/Kconfig                   |  19 +-
arch/riscv/include/asm/cpu_ops.h     |  46 +++
arch/riscv/include/asm/sbi.h         | 194 ++++++----
arch/riscv/include/asm/smp.h         |  24 ++
arch/riscv/kernel/Makefile           |   6 +
arch/riscv/kernel/cpu-hotplug.c      |  87 +++++
arch/riscv/kernel/cpu_ops.c          |  48 +++
arch/riscv/kernel/cpu_ops_sbi.c      | 113 ++++++
arch/riscv/kernel/cpu_ops_spinwait.c |  42 +++
arch/riscv/kernel/head.S             | 179 +++++----
arch/riscv/kernel/sbi.c              | 524 ++++++++++++++++++++++++++-
arch/riscv/kernel/setup.c            |  24 +-
arch/riscv/kernel/smpboot.c          |  56 +--
arch/riscv/kernel/traps.c            |   2 +-
arch/riscv/kernel/vmlinux.lds.S      |   5 +-
drivers/irqchip/irq-sifive-plic.c    |  34 +-
include/linux/cpuhotplug.h           |   1 +
17 files changed, 1227 insertions(+), 177 deletions(-)

create mode 100644 arch/riscv/include/asm/cpu_ops.h create mode 100644 arch/riscv/kernel/cpu-hotplug.c create mode 100644 arch/riscv/kernel/cpu_ops.c create mode 100644 arch/riscv/kernel/cpu_ops_sbi.c create mode 100644 arch/riscv/kernel/cpu_ops_spinwait.c

--
2.24.0

#1 lore raw

PATCH v8 1/11 RISC-V: Mark existing SBI as 0.1 SBI.

As per the new SBI specification, current SBI implementation version is defined as 0.1 and will be removed/replaced in future. Each of the function call in 0.1 is defined as a separate extension which makes easier to replace them one at a time.

Rename existing implementation to reflect that. This patch is just a preparatory patch for SBI v0.2 and doesn't introduce any functional changes.

---
 arch/riscv/include/asm/sbi.h | 44 ++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 20 deletions(-)
arch/riscv/include/asm/sbi.h +24 -20
index 2570c1e683d3..b38bc36f7429 100644
@@ -1,6 +1,7 @@
 /* SPDX-License-Identifier: GPL-2.0-only */
 /*
  * Copyright (C) 2015 Regents of the University of California
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
  */
 
 #ifndef _ASM_RISCV_SBI_H
@@ -9,17 +10,17 @@
 #include <linux/types.h>
 
 #ifdef CONFIG_RISCV_SBI
-#define SBI_SET_TIMER 0
-#define SBI_CONSOLE_PUTCHAR 1
-#define SBI_CONSOLE_GETCHAR 2
-#define SBI_CLEAR_IPI 3
-#define SBI_SEND_IPI 4
-#define SBI_REMOTE_FENCE_I 5
-#define SBI_REMOTE_SFENCE_VMA 6
-#define SBI_REMOTE_SFENCE_VMA_ASID 7
-#define SBI_SHUTDOWN 8
+#define SBI_EXT_0_1_SET_TIMER 0x0
+#define SBI_EXT_0_1_CONSOLE_PUTCHAR 0x1
+#define SBI_EXT_0_1_CONSOLE_GETCHAR 0x2
+#define SBI_EXT_0_1_CLEAR_IPI 0x3
+#define SBI_EXT_0_1_SEND_IPI 0x4
+#define SBI_EXT_0_1_REMOTE_FENCE_I 0x5
+#define SBI_EXT_0_1_REMOTE_SFENCE_VMA 0x6
+#define SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID 0x7
+#define SBI_EXT_0_1_SHUTDOWN 0x8
 
-#define SBI_CALL(which, arg0, arg1, arg2, arg3) ({		\
+#define SBI_CALL(which, arg0, arg1, arg2, arg3) ({             \
 	register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);	\
 	register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);	\
 	register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);	\
@@ -43,48 +44,50 @@
 
 static inline void sbi_console_putchar(int ch)
 {
-	SBI_CALL_1(SBI_CONSOLE_PUTCHAR, ch);
+	SBI_CALL_1(SBI_EXT_0_1_CONSOLE_PUTCHAR, ch);
 }
 
 static inline int sbi_console_getchar(void)
 {
-	return SBI_CALL_0(SBI_CONSOLE_GETCHAR);
+	return SBI_CALL_0(SBI_EXT_0_1_CONSOLE_GETCHAR);
 }
 
 static inline void sbi_set_timer(uint64_t stime_value)
 {
 #if __riscv_xlen == 32
-	SBI_CALL_2(SBI_SET_TIMER, stime_value, stime_value >> 32);
+	SBI_CALL_2(SBI_EXT_0_1_SET_TIMER, stime_value,
+			  stime_value >> 32);
 #else
-	SBI_CALL_1(SBI_SET_TIMER, stime_value);
+	SBI_CALL_1(SBI_EXT_0_1_SET_TIMER, stime_value);
 #endif
 }
 
 static inline void sbi_shutdown(void)
 {
-	SBI_CALL_0(SBI_SHUTDOWN);
+	SBI_CALL_0(SBI_EXT_0_1_SHUTDOWN);
 }
 
 static inline void sbi_clear_ipi(void)
 {
-	SBI_CALL_0(SBI_CLEAR_IPI);
+	SBI_CALL_0(SBI_EXT_0_1_CLEAR_IPI);
 }
 
 static inline void sbi_send_ipi(const unsigned long *hart_mask)
 {
-	SBI_CALL_1(SBI_SEND_IPI, hart_mask);
+	SBI_CALL_1(SBI_EXT_0_1_SEND_IPI, hart_mask);
 }
 
 static inline void sbi_remote_fence_i(const unsigned long *hart_mask)
 {
-	SBI_CALL_1(SBI_REMOTE_FENCE_I, hart_mask);
+	SBI_CALL_1(SBI_EXT_0_1_REMOTE_FENCE_I, hart_mask);
 }
 
 static inline void sbi_remote_sfence_vma(const unsigned long *hart_mask,
 					 unsigned long start,
 					 unsigned long size)
 {
-	SBI_CALL_3(SBI_REMOTE_SFENCE_VMA, hart_mask, start, size);
+	SBI_CALL_3(SBI_EXT_0_1_REMOTE_SFENCE_VMA, hart_mask,
+			  start, size);
 }
 
 static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
@@ -92,7 +95,8 @@ static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
 					      unsigned long size,
 					      unsigned long asid)
 {
-	SBI_CALL_4(SBI_REMOTE_SFENCE_VMA_ASID, hart_mask, start, size, asid);
+	SBI_CALL_4(SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, hart_mask,
+			  start, size, asid);
 }
 #else /* CONFIG_RISCV_SBI */
 /* stubs for code that is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */
Signature · 1 line
2.24.0
#2 lore raw

PATCH v8 2/11 RISC-V: Add basic support for SBI v0.2

In reply to Atish Patra

The SBI v0.2 introduces a base extension which is backward compatible with v0.1. Implement all helper functions and minimum required SBI calls from v0.2 for now. All other base extension function will be added later as per need.
As v0.2 calling convention is backward compatible with v0.1, remove the v0.1 helper functions and just use v0.2 calling convention.

---
 arch/riscv/include/asm/sbi.h | 140 ++++++++++----------
 arch/riscv/kernel/sbi.c      | 243 ++++++++++++++++++++++++++++++++++-
 arch/riscv/kernel/setup.c    |   5 +
 3 files changed, 314 insertions(+), 74 deletions(-)
arch/riscv/include/asm/sbi.h +68 -72
index b38bc36f7429..fbdb7443784a 100644
@@ -10,93 +10,88 @@
 #include <linux/types.h>
 
 #ifdef CONFIG_RISCV_SBI
-#define SBI_EXT_0_1_SET_TIMER 0x0
-#define SBI_EXT_0_1_CONSOLE_PUTCHAR 0x1
-#define SBI_EXT_0_1_CONSOLE_GETCHAR 0x2
-#define SBI_EXT_0_1_CLEAR_IPI 0x3
-#define SBI_EXT_0_1_SEND_IPI 0x4
-#define SBI_EXT_0_1_REMOTE_FENCE_I 0x5
-#define SBI_EXT_0_1_REMOTE_SFENCE_VMA 0x6
-#define SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID 0x7
-#define SBI_EXT_0_1_SHUTDOWN 0x8
+enum sbi_ext_id {
+	SBI_EXT_0_1_SET_TIMER = 0x0,
+	SBI_EXT_0_1_CONSOLE_PUTCHAR = 0x1,
+	SBI_EXT_0_1_CONSOLE_GETCHAR = 0x2,
+	SBI_EXT_0_1_CLEAR_IPI = 0x3,
+	SBI_EXT_0_1_SEND_IPI = 0x4,
+	SBI_EXT_0_1_REMOTE_FENCE_I = 0x5,
+	SBI_EXT_0_1_REMOTE_SFENCE_VMA = 0x6,
+	SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID = 0x7,
+	SBI_EXT_0_1_SHUTDOWN = 0x8,
+	SBI_EXT_BASE = 0x10,
+};
 
-#define SBI_CALL(which, arg0, arg1, arg2, arg3) ({             \
-	register uintptr_t a0 asm ("a0") = (uintptr_t)(arg0);	\
-	register uintptr_t a1 asm ("a1") = (uintptr_t)(arg1);	\
-	register uintptr_t a2 asm ("a2") = (uintptr_t)(arg2);	\
-	register uintptr_t a3 asm ("a3") = (uintptr_t)(arg3);	\
-	register uintptr_t a7 asm ("a7") = (uintptr_t)(which);	\
-	asm volatile ("ecall"					\
-		      : "+r" (a0)				\
-		      : "r" (a1), "r" (a2), "r" (a3), "r" (a7)	\
-		      : "memory");				\
-	a0;							\
-})
+enum sbi_ext_base_fid {
+	SBI_EXT_BASE_GET_SPEC_VERSION = 0,
+	SBI_EXT_BASE_GET_IMP_ID,
+	SBI_EXT_BASE_GET_IMP_VERSION,
+	SBI_EXT_BASE_PROBE_EXT,
+	SBI_EXT_BASE_GET_MVENDORID,
+	SBI_EXT_BASE_GET_MARCHID,
+	SBI_EXT_BASE_GET_MIMPID,
+};
 
-/* Lazy implementations until SBI is finalized */
-#define SBI_CALL_0(which) SBI_CALL(which, 0, 0, 0, 0)
-#define SBI_CALL_1(which, arg0) SBI_CALL(which, arg0, 0, 0, 0)
-#define SBI_CALL_2(which, arg0, arg1) SBI_CALL(which, arg0, arg1, 0, 0)
-#define SBI_CALL_3(which, arg0, arg1, arg2) \
-		SBI_CALL(which, arg0, arg1, arg2, 0)
-#define SBI_CALL_4(which, arg0, arg1, arg2, arg3) \
-		SBI_CALL(which, arg0, arg1, arg2, arg3)
+#define SBI_SPEC_VERSION_DEFAULT	0x1
+#define SBI_SPEC_VERSION_MAJOR_SHIFT	24
+#define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
+#define SBI_SPEC_VERSION_MINOR_MASK	0xffffff
 
-static inline void sbi_console_putchar(int ch)
-{
-	SBI_CALL_1(SBI_EXT_0_1_CONSOLE_PUTCHAR, ch);
-}
+/* SBI return error codes */
+#define SBI_SUCCESS		0
+#define SBI_ERR_FAILURE		-1
+#define SBI_ERR_NOT_SUPPORTED	-2
+#define SBI_ERR_INVALID_PARAM   -3
+#define SBI_ERR_DENIED		-4
+#define SBI_ERR_INVALID_ADDRESS -5
 
-static inline int sbi_console_getchar(void)
-{
-	return SBI_CALL_0(SBI_EXT_0_1_CONSOLE_GETCHAR);
-}
+extern unsigned long sbi_spec_version;
+struct sbiret {
+	long error;
+	long value;
+};
 
-static inline void sbi_set_timer(uint64_t stime_value)
-{
-#if __riscv_xlen == 32
-	SBI_CALL_2(SBI_EXT_0_1_SET_TIMER, stime_value,
-			  stime_value >> 32);
-#else
-	SBI_CALL_1(SBI_EXT_0_1_SET_TIMER, stime_value);
-#endif
-}
-
-static inline void sbi_shutdown(void)
-{
-	SBI_CALL_0(SBI_EXT_0_1_SHUTDOWN);
-}
+int sbi_init(void);
+struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
+			unsigned long arg1, unsigned long arg2,
+			unsigned long arg3, unsigned long arg4,
+			unsigned long arg5);
 
-static inline void sbi_clear_ipi(void)
-{
-	SBI_CALL_0(SBI_EXT_0_1_CLEAR_IPI);
-}
+void sbi_console_putchar(int ch);
+int sbi_console_getchar(void);
+void sbi_set_timer(uint64_t stime_value);
+void sbi_shutdown(void);
+void sbi_clear_ipi(void);
+void sbi_send_ipi(const unsigned long *hart_mask);
+void sbi_remote_fence_i(const unsigned long *hart_mask);
+void sbi_remote_sfence_vma(const unsigned long *hart_mask,
+			   unsigned long start,
+			   unsigned long size);
 
-static inline void sbi_send_ipi(const unsigned long *hart_mask)
-{
-	SBI_CALL_1(SBI_EXT_0_1_SEND_IPI, hart_mask);
-}
+void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
+				unsigned long start,
+				unsigned long size,
+				unsigned long asid);
+int sbi_probe_extension(int ext);
 
-static inline void sbi_remote_fence_i(const unsigned long *hart_mask)
+/* Check if current SBI specification version is 0.1 or not */
+static inline int sbi_spec_is_0_1(void)
 {
-	SBI_CALL_1(SBI_EXT_0_1_REMOTE_FENCE_I, hart_mask);
+	return (sbi_spec_version == SBI_SPEC_VERSION_DEFAULT) ? 1 : 0;
 }
 
-static inline void sbi_remote_sfence_vma(const unsigned long *hart_mask,
-					 unsigned long start,
-					 unsigned long size)
+/* Get the major version of SBI */
+static inline unsigned long sbi_major_version(void)
 {
-	SBI_CALL_3(SBI_EXT_0_1_REMOTE_SFENCE_VMA, hart_mask,
-			  start, size);
+	return (sbi_spec_version >> SBI_SPEC_VERSION_MAJOR_SHIFT) &
+		SBI_SPEC_VERSION_MAJOR_MASK;
 }
 
-static inline void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
-					      unsigned long start,
-					      unsigned long size,
-					      unsigned long asid)
+/* Get the minor version of SBI */
+static inline unsigned long sbi_minor_version(void)
 {
-	SBI_CALL_4(SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID, hart_mask,
-			  start, size, asid);
+	return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
 }
 #else /* CONFIG_RISCV_SBI */
 /* stubs for code that is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */
@@ -104,5 +99,6 @@ void sbi_set_timer(uint64_t stime_value);
 void sbi_clear_ipi(void);
 void sbi_send_ipi(const unsigned long *hart_mask);
 void sbi_remote_fence_i(const unsigned long *hart_mask);
+void sbi_init(void);
 #endif /* CONFIG_RISCV_SBI */
 #endif /* _ASM_RISCV_SBI_H */
arch/riscv/kernel/sbi.c +241 -2

258 lines not on this page · raw message

arch/riscv/kernel/setup.c +5 -0
index 365ff8420bfe..64d17cbae66b 100644
@@ -22,6 +22,7 @@
 #include <asm/sections.h>
 #include <asm/pgtable.h>
 #include <asm/smp.h>
+#include <asm/sbi.h>
 #include <asm/tlbflush.h>
 #include <asm/thread_info.h>
 
@@ -74,6 +75,10 @@ void __init setup_arch(char **cmdline_p)
 	swiotlb_init(1);
 #endif
 
+#if IS_ENABLED(CONFIG_RISCV_SBI)
+		sbi_init();
+#endif
+
 #ifdef CONFIG_SMP
 	setup_smp();
 #endif
Signature · 1 line
2.24.0
#3 lore raw

PATCH v8 3/11 RISC-V: Add SBI v0.2 extension definitions

In reply to Atish Patra

Few v0.1 SBI calls are being replaced by new SBI calls that follows v0.2 calling convention.

This patch just defines these new extensions.

---
 arch/riscv/include/asm/sbi.h | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/riscv/include/asm/sbi.h +21 -0
index fbdb7443784a..e478368a47f3 100644
@@ -21,6 +21,9 @@ enum sbi_ext_id {
 	SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID = 0x7,
 	SBI_EXT_0_1_SHUTDOWN = 0x8,
 	SBI_EXT_BASE = 0x10,
+	SBI_EXT_TIME = 0x54494D45,
+	SBI_EXT_IPI = 0x735049,
+	SBI_EXT_RFENCE = 0x52464E43,
 };
 
 enum sbi_ext_base_fid {
@@ -33,6 +36,24 @@ enum sbi_ext_base_fid {
 	SBI_EXT_BASE_GET_MIMPID,
 };
 
+enum sbi_ext_time_fid {
+	SBI_EXT_TIME_SET_TIMER = 0,
+};
+
+enum sbi_ext_ipi_fid {
+	SBI_EXT_IPI_SEND_IPI = 0,
+};
+
+enum sbi_ext_rfence_fid {
+	SBI_EXT_RFENCE_REMOTE_FENCE_I = 0,
+	SBI_EXT_RFENCE_REMOTE_SFENCE_VMA,
+	SBI_EXT_RFENCE_REMOTE_SFENCE_VMA_ASID,
+	SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA,
+	SBI_EXT_RFENCE_REMOTE_HFENCE_GVMA_VMID,
+	SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA,
+	SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
+};
+
 #define SBI_SPEC_VERSION_DEFAULT	0x1
 #define SBI_SPEC_VERSION_MAJOR_SHIFT	24
 #define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
Signature · 1 line
2.24.0
#4 lore raw

PATCH v8 4/11 RISC-V: Introduce a new config for SBI v0.1

In reply to Atish Patra

We now have SBI v0.2 which is more scalable and extendable to handle future needs for RISC-V supervisor interfaces.

Introduce a new config and move all SBI v0.1 code under that config. This allows to implement the new replacement SBI extensions cleanly and remove v0.1 extensions easily in future. Currently, the config is enabled by default. Once all M-mode software, with v0.1, is no longer in use, this config option and all relevant code can be easily removed.

---
 arch/riscv/Kconfig           |   7 ++
 arch/riscv/include/asm/sbi.h |   2 +
 arch/riscv/kernel/sbi.c      | 137 +++++++++++++++++++++++++++++------
 3 files changed, 122 insertions(+), 24 deletions(-)
arch/riscv/Kconfig +7 -0
index fa7dc03459e7..2afe1382dc36 100644
@@ -307,6 +307,13 @@ config SECCOMP
 	  and the task is only allowed to execute a few safe syscalls
 	  defined by each seccomp mode.
 
+config RISCV_SBI_V01
+	bool "SBI v0.1 support"
+	default y
+	depends on RISCV_SBI
+	help
+	  This config allows kernel to use SBI v0.1 APIs. This will be
+	  deprecated in future once legacy M-mode software are no longer in use.
 endmenu
 
 menu "Boot options"
arch/riscv/include/asm/sbi.h +2 -0
index e478368a47f3..4d67bef8f894 100644
@@ -11,6 +11,7 @@
 
 #ifdef CONFIG_RISCV_SBI
 enum sbi_ext_id {
+#ifdef CONFIG_RISCV_SBI_V01
 	SBI_EXT_0_1_SET_TIMER = 0x0,
 	SBI_EXT_0_1_CONSOLE_PUTCHAR = 0x1,
 	SBI_EXT_0_1_CONSOLE_GETCHAR = 0x2,
@@ -20,6 +21,7 @@ enum sbi_ext_id {
 	SBI_EXT_0_1_REMOTE_SFENCE_VMA = 0x6,
 	SBI_EXT_0_1_REMOTE_SFENCE_VMA_ASID = 0x7,
 	SBI_EXT_0_1_SHUTDOWN = 0x8,
+#endif
 	SBI_EXT_BASE = 0x10,
 	SBI_EXT_TIME = 0x54494D45,
 	SBI_EXT_IPI = 0x735049,
arch/riscv/kernel/sbi.c +113 -24

209 lines not on this page · raw message

Signature · 1 line
2.24.0
#5 lore raw

PATCH v8 5/11 RISC-V: Implement new SBI v0.2 extensions

In reply to Atish Patra

Few v0.1 SBI calls are being replaced by new SBI calls that follows v0.2 calling convention.

Implement the replacement extensions and few additional new SBI function calls that makes way for a better SBI interface in future.

---
 arch/riscv/include/asm/sbi.h |  14 +++
 arch/riscv/include/asm/smp.h |   7 ++
 arch/riscv/kernel/sbi.c      | 199 ++++++++++++++++++++++++++++++++++-
 3 files changed, 216 insertions(+), 4 deletions(-)
arch/riscv/include/asm/sbi.h +14 -0
index 4d67bef8f894..d55d8090ab5c 100644
@@ -96,6 +96,20 @@ void sbi_remote_sfence_vma_asid(const unsigned long *hart_mask,
 				unsigned long start,
 				unsigned long size,
 				unsigned long asid);
+int sbi_remote_hfence_gvma(const unsigned long *hart_mask,
+			   unsigned long start,
+			   unsigned long size);
+int sbi_remote_hfence_gvma_vmid(const unsigned long *hart_mask,
+				unsigned long start,
+				unsigned long size,
+				unsigned long vmid);
+int sbi_remote_hfence_vvma(const unsigned long *hart_mask,
+			   unsigned long start,
+			   unsigned long size);
+int sbi_remote_hfence_vvma_asid(const unsigned long *hart_mask,
+				unsigned long start,
+				unsigned long size,
+				unsigned long asid);
 int sbi_probe_extension(int ext);
 
 /* Check if current SBI specification version is 0.1 or not */
arch/riscv/include/asm/smp.h +7 -0
index a83451d73a4e..023f74fb8b3b 100644
@@ -61,5 +61,12 @@ static inline unsigned long cpuid_to_hartid_map(int cpu)
 	return boot_cpu_hartid;
 }
 
+static inline void riscv_cpuid_to_hartid_mask(const struct cpumask *in,
+					      struct cpumask *out)
+{
+	cpumask_clear(out);
+	cpumask_set_cpu(boot_cpu_hartid, out);
+}
+
 #endif /* CONFIG_SMP */
 #endif /* _ASM_RISCV_SMP_H */
arch/riscv/kernel/sbi.c +195 -4

224 lines not on this page · raw message

Signature · 1 line
2.24.0
#6 lore raw

PATCH v8 6/11 RISC-V: Move relocate and few other functions out of __init

In reply to Atish Patra

The secondary hart booting and relocation code are under .init section. As a result, it will be freed once kernel booting is done. However, ordered booting protocol and CPU hotplug always requires these sections to be present to bringup harts after initial kernel boot.

Move the required sections to a different section and make sure that they are in memory within first 2MB offset as trampoline page directory only maps first 2MB.

---
 arch/riscv/kernel/head.S        | 153 +++++++++++++++++---------------
 arch/riscv/kernel/vmlinux.lds.S |   5 +-
 2 files changed, 86 insertions(+), 72 deletions(-)
arch/riscv/kernel/head.S +82 -71
index a4242be66966..c1be597d22a1 100644
@@ -14,7 +14,7 @@
 #include <asm/hwcap.h>
 #include <asm/image.h>
 
-__INIT
+__HEAD
 ENTRY(_start)
 	/*
 	 * Image header expected by Linux boot-loaders. The image header data
@@ -45,8 +45,85 @@ ENTRY(_start)
 	.ascii RISCV_IMAGE_MAGIC2
 	.word 0
 
-.global _start_kernel
-_start_kernel:
+.align 2
+#ifdef CONFIG_MMU
+relocate:
+	/* Relocate return address */
+	li a1, PAGE_OFFSET
+	la a2, _start
+	sub a1, a1, a2
+	add ra, ra, a1
+
+	/* Point stvec to virtual address of intruction after satp write */
+	la a2, 1f
+	add a2, a2, a1
+	csrw CSR_TVEC, a2
+
+	/* Compute satp for kernel page tables, but don't load it yet */
+	srl a2, a0, PAGE_SHIFT
+	li a1, SATP_MODE
+	or a2, a2, a1
+
+	/*
+	 * Load trampoline page directory, which will cause us to trap to
+	 * stvec if VA != PA, or simply fall through if VA == PA.  We need a
+	 * full fence here because setup_vm() just wrote these PTEs and we need
+	 * to ensure the new translations are in use.
+	 */
+	la a0, trampoline_pg_dir
+	srl a0, a0, PAGE_SHIFT
+	or a0, a0, a1
+	sfence.vma
+	csrw CSR_SATP, a0
+.align 2
+1:
+	/* Set trap vector to spin forever to help debug */
+	la a0, .Lsecondary_park
+	csrw CSR_TVEC, a0
+
+	/* Reload the global pointer */
+.option push
+.option norelax
+	la gp, __global_pointer$
+.option pop
+
+	/*
+	 * Switch to kernel page tables.  A full fence is necessary in order to
+	 * avoid using the trampoline translations, which are only correct for
+	 * the first superpage.  Fetching the fence is guarnteed to work
+	 * because that first superpage is translated the same way.
+	 */
+	csrw CSR_SATP, a2
+	sfence.vma
+
+	ret
+#endif /* CONFIG_MMU */
+#ifdef CONFIG_SMP
+	/* Set trap vector to spin forever to help debug */
+	la a3, .Lsecondary_park
+	csrw CSR_TVEC, a3
+
+	slli a3, a0, LGREG
+	.global secondary_start_common
+secondary_start_common:
+
+#ifdef CONFIG_MMU
+	/* Enable virtual memory and relocate to virtual address */
+	la a0, swapper_pg_dir
+	call relocate
+#endif
+	tail smp_callin
+#endif /* CONFIG_SMP */
+
+.Lsecondary_park:
+	/* We lack SMP support or have too many harts, so park this hart */
+	wfi
+	j .Lsecondary_park
+
+END(_start)
+
+	__INIT
+ENTRY(_start_kernel)
 	/* Mask all interrupts */
 	csrw CSR_IE, zero
 	csrw CSR_IP, zero
@@ -125,59 +202,6 @@ clear_bss_done:
 	call parse_dtb
 	tail start_kernel
 
-#ifdef CONFIG_MMU
-relocate:
-	/* Relocate return address */
-	li a1, PAGE_OFFSET
-	la a2, _start
-	sub a1, a1, a2
-	add ra, ra, a1
-
-	/* Point stvec to virtual address of intruction after satp write */
-	la a2, 1f
-	add a2, a2, a1
-	csrw CSR_TVEC, a2
-
-	/* Compute satp for kernel page tables, but don't load it yet */
-	srl a2, a0, PAGE_SHIFT
-	li a1, SATP_MODE
-	or a2, a2, a1
-
-	/*
-	 * Load trampoline page directory, which will cause us to trap to
-	 * stvec if VA != PA, or simply fall through if VA == PA.  We need a
-	 * full fence here because setup_vm() just wrote these PTEs and we need
-	 * to ensure the new translations are in use.
-	 */
-	la a0, trampoline_pg_dir
-	srl a0, a0, PAGE_SHIFT
-	or a0, a0, a1
-	sfence.vma
-	csrw CSR_SATP, a0
-.align 2
-1:
-	/* Set trap vector to spin forever to help debug */
-	la a0, .Lsecondary_park
-	csrw CSR_TVEC, a0
-
-	/* Reload the global pointer */
-.option push
-.option norelax
-	la gp, __global_pointer$
-.option pop
-
-	/*
-	 * Switch to kernel page tables.  A full fence is necessary in order to
-	 * avoid using the trampoline translations, which are only correct for
-	 * the first superpage.  Fetching the fence is guarnteed to work
-	 * because that first superpage is translated the same way.
-	 */
-	csrw CSR_SATP, a2
-	sfence.vma
-
-	ret
-#endif /* CONFIG_MMU */
-
 .Lsecondary_start:
 #ifdef CONFIG_SMP
 	/* Set trap vector to spin forever to help debug */
@@ -202,16 +226,10 @@ relocate:
 	beqz tp, .Lwait_for_cpu_up
 	fence
 
-#ifdef CONFIG_MMU
-	/* Enable virtual memory and relocate to virtual address */
-	la a0, swapper_pg_dir
-	call relocate
+	tail secondary_start_common
 #endif
 
-	tail smp_callin
-#endif
-
-END(_start)
+END(_start_kernel)
 
 #ifdef CONFIG_RISCV_M_MODE
 ENTRY(reset_regs)
@@ -292,13 +310,6 @@ ENTRY(reset_regs)
 END(reset_regs)
 #endif /* CONFIG_RISCV_M_MODE */
 
-.section ".text", "ax",@progbits
-.align 2
-.Lsecondary_park:
-	/* We lack SMP support or have too many harts, so park this hart */
-	wfi
-	j .Lsecondary_park
-
 __PAGE_ALIGNED_BSS
 	/* Empty zero page */
 	.balign PAGE_SIZE
arch/riscv/kernel/vmlinux.lds.S +4 -1
index 12f42f96d46e..18c397953bfc 100644
@@ -10,6 +10,7 @@
 #include <asm/cache.h>
 #include <asm/thread_info.h>
 
+#include <linux/sizes.h>
 OUTPUT_ARCH(riscv)
 ENTRY(_start)
 
@@ -20,8 +21,10 @@ SECTIONS
 	/* Beginning of code and text segment */
 	. = LOAD_OFFSET;
 	_start = .;
-	__init_begin = .;
 	HEAD_TEXT_SECTION
+	. = ALIGN(PAGE_SIZE);
+
+	__init_begin = .;
 	INIT_TEXT_SECTION(PAGE_SIZE)
 	INIT_DATA_SECTION(16)
 	/* we have to discard exit text and such at runtime, not link time */
Signature · 1 line
2.24.0
Reviewed-by on v8 6/11
#7 lore raw

Re: [PATCH v8 06/11] RISC-V: Move relocate and few other functions out of __init

Quoted 4 lines · Atish Patra

The secondary hart booting and relocation code are under .init section. As a result, it will be freed once kernel booting is done. However, ordered booting protocol and CPU hotplug always requires these sections

I think you meant "... require these functions" here.

Quoted 3 lines

to be present to bringup harts after initial kernel boot.

Move the required sections to a different section and make sure that

Same here, I think you meant "... the required functions to a different section ..."

Quoted 228 lines

they are in memory within first 2MB offset as trampoline page directory only maps first 2MB.

---
 arch/riscv/kernel/head.S        | 153 +++++++++++++++++---------------
 arch/riscv/kernel/vmlinux.lds.S |   5 +-
 2 files changed, 86 insertions(+), 72 deletions(-)
arch/riscv/kernel/head.S +82 -71
index a4242be66966..c1be597d22a1 100644
@@ -14,7 +14,7 @@
 #include <asm/hwcap.h>
 #include <asm/image.h>
 
-__INIT
+__HEAD
 ENTRY(_start)
        /*
         * Image header expected by Linux boot-loaders. The image header data
@@ -45,8 +45,85 @@ ENTRY(_start)
        .ascii RISCV_IMAGE_MAGIC2
        .word 0
 
-.global _start_kernel
-_start_kernel:
+.align 2
+#ifdef CONFIG_MMU
+relocate:
+       /* Relocate return address */
+       li a1, PAGE_OFFSET
+       la a2, _start
+       sub a1, a1, a2
+       add ra, ra, a1
+
+       /* Point stvec to virtual address of intruction after satp write */
+       la a2, 1f
+       add a2, a2, a1
+       csrw CSR_TVEC, a2
+
+       /* Compute satp for kernel page tables, but don't load it yet */
+       srl a2, a0, PAGE_SHIFT
+       li a1, SATP_MODE
+       or a2, a2, a1
+
+       /*
+        * Load trampoline page directory, which will cause us to trap to
+        * stvec if VA != PA, or simply fall through if VA == PA.  We need a
+        * full fence here because setup_vm() just wrote these PTEs and we need
+        * to ensure the new translations are in use.
+        */
+       la a0, trampoline_pg_dir
+       srl a0, a0, PAGE_SHIFT
+       or a0, a0, a1
+       sfence.vma
+       csrw CSR_SATP, a0
+.align 2
+1:
+       /* Set trap vector to spin forever to help debug */
+       la a0, .Lsecondary_park
+       csrw CSR_TVEC, a0
+
+       /* Reload the global pointer */
+.option push
+.option norelax
+       la gp, __global_pointer$
+.option pop
+
+       /*
+        * Switch to kernel page tables.  A full fence is necessary in order to
+        * avoid using the trampoline translations, which are only correct for
+        * the first superpage.  Fetching the fence is guarnteed to work
+        * because that first superpage is translated the same way.
+        */
+       csrw CSR_SATP, a2
+       sfence.vma
+
+       ret
+#endif /* CONFIG_MMU */
+#ifdef CONFIG_SMP
+       /* Set trap vector to spin forever to help debug */
+       la a3, .Lsecondary_park
+       csrw CSR_TVEC, a3
+
+       slli a3, a0, LGREG
+       .global secondary_start_common
+secondary_start_common:
+
+#ifdef CONFIG_MMU
+       /* Enable virtual memory and relocate to virtual address */
+       la a0, swapper_pg_dir
+       call relocate
+#endif
+       tail smp_callin
+#endif /* CONFIG_SMP */
+
+.Lsecondary_park:
+       /* We lack SMP support or have too many harts, so park this hart */
+       wfi
+       j .Lsecondary_park
+
+END(_start)
+
+       __INIT
+ENTRY(_start_kernel)
        /* Mask all interrupts */
        csrw CSR_IE, zero
        csrw CSR_IP, zero
@@ -125,59 +202,6 @@ clear_bss_done:
        call parse_dtb
        tail start_kernel
 
-#ifdef CONFIG_MMU
-relocate:
-       /* Relocate return address */
-       li a1, PAGE_OFFSET
-       la a2, _start
-       sub a1, a1, a2
-       add ra, ra, a1
-
-       /* Point stvec to virtual address of intruction after satp write */
-       la a2, 1f
-       add a2, a2, a1
-       csrw CSR_TVEC, a2
-
-       /* Compute satp for kernel page tables, but don't load it yet */
-       srl a2, a0, PAGE_SHIFT
-       li a1, SATP_MODE
-       or a2, a2, a1
-
-       /*
-        * Load trampoline page directory, which will cause us to trap to
-        * stvec if VA != PA, or simply fall through if VA == PA.  We need a
-        * full fence here because setup_vm() just wrote these PTEs and we need
-        * to ensure the new translations are in use.
-        */
-       la a0, trampoline_pg_dir
-       srl a0, a0, PAGE_SHIFT
-       or a0, a0, a1
-       sfence.vma
-       csrw CSR_SATP, a0
-.align 2
-1:
-       /* Set trap vector to spin forever to help debug */
-       la a0, .Lsecondary_park
-       csrw CSR_TVEC, a0
-
-       /* Reload the global pointer */
-.option push
-.option norelax
-       la gp, __global_pointer$
-.option pop
-
-       /*
-        * Switch to kernel page tables.  A full fence is necessary in order to
-        * avoid using the trampoline translations, which are only correct for
-        * the first superpage.  Fetching the fence is guarnteed to work
-        * because that first superpage is translated the same way.
-        */
-       csrw CSR_SATP, a2
-       sfence.vma
-
-       ret
-#endif /* CONFIG_MMU */
-
 .Lsecondary_start:
 #ifdef CONFIG_SMP
        /* Set trap vector to spin forever to help debug */
@@ -202,16 +226,10 @@ relocate:
        beqz tp, .Lwait_for_cpu_up
        fence
 
-#ifdef CONFIG_MMU
-       /* Enable virtual memory and relocate to virtual address */
-       la a0, swapper_pg_dir
-       call relocate
+       tail secondary_start_common
 #endif
 
-       tail smp_callin
-#endif
-
-END(_start)
+END(_start_kernel)
 
 #ifdef CONFIG_RISCV_M_MODE
 ENTRY(reset_regs)
@@ -292,13 +310,6 @@ ENTRY(reset_regs)
 END(reset_regs)
 #endif /* CONFIG_RISCV_M_MODE */
 
-.section ".text", "ax",@progbits
-.align 2
-.Lsecondary_park:
-       /* We lack SMP support or have too many harts, so park this hart */
-       wfi
-       j .Lsecondary_park
-
 __PAGE_ALIGNED_BSS
        /* Empty zero page */
        .balign PAGE_SIZE
arch/riscv/kernel/vmlinux.lds.S +4 -2
index 12f42f96d46e..18c397953bfc 100644
@@ -10,6 +10,7 @@
 #include <asm/cache.h>
 #include <asm/thread_info.h>
 
+#include <linux/sizes.h>
 OUTPUT_ARCH(riscv)
 ENTRY(_start)
 
@@ -20,8 +21,10 @@ SECTIONS
        /* Beginning of code and text segment */
        . = LOAD_OFFSET;
        _start = .;
-       __init_begin = .;
        HEAD_TEXT_SECTION
+       . = ALIGN(PAGE_SIZE);
+
+       __init_begin = .;
        INIT_TEXT_SECTION(PAGE_SIZE)
        INIT_DATA_SECTION(16)
        /* we have to discard exit text and such at runtime, not link time */
--
Quoted 2 lines

2.24.0

Apart from above nit in commit description, looks good to me.

Regards,
Anup

#8 lore raw

Re: [PATCH v8 06/11] RISC-V: Move relocate and few other functions out of __init

Quoted 7 lines · Anup Patel
Quoted 4 lines · Atish Patra

The secondary hart booting and relocation code are under .init section. As a result, it will be freed once kernel booting is done. However, ordered booting protocol and CPU hotplug always requires these sections

Quoted 9 lines

I think you meant "... require these functions" here.

Quoted 3 lines

to be present to bringup harts after initial kernel boot.

Move the required sections to a different section and make sure that

Same here, I think you meant "... the required functions to a different section ..."

My bad. Thanks for pointing out. Fixed.

Quoted 238 lines
Quoted 228 lines

they are in memory within first 2MB offset as trampoline page directory only maps first 2MB.

---
 arch/riscv/kernel/head.S        | 153 +++++++++++++++++---------------
 arch/riscv/kernel/vmlinux.lds.S |   5 +-
 2 files changed, 86 insertions(+), 72 deletions(-)
arch/riscv/kernel/head.S +82 -71
index a4242be66966..c1be597d22a1 100644
@@ -14,7 +14,7 @@
 #include <asm/hwcap.h>
 #include <asm/image.h>
 
-__INIT
+__HEAD
 ENTRY(_start)
        /*
         * Image header expected by Linux boot-loaders. The image header data
@@ -45,8 +45,85 @@ ENTRY(_start)
        .ascii RISCV_IMAGE_MAGIC2
        .word 0
 
-.global _start_kernel
-_start_kernel:
+.align 2
+#ifdef CONFIG_MMU
+relocate:
+       /* Relocate return address */
+       li a1, PAGE_OFFSET
+       la a2, _start
+       sub a1, a1, a2
+       add ra, ra, a1
+
+       /* Point stvec to virtual address of intruction after satp write */
+       la a2, 1f
+       add a2, a2, a1
+       csrw CSR_TVEC, a2
+
+       /* Compute satp for kernel page tables, but don't load it yet */
+       srl a2, a0, PAGE_SHIFT
+       li a1, SATP_MODE
+       or a2, a2, a1
+
+       /*
+        * Load trampoline page directory, which will cause us to trap to
+        * stvec if VA != PA, or simply fall through if VA == PA.  We need a
+        * full fence here because setup_vm() just wrote these PTEs and we need
+        * to ensure the new translations are in use.
+        */
+       la a0, trampoline_pg_dir
+       srl a0, a0, PAGE_SHIFT
+       or a0, a0, a1
+       sfence.vma
+       csrw CSR_SATP, a0
+.align 2
+1:
+       /* Set trap vector to spin forever to help debug */
+       la a0, .Lsecondary_park
+       csrw CSR_TVEC, a0
+
+       /* Reload the global pointer */
+.option push
+.option norelax
+       la gp, __global_pointer$
+.option pop
+
+       /*
+        * Switch to kernel page tables.  A full fence is necessary in order to
+        * avoid using the trampoline translations, which are only correct for
+        * the first superpage.  Fetching the fence is guarnteed to work
+        * because that first superpage is translated the same way.
+        */
+       csrw CSR_SATP, a2
+       sfence.vma
+
+       ret
+#endif /* CONFIG_MMU */
+#ifdef CONFIG_SMP
+       /* Set trap vector to spin forever to help debug */
+       la a3, .Lsecondary_park
+       csrw CSR_TVEC, a3
+
+       slli a3, a0, LGREG
+       .global secondary_start_common
+secondary_start_common:
+
+#ifdef CONFIG_MMU
+       /* Enable virtual memory and relocate to virtual address */
+       la a0, swapper_pg_dir
+       call relocate
+#endif
+       tail smp_callin
+#endif /* CONFIG_SMP */
+
+.Lsecondary_park:
+       /* We lack SMP support or have too many harts, so park this hart */
+       wfi
+       j .Lsecondary_park
+
+END(_start)
+
+       __INIT
+ENTRY(_start_kernel)
        /* Mask all interrupts */
        csrw CSR_IE, zero
        csrw CSR_IP, zero
@@ -125,59 +202,6 @@ clear_bss_done:
        call parse_dtb
        tail start_kernel
 
-#ifdef CONFIG_MMU
-relocate:
-       /* Relocate return address */
-       li a1, PAGE_OFFSET
-       la a2, _start
-       sub a1, a1, a2
-       add ra, ra, a1
-
-       /* Point stvec to virtual address of intruction after satp write */
-       la a2, 1f
-       add a2, a2, a1
-       csrw CSR_TVEC, a2
-
-       /* Compute satp for kernel page tables, but don't load it yet */
-       srl a2, a0, PAGE_SHIFT
-       li a1, SATP_MODE
-       or a2, a2, a1
-
-       /*
-        * Load trampoline page directory, which will cause us to trap to
-        * stvec if VA != PA, or simply fall through if VA == PA.  We need a
-        * full fence here because setup_vm() just wrote these PTEs and we need
-        * to ensure the new translations are in use.
-        */
-       la a0, trampoline_pg_dir
-       srl a0, a0, PAGE_SHIFT
-       or a0, a0, a1
-       sfence.vma
-       csrw CSR_SATP, a0
-.align 2
-1:
-       /* Set trap vector to spin forever to help debug */
-       la a0, .Lsecondary_park
-       csrw CSR_TVEC, a0
-
-       /* Reload the global pointer */
-.option push
-.option norelax
-       la gp, __global_pointer$
-.option pop
-
-       /*
-        * Switch to kernel page tables.  A full fence is necessary in order to
-        * avoid using the trampoline translations, which are only correct for
-        * the first superpage.  Fetching the fence is guarnteed to work
-        * because that first superpage is translated the same way.
-        */
-       csrw CSR_SATP, a2
-       sfence.vma
-
-       ret
-#endif /* CONFIG_MMU */
-
 .Lsecondary_start:
 #ifdef CONFIG_SMP
        /* Set trap vector to spin forever to help debug */
@@ -202,16 +226,10 @@ relocate:
        beqz tp, .Lwait_for_cpu_up
        fence
 
-#ifdef CONFIG_MMU
-       /* Enable virtual memory and relocate to virtual address */
-       la a0, swapper_pg_dir
-       call relocate
+       tail secondary_start_common
 #endif
 
-       tail smp_callin
-#endif
-
-END(_start)
+END(_start_kernel)
 
 #ifdef CONFIG_RISCV_M_MODE
 ENTRY(reset_regs)
@@ -292,13 +310,6 @@ ENTRY(reset_regs)
 END(reset_regs)
 #endif /* CONFIG_RISCV_M_MODE */
 
-.section ".text", "ax",@progbits
-.align 2
-.Lsecondary_park:
-       /* We lack SMP support or have too many harts, so park this hart */
-       wfi
-       j .Lsecondary_park
-
 __PAGE_ALIGNED_BSS
        /* Empty zero page */
        .balign PAGE_SIZE
arch/riscv/kernel/vmlinux.lds.S +4 -2
index 12f42f96d46e..18c397953bfc 100644
@@ -10,6 +10,7 @@
 #include <asm/cache.h>
 #include <asm/thread_info.h>
 
+#include <linux/sizes.h>
 OUTPUT_ARCH(riscv)
 ENTRY(_start)
 
@@ -20,8 +21,10 @@ SECTIONS
        /* Beginning of code and text segment */
        . = LOAD_OFFSET;
        _start = .;
-       __init_begin = .;
        HEAD_TEXT_SECTION
+       . = ALIGN(PAGE_SIZE);
+
+       __init_begin = .;
        INIT_TEXT_SECTION(PAGE_SIZE)
        INIT_DATA_SECTION(16)
        /* we have to discard exit text and such at runtime, not link time */
--
Quoted 2 lines

2.24.0

Apart from above nit in commit description, looks good to me.

Regards,
Anup

Signature · 2 lines
Regards,
Atish
#9 lore raw

PATCH v8 7/11 RISC-V: Add cpu_ops and modify default booting method

In reply to Atish Patra

Currently, all non-booting harts start booting after the booting hart updates the per-hart stack pointer. This is done in a way that, it's difficult to implement any other booting method without breaking the backward compatibility.

Define a cpu_ops method that allows to introduce other booting methods in future. Modify the current booting method to be compatible with cpu_ops.

---
 arch/riscv/include/asm/cpu_ops.h     | 34 ++++++++++++++++++
 arch/riscv/kernel/Makefile           |  2 ++
 arch/riscv/kernel/cpu_ops.c          | 40 +++++++++++++++++++++
 arch/riscv/kernel/cpu_ops_spinwait.c | 42 ++++++++++++++++++++++
 arch/riscv/kernel/smpboot.c          | 54 +++++++++++++++++-----------
 5 files changed, 151 insertions(+), 21 deletions(-)
 create mode 100644 arch/riscv/include/asm/cpu_ops.h
 create mode 100644 arch/riscv/kernel/cpu_ops.c
 create mode 100644 arch/riscv/kernel/cpu_ops_spinwait.c
arch/riscv/include/asm/cpu_ops.h new file +34 -0
new file mode 100644
index 000000000000..7db276284009
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ * Based on arch/arm64/include/asm/cpu_ops.h
+ */
+#ifndef __ASM_CPU_OPS_H
+#define __ASM_CPU_OPS_H
+
+#include <linux/init.h>
+#include <linux/sched.h>
+#include <linux/threads.h>
+
+/**
+ * struct cpu_operations - Callback operations for hotplugging CPUs.
+ *
+ * @name:		Name of the boot protocol.
+ * @cpu_prepare:	Early one-time preparation step for a cpu. If there
+ *			is a mechanism for doing so, tests whether it is
+ *			possible to boot the given HART.
+ * @cpu_start:		Boots a cpu into the kernel.
+ */
+struct cpu_operations {
+	const char	*name;
+	int		(*cpu_prepare)(unsigned int cpu);
+	int		(*cpu_start)(unsigned int cpu,
+				     struct task_struct *tidle);
+};
+
+extern const struct cpu_operations *cpu_ops[NR_CPUS];
+int __init cpu_set_ops(int cpu);
+void cpu_update_secondary_bootdata(unsigned int cpuid,
+				   struct task_struct *tidle);
+
+#endif /* ifndef __ASM_CPU_OPS_H */
arch/riscv/kernel/Makefile +2 -0
index f40205cb9a22..f81a6ff88005 100644
@@ -32,6 +32,8 @@ obj-$(CONFIG_RISCV_M_MODE)	+= clint.o
 obj-$(CONFIG_FPU)		+= fpu.o
 obj-$(CONFIG_SMP)		+= smpboot.o
 obj-$(CONFIG_SMP)		+= smp.o
+obj-$(CONFIG_SMP)		+= cpu_ops.o
+obj-$(CONFIG_SMP)		+= cpu_ops_spinwait.o
 obj-$(CONFIG_MODULES)		+= module.o
 obj-$(CONFIG_MODULE_SECTIONS)	+= module-sections.o
 
arch/riscv/kernel/cpu_ops.c new file +40 -0
new file mode 100644
index 000000000000..1085def3735a
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/errno.h>
+#include <linux/mm.h>
+#include <linux/of.h>
+#include <linux/string.h>
+#include <linux/sched.h>
+#include <linux/sched/task_stack.h>
+#include <asm/cpu_ops.h>
+#include <asm/sbi.h>
+#include <asm/smp.h>
+
+const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init;
+
+void *__cpu_up_stack_pointer[NR_CPUS];
+void *__cpu_up_task_pointer[NR_CPUS];
+
+extern const struct cpu_operations cpu_ops_spinwait;
+
+void cpu_update_secondary_bootdata(unsigned int cpuid,
+				   struct task_struct *tidle)
+{
+	int hartid = cpuid_to_hartid_map(cpuid);
+
+	/* Make sure tidle is updated */
+	smp_mb();
+	WRITE_ONCE(__cpu_up_stack_pointer[hartid],
+		  task_stack_page(tidle) + THREAD_SIZE);
+	WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
+}
+
+int __init cpu_set_ops(int cpuid)
+{
+	cpu_ops[cpuid] = &cpu_ops_spinwait;
+
+	return 0;
+}
arch/riscv/kernel/cpu_ops_spinwait.c new file +42 -0

42 lines not on this page · raw message

arch/riscv/kernel/smpboot.c +33 -21

101 lines not on this page · raw message

Signature · 1 line
2.24.0
Reviewed-by on v8 7/11
#10 lore raw

Re: [PATCH v8 07/11] RISC-V: Add cpu_ops and modify default booting method

Quoted 48 lines · Atish Patra

Currently, all non-booting harts start booting after the booting hart updates the per-hart stack pointer. This is done in a way that, it's difficult to implement any other booting method without breaking the backward compatibility.

Define a cpu_ops method that allows to introduce other booting methods in future. Modify the current booting method to be compatible with cpu_ops.

---
 arch/riscv/include/asm/cpu_ops.h     | 34 ++++++++++++++++++
 arch/riscv/kernel/Makefile           |  2 ++
 arch/riscv/kernel/cpu_ops.c          | 40 +++++++++++++++++++++
 arch/riscv/kernel/cpu_ops_spinwait.c | 42 ++++++++++++++++++++++
 arch/riscv/kernel/smpboot.c          | 54 +++++++++++++++++-----------
 5 files changed, 151 insertions(+), 21 deletions(-)
 create mode 100644 arch/riscv/include/asm/cpu_ops.h
 create mode 100644 arch/riscv/kernel/cpu_ops.c
 create mode 100644 arch/riscv/kernel/cpu_ops_spinwait.c
arch/riscv/include/asm/cpu_ops.h new file +20 -0
new file mode 100644
index 000000000000..7db276284009
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ * Based on arch/arm64/include/asm/cpu_ops.h
+ */
+#ifndef __ASM_CPU_OPS_H
+#define __ASM_CPU_OPS_H
+
+#include <linux/init.h>
+#include <linux/sched.h>
+#include <linux/threads.h>
+
+/**
+ * struct cpu_operations - Callback operations for hotplugging CPUs.
+ *
+ * @name:              Name of the boot protocol.
+ * @cpu_prepare:       Early one-time preparation step for a cpu. If there
+ *                     is a mechanism for doing so, tests whether it is
+ *                     possible to boot the given HART.
+ * @cpu_start:         Boots a cpu into the kernel.

arch/riscv/include/asm/cpu_ops.h line 30

Quoted 10 lines
arch/riscv/include/asm/cpu_ops.h new file +10 -0
+ */
+struct cpu_operations {
+       const char      *name;
+       int             (*cpu_prepare)(unsigned int cpu);
+       int             (*cpu_start)(unsigned int cpu,
+                                    struct task_struct *tidle);
+};
+
+extern const struct cpu_operations *cpu_ops[NR_CPUS];
+int __init cpu_set_ops(int cpu);

This function is more like probing for appropriate cpu_ops. Also, I think we don't need to return anything from cpu_set_ops().

Maybe rename it to "void cpu_probe_ops(int cpu)" ?

Quoted 48 lines
arch/riscv/include/asm/cpu_ops.h new file +4 -0
+void cpu_update_secondary_bootdata(unsigned int cpuid,
+                                  struct task_struct *tidle);
+
+#endif /* ifndef __ASM_CPU_OPS_H */
arch/riscv/kernel/Makefile +2 -0
index f40205cb9a22..f81a6ff88005 100644
@@ -32,6 +32,8 @@ obj-$(CONFIG_RISCV_M_MODE)    += clint.o
 obj-$(CONFIG_FPU)              += fpu.o
 obj-$(CONFIG_SMP)              += smpboot.o
 obj-$(CONFIG_SMP)              += smp.o
+obj-$(CONFIG_SMP)              += cpu_ops.o
+obj-$(CONFIG_SMP)              += cpu_ops_spinwait.o
 obj-$(CONFIG_MODULES)          += module.o
 obj-$(CONFIG_MODULE_SECTIONS)  += module-sections.o
 
arch/riscv/kernel/cpu_ops.c new file +25 -0
new file mode 100644
index 000000000000..1085def3735a
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/errno.h>
+#include <linux/mm.h>
+#include <linux/of.h>
+#include <linux/string.h>
+#include <linux/sched.h>
+#include <linux/sched/task_stack.h>
+#include <asm/cpu_ops.h>
+#include <asm/sbi.h>
+#include <asm/smp.h>
+
+const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init;
+
+void *__cpu_up_stack_pointer[NR_CPUS];
+void *__cpu_up_task_pointer[NR_CPUS];
+
+extern const struct cpu_operations cpu_ops_spinwait;
+
+void cpu_update_secondary_bootdata(unsigned int cpuid,
+                                  struct task_struct *tidle)
+{

arch/riscv/kernel/cpu_ops.c line 35

Quoted 10 lines
arch/riscv/kernel/cpu_ops.c new file +10 -0
+       int hartid = cpuid_to_hartid_map(cpuid);
+
+       /* Make sure tidle is updated */
+       smp_mb();
+       WRITE_ONCE(__cpu_up_stack_pointer[hartid],
+                 task_stack_page(tidle) + THREAD_SIZE);
+       WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
+}
+
+int __init cpu_set_ops(int cpuid)

Maybe rename it to "void cpu_probe_ops(int cpu)" ?

Quoted 109 lines
arch/riscv/kernel/cpu_ops.c new file +5 -0
+{
+       cpu_ops[cpuid] = &cpu_ops_spinwait;
+
+       return 0;
+}
arch/riscv/kernel/cpu_ops_spinwait.c new file +42 -0
new file mode 100644
index 000000000000..f828e660294e
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/errno.h>
+#include <linux/of.h>
+#include <linux/string.h>
+#include <asm/cpu_ops.h>
+#include <asm/sbi.h>
+#include <asm/smp.h>
+
+const struct cpu_operations cpu_ops_spinwait;
+
+static int spinwait_cpu_prepare(unsigned int cpuid)
+{
+       if (!cpu_ops_spinwait.cpu_start) {
+               pr_err("cpu start method not defined for CPU [%d]\n", cpuid);
+               return -ENODEV;
+       }
+       return 0;
+}
+
+static int spinwait_cpu_start(unsigned int cpuid, struct task_struct *tidle)
+{
+       /*
+        * In this protocol, all cpus boot on their own accord.  _start
+        * selects the first cpu to boot the kernel and causes the remainder
+        * of the cpus to spin in a loop waiting for their stack pointer to be
+        * setup by that main cpu.  Writing to bootdata (i.e __cpu_up_stack_pointer) signals to
+        * the spinning cpus that they can continue the boot process.
+        */
+       cpu_update_secondary_bootdata(cpuid, tidle);
+
+       return 0;
+}
+
+const struct cpu_operations cpu_ops_spinwait = {
+       .name           = "spinwait",
+       .cpu_prepare    = spinwait_cpu_prepare,
+       .cpu_start      = spinwait_cpu_start,
+};
arch/riscv/kernel/smpboot.c +10 -3
index 8bc01f0ca73b..2ee41c779a16 100644
@@ -25,6 +25,7 @@
 #include <linux/sched/task_stack.h>
 #include <linux/sched/mm.h>
 #include <asm/clint.h>
+#include <asm/cpu_ops.h>
 #include <asm/irq.h>
 #include <asm/mmu_context.h>
 #include <asm/tlbflush.h>
@@ -34,8 +35,6 @@
 
 #include "head.h"
 
-void *__cpu_up_stack_pointer[NR_CPUS];
-void *__cpu_up_task_pointer[NR_CPUS];
 static DECLARE_COMPLETION(cpu_running);
 
 void __init smp_prepare_boot_cpu(void)
@@ -46,6 +45,7 @@ void __init smp_prepare_boot_cpu(void)
 void __init smp_prepare_cpus(unsigned int max_cpus)
 {
        int cpuid;
+       int ret;
 
        /* This covers non-smp usecase mandated by "nosmp" option */
        if (max_cpus == 0)
@@ -54,6 +54,11 @@ void __init smp_prepare_cpus(unsigned int max_cpus)
        for_each_possible_cpu(cpuid) {
                if (cpuid == smp_processor_id())
                        continue;
+               if (cpu_ops[cpuid]->cpu_prepare) {
+                       ret = cpu_ops[cpuid]->cpu_prepare(cpuid);
+                       if (ret)
+                               continue;
+               }
                set_cpu_present(cpuid, true);
        }
 }
@@ -65,6 +70,8 @@ void __init setup_smp(void)
        bool found_boot_cpu = false;
        int cpuid = 1;
 
+       cpu_set_ops(0);
+
        for_each_of_cpu_node(dn) {
                hart = riscv_of_processor_hartid(dn);
                if (hart < 0)
@@ -92,36 +99,41 @@ void __init setup_smp(void)
                        cpuid, nr_cpu_ids);
 
        for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) {
-               if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID)
+               if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) {

arch/riscv/kernel/smpboot.c void __init setup_smp(void) · line 112

Quoted 10 lines
arch/riscv/kernel/smpboot.c +6 -0
+                       if (cpu_set_ops(cpuid)) {
+                               cpuid_to_hartid_map(cpuid) = INVALID_HARTID;
+                               continue;
+                       }
                        set_cpu_possible(cpuid, true);
+               }
        }
 }
 
+int start_secondary_cpu(int cpu, struct task_struct *tidle)

Make this function static.

Quoted 46 lines
arch/riscv/kernel/smpboot.c +17 -19
+{
+       if (cpu_ops[cpu]->cpu_start)
+               return cpu_ops[cpu]->cpu_start(cpu, tidle);
+
+       return -EOPNOTSUPP;
+}
+
 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
 {
        int ret = 0;
-       int hartid = cpuid_to_hartid_map(cpu);
        tidle->thread_info.cpu = cpu;
 
-       /*
-        * On RISC-V systems, all harts boot on their own accord.  Our _start
-        * selects the first hart to boot the kernel and causes the remainder
-        * of the harts to spin in a loop waiting for their stack pointer to be
-        * setup by that main hart.  Writing __cpu_up_stack_pointer signals to
-        * the spinning harts that they can continue the boot process.
-        */
-       smp_mb();
-       WRITE_ONCE(__cpu_up_stack_pointer[hartid],
-                 task_stack_page(tidle) + THREAD_SIZE);
-       WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
-
-       lockdep_assert_held(&cpu_running);
-       wait_for_completion_timeout(&cpu_running,
+       ret = start_secondary_cpu(cpu, tidle);
+       if (!ret) {
+               lockdep_assert_held(&cpu_running);
+               wait_for_completion_timeout(&cpu_running,
                                            msecs_to_jiffies(1000));
 
-       if (!cpu_online(cpu)) {
-               pr_crit("CPU%u: failed to come online\n", cpu);
-               ret = -EIO;
+               if (!cpu_online(cpu)) {
+                       pr_crit("CPU%u: failed to come online\n", cpu);
+                       ret = -EIO;
+               }
+       } else {
+               pr_crit("CPU%u: failed to start\n", cpu);
        }
 
        return ret;
--
Quoted 2 lines

2.24.0

Apart from minor comments above, looks good to me.

Regards,
Anup

#11 lore raw

Re: [PATCH v8 07/11] RISC-V: Add cpu_ops and modify default booting method

Quoted 55 lines · Anup Patel
Quoted 53 lines · Atish Patra

Currently, all non-booting harts start booting after the booting hart updates the per-hart stack pointer. This is done in a way that, it's difficult to implement any other booting method without breaking the backward compatibility.

Define a cpu_ops method that allows to introduce other booting methods in future. Modify the current booting method to be compatible with cpu_ops.

---
 arch/riscv/include/asm/cpu_ops.h     | 34 ++++++++++++++++++
 arch/riscv/kernel/Makefile           |  2 ++
 arch/riscv/kernel/cpu_ops.c          | 40 +++++++++++++++++++++
 arch/riscv/kernel/cpu_ops_spinwait.c | 42 ++++++++++++++++++++++
 arch/riscv/kernel/smpboot.c          | 54 +++++++++++++++++-----------
 5 files changed, 151 insertions(+), 21 deletions(-)
 create mode 100644 arch/riscv/include/asm/cpu_ops.h
 create mode 100644 arch/riscv/kernel/cpu_ops.c
 create mode 100644 arch/riscv/kernel/cpu_ops_spinwait.c
arch/riscv/include/asm/cpu_ops.h new file +25 -0
new file mode 100644
index 000000000000..7db276284009
@@ -0,0 +1,34 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * Copyright (c) 2019 Western Digital Corporation or its affiliates.
+ * Based on arch/arm64/include/asm/cpu_ops.h
+ */
+#ifndef __ASM_CPU_OPS_H
+#define __ASM_CPU_OPS_H
+
+#include <linux/init.h>
+#include <linux/sched.h>
+#include <linux/threads.h>
+
+/**
+ * struct cpu_operations - Callback operations for hotplugging CPUs.
+ *
+ * @name:              Name of the boot protocol.
+ * @cpu_prepare:       Early one-time preparation step for a cpu. If there
+ *                     is a mechanism for doing so, tests whether it is
+ *                     possible to boot the given HART.
+ * @cpu_start:         Boots a cpu into the kernel.
+ */
+struct cpu_operations {
+       const char      *name;
+       int             (*cpu_prepare)(unsigned int cpu);
+       int             (*cpu_start)(unsigned int cpu,
Quoted 9 lines

arch/riscv/include/asm/cpu_ops.h line 30

Quoted 5 lines
arch/riscv/include/asm/cpu_ops.h new file +5 -0
+                                    struct task_struct *tidle);
+};
+
+extern const struct cpu_operations *cpu_ops[NR_CPUS];
+int __init cpu_set_ops(int cpu);

This function is more like probing for appropriate cpu_ops. Also, I think we don't need to return anything from cpu_set_ops().

Correct. I will change it to void but I think set_ops is a better name as we are actually
setting the right type of ops to cpu_ops.

Quoted 181 lines

Maybe rename it to "void cpu_probe_ops(int cpu)" ?

Quoted 48 lines
arch/riscv/include/asm/cpu_ops.h new file +4 -0
+void cpu_update_secondary_bootdata(unsigned int cpuid,
+                                  struct task_struct *tidle);
+
+#endif /* ifndef __ASM_CPU_OPS_H */
arch/riscv/kernel/Makefile +2 -0
index f40205cb9a22..f81a6ff88005 100644
@@ -32,6 +32,8 @@ obj-$(CONFIG_RISCV_M_MODE)    += clint.o
 obj-$(CONFIG_FPU)              += fpu.o
 obj-$(CONFIG_SMP)              += smpboot.o
 obj-$(CONFIG_SMP)              += smp.o
+obj-$(CONFIG_SMP)              += cpu_ops.o
+obj-$(CONFIG_SMP)              += cpu_ops_spinwait.o
 obj-$(CONFIG_MODULES)          += module.o
 obj-$(CONFIG_MODULE_SECTIONS)  += module-sections.o
 
arch/riscv/kernel/cpu_ops.c new file +25 -0
new file mode 100644
index 000000000000..1085def3735a
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/errno.h>
+#include <linux/mm.h>
+#include <linux/of.h>
+#include <linux/string.h>
+#include <linux/sched.h>
+#include <linux/sched/task_stack.h>
+#include <asm/cpu_ops.h>
+#include <asm/sbi.h>
+#include <asm/smp.h>
+
+const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init;
+
+void *__cpu_up_stack_pointer[NR_CPUS];
+void *__cpu_up_task_pointer[NR_CPUS];
+
+extern const struct cpu_operations cpu_ops_spinwait;
+
+void cpu_update_secondary_bootdata(unsigned int cpuid,
+                                  struct task_struct *tidle)
+{

arch/riscv/kernel/cpu_ops.c line 35

Quoted 10 lines
arch/riscv/kernel/cpu_ops.c new file +10 -0
+       int hartid = cpuid_to_hartid_map(cpuid);
+
+       /* Make sure tidle is updated */
+       smp_mb();
+       WRITE_ONCE(__cpu_up_stack_pointer[hartid],
+                 task_stack_page(tidle) + THREAD_SIZE);
+       WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
+}
+
+int __init cpu_set_ops(int cpuid)

Maybe rename it to "void cpu_probe_ops(int cpu)" ?

Quoted 118 lines
arch/riscv/kernel/cpu_ops.c new file +5 -0
+{
+       cpu_ops[cpuid] = &cpu_ops_spinwait;
+
+       return 0;
+}
arch/riscv/kernel/cpu_ops_spinwait.c new file +42 -0
new file mode 100644
index 000000000000..f828e660294e
@@ -0,0 +1,42 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/errno.h>
+#include <linux/of.h>
+#include <linux/string.h>
+#include <asm/cpu_ops.h>
+#include <asm/sbi.h>
+#include <asm/smp.h>
+
+const struct cpu_operations cpu_ops_spinwait;
+
+static int spinwait_cpu_prepare(unsigned int cpuid)
+{
+       if (!cpu_ops_spinwait.cpu_start) {
+               pr_err("cpu start method not defined for CPU [%d]\n", cpuid);
+               return -ENODEV;
+       }
+       return 0;
+}
+
+static int spinwait_cpu_start(unsigned int cpuid, struct task_struct *tidle)
+{
+       /*
+        * In this protocol, all cpus boot on their own accord.  _start
+        * selects the first cpu to boot the kernel and causes the remainder
+        * of the cpus to spin in a loop waiting for their stack pointer to be
+        * setup by that main cpu.  Writing to bootdata (i.e __cpu_up_stack_pointer) signals to
+        * the spinning cpus that they can continue the boot process.
+        */
+       cpu_update_secondary_bootdata(cpuid, tidle);
+
+       return 0;
+}
+
+const struct cpu_operations cpu_ops_spinwait = {
+       .name           = "spinwait",
+       .cpu_prepare    = spinwait_cpu_prepare,
+       .cpu_start      = spinwait_cpu_start,
+};
arch/riscv/kernel/smpboot.c +15 -3
index 8bc01f0ca73b..2ee41c779a16 100644
@@ -25,6 +25,7 @@
 #include <linux/sched/task_stack.h>
 #include <linux/sched/mm.h>
 #include <asm/clint.h>
+#include <asm/cpu_ops.h>
 #include <asm/irq.h>
 #include <asm/mmu_context.h>
 #include <asm/tlbflush.h>
@@ -34,8 +35,6 @@
 
 #include "head.h"
 
-void *__cpu_up_stack_pointer[NR_CPUS];
-void *__cpu_up_task_pointer[NR_CPUS];
 static DECLARE_COMPLETION(cpu_running);
 
 void __init smp_prepare_boot_cpu(void)
@@ -46,6 +45,7 @@ void __init smp_prepare_boot_cpu(void)
 void __init smp_prepare_cpus(unsigned int max_cpus)
 {
        int cpuid;
+       int ret;
 
        /* This covers non-smp usecase mandated by "nosmp" option */
        if (max_cpus == 0)
@@ -54,6 +54,11 @@ void __init smp_prepare_cpus(unsigned int max_cpus)
        for_each_possible_cpu(cpuid) {
                if (cpuid == smp_processor_id())
                        continue;
+               if (cpu_ops[cpuid]->cpu_prepare) {
+                       ret = cpu_ops[cpuid]->cpu_prepare(cpuid);
+                       if (ret)
+                               continue;
+               }
                set_cpu_present(cpuid, true);
        }
 }
@@ -65,6 +70,8 @@ void __init setup_smp(void)
        bool found_boot_cpu = false;
        int cpuid = 1;
 
+       cpu_set_ops(0);
+
        for_each_of_cpu_node(dn) {
                hart = riscv_of_processor_hartid(dn);
                if (hart < 0)
@@ -92,36 +99,41 @@ void __init setup_smp(void)
                        cpuid, nr_cpu_ids);
 
        for (cpuid = 1; cpuid < nr_cpu_ids; cpuid++) {
-               if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID)
+               if (cpuid_to_hartid_map(cpuid) != INVALID_HARTID) {
+                       if (cpu_set_ops(cpuid)) {
+                               cpuid_to_hartid_map(cpuid) = INVALID_HARTID;
+                               continue;
+                       }
                        set_cpu_possible(cpuid, true);
+               }
        }
 }
 
Quoted 4 lines

arch/riscv/kernel/smpboot.c void __init setup_smp(void) · line 112

Quoted 1 line
arch/riscv/kernel/smpboot.c +1 -0
+int start_secondary_cpu(int cpu, struct task_struct *tidle)

Make this function static.

sure.

Quoted 56 lines
Quoted 46 lines
arch/riscv/kernel/smpboot.c +17 -19
+{
+       if (cpu_ops[cpu]->cpu_start)
+               return cpu_ops[cpu]->cpu_start(cpu, tidle);
+
+       return -EOPNOTSUPP;
+}
+
 int __cpu_up(unsigned int cpu, struct task_struct *tidle)
 {
        int ret = 0;
-       int hartid = cpuid_to_hartid_map(cpu);
        tidle->thread_info.cpu = cpu;
 
-       /*
-        * On RISC-V systems, all harts boot on their own accord.  Our _start
-        * selects the first hart to boot the kernel and causes the remainder
-        * of the harts to spin in a loop waiting for their stack pointer to be
-        * setup by that main hart.  Writing __cpu_up_stack_pointer signals to
-        * the spinning harts that they can continue the boot process.
-        */
-       smp_mb();
-       WRITE_ONCE(__cpu_up_stack_pointer[hartid],
-                 task_stack_page(tidle) + THREAD_SIZE);
-       WRITE_ONCE(__cpu_up_task_pointer[hartid], tidle);
-
-       lockdep_assert_held(&cpu_running);
-       wait_for_completion_timeout(&cpu_running,
+       ret = start_secondary_cpu(cpu, tidle);
+       if (!ret) {
+               lockdep_assert_held(&cpu_running);
+               wait_for_completion_timeout(&cpu_running,
                                            msecs_to_jiffies(1000));
 
-       if (!cpu_online(cpu)) {
-               pr_crit("CPU%u: failed to come online\n", cpu);
-               ret = -EIO;
+               if (!cpu_online(cpu)) {
+                       pr_crit("CPU%u: failed to come online\n", cpu);
+                       ret = -EIO;
+               }
+       } else {
+               pr_crit("CPU%u: failed to start\n", cpu);
        }
 
        return ret;
--
Quoted 2 lines

2.24.0

Apart from minor comments above, looks good to me.

Regards,
Anup

Signature · 2 lines
Regards,
Atish
#12 lore raw

PATCH v8 8/11 RISC-V: Add SBI HSM extension

In reply to Atish Patra

SBI specification defines HSM extension that allows to start/stop a hart by a supervisor anytime. The specification is available at

https://github.com/riscv/riscv-sbi-doc/blob/master/riscv-sbi.adoc

Implement SBI HSM extension.

---
 arch/riscv/include/asm/sbi.h    | 15 +++++++++++
 arch/riscv/kernel/Makefile      |  3 +++
 arch/riscv/kernel/cpu_ops_sbi.c | 48 +++++++++++++++++++++++++++++++++
 arch/riscv/kernel/sbi.c         |  3 ++-
 4 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/kernel/cpu_ops_sbi.c
arch/riscv/include/asm/sbi.h +15 -0
index d55d8090ab5c..0981a0c97eda 100644
@@ -26,6 +26,7 @@ enum sbi_ext_id {
 	SBI_EXT_TIME = 0x54494D45,
 	SBI_EXT_IPI = 0x735049,
 	SBI_EXT_RFENCE = 0x52464E43,
+	SBI_EXT_HSM = 0x48534D,
 };
 
 enum sbi_ext_base_fid {
@@ -56,6 +57,19 @@ enum sbi_ext_rfence_fid {
 	SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
 };
 
+enum sbi_ext_hsm_fid {
+	SBI_EXT_HSM_HART_START = 0,
+	SBI_EXT_HSM_HART_STOP,
+	SBI_EXT_HSM_HART_STATUS,
+};
+
+enum sbi_hsm_hart_status {
+	SBI_HSM_HART_STATUS_AVAILABLE = 0,
+	SBI_HSM_HART_STATUS_NOT_AVAILABLE,
+	SBI_HSM_HART_STATUS_START_PENDING,
+	SBI_HSM_HART_STATUS_STOP_PENDING,
+};
+
 #define SBI_SPEC_VERSION_DEFAULT	0x1
 #define SBI_SPEC_VERSION_MAJOR_SHIFT	24
 #define SBI_SPEC_VERSION_MAJOR_MASK	0x7f
@@ -130,6 +144,7 @@ static inline unsigned long sbi_minor_version(void)
 {
 	return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
 }
+int sbi_err_map_linux_errno(int err);
 #else /* CONFIG_RISCV_SBI */
 /* stubs for code that is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */
 void sbi_set_timer(uint64_t stime_value);
arch/riscv/kernel/Makefile +3 -0
index f81a6ff88005..a0be34b96846 100644
@@ -44,5 +44,8 @@ obj-$(CONFIG_PERF_EVENTS)	+= perf_event.o
 obj-$(CONFIG_PERF_EVENTS)	+= perf_callchain.o
 obj-$(CONFIG_HAVE_PERF_REGS)	+= perf_regs.o
 obj-$(CONFIG_RISCV_SBI)		+= sbi.o
+ifeq ($(CONFIG_RISCV_SBI), y)
+obj-$(CONFIG_SMP) += cpu_ops_sbi.o
+endif
 
 clean:
arch/riscv/kernel/cpu_ops_sbi.c new file +48 -0

48 lines not on this page · raw message

arch/riscv/kernel/sbi.c +2 -1
index cd0f68aeac70..45ad49269f2c 100644
@@ -47,7 +47,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 }
 EXPORT_SYMBOL(sbi_ecall);
 
-static int sbi_err_map_linux_errno(int err)
+int sbi_err_map_linux_errno(int err)
 {
 	switch (err) {
 	case SBI_SUCCESS:
@@ -64,6 +64,7 @@ static int sbi_err_map_linux_errno(int err)
 		return -ENOTSUPP;
 	};
 }
+EXPORT_SYMBOL(sbi_err_map_linux_errno);
 
 #ifdef CONFIG_RISCV_SBI_V01
 /**
Signature · 1 line
2.24.0
#13 lore raw

Re: [PATCH v8 08/11] RISC-V: Add SBI HSM extension

Quoted 7 lines · Atish Patra

SBI specification defines HSM extension that allows to start/stop a hart by a supervisor anytime. The specification is available at

https://github.com/riscv/riscv-sbi-doc/blob/master/riscv-sbi.adoc

Implement SBI HSM extension.

I think this PATCH needs to be further broken down.

There are three distinct changes here:
1. Exporting sbi_err_map_linux_errno() function arch/riscv/kernel/sbi.c arch/riscv/include/asm/sbi.h
2. SBI HSM defines
arch/riscv/include/asm/sbi.h
3. SBI HSM helper functions (which are mostly static functions) arch/riscv/kernel/Makefile arch/riscv/kernel/cpu_ops_sbi.c

We need separate patches for point1 and point2 above.

Also, point3 can be part of current PATCH9.

Regards,
Anup

Quoted 141 lines
---
 arch/riscv/include/asm/sbi.h    | 15 +++++++++++
 arch/riscv/kernel/Makefile      |  3 +++
 arch/riscv/kernel/cpu_ops_sbi.c | 48 +++++++++++++++++++++++++++++++++
 arch/riscv/kernel/sbi.c         |  3 ++-
 4 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/kernel/cpu_ops_sbi.c
arch/riscv/include/asm/sbi.h +15 -0
index d55d8090ab5c..0981a0c97eda 100644
@@ -26,6 +26,7 @@ enum sbi_ext_id {
        SBI_EXT_TIME = 0x54494D45,
        SBI_EXT_IPI = 0x735049,
        SBI_EXT_RFENCE = 0x52464E43,
+       SBI_EXT_HSM = 0x48534D,
 };
 
 enum sbi_ext_base_fid {
@@ -56,6 +57,19 @@ enum sbi_ext_rfence_fid {
        SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
 };
 
+enum sbi_ext_hsm_fid {
+       SBI_EXT_HSM_HART_START = 0,
+       SBI_EXT_HSM_HART_STOP,
+       SBI_EXT_HSM_HART_STATUS,
+};
+
+enum sbi_hsm_hart_status {
+       SBI_HSM_HART_STATUS_AVAILABLE = 0,
+       SBI_HSM_HART_STATUS_NOT_AVAILABLE,
+       SBI_HSM_HART_STATUS_START_PENDING,
+       SBI_HSM_HART_STATUS_STOP_PENDING,
+};
+
 #define SBI_SPEC_VERSION_DEFAULT       0x1
 #define SBI_SPEC_VERSION_MAJOR_SHIFT   24
 #define SBI_SPEC_VERSION_MAJOR_MASK    0x7f
@@ -130,6 +144,7 @@ static inline unsigned long sbi_minor_version(void)
 {
        return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
 }
+int sbi_err_map_linux_errno(int err);
 #else /* CONFIG_RISCV_SBI */
 /* stubs for code that is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */
 void sbi_set_timer(uint64_t stime_value);
arch/riscv/kernel/Makefile +3 -0
index f81a6ff88005..a0be34b96846 100644
@@ -44,5 +44,8 @@ obj-$(CONFIG_PERF_EVENTS)     += perf_event.o
 obj-$(CONFIG_PERF_EVENTS)      += perf_callchain.o
 obj-$(CONFIG_HAVE_PERF_REGS)   += perf_regs.o
 obj-$(CONFIG_RISCV_SBI)                += sbi.o
+ifeq ($(CONFIG_RISCV_SBI), y)
+obj-$(CONFIG_SMP) += cpu_ops_sbi.o
+endif
 
 clean:
arch/riscv/kernel/cpu_ops_sbi.c new file +48 -0
new file mode 100644
index 000000000000..9bdb60e0a4df
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * HSM extension and cpu_ops implementation.
+ *
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/init.h>
+#include <linux/mm.h>
+#include <asm/sbi.h>
+#include <asm/smp.h>
+
+static int sbi_hsm_hart_stop(void)
+{
+       struct sbiret ret;
+
+       ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_STOP, 0, 0, 0, 0, 0, 0);
+
+       if (ret.error)
+               return sbi_err_map_linux_errno(ret.error);
+       else
+               return 0;
+}
+
+static int sbi_hsm_hart_start(unsigned long hartid, unsigned long saddr,
+                      unsigned long priv)
+{
+       struct sbiret ret;
+
+       ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_START,
+                             hartid, saddr, priv, 0, 0, 0);
+       if (ret.error)
+               return sbi_err_map_linux_errno(ret.error);
+       else
+               return 0;
+}
+
+static int sbi_hsm_hart_get_status(unsigned long hartid)
+{
+       struct sbiret ret;
+
+       ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_STATUS,
+                             hartid, 0, 0, 0, 0, 0);
+       if (ret.error)
+               return sbi_err_map_linux_errno(ret.error);
+       else
+               return ret.value;
+}
arch/riscv/kernel/sbi.c +2 -2
index cd0f68aeac70..45ad49269f2c 100644
@@ -47,7 +47,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 }
 EXPORT_SYMBOL(sbi_ecall);
 
-static int sbi_err_map_linux_errno(int err)
+int sbi_err_map_linux_errno(int err)
 {
        switch (err) {
        case SBI_SUCCESS:
@@ -64,6 +64,7 @@ static int sbi_err_map_linux_errno(int err)
                return -ENOTSUPP;
        };
 }
+EXPORT_SYMBOL(sbi_err_map_linux_errno);
 
 #ifdef CONFIG_RISCV_SBI_V01
 /**
--

2.24.0

#14 lore raw

Re: [PATCH v8 08/11] RISC-V: Add SBI HSM extension

Quoted 22 lines · Anup Patel
Quoted 7 lines · Atish Patra

SBI specification defines HSM extension that allows to start/stop a hart by a supervisor anytime. The specification is available at

https://github.com/riscv/riscv-sbi-doc/blob/master/riscv-sbi.adoc

Implement SBI HSM extension.

I think this PATCH needs to be further broken down.

There are three distinct changes here:
1. Exporting sbi_err_map_linux_errno() function arch/riscv/kernel/sbi.c arch/riscv/include/asm/sbi.h
2. SBI HSM defines
arch/riscv/include/asm/sbi.h
3. SBI HSM helper functions (which are mostly static functions) arch/riscv/kernel/Makefile arch/riscv/kernel/cpu_ops_sbi.c

Quoted 4 lines

We need separate patches for point1 and point2 above.

Also, point3 can be part of current PATCH9.

Done.

Quoted 145 lines

Regards,
Anup

Quoted 141 lines
---
 arch/riscv/include/asm/sbi.h    | 15 +++++++++++
 arch/riscv/kernel/Makefile      |  3 +++
 arch/riscv/kernel/cpu_ops_sbi.c | 48 +++++++++++++++++++++++++++++++++
 arch/riscv/kernel/sbi.c         |  3 ++-
 4 files changed, 68 insertions(+), 1 deletion(-)
 create mode 100644 arch/riscv/kernel/cpu_ops_sbi.c
arch/riscv/include/asm/sbi.h +15 -0
index d55d8090ab5c..0981a0c97eda 100644
@@ -26,6 +26,7 @@ enum sbi_ext_id {
        SBI_EXT_TIME = 0x54494D45,
        SBI_EXT_IPI = 0x735049,
        SBI_EXT_RFENCE = 0x52464E43,
+       SBI_EXT_HSM = 0x48534D,
 };
 
 enum sbi_ext_base_fid {
@@ -56,6 +57,19 @@ enum sbi_ext_rfence_fid {
        SBI_EXT_RFENCE_REMOTE_HFENCE_VVMA_ASID,
 };
 
+enum sbi_ext_hsm_fid {
+       SBI_EXT_HSM_HART_START = 0,
+       SBI_EXT_HSM_HART_STOP,
+       SBI_EXT_HSM_HART_STATUS,
+};
+
+enum sbi_hsm_hart_status {
+       SBI_HSM_HART_STATUS_AVAILABLE = 0,
+       SBI_HSM_HART_STATUS_NOT_AVAILABLE,
+       SBI_HSM_HART_STATUS_START_PENDING,
+       SBI_HSM_HART_STATUS_STOP_PENDING,
+};
+
 #define SBI_SPEC_VERSION_DEFAULT       0x1
 #define SBI_SPEC_VERSION_MAJOR_SHIFT   24
 #define SBI_SPEC_VERSION_MAJOR_MASK    0x7f
@@ -130,6 +144,7 @@ static inline unsigned long sbi_minor_version(void)
 {
        return sbi_spec_version & SBI_SPEC_VERSION_MINOR_MASK;
 }
+int sbi_err_map_linux_errno(int err);
 #else /* CONFIG_RISCV_SBI */
 /* stubs for code that is only reachable under IS_ENABLED(CONFIG_RISCV_SBI): */
 void sbi_set_timer(uint64_t stime_value);
arch/riscv/kernel/Makefile +3 -0
index f81a6ff88005..a0be34b96846 100644
@@ -44,5 +44,8 @@ obj-$(CONFIG_PERF_EVENTS)     += perf_event.o
 obj-$(CONFIG_PERF_EVENTS)      += perf_callchain.o
 obj-$(CONFIG_HAVE_PERF_REGS)   += perf_regs.o
 obj-$(CONFIG_RISCV_SBI)                += sbi.o
+ifeq ($(CONFIG_RISCV_SBI), y)
+obj-$(CONFIG_SMP) += cpu_ops_sbi.o
+endif
 
 clean:
arch/riscv/kernel/cpu_ops_sbi.c new file +48 -0
new file mode 100644
index 000000000000..9bdb60e0a4df
@@ -0,0 +1,48 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * HSM extension and cpu_ops implementation.
+ *
+ * Copyright (c) 2020 Western Digital Corporation or its affiliates.
+ */
+
+#include <linux/init.h>
+#include <linux/mm.h>
+#include <asm/sbi.h>
+#include <asm/smp.h>
+
+static int sbi_hsm_hart_stop(void)
+{
+       struct sbiret ret;
+
+       ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_STOP, 0, 0, 0, 0, 0, 0);
+
+       if (ret.error)
+               return sbi_err_map_linux_errno(ret.error);
+       else
+               return 0;
+}
+
+static int sbi_hsm_hart_start(unsigned long hartid, unsigned long saddr,
+                      unsigned long priv)
+{
+       struct sbiret ret;
+
+       ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_START,
+                             hartid, saddr, priv, 0, 0, 0);
+       if (ret.error)
+               return sbi_err_map_linux_errno(ret.error);
+       else
+               return 0;
+}
+
+static int sbi_hsm_hart_get_status(unsigned long hartid)
+{
+       struct sbiret ret;
+
+       ret = sbi_ecall(SBI_EXT_HSM, SBI_EXT_HSM_HART_STATUS,
+                             hartid, 0, 0, 0, 0, 0);
+       if (ret.error)
+               return sbi_err_map_linux_errno(ret.error);
+       else
+               return ret.value;
+}
arch/riscv/kernel/sbi.c +2 -2
index cd0f68aeac70..45ad49269f2c 100644
@@ -47,7 +47,7 @@ struct sbiret sbi_ecall(int ext, int fid, unsigned long arg0,
 }
 EXPORT_SYMBOL(sbi_ecall);
 
-static int sbi_err_map_linux_errno(int err)
+int sbi_err_map_linux_errno(int err)
 {
        switch (err) {
        case SBI_SUCCESS:
@@ -64,6 +64,7 @@ static int sbi_err_map_linux_errno(int err)
                return -ENOTSUPP;
        };
 }
+EXPORT_SYMBOL(sbi_err_map_linux_errno);
 
 #ifdef CONFIG_RISCV_SBI_V01
 /**
--

2.24.0

Signature · 2 lines
Regards,
Atish
#15 lore raw

PATCH v8 9/11 RISC-V: Add supported for ordered booting method using HSM

In reply to Atish Patra

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.

---
 arch/riscv/kernel/cpu_ops.c     | 10 +++++++++-
 arch/riscv/kernel/cpu_ops_sbi.c | 31 +++++++++++++++++++++++++++++++
 arch/riscv/kernel/head.S        | 26 ++++++++++++++++++++++++++
 arch/riscv/kernel/smpboot.c     |  2 +-
 arch/riscv/kernel/traps.c       |  2 +-
 5 files changed, 68 insertions(+), 3 deletions(-)
arch/riscv/kernel/cpu_ops.c +9 -1
index 1085def3735a..6221bbedaea4 100644
@@ -18,6 +18,7 @@ const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init;
 void *__cpu_up_stack_pointer[NR_CPUS];
 void *__cpu_up_task_pointer[NR_CPUS];
 
+extern const struct cpu_operations cpu_ops_sbi;
 extern const struct cpu_operations cpu_ops_spinwait;
 
 void cpu_update_secondary_bootdata(unsigned int cpuid,
@@ -34,7 +35,14 @@ void cpu_update_secondary_bootdata(unsigned int cpuid,
 
 int __init cpu_set_ops(int cpuid)
 {
-	cpu_ops[cpuid] = &cpu_ops_spinwait;
+#if IS_ENABLED(CONFIG_RISCV_SBI)
+	if (sbi_probe_extension(SBI_EXT_HSM) > 0) {
+		if (!cpuid)
+			pr_info("SBI v0.2 HSM extension detected\n");
+		cpu_ops[cpuid] = &cpu_ops_sbi;
+	} else
+#endif
+		cpu_ops[cpuid] = &cpu_ops_spinwait;
 
 	return 0;
 }
arch/riscv/kernel/cpu_ops_sbi.c +31 -0
index 9bdb60e0a4df..31487a80c3b8 100644
@@ -7,9 +7,13 @@
 
 #include <linux/init.h>
 #include <linux/mm.h>
+#include <asm/cpu_ops.h>
 #include <asm/sbi.h>
 #include <asm/smp.h>
 
+extern char secondary_start_sbi[];
+const struct cpu_operations cpu_ops_sbi;
+
 static int sbi_hsm_hart_stop(void)
 {
 	struct sbiret ret;
@@ -46,3 +50,30 @@ static int sbi_hsm_hart_get_status(unsigned long hartid)
 	else
 		return ret.value;
 }
+
+static int sbi_cpu_start(unsigned int cpuid, struct task_struct *tidle)
+{
+	int rc;
+	unsigned long boot_addr = __pa_symbol(secondary_start_sbi);
+	int hartid = cpuid_to_hartid_map(cpuid);
+
+	cpu_update_secondary_bootdata(cpuid, tidle);
+	rc = sbi_hsm_hart_start(hartid, boot_addr, 0);
+
+	return rc;
+}
+
+static int sbi_cpu_prepare(unsigned int cpuid)
+{
+	if (!cpu_ops_sbi.cpu_start) {
+		pr_err("cpu start method not defined for CPU [%d]\n", cpuid);
+		return -ENODEV;
+	}
+	return 0;
+}
+
+const struct cpu_operations cpu_ops_sbi = {
+	.name		= "sbi",
+	.cpu_prepare	= sbi_cpu_prepare,
+	.cpu_start	= sbi_cpu_start,
+};
arch/riscv/kernel/head.S +26 -0
index c1be597d22a1..8386cfafba98 100644
@@ -99,11 +99,37 @@ relocate:
 	ret
 #endif /* CONFIG_MMU */
 #ifdef CONFIG_SMP
+	.global secondary_start_sbi
+secondary_start_sbi:
+	/* Mask all interrupts */
+	csrw CSR_IE, zero
+	csrw CSR_IP, zero
+
+	/* Load the global pointer */
+	.option push
+	.option norelax
+		la gp, __global_pointer$
+	.option pop
+
+	/*
+	 * Disable FPU to detect illegal usage of
+	 * floating point in kernel space
+	 */
+	li t0, SR_FS
+	csrc CSR_STATUS, t0
+
 	/* Set trap vector to spin forever to help debug */
 	la a3, .Lsecondary_park
 	csrw CSR_TVEC, a3
 
 	slli a3, a0, LGREG
+	la a4, __cpu_up_stack_pointer
+	la a5, __cpu_up_task_pointer
+	add a4, a3, a4
+	add a5, a3, a5
+	REG_L sp, (a4)
+	REG_L tp, (a5)
+
 	.global secondary_start_common
 secondary_start_common:
 
arch/riscv/kernel/smpboot.c +1 -1
index 2ee41c779a16..2c56ac70e64d 100644
@@ -146,7 +146,7 @@ void __init smp_cpus_done(unsigned int max_cpus)
 /*
  * C entry point for a secondary processor.
  */
-asmlinkage __visible void __init smp_callin(void)
+asmlinkage __visible void smp_callin(void)
 {
 	struct mm_struct *mm = &init_mm;
 
arch/riscv/kernel/traps.c +1 -1
index f4cad5163bf2..0063dd7318d6 100644
@@ -147,7 +147,7 @@ int is_valid_bugaddr(unsigned long pc)
 }
 #endif /* CONFIG_GENERIC_BUG */
 
-void __init trap_init(void)
+void trap_init(void)
 {
 	/*
 	 * Set sup0 scratch register to 0, indicating to exception vector
Signature · 1 line
2.24.0
Reviewed-by on v8 9/11
#16 lore raw

Re: [PATCH v8 09/11] RISC-V: Add supported for ordered booting method using HSM

Quoted 170 lines · Atish Patra

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.

---
 arch/riscv/kernel/cpu_ops.c     | 10 +++++++++-
 arch/riscv/kernel/cpu_ops_sbi.c | 31 +++++++++++++++++++++++++++++++
 arch/riscv/kernel/head.S        | 26 ++++++++++++++++++++++++++
 arch/riscv/kernel/smpboot.c     |  2 +-
 arch/riscv/kernel/traps.c       |  2 +-
 5 files changed, 68 insertions(+), 3 deletions(-)
arch/riscv/kernel/cpu_ops.c +9 -1
index 1085def3735a..6221bbedaea4 100644
@@ -18,6 +18,7 @@ const struct cpu_operations *cpu_ops[NR_CPUS] __ro_after_init;
 void *__cpu_up_stack_pointer[NR_CPUS];
 void *__cpu_up_task_pointer[NR_CPUS];
 
+extern const struct cpu_operations cpu_ops_sbi;
 extern const struct cpu_operations cpu_ops_spinwait;
 
 void cpu_update_secondary_bootdata(unsigned int cpuid,
@@ -34,7 +35,14 @@ void cpu_update_secondary_bootdata(unsigned int cpuid,
 
 int __init cpu_set_ops(int cpuid)
 {
-       cpu_ops[cpuid] = &cpu_ops_spinwait;
+#if IS_ENABLED(CONFIG_RISCV_SBI)
+       if (sbi_probe_extension(SBI_EXT_HSM) > 0) {
+               if (!cpuid)
+                       pr_info("SBI v0.2 HSM extension detected\n");
+               cpu_ops[cpuid] = &cpu_ops_sbi;
+       } else
+#endif
+               cpu_ops[cpuid] = &cpu_ops_spinwait;
 
        return 0;
 }
arch/riscv/kernel/cpu_ops_sbi.c +31 -0
index 9bdb60e0a4df..31487a80c3b8 100644
@@ -7,9 +7,13 @@
 
 #include <linux/init.h>
 #include <linux/mm.h>
+#include <asm/cpu_ops.h>
 #include <asm/sbi.h>
 #include <asm/smp.h>
 
+extern char secondary_start_sbi[];
+const struct cpu_operations cpu_ops_sbi;
+
 static int sbi_hsm_hart_stop(void)
 {
        struct sbiret ret;
@@ -46,3 +50,30 @@ static int sbi_hsm_hart_get_status(unsigned long hartid)
        else
                return ret.value;
 }
+
+static int sbi_cpu_start(unsigned int cpuid, struct task_struct *tidle)
+{
+       int rc;
+       unsigned long boot_addr = __pa_symbol(secondary_start_sbi);
+       int hartid = cpuid_to_hartid_map(cpuid);
+
+       cpu_update_secondary_bootdata(cpuid, tidle);
+       rc = sbi_hsm_hart_start(hartid, boot_addr, 0);
+
+       return rc;
+}
+
+static int sbi_cpu_prepare(unsigned int cpuid)
+{
+       if (!cpu_ops_sbi.cpu_start) {
+               pr_err("cpu start method not defined for CPU [%d]\n", cpuid);
+               return -ENODEV;
+       }
+       return 0;
+}
+
+const struct cpu_operations cpu_ops_sbi = {
+       .name           = "sbi",
+       .cpu_prepare    = sbi_cpu_prepare,
+       .cpu_start      = sbi_cpu_start,
+};
arch/riscv/kernel/head.S +26 -0
index c1be597d22a1..8386cfafba98 100644
@@ -99,11 +99,37 @@ relocate:
        ret
 #endif /* CONFIG_MMU */
 #ifdef CONFIG_SMP
+       .global secondary_start_sbi
+secondary_start_sbi:
+       /* Mask all interrupts */
+       csrw CSR_IE, zero
+       csrw CSR_IP, zero
+
+       /* Load the global pointer */
+       .option push
+       .option norelax
+               la gp, __global_pointer$
+       .option pop
+
+       /*
+        * Disable FPU to detect illegal usage of
+        * floating point in kernel space
+        */
+       li t0, SR_FS
+       csrc CSR_STATUS, t0
+
        /* Set trap vector to spin forever to help debug */
        la a3, .Lsecondary_park
        csrw CSR_TVEC, a3
 
        slli a3, a0, LGREG
+       la a4, __cpu_up_stack_pointer
+       la a5, __cpu_up_task_pointer
+       add a4, a3, a4
+       add a5, a3, a5
+       REG_L sp, (a4)
+       REG_L tp, (a5)
+
        .global secondary_start_common
 secondary_start_common:
 
arch/riscv/kernel/smpboot.c +1 -1
index 2ee41c779a16..2c56ac70e64d 100644
@@ -146,7 +146,7 @@ void __init smp_cpus_done(unsigned int max_cpus)
 /*
  * C entry point for a secondary processor.
  */
-asmlinkage __visible void __init smp_callin(void)
+asmlinkage __visible void smp_callin(void)
 {
        struct mm_struct *mm = &init_mm;
 
arch/riscv/kernel/traps.c +1 -2
index f4cad5163bf2..0063dd7318d6 100644
@@ -147,7 +147,7 @@ int is_valid_bugaddr(unsigned long pc)
 }
 #endif /* CONFIG_GENERIC_BUG */
 
-void __init trap_init(void)
+void trap_init(void)
 {
        /*
         * Set sup0 scratch register to 0, indicating to exception vector
--
Quoted 2 lines

2.24.0

Overall, this patch looks good but SBI HSM helper functions from PATCH8 should be moved to this patch.

Regards,
Anup

#17 lore raw

PATCH v8 10/11 irqchip/sifive-plic: Initialize the plic handler when cpu comes online

In reply to Atish Patra

Currently, plic threshold and priority are only initialized once in the beginning. However, threshold can be set to disabled if cpu is marked offline with cpu hotplug feature. This will not allow to change the irq affinity to a cpu that just came online.

Add plic specific cpu hotplug callback and initialize the per cpu handler when cpu comes online.

---
 drivers/irqchip/irq-sifive-plic.c | 34 ++++++++++++++++++++++++-------
 include/linux/cpuhotplug.h        |  1 +
 2 files changed, 28 insertions(+), 7 deletions(-)
drivers/irqchip/irq-sifive-plic.c +27 -7
index 0aca5807a119..9b564b19f4bf 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2018 Christoph Hellwig
  */
 #define pr_fmt(fmt) "plic: " fmt
+#include <linux/cpu.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/irq.h>
@@ -55,6 +56,8 @@
 #define     CONTEXT_THRESHOLD		0x00
 #define     CONTEXT_CLAIM		0x04
 
+#define	PLIC_DISABLE_THRESHOLD		0xffffffff
+
 static void __iomem *plic_regs;
 
 struct plic_handler {
@@ -208,6 +211,26 @@ static int plic_find_hart_id(struct device_node *node)
 	return -1;
 }
 
+static void plic_handler_init(struct plic_handler *handler, u32 threshold)
+{
+	irq_hw_number_t hwirq;
+
+	/* priority must be > threshold to trigger an interrupt */
+	writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
+	for (hwirq = 1; hwirq < plic_irqdomain->hwirq_max; hwirq++)
+		plic_toggle(handler, hwirq, 0);
+}
+
+static int plic_starting_cpu(unsigned int cpu)
+{
+	u32 threshold = 0;
+	struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
+
+	plic_handler_init(handler, threshold);
+
+	return 0;
+}
+
 static int __init plic_init(struct device_node *node,
 		struct device_node *parent)
 {
@@ -243,9 +266,7 @@ static int __init plic_init(struct device_node *node,
 	for (i = 0; i < nr_contexts; i++) {
 		struct of_phandle_args parent;
 		struct plic_handler *handler;
-		irq_hw_number_t hwirq;
 		int cpu, hartid;
-		u32 threshold = 0;
 
 		if (of_irq_parse_one(node, i, &parent)) {
 			pr_err("failed to parse parent for context %d.\n", i);
@@ -279,7 +300,7 @@ static int __init plic_init(struct device_node *node,
 		handler = per_cpu_ptr(&plic_handlers, cpu);
 		if (handler->present) {
 			pr_warn("handler already present for context %d.\n", i);
-			threshold = 0xffffffff;
+			plic_handler_init(handler, PLIC_DISABLE_THRESHOLD);
 			goto done;
 		}
 
@@ -291,13 +312,12 @@ static int __init plic_init(struct device_node *node,
 			plic_regs + ENABLE_BASE + i * ENABLE_PER_HART;
 
 done:
-		/* priority must be > threshold to trigger an interrupt */
-		writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
-		for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
-			plic_toggle(handler, hwirq, 0);
 		nr_handlers++;
 	}
 
+	cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
+				  "irqchip/sifive/plic:starting",
+				  plic_starting_cpu, NULL);
 	pr_info("mapped %d interrupts with %d handlers for %d contexts.\n",
 		nr_irqs, nr_handlers, nr_contexts);
 	set_handle_irq(plic_handle_irq);
include/linux/cpuhotplug.h +1 -0
index e51ee772b9f5..5360e03db08c 100644
@@ -100,6 +100,7 @@ enum cpuhp_state {
 	CPUHP_AP_IRQ_ARMADA_XP_STARTING,
 	CPUHP_AP_IRQ_BCM2836_STARTING,
 	CPUHP_AP_IRQ_MIPS_GIC_STARTING,
+	CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
 	CPUHP_AP_ARM_MVEBU_COHERENCY,
 	CPUHP_AP_MICROCODE_LOADER,
 	CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING,
Signature · 1 line
2.24.0
#18 lore raw

Re: [PATCH v8 10/11] irqchip/sifive-plic: Initialize the plic handler when cpu comes online

Quoted 38 lines · Atish Patra

Currently, plic threshold and priority are only initialized once in the beginning. However, threshold can be set to disabled if cpu is marked offline with cpu hotplug feature. This will not allow to change the irq affinity to a cpu that just came online.

Add plic specific cpu hotplug callback and initialize the per cpu handler when cpu comes online.

---
 drivers/irqchip/irq-sifive-plic.c | 34 ++++++++++++++++++++++++-------
 include/linux/cpuhotplug.h        |  1 +
 2 files changed, 28 insertions(+), 7 deletions(-)
drivers/irqchip/irq-sifive-plic.c +3 -0
index 0aca5807a119..9b564b19f4bf 100644
@@ -4,6 +4,7 @@
  * Copyright (C) 2018 Christoph Hellwig
  */
 #define pr_fmt(fmt) "plic: " fmt
+#include <linux/cpu.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
 #include <linux/irq.h>
@@ -55,6 +56,8 @@
 #define     CONTEXT_THRESHOLD          0x00
 #define     CONTEXT_CLAIM              0x04
 
+#define        PLIC_DISABLE_THRESHOLD          0xffffffff
+
 static void __iomem *plic_regs;
 
 struct plic_handler {
@@ -208,6 +211,26 @@ static int plic_find_hart_id(struct device_node *node)
        return -1;

drivers/irqchip/irq-sifive-plic.c static int plic_find_hart_id(struct device_node *node) · line 221

Quoted 10 lines
drivers/irqchip/irq-sifive-plic.c +8 -0
 }
 
+static void plic_handler_init(struct plic_handler *handler, u32 threshold)
+{
+       irq_hw_number_t hwirq;
+
+       /* priority must be > threshold to trigger an interrupt */
+       writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
+       for (hwirq = 1; hwirq < plic_irqdomain->hwirq_max; hwirq++)
+               plic_toggle(handler, hwirq, 0);

Ensuring that all IRQs are disabled is only required at boot-time. For run-time, CPU hotplug, I am sure Linux irq subsystem will migrate-and-disable IRQs routed to given CPU when the CPU does down.

Further, we should also ensure that all IRQs are disabled for PLIC contexts not used by S-mode Linux kernel.

Based on the above rationale, the loop to disable all IRQs should still be part of plic_init().

drivers/irqchip/irq-sifive-plic.c static int plic_find_hart_id(struct device_node *node) · line 233

Quoted 12 lines
drivers/irqchip/irq-sifive-plic.c +12 -0
+}
+
+static int plic_starting_cpu(unsigned int cpu)
+{
+       u32 threshold = 0;
+       struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
+
+       plic_handler_init(handler, threshold);
+
+       return 0;
+}
+

Addition to PLIC context threshold programming, the plic_starting_cpu() should also set IE_EIE bit in CSR_IE instead of doing it in trap_init() function of arch/riscv/kernel.trap.c

You can also define plic_stoping_cpu() which does the reverse of what plic_starting_cpu() is doing.

Quoted 52 lines
drivers/irqchip/irq-sifive-plic.c +4 -7
 static int __init plic_init(struct device_node *node,
                struct device_node *parent)
 {
@@ -243,9 +266,7 @@ static int __init plic_init(struct device_node *node,
        for (i = 0; i < nr_contexts; i++) {
                struct of_phandle_args parent;
                struct plic_handler *handler;
-               irq_hw_number_t hwirq;
                int cpu, hartid;
-               u32 threshold = 0;
 
                if (of_irq_parse_one(node, i, &parent)) {
                        pr_err("failed to parse parent for context %d.\n", i);
@@ -279,7 +300,7 @@ static int __init plic_init(struct device_node *node,
                handler = per_cpu_ptr(&plic_handlers, cpu);
                if (handler->present) {
                        pr_warn("handler already present for context %d.\n", i);
-                       threshold = 0xffffffff;
+                       plic_handler_init(handler, PLIC_DISABLE_THRESHOLD);
                        goto done;
                }
 
@@ -291,13 +312,12 @@ static int __init plic_init(struct device_node *node,
                        plic_regs + ENABLE_BASE + i * ENABLE_PER_HART;
 
 done:
-               /* priority must be > threshold to trigger an interrupt */
-               writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
-               for (hwirq = 1; hwirq <= nr_irqs; hwirq++)
-                       plic_toggle(handler, hwirq, 0);
                nr_handlers++;
        }
 
+       cpuhp_setup_state(CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
+                                 "irqchip/sifive/plic:starting",
+                                 plic_starting_cpu, NULL);
        pr_info("mapped %d interrupts with %d handlers for %d contexts.\n",
                nr_irqs, nr_handlers, nr_contexts);
        set_handle_irq(plic_handle_irq);
include/linux/cpuhotplug.h +1 -1
index e51ee772b9f5..5360e03db08c 100644
@@ -100,6 +100,7 @@ enum cpuhp_state {
        CPUHP_AP_IRQ_ARMADA_XP_STARTING,
        CPUHP_AP_IRQ_BCM2836_STARTING,
        CPUHP_AP_IRQ_MIPS_GIC_STARTING,
+       CPUHP_AP_IRQ_SIFIVE_PLIC_STARTING,
        CPUHP_AP_ARM_MVEBU_COHERENCY,
        CPUHP_AP_MICROCODE_LOADER,
        CPUHP_AP_PERF_X86_AMD_UNCORE_STARTING,
--
Quoted 2 lines

2.24.0

Regards,
Anup

#19 lore raw

Re: [PATCH v8 10/11] irqchip/sifive-plic: Initialize the plic handler when cpu comes online

In reply to Atish Patra
Quoted 4 lines · Atish Patra
drivers/irqchip/irq-sifive-plic.c +4 -0
+static void plic_handler_init(struct plic_handler *handler, u32 threshold)
+{
+	irq_hw_number_t hwirq;
+

drivers/irqchip/irq-sifive-plic.c static int plic_find_hart_id(struct device_node *node) · line 226

Quoted 10 lines
drivers/irqchip/irq-sifive-plic.c +9 -0
+	/* priority must be > threshold to trigger an interrupt */
+	writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
+	for (hwirq = 1; hwirq < plic_irqdomain->hwirq_max; hwirq++)
+		plic_toggle(handler, hwirq, 0);
+}
 
+
+static int plic_starting_cpu(unsigned int cpu)
+{
+	u32 threshold = 0;

Pointless variable. Also you use PLIC_DISABLE_THRESHOLD down below, so please add a proper define for enable as well.

drivers/irqchip/irq-sifive-plic.c static int plic_find_hart_id(struct device_node *node) · line 227

Quoted 1 line
drivers/irqchip/irq-sifive-plic.c +1 -0
+	struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);
        this_cpu_ptr*&...)

The callback is guaranteed to run on the plugged in CPU.

drivers/irqchip/irq-sifive-plic.c static int __init plic_init(struct device_node *node, · line 303

Quoted 2 lines
drivers/irqchip/irq-sifive-plic.c +1 -1
-			threshold = 0xffffffff;
+			plic_handler_init(handler, PLIC_DISABLE_THRESHOLD);

Thanks,

tglx

#20 lore raw

Re: [PATCH v8 10/11] irqchip/sifive-plic: Initialize the plic handler when cpu comes online

Quoted 13 lines · Thomas Gleixner
Quoted 9 lines · Atish Patra
drivers/irqchip/irq-sifive-plic.c +9 -0
+static void plic_handler_init(struct plic_handler *handler, u32 threshold)
+{
+     irq_hw_number_t hwirq;
+
+     /* priority must be > threshold to trigger an interrupt */
+     writel(threshold, handler->hart_base + CONTEXT_THRESHOLD);
+     for (hwirq = 1; hwirq < plic_irqdomain->hwirq_max; hwirq++)
+             plic_toggle(handler, hwirq, 0);
+}
Quoted 8 lines

drivers/irqchip/irq-sifive-plic.c static int plic_find_hart_id(struct device_node *node) · line 226

Quoted 4 lines
drivers/irqchip/irq-sifive-plic.c +4 -0
+
+static int plic_starting_cpu(unsigned int cpu)
+{
+     u32 threshold = 0;

Pointless variable. Also you use PLIC_DISABLE_THRESHOLD down below, so please add a proper define for enable as well.

Sure. Will do that.

Quoted 6 lines
Quoted 1 line

+ struct plic_handler *handler = per_cpu_ptr(&plic_handlers, cpu);

        this_cpu_ptr*&...)

The callback is guaranteed to run on the plugged in CPU.

Ah yes. I will change it to this_cpu_ptr. Thanks.

Quoted 7 lines

drivers/irqchip/irq-sifive-plic.c static int __init plic_init(struct device_node *node, · line 303

Quoted 2 lines
drivers/irqchip/irq-sifive-plic.c +1 -1
-                     threshold = 0xffffffff;
+                     plic_handler_init(handler, PLIC_DISABLE_THRESHOLD);

Thanks,

tglx

Signature · 2 lines
Regards,
Atish
#21 lore raw

PATCH v8 11/11 RISC-V: Support cpu hotplug

In reply to Atish Patra

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

---
 arch/riscv/Kconfig               | 12 ++++-
 arch/riscv/include/asm/cpu_ops.h | 12 +++++
 arch/riscv/include/asm/smp.h     | 17 +++++++
 arch/riscv/kernel/Makefile       |  1 +
 arch/riscv/kernel/cpu-hotplug.c  | 87 ++++++++++++++++++++++++++++++++
 arch/riscv/kernel/cpu_ops_sbi.c  | 34 +++++++++++++
 arch/riscv/kernel/setup.c        | 19 ++++++-
 7 files changed, 180 insertions(+), 2 deletions(-)
 create mode 100644 arch/riscv/kernel/cpu-hotplug.c
arch/riscv/Kconfig +11 -1
index 2afe1382dc36..ffd9f4dbd4fc 100644
@@ -22,7 +22,6 @@ config RISCV
 	select CLONE_BACKWARDS
 	select COMMON_CLK
 	select GENERIC_CLOCKEVENTS
-	select GENERIC_CPU_DEVICES
 	select GENERIC_IRQ_SHOW
 	select GENERIC_PCI_IOMAP
 	select GENERIC_SCHED_CLOCK
@@ -247,6 +246,17 @@ config NR_CPUS
 	depends on SMP
 	default "8"
 
+config HOTPLUG_CPU
+	bool "Support for hot-pluggable CPUs"
+	depends on SMP
+	select GENERIC_IRQ_MIGRATION
+	help
+
+	  Say Y here to experiment with turning CPUs off and on.  CPUs
+	  can be controlled through /sys/devices/system/cpu.
+
+	  Say N if you want to disable CPU hotplug.
+
 choice
 	prompt "CPU Tuning"
 	default TUNE_GENERIC
arch/riscv/include/asm/cpu_ops.h +12 -0
index 7db276284009..df92d20e3a50 100644
@@ -18,12 +18,24 @@
  *			is a mechanism for doing so, tests whether it is
  *			possible to boot the given HART.
  * @cpu_start:		Boots a cpu into the kernel.
+ * @cpu_disable:	Prepares a cpu to die. May fail for some
+ *			mechanism-specific reason, which will cause the hot
+ *			unplug to be aborted. Called from the cpu to be killed.
+ * @cpu_stop:		Makes a cpu leave the kernel. Must not fail. Called from
+ *			the cpu being stopped.
+ * @cpu_is_stopped:	Ensures a cpu has left the kernel. Called from another
+ *			cpu.
  */
 struct cpu_operations {
 	const char	*name;
 	int		(*cpu_prepare)(unsigned int cpu);
 	int		(*cpu_start)(unsigned int cpu,
 				     struct task_struct *tidle);
+#ifdef CONFIG_HOTPLUG_CPU
+	int		(*cpu_disable)(unsigned int cpu);
+	void		(*cpu_stop)(void);
+	int		(*cpu_is_stopped)(unsigned int cpu);
+#endif
 };
 
 extern const struct cpu_operations *cpu_ops[NR_CPUS];
arch/riscv/include/asm/smp.h +17 -0
index 023f74fb8b3b..f4c7cfda6b7f 100644
@@ -43,6 +43,13 @@ void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out);
  */
 #define raw_smp_processor_id() (current_thread_info()->cpu)
 
+#if defined CONFIG_HOTPLUG_CPU
+int __cpu_disable(void);
+void __cpu_die(unsigned int cpu);
+void cpu_stop(void);
+#else
+#endif /* CONFIG_HOTPLUG_CPU */
+
 #else
 
 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,
 }
 
 #endif /* CONFIG_SMP */
+
+#if defined(CONFIG_HOTPLUG_CPU) && (CONFIG_SMP)
+bool cpu_has_hotplug(unsigned int cpu);
+#else
+static inline bool cpu_has_hotplug(unsigned int cpu)
+{
+	return false;
+}
+#endif
+
 #endif /* _ASM_RISCV_SMP_H */
arch/riscv/kernel/Makefile +1 -0
index a0be34b96846..9601ac907f70 100644
@@ -47,5 +47,6 @@ obj-$(CONFIG_RISCV_SBI)		+= sbi.o
 ifeq ($(CONFIG_RISCV_SBI), y)
 obj-$(CONFIG_SMP) += cpu_ops_sbi.o
 endif
+obj-$(CONFIG_HOTPLUG_CPU)	+= cpu-hotplug.o
 
 clean:
arch/riscv/kernel/cpu-hotplug.c new file +87 -0

87 lines not on this page · raw message

arch/riscv/kernel/cpu_ops_sbi.c +34 -0
index 31487a80c3b8..3f77db8a256a 100644
@@ -72,8 +72,42 @@ static int sbi_cpu_prepare(unsigned int cpuid)
 	return 0;
 }
 
+#ifdef CONFIG_HOTPLUG_CPU
+static int sbi_cpu_disable(unsigned int cpuid)
+{
+	if (!cpu_ops_sbi.cpu_stop)
+		return -EOPNOTSUPP;
+	return 0;
+}
+
+static void sbi_cpu_stop(void)
+{
+	int ret;
+
+	ret = sbi_hsm_hart_stop();
+	pr_crit("Unable to stop the cpu %u (%d)\n", smp_processor_id(), ret);
+}
+
+static int sbi_cpu_is_stopped(unsigned int cpuid)
+{
+	int rc;
+	int hartid = cpuid_to_hartid_map(cpuid);
+
+	rc = sbi_hsm_hart_get_status(hartid);
+
+	if (rc == SBI_HSM_HART_STATUS_NOT_AVAILABLE)
+		return 0;
+	return rc;
+}
+#endif
+
 const struct cpu_operations cpu_ops_sbi = {
 	.name		= "sbi",
 	.cpu_prepare	= sbi_cpu_prepare,
 	.cpu_start	= sbi_cpu_start,
+#ifdef CONFIG_HOTPLUG_CPU
+	.cpu_disable	= sbi_cpu_disable,
+	.cpu_stop	= sbi_cpu_stop,
+	.cpu_is_stopped	= sbi_cpu_is_stopped,
+#endif
 };
arch/riscv/kernel/setup.c +18 -1
index 64d17cbae66b..21543a732a3d 100644
@@ -16,12 +16,13 @@
 #include <linux/of_platform.h>
 #include <linux/sched/task.h>
 #include <linux/swiotlb.h>
+#include <linux/smp.h>
 
 #include <asm/clint.h>
+#include <asm/cpu_ops.h>
 #include <asm/setup.h>
 #include <asm/sections.h>
 #include <asm/pgtable.h>
-#include <asm/smp.h>
 #include <asm/sbi.h>
 #include <asm/tlbflush.h>
 #include <asm/thread_info.h>
@@ -42,6 +43,7 @@ struct screen_info screen_info = {
 /* The lucky hart to first increment this variable will boot the other cores */
 atomic_t hart_lottery;
 unsigned long boot_cpu_hartid;
+static DEFINE_PER_CPU(struct cpu, cpu_devices);
 
 void __init parse_dtb(void)
 {
@@ -89,3 +91,18 @@ void __init setup_arch(char **cmdline_p)
 
 	riscv_fill_hwcap();
 }
+
+static int __init topology_init(void)
+{
+	int i;
+
+	for_each_possible_cpu(i) {
+		struct cpu *cpu = &per_cpu(cpu_devices, i);
+
+		cpu->hotpluggable = cpu_has_hotplug(i);
+		register_cpu(cpu, i);
+	}
+
+	return 0;
+}
+subsys_initcall(topology_init);
Signature · 1 line
2.24.0
Reviewed-by on v8 11/11
#22 lore raw

Re: [PATCH v8 11/11] RISC-V: Support cpu hotplug

Quoted 217 lines · Atish Patra

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

---
 arch/riscv/Kconfig               | 12 ++++-
 arch/riscv/include/asm/cpu_ops.h | 12 +++++
 arch/riscv/include/asm/smp.h     | 17 +++++++
 arch/riscv/kernel/Makefile       |  1 +
 arch/riscv/kernel/cpu-hotplug.c  | 87 ++++++++++++++++++++++++++++++++
 arch/riscv/kernel/cpu_ops_sbi.c  | 34 +++++++++++++
 arch/riscv/kernel/setup.c        | 19 ++++++-
 7 files changed, 180 insertions(+), 2 deletions(-)
 create mode 100644 arch/riscv/kernel/cpu-hotplug.c
arch/riscv/Kconfig +11 -1
index 2afe1382dc36..ffd9f4dbd4fc 100644
@@ -22,7 +22,6 @@ config RISCV
        select CLONE_BACKWARDS
        select COMMON_CLK
        select GENERIC_CLOCKEVENTS
-       select GENERIC_CPU_DEVICES
        select GENERIC_IRQ_SHOW
        select GENERIC_PCI_IOMAP
        select GENERIC_SCHED_CLOCK
@@ -247,6 +246,17 @@ config NR_CPUS
        depends on SMP
        default "8"
 
+config HOTPLUG_CPU
+       bool "Support for hot-pluggable CPUs"
+       depends on SMP
+       select GENERIC_IRQ_MIGRATION
+       help
+
+         Say Y here to experiment with turning CPUs off and on.  CPUs
+         can be controlled through /sys/devices/system/cpu.
+
+         Say N if you want to disable CPU hotplug.
+
 choice
        prompt "CPU Tuning"
        default TUNE_GENERIC
arch/riscv/include/asm/cpu_ops.h +12 -0
index 7db276284009..df92d20e3a50 100644
@@ -18,12 +18,24 @@
  *                     is a mechanism for doing so, tests whether it is
  *                     possible to boot the given HART.
  * @cpu_start:         Boots a cpu into the kernel.
+ * @cpu_disable:       Prepares a cpu to die. May fail for some
+ *                     mechanism-specific reason, which will cause the hot
+ *                     unplug to be aborted. Called from the cpu to be killed.
+ * @cpu_stop:          Makes a cpu leave the kernel. Must not fail. Called from
+ *                     the cpu being stopped.
+ * @cpu_is_stopped:    Ensures a cpu has left the kernel. Called from another
+ *                     cpu.
  */
 struct cpu_operations {
        const char      *name;
        int             (*cpu_prepare)(unsigned int cpu);
        int             (*cpu_start)(unsigned int cpu,
                                     struct task_struct *tidle);
+#ifdef CONFIG_HOTPLUG_CPU
+       int             (*cpu_disable)(unsigned int cpu);
+       void            (*cpu_stop)(void);
+       int             (*cpu_is_stopped)(unsigned int cpu);
+#endif
 };
 
 extern const struct cpu_operations *cpu_ops[NR_CPUS];
arch/riscv/include/asm/smp.h +17 -0
index 023f74fb8b3b..f4c7cfda6b7f 100644
@@ -43,6 +43,13 @@ void riscv_cpuid_to_hartid_mask(const struct cpumask *in, struct cpumask *out);
  */
 #define raw_smp_processor_id() (current_thread_info()->cpu)
 
+#if defined CONFIG_HOTPLUG_CPU
+int __cpu_disable(void);
+void __cpu_die(unsigned int cpu);
+void cpu_stop(void);
+#else
+#endif /* CONFIG_HOTPLUG_CPU */
+
 #else
 
 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,
 }
 
 #endif /* CONFIG_SMP */
+
+#if defined(CONFIG_HOTPLUG_CPU) && (CONFIG_SMP)
+bool cpu_has_hotplug(unsigned int cpu);
+#else
+static inline bool cpu_has_hotplug(unsigned int cpu)
+{
+       return false;
+}
+#endif
+
 #endif /* _ASM_RISCV_SMP_H */
arch/riscv/kernel/Makefile +1 -0
index a0be34b96846..9601ac907f70 100644
@@ -47,5 +47,6 @@ obj-$(CONFIG_RISCV_SBI)               += sbi.o
 ifeq ($(CONFIG_RISCV_SBI), y)
 obj-$(CONFIG_SMP) += cpu_ops_sbi.o
 endif
+obj-$(CONFIG_HOTPLUG_CPU)      += cpu-hotplug.o
 
 clean:
arch/riscv/kernel/cpu-hotplug.c new file +0 -0
new file mode 100644
index 000000000000..2a37cfb6eb46

arch/riscv/kernel/cpu-hotplug.c line 3

Quoted 3 lines
arch/riscv/kernel/cpu-hotplug.c new file +3 -0
@@ -0,0 +1,87 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (C) 2018 Western Digital Corporation or its affiliates.

Use Copyright (C) 2020

Quoted 178 lines
arch/riscv/kernel/cpu-hotplug.c new file +84 -0
+ */
+
+#include <linux/kernel.h>
+#include <linux/mm.h>
+#include <linux/sched.h>
+#include <linux/err.h>
+#include <linux/irq.h>
+#include <linux/cpu.h>
+#include <linux/sched/hotplug.h>
+#include <asm/irq.h>
+#include <asm/cpu_ops.h>
+#include <asm/sbi.h>
+
+void cpu_stop(void);
+void arch_cpu_idle_dead(void)
+{
+       cpu_stop();
+}
+
+bool cpu_has_hotplug(unsigned int cpu)
+{
+       if (cpu_ops[cpu]->cpu_stop)
+               return true;
+
+       return false;
+}
+
+/*
+ * __cpu_disable runs on the processor to be shutdown.
+ */
+int __cpu_disable(void)
+{
+       int ret = 0;
+       unsigned int cpu = smp_processor_id();
+
+       if (!cpu_ops[cpu] || !cpu_ops[cpu]->cpu_stop)
+               return -EOPNOTSUPP;
+
+       if (cpu_ops[cpu]->cpu_disable)
+               ret = cpu_ops[cpu]->cpu_disable(cpu);
+
+       if (ret)
+               return ret;
+
+       remove_cpu_topology(cpu);
+       set_cpu_online(cpu, false);
+       irq_migrate_all_off_this_cpu();
+
+       return ret;
+}
+
+/*
+ * Called on the thread which is asking for a CPU to be shutdown.
+ */
+void __cpu_die(unsigned int cpu)
+{
+       int ret = 0;
+
+       if (!cpu_wait_death(cpu, 5)) {
+               pr_err("CPU %u: didn't die\n", cpu);
+               return;
+       }
+       pr_notice("CPU%u: off\n", cpu);
+
+       /* Verify from the firmware if the cpu is really stopped*/
+       if (cpu_ops[cpu]->cpu_is_stopped)
+               ret = cpu_ops[cpu]->cpu_is_stopped(cpu);
+       if (ret)
+               pr_warn("CPU%d may not have stopped: %d\n", cpu, ret);
+}
+
+/*
+ * Called from the idle thread for the CPU which has been shutdown.
+ */
+void cpu_stop(void)
+{
+       idle_task_exit();
+
+       (void)cpu_report_death();
+
+       cpu_ops[smp_processor_id()]->cpu_stop();
+       /* It should never reach here */
+       BUG();
+}
arch/riscv/kernel/cpu_ops_sbi.c +34 -0
index 31487a80c3b8..3f77db8a256a 100644
@@ -72,8 +72,42 @@ static int sbi_cpu_prepare(unsigned int cpuid)
        return 0;
 }
 
+#ifdef CONFIG_HOTPLUG_CPU
+static int sbi_cpu_disable(unsigned int cpuid)
+{
+       if (!cpu_ops_sbi.cpu_stop)
+               return -EOPNOTSUPP;
+       return 0;
+}
+
+static void sbi_cpu_stop(void)
+{
+       int ret;
+
+       ret = sbi_hsm_hart_stop();
+       pr_crit("Unable to stop the cpu %u (%d)\n", smp_processor_id(), ret);
+}
+
+static int sbi_cpu_is_stopped(unsigned int cpuid)
+{
+       int rc;
+       int hartid = cpuid_to_hartid_map(cpuid);
+
+       rc = sbi_hsm_hart_get_status(hartid);
+
+       if (rc == SBI_HSM_HART_STATUS_NOT_AVAILABLE)
+               return 0;
+       return rc;
+}
+#endif
+
 const struct cpu_operations cpu_ops_sbi = {
        .name           = "sbi",
        .cpu_prepare    = sbi_cpu_prepare,
        .cpu_start      = sbi_cpu_start,
+#ifdef CONFIG_HOTPLUG_CPU
+       .cpu_disable    = sbi_cpu_disable,
+       .cpu_stop       = sbi_cpu_stop,
+       .cpu_is_stopped = sbi_cpu_is_stopped,
+#endif
 };
arch/riscv/kernel/setup.c +18 -2
index 64d17cbae66b..21543a732a3d 100644
@@ -16,12 +16,13 @@
 #include <linux/of_platform.h>
 #include <linux/sched/task.h>
 #include <linux/swiotlb.h>
+#include <linux/smp.h>
 
 #include <asm/clint.h>
+#include <asm/cpu_ops.h>
 #include <asm/setup.h>
 #include <asm/sections.h>
 #include <asm/pgtable.h>
-#include <asm/smp.h>
 #include <asm/sbi.h>
 #include <asm/tlbflush.h>
 #include <asm/thread_info.h>
@@ -42,6 +43,7 @@ struct screen_info screen_info = {
 /* The lucky hart to first increment this variable will boot the other cores */
 atomic_t hart_lottery;
 unsigned long boot_cpu_hartid;
+static DEFINE_PER_CPU(struct cpu, cpu_devices);
 
 void __init parse_dtb(void)
 {
@@ -89,3 +91,18 @@ void __init setup_arch(char **cmdline_p)
 
        riscv_fill_hwcap();
 }
+
+static int __init topology_init(void)
+{
+       int i;
+
+       for_each_possible_cpu(i) {
+               struct cpu *cpu = &per_cpu(cpu_devices, i);
+
+               cpu->hotpluggable = cpu_has_hotplug(i);
+               register_cpu(cpu, i);
+       }
+
+       return 0;
+}
+subsys_initcall(topology_init);
--
Quoted 2 lines

2.24.0

Looks good to me.

Regards,
Anup

maintainer
#23 lore raw
In reply to Atish Patra
Quoted 10 lines · Atish Patra

The Supervisor Binary Interface(SBI) specification[1] now defines a base extension that provides extendability to add future extensions while maintaining backward compatibility with previous versions. The new version is defined as 0.2 and older version is marked as 0.1.

This series adds support v0.2 and a unified calling convention implementation between 0.1 and 0.2. It also add other SBI v0.2 functionality defined in [2]. The base support for SBI v0.2 is already available in OpenSBI v0.5. It also adds SBI HSM extension and cpu-hotplug support for RISC-V which requires additional patches[3] in OpenSBI.

Now that 0.2-rc1 has been tagged we should really start to get this into shape to merge this. My biggest worry is being able to put together a kernel that can boot on both 0.1 and 0.2 SBIs, with the hart lottery being my major worry there. I just skimmed this, but I was expected to see something like

diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S index 271860fc2c3f..849ba75959ba 100644
--- a/arch/riscv/kernel/head.S
+++ b/arch/riscv/kernel/head.S
@@ -187,6 +187,7 @@ relocate:
la a3, .Lsecondary_park
csrw CSR_TVEC, a3

+#ifdef CONFIG_SBI_V01
slli a3, a0, LGREG
la a1, __cpu_up_stack_pointer
la a2, __cpu_up_task_pointer
@@ -212,7 +213,10 @@ relocate:

    #endif
    
    	tail smp_callin

-#endif
+#else /* !CONFIG_SBI_V01 */
+ pr_warn("multiple harts booted an SBI v0.2+ only kernel");
+#endif /* CONFIG_SBI_V01 */
+#endif /* CONFIG_SMP */

    END(_start)

but I don't. Is there something else doing this?

Quoted 79 lines

[1] https://github.com/riscv/riscv-sbi-doc/blob/master/riscv-sbi.adoc
[2] https://github.com/riscv/riscv-sbi-doc/pull/27
[3] http://lists.infradead.org/pipermail/opensbi/2020-January/001050.html

The patches are also available in following github repositery.

OpenSBI : https://github.com/atishp04/opensbi/tree/sbi_hsm_v1
Linux Kernel: https://github.com/atishp04/linux/tree/sbi_v0.2_v8

Changes from v7->v8:
1. Refactored to code to have modular cpu_ops calls.
2. Refactored HSM extension from sbi.c to cpu_ops_sbi.c.
3. Fix plic driver to handle cpu hotplug.

Changes from v6->v7:
1. Rebased on v5.5
2. Fixed few compilation issues for !CONFIG_SMP and !CONFIG_RISCV_SBI
3. Added SBI HSM extension
4. Add CPU hotplug support

Changes from v5->v6
1. Fixed few compilation issues around config.
2. Fixed hart mask generation issues for RFENCE & IPI extensions.

Changes from v4->v5
1. Fixed few minor comments related to static & inline.
2. Make sure that every patch is boot tested individually.

Changes from v3->v4.
1. Rebased on for-next.
2. Fixed issuses with checkpatch --strict.
3. Unfied all IPI/fence related functions.
4. Added Hfence related SBI calls.

Changes from v2->v3.
1. Moved v0.1 extensions to a new config.
2. Added support for relacement extensions of v0.1 extensions.

Changes from v1->v2
1. Removed the legacy calling convention.
2. Moved all SBI related calls to sbi.c.
3. Moved all SBI related macros to uapi.

Atish Patra (11):
RISC-V: Mark existing SBI as 0.1 SBI.
RISC-V: Add basic support for SBI v0.2
RISC-V: Add SBI v0.2 extension definitions
RISC-V: Introduce a new config for SBI v0.1
RISC-V: Implement new SBI v0.2 extensions
RISC-V: Move relocate and few other functions out of __init
RISC-V: Add cpu_ops and modify default booting method
RISC-V: Add SBI HSM extension
RISC-V: Add supported for ordered booting method using HSM irqchip/sifive-plic: Initialize the plic handler when cpu comes online
RISC-V: Support cpu hotplug

arch/riscv/Kconfig                   |  19 +-
arch/riscv/include/asm/cpu_ops.h     |  46 +++
arch/riscv/include/asm/sbi.h         | 194 ++++++----
arch/riscv/include/asm/smp.h         |  24 ++
arch/riscv/kernel/Makefile           |   6 +
arch/riscv/kernel/cpu-hotplug.c      |  87 +++++
arch/riscv/kernel/cpu_ops.c          |  48 +++
arch/riscv/kernel/cpu_ops_sbi.c      | 113 ++++++
arch/riscv/kernel/cpu_ops_spinwait.c |  42 +++
arch/riscv/kernel/head.S             | 179 +++++----
arch/riscv/kernel/sbi.c              | 524 ++++++++++++++++++++++++++-
arch/riscv/kernel/setup.c            |  24 +-
arch/riscv/kernel/smpboot.c          |  56 +--
arch/riscv/kernel/traps.c            |   2 +-
arch/riscv/kernel/vmlinux.lds.S      |   5 +-
drivers/irqchip/irq-sifive-plic.c    |  34 +-
include/linux/cpuhotplug.h           |   1 +
17 files changed, 1227 insertions(+), 177 deletions(-)

create mode 100644 arch/riscv/include/asm/cpu_ops.h create mode 100644 arch/riscv/kernel/cpu-hotplug.c create mode 100644 arch/riscv/kernel/cpu_ops.c create mode 100644 arch/riscv/kernel/cpu_ops_sbi.c create mode 100644 arch/riscv/kernel/cpu_ops_spinwait.c

#24 lore raw
Quoted 48 lines · Palmer Dabbelt
Quoted 6 lines · Atish Patra

The Supervisor Binary Interface(SBI) specification[1] now defines a base extension that provides extendability to add future extensions while maintaining backward compatibility with previous versions. The new version is defined as 0.2 and older version is marked as 0.1.

Quoted 7 lines

This series adds support v0.2 and a unified calling convention implementation between 0.1 and 0.2. It also add other SBI v0.2 functionality defined in [2]. The base support for SBI v0.2 is already
available in OpenSBI v0.5. It also adds SBI HSM extension and cpu- hotplug
support for RISC-V which requires additional patches[3] in OpenSBI.

Now that 0.2-rc1 has been tagged we should really start to get this into shape
to merge this. My biggest worry is being able to put together a kernel that
can boot on both 0.1 and 0.2 SBIs, with the hart lottery being my major worry
there. I just skimmed this, but I was expected to see something like

    diff --git a/arch/riscv/kernel/head.S b/arch/riscv/kernel/head.S
    index 271860fc2c3f..849ba75959ba 100644
    --- a/arch/riscv/kernel/head.S
    +++ b/arch/riscv/kernel/head.S
    @@ -187,6 +187,7 @@ relocate:
     	la a3, .Lsecondary_park
     	csrw CSR_TVEC, a3
     
    +#ifdef CONFIG_SBI_V01
     	slli a3, a0, LGREG
     	la a1, __cpu_up_stack_pointer
     	la a2, __cpu_up_task_pointer
    @@ -212,7 +213,10 @@ relocate:
     #endif
     
     	tail smp_callin
    -#endif
    +#else /* !CONFIG_SBI_V01 */
    +	pr_warn("multiple harts booted an SBI v0.2+ only kernel");
    +#endif /* CONFIG_SBI_V01 */
    +#endif /* CONFIG_SMP */
     
     END(_start)
Quoted 2 lines

but I don't. Is there something else doing this?

Yes. Current series ensures that it boots both 0.1 and 0.2 SBIs. It is done based on dynamic detection of HSM extension and cpu_ops. You can take a look at the patch 9.

+#if IS_ENABLED(CONFIG_RISCV_SBI)
+ if (sbi_probe_extension(SBI_EXT_HSM) > 0) {
+ if (!cpuid)
+ pr_info("SBI v0.2 HSM extension detected\n");
+ cpu_ops[cpuid] = &cpu_ops_sbi;
+ } else
+#endif
+ cpu_ops[cpuid] = &cpu_ops_spinwait;

https://patchwork.kernel.org/patch/11377359/

For SBI v0.1, probing will fail for HSM extension and cpu_ops_spinwait will be set as th cpu_ops.

Quoted 84 lines
Quoted 84 lines

[1]
https://github.com/riscv/riscv-sbi-doc/blob/master/riscv-sbi.adoc
[2] https://github.com/riscv/riscv-sbi-doc/pull/27
[3]
http://lists.infradead.org/pipermail/opensbi/2020-January/001050.html

The patches are also available in following github repositery.

OpenSBI : https://github.com/atishp04/opensbi/tree/sbi_hsm_v1
Linux Kernel: https://github.com/atishp04/linux/tree/sbi_v0.2_v8

Changes from v7->v8:
1. Refactored to code to have modular cpu_ops calls.
2. Refactored HSM extension from sbi.c to cpu_ops_sbi.c.
3. Fix plic driver to handle cpu hotplug.

Changes from v6->v7:
1. Rebased on v5.5
2. Fixed few compilation issues for !CONFIG_SMP and !CONFIG_RISCV_SBI
3. Added SBI HSM extension
4. Add CPU hotplug support

Changes from v5->v6
1. Fixed few compilation issues around config.
2. Fixed hart mask generation issues for RFENCE & IPI extensions.

Changes from v4->v5
1. Fixed few minor comments related to static & inline.
2. Make sure that every patch is boot tested individually.

Changes from v3->v4.
1. Rebased on for-next.
2. Fixed issuses with checkpatch --strict.
3. Unfied all IPI/fence related functions.
4. Added Hfence related SBI calls.

Changes from v2->v3.
1. Moved v0.1 extensions to a new config.
2. Added support for relacement extensions of v0.1 extensions.

Changes from v1->v2
1. Removed the legacy calling convention.
2. Moved all SBI related calls to sbi.c.
3. Moved all SBI related macros to uapi.

Atish Patra (11):
RISC-V: Mark existing SBI as 0.1 SBI.
RISC-V: Add basic support for SBI v0.2
RISC-V: Add SBI v0.2 extension definitions
RISC-V: Introduce a new config for SBI v0.1
RISC-V: Implement new SBI v0.2 extensions
RISC-V: Move relocate and few other functions out of __init
RISC-V: Add cpu_ops and modify default booting method
RISC-V: Add SBI HSM extension
RISC-V: Add supported for ordered booting method using HSM irqchip/sifive-plic: Initialize the plic handler when cpu comes online
RISC-V: Support cpu hotplug

arch/riscv/Kconfig                   |  19 +-
arch/riscv/include/asm/cpu_ops.h     |  46 +++
arch/riscv/include/asm/sbi.h         | 194 ++++++----
arch/riscv/include/asm/smp.h         |  24 ++
arch/riscv/kernel/Makefile           |   6 +
arch/riscv/kernel/cpu-hotplug.c      |  87 +++++
arch/riscv/kernel/cpu_ops.c          |  48 +++
arch/riscv/kernel/cpu_ops_sbi.c      | 113 ++++++
arch/riscv/kernel/cpu_ops_spinwait.c |  42 +++
arch/riscv/kernel/head.S             | 179 +++++----
arch/riscv/kernel/sbi.c              | 524
++++++++++++++++++++++++++-
arch/riscv/kernel/setup.c            |  24 +-
arch/riscv/kernel/smpboot.c          |  56 +--
arch/riscv/kernel/traps.c            |   2 +-
arch/riscv/kernel/vmlinux.lds.S      |   5 +-
drivers/irqchip/irq-sifive-plic.c    |  34 +-
include/linux/cpuhotplug.h           |   1 +
17 files changed, 1227 insertions(+), 177 deletions(-)

create mode 100644 arch/riscv/include/asm/cpu_ops.h create mode 100644 arch/riscv/kernel/cpu-hotplug.c create mode 100644 arch/riscv/kernel/cpu_ops.c create mode 100644 arch/riscv/kernel/cpu_ops_sbi.c create mode 100644 arch/riscv/kernel/cpu_ops_spinwait.c

Signature · 2 lines
Regards,
Atish