Skip to contentSkip to conversation

Set the scratch allocation to alignment to cacheline size.

PATCH Superseded Raj Vishwanathan 5 participants 14 messages Mar 18, 2025 – Apr 24, 2025 2 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20250127202017.1043240-1-Raj.Vishwanathan

#1 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  1 +
 lib/sbi/sbi_scratch.c              | 27 +++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  7 +++++++
 5 files changed, 59 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {
 	 * 2. HART id < SBI_HARTMASK_MAX_BITS
 	 */
 	const u32 *hart_index2id;
+	/** Allocation alignment for Scratch */
+	u32 cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +1 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);
 
 int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size);
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/sbi/sbi_scratch.c +25 -2
index ccbbc68..fdb9e20 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 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 };
@@ -21,6 +23,20 @@ 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;
 
+static u32 sbi_get_scratch_alloc_align(void)
+{
+	const struct sbi_platform *plat;
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */
+	plat = sbi_platform_thishart_ptr();
+	if (!plat)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size ? plat->cbom_block_size : \
+                                  DEFAULT_SCRATCH_ALLOC_ALIGN;
+
+}
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
 	u32 i;
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	void *ptr;
 	unsigned long ret = 0;
 	struct sbi_scratch *rscratch;
+	u32 scratch_alloc_align = 0;
 
 	/*
 	 * We have a simple brain-dead allocator which never expects
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	if (!size)
 		return 0;
 
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+	 * We let the allocation align to cacheline bytes to avoid livelock on
+	 * certain platforms due to atomic variables from the same cache line.
+	 */
+    size += scratch_alloc_align - 1;
+    size &= ~((unsigned long)scratch_alloc_align - 1);
 
 	spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 
 	return 0;
 }
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+    int len;
+    const void *prop;
+    const fdt32_t *val;
+
+    if (!fdt || cpu_offset < 0)
+        return SBI_EINVAL;
+
+    prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+    if (!prop || !len)
+        return SBI_EINVAL;
+    if (strncmp (prop, "cpu", strlen ("cpu")))
+        return SBI_EINVAL;
+
+    val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+    if (!val || len < sizeof(fdt32_t))
+        return SBI_EINVAL;
+
+    if (cbom_block_size)
+        *cbom_block_size = fdt32_to_cpu(*val);
+    return 0;
+
+}
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
platform/generic/platform.c +7 -0
index c03ed88..0ff8d46 100644
@@ -174,6 +174,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 	const void *fdt = (void *)arg1;
 	u32 hartid, hart_count = 0;
 	int rc, root_offset, cpus_offset, cpu_offset, len;
+	u32 cbom_block_size = 0;
+	u32 tmp=0;
 
 	root_offset = fdt_path_offset(fdt, "/");
 	if (root_offset < 0)
@@ -207,11 +209,16 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 			continue;
 
 		generic_hart_index2id[hart_count++] = hartid;
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+		if (rc)
+			continue;
+		cbom_block_size = MAX(tmp,cbom_block_size);
 	}
 
 	platform.hart_count = hart_count;
 	platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
 	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+	platform.cbom_block_size = cbom_block_size;
 
 	fw_platform_coldboot_harts_init(fdt);
 
Signature · 1 line
2.43.0
#2 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

? 2025-03-18?? 11:51 -0700?Raj Vishwanathan???

Quoted 6 lines

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

LGTM

Quoted 167 lines

---
Changes in V3:
??? Remove platform specific references to 64 Bytes. Changes in V2:
??? Added a new configuration to get the alignment size. Change in V1:
??? Original Patch
---

?include/sbi/sbi_platform.h???????? |? 2 ++
?include/sbi_utils/fdt/fdt_helper.h |? 1 +
?lib/sbi/sbi_scratch.c????????????? | 27 +++++++++++++++++++++++++--
?lib/utils/fdt/fdt_helper.c???????? | 24 ++++++++++++++++++++++++
?platform/generic/platform.c??????? |? 7 +++++++

?5 files changed, 59 insertions(+), 2 deletions(-)

include/sbi/sbi_platform.h +0 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {

? * 2. HART id < SBI_HARTMASK_MAX_BITS
? */
? const u32 *hart_index2id;

include/sbi/sbi_platform.h +2 -0
+	/** Allocation alignment for Scratch */
+	u32 cbom_block_size;

?};
?
?/**

include/sbi_utils/fdt/fdt_helper.h +0 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);

?
?int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
?
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size); ?int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
?
?int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);

lib/sbi/sbi_scratch.c +0 -0
index ccbbc68..fdb9e20 100644
@@ -14,6 +14,8 @@

?#include <sbi/sbi_scratch.h>
?#include <sbi/sbi_string.h>
?

lib/sbi/sbi_scratch.c +2 -0
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+

?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 };

lib/sbi/sbi_scratch.c +0 -0
@@ -21,6 +23,20 @@ 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 +14 -0
+static u32 sbi_get_scratch_alloc_align(void)
+{
+	const struct sbi_platform *plat;
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */
+	plat = sbi_platform_thishart_ptr();
+	if (!plat)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size ? plat->cbom_block_size : \
+????????????????????????????????? DEFAULT_SCRATCH_ALLOC_ALIGN;
+
+}

?u32 sbi_hartid_to_hartindex(u32 hartid)
?{
? u32 i;

lib/sbi/sbi_scratch.c +1 -0
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 ?	void *ptr;
 ?	unsigned long ret = 0;
 ?	struct sbi_scratch *rscratch;
+	u32 scratch_alloc_align = 0;

?
? /*
? * We have a simple brain-dead allocator which never expects

lib/sbi/sbi_scratch.c +0 -0
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 ?	if (!size)
 ?		return 0;

?

lib/sbi/sbi_scratch.c +8 -2
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+	 * We let the allocation align to cacheline bytes to avoid livelock on
+	 * certain platforms due to atomic variables from the same cache line.
+	 */
+??? size += scratch_alloc_align - 1;
+??? size &= ~((unsigned long)scratch_alloc_align - 1);

?
? spin_lock(&extra_lock);
?

lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)

?
? return 0;
?}

lib/utils/fdt/fdt_helper.c +24 -0
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+??? int len;
+??? const void *prop;
+??? const fdt32_t *val;
+
+??? if (!fdt || cpu_offset < 0)
+??????? return SBI_EINVAL;
+
+??? prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+??? if (!prop || !len)
+??????? return SBI_EINVAL;
+??? if (strncmp (prop, "cpu", strlen ("cpu")))
+??????? return SBI_EINVAL;
+
+??? val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+??? if (!val || len < sizeof(fdt32_t))
+??????? return SBI_EINVAL;
+
+??? if (cbom_block_size)
+??????? *cbom_block_size = fdt32_to_cpu(*val);
+??? return 0;
+
+}

?
?int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid) ?{

platform/generic/platform.c +2 -0
index c03ed88..0ff8d46 100644
@@ -174,6 +174,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?	const void *fdt = (void *)arg1;
 ?	u32 hartid, hart_count = 0;
 ?	int rc, root_offset, cpus_offset, cpu_offset, len;
+	u32 cbom_block_size = 0;
+	u32 tmp=0;

?
? root_offset = fdt_path_offset(fdt, "/");
? if (root_offset < 0)

platform/generic/platform.c +0 -0
@@ -207,11 +209,16 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 ?			continue;

?
? generic_hart_index2id[hart_count++] = hartid;

platform/generic/platform.c +4 -0
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+		if (rc)
+			continue;
+		cbom_block_size = MAX(tmp,cbom_block_size);

? }
?
? platform.hart_count = hart_count;
? platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
? platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);

platform/generic/platform.c +1 -0
+	platform.cbom_block_size = cbom_block_size;

?
? fw_platform_coldboot_harts_init(fdt);
?

Signature · 1 line
2.43.0
#3 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

In reply to Raj Vishwanathan
Quoted 121 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  1 +
 lib/sbi/sbi_scratch.c              | 27 +++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  7 +++++++
 5 files changed, 59 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {
 	 * 2. HART id < SBI_HARTMASK_MAX_BITS
 	 */
 	const u32 *hart_index2id;
+	/** Allocation alignment for Scratch */
+	u32 cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +1 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);
 
 int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size);
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/sbi/sbi_scratch.c +25 -2
index ccbbc68..fdb9e20 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 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 };
@@ -21,6 +23,20 @@ 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;
 
