miscellaneous about sbi_dtbr
v3 cover miscellaneous about sbi_dtbr
v2 changes:
- fix shmem_base checking in PATCH2
Xiang W (6):
lib: sbi: fix hart_shmem_base
lib: sbi: Removal unnecessary check dbtr_thishart_state_ptr
lib: sbi: fix DBTR_SHMEM_MAKE_PHYS for RV64
lib: sbi: Fixed memory permission check in sbi_dbtr_setup_shmem
lib: sbi: fix return type of sbi_dbtr_shmem_disabled
lib: sbi: simplify inline function in sbi_dtbr.c
lib/sbi/sbi_dbtr.c | 97 +++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 61 deletions(-)
Signature · 1 line
2.43.0
PATCH v3 1/6 lib: sbi: fix hart_shmem_base
When only phys_hi is equal to SBI_DBTR_SHMEM_INVALID_ADDR, it may be a legal address. The old code would modify the legal address.
--- lib/sbi/sbi_dbtr.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-)
lib/sbi/sbi_dbtr.c +4 -7
@@ -84,21 +84,18 @@ static inline void sbi_dbtr_disable_shmem(void)static inline void *hart_shmem_base (void ) { -struct sbi_dbtr_shmem * shmem;unsigned long phys_hi, phys_lo;struct sbi_dbtr_hart_triggers_state *hs =NULL ; hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ()); -if (!hs)return NULL ; - shmem = &hs->shmem; + phys_hi = hs->shmem.phys_hi; + phys_lo = hs->shmem.phys_lo; - phys_hi = (shmem->phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR - ? shmem->phys_hi :0 ); - phys_lo = (shmem->phys_lo ==SBI_DBTR_SHMEM_INVALID_ADDR - ?0 : shmem->phys_lo); +if (phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR && phys_hi == phys_lo) +return NULL ;return ((void *)(unsigned long )DBTR_SHMEM_MAKE_PHYS (phys_hi, phys_lo)); }
Signature · 1 line
2.43.0
PATCH v3 2/6 lib: sbi: Removal unnecessary check dbtr_thishart_state_ptr
In reply to Xiang WAfter getting hart_shmem_base, dbtr_thishart_state_ptr cannot be empty. So remove check code.
--- lib/sbi/sbi_dbtr.c | 2 -- 1 file changed, 2 deletions(-)
lib/sbi/sbi_dbtr.c +0 -2
@@ -684,8 +684,6 @@ int sbi_dbtr_update_trig(unsigned long smode, shmem_base =hart_shmem_base (); hs =dbtr_thishart_state_ptr (); -if (!hs) -return SBI_ERR_FAILED ;for_each_set_bit_from (idx, &trig_mask, hs->total_trigs) { trig =INDEX_TO_TRIGGER (idx);
Signature · 1 line
2.43.0
PATCH v3 3/6 lib: sbi: fix DBTR_SHMEM_MAKE_PHYS for RV64
In reply to Xiang WObtaining a 64-bit address under rv64 does not require combining 32-bit registers
--- lib/sbi/sbi_dbtr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_dbtr.c +1 -1
@@ -48,7 +48,7 @@ static unsigned long hart_state_ptr_offset; _idx++, _entry = ((_etype *)_base + _idx))#if __riscv_xlen ==64 -#define DBTR_SHMEM_MAKE_PHYS (_p_hi, _p_lo) (((u64)(_p_hi) << 32) | (_p_lo)) +#define DBTR_SHMEM_MAKE_PHYS (_p_hi, _p_lo) (_p_lo)#elif __riscv_xlen ==32 #define DBTR_SHMEM_MAKE_PHYS (_p_hi, _p_lo) (((u64)(_p_hi) << 32) | (_p_lo))#else
Signature · 1 line
2.43.0
PATCH v3 4/6 lib: sbi: Fixed memory permission check in sbi_dbtr_setup_shmem
In reply to Xiang WThe previous code detected shmem_phys_hi and shmem_phys_lo as two addresses. fix this bug
--- lib/sbi/sbi_dbtr.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
lib/sbi/sbi_dbtr.c +3 -9
@@ -293,17 +293,11 @@ int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode,if (shmem_phys_lo &SBI_DBTR_SHMEM_ALIGN_MASK )return SBI_ERR_INVALID_PARAM ; -if (dom && !sbi_domain_check_addr (dom, shmem_phys_lo, smode, -SBI_DOMAIN_READ |SBI_DOMAIN_WRITE )) +if (dom && !sbi_domain_check_addr (dom, +DBTR_SHMEM_MAKE_PHYS (shmem_phys_hi, shmem_phys_lo), smode, +SBI_DOMAIN_READ |SBI_DOMAIN_WRITE ))return SBI_ERR_INVALID_ADDRESS ; -if (shmem_phys_hi !=SBI_DBTR_SHMEM_INVALID_ADDR ) { -if (dom && - !sbi_domain_check_addr (dom, shmem_phys_hi, smode, -SBI_DOMAIN_READ |SBI_DOMAIN_WRITE )) -return SBI_ERR_INVALID_ADDRESS ; - } - hart_state =dbtr_thishart_state_ptr ();if (!hart_state)return SBI_ERR_FAILED ;
Signature · 1 line
2.43.0
PATCH v3 5/6 lib: sbi: fix return type of sbi_dbtr_shmem_disabled
In reply to Xiang WModify the return value of the sbi_dbtr_shmem_disabled function to bool to make the semantics clearer.
--- lib/sbi/sbi_dbtr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
lib/sbi/sbi_dbtr.c +3 -3
@@ -55,18 +55,18 @@ static unsigned long hart_state_ptr_offset;#error "Undefined XLEN"#endif -static inline int sbi_dbtr_shmem_disabled (void ) +static inline bool sbi_dbtr_shmem_disabled (void ) {struct sbi_dbtr_hart_triggers_state *hs =NULL ; hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ());if (!hs) -return 1 ; +return true;return (hs->shmem.phys_lo ==SBI_DBTR_SHMEM_INVALID_ADDR && hs->shmem.phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR - ?1 :0 ); + ? true : false); }static inline void sbi_dbtr_disable_shmem (void )
Signature · 1 line
2.43.0
PATCH v3 5/6 lib: sbi: fix return type of sbi_dbtr_shmem_disabled
Quoted 5 lines · On Fri, May 3, 2024 at 7:24?PM Xiang W <[email protected]> wrote:
Modify the return value of the
sbi_dbtr_shmem_disabledfunction to bool to make the semantics clearer.Signed-off-by Xiang W <[email protected]>
LGTM.
Regards,
Anup
Quoted 33 lines
--- lib/sbi/sbi_dbtr.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)lib/sbi/sbi_dbtr.c +3
-4@@ -55,18 +55,18 @@ static unsigned long hart_state_ptr_offset;#error "Undefined XLEN"#endif -static inline int sbi_dbtr_shmem_disabled (void ) +static inline bool sbi_dbtr_shmem_disabled (void ) {struct sbi_dbtr_hart_triggers_state *hs =NULL ; hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ());if (!hs) -return 1 ; +return true;return (hs->shmem.phys_lo ==SBI_DBTR_SHMEM_INVALID_ADDR && hs->shmem.phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR - ?1 :0 ); + ? true : false); }static inline void sbi_dbtr_disable_shmem (void ) --2.43.0
PATCH v3 6/6 lib: sbi: simplify inline function in sbi_dtbr.c
In reply to Xiang WThe inline function can simplify the code by setting some call restrictions. This ensures logical smoothness
--- lib/sbi/sbi_dbtr.c | 78 +++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 46 deletions(-)
lib/sbi/sbi_dbtr.c +32 -46
@@ -55,49 +55,29 @@ static unsigned long hart_state_ptr_offset;#error "Undefined XLEN"#endif -static inline bool sbi_dbtr_shmem_disabled (void ) +/* must call with hs != NULL */ +static inline bool sbi_dbtr_shmem_disabled ( +struct sbi_dbtr_hart_triggers_state *hs) { -struct sbi_dbtr_hart_triggers_state *hs =NULL ; - - hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ()); - -if (!hs) -return true; -return (hs->shmem.phys_lo ==SBI_DBTR_SHMEM_INVALID_ADDR && hs->shmem.phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR ? true : false); } -static inline void sbi_dbtr_disable_shmem (void ) +/* must call with hs != NULL */ +static inline void sbi_dbtr_disable_shmem ( +struct sbi_dbtr_hart_triggers_state *hs) { -struct sbi_dbtr_hart_triggers_state *hs =NULL ; - - hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ()); - -if (!hs) -return ; - hs->shmem.phys_lo =SBI_DBTR_SHMEM_INVALID_ADDR ; hs->shmem.phys_hi =SBI_DBTR_SHMEM_INVALID_ADDR ; } -static inline void *hart_shmem_base (void ) +/* must call with hs which is not disabled */ +static inline void *hart_shmem_base ( +struct sbi_dbtr_hart_triggers_state *hs) { -unsigned long phys_hi, phys_lo; -struct sbi_dbtr_hart_triggers_state *hs =NULL ; - - hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ()); -if (!hs) -return NULL ; - - phys_hi = hs->shmem.phys_hi; - phys_lo = hs->shmem.phys_lo; - -if (phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR && phys_hi == phys_lo) -return NULL ; - -return ((void *)(unsigned long )DBTR_SHMEM_MAKE_PHYS (phys_hi, phys_lo)); +return ((void *)(unsigned long )DBTR_SHMEM_MAKE_PHYS ( + hs->shmem.phys_hi, hs->shmem.phys_lo)); } static void sbi_trigger_init(struct sbi_dbtr_trigger *trig,
@@ -186,7 +166,7 @@ int sbi_dbtr_init(struct sbi_scratch *scratch, bool coldboot) }/* disable the shared memory */ -sbi_dbtr_disable_shmem (); +sbi_dbtr_disable_shmem (hart_state);/* Skip probing triggers if already probed */ if (hart_state->probed)
@@ -278,15 +258,19 @@ int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode,return SBI_ERR_DENIED ; } + hart_state =dbtr_thishart_state_ptr (); +if (!hart_state) +return SBI_ERR_FAILED ; +/* call is to disable shared memory */ if (shmem_phys_lo ==SBI_DBTR_SHMEM_INVALID_ADDR && shmem_phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR ) { -sbi_dbtr_disable_shmem (); +sbi_dbtr_disable_shmem (hart_state);return SBI_SUCCESS ; }/* the shared memory must be disabled on this hart */ -if (!sbi_dbtr_shmem_disabled ()) +if (!sbi_dbtr_shmem_disabled (hart_state))return SBI_ERR_ALREADY_AVAILABLE ;/* lower physical address must be XLEN/8 bytes aligned */
@@ -298,10 +282,6 @@ int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode, SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))return SBI_ERR_INVALID_ADDRESS ; - hart_state =dbtr_thishart_state_ptr (); -if (!hart_state) -return SBI_ERR_FAILED ; - hart_state->shmem.phys_lo = shmem_phys_lo; hart_state->shmem.phys_hi = shmem_phys_hi;
@@ -531,10 +511,10 @@ int sbi_dbtr_read_trig(unsigned long smode, trig_idx_base + trig_count >= hs->total_trigs)return SBI_ERR_INVALID_PARAM ; -if (sbi_dbtr_shmem_disabled ()) +if (sbi_dbtr_shmem_disabled (hs))return SBI_ERR_NO_SHMEM ; - shmem_base =hart_shmem_base (); + shmem_base =hart_shmem_base (hs);for_each_trig_entry (shmem_base, trig_count,typeof (*entry), entry) {sbi_hart_map_saddr ((unsigned long )entry,sizeof (*entry));
@@ -561,11 +541,14 @@ int sbi_dbtr_install_trig(unsigned long smode,struct sbi_dbtr_trigger *trig;struct sbi_dbtr_hart_triggers_state *hs =NULL ; -if (sbi_dbtr_shmem_disabled ()) + hs =dbtr_thishart_state_ptr (); +if (!hs) +return SBI_ERR_FAILED ; + +if (sbi_dbtr_shmem_disabled (hs))return SBI_ERR_NO_SHMEM ; - shmem_base =hart_shmem_base (); - hs =dbtr_thishart_state_ptr (); + shmem_base =hart_shmem_base (hs);/* Check requested triggers configuration */ for_each_trig_entry (shmem_base, trig_count,typeof (*entry), entry) {
@@ -673,11 +656,14 @@ int sbi_dbtr_update_trig(unsigned long smode,void *shmem_base =NULL ;struct sbi_dbtr_hart_triggers_state *hs =NULL ; -if (sbi_dbtr_shmem_disabled ()) + hs =dbtr_thishart_state_ptr (); +if (!hs) +return SBI_ERR_FAILED ; + +if (sbi_dbtr_shmem_disabled (hs))return SBI_ERR_NO_SHMEM ; - shmem_base =hart_shmem_base (); - hs =dbtr_thishart_state_ptr (); + shmem_base =hart_shmem_base (hs);for_each_set_bit_from (idx, &trig_mask, hs->total_trigs) { trig =INDEX_TO_TRIGGER (idx);
Signature · 1 line
2.43.0
PATCH v3 6/6 lib: sbi: simplify inline function in sbi_dtbr.c
Quoted 5 lines · On Fri, May 3, 2024 at 7:24?PM Xiang W <[email protected]> wrote:
The inline function can simplify the code by setting some call restrictions. This ensures logical smoothness
Signed-off-by Xiang W <[email protected]>
LGTM.
Regards,
Anup
Quoted 163 lines
--- lib/sbi/sbi_dbtr.c | 78 +++++++++++++++++++--------------------------- 1 file changed, 32 insertions(+), 46 deletions(-)lib/sbi/sbi_dbtr.c +32
-47@@ -55,49 +55,29 @@ static unsigned long hart_state_ptr_offset;#error "Undefined XLEN"#endif -static inline bool sbi_dbtr_shmem_disabled (void ) +/* must call with hs != NULL */ +static inline bool sbi_dbtr_shmem_disabled ( +struct sbi_dbtr_hart_triggers_state *hs) { -struct sbi_dbtr_hart_triggers_state *hs =NULL ; - - hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ()); - -if (!hs) -return true; -return (hs->shmem.phys_lo ==SBI_DBTR_SHMEM_INVALID_ADDR && hs->shmem.phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR ? true : false); } -static inline void sbi_dbtr_disable_shmem (void ) +/* must call with hs != NULL */ +static inline void sbi_dbtr_disable_shmem ( +struct sbi_dbtr_hart_triggers_state *hs) { -struct sbi_dbtr_hart_triggers_state *hs =NULL ; - - hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ()); - -if (!hs) -return ; - hs->shmem.phys_lo =SBI_DBTR_SHMEM_INVALID_ADDR ; hs->shmem.phys_hi =SBI_DBTR_SHMEM_INVALID_ADDR ; } -static inline void *hart_shmem_base (void ) +/* must call with hs which is not disabled */ +static inline void *hart_shmem_base ( +struct sbi_dbtr_hart_triggers_state *hs) { -unsigned long phys_hi, phys_lo; -struct sbi_dbtr_hart_triggers_state *hs =NULL ; - - hs =dbtr_get_hart_state_ptr (sbi_scratch_thishart_ptr ()); -if (!hs) -return NULL ; - - phys_hi = hs->shmem.phys_hi; - phys_lo = hs->shmem.phys_lo; - -if (phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR && phys_hi == phys_lo) -return NULL ; - -return ((void *)(unsigned long )DBTR_SHMEM_MAKE_PHYS (phys_hi, phys_lo)); +return ((void *)(unsigned long )DBTR_SHMEM_MAKE_PHYS ( + hs->shmem.phys_hi, hs->shmem.phys_lo)); } static void sbi_trigger_init(struct sbi_dbtr_trigger *trig,@@ -186,7 +166,7 @@ int sbi_dbtr_init(struct sbi_scratch *scratch, bool coldboot) }/* disable the shared memory */ -sbi_dbtr_disable_shmem (); +sbi_dbtr_disable_shmem (hart_state);/* Skip probing triggers if already probed */ if (hart_state->probed)@@ -278,15 +258,19 @@ int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode,return SBI_ERR_DENIED ; } + hart_state =dbtr_thishart_state_ptr (); +if (!hart_state) +return SBI_ERR_FAILED ; +/* call is to disable shared memory */ if (shmem_phys_lo ==SBI_DBTR_SHMEM_INVALID_ADDR && shmem_phys_hi ==SBI_DBTR_SHMEM_INVALID_ADDR ) { -sbi_dbtr_disable_shmem (); +sbi_dbtr_disable_shmem (hart_state);return SBI_SUCCESS ; }/* the shared memory must be disabled on this hart */ -if (!sbi_dbtr_shmem_disabled ()) +if (!sbi_dbtr_shmem_disabled (hart_state))return SBI_ERR_ALREADY_AVAILABLE ;/* lower physical address must be XLEN/8 bytes aligned */ @@ -298,10 +282,6 @@ int sbi_dbtr_setup_shmem(const struct sbi_domain *dom, unsigned long smode, SBI_DOMAIN_READ | SBI_DOMAIN_WRITE))return SBI_ERR_INVALID_ADDRESS ; - hart_state =dbtr_thishart_state_ptr (); -if (!hart_state) -return SBI_ERR_FAILED ; - hart_state->shmem.phys_lo = shmem_phys_lo; hart_state->shmem.phys_hi = shmem_phys_hi;@@ -531,10 +511,10 @@ int sbi_dbtr_read_trig(unsigned long smode, trig_idx_base + trig_count >= hs->total_trigs)return SBI_ERR_INVALID_PARAM ; -if (sbi_dbtr_shmem_disabled ()) +if (sbi_dbtr_shmem_disabled (hs))return SBI_ERR_NO_SHMEM ; - shmem_base =hart_shmem_base (); + shmem_base =hart_shmem_base (hs);for_each_trig_entry (shmem_base, trig_count,typeof (*entry), entry) {sbi_hart_map_saddr ((unsigned long )entry,sizeof (*entry));@@ -561,11 +541,14 @@ int sbi_dbtr_install_trig(unsigned long smode,struct sbi_dbtr_trigger *trig;struct sbi_dbtr_hart_triggers_state *hs =NULL ; -if (sbi_dbtr_shmem_disabled ()) + hs =dbtr_thishart_state_ptr (); +if (!hs) +return SBI_ERR_FAILED ; + +if (sbi_dbtr_shmem_disabled (hs))return SBI_ERR_NO_SHMEM ; - shmem_base =hart_shmem_base (); - hs =dbtr_thishart_state_ptr (); + shmem_base =hart_shmem_base (hs);/* Check requested triggers configuration */ for_each_trig_entry (shmem_base, trig_count,typeof (*entry), entry) {@@ -673,11 +656,14 @@ int sbi_dbtr_update_trig(unsigned long smode,void *shmem_base =NULL ;struct sbi_dbtr_hart_triggers_state *hs =NULL ; -if (sbi_dbtr_shmem_disabled ()) + hs =dbtr_thishart_state_ptr (); +if (!hs) +return SBI_ERR_FAILED ; + +if (sbi_dbtr_shmem_disabled (hs))return SBI_ERR_NO_SHMEM ; - shmem_base =hart_shmem_base (); - hs =dbtr_thishart_state_ptr (); + shmem_base =hart_shmem_base (hs);for_each_set_bit_from (idx, &trig_mask, hs->total_trigs) { trig =INDEX_TO_TRIGGER (idx); --2.43.0
v3 cover miscellaneous about sbi_dtbr
In reply to Xiang W? 2024-05-03???? 21:53 +0800?Xiang W???
v3 changes:
- remove PATCH2 suggest by Himanshu
- Add PATCH5 fix return type of sbi_dbtr_shmem_disabled suggest by Himanshu
- Add PATCH6 simplify inline function in sbi_dtbr.c
Regards,
Xiang W
Quoted 14 lines
v2 changes:
- fixshmem_basechecking in PATCH2Xiang W (6):
? lib: sbi: fixhart_shmem_base
? lib: sbi: Removal unnecessary checkdbtr_thishart_state_ptr? lib: sbi: fixDBTR_SHMEM_MAKE_PHYSfor RV64
? lib: sbi: Fixed memory permission check insbi_dbtr_setup_shmem? lib: sbi: fix return type ofsbi_dbtr_shmem_disabled? lib: sbi: simplify inline function insbi_dtbr.c?lib/sbi/sbi_dbtr.c | 97 +++++++++++++++++----------------------------- ?1 file changed, 36 insertions(+), 61 deletions(-)
Anup Patel
Appliedv3 cover miscellaneous about sbi_dtbr
In reply to Xiang WQuoted 11 lines · On Fri, May 3, 2024 at 7:23?PM Xiang W <[email protected]> wrote:
v2 changes:
- fixshmem_basechecking in PATCH2Xiang W (6):
lib: sbi: fixhart_shmem_base
lib: sbi: Removal unnecessary checkdbtr_thishart_state_ptr
lib: sbi: fixDBTR_SHMEM_MAKE_PHYSfor RV64
lib: sbi: Fixed memory permission check insbi_dbtr_setup_shmem
lib: sbi: fix return type ofsbi_dbtr_shmem_disabled
lib: sbi: simplify inline function insbi_dtbr.c
Applied this series to the riscv/opensbi repo.
Thanks,
Anup
Quoted 7 lines
lib/sbi/sbi_dbtr.c | 97 +++++++++++++++++----------------------------- 1 file changed, 36 insertions(+), 61 deletions(-)--
2.43.0