lib: sbi: Convert hart features into hart extensions

Since past few years, we have been using "hart features" in OpenSBI
to represent all optionalities and multi-letter extensions defined
by the RISC-V specifications.

The RISC-V profiles specification has taken a different approach and
started assigning extension names for all optionalities which did not
have any extension name previously.
(Refer, https://github.com/riscv/riscv-profiles/blob/main/profiles.adoc)

Inspired from the RISC-V profiles specification, we convert OpenSBI
hart features into hart extensions. Going forward, we align the
extension naming with RISC-V profiles specification. Currently, only
"time CSR" and "AIA CSR" have not been assigned extension name but
for everything else we have a name.

Signed-off-by: Anup Patel <apatel@ventanamicro.com>
Reviewed-by: Atish Patra <atishp@rivosinc.com>
This commit is contained in:
Anup Patel
2022-04-28 21:29:22 +05:30
committed by Anup Patel
parent a6ab94fdbf
commit cad6c91045
7 changed files with 86 additions and 93 deletions

View File

@@ -24,21 +24,21 @@ enum sbi_hart_priv_versions {
SBI_HART_PRIV_VER_1_12 = 3, SBI_HART_PRIV_VER_1_12 = 3,
}; };
/** Possible feature flags of a hart */ /** Possible ISA extensions of a hart */
enum sbi_hart_features { enum sbi_hart_extensions {
/** Hart has sscofpmf extension */ /** Hart has Sscofpmt extension */
SBI_HART_HAS_SSCOFPMF = (1 << 0), SBI_HART_EXT_SSCOFPMF = 0,
/** HART has timer csr implementation in hardware */ /** HART has HW time CSR (extension name not available) */
SBI_HART_HAS_TIME = (1 << 1), SBI_HART_EXT_TIME,
/** HART has AIA local interrupt CSRs */ /** HART has AIA CSRs (extension name not available) */
SBI_HART_HAS_AIA = (1 << 2), SBI_HART_EXT_AIA,
/** HART has mstateen CSR **/ /** HART has Smstateen CSR **/
SBI_HART_HAS_SMSTATEEN = (1 << 3), SBI_HART_EXT_SMSTATEEN,
/** HART has SSTC extension implemented in hardware */ /** HART has Sstc extension */
SBI_HART_HAS_SSTC = (1 << 4), SBI_HART_EXT_SSTC,
/** Last index of Hart features*/ /** Maximum index of Hart extension */
SBI_HART_HAS_LAST_FEATURE = SBI_HART_HAS_SSTC, SBI_HART_EXT_MAX,
}; };
struct sbi_scratch; struct sbi_scratch;
@@ -63,9 +63,10 @@ int sbi_hart_pmp_configure(struct sbi_scratch *scratch);
int sbi_hart_priv_version(struct sbi_scratch *scratch); int sbi_hart_priv_version(struct sbi_scratch *scratch);
void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch, void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch,
char *version_str, int nvstr); char *version_str, int nvstr);
bool sbi_hart_has_feature(struct sbi_scratch *scratch, unsigned long feature); bool sbi_hart_has_extension(struct sbi_scratch *scratch,
void sbi_hart_get_features_str(struct sbi_scratch *scratch, enum sbi_hart_extensions ext);
char *features_str, int nfstr); void sbi_hart_get_extensions_str(struct sbi_scratch *scratch,
char *extension_str, int nestr);
void __attribute__((noreturn)) sbi_hart_hang(void); void __attribute__((noreturn)) sbi_hart_hang(void);

View File

