spe.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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_init_el3(void)
  26. {
  27. uint64_t v;
  28. /*
  29. * MDCR_EL3.NSPB (ARM v8.2): SPE enabled in Non-secure state
  30. * and disabled in secure state. Accesses to SPE registers at
  31. * S-EL1 generate trap exceptions to EL3.
  32. *
  33. * MDCR_EL3.NSPBE: Profiling Buffer uses Non-secure Virtual Addresses.
  34. * When FEAT_RME is not implemented, this field is RES0.
  35. *
  36. * MDCR_EL3.EnPMSN (ARM v8.7): Do not trap access to PMSNEVFR_EL1
  37. * register at NS-EL1 or NS-EL2 to EL3 if FEAT_SPEv1p2 is implemented.
  38. * Setting this bit to 1 doesn't have any effect on it when
  39. * FEAT_SPEv1p2 not implemented.
  40. */
  41. v = read_mdcr_el3();
  42. v |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT;
  43. v &= ~(MDCR_NSPBE_BIT);
  44. write_mdcr_el3(v);
  45. }
  46. void spe_init_el2_unused(void)
  47. {
  48. uint64_t v;
  49. /*
  50. * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical
  51. * profiling controls to EL2.
  52. *
  53. * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure
  54. * state. Accesses to profiling buffer controls at
  55. * Non-secure EL1 are not trapped to EL2.
  56. */
  57. v = read_mdcr_el2();
  58. v &= ~MDCR_EL2_TPMS;
  59. v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
  60. write_mdcr_el2(v);
  61. }
  62. void spe_disable(void)
  63. {
  64. uint64_t v;
  65. /* Drain buffered data */
  66. psb_csync();
  67. dsbnsh();
  68. /* Disable profiling buffer */
  69. v = read_pmblimitr_el1();
  70. v &= ~(1ULL << 0);
  71. write_pmblimitr_el1(v);
  72. isb();
  73. }
  74. static void *spe_drain_buffers_hook(const void *arg)
  75. {
  76. if (!is_feat_spe_supported())
  77. return (void *)-1;
  78. /* Drain buffered data */
  79. psb_csync();
  80. dsbnsh();
  81. return (void *)0;
  82. }
  83. static void *spe_context_save(const void *arg)
  84. {
  85. unsigned int core_pos;
  86. struct spe_ctx *ctx;
  87. if (is_feat_spe_supported()) {
  88. core_pos = plat_my_core_pos();
  89. ctx = &spe_ctxs[core_pos];
  90. ctx->pmblimitr_el1 = read_pmblimitr_el1();
  91. }
  92. return NULL;
  93. }
  94. static void *spe_context_restore(const void *arg)
  95. {
  96. unsigned int core_pos;
  97. struct spe_ctx *ctx;
  98. if (is_feat_spe_supported()) {
  99. core_pos = plat_my_core_pos();
  100. ctx = &spe_ctxs[core_pos];
  101. write_pmblimitr_el1(ctx->pmblimitr_el1);
  102. }
  103. return NULL;
  104. }
  105. SUBSCRIBE_TO_EVENT(cm_entering_secure_world, spe_drain_buffers_hook);
  106. SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_start, spe_context_save);
  107. SUBSCRIBE_TO_EVENT(psci_suspend_pwrdown_finish, spe_context_restore);