Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 1 | /* |
Boyan Karatotev | f926322 | 2024-12-16 16:23:26 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2025, Arm Limited. All rights reserved. |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <arch.h> |
Jayanth Dodderi Chidanand | a793ccc | 2022-05-19 14:08:28 +0100 | [diff] [blame] | 8 | #include <arch_features.h> |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 9 | #include <arch_helpers.h> |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 10 | #include <lib/extensions/trbe.h> |
| 11 | |
Jayanth Dodderi Chidanand | 118b335 | 2024-06-18 15:22:54 +0100 | [diff] [blame] | 12 | void trbe_enable(cpu_context_t *ctx) |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 13 | { |
Jayanth Dodderi Chidanand | 118b335 | 2024-06-18 15:22:54 +0100 | [diff] [blame] | 14 | el3_state_t *state = get_el3state_ctx(ctx); |
| 15 | u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3); |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 16 | |
Andre Przywara | 191eff6 | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 17 | /* |
Boyan Karatotev | 919d3c8 | 2023-02-13 16:32:47 +0000 | [diff] [blame] | 18 | * 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 Przywara | 191eff6 | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 22 | * MDCR_EL3.NSTB = 0b11 |
Boyan Karatotev | 919d3c8 | 2023-02-13 16:32:47 +0000 | [diff] [blame] | 23 | * 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 Przywara | 191eff6 | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 26 | */ |
Jayanth Dodderi Chidanand | 118b335 | 2024-06-18 15:22:54 +0100 | [diff] [blame] | 27 | 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 Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 30 | } |
| 31 | |
Arvind Ram Prakash | 58f89ed | 2024-07-19 11:39:49 -0500 | [diff] [blame] | 32 | void 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 Karatotev | 3eaf1b3 | 2025-01-10 12:04:50 +0000 | [diff] [blame] | 38 | * MDCR_EL3.{NSTBE,NSTB} = 0b0, 0b00 |
Boyan Karatotev | f926322 | 2024-12-16 16:23:26 +0000 | [diff] [blame] | 39 | * Disable access of trace buffer control registers from lower ELs in |
Boyan Karatotev | 3eaf1b3 | 2025-01-10 12:04:50 +0000 | [diff] [blame] | 40 | * any security state. Secure state owns the buffer. |
Arvind Ram Prakash | 58f89ed | 2024-07-19 11:39:49 -0500 | [diff] [blame] | 41 | */ |
| 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 Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 47 | void 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 | } |