@@ -29,7 +29,7 @@ void (*sbi_hart_expected_trap)(void) = &__sbi_expected_trap;
struct hart_features { struct hart_features {
int priv_version; int priv_version;
unsigned long features; unsigned long extensions;
unsigned int pmp_count; unsigned int pmp_count;
unsigned int pmp_addr_bits; unsigned int pmp_addr_bits;
unsigned long pmp_gran; unsigned long pmp_gran;
@@ -83,7 +83,7 @@ static void mstatus_init(struct sbi_scratch *scratch)
for (cidx = 0; cidx < num_mhpm; cidx++) { for (cidx = 0; cidx < num_mhpm; cidx++) {
#if __riscv_xlen == 32 #if __riscv_xlen == 32
csr_write_num(CSR_MHPMEVENT3 + cidx, mhpmevent_init_val & 0xFFFFFFFF); csr_write_num(CSR_MHPMEVENT3 + cidx, mhpmevent_init_val & 0xFFFFFFFF);
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF)) if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
csr_write_num(CSR_MHPMEVENT3H + cidx, csr_write_num(CSR_MHPMEVENT3H + cidx,
mhpmevent_init_val >> BITS_PER_LONG); mhpmevent_init_val >> BITS_PER_LONG);
#else #else
@@ -91,7 +91,7 @@ static void mstatus_init(struct sbi_scratch *scratch)
#endif #endif
} }
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_SMSTATEEN)) { if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SMSTATEEN)) {
mstateen_val = csr_read(CSR_MSTATEEN0); mstateen_val = csr_read(CSR_MSTATEEN0);
#if __riscv_xlen == 32 #if __riscv_xlen == 32
mstateen_val |= ((uint64_t)csr_read(CSR_MSTATEEN0H)) << 32; mstateen_val |= ((uint64_t)csr_read(CSR_MSTATEEN0H)) << 32;
@@ -99,7 +99,7 @@ static void mstatus_init(struct sbi_scratch *scratch)
mstateen_val |= SMSTATEEN_STATEN; mstateen_val |= SMSTATEEN_STATEN;
mstateen_val |= SMSTATEEN0_HSENVCFG; mstateen_val |= SMSTATEEN0_HSENVCFG;
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_AIA)) if (sbi_hart_has_extension(scratch, SBI_HART_EXT_AIA))
mstateen_val |= (SMSTATEEN0_AIA | SMSTATEEN0_SVSLCT | mstateen_val |= (SMSTATEEN0_AIA | SMSTATEEN0_SVSLCT |
SMSTATEEN0_IMSIC); SMSTATEEN0_IMSIC);
else else
@@ -153,7 +153,7 @@ static void mstatus_init(struct sbi_scratch *scratch)
* Enable access to stimecmp if sstc extension is present in the * Enable access to stimecmp if sstc extension is present in the
* hardware. * hardware.
*/ */
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_SSTC)) { if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSTC)) {
#if __riscv_xlen == 32 #if __riscv_xlen == 32
unsigned long menvcfgh_val; unsigned long menvcfgh_val;
menvcfgh_val = csr_read(CSR_MENVCFGH); menvcfgh_val = csr_read(CSR_MENVCFGH);
@@ -207,7 +207,7 @@ static int delegate_traps(struct sbi_scratch *scratch)
/* Send M-mode interrupts and most exceptions to S-mode */ /* Send M-mode interrupts and most exceptions to S-mode */
interrupts = MIP_SSIP | MIP_STIP | MIP_SEIP; interrupts = MIP_SSIP | MIP_STIP | MIP_SEIP;
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF)) if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
interrupts |= MIP_LCOFIP; interrupts |= MIP_LCOFIP;
exceptions = (1U << CAUSE_MISALIGNED_FETCH) | (1U << CAUSE_BREAKPOINT) | exceptions = (1U << CAUSE_MISALIGNED_FETCH) | (1U << CAUSE_BREAKPOINT) |
@@ -367,102 +367,94 @@ void sbi_hart_get_priv_version_str(struct sbi_scratch *scratch,
} }
/** /**
* Check whether a particular hart feature is available * Check whether a particular hart extension is available
* *
* @param scratch pointer to the HART scratch space * @param scratch pointer to the HART scratch space
* @param feature the feature to check * @param ext the extension number to check
* @returns true (feature available) or false (feature not available) * @returns true (available) or false (not available)
*/ */
bool sbi_hart_has_feature(struct sbi_scratch *scratch, unsigned long feature) bool sbi_hart_has_extension(struct sbi_scratch *scratch,
enum sbi_hart_extensions ext)
{ {
struct hart_features *hfeatures = struct hart_features *hfeatures =
sbi_scratch_offset_ptr(scratch, hart_features_offset); sbi_scratch_offset_ptr(scratch, hart_features_offset);
if (hfeatures->features & feature) if (hfeatures->extensions & BIT(ext))
return true; return true;
else else
return false; return false;
} }
static unsigned long hart_get_features(struct sbi_scratch *scratch) static inline char *sbi_hart_extension_id2string(int ext)
{ {
struct hart_features *hfeatures = char *estr = NULL;
sbi_scratch_offset_ptr(scratch, hart_features_offset);
return hfeatures->features; switch (ext) {
} case SBI_HART_EXT_SSCOFPMF:
estr = "sscofpmf";
static inline char *sbi_hart_feature_id2string(unsigned long feature)
{
char *fstr = NULL;
if (!feature)
return NULL;
switch (feature) {
case SBI_HART_HAS_SSCOFPMF:
fstr = "sscofpmf";
break; break;
case SBI_HART_HAS_TIME: case SBI_HART_EXT_TIME:
fstr = "time"; estr = "time";
break; break;
case SBI_HART_HAS_AIA: case SBI_HART_EXT_AIA:
fstr = "aia"; estr = "aia";
break; break;
case SBI_HART_HAS_SSTC: case SBI_HART_EXT_SSTC:
fstr = "sstc"; estr = "sstc";
break; break;
case SBI_HART_HAS_SMSTATEEN: case SBI_HART_EXT_SMSTATEEN:
fstr = "smstateen"; estr = "smstateen";
break; break;
default: default:
break; break;
} }
return fstr; return estr;
} }
/** /**
* Get the hart features in string format * Get the hart extensions in string format
* *
* @param scratch pointer to the HART scratch space * @param scratch pointer to the HART scratch space
* @param features_str pointer to a char array where the features string will be * @param extensions_str pointer to a char array where the extensions string
* updated * will be updated
* @param nfstr length of the features_str. The feature string will be truncated * @param nestr length of the features_str. The feature string will be
* if nfstr is not long enough. * truncated if nestr is not long enough.
*/ */
void sbi_hart_get_features_str(struct sbi_scratch *scratch, void sbi_hart_get_extensions_str(struct sbi_scratch *scratch,
char *features_str, int nfstr) char *extensions_str, int nestr)
{ {
unsigned long features, feat = 1UL; struct hart_features *hfeatures =
sbi_scratch_offset_ptr(scratch, hart_features_offset);
int offset = 0, ext = 0;
char *temp; char *temp;
int offset = 0;
if (!features_str || nfstr <= 0) if (!extensions_str || nestr <= 0)
return; return;
sbi_memset(features_str, 0, nfstr); sbi_memset(extensions_str, 0, nestr);
features = hart_get_features(scratch); if (!hfeatures->extensions)
if (!features)
goto done; goto done;
do { do {
if (features & feat) { if (hfeatures->extensions & BIT(ext)) {
temp = sbi_hart_feature_id2string(feat); temp = sbi_hart_extension_id2string(ext);
if (temp) { if (temp) {
sbi_snprintf(features_str + offset, nfstr, sbi_snprintf(extensions_str + offset,
nestr - offset,
"%s,", temp); "%s,", temp);
offset = offset + sbi_strlen(temp) + 1; offset = offset + sbi_strlen(temp) + 1;
} }
} }
feat = feat << 1;
} while (feat <= SBI_HART_HAS_LAST_FEATURE); ext++;
} while (ext < SBI_HART_EXT_MAX);
done: done:
if (offset) if (offset)
features_str[offset - 1] = '\0'; extensions_str[offset - 1] = '\0';
else else
sbi_strncpy(features_str, "none", nfstr); sbi_strncpy(extensions_str, "none", nestr);
} }
static unsigned long hart_pmp_get_allowed_addr(void) static unsigned long hart_pmp_get_allowed_addr(void)
@@ -523,7 +515,7 @@ static void hart_detect_features(struct sbi_scratch *scratch)
/* Reset hart features */ /* Reset hart features */
hfeatures = sbi_scratch_offset_ptr(scratch, hart_features_offset); hfeatures = sbi_scratch_offset_ptr(scratch, hart_features_offset);
hfeatures->features = 0; hfeatures->extensions = 0;
hfeatures->pmp_count = 0; hfeatures->pmp_count = 0;
hfeatures->mhpm_count = 0; hfeatures->mhpm_count = 0;
@@ -623,31 +615,31 @@ __mhpm_skip:
/* Detect if hart supports sscofpmf */ /* Detect if hart supports sscofpmf */
csr_read_allowed(CSR_SCOUNTOVF, (unsigned long)&trap); csr_read_allowed(CSR_SCOUNTOVF, (unsigned long)&trap);
if (!trap.cause) if (!trap.cause)
hfeatures->features |= SBI_HART_HAS_SSCOFPMF; hfeatures->extensions |= BIT(SBI_HART_EXT_SSCOFPMF);
} }
/* Detect if hart supports time CSR */ /* Detect if hart supports time CSR */
csr_read_allowed(CSR_TIME, (unsigned long)&trap); csr_read_allowed(CSR_TIME, (unsigned long)&trap);
if (!trap.cause) if (!trap.cause)
hfeatures->features |= SBI_HART_HAS_TIME; hfeatures->extensions |= BIT(SBI_HART_EXT_TIME);
/* Detect if hart has AIA local interrupt CSRs */ /* Detect if hart has AIA local interrupt CSRs */
csr_read_allowed(CSR_MTOPI, (unsigned long)&trap); csr_read_allowed(CSR_MTOPI, (unsigned long)&trap);
if (!trap.cause) if (!trap.cause)
hfeatures->features |= SBI_HART_HAS_AIA; hfeatures->extensions |= BIT(SBI_HART_EXT_AIA);
/* Detect if hart supports stimecmp CSR(Sstc extension) */ /* Detect if hart supports stimecmp CSR(Sstc extension) */
if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_12) { if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_12) {
csr_read_allowed(CSR_STIMECMP, (unsigned long)&trap); csr_read_allowed(CSR_STIMECMP, (unsigned long)&trap);
if (!trap.cause) if (!trap.cause)
hfeatures->features |= SBI_HART_HAS_SSTC; hfeatures->extensions |= BIT(SBI_HART_EXT_SSTC);
} }
/* Detect if hart supports mstateen CSRs */ /* Detect if hart supports mstateen CSRs */
if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_12) { if (hfeatures->priv_version >= SBI_HART_PRIV_VER_1_12) {
val = csr_read_allowed(CSR_MSTATEEN0, (unsigned long)&trap); val = csr_read_allowed(CSR_MSTATEEN0, (unsigned long)&trap);
if (!trap.cause) if (!trap.cause)
hfeatures->features |= SBI_HART_HAS_SMSTATEEN; hfeatures->extensions |= BIT(SBI_HART_EXT_SMSTATEEN);
} }
return; return;