+static u32 sbi_get_scratch_alloc_align(void)
+{
+	const struct sbi_platform *plat;
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */
+	plat = sbi_platform_thishart_ptr();
+	if (!plat)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size ? plat->cbom_block_size : \
+                                  DEFAULT_SCRATCH_ALLOC_ALIGN;
+
+}
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
 	u32 i;
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	void *ptr;
 	unsigned long ret = 0;
 	struct sbi_scratch *rscratch;
+	u32 scratch_alloc_align = 0;
 
 	/*
 	 * We have a simple brain-dead allocator which never expects
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	if (!size)
 		return 0;
 
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+	 * We let the allocation align to cacheline bytes to avoid livelock on
+	 * certain platforms due to atomic variables from the same cache line.
+	 */
+    size += scratch_alloc_align - 1;
+    size &= ~((unsigned long)scratch_alloc_align - 1);
 
 	spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +8 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 
 	return 0;
 }
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+    int len;
+    const void *prop;
+    const fdt32_t *val;
+
+    if (!fdt || cpu_offset < 0)
+        return SBI_EINVAL;

lib/utils/fdt/fdt_helper.c int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid) · line 307

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +10 -0
+
+    prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+    if (!prop || !len)
+        return SBI_EINVAL;
+    if (strncmp (prop, "cpu", strlen ("cpu")))
+        return SBI_EINVAL;
+
+    val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+    if (!val || len < sizeof(fdt32_t))
+        return SBI_EINVAL;

There's another way to get the CBO block sizes, which we haven't started doing in Linux yet, but at some point maybe we should. If we don't have the riscv,cbom-block-size nodes, then we can still check for the existence of the Zic64b extension in the isa-string/isa-extensions. The existence of that extension states that all cache blocks (cbom/cboz) are 64 bytes in size.

Thanks,
drew

Quoted 47 lines
lib/utils/fdt/fdt_helper.c +6 -0
+
+    if (cbom_block_size)
+        *cbom_block_size = fdt32_to_cpu(*val);
+    return 0;
+
+}
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
platform/generic/platform.c +7 -0
index c03ed88..0ff8d46 100644
@@ -174,6 +174,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 	const void *fdt = (void *)arg1;
 	u32 hartid, hart_count = 0;
 	int rc, root_offset, cpus_offset, cpu_offset, len;
+	u32 cbom_block_size = 0;
+	u32 tmp=0;
 
 	root_offset = fdt_path_offset(fdt, "/");
 	if (root_offset < 0)
@@ -207,11 +209,16 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 			continue;
 
 		generic_hart_index2id[hart_count++] = hartid;
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+		if (rc)
+			continue;
+		cbom_block_size = MAX(tmp,cbom_block_size);
 	}
 
 	platform.hart_count = hart_count;
 	platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
 	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+	platform.cbom_block_size = cbom_block_size;
 
 	fw_platform_coldboot_harts_init(fdt);
 
Signature · 7 lines
2.43.0


-- 
opensbi mailing list
opensbi at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/opensbi
#4 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

Andrew,

Thanks for the tip. It may be the way to in the future. Just out of curiosity, does the absence of Zic64b extension mean that cache blocks are not 64 bytes in size?

We are testing on a system that does not have this but a 64byte is still the cacheline size

Thanks

Raj

Quoted 191 lines · On Wed, Mar 19, 2025 at 4:59?AM Andrew Jones <[email protected]> wrote:
Quoted 121 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  1 +
 lib/sbi/sbi_scratch.c              | 27 +++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  7 +++++++
 5 files changed, 59 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {
       * 2. HART id < SBI_HARTMASK_MAX_BITS
       */
      const u32 *hart_index2id;
+     /** Allocation alignment for Scratch */
+     u32 cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +1 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);
 
 int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size);
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/sbi/sbi_scratch.c +25 -2
index ccbbc68..fdb9e20 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 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 };
@@ -21,6 +23,20 @@ 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;
 
+static u32 sbi_get_scratch_alloc_align(void)
+{
+     const struct sbi_platform *plat;
+     /*
+      * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+      * or riscv,cbom_block_size
+      */
+     plat = sbi_platform_thishart_ptr();
+     if (!plat)
+             return DEFAULT_SCRATCH_ALLOC_ALIGN;
+     return plat->cbom_block_size ? plat->cbom_block_size : \
+                                  DEFAULT_SCRATCH_ALLOC_ALIGN;
+
+}
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
      u32 i;
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      void *ptr;
      unsigned long ret = 0;
      struct sbi_scratch *rscratch;
+     u32 scratch_alloc_align = 0;
 
      /*
       * We have a simple brain-dead allocator which never expects
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      if (!size)
              return 0;
 
-     size += __SIZEOF_POINTER__ - 1;
-     size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+     scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+     /*
+      * We let the allocation align to cacheline bytes to avoid livelock on
+      * certain platforms due to atomic variables from the same cache line.
+      */
+    size += scratch_alloc_align - 1;
+    size &= ~((unsigned long)scratch_alloc_align - 1);
 
      spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +8 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 
      return 0;
 }
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+    int len;
+    const void *prop;
+    const fdt32_t *val;
+
+    if (!fdt || cpu_offset < 0)
+        return SBI_EINVAL;

lib/utils/fdt/fdt_helper.c int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid) · line 307

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +10 -0
+
+    prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+    if (!prop || !len)
+        return SBI_EINVAL;
+    if (strncmp (prop, "cpu", strlen ("cpu")))
+        return SBI_EINVAL;
+
+    val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+    if (!val || len < sizeof(fdt32_t))
+        return SBI_EINVAL;

There's another way to get the CBO block sizes, which we haven't started doing in Linux yet, but at some point maybe we should. If we don't have the riscv,cbom-block-size nodes, then we can still check for the existence of the Zic64b extension in the isa-string/isa-extensions. The existence of that extension states that all cache blocks (cbom/cboz) are 64 bytes in size.

Thanks,
drew

Quoted 47 lines
lib/utils/fdt/fdt_helper.c +6 -0
+
+    if (cbom_block_size)
+        *cbom_block_size = fdt32_to_cpu(*val);
+    return 0;
+
+}
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
platform/generic/platform.c +7 -1
index c03ed88..0ff8d46 100644
@@ -174,6 +174,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
      const void *fdt = (void *)arg1;
      u32 hartid, hart_count = 0;
      int rc, root_offset, cpus_offset, cpu_offset, len;
+     u32 cbom_block_size = 0;
+     u32 tmp=0;
 
      root_offset = fdt_path_offset(fdt, "/");
      if (root_offset < 0)
@@ -207,11 +209,16 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                      continue;
 
              generic_hart_index2id[hart_count++] = hartid;
+             rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+             if (rc)
+                     continue;
+             cbom_block_size = MAX(tmp,cbom_block_size);
      }
 
      platform.hart_count = hart_count;
      platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
      platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+     platform.cbom_block_size = cbom_block_size;
 
      fw_platform_coldboot_harts_init(fdt);
 
--

2.43.0

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

#5 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

Quoted 5 lines · Raj Vishwanathan

Andrew,

Thanks for the tip. It may be the way to in the future. Just out of curiosity, does the absence of Zic64b extension mean that cache blocks are not 64 bytes in size?

Nope, it just means the cache block sizes will need to be determined in some other way, such as with the riscv,cbom/cbo{m,p,z}-block-size nodes.

My comment on the patch is to not give up when riscv,cbom-block-size is missing, but rather to also search the extension list for zic64b. Or vice versa, search for zic64b, if it's not present, then search for the block-size nodes.

Thanks,
drew

Quoted 200 lines

We are testing on a system that does not have this but a 64byte is still the cacheline size

Thanks

Raj

