Skip to contentSkip to conversation

sbi_domain fixes

PATCH New Nikita Shubin 5 participants 16 messages Feb 11, 2022 – Feb 18, 2022

lore
Share & tools
lore
#0 lore raw

cover sbi_domain fixes

From: Nikita Shubin <[email protected]>

The first patch is self-explanatory.

In the second one i would prefer implementing new SBI_EEXISTS code instead of using SBI_EALREADY, we should really give more hints on this type of error instead of returning SBI_EINVAL for all kind of errors.

The third one is attempt to fix case when we share mtimeaddr among several mtimer's with different or same mtimercmp addresses.

I personally don't like the third patch - so suggestions are welcome.

Nikita Shubin (3):
lib: sbi: fix typo in is_region_subset
lib: sbi: verbose sbi_domain_root_add_memregion
lib: utils/timer: check if addr exists

 include/sbi/sbi_domain.h        |  4 ++-
 lib/sbi/sbi_domain.c            | 10 +++++--
 lib/utils/timer/aclint_mtimer.c | 51 ++++++++++++++++++++++++++-------
 3 files changed, 50 insertions(+), 15 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH 1/3 lib: sbi: fix typo in is_region_subset

From: Nikita Shubin <[email protected]>

Fix typo in is_region_subset, regB_end should be calculated from regB.

---
 lib/sbi/sbi_domain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_domain.c +1 -1
