trbe.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2021-2024, 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_enable(cpu_context_t *ctx)
  20. {
  21. el3_state_t *state = get_el3state_ctx(ctx);
  22. u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
  23. /*
  24. * MDCR_EL3.NSTBE = 0b0
  25. * Trace Buffer owning Security state is Non-secure state. If FEAT_RME
  26. * is not implemented, this field is RES0.
  27. *
  28. * MDCR_EL3.NSTB = 0b11
  29. * Allow access of trace buffer control registers from NS-EL1 and
  30. * NS-EL2, tracing is prohibited in Secure and Realm state (if
  31. * implemented).
  32. */
  33. mdcr_el3_val |= MDCR_NSTB(MDCR_NSTB_EL1);
  34. mdcr_el3_val &= ~(MDCR_NSTBE_BIT);
  35. write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
  36. }
  37. void trbe_disable(cpu_context_t *ctx)
  38. {
  39. el3_state_t *state = get_el3state_ctx(ctx);
  40. u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
  41. /*
  42. * MDCR_EL3.NSTBE = 0b0
  43. * Trace Buffer owning Security state is secure state. If FEAT_RME
  44. * is not implemented, this field is RES0.
  45. *
  46. * MDCR_EL3.NSTB = 0b00
  47. * Clear these bits to disable access of trace buffer control registers
  48. * from lower ELs in any security state.
  49. */
  50. mdcr_el3_val &= ~(MDCR_NSTB(MDCR_NSTB_EL1));
  51. mdcr_el3_val &= ~(MDCR_NSTBE_BIT);
  52. write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
  53. }
  54. void trbe_init_el2_unused(void)
  55. {
  56. /*
  57. * MDCR_EL2.E2TB: Set to zero so that the trace Buffer
  58. * owning exception level is NS-EL1 and, tracing is
  59. * prohibited at NS-EL2. These bits are RES0 when
  60. * FEAT_TRBE is not implemented.
  61. */
  62. write_mdcr_el2(read_mdcr_el2() & ~MDCR_EL2_E2TB(MDCR_EL2_E2TB_EL1));
  63. }
  64. static void *trbe_drain_trace_buffers_hook(const void *arg __unused)
  65. {
  66. if (is_feat_trbe_supported()) {
  67. /*
  68. * Before switching from normal world to secure world
  69. * the trace buffers need to be drained out to memory. This is
  70. * required to avoid an invalid memory access when TTBR is switched
  71. * for entry to S-EL1.
  72. */
  73. tsb_csync();
  74. dsbnsh();
  75. }
  76. return (void *)0;
  77. }
  78. SUBSCRIBE_TO_EVENT(cm_entering_secure_world, trbe_drain_trace_buffers_hook);