Quoted 191 lines · On Wed, Mar 19, 2025 at 4:59?AM Andrew Jones <[email protected]> wrote:
Quoted 121 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  1 +
 lib/sbi/sbi_scratch.c              | 27 +++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  7 +++++++
 5 files changed, 59 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {
       * 2. HART id < SBI_HARTMASK_MAX_BITS
       */
      const u32 *hart_index2id;
+     /** Allocation alignment for Scratch */
+     u32 cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +1 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);
 
 int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size);
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/sbi/sbi_scratch.c +25 -2
index ccbbc68..fdb9e20 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 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 };
@@ -21,6 +23,20 @@ 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;
 
+static u32 sbi_get_scratch_alloc_align(void)
+{
+     const struct sbi_platform *plat;
+     /*
+      * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+      * or riscv,cbom_block_size
+      */
+     plat = sbi_platform_thishart_ptr();
+     if (!plat)
+             return DEFAULT_SCRATCH_ALLOC_ALIGN;
+     return plat->cbom_block_size ? plat->cbom_block_size : \
+                                  DEFAULT_SCRATCH_ALLOC_ALIGN;
+
+}
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
      u32 i;
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      void *ptr;
      unsigned long ret = 0;
      struct sbi_scratch *rscratch;
+     u32 scratch_alloc_align = 0;
 
      /*
       * We have a simple brain-dead allocator which never expects
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      if (!size)
              return 0;
 
-     size += __SIZEOF_POINTER__ - 1;
-     size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+     scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+     /*
+      * We let the allocation align to cacheline bytes to avoid livelock on
+      * certain platforms due to atomic variables from the same cache line.
+      */
+    size += scratch_alloc_align - 1;
+    size &= ~((unsigned long)scratch_alloc_align - 1);
 
      spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +8 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 
      return 0;
 }
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+    int len;
+    const void *prop;
+    const fdt32_t *val;
+
+    if (!fdt || cpu_offset < 0)
+        return SBI_EINVAL;

lib/utils/fdt/fdt_helper.c int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid) · line 307

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +10 -0
+
+    prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+    if (!prop || !len)
+        return SBI_EINVAL;
+    if (strncmp (prop, "cpu", strlen ("cpu")))
+        return SBI_EINVAL;
+
+    val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+    if (!val || len < sizeof(fdt32_t))
+        return SBI_EINVAL;

There's another way to get the CBO block sizes, which we haven't started doing in Linux yet, but at some point maybe we should. If we don't have the riscv,cbom-block-size nodes, then we can still check for the existence of the Zic64b extension in the isa-string/isa-extensions. The existence of that extension states that all cache blocks (cbom/cboz) are 64 bytes in size.

Thanks,
drew

Quoted 47 lines
lib/utils/fdt/fdt_helper.c +6 -0
+
+    if (cbom_block_size)
+        *cbom_block_size = fdt32_to_cpu(*val);
+    return 0;
+
+}
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
platform/generic/platform.c +7 -1
index c03ed88..0ff8d46 100644
@@ -174,6 +174,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
      const void *fdt = (void *)arg1;
      u32 hartid, hart_count = 0;
      int rc, root_offset, cpus_offset, cpu_offset, len;
+     u32 cbom_block_size = 0;
+     u32 tmp=0;
 
      root_offset = fdt_path_offset(fdt, "/");
      if (root_offset < 0)
@@ -207,11 +209,16 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                      continue;
 
              generic_hart_index2id[hart_count++] = hartid;
+             rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+             if (rc)
+                     continue;
+             cbom_block_size = MAX(tmp,cbom_block_size);
      }
 
      platform.hart_count = hart_count;
      platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
      platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+     platform.cbom_block_size = cbom_block_size;
 
      fw_platform_coldboot_harts_init(fdt);
 
--

2.43.0

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

#6 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

Andrew

Thanks for the explanation. I would prefer to do this in two different patches. The first one being the one that I sent out and when it is ready in Linux I can add this to OpenSBI.
The current patch cleanly resolves the livelock issue we have and maintains the default for the scratch allocation. If we add the Zic64b check, this could change the default value for scratch allocation.

Thanks

Raj

Quoted 219 lines · On Fri, Mar 21, 2025 at 12:50?AM Andrew Jones <[email protected]> wrote:
Quoted 5 lines · Raj Vishwanathan

Andrew,

Thanks for the tip. It may be the way to in the future. Just out of curiosity, does the absence of Zic64b extension mean that cache blocks are not 64 bytes in size?

Nope, it just means the cache block sizes will need to be determined in some other way, such as with the riscv,cbom/cbo{m,p,z}-block-size nodes.

My comment on the patch is to not give up when riscv,cbom-block-size is missing, but rather to also search the extension list for zic64b. Or vice versa, search for zic64b, if it's not present, then search for the block-size nodes.

Thanks,
drew

Quoted 200 lines

We are testing on a system that does not have this but a 64byte is still the cacheline size

Thanks

Raj

Quoted 191 lines · On Wed, Mar 19, 2025 at 4:59?AM Andrew Jones <[email protected]> wrote:
Quoted 121 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  1 +
 lib/sbi/sbi_scratch.c              | 27 +++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  7 +++++++
 5 files changed, 59 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {
       * 2. HART id < SBI_HARTMASK_MAX_BITS
       */
      const u32 *hart_index2id;
+     /** Allocation alignment for Scratch */
+     u32 cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +1 -0
index 7329b84..0b82159 100644
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);
 
 int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size);
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/sbi/sbi_scratch.c +25 -2
index ccbbc68..fdb9e20 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 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 };
@@ -21,6 +23,20 @@ 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;
 
+static u32 sbi_get_scratch_alloc_align(void)
+{
+     const struct sbi_platform *plat;
+     /*
+      * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+      * or riscv,cbom_block_size
+      */
+     plat = sbi_platform_thishart_ptr();
+     if (!plat)
+             return DEFAULT_SCRATCH_ALLOC_ALIGN;
+     return plat->cbom_block_size ? plat->cbom_block_size : \
+                                  DEFAULT_SCRATCH_ALLOC_ALIGN;
+
+}
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
      u32 i;
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      void *ptr;
      unsigned long ret = 0;
      struct sbi_scratch *rscratch;
+     u32 scratch_alloc_align = 0;
 
      /*
       * We have a simple brain-dead allocator which never expects
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      if (!size)
              return 0;
 
-     size += __SIZEOF_POINTER__ - 1;
-     size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+     scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+     /*
+      * We let the allocation align to cacheline bytes to avoid livelock on
+      * certain platforms due to atomic variables from the same cache line.
+      */
+    size += scratch_alloc_align - 1;
+    size &= ~((unsigned long)scratch_alloc_align - 1);
 
      spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +8 -0
index cb350e5..bea4fdc 100644
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 
      return 0;
 }
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+    int len;
+    const void *prop;
+    const fdt32_t *val;
+
+    if (!fdt || cpu_offset < 0)
+        return SBI_EINVAL;

lib/utils/fdt/fdt_helper.c int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid) · line 307

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +10 -0
+
+    prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+    if (!prop || !len)
+        return SBI_EINVAL;
+    if (strncmp (prop, "cpu", strlen ("cpu")))
+        return SBI_EINVAL;
+
+    val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+    if (!val || len < sizeof(fdt32_t))
+        return SBI_EINVAL;

There's another way to get the CBO block sizes, which we haven't started doing in Linux yet, but at some point maybe we should. If we don't have the riscv,cbom-block-size nodes, then we can still check for the existence of the Zic64b extension in the isa-string/isa-extensions. The existence of that extension states that all cache blocks (cbom/cboz) are 64 bytes in size.

Thanks,
drew

Quoted 47 lines
lib/utils/fdt/fdt_helper.c +6 -0
+
+    if (cbom_block_size)
+        *cbom_block_size = fdt32_to_cpu(*val);
+    return 0;
+
+}
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
platform/generic/platform.c +7 -1
index c03ed88..0ff8d46 100644
@@ -174,6 +174,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
      const void *fdt = (void *)arg1;
      u32 hartid, hart_count = 0;
      int rc, root_offset, cpus_offset, cpu_offset, len;
+     u32 cbom_block_size = 0;
+     u32 tmp=0;
 
      root_offset = fdt_path_offset(fdt, "/");
      if (root_offset < 0)
