| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
#include <string.h> |
| 4 |
#include <sys/auxv.h> |
| 5 |
#include <sys/utsname.h> |
| 6 |
|
| 7 |
#include "../../zbuild.h" |
| 8 |
#include "riscv_features.h" |
| 9 |
|
| 10 |
#define ISA_V_HWCAP (1 << ('v' - 'a')) |
| 11 |
|
| 12 |
int Z_INTERNAL is_kernel_version_greater_or_equal_to_6_5() { |
| 13 |
struct utsname buffer; |
| 14 |
uname(&buffer); |
| 15 |
|
| 16 |
int major, minor; |
| 17 |
if (sscanf(buffer.release, "%d.%d", &major, &minor) != 2) { |
| 18 |
// Something bad with uname() |
| 19 |
return 0; |
| 20 |
} |
| 21 |
|
| 22 |
if (major > 6 || major == 6 && minor >= 5) |
| 23 |
return 1; |
| 24 |
return 0; |
| 25 |
} |
| 26 |
|
| 27 |
void Z_INTERNAL riscv_check_features_compile_time(struct riscv_cpu_features *features) { |
| 28 |
#if defined(__riscv_v) && defined(__linux__) |
| 29 |
features->has_rvv = 1; |
| 30 |
#else |
| 31 |
features->has_rvv = 0; |
| 32 |
#endif |
| 33 |
} |
| 34 |
|
| 35 |
void Z_INTERNAL riscv_check_features_runtime(struct riscv_cpu_features *features) { |
| 36 |
unsigned long hw_cap = getauxval(AT_HWCAP); |
| 37 |
features->has_rvv = hw_cap & ISA_V_HWCAP; |
| 38 |
} |
| 39 |
|
| 40 |
void Z_INTERNAL riscv_check_features(struct riscv_cpu_features *features) { |
| 41 |
if (is_kernel_version_greater_or_equal_to_6_5()) |
| 42 |
riscv_check_features_runtime(features); |
| 43 |
else |
| 44 |
riscv_check_features_compile_time(features); |
| 45 |
} |