pmuv3.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2023-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/extensions/pmuv3.h>
  10. static u_register_t init_mdcr_el2_hpmn(u_register_t mdcr_el2)
  11. {
  12. /*
  13. * Initialize MDCR_EL2.HPMN to its hardware reset value so we don't
  14. * throw anyone off who expects this to be sensible.
  15. */
  16. mdcr_el2 &= ~MDCR_EL2_HPMN_MASK;
  17. mdcr_el2 |= ((read_pmcr_el0() >> PMCR_EL0_N_SHIFT) & PMCR_EL0_N_MASK);
  18. return mdcr_el2;
  19. }
  20. void pmuv3_enable(cpu_context_t *ctx)
  21. {
  22. #if (CTX_INCLUDE_EL2_REGS && IMAGE_BL31)
  23. u_register_t mdcr_el2_val;
  24. mdcr_el2_val = read_el2_ctx_common(get_el2_sysregs_ctx(ctx), mdcr_el2);
  25. mdcr_el2_val = init_mdcr_el2_hpmn(mdcr_el2_val);
  26. write_el2_ctx_common(get_el2_sysregs_ctx(ctx), mdcr_el2, mdcr_el2_val);
  27. #endif /* (CTX_INCLUDE_EL2_REGS && IMAGE_BL31) */
  28. }
  29. static u_register_t mtpmu_disable_el3(u_register_t mdcr_el3)
  30. {
  31. if (!is_feat_mtpmu_supported()) {
  32. return mdcr_el3;
  33. }
  34. /*
  35. * MDCR_EL3.MTPME = 0
  36. * FEAT_MTPMU is disabled. The Effective value of PMEVTYPER<n>_EL0.MT is
  37. * zero.
  38. */
  39. mdcr_el3 &= ~MDCR_MTPME_BIT;
  40. return mdcr_el3;
  41. }
  42. void pmuv3_init_el3(void)
  43. {
  44. u_register_t mdcr_el3 = read_mdcr_el3();
  45. /* ---------------------------------------------------------------------
  46. * Initialise MDCR_EL3, setting all fields rather than relying on hw.
  47. * Some fields are architecturally UNKNOWN on reset.
  48. *
  49. * MDCR_EL3.MPMX: Set to zero to not affect event counters (when
  50. * SPME = 0).
  51. *
  52. * MDCR_EL3.MCCD: Set to one so that cycle counting by PMCCNTR_EL0 is
  53. * prohibited in EL3. This bit is RES0 in versions of the
  54. * architecture with FEAT_PMUv3p7 not implemented.
  55. *
  56. * MDCR_EL3.SCCD: Set to one so that cycle counting by PMCCNTR_EL0 is
  57. * prohibited in Secure state. This bit is RES0 in versions of the
  58. * architecture with FEAT_PMUv3p5 not implemented.
  59. *
  60. * MDCR_EL3.SPME: Set to zero so that event counting is prohibited in
  61. * Secure state (and explicitly EL3 with later revisions). If ARMv8.2
  62. * Debug is not implemented this bit does not have any effect on the
  63. * counters unless there is support for the implementation defined
  64. * authentication interface ExternalSecureNoninvasiveDebugEnabled().
  65. *
  66. * The SPME/MPMX combination is a little tricky. Below is a small
  67. * summary if another combination is ever needed:
  68. * SPME | MPMX | secure world | EL3
  69. * -------------------------------------
  70. * 0 | 0 | disabled | disabled
  71. * 1 | 0 | enabled | enabled
  72. * 0 | 1 | enabled | disabled
  73. * 1 | 1 | enabled | disabled only for counters 0 to
  74. * MDCR_EL2.HPMN - 1. Enabled for the rest
  75. *
  76. * MDCR_EL3.TPM: Set to zero so that EL0, EL1, and EL2 System register
  77. * accesses to all Performance Monitors registers do not trap to EL3.
  78. */
  79. mdcr_el3 = (mdcr_el3 | MDCR_SCCD_BIT | MDCR_MCCD_BIT) &
  80. ~(MDCR_MPMX_BIT | MDCR_SPME_BIT | MDCR_TPM_BIT);
  81. mdcr_el3 = mtpmu_disable_el3(mdcr_el3);
  82. write_mdcr_el3(mdcr_el3);
  83. /* ---------------------------------------------------------------------
  84. * Initialise PMCR_EL0 setting all fields rather than relying
  85. * on hw. Some fields are architecturally UNKNOWN on reset.
  86. *
  87. * PMCR_EL0.DP: Set to one so that the cycle counter,
  88. * PMCCNTR_EL0 does not count when event counting is prohibited.
  89. * Necessary on PMUv3 <= p7 where MDCR_EL3.{SCCD,MCCD} are not
  90. * available
  91. *
  92. * PMCR_EL0.X: Set to zero to disable export of events.
  93. *
  94. * PMCR_EL0.C: Set to one to reset PMCCNTR_EL0 to zero.
  95. *
  96. * PMCR_EL0.P: Set to one to reset each event counter PMEVCNTR<n>_EL0 to
  97. * zero.
  98. *
  99. * PMCR_EL0.E: Set to zero to disable cycle and event counters.
  100. * ---------------------------------------------------------------------
  101. */
  102. write_pmcr_el0((read_pmcr_el0() | PMCR_EL0_DP_BIT | PMCR_EL0_C_BIT |
  103. PMCR_EL0_P_BIT) & ~(PMCR_EL0_X_BIT | PMCR_EL0_E_BIT));
  104. }
  105. static u_register_t mtpmu_disable_el2(u_register_t mdcr_el2)
  106. {
  107. if (!is_feat_mtpmu_supported()) {
  108. return mdcr_el2;
  109. }
  110. /*
  111. * MDCR_EL2.MTPME = 0
  112. * FEAT_MTPMU is disabled. The Effective value of PMEVTYPER<n>_EL0.MT is
  113. * zero.
  114. */
  115. mdcr_el2 &= ~MDCR_EL2_MTPME;
  116. return mdcr_el2;
  117. }
  118. void pmuv3_init_el2_unused(void)
  119. {
  120. u_register_t mdcr_el2 = read_mdcr_el2();
  121. /*
  122. * Initialise MDCR_EL2, setting all fields rather than
  123. * relying on hw. Some fields are architecturally
  124. * UNKNOWN on reset.
  125. *
  126. * MDCR_EL2.HLP: Set to one so that event counter overflow, that is
  127. * recorded in PMOVSCLR_EL0[0-30], occurs on the increment that changes
  128. * PMEVCNTR<n>_EL0[63] from 1 to 0, when ARMv8.5-PMU is implemented.
  129. * This bit is RES0 in versions of the architecture earlier than
  130. * ARMv8.5, setting it to 1 doesn't have any effect on them.
  131. *
  132. * MDCR_EL2.HCCD: Set to one to prohibit cycle counting at EL2. This bit
  133. * is RES0 in versions of the architecture with FEAT_PMUv3p5 not
  134. * implemented.
  135. *
  136. * MDCR_EL2.HPMD: Set to one so that event counting is
  137. * prohibited at EL2 for counter n < MDCR_EL2.HPMN. This bit is RES0
  138. * in versions of the architecture with FEAT_PMUv3p1 not implemented.
  139. *
  140. * MDCR_EL2.HPME: Set to zero to disable event counters for counters
  141. * n >= MDCR_EL2.HPMN.
  142. *
  143. * MDCR_EL2.TPM: Set to zero so that Non-secure EL0 and
  144. * EL1 accesses to all Performance Monitors registers
  145. * are not trapped to EL2.
  146. *
  147. * MDCR_EL2.TPMCR: Set to zero so that Non-secure EL0
  148. * and EL1 accesses to the PMCR_EL0 or PMCR are not
  149. * trapped to EL2.
  150. */
  151. mdcr_el2 = (mdcr_el2 | MDCR_EL2_HLP_BIT | MDCR_EL2_HPMD_BIT |
  152. MDCR_EL2_HCCD_BIT) &
  153. ~(MDCR_EL2_HPME_BIT | MDCR_EL2_TPM_BIT | MDCR_EL2_TPMCR_BIT);
  154. mdcr_el2 = init_mdcr_el2_hpmn(mdcr_el2);
  155. mdcr_el2 = mtpmu_disable_el2(mdcr_el2);
  156. write_mdcr_el2(mdcr_el2);
  157. }