@@ -207,11 +209,16 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                      continue;
 
              generic_hart_index2id[hart_count++] = hartid;
+             rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+             if (rc)
+                     continue;
+             cbom_block_size = MAX(tmp,cbom_block_size);
      }
 
      platform.hart_count = hart_count;
      platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
      platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+     platform.cbom_block_size = cbom_block_size;
 
      fw_platform_coldboot_harts_init(fdt);
 
--

2.43.0

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

#7 lore raw

PATCH v4 1/1 Set the scratch allocation to alignment to cacheline size.

In reply to Raj Vishwanathan

Hi Raj,

Quoted 39 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  1 +
 lib/sbi/sbi_scratch.c              | 27 +++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  7 +++++++
 5 files changed, 59 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 6d5fbc7..0cea0fe 100644
@@ -197,6 +197,8 @@ struct sbi_platform {
 	 * 2. HART id < SBI_HARTMASK_MAX_BITS
 	 */
 	const u32 *hart_index2id;
+	/** Allocation alignment for Scratch */
+	u32 cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +0 -0
index 7329b84..0b82159 100644

include/sbi_utils/fdt/fdt_helper.h bool fdt_node_is_enabled(const void *fdt, int nodeoff); · line 59

Quoted 5 lines
include/sbi_utils/fdt/fdt_helper.h +1 -0
@@ -55,6 +55,7 @@ bool fdt_node_is_enabled(const void *fdt, int nodeoff);
 
 int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, u32 *cbom_block_size);
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);

Please match the surrounding style (blank line between declarations).

Quoted 21 lines
include/sbi_utils/fdt/fdt_helper.h +0 -0
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
lib/sbi/sbi_scratch.c +4 -0
index ccbbc68..fdb9e20 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 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 };
@@ -21,6 +23,20 @@ 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;
 
+static u32 sbi_get_scratch_alloc_align(void)
+{

lib/sbi/sbi_scratch.c struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 · line 37

Quoted 10 lines
lib/sbi/sbi_scratch.c +10 -0
+	const struct sbi_platform *plat;
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */
+	plat = sbi_platform_thishart_ptr();
+	if (!plat)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size ? plat->cbom_block_size : \
+                                  DEFAULT_SCRATCH_ALLOC_ALIGN;

You can simplify this:

	if (!plat || !plat->cbom_block_size)
		return DEFAULT_SCRATCH_ALLOC_ALIGN;
	return plat->cbom_block_size;

lib/sbi/sbi_scratch.c struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS + 1] = { 0 · line 40

Quoted 3 lines
lib/sbi/sbi_scratch.c +2 -0
+
+}
 u32 sbi_hartid_to_hartindex(u32 hartid)

Please match the surrounding style (blank line between functions).

lib/sbi/sbi_scratch.c unsigned long sbi_scratch_alloc_offset(unsigned long size) · line 76

Quoted 7 lines
lib/sbi/sbi_scratch.c +1 -0
 {
 	u32 i;
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	void *ptr;
 	unsigned long ret = 0;
 	struct sbi_scratch *rscratch;
+	u32 scratch_alloc_align = 0;

Since the only user of this value needs it to be unsigned long, you can change the type here (and the return type of sbi_get_scratch_alloc_align()) and drop the cast below.

Quoted 7 lines
lib/sbi/sbi_scratch.c +0 -0
 	/*
 	 * We have a simple brain-dead allocator which never expects
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	if (!size)
 		return 0;
 

lib/sbi/sbi_scratch.c unsigned long sbi_scratch_alloc_offset(unsigned long size) · line 97

Quoted 10 lines
lib/sbi/sbi_scratch.c +8 -2
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+	 * We let the allocation align to cacheline bytes to avoid livelock on
+	 * certain platforms due to atomic variables from the same cache line.
+	 */
+    size += scratch_alloc_align - 1;
+    size &= ~((unsigned long)scratch_alloc_align - 1);

Please match the documented coding style (use tabs for indent).

Quoted 8 lines
lib/sbi/sbi_scratch.c +0 -0
 	spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +0 -0
index cb350e5..bea4fdc 100644

lib/utils/fdt/fdt_helper.c int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid) · line 292

Quoted 6 lines
lib/utils/fdt/fdt_helper.c +3 -0
@@ -287,6 +287,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 
 	return 0;
 }
+int fdt_parse_cbom_block_size(const void *fdt,int cpu_offset,u32 *cbom_block_size)
+{
+    int len;

Please match the documented coding style (use tabs for indent).

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +10 -0
+    const void *prop;
+    const fdt32_t *val;
+
+    if (!fdt || cpu_offset < 0)
+        return SBI_EINVAL;
+
+    prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+    if (!prop || !len)
+        return SBI_EINVAL;
+    if (strncmp (prop, "cpu", strlen ("cpu")))

lib/utils/fdt/fdt_helper.c int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid) · line 312

Quoted 10 lines
lib/utils/fdt/fdt_helper.c +10 -0
+        return SBI_EINVAL;
+
+    val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+    if (!val || len < sizeof(fdt32_t))
+        return SBI_EINVAL;
+
+    if (cbom_block_size)
+        *cbom_block_size = fdt32_to_cpu(*val);
+    return 0;
+

Please match the surrounding coding style (no blank line here).

Quoted 9 lines
lib/utils/fdt/fdt_helper.c +1 -0
+}
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
platform/generic/platform.c +0 -0
index c03ed88..0ff8d46 100644

platform/generic/platform.c unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, · line 178

Quoted 5 lines
platform/generic/platform.c +2 -0
@@ -174,6 +174,8 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 	const void *fdt = (void *)arg1;
 	u32 hartid, hart_count = 0;
 	int rc, root_offset, cpus_offset, cpu_offset, len;
+	u32 cbom_block_size = 0;
+	u32 tmp=0;

Please match the surrounding coding style (spaces around assignments).

platform/generic/platform.c unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, · line 211

Quoted 7 lines
platform/generic/platform.c +0 -0
 	root_offset = fdt_path_offset(fdt, "/");
 	if (root_offset < 0)
@@ -207,11 +209,16 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 			continue;
 
 		generic_hart_index2id[hart_count++] = hartid;

Please add a blank line here.

Regards,
Samuel

Quoted 13 lines
platform/generic/platform.c +5 -0
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset,&tmp);
+		if (rc)
+			continue;
+		cbom_block_size = MAX(tmp,cbom_block_size);
 	}
 
 	platform.hart_count = hart_count;
 	platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
 	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+	platform.cbom_block_size = cbom_block_size;
 
 	fw_platform_coldboot_harts_init(fdt);
 

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

#9 lore raw

Hi

Any update on this patch?

Best regards

Raj

Quoted 179 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

---
Changes in V4:
Pickup the cacheline size from the dts file
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/sbi/sbi_scratch.c              | 27 +++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  9 +++++++++
 5 files changed, 62 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +2 -0
index 3c99d43..d69215b 100644
@@ -192,6 +192,8 @@ struct sbi_platform {
         *     hart_index2id[<abc>] = <abc>
         */
        const u32 *hart_index2id;
+       /** Allocation alignment for Scratch */
+       unsigned long cbom_block_size;
 };
 
 /**
include/sbi_utils/fdt/fdt_helper.h +2 -0
index ff83002..92de9d9 100644
@@ -53,6 +53,8 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *cbom_block_size);
+
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
 int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
lib/sbi/sbi_scratch.c +25 -2
index 8c7eeaf..ca88204 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 u32 sbi_scratch_hart_count;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS] = { [0 ... SBI_HARTMASK_MAX_BITS-1] = -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
@@ -21,6 +23,20 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
 static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 
+static unsigned long sbi_get_scratch_alloc_align(void)
+{
+       const struct sbi_platform *plat;
+       /*
+        * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+        * or riscv,cbom_block_size
+        */
+       plat = sbi_platform_thishart_ptr();
+       if (!plat)
+               return DEFAULT_SCRATCH_ALLOC_ALIGN;
+       return plat->cbom_block_size ? plat->cbom_block_size : \
+                                                                       DEFAULT_SCRATCH_ALLOC_ALIGN;
+}
+
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
        sbi_for_each_hartindex(i)
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        void *ptr;
        unsigned long ret = 0;
        struct sbi_scratch *rscratch;
