spe.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/extensions/spe.h>
  11. #include <plat/common/platform.h>
  12. void spe_enable(cpu_context_t *ctx)
  13. {
  14. el3_state_t *state = get_el3state_ctx(ctx);
  15. u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
  16. /*
  17. * MDCR_EL3.NSPB (ARM v8.2): SPE enabled in Non-secure state
  18. * and disabled in secure state. Accesses to SPE registers at
  19. * S-EL1 generate trap exceptions to EL3.
  20. *
  21. * MDCR_EL3.NSPBE: Profiling Buffer uses Non-secure Virtual Addresses.
  22. * When FEAT_RME is not implemented, this field is RES0.
  23. *
  24. * MDCR_EL3.EnPMSN (ARM v8.7): Do not trap access to PMSNEVFR_EL1
  25. * register at NS-EL1 or NS-EL2 to EL3 if FEAT_SPEv1p2 is implemented.
  26. * Setting this bit to 1 doesn't have any effect on it when
  27. * FEAT_SPEv1p2 not implemented.
  28. */
  29. mdcr_el3_val |= MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT;
  30. mdcr_el3_val &= ~(MDCR_NSPBE_BIT);
  31. write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
  32. }
  33. void spe_disable(cpu_context_t *ctx)
  34. {
  35. el3_state_t *state = get_el3state_ctx(ctx);
  36. u_register_t mdcr_el3_val = read_ctx_reg(state, CTX_MDCR_EL3);
  37. /*
  38. * MDCR_EL3.NSPB: Clear these bits to disable SPE feature, as it was enabled
  39. * for Non-secure state only. After clearing these bits Secure state owns
  40. * the Profiling Buffer and accesses to Statistical Profiling and Profiling
  41. * Buffer control registers at EL2 and EL1 generate Trap exceptions to EL3
  42. *
  43. * MDCR_EL3.NSPBE: Don't care as it was cleared during spe_enable and setting
  44. * this to 1 does not make sense as NSPBE{1} and NSPB{0b0x} is RESERVED.
  45. *
  46. * MDCR_EL3.EnPMSN (ARM v8.7): Clear the bit to trap access of PMSNEVFR_EL1
  47. * from EL2/EL1 to EL3.
  48. */
  49. mdcr_el3_val &= ~(MDCR_NSPB(MDCR_NSPB_EL1) | MDCR_EnPMSN_BIT);
  50. write_ctx_reg(state, CTX_MDCR_EL3, mdcr_el3_val);
  51. }
  52. void spe_init_el2_unused(void)
  53. {
  54. uint64_t v;
  55. /*
  56. * MDCR_EL2.TPMS (ARM v8.2): Do not trap statistical
  57. * profiling controls to EL2.
  58. *
  59. * MDCR_EL2.E2PB (ARM v8.2): SPE enabled in Non-secure
  60. * state. Accesses to profiling buffer controls at
  61. * Non-secure EL1 are not trapped to EL2.
  62. */
  63. v = read_mdcr_el2();
  64. v &= ~MDCR_EL2_TPMS;
  65. v |= MDCR_EL2_E2PB(MDCR_EL2_E2PB_EL1);
  66. write_mdcr_el2(v);
  67. }