sme.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2021-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 <common/debug.h>
  11. #include <lib/el3_runtime/context_mgmt.h>
  12. #include <lib/extensions/sme.h>
  13. #include <lib/extensions/sve.h>
  14. void sme_enable(cpu_context_t *context)
  15. {
  16. u_register_t reg;
  17. el3_state_t *state;
  18. /* Get the context state. */
  19. state = get_el3state_ctx(context);
  20. /* Set the ENTP2 bit in SCR_EL3 to enable access to TPIDR2_EL0. */
  21. reg = read_ctx_reg(state, CTX_SCR_EL3);
  22. reg |= SCR_ENTP2_BIT;
  23. write_ctx_reg(state, CTX_SCR_EL3, reg);
  24. }
  25. void sme_enable_per_world(per_world_context_t *per_world_ctx)
  26. {
  27. u_register_t reg;
  28. /* Enable SME in CPTR_EL3. */
  29. reg = per_world_ctx->ctx_cptr_el3;
  30. reg |= ESM_BIT;
  31. per_world_ctx->ctx_cptr_el3 = reg;
  32. }
  33. void sme_init_el3(void)
  34. {
  35. u_register_t cptr_el3 = read_cptr_el3();
  36. u_register_t smcr_el3;
  37. /* Set CPTR_EL3.ESM bit so we can access SMCR_EL3 without trapping. */
  38. write_cptr_el3(cptr_el3 | ESM_BIT);
  39. isb();
  40. /*
  41. * Set the max LEN value and FA64 bit. This register is set up per_world
  42. * to be the least restrictive, then lower ELs can restrict as needed
  43. * using SMCR_EL2 and SMCR_EL1.
  44. */
  45. smcr_el3 = SMCR_ELX_LEN_MAX;
  46. if (is_feat_sme_fa64_present()) {
  47. VERBOSE("[SME] FA64 enabled\n");
  48. smcr_el3 |= SMCR_ELX_FA64_BIT;
  49. }
  50. /*
  51. * Enable access to ZT0 register.
  52. * Make sure FEAT_SME2 is supported by the hardware before continuing.
  53. * If supported, Set the EZT0 bit in SMCR_EL3 to allow instructions to
  54. * access ZT0 register without trapping.
  55. */
  56. if (is_feat_sme2_supported()) {
  57. VERBOSE("SME2 enabled\n");
  58. smcr_el3 |= SMCR_ELX_EZT0_BIT;
  59. }
  60. write_smcr_el3(smcr_el3);
  61. /* Reset CPTR_EL3 value. */
  62. write_cptr_el3(cptr_el3);
  63. isb();
  64. }
  65. void sme_init_el2_unused(void)
  66. {
  67. /*
  68. * CPTR_EL2.TCPAC: Set to zero so that Non-secure EL1 accesses to the
  69. * CPACR_EL1 or CPACR from both Execution states do not trap to EL2.
  70. */
  71. write_cptr_el2(read_cptr_el2() & ~CPTR_EL2_TCPAC_BIT);
  72. }
  73. void sme_disable(cpu_context_t *context)
  74. {
  75. u_register_t reg;
  76. el3_state_t *state;
  77. /* Get the context state. */
  78. state = get_el3state_ctx(context);
  79. /* Disable access to TPIDR2_EL0. */
  80. reg = read_ctx_reg(state, CTX_SCR_EL3);
  81. reg &= ~SCR_ENTP2_BIT;
  82. write_ctx_reg(state, CTX_SCR_EL3, reg);
  83. }
  84. void sme_disable_per_world(per_world_context_t *per_world_ctx)
  85. {
  86. u_register_t reg;
  87. /* Disable SME, SVE, and FPU since they all share registers. */
  88. reg = per_world_ctx->ctx_cptr_el3;
  89. reg &= ~ESM_BIT; /* Trap SME */
  90. reg &= ~CPTR_EZ_BIT; /* Trap SVE */
  91. reg |= TFP_BIT; /* Trap FPU/SIMD */
  92. per_world_ctx->ctx_cptr_el3 = reg;
  93. }