T-Head: Allow read access to th.mxstatus CSR
cover T-Head: Allow read access to th.mxstatus CSR
On T-Head C9xx harts, the th.mxstatus CSR provides valuable information about the available vendor extensions. As this CSR is only accessible in M-mode, we need to provide a mechanism for other modes to access this information. This patchset solves this by extending the existing platform abstraction such that platforms (or platform overrides) can provide a CSR read/write handler.
A CSR read-handler for th.mxstatus is then added that is utilized by the Allwinner D1, the T-Head TH1520, and the Sophgo SG2042. Further, a generic mvendorid-based platform override is added that also enables the CSR read-handler for th.mxstatus (needed for QEMU).
The CSR is documented here:
https://github.com/T-head-Semi/thead-extension-spec/pull/45
For QEMU this patch depends on
https://lists.gnu.org/archive/html/qemu-devel/2024-03/msg06368.html
Christoph M?llner (9):
sbi_platform: Add mechanism to handle CSR reads/writes in platform
platform: generic: Add mechanism to handle CSR reads/writes in
overrides
lib: sbi_emulate_csr: Utilize platform hooks for unknown CSRs
platform: generic: thead: Add CSR read handler for T-Head C9xx {allwinner/sun20i-d1,sophgo/sg2042,thead-generic}: Enable T-Head CSR
read access
platform: thead: Rename thead-generic to thead-th1520
platform: generic: Add mechanism to match via mvendorid
platform: generic: thead: Add definition of T-Head's vendor ID
platform: generic: Add generic T-Head override
include/sbi/sbi_platform.h | 48 +++++++++++++++ lib/sbi/sbi_emulate_csr.c | 8 ++- platform/generic/Kconfig | 12 +++- platform/generic/allwinner/sun20i-d1.c | 2 + platform/generic/configs/defconfig | 3 +- platform/generic/include/platform_override.h | 5 ++ platform/generic/include/thead/c9xx_csr.h | 16 +++++ .../generic/include/thead/c9xx_encoding.h | 2 + platform/generic/platform.c | 58 ++++++++++++++++++- platform/generic/sophgo/sg2042.c | 2 + platform/generic/thead/Kconfig | 4 ++ platform/generic/thead/objects.mk | 9 ++- platform/generic/thead/thead-generic.c | 42 +++----------- platform/generic/thead/thead-th1520.c | 55 ++++++++++++++++++ platform/generic/thead/thead_c9xx_csr.c | 30 ++++++++++ 15 files changed, 252 insertions(+), 44 deletions(-) create mode 100644 platform/generic/include/thead/c9xx_csr.h create mode 100644 platform/generic/thead/thead-th1520.c create mode 100644 platform/generic/thead/thead_c9xx_csr.c
Signature · 1 line
2.44.0
PATCH 1/9 sbi_platform: Add mechanism to handle CSR reads/writes in platform
Platforms may want to provide access to custom CSRs. Let's add a layer that allows platforms to implement CSR read/write handlers, where custom CSRs can be accessed.
--- include/sbi/sbi_platform.h | 48 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
include/sbi/sbi_platform.h +48 -0
@@ -148,6 +148,12 @@ struct sbi_platform_operations {/** platform specific handler to fixup store fault */ int (*emulate_store)(int wlen,unsigned long addr,union sbi_ldst_data in_val); +/** Platform-specific handler to read custom CSRs. */ +int (*read_csr)(int csr_num,struct sbi_trap_regs *regs, +ulong *csr_val); +/** Platform-specific handler to write custom CSRs. */ +int (*write_csr)(int csr_num,struct sbi_trap_regs *regs, +ulong csr_val); };/** Platform default per-HART stack size for exception/interrupt handling */
@@ -728,6 +734,48 @@ static inline int sbi_platform_emulate_store(const struct sbi_platform *plat,return SBI_ENOTSUPP ; } +/** +* Ask platform to read from CSR. +* +* @param plat pointer to struct sbi_platform +* @param csr_num CSR number +* @param regs Pointer to register state +* @param csr_val Pointer where the read value will be stored +* +* @return 0 on success and negative error code on failure +*/ +static inline int sbi_platform_read_csr (const struct sbi_platform *plat, +int csr_num, +struct sbi_trap_regs *regs, +ulong *csr_val) +{ +if (plat &&sbi_platform_ops (plat)->read_csr) +return sbi_platform_ops (plat)->read_csr (csr_num, regs, csr_val); + +return SBI_ENOTSUPP ; +} + +/** +* Ask platform to write to CSR. +* +* @param plat pointer to struct sbi_platform +* @param csr_num CSR number +* @param regs Pointer to register state +* @param csr_val Value to write +* +* @return 0 on success and negative error code on failure +*/ +static inline int sbi_platform_write_csr (const struct sbi_platform *plat, +int csr_num, +struct sbi_trap_regs *regs, +ulong csr_val) +{ +if (plat &&sbi_platform_ops (plat)->write_csr) +return sbi_platform_ops (plat)->write_csr (csr_num, regs, csr_val); + +return SBI_ENOTSUPP ; +} +#endif #endif
Signature · 1 line
2.44.0
PATCH 2/9 platform: generic: Add mechanism to handle CSR reads/writes in overrides
In reply to Christoph MüllnerOverrides may want to provide access to custom CSRs.
Let's add a layer that allows platform overrides to implement CSR read/write handlers, where custom CSRs can be accessed.
This utilizes the corresponding platform hooks to access CSRs.
--- platform/generic/include/platform_override.h | 4 ++++ platform/generic/platform.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+)
platform/generic/include/platform_override.h +4 -0
@@ -34,6 +34,10 @@ struct platform_override {struct sbi_trap_regs *regs, struct sbi_ecall_return *out, const struct fdt_match *match); +int (*read_csr)(int csr_num,struct sbi_trap_regs *regs, +ulong *csr_val); +int (*write_csr)(int csr_num,struct sbi_trap_regs *regs, + ulong csr_val); };#endif
platform/generic/platform.c +20 -0
@@ -273,6 +273,24 @@ static int generic_vendor_ext_provider(long funcid, generic_plat_match); } +static int generic_read_csr (int csr_num,struct sbi_trap_regs *regs, +ulong *csr_val) +{ +if (generic_plat && generic_plat->read_csr) +return generic_plat->read_csr (csr_num, regs, csr_val); + +return SBI_ENOTSUPP ; +} + +static int generic_write_csr (int csr_num,struct sbi_trap_regs *regs, +ulong csr_val) +{ +if (generic_plat && generic_plat->write_csr) +return generic_plat->write_csr (csr_num, regs, csr_val); + +return SBI_ENOTSUPP ; +} +static void generic_early_exit (void ) {if (generic_plat && generic_plat->early_exit)
@@ -408,6 +426,8 @@ const struct sbi_platform_operations platform_ops = { .timer_exit = fdt_timer_exit, .vendor_ext_check = generic_vendor_ext_check, .vendor_ext_provider = generic_vendor_ext_provider, + .read_csr = generic_read_csr, + .write_csr = generic_write_csr, };struct sbi_platform platform = {
Signature · 1 line
2.44.0
PATCH 3/9 lib: sbi_emulate_csr: Utilize platform hooks for unknown CSRs
In reply to Christoph MüllnerIn case we trap during CSR accesses, we have a few defined CSRs that are gracefully emulated. In case we don't know what to do with an unknown CSR, let's give the platform a chance to handle the CSR access.
--- lib/sbi/sbi_emulate_csr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
lib/sbi/sbi_emulate_csr.c +6 -2
@@ -14,6 +14,7 @@#include <sbi/sbi_emulate_csr.h> #include <sbi/sbi_error.h> #include <sbi/sbi_hart.h> +#include <sbi/sbi_platform.h> #include <sbi/sbi_scratch.h> #include <sbi/sbi_timer.h> #include <sbi/sbi_trap.h>
@@ -147,7 +148,8 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,#undef switchcase_hpmdefault : - ret =SBI_ENOTSUPP ; + ret =sbi_platform_read_csr (sbi_platform_ptr (scratch), + csr_num, regs, csr_val);break ; }
@@ -162,6 +164,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs, ulong csr_val) {int ret =0 ; +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();ulong prev_mode = (regs->mstatus &MSTATUS_MPP ) >>MSTATUS_MPP_SHIFT ;#if __riscv_xlen ==32 bool virt = (regs->mstatusH &MSTATUSH_MPV ) ? true : false;
@@ -185,7 +188,8 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,break ;#endif default : - ret =SBI_ENOTSUPP ; + ret =sbi_platform_write_csr (sbi_platform_ptr (scratch), + csr_num, regs, csr_val);break ; }
Signature · 1 line
2.44.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 3/9 lib: sbi_emulate_csr: Utilize platform hooks for unknown CSRs
[adding the list]
Quoted 7 lines · On Wed, Mar 27, 2024 at 12:22?PM Vivian Wang <[email protected]> wrote:
Quoted 4 lines · Christoph M?llner
In case we trap during CSR accesses, we have a few defined CSRs that are gracefully emulated. In case we don't know what to do with an unknown CSR, let's give the platform a chance to handle the CSR access.
Quoted 6 lines
This seems to be a pretty awful way to handle this. Can we use a vendor SBI extension [1] instead? It would be far easier to implement and also easier for users to use and probe for suppport.
Also, the C910 manual mentions a sxstatus register that is partly RO and partly RW in Supervisor mode. Can we just use that instead?
Yes, I'll switch to sxstatus (I was not aware of this CSR). Thanks!
Quoted 52 lines
[1] https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-vendor.adoc
[2] https://github.com/T-head-Semi/openc910/blob/main/doc/%E7%8E%84%E9%93%81C910%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C.pdfVivian "dram"
Quoted 45 lines
Signed-off-by Christoph M?llner <[email protected]>--- lib/sbi/sbi_emulate_csr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)lib/sbi/sbi_emulate_csr.c +6
-2@@ -14,6 +14,7 @@#include <sbi/sbi_emulate_csr.h> #include <sbi/sbi_error.h> #include <sbi/sbi_hart.h> +#include <sbi/sbi_platform.h> #include <sbi/sbi_scratch.h> #include <sbi/sbi_timer.h> #include <sbi/sbi_trap.h> @@ -147,7 +148,8 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,#undef switchcase_hpmdefault : - ret =SBI_ENOTSUPP ; + ret =sbi_platform_read_csr (sbi_platform_ptr (scratch), + csr_num, regs, csr_val);break ; }@@ -162,6 +164,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs, ulong csr_val) {int ret =0 ; +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();ulong prev_mode = (regs->mstatus &MSTATUS_MPP ) >>MSTATUS_MPP_SHIFT ;#if __riscv_xlen ==32 bool virt = (regs->mstatusH &MSTATUSH_MPV ) ? true : false;@@ -185,7 +188,8 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,break ;#endif default : - ret =SBI_ENOTSUPP ; + ret =sbi_platform_write_csr (sbi_platform_ptr (scratch), + csr_num, regs, csr_val);break ; }
PATCH 3/9 lib: sbi_emulate_csr: Utilize platform hooks for unknown CSRs
Quoted 1 line · Christoph M?llner
[adding the list]
Oops! Thanks a lot for catching that
Vivian "dram"
Quoted 66 lines · On Wed, Mar 27, 2024 at 12:22?PM Vivian Wang <[email protected]> wrote:
Quoted 11 lines · Christoph M?llner
Quoted 4 lines
In case we trap during CSR accesses, we have a few defined CSRs that are gracefully emulated. In case we don't know what to do with an unknown CSR, let's give the platform a chance to handle the CSR access.
This seems to be a pretty awful way to handle this. Can we use a vendor SBI extension [1] instead? It would be far easier to implement and also easier for users to use and probe for suppport.
Also, the C910 manual mentions a sxstatus register that is partly RO and partly RW in Supervisor mode. Can we just use that instead?
Yes, I'll switch to sxstatus (I was not aware of this CSR). Thanks!
Quoted 50 lines
[1] https://github.com/riscv-non-isa/riscv-sbi-doc/blob/master/src/ext-vendor.adoc
[2] https://github.com/T-head-Semi/openc910/blob/main/doc/%E7%8E%84%E9%93%81C910%E7%94%A8%E6%88%B7%E6%89%8B%E5%86%8C.pdfVivian "dram"
Quoted 45 lines
Signed-off-by Christoph M?llner <[email protected]>--- lib/sbi/sbi_emulate_csr.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)lib/sbi/sbi_emulate_csr.c +6
-2@@ -14,6 +14,7 @@#include <sbi/sbi_emulate_csr.h> #include <sbi/sbi_error.h> #include <sbi/sbi_hart.h> +#include <sbi/sbi_platform.h> #include <sbi/sbi_scratch.h> #include <sbi/sbi_timer.h> #include <sbi/sbi_trap.h> @@ -147,7 +148,8 @@ int sbi_emulate_csr_read(int csr_num, struct sbi_trap_regs *regs,#undef switchcase_hpmdefault : - ret =SBI_ENOTSUPP ; + ret =sbi_platform_read_csr (sbi_platform_ptr (scratch), + csr_num, regs, csr_val);break ; }@@ -162,6 +164,7 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs, ulong csr_val) {int ret =0 ; +struct sbi_scratch *scratch =sbi_scratch_thishart_ptr ();ulong prev_mode = (regs->mstatus &MSTATUS_MPP ) >>MSTATUS_MPP_SHIFT ;#if __riscv_xlen ==32 bool virt = (regs->mstatusH &MSTATUSH_MPV ) ? true : false;@@ -185,7 +188,8 @@ int sbi_emulate_csr_write(int csr_num, struct sbi_trap_regs *regs,break ;#endif default : - ret =SBI_ENOTSUPP ; + ret =sbi_platform_write_csr (sbi_platform_ptr (scratch), + csr_num, regs, csr_val);break ; }
PATCH 4/9 platform: generic: thead: Add CSR read handler for T-Head C9xx
In reply to Christoph MüllnerT-Head C9xx harts provide a TH_MXSTATUS CSR, that allows viewing the state of vendor extensions. Let's provide a mechanism to handle read requests to this CSR, where the actual value of the CSR is provided.
--- platform/generic/include/thead/c9xx_csr.h | 16 ++++++++++++ platform/generic/thead/Kconfig | 4 +++ platform/generic/thead/objects.mk | 2 ++ platform/generic/thead/thead_c9xx_csr.c | 30 +++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 platform/generic/include/thead/c9xx_csr.h create mode 100644 platform/generic/thead/thead_c9xx_csr.c
platform/generic/include/thead/c9xx_csr.h new file +16 -0
@@ -0,0 +1,16 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Authors: +* Christoph M?llner <[email protected]> +*/ + +#ifndef __RISCV_THEAD_C9XX_CSR_H____ +#define __RISCV_THEAD_C9XX_CSR_H____ + +struct sbi_trap_regs ; + +int thead_c9xx_read_csr (int csr_num,struct sbi_trap_regs *regs, +ulong *csr_val); + +#endif // __RISCV_THEAD_C9XX_CSR_H____
platform/generic/thead/Kconfig +4 -0
@@ -1,5 +1,9 @@ # SPDX-License-Identifier: BSD-2-Clause +config THEAD_C9XX_CSR + bool "T-HEAD c9xx M-mode vendor CSR support" + default n + config THEAD_C9XX_PMU bool "T-HEAD c9xx M-mode PMU support" default n
platform/generic/thead/objects.mk +2 -0
@@ -5,6 +5,8 @@ # Copyright (C) 2023 Alibaba Group Holding Limited. # +platform-objs-$(CONFIG_THEAD_C9XX_CSR) += thead/thead_c9xx_csr.o + platform-objs-$(CONFIG_THEAD_C9XX_PMU) += thead/thead_c9xx_pmu.o platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_tlb_trap_handler.o
platform/generic/thead/thead_c9xx_csr.c new file +30 -0
@@ -0,0 +1,30 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Authors: +* Christoph M?llner <[email protected]> +*/ + +#include <thead/c9xx_encoding.h> +#include <sbi/riscv_asm.h> +#include <sbi/sbi_bitops.h> +#include <sbi/sbi_ecall.h> +#include <sbi/sbi_error.h> + +int thead_c9xx_read_csr (int csr_num,struct sbi_trap_regs *regs, +ulong *csr_val) +{ +int ret =0 ; + +switch (csr_num) { +case THEAD_C9XX_CSR_MXSTATUS : + *csr_val =csr_read (THEAD_C9XX_CSR_MXSTATUS ); +break ; + +default : + ret =SBI_ENOTSUPP ; +break ; + } + +return ret; +}
Signature · 1 line
2.44.0
PATCH 5/9 {allwinner/sun20i-d1,sophgo/sg2042,thead-generic}: Enable T-Head CSR read access
In reply to Christoph MüllnerLet's enable the T-Head CSR read handler for all platform-overrides that include a T-Head hart.
--- platform/generic/Kconfig | 3 +++ platform/generic/allwinner/sun20i-d1.c | 2 ++ platform/generic/sophgo/sg2042.c | 2 ++ platform/generic/thead/thead-generic.c | 2 ++ 4 files changed, 9 insertions(+)
platform/generic/Kconfig +3 -0
@@ -26,6 +26,7 @@ config PLATFORM_GENERIC_MINOR_VER config PLATFORM_ALLWINNER_D1 bool "Allwinner D1 support" depends on FDT_IRQCHIP_PLIC + select THEAD_C9XX_CSR select THEAD_C9XX_PMU default n
@@ -53,6 +54,7 @@ config PLATFORM_SIFIVE_FU740 config PLATFORM_SOPHGO_SG2042 bool "Sophgo sg2042 support" + select THEAD_C9XX_CSR select THEAD_C9XX_ERRATA select THEAD_C9XX_PMU default n
@@ -63,6 +65,7 @@ config PLATFORM_STARFIVE_JH7110 config PLATFORM_THEAD bool "THEAD C9xx support" + select THEAD_C9XX_CSR select THEAD_C9XX_ERRATA select THEAD_C9XX_PMU default n
platform/generic/allwinner/sun20i-d1.c +2 -0
@@ -5,6 +5,7 @@*/ #include <platform_override.h> +#include <thead/c9xx_csr.h> #include <thead/c9xx_encoding.h> #include <thead/c9xx_pmu.h> #include <sbi/riscv_asm.h>
@@ -238,4 +239,5 @@ const struct platform_override sun20i_d1 = { .final_init = sun20i_d1_final_init, .fdt_fixup = sun20i_d1_fdt_fixup, .extensions_init = sun20i_d1_extensions_init, + .read_csr = thead_c9xx_read_csr, };
platform/generic/sophgo/sg2042.c +2 -0
@@ -7,6 +7,7 @@*/ #include <platform_override.h> +#include <thead/c9xx_csr.h> #include <thead/c9xx_errata.h> #include <thead/c9xx_pmu.h> #include <sbi/sbi_const.h>
@@ -60,4 +61,5 @@ const struct platform_override sophgo_sg2042 = { .match_table = sophgo_sg2042_match, .early_init = sophgo_sg2042_early_init, .extensions_init = sophgo_sg2042_extensions_init, + .read_csr = thead_c9xx_read_csr, };
platform/generic/thead/thead-generic.c +2 -0
@@ -7,6 +7,7 @@*/ #include <platform_override.h> +#include <thead/c9xx_csr.h> #include <thead/c9xx_errata.h> #include <thead/c9xx_pmu.h> #include <sbi/sbi_const.h>
@@ -50,4 +51,5 @@ const struct platform_override thead_generic = { .match_table = thead_generic_match, .early_init = thead_generic_early_init, .extensions_init = thead_generic_extensions_init, + .read_csr = thead_c9xx_read_csr, };
Signature · 1 line
2.44.0
PATCH 6/9 platform: thead: Rename thead-generic to thead-th1520
In reply to Christoph MüllnerThe name generic is misleading since the compatible string list only has a single entry for the TH1520 ('thead,th1520'). Further, thead-generic is irrelevant for other platforms that include a T-Head CPU (e.g. the Allwinner D1). Therefore, let's rename thead-generic to thead-th1520.
---
platform/generic/Kconfig | 4 ++--
platform/generic/configs/defconfig | 2 +-
platform/generic/thead/objects.mk | 4 ++--
.../thead/{thead-generic.c => thead-th1520.c} | 20 +++++++++----------
4 files changed, 15 insertions(+), 15 deletions(-)
rename platform/generic/thead/{thead-generic.c => thead-th1520.c} (61%)platform/generic/Kconfig +2 -2
@@ -63,8 +63,8 @@ config PLATFORM_STARFIVE_JH7110 bool "StarFive JH7110 support" default n -config PLATFORM_THEAD - bool "THEAD C9xx support" +config PLATFORM_THEAD_TH1520 + bool "T-HEAD TH1520 support" select THEAD_C9XX_CSR select THEAD_C9XX_ERRATA select THEAD_C9XX_PMU
platform/generic/configs/defconfig +1 -1
@@ -5,7 +5,7 @@ CONFIG_PLATFORM_SIFIVE_FU540=y CONFIG_PLATFORM_SIFIVE_FU740=y CONFIG_PLATFORM_SOPHGO_SG2042=y CONFIG_PLATFORM_STARFIVE_JH7110=y -CONFIG_PLATFORM_THEAD=y +CONFIG_PLATFORM_THEAD_TH1520=y CONFIG_FDT_GPIO=y CONFIG_FDT_GPIO_DESIGNWARE=y CONFIG_FDT_GPIO_SIFIVE=y
platform/generic/thead/objects.mk +2 -2
@@ -12,5 +12,5 @@ platform-objs-$(CONFIG_THEAD_C9XX_PMU) += thead/thead_c9xx_pmu.o platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_tlb_trap_handler.o platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_errata_tlb_flush.o -carray-platform_override_modules-$(CONFIG_PLATFORM_THEAD) += thead_generic -platform-objs-$(CONFIG_PLATFORM_THEAD) += thead/thead-generic.o +carray-platform_override_modules-$(CONFIG_PLATFORM_THEAD_TH1520) += thead_th1520 +platform-objs-$(CONFIG_PLATFORM_THEAD_TH1520) += thead/thead-th1520.o
platform/generic/thead/thead-th1520.c renamed from platform/generic/thead/thead-generic.c +10 -10
@@ -16,14 +16,14 @@#include <sbi/sbi_string.h> #include <sbi_utils/fdt/fdt_helper.h> -struct thead_generic_quirks { +struct thead_th1520_quirks {u64 errata; }; -static int thead_generic_early_init (bool cold_boot, +static int thead_th1520_early_init (bool cold_boot,const struct fdt_match *match) { -struct thead_generic_quirks *quirks = (void *)match->data; +struct thead_th1520_quirks *quirks = (void *)match->data;if (quirks->errata &THEAD_QUIRK_ERRATA_TLB_FLUSH )thead_register_tlb_flush_trap_handler ();
@@ -31,25 +31,25 @@ static int thead_generic_early_init(bool cold_boot,return 0 ; } -static int thead_generic_extensions_init (const struct fdt_match *match, +static int thead_th1520_extensions_init (const struct fdt_match *match,struct sbi_hart_features *hfeatures) {thead_c9xx_register_pmu_device ();return 0 ; } -static struct thead_generic_quirks thead_th1520_quirks = { +static struct thead_th1520_quirks thead_th1520_quirks = { .errata =THEAD_QUIRK_ERRATA_TLB_FLUSH , }; -static const struct fdt_match thead_generic_match[] = { +static const struct fdt_match thead_th1520_match[] = { { .compatible ="thead,th1520" , .data = &thead_th1520_quirks }, { }, }; -const struct platform_override thead_generic = { - .match_table = thead_generic_match, - .early_init = thead_generic_early_init, - .extensions_init = thead_generic_extensions_init, +const struct platform_override thead_th1520 = { + .match_table = thead_th1520_match, + .early_init = thead_th1520_early_init, + .extensions_init = thead_th1520_extensions_init, .read_csr = thead_c9xx_read_csr, };
Signature · 1 line
2.44.0
PATCH 7/9 platform: generic: Add mechanism to match via mvendorid
In reply to Christoph MüllnerCurrently, all platforms and their overrides are matched via FDT-compatible strings. While this is the right approach, we are sometimes confronted with platforms that don't provide the necessary FDT-compatible strings.
To still be able to match a platforms, let's add a secondary matching mechanism for the MVENDORID CSR.
--- platform/generic/include/platform_override.h | 1 + platform/generic/platform.c | 38 ++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-)
platform/generic/include/platform_override.h +1 -0
@@ -17,6 +17,7 @@struct platform_override {const struct fdt_match *match_table; +const unsigned long *match_vendor_ids;u64 (*features)(const struct fdt_match *match);u64 (*tlbr_flush_limit)(const struct fdt_match *match);u32 (*tlb_num_entries)(const struct fdt_match *match);
platform/generic/platform.c +35 -3
@@ -36,12 +36,28 @@ extern unsigned long platform_override_modules_size;static const struct platform_override *generic_plat =NULL ;static const struct fdt_match *generic_plat_match =NULL ; -static void fw_platform_lookup_special (void *fdt,int root_offset) +static unsigned long vendor_id_match (unsigned long vendor_id, +const unsigned long *vendor_ids) +{ +const unsigned long *vid_ptr = vendor_ids; + +while (*vid_ptr) { +if (*vid_ptr == vendor_id) +return vendor_id; + + vid_ptr++; + } + +return 0 ; +} + +static void fw_platform_lookup_override (void *fdt,int root_offset) {const struct platform_override *plat;const struct fdt_match *match;int pos; +/* First try to find a FDT match. */ for (pos =0 ; pos < platform_override_modules_size; pos++) { plat = platform_override_modules[pos];if (!plat->match_table)
@@ -53,7 +69,23 @@ static void fw_platform_lookup_special(void *fdt, int root_offset) generic_plat = plat; generic_plat_match = match; -break ; +return ; + } + +/* No match via FDT -> try matching the vendor ID. */ +const unsigned long vendor_id =csr_read (CSR_MVENDORID ); +for (pos =0 ; pos < platform_override_modules_size; pos++) { + plat = platform_override_modules[pos]; +if (!plat->match_vendor_ids) +continue ; + +unsigned long match =vendor_id_match (vendor_id, plat->match_vendor_ids); +if (!match) +continue ; + + generic_plat = plat; + generic_plat_match =NULL ; +return ; } }
@@ -154,7 +186,7 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,if (root_offset <0 ) goto fail; -fw_platform_lookup_special (fdt, root_offset); +fw_platform_lookup_override (fdt, root_offset);if (generic_plat && generic_plat->fw_init) generic_plat->fw_init (fdt, generic_plat_match);
Signature · 1 line
2.44.0
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 7/9 platform: generic: Add mechanism to match via mvendorid
[adding the list]
Quoted 7 lines · On Wed, Mar 27, 2024 at 12:32?PM Vivian Wang <[email protected]> wrote:
Quoted 4 lines · Christoph M?llner
Currently, all platforms and their overrides are matched via FDT-compatible strings. While this is the right approach, we are sometimes confronted with platforms that don't provide the necessary FDT-compatible strings.
Quoted 7 lines
In that case, wouldn't it be easier to just provide a different FDT? If anything, probing mvendorid makes it much harder to fix things later on since it will match all future T-Head cores.
I know Linux has some mvendorid-probing code, but those are for situations so early in boot that FDT code isn't working yet. It wouldn't be necessary here at all.
MAEE probing is needed before setting up the page table (this is about PTE bits).
So, I think this qualifies for CSR probing during early boot (and that's how it is implemented in Linux).
Quoted 94 lines
Vivian "dram"
Quoted 89 lines
To still be able to match a platforms, let's add a secondary matching mechanism for the MVENDORID CSR.
Signed-off-by Christoph M?llner <[email protected]>--- platform/generic/include/platform_override.h | 1 + platform/generic/platform.c | 38 ++++++++++++++++++-- 2 files changed, 36 insertions(+), 3 deletions(-)platform/generic/include/platform_override.h +1
-0@@ -17,6 +17,7 @@struct platform_override {const struct fdt_match *match_table; +const unsigned long *match_vendor_ids;u64 (*features)(const struct fdt_match *match);u64 (*tlbr_flush_limit)(const struct fdt_match *match);u32 (*tlb_num_entries)(const struct fdt_match *match);platform/generic/platform.c +35
-3@@ -36,12 +36,28 @@ extern unsigned long platform_override_modules_size;static const struct platform_override *generic_plat =NULL ;static const struct fdt_match *generic_plat_match =NULL ; -static void fw_platform_lookup_special (void *fdt,int root_offset) +static unsigned long vendor_id_match (unsigned long vendor_id, +const unsigned long *vendor_ids) +{ +const unsigned long *vid_ptr = vendor_ids; + +while (*vid_ptr) { +if (*vid_ptr == vendor_id) +return vendor_id; + + vid_ptr++; + } + +return 0 ; +} + +static void fw_platform_lookup_override (void *fdt,int root_offset) {const struct platform_override *plat;const struct fdt_match *match;int pos; +/* First try to find a FDT match. */ for (pos =0 ; pos < platform_override_modules_size; pos++) { plat = platform_override_modules[pos];if (!plat->match_table)@@ -53,7 +69,23 @@ static void fw_platform_lookup_special(void *fdt, int root_offset) generic_plat = plat; generic_plat_match = match; -break ; +return ; + } + +/* No match via FDT -> try matching the vendor ID. */ +const unsigned long vendor_id =csr_read (CSR_MVENDORID ); +for (pos =0 ; pos < platform_override_modules_size; pos++) { + plat = platform_override_modules[pos]; +if (!plat->match_vendor_ids) +continue ; + +unsigned long match =vendor_id_match (vendor_id, plat->match_vendor_ids); +if (!match) +continue ; + + generic_plat = plat; + generic_plat_match =NULL ; +return ; } }@@ -154,7 +186,7 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,if (root_offset <0 ) goto fail; -fw_platform_lookup_special (fdt, root_offset); +fw_platform_lookup_override (fdt, root_offset);if (generic_plat && generic_plat->fw_init) generic_plat->fw_init (fdt, generic_plat_match);
PATCH 8/9 platform: generic: thead: Add definition of T-Head's vendor ID
In reply to Christoph MüllnerThis patch adds a macro for T-Head's vendor ID to the C9xx encodings.
--- platform/generic/include/thead/c9xx_encoding.h | 2 ++ 1 file changed, 2 insertions(+)
platform/generic/include/thead/c9xx_encoding.h +2 -0
@@ -1,6 +1,8 @@#ifndef __RISCV_THEAD_C9XX_ENCODING_H____#define __RISCV_THEAD_C9XX_ENCODING_H____ +#define THEAD_VENDOR_ID 0x5b7 +/* T-HEAD C9xx M mode CSR. */ #define THEAD_C9XX_CSR_MXSTATUS 0x7c0#define THEAD_C9XX_CSR_MHCR 0x7c1
Signature · 1 line
2.44.0
PATCH 9/9 platform: generic: Add generic T-Head override
In reply to Christoph MüllnerCurrently, we have support for three T-Head C9xx platforms:
* Allwinner D1
* T-Head TH1520
* Sophgo SG2042
To make the platform detection more generic, let's add a T-Head generic override that matches on T-Head's vendor ID.
Proper detection of T-Head harts is needed so we can insert the CSR read-handler for th.mxstatus.
--- platform/generic/Kconfig | 5 +++++ platform/generic/configs/defconfig | 1 + platform/generic/thead/objects.mk | 3 +++ platform/generic/thead/thead-generic.c | 27 ++++++++++++++++++++++++++ 4 files changed, 36 insertions(+) create mode 100644 platform/generic/thead/thead-generic.c
platform/generic/Kconfig +5 -0
@@ -63,6 +63,11 @@ config PLATFORM_STARFIVE_JH7110 bool "StarFive JH7110 support" default n +config PLATFORM_THEAD_GENERIC + bool "T-HEAD generic support" + select THEAD_C9XX_CSR + default n + config PLATFORM_THEAD_TH1520 bool "T-HEAD TH1520 support" select THEAD_C9XX_CSR
platform/generic/configs/defconfig +1 -0
@@ -5,6 +5,7 @@ CONFIG_PLATFORM_SIFIVE_FU540=y CONFIG_PLATFORM_SIFIVE_FU740=y CONFIG_PLATFORM_SOPHGO_SG2042=y CONFIG_PLATFORM_STARFIVE_JH7110=y +CONFIG_PLATFORM_THEAD_GENERIC=y CONFIG_PLATFORM_THEAD_TH1520=y CONFIG_FDT_GPIO=y CONFIG_FDT_GPIO_DESIGNWARE=y
platform/generic/thead/objects.mk +3 -0
@@ -12,5 +12,8 @@ platform-objs-$(CONFIG_THEAD_C9XX_PMU) += thead/thead_c9xx_pmu.o platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_tlb_trap_handler.o platform-objs-$(CONFIG_THEAD_C9XX_ERRATA) += thead/thead_c9xx_errata_tlb_flush.o +carray-platform_override_modules-$(CONFIG_PLATFORM_THEAD_GENERIC) += thead_generic +platform-objs-$(CONFIG_PLATFORM_THEAD_GENERIC) += thead/thead-generic.o + carray-platform_override_modules-$(CONFIG_PLATFORM_THEAD_TH1520) += thead_th1520 platform-objs-$(CONFIG_PLATFORM_THEAD_TH1520) += thead/thead-th1520.o
platform/generic/thead/thead-generic.c new file +27 -0
@@ -0,0 +1,27 @@ +/* +* SPDX-License-Identifier: BSD-2-Clause +* +* Authors: +* Christoph M?llner <[email protected]> +*/ + +#include <platform_override.h> +#include <sbi/sbi_platform.h> +#include <sbi_utils/fdt/fdt_helper.h> +#include <thead/c9xx_csr.h> +#include <thead/c9xx_encoding.h> + +static unsigned long thead_generic_vendor_ids[] = { +THEAD_VENDOR_ID , +0 , +}; + +static const struct fdt_match thead_generic_match[] = { + { }, +}; + +const struct platform_override thead_generic = { + .match_table = thead_generic_match, + .match_vendor_ids = thead_generic_vendor_ids, + .read_csr = thead_c9xx_read_csr, +};
Signature · 1 line
2.44.0
cover T-Head: Allow read access to th.mxstatus CSR
In reply to Christoph MüllnerHi Christoph,
Quoted 6 lines · Christoph M?llner
On T-Head C9xx harts, the th.mxstatus CSR provides valuable information about the available vendor extensions. As this CSR is only accessible in M-mode, we need to provide a mechanism for other modes to access this information. This patchset solves this by extending the existing platform abstraction such that platforms (or platform overrides) can provide a CSR read/write handler.
As mentioned in the LKML thread, this change is not necessary, because th.mxstatus is readable in S-mode via its th.sxstatus mirror.
Regards,
Samuel
Quoted 44 lines
A CSR read-handler for th.mxstatus is then added that is utilized by the Allwinner D1, the T-Head TH1520, and the Sophgo SG2042. Further, a generic mvendorid-based platform override is added that also enables the CSR read-handler for th.mxstatus (needed for QEMU).
The CSR is documented here:
https://github.com/T-head-Semi/thead-extension-spec/pull/45For QEMU this patch depends on
https://lists.gnu.org/archive/html/qemu-devel/2024-03/msg06368.htmlChristoph M?llner (9):
sbi_platform: Add mechanism to handle CSR reads/writes in platform
platform: generic: Add mechanism to handle CSR reads/writes in
overrides
lib:sbi_emulate_csr: Utilize platform hooks for unknown CSRs
platform: generic: thead: Add CSR read handler for T-Head C9xx {allwinner/sun20i-d1,sophgo/sg2042,thead-generic}: Enable T-Head CSR
read access
platform: thead: Rename thead-generic to thead-th1520
platform: generic: Add mechanism to match via mvendorid
platform: generic: thead: Add definition of T-Head's vendor ID
platform: generic: Add generic T-Head overrideinclude/sbi/sbi_platform.h | 48 +++++++++++++++ lib/sbi/sbi_emulate_csr.c | 8 ++- platform/generic/Kconfig | 12 +++- platform/generic/allwinner/sun20i-d1.c | 2 + platform/generic/configs/defconfig | 3 +- platform/generic/include/platform_override.h | 5 ++ platform/generic/include/thead/c9xx_csr.h | 16 +++++ .../generic/include/thead/c9xx_encoding.h | 2 + platform/generic/platform.c | 58 ++++++++++++++++++- platform/generic/sophgo/sg2042.c | 2 + platform/generic/thead/Kconfig | 4 ++ platform/generic/thead/objects.mk | 9 ++- platform/generic/thead/thead-generic.c | 42 +++----------- platform/generic/thead/thead-th1520.c | 55 ++++++++++++++++++ platform/generic/thead/thead_c9xx_csr.c | 30 ++++++++++ 15 files changed, 252 insertions(+), 44 deletions(-) create mode 100644 platform/generic/include/thead/c9xx_csr.h create mode 100644 platform/generic/thead/thead-th1520.c create mode 100644 platform/generic/thead/thead_c9xx_csr.c
cover T-Head: Allow read access to th.mxstatus CSR
In reply to Christoph MüllnerHi Christoph,
You don't need the access to MXSTATUS. There is SXSTATUS which is a read-only mirror of MXSTATUS in S-mode.
cover T-Head: Allow read access to th.mxstatus CSR
In reply to Christoph MüllnerQuoted 12 lines · Christoph M?llner
On T-Head C9xx harts, the th.mxstatus CSR provides valuable information about the available vendor extensions. As this CSR is only accessible in M-mode, we need to provide a mechanism for other modes to access this information. This patchset solves this by extending the existing platform abstraction such that platforms (or platform overrides) can provide a CSR read/write handler.
A CSR read-handler for th.mxstatus is then added that is utilized by the Allwinner D1, the T-Head TH1520, and the Sophgo SG2042. Further, a generic mvendorid-based platform override is added that also enables the CSR read-handler for th.mxstatus (needed for QEMU).
Quoted 6 lines
The CSR is documented here:
https://github.com/T-head-Semi/thead-extension-spec/pull/45For QEMU this patch depends on
https://lists.gnu.org/archive/html/qemu-devel/2024-03/msg06368.html
Although using something like vendor id to provide basic init code for platform with the same cpus is a good idea, it may be too hard to provide common code for the whole vendor. As the cpu evolves, a vendor may provide something different completely from the previous cpu. This make provide common code for the entire vendor almost impossible.
There is an another suggestion discussed in the kernel, which suggest using pseudo extension to provide generic support. I think you want to check it.
https://lore.kernel.org/all/20240311063018.1886757-1-dqfext at gmail.com/
cover T-Head: Allow read access to th.mxstatus CSR
Quoted 27 lines · On Thu, Mar 28, 2024 at 8:30?AM Inochi Amaoto <[email protected]> wrote:
Quoted 12 lines · Christoph M?llner
On T-Head C9xx harts, the th.mxstatus CSR provides valuable information about the available vendor extensions. As this CSR is only accessible in M-mode, we need to provide a mechanism for other modes to access this information. This patchset solves this by extending the existing platform abstraction such that platforms (or platform overrides) can provide a CSR read/write handler.
A CSR read-handler for th.mxstatus is then added that is utilized by the Allwinner D1, the T-Head TH1520, and the Sophgo SG2042. Further, a generic mvendorid-based platform override is added that also enables the CSR read-handler for th.mxstatus (needed for QEMU).
Quoted 6 lines
The CSR is documented here:
https://github.com/T-head-Semi/thead-extension-spec/pull/45For QEMU this patch depends on
https://lists.gnu.org/archive/html/qemu-devel/2024-03/msg06368.htmlAlthough using something like vendor id to provide basic init code for platform with the same cpus is a good idea, it may be too hard to provide common code for the whole vendor. As the cpu evolves, a vendor may provide something different completely from the previous cpu. This make provide common code for the entire vendor almost impossible.
Quoted 4 lines
There is an another suggestion discussed in the kernel, which suggest using pseudo extension to provide generic support. I think you want to check it.
https://lore.kernel.org/all/20240311063018.1886757-1-dqfext at gmail.com/
This is all about custom extensions and their discovery. In this particular case, we have the additional challenge that MAEE affects early boot.
We have modeled it such that:
* XTheadMxStatus/XTheadSxStatus provides the CSR to probe
(https://github.com/T-head-Semi/thead-extension-spec/pull/45)
* XTheadMaee is the extension for the page-based memory attributes So, all functionality is covered by custom extensions.
The question if the kernel reads out the CSR or a DT property is a different discussion (DT is preferred but implies dependencies to FW providing that, and requires a boot process stage where we can already process a DT).
cover T-Head: Allow read access to th.mxstatus CSR
In reply to Christoph MüllnerHi Christoph,
Quoted 19 lines · On Wed, Mar 27, 2024 at 3:41?PM Christoph M?llner…
On T-Head C9xx harts, the th.mxstatus CSR provides valuable information about the available vendor extensions. As this CSR is only accessible in M-mode, we need to provide a mechanism for other modes to access this information. This patchset solves this by extending the existing platform abstraction such that platforms (or platform overrides) can provide a CSR read/write handler.
A CSR read-handler for th.mxstatus is then added that is utilized by the Allwinner D1, the T-Head TH1520, and the Sophgo SG2042. Further, a generic mvendorid-based platform override is added that also enables the CSR read-handler for th.mxstatus (needed for QEMU).
The CSR is documented here:
https://github.com/T-head-Semi/thead-extension-spec/pull/45For QEMU this patch depends on
https://lists.gnu.org/archive/html/qemu-devel/2024-03/msg06368.html
Quoted 12 lines
Christoph M?llner (9):
sbi_platform: Add mechanism to handle CSR reads/writes in platform
platform: generic: Add mechanism to handle CSR reads/writes in
overrides
lib:sbi_emulate_csr: Utilize platform hooks for unknown CSRs
platform: generic: thead: Add CSR read handler for T-Head C9xx {allwinner/sun20i-d1,sophgo/sg2042,thead-generic}: Enable T-Head CSR
read access
platform: thead: Rename thead-generic to thead-th1520
platform: generic: Add mechanism to match via mvendorid
platform: generic: thead: Add definition of T-Head's vendor ID
platform: generic: Add generic T-Head override
There is no activity on this series so I assume it is abandoned.
My only comment is that exposing M-mode CSRs to S-mode via CSR trap-n-emulate has been discouraged. If you really require to expose read-only information to supervisor software then prefer DT/ACPI.
Regards,
Anup
Quoted 29 lines
include/sbi/sbi_platform.h | 48 +++++++++++++++ lib/sbi/sbi_emulate_csr.c | 8 ++- platform/generic/Kconfig | 12 +++- platform/generic/allwinner/sun20i-d1.c | 2 + platform/generic/configs/defconfig | 3 +- platform/generic/include/platform_override.h | 5 ++ platform/generic/include/thead/c9xx_csr.h | 16 +++++ .../generic/include/thead/c9xx_encoding.h | 2 + platform/generic/platform.c | 58 ++++++++++++++++++- platform/generic/sophgo/sg2042.c | 2 + platform/generic/thead/Kconfig | 4 ++ platform/generic/thead/objects.mk | 9 ++- platform/generic/thead/thead-generic.c | 42 +++----------- platform/generic/thead/thead-th1520.c | 55 ++++++++++++++++++ platform/generic/thead/thead_c9xx_csr.c | 30 ++++++++++ 15 files changed, 252 insertions(+), 44 deletions(-) create mode 100644 platform/generic/include/thead/c9xx_csr.h create mode 100644 platform/generic/thead/thead-th1520.c create mode 100644 platform/generic/thead/thead_c9xx_csr.c--
2.44.0--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
cover T-Head: Allow read access to th.mxstatus CSR
Quoted 37 lines · On Mon, Jun 10, 2024 at 12:28?PM Anup Patel <[email protected]> wrote:
Hi Christoph,
Quoted 19 lines · On Wed, Mar 27, 2024 at 3:41?PM Christoph M?llner…
On T-Head C9xx harts, the th.mxstatus CSR provides valuable information about the available vendor extensions. As this CSR is only accessible in M-mode, we need to provide a mechanism for other modes to access this information. This patchset solves this by extending the existing platform abstraction such that platforms (or platform overrides) can provide a CSR read/write handler.
A CSR read-handler for th.mxstatus is then added that is utilized by the Allwinner D1, the T-Head TH1520, and the Sophgo SG2042. Further, a generic mvendorid-based platform override is added that also enables the CSR read-handler for th.mxstatus (needed for QEMU).
The CSR is documented here:
https://github.com/T-head-Semi/thead-extension-spec/pull/45For QEMU this patch depends on
https://lists.gnu.org/archive/html/qemu-devel/2024-03/msg06368.htmlQuoted 12 lines
Christoph M?llner (9):
sbi_platform: Add mechanism to handle CSR reads/writes in platform
platform: generic: Add mechanism to handle CSR reads/writes in
overrides
lib:sbi_emulate_csr: Utilize platform hooks for unknown CSRs
platform: generic: thead: Add CSR read handler for T-Head C9xx {allwinner/sun20i-d1,sophgo/sg2042,thead-generic}: Enable T-Head CSR
read access
platform: thead: Rename thead-generic to thead-th1520
platform: generic: Add mechanism to match via mvendorid
platform: generic: thead: Add definition of T-Head's vendor ID
platform: generic: Add generic T-Head override
Quoted 1 line
There is no activity on this series so I assume it is abandoned.
Yes, this can be abandoned.
Quoted 4 lines
My only comment is that exposing M-mode CSRs to S-mode via CSR trap-n-emulate has been discouraged. If you really require to expose read-only information to supervisor software then prefer DT/ACPI.
Besides th.mxstatus there is also th.sxstatus to access the same information from S-mode. So, no need to access th.mxstatus from Linux anymore and thus no need for this series.
And yes, exposing the vendor extensions implied by that CSR bit via DT/ACPI would be the better approach.
Thanks,
Christoph
Quoted 33 lines
Regards,
AnupQuoted 29 lines
include/sbi/sbi_platform.h | 48 +++++++++++++++ lib/sbi/sbi_emulate_csr.c | 8 ++- platform/generic/Kconfig | 12 +++- platform/generic/allwinner/sun20i-d1.c | 2 + platform/generic/configs/defconfig | 3 +- platform/generic/include/platform_override.h | 5 ++ platform/generic/include/thead/c9xx_csr.h | 16 +++++ .../generic/include/thead/c9xx_encoding.h | 2 + platform/generic/platform.c | 58 ++++++++++++++++++- platform/generic/sophgo/sg2042.c | 2 + platform/generic/thead/Kconfig | 4 ++ platform/generic/thead/objects.mk | 9 ++- platform/generic/thead/thead-generic.c | 42 +++----------- platform/generic/thead/thead-th1520.c | 55 ++++++++++++++++++ platform/generic/thead/thead_c9xx_csr.c | 30 ++++++++++ 15 files changed, 252 insertions(+), 44 deletions(-) create mode 100644 platform/generic/include/thead/c9xx_csr.h create mode 100644 platform/generic/thead/thead-th1520.c create mode 100644 platform/generic/thead/thead_c9xx_csr.c--
2.44.0--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi