blob: f7dcc767aa252d6030fe0bda39a7095fc2ebabd7 [file] [log] [blame]
David Cunadoce88eee2017-10-20 11:30:57 +01001/*
Mark Brown64869972022-04-20 18:14:32 +01002 * Copyright (c) 2017-2022, 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
25static bool sve_supported(void)
David Cunadoce88eee2017-10-20 11:30:57 +010026{
27 uint64_t features;
28
29 features = read_id_aa64pfr0_el1() >> ID_AA64PFR0_SVE_SHIFT;
Antonio Nino Diaz033b4bb2018-10-25 16:52:26 +010030 return (features & ID_AA64PFR0_SVE_MASK) == 1U;
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000031}
David Cunadoce88eee2017-10-20 11:30:57 +010032
Max Shvetsovc4502772021-03-22 11:59:37 +000033void sve_enable(cpu_context_t *context)
David Cunadoce88eee2017-10-20 11:30:57 +010034{
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +010035 u_register_t cptr_el3;
36
Max Shvetsovc4502772021-03-22 11:59:37 +000037 if (!sve_supported()) {
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000038 return;
Max Shvetsovc4502772021-03-22 11:59:37 +000039 }
Dimitris Papastamos5e8cd792018-02-19 14:52:19 +000040
Arunachalam Ganapathycac7d162021-07-08 09:35:57 +010041 cptr_el3 = read_ctx_reg(get_el3state_ctx(context), CTX_CPTR_EL3);
David Cunadoce88eee2017-10-20 11:30:57 +010042
Max Shvetsovc4502772021-03-22 11:59:37 +000043 /* Enable access to SVE functionality for all ELs. */
44 cptr_el3 = (cptr_el3 | CPTR_EZ_BIT) & ~(TFP_BIT);
45 write_ctx_reg(get_el3state_ctx(context), CTX_CPTR_EL3, cptr_el3);
David Cunadoce88eee2017-10-20 11:30:57 +010046
Mark Brown64869972022-04-20 18:14:32 +010047 /* Restrict maximum SVE vector length (SVE_VECTOR_LEN+1) * 128. */
Max Shvetsovc4502772021-03-22 11:59:37 +000048 write_ctx_reg(get_el3state_ctx(context), CTX_ZCR_EL3,
Mark Brown64869972022-04-20 18:14:32 +010049 (ZCR_EL3_LEN_MASK & CONVERT_SVE_LENGTH(SVE_VECTOR_LEN)));
David Cunadoce88eee2017-10-20 11:30:57 +010050}
johpow019baade32021-07-08 14:14:00 -050051
52void sve_disable(cpu_context_t *context)
53{
54 u_register_t reg;
55 el3_state_t *state;
56
57 /* Make sure SME is implemented in hardware before continuing. */
58 if (!sve_supported()) {
59 return;
60 }
61
62 /* Get the context state. */
63 state = get_el3state_ctx(context);
64
65 /* Disable SVE and FPU since they share registers. */
66 reg = read_ctx_reg(state, CTX_CPTR_EL3);
67 reg &= ~CPTR_EZ_BIT; /* Trap SVE */
68 reg |= TFP_BIT; /* Trap FPU/SIMD */
69 write_ctx_reg(state, CTX_CPTR_EL3, reg);
70}