1 /*
2  * Copyright (c) 2017-2023, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stdbool.h>
8 
9 #include <arch.h>
10 #include <arch_helpers.h>
11 #include <lib/cassert.h>
12 #include <lib/el3_runtime/pubsub.h>
13 #include <lib/extensions/sve.h>
14 
15 CASSERT(SVE_VECTOR_LEN <= 2048, assert_sve_vl_too_long);
16 CASSERT(SVE_VECTOR_LEN >= 128, assert_sve_vl_too_short);
17 CASSERT((SVE_VECTOR_LEN % 128) == 0, assert_sve_vl_granule);
18 
19 /*
20  * Converts SVE vector size restriction in bytes to LEN according to ZCR_EL3 documentation.
21  * VECTOR_SIZE = (LEN+1) * 128
22  */
23 #define CONVERT_SVE_LENGTH(x)	(((x / 128) - 1))
24 
sve_enable_per_world(per_world_context_t * per_world_ctx)25 void sve_enable_per_world(per_world_context_t *per_world_ctx)
26 {
27 	u_register_t cptr_el3;
28 
29 	/* Enable access to SVE functionality for all ELs. */
30 	cptr_el3 = per_world_ctx->ctx_cptr_el3;
31 	cptr_el3 = (cptr_el3 | CPTR_EZ_BIT) & ~(TFP_BIT);
32 	per_world_ctx->ctx_cptr_el3 = cptr_el3;
33 
34 	/* Restrict maximum SVE vector length (SVE_VECTOR_LEN+1) * 128. */
35 	per_world_ctx->ctx_zcr_el3 = (ZCR_EL3_LEN_MASK & CONVERT_SVE_LENGTH(SVE_VECTOR_LEN));
36 }
37 
sve_init_el2_unused(void)38 void sve_init_el2_unused(void)
39 {
40 	/*
41 	 * CPTR_EL2.TFP: Set to zero so that Non-secure accesses to Advanced
42 	 *  SIMD and floating-point functionality from both Execution states do
43 	 *  not trap to EL2.
44 	 */
45 	write_cptr_el2(read_cptr_el2() & ~CPTR_EL2_TFP_BIT);
46 }
47 
sve_disable_per_world(per_world_context_t * per_world_ctx)48 void sve_disable_per_world(per_world_context_t *per_world_ctx)
49 {
50 	u_register_t reg;
51 
52 	/* Disable SVE and FPU since they share registers. */
53 	reg = per_world_ctx->ctx_cptr_el3;
54 	reg &= ~CPTR_EZ_BIT;	/* Trap SVE */
55 	reg |= TFP_BIT;		/* Trap FPU/SIMD */
56 	per_world_ctx->ctx_cptr_el3 = reg;
57 }
58