index b60915c..a1ac1bb 100644
@@ -159,7 +159,7 @@ static bool is_region_subset(const struct sbi_domain_memregion *regA,
 	ulong regA_start = regA->base;
 	ulong regA_end = regA->base + (BIT(regA->order) - 1);
 	ulong regB_start = regB->base;
-	ulong regB_end = regB->base + (BIT(regA->order) - 1);
+	ulong regB_end = regB->base + (BIT(regB->order) - 1);
 
 	if ((regB_start <= regA_start) &&
 	    (regA_start < regB_end) &&
Signature · 1 line
2.31.1
#2 lore raw

PATCH 1/3 lib: sbi: fix typo in is_region_subset

Quoted 5 lines · Nikita Shubin nikita.shubin at maquefel.me

From: Nikita Shubin <[email protected]>

Fix typo in is_region_subset, regB_end should be calculated from regB.

Looks good to me. Thx for the patch.

Regards,
Dong

Quoted 26 lines

---
lib/sbi/sbi_domain.c | 2 +-

1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_domain.c +1 -1
index b60915c..a1ac1bb 100644
@@ -159,7 +159,7 @@ static bool is_region_subset(const struct
 sbi_domain_memregion *regA,
 	ulong regA_start = regA->base;
 	ulong regA_end = regA->base + (BIT(regA->order) - 1);
 	ulong regB_start = regB->base;
-	ulong regB_end = regB->base + (BIT(regA->order) - 1);
+	ulong regB_end = regB->base + (BIT(regB->order) - 1);
 
 	if ((regB_start <= regA_start) &&
	    (regA_start < regB_end) &&

--
2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#3 lore raw

PATCH 1/3 lib: sbi: fix typo in is_region_subset

In reply to Nikita Shubin

? 2022-02-11???? 14:11 +0300?Nikita Shubin???

Quoted 5 lines

From: Nikita Shubin <[email protected]>

Fix typo in is_region_subset, regB_end should be calculated from regB.

LGTM

Quoted 22 lines

---
?lib/sbi/sbi_domain.c | 2 +-
?1 file changed, 1 insertion(+), 1 deletion(-)

lib/sbi/sbi_domain.c +1 -1
index b60915c..a1ac1bb 100644
@@ -159,7 +159,7 @@ static bool is_region_subset(const struct
 sbi_domain_memregion *regA,
 ????????ulong regA_start = regA->base;
 ????????ulong regA_end = regA->base + (BIT(regA->order) - 1);
 ????????ulong regB_start = regB->base;
-???????ulong regB_end = regB->base + (BIT(regA->order) - 1);
+???????ulong regB_end = regB->base + (BIT(regB->order) - 1);

?
????????if ((regB_start <= regA_start) &&
??????????? (regA_start < regB_end) &&

Signature · 1 line
2.31.1
Applied
#4 lore raw

PATCH 1/3 lib: sbi: fix typo in is_region_subset

In reply to Nikita Shubin
Quoted 6 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

Fix typo in is_region_subset, regB_end should be calculated from regB.

Thanks for catching this issue.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 25 lines
---
 lib/sbi/sbi_domain.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/sbi/sbi_domain.c +1 -2
index b60915c..a1ac1bb 100644
@@ -159,7 +159,7 @@ static bool is_region_subset(const struct sbi_domain_memregion *regA,
        ulong regA_start = regA->base;
        ulong regA_end = regA->base + (BIT(regA->order) - 1);
        ulong regB_start = regB->base;
-       ulong regB_end = regB->base + (BIT(regA->order) - 1);
+       ulong regB_end = regB->base + (BIT(regB->order) - 1);
 
        if ((regB_start <= regA_start) &&
            (regA_start < regB_end) &&
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#5 lore raw

PATCH 2/3 lib: sbi: verbose sbi_domain_root_add_memregion

In reply to Nikita Shubin

From: Nikita Shubin <[email protected]>

Be more verbose on region confict, print addresses in conflict.

---
 include/sbi/sbi_domain.h | 4 +++-
 lib/sbi/sbi_domain.c     | 8 ++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)
include/sbi/sbi_domain.h +3 -1
index 15e3a53..b90f59c 100644
@@ -174,7 +174,9 @@ int sbi_domain_register(struct sbi_domain *dom,
  * Add a memory region to the root domain
  * @param reg pointer to the memory region to be added
  *
- * @return 0 on success and negative error code on failure
+ * @return 0 on success
+ * @return SBI_EALREADY if memory region conflicts with existing
+ * @return SBI_EINVAL otherwise
  */
 int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg);
 
lib/sbi/sbi_domain.c +6 -2
index a1ac1bb..4e4c1e1 100644
@@ -471,8 +471,12 @@ int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg)
 
 	/* Check for conflicts */
 	sbi_domain_for_each_memregion(&root, nreg) {
-		if (is_region_conflict(reg, nreg))
-			return SBI_EINVAL;
+		if (is_region_conflict(reg, nreg)) {
+			sbi_printf("%s: is_region_conflict check failed"
+			" 0x%lx conflicts existing 0x%lx\n", __func__,
+				   reg->base, nreg->base);
+			return SBI_EALREADY;
+		}
 	}
 
 	/* Append the memregion to root memregions */
Signature · 1 line
2.31.1
#6 lore raw

PATCH 2/3 lib: sbi: verbose sbi_domain_root_add_memregion

? 2022-02-11???? 14:11 +0300?Nikita Shubin???

Quoted 5 lines

From: Nikita Shubin <[email protected]>

Be more verbose on region confict, print addresses in conflict.

Look good to me.

Quoted 46 lines

---
?include/sbi/sbi_domain.h | 4 +++-
?lib/sbi/sbi_domain.c???? | 8 ++++++--
?2 files changed, 9 insertions(+), 3 deletions(-)

include/sbi/sbi_domain.h +0 -0
index 15e3a53..b90f59c 100644
@@ -174,7 +174,9 @@ int sbi_domain_register(struct sbi_domain *dom,

? * Add a memory region to the root domain
? * @param reg pointer to the memory region to be added
? *
- * @return 0 on success and negative error code on failure
+ * @return 0 on success
+ * @return SBI_EALREADY if memory region conflicts with existing
+ * @return SBI_EINVAL otherwise
? */
?int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg);
?

lib/sbi/sbi_domain.c +0 -0
index a1ac1bb..4e4c1e1 100644
@@ -471,8 +471,12 @@ int sbi_domain_root_add_memregion(const struct
 sbi_domain_memregion *reg)

?
????????/* Check for conflicts */ ????????sbi_domain_for_each_memregion(&root, nreg) { -???????????????if (is_region_conflict(reg, nreg)) -???????????????????????return SBI_EINVAL; +???????????????if (is_region_conflict(reg, nreg)) { +???????????????????????sbi_printf("%s: is_region_conflict check failed"
+???????????????????????" 0x%lx conflicts existing 0x%lx\n", __func__, +????????????????????????????????? reg->base, nreg->base); +???????????????????????return SBI_EALREADY; +???????????????}
????????}
?
????????/* Append the memregion to root memregions */

Signature · 1 line
2.31.1
Applied
#7 lore raw

PATCH 2/3 lib: sbi: verbose sbi_domain_root_add_memregion

In reply to Nikita Shubin
Quoted 6 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

Be more verbose on region confict, print addresses in conflict.

Looks good to me.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 47 lines
---
 include/sbi/sbi_domain.h | 4 +++-
 lib/sbi/sbi_domain.c     | 8 ++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)
include/sbi/sbi_domain.h +3 -1
index 15e3a53..b90f59c 100644
@@ -174,7 +174,9 @@ int sbi_domain_register(struct sbi_domain *dom,
  * Add a memory region to the root domain
  * @param reg pointer to the memory region to be added
  *
- * @return 0 on success and negative error code on failure
+ * @return 0 on success
+ * @return SBI_EALREADY if memory region conflicts with existing
+ * @return SBI_EINVAL otherwise
  */
 int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg);
 
lib/sbi/sbi_domain.c +6 -3
index a1ac1bb..4e4c1e1 100644
@@ -471,8 +471,12 @@ int sbi_domain_root_add_memregion(const struct sbi_domain_memregion *reg)
 
        /* Check for conflicts */
        sbi_domain_for_each_memregion(&root, nreg) {
-               if (is_region_conflict(reg, nreg))
-                       return SBI_EINVAL;
+               if (is_region_conflict(reg, nreg)) {
+                       sbi_printf("%s: is_region_conflict check failed"
+                       " 0x%lx conflicts existing 0x%lx\n", __func__,
+                                  reg->base, nreg->base);
+                       return SBI_EALREADY;
+               }
        }
 
        /* Append the memregion to root memregions */
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#8 lore raw

PATCH 3/3 lib: utils/timer: check if addr exists

In reply to Nikita Shubin

From: Nikita Shubin <[email protected]>

In case we want shared time addr register for different mtimer's, we need to check if region was already added to domain, otherwise sbi_domain_root_add_memregion will fail and will couse init to fail with:
init_coldboot: timer init failed (error -3)

---
 lib/utils/timer/aclint_mtimer.c | 51 ++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 11 deletions(-)
lib/utils/timer/aclint_mtimer.c +40 -11
index a957b1c..2c90e22 100644
@@ -175,6 +175,9 @@ int aclint_mtimer_cold_init(struct aclint_mtimer_data *mt,
 {
 	u32 i;
 	int rc;
+	struct aclint_mtimer_data *last_mt = NULL;
+	bool skip_time_addr = false;
+	bool skip_timecmp_addr = false;
 
 	/* Sanity checks */
 	if (!mt || !mt->mtime_size ||
@@ -206,27 +209,53 @@ int aclint_mtimer_cold_init(struct aclint_mtimer_data *mt,
 	for (i = 0; i < mt->hart_count; i++)
 		mtimer_hartid2data[mt->first_hartid + i] = mt;
 
+	/* Check if timer or timercmp addr already exists
+	 * otherwise aclint_mtimer_add_regions will fail
+	 * becouse of regions conflict.
+	 */
+	for (i = 0; i < mt->first_hartid; i++) {
+		if (last_mt && last_mt == mtimer_hartid2data[i])
+			continue;
+
+		if (mtimer_hartid2data[i]->mtime_addr == mt->mtime_addr)
+			skip_time_addr = true;
+
+		if (mtimer_hartid2data[i]->mtimecmp_addr == mt->mtimecmp_addr)
+			skip_timecmp_addr = true;
+
+		if (skip_time_addr && skip_timecmp_addr)
+			break;
+
+		last_mt = mtimer_hartid2data[i];
+	}
+
 	/* Add MTIMER regions to the root domain */
-	if (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
+	if ((mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) &&
+		!skip_timecmp_addr) {
 		rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
 					mt->mtime_size + mt->mtimecmp_size);
 		if (rc)
 			return rc;
-	} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {
+	} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size) &&
+		!skip_time_addr) {
 		rc = aclint_mtimer_add_regions(mt->mtime_addr,
 					mt->mtime_size + mt->mtimecmp_size);
 		if (rc)
 			return rc;
 	} else {
-		rc = aclint_mtimer_add_regions(mt->mtime_addr,
-						mt->mtime_size);
-		if (rc)
-			return rc;
-
-		rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
-						mt->mtimecmp_size);
-		if (rc)
-			return rc;
+		if (!skip_time_addr) {
+			rc = aclint_mtimer_add_regions(mt->mtime_addr,
+							mt->mtime_size);
+			if (rc)
+				return rc;
+		}
+
+		if (!skip_timecmp_addr) {
+			rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
+							mt->mtimecmp_size);
+			if (rc)
+				return rc;
+		}
 	}
 
 	mtimer.timer_freq = mt->mtime_freq;
Signature · 1 line
2.31.1
#9 lore raw

PATCH 3/3 lib: utils/timer: check if addr exists

Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

In case we want shared time addr register for different mtimer's, we need to check if region was already added to domain, otherwise sbi_domain_root_add_memregion will fail and will couse init to fail with:
init_coldboot: timer init failed (error -3)

A mtimer device has two separate base addresses:
1) mtime base address
2) mtimecmp base address

Two separate mtimer devices can share the same mtime register but cannot share mtimecmp registers.

Regards,
Anup

Quoted 93 lines
---
 lib/utils/timer/aclint_mtimer.c | 51 ++++++++++++++++++++++++++-------
 1 file changed, 40 insertions(+), 11 deletions(-)
lib/utils/timer/aclint_mtimer.c +40 -12
index a957b1c..2c90e22 100644
@@ -175,6 +175,9 @@ int aclint_mtimer_cold_init(struct aclint_mtimer_data *mt,
 {
        u32 i;
        int rc;
+       struct aclint_mtimer_data *last_mt = NULL;
+       bool skip_time_addr = false;
+       bool skip_timecmp_addr = false;
 
        /* Sanity checks */
        if (!mt || !mt->mtime_size ||
@@ -206,27 +209,53 @@ int aclint_mtimer_cold_init(struct aclint_mtimer_data *mt,
        for (i = 0; i < mt->hart_count; i++)
                mtimer_hartid2data[mt->first_hartid + i] = mt;
 
+       /* Check if timer or timercmp addr already exists
+        * otherwise aclint_mtimer_add_regions will fail
+        * becouse of regions conflict.
+        */
+       for (i = 0; i < mt->first_hartid; i++) {
+               if (last_mt && last_mt == mtimer_hartid2data[i])
+                       continue;
+
+               if (mtimer_hartid2data[i]->mtime_addr == mt->mtime_addr)
+                       skip_time_addr = true;
+
+               if (mtimer_hartid2data[i]->mtimecmp_addr == mt->mtimecmp_addr)
+                       skip_timecmp_addr = true;
+
+               if (skip_time_addr && skip_timecmp_addr)
+                       break;
+
+               last_mt = mtimer_hartid2data[i];
+       }
+
        /* Add MTIMER regions to the root domain */
-       if (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
+       if ((mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) &&
+               !skip_timecmp_addr) {
                rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
                                        mt->mtime_size + mt->mtimecmp_size);
                if (rc)
                        return rc;
-       } else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {
+       } else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size) &&
+               !skip_time_addr) {
                rc = aclint_mtimer_add_regions(mt->mtime_addr,
                                        mt->mtime_size + mt->mtimecmp_size);
                if (rc)
                        return rc;
        } else {
-               rc = aclint_mtimer_add_regions(mt->mtime_addr,
-                                               mt->mtime_size);
-               if (rc)
-                       return rc;
-
-               rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
-                                               mt->mtimecmp_size);
-               if (rc)
-                       return rc;
+               if (!skip_time_addr) {
+                       rc = aclint_mtimer_add_regions(mt->mtime_addr,
+                                                       mt->mtime_size);
+                       if (rc)
+                               return rc;
+               }
+
+               if (!skip_timecmp_addr) {
+                       rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
+                                                       mt->mtimecmp_size);
+                       if (rc)
+                               return rc;
+               }
        }
 
        mtimer.timer_freq = mt->mtime_freq;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#10 lore raw

PATCH 3/3 lib: utils/timer: check if addr exists

Hello Anup!

Quoted 11 lines · On Mon, 14 Feb 2022 17:08:36 +0530 Anup Patel <[email protected]> wrote:
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

In case we want shared time addr register for different mtimer's, we need to check if region was already added to domain, otherwise sbi_domain_root_add_memregion will fail and will couse init to fail with:
init_coldboot: timer init failed (error -3)

Quoted 6 lines

A mtimer device has two separate base addresses:
1) mtime base address
2) mtimecmp base address

Two separate mtimer devices can share the same mtime register but cannot share mtimecmp registers.

In case mtimecmp is less than mtime it is possible to run in following situation:

- mtimecmp 0 - N - 1 and mtime are not adjusted and treated separately, mtime exists check succeed
- mtimecmp N is adjusted to mtime and gets merged to mt->mtimecmp_addr
+ mt->mtime_size + mt->mtimecmp_size and fails with region conflict with existing mtime region added from previous steps

How about dropping these two checks:

if (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

sbi_domain will take care of and merge these separate regions, so we can check only mtime addr.

What do you think about this ?

Quoted 108 lines

Regards,
Anup

Quoted 104 lines
---
 lib/utils/timer/aclint_mtimer.c | 51

++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/lib/utils/timer/aclint_mtimer.c index a957b1c..2c90e22 100644

lib/utils/timer/aclint_mtimer.c +24 -1
@@ -175,6 +175,9 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, {
        u32 i;
        int rc;
+       struct aclint_mtimer_data *last_mt = NULL;
+       bool skip_time_addr = false;
+       bool skip_timecmp_addr = false;
 
        /* Sanity checks */
        if (!mt || !mt->mtime_size ||
@@ -206,27 +209,53 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, for (i = 0; i < mt->hart_count; i++)
                mtimer_hartid2data[mt->first_hartid + i] = mt;
 
+       /* Check if timer or timercmp addr already exists
+        * otherwise aclint_mtimer_add_regions will fail
+        * becouse of regions conflict.
+        */
+       for (i = 0; i < mt->first_hartid; i++) {
+               if (last_mt && last_mt == mtimer_hartid2data[i])
+                       continue;
+
+               if (mtimer_hartid2data[i]->mtime_addr ==
 mt->mtime_addr)
+                       skip_time_addr = true;
+
+               if (mtimer_hartid2data[i]->mtimecmp_addr ==
 mt->mtimecmp_addr)
+                       skip_timecmp_addr = true;
+
+               if (skip_time_addr && skip_timecmp_addr)
+                       break;
+
+               last_mt = mtimer_hartid2data[i];
+       }
+
        /* Add MTIMER regions to the root domain */
-       if (mt->mtime_addr == (mt->mtimecmp_addr +
 mt->mtimecmp_size)) {
+       if ((mt->mtime_addr == (mt->mtimecmp_addr +

mt->mtimecmp_size)) &&

+               !skip_timecmp_addr) {
                rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
                                        mt->mtime_size +
mt->mtimecmp_size); if (rc)
                        return rc;

- } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size)) {
+ } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size) &&

+               !skip_time_addr) {
                rc = aclint_mtimer_add_regions(mt->mtime_addr,
                                        mt->mtime_size +
mt->mtimecmp_size); if (rc)
                        return rc;
        } else {
lib/utils/timer/aclint_mtimer.c +2 -9
-               rc = aclint_mtimer_add_regions(mt->mtime_addr,
-                                               mt->mtime_size);
-               if (rc)
-                       return rc;
-
-               rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
-                                               mt->mtimecmp_size);
-               if (rc)
-                       return rc;
+               if (!skip_time_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtime_addr,
+
mt->mtime_size);

