blob: 24d88aee5930a205a6d58e75d46d0575574789bb [file] [log] [blame]
Manish V Badarkhe20df29c2021-07-02 09:10:56 +01001/*
Boyan Karatotevf9263222024-12-16 16:23:26 +00002 * Copyright (c) 2021-2025, Arm Limited. All rights reserved.
Manish V Badarkhe20df29c2021-07-02 09:10:56 +01003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <arch.h>
Jayanth Dodderi Chidananda793ccc2022-05-19 14:08:28 +01008#include <arch_features.h>
Manish V Badarkhe20df29c2021-07-02 09:10:56 +01009#include <arch_helpers.h>
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010010#include <lib/extensions/trbe.h>
11
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +010012void trbe_enable(cpu_context_t *ctx)
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010013{
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +010014 el3_state_t *state = get_el3state_ctx(ctx);
15 u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010016
Andre Przywara191eff62022-11-17 16:42:09 +000017 /*
Boyan Karatotev919d3c82023-02-13 16:32:47 +000018 * MDCR_EL3.NSTBE = 0b0
19 * Trace Buffer owning Security state is Non-secure state. If FEAT_RME
20 * is not implemented, this field is RES0.
21 *
Andre Przywara191eff62022-11-17 16:42:09 +000022 * MDCR_EL3.NSTB = 0b11
Boyan Karatotev919d3c82023-02-13 16:32:47 +000023 * Allow access of trace buffer control registers from NS-EL1 and
24 * NS-EL2, tracing is prohibited in Secure and Realm state (if
25 * implemented).
Andre Przywara191eff62022-11-17 16:42:09 +000026 */
Jayanth Dodderi Chidanand118b3352024-06-18 15:22:54 +010027 mdcr_el3_val |= MDCR_NSTB(MDCR_NSTB_EL1);
28 mdcr_el3_val &= ~(MDCR_NSTBE_BIT);
29 write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010030}
31
Arvind Ram Prakash58f89ed2024-07-19 11:39:49 -050032void trbe_disable(cpu_context_t *ctx)
33{
34 el3_state_t *state = get_el3state_ctx(ctx);
35 u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
36
37 /*
Boyan Karatotev3eaf1b32025-01-10 12:04:50 +000038 * MDCR_EL3.{NSTBE,NSTB} = 0b0, 0b00
Boyan Karatotevf9263222024-12-16 16:23:26 +000039 * Disable access of trace buffer control registers from lower ELs in
Boyan Karatotev3eaf1b32025-01-10 12:04:50 +000040 * any security state. Secure state owns the buffer.
Arvind Ram Prakash58f89ed2024-07-19 11:39:49 -050041 */
42 mdcr_el3_val &= ~(MDCR_NSTB(MDCR_NSTB_EL1));
43 mdcr_el3_val &= ~(MDCR_NSTBE_BIT);
44 write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
45}
46
Boyan Karatotev6468d4a2023-02-16 15:12:45 +000047void trbe_init_el2_unused(void)
48{
49 /*
50 * MDCR_EL2.E2TB: Set to zero so that the trace Buffer
51 * owning exception level is NS-EL1 and, tracing is
52 * prohibited at NS-EL2. These bits are RES0 when
53 * FEAT_TRBE is not implemented.
54 */
55 write_mdcr_el2(read_mdcr_el2() & ~MDCR_EL2_E2TB(MDCR_EL2_E2TB_EL1));
56}