Skip to contentSkip to conversation

riscv: Fix detecting FPU support in standard extension

PATCH In review Yu Chien Peter Lin 4 participants 5 messages Nov 5, 2022 – Nov 14, 2022 1 message not in the archive RISC-V

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 riscv: Fix detecting FPU support in standard extension

We should check the string until it hits underscore, in case it searches for the letters in the custom extension. For example, "rv64imac_xandes" will be treated as D extension support since there is a "d" in "andes", resulting illegal instruction caused by initializing FCSR.

---
 arch/riscv/cpu/cpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/riscv/cpu/cpu.c +11 -3
index 52ab02519f..d34c8efce0 100644
@@ -36,6 +36,7 @@ static inline bool supports_extension(char ext)
 #ifdef CONFIG_CPU
 	struct udevice *dev;
 	char desc[32];
+	int i;
 
 	uclass_find_first_device(UCLASS_CPU, &dev);
 	if (!dev) {
@@ -43,9 +44,16 @@ static inline bool supports_extension(char ext)
 		return false;
 	}
 	if (!cpu_get_desc(dev, desc, sizeof(desc))) {
-		/* skip the first 4 characters (rv32|rv64) */
-		if (strchr(desc + 4, ext))
-			return true;
+		/*
+		 * skip the first 4 characters (rv32|rv64) and
+		 * check until underscore
+		 */
+		for (i = 4; i < sizeof(desc); i++) {
+			if (desc[i] == '_' || desc[i] == '\0')
+				break;
+			if (desc[i] == ext)
+				return true;
+		}
 	}
 
 	return false;
Signature · 1 line
2.34.1

A message that is not in the archive (sent off-list or not yet received): HK0PR03MB2994D5179CFED4510DB0DCCDC1019@HK0PR03MB2994.apcprd03.prod.outlook.com

Reviewed-by
#2 lore raw
Quoted 6 lines

From: U-Boot <[email protected]> On Behalf Of Yu Chien Peter Lin
Sent: Saturday, November 05, 2022 2:02 PM
To: [email protected]
Cc: [email protected]; Peter Yu-Chien Lin(林宇謙) <[email protected]>
Subject: [PATCH v2] riscv: Fix detecting FPU support in standard extension

Quoted 6 lines

We should check the string until it hits underscore, in case it searches for the letters in the custom extension. For example, "rv64imac_xandes" will be treated as D extension support since there is a "d" in "andes", resulting illegal instruction caused by initializing FCSR.

---
 arch/riscv/cpu/cpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
Reviewed-by on v2 1/1
#3 lore raw
In reply to Yu Chien Peter Lin
Quoted 33 lines · Yu Chien Peter Lin

We should check the string until it hits underscore, in case it searches for the letters in the custom extension. For example, "rv64imac_xandes" will be treated as D extension support since there is a "d" in "andes", resulting illegal instruction caused by initializing FCSR.

---
 arch/riscv/cpu/cpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/riscv/cpu/cpu.c +2 -3
index 52ab02519f..d34c8efce0 100644
@@ -36,6 +36,7 @@ static inline bool supports_extension(char ext)
 #ifdef CONFIG_CPU
        struct udevice *dev;
        char desc[32];
+       int i;
 
        uclass_find_first_device(UCLASS_CPU, &dev);
        if (!dev) {
@@ -43,9 +44,16 @@ static inline bool supports_extension(char ext)
                return false;
        }
        if (!cpu_get_desc(dev, desc, sizeof(desc))) {
-               /* skip the first 4 characters (rv32|rv64) */
-               if (strchr(desc + 4, ext))
-                       return true;
+               /*

arch/riscv/cpu/cpu.c static inline bool supports_extension(char ext) · line 57

Quoted 10 lines
arch/riscv/cpu/cpu.c +9 -0
+                * skip the first 4 characters (rv32|rv64) and
+                * check until underscore
+                */
+               for (i = 4; i < sizeof(desc); i++) {
+                       if (desc[i] == '_' || desc[i] == '\0')
+                               break;
+                       if (desc[i] == ext)
+                               return true;
+               }
 
Quoted 6 lines
arch/riscv/cpu/cpu.c +0 -0
        }
 
        return false;

