spe.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2017-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdbool.h>
  7. #include <arch.h>
  8. #include <arch_features.h>
  9. #include <arch_helpers.h>
  10. #include <lib/el3_runtime/pubsub.h>
  11. #include <lib/extensions/spe.h>
  12. #include <plat/common/platform.h>
  13. typedef struct spe_ctx {
  14. u_register_t pmblimitr_el1;
  15. } spe_ctx_t;
  16. static struct spe_ctx spe_ctxs[PLATFORM_CORE_COUNT];
  17. static inline void psb_csync(void)
  18. {
  19. /*
  20. * The assembler does not yet understand the psb csync mnemonic
  21. * so use the equivalent hint instruction.
  22. */
  23. __asm__ volatile("hint #17");
  24. }
  25. void spe_enable(cpu_context_t *ctx)
  26. {
  27. el3_state_t *state = get_el3state_ctx(ctx);
  28. u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
  29. /*
  30. * MDCR_EL3.NSPB (ARM v8.2): SPE enabled in Non-secure state
  31. * and disabled in secure state. Accesses to SPE registers at
  32. * S-EL1 generate trap exceptions to EL3.
  33. *
  34. * MDCR_EL3.NSPBE: Profiling Buffer uses Non-secure Virtual Addresses.
  35. * When FEAT_RME is not implemented, this field is RES0.
  36. *
  37. * MDCR_EL3.EnPMSN (ARM v8.7): Do not trap access to PMSNEVFR_EL1
  38. * register at NS-EL1 or NS-EL2 to EL3 if FEAT_SPEv1p2 is implemented.
  39. * Setting this bit to 1 doesn't have any effect on it when
  40. * FEAT_SPEv1p2 not implemented.
  41. */
  42. mdcr_el3_val |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT;
  43. mdcr_el3_val &= ~(MDCR_NSPBE_BIT);
  44. write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
  45. }
  46. void spe_disable(cpu_context_t *ctx)
  47. {
  48. el3_state_t *state = get_el3state_ctx(ctx);
  49. u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
  50. /*
  51. * MDCR_EL3.NSPB: Clear these bits to disable SPE feature, as it was enabled
  52. * for Non-secure state only. After clearing these bits Secure state owns
  53. * the Profiling Buffer and accesses to Statistical Profiling and Profiling
  54. * Buffer control registers at EL2 and EL1 generate Trap exceptions to EL3
  55. *
  56. * MDCR_EL3.NSPBE: Don't care as it was cleared during spe_enable and setting
  57. * this to 1 does not make sense as NSPBE{1} and NSPB{0b0x} is RESERVED.
  58. *
  59. * MDCR_EL3.EnPMSN (ARM v8.7): Clear the bit to trap access of PMSNEVFR_EL1
  60. * from EL2/EL1 to EL3.
  61. */
  62. mdcr_el3_val &= ~(MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT);
  63. write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
  64. }
  65. void spe_init_el2_unused(void)
  66. {
  67. uint64_t v;
  68. /*
  69. * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical
  70. * profiling controls to EL2.
  71. *
  72. * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure
  73. * state. Accesses to profiling buffer controls at
  74. * Non-secure EL1 are not trapped to EL2.
  75. */
  76. v = read_mdcr_el2();
  77. v &= ~MDCR_EL2_TPMS;
  78. v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
  79. write_mdcr_el2(v);
  80. }
  81. void spe_stop(void)
  82. {
  83. uint64_t v;
  84. /* Drain buffered data */
  85. psb_csync();
  86. dsbnsh();
  87. /* Disable profiling buffer */
  88. v = read_pmblimitr_el1();
  89. v &= ~(1ULL << 0);
  90. write_pmblimitr_el1(v);
  91. isb();
  92. }
  93. static void *spe_drain_buffers_hook(const void *arg)
  94. {
  95. if (!is_feat_spe_supported())
  96. return (void *)-1;
  97. /* Drain buffered data */
  98. psb_csync();
  99. dsbnsh();
  100. return (void *)0;
  101. }
  102. static void *spe_context_save(const void *arg)
  103. {
  104. unsigned int core_pos;
  105. struct spe_ctx *ctx;
  106. if (is_feat_spe_supported()) {
  107. core_pos = plat_my_core_pos();
  108. ctx = &spe_ctxs[core_pos];
  109. ctx->pmblimitr_el1 = read_pmblimitr_el1();
  110. }
  111. return NULL;
  112. }
  113. static void *spe_context_restore(const void *arg)
  114. {
  115. unsigned int core_pos;
  116. struct spe_ctx *ctx;
  117. if (is_feat_spe_supported()) {
  118. core_pos = plat_my_core_pos();
  119. ctx = &spe_ctxs[core_pos];
  120. write_pmblimitr_el1(ctx->pmblimitr_el1);
  121. }
  122. return NULL;
  123. }
  124. SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook);
  125. SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, spe_context_save);
  126. SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, spe_context_restore);