sve.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2017-2023, 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_helpers.h>
  9. #include <lib/cassert.h>
  10. #include <lib/el3_runtime/pubsub.h>
  11. #include <lib/extensions/sve.h>
  12. CASSERT(SVE_VECTOR_LEN <= 2048, assert_sve_vl_too_long);
  13. CASSERT(SVE_VECTOR_LEN >= 128, assert_sve_vl_too_short);
  14. CASSERT((SVE_VECTOR_LEN % 128) == 0, assert_sve_vl_granule);
  15. /*
  16. * Converts SVE vector size restriction in bytes to LEN according to ZCR_EL3 documentation.
  17. * VECTOR_SIZE = (LEN+1) * 128
  18. */
  19. #define CONVERT_SVE_LENGTH(x) (((x / 128) - 1))
  20. void sve_enable_per_world(per_world_context_t *per_world_ctx)
  21. {
  22. u_register_t cptr_el3;
  23. /* Enable access to SVE functionality for all ELs. */
  24. cptr_el3 = per_world_ctx->ctx_cptr_el3;
  25. cptr_el3 = (cptr_el3 | CPTR_EZ_BIT) & ~(TFP_BIT);
  26. per_world_ctx->ctx_cptr_el3 = cptr_el3;
  27. /* Restrict maximum SVE vector length (SVE_VECTOR_LEN+1) * 128. */
  28. per_world_ctx->ctx_zcr_el3 = (ZCR_EL3_LEN_MASK & CONVERT_SVE_LENGTH(SVE_VECTOR_LEN));
  29. }
  30. void sve_init_el2_unused(void)
  31. {
  32. /*
  33. * CPTR_EL2.TFP: Set to zero so that Non-secure accesses to Advanced
  34. * SIMD and floating-point functionality from both Execution states do
  35. * not trap to EL2.
  36. */
  37. write_cptr_el2(read_cptr_el2() & ~CPTR_EL2_TFP_BIT);
  38. }
  39. void sve_disable_per_world(per_world_context_t *per_world_ctx)
  40. {
  41. u_register_t reg;
  42. /* Disable SVE and FPU since they share registers. */
  43. reg = per_world_ctx->ctx_cptr_el3;
  44. reg &= ~CPTR_EZ_BIT; /* Trap SVE */
  45. reg |= TFP_BIT; /* Trap FPU/SIMD */
  46. per_world_ctx->ctx_cptr_el3 = reg;
  47. }