Skip to content

Commit 377be47

Browse files
charlie-rivospalmer-dabbelt
authored andcommitted
riscv: vector: Use vlenb from DT for thead
If thead,vlenb is provided in the device tree, prefer that over reading the vlenb csr. Signed-off-by: Charlie Jenkins <[email protected]> Acked-by: Conor Dooley <[email protected]> Tested-by: Yangyu Chen <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent cddd638 commit 377be47

File tree

6 files changed

+91
-1
lines changed

6 files changed

+91
-1
lines changed

arch/riscv/Kconfig.vendor

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ config RISCV_ISA_VENDOR_EXT_THEAD
2626
extensions. Without this option enabled, T-Head vendor extensions will
2727
not be detected at boot and their presence not reported to userspace.
2828

29+
If you don't know what to do here, say Y.
30+
31+
config RISCV_ISA_XTHEADVECTOR
32+
bool "xtheadvector extension support"
33+
depends on RISCV_ISA_VENDOR_EXT_THEAD
34+
depends on RISCV_ISA_V
35+
depends on FPU
36+
default y
37+
help
38+
Say N here if you want to disable all xtheadvector related procedures
39+
in the kernel. This will disable vector for any T-Head board that
40+
contains xtheadvector rather than the standard vector.
41+
2942
If you don't know what to do here, say Y.
3043
endmenu
3144

arch/riscv/include/asm/cpufeature.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo);
3434
/* Per-cpu ISA extensions. */
3535
extern struct riscv_isainfo hart_isa[NR_CPUS];
3636

37+
extern u32 thead_vlenb_of;
38+
3739
void __init riscv_user_isa_enable(void);
3840

3941
#define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size, _validate) { \

arch/riscv/include/asm/vendor_extensions/thead.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313

1414
extern struct riscv_isa_vendor_ext_data_list riscv_isa_vendor_ext_list_thead;
1515

16+
#ifdef CONFIG_RISCV_ISA_VENDOR_EXT_THEAD
17+
void disable_xtheadvector(void);
18+
#else
19+
static inline void disable_xtheadvector(void) { }
20+
#endif
21+
1622
#endif

arch/riscv/kernel/cpufeature.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX) __read_mostly;
3939
/* Per-cpu ISA extensions. */
4040
struct riscv_isainfo hart_isa[NR_CPUS];
4141

42+
u32 thead_vlenb_of;
43+
4244
/**
4345
* riscv_isa_extension_base() - Get base extension word
4446
*
@@ -779,6 +781,46 @@ static void __init riscv_fill_vendor_ext_list(int cpu)
779781
}
780782
}
781783

784+
static int has_thead_homogeneous_vlenb(void)
785+
{
786+
int cpu;
787+
u32 prev_vlenb = 0;
788+
u32 vlenb;
789+
790+
/* Ignore thead,vlenb property if xtheavector is not enabled in the kernel */
791+
if (!IS_ENABLED(CONFIG_RISCV_ISA_XTHEADVECTOR))
792+
return 0;
793+
794+
for_each_possible_cpu(cpu) {
795+
struct device_node *cpu_node;
796+
797+
cpu_node = of_cpu_device_node_get(cpu);
798+
if (!cpu_node) {
799+
pr_warn("Unable to find cpu node\n");
800+
return -ENOENT;
801+
}
802+
803+
if (of_property_read_u32(cpu_node, "thead,vlenb", &vlenb)) {
804+
of_node_put(cpu_node);
805+
806+
if (prev_vlenb)
807+
return -ENOENT;
808+
continue;
809+
}
810+
811+
if (prev_vlenb && vlenb != prev_vlenb) {
812+
of_node_put(cpu_node);
813+
return -ENOENT;
814+
}
815+
816+
prev_vlenb = vlenb;
817+
of_node_put(cpu_node);
818+
}
819+
820+
thead_vlenb_of = vlenb;
821+
return 0;
822+
}
823+
782824
static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)
783825
{
784826
unsigned int cpu;
@@ -832,6 +874,12 @@ static int __init riscv_fill_hwcap_from_ext_list(unsigned long *isa2hwcap)
832874
riscv_fill_vendor_ext_list(cpu);
833875
}
834876

877+
if (riscv_isa_vendor_extension_available(THEAD_VENDOR_ID, XTHEADVECTOR) &&
878+
has_thead_homogeneous_vlenb() < 0) {
879+
pr_warn("Unsupported heterogeneous vlenb detected, vector extension disabled.\n");
880+
disable_xtheadvector();
881+
}
882+
835883
if (bitmap_empty(riscv_isa, RISCV_ISA_EXT_MAX))
836884
return -ENOENT;
837885

arch/riscv/kernel/vector.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ int riscv_v_setup_vsize(void)
3333
{
3434
unsigned long this_vsize;
3535

36-
/* There are 32 vector registers with vlenb length. */
36+
/*
37+
* There are 32 vector registers with vlenb length.
38+
*
39+
* If the thead,vlenb property was provided by the firmware, use that
40+
* instead of probing the CSRs.
41+
*/
42+
if (thead_vlenb_of) {
43+
riscv_v_vsize = thead_vlenb_of * 32;
44+
return 0;
45+
}
46+
3747
riscv_v_enable();
3848
this_vsize = csr_read(CSR_VLENB) * 32;
3949
riscv_v_disable();

arch/riscv/kernel/vendor_extensions/thead.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <asm/vendor_extensions/thead.h>
66

77
#include <linux/array_size.h>
8+
#include <linux/cpumask.h>
89
#include <linux/types.h>
910

1011
/* All T-Head vendor extensions supported in Linux */
@@ -16,3 +17,13 @@ struct riscv_isa_vendor_ext_data_list riscv_isa_vendor_ext_list_thead = {
1617
.ext_data_count = ARRAY_SIZE(riscv_isa_vendor_ext_thead),
1718
.ext_data = riscv_isa_vendor_ext_thead,
1819
};
20+
21+
void disable_xtheadvector(void)
22+
{
23+
int cpu;
24+
25+
for_each_possible_cpu(cpu)
26+
clear_bit(RISCV_ISA_VENDOR_EXT_XTHEADVECTOR, riscv_isa_vendor_ext_list_thead.per_hart_isa_bitmap[cpu].isa);
27+
28+
clear_bit(RISCV_ISA_VENDOR_EXT_XTHEADVECTOR, riscv_isa_vendor_ext_list_thead.all_harts_isa_bitmap.isa);
29+
}

0 commit comments

Comments
 (0)