lib/utils/timer/aclint_mtimer.c +6 -0
+                       if (rc)
+                               return rc;
+               }
+
+               if (!skip_timecmp_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
+
mt->mtimecmp_size);

lib/utils/timer/aclint_mtimer.c +3 -1
+                       if (rc)
+                               return rc;
+               }
        }
 
        mtimer.timer_freq = mt->mtime_freq;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#11 lore raw

PATCH 3/3 lib: utils/timer: check if addr exists

Quoted 35 lines · Nikita Shubin

Hello Anup!

Quoted 11 lines · On Mon, 14 Feb 2022 17:08:36 +0530 Anup Patel <[email protected]> wrote:
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

In case we want shared time addr register for different mtimer's, we need to check if region was already added to domain, otherwise sbi_domain_root_add_memregion will fail and will couse init to fail with:
init_coldboot: timer init failed (error -3)

Quoted 6 lines

A mtimer device has two separate base addresses:
1) mtime base address
2) mtimecmp base address

Two separate mtimer devices can share the same mtime register but cannot share mtimecmp registers.

In case mtimecmp is less than mtime it is possible to run in following situation:

- mtimecmp 0 - N - 1 and mtime are not adjusted and treated separately, mtime exists check succeed
- mtimecmp N is adjusted to mtime and gets merged to mt->mtimecmp_addr
+ mt->mtime_size + mt->mtimecmp_size and fails with region conflict with existing mtime region added from previous steps

