Skip to contentSkip to conversation

Unify implementation of cleanup_before_linux() for RISC-V ports

RFC PATCH In review Yao Zi 3 participants 8 messages Jan 23, 2025 – Feb 3, 2025 RISC-V

lore
Share & tools
lore

Yao Zi

author
#0 lore raw

cover Unify implementation of cleanup_before_linux() for RISC-V ports

This series introduces a generic version of cleanup_before_linux(), because most RISC-V SoCs have similar implementation. Ports for generic platforms and JH7110 are converted to use it for now: these devices are available to me and I have verified that Linux boots with these changes.

With a quick look at the codebase, I think this could be done for all RISC-V ports ultimately. Thanks for your feedback and review.

Yao Zi (3):
riscv: add a generic implementation for cleanup_before_linux()
riscv: cpu: generic: fallback to generic cleanup_before_linux()
riscv: cpu: jh7110: fallback to generic cleanup_before_linux()

 arch/riscv/cpu/cpu.c            | 17 +++++++++++++++++
 arch/riscv/cpu/generic/Makefile |  1 -
 arch/riscv/cpu/generic/cpu.c    | 22 ----------------------
 arch/riscv/cpu/jh7110/Makefile  |  1 -
 arch/riscv/cpu/jh7110/cpu.c     | 23 -----------------------
 5 files changed, 17 insertions(+), 47 deletions(-)
 delete mode 100644 arch/riscv/cpu/generic/cpu.c
 delete mode 100644 arch/riscv/cpu/jh7110/cpu.c
Signature · 1 line
2.48.0
author
#1 lore raw

RFC PATCH 1/3 riscv: add a generic implementation for cleanup_before_linux()

Most RISC-V SoCs have similar cleanup_before_linux() functions. Let's provide a weak symbol as fallback to reduce duplicated code.

---
 arch/riscv/cpu/cpu.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
arch/riscv/cpu/cpu.c +17 -0
index 06ecd92b9bc..5b31da64cbd 100644
@@ -11,11 +11,13 @@
 #include <event.h>
 #include <hang.h>
 #include <init.h>
+#include <irq_func.h>
 #include <log.h>
 #include <asm/encoding.h>
 #include <asm/system.h>
 #include <asm/hwcap.h>
 #include <asm/cpufeature.h>
+#include <asm/cache.h>
 #include <dm/uclass-internal.h>
 #include <linux/bitops.h>
 #include <linux/log2.h>
@@ -729,3 +731,18 @@ void reset_cpu(void)
 	hang();
 }
 #endif
+
+/*
+ * cleanup_before_linux() is called just before we call linux, which prepares
+ * the processor for linux.
+ * this weak implementation is used by default. we disable interrupts and flush
+ * the cache.
+ */
+__weak int cleanup_before_linux(void)
+{
+	disable_interrupts();
+
+	cache_flush();
+
+	return 0;
+}
Signature · 1 line
2.48.0
Reviewed-by on v1 1/3
#2 lore raw

Re: [RFC PATCH 1/3] riscv: add a generic implementation for cleanup_before_linux()

Quoted 7 lines · Yao Zi

Most RISC-V SoCs have similar cleanup_before_linux() functions. Let's provide a weak symbol as fallback to reduce duplicated code.

---
 arch/riscv/cpu/cpu.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
author
#3 lore raw

RFC PATCH 2/3 riscv: cpu: generic: fallback to generic cleanup_before_linux()

In reply to Yao Zi

The current implementation is equivalent to the fallback one, so this shouldn't change any behaviour but cleans the code up only.

---
 arch/riscv/cpu/generic/Makefile |  1 -
 arch/riscv/cpu/generic/cpu.c    | 22 ----------------------
 2 files changed, 23 deletions(-)
 delete mode 100644 arch/riscv/cpu/generic/cpu.c
arch/riscv/cpu/generic/Makefile +0 -1
index 258e4620dd4..a9be44ec387 100644
@@ -3,4 +3,3 @@
 # Copyright (C) 2018, Bin Meng <[email protected]>
 
 obj-y += dram.o
-obj-y += cpu.o
arch/riscv/cpu/generic/cpu.c deleted +0 -22
deleted file mode 100644
index f13c18942f3..00000000000
@@ -1,22 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * Copyright (C) 2018, Bin Meng <[email protected]>
- */
-
-#include <irq_func.h>
-#include <asm/cache.h>
-
-/*
- * cleanup_before_linux() is called just before we call linux
- * it prepares the processor for linux
- *
- * we disable interrupt and caches.
- */
-int cleanup_before_linux(void)
-{
-	disable_interrupts();
-
-	cache_flush();
-
-	return 0;
-}
Signature · 1 line
2.48.0
Reviewed-by on v1 2/3
#4 lore raw