View File

@@ -143,8 +143,8 @@ static void sbi_boot_print_hart(struct sbi_scratch *scratch, u32 hartid)
sbi_printf("Boot HART Priv Version : %s\n", str); sbi_printf("Boot HART Priv Version : %s\n", str);
misa_string(xlen, str, sizeof(str)); misa_string(xlen, str, sizeof(str));
sbi_printf("Boot HART Base ISA : %s\n", str); sbi_printf("Boot HART Base ISA : %s\n", str);
sbi_hart_get_features_str(scratch, str, sizeof(str)); sbi_hart_get_extensions_str(scratch, str, sizeof(str));
sbi_printf("Boot HART Features : %s\n", str); sbi_printf("Boot HART ISA Extensions : %s\n", str);
sbi_printf("Boot HART PMP Count : %d\n", sbi_printf("Boot HART PMP Count : %d\n",
sbi_hart_pmp_count(scratch)); sbi_hart_pmp_count(scratch));
sbi_printf("Boot HART PMP Granularity : %lu\n", sbi_printf("Boot HART PMP Granularity : %lu\n",

View File

@@ -313,7 +313,7 @@ static int pmu_ctr_start_hw(uint32_t cidx, uint64_t ival, bool ival_update)
__clear_bit(cidx, &mctr_inhbt); __clear_bit(cidx, &mctr_inhbt);
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF)) if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
pmu_ctr_enable_irq_hw(cidx); pmu_ctr_enable_irq_hw(cidx);
csr_write(CSR_MCOUNTINHIBIT, mctr_inhbt); csr_write(CSR_MCOUNTINHIBIT, mctr_inhbt);
@@ -404,8 +404,8 @@ static int pmu_reset_hw_mhpmevent(int ctr_idx)
return SBI_EFAIL; return SBI_EFAIL;
#if __riscv_xlen == 32 #if __riscv_xlen == 32
csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, 0); csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, 0);
if (sbi_hart_has_feature(sbi_scratch_thishart_ptr(), if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
SBI_HART_HAS_SSCOFPMF)) SBI_HART_EXT_SSCOFPMF))
csr_write_num(CSR_MHPMEVENT3H + ctr_idx - 3, 0); csr_write_num(CSR_MHPMEVENT3H + ctr_idx - 3, 0);
#else #else
csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, 0); csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, 0);
@@ -476,7 +476,7 @@ static int pmu_update_hw_mhpmevent(struct sbi_pmu_hw_event *hw_evt, int ctr_idx,
* Always set the OVF bit(disable interrupts) and inhibit counting of * Always set the OVF bit(disable interrupts) and inhibit counting of
* events in M-mode. The OVF bit should be enabled during the start call. * events in M-mode. The OVF bit should be enabled during the start call.
*/ */
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF)) if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
mhpmevent_val = (mhpmevent_val & ~MHPMEVENT_SSCOF_MASK) | mhpmevent_val = (mhpmevent_val & ~MHPMEVENT_SSCOF_MASK) |
MHPMEVENT_MINH | MHPMEVENT_OF; MHPMEVENT_MINH | MHPMEVENT_OF;
@@ -485,7 +485,7 @@ static int pmu_update_hw_mhpmevent(struct sbi_pmu_hw_event *hw_evt, int ctr_idx,
#if __riscv_xlen == 32 #if __riscv_xlen == 32
csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, mhpmevent_val & 0xFFFFFFFF); csr_write_num(CSR_MHPMEVENT3 + ctr_idx - 3, mhpmevent_val & 0xFFFFFFFF);
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF)) if (sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
csr_write_num(CSR_MHPMEVENT3H + ctr_idx - 3, csr_write_num(CSR_MHPMEVENT3H + ctr_idx - 3,
mhpmevent_val >> BITS_PER_LONG); mhpmevent_val >> BITS_PER_LONG);
#else #else
@@ -525,7 +525,7 @@ static int pmu_ctr_find_hw(unsigned long cbase, unsigned long cmask, unsigned lo
*/ */
fixed_ctr = pmu_ctr_find_fixed_fw(event_idx); fixed_ctr = pmu_ctr_find_fixed_fw(event_idx);
if (fixed_ctr >= 0 && if (fixed_ctr >= 0 &&
!sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF)) !sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
return fixed_ctr; return fixed_ctr;
if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_11) if (sbi_hart_priv_version(scratch) >= SBI_HART_PRIV_VER_1_11)

View File

@@ -129,7 +129,7 @@ void sbi_timer_event_start(u64 next_event)
* Update the stimecmp directly if available. This allows * Update the stimecmp directly if available. This allows
* the older software to leverage sstc extension on newer hardware. * the older software to leverage sstc extension on newer hardware.
*/ */
if (sbi_hart_has_feature(sbi_scratch_thishart_ptr(), SBI_HART_HAS_SSTC)) { if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(), SBI_HART_EXT_SSTC)) {
#if __riscv_xlen == 32 #if __riscv_xlen == 32
csr_write(CSR_STIMECMP, next_event & 0xFFFFFFFF); csr_write(CSR_STIMECMP, next_event & 0xFFFFFFFF);
csr_write(CSR_STIMECMPH, next_event >> 32); csr_write(CSR_STIMECMPH, next_event >> 32);
@@ -151,7 +151,7 @@ void sbi_timer_process(void)
* directly without M-mode come in between. This function should * directly without M-mode come in between. This function should
* only invoked if M-mode programs the timer for its own purpose. * only invoked if M-mode programs the timer for its own purpose.
*/ */
if (!sbi_hart_has_feature(sbi_scratch_thishart_ptr(), SBI_HART_HAS_SSTC)) if (!sbi_hart_has_extension(sbi_scratch_thishart_ptr(), SBI_HART_EXT_SSTC))
csr_set(CSR_MIP, MIP_STIP); csr_set(CSR_MIP, MIP_STIP);
} }
@@ -180,7 +180,7 @@ int sbi_timer_init(struct sbi_scratch *scratch, bool cold_boot)
if (!time_delta_off) if (!time_delta_off)
return SBI_ENOMEM; return SBI_ENOMEM;
if (sbi_hart_has_feature(scratch, SBI_HART_HAS_TIME)) if (sbi_hart_has_extension(scratch, SBI_HART_EXT_TIME))
get_time_val = get_ticks; get_time_val = get_ticks;
} else { } else {
if (!time_delta_off) if (!time_delta_off)

View File

@@ -272,8 +272,8 @@ struct sbi_trap_regs *sbi_trap_handler(struct sbi_trap_regs *regs)
} }
if (mcause & (1UL << (__riscv_xlen - 1))) { if (mcause & (1UL << (__riscv_xlen - 1))) {
if (sbi_hart_has_feature(sbi_scratch_thishart_ptr(), if (sbi_hart_has_extension(sbi_scratch_thishart_ptr(),
SBI_HART_HAS_AIA)) SBI_HART_EXT_AIA))
rc = sbi_trap_aia_irq(regs, mcause); rc = sbi_trap_aia_irq(regs, mcause);
else else
rc = sbi_trap_nonaia_irq(regs, mcause); rc = sbi_trap_nonaia_irq(regs, mcause);

View File

@@ -53,7 +53,7 @@ int fdt_pmu_fixup(void *fdt)
fdt_delprop(fdt, pmu_offset, "riscv,event-to-mhpmcounters"); fdt_delprop(fdt, pmu_offset, "riscv,event-to-mhpmcounters");
fdt_delprop(fdt, pmu_offset, "riscv,event-to-mhpmevent"); fdt_delprop(fdt, pmu_offset, "riscv,event-to-mhpmevent");
fdt_delprop(fdt, pmu_offset, "riscv,raw-event-to-mhpmcounters"); fdt_delprop(fdt, pmu_offset, "riscv,raw-event-to-mhpmcounters");
if (!sbi_hart_has_feature(scratch, SBI_HART_HAS_SSCOFPMF)) if (!sbi_hart_has_extension(scratch, SBI_HART_EXT_SSCOFPMF))
fdt_delprop(fdt, pmu_offset, "interrupts-extended"); fdt_delprop(fdt, pmu_offset, "interrupts-extended");
return 0; return 0;