[PATCH v2 4/5] include: sbi: Add helpers for sbi_domain_memregion

Bo Gan
2025-11-17T05:50:43Z

New helpers:

- sbi_domain_memregion_disjoint:
Check if two memregion are disjoint

- sbi_domain_for_each_memregion_idx:
Iterate domain memregion with index

Signed-off-by: Bo Gan <ganboing@gmail.com>
---
include/sbi/sbi_domain.h | 23 +++++++++++++++++++++++
1 file changed, 23 insertions(+)

diff --git a/include/sbi/sbi_domain.h b/include/sbi/sbi_domain.h
index 1196d609..0d1b095f 100644
--- a/include/sbi/sbi_domain.h
+++ b/include/sbi/sbi_domain.h
@@ -164,6 +164,26 @@ struct sbi_domain_memregion {
unsigned long flags;
};

+/**
+ * Check if two memregion A/B are disjoint.
+ * @param a memregion A
+ * @param b memregion B
+ * @return true if disjoint
+ */
+static inline bool sbi_domain_memregion_disjoint(
+ struct sbi_domain_memregion* a,
+ struct sbi_domain_memregion* b)
+{
+ /* Bail out early to avoid overflow */
+ if (a->order >= __riscv_xlen || b->order >= __riscv_xlen)
+ return false;
+
+ if (a->base < b->base)
+ return b->base - a->base >= (1UL << a->order);
+ else
+ return a->base - b->base >= (1UL << b->order);
+}
+
/** Representation of OpenSBI domain */
struct sbi_domain {
/** Node in linked list of domains */
@@ -222,6 +242,9 @@ extern struct sbi_dlist domain_list;
#define sbi_domain_for_each_memregion(__d, __r) \
for ((__r) = (__d)->regions; (__r)->order; (__r)++)

+#define sbi_domain_for_each_memregion_idx(__d, __r, __i) \
+ for ((__r) = (__d)->regions, (__i) = 0; (__r)->order; (__r)++, (__i)++)
+
/**
* Check whether given HART is assigned to specified domain
* @param dom pointer to domain