trbe.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2021-2023, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch.h>
  7. #include <arch_features.h>
  8. #include <arch_helpers.h>
  9. #include <lib/el3_runtime/pubsub.h>
  10. #include <lib/extensions/trbe.h>
  11. static void tsb_csync(void)
  12. {
  13. /*
  14. * The assembler does not yet understand the tsb csync mnemonic
  15. * so use the equivalent hint instruction.
  16. */
  17. __asm__ volatile("hint #18");
  18. }
  19. void trbe_init_el3(void)
  20. {
  21. u_register_t val;
  22. /*
  23. * MDCR_EL3.NSTBE = 0b0
  24. * Trace Buffer owning Security state is Non-secure state. If FEAT_RME
  25. * is not implemented, this field is RES0.
  26. *
  27. * MDCR_EL3.NSTB = 0b11
  28. * Allow access of trace buffer control registers from NS-EL1 and
  29. * NS-EL2, tracing is prohibited in Secure and Realm state (if
  30. * implemented).
  31. */
  32. val = read_mdcr_el3();
  33. val |= MDCR_NSTB(MDCR_NSTB_EL1);
  34. val &= ~(MDCR_NSTBE_BIT);
  35. write_mdcr_el3(val);
  36. }
  37. void trbe_init_el2_unused(void)
  38. {
  39. /*
  40. * MDCR_EL2.E2TB: Set to zero so that the trace Buffer
  41. * owning exception level is NS-EL1 and, tracing is
  42. * prohibited at NS-EL2. These bits are RES0 when
  43. * FEAT_TRBE is not implemented.
  44. */
  45. write_mdcr_el2(read_mdcr_el2() & ~MDCR_EL2_E2TB(MDCR_EL2_E2TB_EL1));
  46. }
  47. static void *trbe_drain_trace_buffers_hook(const void *arg __unused)
  48. {
  49. if (is_feat_trbe_supported()) {
  50. /*
  51. * Before switching from normal world to secure world
  52. * the trace buffers need to be drained out to memory. This is
  53. * required to avoid an invalid memory access when TTBR is switched
  54. * for entry to S-EL1.
  55. */
  56. tsb_csync();
  57. dsbnsh();
  58. }
  59. return (void *)0;
  60. }
  61. SUBSCRIBE_TO_EVENT(cm_entering_secure_world, trbe_drain_trace_buffers_hook);