blob: 143717e33970526645ca65eeb075357a304463b7 [file] [log] [blame]
David Cunadoce88eee2017-10-20 11:30:57 +01001/*
Elizabeth Ho4fc00d22023-07-18 14:10:25 +01002 * Copyright (c) 2017-2023, Arm Limited and Contributors. All rights reserved.
David Cunadoce88eee2017-10-20 11:30:57 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00007#include <stdbool.h>
8
David Cunadoce88eee2017-10-20 11:30:57 +01009#include <arch.h>
10#include <arch_helpers.h>
Mark Brown64869972022-04-20 18:14:32 +010011#include <lib/cassert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <lib/el3_runtime/pubsub.h>
13#include <lib/extensions/sve.h>
David Cunadoce88eee2017-10-20 11:30:57 +010014
Mark Brown64869972022-04-20 18:14:32 +010015CASSERT(SVE_VECTOR_LEN <= 2048, assert_sve_vl_too_long);
16CASSERT(SVE_VECTOR_LEN >= 128, assert_sve_vl_too_short);
17CASSERT((SVE_VECTOR_LEN % 128) == 0, assert_sve_vl_granule);
18
Max Shvetsovc4502772021-03-22 11:59:37 +000019/*
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
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010025void sve_enable_per_world(per_world_context_t *per_world_ctx)
David Cunadoce88eee2017-10-20 11:30:57 +010026{
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +010027 u_register_t cptr_el3;
28
Max Shvetsovc4502772021-03-22 11:59:37 +000029 /* Enable access to SVE functionality for all ELs. */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010030 cptr_el3 = per_world_ctx->ctx_cptr_el3;
Max Shvetsovc4502772021-03-22 11:59:37 +000031 cptr_el3 = (cptr_el3 | CPTR_EZ_BIT) & ~(TFP_BIT);
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010032 per_world_ctx->ctx_cptr_el3 = cptr_el3;
David Cunadoce88eee2017-10-20 11:30:57 +010033
Mark Brown64869972022-04-20 18:14:32 +010034 /* Restrict maximum SVE vector length (SVE_VECTOR_LEN+1) * 128. */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010035 per_world_ctx->ctx_zcr_el3 = (ZCR_EL3_LEN_MASK & CONVERT_SVE_LENGTH(SVE_VECTOR_LEN));
David Cunadoce88eee2017-10-20 11:30:57 +010036}
johpow019baade32021-07-08 14:14:00 -050037
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000038void 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
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010048void sve_disable_per_world(per_world_context_t *per_world_ctx)
johpow019baade32021-07-08 14:14:00 -050049{
50 u_register_t reg;
johpow019baade32021-07-08 14:14:00 -050051
52 /* Disable SVE and FPU since they share registers. */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010053 reg = per_world_ctx->ctx_cptr_el3;
johpow019baade32021-07-08 14:14:00 -050054 reg &= ~CPTR_EZ_BIT; /* Trap SVE */
55 reg |= TFP_BIT; /* Trap FPU/SIMD */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010056 per_world_ctx->ctx_cptr_el3 = reg;
johpow019baade32021-07-08 14:14:00 -050057}