blob: fa139cad22729c936c822688adcb9bb0cc2cc21e [file] [log] [blame]
Manish V Badarkhe20df29c2021-07-02 09:10:56 +01001/*
Jayanth Dodderi Chidananda793ccc2022-05-19 14:08:28 +01002 * Copyright (c) 2021-2022, 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>
10#include <lib/el3_runtime/pubsub.h>
11#include <lib/extensions/trbe.h>
12
13static 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
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010022void trbe_enable(void)
23{
24 uint64_t val;
25
Andre Przywara191eff62022-11-17 16:42:09 +000026 /*
27 * MDCR_EL3.NSTB = 0b11
28 * Allow access of trace buffer control registers from NS-EL1
29 * and NS-EL2, tracing is prohibited in Secure and Realm state
30 * (if implemented).
31 */
32 val = read_mdcr_el3();
33 val |= MDCR_NSTB(MDCR_NSTB_EL1);
34 write_mdcr_el3(val);
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010035}
36
37static void *trbe_drain_trace_buffers_hook(const void *arg __unused)
38{
Andre Przywara191eff62022-11-17 16:42:09 +000039 if (is_feat_trbe_supported()) {
Manish V Badarkhe20df29c2021-07-02 09:10:56 +010040 /*
41 * Before switching from normal world to secure world
42 * the trace buffers need to be drained out to memory. This is
43 * required to avoid an invalid memory access when TTBR is switched
44 * for entry to S-EL1.
45 */
46 tsb_csync();
47 dsbnsh();
48 }
49
50 return (void *)0;
51}
52
53SUBSCRIBE_TO_EVENT(cm_entering_secure_world, trbe_drain_trace_buffers_hook);