+       unsigned long scratch_alloc_align = 0;
 
        /*
         * We have a simple brain-dead allocator which never expects
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        if (!size)
                return 0;
 
-       size += __SIZEOF_POINTER__ - 1;
-       size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+       scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+       /*
+        * We let the allocation align to cacheline bytes to avoid livelock on
+        * certain platforms due to atomic variables from the same cache line.
+        */
+       size += scratch_alloc_align - 1;
+       size &= ~(scratch_alloc_align - 1);
 
        spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index bc357b2..be3e19c 100644
@@ -265,6 +265,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
        return 0;
 }
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long *cbom_block_size)
+{
+       int len;
+       const void *prop;
+       const fdt32_t *val;
+
+       if (!fdt || cpu_offset < 0)
+               return SBI_EINVAL;
+
+       prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+       if (!prop || !len)
+               return SBI_EINVAL;
+       if (strncmp (prop, "cpu", strlen ("cpu")))
+               return SBI_EINVAL;
+
+       val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+       if (!val || len < sizeof(fdt32_t))
+               return SBI_EINVAL;
+
+       if (cbom_block_size)
+               *cbom_block_size = fdt32_to_cpu(*val);
+       return 0;
+}
+
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
        u32 hartid;
platform/generic/platform.c +9 -1
index b2f29e8..da77ea3 100644
@@ -171,6 +171,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
        const void *fdt = (void *)arg1;
        u32 hartid, hart_count = 0;
        int rc, root_offset, cpus_offset, cpu_offset, len;
+       unsigned long cbom_block_size = 0;
+       unsigned long tmp = 0;
+
 
        root_offset = fdt_path_offset(fdt, "/");
        if (root_offset < 0)
@@ -204,11 +207,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                        continue;
 
                generic_hart_index2id[hart_count++] = hartid;
+
+               rc = fdt_parse_cbom_block_size(fdt, cpu_offset, &tmp);
+               if (rc)
+                       continue;
+               cbom_block_size = MAX(tmp, cbom_block_size);
        }
 
        platform.hart_count = hart_count;
        platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
        platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+       platform.cbom_block_size = cbom_block_size;
 
        fw_platform_coldboot_harts_init(fdt);
 
--

2.43.0

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by
#10 lore raw
In reply to a message not in the archive
Quoted 5 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

Add "lib: sbi: " prefix to patch the subject.

Quoted 24 lines

---
Changes in V4:
Pickup the cacheline size from the dts file
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  2 ++
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/sbi/sbi_scratch.c              | 27 +++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  9 +++++++++
 5 files changed, 62 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +0 -0
index 3c99d43..d69215b 100644

include/sbi/sbi_platform.h struct sbi_platform { · line 197

Quoted 6 lines
include/sbi/sbi_platform.h +2 -0
@@ -192,6 +192,8 @@ struct sbi_platform {
         *     hart_index2id[<abc>] = <abc>
         */
        const u32 *hart_index2id;
+       /** Allocation alignment for Scratch */
+       unsigned long cbom_block_size;
 };

Please add below an assert_member_offset() line for this new member.

Quoted 32 lines
include/sbi/sbi_platform.h +0 -0
 /**
include/sbi_utils/fdt/fdt_helper.h +2 -0
index ff83002..92de9d9 100644
@@ -53,6 +53,8 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *cbom_block_size);
+
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
 int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
lib/sbi/sbi_scratch.c +2 -0
index 8c7eeaf..ca88204 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 u32 sbi_scratch_hart_count;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS] = { [0 ... SBI_HARTMASK_MAX_BITS-1] = -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
@@ -21,6 +23,20 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
 static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 

lib/sbi/sbi_scratch.c struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS]; · line 35

Quoted 10 lines
lib/sbi/sbi_scratch.c +10 -0
+static unsigned long sbi_get_scratch_alloc_align(void)
+{
+       const struct sbi_platform *plat;
+       /*
+        * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+        * or riscv,cbom_block_size
+        */
+       plat = sbi_platform_thishart_ptr();
+       if (!plat)
+               return DEFAULT_SCRATCH_ALLOC_ALIGN;

Like Samuel commented, this can be:

if (!plat || !plat->cbom_block_size)

     return DEFAULT_SCRATCH_ALLOC_ALIGN;

return plat->cbom_block_size;

Quoted 103 lines
lib/sbi/sbi_scratch.c +13 -2
+       return plat->cbom_block_size ? plat->cbom_block_size : \
+                                                                       DEFAULT_SCRATCH_ALLOC_ALIGN;
+}
+
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
        sbi_for_each_hartindex(i)
@@ -57,6 +73,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        void *ptr;
        unsigned long ret = 0;
        struct sbi_scratch *rscratch;
+       unsigned long scratch_alloc_align = 0;
 
        /*
         * We have a simple brain-dead allocator which never expects
@@ -70,8 +87,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        if (!size)
                return 0;
 
-       size += __SIZEOF_POINTER__ - 1;
-       size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+       scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+       /*
+        * We let the allocation align to cacheline bytes to avoid livelock on
+        * certain platforms due to atomic variables from the same cache line.
+        */
+       size += scratch_alloc_align - 1;
+       size &= ~(scratch_alloc_align - 1);
 
        spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index bc357b2..be3e19c 100644
@@ -265,6 +265,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
        return 0;
 }
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long *cbom_block_size)
+{
+       int len;
+       const void *prop;
+       const fdt32_t *val;
+
+       if (!fdt || cpu_offset < 0)
+               return SBI_EINVAL;
+
+       prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+       if (!prop || !len)
+               return SBI_EINVAL;
+       if (strncmp (prop, "cpu", strlen ("cpu")))
+               return SBI_EINVAL;
+
+       val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+       if (!val || len < sizeof(fdt32_t))
+               return SBI_EINVAL;
+
+       if (cbom_block_size)
+               *cbom_block_size = fdt32_to_cpu(*val);
+       return 0;
+}
+
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
        u32 hartid;
platform/generic/platform.c +9 -1
index b2f29e8..da77ea3 100644
@@ -171,6 +171,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
        const void *fdt = (void *)arg1;
        u32 hartid, hart_count = 0;
        int rc, root_offset, cpus_offset, cpu_offset, len;
+       unsigned long cbom_block_size = 0;
+       unsigned long tmp = 0;
+
 
        root_offset = fdt_path_offset(fdt, "/");
        if (root_offset < 0)
@@ -204,11 +207,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                        continue;
 
                generic_hart_index2id[hart_count++] = hartid;
+
+               rc = fdt_parse_cbom_block_size(fdt, cpu_offset, &tmp);
+               if (rc)
+                       continue;
+               cbom_block_size = MAX(tmp, cbom_block_size);
        }
 
        platform.hart_count = hart_count;
        platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
        platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+       platform.cbom_block_size = cbom_block_size;
 
        fw_platform_coldboot_harts_init(fdt);
 
--

2.43.0

Quoted 4 lines

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Otherwise, this looks good to me.

Regards,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#11 lore raw

Hi Raj,

Quoted 9 lines · Anup Patel
Quoted 5 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

Do you mind sending v6 of this patch ?

Regards,
Anup

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#12 lore raw

PATCH v6 1/1 lib: sbi: Set the scratch allocation to alignment to cacheline size.

In reply to Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

--
Changes in V6:
Add assert for assert_member_offset for cbom_block_size Update the subject line
Changes in V5:
Formatting issues
Changes in V4:
Pickup the cacheline size from the dts file
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  5 +++++
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/sbi/sbi_scratch.c              | 26 ++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  9 +++++++++
 5 files changed, 64 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +5 -0
index 82840ae..08ece32 100644
@@ -39,6 +39,8 @@
 #define SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET (0x60 + __SIZEOF_POINTER__)
 /** Offset of hart_index2id in struct sbi_platform */
 #define SBI_PLATFORM_HART_INDEX2ID_OFFSET (0x60 + (__SIZEOF_POINTER__ * 2))
+/** Offset of cbom_block_size in struct sbi_platform */
+#define SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET (0x60 + (__SIZEOF_POINTER__ * 3))
 
 #define SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT		(1UL << 12)
 
