blob: e477da36ef786cd92aa2a4792c443802581d4fe2 [file] [log] [blame]
johpow019baade32021-07-08 14:14:00 -05001/*
Boyan Karatotev90b7b752024-11-15 15:03:02 +00002 * Copyright (c) 2021-2025, Arm Limited and Contributors. All rights reserved.
johpow019baade32021-07-08 14:14:00 -05003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <stdbool.h>
8
9#include <arch.h>
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +000010#include <arch_features.h>
johpow019baade32021-07-08 14:14:00 -050011#include <arch_helpers.h>
12#include <common/debug.h>
13#include <lib/el3_runtime/context_mgmt.h>
14#include <lib/extensions/sme.h>
15#include <lib/extensions/sve.h>
16
johpow019baade32021-07-08 14:14:00 -050017void sme_enable(cpu_context_t *context)
18{
19 u_register_t reg;
johpow019baade32021-07-08 14:14:00 -050020 el3_state_t *state;
21
johpow019baade32021-07-08 14:14:00 -050022 /* Get the context state. */
23 state = get_el3state_ctx(context);
24
johpow019baade32021-07-08 14:14:00 -050025 /* Set the ENTP2 bit in SCR_EL3 to enable access to TPIDR2_EL0. */
26 reg = read_ctx_reg(state, CTX_SCR_EL3);
27 reg |= SCR_ENTP2_BIT;
28 write_ctx_reg(state, CTX_SCR_EL3, reg);
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000029}
johpow019baade32021-07-08 14:14:00 -050030
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010031void sme_enable_per_world(per_world_context_t *per_world_ctx)
32{
33 u_register_t reg;
34
35 /* Enable SME in CPTR_EL3. */
36 reg = per_world_ctx->ctx_cptr_el3;
37 reg |= ESM_BIT;
38 per_world_ctx->ctx_cptr_el3 = reg;
39}
40
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000041void sme_init_el3(void)
42{
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000043 u_register_t smcr_el3;
44
johpow019baade32021-07-08 14:14:00 -050045 /*
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010046 * Set the max LEN value and FA64 bit. This register is set up per_world
johpow019baade32021-07-08 14:14:00 -050047 * to be the least restrictive, then lower ELs can restrict as needed
48 * using SMCR_EL2 and SMCR_EL1.
49 */
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000050 smcr_el3 = SMCR_ELX_LEN_MAX;
Sona Mathew9e505f92024-03-13 11:33:54 -050051 if (is_feat_sme_fa64_present()) {
johpow019baade32021-07-08 14:14:00 -050052 VERBOSE("[SME] FA64 enabled\n");
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000053 smcr_el3 |= SMCR_ELX_FA64_BIT;
johpow019baade32021-07-08 14:14:00 -050054 }
Jayanth Dodderi Chidanandcfe053a2022-11-08 10:31:07 +000055
56 /*
57 * Enable access to ZT0 register.
58 * Make sure FEAT_SME2 is supported by the hardware before continuing.
59 * If supported, Set the EZT0 bit in SMCR_EL3 to allow instructions to
60 * access ZT0 register without trapping.
61 */
62 if (is_feat_sme2_supported()) {
63 VERBOSE("SME2 enabled\n");
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000064 smcr_el3 |= SMCR_ELX_EZT0_BIT;
Jayanth Dodderi Chidanandcfe053a2022-11-08 10:31:07 +000065 }
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000066 write_smcr_el3(smcr_el3);
johpow019baade32021-07-08 14:14:00 -050067}
68
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000069void sme_init_el2_unused(void)
70{
71 /*
72 * CPTR_EL2.TCPAC: Set to zero so that Non-secure EL1 accesses to the
73 * CPACR_EL1 or CPACR from both Execution states do not trap to EL2.
74 */
75 write_cptr_el2(read_cptr_el2() & ~CPTR_EL2_TCPAC_BIT);
76}
77
johpow019baade32021-07-08 14:14:00 -050078void sme_disable(cpu_context_t *context)
79{
80 u_register_t reg;
81 el3_state_t *state;
82
johpow019baade32021-07-08 14:14:00 -050083 /* Get the context state. */
84 state = get_el3state_ctx(context);
85
johpow019baade32021-07-08 14:14:00 -050086 /* Disable access to TPIDR2_EL0. */
87 reg = read_ctx_reg(state, CTX_SCR_EL3);
88 reg &= ~SCR_ENTP2_BIT;
89 write_ctx_reg(state, CTX_SCR_EL3, reg);
90}
Elizabeth Ho4fc00d22023-07-18 14:10:25 +010091
92void sme_disable_per_world(per_world_context_t *per_world_ctx)
93{
94 u_register_t reg;
95
96 /* Disable SME, SVE, and FPU since they all share registers. */
97 reg = per_world_ctx->ctx_cptr_el3;
98 reg &= ~ESM_BIT; /* Trap SME */
99 reg &= ~CPTR_EZ_BIT; /* Trap SVE */
100 reg |= TFP_BIT; /* Trap FPU/SIMD */
101 per_world_ctx->ctx_cptr_el3 = reg;
102}