How about dropping these two checks:

Quoted 8 lines

if (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

sbi_domain will take care of and merge these separate regions, so we can check only mtime addr.

What do you think about this ?

I am still trying to understand the issue.

Can you share example DT nodes ?

Regards,
Anup

Quoted 111 lines
Quoted 110 lines

Regards,
Anup

Quoted 104 lines
---
 lib/utils/timer/aclint_mtimer.c | 51

++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/lib/utils/timer/aclint_mtimer.c index a957b1c..2c90e22 100644

lib/utils/timer/aclint_mtimer.c +24 -1
@@ -175,6 +175,9 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, {
        u32 i;
        int rc;
+       struct aclint_mtimer_data *last_mt = NULL;
+       bool skip_time_addr = false;
+       bool skip_timecmp_addr = false;
 
        /* Sanity checks */
        if (!mt || !mt->mtime_size ||
@@ -206,27 +209,53 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, for (i = 0; i < mt->hart_count; i++)
                mtimer_hartid2data[mt->first_hartid + i] = mt;
 
+       /* Check if timer or timercmp addr already exists
+        * otherwise aclint_mtimer_add_regions will fail
+        * becouse of regions conflict.
+        */
+       for (i = 0; i < mt->first_hartid; i++) {
+               if (last_mt && last_mt == mtimer_hartid2data[i])
+                       continue;
+
+               if (mtimer_hartid2data[i]->mtime_addr ==
 mt->mtime_addr)
+                       skip_time_addr = true;
+
+               if (mtimer_hartid2data[i]->mtimecmp_addr ==
 mt->mtimecmp_addr)
+                       skip_timecmp_addr = true;
+
+               if (skip_time_addr && skip_timecmp_addr)
+                       break;
+
+               last_mt = mtimer_hartid2data[i];
+       }
+
        /* Add MTIMER regions to the root domain */
-       if (mt->mtime_addr == (mt->mtimecmp_addr +
 mt->mtimecmp_size)) {
+       if ((mt->mtime_addr == (mt->mtimecmp_addr +

mt->mtimecmp_size)) &&

+               !skip_timecmp_addr) {
                rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
                                        mt->mtime_size +
mt->mtimecmp_size); if (rc)
                        return rc;

- } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size)) {
+ } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size) &&

+               !skip_time_addr) {
                rc = aclint_mtimer_add_regions(mt->mtime_addr,
                                        mt->mtime_size +
mt->mtimecmp_size); if (rc)
                        return rc;
        } else {
lib/utils/timer/aclint_mtimer.c +2 -9
-               rc = aclint_mtimer_add_regions(mt->mtime_addr,
-                                               mt->mtime_size);
-               if (rc)
-                       return rc;
-
-               rc = aclint_mtimer_add_regions(mt->mtimecmp_addr,
-                                               mt->mtimecmp_size);
-               if (rc)
-                       return rc;
+               if (!skip_time_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtime_addr,
+
mt->mtime_size);

lib/utils/timer/aclint_mtimer.c +6 -0
+                       if (rc)
+                               return rc;
+               }
+
+               if (!skip_timecmp_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
+
mt->mtimecmp_size);

lib/utils/timer/aclint_mtimer.c +3 -1
+                       if (rc)
+                               return rc;
+               }
        }
 
        mtimer.timer_freq = mt->mtime_freq;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#12 lore raw

PATCH 3/3 lib: utils/timer: check if addr exists

Quoted 42 lines · On Mon, 14 Feb 2022 17:57:44 +0530 Anup Patel <[email protected]> wrote:
Quoted 40 lines · Nikita Shubin

Hello Anup!

Quoted 11 lines · On Mon, 14 Feb 2022 17:08:36 +0530 Anup Patel <[email protected]> wrote:
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

In case we want shared time addr register for different mtimer's, we need to check if region was already added to domain, otherwise sbi_domain_root_add_memregion will fail and will couse init to fail with:
init_coldboot: timer init failed (error -3)

Quoted 6 lines

A mtimer device has two separate base addresses:
1) mtime base address
2) mtimecmp base address

Two separate mtimer devices can share the same mtime register but cannot share mtimecmp registers.

In case mtimecmp is less than mtime it is possible to run in following situation:

- mtimecmp 0 - N - 1 and mtime are not adjusted and treated separately, mtime exists check succeed
- mtimecmp N is adjusted to mtime and gets merged to mt->mtimecmp_addr
+ mt->mtime_size + mt->mtimecmp_size and fails with region
conflict with existing mtime region added from previous steps

How about dropping these two checks:

