blob: 4e18cdf501143c2c0d80d40619a9de386b967e46 [file] [log] [blame]
David Cunadoce88eee2017-10-20 11:30:57 +01001/*
Boyan Karatotev90b7b752024-11-15 15:03:02 +00002 * Copyright (c) 2017-2025, 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
Boyan Karatotev90b7b752024-11-15 15:03:02 +000025void sve_init_el3(void)
26{
27 /* Restrict maximum SVE vector length (SVE_VECTOR_LEN+1) * 128. */
28 write_zcr_el3(ZCR_EL3_LEN_MASK & CONVERT_SVE_LENGTH(SVE_VECTOR_LEN));
29}
30
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010031void sve_enable_per_world(per_world_context_t *per_world_ctx)
David Cunadoce88eee2017-10-20 11:30:57 +010032{
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +010033 u_register_t cptr_el3;
34
Max Shvetsovc4502772021-03-22 11:59:37 +000035 /* Enable access to SVE functionality for all ELs. */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010036 cptr_el3 = per_world_ctx->ctx_cptr_el3;
Max Shvetsovc4502772021-03-22 11:59:37 +000037 cptr_el3 = (cptr_el3 | CPTR_EZ_BIT) & ~(TFP_BIT);
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010038 per_world_ctx->ctx_cptr_el3 = cptr_el3;
David Cunadoce88eee2017-10-20 11:30:57 +010039}
johpow019baade32021-07-08 14:14:00 -050040
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000041void sve_init_el2_unused(void)
42{
43 /*
44 * CPTR_EL2.TFP: Set to zero so that Non-secure accesses to Advanced
45 * SIMD and floating-point functionality from both Execution states do
46 * not trap to EL2.
47 */
48 write_cptr_el2(read_cptr_el2() & ~CPTR_EL2_TFP_BIT);
49}
50
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010051void sve_disable_per_world(per_world_context_t *per_world_ctx)
johpow019baade32021-07-08 14:14:00 -050052{
53 u_register_t reg;
johpow019baade32021-07-08 14:14:00 -050054
55 /* Disable SVE and FPU since they share registers. */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010056 reg = per_world_ctx->ctx_cptr_el3;
johpow019baade32021-07-08 14:14:00 -050057 reg &= ~CPTR_EZ_BIT; /* Trap SVE */
58 reg |= TFP_BIT; /* Trap FPU/SIMD */
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010059 per_world_ctx->ctx_cptr_el3 = reg;
johpow019baade32021-07-08 14:14:00 -050060}