blob: 5670afcdaddadfe7647a17795fa621f5497eb579 [file] [log] [blame]
Max Shvetsov959be332021-03-16 14:18:13 +00001/*
Arunachalam Ganapathy0bbdc2d2023-04-05 15:30:18 +01002 * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
Max Shvetsov959be332021-03-16 14:18:13 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#ifndef SVE_H
8#define SVE_H
9
Arunachalam Ganapathy0bbdc2d2023-04-05 15:30:18 +010010#include <arch.h>
Arunachalam Ganapathyc1136a82023-04-12 15:24:44 +010011#include <stdlib.h> /* for rand() */
Arunachalam Ganapathy5b68e202023-06-06 16:31:19 +010012#include <lib/extensions/sme.h>
Arunachalam Ganapathy0bbdc2d2023-04-05 15:30:18 +010013
Max Shvetsov959be332021-03-16 14:18:13 +000014#define fill_sve_helper(num) "ldr z"#num", [%0, #"#num", MUL VL];"
15#define read_sve_helper(num) "str z"#num", [%0, #"#num", MUL VL];"
16
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010017#define fill_sve_p_helper(num) "ldr p"#num", [%0, #"#num", MUL VL];"
18#define read_sve_p_helper(num) "str p"#num", [%0, #"#num", MUL VL];"
19
Olivier Deprez569be402022-07-08 10:24:39 +020020/*
21 * Max. vector length permitted by the architecture:
22 * SVE: 2048 bits = 256 bytes
23 */
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010024#define SVE_VECTOR_LEN_BYTES (256U)
25#define SVE_NUM_VECTORS (32U)
26
27/* Max size of one predicate register is 1/8 of Z register */
28#define SVE_P_REG_LEN_BYTES (SVE_VECTOR_LEN_BYTES / 8U)
29#define SVE_NUM_P_REGS (16U)
30
31/* Max size of one FFR register is 1/8 of Z register */
32#define SVE_FFR_REG_LEN_BYTES (SVE_VECTOR_LEN_BYTES / 8U)
33#define SVE_NUM_FFR_REGS (1U)
Olivier Deprez569be402022-07-08 10:24:39 +020034
Arunachalam Ganapathy5270d012023-04-19 14:53:42 +010035#define SVE_VQ_ARCH_MIN (0U)
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010036#define SVE_VQ_ARCH_MAX ((1U << ZCR_EL2_SVE_VL_WIDTH) - 1U)
Arunachalam Ganapathy0bbdc2d2023-04-05 15:30:18 +010037
38/* convert SVE VL in bytes to VQ */
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010039#define SVE_VL_TO_VQ(vl_bytes) (((vl_bytes) >> 4U) - 1U)
Arunachalam Ganapathy0bbdc2d2023-04-05 15:30:18 +010040
41/* convert SVE VQ to bits */
42#define SVE_VQ_TO_BITS(vq) (((vq) + 1U) << 7U)
43
Arunachalam Ganapathy5270d012023-04-19 14:53:42 +010044/* convert SVE VQ to bytes */
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010045#define SVE_VQ_TO_BYTES(vq) (SVE_VQ_TO_BITS(vq) / 8U)
Arunachalam Ganapathy5270d012023-04-19 14:53:42 +010046
Arunachalam Ganapathyc1136a82023-04-12 15:24:44 +010047/* get a random SVE VQ b/w 0 to SVE_VQ_ARCH_MAX */
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010048#define SVE_GET_RANDOM_VQ (rand() % (SVE_VQ_ARCH_MAX + 1U))
Arunachalam Ganapathyc1136a82023-04-12 15:24:44 +010049
Kathleen Capellac59184c2022-08-23 19:09:41 -040050#ifndef __ASSEMBLY__
51
Arunachalam Ganapathy03589972023-08-30 11:04:51 +010052typedef uint8_t sve_z_regs_t[SVE_NUM_VECTORS * SVE_VECTOR_LEN_BYTES]
53 __aligned(16);
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010054typedef uint8_t sve_p_regs_t[SVE_NUM_P_REGS * SVE_P_REG_LEN_BYTES]
55 __aligned(16);
56typedef uint8_t sve_ffr_regs_t[SVE_NUM_FFR_REGS * SVE_FFR_REG_LEN_BYTES]
57 __aligned(16);
Olivier Deprez569be402022-07-08 10:24:39 +020058
Arunachalam Ganapathy0bbdc2d2023-04-05 15:30:18 +010059void sve_config_vq(uint8_t sve_vq);
60uint32_t sve_probe_vl(uint8_t sve_max_vq);
Arunachalam Ganapathy03589972023-08-30 11:04:51 +010061
62void sve_z_regs_write(const sve_z_regs_t *z_regs);
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010063void sve_z_regs_write_rand(sve_z_regs_t *z_regs);
Arunachalam Ganapathy03589972023-08-30 11:04:51 +010064void sve_z_regs_read(sve_z_regs_t *z_regs);
Arunachalam Ganapathyfa05bd92023-08-30 14:36:53 +010065uint64_t sve_z_regs_compare(const sve_z_regs_t *s1, const sve_z_regs_t *s2);
66
67void sve_p_regs_write(const sve_p_regs_t *p_regs);
68void sve_p_regs_write_rand(sve_p_regs_t *p_regs);
69void sve_p_regs_read(sve_p_regs_t *p_regs);
70uint64_t sve_p_regs_compare(const sve_p_regs_t *s1, const sve_p_regs_t *s2);
71
72void sve_ffr_regs_write(const sve_ffr_regs_t *ffr_regs);
73void sve_ffr_regs_write_rand(sve_ffr_regs_t *ffr_regs);
74void sve_ffr_regs_read(sve_ffr_regs_t *ffr_regs);
75uint64_t sve_ffr_regs_compare(const sve_ffr_regs_t *s1,
76 const sve_ffr_regs_t *s2);
Arunachalam Ganapathyd179ddc2023-04-12 10:41:42 +010077
78/* Assembly routines */
79bool sve_subtract_arrays_interleaved(int *dst_array, int *src_array1,
80 int *src_array2, int array_size,
81 bool (*world_switch_cb)(void));
82
83void sve_subtract_arrays(int *dst_array, int *src_array1, int *src_array2,
84 int array_size);
Arunachalam Ganapathy0bbdc2d2023-04-05 15:30:18 +010085
Olivier Deprez569be402022-07-08 10:24:39 +020086#ifdef __aarch64__
87
88/* Returns the SVE implemented VL in bytes (constrained by ZCR_EL3.LEN) */
Arunachalam Ganapathy03589972023-08-30 11:04:51 +010089static inline uint64_t sve_rdvl_1(void)
Olivier Deprez569be402022-07-08 10:24:39 +020090{
91 uint64_t vl;
92
93 __asm__ volatile(
94 ".arch_extension sve\n"
95 "rdvl %0, #1;"
96 ".arch_extension nosve\n"
97 : "=r" (vl)
98 );
99
100 return vl;
101}
102
103#endif /* __aarch64__ */
Kathleen Capellac59184c2022-08-23 19:09:41 -0400104#endif /* __ASSEMBLY__ */
Olivier Deprez569be402022-07-08 10:24:39 +0200105#endif /* SVE_H */