if (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

Quoted 8 lines
Quoted 4 lines

sbi_domain will take care of and merge these separate regions, so we can check only mtime addr.

What do you think about this ?

I am still trying to understand the issue.

Can you share example DT nodes ?

That's a bit modified example from your presentation on plumbers:

mtimer0: mtimer at 30000000 {
	compatible = "riscv,aclint-mtimer";
	reg = <0x3a000000 0x8>,
	 /* MTIME */
	<0x30000000 0x20>; /* MTIMECMPs */
	interrupts-extended = <0x04 7>;
};
mtimer1: mtimer at 31000000 {
	compatible = "riscv,aclint-mtimer";
	reg = <0x3a000000 0x8>,
	 /* MTIME */
	<0x31000000 0x20>; /* MTIMECMPs */
	interrupts-extended = <0x02 7>;
};

The regions are not adjusted and this will fail.

Adjusted regions like i described:

mtimer0: mtimer at 30000000 {
	compatible = "riscv,aclint-mtimer";
	reg = <0x30000000 0x1000>,
	 /* MTIME */
	<0x2ffff000 0x1000>; /* MTIMECMPs */
	interrupts-extended = <0x04 7>;
};
mtimer1: mtimer at 31000000 {
	compatible = "riscv,aclint-mtimer";
	reg = <0x30000000 0x1000>,
	 /* MTIME */
	<0x2fffe000 0x1000>; /* MTIMECMPs */
	interrupts-extended = <0x02 7>;
};

The main reason why earlier worked becouse qemu generates mtimer bindings like this:

reg = <0x00 0x200bff8 0x00 0x4008 0x00 0x2004000 0x00 0x7ff8>;

In other words everything bigger than 0x1000 will succeed becouse of sbi_domain bug corrected in patch 1.

Quoted 117 lines

Regards,
Anup

Quoted 113 lines
Quoted 112 lines

Regards,
Anup

Quoted 106 lines
---
 lib/utils/timer/aclint_mtimer.c | 51

++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/lib/utils/timer/aclint_mtimer.c index a957b1c..2c90e22 100644

lib/utils/timer/aclint_mtimer.c +24 -1
@@ -175,6 +175,9 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, {
        u32 i;
        int rc;
+       struct aclint_mtimer_data *last_mt = NULL;
+       bool skip_time_addr = false;
+       bool skip_timecmp_addr = false;
 
        /* Sanity checks */
        if (!mt || !mt->mtime_size ||
@@ -206,27 +209,53 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, for (i = 0; i < mt->hart_count; i++)
                mtimer_hartid2data[mt->first_hartid + i] = mt;
 
+       /* Check if timer or timercmp addr already exists
+        * otherwise aclint_mtimer_add_regions will fail
+        * becouse of regions conflict.
+        */
+       for (i = 0; i < mt->first_hartid; i++) {
+               if (last_mt && last_mt == mtimer_hartid2data[i])
+                       continue;
+
+               if (mtimer_hartid2data[i]->mtime_addr ==
 mt->mtime_addr)
+                       skip_time_addr = true;
+
+               if (mtimer_hartid2data[i]->mtimecmp_addr ==
 mt->mtimecmp_addr)
+                       skip_timecmp_addr = true;
+
+               if (skip_time_addr && skip_timecmp_addr)
+                       break;
+
+               last_mt = mtimer_hartid2data[i];
+       }
+
        /* Add MTIMER regions to the root domain */
-       if (mt->mtime_addr == (mt->mtimecmp_addr +
 mt->mtimecmp_size)) {
+       if ((mt->mtime_addr == (mt->mtimecmp_addr +

mt->mtimecmp_size)) &&
+ !skip_timecmp_addr) {
rc =
aclint_mtimer_add_regions(mt->mtimecmp_addr, mt->mtime_size + mt->mtimecmp_size); if (rc)

                        return rc;

- } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size)) {
+ } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size) &&

+               !skip_time_addr) {
                rc = aclint_mtimer_add_regions(mt->mtime_addr,
                                        mt->mtime_size +
mt->mtimecmp_size); if (rc)
                        return rc;
        } else {
lib/utils/timer/aclint_mtimer.c +0 -6
-               rc = aclint_mtimer_add_regions(mt->mtime_addr,
-                                               mt->mtime_size);
-               if (rc)
-                       return rc;
-
-               rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
-
mt->mtimecmp_size);

lib/utils/timer/aclint_mtimer.c +2 -2
-               if (rc)
-                       return rc;
+               if (!skip_time_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtime_addr,
+
mt->mtime_size);

lib/utils/timer/aclint_mtimer.c +6 -0
+                       if (rc)
+                               return rc;
+               }
+
+               if (!skip_timecmp_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
+
mt->mtimecmp_size);

lib/utils/timer/aclint_mtimer.c +3 -1
+                       if (rc)
+                               return rc;
+               }
        }
 
        mtimer.timer_freq = mt->mtime_freq;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#13 lore raw

PATCH 3/3 lib: utils/timer: check if addr exists

Quoted 93 lines · Nikita Shubin
Quoted 42 lines · On Mon, 14 Feb 2022 17:57:44 +0530 Anup Patel <[email protected]> wrote:
Quoted 40 lines · Nikita Shubin

Hello Anup!

Quoted 11 lines · On Mon, 14 Feb 2022 17:08:36 +0530 Anup Patel <[email protected]> wrote:
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

In case we want shared time addr register for different mtimer's, we need to check if region was already added to domain, otherwise sbi_domain_root_add_memregion will fail and will couse init to fail with:
init_coldboot: timer init failed (error -3)

Quoted 6 lines

A mtimer device has two separate base addresses:
1) mtime base address
2) mtimecmp base address

Two separate mtimer devices can share the same mtime register but cannot share mtimecmp registers.

In case mtimecmp is less than mtime it is possible to run in following situation:

- mtimecmp 0 - N - 1 and mtime are not adjusted and treated separately, mtime exists check succeed
- mtimecmp N is adjusted to mtime and gets merged to mt->mtimecmp_addr
+ mt->mtime_size + mt->mtimecmp_size and fails with region
conflict with existing mtime region added from previous steps

How about dropping these two checks:

if (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

Quoted 8 lines
Quoted 4 lines

sbi_domain will take care of and merge these separate regions, so we can check only mtime addr.

What do you think about this ?

I am still trying to understand the issue.

Can you share example DT nodes ?

That's a bit modified example from your presentation on plumbers:

mtimer0: mtimer at 30000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x3a000000 0x8>,
         /* MTIME */
        <0x30000000 0x20>; /* MTIMECMPs */
        interrupts-extended = <0x04 7>;
};
mtimer1: mtimer at 31000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x3a000000 0x8>,
         /* MTIME */
        <0x31000000 0x20>; /* MTIMECMPs */
        interrupts-extended = <0x02 7>;
};

The regions are not adjusted and this will fail.

Adjusted regions like i described:

mtimer0: mtimer at 30000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x30000000 0x1000>,
         /* MTIME */
        <0x2ffff000 0x1000>; /* MTIMECMPs */
        interrupts-extended = <0x04 7>;
};
mtimer1: mtimer at 31000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x30000000 0x1000>,
         /* MTIME */
        <0x2fffe000 0x1000>; /* MTIMECMPs */
        interrupts-extended = <0x02 7>;
};
Quoted 7 lines

The main reason why earlier worked becouse qemu generates mtimer bindings like this:

reg = <0x00 0x200bff8 0x00 0x4008 0x00 0x2004000 0x00 0x7ff8>;

In other words everything bigger than 0x1000 will succeed becouse of sbi_domain bug corrected in patch 1.

I understand the problem now.

