Add support for T-HEAD C9xx PMU extensions
Subsystems (2)
v2 cover Add support for T-HEAD C9xx PMU extensions
The T-HEAD C9XX cores implement functionality very similar to SSCOFPMF. Instead of implementing a separate interface into SBI as done in v1, we can add the C9XX tidbits with quite minimal overhead and leaverage the existing SBI pmu interface including giving access to the firmware counters.
The current only hickup is the detection override in sbi_hart, where I still need to find out why the MHPMCOUNTERs are not writeable at _that_ point of the boot, but with this series on top of openSBI 1.1 and the matching kernel perf patch I get reasonable results for example for:
perf stat -e cycles -e instructions -e L1-icache-load-misses -e L1-icache-loads ls
Performance counter stats for 'ls':
17119496 cycles
2867765 instructions # 0.17 insn per cycle
55929 L1-icache-load-misses # 1.61% of all L1-icache accesses
3467346 L1-icache-loads 0.030156216 seconds time elapsed
0.000000000 seconds user
0.023496000 seconds sysHeiko Stuebner (5):
lib: sbi_platform: pass extension bitmap pointer to extension_init
lib: sbi: do platform-specific extension population earlier
platform: generic: add extensions_init handler and platform-override
platform: generic: allwinner: add an extension for C9xx cores
lib: sbi_pmu: add t-head pmu support
include/sbi/riscv_thead_c9xx.h | 127 +++++++++++++++++++ include/sbi/sbi_hart.h | 3 + include/sbi/sbi_platform.h | 7 +- lib/sbi/sbi_hart.c | 29 ++++- lib/sbi/sbi_pmu.c | 48 ++++++- platform/generic/allwinner/sun20i-d1.c | 10 ++ platform/generic/include/platform_override.h | 2 + platform/generic/platform.c | 9 ++ 8 files changed, 226 insertions(+), 9 deletions(-) create mode 100644 include/sbi/riscv_thead_c9xx.h
Signature · 1 line
2.35.1
PATCH v2 1/5 lib: sbi_platform: pass extension bitmap pointer to extension_init
The platform extension_init callback is called once per hart and if it's meant to handle platform-specific extensions it should have access to the extension bitmap to update it.
--- include/sbi/sbi_platform.h | 7 ++++--- lib/sbi/sbi_hart.c | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-)
include/sbi/sbi_platform.h +4 -3
@@ -90,7 +90,7 @@ struct sbi_platform_operations {int (*misa_get_xlen)(void );/** Initialize (or populate) HART extensions for the platform */ -int (*extensions_init)(void ); +int (*extensions_init)(unsigned long *extensions);/** Initialize (or populate) domains for the platform */ int (*domains_init)(void );
@@ -464,10 +464,11 @@ static inline int sbi_platform_misa_xlen(const struct sbi_platform *plat)* @return 0 on success and negative error code on failure */ static inline int sbi_platform_extensions_init ( -const struct sbi_platform *plat) +const struct sbi_platform *plat, +unsigned long *extensions) {if (plat &&sbi_platform_ops (plat)->extensions_init) -return sbi_platform_ops (plat)->extensions_init (); +return sbi_platform_ops (plat)->extensions_init (extensions);return 0 ; }
lib/sbi/sbi_hart.c +2 -1
@@ -682,7 +682,8 @@ __mhpm_skip: }/* Let platform populate extensions */ - rc =sbi_platform_extensions_init (sbi_platform_thishart_ptr ()); + rc =sbi_platform_extensions_init (sbi_platform_thishart_ptr (), + &hfeatures->extensions);if (rc)return rc;
Signature · 1 line
2.35.1
PATCH v2 2/5 lib: sbi: do platform-specific extension population earlier
In reply to Heiko StuebnerSome of the more specific detections in hart_detect_features() might need to check platform-specific extensions so might need the platform-extensions being populated earlier.
So move the call to sbi_platform_extensions_init() to an earlier place.
--- lib/sbi/sbi_hart.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
lib/sbi/sbi_hart.c +6 -6
@@ -553,6 +553,12 @@ static int hart_detect_features(struct sbi_scratch *scratch) hfeatures->pmp_count =0 ; hfeatures->mhpm_count =0 ; +/* Let platform populate extensions */ + rc =sbi_platform_extensions_init (sbi_platform_thishart_ptr (), + &hfeatures->extensions); +if (rc) +return rc; +#define __check_csr (__csr, __rdonly, __wrval, __field, __skip) \ oldval = csr_read_allowed(__csr, (ulong)&trap); \ if (!trap.cause) { \
@@ -681,12 +687,6 @@ __mhpm_skip: SBI_HART_EXT_SMSTATEEN, true); } -/* Let platform populate extensions */ - rc =sbi_platform_extensions_init (sbi_platform_thishart_ptr (), - &hfeatures->extensions); -if (rc) -return rc; -/* Mark hart feature detection done */ hfeatures->detected = true;
Signature · 1 line
2.35.1
PATCH v2 3/5 platform: generic: add extensions_init handler and platform-override
In reply to Heiko StuebnerInit of non-standard extensions is a platform-specific thing, so allow generic platforms to do this via a platform-override.
--- platform/generic/include/platform_override.h | 2 ++ platform/generic/platform.c | 9 +++++++++ 2 files changed, 11 insertions(+)
platform/generic/include/platform_override.h +2 -0
@@ -22,6 +22,8 @@ struct platform_override {void (*early_exit)(const struct fdt_match *match);void (*final_exit)(const struct fdt_match *match);int (*fdt_fixup)(void *fdt,const struct fdt_match *match); +int (*extensions_init)(unsigned long *extensions, +const struct fdt_match *match);int (*vendor_ext_check)(long extid,const struct fdt_match *match); int (*vendor_ext_provider)(long extid, long funcid, const struct sbi_trap_regs *regs,
platform/generic/platform.c +9 -0
@@ -203,6 +203,14 @@ static void generic_final_exit(void) generic_plat->final_exit (generic_plat_match); } +static int generic_extensions_init (unsigned long *extensions) +{ +if (generic_plat && generic_plat->extensions_init) +return generic_plat->extensions_init (extensions, generic_plat_match); + +return 0 ; +} +static int generic_domains_init (void ) {return fdt_domains_populate (fdt_get_address ());
@@ -248,6 +256,7 @@ const struct sbi_platform_operations platform_ops = { .final_init = generic_final_init, .early_exit = generic_early_exit, .final_exit = generic_final_exit, + .extensions_init = generic_extensions_init, .domains_init = generic_domains_init, .console_init = fdt_serial_init, .irqchip_init = fdt_irqchip_init,
Signature · 1 line
2.35.1
PATCH v2 4/5 platform: generic: allwinner: add an extension for C9xx cores
In reply to Heiko StuebnerThe T-Head c9xx cores extend the RISC-V ISA in a number of ways, both adding new instructions and also new (and extended) CSRs.
Add an extension constant for this and set it in extension-init.
--- include/sbi/riscv_thead_c9xx.h | 127 +++++++++++++++++++++++++ include/sbi/sbi_hart.h | 3 + lib/sbi/sbi_hart.c | 3 + platform/generic/allwinner/sun20i-d1.c | 10 ++ 4 files changed, 143 insertions(+) create mode 100644 include/sbi/riscv_thead_c9xx.h
include/sbi/riscv_thead_c9xx.h new file +127 -0
@@ -0,0 +1,127 @@ +#ifndef __RISCV_THEAD_C9XX_H____ +#define __RISCV_THEAD_C9XX_H____ + +/* T-HEAD C9xx M mode CSR. */ +#define THEAD_C9XX_CSR_MXSTATUS 0x7c0 +#define THEAD_C9XX_CSR_MHCR 0x7c1 +#define THEAD_C9XX_CSR_MCOR 0x7c2 +#define THEAD_C9XX_CSR_MCCR2 0x7c3 +#define THEAD_C9XX_CSR_MCER2 0x7c4 +#define THEAD_C9XX_CSR_MHINT 0x7c5 +#define THEAD_C9XX_CSR_MRMR 0x7c6 +#define THEAD_C9XX_CSR_MRVBR 0x7c7 +#define THEAD_C9XX_CSR_MCER 0x7c8 +#define THEAD_C9XX_CSR_MCOUNTERWEN 0x7c9 +#define THEAD_C9XX_CSR_MCOUNTERINTEN 0x7ca +#define THEAD_C9XX_CSR_MCOUNTEROF 0x7cb +#define THEAD_C9XX_CSR_MHINT2 0x7cc +#define THEAD_C9XX_CSR_MHINT3 0x7cd +#define THEAD_C9XX_CSR_MRADDR 0x7e0 +#define THEAD_C9XX_CSR_MEXSTATUS 0x7e1 +#define THEAD_C9XX_CSR_MNMICAUSE 0x7e2 +#define THEAD_C9XX_CSR_MNMIPC 0x7e3 +#define THEAD_C9XX_CSR_MHPMCR 0x7f0 +#define THEAD_C9XX_CSR_MHPMSR 0x7f1 +#define THEAD_C9XX_CSR_MHPMER 0x7f2 +#define THEAD_C9XX_CSR_MSMPR 0x7f3 +#define THEAD_C9XX_CSR_MTEECFG 0x7f4 +#define THEAD_C9XX_CSR_MZONEID 0x7f5 +#define THEAD_C9XX_CSR_ML2CPID 0x7f6 +#define THEAD_C9XX_CSR_ML2WP 0x7f7 +#define THEAD_C9XX_CSR_MDTCMCR 0x7f8 +#define THEAD_C9XX_CSR_USP 0x7d1 +#define THEAD_C9XX_CSR_MCINS 0x7d2 +#define THEAD_C9XX_CSR_MCINDEX 0x7d3 +#define THEAD_C9XX_CSR_MCDATA0 0x7d4 +#define THEAD_C9XX_CSR_MCDATA1 0x7d5 +#define THEAD_C9XX_CSR_MEICR 0x7d6 +#define THEAD_C9XX_CSR_MEICR2 0x7d7 +#define THEAD_C9XX_CSR_MBEADDR 0x7d8 +#define THEAD_C9XX_CSR_MCPUID 0xfc0 +#define THEAD_C9XX_CSR_MAPBADDR 0xfc1 +#define THEAD_C9XX_CSR_MWMSR 0xfc2 +#define THEAD_C9XX_CSR_MHALTCAUSE 0xfe0 +#define THEAD_C9XX_CSR_MDBGINFO 0xfe1 +#define THEAD_C9XX_CSR_MPCFIFO 0xfe2 + +/* T-HEAD C9xx S mode CSR. */ +#define THEAD_C9XX_CSR_SXSTATUS 0x5c0 +#define THEAD_C9XX_CSR_SHCR 0x5c1 +#define THEAD_C9XX_CSR_SCER2 0x5c2 +#define THEAD_C9XX_CSR_SCER 0x5c3 +#define THEAD_C9XX_CSR_SCOUNTERINTEN 0x5c4 +#define THEAD_C9XX_CSR_SCOUNTEROF 0x5c5 +#define THEAD_C9XX_CSR_SHINT 0x5c6 +#define THEAD_C9XX_CSR_SHINT2 0x5c7 +#define THEAD_C9XX_CSR_SHPMINHIBIT 0x5c8 +#define THEAD_C9XX_CSR_SHPMCR 0x5c9 +#define THEAD_C9XX_CSR_SHPMSR 0x5ca +#define THEAD_C9XX_CSR_SHPMER 0x5cb +#define THEAD_C9XX_CSR_SL2CPID 0x5cc +#define THEAD_C9XX_CSR_SL2WP 0x5cd +#define THEAD_C9XX_CSR_SBEADDR 0x5d0 +#define THEAD_C9XX_CSR_SCYCLE 0x5e0 +#define THEAD_C9XX_CSR_SHPMCOUNTER1 0x5e1 +#define THEAD_C9XX_CSR_SHPMCOUNTER2 0x5e2 +#define THEAD_C9XX_CSR_SHPMCOUNTER3 0x5e3 +#define THEAD_C9XX_CSR_SHPMCOUNTER4 0x5e4 +#define THEAD_C9XX_CSR_SHPMCOUNTER5 0x5e5 +#define THEAD_C9XX_CSR_SHPMCOUNTER6 0x5e6 +#define THEAD_C9XX_CSR_SHPMCOUNTER7 0x5e7 +#define THEAD_C9XX_CSR_SHPMCOUNTER8 0x5e8 +#define THEAD_C9XX_CSR_SHPMCOUNTER9 0x5e9 +#define THEAD_C9XX_CSR_SHPMCOUNTER10 0x5ea +#define THEAD_C9XX_CSR_SHPMCOUNTER11 0x5eb +#define THEAD_C9XX_CSR_SHPMCOUNTER12 0x5ec +#define THEAD_C9XX_CSR_SHPMCOUNTER13 0x5ed +#define THEAD_C9XX_CSR_SHPMCOUNTER14 0x5ee +#define THEAD_C9XX_CSR_SHPMCOUNTER15 0x5ef +#define THEAD_C9XX_CSR_SHPMCOUNTER16 0x5f0 +#define THEAD_C9XX_CSR_SHPMCOUNTER17 0x5f1 +#define THEAD_C9XX_CSR_SHPMCOUNTER18 0x5f2 +#define THEAD_C9XX_CSR_SHPMCOUNTER19 0x5f3 +#define THEAD_C9XX_CSR_SHPMCOUNTER20 0x5f4 +#define THEAD_C9XX_CSR_SHPMCOUNTER21 0x5f5 +#define THEAD_C9XX_CSR_SHPMCOUNTER22 0x5f6 +#define THEAD_C9XX_CSR_SHPMCOUNTER23 0x5f7 +#define THEAD_C9XX_CSR_SHPMCOUNTER24 0x5f8 +#define THEAD_C9XX_CSR_SHPMCOUNTER25 0x5f9 +#define THEAD_C9XX_CSR_SHPMCOUNTER26 0x5fa +#define THEAD_C9XX_CSR_SHPMCOUNTER27 0x5fb +#define THEAD_C9XX_CSR_SHPMCOUNTER28 0x5fc +#define THEAD_C9XX_CSR_SHPMCOUNTER29 0x5fd +#define THEAD_C9XX_CSR_SHPMCOUNTER30 0x5fe +#define THEAD_C9XX_CSR_SHPMCOUNTER31 0x5ff + +/* T-HEAD C9xx U mode CSR. */ +#define THEAD_C9XX_CSR_FXCR 0x800 + +/* T-HEAD C9xx MMU extentions. */ +#define THEAD_C9XX_CSR_SMIR 0x9c0 +#define THEAD_C9XX_CSR_SMEL 0x9c1 +#define THEAD_C9XX_CSR_SMEH 0x9c2 +#define THEAD_C9XX_CSR_SMCIR 0x9c3 + +/* T-HEAD C9xx Security CSR(May be droped). */ +#define THEAD_C9XX_CSR_MEBR 0xbe0 +#define THEAD_C9XX_CSR_NT_MSTATUS 0xbe1 +#define THEAD_C9XX_CSR_NT_MIE 0xbe2 +#define THEAD_C9XX_CSR_NT_MTVEC 0xbe3 +#define THEAD_C9XX_CSR_NT_MTVT 0xbe4 +#define THEAD_C9XX_CSR_NT_MEPC 0xbe5 +#define THEAD_C9XX_CSR_NT_MCAUSE 0xbe6 +#define THEAD_C9XX_CSR_NT_MIP 0xbe7 +#define THEAD_C9XX_CSR_NT_MINTSTATE 0xbe8 +#define THEAD_C9XX_CSR_NT_MXSTATUS 0xbe9 +#define THEAD_C9XX_CSR_NT_MEBR 0xbea +#define THEAD_C9XX_CSR_NT_MSP 0xbeb +#define THEAD_C9XX_CSR_T_USP 0xbec +#define THEAD_C9XX_CSR_T_MDCR 0xbed +#define THEAD_C9XX_CSR_T_MPCR 0xbee +#define THEAD_C9XX_CSR_PMPTEECFG 0xbef + +/* T-HEAD C9xx MIP CSR extension */ +#define THEAD_C9XX_IRQ_PMU_OVF 17 +#define THEAD_C9XX_MIP_MOIP (_UL(1) << THEAD_C9XX_IRQ_PMU_OVF) + +#endif
include/sbi/sbi_hart.h +3 -0
@@ -37,6 +37,9 @@ enum sbi_hart_extensions {/** HART has Sstc extension */ SBI_HART_EXT_SSTC , +/** HART is a C9xx core with its ISA extensions */ +SBI_HART_EXT_THEAD_C9XX , +/** Maximum index of Hart extension */ SBI_HART_EXT_MAX, };
lib/sbi/sbi_hart.c +3 -0
@@ -434,6 +434,9 @@ static inline char *sbi_hart_extension_id2string(int ext)case SBI_HART_EXT_SMSTATEEN : estr ="smstateen" ;break ; +case SBI_HART_EXT_THEAD_C9XX : + estr ="c9xx" ; +break ;default :break ; }
platform/generic/allwinner/sun20i-d1.c +10 -0
@@ -9,6 +9,7 @@#include <sbi/sbi_bitops.h> #include <sbi/sbi_ecall_interface.h> #include <sbi/sbi_error.h> +#include <sbi/sbi_hart.h> #include <sbi/sbi_hsm.h> #include <sbi_utils/fdt/fdt_helper.h> #include <sbi_utils/irqchip/fdt_irqchip_plic.h>
@@ -199,6 +200,14 @@ static int sun20i_d1_final_init(bool cold_boot, const struct fdt_match *match)return 0 ; } +static int sun20i_d1_extensions_init (unsigned long *extensions, +const struct fdt_match *match) +{ + *extensions |=BIT (SBI_HART_EXT_THEAD_C9XX ); + +return 0 ; +} +static const struct fdt_match sun20i_d1_match[] = { { .compatible ="allwinner,sun20i-d1" }, { },
@@ -207,4 +216,5 @@ static const struct fdt_match sun20i_d1_match[] = {const struct platform_override sun20i_d1 = { .match_table = sun20i_d1_match, .final_init = sun20i_d1_final_init, + .extensions_init = sun20i_d1_extensions_init, };
Signature · 1 line
2.35.1
PATCH v2 5/5 lib: sbi_pmu: add t-head pmu support
In reply to Heiko StuebnerWith the T-HEAD C9XX cores being designed before or during ratification of the SSCOFPMF extension, they implement a PMU extension that behaves very similar but not equal to it by providing overflow interrupts though in a slightly different registers format.
Use the presence of C9XX extension to adapt the pmu's behaviour accordingly to enable the C9XX to leverage all the features the SBI pmu interface provides.
--- lib/sbi/sbi_hart.c | 15 +++++++++++++++ lib/sbi/sbi_pmu.c | 48 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-)
lib/sbi/sbi_hart.c +15 -0
@@ -11,6 +11,7 @@#include <sbi/riscv_barrier.h> #include <sbi/riscv_encoding.h> #include <sbi/riscv_fp.h> +#include <sbi/riscv_thead_c9xx.h> #include <sbi/sbi_bitops.h> #include <sbi/sbi_console.h> #include <sbi/sbi_domain.h>
@@ -210,6 +211,8 @@ static int delegate_traps(struct sbi_scratch *scratch) interrupts =MIP_SSIP |MIP_STIP |MIP_SEIP ;if (sbi_hart_has_extension (scratch,SBI_HART_EXT_SSCOFPMF )) interrupts |=MIP_LCOFIP ; +else if (sbi_hart_has_extension (scratch,SBI_HART_EXT_THEAD_C9XX )) + interrupts |=THEAD_C9XX_MIP_MOIP ; exceptions = (1U <<CAUSE_MISALIGNED_FETCH ) | (1U <<CAUSE_BREAKPOINT ) | (1U <<CAUSE_USER_ECALL );
@@ -636,6 +639,18 @@ __mhpm_skip:#undef __check_csr_2#undef __check_csr +/** +* On T-HEAD C9xx cores csr_swap on MHPMCOUNTERx behaves differently +* which is a result of the csr_write not actually changing the value +*@this time. Later on during pmu operation, setting MHPMCOUNTERx +* values is possible, but so far it's unknown which setting changes +* that. So for now override the settings statically. +*/ +if (sbi_hart_has_extension (scratch,SBI_HART_EXT_THEAD_C9XX )) { + hfeatures->mhpm_count =29 ; + hfeatures->mhpm_bits =64 ; + } +/* Detect if hart supports Priv v1.10 */ val =csr_read_allowed (CSR_MCOUNTEREN , (unsigned long )&trap);if (!trap.cause)
lib/sbi/sbi_pmu.c +47 -1
74 lines not on this page · raw message
Signature · 1 line
2.35.1
v2 cover Add support for T-HEAD C9xx PMU extensions
In reply to Heiko StuebnerQuoted 7 lines · Heiko Stuebner
The T-HEAD C9XX cores implement functionality very similar to SSCOFPMF. Instead of implementing a separate interface into SBI as done in v1, we can add the C9XX tidbits with quite minimal overhead and leaverage the existing SBI pmu interface including giving access to the firmware counters.
That's great. But modifying generic code for a vendor errata is not a good idea as I pointed out
in the kernel patch as well. Instead of that, can you please define pmu_ops so that platforms
can override those and have a clean implementation ?
You won't need a HART extension any more. Technically, this is not a valid extension also.
Quoted 46 lines
The current only hickup is the detection override in
sbi_hart, where I still need to find out why the MHPMCOUNTERs are not writeable at _that_ point of the boot, but with this series on top of openSBI 1.1 and the matching kernel perf patch I get reasonable results for example for:perf stat -e cycles -e instructions -e L1-icache-load-misses -e L1-icache-loads ls
Performance counter stats for 'ls':
17119496 cycles 2867765 instructions # 0.17 insn per cycle 55929 L1-icache-load-misses # 1.61% of all L1-icache accesses 3467346 L1-icache-loads0.030156216 seconds time elapsed 0.000000000 seconds user 0.023496000 seconds sysHeiko Stuebner (5):
lib:sbi_platform: pass extension bitmap pointer toextension_init
lib: sbi: do platform-specific extension population earlier
platform: generic: addextensions_inithandler and platform-override
platform: generic: allwinner: add an extension for C9xx cores
lib:sbi_pmu: add t-head pmu supportinclude/sbi/riscv_thead_c9xx.h | 127 +++++++++++++++++++ include/sbi/sbi_hart.h | 3 + include/sbi/sbi_platform.h | 7 +- lib/sbi/sbi_hart.c | 29 ++++- lib/sbi/sbi_pmu.c | 48 ++++++- platform/generic/allwinner/sun20i-d1.c | 10 ++ platform/generic/include/platform_override.h | 2 + platform/generic/platform.c | 9 ++ 8 files changed, 226 insertions(+), 9 deletions(-) create mode 100644 include/sbi/riscv_thead_c9xx.h--
2.35.1--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
Signature · 2 lines
Regards, Atish