tsp_main.c 9.3 KB

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