I agree with your suggestion. We should let sbi_domain do all the region merging so we should drop following checks:
f (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

Also, we should only add mtime region if it is not already added by other mtimer devices.

Can you send a v2 patch ?

Regards,
Anup

Quoted 120 lines
Quoted 119 lines

Regards,
Anup

Quoted 113 lines
Quoted 112 lines

Regards,
Anup

Quoted 106 lines
---
 lib/utils/timer/aclint_mtimer.c | 51

++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/lib/utils/timer/aclint_mtimer.c index a957b1c..2c90e22 100644

lib/utils/timer/aclint_mtimer.c +24 -1
@@ -175,6 +175,9 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, {
        u32 i;
        int rc;
+       struct aclint_mtimer_data *last_mt = NULL;
+       bool skip_time_addr = false;
+       bool skip_timecmp_addr = false;
 
        /* Sanity checks */
        if (!mt || !mt->mtime_size ||
@@ -206,27 +209,53 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, for (i = 0; i < mt->hart_count; i++)
                mtimer_hartid2data[mt->first_hartid + i] = mt;
 
+       /* Check if timer or timercmp addr already exists
+        * otherwise aclint_mtimer_add_regions will fail
+        * becouse of regions conflict.
+        */
+       for (i = 0; i < mt->first_hartid; i++) {
+               if (last_mt && last_mt == mtimer_hartid2data[i])
+                       continue;
+
+               if (mtimer_hartid2data[i]->mtime_addr ==
 mt->mtime_addr)
+                       skip_time_addr = true;
+
+               if (mtimer_hartid2data[i]->mtimecmp_addr ==
 mt->mtimecmp_addr)
+                       skip_timecmp_addr = true;
+
+               if (skip_time_addr && skip_timecmp_addr)
+                       break;
+
+               last_mt = mtimer_hartid2data[i];
+       }
+
        /* Add MTIMER regions to the root domain */
-       if (mt->mtime_addr == (mt->mtimecmp_addr +
 mt->mtimecmp_size)) {
+       if ((mt->mtime_addr == (mt->mtimecmp_addr +

mt->mtimecmp_size)) &&
+ !skip_timecmp_addr) {
rc =
aclint_mtimer_add_regions(mt->mtimecmp_addr, mt->mtime_size + mt->mtimecmp_size); if (rc)

                        return rc;

- } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size)) {
+ } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size) &&

+               !skip_time_addr) {
                rc = aclint_mtimer_add_regions(mt->mtime_addr,
                                        mt->mtime_size +
mt->mtimecmp_size); if (rc)
                        return rc;
        } else {
lib/utils/timer/aclint_mtimer.c +0 -6
-               rc = aclint_mtimer_add_regions(mt->mtime_addr,
-                                               mt->mtime_size);
-               if (rc)
-                       return rc;
-
-               rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
-
mt->mtimecmp_size);

lib/utils/timer/aclint_mtimer.c +2 -2
-               if (rc)
-                       return rc;
+               if (!skip_time_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtime_addr,
+
mt->mtime_size);

lib/utils/timer/aclint_mtimer.c +6 -0
+                       if (rc)
+                               return rc;
+               }
+
+               if (!skip_timecmp_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
+
mt->mtimecmp_size);

lib/utils/timer/aclint_mtimer.c +3 -1
+                       if (rc)
+                               return rc;
+               }
        }
 
        mtimer.timer_freq = mt->mtime_freq;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#14 lore raw

PATCH 3/3 lib: utils/timer: check if addr exists

Hello Anup!

On Fri, 18 Feb 2022 10:59:56 +0530
Anup Patel <[email protected]> wrote:

Thas't a bit more complicated than i thought, if we remove the region bounds checks in aclint_mtimer driver we run into following:

```
aclint_mtimer_add_regions : addr=0x200bff8, size=0x4008
aclint_mtimer_add_regions : pos=0x200bff8, rsize=0x8
aclint_mtimer_add_regions : reg->base=0x200bff8, size=0x7
aclint_mtimer_add_regions : pos=0x200c000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200c000, size=0xfff
aclint_mtimer_add_regions : pos=0x200d000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200d000, size=0xfff
aclint_mtimer_add_regions : pos=0x200e000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200e000, size=0xfff
aclint_mtimer_add_regions : pos=0x200f000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200f000, size=0xfff
aclint_mtimer_add_regions : addr=0x2004000, size=0x7ff8
aclint_mtimer_add_regions : pos=0x2004000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2004000, size=0xfff
aclint_mtimer_add_regions : pos=0x2005000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2005000, size=0xfff
aclint_mtimer_add_regions : pos=0x2006000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2006000, size=0xfff
aclint_mtimer_add_regions : pos=0x2007000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2007000, size=0xfff
aclint_mtimer_add_regions : pos=0x2008000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2008000, size=0xfff
aclint_mtimer_add_regions : pos=0x2009000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2009000, size=0xfff
aclint_mtimer_add_regions : pos=0x200a000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200a000, size=0xfff
aclint_mtimer_add_regions : pos=0x200b000, rsize=0xff8
aclint_mtimer_add_regions : reg->base=0x200b000, size=0xfff
sbi_domain_root_add_memregion: is_region_conflict check failed
0x200b000 conflicts existing 0x200bff8
init_coldboot: timer init failed (error -6)
```

QEMU virt machine dts with aclint=on:

mtimer at 2004000 {
	interrupts-extended = <0x04 0x07 0x02 0x07>;
	reg = <0x00 0x200bff8 0x00 0x4008 
		0x00 0x2004000 0x00 0x7ff8>;
	compatible = "riscv,aclint-mtimer";
};

Indeed:

```
(gdb) p/x 0x2004000 + 0x7ff8 + 0x4008
$8 = 0x2010000
```

Give's us proper aligned region, while adding separate mtime/mtimecmp gives us an overlap on adding, so i have to think about this.

Quoted 244 lines
Quoted 94 lines · Nikita Shubin
Quoted 43 lines · On Mon, 14 Feb 2022 17:57:44 +0530 Anup Patel <[email protected]> wrote:
Quoted 41 lines · Nikita Shubin

Hello Anup!

Quoted 11 lines · On Mon, 14 Feb 2022 17:08:36 +0530 Anup Patel <[email protected]> wrote:
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

In case we want shared time addr register for different mtimer's, we need to check if region was already added to domain, otherwise sbi_domain_root_add_memregion will fail and will couse init to fail with:
init_coldboot: timer init failed (error -3)

Quoted 6 lines

A mtimer device has two separate base addresses:
1) mtime base address
2) mtimecmp base address

Two separate mtimer devices can share the same mtime register but cannot share mtimecmp registers.

In case mtimecmp is less than mtime it is possible to run in following situation:

- mtimecmp 0 - N - 1 and mtime are not adjusted and treated separately, mtime exists check succeed
- mtimecmp N is adjusted to mtime and gets merged to mt->mtimecmp_addr
+ mt->mtime_size + mt->mtimecmp_size and fails with region
conflict with existing mtime region added from previous steps

How about dropping these two checks:

if (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

Quoted 8 lines
Quoted 4 lines

sbi_domain will take care of and merge these separate regions, so we can check only mtime addr.

What do you think about this ?

I am still trying to understand the issue.

Can you share example DT nodes ?

That's a bit modified example from your presentation on plumbers:

mtimer0: mtimer at 30000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x3a000000 0x8>,
         /* MTIME */
        <0x30000000 0x20>; /* MTIMECMPs */
        interrupts-extended = <0x04 7>;
};
mtimer1: mtimer at 31000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x3a000000 0x8>,
         /* MTIME */
        <0x31000000 0x20>; /* MTIMECMPs */
        interrupts-extended = <0x02 7>;
};

The regions are not adjusted and this will fail.

Adjusted regions like i described:

