Skip to contentSkip to conversation

lib: utils: Make sure that hartid and the scratch are aligned

PATCH Superseded Raj Vishwanathan 2 participants 9 messages Feb 13, 2025 – Feb 25, 2025 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v2 1/1 lib: utils: Make sure that hartid and the scratch are aligned

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hard-ids' generate hart-count.

---

hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/utils/fdt/fdt_helper.c +1 -2
index cb350e5..bb3fddd 100644
@@ -1046,8 +1046,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 
 	if ((last_hartid >= first_hartid) && first_hartid != -1U) {
 		*out_first_hartid = first_hartid;
-		count = last_hartid - first_hartid + 1;
-		*out_hart_count = (hart_count < count) ? hart_count : count;
+		*out_hart_count  = last_hartid - first_hartid + 1;
 	}
 
 	return 0;
Signature · 1 line
2.43.0
#2 lore raw

PATCH v2 1/1 lib: utils: Make sure that hartid and the scratch are aligned

Hi Raj,

Quoted 3 lines · Raj Vishwanathan

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hard-ids' generate hart-count.

This doesn't help, because the expression (hartid - first_hartid) is still used to calculate MMIO addresses. See for example mtimer_event_start() and mswi_ipi_send(). If there is a gap in the hartids mapped to the ACLINT MTIMER/MSWI indexes, these functions will compute the wrong MMIO address and fail to set the timer or send an IPI.

Either each device instance needs an array of [ACLINT index] => [hart index] mappings, or the ACLINT index needs to be recorded separately in a new per-hart scratch variable.

Regards,
Samuel

Quoted 25 lines

---

hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/utils/fdt/fdt_helper.c +1 -2
index cb350e5..bb3fddd 100644
@@ -1046,8 +1046,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 
 	if ((last_hartid >= first_hartid) && first_hartid != -1U) {
 		*out_first_hartid = first_hartid;
-		count = last_hartid - first_hartid + 1;
-		*out_hart_count = (hart_count < count) ? hart_count : count;
+		*out_hart_count  = last_hartid - first_hartid + 1;
 	}
 
 	return 0;
#3 lore raw

PATCH v2 1/1 lib: utils: Make sure that hartid and the scratch are aligned

Samuel

Thanks for the quick review. I took a quick look and in our case, where the hart-id's are not sequential, I see that there will be gaps in the MMIO space but nothing should break

I will do a deeper study and respond specifically to your questions.

Raj

Quoted 47 lines · On Thu, Feb 13, 2025 at 2:58?PM Samuel Holland <[email protected]>…

Hi Raj,

Quoted 3 lines · Raj Vishwanathan

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hard-ids' generate hart-count.

This doesn't help, because the expression (hartid - first_hartid) is still used to calculate MMIO addresses. See for example mtimer_event_start() and mswi_ipi_send(). If there is a gap in the hartids mapped to the ACLINT MTIMER/MSWI indexes, these functions will compute the wrong MMIO address and fail to set the timer or send an IPI.

Either each device instance needs an array of [ACLINT index] => [hart index] mappings, or the ACLINT index needs to be recorded separately in a new per-hart scratch variable.

Regards,
Samuel

Quoted 25 lines

---

hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/utils/fdt/fdt_helper.c +1 -2
index cb350e5..bb3fddd 100644
@@ -1046,8 +1046,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 
      if ((last_hartid >= first_hartid) && first_hartid != -1U) {
              *out_first_hartid = first_hartid;
-             count = last_hartid - first_hartid + 1;
-             *out_hart_count = (hart_count < count) ? hart_count : count;
+             *out_hart_count  = last_hartid - first_hartid + 1;
      }
 
      return 0;
#4 lore raw

PATCH v2 1/1 lib: utils: Make sure that hartid and the scratch are aligned

Hi Samuel

I reviewed the two files for mstimer and mswi_ipi and they seem to be correct. We use the same "hartid - first_hartid" to access MMIO addresses. So, the current mtimer_event_start() and mswi_ipi_send() implementations work as is.

Quoted 3 lines
Quoted 3 lines

Either each device instance needs an array of [ACLINT index] => [hart index] mappings, or the ACLINT index needs to be recorded separately in a new per-hart scratch variable.

ACLINT index is actually "hartid - first_hartid"

Let me know if you have any further comments.

Raj

Quoted 60 lines · On Thu, Feb 13, 2025 at 10:01?PM Raj Vishwanathan <[email protected]>…

Samuel

Thanks for the quick review. I took a quick look and in our case, where the hart-id's are not sequential, I see that there will be gaps in the MMIO space but nothing should break