@@ -190,6 +192,8 @@ struct sbi_platform {
 	 *     hart_index2id[<abc>] = <abc>
 	 */
 	const u32 *hart_index2id;
+	/** Allocation alignment for Scratch */
+	unsigned long cbom_block_size;
 };
 
 /**
@@ -207,6 +211,7 @@ assert_member_offset(struct sbi_platform, reserved, SBI_PLATFORM_RESERVED_OFFSET
 assert_member_offset(struct sbi_platform, platform_ops_addr, SBI_PLATFORM_OPS_OFFSET);
 assert_member_offset(struct sbi_platform, firmware_context, SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET);
 assert_member_offset(struct sbi_platform, hart_index2id, SBI_PLATFORM_HART_INDEX2ID_OFFSET);
+assert_member_offset(struct sbi_platform, cbom_block_size, SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET);
 
 /** Get pointer to sbi_platform for sbi_scratch pointer */
 #define sbi_platform_ptr(__s) \
include/sbi_utils/fdt/fdt_helper.h +2 -0
index 5875880..04c850c 100644
@@ -50,6 +50,8 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *cbom_block_size);
+
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
 int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
lib/sbi/sbi_scratch.c +24 -2
index 8c7eeaf..916f283 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 u32 sbi_scratch_hart_count;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS] = { [0 ... SBI_HARTMASK_MAX_BITS-1] = -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
@@ -21,6 +23,19 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
 static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 
+static unsigned long sbi_get_scratch_alloc_align(void)
+{
+	const struct sbi_platform *plat;
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */
+	plat = sbi_platform_thishart_ptr();
+	if (!plat || !plat->cbom_block_size)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size;
+}
+
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
 	sbi_for_each_hartindex(i)
@@ -57,6 +72,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	void *ptr;
 	unsigned long ret = 0;
 	struct sbi_scratch *rscratch;
+	unsigned long scratch_alloc_align = 0;
 
 	/*
 	 * We have a simple brain-dead allocator which never expects
@@ -70,8 +86,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	if (!size)
 		return 0;
 
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+	 * We let the allocation align to cacheline bytes to avoid livelock on
+	 * certain platforms due to atomic variables from the same cache line.
+	 */
+	size += scratch_alloc_align - 1;
+	size &= ~(scratch_alloc_align - 1);
 
 	spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index 79e59dd..b2d91fd 100644
@@ -246,6 +246,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 	return 0;
 }
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long *cbom_block_size)
+{
+	int len;
+	const void *prop;
+	const fdt32_t *val;
+
+	if (!fdt || cpu_offset < 0)
+		return SBI_EINVAL;
+
+	prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+	if (!prop || !len)
+		return SBI_EINVAL;
+	if (strncmp (prop, "cpu", strlen ("cpu")))
+		return SBI_EINVAL;
+
+	val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+	if (!val || len < sizeof(fdt32_t))
+		return SBI_EINVAL;
+
+	if (cbom_block_size)
+		*cbom_block_size = fdt32_to_cpu(*val);
+	return 0;
+}
+
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
 	u32 hartid;
platform/generic/platform.c +9 -0
index f3072be..2b1b949 100644
@@ -147,6 +147,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 	const void *fdt = (void *)arg1;
 	u32 hartid, hart_count = 0;
 	int rc, root_offset, cpus_offset, cpu_offset, len;
+	unsigned long cbom_block_size = 0;
+	unsigned long tmp = 0;
+
 
 	root_offset = fdt_path_offset(fdt, "/");
 	if (root_offset < 0)
@@ -174,11 +177,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 			continue;
 
 		generic_hart_index2id[hart_count++] = hartid;
+
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset, &tmp);
+		if (rc)
+			continue;
+		cbom_block_size = MAX(tmp, cbom_block_size);
 	}
 
 	platform.hart_count = hart_count;
 	platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
 	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+	platform.cbom_block_size = cbom_block_size;
 
 	fw_platform_coldboot_harts_init(fdt);
 
Signature · 7 lines
2.43.0


-- 
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Reviewed-by on v6 1/1
#13 lore raw

Re: [PATCH v6] lib: sbi: Set the scratch allocation to alignment to cacheline size.

Hi Raj,

Quoted 105 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

--
Changes in V6:
Add assert for assert_member_offset for cbom_block_size Update the subject line
Changes in V5:
Formatting issues
Changes in V4:
Pickup the cacheline size from the dts file
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  5 +++++
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/sbi/sbi_scratch.c              | 26 ++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  9 +++++++++
 5 files changed, 64 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +5 -0
index 82840ae..08ece32 100644
@@ -39,6 +39,8 @@
 #define SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET (0x60 + __SIZEOF_POINTER__)
 /** Offset of hart_index2id in struct sbi_platform */
 #define SBI_PLATFORM_HART_INDEX2ID_OFFSET (0x60 + (__SIZEOF_POINTER__ * 2))
+/** Offset of cbom_block_size in struct sbi_platform */
+#define SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET (0x60 + (__SIZEOF_POINTER__ * 3))
 
 #define SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT		(1UL << 12)
 
@@ -190,6 +192,8 @@ struct sbi_platform {
 	 *     hart_index2id[<abc>] = <abc>
 	 */
 	const u32 *hart_index2id;
+	/** Allocation alignment for Scratch */
+	unsigned long cbom_block_size;
 };
 
 /**
@@ -207,6 +211,7 @@ assert_member_offset(struct sbi_platform, reserved, SBI_PLATFORM_RESERVED_OFFSET
 assert_member_offset(struct sbi_platform, platform_ops_addr, SBI_PLATFORM_OPS_OFFSET);
 assert_member_offset(struct sbi_platform, firmware_context, SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET);
 assert_member_offset(struct sbi_platform, hart_index2id, SBI_PLATFORM_HART_INDEX2ID_OFFSET);
+assert_member_offset(struct sbi_platform, cbom_block_size, SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET);
 
 /** Get pointer to sbi_platform for sbi_scratch pointer */
 #define sbi_platform_ptr(__s) \
include/sbi_utils/fdt/fdt_helper.h +2 -0
index 5875880..04c850c 100644
@@ -50,6 +50,8 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *cbom_block_size);
+
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
 int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
lib/sbi/sbi_scratch.c +15 -0
index 8c7eeaf..916f283 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 u32 sbi_scratch_hart_count;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS] = { [0 ... SBI_HARTMASK_MAX_BITS-1] = -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
@@ -21,6 +23,19 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
 static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 
+static unsigned long sbi_get_scratch_alloc_align(void)
+{
+	const struct sbi_platform *plat;
+	/*
+	 * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+	 * or riscv,cbom_block_size
+	 */
+	plat = sbi_platform_thishart_ptr();
+	if (!plat || !plat->cbom_block_size)
+		return DEFAULT_SCRATCH_ALLOC_ALIGN;
+	return plat->cbom_block_size;
+}
+
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
 	sbi_for_each_hartindex(i)

lib/sbi/sbi_scratch.c unsigned long sbi_scratch_alloc_offset(unsigned long size) · line 75

Quoted 4 lines
lib/sbi/sbi_scratch.c +1 -0
@@ -57,6 +72,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	void *ptr;
 	unsigned long ret = 0;
 	struct sbi_scratch *rscratch;
+	unsigned long scratch_alloc_align = 0;

This variable is unconditionally assigned below, so it doesn't need to be initialized here.

Quoted 60 lines
lib/sbi/sbi_scratch.c +8 -2
 	/*
 	 * We have a simple brain-dead allocator which never expects
@@ -70,8 +86,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
 	if (!size)
 		return 0;
 
-	size += __SIZEOF_POINTER__ - 1;
-	size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+	scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+	/*
+	 * We let the allocation align to cacheline bytes to avoid livelock on
+	 * certain platforms due to atomic variables from the same cache line.
+	 */
+	size += scratch_alloc_align - 1;
+	size &= ~(scratch_alloc_align - 1);
 
 	spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index 79e59dd..b2d91fd 100644
@@ -246,6 +246,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
 	return 0;
 }
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long *cbom_block_size)
+{
+	int len;
+	const void *prop;
+	const fdt32_t *val;
+
+	if (!fdt || cpu_offset < 0)
+		return SBI_EINVAL;
+
+	prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+	if (!prop || !len)
+		return SBI_EINVAL;
+	if (strncmp (prop, "cpu", strlen ("cpu")))
+		return SBI_EINVAL;
+
+	val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+	if (!val || len < sizeof(fdt32_t))
+		return SBI_EINVAL;
+
+	if (cbom_block_size)
+		*cbom_block_size = fdt32_to_cpu(*val);
+	return 0;
+}
+
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
 	u32 hartid;
