11a853370SDavid Cunado /* 2*2b0bc4e0SJayanth Dodderi Chidanand * Copyright (c) 2017-2023, ARM Limited and Contributors. All rights reserved. 31a853370SDavid Cunado * 41a853370SDavid Cunado * SPDX-License-Identifier: BSD-3-Clause 51a853370SDavid Cunado */ 61a853370SDavid Cunado 709d40e0eSAntonio Nino Diaz #include <stdbool.h> 809d40e0eSAntonio Nino Diaz 91a853370SDavid Cunado #include <arch.h> 101a853370SDavid Cunado #include <arch_helpers.h> 11bebcf27fSMark Brown #include <lib/cassert.h> 1209d40e0eSAntonio Nino Diaz #include <lib/el3_runtime/pubsub.h> 1309d40e0eSAntonio Nino Diaz #include <lib/extensions/sve.h> 141a853370SDavid Cunado 15bebcf27fSMark Brown CASSERT(SVE_VECTOR_LEN <= 2048, assert_sve_vl_too_long); 16bebcf27fSMark Brown CASSERT(SVE_VECTOR_LEN >= 128, assert_sve_vl_too_short); 17bebcf27fSMark Brown CASSERT((SVE_VECTOR_LEN % 128) == 0, assert_sve_vl_granule); 18bebcf27fSMark Brown 190c5e7d1cSMax Shvetsov /* 200c5e7d1cSMax Shvetsov * Converts SVE vector size restriction in bytes to LEN according to ZCR_EL3 documentation. 210c5e7d1cSMax Shvetsov * VECTOR_SIZE = (LEN+1) * 128 220c5e7d1cSMax Shvetsov */ 230c5e7d1cSMax Shvetsov #define CONVERT_SVE_LENGTH(x) (((x / 128) - 1)) 240c5e7d1cSMax Shvetsov 250c5e7d1cSMax Shvetsov void sve_enable(cpu_context_t *context) 262ff8fbf3SDimitris Papastamos { 2768ac5ed0SArunachalam Ganapathy u_register_t cptr_el3; 2868ac5ed0SArunachalam Ganapathy 2968ac5ed0SArunachalam Ganapathy cptr_el3 = read_ctx_reg(get_el3state_ctx(context), CTX_CPTR_EL3); 300c5e7d1cSMax Shvetsov 310c5e7d1cSMax Shvetsov /* Enable access to SVE functionality for all ELs. */ 320c5e7d1cSMax Shvetsov cptr_el3 = (cptr_el3 | CPTR_EZ_BIT) & ~(TFP_BIT); 330c5e7d1cSMax Shvetsov write_ctx_reg(get_el3state_ctx(context), CTX_CPTR_EL3, cptr_el3); 340c5e7d1cSMax Shvetsov 35bebcf27fSMark Brown /* Restrict maximum SVE vector length (SVE_VECTOR_LEN+1) * 128. */ 360c5e7d1cSMax Shvetsov write_ctx_reg(get_el3state_ctx(context), CTX_ZCR_EL3, 37bebcf27fSMark Brown (ZCR_EL3_LEN_MASK & CONVERT_SVE_LENGTH(SVE_VECTOR_LEN))); 380c5e7d1cSMax Shvetsov } 39dc78e62dSjohpow01 40dc78e62dSjohpow01 void sve_disable(cpu_context_t *context) 41dc78e62dSjohpow01 { 42dc78e62dSjohpow01 u_register_t reg; 43dc78e62dSjohpow01 el3_state_t *state; 44dc78e62dSjohpow01 45dc78e62dSjohpow01 /* Get the context state. */ 46dc78e62dSjohpow01 state = get_el3state_ctx(context); 47dc78e62dSjohpow01 48dc78e62dSjohpow01 /* Disable SVE and FPU since they share registers. */ 49dc78e62dSjohpow01 reg = read_ctx_reg(state, CTX_CPTR_EL3); 50dc78e62dSjohpow01 reg &= ~CPTR_EZ_BIT; /* Trap SVE */ 51dc78e62dSjohpow01 reg |= TFP_BIT; /* Trap FPU/SIMD */ 52dc78e62dSjohpow01 write_ctx_reg(state, CTX_CPTR_EL3, reg); 53dc78e62dSjohpow01 } 54