I will do a deeper study and respond specifically to your questions.

Raj

Quoted 47 lines · On Thu, Feb 13, 2025 at 2:58?PM Samuel Holland <[email protected]>…

Hi Raj,

Quoted 3 lines · Raj Vishwanathan

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hard-ids' generate hart-count.

This doesn't help, because the expression (hartid - first_hartid) is still used to calculate MMIO addresses. See for example mtimer_event_start() and mswi_ipi_send(). If there is a gap in the hartids mapped to the ACLINT MTIMER/MSWI indexes, these functions will compute the wrong MMIO address and fail to set the timer or send an IPI.

Either each device instance needs an array of [ACLINT index] => [hart index] mappings, or the ACLINT index needs to be recorded separately in a new per-hart scratch variable.

Regards,
Samuel

Quoted 25 lines

---

hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/utils/fdt/fdt_helper.c +1 -2
index cb350e5..bb3fddd 100644
@@ -1046,8 +1046,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 
      if ((last_hartid >= first_hartid) && first_hartid != -1U) {
              *out_first_hartid = first_hartid;
-             count = last_hartid - first_hartid + 1;
-             *out_hart_count = (hart_count < count) ? hart_count : count;
+             *out_hart_count  = last_hartid - first_hartid + 1;
      }
 
      return 0;
#5 lore raw

PATCH v2 1/1 lib: utils: Make sure that hartid and the scratch are aligned

Hi Raj,

Quoted 4 lines · Raj Vishwanathan

I reviewed the two files for mstimer and mswi_ipi and they seem to be correct. We use the same "hartid - first_hartid" to access MMIO addresses. So, the current mtimer_event_start() and mswi_ipi_send() implementations work as is.

OK, I looked at the MIPS P8700 reference manual and see what you mean: "The MSIP register for hart mhartid is accessed at GCR_BASE + 0x50000 + 4 * mhartid[11:0]." So there is no gap in the mapping, instead there is a gap in the register space.

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

Either each device instance needs an array of [ACLINT index] => [hart index] mappings, or the ACLINT index needs to be recorded separately in a new per-hart scratch variable.

Quoted 28 lines

ACLINT index is actually "hartid - first_hartid"

Let me know if you have any further comments.

Raj

Quoted 20 lines · On Thu, Feb 13, 2025 at 10:01?PM Raj Vishwanathan <[email protected]>…

Samuel

Thanks for the quick review. I took a quick look and in our case, where the hart-id's are not sequential, I see that there will be gaps in the MMIO space but nothing should break

I will do a deeper study and respond specifically to your questions.

Raj

Quoted 7 lines · On Thu, Feb 13, 2025 at 2:58?PM Samuel Holland <[email protected]>…

Hi Raj,

Quoted 3 lines · Raj Vishwanathan

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hard-ids' generate hart-count.

s/hard/hart/

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines

This doesn't help, because the expression (hartid - first_hartid) is still used to calculate MMIO addresses. See for example mtimer_event_start() and mswi_ipi_send(). If there is a gap in the hartids mapped to the ACLINT MTIMER/MSWI indexes, these functions will compute the wrong MMIO address and fail to set the timer or send an IPI.

Either each device instance needs an array of [ACLINT index] => [hart index] mappings, or the ACLINT index needs to be recorded separately in a new per-hart scratch variable.

Regards,
Samuel

Quoted 17 lines

---

hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..bb3fddd 100644
@@ -1046,8 +1046,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

