tsp_main.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright (c) 2013-2024, 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/build_message.h>
  14. #include <common/debug.h>
  15. #include <lib/spinlock.h>
  16. #include <plat/common/platform.h>
  17. #include <platform_tsp.h>
  18. #include "tsp_private.h"
  19. #include <platform_def.h>
  20. /*******************************************************************************
  21. * TSP main entry point where it gets the opportunity to initialize its secure
  22. * state/applications. Once the state is initialized, it must return to the
  23. * SPD with a pointer to the 'tsp_vector_table' jump table.
  24. ******************************************************************************/
  25. uint64_t tsp_main(void)
  26. {
  27. NOTICE("TSP: %s\n", build_version_string);
  28. NOTICE("TSP: %s\n", build_message);
  29. INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE);
  30. INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE);
  31. uint32_t linear_id = plat_my_core_pos();
  32. /* Initialize the platform */
  33. tsp_platform_setup();
  34. /* Initialize secure/applications state here */
  35. tsp_generic_timer_start();
  36. /* Update this cpu's statistics */
  37. tsp_stats[linear_id].smc_count++;
  38. tsp_stats[linear_id].eret_count++;
  39. tsp_stats[linear_id].cpu_on_count++;
  40. INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
  41. read_mpidr(),
  42. tsp_stats[linear_id].smc_count,
  43. tsp_stats[linear_id].eret_count,
  44. tsp_stats[linear_id].cpu_on_count);
  45. console_flush();
  46. return (uint64_t) &tsp_vector_table;
  47. }
  48. /*******************************************************************************
  49. * This function performs any remaining book keeping in the test secure payload
  50. * after this cpu's architectural state has been setup in response to an earlier
  51. * psci cpu_on request.
  52. ******************************************************************************/
  53. smc_args_t *tsp_cpu_on_main(void)
  54. {
  55. uint32_t linear_id = plat_my_core_pos();
  56. /* Initialize secure/applications state here */
  57. tsp_generic_timer_start();
  58. /* Update this cpu's statistics */
  59. tsp_stats[linear_id].smc_count++;
  60. tsp_stats[linear_id].eret_count++;
  61. tsp_stats[linear_id].cpu_on_count++;
  62. INFO("TSP: cpu 0x%lx turned on\n", read_mpidr());
  63. INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
  64. read_mpidr(),
  65. tsp_stats[linear_id].smc_count,
  66. tsp_stats[linear_id].eret_count,
  67. tsp_stats[linear_id].cpu_on_count);
  68. /* Indicate to the SPD that we have completed turned ourselves on */
  69. return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0);
  70. }
  71. /*******************************************************************************
  72. * This function performs any remaining book keeping in the test secure payload
  73. * before this cpu is turned off in response to a psci cpu_off request.
  74. ******************************************************************************/
  75. smc_args_t *tsp_cpu_off_main(uint64_t arg0,
  76. uint64_t arg1,
  77. uint64_t arg2,
  78. uint64_t arg3,
  79. uint64_t arg4,
  80. uint64_t arg5,
  81. uint64_t arg6,
  82. uint64_t arg7)
  83. {
  84. uint32_t linear_id = plat_my_core_pos();
  85. /*
  86. * This cpu is being turned off, so disable the timer to prevent the
  87. * secure timer interrupt from interfering with power down. A pending
  88. * interrupt will be lost but we do not care as we are turning off.
  89. */
  90. tsp_generic_timer_stop();
  91. /* Update this cpu's statistics */
  92. tsp_stats[linear_id].smc_count++;
  93. tsp_stats[linear_id].eret_count++;
  94. tsp_stats[linear_id].cpu_off_count++;
  95. INFO("TSP: cpu 0x%lx off request\n", read_mpidr());
  96. INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n",
  97. read_mpidr(),
  98. tsp_stats[linear_id].smc_count,
  99. tsp_stats[linear_id].eret_count,
  100. tsp_stats[linear_id].cpu_off_count);
  101. /* Indicate to the SPD that we have completed this request */
  102. return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
  103. }
  104. /*******************************************************************************
  105. * This function performs any book keeping in the test secure payload before
  106. * this cpu's architectural state is saved in response to an earlier psci
  107. * cpu_suspend request.
  108. ******************************************************************************/
  109. smc_args_t *tsp_cpu_suspend_main(uint64_t arg0,
  110. uint64_t arg1,
  111. uint64_t arg2,
  112. uint64_t arg3,
  113. uint64_t arg4,
  114. uint64_t arg5,
  115. uint64_t arg6,
  116. uint64_t arg7)
  117. {
  118. uint32_t linear_id = plat_my_core_pos();
  119. /*
  120. * Save the time context and disable it to prevent the secure timer
  121. * interrupt from interfering with wakeup from the suspend state.
  122. */
  123. tsp_generic_timer_save();
  124. tsp_generic_timer_stop();
  125. /* Update this cpu's statistics */
  126. tsp_stats[linear_id].smc_count++;
  127. tsp_stats[linear_id].eret_count++;
  128. tsp_stats[linear_id].cpu_suspend_count++;
  129. INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
  130. read_mpidr(),
  131. tsp_stats[linear_id].smc_count,
  132. tsp_stats[linear_id].eret_count,
  133. tsp_stats[linear_id].cpu_suspend_count);
  134. /* Indicate to the SPD that we have completed this request */
  135. return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0);
  136. }
  137. /*******************************************************************************
  138. * This function performs any book keeping in the test secure payload after this
  139. * cpu's architectural state has been restored after wakeup from an earlier psci
  140. * cpu_suspend request.
  141. ******************************************************************************/
  142. smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
  143. uint64_t arg1,
  144. uint64_t arg2,
  145. uint64_t arg3,
  146. uint64_t arg4,
  147. uint64_t arg5,
  148. uint64_t arg6,
  149. uint64_t arg7)
  150. {
  151. uint32_t linear_id = plat_my_core_pos();
  152. /* Restore the generic timer context */
  153. tsp_generic_timer_restore();
  154. /* Update this cpu's statistics */
  155. tsp_stats[linear_id].smc_count++;
  156. tsp_stats[linear_id].eret_count++;
  157. tsp_stats[linear_id].cpu_resume_count++;
  158. INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n",
  159. read_mpidr(), max_off_pwrlvl);
  160. INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n",
  161. read_mpidr(),
  162. tsp_stats[linear_id].smc_count,
  163. tsp_stats[linear_id].eret_count,
  164. tsp_stats[linear_id].cpu_resume_count);
  165. /* Indicate to the SPD that we have completed this request */
  166. return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
  167. }
  168. /*******************************************************************************
  169. * TSP fast smc handler. The secure monitor jumps to this function by
  170. * doing the ERET after populating X0-X7 registers. The arguments are received
  171. * in the function arguments in order. Once the service is rendered, this
  172. * function returns to Secure Monitor by raising SMC.
  173. ******************************************************************************/
  174. smc_args_t *tsp_smc_handler(uint64_t func,
  175. uint64_t arg1,
  176. uint64_t arg2,
  177. uint64_t arg3,
  178. uint64_t arg4,
  179. uint64_t arg5,
  180. uint64_t arg6,
  181. uint64_t arg7)
  182. {
  183. uint128_t service_args;
  184. uint64_t service_arg0;
  185. uint64_t service_arg1;
  186. uint64_t results[2];
  187. uint32_t linear_id = plat_my_core_pos();
  188. u_register_t dit;
  189. /* Update this cpu's statistics */
  190. tsp_stats[linear_id].smc_count++;
  191. tsp_stats[linear_id].eret_count++;
  192. INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(),
  193. ((func >> 31) & 1) == 1 ? "fast" : "yielding",
  194. func);
  195. INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(),
  196. tsp_stats[linear_id].smc_count,
  197. tsp_stats[linear_id].eret_count);
  198. /* Render secure services and obtain results here */
  199. results[0] = arg1;
  200. results[1] = arg2;
  201. /*
  202. * Request a service back from dispatcher/secure monitor.
  203. * This call returns and thereafter resumes execution.
  204. */
  205. service_args = tsp_get_magic();
  206. service_arg0 = (uint64_t)service_args;
  207. service_arg1 = (uint64_t)(service_args >> 64U);
  208. /*
  209. * Write a dummy value to an MTE2 register, to simulate usage in the
  210. * secure world
  211. */
  212. if (is_feat_mte2_supported()) {
  213. write_gcr_el1(0x99);
  214. }
  215. /* Determine the function to perform based on the function ID */
  216. switch (TSP_BARE_FID(func)) {
  217. case TSP_ADD:
  218. results[0] += service_arg0;
  219. results[1] += service_arg1;
  220. break;
  221. case TSP_SUB:
  222. results[0] -= service_arg0;
  223. results[1] -= service_arg1;
  224. break;
  225. case TSP_MUL:
  226. results[0] *= service_arg0;
  227. results[1] *= service_arg1;
  228. break;
  229. case TSP_DIV:
  230. results[0] /= service_arg0 ? service_arg0 : 1;
  231. results[1] /= service_arg1 ? service_arg1 : 1;
  232. break;
  233. case TSP_CHECK_DIT:
  234. if (!is_feat_dit_supported()) {
  235. ERROR("DIT not supported\n");
  236. results[0] = 0;
  237. results[1] = 0xffff;
  238. break;
  239. }
  240. dit = read_dit();
  241. results[0] = dit == service_arg0;
  242. results[1] = dit;
  243. /* Toggle the dit bit */
  244. write_dit(service_arg0 != 0U ? 0 : DIT_BIT);
  245. break;
  246. default:
  247. break;
  248. }
  249. return set_smc_args(func, 0,
  250. results[0],
  251. results[1],
  252. 0, 0, 0, 0);
  253. }