fpsimd.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2012 ARM Ltd.
  4. */
  5. #ifndef __ASM_FP_H
  6. #define __ASM_FP_H
  7. #include <asm/errno.h>
  8. #include <asm/percpu.h>
  9. #include <asm/ptrace.h>
  10. #include <asm/processor.h>
  11. #include <asm/sigcontext.h>
  12. #include <asm/sysreg.h>
  13. #ifndef __ASSEMBLY__
  14. #include <linux/bitmap.h>
  15. #include <linux/build_bug.h>
  16. #include <linux/bug.h>
  17. #include <linux/cache.h>
  18. #include <linux/init.h>
  19. #include <linux/stddef.h>
  20. #include <linux/types.h>
  21. /* Masks for extracting the FPSR and FPCR from the FPSCR */
  22. #define VFP_FPSCR_STAT_MASK 0xf800009f
  23. #define VFP_FPSCR_CTRL_MASK 0x07f79f00
  24. /*
  25. * The VFP state has 32x64-bit registers and a single 32-bit
  26. * control/status register.
  27. */
  28. #define VFP_STATE_SIZE ((32 * 8) + 4)
  29. static inline unsigned long cpacr_save_enable_kernel_sve(void)
  30. {
  31. unsigned long old = read_sysreg(cpacr_el1);
  32. unsigned long set = CPACR_EL1_FPEN_EL1EN | CPACR_EL1_ZEN_EL1EN;
  33. write_sysreg(old | set, cpacr_el1);
  34. isb();
  35. return old;
  36. }
  37. static inline unsigned long cpacr_save_enable_kernel_sme(void)
  38. {
  39. unsigned long old = read_sysreg(cpacr_el1);
  40. unsigned long set = CPACR_EL1_FPEN_EL1EN | CPACR_EL1_SMEN_EL1EN;
  41. write_sysreg(old | set, cpacr_el1);
  42. isb();
  43. return old;
  44. }
  45. static inline void cpacr_restore(unsigned long cpacr)
  46. {
  47. write_sysreg(cpacr, cpacr_el1);
  48. isb();
  49. }
  50. /*
  51. * When we defined the maximum SVE vector length we defined the ABI so
  52. * that the maximum vector length included all the reserved for future
  53. * expansion bits in ZCR rather than those just currently defined by
  54. * the architecture. Using this length to allocate worst size buffers
  55. * results in excessively large allocations, and this effect is even
  56. * more pronounced for SME due to ZA. Define more suitable VLs for
  57. * these situations.
  58. */
  59. #define ARCH_SVE_VQ_MAX ((ZCR_ELx_LEN_MASK >> ZCR_ELx_LEN_SHIFT) + 1)
  60. #define SME_VQ_MAX ((SMCR_ELx_LEN_MASK >> SMCR_ELx_LEN_SHIFT) + 1)
  61. struct task_struct;
  62. extern void fpsimd_save_state(struct user_fpsimd_state *state);
  63. extern void fpsimd_load_state(struct user_fpsimd_state *state);
  64. extern void fpsimd_thread_switch(struct task_struct *next);
  65. extern void fpsimd_flush_thread(void);
  66. extern void fpsimd_signal_preserve_current_state(void);
  67. extern void fpsimd_preserve_current_state(void);
  68. extern void fpsimd_restore_current_state(void);
  69. extern void fpsimd_update_current_state(struct user_fpsimd_state const *state);
  70. extern void fpsimd_kvm_prepare(void);
  71. struct cpu_fp_state {
  72. struct user_fpsimd_state *st;
  73. void *sve_state;
  74. void *sme_state;
  75. u64 *svcr;
  76. u64 *fpmr;
  77. unsigned int sve_vl;
  78. unsigned int sme_vl;
  79. enum fp_type *fp_type;
  80. enum fp_type to_save;
  81. };
  82. DECLARE_PER_CPU(struct cpu_fp_state, fpsimd_last_state);
  83. extern void fpsimd_bind_state_to_cpu(struct cpu_fp_state *fp_state);
  84. extern void fpsimd_flush_task_state(struct task_struct *target);
  85. extern void fpsimd_save_and_flush_cpu_state(void);
  86. static inline bool thread_sm_enabled(struct thread_struct *thread)
  87. {
  88. return system_supports_sme() && (thread->svcr & SVCR_SM_MASK);
  89. }
  90. static inline bool thread_za_enabled(struct thread_struct *thread)
  91. {
  92. return system_supports_sme() && (thread->svcr & SVCR_ZA_MASK);
  93. }
  94. /* Maximum VL that SVE/SME VL-agnostic software can transparently support */
  95. #define VL_ARCH_MAX 0x100
  96. /* Offset of FFR in the SVE register dump */
  97. static inline size_t sve_ffr_offset(int vl)
  98. {
  99. return SVE_SIG_FFR_OFFSET(sve_vq_from_vl(vl)) - SVE_SIG_REGS_OFFSET;
  100. }
  101. static inline void *sve_pffr(struct thread_struct *thread)
  102. {
  103. unsigned int vl;
  104. if (system_supports_sme() && thread_sm_enabled(thread))
  105. vl = thread_get_sme_vl(thread);
  106. else
  107. vl = thread_get_sve_vl(thread);
  108. return (char *)thread->sve_state + sve_ffr_offset(vl);
  109. }
  110. static inline void *thread_zt_state(struct thread_struct *thread)
  111. {
  112. /* The ZT register state is stored immediately after the ZA state */
  113. unsigned int sme_vq = sve_vq_from_vl(thread_get_sme_vl(thread));
  114. return thread->sme_state + ZA_SIG_REGS_SIZE(sme_vq);
  115. }
  116. extern void sve_save_state(void *state, u32 *pfpsr, int save_ffr);
  117. extern void sve_load_state(void const *state, u32 const *pfpsr,
  118. int restore_ffr);
  119. extern void sve_flush_live(bool flush_ffr, unsigned long vq_minus_1);
  120. extern unsigned int sve_get_vl(void);
  121. extern void sve_set_vq(unsigned long vq_minus_1);
  122. extern void sme_set_vq(unsigned long vq_minus_1);
  123. extern void sme_save_state(void *state, int zt);
  124. extern void sme_load_state(void const *state, int zt);
  125. struct arm64_cpu_capabilities;
  126. extern void cpu_enable_fpsimd(const struct arm64_cpu_capabilities *__unused);
  127. extern void cpu_enable_sve(const struct arm64_cpu_capabilities *__unused);
  128. extern void cpu_enable_sme(const struct arm64_cpu_capabilities *__unused);
  129. extern void cpu_enable_sme2(const struct arm64_cpu_capabilities *__unused);
  130. extern void cpu_enable_fa64(const struct arm64_cpu_capabilities *__unused);
  131. extern void cpu_enable_fpmr(const struct arm64_cpu_capabilities *__unused);
  132. /*
  133. * Helpers to translate bit indices in sve_vq_map to VQ values (and
  134. * vice versa). This allows find_next_bit() to be used to find the
  135. * _maximum_ VQ not exceeding a certain value.
  136. */
  137. static inline unsigned int __vq_to_bit(unsigned int vq)
  138. {
  139. return SVE_VQ_MAX - vq;
  140. }
  141. static inline unsigned int __bit_to_vq(unsigned int bit)
  142. {
  143. return SVE_VQ_MAX - bit;
  144. }
  145. struct vl_info {
  146. enum vec_type type;
  147. const char *name; /* For display purposes */
  148. /* Minimum supported vector length across all CPUs */
  149. int min_vl;
  150. /* Maximum supported vector length across all CPUs */
  151. int max_vl;
  152. int max_virtualisable_vl;
  153. /*
  154. * Set of available vector lengths,
  155. * where length vq encoded as bit __vq_to_bit(vq):
  156. */
  157. DECLARE_BITMAP(vq_map, SVE_VQ_MAX);
  158. /* Set of vector lengths present on at least one cpu: */
  159. DECLARE_BITMAP(vq_partial_map, SVE_VQ_MAX);
  160. };
  161. #ifdef CONFIG_ARM64_SVE
  162. extern void sve_alloc(struct task_struct *task, bool flush);
  163. extern void fpsimd_release_task(struct task_struct *task);
  164. extern void fpsimd_sync_to_sve(struct task_struct *task);
  165. extern void fpsimd_force_sync_to_sve(struct task_struct *task);
  166. extern void sve_sync_to_fpsimd(struct task_struct *task);
  167. extern void sve_sync_from_fpsimd_zeropad(struct task_struct *task);
  168. extern int vec_set_vector_length(struct task_struct *task, enum vec_type type,
  169. unsigned long vl, unsigned long flags);
  170. extern int sve_set_current_vl(unsigned long arg);
  171. extern int sve_get_current_vl(void);
  172. static inline void sve_user_disable(void)
  173. {
  174. sysreg_clear_set(cpacr_el1, CPACR_EL1_ZEN_EL0EN, 0);
  175. }
  176. static inline void sve_user_enable(void)
  177. {
  178. sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_ZEN_EL0EN);
  179. }
  180. #define sve_cond_update_zcr_vq(val, reg) \
  181. do { \
  182. u64 __zcr = read_sysreg_s((reg)); \
  183. u64 __new = __zcr & ~ZCR_ELx_LEN_MASK; \
  184. __new |= (val) & ZCR_ELx_LEN_MASK; \
  185. if (__zcr != __new) \
  186. write_sysreg_s(__new, (reg)); \
  187. } while (0)
  188. /*
  189. * Probing and setup functions.
  190. * Calls to these functions must be serialised with one another.
  191. */
  192. enum vec_type;
  193. extern void __init vec_init_vq_map(enum vec_type type);
  194. extern void vec_update_vq_map(enum vec_type type);
  195. extern int vec_verify_vq_map(enum vec_type type);
  196. extern void __init sve_setup(void);
  197. extern __ro_after_init struct vl_info vl_info[ARM64_VEC_MAX];
  198. static inline void write_vl(enum vec_type type, u64 val)
  199. {
  200. u64 tmp;
  201. switch (type) {
  202. #ifdef CONFIG_ARM64_SVE
  203. case ARM64_VEC_SVE:
  204. tmp = read_sysreg_s(SYS_ZCR_EL1) & ~ZCR_ELx_LEN_MASK;
  205. write_sysreg_s(tmp | val, SYS_ZCR_EL1);
  206. break;
  207. #endif
  208. #ifdef CONFIG_ARM64_SME
  209. case ARM64_VEC_SME:
  210. tmp = read_sysreg_s(SYS_SMCR_EL1) & ~SMCR_ELx_LEN_MASK;
  211. write_sysreg_s(tmp | val, SYS_SMCR_EL1);
  212. break;
  213. #endif
  214. default:
  215. WARN_ON_ONCE(1);
  216. break;
  217. }
  218. }
  219. static inline int vec_max_vl(enum vec_type type)
  220. {
  221. return vl_info[type].max_vl;
  222. }
  223. static inline int vec_max_virtualisable_vl(enum vec_type type)
  224. {
  225. return vl_info[type].max_virtualisable_vl;
  226. }
  227. static inline int sve_max_vl(void)
  228. {
  229. return vec_max_vl(ARM64_VEC_SVE);
  230. }
  231. static inline int sve_max_virtualisable_vl(void)
  232. {
  233. return vec_max_virtualisable_vl(ARM64_VEC_SVE);
  234. }
  235. /* Ensure vq >= SVE_VQ_MIN && vq <= SVE_VQ_MAX before calling this function */
  236. static inline bool vq_available(enum vec_type type, unsigned int vq)
  237. {
  238. return test_bit(__vq_to_bit(vq), vl_info[type].vq_map);
  239. }
  240. static inline bool sve_vq_available(unsigned int vq)
  241. {
  242. return vq_available(ARM64_VEC_SVE, vq);
  243. }
  244. size_t sve_state_size(struct task_struct const *task);
  245. #else /* ! CONFIG_ARM64_SVE */
  246. static inline void sve_alloc(struct task_struct *task, bool flush) { }
  247. static inline void fpsimd_release_task(struct task_struct *task) { }
  248. static inline void sve_sync_to_fpsimd(struct task_struct *task) { }
  249. static inline void sve_sync_from_fpsimd_zeropad(struct task_struct *task) { }
  250. static inline int sve_max_virtualisable_vl(void)
  251. {
  252. return 0;
  253. }
  254. static inline int sve_set_current_vl(unsigned long arg)
  255. {
  256. return -EINVAL;
  257. }
  258. static inline int sve_get_current_vl(void)
  259. {
  260. return -EINVAL;
  261. }
  262. static inline int sve_max_vl(void)
  263. {
  264. return -EINVAL;
  265. }
  266. static inline bool sve_vq_available(unsigned int vq) { return false; }
  267. static inline void sve_user_disable(void) { BUILD_BUG(); }
  268. static inline void sve_user_enable(void) { BUILD_BUG(); }
  269. #define sve_cond_update_zcr_vq(val, reg) do { } while (0)
  270. static inline void vec_init_vq_map(enum vec_type t) { }
  271. static inline void vec_update_vq_map(enum vec_type t) { }
  272. static inline int vec_verify_vq_map(enum vec_type t) { return 0; }
  273. static inline void sve_setup(void) { }
  274. static inline size_t sve_state_size(struct task_struct const *task)
  275. {
  276. return 0;
  277. }
  278. #endif /* ! CONFIG_ARM64_SVE */
  279. #ifdef CONFIG_ARM64_SME
  280. static inline void sme_user_disable(void)
  281. {
  282. sysreg_clear_set(cpacr_el1, CPACR_EL1_SMEN_EL0EN, 0);
  283. }
  284. static inline void sme_user_enable(void)
  285. {
  286. sysreg_clear_set(cpacr_el1, 0, CPACR_EL1_SMEN_EL0EN);
  287. }
  288. static inline void sme_smstart_sm(void)
  289. {
  290. asm volatile(__msr_s(SYS_SVCR_SMSTART_SM_EL0, "xzr"));
  291. }
  292. static inline void sme_smstop_sm(void)
  293. {
  294. asm volatile(__msr_s(SYS_SVCR_SMSTOP_SM_EL0, "xzr"));
  295. }
  296. static inline void sme_smstop(void)
  297. {
  298. asm volatile(__msr_s(SYS_SVCR_SMSTOP_SMZA_EL0, "xzr"));
  299. }
  300. extern void __init sme_setup(void);
  301. static inline int sme_max_vl(void)
  302. {
  303. return vec_max_vl(ARM64_VEC_SME);
  304. }
  305. static inline int sme_max_virtualisable_vl(void)
  306. {
  307. return vec_max_virtualisable_vl(ARM64_VEC_SME);
  308. }
  309. extern void sme_alloc(struct task_struct *task, bool flush);
  310. extern unsigned int sme_get_vl(void);
  311. extern int sme_set_current_vl(unsigned long arg);
  312. extern int sme_get_current_vl(void);
  313. extern void sme_suspend_exit(void);
  314. /*
  315. * Return how many bytes of memory are required to store the full SME
  316. * specific state for task, given task's currently configured vector
  317. * length.
  318. */
  319. static inline size_t sme_state_size(struct task_struct const *task)
  320. {
  321. unsigned int vl = task_get_sme_vl(task);
  322. size_t size;
  323. size = ZA_SIG_REGS_SIZE(sve_vq_from_vl(vl));
  324. if (system_supports_sme2())
  325. size += ZT_SIG_REG_SIZE;
  326. return size;
  327. }
  328. #else
  329. static inline void sme_user_disable(void) { BUILD_BUG(); }
  330. static inline void sme_user_enable(void) { BUILD_BUG(); }
  331. static inline void sme_smstart_sm(void) { }
  332. static inline void sme_smstop_sm(void) { }
  333. static inline void sme_smstop(void) { }
  334. static inline void sme_alloc(struct task_struct *task, bool flush) { }
  335. static inline void sme_setup(void) { }
  336. static inline unsigned int sme_get_vl(void) { return 0; }
  337. static inline int sme_max_vl(void) { return 0; }
  338. static inline int sme_max_virtualisable_vl(void) { return 0; }
  339. static inline int sme_set_current_vl(unsigned long arg) { return -EINVAL; }
  340. static inline int sme_get_current_vl(void) { return -EINVAL; }
  341. static inline void sme_suspend_exit(void) { }
  342. static inline size_t sme_state_size(struct task_struct const *task)
  343. {
  344. return 0;
  345. }
  346. #endif /* ! CONFIG_ARM64_SME */
  347. /* For use by EFI runtime services calls only */
  348. extern void __efi_fpsimd_begin(void);
  349. extern void __efi_fpsimd_end(void);
  350. #endif
  351. #endif