platform/generic/platform.c +0 -0
index f3072be..2b1b949 100644

platform/generic/platform.c unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, · line 153

Quoted 7 lines
platform/generic/platform.c +3 -0
@@ -147,6 +147,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 	const void *fdt = (void *)arg1;
 	u32 hartid, hart_count = 0;
 	int rc, root_offset, cpus_offset, cpu_offset, len;
+	unsigned long cbom_block_size = 0;
+	unsigned long tmp = 0;
+
 

Extra blank line here. These are trivial issues, so:

Quoted 20 lines
platform/generic/platform.c +6 -0
 	root_offset = fdt_path_offset(fdt, "/");
 	if (root_offset < 0)
@@ -174,11 +177,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
 			continue;
 
 		generic_hart_index2id[hart_count++] = hartid;
+
+		rc = fdt_parse_cbom_block_size(fdt, cpu_offset, &tmp);
+		if (rc)
+			continue;
+		cbom_block_size = MAX(tmp, cbom_block_size);
 	}
 
 	platform.hart_count = hart_count;
 	platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
 	platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+	platform.cbom_block_size = cbom_block_size;
 
 	fw_platform_coldboot_harts_init(fdt);
 
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
#14 lore raw

Re: [PATCH v6] lib: sbi: Set the scratch allocation to alignment to cacheline size.

Samuel

Thank you for the code review. Would you like me send an updated patch? I am confused by "These are trivial issues, so:"

Raj

Quoted 210 lines · Samuel Holland

Hi Raj,

Quoted 105 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

--
Changes in V6:
Add assert for assert_member_offset for cbom_block_size Update the subject line
Changes in V5:
Formatting issues
Changes in V4:
Pickup the cacheline size from the dts file
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  5 +++++
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/sbi/sbi_scratch.c              | 26 ++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  9 +++++++++
 5 files changed, 64 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +5 -0
index 82840ae..08ece32 100644
@@ -39,6 +39,8 @@
 #define SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET (0x60 + __SIZEOF_POINTER__)
 /** Offset of hart_index2id in struct sbi_platform */
 #define SBI_PLATFORM_HART_INDEX2ID_OFFSET (0x60 + (__SIZEOF_POINTER__ * 2))
+/** Offset of cbom_block_size in struct sbi_platform */
+#define SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET (0x60 + (__SIZEOF_POINTER__ * 3))
 
 #define SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT           (1UL << 12)
 
@@ -190,6 +192,8 @@ struct sbi_platform {
       *     hart_index2id[<abc>] = <abc>
       */
      const u32 *hart_index2id;
+     /** Allocation alignment for Scratch */
+     unsigned long cbom_block_size;
 };
 
 /**
@@ -207,6 +211,7 @@ assert_member_offset(struct sbi_platform, reserved, SBI_PLATFORM_RESERVED_OFFSET
 assert_member_offset(struct sbi_platform, platform_ops_addr, SBI_PLATFORM_OPS_OFFSET);
 assert_member_offset(struct sbi_platform, firmware_context, SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET);
 assert_member_offset(struct sbi_platform, hart_index2id, SBI_PLATFORM_HART_INDEX2ID_OFFSET);
+assert_member_offset(struct sbi_platform, cbom_block_size, SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET);
 
 /** Get pointer to sbi_platform for sbi_scratch pointer */
 #define sbi_platform_ptr(__s) \
include/sbi_utils/fdt/fdt_helper.h +2 -0
index 5875880..04c850c 100644
@@ -50,6 +50,8 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *cbom_block_size);
+
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
 int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
lib/sbi/sbi_scratch.c +15 -0
index 8c7eeaf..916f283 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 u32 sbi_scratch_hart_count;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS] = { [0 ... SBI_HARTMASK_MAX_BITS-1] = -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
@@ -21,6 +23,19 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
 static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 
+static unsigned long sbi_get_scratch_alloc_align(void)
+{
+     const struct sbi_platform *plat;
+     /*
+      * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+      * or riscv,cbom_block_size
+      */
+     plat = sbi_platform_thishart_ptr();
+     if (!plat || !plat->cbom_block_size)
+             return DEFAULT_SCRATCH_ALLOC_ALIGN;
+     return plat->cbom_block_size;
+}
+
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
      sbi_for_each_hartindex(i)

lib/sbi/sbi_scratch.c unsigned long sbi_scratch_alloc_offset(unsigned long size) · line 75

Quoted 4 lines
lib/sbi/sbi_scratch.c +1 -0
@@ -57,6 +72,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      void *ptr;
      unsigned long ret = 0;
      struct sbi_scratch *rscratch;
+     unsigned long scratch_alloc_align = 0;

This variable is unconditionally assigned below, so it doesn't need to be initialized here.

Quoted 60 lines
lib/sbi/sbi_scratch.c +8 -2
      /*
       * We have a simple brain-dead allocator which never expects
@@ -70,8 +86,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
      if (!size)
              return 0;
 
-     size += __SIZEOF_POINTER__ - 1;
-     size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+     scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+     /*
+      * We let the allocation align to cacheline bytes to avoid livelock on
+      * certain platforms due to atomic variables from the same cache line.
+      */
+     size += scratch_alloc_align - 1;
+     size &= ~(scratch_alloc_align - 1);
 
      spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index 79e59dd..b2d91fd 100644
@@ -246,6 +246,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
      return 0;
 }
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long *cbom_block_size)
+{
+     int len;
+     const void *prop;
+     const fdt32_t *val;
+
+     if (!fdt || cpu_offset < 0)
+             return SBI_EINVAL;
+
+     prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+     if (!prop || !len)
+             return SBI_EINVAL;
+     if (strncmp (prop, "cpu", strlen ("cpu")))
+             return SBI_EINVAL;
+
+     val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+     if (!val || len < sizeof(fdt32_t))
+             return SBI_EINVAL;
+
+     if (cbom_block_size)
+             *cbom_block_size = fdt32_to_cpu(*val);
+     return 0;
+}
+
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
      u32 hartid;
platform/generic/platform.c +0 -0
index f3072be..2b1b949 100644

platform/generic/platform.c unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1, · line 153

Quoted 7 lines
platform/generic/platform.c +3 -0
@@ -147,6 +147,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
      const void *fdt = (void *)arg1;
      u32 hartid, hart_count = 0;
      int rc, root_offset, cpus_offset, cpu_offset, len;
+     unsigned long cbom_block_size = 0;
+     unsigned long tmp = 0;
+
 

Extra blank line here. These are trivial issues, so:

Quoted 20 lines
platform/generic/platform.c +6 -0
      root_offset = fdt_path_offset(fdt, "/");
      if (root_offset < 0)
@@ -174,11 +177,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                      continue;
 
              generic_hart_index2id[hart_count++] = hartid;
+
+             rc = fdt_parse_cbom_block_size(fdt, cpu_offset, &tmp);
+             if (rc)
+                     continue;
+             cbom_block_size = MAX(tmp, cbom_block_size);
      }
 
      platform.hart_count = hart_count;
      platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
      platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+     platform.cbom_block_size = cbom_block_size;
 
      fw_platform_coldboot_harts_init(fdt);
 
Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi
Applied
#15 lore raw

Re: [PATCH v6] lib: sbi: Set the scratch allocation to alignment to cacheline size.

In reply to Raj Vishwanathan
Quoted 7 lines · Raj Vishwanathan

We set the scratch allocation alignment to cacheline size,specified by riscv,cbom-block-size in the dts file to avoid two atomic variables from the same cache line causing livelock on some platforms. If the cacheline is not specified, we set it a default value.

