Skip to content

Commit 255b34d

Browse files
cyyselfpalmer-dabbelt
authored andcommitted
riscv: allow case-insensitive ISA string parsing
According to RISC-V Hart Capabilities Table (RHCT) description in UEFI Forum ECR, the format of the ISA string is defined in the RISC-V unprivileged specification which is case-insensitive. However, the current ISA string parser in the kernel does not support ISA strings with uppercase letters. This patch modifies the ISA string parser in the kernel to support case-insensitive ISA string parsing. Reviewed-by: Andrew Jones <[email protected]> Reviewed-by: Conor Dooley <[email protected]> Signed-off-by: Yangyu Chen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent ac9a786 commit 255b34d

File tree

2 files changed

+19
-19
lines changed

2 files changed

+19
-19
lines changed

arch/riscv/kernel/cpu.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <linux/cpu.h>
7+
#include <linux/ctype.h>
78
#include <linux/init.h>
89
#include <linux/seq_file.h>
910
#include <linux/of.h>
@@ -42,7 +43,7 @@ int riscv_of_processor_hartid(struct device_node *node, unsigned long *hart)
4243
pr_warn("CPU with hartid=%lu has no \"riscv,isa\" property\n", *hart);
4344
return -ENODEV;
4445
}
45-
if (isa[0] != 'r' || isa[1] != 'v') {
46+
if (tolower(isa[0]) != 'r' || tolower(isa[1]) != 'v') {
4647
pr_warn("CPU with hartid=%lu has an invalid ISA of \"%s\"\n", *hart, isa);
4748
return -ENODEV;
4849
}

arch/riscv/kernel/cpufeature.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,10 @@ void __init riscv_fill_hwcap(void)
127127
}
128128

129129
temp = isa;
130-
#if IS_ENABLED(CONFIG_32BIT)
131-
if (!strncmp(isa, "rv32", 4))
130+
if (IS_ENABLED(CONFIG_32BIT) && !strncasecmp(isa, "rv32", 4))
132131
isa += 4;
133-
#elif IS_ENABLED(CONFIG_64BIT)
134-
if (!strncmp(isa, "rv64", 4))
132+
else if (IS_ENABLED(CONFIG_64BIT) && !strncasecmp(isa, "rv64", 4))
135133
isa += 4;
136-
#endif
137134
/* The riscv,isa DT property must start with rv64 or rv32 */
138135
if (temp == isa)
139136
continue;
@@ -157,13 +154,15 @@ void __init riscv_fill_hwcap(void)
157154
break;
158155
}
159156
fallthrough;
157+
case 'S':
160158
case 'x':
159+
case 'X':
161160
case 'z':
161+
case 'Z':
162162
ext_long = true;
163163
/* Multi-letter extension must be delimited */
164164
for (; *isa && *isa != '_'; ++isa)
165-
if (unlikely(!islower(*isa)
166-
&& !isdigit(*isa)))
165+
if (unlikely(!isalnum(*isa)))
167166
ext_err = true;
168167
/* Parse backwards */
169168
ext_end = isa;
@@ -174,7 +173,7 @@ void __init riscv_fill_hwcap(void)
174173
/* Skip the minor version */
175174
while (isdigit(*--ext_end))
176175
;
177-
if (ext_end[0] != 'p'
176+
if (tolower(ext_end[0]) != 'p'
178177
|| !isdigit(ext_end[-1])) {
179178
/* Advance it to offset the pre-decrement */
180179
++ext_end;
@@ -186,7 +185,7 @@ void __init riscv_fill_hwcap(void)
186185
++ext_end;
187186
break;
188187
default:
189-
if (unlikely(!islower(*ext))) {
188+
if (unlikely(!isalpha(*ext))) {
190189
ext_err = true;
191190
break;
192191
}
@@ -196,7 +195,7 @@ void __init riscv_fill_hwcap(void)
196195
/* Skip the minor version */
197196
while (isdigit(*++isa))
198197
;
199-
if (*isa != 'p')
198+
if (tolower(*isa) != 'p')
200199
break;
201200
if (!isdigit(*++isa)) {
202201
--isa;
@@ -210,18 +209,18 @@ void __init riscv_fill_hwcap(void)
210209
if (*isa != '_')
211210
--isa;
212211

213-
#define SET_ISA_EXT_MAP(name, bit) \
214-
do { \
215-
if ((ext_end - ext == sizeof(name) - 1) && \
216-
!memcmp(ext, name, sizeof(name) - 1) && \
217-
riscv_isa_extension_check(bit)) \
218-
set_bit(bit, this_isa); \
219-
} while (false) \
212+
#define SET_ISA_EXT_MAP(name, bit) \
213+
do { \
214+
if ((ext_end - ext == sizeof(name) - 1) && \
215+
!strncasecmp(ext, name, sizeof(name) - 1) && \
216+
riscv_isa_extension_check(bit)) \
217+
set_bit(bit, this_isa); \
218+
} while (false) \
220219

221220
if (unlikely(ext_err))
222221
continue;
223222
if (!ext_long) {
224-
int nr = *ext - 'a';
223+
int nr = tolower(*ext) - 'a';
225224

226225
if (riscv_isa_extension_check(nr)) {
227226
this_hwcap |= isa2hwcap[nr];

0 commit comments

Comments
 (0)