tsp_common.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <inttypes.h>
  8. #include <stdint.h>
  9. #include <arch_features.h>
  10. #include <arch_helpers.h>
  11. #include <bl32/tsp/tsp.h>
  12. #include <common/bl_common.h>
  13. #include <common/debug.h>
  14. #include <lib/spinlock.h>
  15. #include <plat/common/platform.h>
  16. #include <platform_tsp.h>
  17. #include "tsp_private.h"
  18. #include <platform_def.h>
  19. /*******************************************************************************
  20. * Per cpu data structure to populate parameters for an SMC in C code and use
  21. * a pointer to this structure in assembler code to populate x0-x7.
  22. ******************************************************************************/
  23. static smc_args_t tsp_smc_args[PLATFORM_CORE_COUNT];
  24. /*******************************************************************************
  25. * Per cpu data structure to keep track of TSP activity
  26. ******************************************************************************/
  27. work_statistics_t tsp_stats[PLATFORM_CORE_COUNT];
  28. smc_args_t *set_smc_args(uint64_t arg0,
  29. uint64_t arg1,
  30. uint64_t arg2,
  31. uint64_t arg3,
  32. uint64_t arg4,
  33. uint64_t arg5,
  34. uint64_t arg6,
  35. uint64_t arg7)
  36. {
  37. uint32_t linear_id;
  38. smc_args_t *pcpu_smc_args;
  39. /*
  40. * Return to Secure Monitor by raising an SMC. The results of the
  41. * service are passed as an arguments to the SMC.
  42. */
  43. linear_id = plat_my_core_pos();
  44. pcpu_smc_args = &tsp_smc_args[linear_id];
  45. write_sp_arg(pcpu_smc_args, SMC_ARG0, arg0);
  46. write_sp_arg(pcpu_smc_args, SMC_ARG1, arg1);
  47. write_sp_arg(pcpu_smc_args, SMC_ARG2, arg2);
  48. write_sp_arg(pcpu_smc_args, SMC_ARG3, arg3);
  49. write_sp_arg(pcpu_smc_args, SMC_ARG4, arg4);
  50. write_sp_arg(pcpu_smc_args, SMC_ARG5, arg5);
  51. write_sp_arg(pcpu_smc_args, SMC_ARG6, arg6);
  52. write_sp_arg(pcpu_smc_args, SMC_ARG7, arg7);
  53. return pcpu_smc_args;
  54. }
  55. /*******************************************************************************
  56. * Setup function for TSP.
  57. ******************************************************************************/
  58. void tsp_setup(void)
  59. {
  60. /* Perform early platform-specific setup. */
  61. tsp_early_platform_setup();
  62. /* Perform late platform-specific setup. */
  63. tsp_plat_arch_setup();
  64. #if ENABLE_PAUTH
  65. /*
  66. * Assert that the ARMv8.3-PAuth registers are present or an access
  67. * fault will be triggered when they are being saved or restored.
  68. */
  69. assert(is_armv8_3_pauth_present());
  70. #endif /* ENABLE_PAUTH */
  71. }
  72. /*******************************************************************************
  73. * This function performs any remaining bookkeeping in the test secure payload
  74. * before the system is switched off (in response to a psci SYSTEM_OFF request).
  75. ******************************************************************************/
  76. smc_args_t *tsp_system_off_main(uint64_t arg0,
  77. uint64_t arg1,
  78. uint64_t arg2,
  79. uint64_t arg3,
  80. uint64_t arg4,
  81. uint64_t arg5,
  82. uint64_t arg6,
  83. uint64_t arg7)
  84. {
  85. uint32_t linear_id = plat_my_core_pos();
  86. /* Update this cpu's statistics. */
  87. tsp_stats[linear_id].smc_count++;
  88. tsp_stats[linear_id].eret_count++;
  89. INFO("TSP: cpu 0x%lx SYSTEM_OFF request\n", read_mpidr());
  90. INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
  91. tsp_stats[linear_id].smc_count,
  92. tsp_stats[linear_id].eret_count);
  93. /* Indicate to the SPD that we have completed this request. */
  94. return set_smc_args(TSP_SYSTEM_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
  95. }
  96. /*******************************************************************************
  97. * This function performs any remaining bookkeeping in the test secure payload
  98. * before the system is reset (in response to a psci SYSTEM_RESET request).
  99. ******************************************************************************/
  100. smc_args_t *tsp_system_reset_main(uint64_t arg0,
  101. uint64_t arg1,
  102. uint64_t arg2,
  103. uint64_t arg3,
  104. uint64_t arg4,
  105. uint64_t arg5,
  106. uint64_t arg6,
  107. uint64_t arg7)
  108. {
  109. uint32_t linear_id = plat_my_core_pos();
  110. /* Update this cpu's statistics. */
  111. tsp_stats[linear_id].smc_count++;
  112. tsp_stats[linear_id].eret_count++;
  113. INFO("TSP: cpu 0x%lx SYSTEM_RESET request\n", read_mpidr());
  114. INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
  115. tsp_stats[linear_id].smc_count,
  116. tsp_stats[linear_id].eret_count);
  117. /* Indicate to the SPD that we have completed this request. */
  118. return set_smc_args(TSP_SYSTEM_RESET_DONE, 0, 0, 0, 0, 0, 0, 0);
  119. }
  120. /*******************************************************************************
  121. * TSP smc abort handler. This function is called when aborting a preempted
  122. * yielding SMC request. It should cleanup all resources owned by the SMC
  123. * handler such as locks or dynamically allocated memory so following SMC
  124. * request are executed in a clean environment.
  125. ******************************************************************************/
  126. smc_args_t *tsp_abort_smc_handler(uint64_t func,
  127. uint64_t arg1,
  128. uint64_t arg2,
  129. uint64_t arg3,
  130. uint64_t arg4,
  131. uint64_t arg5,
  132. uint64_t arg6,
  133. uint64_t arg7)
  134. {
  135. return set_smc_args(TSP_ABORT_DONE, 0, 0, 0, 0, 0, 0, 0);
  136. }