Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 1 | /* |
Boyan Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021-2023, 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> |
| 10 | #include <lib/el3_runtime/pubsub.h> |
| 11 | #include <lib/extensions/trbe.h> |
| 12 | |
| 13 | static void tsb_csync(void) |
| 14 | { |
| 15 | /* |
| 16 | * The assembler does not yet understand the tsb csync mnemonic |
| 17 | * so use the equivalent hint instruction. |
| 18 | */ |
| 19 | __asm__ volatile("hint #18"); |
| 20 | } |
| 21 | |
Boyan Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 22 | void trbe_init_el3(void) |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 23 | { |
Boyan Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 24 | u_register_t val; |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 25 | |
Andre Przywara | 191eff6 | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 26 | /* |
Boyan Karatotev | 919d3c8 | 2023-02-13 16:32:47 +0000 | [diff] [blame] | 27 | * MDCR_EL3.NSTBE = 0b0 |
| 28 | * Trace Buffer owning Security state is Non-secure state. If FEAT_RME |
| 29 | * is not implemented, this field is RES0. |
| 30 | * |
Andre Przywara | 191eff6 | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 31 | * MDCR_EL3.NSTB = 0b11 |
Boyan Karatotev | 919d3c8 | 2023-02-13 16:32:47 +0000 | [diff] [blame] | 32 | * Allow access of trace buffer control registers from NS-EL1 and |
| 33 | * NS-EL2, tracing is prohibited in Secure and Realm state (if |
| 34 | * implemented). |
Andre Przywara | 191eff6 | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 35 | */ |
| 36 | val = read_mdcr_el3(); |
| 37 | val |= MDCR_NSTB(MDCR_NSTB_EL1); |
Boyan Karatotev | 919d3c8 | 2023-02-13 16:32:47 +0000 | [diff] [blame] | 38 | val &= ~(MDCR_NSTBE_BIT); |
Andre Przywara | 191eff6 | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 39 | write_mdcr_el3(val); |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Boyan Karatotev | 6468d4a | 2023-02-16 15:12:45 +0000 | [diff] [blame] | 42 | void trbe_init_el2_unused(void) |
| 43 | { |
| 44 | /* |
| 45 | * MDCR_EL2.E2TB: Set to zero so that the trace Buffer |
| 46 | * owning exception level is NS-EL1 and, tracing is |
| 47 | * prohibited at NS-EL2. These bits are RES0 when |
| 48 | * FEAT_TRBE is not implemented. |
| 49 | */ |
| 50 | write_mdcr_el2(read_mdcr_el2() & ~MDCR_EL2_E2TB(MDCR_EL2_E2TB_EL1)); |
| 51 | } |
| 52 | |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 53 | static void *trbe_drain_trace_buffers_hook(const void *arg __unused) |
| 54 | { |
Andre Przywara | 191eff6 | 2022-11-17 16:42:09 +0000 | [diff] [blame] | 55 | if (is_feat_trbe_supported()) { |
Manish V Badarkhe | 20df29c | 2021-07-02 09:10:56 +0100 | [diff] [blame] | 56 | /* |
| 57 | * Before switching from normal world to secure world |
| 58 | * the trace buffers need to be drained out to memory. This is |
| 59 | * required to avoid an invalid memory access when TTBR is switched |
| 60 | * for entry to S-EL1. |
| 61 | */ |
| 62 | tsb_csync(); |
| 63 | dsbnsh(); |
| 64 | } |
| 65 | |
| 66 | return (void *)0; |
| 67 | } |
| 68 | |
| 69 | SUBSCRIBE_TO_EVENT(cm_entering_secure_world, trbe_drain_trace_buffers_hook); |