lib/utils/fdt/fdt_helper.c int fdt_parse_aclint_node(const void *fdt, int nodeoffset, · line 1049

Quoted 5 lines
lib/utils/fdt/fdt_helper.c +1 -2
      if ((last_hartid >= first_hartid) && first_hartid != -1U) {
              *out_first_hartid = first_hartid;
-             count = last_hartid - first_hartid + 1;
-             *out_hart_count = (hart_count < count) ? hart_count : count;
+             *out_hart_count  = last_hartid - first_hartid + 1;

Now that I understand the context, this looks like the right change to make.

Please remove the hart_count variable entirely, since it is now unused. Also please fix the spacing in this line. With those changes:

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
lib/utils/fdt/fdt_helper.c +0 -0
      }
 
      return 0;
#6 lore raw

PATCH v2 1/1 lib: utils: Make sure that hartid and the scratch are aligned

Samuel

I appreciate your response. I will make the changes and send out the changes soon.

Many thanks

Raj

Quoted 98 lines · On Mon, Feb 24, 2025 at 6:00?PM Samuel Holland <[email protected]>…

Hi Raj,

Quoted 4 lines · Raj Vishwanathan

I reviewed the two files for mstimer and mswi_ipi and they seem to be correct. We use the same "hartid - first_hartid" to access MMIO addresses. So, the current mtimer_event_start() and mswi_ipi_send() implementations work as is.

OK, I looked at the MIPS P8700 reference manual and see what you mean: "The MSIP register for hart mhartid is accessed at GCR_BASE + 0x50000 + 4 * mhartid[11:0]." So there is no gap in the mapping, instead there is a gap in the register space.

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

Either each device instance needs an array of [ACLINT index] => [hart index] mappings, or the ACLINT index needs to be recorded separately in a new per-hart scratch variable.

Quoted 28 lines

ACLINT index is actually "hartid - first_hartid"

Let me know if you have any further comments.

Raj

Quoted 20 lines · On Thu, Feb 13, 2025 at 10:01?PM Raj Vishwanathan <[email protected]>…

Samuel

Thanks for the quick review. I took a quick look and in our case, where the hart-id's are not sequential, I see that there will be gaps in the MMIO space but nothing should break

I will do a deeper study and respond specifically to your questions.

Raj

Quoted 7 lines · On Thu, Feb 13, 2025 at 2:58?PM Samuel Holland <[email protected]>…

Hi Raj,

Quoted 3 lines · Raj Vishwanathan

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hard-ids' generate hart-count.

s/hard/hart/

Quoted 31 lines
Quoted 31 lines
Quoted 31 lines

This doesn't help, because the expression (hartid - first_hartid) is still used to calculate MMIO addresses. See for example mtimer_event_start() and mswi_ipi_send(). If there is a gap in the hartids mapped to the ACLINT MTIMER/MSWI indexes, these functions will compute the wrong MMIO address and fail to set the timer or send an IPI.

Either each device instance needs an array of [ACLINT index] => [hart index] mappings, or the ACLINT index needs to be recorded separately in a new per-hart scratch variable.

Regards,
Samuel

Quoted 17 lines

---

hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..bb3fddd 100644
@@ -1046,8 +1046,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

lib/utils/fdt/fdt_helper.c int fdt_parse_aclint_node(const void *fdt, int nodeoffset, · line 1049

Quoted 5 lines
lib/utils/fdt/fdt_helper.c +1 -2
      if ((last_hartid >= first_hartid) && first_hartid != -1U) {
              *out_first_hartid = first_hartid;
-             count = last_hartid - first_hartid + 1;
-             *out_hart_count = (hart_count < count) ? hart_count : count;
+             *out_hart_count  = last_hartid - first_hartid + 1;

Now that I understand the context, this looks like the right change to make.

Please remove the hart_count variable entirely, since it is now unused. Also please fix the spacing in this line. With those changes:

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
lib/utils/fdt/fdt_helper.c +0 -0
      }
 
      return 0;
#7 lore raw

PATCH v3 1/1 lib: utils: Make sure that hartid and the scratch are aligned

In reply to Raj Vishwanathan

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hart-ids' generate hart-count.

---
hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_helper.c +3 -6
index cb350e5..ccf2d5f 100644
@@ -986,7 +986,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 {
 	const fdt32_t *val;
 	int i, rc, count, cpu_offset, cpu_intc_offset;
-	u32 phandle, hwirq, hartid, first_hartid, last_hartid, hart_count;
+	u32 phandle, hwirq, hartid, first_hartid, last_hartid;
 	u32 match_hwirq = (for_timer) ? IRQ_M_TIMER : IRQ_M_SOFT;
 
 	if (nodeoffset < 0 || !fdt ||
@@ -1015,7 +1015,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 	count = count / sizeof(fdt32_t);
 
 	first_hartid = -1U;
-	hart_count = last_hartid = 0;
+	last_hartid = 0;
 	for (i = 0; i < (count / 2); i++) {
 		phandle = fdt32_to_cpu(val[2 * i]);
 		hwirq = fdt32_to_cpu(val[(2 * i) + 1]);
@@ -1040,16 +1040,13 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 				first_hartid = hartid;
 			if (hartid > last_hartid)
 				last_hartid = hartid;
-			hart_count++;
 		}
 	}
 
 	if ((last_hartid >= first_hartid) && first_hartid != -1U) {
 		*out_first_hartid = first_hartid;
-		count = last_hartid - first_hartid + 1;
-		*out_hart_count = (hart_count < count) ? hart_count : count;
+		*out_hart_count  = last_hartid - first_hartid + 1;
 	}
-
 	return 0;
 }
 
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 1/1 lib: utils: Make sure that hartid and the scratch are aligned

Quoted 39 lines · Raj Vishwanathan

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hart-ids' generate hart-count.

---
hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_helper.c +2 -2
index cb350e5..ccf2d5f 100644
@@ -986,7 +986,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 {
 	const fdt32_t *val;
 	int i, rc, count, cpu_offset, cpu_intc_offset;
-	u32 phandle, hwirq, hartid, first_hartid, last_hartid, hart_count;
+	u32 phandle, hwirq, hartid, first_hartid, last_hartid;
 	u32 match_hwirq = (for_timer) ? IRQ_M_TIMER : IRQ_M_SOFT;
 
 	if (nodeoffset < 0 || !fdt ||
@@ -1015,7 +1015,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 	count = count / sizeof(fdt32_t);
 
 	first_hartid = -1U;
-	hart_count = last_hartid = 0;
+	last_hartid = 0;
 	for (i = 0; i < (count / 2); i++) {
 		phandle = fdt32_to_cpu(val[2 * i]);
 		hwirq = fdt32_to_cpu(val[(2 * i) + 1]);
@@ -1040,16 +1040,13 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 				first_hartid = hartid;
 			if (hartid > last_hartid)

lib/utils/fdt/fdt_helper.c int fdt_parse_aclint_node(const void *fdt, int nodeoffset, · line 1048

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +1 -3
 				last_hartid = hartid;
-			hart_count++;
 		}
 	}
 
 	if ((last_hartid >= first_hartid) && first_hartid != -1U) {
 		*out_first_hartid = first_hartid;
-		count = last_hartid - first_hartid + 1;
-		*out_hart_count = (hart_count < count) ? hart_count : count;
+		*out_hart_count  = last_hartid - first_hartid + 1;

You still have an extra space before the assignment here.

lib/utils/fdt/fdt_helper.c int fdt_parse_aclint_node(const void *fdt, int nodeoffset, · line 1052

Quoted 2 lines
lib/utils/fdt/fdt_helper.c +0 -1
 	}
-

Unrelated change.

With those style issues fixed:

Quoted 3 lines
lib/utils/fdt/fdt_helper.c +0 -0
 	return 0;
 }
 
#9 lore raw

PATCH v3 1/1 lib: utils: Make sure that hartid and the scratch are aligned

In reply to Raj Vishwanathan

Harts associated with an ACLINT_MSWI need not have sequential hartids. It is insufficient to use first_hartid and hart_count. To account for non-sequential hart ids, include the empty hart-ids' generate hart-count.

---
hart_count = last_hart_id - first_hart_id +1
count = number of harts
For sequential hart_id's, count = hart_count
For non-sequential hart_id's, count < hart_count

---
 lib/utils/fdt/fdt_helper.c | 9 +++------
 1 file changed, 3 insertions(+), 6 deletions(-)
lib/utils/fdt/fdt_helper.c +3 -6
index cb350e5..80fe4e8 100644
@@ -986,7 +986,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 {
 	const fdt32_t *val;
 	int i, rc, count, cpu_offset, cpu_intc_offset;
-	u32 phandle, hwirq, hartid, first_hartid, last_hartid, hart_count;
+	u32 phandle, hwirq, hartid, first_hartid, last_hartid;
 	u32 match_hwirq = (for_timer) ? IRQ_M_TIMER : IRQ_M_SOFT;
 
 	if (nodeoffset < 0 || !fdt ||
@@ -1015,7 +1015,7 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 	count = count / sizeof(fdt32_t);
 
 	first_hartid = -1U;
-	hart_count = last_hartid = 0;
+	last_hartid = 0;
 	for (i = 0; i < (count / 2); i++) {
 		phandle = fdt32_to_cpu(val[2 * i]);
 		hwirq = fdt32_to_cpu(val[(2 * i) + 1]);
@@ -1040,16 +1040,13 @@ int fdt_parse_aclint_node(const void *fdt, int nodeoffset,
 				first_hartid = hartid;
 			if (hartid > last_hartid)
 				last_hartid = hartid;
-			hart_count++;
 		}
 	}
 
 	if ((last_hartid >= first_hartid) && first_hartid != -1U) {
 		*out_first_hartid = first_hartid;
-		count = last_hartid - first_hartid + 1;
-		*out_hart_count = (hart_count < count) ? hart_count : count;
+		*out_hart_count = last_hartid - first_hartid + 1;
 	}
-
 	return 0;
 }
 
Signature · 1 line
2.43.0