tsp_main.c 8.9 KB

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