1 /* SPDX-License-Identifier: GPL-2.0-only */ 2 /* 3 * Copyright 2022-2024 Rivos, Inc 4 */ 5 6 #ifndef _ASM_CPUFEATURE_H 7 #define _ASM_CPUFEATURE_H 8 9 #include <linux/bitmap.h> 10 #include <linux/jump_label.h> 11 #include <linux/workqueue.h> 12 #include <linux/kconfig.h> 13 #include <linux/percpu-defs.h> 14 #include <linux/threads.h> 15 #include <asm/hwcap.h> 16 #include <asm/cpufeature-macros.h> 17 18 /* 19 * These are probed via a device_initcall(), via either the SBI or directly 20 * from the corresponding CSRs. 21 */ 22 struct riscv_cpuinfo { 23 unsigned long mvendorid; 24 unsigned long marchid; 25 unsigned long mimpid; 26 }; 27 28 struct riscv_isainfo { 29 DECLARE_BITMAP(isa, RISCV_ISA_EXT_MAX); 30 }; 31 32 DECLARE_PER_CPU(struct riscv_cpuinfo, riscv_cpuinfo); 33 34 extern const struct seq_operations cpuinfo_op; 35 36 /* Per-cpu ISA extensions. */ 37 extern struct riscv_isainfo hart_isa[NR_CPUS]; 38 39 extern DECLARE_BITMAP(riscv_isa, RISCV_ISA_EXT_MAX); 40 41 extern u32 thead_vlenb_of; 42 43 void __init riscv_user_isa_enable(void); 44 45 void riscv_clear_hypervisor_csr(void); 46 47 #define _RISCV_ISA_EXT_DATA(_name, _id, _subset_exts, _subset_exts_size, _validate) { \ 48 .name = #_name, \ 49 .property = #_name, \ 50 .id = _id, \ 51 .subset_ext_ids = _subset_exts, \ 52 .subset_ext_size = _subset_exts_size, \ 53 .validate = _validate \ 54 } 55 56 #define __RISCV_ISA_EXT_DATA(_name, _id) _RISCV_ISA_EXT_DATA(_name, _id, NULL, 0, NULL) 57 58 #define __RISCV_ISA_EXT_DATA_VALIDATE(_name, _id, _validate) \ 59 _RISCV_ISA_EXT_DATA(_name, _id, NULL, 0, _validate) 60 61 /* Used to declare pure "lasso" extension (Zk for instance) */ 62 #define __RISCV_ISA_EXT_BUNDLE(_name, _bundled_exts) \ 63 _RISCV_ISA_EXT_DATA(_name, RISCV_ISA_EXT_INVALID, _bundled_exts, \ 64 ARRAY_SIZE(_bundled_exts), NULL) 65 #define __RISCV_ISA_EXT_BUNDLE_VALIDATE(_name, _bundled_exts, _validate) \ 66 _RISCV_ISA_EXT_DATA(_name, RISCV_ISA_EXT_INVALID, _bundled_exts, \ 67 ARRAY_SIZE(_bundled_exts), _validate) 68 69 /* Used to declare extensions that are a superset of other extensions (Zvbb for instance) */ 70 #define __RISCV_ISA_EXT_SUPERSET(_name, _id, _sub_exts) \ 71 _RISCV_ISA_EXT_DATA(_name, _id, _sub_exts, ARRAY_SIZE(_sub_exts), NULL) 72 #define __RISCV_ISA_EXT_SUPERSET_VALIDATE(_name, _id, _sub_exts, _validate) \ 73 _RISCV_ISA_EXT_DATA(_name, _id, _sub_exts, ARRAY_SIZE(_sub_exts), _validate) 74 75 bool __init check_unaligned_access_emulated_all_cpus(void); 76 void unaligned_access_init(void); 77 int cpu_online_unaligned_access_init(unsigned int cpu); 78 #if defined(CONFIG_RISCV_SCALAR_MISALIGNED) 79 void unaligned_emulation_finish(void); 80 bool unaligned_ctl_available(void); 81 #else 82 static inline bool unaligned_ctl_available(void) 83 { 84 return false; 85 } 86 #endif 87 88 #if defined(CONFIG_RISCV_MISALIGNED) 89 DECLARE_PER_CPU(long, misaligned_access_speed); 90 bool misaligned_traps_can_delegate(void); 91 #else 92 static inline bool misaligned_traps_can_delegate(void) 93 { 94 return false; 95 } 96 #endif 97 98 bool __init check_vector_unaligned_access_emulated_all_cpus(void); 99 #if defined(CONFIG_RISCV_VECTOR_MISALIGNED) 100 void check_vector_unaligned_access_emulated(struct work_struct *work __always_unused); 101 DECLARE_PER_CPU(long, vector_misaligned_access); 102 #endif 103 104 #if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS) 105 DECLARE_STATIC_KEY_FALSE(fast_unaligned_access_speed_key); 106 107 static __always_inline bool has_fast_unaligned_accesses(void) 108 { 109 return static_branch_likely(&fast_unaligned_access_speed_key); 110 } 111 #else 112 static __always_inline bool has_fast_unaligned_accesses(void) 113 { 114 if (IS_ENABLED(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)) 115 return true; 116 else 117 return false; 118 } 119 #endif 120 121 unsigned long riscv_get_elf_hwcap(void); 122 123 struct riscv_isa_ext_data { 124 const unsigned int id; 125 const char *name; 126 const char *property; 127 const unsigned int *subset_ext_ids; 128 const unsigned int subset_ext_size; 129 int (*validate)(const struct riscv_isa_ext_data *data, const unsigned long *isa_bitmap); 130 }; 131 132 extern const struct riscv_isa_ext_data riscv_isa_ext[]; 133 extern const size_t riscv_isa_ext_count; 134 extern bool riscv_isa_fallback; 135 136 unsigned long riscv_isa_extension_base(const unsigned long *isa_bitmap); 137 static __always_inline bool riscv_cpu_has_extension_likely(int cpu, const unsigned long ext) 138 { 139 compiletime_assert(ext < RISCV_ISA_EXT_MAX, "ext must be < RISCV_ISA_EXT_MAX"); 140 141 if (__riscv_has_extension_likely(STANDARD_EXT, ext)) 142 return true; 143 144 return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); 145 } 146 147 static __always_inline bool riscv_cpu_has_extension_unlikely(int cpu, const unsigned long ext) 148 { 149 compiletime_assert(ext < RISCV_ISA_EXT_MAX, "ext must be < RISCV_ISA_EXT_MAX"); 150 151 if (__riscv_has_extension_unlikely(STANDARD_EXT, ext)) 152 return true; 153 154 return __riscv_isa_extension_available(hart_isa[cpu].isa, ext); 155 } 156 157 static inline bool cpu_supports_shadow_stack(void) 158 { 159 return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && 160 riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICFISS)); 161 } 162 163 static inline bool cpu_supports_indirect_br_lp_instr(void) 164 { 165 return (IS_ENABLED(CONFIG_RISCV_USER_CFI) && 166 riscv_has_extension_unlikely(RISCV_ISA_EXT_ZICFILP)); 167 } 168 169 #endif 170