forked from Mirrors/opensbi
lib: sbi_scratch: Add sbi_hart_count() and for_each_hartindex()
There is currently no helper for iterating through the harts in a system, and code must choose between sbi_scratch_last_hartindex() and sbi_platform_hart_count() for the loop condition. sbi_scratch_last_hartindex() has unusual semantics, leading to the likelihood of off-by-one errors, and sbi_platform_hart_count() is provided by the platform and so may not be properly bounded. Add a new helper which definitively reports the number of harts managed by this OpenSBI instance, i.e. the number of valid hart indexes, and a convenient iterator macro. Signed-off-by: Samuel Holland <samuel.holland@sifive.com> Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:

committed by
Anup Patel

parent
6b97950cf5
commit
757f7acafd
@@ -210,15 +210,21 @@ do { \
|
|||||||
#define current_hartindex() \
|
#define current_hartindex() \
|
||||||
(sbi_scratch_thishart_ptr()->hartindex)
|
(sbi_scratch_thishart_ptr()->hartindex)
|
||||||
|
|
||||||
/** Last HART index having a sbi_scratch pointer */
|
/** Number of harts managed by this OpenSBI instance */
|
||||||
extern u32 last_hartindex_having_scratch;
|
extern u32 sbi_scratch_hart_count;
|
||||||
|
|
||||||
|
/** Get the number of harts managed by this OpenSBI instance */
|
||||||
|
#define sbi_hart_count() sbi_scratch_hart_count
|
||||||
|
|
||||||
|
/** Iterate over the harts managed by this OpenSBI instance */
|
||||||
|
#define sbi_for_each_hartindex(__var) \
|
||||||
|
for (u32 __var = 0; __var < sbi_hart_count(); ++__var)
|
||||||
|
|
||||||
/** Get last HART index having a sbi_scratch pointer */
|
/** Get last HART index having a sbi_scratch pointer */
|
||||||
#define sbi_scratch_last_hartindex() last_hartindex_having_scratch
|
#define sbi_scratch_last_hartindex() (sbi_hart_count() - 1)
|
||||||
|
|
||||||
/** Check whether a particular HART index is valid or not */
|
/** Check whether a particular HART index is valid or not */
|
||||||
#define sbi_hartindex_valid(__hartindex) \
|
#define sbi_hartindex_valid(__hartindex) ((__hartindex) < sbi_hart_count())
|
||||||
(((__hartindex) <= sbi_scratch_last_hartindex()) ? true : false)
|
|
||||||
|
|
||||||
/** HART index to HART id table */
|
/** HART index to HART id table */
|
||||||
extern u32 hartindex_to_hartid_table[];
|
extern u32 hartindex_to_hartid_table[];
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
#include <sbi/sbi_scratch.h>
|
#include <sbi/sbi_scratch.h>
|
||||||
#include <sbi/sbi_string.h>
|
#include <sbi/sbi_string.h>
|
||||||
|
|
||||||
u32 last_hartindex_having_scratch = 0;
|
u32 sbi_scratch_hart_count;
|
||||||
u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS] = { [0 ... SBI_HARTMASK_MAX_BITS-1] = -1U };
|
u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS] = { [0 ... SBI_HARTMASK_MAX_BITS-1] = -1U };
|
||||||
struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
|
struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
|
||||||
|
|
||||||
@@ -23,9 +23,7 @@ static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
|
|||||||
|
|
||||||
u32 sbi_hartid_to_hartindex(u32 hartid)
|
u32 sbi_hartid_to_hartindex(u32 hartid)
|
||||||
{
|
{
|
||||||
u32 i;
|
sbi_for_each_hartindex(i)
|
||||||
|
|
||||||
for (i = 0; i <= last_hartindex_having_scratch; i++)
|
|
||||||
if (hartindex_to_hartid_table[i] == hartid)
|
if (hartindex_to_hartid_table[i] == hartid)
|
||||||
return i;
|
return i;
|
||||||
|
|
||||||
@@ -36,22 +34,21 @@ typedef struct sbi_scratch *(*hartid2scratch)(ulong hartid, ulong hartindex);
|
|||||||
|
|
||||||
int sbi_scratch_init(struct sbi_scratch *scratch)
|
int sbi_scratch_init(struct sbi_scratch *scratch)
|
||||||
{
|
{
|
||||||
u32 i, h, hart_count;
|
u32 h, hart_count;
|
||||||
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
const struct sbi_platform *plat = sbi_platform_ptr(scratch);
|
||||||
|
|
||||||
hart_count = plat->hart_count;
|
hart_count = plat->hart_count;
|
||||||
if (hart_count > SBI_HARTMASK_MAX_BITS)
|
if (hart_count > SBI_HARTMASK_MAX_BITS)
|
||||||
hart_count = SBI_HARTMASK_MAX_BITS;
|
hart_count = SBI_HARTMASK_MAX_BITS;
|
||||||
|
sbi_scratch_hart_count = hart_count;
|
||||||
|
|
||||||
for (i = 0; i < hart_count; i++) {
|
sbi_for_each_hartindex(i) {
|
||||||
h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
|
h = (plat->hart_index2id) ? plat->hart_index2id[i] : i;
|
||||||
hartindex_to_hartid_table[i] = h;
|
hartindex_to_hartid_table[i] = h;
|
||||||
hartindex_to_scratch_table[i] =
|
hartindex_to_scratch_table[i] =
|
||||||
((hartid2scratch)scratch->hartid_to_scratch)(h, i);
|
((hartid2scratch)scratch->hartid_to_scratch)(h, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
last_hartindex_having_scratch = hart_count - 1;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user