mtimer0: mtimer at 30000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x30000000 0x1000>,
         /* MTIME */
        <0x2ffff000 0x1000>; /* MTIMECMPs */
        interrupts-extended = <0x04 7>;
};
mtimer1: mtimer at 31000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x30000000 0x1000>,
         /* MTIME */
        <0x2fffe000 0x1000>; /* MTIMECMPs */
        interrupts-extended = <0x02 7>;
};
Quoted 7 lines

The main reason why earlier worked becouse qemu generates mtimer bindings like this:

reg = <0x00 0x200bff8 0x00 0x4008 0x00 0x2004000 0x00 0x7ff8>;

In other words everything bigger than 0x1000 will succeed becouse of sbi_domain bug corrected in patch 1.

I understand the problem now.

I agree with your suggestion. We should let sbi_domain do all the region merging so we should drop following checks:
f (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

Also, we should only add mtime region if it is not already added by other mtimer devices.

Can you send a v2 patch ?

Regards,
Anup

Quoted 124 lines
Quoted 123 lines

Regards,
Anup

Quoted 117 lines
Quoted 116 lines

Regards,
Anup

Quoted 110 lines
---
 lib/utils/timer/aclint_mtimer.c | 51

++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/lib/utils/timer/aclint_mtimer.c index a957b1c..2c90e22 100644 --- a/lib/utils/timer/aclint_mtimer.c +++ b/lib/utils/timer/aclint_mtimer.c

lib/utils/timer/aclint_mtimer.c +24 -1
@@ -175,6 +175,9 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, {
        u32 i;
        int rc;
+       struct aclint_mtimer_data *last_mt = NULL;
+       bool skip_time_addr = false;
+       bool skip_timecmp_addr = false;
 
        /* Sanity checks */
        if (!mt || !mt->mtime_size ||
@@ -206,27 +209,53 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, for (i = 0; i < mt->hart_count; i++)
                mtimer_hartid2data[mt->first_hartid + i] =
 mt;
 
+       /* Check if timer or timercmp addr already exists
+        * otherwise aclint_mtimer_add_regions will fail
+        * becouse of regions conflict.
+        */
+       for (i = 0; i < mt->first_hartid; i++) {
+               if (last_mt && last_mt ==
 mtimer_hartid2data[i])
+                       continue;
+
+               if (mtimer_hartid2data[i]->mtime_addr ==
 mt->mtime_addr)
+                       skip_time_addr = true;
+
+               if (mtimer_hartid2data[i]->mtimecmp_addr ==
 mt->mtimecmp_addr)
+                       skip_timecmp_addr = true;
+
+               if (skip_time_addr && skip_timecmp_addr)
+                       break;
+
+               last_mt = mtimer_hartid2data[i];
+       }
+
        /* Add MTIMER regions to the root domain */
-       if (mt->mtime_addr == (mt->mtimecmp_addr +
 mt->mtimecmp_size)) {
+       if ((mt->mtime_addr == (mt->mtimecmp_addr +

mt->mtimecmp_size)) &&
+ !skip_timecmp_addr) {
rc =
aclint_mtimer_add_regions(mt->mtimecmp_addr, mt->mtime_size
+ mt->mtimecmp_size); if (rc)

                        return rc;

- } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size)) {
+ } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size) &&
+ !skip_time_addr) {
rc =
aclint_mtimer_add_regions(mt->mtime_addr, mt->mtime_size + mt->mtimecmp_size); if (rc)

                        return rc;
        } else {

- rc =
aclint_mtimer_add_regions(mt->mtime_addr,
-
mt->mtime_size);

lib/utils/timer/aclint_mtimer.c +0 -4
-               if (rc)
-                       return rc;
-
-               rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
-
mt->mtimecmp_size);

lib/utils/timer/aclint_mtimer.c +2 -2
-               if (rc)
-                       return rc;
+               if (!skip_time_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtime_addr,
+
mt->mtime_size);

lib/utils/timer/aclint_mtimer.c +6 -0
+                       if (rc)
+                               return rc;
+               }
+
+               if (!skip_timecmp_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
+
mt->mtimecmp_size);

lib/utils/timer/aclint_mtimer.c +3 -1
+                       if (rc)
+                               return rc;
+               }
        }
 
        mtimer.timer_freq = mt->mtime_freq;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

#15 lore raw

PATCH 3/3 lib: utils/timer: check if addr exists

Quoted 55 lines · Nikita Shubin

Hello Anup!

On Fri, 18 Feb 2022 10:59:56 +0530
Anup Patel <[email protected]> wrote:

Thas't a bit more complicated than i thought, if we remove the region bounds checks in aclint_mtimer driver we run into following:

```
aclint_mtimer_add_regions : addr=0x200bff8, size=0x4008
aclint_mtimer_add_regions : pos=0x200bff8, rsize=0x8
aclint_mtimer_add_regions : reg->base=0x200bff8, size=0x7
aclint_mtimer_add_regions : pos=0x200c000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200c000, size=0xfff
aclint_mtimer_add_regions : pos=0x200d000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200d000, size=0xfff
aclint_mtimer_add_regions : pos=0x200e000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200e000, size=0xfff
aclint_mtimer_add_regions : pos=0x200f000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200f000, size=0xfff
aclint_mtimer_add_regions : addr=0x2004000, size=0x7ff8
aclint_mtimer_add_regions : pos=0x2004000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2004000, size=0xfff
aclint_mtimer_add_regions : pos=0x2005000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2005000, size=0xfff
aclint_mtimer_add_regions : pos=0x2006000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2006000, size=0xfff
aclint_mtimer_add_regions : pos=0x2007000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2007000, size=0xfff
aclint_mtimer_add_regions : pos=0x2008000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2008000, size=0xfff
aclint_mtimer_add_regions : pos=0x2009000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x2009000, size=0xfff
aclint_mtimer_add_regions : pos=0x200a000, rsize=0x1000
aclint_mtimer_add_regions : reg->base=0x200a000, size=0xfff
aclint_mtimer_add_regions : pos=0x200b000, rsize=0xff8
aclint_mtimer_add_regions : reg->base=0x200b000, size=0xfff
sbi_domain_root_add_memregion: is_region_conflict check failed
0x200b000 conflicts existing 0x200bff8
init_coldboot: timer init failed (error -6)
```

QEMU virt machine dts with aclint=on:

mtimer at 2004000 {
        interrupts-extended = <0x04 0x07 0x02 0x07>;
        reg = <0x00 0x200bff8 0x00 0x4008
                0x00 0x2004000 0x00 0x7ff8>;
        compatible = "riscv,aclint-mtimer";
};

Indeed:

Quoted 7 lines
```
(gdb) p/x 0x2004000 + 0x7ff8 + 0x4008
$8 = 0x2010000
```

Give's us proper aligned region, while adding separate mtime/mtimecmp gives us an overlap on adding, so i have to think about this.

When mtime and mtimecmp are at separate locations, we should at least check whether the mtime region is already added.
OR
Maybe we can do something similar to this patch except we don't need skip_timecmp_addr since every mtimer device will have a unique set of mtimecmp registers.