I have taken care of Samuel's comments at the time of merging.

Please carry Reviewed-by tags obtained in previous versions over here.

Applied this patch to the riscv/opensbi repo.

Thanks,
Anup

Quoted 198 lines

--
Changes in V6:
Add assert for assert_member_offset for cbom_block_size Update the subject line
Changes in V5:
Formatting issues
Changes in V4:
Pickup the cacheline size from the dts file
Changes in V3:
Remove platform specific references to 64 Bytes.
Changes in V2:
Added a new configuration to get the alignment size.
Change in V1:
Original Patch

---
 include/sbi/sbi_platform.h         |  5 +++++
 include/sbi_utils/fdt/fdt_helper.h |  2 ++
 lib/sbi/sbi_scratch.c              | 26 ++++++++++++++++++++++++--
 lib/utils/fdt/fdt_helper.c         | 24 ++++++++++++++++++++++++
 platform/generic/platform.c        |  9 +++++++++
 5 files changed, 64 insertions(+), 2 deletions(-)
include/sbi/sbi_platform.h +5 -0
index 82840ae..08ece32 100644
@@ -39,6 +39,8 @@
 #define SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET (0x60 + __SIZEOF_POINTER__)
 /** Offset of hart_index2id in struct sbi_platform */
 #define SBI_PLATFORM_HART_INDEX2ID_OFFSET (0x60 + (__SIZEOF_POINTER__ * 2))
+/** Offset of cbom_block_size in struct sbi_platform */
+#define SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET (0x60 + (__SIZEOF_POINTER__ * 3))
 
 #define SBI_PLATFORM_TLB_RANGE_FLUSH_LIMIT_DEFAULT             (1UL << 12)
 
@@ -190,6 +192,8 @@ struct sbi_platform {
         *     hart_index2id[<abc>] = <abc>
         */
        const u32 *hart_index2id;
+       /** Allocation alignment for Scratch */
+       unsigned long cbom_block_size;
 };
 
 /**
@@ -207,6 +211,7 @@ assert_member_offset(struct sbi_platform, reserved, SBI_PLATFORM_RESERVED_OFFSET
 assert_member_offset(struct sbi_platform, platform_ops_addr, SBI_PLATFORM_OPS_OFFSET);
 assert_member_offset(struct sbi_platform, firmware_context, SBI_PLATFORM_FIRMWARE_CONTEXT_OFFSET);
 assert_member_offset(struct sbi_platform, hart_index2id, SBI_PLATFORM_HART_INDEX2ID_OFFSET);
+assert_member_offset(struct sbi_platform, cbom_block_size, SBI_PLATFORM_CBOM_BLOCK_SIZE_OFFSET);
 
 /** Get pointer to sbi_platform for sbi_scratch pointer */
 #define sbi_platform_ptr(__s) \
include/sbi_utils/fdt/fdt_helper.h +2 -0
index 5875880..04c850c 100644
@@ -50,6 +50,8 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid);
 
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid);
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long  *cbom_block_size);
+
 int fdt_parse_timebase_frequency(const void *fdt, unsigned long *freq);
 
 int fdt_parse_isa_extensions(const void *fdt, unsigned int hartid,
lib/sbi/sbi_scratch.c +24 -2
index 8c7eeaf..916f283 100644
@@ -14,6 +14,8 @@
 #include <sbi/sbi_scratch.h>
 #include <sbi/sbi_string.h>
 
+#define DEFAULT_SCRATCH_ALLOC_ALIGN __SIZEOF_POINTER__
+
 u32 sbi_scratch_hart_count;
 u32 hartindex_to_hartid_table[SBI_HARTMASK_MAX_BITS] = { [0 ... SBI_HARTMASK_MAX_BITS-1] = -1U };
 struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
@@ -21,6 +23,19 @@ struct sbi_scratch *hartindex_to_scratch_table[SBI_HARTMASK_MAX_BITS];
 static spinlock_t extra_lock = SPIN_LOCK_INITIALIZER;
 static unsigned long extra_offset = SBI_SCRATCH_EXTRA_SPACE_OFFSET;
 
+static unsigned long sbi_get_scratch_alloc_align(void)
+{
+       const struct sbi_platform *plat;
+       /*
+        * Get the alignment size. We will return DEFAULT_SCRATCH_ALLOC_ALIGNMENT
+        * or riscv,cbom_block_size
+        */
+       plat = sbi_platform_thishart_ptr();
+       if (!plat || !plat->cbom_block_size)
+               return DEFAULT_SCRATCH_ALLOC_ALIGN;
+       return plat->cbom_block_size;
+}
+
 u32 sbi_hartid_to_hartindex(u32 hartid)
 {
        sbi_for_each_hartindex(i)
@@ -57,6 +72,7 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        void *ptr;
        unsigned long ret = 0;
        struct sbi_scratch *rscratch;
+       unsigned long scratch_alloc_align = 0;
 
        /*
         * We have a simple brain-dead allocator which never expects
@@ -70,8 +86,14 @@ unsigned long sbi_scratch_alloc_offset(unsigned long size)
        if (!size)
                return 0;
 
-       size += __SIZEOF_POINTER__ - 1;
-       size &= ~((unsigned long)__SIZEOF_POINTER__ - 1);
+       scratch_alloc_align = sbi_get_scratch_alloc_align();
+
+       /*
+        * We let the allocation align to cacheline bytes to avoid livelock on
+        * certain platforms due to atomic variables from the same cache line.
+        */
+       size += scratch_alloc_align - 1;
+       size &= ~(scratch_alloc_align - 1);
 
        spin_lock(&extra_lock);
 
lib/utils/fdt/fdt_helper.c +24 -0
index 79e59dd..b2d91fd 100644
@@ -246,6 +246,30 @@ int fdt_parse_hart_id(const void *fdt, int cpu_offset, u32 *hartid)
        return 0;
 }
 
+int fdt_parse_cbom_block_size(const void *fdt, int cpu_offset, unsigned long *cbom_block_size)
+{
+       int len;
+       const void *prop;
+       const fdt32_t *val;
+
+       if (!fdt || cpu_offset < 0)
+               return SBI_EINVAL;
+
+       prop = fdt_getprop(fdt, cpu_offset, "device_type", &len);
+       if (!prop || !len)
+               return SBI_EINVAL;
+       if (strncmp (prop, "cpu", strlen ("cpu")))
+               return SBI_EINVAL;
+
+       val = fdt_getprop(fdt, cpu_offset, "riscv,cbom-block-size", &len);
+       if (!val || len < sizeof(fdt32_t))
+               return SBI_EINVAL;
+
+       if (cbom_block_size)
+               *cbom_block_size = fdt32_to_cpu(*val);
+       return 0;
+}
+
 int fdt_parse_max_enabled_hart_id(const void *fdt, u32 *max_hartid)
 {
        u32 hartid;
platform/generic/platform.c +9 -1
index f3072be..2b1b949 100644
@@ -147,6 +147,9 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
        const void *fdt = (void *)arg1;
        u32 hartid, hart_count = 0;
        int rc, root_offset, cpus_offset, cpu_offset, len;
+       unsigned long cbom_block_size = 0;
+       unsigned long tmp = 0;
+
 
        root_offset = fdt_path_offset(fdt, "/");
        if (root_offset < 0)
@@ -174,11 +177,17 @@ unsigned long fw_platform_init(unsigned long arg0, unsigned long arg1,
                        continue;
 
                generic_hart_index2id[hart_count++] = hartid;
+
+               rc = fdt_parse_cbom_block_size(fdt, cpu_offset, &tmp);
+               if (rc)
+                       continue;
+               cbom_block_size = MAX(tmp, cbom_block_size);
        }
 
        platform.hart_count = hart_count;
        platform.heap_size = fw_platform_get_heap_size(fdt, hart_count);
        platform_has_mlevel_imsic = fdt_check_imsic_mlevel(fdt);
+       platform.cbom_block_size = cbom_block_size;
 
        fw_platform_coldboot_harts_init(fdt);
 
--

2.43.0

--
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi

Signature · 3 lines
opensbi mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/opensbi