--
2.34.1

Reviewed-by on v2 1/1
#4 lore raw
In reply to Yu Chien Peter Lin
Quoted 24 lines · Yu Chien Peter Lin

We should check the string until it hits underscore, in case it searches for the letters in the custom extension. For example, "rv64imac_xandes" will be treated as D extension support since there is a "d" in "andes", resulting illegal instruction caused by initializing FCSR.

---
 arch/riscv/cpu/cpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/riscv/cpu/cpu.c +1 -0
index 52ab02519f..d34c8efce0 100644
@@ -36,6 +36,7 @@ static inline bool supports_extension(char ext)
 #ifdef CONFIG_CPU
 	struct udevice *dev;
 	char desc[32];
+	int i;
 
 	uclass_find_first_device(UCLASS_CPU, &dev);
 	if (!dev) {

arch/riscv/cpu/cpu.c static inline bool supports_extension(char ext) · line 50

Quoted 10 lines
arch/riscv/cpu/cpu.c +4 -3
@@ -43,9 +44,16 @@ static inline bool supports_extension(char ext)
 		return false;
 	}
 	if (!cpu_get_desc(dev, desc, sizeof(desc))) {
-		/* skip the first 4 characters (rv32|rv64) */
-		if (strchr(desc + 4, ext))
-			return true;
+		/*
+		 * skip the first 4 characters (rv32|rv64) and
+		 * check until underscore
+		 */

This probably works for the intended usage, but it is not guaranteed to work. Underscores are allowed even between single-letter extensions. See section 29.5 ("Underscores") of the unprivileged spec. Still, this is an improvement, so:

Quoted 9 lines
arch/riscv/cpu/cpu.c +6 -0
+		for (i = 4; i < sizeof(desc); i++) {
+			if (desc[i] == '_' || desc[i] == '\0')
+				break;
+			if (desc[i] == ext)
+				return true;
+		}
 	}
 
 	return false;
#5 lore raw
Quoted 41 lines · Samuel Holland
Quoted 24 lines · Yu Chien Peter Lin

We should check the string until it hits underscore, in case it searches for the letters in the custom extension. For example, "rv64imac_xandes" will be treated as D extension support since there is a "d" in "andes", resulting illegal instruction caused by initializing FCSR.

---
 arch/riscv/cpu/cpu.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
arch/riscv/cpu/cpu.c +1 -0
index 52ab02519f..d34c8efce0 100644
@@ -36,6 +36,7 @@ static inline bool supports_extension(char ext)
 #ifdef CONFIG_CPU
 	struct udevice *dev;
 	char desc[32];
+	int i;
 
 	uclass_find_first_device(UCLASS_CPU, &dev);
 	if (!dev) {

arch/riscv/cpu/cpu.c static inline bool supports_extension(char ext) · line 50

Quoted 10 lines
arch/riscv/cpu/cpu.c +4 -3
@@ -43,9 +44,16 @@ static inline bool supports_extension(char ext)
 		return false;
 	}
 	if (!cpu_get_desc(dev, desc, sizeof(desc))) {
-		/* skip the first 4 characters (rv32|rv64) */
-		if (strchr(desc + 4, ext))
-			return true;
+		/*
+		 * skip the first 4 characters (rv32|rv64) and
+		 * check until underscore
+		 */

This probably works for the intended usage, but it is not guaranteed to work. Underscores are allowed even between single-letter extensions. See section 29.5 ("Underscores") of the unprivileged spec. Still, this is an improvement, so:

Quoted 1 line

Aah, I missed that case.
I supposed the strings always match the pattern stated in riscv cpus binding [1], the detection logic here needs to be updated once the case is also allowed.

[1] https://github.com/torvalds/linux/blob/v6.1-rc5/Documentation/devicetree/bindings/riscv/cpus.yaml#L83

Thanks for your review!

Best regards,
Peter Lin

Quoted 11 lines
Quoted 10 lines
arch/riscv/cpu/cpu.c +6 -0
+		for (i = 4; i < sizeof(desc); i++) {
+			if (desc[i] == '_' || desc[i] == '\0')
+				break;
+			if (desc[i] == ext)
+				return true;
+		}
 	}
 
 	return false;