From 296e70d69da74b09426e4f565306f26e9a6c79f8 Mon Sep 17 00:00:00 2001 From: Xiang W Date: Thu, 31 Aug 2023 11:39:30 +0800 Subject: lib: sbi: Extend sbi_hartmask to support both hartid and hartindex Currently, the sbi_hartmask is indexed by hartid which puts a limit on hartid to be less than SBI_HARTMASK_MAX_BITS. We extend the sbi_hartmask implementation to use hartindex and support updating sbi_hartmask using hartid. This removes the limit on hartid and existing code works largely unmodified. Signed-off-by: Xiang W Signed-off-by: Anup Patel --- include/sbi/sbi_hartmask.h | 88 +++++++++++++++++++++++++++++++++++++--------- 1 file changed, 71 insertions(+), 17 deletions(-) (limited to 'include') diff --git a/include/sbi/sbi_hartmask.h b/include/sbi/sbi_hartmask.h index f1cef0c2..105653e1 100644 --- a/include/sbi/sbi_hartmask.h +++ b/include/sbi/sbi_hartmask.h @@ -11,6 +11,7 @@ #define __SBI_HARTMASK_H__ #include +#include /** * Maximum number of bits in a hartmask @@ -32,7 +33,10 @@ struct sbi_hartmask { /** Initialize hartmask to zero except a particular HART id */ #define SBI_HARTMASK_INIT_EXCEPT(__m, __h) \ - bitmap_zero_except(((__m)->bits), (__h), SBI_HARTMASK_MAX_BITS) + do { \ + u32 __i = sbi_hartid_to_hartindex(__h); \ + bitmap_zero_except(((__m)->bits), __i, SBI_HARTMASK_MAX_BITS); \ + } while(0) /** * Get underlying bitmap of hartmask @@ -41,39 +45,70 @@ struct sbi_hartmask { #define sbi_hartmask_bits(__m) ((__m)->bits) /** - * Set a HART in hartmask + * Set a HART index in hartmask + * @param i HART index to set + * @param m the hartmask pointer + */ +static inline void sbi_hartmask_set_hartindex(u32 i, struct sbi_hartmask *m) +{ + if (i < SBI_HARTMASK_MAX_BITS) + __set_bit(i, m->bits); +} + +/** + * Set a HART id in hartmask * @param h HART id to set * @param m the hartmask pointer */ -static inline void sbi_hartmask_set_hart(u32 h, struct sbi_hartmask *m) +static inline void sbi_hartmask_set_hartid(u32 h, struct sbi_hartmask *m) +{ + sbi_hartmask_set_hartindex(sbi_hartid_to_hartindex(h), m); +} + +/** + * Clear a HART index in hartmask + * @param i HART index to clear + * @param m the hartmask pointer + */ +static inline void sbi_hartmask_clear_hartindex(u32 i, struct sbi_hartmask *m) { - if (h < SBI_HARTMASK_MAX_BITS) - __set_bit(h, m->bits); + if (i < SBI_HARTMASK_MAX_BITS) + __clear_bit(i, m->bits); } /** - * Clear a HART in hartmask + * Clear a HART id in hartmask * @param h HART id to clear * @param m the hartmask pointer */ -static inline void sbi_hartmask_clear_hart(u32 h, struct sbi_hartmask *m) +static inline void sbi_hartmask_clear_hartid(u32 h, struct sbi_hartmask *m) { - if (h < SBI_HARTMASK_MAX_BITS) - __clear_bit(h, m->bits); + sbi_hartmask_clear_hartindex(sbi_hartid_to_hartindex(h), m); } /** - * Test a HART in hartmask - * @param h HART id to test + * Test a HART index in hartmask + * @param i HART index to test * @param m the hartmask pointer */ -static inline int sbi_hartmask_test_hart(u32 h, const struct sbi_hartmask *m) +static inline int sbi_hartmask_test_hartindex(u32 i, + const struct sbi_hartmask *m) { - if (h < SBI_HARTMASK_MAX_BITS) - return __test_bit(h, m->bits); + if (i < SBI_HARTMASK_MAX_BITS) + return __test_bit(i, m->bits); return 0; } +/** + * Test a HART id in hartmask + * @param h HART id to test + * @param m the hartmask pointer + */ +static inline int sbi_hartmask_test_hartid(u32 h, const struct sbi_hartmask *m) +{ + return sbi_hartmask_test_hartindex(sbi_hartid_to_hartindex(h), m); +} + /** * Set all HARTs in a hartmask * @param dstp the hartmask pointer @@ -134,8 +169,27 @@ static inline void sbi_hartmask_xor(struct sbi_hartmask *dstp, sbi_hartmask_bits(src2p), SBI_HARTMASK_MAX_BITS); } -/** Iterate over each HART in hartmask */ -#define sbi_hartmask_for_each_hart(__h, __m) \ - for_each_set_bit(__h, (__m)->bits, SBI_HARTMASK_MAX_BITS) +/** + * Iterate over each HART in hartmask + * __h hart id + * __i hart index + * __m hartmask +*/ +#define sbi_hartmask_for_each_hart(__h, __i, __m) \ + for((__i) = find_first_bit((__m)->bits, SBI_HARTMASK_MAX_BITS), \ + (__h) = sbi_hartindex_to_hartid(__i); \ + (__i) < SBI_HARTMASK_MAX_BITS; \ + (__i) = find_next_bit((__m)->bits, SBI_HARTMASK_MAX_BITS, (__i) + 1), \ + (__h) = sbi_hartindex_to_hartid(__i)) + +/** + * Iterate over each HART index in hartmask + * __i hart index + * __m hartmask +*/ +#define sbi_hartmask_for_each_hartindex(__i, __m) \ + for((__i) = find_first_bit((__m)->bits, SBI_HARTMASK_MAX_BITS); \ + (__i) < SBI_HARTMASK_MAX_BITS; \ + (__i) = find_next_bit((__m)->bits, SBI_HARTMASK_MAX_BITS, (__i) + 1)) #endif -- cgit v1.3.1