David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 1 | /* |
Jayanth Dodderi Chidanand | d62c681 | 2023-03-07 10:43:19 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2023, ARM Limited and Contributors. All rights reserved. |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 7 | #include <stdbool.h> |
| 8 | |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 9 | #include <arch.h> |
| 10 | #include <arch_helpers.h> |
Mark Brown | 6486997 | 2022-04-20 18:14:32 +0100 | [diff] [blame] | 11 | #include <lib/cassert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 12 | #include <lib/el3_runtime/pubsub.h> |
| 13 | #include <lib/extensions/sve.h> |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 14 | |
Mark Brown | 6486997 | 2022-04-20 18:14:32 +0100 | [diff] [blame] | 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 | |
Max Shvetsov | c450277 | 2021-03-22 11:59:37 +0000 | [diff] [blame] | 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 | |
Max Shvetsov | c450277 | 2021-03-22 11:59:37 +0000 | [diff] [blame] | 25 | void sve_enable(cpu_context_t *context) |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 26 | { |
Arunachalam Ganapathy | cac7d16 | 2021-07-08 09:35:57 +0100 | [diff] [blame] | 27 | u_register_t cptr_el3; |
| 28 | |
Arunachalam Ganapathy | cac7d16 | 2021-07-08 09:35:57 +0100 | [diff] [blame] | 29 | cptr_el3 = read_ctx_reg(get_el3state_ctx(context), CTX_CPTR_EL3); |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 30 | |
Max Shvetsov | c450277 | 2021-03-22 11:59:37 +0000 | [diff] [blame] | 31 | /* Enable access to SVE functionality for all ELs. */ |
| 32 | cptr_el3 = (cptr_el3 | CPTR_EZ_BIT) & ~(TFP_BIT); |
| 33 | write_ctx_reg(get_el3state_ctx(context), CTX_CPTR_EL3, cptr_el3); |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 34 | |
Mark Brown | 6486997 | 2022-04-20 18:14:32 +0100 | [diff] [blame] | 35 | /* Restrict maximum SVE vector length (SVE_VECTOR_LEN+1) * 128. */ |
Max Shvetsov | c450277 | 2021-03-22 11:59:37 +0000 | [diff] [blame] | 36 | write_ctx_reg(get_el3state_ctx(context), CTX_ZCR_EL3, |
Mark Brown | 6486997 | 2022-04-20 18:14:32 +0100 | [diff] [blame] | 37 | (ZCR_EL3_LEN_MASK & CONVERT_SVE_LENGTH(SVE_VECTOR_LEN))); |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 38 | } |
johpow01 | 9baade3 | 2021-07-08 14:14:00 -0500 | [diff] [blame] | 39 | |
Boyan Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 40 | void sve_init_el2_unused(void) |
| 41 | { |
| 42 | /* |
| 43 | * CPTR_EL2.TFP: Set to zero so that Non-secure accesses to Advanced |
| 44 | * SIMD and floating-point functionality from both Execution states do |
| 45 | * not trap to EL2. |
| 46 | */ |
| 47 | write_cptr_el2(read_cptr_el2() & ~CPTR_EL2_TFP_BIT); |
| 48 | } |
| 49 | |
johpow01 | 9baade3 | 2021-07-08 14:14:00 -0500 | [diff] [blame] | 50 | void sve_disable(cpu_context_t *context) |
| 51 | { |
| 52 | u_register_t reg; |
| 53 | el3_state_t *state; |
| 54 | |
johpow01 | 9baade3 | 2021-07-08 14:14:00 -0500 | [diff] [blame] | 55 | /* Get the context state. */ |
| 56 | state = get_el3state_ctx(context); |
| 57 | |
| 58 | /* Disable SVE and FPU since they share registers. */ |
| 59 | reg = read_ctx_reg(state, CTX_CPTR_EL3); |
| 60 | reg &= ~CPTR_EZ_BIT; /* Trap SVE */ |
| 61 | reg |= TFP_BIT; /* Trap FPU/SIMD */ |
| 62 | write_ctx_reg(state, CTX_CPTR_EL3, reg); |
| 63 | } |