blob: 1846e003a833323664da256acd65696626c23aa3 [file] [log] [blame]
johpow019baade32021-07-08 14:14:00 -05001/*
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +00002 * Copyright (c) 2021-2023, 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;
20 u_register_t cptr_el3;
21 el3_state_t *state;
22
23 /* Make sure SME is implemented in hardware before continuing. */
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +000024 if (!is_feat_sme_supported()) {
Mark Brownbeaf5e82022-05-09 13:26:36 +010025 /* Perhaps the hardware supports SVE only */
26 sve_enable(context);
johpow019baade32021-07-08 14:14:00 -050027 return;
28 }
29
30 /* Get the context state. */
31 state = get_el3state_ctx(context);
32
33 /* Enable SME in CPTR_EL3. */
34 reg = read_ctx_reg(state, CTX_CPTR_EL3);
35 reg |= ESM_BIT;
36 write_ctx_reg(state, CTX_CPTR_EL3, reg);
37
38 /* Set the ENTP2 bit in SCR_EL3 to enable access to TPIDR2_EL0. */
39 reg = read_ctx_reg(state, CTX_SCR_EL3);
40 reg |= SCR_ENTP2_BIT;
41 write_ctx_reg(state, CTX_SCR_EL3, reg);
42
43 /* Set CPTR_EL3.ESM bit so we can write SMCR_EL3 without trapping. */
44 cptr_el3 = read_cptr_el3();
45 write_cptr_el3(cptr_el3 | ESM_BIT);
Boyan Karatotevbe028b42022-10-13 13:51:05 +010046 isb();
johpow019baade32021-07-08 14:14:00 -050047
48 /*
49 * Set the max LEN value and FA64 bit. This register is set up globally
50 * to be the least restrictive, then lower ELs can restrict as needed
51 * using SMCR_EL2 and SMCR_EL1.
52 */
53 reg = SMCR_ELX_LEN_MASK;
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +000054 if (read_feat_sme_fa64_id_field() != 0U) {
johpow019baade32021-07-08 14:14:00 -050055 VERBOSE("[SME] FA64 enabled\n");
56 reg |= SMCR_ELX_FA64_BIT;
57 }
58 write_smcr_el3(reg);
59
60 /* Reset CPTR_EL3 value. */
61 write_cptr_el3(cptr_el3);
Boyan Karatotevbe028b42022-10-13 13:51:05 +010062 isb();
johpow019baade32021-07-08 14:14:00 -050063
64 /* Enable SVE/FPU in addition to SME. */
65 sve_enable(context);
66}
67
68void sme_disable(cpu_context_t *context)
69{
70 u_register_t reg;
71 el3_state_t *state;
72
73 /* Make sure SME is implemented in hardware before continuing. */
Jayanth Dodderi Chidanand605419a2023-03-06 23:56:14 +000074 if (!is_feat_sme_supported()) {
Mark Brownbeaf5e82022-05-09 13:26:36 +010075 /* Perhaps the hardware supports SVE only */
76 sve_disable(context);
johpow019baade32021-07-08 14:14:00 -050077 return;
78 }
79
80 /* Get the context state. */
81 state = get_el3state_ctx(context);
82
83 /* Disable SME, SVE, and FPU since they all share registers. */
84 reg = read_ctx_reg(state, CTX_CPTR_EL3);
85 reg &= ~ESM_BIT; /* Trap SME */
86 reg &= ~CPTR_EZ_BIT; /* Trap SVE */
87 reg |= TFP_BIT; /* Trap FPU/SIMD */
88 write_ctx_reg(state, CTX_CPTR_EL3, reg);
89
90 /* Disable access to TPIDR2_EL0. */
91 reg = read_ctx_reg(state, CTX_SCR_EL3);
92 reg &= ~SCR_ENTP2_BIT;
93 write_ctx_reg(state, CTX_SCR_EL3, reg);
94}