* [PATCH 0/8] OpenSBI sparse HART id support
@ 2023-09-04 4:03 Anup Patel
2023-09-04 4:03 ` [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch Anup Patel
` (8 more replies)
0 siblings, 9 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
Currently, we requires HART id to be less than SBI_HARTMASK_MAX_BITS
because we use HART id as bit index in the sbi_hartmask. This is a
very limiting requirement because RISC-V platforms can have sparse
HART ids (with possibly large values).
This series addresses the above described limitation by adding
sparse HART id support in OpenSBI.
These patches can also be found the full_sparse_hartid_v1 branch at
https://github.com/avpatel/opensbi.git
Anup Patel (7):
lib: sbi: Introduce HART index in sbi_scratch
lib: sbi: Remove sbi_platform_hart_index/invalid() functions
lib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment
lib: sbi: Prefer hartindex over hartid in IPI framework
lib: sbi: Remove sbi_scratch_last_hartid() macro
lib: sbi: Maximize the use of HART index in sbi_domain
include: sbi: Remove sbi_hartmask_for_each_hart() macro
Xiang W (1):
lib: sbi: Extend sbi_hartmask to support both hartid and hartindex
include/sbi/sbi_domain.h | 6 +--
include/sbi/sbi_hartmask.h | 75 ++++++++++++++++++++++++-------
include/sbi/sbi_ipi.h | 14 +++---
include/sbi/sbi_platform.h | 28 ------------
include/sbi/sbi_scratch.h | 49 ++++++++++++++++----
lib/sbi/sbi_domain.c | 82 ++++++++++++++++------------------
lib/sbi/sbi_hsm.c | 30 ++++++-------
lib/sbi/sbi_init.c | 28 +++++++-----
lib/sbi/sbi_ipi.c | 28 ++++++------
lib/sbi/sbi_platform.c | 17 -------
lib/sbi/sbi_scratch.c | 37 +++++++++------
lib/sbi/sbi_system.c | 5 ++-
lib/sbi/sbi_tlb.c | 14 +++---
lib/utils/fdt/fdt_domain.c | 4 +-
lib/utils/ipi/aclint_mswi.c | 14 +++---
lib/utils/ipi/andes_plicsw.c | 8 +++-
lib/utils/irqchip/imsic.c | 4 +-
platform/generic/andes/ae350.c | 2 +-
18 files changed, 246 insertions(+), 199 deletions(-)
--
2.34.1
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
@ 2023-09-04 4:03 ` Anup Patel
2023-09-20 17:54 ` Xiang W
2023-09-04 4:03 ` [PATCH 2/8] lib: sbi: Remove sbi_platform_hart_index/invalid() functions Anup Patel
` (7 subsequent siblings)
8 siblings, 1 reply; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
We introduce HART index and related helper functions in sbi_scratch
where HART index is contiguous and each HART index maps to a physical
HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.
The HART index to HART id mapping follows the index2id mapping provided
by the platform. If the platform does not provide index2id mapping then
identity mapping is assumed.
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
lib/sbi/sbi_scratch.c | 38 ++++++++++++++++++++++-----------
2 files changed, 68 insertions(+), 15 deletions(-)
diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
index 4801492..9a4dce1 100644
--- a/include/sbi/sbi_scratch.h
+++ b/include/sbi/sbi_scratch.h
@@ -202,12 +202,47 @@ do { \
= (__type)(__ptr); \
} while (0)
-/** HART id to scratch table */
-extern struct sbi_scratch *hartid_to_scratch_table[];
+/** Last HART index having a sbi_scratch pointer */
+extern u32 last_hartindex_having_scratch;
+
+/** Get last HART index having a sbi_scratch pointer */
+#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
+
+/** Check whether a particular HART index is valid or not */
+#define sbi_hartindex_valid(__hartindex) \
+(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
+
+/** HART index to HART id table */
+extern u32 hartindex_to_hartid_table[];
+
+/** Get sbi_scratch from HART index */
+#define sbi_hartindex_to_hartid(__hartindex) \
+({ \
+ ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
+ hartindex_to_hartid_table[__hartindex] : -1U; \
+})
+
+/** HART index to scratch table */
+extern struct sbi_scratch *hartindex_to_scratch_table[];
+
+/** Get sbi_scratch from HART index */
+#define sbi_hartindex_to_scratch(__hartindex) \
+({ \
+ ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
+ hartindex_to_scratch_table[__hartindex] : NULL;\
+})
+
+/**
+ * Get logical index for given HART id
+ * @param hartid physical HART id
+ * @returns value between 0 to SBI_HARTMASK_MAX_BITS upon success and
+ * SBI_HARTMASK_MAX_BITS upon failure.
+ */
+u32 sbi_hartid_to_hartindex(u32 hartid);
/** Get sbi_scratch from HART id */
#define sbi_hartid_to_scratch(__hartid) \
- hartid_to_scratch_table[__hartid]
+ sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
/** Last HART id having a sbi_scratch pointer */
extern u32 last_hartid_having_scratch;
@@ -215,6 +250,10 @@ extern u32 last_hartid_having_scratch;
/** Get last HART id having a sbi_scratch pointer */
#define sbi_scratch_last_hartid() last_hartid_having_scratch
+/** Check whether particular HART id is valid or not */
+#define sbi_hartid_valid(__hartid) \
+ sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
+
#endif
#endif
diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
index 87ef84c..d2abc89 100644
--- a/lib/sbi/sbi_scratch.c
+++ b/lib/sbi/sbi_scratch.c
@@ -15,26 +15,40 @@
#include <sbi/sbi_string.h>
u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
-struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
+u32 last_hartindex_having_scratch = 0;
+u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
+struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
+u32 sbi_hartid_to_hartindex(u32 hartid)
+{
+ u32 i;
+
+ for (i = 0; i <= last_hartindex_having_scratch; i++)
+ if (hartindex_to_hartid_table[i] == hartid)
+ return i;
+
+ return -1U;
+}
+
typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
int sbi_scratch_init(struct sbi_scratch *scratch)
{
- u32 i;
+ u32 i, h;
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
- for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
- if (sbi_platform_hart_invalid(plat, i))
- continue;
- hartid_to_scratch_table[i] =
- ((hartid2scratch)scratch->hartid_to_scratch)(i,
- sbi_platform_hart_index(plat, i));
- if (hartid_to_scratch_table[i])
- last_hartid_having_scratch = i;
+ for (i = 0; i < plat->hart_count; i++) {
+ h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
+ hartindex_to_hartid_table[i] = h;
+ hartindex_to_scratch_table[i] =
+ ((hartid2scratch)scratch->hartid_to_scratch)(h, i);
+ if (hartindex_to_scratch_table[i]) {
+ last_hartid_having_scratch = h;
+ last_hartindex_having_scratch = i;
+ }
}
return 0;
@@ -74,8 +88,8 @@ done:
spin_unlock(&extra_lock);
if (ret) {
- for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
- rscratch = sbi_hartid_to_scratch(i);
+ for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
+ rscratch = sbi_hartindex_to_scratch(i);
if (!rscratch)
continue;
ptr = sbi_scratch_offset_ptr(rscratch, ret);
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 2/8] lib: sbi: Remove sbi_platform_hart_index/invalid() functions
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
2023-09-04 4:03 ` [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch Anup Patel
@ 2023-09-04 4:03 ` Anup Patel
2023-09-04 4:03 ` [PATCH 3/8] lib: sbi: Extend sbi_hartmask to support both hartid and hartindex Anup Patel
` (6 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
The hartid to hartindex mapping is now tracked in sbi_scratch so we
don't need sbi_platform_hart_index() and sbi_platform_hart_invalid()
functions hence let us remove them.
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
include/sbi/sbi_platform.h | 28 ----------------------------
lib/sbi/sbi_domain.c | 5 ++---
lib/sbi/sbi_init.c | 12 +++++++++---
lib/sbi/sbi_platform.c | 17 -----------------
4 files changed, 11 insertions(+), 51 deletions(-)
diff --git a/include/sbi/sbi_platform.h b/include/sbi/sbi_platform.h
index 3e9616f..5f81bb1 100644
--- a/include/sbi/sbi_platform.h
+++ b/include/sbi/sbi_platform.h
@@ -258,16 +258,6 @@ _Static_assert(
#define sbi_platform_has_mfaults_delegation(__p) \
((__p)->features & SBI_PLATFORM_HAS_MFAULTS_DELEGATION)
-/**
- * Get HART index for the given HART
- *
- * @param plat pointer to struct sbi_platform
- * @param hartid HART ID
- *
- * @return 0 <= value < hart_count for valid HART otherwise -1U
- */
-u32 sbi_platform_hart_index(const struct sbi_platform *plat, u32 hartid);
-
/**
* Get the platform features in string format
*
@@ -353,24 +343,6 @@ static inline u32 sbi_platform_hart_stack_size(const struct sbi_platform *plat)
return 0;
}
-/**
- * Check whether given HART is invalid
- *
- * @param plat pointer to struct sbi_platform
- * @param hartid HART ID
- *
- * @return true if HART is invalid and false otherwise
- */
-static inline bool sbi_platform_hart_invalid(const struct sbi_platform *plat,
- u32 hartid)
-{
- if (!plat)
- return true;
- if (plat->hart_count <= sbi_platform_hart_index(plat, hartid))
- return true;
- return false;
-}
-
/**
* Check whether given HART is allowed to do cold boot
*
diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
index acd0f74..77d6ca4 100644
--- a/lib/sbi/sbi_domain.c
+++ b/lib/sbi/sbi_domain.c
@@ -277,7 +277,7 @@ static int sanitize_domain(const struct sbi_platform *plat,
return SBI_EINVAL;
}
sbi_hartmask_for_each_hart(i, dom->possible_harts) {
- if (sbi_platform_hart_invalid(plat, i)) {
+ if (!sbi_hartid_valid(i)) {
sbi_printf("%s: %s possible HART mask has invalid "
"hart %d\n", __func__, dom->name, i);
return SBI_EINVAL;
@@ -723,7 +723,6 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
int rc;
struct sbi_hartmask *root_hmask;
struct sbi_domain_memregion *root_memregs;
- const struct sbi_platform *plat = sbi_platform_ptr(scratch);
if (scratch->fw_rw_offset == 0 ||
(scratch->fw_rw_offset & (scratch->fw_rw_offset - 1)) != 0) {
@@ -798,7 +797,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
/* Root domain possible and assigned HARTs */
for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
- if (sbi_platform_hart_invalid(plat, i))
+ if (!sbi_hartid_valid(i))
continue;
sbi_hartmask_set_hart(i, root_hmask);
}
diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c
index 252b41a..07be3d2 100644
--- a/lib/sbi/sbi_init.c
+++ b/lib/sbi/sbi_init.c
@@ -520,13 +520,19 @@ static atomic_t coldboot_lottery = ATOMIC_INITIALIZER(0);
*/
void __noreturn sbi_init(struct sbi_scratch *scratch)
{
+ u32 i, h;
+ bool hartid_valid = false;
bool next_mode_supported = false;
bool coldboot = false;
u32 hartid = current_hartid();
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
- if ((SBI_HARTMASK_MAX_BITS <= hartid) ||
- sbi_platform_hart_invalid(plat, hartid))
+ for (i = 0; i < plat->hart_count; i++) {
+ h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
+ if (h == hartid)
+ hartid_valid = true;
+ }
+ if (SBI_HARTMASK_MAX_BITS <= hartid || !hartid_valid)
sbi_hart_hang();
switch (scratch->next_mode) {
@@ -623,7 +629,7 @@ void __noreturn sbi_exit(struct sbi_scratch *scratch)
u32 hartid = current_hartid();
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
- if (sbi_platform_hart_invalid(plat, hartid))
+ if (!sbi_hartid_valid(hartid))
sbi_hart_hang();
sbi_platform_early_exit(plat);
diff --git a/lib/sbi/sbi_platform.c b/lib/sbi/sbi_platform.c
index 445a8c1..43fc88a 100644
--- a/lib/sbi/sbi_platform.c
+++ b/lib/sbi/sbi_platform.c
@@ -71,20 +71,3 @@ done:
else
sbi_strncpy(features_str, "none", nfstr);
}
-
-u32 sbi_platform_hart_index(const struct sbi_platform *plat, u32 hartid)
-{
- u32 i;
-
- if (!plat)
- return -1U;
- if (plat->hart_index2id) {
- for (i = 0; i < plat->hart_count; i++) {
- if (plat->hart_index2id[i] == hartid)
- return i;
- }
- return -1U;
- }
-
- return hartid;
-}
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 3/8] lib: sbi: Extend sbi_hartmask to support both hartid and hartindex
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
2023-09-04 4:03 ` [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch Anup Patel
2023-09-04 4:03 ` [PATCH 2/8] lib: sbi: Remove sbi_platform_hart_index/invalid() functions Anup Patel
@ 2023-09-04 4:03 ` Anup Patel
2023-09-04 4:03 ` [PATCH 4/8] lib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment Anup Patel
` (5 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
From: Xiang W <wxjstz@126.com>
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 <wxjstz@126.com>
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
include/sbi/sbi_hartmask.h | 88 ++++++++++++++++++++++++++++++--------
lib/sbi/sbi_domain.c | 47 ++++++++------------
lib/sbi/sbi_init.c | 8 ++--
lib/sbi/sbi_ipi.c | 10 ++---
lib/sbi/sbi_system.c | 4 +-
lib/sbi/sbi_tlb.c | 4 +-
lib/utils/fdt/fdt_domain.c | 4 +-
7 files changed, 105 insertions(+), 60 deletions(-)
diff --git a/include/sbi/sbi_hartmask.h b/include/sbi/sbi_hartmask.h
index f1cef0c..105653e 100644
--- a/include/sbi/sbi_hartmask.h
+++ b/include/sbi/sbi_hartmask.h
@@ -11,6 +11,7 @@
#define __SBI_HARTMASK_H__
#include <sbi/sbi_bitmap.h>
+#include <sbi/sbi_scratch.h>
/**
* 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
diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
index 77d6ca4..ee3a5e9 100644
--- a/lib/sbi/sbi_domain.c
+++ b/lib/sbi/sbi_domain.c
@@ -65,7 +65,7 @@ static void update_hartid_to_domain(u32 hartid, struct sbi_domain *dom)
bool sbi_domain_is_assigned_hart(const struct sbi_domain *dom, u32 hartid)
{
if (dom)
- return sbi_hartmask_test_hart(hartid, &dom->assigned_harts);
+ return sbi_hartmask_test_hartid(hartid, &dom->assigned_harts);
return false;
}
@@ -73,18 +73,10 @@ bool sbi_domain_is_assigned_hart(const struct sbi_domain *dom, u32 hartid)
ulong sbi_domain_get_assigned_hartmask(const struct sbi_domain *dom,
ulong hbase)
{
- ulong ret, bword, boff;
-
- if (!dom)
- return 0;
-
- bword = BIT_WORD(hbase);
- boff = BIT_WORD_OFFSET(hbase);
-
- ret = sbi_hartmask_bits(&dom->assigned_harts)[bword++] >> boff;
- if (boff && bword < BIT_WORD(SBI_HARTMASK_MAX_BITS)) {
- ret |= (sbi_hartmask_bits(&dom->assigned_harts)[bword] &
- (BIT(boff) - 1UL)) << (BITS_PER_LONG - boff);
+ ulong ret = 0;
+ for (int i = 0; i < 8 * sizeof(ret); i++) {
+ if (sbi_domain_is_assigned_hart(dom, hbase + i))
+ ret |= 1 << i;
}
return ret;
@@ -276,7 +268,7 @@ static int sanitize_domain(const struct sbi_platform *plat,
__func__, dom->name);
return SBI_EINVAL;
}
- sbi_hartmask_for_each_hart(i, dom->possible_harts) {
+ sbi_hartmask_for_each_hart(i, j, dom->possible_harts) {
if (!sbi_hartid_valid(i)) {
sbi_printf("%s: %s possible HART mask has invalid "
"hart %d\n", __func__, dom->name, i);
@@ -400,7 +392,7 @@ bool sbi_domain_check_addr_range(const struct sbi_domain *dom,
void sbi_domain_dump(const struct sbi_domain *dom, const char *suffix)
{
- u32 i, k;
+ u32 i, j, k;
unsigned long rstart, rend;
struct sbi_domain_memregion *reg;
@@ -412,7 +404,7 @@ void sbi_domain_dump(const struct sbi_domain *dom, const char *suffix)
k = 0;
sbi_printf("Domain%d HARTs %s: ", dom->index, suffix);
- sbi_hartmask_for_each_hart(i, dom->possible_harts)
+ sbi_hartmask_for_each_hart(i, j, dom->possible_harts)
sbi_printf("%s%d%s", (k++) ? "," : "",
i, sbi_domain_is_assigned_hart(dom, i) ? "*" : "");
sbi_printf("\n");
@@ -495,7 +487,7 @@ void sbi_domain_dump_all(const char *suffix)
int sbi_domain_register(struct sbi_domain *dom,
const struct sbi_hartmask *assign_mask)
{
- u32 i;
+ u32 i, j;
int rc;
struct sbi_domain *tdom;
u32 cold_hartid = current_hartid();
@@ -538,16 +530,16 @@ int sbi_domain_register(struct sbi_domain *dom,
sbi_hartmask_clear_all(&dom->assigned_harts);
/* Assign domain to HART if HART is a possible HART */
- sbi_hartmask_for_each_hart(i, assign_mask) {
- if (!sbi_hartmask_test_hart(i, dom->possible_harts))
+ sbi_hartmask_for_each_hart(i, j, assign_mask) {
+ if (!sbi_hartmask_test_hartid(i, dom->possible_harts))
continue;
tdom = sbi_hartid_to_domain(i);
if (tdom)
- sbi_hartmask_clear_hart(i,
+ sbi_hartmask_clear_hartid(i,
&tdom->assigned_harts);
update_hartid_to_domain(i, dom);
- sbi_hartmask_set_hart(i, &dom->assigned_harts);
+ sbi_hartmask_set_hartid(i, &dom->assigned_harts);
/*
* If cold boot HART is assigned to this domain then
@@ -681,12 +673,12 @@ int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid)
continue;
/* Ignore if boot HART not possible for this domain */
- if (!sbi_hartmask_test_hart(dhart, dom->possible_harts))
+ if (!sbi_hartmask_test_hartid(dhart, dom->possible_harts))
continue;
/* Ignore if boot HART assigned different domain */
if (sbi_hartid_to_domain(dhart) != dom ||
- !sbi_hartmask_test_hart(dhart, &dom->assigned_harts))
+ !sbi_hartmask_test_hartid(dhart, &dom->assigned_harts))
continue;
/* Startup boot HART of domain */
@@ -723,6 +715,7 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
int rc;
struct sbi_hartmask *root_hmask;
struct sbi_domain_memregion *root_memregs;
+ const struct sbi_platform *plat = sbi_platform_ptr(scratch);
if (scratch->fw_rw_offset == 0 ||
(scratch->fw_rw_offset & (scratch->fw_rw_offset - 1)) != 0) {
@@ -796,11 +789,9 @@ int sbi_domain_init(struct sbi_scratch *scratch, u32 cold_hartid)
root.next_mode = scratch->next_mode;
/* Root domain possible and assigned HARTs */
- for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
- if (!sbi_hartid_valid(i))
- continue;
- sbi_hartmask_set_hart(i, root_hmask);
- }
+ for (i = 0; i < plat->hart_count; i++)
+ sbi_hartmask_set_hartid(sbi_hartindex_to_hartid(i),
+ root_hmask);
/* Finally register the root domain */
rc = sbi_domain_register(&root, root_hmask);
diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c
index 07be3d2..10dbd0a 100644
--- a/lib/sbi/sbi_init.c
+++ b/lib/sbi/sbi_init.c
@@ -205,7 +205,7 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
spin_lock(&coldboot_lock);
/* Mark current HART as waiting */
- sbi_hartmask_set_hart(hartid, &coldboot_wait_hmask);
+ sbi_hartmask_set_hartid(hartid, &coldboot_wait_hmask);
/* Release coldboot lock */
spin_unlock(&coldboot_lock);
@@ -222,7 +222,7 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
spin_lock(&coldboot_lock);
/* Unmark current HART as waiting */
- sbi_hartmask_clear_hart(hartid, &coldboot_wait_hmask);
+ sbi_hartmask_clear_hartid(hartid, &coldboot_wait_hmask);
/* Release coldboot lock */
spin_unlock(&coldboot_lock);
@@ -251,7 +251,7 @@ static void wake_coldboot_harts(struct sbi_scratch *scratch, u32 hartid)
/* Send an IPI to all HARTs waiting for coldboot */
for (u32 i = 0; i <= sbi_scratch_last_hartid(); i++) {
if ((i != hartid) &&
- sbi_hartmask_test_hart(i, &coldboot_wait_hmask))
+ sbi_hartmask_test_hartid(i, &coldboot_wait_hmask))
sbi_ipi_raw_send(i);
}
@@ -532,7 +532,7 @@ void __noreturn sbi_init(struct sbi_scratch *scratch)
if (h == hartid)
hartid_valid = true;
}
- if (SBI_HARTMASK_MAX_BITS <= hartid || !hartid_valid)
+ if (!hartid_valid)
sbi_hart_hang();
switch (scratch->next_mode) {
diff --git a/lib/sbi/sbi_ipi.c b/lib/sbi/sbi_ipi.c
index ad09154..09c8d10 100644
--- a/lib/sbi/sbi_ipi.c
+++ b/lib/sbi/sbi_ipi.c
@@ -96,7 +96,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
{
int rc;
bool retry_needed;
- ulong i, m;
+ ulong i, j, m;
struct sbi_hartmask target_mask = {0};
struct sbi_domain *dom = sbi_domain_thishart_ptr();
struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
@@ -110,14 +110,14 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
for (i = hbase; m; i++, m >>= 1) {
if (m & 1UL)
- sbi_hartmask_set_hart(i, &target_mask);
+ sbi_hartmask_set_hartid(i, &target_mask);
}
} else {
hbase = 0;
while (!sbi_hsm_hart_interruptible_mask(dom, hbase, &m)) {
for (i = hbase; m; i++, m >>= 1) {
if (m & 1UL)
- sbi_hartmask_set_hart(i, &target_mask);
+ sbi_hartmask_set_hartid(i, &target_mask);
}
hbase += BITS_PER_LONG;
}
@@ -126,12 +126,12 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
/* Send IPIs */
do {
retry_needed = false;
- sbi_hartmask_for_each_hart(i, &target_mask) {
+ sbi_hartmask_for_each_hart(i, j, &target_mask) {
rc = sbi_ipi_send(scratch, i, event, data);
if (rc == SBI_IPI_UPDATE_RETRY)
retry_needed = true;
else
- sbi_hartmask_clear_hart(i, &target_mask);
+ sbi_hartmask_clear_hartid(i, &target_mask);
}
} while (retry_needed);
diff --git a/lib/sbi/sbi_system.c b/lib/sbi/sbi_system.c
index 2e917c9..bae9730 100644
--- a/lib/sbi/sbi_system.c
+++ b/lib/sbi/sbi_system.c
@@ -152,7 +152,7 @@ int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque)
void (*jump_warmboot)(void) = (void (*)(void))scratch->warmboot_addr;
unsigned int hartid = current_hartid();
unsigned long prev_mode;
- unsigned long i;
+ unsigned long i, j;
int ret;
if (!dom || !dom->system_suspend_allowed)
@@ -170,7 +170,7 @@ int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque)
if (prev_mode != PRV_S && prev_mode != PRV_U)
return SBI_EFAIL;
- sbi_hartmask_for_each_hart(i, &dom->assigned_harts) {
+ sbi_hartmask_for_each_hart(i, j, &dom->assigned_harts) {
if (i == hartid)
continue;
if (__sbi_hsm_hart_get_state(i) != SBI_HSM_STATE_STOPPED)
diff --git a/lib/sbi/sbi_tlb.c b/lib/sbi/sbi_tlb.c
index 26a87f3..f8a1aab 100644
--- a/lib/sbi/sbi_tlb.c
+++ b/lib/sbi/sbi_tlb.c
@@ -213,13 +213,13 @@ static void tlb_pmu_incr_fw_ctr(struct sbi_tlb_info *data)
static void tlb_entry_process(struct sbi_tlb_info *tinfo)
{
- u32 rhartid;
+ u32 rhartid, rindex;
struct sbi_scratch *rscratch = NULL;
atomic_t *rtlb_sync = NULL;
tinfo->local_fn(tinfo);
- sbi_hartmask_for_each_hart(rhartid, &tinfo->smask) {
+ sbi_hartmask_for_each_hart(rhartid, rindex, &tinfo->smask) {
rscratch = sbi_hartid_to_scratch(rhartid);
if (!rscratch)
continue;
diff --git a/lib/utils/fdt/fdt_domain.c b/lib/utils/fdt/fdt_domain.c
index 788683d..fa1c357 100644
--- a/lib/utils/fdt/fdt_domain.c
+++ b/lib/utils/fdt/fdt_domain.c
@@ -342,7 +342,7 @@ static int __fdt_parse_domain(void *fdt, int domain_offset, void *opaque)
if (!fdt_node_is_enabled(fdt, cpu_offset))
continue;
- sbi_hartmask_set_hart(val32, mask);
+ sbi_hartmask_set_hartid(val32, mask);
}
}
@@ -472,7 +472,7 @@ static int __fdt_parse_domain(void *fdt, int domain_offset, void *opaque)
}
if (doffset == domain_offset)
- sbi_hartmask_set_hart(val32, &assign_mask);
+ sbi_hartmask_set_hartid(val32, &assign_mask);
}
/* Register the domain */
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 4/8] lib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
` (2 preceding siblings ...)
2023-09-04 4:03 ` [PATCH 3/8] lib: sbi: Extend sbi_hartmask to support both hartid and hartindex Anup Patel
@ 2023-09-04 4:03 ` Anup Patel
2023-09-04 4:03 ` [PATCH 5/8] lib: sbi: Prefer hartindex over hartid in IPI framework Anup Patel
` (4 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
The sbi_hartid_to_scratch() involves translating hartid to hartindex
which is expensive so let's use sbi_hartindex_to_scratch() instead.
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
lib/sbi/sbi_tlb.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/lib/sbi/sbi_tlb.c b/lib/sbi/sbi_tlb.c
index f8a1aab..1604669 100644
--- a/lib/sbi/sbi_tlb.c
+++ b/lib/sbi/sbi_tlb.c
@@ -213,14 +213,14 @@ static void tlb_pmu_incr_fw_ctr(struct sbi_tlb_info *data)
static void tlb_entry_process(struct sbi_tlb_info *tinfo)
{
- u32 rhartid, rindex;
+ u32 rindex;
struct sbi_scratch *rscratch = NULL;
atomic_t *rtlb_sync = NULL;
tinfo->local_fn(tinfo);
- sbi_hartmask_for_each_hart(rhartid, rindex, &tinfo->smask) {
- rscratch = sbi_hartid_to_scratch(rhartid);
+ sbi_hartmask_for_each_hartindex(rindex, &tinfo->smask) {
+ rscratch = sbi_hartindex_to_scratch(rindex);
if (!rscratch)
continue;
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 5/8] lib: sbi: Prefer hartindex over hartid in IPI framework
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
` (3 preceding siblings ...)
2023-09-04 4:03 ` [PATCH 4/8] lib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment Anup Patel
@ 2023-09-04 4:03 ` Anup Patel
2023-09-04 4:03 ` [PATCH 6/8] lib: sbi: Remove sbi_scratch_last_hartid() macro Anup Patel
` (3 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
Let us prefer hartindex over hartid in IPI framework which in-turn
forces IPI users to also prefer hartindex.
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
include/sbi/sbi_ipi.h | 14 +++++++-------
lib/sbi/sbi_hsm.c | 2 +-
lib/sbi/sbi_init.c | 4 ++--
lib/sbi/sbi_ipi.c | 26 +++++++++++++-------------
lib/sbi/sbi_tlb.c | 8 ++++----
lib/utils/ipi/aclint_mswi.c | 14 ++++++++------
lib/utils/ipi/andes_plicsw.c | 8 ++++++--
lib/utils/irqchip/imsic.c | 4 ++--
platform/generic/andes/ae350.c | 2 +-
9 files changed, 44 insertions(+), 38 deletions(-)
diff --git a/include/sbi/sbi_ipi.h b/include/sbi/sbi_ipi.h
index c64f422..cea8dee 100644
--- a/include/sbi/sbi_ipi.h
+++ b/include/sbi/sbi_ipi.h
@@ -23,11 +23,11 @@ struct sbi_ipi_device {
/** Name of the IPI device */
char name[32];
- /** Send IPI to a target HART */
- void (*ipi_send)(u32 target_hart);
+ /** Send IPI to a target HART index */
+ void (*ipi_send)(u32 hart_index);
- /** Clear IPI for a target HART */
- void (*ipi_clear)(u32 target_hart);
+ /** Clear IPI for a target HART index */
+ void (*ipi_clear)(u32 hart_index);
};
enum sbi_ipi_update_type {
@@ -54,7 +54,7 @@ struct sbi_ipi_event_ops {
*/
int (* update)(struct sbi_scratch *scratch,
struct sbi_scratch *remote_scratch,
- u32 remote_hartid, void *data);
+ u32 remote_hartindex, void *data);
/**
* Sync callback to wait for remote HART
@@ -85,9 +85,9 @@ int sbi_ipi_send_halt(ulong hmask, ulong hbase);
void sbi_ipi_process(void);
-int sbi_ipi_raw_send(u32 target_hart);
+int sbi_ipi_raw_send(u32 hartindex);
-void sbi_ipi_raw_clear(u32 target_hart);
+void sbi_ipi_raw_clear(u32 hartindex);
const struct sbi_ipi_device *sbi_ipi_get_device(void);
diff --git a/lib/sbi/sbi_hsm.c b/lib/sbi/sbi_hsm.c
index f870ca7..814130e 100644
--- a/lib/sbi/sbi_hsm.c
+++ b/lib/sbi/sbi_hsm.c
@@ -356,7 +356,7 @@ int sbi_hsm_hart_start(struct sbi_scratch *scratch,
(hsm_device_has_hart_secondary_boot() && !init_count)) {
rc = hsm_device_hart_start(hartid, scratch->warmboot_addr);
} else {
- rc = sbi_ipi_raw_send(hartid);
+ rc = sbi_ipi_raw_send(sbi_hartid_to_hartindex(hartid));
}
if (!rc)
diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c
index 10dbd0a..a6d96e6 100644
--- a/lib/sbi/sbi_init.c
+++ b/lib/sbi/sbi_init.c
@@ -252,7 +252,7 @@ static void wake_coldboot_harts(struct sbi_scratch *scratch, u32 hartid)
for (u32 i = 0; i <= sbi_scratch_last_hartid(); i++) {
if ((i != hartid) &&
sbi_hartmask_test_hartid(i, &coldboot_wait_hmask))
- sbi_ipi_raw_send(i);
+ sbi_ipi_raw_send(sbi_hartid_to_hartindex(i));
}
/* Release coldboot lock */
@@ -499,7 +499,7 @@ static void __noreturn init_warmboot(struct sbi_scratch *scratch, u32 hartid)
if (hstate == SBI_HSM_STATE_SUSPENDED) {
init_warm_resume(scratch, hartid);
} else {
- sbi_ipi_raw_clear(hartid);
+ sbi_ipi_raw_clear(sbi_hartid_to_hartindex(hartid));
init_warm_startup(scratch, hartid);
}
}
diff --git a/lib/sbi/sbi_ipi.c b/lib/sbi/sbi_ipi.c
index 09c8d10..42d56c7 100644
--- a/lib/sbi/sbi_ipi.c
+++ b/lib/sbi/sbi_ipi.c
@@ -31,7 +31,7 @@ static unsigned long ipi_data_off;
static const struct sbi_ipi_device *ipi_dev = NULL;
static const struct sbi_ipi_event_ops *ipi_ops_array[SBI_IPI_EVENT_MAX];
-static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartid,
+static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartindex,
u32 event, void *data)
{
int ret;
@@ -44,7 +44,7 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartid,
return SBI_EINVAL;
ipi_ops = ipi_ops_array[event];
- remote_scratch = sbi_hartid_to_scratch(remote_hartid);
+ remote_scratch = sbi_hartindex_to_scratch(remote_hartindex);
if (!remote_scratch)
return SBI_EINVAL;
@@ -52,7 +52,7 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartid,
if (ipi_ops->update) {
ret = ipi_ops->update(scratch, remote_scratch,
- remote_hartid, data);
+ remote_hartindex, data);
if (ret != SBI_IPI_UPDATE_SUCCESS)
return ret;
}
@@ -65,7 +65,7 @@ static int sbi_ipi_send(struct sbi_scratch *scratch, u32 remote_hartid,
smp_wmb();
if (ipi_dev && ipi_dev->ipi_send)
- ipi_dev->ipi_send(remote_hartid);
+ ipi_dev->ipi_send(remote_hartindex);
sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_SENT);
@@ -96,7 +96,7 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
{
int rc;
bool retry_needed;
- ulong i, j, m;
+ ulong i, m;
struct sbi_hartmask target_mask = {0};
struct sbi_domain *dom = sbi_domain_thishart_ptr();
struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
@@ -126,12 +126,12 @@ int sbi_ipi_send_many(ulong hmask, ulong hbase, u32 event, void *data)
/* Send IPIs */
do {
retry_needed = false;
- sbi_hartmask_for_each_hart(i, j, &target_mask) {
+ sbi_hartmask_for_each_hartindex(i, &target_mask) {
rc = sbi_ipi_send(scratch, i, event, data);
if (rc == SBI_IPI_UPDATE_RETRY)
retry_needed = true;
else
- sbi_hartmask_clear_hartid(i, &target_mask);
+ sbi_hartmask_clear_hartindex(i, &target_mask);
}
} while (retry_needed);
@@ -214,11 +214,11 @@ void sbi_ipi_process(void)
struct sbi_scratch *scratch = sbi_scratch_thishart_ptr();
struct sbi_ipi_data *ipi_data =
sbi_scratch_offset_ptr(scratch, ipi_data_off);
- u32 hartid = current_hartid();
+ u32 hartindex = sbi_hartid_to_hartindex(current_hartid());
sbi_pmu_ctr_incr_fw(SBI_PMU_FW_IPI_RECVD);
if (ipi_dev && ipi_dev->ipi_clear)
- ipi_dev->ipi_clear(hartid);
+ ipi_dev->ipi_clear(hartindex);
ipi_type = atomic_raw_xchg_ulong(&ipi_data->ipi_type, 0);
ipi_event = 0;
@@ -233,19 +233,19 @@ void sbi_ipi_process(void)
}
}
-int sbi_ipi_raw_send(u32 target_hart)
+int sbi_ipi_raw_send(u32 hartindex)
{
if (!ipi_dev || !ipi_dev->ipi_send)
return SBI_EINVAL;
- ipi_dev->ipi_send(target_hart);
+ ipi_dev->ipi_send(hartindex);
return 0;
}
-void sbi_ipi_raw_clear(u32 target_hart)
+void sbi_ipi_raw_clear(u32 hartindex)
{
if (ipi_dev && ipi_dev->ipi_clear)
- ipi_dev->ipi_clear(target_hart);
+ ipi_dev->ipi_clear(hartindex);
}
const struct sbi_ipi_device *sbi_ipi_get_device(void)
diff --git a/lib/sbi/sbi_tlb.c b/lib/sbi/sbi_tlb.c
index 1604669..b1bd7dc 100644
--- a/lib/sbi/sbi_tlb.c
+++ b/lib/sbi/sbi_tlb.c
@@ -333,7 +333,7 @@ static int tlb_update_cb(void *in, void *data)
static int tlb_update(struct sbi_scratch *scratch,
struct sbi_scratch *remote_scratch,
- u32 remote_hartid, void *data)
+ u32 remote_hartindex, void *data)
{
int ret;
atomic_t *tlb_sync;
@@ -355,7 +355,7 @@ static int tlb_update(struct sbi_scratch *scratch,
* If the request is to queue a tlb flush entry for itself
* then just do a local flush and return;
*/
- if (remote_hartid == curr_hartid) {
+ if (sbi_hartindex_to_hartid(remote_hartindex) == curr_hartid) {
tinfo->local_fn(tinfo);
return SBI_IPI_UPDATE_BREAK;
}
@@ -374,8 +374,8 @@ static int tlb_update(struct sbi_scratch *scratch,
* this properly.
*/
tlb_process_once(scratch);
- sbi_dprintf("hart%d: hart%d tlb fifo full\n",
- curr_hartid, remote_hartid);
+ sbi_dprintf("hart%d: hart%d tlb fifo full\n", curr_hartid,
+ sbi_hartindex_to_hartid(remote_hartindex));
return SBI_IPI_UPDATE_RETRY;
}
diff --git a/lib/utils/ipi/aclint_mswi.c b/lib/utils/ipi/aclint_mswi.c
index 140a49b..a3bfb4a 100644
--- a/lib/utils/ipi/aclint_mswi.c
+++ b/lib/utils/ipi/aclint_mswi.c
@@ -25,13 +25,13 @@ static unsigned long mswi_ptr_offset;
#define mswi_set_hart_data_ptr(__scratch, __mswi) \
sbi_scratch_write_type((__scratch), void *, mswi_ptr_offset, (__mswi))
-static void mswi_ipi_send(u32 target_hart)
+static void mswi_ipi_send(u32 hart_index)
{
u32 *msip;
struct sbi_scratch *scratch;
struct aclint_mswi_data *mswi;
- scratch = sbi_hartid_to_scratch(target_hart);
+ scratch = sbi_hartindex_to_scratch(hart_index);
if (!scratch)
return;
@@ -41,16 +41,17 @@ static void mswi_ipi_send(u32 target_hart)
/* Set ACLINT IPI */
msip = (void *)mswi->addr;
- writel(1, &msip[target_hart - mswi->first_hartid]);
+ writel(1, &msip[sbi_hartindex_to_hartid(hart_index) -
+ mswi->first_hartid]);
}
-static void mswi_ipi_clear(u32 target_hart)
+static void mswi_ipi_clear(u32 hart_index)
{
u32 *msip;
struct sbi_scratch *scratch;
struct aclint_mswi_data *mswi;
- scratch = sbi_hartid_to_scratch(target_hart);
+ scratch = sbi_hartindex_to_scratch(hart_index);
if (!scratch)
return;
@@ -60,7 +61,8 @@ static void mswi_ipi_clear(u32 target_hart)
/* Clear ACLINT IPI */
msip = (void *)mswi->addr;
- writel(0, &msip[target_hart - mswi->first_hartid]);
+ writel(0, &msip[sbi_hartindex_to_hartid(hart_index) -
+ mswi->first_hartid]);
}
static struct sbi_ipi_device aclint_mswi = {
diff --git a/lib/utils/ipi/andes_plicsw.c b/lib/utils/ipi/andes_plicsw.c
index db25ae2..dde39c0 100644
--- a/lib/utils/ipi/andes_plicsw.c
+++ b/lib/utils/ipi/andes_plicsw.c
@@ -68,8 +68,10 @@ static inline void plic_sw_pending(u32 target_hart)
writel(val, (void *)plicsw.addr + PLICSW_PENDING_BASE + word_index * 4);
}
-static void plicsw_ipi_send(u32 target_hart)
+static void plicsw_ipi_send(u32 hart_index)
{
+ u32 target_hart = sbi_hartindex_to_hartid(hart_index);
+
if (plicsw.hart_count <= target_hart)
ebreak();
@@ -77,8 +79,10 @@ static void plicsw_ipi_send(u32 target_hart)
plic_sw_pending(target_hart);
}
-static void plicsw_ipi_clear(u32 target_hart)
+static void plicsw_ipi_clear(u32 hart_index)
{
+ u32 target_hart = sbi_hartindex_to_hartid(hart_index);
+
if (plicsw.hart_count <= target_hart)
ebreak();
diff --git a/lib/utils/irqchip/imsic.c b/lib/utils/irqchip/imsic.c
index 7fc61d9..78f5895 100644
--- a/lib/utils/irqchip/imsic.c
+++ b/lib/utils/irqchip/imsic.c
@@ -161,7 +161,7 @@ static int imsic_external_irqfn(struct sbi_trap_regs *regs)
return 0;
}
-static void imsic_ipi_send(u32 target_hart)
+static void imsic_ipi_send(u32 hart_index)
{
unsigned long reloff;
struct imsic_regs *regs;
@@ -169,7 +169,7 @@ static void imsic_ipi_send(u32 target_hart)
struct sbi_scratch *scratch;
int file;
- scratch = sbi_hartid_to_scratch(target_hart);
+ scratch = sbi_hartindex_to_scratch(hart_index);
if (!scratch)
return;
diff --git a/platform/generic/andes/ae350.c b/platform/generic/andes/ae350.c
index 01bd02d..80eca05 100644
--- a/platform/generic/andes/ae350.c
+++ b/platform/generic/andes/ae350.c
@@ -33,7 +33,7 @@ static int ae350_hart_start(u32 hartid, ulong saddr)
{
/* Don't send wakeup command@boot-time */
if (!sbi_init_count(hartid) || (is_andes25() && hartid == 0))
- return sbi_ipi_raw_send(hartid);
+ return sbi_ipi_raw_send(sbi_hartid_to_hartindex(hartid));
/* Write wakeup command to the sleep hart */
smu_set_command(&smu, WAKEUP_CMD, hartid);
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 6/8] lib: sbi: Remove sbi_scratch_last_hartid() macro
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
` (4 preceding siblings ...)
2023-09-04 4:03 ` [PATCH 5/8] lib: sbi: Prefer hartindex over hartid in IPI framework Anup Patel
@ 2023-09-04 4:03 ` Anup Patel
2023-09-04 4:03 ` [PATCH 7/8] lib: sbi: Maximize the use of HART index in sbi_domain Anup Patel
` (2 subsequent siblings)
8 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
The sbi_scratch_last_hartid() macro is not of much use on platforms
with really sparse hartids so let us replace use of this macro with
other approaches.
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
include/sbi/sbi_scratch.h | 6 ------
lib/sbi/sbi_hsm.c | 28 +++++++++++++---------------
lib/sbi/sbi_init.c | 10 ++++++----
lib/sbi/sbi_scratch.c | 5 +----
4 files changed, 20 insertions(+), 29 deletions(-)
diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
index 9a4dce1..e6a33ba 100644
--- a/include/sbi/sbi_scratch.h
+++ b/include/sbi/sbi_scratch.h
@@ -244,12 +244,6 @@ u32 sbi_hartid_to_hartindex(u32 hartid);
#define sbi_hartid_to_scratch(__hartid) \
sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
-/** Last HART id having a sbi_scratch pointer */
-extern u32 last_hartid_having_scratch;
-
-/** Get last HART id having a sbi_scratch pointer */
-#define sbi_scratch_last_hartid() last_hartid_having_scratch
-
/** Check whether particular HART id is valid or not */
#define sbi_hartid_valid(__hartid) \
sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
diff --git a/lib/sbi/sbi_hsm.c b/lib/sbi/sbi_hsm.c
index 814130e..147f954 100644
--- a/lib/sbi/sbi_hsm.c
+++ b/lib/sbi/sbi_hsm.c
@@ -115,23 +115,21 @@ int sbi_hsm_hart_interruptible_mask(const struct sbi_domain *dom,
{
int hstate;
ulong i, hmask, dmask;
- ulong hend = sbi_scratch_last_hartid() + 1;
*out_hmask = 0;
- if (hend <= hbase)
+ if (!sbi_hartid_valid(hbase))
return SBI_EINVAL;
- if (BITS_PER_LONG < (hend - hbase))
- hend = hbase + BITS_PER_LONG;
dmask = sbi_domain_get_assigned_hartmask(dom, hbase);
- for (i = hbase; i < hend; i++) {
- hmask = 1UL << (i - hbase);
- if (dmask & hmask) {
- hstate = __sbi_hsm_hart_get_state(i);
- if (hstate == SBI_HSM_STATE_STARTED ||
- hstate == SBI_HSM_STATE_SUSPENDED)
- *out_hmask |= hmask;
- }
+ for (i = 0; i < BITS_PER_LONG; i++) {
+ hmask = 1UL << i;
+ if (!(dmask & hmask))
+ continue;
+
+ hstate = __sbi_hsm_hart_get_state(hbase + i);
+ if (hstate == SBI_HSM_STATE_STARTED ||
+ hstate == SBI_HSM_STATE_SUSPENDED)
+ *out_hmask |= hmask;
}
return 0;
@@ -249,15 +247,15 @@ int sbi_hsm_init(struct sbi_scratch *scratch, u32 hartid, bool cold_boot)
return SBI_ENOMEM;
/* Initialize hart state data for every hart */
- for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
- rscratch = sbi_hartid_to_scratch(i);
+ for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
+ rscratch = sbi_hartindex_to_scratch(i);
if (!rscratch)
continue;
hdata = sbi_scratch_offset_ptr(rscratch,
hart_data_offset);
ATOMIC_INIT(&hdata->state,
- (i == hartid) ?
+ (sbi_hartindex_to_hartid(i) == hartid) ?
SBI_HSM_STATE_START_PENDING :
SBI_HSM_STATE_STOPPED);
ATOMIC_INIT(&hdata->start_ticket, 0);
diff --git a/lib/sbi/sbi_init.c b/lib/sbi/sbi_init.c
index a6d96e6..e723553 100644
--- a/lib/sbi/sbi_init.c
+++ b/lib/sbi/sbi_init.c
@@ -242,6 +242,8 @@ static void wait_for_coldboot(struct sbi_scratch *scratch, u32 hartid)
static void wake_coldboot_harts(struct sbi_scratch *scratch, u32 hartid)
{
+ u32 i, hartindex = sbi_hartid_to_hartindex(hartid);
+
/* Mark coldboot done */
__smp_store_release(&coldboot_done, 1);
@@ -249,10 +251,10 @@ static void wake_coldboot_harts(struct sbi_scratch *scratch, u32 hartid)
spin_lock(&coldboot_lock);
/* Send an IPI to all HARTs waiting for coldboot */
- for (u32 i = 0; i <= sbi_scratch_last_hartid(); i++) {
- if ((i != hartid) &&
- sbi_hartmask_test_hartid(i, &coldboot_wait_hmask))
- sbi_ipi_raw_send(sbi_hartid_to_hartindex(i));
+ sbi_hartmask_for_each_hartindex(i, &coldboot_wait_hmask) {
+ if (i == hartindex)
+ continue;
+ sbi_ipi_raw_send(i);
}
/* Release coldboot lock */
diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
index d2abc89..6aeb0ca 100644
--- a/lib/sbi/sbi_scratch.c
+++ b/lib/sbi/sbi_scratch.c
@@ -14,7 +14,6 @@
#include <sbi/sbi_scratch.h>
#include <sbi/sbi_string.h>
-u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
u32 last_hartindex_having_scratch = 0;
u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
@@ -45,10 +44,8 @@ int sbi_scratch_init(struct sbi_scratch *scratch)
hartindex_to_hartid_table[i] = h;
hartindex_to_scratch_table[i] =
((hartid2scratch)scratch->hartid_to_scratch)(h, i);
- if (hartindex_to_scratch_table[i]) {
- last_hartid_having_scratch = h;
+ if (hartindex_to_scratch_table[i])
last_hartindex_having_scratch = i;
- }
}
return 0;
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 7/8] lib: sbi: Maximize the use of HART index in sbi_domain
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
` (5 preceding siblings ...)
2023-09-04 4:03 ` [PATCH 6/8] lib: sbi: Remove sbi_scratch_last_hartid() macro Anup Patel
@ 2023-09-04 4:03 ` Anup Patel
2023-09-04 4:03 ` [PATCH 8/8] include: sbi: Remove sbi_hartmask_for_each_hart() macro Anup Patel
2023-09-24 6:34 ` [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
8 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
Let us maximize the use of HART index in sbi_domain because hartindex
based hartmask access and sbi_scratch lookup is faster.
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
include/sbi/sbi_domain.h | 6 ++---
lib/sbi/sbi_domain.c | 56 +++++++++++++++++++++-------------------
2 files changed, 33 insertions(+), 29 deletions(-)
diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h
index da2a65a..e10daff 100644
--- a/include/sbi/sbi_domain.h
+++ b/include/sbi/sbi_domain.h
@@ -201,12 +201,12 @@ struct sbi_domain {
/** The root domain instance */
extern struct sbi_domain root;
-/** Get pointer to sbi_domain from HART id */
-struct sbi_domain *sbi_hartid_to_domain(u32 hartid);
+/** Get pointer to sbi_domain from HART index */
+struct sbi_domain *sbi_hartindex_to_domain(u32 hartindex);
/** Get pointer to sbi_domain for current HART */
#define sbi_domain_thishart_ptr() \
- sbi_hartid_to_domain(current_hartid())
+ sbi_hartindex_to_domain(sbi_hartid_to_hartindex(current_hartid()))
/** Index to domain table */
extern struct sbi_domain *domidx_to_domain_table[];
diff --git a/lib/sbi/sbi_domain.c b/lib/sbi/sbi_domain.c
index ee3a5e9..b1f485d 100644
--- a/lib/sbi/sbi_domain.c
+++ b/lib/sbi/sbi_domain.c
@@ -40,22 +40,22 @@ struct sbi_domain root = {
static unsigned long domain_hart_ptr_offset;
-struct sbi_domain *sbi_hartid_to_domain(u32 hartid)
+struct sbi_domain *sbi_hartindex_to_domain(u32 hartindex)
{
struct sbi_scratch *scratch;
- scratch = sbi_hartid_to_scratch(hartid);
+ scratch = sbi_hartindex_to_scratch(hartindex);
if (!scratch || !domain_hart_ptr_offset)
return NULL;
return sbi_scratch_read_type(scratch, void *, domain_hart_ptr_offset);
}
-static void update_hartid_to_domain(u32 hartid, struct sbi_domain *dom)
+static void update_hartindex_to_domain(u32 hartindex, struct sbi_domain *dom)
{
struct sbi_scratch *scratch;
- scratch = sbi_hartid_to_scratch(hartid);
+ scratch = sbi_hartindex_to_scratch(hartindex);
if (!scratch)
return;
@@ -268,10 +268,11 @@ static int sanitize_domain(const struct sbi_platform *plat,
__func__, dom->name);
return SBI_EINVAL;
}
- sbi_hartmask_for_each_hart(i, j, dom->possible_harts) {
- if (!sbi_hartid_valid(i)) {
+ sbi_hartmask_for_each_hartindex(i, dom->possible_harts) {
+ if (!sbi_hartindex_valid(i)) {
sbi_printf("%s: %s possible HART mask has invalid "
- "hart %d\n", __func__, dom->name, i);
+ "hart %d\n", __func__,
+ dom->name, sbi_hartindex_to_hartid(i));
return SBI_EINVAL;
}
}
@@ -404,9 +405,11 @@ void sbi_domain_dump(const struct sbi_domain *dom, const char *suffix)
k = 0;
sbi_printf("Domain%d HARTs %s: ", dom->index, suffix);
- sbi_hartmask_for_each_hart(i, j, dom->possible_harts)
+ sbi_hartmask_for_each_hartindex(i, dom->possible_harts) {
+ j = sbi_hartindex_to_hartid(i);
sbi_printf("%s%d%s", (k++) ? "," : "",
- i, sbi_domain_is_assigned_hart(dom, i) ? "*" : "");
+ j, sbi_domain_is_assigned_hart(dom, j) ? "*" : "");
+ }
sbi_printf("\n");
i = 0;
@@ -487,7 +490,7 @@ void sbi_domain_dump_all(const char *suffix)
int sbi_domain_register(struct sbi_domain *dom,
const struct sbi_hartmask *assign_mask)
{
- u32 i, j;
+ u32 i;
int rc;
struct sbi_domain *tdom;
u32 cold_hartid = current_hartid();
@@ -530,22 +533,22 @@ int sbi_domain_register(struct sbi_domain *dom,
sbi_hartmask_clear_all(&dom->assigned_harts);
/* Assign domain to HART if HART is a possible HART */
- sbi_hartmask_for_each_hart(i, j, assign_mask) {
- if (!sbi_hartmask_test_hartid(i, dom->possible_harts))
+ sbi_hartmask_for_each_hartindex(i, assign_mask) {
+ if (!sbi_hartmask_test_hartindex(i, dom->possible_harts))
continue;
- tdom = sbi_hartid_to_domain(i);
+ tdom = sbi_hartindex_to_domain(i);
if (tdom)
- sbi_hartmask_clear_hartid(i,
+ sbi_hartmask_clear_hartindex(i,
&tdom->assigned_harts);
- update_hartid_to_domain(i, dom);
- sbi_hartmask_set_hartid(i, &dom->assigned_harts);
+ update_hartindex_to_domain(i, dom);
+ sbi_hartmask_set_hartindex(i, &dom->assigned_harts);
/*
* If cold boot HART is assigned to this domain then
* override boot HART of this domain.
*/
- if (i == cold_hartid &&
+ if (sbi_hartindex_to_hartid(i) == cold_hartid &&
dom->boot_hartid != cold_hartid) {
sbi_printf("Domain%d Boot HARTID forced to"
" %d\n", dom->index, cold_hartid);
@@ -665,36 +668,37 @@ int sbi_domain_finalize(struct sbi_scratch *scratch, u32 cold_hartid)
/* Startup boot HART of domains */
sbi_domain_for_each(i, dom) {
- /* Domain boot HART */
- dhart = dom->boot_hartid;
+ /* Domain boot HART index */
+ dhart = sbi_hartid_to_hartindex(dom->boot_hartid);
/* Ignore of boot HART is off limits */
- if (SBI_HARTMASK_MAX_BITS <= dhart)
+ if (!sbi_hartindex_valid(dhart))
continue;
/* Ignore if boot HART not possible for this domain */
- if (!sbi_hartmask_test_hartid(dhart, dom->possible_harts))
+ if (!sbi_hartmask_test_hartindex(dhart, dom->possible_harts))
continue;
/* Ignore if boot HART assigned different domain */
- if (sbi_hartid_to_domain(dhart) != dom ||
- !sbi_hartmask_test_hartid(dhart, &dom->assigned_harts))
+ if (sbi_hartindex_to_domain(dhart) != dom ||
+ !sbi_hartmask_test_hartindex(dhart, &dom->assigned_harts))
continue;
/* Startup boot HART of domain */
- if (dhart == cold_hartid) {
+ if (dom->boot_hartid == cold_hartid) {
scratch->next_addr = dom->next_addr;
scratch->next_mode = dom->next_mode;
scratch->next_arg1 = dom->next_arg1;
} else {
- rc = sbi_hsm_hart_start(scratch, NULL, dhart,
+ rc = sbi_hsm_hart_start(scratch, NULL,
+ dom->boot_hartid,
dom->next_addr,
dom->next_mode,
dom->next_arg1);
if (rc) {
sbi_printf("%s: failed to start boot HART %d"
" for %s (error %d)\n", __func__,
- dhart, dom->name, rc);
+ dom->boot_hartid, dom->name, rc);
return rc;
}
}
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 8/8] include: sbi: Remove sbi_hartmask_for_each_hart() macro
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
` (6 preceding siblings ...)
2023-09-04 4:03 ` [PATCH 7/8] lib: sbi: Maximize the use of HART index in sbi_domain Anup Patel
@ 2023-09-04 4:03 ` Anup Patel
2023-09-24 6:34 ` [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
8 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-04 4:03 UTC (permalink / raw)
To: opensbi
The sbi_hartmask_for_each_hart() macro is slow and has only one user
so let us completely remove the sbi_hartmask_for_each_hart() macro.
Signed-off-by: Anup Patel <apatel@ventanamicro.com>
---
include/sbi/sbi_hartmask.h | 13 -------------
lib/sbi/sbi_system.c | 3 ++-
2 files changed, 2 insertions(+), 14 deletions(-)
diff --git a/include/sbi/sbi_hartmask.h b/include/sbi/sbi_hartmask.h
index 105653e..bcfa50d 100644
--- a/include/sbi/sbi_hartmask.h
+++ b/include/sbi/sbi_hartmask.h
@@ -169,19 +169,6 @@ static inline void sbi_hartmask_xor(struct sbi_hartmask *dstp,
sbi_hartmask_bits(src2p), 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
diff --git a/lib/sbi/sbi_system.c b/lib/sbi/sbi_system.c
index bae9730..d1fa349 100644
--- a/lib/sbi/sbi_system.c
+++ b/lib/sbi/sbi_system.c
@@ -170,7 +170,8 @@ int sbi_system_suspend(u32 sleep_type, ulong resume_addr, ulong opaque)
if (prev_mode != PRV_S && prev_mode != PRV_U)
return SBI_EFAIL;
- sbi_hartmask_for_each_hart(i, j, &dom->assigned_harts) {
+ sbi_hartmask_for_each_hartindex(j, &dom->assigned_harts) {
+ i = sbi_hartindex_to_hartid(j);
if (i == hartid)
continue;
if (__sbi_hsm_hart_get_state(i) != SBI_HSM_STATE_STOPPED)
--
2.34.1
^ permalink raw reply related [flat|nested] 17+ messages in thread
* [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch
2023-09-04 4:03 ` [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch Anup Patel
@ 2023-09-20 17:54 ` Xiang W
2023-09-22 5:44 ` Anup Patel
0 siblings, 1 reply; 17+ messages in thread
From: Xiang W @ 2023-09-20 17:54 UTC (permalink / raw)
To: opensbi
? 2023-09-04???? 09:33 +0530?Anup Patel???
> We introduce HART index and related helper functions in sbi_scratch
> where HART index is contiguous and each HART index maps to a physical
> HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.
>
> The HART index to HART id mapping follows the index2id mapping provided
> by the platform. If the platform does not provide index2id mapping then
> identity mapping is assumed.
>
> Signed-off-by: Anup Patel <apatel@ventanamicro.com>
> ---
> ?include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
> ?lib/sbi/sbi_scratch.c???? | 38 ++++++++++++++++++++++-----------
> ?2 files changed, 68 insertions(+), 15 deletions(-)
>
> diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
> index 4801492..9a4dce1 100644
> --- a/include/sbi/sbi_scratch.h
> +++ b/include/sbi/sbi_scratch.h
> @@ -202,12 +202,47 @@ do { \
> ? = (__type)(__ptr); \
> ?} while (0)
> ?
> -/** HART id to scratch table */
> -extern struct sbi_scratch *hartid_to_scratch_table[];
> +/** Last HART index having a sbi_scratch pointer */
> +extern u32 last_hartindex_having_scratch;
> +
> +/** Get last HART index having a sbi_scratch pointer */
> +#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
> +
> +/** Check whether a particular HART index is valid or not */
> +#define sbi_hartindex_valid(__hartindex) \
> +(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
The implementation here is strange. If the last index does not have scratch,
it will be considered an invalid index. But other index do not be considered
invalid without scratch.
Regards,
Xiang W
> +
> +/** HART index to HART id table */
> +extern u32 hartindex_to_hartid_table[];
> +
> +/** Get sbi_scratch from HART index */
> +#define sbi_hartindex_to_hartid(__hartindex) \
> +({ \
> + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> + hartindex_to_hartid_table[__hartindex] : -1U; \
> +})
> +
> +/** HART index to scratch table */
> +extern struct sbi_scratch *hartindex_to_scratch_table[];
> +
> +/** Get sbi_scratch from HART index */
> +#define sbi_hartindex_to_scratch(__hartindex) \
> +({ \
> + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> + hartindex_to_scratch_table[__hartindex] : NULL;\
> +})
> +
> +/**
> + * Get logical index for given HART id
> + * @param hartid physical HART id
> + * @returns value between 0 to SBI_HARTMASK_MAX_BITS upon success and
> + * ??? SBI_HARTMASK_MAX_BITS upon failure.
> + */
> +u32 sbi_hartid_to_hartindex(u32 hartid);
> ?
> ?/** Get sbi_scratch from HART id */
> ?#define sbi_hartid_to_scratch(__hartid) \
> - hartid_to_scratch_table[__hartid]
> + sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
> ?
> ?/** Last HART id having a sbi_scratch pointer */
> ?extern u32 last_hartid_having_scratch;
> @@ -215,6 +250,10 @@ extern u32 last_hartid_having_scratch;
> ?/** Get last HART id having a sbi_scratch pointer */
> ?#define sbi_scratch_last_hartid() last_hartid_having_scratch
> ?
> +/** Check whether particular HART id is valid or not */
> +#define sbi_hartid_valid(__hartid) \
> + sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
> +
> ?#endif
> ?
> ?#endif
> diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
> index 87ef84c..d2abc89 100644
> --- a/lib/sbi/sbi_scratch.c
> +++ b/lib/sbi/sbi_scratch.c
> @@ -15,26 +15,40 @@
> ?#include <sbi/sbi_string.h>
> ?
> ?u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
> -struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
> +u32 last_hartindex_having_scratch = 0;
> +u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
> +struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
> ?
> ?static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
> ?static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
> ?
> +u32 sbi_hartid_to_hartindex(u32 hartid)
> +{
> + u32 i;
> +
> + for (i = 0; i <= last_hartindex_having_scratch; i++)
> + if (hartindex_to_hartid_table[i] == hartid)
> + return i;
> +
> + return -1U;
> +}
> +
> ?typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
> ?
> ?int sbi_scratch_init(struct sbi_scratch *scratch)
> ?{
> - u32 i;
> + u32 i, h;
> ? const struct sbi_platform *plat = sbi_platform_ptr(scratch);
> ?
> - for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
> - if (sbi_platform_hart_invalid(plat, i))
> - continue;
> - hartid_to_scratch_table[i] =
> - ((hartid2scratch)scratch->hartid_to_scratch)(i,
> - sbi_platform_hart_index(plat, i));
> - if (hartid_to_scratch_table[i])
> - last_hartid_having_scratch = i;
> + for (i = 0; i < plat->hart_count; i++) {
> + h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
> + hartindex_to_hartid_table[i] = h;
> + hartindex_to_scratch_table[i] =
> + ((hartid2scratch)scratch->hartid_to_scratch)(h, i);
> + if (hartindex_to_scratch_table[i]) {
> + last_hartid_having_scratch = h;
> + last_hartindex_having_scratch = i;
> + }
> ? }
> ?
> ? return 0;
> @@ -74,8 +88,8 @@ done:
> ? spin_unlock(&extra_lock);
> ?
> ? if (ret) {
> - for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
> - rscratch = sbi_hartid_to_scratch(i);
> + for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
> + rscratch = sbi_hartindex_to_scratch(i);
> ? if (!rscratch)
> ? continue;
> ? ptr = sbi_scratch_offset_ptr(rscratch, ret);
> --
> 2.34.1
>
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch
2023-09-20 17:54 ` Xiang W
@ 2023-09-22 5:44 ` Anup Patel
2023-09-22 7:56 ` Xiang W
0 siblings, 1 reply; 17+ messages in thread
From: Anup Patel @ 2023-09-22 5:44 UTC (permalink / raw)
To: opensbi
On Wed, Sep 20, 2023 at 11:24?PM Xiang W <wxjstz@126.com> wrote:
>
> ? 2023-09-04???? 09:33 +0530?Anup Patel???
> > We introduce HART index and related helper functions in sbi_scratch
> > where HART index is contiguous and each HART index maps to a physical
> > HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.
> >
> > The HART index to HART id mapping follows the index2id mapping provided
> > by the platform. If the platform does not provide index2id mapping then
> > identity mapping is assumed.
> >
> > Signed-off-by: Anup Patel <apatel@ventanamicro.com>
> > ---
> > include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
> > lib/sbi/sbi_scratch.c | 38 ++++++++++++++++++++++-----------
> > 2 files changed, 68 insertions(+), 15 deletions(-)
> >
> > diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
> > index 4801492..9a4dce1 100644
> > --- a/include/sbi/sbi_scratch.h
> > +++ b/include/sbi/sbi_scratch.h
> > @@ -202,12 +202,47 @@ do { \
> > = (__type)(__ptr); \
> > } while (0)
> >
> > -/** HART id to scratch table */
> > -extern struct sbi_scratch *hartid_to_scratch_table[];
> > +/** Last HART index having a sbi_scratch pointer */
> > +extern u32 last_hartindex_having_scratch;
> > +
> > +/** Get last HART index having a sbi_scratch pointer */
> > +#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
> > +
> > +/** Check whether a particular HART index is valid or not */
> > +#define sbi_hartindex_valid(__hartindex) \
> > +(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
> The implementation here is strange. If the last index does not have scratch,
> it will be considered an invalid index. But other index do not be considered
> invalid without scratch.
I don't understand. The "last_hartindex" is literally the index of the last
HART having a scratch.
Regards,
Anup
>
> Regards,
> Xiang W
> > +
> > +/** HART index to HART id table */
> > +extern u32 hartindex_to_hartid_table[];
> > +
> > +/** Get sbi_scratch from HART index */
> > +#define sbi_hartindex_to_hartid(__hartindex) \
> > +({ \
> > + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > + hartindex_to_hartid_table[__hartindex] : -1U; \
> > +})
> > +
> > +/** HART index to scratch table */
> > +extern struct sbi_scratch *hartindex_to_scratch_table[];
> > +
> > +/** Get sbi_scratch from HART index */
> > +#define sbi_hartindex_to_scratch(__hartindex) \
> > +({ \
> > + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > + hartindex_to_scratch_table[__hartindex] : NULL;\
> > +})
> > +
> > +/**
> > + * Get logical index for given HART id
> > + * @param hartid physical HART id
> > + * @returns value between 0 to SBI_HARTMASK_MAX_BITS upon success and
> > + * SBI_HARTMASK_MAX_BITS upon failure.
> > + */
> > +u32 sbi_hartid_to_hartindex(u32 hartid);
> >
> > /** Get sbi_scratch from HART id */
> > #define sbi_hartid_to_scratch(__hartid) \
> > - hartid_to_scratch_table[__hartid]
> > + sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
> >
> > /** Last HART id having a sbi_scratch pointer */
> > extern u32 last_hartid_having_scratch;
> > @@ -215,6 +250,10 @@ extern u32 last_hartid_having_scratch;
> > /** Get last HART id having a sbi_scratch pointer */
> > #define sbi_scratch_last_hartid() last_hartid_having_scratch
> >
> > +/** Check whether particular HART id is valid or not */
> > +#define sbi_hartid_valid(__hartid) \
> > + sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
> > +
> > #endif
> >
> > #endif
> > diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
> > index 87ef84c..d2abc89 100644
> > --- a/lib/sbi/sbi_scratch.c
> > +++ b/lib/sbi/sbi_scratch.c
> > @@ -15,26 +15,40 @@
> > #include <sbi/sbi_string.h>
> >
> > u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
> > -struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
> > +u32 last_hartindex_having_scratch = 0;
> > +u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
> > +struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
> >
> > static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
> > static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
> >
> > +u32 sbi_hartid_to_hartindex(u32 hartid)
> > +{
> > + u32 i;
> > +
> > + for (i = 0; i <= last_hartindex_having_scratch; i++)
> > + if (hartindex_to_hartid_table[i] == hartid)
> > + return i;
> > +
> > + return -1U;
> > +}
> > +
> > typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
> >
> > int sbi_scratch_init(struct sbi_scratch *scratch)
> > {
> > - u32 i;
> > + u32 i, h;
> > const struct sbi_platform *plat = sbi_platform_ptr(scratch);
> >
> > - for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
> > - if (sbi_platform_hart_invalid(plat, i))
> > - continue;
> > - hartid_to_scratch_table[i] =
> > - ((hartid2scratch)scratch->hartid_to_scratch)(i,
> > - sbi_platform_hart_index(plat, i));
> > - if (hartid_to_scratch_table[i])
> > - last_hartid_having_scratch = i;
> > + for (i = 0; i < plat->hart_count; i++) {
> > + h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
> > + hartindex_to_hartid_table[i] = h;
> > + hartindex_to_scratch_table[i] =
> > + ((hartid2scratch)scratch->hartid_to_scratch)(h, i);
> > + if (hartindex_to_scratch_table[i]) {
> > + last_hartid_having_scratch = h;
> > + last_hartindex_having_scratch = i;
> > + }
> > }
> >
> > return 0;
> > @@ -74,8 +88,8 @@ done:
> > spin_unlock(&extra_lock);
> >
> > if (ret) {
> > - for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
> > - rscratch = sbi_hartid_to_scratch(i);
> > + for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
> > + rscratch = sbi_hartindex_to_scratch(i);
> > if (!rscratch)
> > continue;
> > ptr = sbi_scratch_offset_ptr(rscratch, ret);
> > --
> > 2.34.1
> >
> >
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch
2023-09-22 5:44 ` Anup Patel
@ 2023-09-22 7:56 ` Xiang W
2023-09-22 8:23 ` Anup Patel
2023-09-22 8:38 ` Anup Patel
0 siblings, 2 replies; 17+ messages in thread
From: Xiang W @ 2023-09-22 7:56 UTC (permalink / raw)
To: opensbi
? 2023-09-22???? 11:14 +0530?Anup Patel???
> On Wed, Sep 20, 2023 at 11:24?PM Xiang W <wxjstz@126.com> wrote:
> >
> > ? 2023-09-04???? 09:33 +0530?Anup Patel???
> > > We introduce HART index and related helper functions in sbi_scratch
> > > where HART index is contiguous and each HART index maps to a physical
> > > HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.
> > >
> > > The HART index to HART id mapping follows the index2id mapping provided
> > > by the platform. If the platform does not provide index2id mapping then
> > > identity mapping is assumed.
> > >
> > > Signed-off-by: Anup Patel <apatel@ventanamicro.com>
> > > ---
> > > ?include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
> > > ?lib/sbi/sbi_scratch.c???? | 38 ++++++++++++++++++++++-----------
> > > ?2 files changed, 68 insertions(+), 15 deletions(-)
> > >
> > > diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
> > > index 4801492..9a4dce1 100644
> > > --- a/include/sbi/sbi_scratch.h
> > > +++ b/include/sbi/sbi_scratch.h
> > > @@ -202,12 +202,47 @@ do {??????????????????????????????????????????????????????????????????? \
> > > ????????????????????????????????????? = (__type)(__ptr);????????????? \
> > > ?} while (0)
> > >
> > > -/** HART id to scratch table */
> > > -extern struct sbi_scratch *hartid_to_scratch_table[];
> > > +/** Last HART index having a sbi_scratch pointer */
> > > +extern u32 last_hartindex_having_scratch;
> > > +
> > > +/** Get last HART index having a sbi_scratch pointer */
> > > +#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
> > > +
> > > +/** Check whether a particular HART index is valid or not */
> > > +#define sbi_hartindex_valid(__hartindex) \
> > > +(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
> > The implementation here is strange. If the last index does not have scratch,
> > it will be considered an invalid index. But other index do not be considered
> > invalid without scratch.
>
> I don't understand. The "last_hartindex" is literally the index of the last
> HART having a scratch.
The following case:
hart_count==4
hart_index2id==NULL
hartindex_to_scratch_table[1]==NULL
hartindex_to_scratch_table[3]==NULL
sbi_hartindex_valid(1) return true
sbi_hartindex_valid(3) return false
The last hart will be considered invalid because there is no scratch.
Regards,
Xiang W
>
> Regards,
> Anup
>
> >
> > Regards,
> > Xiang W
> > > +
> > > +/** HART index to HART id table */
> > > +extern u32 hartindex_to_hartid_table[];
> > > +
> > > +/** Get sbi_scratch from HART index */
> > > +#define sbi_hartindex_to_hartid(__hartindex)???????? \
> > > +({?????????????????????????????????????????????????? \
> > > +???? ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > +???? hartindex_to_hartid_table[__hartindex] : -1U;?? \
> > > +})
> > > +
> > > +/** HART index to scratch table */
> > > +extern struct sbi_scratch *hartindex_to_scratch_table[];
> > > +
> > > +/** Get sbi_scratch from HART index */
> > > +#define sbi_hartindex_to_scratch(__hartindex)??????????????? \
> > > +({?????????????????????????????????????????????????? \
> > > +???? ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > +???? hartindex_to_scratch_table[__hartindex] : NULL;\
> > > +})
> > > +
> > > +/**
> > > + * Get logical index for given HART id
> > > + * @param hartid physical HART id
> > > + * @returns value between 0 to SBI_HARTMASK_MAX_BITS upon success and
> > > + *?????? SBI_HARTMASK_MAX_BITS upon failure.
> > > + */
> > > +u32 sbi_hartid_to_hartindex(u32 hartid);
> > >
> > > ?/** Get sbi_scratch from HART id */
> > > ?#define sbi_hartid_to_scratch(__hartid) \
> > > -???? hartid_to_scratch_table[__hartid]
> > > +???? sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
> > >
> > > ?/** Last HART id having a sbi_scratch pointer */
> > > ?extern u32 last_hartid_having_scratch;
> > > @@ -215,6 +250,10 @@ extern u32 last_hartid_having_scratch;
> > > ?/** Get last HART id having a sbi_scratch pointer */
> > > ?#define sbi_scratch_last_hartid()??? last_hartid_having_scratch
> > >
> > > +/** Check whether particular HART id is valid or not */
> > > +#define sbi_hartid_valid(__hartid)?? \
> > > +???? sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
> > > +
> > > ?#endif
> > >
> > > ?#endif
> > > diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
> > > index 87ef84c..d2abc89 100644
> > > --- a/lib/sbi/sbi_scratch.c
> > > +++ b/lib/sbi/sbi_scratch.c
> > > @@ -15,26 +15,40 @@
> > > ?#include <sbi/sbi_string.h>
> > >
> > > ?u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
> > > -struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
> > > +u32 last_hartindex_having_scratch = 0;
> > > +u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
> > > +struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
> > >
> > > ?static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
> > > ?static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
> > >
> > > +u32 sbi_hartid_to_hartindex(u32 hartid)
> > > +{
> > > +???? u32 i;
> > > +
> > > +???? for (i = 0; i <= last_hartindex_having_scratch; i++)
> > > +???????????? if (hartindex_to_hartid_table[i] == hartid)
> > > +???????????????????? return i;
> > > +
> > > +???? return -1U;
> > > +}
> > > +
> > > ?typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
> > >
> > > ?int sbi_scratch_init(struct sbi_scratch *scratch)
> > > ?{
> > > -???? u32 i;
> > > +???? u32 i, h;
> > > ????? const struct sbi_platform *plat = sbi_platform_ptr(scratch);
> > >
> > > -???? for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
> > > -???????????? if (sbi_platform_hart_invalid(plat, i))
> > > -???????????????????? continue;
> > > -???????????? hartid_to_scratch_table[i] =
> > > -???????????????????? ((hartid2scratch)scratch->hartid_to_scratch)(i,
> > > -???????????????????????????????????? sbi_platform_hart_index(plat, i));
> > > -???????????? if (hartid_to_scratch_table[i])
> > > -???????????????????? last_hartid_having_scratch = i;
> > > +???? for (i = 0; i < plat->hart_count; i++) {
> > > +???????????? h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
> > > +???????????? hartindex_to_hartid_table[i] = h;
> > > +???????????? hartindex_to_scratch_table[i] =
> > > +???????????????????? ((hartid2scratch)scratch->hartid_to_scratch)(h, i);
> > > +???????????? if (hartindex_to_scratch_table[i]) {
> > > +???????????????????? last_hartid_having_scratch = h;
> > > +???????????????????? last_hartindex_having_scratch = i;
> > > +???????????? }
> > > ????? }
> > >
> > > ????? return 0;
> > > @@ -74,8 +88,8 @@ done:
> > > ????? spin_unlock(&extra_lock);
> > >
> > > ????? if (ret) {
> > > -???????????? for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
> > > -???????????????????? rscratch = sbi_hartid_to_scratch(i);
> > > +???????????? for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
> > > +???????????????????? rscratch = sbi_hartindex_to_scratch(i);
> > > ????????????????????? if (!rscratch)
> > > ????????????????????????????? continue;
> > > ????????????????????? ptr = sbi_scratch_offset_ptr(rscratch, ret);
> > > --
> > > 2.34.1
> > >
> > >
> >
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch
2023-09-22 7:56 ` Xiang W
@ 2023-09-22 8:23 ` Anup Patel
2023-09-22 8:38 ` Anup Patel
1 sibling, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-22 8:23 UTC (permalink / raw)
To: opensbi
On Fri, Sep 22, 2023 at 1:28?PM Xiang W <wxjstz@126.com> wrote:
>
> ? 2023-09-22???? 11:14 +0530?Anup Patel???
> > On Wed, Sep 20, 2023 at 11:24?PM Xiang W <wxjstz@126.com> wrote:
> > >
> > > ? 2023-09-04???? 09:33 +0530?Anup Patel???
> > > > We introduce HART index and related helper functions in sbi_scratch
> > > > where HART index is contiguous and each HART index maps to a physical
> > > > HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.
> > > >
> > > > The HART index to HART id mapping follows the index2id mapping provided
> > > > by the platform. If the platform does not provide index2id mapping then
> > > > identity mapping is assumed.
> > > >
> > > > Signed-off-by: Anup Patel <apatel@ventanamicro.com>
> > > > ---
> > > > include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
> > > > lib/sbi/sbi_scratch.c | 38 ++++++++++++++++++++++-----------
> > > > 2 files changed, 68 insertions(+), 15 deletions(-)
> > > >
> > > > diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
> > > > index 4801492..9a4dce1 100644
> > > > --- a/include/sbi/sbi_scratch.h
> > > > +++ b/include/sbi/sbi_scratch.h
> > > > @@ -202,12 +202,47 @@ do { \
> > > > = (__type)(__ptr); \
> > > > } while (0)
> > > >
> > > > -/** HART id to scratch table */
> > > > -extern struct sbi_scratch *hartid_to_scratch_table[];
> > > > +/** Last HART index having a sbi_scratch pointer */
> > > > +extern u32 last_hartindex_having_scratch;
> > > > +
> > > > +/** Get last HART index having a sbi_scratch pointer */
> > > > +#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
> > > > +
> > > > +/** Check whether a particular HART index is valid or not */
> > > > +#define sbi_hartindex_valid(__hartindex) \
> > > > +(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
> > > The implementation here is strange. If the last index does not have scratch,
> > > it will be considered an invalid index. But other index do not be considered
> > > invalid without scratch.
> >
> > I don't understand. The "last_hartindex" is literally the index of the last
> > HART having a scratch.
>
> The following case:
> hart_count==4
> hart_index2id==NULL
> hartindex_to_scratch_table[1]==NULL
> hartindex_to_scratch_table[3]==NULL
There are no holes expected in the hart_index2id[] table
provided by platform support code. The above example
shows that platform code is buggy.
Regards,
Anup
>
> sbi_hartindex_valid(1) return true
> sbi_hartindex_valid(3) return false
>
> The last hart will be considered invalid because there is no scratch.
>
> Regards,
> Xiang W
>
> >
> > Regards,
> > Anup
> >
> > >
> > > Regards,
> > > Xiang W
> > > > +
> > > > +/** HART index to HART id table */
> > > > +extern u32 hartindex_to_hartid_table[];
> > > > +
> > > > +/** Get sbi_scratch from HART index */
> > > > +#define sbi_hartindex_to_hartid(__hartindex) \
> > > > +({ \
> > > > + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > > + hartindex_to_hartid_table[__hartindex] : -1U; \
> > > > +})
> > > > +
> > > > +/** HART index to scratch table */
> > > > +extern struct sbi_scratch *hartindex_to_scratch_table[];
> > > > +
> > > > +/** Get sbi_scratch from HART index */
> > > > +#define sbi_hartindex_to_scratch(__hartindex) \
> > > > +({ \
> > > > + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > > + hartindex_to_scratch_table[__hartindex] : NULL;\
> > > > +})
> > > > +
> > > > +/**
> > > > + * Get logical index for given HART id
> > > > + * @param hartid physical HART id
> > > > + * @returns value between 0 to SBI_HARTMASK_MAX_BITS upon success and
> > > > + * SBI_HARTMASK_MAX_BITS upon failure.
> > > > + */
> > > > +u32 sbi_hartid_to_hartindex(u32 hartid);
> > > >
> > > > /** Get sbi_scratch from HART id */
> > > > #define sbi_hartid_to_scratch(__hartid) \
> > > > - hartid_to_scratch_table[__hartid]
> > > > + sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
> > > >
> > > > /** Last HART id having a sbi_scratch pointer */
> > > > extern u32 last_hartid_having_scratch;
> > > > @@ -215,6 +250,10 @@ extern u32 last_hartid_having_scratch;
> > > > /** Get last HART id having a sbi_scratch pointer */
> > > > #define sbi_scratch_last_hartid() last_hartid_having_scratch
> > > >
> > > > +/** Check whether particular HART id is valid or not */
> > > > +#define sbi_hartid_valid(__hartid) \
> > > > + sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
> > > > +
> > > > #endif
> > > >
> > > > #endif
> > > > diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
> > > > index 87ef84c..d2abc89 100644
> > > > --- a/lib/sbi/sbi_scratch.c
> > > > +++ b/lib/sbi/sbi_scratch.c
> > > > @@ -15,26 +15,40 @@
> > > > #include <sbi/sbi_string.h>
> > > >
> > > > u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
> > > > -struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
> > > > +u32 last_hartindex_having_scratch = 0;
> > > > +u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
> > > > +struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
> > > >
> > > > static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
> > > > static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
> > > >
> > > > +u32 sbi_hartid_to_hartindex(u32 hartid)
> > > > +{
> > > > + u32 i;
> > > > +
> > > > + for (i = 0; i <= last_hartindex_having_scratch; i++)
> > > > + if (hartindex_to_hartid_table[i] == hartid)
> > > > + return i;
> > > > +
> > > > + return -1U;
> > > > +}
> > > > +
> > > > typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
> > > >
> > > > int sbi_scratch_init(struct sbi_scratch *scratch)
> > > > {
> > > > - u32 i;
> > > > + u32 i, h;
> > > > const struct sbi_platform *plat = sbi_platform_ptr(scratch);
> > > >
> > > > - for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
> > > > - if (sbi_platform_hart_invalid(plat, i))
> > > > - continue;
> > > > - hartid_to_scratch_table[i] =
> > > > - ((hartid2scratch)scratch->hartid_to_scratch)(i,
> > > > - sbi_platform_hart_index(plat, i));
> > > > - if (hartid_to_scratch_table[i])
> > > > - last_hartid_having_scratch = i;
> > > > + for (i = 0; i < plat->hart_count; i++) {
> > > > + h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
> > > > + hartindex_to_hartid_table[i] = h;
> > > > + hartindex_to_scratch_table[i] =
> > > > + ((hartid2scratch)scratch->hartid_to_scratch)(h, i);
> > > > + if (hartindex_to_scratch_table[i]) {
> > > > + last_hartid_having_scratch = h;
> > > > + last_hartindex_having_scratch = i;
> > > > + }
> > > > }
> > > >
> > > > return 0;
> > > > @@ -74,8 +88,8 @@ done:
> > > > spin_unlock(&extra_lock);
> > > >
> > > > if (ret) {
> > > > - for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
> > > > - rscratch = sbi_hartid_to_scratch(i);
> > > > + for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
> > > > + rscratch = sbi_hartindex_to_scratch(i);
> > > > if (!rscratch)
> > > > continue;
> > > > ptr = sbi_scratch_offset_ptr(rscratch, ret);
> > > > --
> > > > 2.34.1
> > > >
> > > >
> > >
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch
2023-09-22 7:56 ` Xiang W
2023-09-22 8:23 ` Anup Patel
@ 2023-09-22 8:38 ` Anup Patel
2023-09-22 12:58 ` Xiang W
1 sibling, 1 reply; 17+ messages in thread
From: Anup Patel @ 2023-09-22 8:38 UTC (permalink / raw)
To: opensbi
On Fri, Sep 22, 2023 at 1:29?PM Xiang W <wxjstz@126.com> wrote:
>
> ? 2023-09-22???? 11:14 +0530?Anup Patel???
> > On Wed, Sep 20, 2023 at 11:24?PM Xiang W <wxjstz@126.com> wrote:
> > >
> > > ? 2023-09-04???? 09:33 +0530?Anup Patel???
> > > > We introduce HART index and related helper functions in sbi_scratch
> > > > where HART index is contiguous and each HART index maps to a physical
> > > > HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.
> > > >
> > > > The HART index to HART id mapping follows the index2id mapping provided
> > > > by the platform. If the platform does not provide index2id mapping then
> > > > identity mapping is assumed.
> > > >
> > > > Signed-off-by: Anup Patel <apatel@ventanamicro.com>
> > > > ---
> > > > include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
> > > > lib/sbi/sbi_scratch.c | 38 ++++++++++++++++++++++-----------
> > > > 2 files changed, 68 insertions(+), 15 deletions(-)
> > > >
> > > > diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
> > > > index 4801492..9a4dce1 100644
> > > > --- a/include/sbi/sbi_scratch.h
> > > > +++ b/include/sbi/sbi_scratch.h
> > > > @@ -202,12 +202,47 @@ do { \
> > > > = (__type)(__ptr); \
> > > > } while (0)
> > > >
> > > > -/** HART id to scratch table */
> > > > -extern struct sbi_scratch *hartid_to_scratch_table[];
> > > > +/** Last HART index having a sbi_scratch pointer */
> > > > +extern u32 last_hartindex_having_scratch;
> > > > +
> > > > +/** Get last HART index having a sbi_scratch pointer */
> > > > +#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
> > > > +
> > > > +/** Check whether a particular HART index is valid or not */
> > > > +#define sbi_hartindex_valid(__hartindex) \
> > > > +(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
> > > The implementation here is strange. If the last index does not have scratch,
> > > it will be considered an invalid index. But other index do not be considered
> > > invalid without scratch.
> >
> > I don't understand. The "last_hartindex" is literally the index of the last
> > HART having a scratch.
>
> The following case:
> hart_count==4
> hart_index2id==NULL
If hart_index2id==NULL then we are assuming hartindex == hartid
so there are no holes when hart_index2id==NULL.
Regards,
Anup
> hartindex_to_scratch_table[1]==NULL
> hartindex_to_scratch_table[3]==NULL
>
> sbi_hartindex_valid(1) return true
> sbi_hartindex_valid(3) return false
>
> The last hart will be considered invalid because there is no scratch.
>
> Regards,
> Xiang W
>
> >
> > Regards,
> > Anup
> >
> > >
> > > Regards,
> > > Xiang W
> > > > +
> > > > +/** HART index to HART id table */
> > > > +extern u32 hartindex_to_hartid_table[];
> > > > +
> > > > +/** Get sbi_scratch from HART index */
> > > > +#define sbi_hartindex_to_hartid(__hartindex) \
> > > > +({ \
> > > > + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > > + hartindex_to_hartid_table[__hartindex] : -1U; \
> > > > +})
> > > > +
> > > > +/** HART index to scratch table */
> > > > +extern struct sbi_scratch *hartindex_to_scratch_table[];
> > > > +
> > > > +/** Get sbi_scratch from HART index */
> > > > +#define sbi_hartindex_to_scratch(__hartindex) \
> > > > +({ \
> > > > + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > > + hartindex_to_scratch_table[__hartindex] : NULL;\
> > > > +})
> > > > +
> > > > +/**
> > > > + * Get logical index for given HART id
> > > > + * @param hartid physical HART id
> > > > + * @returns value between 0 to SBI_HARTMASK_MAX_BITS upon success and
> > > > + * SBI_HARTMASK_MAX_BITS upon failure.
> > > > + */
> > > > +u32 sbi_hartid_to_hartindex(u32 hartid);
> > > >
> > > > /** Get sbi_scratch from HART id */
> > > > #define sbi_hartid_to_scratch(__hartid) \
> > > > - hartid_to_scratch_table[__hartid]
> > > > + sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
> > > >
> > > > /** Last HART id having a sbi_scratch pointer */
> > > > extern u32 last_hartid_having_scratch;
> > > > @@ -215,6 +250,10 @@ extern u32 last_hartid_having_scratch;
> > > > /** Get last HART id having a sbi_scratch pointer */
> > > > #define sbi_scratch_last_hartid() last_hartid_having_scratch
> > > >
> > > > +/** Check whether particular HART id is valid or not */
> > > > +#define sbi_hartid_valid(__hartid) \
> > > > + sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
> > > > +
> > > > #endif
> > > >
> > > > #endif
> > > > diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
> > > > index 87ef84c..d2abc89 100644
> > > > --- a/lib/sbi/sbi_scratch.c
> > > > +++ b/lib/sbi/sbi_scratch.c
> > > > @@ -15,26 +15,40 @@
> > > > #include <sbi/sbi_string.h>
> > > >
> > > > u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
> > > > -struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
> > > > +u32 last_hartindex_having_scratch = 0;
> > > > +u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
> > > > +struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
> > > >
> > > > static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
> > > > static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
> > > >
> > > > +u32 sbi_hartid_to_hartindex(u32 hartid)
> > > > +{
> > > > + u32 i;
> > > > +
> > > > + for (i = 0; i <= last_hartindex_having_scratch; i++)
> > > > + if (hartindex_to_hartid_table[i] == hartid)
> > > > + return i;
> > > > +
> > > > + return -1U;
> > > > +}
> > > > +
> > > > typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
> > > >
> > > > int sbi_scratch_init(struct sbi_scratch *scratch)
> > > > {
> > > > - u32 i;
> > > > + u32 i, h;
> > > > const struct sbi_platform *plat = sbi_platform_ptr(scratch);
> > > >
> > > > - for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
> > > > - if (sbi_platform_hart_invalid(plat, i))
> > > > - continue;
> > > > - hartid_to_scratch_table[i] =
> > > > - ((hartid2scratch)scratch->hartid_to_scratch)(i,
> > > > - sbi_platform_hart_index(plat, i));
> > > > - if (hartid_to_scratch_table[i])
> > > > - last_hartid_having_scratch = i;
> > > > + for (i = 0; i < plat->hart_count; i++) {
> > > > + h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
> > > > + hartindex_to_hartid_table[i] = h;
> > > > + hartindex_to_scratch_table[i] =
> > > > + ((hartid2scratch)scratch->hartid_to_scratch)(h, i);
> > > > + if (hartindex_to_scratch_table[i]) {
> > > > + last_hartid_having_scratch = h;
> > > > + last_hartindex_having_scratch = i;
> > > > + }
> > > > }
> > > >
> > > > return 0;
> > > > @@ -74,8 +88,8 @@ done:
> > > > spin_unlock(&extra_lock);
> > > >
> > > > if (ret) {
> > > > - for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
> > > > - rscratch = sbi_hartid_to_scratch(i);
> > > > + for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
> > > > + rscratch = sbi_hartindex_to_scratch(i);
> > > > if (!rscratch)
> > > > continue;
> > > > ptr = sbi_scratch_offset_ptr(rscratch, ret);
> > > > --
> > > > 2.34.1
> > > >
> > > >
> > >
>
> --
> opensbi mailing list
> opensbi at lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/opensbi
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch
2023-09-22 8:38 ` Anup Patel
@ 2023-09-22 12:58 ` Xiang W
2023-09-24 6:06 ` Anup Patel
0 siblings, 1 reply; 17+ messages in thread
From: Xiang W @ 2023-09-22 12:58 UTC (permalink / raw)
To: opensbi
? 2023-09-22???? 14:08 +0530?Anup Patel???
> On Fri, Sep 22, 2023 at 1:29?PM Xiang W <wxjstz@126.com> wrote:
> >
> > ? 2023-09-22???? 11:14 +0530?Anup Patel???
> > > On Wed, Sep 20, 2023 at 11:24?PM Xiang W <wxjstz@126.com> wrote:
> > > >
> > > > ? 2023-09-04???? 09:33 +0530?Anup Patel???
> > > > > We introduce HART index and related helper functions in sbi_scratch
> > > > > where HART index is contiguous and each HART index maps to a physical
> > > > > HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.
> > > > >
> > > > > The HART index to HART id mapping follows the index2id mapping provided
> > > > > by the platform. If the platform does not provide index2id mapping then
> > > > > identity mapping is assumed.
> > > > >
> > > > > Signed-off-by: Anup Patel <apatel@ventanamicro.com>
> > > > > ---
> > > > > ?include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
> > > > > ?lib/sbi/sbi_scratch.c???? | 38 ++++++++++++++++++++++-----------
> > > > > ?2 files changed, 68 insertions(+), 15 deletions(-)
> > > > >
> > > > > diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
> > > > > index 4801492..9a4dce1 100644
> > > > > --- a/include/sbi/sbi_scratch.h
> > > > > +++ b/include/sbi/sbi_scratch.h
> > > > > @@ -202,12 +202,47 @@ do {??????????????????????????????????????????????????????????????????? \
> > > > > ????????????????????????????????????? = (__type)(__ptr);????????????? \
> > > > > ?} while (0)
> > > > >
> > > > > -/** HART id to scratch table */
> > > > > -extern struct sbi_scratch *hartid_to_scratch_table[];
> > > > > +/** Last HART index having a sbi_scratch pointer */
> > > > > +extern u32 last_hartindex_having_scratch;
> > > > > +
> > > > > +/** Get last HART index having a sbi_scratch pointer */
> > > > > +#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
> > > > > +
> > > > > +/** Check whether a particular HART index is valid or not */
> > > > > +#define sbi_hartindex_valid(__hartindex) \
> > > > > +(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
> > > > The implementation here is strange. If the last index does not have scratch,
> > > > it will be considered an invalid index. But other index do not be considered
> > > > invalid without scratch.
> > >
> > > I don't understand. The "last_hartindex" is literally the index of the last
> > > HART having a scratch.
> >
> > The following case:
> > hart_count==4
> > hart_index2id==NULL
>
> If hart_index2id==NULL then we are assuming hartindex == hartid
> so there are no holes when hart_index2id==NULL.
In what case last_hartindex_having_scratch is not equal to hart_count - 1?
Why not just assign the value directly?
Regards,
Xiang W
>
> Regards,
> Anup
>
> > hartindex_to_scratch_table[1]==NULL
> > hartindex_to_scratch_table[3]==NULL
> >
> > sbi_hartindex_valid(1) return true
> > sbi_hartindex_valid(3) return false
> >
> > The last hart will be considered invalid because there is no scratch.
> >
> > Regards,
> > Xiang W
> >
> > >
> > > Regards,
> > > Anup
> > >
> > > >
> > > > Regards,
> > > > Xiang W
> > > > > +
> > > > > +/** HART index to HART id table */
> > > > > +extern u32 hartindex_to_hartid_table[];
> > > > > +
> > > > > +/** Get sbi_scratch from HART index */
> > > > > +#define sbi_hartindex_to_hartid(__hartindex)???????? \
> > > > > +({?????????????????????????????????????????????????? \
> > > > > +???? ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > > > +???? hartindex_to_hartid_table[__hartindex] : -1U;?? \
> > > > > +})
> > > > > +
> > > > > +/** HART index to scratch table */
> > > > > +extern struct sbi_scratch *hartindex_to_scratch_table[];
> > > > > +
> > > > > +/** Get sbi_scratch from HART index */
> > > > > +#define sbi_hartindex_to_scratch(__hartindex)??????????????? \
> > > > > +({?????????????????????????????????????????????????? \
> > > > > +???? ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > > > +???? hartindex_to_scratch_table[__hartindex] : NULL;\
> > > > > +})
> > > > > +
> > > > > +/**
> > > > > + * Get logical index for given HART id
> > > > > + * @param hartid physical HART id
> > > > > + * @returns value between 0 to SBI_HARTMASK_MAX_BITS upon success and
> > > > > + *?????? SBI_HARTMASK_MAX_BITS upon failure.
> > > > > + */
> > > > > +u32 sbi_hartid_to_hartindex(u32 hartid);
> > > > >
> > > > > ?/** Get sbi_scratch from HART id */
> > > > > ?#define sbi_hartid_to_scratch(__hartid) \
> > > > > -???? hartid_to_scratch_table[__hartid]
> > > > > +???? sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
> > > > >
> > > > > ?/** Last HART id having a sbi_scratch pointer */
> > > > > ?extern u32 last_hartid_having_scratch;
> > > > > @@ -215,6 +250,10 @@ extern u32 last_hartid_having_scratch;
> > > > > ?/** Get last HART id having a sbi_scratch pointer */
> > > > > ?#define sbi_scratch_last_hartid()??? last_hartid_having_scratch
> > > > >
> > > > > +/** Check whether particular HART id is valid or not */
> > > > > +#define sbi_hartid_valid(__hartid)?? \
> > > > > +???? sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
> > > > > +
> > > > > ?#endif
> > > > >
> > > > > ?#endif
> > > > > diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
> > > > > index 87ef84c..d2abc89 100644
> > > > > --- a/lib/sbi/sbi_scratch.c
> > > > > +++ b/lib/sbi/sbi_scratch.c
> > > > > @@ -15,26 +15,40 @@
> > > > > ?#include <sbi/sbi_string.h>
> > > > >
> > > > > ?u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
> > > > > -struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
> > > > > +u32 last_hartindex_having_scratch = 0;
> > > > > +u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
> > > > > +struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
> > > > >
> > > > > ?static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
> > > > > ?static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
> > > > >
> > > > > +u32 sbi_hartid_to_hartindex(u32 hartid)
> > > > > +{
> > > > > +???? u32 i;
> > > > > +
> > > > > +???? for (i = 0; i <= last_hartindex_having_scratch; i++)
> > > > > +???????????? if (hartindex_to_hartid_table[i] == hartid)
> > > > > +???????????????????? return i;
> > > > > +
> > > > > +???? return -1U;
> > > > > +}
> > > > > +
> > > > > ?typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
> > > > >
> > > > > ?int sbi_scratch_init(struct sbi_scratch *scratch)
> > > > > ?{
> > > > > -???? u32 i;
> > > > > +???? u32 i, h;
> > > > > ????? const struct sbi_platform *plat = sbi_platform_ptr(scratch);
> > > > >
> > > > > -???? for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
> > > > > -???????????? if (sbi_platform_hart_invalid(plat, i))
> > > > > -???????????????????? continue;
> > > > > -???????????? hartid_to_scratch_table[i] =
> > > > > -???????????????????? ((hartid2scratch)scratch->hartid_to_scratch)(i,
> > > > > -???????????????????????????????????? sbi_platform_hart_index(plat, i));
> > > > > -???????????? if (hartid_to_scratch_table[i])
> > > > > -???????????????????? last_hartid_having_scratch = i;
> > > > > +???? for (i = 0; i < plat->hart_count; i++) {
> > > > > +???????????? h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
> > > > > +???????????? hartindex_to_hartid_table[i] = h;
> > > > > +???????????? hartindex_to_scratch_table[i] =
> > > > > +???????????????????? ((hartid2scratch)scratch->hartid_to_scratch)(h, i);
> > > > > +???????????? if (hartindex_to_scratch_table[i]) {
> > > > > +???????????????????? last_hartid_having_scratch = h;
> > > > > +???????????????????? last_hartindex_having_scratch = i;
> > > > > +???????????? }
> > > > > ????? }
> > > > >
> > > > > ????? return 0;
> > > > > @@ -74,8 +88,8 @@ done:
> > > > > ????? spin_unlock(&extra_lock);
> > > > >
> > > > > ????? if (ret) {
> > > > > -???????????? for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
> > > > > -???????????????????? rscratch = sbi_hartid_to_scratch(i);
> > > > > +???????????? for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
> > > > > +???????????????????? rscratch = sbi_hartindex_to_scratch(i);
> > > > > ????????????????????? if (!rscratch)
> > > > > ????????????????????????????? continue;
> > > > > ????????????????????? ptr = sbi_scratch_offset_ptr(rscratch, ret);
> > > > > --
> > > > > 2.34.1
> > > > >
> > > > >
> > > >
> >
> > --
> > opensbi mailing list
> > opensbi at lists.infradead.org
> > http://lists.infradead.org/mailman/listinfo/opensbi
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch
2023-09-22 12:58 ` Xiang W
@ 2023-09-24 6:06 ` Anup Patel
0 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-24 6:06 UTC (permalink / raw)
To: opensbi
On Fri, Sep 22, 2023 at 6:28?PM Xiang W <wxjstz@126.com> wrote:
>
> ? 2023-09-22???? 14:08 +0530?Anup Patel???
> > On Fri, Sep 22, 2023 at 1:29?PM Xiang W <wxjstz@126.com> wrote:
> > >
> > > ? 2023-09-22???? 11:14 +0530?Anup Patel???
> > > > On Wed, Sep 20, 2023 at 11:24?PM Xiang W <wxjstz@126.com> wrote:
> > > > >
> > > > > ? 2023-09-04???? 09:33 +0530?Anup Patel???
> > > > > > We introduce HART index and related helper functions in sbi_scratch
> > > > > > where HART index is contiguous and each HART index maps to a physical
> > > > > > HART id such that 0 <= HART index and HART index < SBI_HARTMASK_MAX_BITS.
> > > > > >
> > > > > > The HART index to HART id mapping follows the index2id mapping provided
> > > > > > by the platform. If the platform does not provide index2id mapping then
> > > > > > identity mapping is assumed.
> > > > > >
> > > > > > Signed-off-by: Anup Patel <apatel@ventanamicro.com>
> > > > > > ---
> > > > > > include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
> > > > > > lib/sbi/sbi_scratch.c | 38 ++++++++++++++++++++++-----------
> > > > > > 2 files changed, 68 insertions(+), 15 deletions(-)
> > > > > >
> > > > > > diff --git a/include/sbi/sbi_scratch.h b/include/sbi/sbi_scratch.h
> > > > > > index 4801492..9a4dce1 100644
> > > > > > --- a/include/sbi/sbi_scratch.h
> > > > > > +++ b/include/sbi/sbi_scratch.h
> > > > > > @@ -202,12 +202,47 @@ do { \
> > > > > > = (__type)(__ptr); \
> > > > > > } while (0)
> > > > > >
> > > > > > -/** HART id to scratch table */
> > > > > > -extern struct sbi_scratch *hartid_to_scratch_table[];
> > > > > > +/** Last HART index having a sbi_scratch pointer */
> > > > > > +extern u32 last_hartindex_having_scratch;
> > > > > > +
> > > > > > +/** Get last HART index having a sbi_scratch pointer */
> > > > > > +#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
> > > > > > +
> > > > > > +/** Check whether a particular HART index is valid or not */
> > > > > > +#define sbi_hartindex_valid(__hartindex) \
> > > > > > +(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
> > > > > The implementation here is strange. If the last index does not have scratch,
> > > > > it will be considered an invalid index. But other index do not be considered
> > > > > invalid without scratch.
> > > >
> > > > I don't understand. The "last_hartindex" is literally the index of the last
> > > > HART having a scratch.
> > >
> > > The following case:
> > > hart_count==4
> > > hart_index2id==NULL
> >
> > If hart_index2id==NULL then we are assuming hartindex == hartid
> > so there are no holes when hart_index2id==NULL.
> In what case last_hartindex_having_scratch is not equal to hart_count - 1?
> Why not just assign the value directly?
I agree. The last_hartindex_having_scratch should be directly assigned
plat->hart_count - 1
Regards,
Anup
>
> Regards,
> Xiang W
> >
> > Regards,
> > Anup
> >
> > > hartindex_to_scratch_table[1]==NULL
> > > hartindex_to_scratch_table[3]==NULL
> > >
> > > sbi_hartindex_valid(1) return true
> > > sbi_hartindex_valid(3) return false
> > >
> > > The last hart will be considered invalid because there is no scratch.
> > >
> > > Regards,
> > > Xiang W
> > >
> > > >
> > > > Regards,
> > > > Anup
> > > >
> > > > >
> > > > > Regards,
> > > > > Xiang W
> > > > > > +
> > > > > > +/** HART index to HART id table */
> > > > > > +extern u32 hartindex_to_hartid_table[];
> > > > > > +
> > > > > > +/** Get sbi_scratch from HART index */
> > > > > > +#define sbi_hartindex_to_hartid(__hartindex) \
> > > > > > +({ \
> > > > > > + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > > > > + hartindex_to_hartid_table[__hartindex] : -1U; \
> > > > > > +})
> > > > > > +
> > > > > > +/** HART index to scratch table */
> > > > > > +extern struct sbi_scratch *hartindex_to_scratch_table[];
> > > > > > +
> > > > > > +/** Get sbi_scratch from HART index */
> > > > > > +#define sbi_hartindex_to_scratch(__hartindex) \
> > > > > > +({ \
> > > > > > + ((__hartindex) <= sbi_scratch_last_hartindex()) ?\
> > > > > > + hartindex_to_scratch_table[__hartindex] : NULL;\
> > > > > > +})
> > > > > > +
> > > > > > +/**
> > > > > > + * Get logical index for given HART id
> > > > > > + * @param hartid physical HART id
> > > > > > + * @returns value between 0 to SBI_HARTMASK_MAX_BITS upon success and
> > > > > > + * SBI_HARTMASK_MAX_BITS upon failure.
> > > > > > + */
> > > > > > +u32 sbi_hartid_to_hartindex(u32 hartid);
> > > > > >
> > > > > > /** Get sbi_scratch from HART id */
> > > > > > #define sbi_hartid_to_scratch(__hartid) \
> > > > > > - hartid_to_scratch_table[__hartid]
> > > > > > + sbi_hartindex_to_scratch(sbi_hartid_to_hartindex(__hartid))
> > > > > >
> > > > > > /** Last HART id having a sbi_scratch pointer */
> > > > > > extern u32 last_hartid_having_scratch;
> > > > > > @@ -215,6 +250,10 @@ extern u32 last_hartid_having_scratch;
> > > > > > /** Get last HART id having a sbi_scratch pointer */
> > > > > > #define sbi_scratch_last_hartid() last_hartid_having_scratch
> > > > > >
> > > > > > +/** Check whether particular HART id is valid or not */
> > > > > > +#define sbi_hartid_valid(__hartid) \
> > > > > > + sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
> > > > > > +
> > > > > > #endif
> > > > > >
> > > > > > #endif
> > > > > > diff --git a/lib/sbi/sbi_scratch.c b/lib/sbi/sbi_scratch.c
> > > > > > index 87ef84c..d2abc89 100644
> > > > > > --- a/lib/sbi/sbi_scratch.c
> > > > > > +++ b/lib/sbi/sbi_scratch.c
> > > > > > @@ -15,26 +15,40 @@
> > > > > > #include <sbi/sbi_string.h>
> > > > > >
> > > > > > u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;
> > > > > > -struct sbi_scratch *hartid_to_scratch_table[SBI_HARTMASK_MAX_BITS] = { 0 };
> > > > > > +u32 last_hartindex_having_scratch = 0;
> > > > > > +u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS + 1] = { -1U };
> > > > > > +struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 };
> > > > > >
> > > > > > static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
> > > > > > static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
> > > > > >
> > > > > > +u32 sbi_hartid_to_hartindex(u32 hartid)
> > > > > > +{
> > > > > > + u32 i;
> > > > > > +
> > > > > > + for (i = 0; i <= last_hartindex_having_scratch; i++)
> > > > > > + if (hartindex_to_hartid_table[i] == hartid)
> > > > > > + return i;
> > > > > > +
> > > > > > + return -1U;
> > > > > > +}
> > > > > > +
> > > > > > typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
> > > > > >
> > > > > > int sbi_scratch_init(struct sbi_scratch *scratch)
> > > > > > {
> > > > > > - u32 i;
> > > > > > + u32 i, h;
> > > > > > const struct sbi_platform *plat = sbi_platform_ptr(scratch);
> > > > > >
> > > > > > - for (i = 0; i < SBI_HARTMASK_MAX_BITS; i++) {
> > > > > > - if (sbi_platform_hart_invalid(plat, i))
> > > > > > - continue;
> > > > > > - hartid_to_scratch_table[i] =
> > > > > > - ((hartid2scratch)scratch->hartid_to_scratch)(i,
> > > > > > - sbi_platform_hart_index(plat, i));
> > > > > > - if (hartid_to_scratch_table[i])
> > > > > > - last_hartid_having_scratch = i;
> > > > > > + for (i = 0; i < plat->hart_count; i++) {
> > > > > > + h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
> > > > > > + hartindex_to_hartid_table[i] = h;
> > > > > > + hartindex_to_scratch_table[i] =
> > > > > > + ((hartid2scratch)scratch->hartid_to_scratch)(h, i);
> > > > > > + if (hartindex_to_scratch_table[i]) {
> > > > > > + last_hartid_having_scratch = h;
> > > > > > + last_hartindex_having_scratch = i;
> > > > > > + }
> > > > > > }
> > > > > >
> > > > > > return 0;
> > > > > > @@ -74,8 +88,8 @@ done:
> > > > > > spin_unlock(&extra_lock);
> > > > > >
> > > > > > if (ret) {
> > > > > > - for (i = 0; i <= sbi_scratch_last_hartid(); i++) {
> > > > > > - rscratch = sbi_hartid_to_scratch(i);
> > > > > > + for (i = 0; i <= sbi_scratch_last_hartindex(); i++) {
> > > > > > + rscratch = sbi_hartindex_to_scratch(i);
> > > > > > if (!rscratch)
> > > > > > continue;
> > > > > > ptr = sbi_scratch_offset_ptr(rscratch, ret);
> > > > > > --
> > > > > > 2.34.1
> > > > > >
> > > > > >
> > > > >
> > >
> > > --
> > > opensbi mailing list
> > > opensbi at lists.infradead.org
> > > http://lists.infradead.org/mailman/listinfo/opensbi
> >
>
^ permalink raw reply [flat|nested] 17+ messages in thread
* [PATCH 0/8] OpenSBI sparse HART id support
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
` (7 preceding siblings ...)
2023-09-04 4:03 ` [PATCH 8/8] include: sbi: Remove sbi_hartmask_for_each_hart() macro Anup Patel
@ 2023-09-24 6:34 ` Anup Patel
8 siblings, 0 replies; 17+ messages in thread
From: Anup Patel @ 2023-09-24 6:34 UTC (permalink / raw)
To: opensbi
On Mon, Sep 4, 2023 at 9:33?AM Anup Patel <apatel@ventanamicro.com> wrote:
>
> Currently, we requires HART id to be less than SBI_HARTMASK_MAX_BITS
> because we use HART id as bit index in the sbi_hartmask. This is a
> very limiting requirement because RISC-V platforms can have sparse
> HART ids (with possibly large values).
>
> This series addresses the above described limitation by adding
> sparse HART id support in OpenSBI.
>
> These patches can also be found the full_sparse_hartid_v1 branch at
> https://github.com/avpatel/opensbi.git
>
> Anup Patel (7):
> lib: sbi: Introduce HART index in sbi_scratch
> lib: sbi: Remove sbi_platform_hart_index/invalid() functions
> lib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment
> lib: sbi: Prefer hartindex over hartid in IPI framework
> lib: sbi: Remove sbi_scratch_last_hartid() macro
> lib: sbi: Maximize the use of HART index in sbi_domain
> include: sbi: Remove sbi_hartmask_for_each_hart() macro
>
> Xiang W (1):
> lib: sbi: Extend sbi_hartmask to support both hartid and hartindex
Addressed comment on PATCH1 and applied this series to the riscv/opensbi repo.
Thanks,
Anup
>
> include/sbi/sbi_domain.h | 6 +--
> include/sbi/sbi_hartmask.h | 75 ++++++++++++++++++++++++-------
> include/sbi/sbi_ipi.h | 14 +++---
> include/sbi/sbi_platform.h | 28 ------------
> include/sbi/sbi_scratch.h | 49 ++++++++++++++++----
> lib/sbi/sbi_domain.c | 82 ++++++++++++++++------------------
> lib/sbi/sbi_hsm.c | 30 ++++++-------
> lib/sbi/sbi_init.c | 28 +++++++-----
> lib/sbi/sbi_ipi.c | 28 ++++++------
> lib/sbi/sbi_platform.c | 17 -------
> lib/sbi/sbi_scratch.c | 37 +++++++++------
> lib/sbi/sbi_system.c | 5 ++-
> lib/sbi/sbi_tlb.c | 14 +++---
> lib/utils/fdt/fdt_domain.c | 4 +-
> lib/utils/ipi/aclint_mswi.c | 14 +++---
> lib/utils/ipi/andes_plicsw.c | 8 +++-
> lib/utils/irqchip/imsic.c | 4 +-
> platform/generic/andes/ae350.c | 2 +-
> 18 files changed, 246 insertions(+), 199 deletions(-)
>
> --
> 2.34.1
>
^ permalink raw reply [flat|nested] 17+ messages in thread
end of thread, other threads:[~2023-09-24 6:34 UTC | newest]
Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-04 4:03 [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
2023-09-04 4:03 ` [PATCH 1/8] lib: sbi: Introduce HART index in sbi_scratch Anup Patel
2023-09-20 17:54 ` Xiang W
2023-09-22 5:44 ` Anup Patel
2023-09-22 7:56 ` Xiang W
2023-09-22 8:23 ` Anup Patel
2023-09-22 8:38 ` Anup Patel
2023-09-22 12:58 ` Xiang W
2023-09-24 6:06 ` Anup Patel
2023-09-04 4:03 ` [PATCH 2/8] lib: sbi: Remove sbi_platform_hart_index/invalid() functions Anup Patel
2023-09-04 4:03 ` [PATCH 3/8] lib: sbi: Extend sbi_hartmask to support both hartid and hartindex Anup Patel
2023-09-04 4:03 ` [PATCH 4/8] lib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment Anup Patel
2023-09-04 4:03 ` [PATCH 5/8] lib: sbi: Prefer hartindex over hartid in IPI framework Anup Patel
2023-09-04 4:03 ` [PATCH 6/8] lib: sbi: Remove sbi_scratch_last_hartid() macro Anup Patel
2023-09-04 4:03 ` [PATCH 7/8] lib: sbi: Maximize the use of HART index in sbi_domain Anup Patel
2023-09-04 4:03 ` [PATCH 8/8] include: sbi: Remove sbi_hartmask_for_each_hart() macro Anup Patel
2023-09-24 6:34 ` [PATCH 0/8] OpenSBI sparse HART id support Anup Patel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox