David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 1 | /* |
Elizabeth Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [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 | |
Elizabeth Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [diff] [blame] | 25 | void sve_enable_per_world(per_world_context_t *per_world_ctx) |
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 | |
Max Shvetsov | c450277 | 2021-03-22 11:59:37 +0000 | [diff] [blame] | 29 | /* Enable access to SVE functionality for all ELs. */ |
Elizabeth Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [diff] [blame] | 30 | cptr_el3 = per_world_ctx->ctx_cptr_el3; |
Max Shvetsov | c450277 | 2021-03-22 11:59:37 +0000 | [diff] [blame] | 31 | cptr_el3 = (cptr_el3 | CPTR_EZ_BIT) & ~(TFP_BIT); |
Elizabeth Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [diff] [blame] | 32 | per_world_ctx->ctx_cptr_el3 = cptr_el3; |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 33 | |
Mark Brown | 6486997 | 2022-04-20 18:14:32 +0100 | [diff] [blame] | 34 | /* Restrict maximum SVE vector length (SVE_VECTOR_LEN+1) * 128. */ |
Elizabeth Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [diff] [blame] | 35 | per_world_ctx->ctx_zcr_el3 = (ZCR_EL3_LEN_MASK & CONVERT_SVE_LENGTH(SVE_VECTOR_LEN)); |
David Cunado | ce88eee | 2017-10-20 11:30:57 +0100 | [diff] [blame] | 36 | } |
johpow01 | 9baade3 | 2021-07-08 14:14:00 -0500 | [diff] [blame] | 37 | |
Boyan Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 38 | void 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 Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [diff] [blame] | 48 | void sve_disable_per_world(per_world_context_t *per_world_ctx) |
johpow01 | 9baade3 | 2021-07-08 14:14:00 -0500 | [diff] [blame] | 49 | { |
| 50 | u_register_t reg; |
johpow01 | 9baade3 | 2021-07-08 14:14:00 -0500 | [diff] [blame] | 51 | |
| 52 | /* Disable SVE and FPU since they share registers. */ |
Elizabeth Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [diff] [blame] | 53 | reg = per_world_ctx->ctx_cptr_el3; |
johpow01 | 9baade3 | 2021-07-08 14:14:00 -0500 | [diff] [blame] | 54 | reg &= ~CPTR_EZ_BIT; /* Trap SVE */ |
| 55 | reg |= TFP_BIT; /* Trap FPU/SIMD */ |
Elizabeth Ho | 4fc00d2 | 2023-07-18 14:10:25 +0100 | [diff] [blame] | 56 | per_world_ctx->ctx_cptr_el3 = reg; |
johpow01 | 9baade3 | 2021-07-08 14:14:00 -0500 | [diff] [blame] | 57 | } |