Re: [RFC PATCH 2/3] riscv: cpu: generic: fallback to generic cleanup_before_linux()

Quoted 9 lines · Yao Zi

The current implementation is equivalent to the fallback one, so this shouldn't change any behaviour but cleans the code up only.

---
 arch/riscv/cpu/generic/Makefile |  1 -
 arch/riscv/cpu/generic/cpu.c    | 22 ----------------------
 2 files changed, 23 deletions(-)
 delete mode 100644 arch/riscv/cpu/generic/cpu.c
author
#5 lore raw

RFC PATCH 3/3 riscv: cpu: jh7110: fallback to generic cleanup_before_linux()

In reply to Yao Zi

JH7110 SoC requires no specific handling before entering Linux kernel. Let's drop the specific implementation to avoid duplication.

---
 arch/riscv/cpu/jh7110/Makefile |  1 -
 arch/riscv/cpu/jh7110/cpu.c    | 23 -----------------------
 2 files changed, 24 deletions(-)
 delete mode 100644 arch/riscv/cpu/jh7110/cpu.c
arch/riscv/cpu/jh7110/Makefile +0 -1
index 0939c1061d0..4f91aafa9da 100644
@@ -5,6 +5,5 @@
 ifeq ($(CONFIG_XPL_BUILD),y)
 obj-y += spl.o
 else
-obj-y += cpu.o
 obj-y += dram.o
 endif
arch/riscv/cpu/jh7110/cpu.c deleted +0 -23
deleted file mode 100644
index 1d7c026584a..00000000000
@@ -1,23 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0+
-/*
- * Copyright (C) 2022 StarFive Technology Co., Ltd.
- * Author: Yanhong Wang <[email protected]>
- */
-
-#include <asm/cache.h>
-#include <irq_func.h>
-
-/*
- * cleanup_before_linux() is called just before we call linux
- * it prepares the processor for linux
- *
- * we disable interrupt and caches.
- */
-int cleanup_before_linux(void)
-{
-	disable_interrupts();
-
-	cache_flush();
-
-	return 0;
-}
Signature · 1 line
2.48.0
Reviewed-by on v1 3/3
#6 lore raw

Re: [RFC PATCH 3/3] riscv: cpu: jh7110: fallback to generic cleanup_before_linux()

Quoted 9 lines · Yao Zi

JH7110 SoC requires no specific handling before entering Linux kernel. Let's drop the specific implementation to avoid duplication.

---
 arch/riscv/cpu/jh7110/Makefile |  1 -
 arch/riscv/cpu/jh7110/cpu.c    | 23 -----------------------
 2 files changed, 24 deletions(-)
 delete mode 100644 arch/riscv/cpu/jh7110/cpu.c
#7 lore raw
In reply to Yao Zi

Hi Yao:

I think you can just drop "RFC" in next version

Quoted 5 lines · Yao Zi

This series introduces a generic version of cleanup_before_linux(), because most RISC-V SoCs have similar implementation. Ports for generic platforms and JH7110 are converted to use it for now: these devices are available to me and I have verified that Linux boots with these changes.

thanks, generally I like this direction

Quoted 3 lines

With a quick look at the codebase, I think this could be done for all RISC-V ports ultimately. Thanks for your feedback and review.

can you go ahead and further cleanup other boards?

Quoted 17 lines

Yao Zi (3):
riscv: add a generic implementation for cleanup_before_linux()
riscv: cpu: generic: fallback to generic cleanup_before_linux()
riscv: cpu: jh7110: fallback to generic cleanup_before_linux()

 arch/riscv/cpu/cpu.c            | 17 +++++++++++++++++
 arch/riscv/cpu/generic/Makefile |  1 -
 arch/riscv/cpu/generic/cpu.c    | 22 ----------------------
 arch/riscv/cpu/jh7110/Makefile  |  1 -
 arch/riscv/cpu/jh7110/cpu.c     | 23 -----------------------
 5 files changed, 17 insertions(+), 47 deletions(-)
 delete mode 100644 arch/riscv/cpu/generic/cpu.c
 delete mode 100644 arch/riscv/cpu/jh7110/cpu.c
Signature · 1 line
2.48.0
Signature · 3 lines
Yixun Lan (dlan)
Gentoo Linux Developer
GPG Key ID AABEFD55