Regards,
Anup

Quoted 252 lines
Quoted 114 lines
Quoted 94 lines · Nikita Shubin
Quoted 43 lines · On Mon, 14 Feb 2022 17:57:44 +0530 Anup Patel <[email protected]> wrote:
Quoted 41 lines · Nikita Shubin

Hello Anup!

Quoted 11 lines · On Mon, 14 Feb 2022 17:08:36 +0530 Anup Patel <[email protected]> wrote:
Quoted 8 lines · Nikita Shubin

From: Nikita Shubin <[email protected]>

In case we want shared time addr register for different mtimer's, we need to check if region was already added to domain, otherwise sbi_domain_root_add_memregion will fail and will couse init to fail with:
init_coldboot: timer init failed (error -3)

Quoted 6 lines

A mtimer device has two separate base addresses:
1) mtime base address
2) mtimecmp base address

Two separate mtimer devices can share the same mtime register but cannot share mtimecmp registers.

In case mtimecmp is less than mtime it is possible to run in following situation:

- mtimecmp 0 - N - 1 and mtime are not adjusted and treated separately, mtime exists check succeed
- mtimecmp N is adjusted to mtime and gets merged to mt->mtimecmp_addr
+ mt->mtime_size + mt->mtimecmp_size and fails with region
conflict with existing mtime region added from previous steps

How about dropping these two checks:

if (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

Quoted 8 lines
Quoted 4 lines

sbi_domain will take care of and merge these separate regions, so we can check only mtime addr.

What do you think about this ?

I am still trying to understand the issue.

Can you share example DT nodes ?

That's a bit modified example from your presentation on plumbers:

mtimer0: mtimer at 30000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x3a000000 0x8>,
         /* MTIME */
        <0x30000000 0x20>; /* MTIMECMPs */
        interrupts-extended = <0x04 7>;
};
mtimer1: mtimer at 31000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x3a000000 0x8>,
         /* MTIME */
        <0x31000000 0x20>; /* MTIMECMPs */
        interrupts-extended = <0x02 7>;
};

The regions are not adjusted and this will fail.

Adjusted regions like i described:

mtimer0: mtimer at 30000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x30000000 0x1000>,
         /* MTIME */
        <0x2ffff000 0x1000>; /* MTIMECMPs */
        interrupts-extended = <0x04 7>;
};
mtimer1: mtimer at 31000000 {
        compatible = "riscv,aclint-mtimer";
        reg = <0x30000000 0x1000>,
         /* MTIME */
        <0x2fffe000 0x1000>; /* MTIMECMPs */
        interrupts-extended = <0x02 7>;
};
Quoted 7 lines

The main reason why earlier worked becouse qemu generates mtimer bindings like this:

reg = <0x00 0x200bff8 0x00 0x4008 0x00 0x2004000 0x00 0x7ff8>;

In other words everything bigger than 0x1000 will succeed becouse of sbi_domain bug corrected in patch 1.

I understand the problem now.

I agree with your suggestion. We should let sbi_domain do all the region merging so we should drop following checks:
f (mt->mtime_addr == (mt->mtimecmp_addr + mt->mtimecmp_size)) {
...
} else if (mt->mtimecmp_addr == (mt->mtime_addr + mt->mtime_size)) {

Quoted 132 lines

Also, we should only add mtime region if it is not already added by other mtimer devices.

Can you send a v2 patch ?

Regards,
Anup

Quoted 124 lines
Quoted 123 lines

Regards,
Anup

Quoted 117 lines
Quoted 116 lines

Regards,
Anup

Quoted 110 lines
---
 lib/utils/timer/aclint_mtimer.c | 51

++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 11 deletions(-)

(quoted diff) +0 -0

b/lib/utils/timer/aclint_mtimer.c index a957b1c..2c90e22 100644 --- a/lib/utils/timer/aclint_mtimer.c +++ b/lib/utils/timer/aclint_mtimer.c

(quoted diff) +24 -1
@@ -175,6 +175,9 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, {
        u32 i;
        int rc;
+       struct aclint_mtimer_data *last_mt = NULL;
+       bool skip_time_addr = false;
+       bool skip_timecmp_addr = false;
 
        /* Sanity checks */
        if (!mt || !mt->mtime_size ||
@@ -206,27 +209,53 @@ int aclint_mtimer_cold_init(struct
 aclint_mtimer_data *mt, for (i = 0; i < mt->hart_count; i++)
                mtimer_hartid2data[mt->first_hartid + i] =
 mt;
 
+       /* Check if timer or timercmp addr already exists
+        * otherwise aclint_mtimer_add_regions will fail
+        * becouse of regions conflict.
+        */
+       for (i = 0; i < mt->first_hartid; i++) {
+               if (last_mt && last_mt ==
 mtimer_hartid2data[i])
+                       continue;
+
+               if (mtimer_hartid2data[i]->mtime_addr ==
 mt->mtime_addr)
+                       skip_time_addr = true;
+
+               if (mtimer_hartid2data[i]->mtimecmp_addr ==
 mt->mtimecmp_addr)
+                       skip_timecmp_addr = true;
+
+               if (skip_time_addr && skip_timecmp_addr)
+                       break;
+
+               last_mt = mtimer_hartid2data[i];
+       }
+
        /* Add MTIMER regions to the root domain */
-       if (mt->mtime_addr == (mt->mtimecmp_addr +
 mt->mtimecmp_size)) {
+       if ((mt->mtime_addr == (mt->mtimecmp_addr +

mt->mtimecmp_size)) &&
+ !skip_timecmp_addr) {
rc =
aclint_mtimer_add_regions(mt->mtimecmp_addr, mt->mtime_size
+ mt->mtimecmp_size); if (rc)

                        return rc;

- } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size)) {
+ } else if (mt->mtimecmp_addr == (mt->mtime_addr +
mt->mtime_size) &&
+ !skip_time_addr) {
rc =
aclint_mtimer_add_regions(mt->mtime_addr, mt->mtime_size + mt->mtimecmp_size); if (rc)

                        return rc;
        } else {

- rc =
aclint_mtimer_add_regions(mt->mtime_addr,
-
mt->mtime_size);

(quoted diff) +0 -4
-               if (rc)
-                       return rc;
-
-               rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
-
mt->mtimecmp_size);

(quoted diff) +2 -2
-               if (rc)
-                       return rc;
+               if (!skip_time_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtime_addr,
+
mt->mtime_size);

(quoted diff) +6 -0
+                       if (rc)
+                               return rc;
+               }
+
+               if (!skip_timecmp_addr) {
+                       rc =

aclint_mtimer_add_regions(mt->mtimecmp_addr,
+
mt->mtimecmp_size);

(quoted diff) +3 -1
+                       if (rc)
+                               return rc;
+               }
        }
 
        mtimer.timer_freq = mt->mtime_freq;
--

2.31.1

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi

--
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi