Skip to contentSkip to conversation

OpenSBI sparse HART id support

PATCH New Anup Patel 3 participants 17 messages Sep 4, 2023 – Sep 24, 2023

lore
Share & tools
lore

Anup Patel

author
#0 lore raw

cover OpenSBI sparse HART id support

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(-)
Signature · 1 line
2.34.1
author
#1 lore raw

PATCH 1/8 lib: sbi: Introduce HART index in sbi_scratch

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.

---
 include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
 lib/sbi/sbi_scratch.c     | 38 ++++++++++++++++++++++-----------
 2 files changed, 68 insertions(+), 15 deletions(-)
include/sbi/sbi_scratch.h +42 -3
index 4801492..9a4dce1 100644
@@ -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
lib/sbi/sbi_scratch.c +26 -12
index 87ef84c..d2abc89 100644
@@ -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);
Signature · 1 line
2.34.1
#2 lore raw

PATCH 1/8 lib: sbi: Introduce HART index in sbi_scratch

? 2023-09-04???? 09:33 +0530?Anup Patel???

Quoted 23 lines

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.

---
?include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++--- ?lib/sbi/sbi_scratch.c???? | 38 ++++++++++++++++++++++----------- ?2 files changed, 68 insertions(+), 15 deletions(-)

include/sbi/sbi_scratch.h +0 -0
index 4801492..9a4dce1 100644
@@ -202,12 +202,47 @@ do {									\
 ?					= (__type)(__ptr);		\
 ?} while (0)

?

include/sbi/sbi_scratch.h +0 -1
-/** HART id to scratch table */

include/sbi/sbi_scratch.h do { \ · line 213

Quoted 10 lines
include/sbi/sbi_scratch.h +9 -1
-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

Quoted 117 lines
include/sbi/sbi_scratch.h +28 -0
+
+/** 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) \

include/sbi/sbi_scratch.h +1 -1
-	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;

include/sbi/sbi_scratch.h +0 -0
@@ -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
?

include/sbi/sbi_scratch.h +4 -0
+/** Check whether particular HART id is valid or not */
+#define sbi_hartid_valid(__hartid)	\
+	sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
+

?#endif
?
?#endif

lib/sbi/sbi_scratch.c +0 -0
index 87ef84c..d2abc89 100644
@@ -15,26 +15,40 @@

?#include <sbi/sbi_string.h>
?
?u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;

lib/sbi/sbi_scratch.c +3 -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; ?

lib/sbi/sbi_scratch.c +11 -0
+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)
?{

lib/sbi/sbi_scratch.c +1 -1
-	u32 i;
+	u32 i, h;

? const struct sbi_platform *plat = sbi_platform_ptr(scratch);
?

lib/sbi/sbi_scratch.c +9 -8
-	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;

lib/sbi/sbi_scratch.c +0 -0
@@ -74,8 +88,8 @@ done:
 ?	spin_unlock(&extra_lock);

?
? if (ret) {

lib/sbi/sbi_scratch.c +2 -2
-		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);

Signature · 1 line
2.34.1
#3 lore raw

PATCH 1/8 lib: sbi: Introduce HART index in sbi_scratch

Quoted 24 lines · On Wed, Sep 20, 2023 at 11:24?PM Xiang W <[email protected]> wrote:

? 2023-09-04???? 09:33 +0530?Anup Patel???

Quoted 22 lines

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.

---
 include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
 lib/sbi/sbi_scratch.c     | 38 ++++++++++++++++++++++-----------
 2 files changed, 68 insertions(+), 15 deletions(-)
include/sbi/sbi_scratch.h +0 -0
index 4801492..9a4dce1 100644
@@ -202,12 +202,47 @@ do {                                                                    \
                                      = (__type)(__ptr);              \
 } while (0)
 
Quoted 14 lines

include/sbi/sbi_scratch.h do { \ · line 213

Quoted 11 lines
include/sbi/sbi_scratch.h +9 -2
-/** 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

Quoted 121 lines

Regards,
Xiang W

Quoted 117 lines
include/sbi/sbi_scratch.h +33 -1
+
+/** 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
lib/sbi/sbi_scratch.c +26 -13
index 87ef84c..d2abc89 100644
@@ -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

#4 lore raw

PATCH 1/8 lib: sbi: Introduce HART index in sbi_scratch

? 2023-09-22???? 11:14 +0530?Anup Patel???

Quoted 40 lines · On Wed, Sep 20, 2023 at 11:24?PM Xiang W <[email protected]> wrote:
Quoted 24 lines

? 2023-09-04???? 09:33 +0530?Anup Patel???

Quoted 22 lines

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.

---
?include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++--- ?lib/sbi/sbi_scratch.c???? | 38 ++++++++++++++++++++++----------- ?2 files changed, 68 insertions(+), 15 deletions(-)

include/sbi/sbi_scratch.h +0 -0
index 4801492..9a4dce1 100644
@@ -202,12 +202,47 @@ do {??????????????????????????????????????????????????????????????????? \
 ????????????????????????????????????? = (__type)(__ptr);????????????? \
 ?} while (0)
 
Quoted 14 lines

include/sbi/sbi_scratch.h do { \ · line 213

Quoted 11 lines
include/sbi/sbi_scratch.h +9 -2
-/** 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.

Quoted 2 lines

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

Quoted 125 lines

Regards,
Anup

Quoted 121 lines

Regards,
Xiang W

Quoted 117 lines
include/sbi/sbi_scratch.h +28 -0
+
+/** 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;

include/sbi/sbi_scratch.h +0 -0
@@ -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

include/sbi/sbi_scratch.h +4 -0
+/** Check whether particular HART id is valid or not */
+#define sbi_hartid_valid(__hartid)?? \
+???? sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
+

?#endif

?#endif

lib/sbi/sbi_scratch.c +0 -0
index 87ef84c..d2abc89 100644
@@ -15,26 +15,40 @@

?#include <sbi/sbi_string.h>

?u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;

lib/sbi/sbi_scratch.c +3 -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;

lib/sbi/sbi_scratch.c +11 -0
+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;

lib/sbi/sbi_scratch.c +2 -3
@@ -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

#5 lore raw

PATCH 1/8 lib: sbi: Introduce HART index in sbi_scratch

Quoted 42 lines · On Fri, Sep 22, 2023 at 1:28?PM Xiang W <[email protected]> wrote:

? 2023-09-22???? 11:14 +0530?Anup Patel???

Quoted 40 lines · On Wed, Sep 20, 2023 at 11:24?PM Xiang W <[email protected]> wrote:
Quoted 38 lines

? 2023-09-04???? 09:33 +0530?Anup Patel???

Quoted 23 lines

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.

---
 include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
 lib/sbi/sbi_scratch.c     | 38 ++++++++++++++++++++++-----------
 2 files changed, 68 insertions(+), 15 deletions(-)
include/sbi/sbi_scratch.h +0 -1
index 4801492..9a4dce1 100644
@@ -202,12 +202,47 @@ do {                                                                    \
                                      = (__type)(__ptr);              \
 } while (0)
 
-/** HART id to scratch table */

include/sbi/sbi_scratch.h do { \ · line 213

Quoted 10 lines
include/sbi/sbi_scratch.h +9 -1
-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.

Quoted 8 lines
Quoted 2 lines

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

Quoted 135 lines

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

Quoted 125 lines

Regards,
Anup

Quoted 121 lines

Regards,
Xiang W

Quoted 117 lines
include/sbi/sbi_scratch.h +33 -1
+
+/** 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
lib/sbi/sbi_scratch.c +26 -13
index 87ef84c..d2abc89 100644
@@ -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

author
#6 lore raw

PATCH 1/8 lib: sbi: Introduce HART index in sbi_scratch

In reply to Xiang W
Quoted 42 lines · On Fri, Sep 22, 2023 at 1:29?PM Xiang W <[email protected]> wrote:

? 2023-09-22???? 11:14 +0530?Anup Patel???

Quoted 40 lines · On Wed, Sep 20, 2023 at 11:24?PM Xiang W <[email protected]> wrote:
Quoted 38 lines

? 2023-09-04???? 09:33 +0530?Anup Patel???

Quoted 23 lines

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.

---
 include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
 lib/sbi/sbi_scratch.c     | 38 ++++++++++++++++++++++-----------
 2 files changed, 68 insertions(+), 15 deletions(-)
include/sbi/sbi_scratch.h +0 -1
index 4801492..9a4dce1 100644
@@ -202,12 +202,47 @@ do {                                                                    \
                                      = (__type)(__ptr);              \
 } while (0)
 
-/** HART id to scratch table */

include/sbi/sbi_scratch.h do { \ · line 213

Quoted 10 lines
include/sbi/sbi_scratch.h +9 -1
-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.

Quoted 6 lines
Quoted 2 lines

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

Quoted 141 lines

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

Quoted 125 lines

Regards,
Anup

Quoted 121 lines

Regards,
Xiang W

Quoted 117 lines
include/sbi/sbi_scratch.h +33 -1
+
+/** 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
lib/sbi/sbi_scratch.c +26 -13
index 87ef84c..d2abc89 100644
@@ -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

#7 lore raw

PATCH 1/8 lib: sbi: Introduce HART index in sbi_scratch

? 2023-09-22???? 14:08 +0530?Anup Patel???

Quoted 46 lines · On Fri, Sep 22, 2023 at 1:29?PM Xiang W <[email protected]> wrote:
Quoted 45 lines

? 2023-09-22???? 11:14 +0530?Anup Patel???

Quoted 42 lines · On Wed, Sep 20, 2023 at 11:24?PM Xiang W <[email protected]> wrote:
Quoted 24 lines

? 2023-09-04???? 09:33 +0530?Anup Patel???

Quoted 22 lines

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.

---
?include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++--- ?lib/sbi/sbi_scratch.c???? | 38 ++++++++++++++++++++++----------- ?2 files changed, 68 insertions(+), 15 deletions(-)

include/sbi/sbi_scratch.h +0 -0
index 4801492..9a4dce1 100644
@@ -202,12 +202,47 @@ do {??????????????????????????????????????????????????????????????????? \
 ????????????????????????????????????? = (__type)(__ptr);????????????? \
 ?} while (0)
 
Quoted 14 lines

include/sbi/sbi_scratch.h do { \ · line 213

Quoted 11 lines
include/sbi/sbi_scratch.h +9 -2
-/** 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.

Quoted 6 lines
Quoted 3 lines

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

Quoted 146 lines

Regards,
Anup

Quoted 141 lines

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

Quoted 125 lines

Regards,
Anup

Quoted 121 lines

Regards,
Xiang W

Quoted 117 lines
include/sbi/sbi_scratch.h +28 -0
+
+/** 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;

include/sbi/sbi_scratch.h +0 -0
@@ -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

include/sbi/sbi_scratch.h +4 -0
+/** Check whether particular HART id is valid or not */
+#define sbi_hartid_valid(__hartid)?? \
+???? sbi_hartindex_valid(sbi_hartid_to_hartindex(__hartid))
+

?#endif

?#endif

lib/sbi/sbi_scratch.c +0 -0
index 87ef84c..d2abc89 100644
@@ -15,26 +15,40 @@

?#include <sbi/sbi_string.h>

?u32 last_hartid_having_scratch = SBI_HARTMASK_MAX_BITS - 1;

lib/sbi/sbi_scratch.c +3 -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;

lib/sbi/sbi_scratch.c +11 -0
+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;

lib/sbi/sbi_scratch.c +2 -3
@@ -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

#8 lore raw

PATCH 1/8 lib: sbi: Introduce HART index in sbi_scratch

Quoted 48 lines · On Fri, Sep 22, 2023 at 6:28?PM Xiang W <[email protected]> wrote:

? 2023-09-22???? 14:08 +0530?Anup Patel???

Quoted 46 lines · On Fri, Sep 22, 2023 at 1:29?PM Xiang W <[email protected]> wrote:
Quoted 45 lines

? 2023-09-22???? 11:14 +0530?Anup Patel???

Quoted 42 lines · On Wed, Sep 20, 2023 at 11:24?PM Xiang W <[email protected]> wrote:
Quoted 24 lines

? 2023-09-04???? 09:33 +0530?Anup Patel???

Quoted 22 lines

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.

---
 include/sbi/sbi_scratch.h | 45 ++++++++++++++++++++++++++++++++++++---
 lib/sbi/sbi_scratch.c     | 38 ++++++++++++++++++++++-----------
 2 files changed, 68 insertions(+), 15 deletions(-)
include/sbi/sbi_scratch.h +0 -0
index 4801492..9a4dce1 100644
@@ -202,12 +202,47 @@ do {                                                                    \
                                      = (__type)(__ptr);              \
 } while (0)
 
Quoted 14 lines

include/sbi/sbi_scratch.h do { \ · line 213

Quoted 11 lines
include/sbi/sbi_scratch.h +9 -2
-/** 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.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

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

Quoted 150 lines

Regards,
Xiang W

Quoted 146 lines

Regards,
Anup

Quoted 141 lines

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

Quoted 125 lines

Regards,
Anup

Quoted 121 lines

Regards,
Xiang W

Quoted 117 lines
include/sbi/sbi_scratch.h +33 -1
+
+/** 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
lib/sbi/sbi_scratch.c +26 -13
index 87ef84c..d2abc89 100644
@@ -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

author
#9 lore raw

PATCH 2/8 lib: sbi: Remove sbi_platform_hart_index/invalid() functions

In reply to Anup Patel

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.

---
 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(-)
include/sbi/sbi_platform.h +0 -28
index 3e9616f..5f81bb1 100644
@@ -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
  *
lib/sbi/sbi_domain.c +2 -3
index acd0f74..77d6ca4 100644
@@ -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);
 	}
lib/sbi/sbi_init.c +9 -3
index 252b41a..07be3d2 100644
@@ -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);
lib/sbi/sbi_platform.c +0 -17
index 445a8c1..43fc88a 100644
@@ -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;
-}
Signature · 1 line
2.34.1
author
#10 lore raw

PATCH 3/8 lib: sbi: Extend sbi_hartmask to support both hartid and hartindex

In reply to Anup Patel

From: Xiang W <[email protected]>

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.

---
 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(-)
include/sbi/sbi_hartmask.h +71 -17
index f1cef0c..105653e 100644
@@ -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
lib/sbi/sbi_domain.c +19 -28

117 lines not on this page · raw message

lib/sbi/sbi_init.c +4 -4
index 07be3d2..10dbd0a 100644
@@ -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) {
lib/sbi/sbi_ipi.c +5 -5
index ad09154..09c8d10 100644
@@ -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);
 
lib/sbi/sbi_system.c +2 -2
index 2e917c9..bae9730 100644
@@ -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)
lib/sbi/sbi_tlb.c +2 -2
index 26a87f3..f8a1aab 100644
@@ -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;
lib/utils/fdt/fdt_domain.c +2 -2
index 788683d..fa1c357 100644
@@ -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 */
Signature · 1 line
2.34.1
author
#11 lore raw

PATCH 4/8 lib: sbi: Use sbi_scratch_last_hartindex() in remote TLB managment

In reply to Anup Patel

The sbi_hartid_to_scratch() involves translating hartid to hartindex which is expensive so let's use sbi_hartindex_to_scratch() instead.

---
 lib/sbi/sbi_tlb.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/sbi/sbi_tlb.c +3 -3
index f8a1aab..1604669 100644
@@ -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;
 
Signature · 1 line
2.34.1
author
#12 lore raw

PATCH 5/8 lib: sbi: Prefer hartindex over hartid in IPI framework

In reply to Anup Patel

Let us prefer hartindex over hartid in IPI framework which in-turn forces IPI users to also prefer hartindex.

---
 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(-)
include/sbi/sbi_ipi.h +7 -7
index c64f422..cea8dee 100644
@@ -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);
 
lib/sbi/sbi_hsm.c +1 -1
index f870ca7..814130e 100644
@@ -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)
lib/sbi/sbi_init.c +2 -2
index 10dbd0a..a6d96e6 100644
@@ -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);
 	}
 }
lib/sbi/sbi_ipi.c +13 -13
index 09c8d10..42d56c7 100644
@@ -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)
lib/sbi/sbi_tlb.c +4 -4
index 1604669..b1bd7dc 100644
@@ -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;
 	}
 
lib/utils/ipi/aclint_mswi.c +8 -6
index 140a49b..a3bfb4a 100644
@@ -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 = {
lib/utils/ipi/andes_plicsw.c +6 -2
index db25ae2..dde39c0 100644
@@ -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();
 
lib/utils/irqchip/imsic.c +2 -2
index 7fc61d9..78f5895 100644
@@ -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;
 
platform/generic/andes/ae350.c +1 -1
index 01bd02d..80eca05 100644
@@ -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);
Signature · 1 line
2.34.1
author
#13 lore raw

PATCH 6/8 lib: sbi: Remove sbi_scratch_last_hartid() macro

In reply to Anup Patel

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.

---
 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(-)
include/sbi/sbi_scratch.h +0 -6
index 9a4dce1..e6a33ba 100644
@@ -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))
lib/sbi/sbi_hsm.c +13 -15
index 814130e..147f954 100644
@@ -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);
lib/sbi/sbi_init.c +6 -4
index a6d96e6..e723553 100644
@@ -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 */
lib/sbi/sbi_scratch.c +1 -4
index d2abc89..6aeb0ca 100644
@@ -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;
Signature · 1 line
2.34.1
author
#14 lore raw

PATCH 7/8 lib: sbi: Maximize the use of HART index in sbi_domain

In reply to Anup Patel

Let us maximize the use of HART index in sbi_domain because hartindex based hartmask access and sbi_scratch lookup is faster.

---
 include/sbi/sbi_domain.h |  6 ++---
 lib/sbi/sbi_domain.c     | 56 +++++++++++++++++++++-------------------
 2 files changed, 33 insertions(+), 29 deletions(-)
include/sbi/sbi_domain.h +3 -3
index da2a65a..e10daff 100644
@@ -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[];
lib/sbi/sbi_domain.c +30 -26

136 lines not on this page · raw message

Signature · 1 line
2.34.1
author
#15 lore raw

PATCH 8/8 include: sbi: Remove sbi_hartmask_for_each_hart() macro

In reply to Anup Patel

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.

---
 include/sbi/sbi_hartmask.h | 13 -------------
 lib/sbi/sbi_system.c       |  3 ++-
 2 files changed, 2 insertions(+), 14 deletions(-)
include/sbi/sbi_hartmask.h +0 -13
index 105653e..bcfa50d 100644
@@ -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
lib/sbi/sbi_system.c +2 -1
index bae9730..d1fa349 100644
@@ -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)
Signature · 1 line
2.34.1
#16 lore raw

cover OpenSBI sparse HART id support

In reply to Anup Patel
Quoted 21 lines · On Mon, Sep 4, 2023 at 9:33?AM Anup Patel <[email protected]> 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

Quoted 2 lines

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

Quoted 24 lines
 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