tsp_ffa_main.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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 "../../services/std_svc/spm/el3_spmc/spmc.h"
  10. #include "../../services/std_svc/spm/el3_spmc/spmc_shared_mem.h"
  11. #include <arch_features.h>
  12. #include <arch_helpers.h>
  13. #include <bl32/tsp/tsp.h>
  14. #include <common/bl_common.h>
  15. #include <common/debug.h>
  16. #include "ffa_helpers.h"
  17. #include <lib/psci/psci.h>
  18. #include <lib/spinlock.h>
  19. #include <lib/xlat_tables/xlat_tables_defs.h>
  20. #include <lib/xlat_tables/xlat_tables_v2.h>
  21. #include <plat/common/platform.h>
  22. #include <platform_tsp.h>
  23. #include <services/ffa_svc.h>
  24. #include "tsp_private.h"
  25. #include <platform_def.h>
  26. static ffa_endpoint_id16_t tsp_id, spmc_id;
  27. uint8_t mem_region_buffer[4096 * 2] __aligned(PAGE_SIZE);
  28. /* Partition Mailbox. */
  29. static uint8_t send_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  30. static uint8_t recv_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  31. /*
  32. * Declare a global mailbox for use within the TSP.
  33. * This will be initialized appropriately when the buffers
  34. * are mapped with the SPMC.
  35. */
  36. static struct mailbox mailbox;
  37. /*******************************************************************************
  38. * This enum is used to handle test cases driven from the FF-A Test Driver.
  39. ******************************************************************************/
  40. /* Keep in Sync with FF-A Test Driver. */
  41. enum message_t {
  42. /* Partition Only Messages. */
  43. FF_A_RELAY_MESSAGE = 0,
  44. /* Basic Functionality. */
  45. FF_A_ECHO_MESSAGE,
  46. FF_A_RELAY_MESSAGE_EL3,
  47. /* Memory Sharing. */
  48. FF_A_MEMORY_SHARE,
  49. FF_A_MEMORY_SHARE_FRAGMENTED,
  50. FF_A_MEMORY_LEND,
  51. FF_A_MEMORY_LEND_FRAGMENTED,
  52. FF_A_MEMORY_SHARE_MULTI_ENDPOINT,
  53. FF_A_MEMORY_LEND_MULTI_ENDPOINT,
  54. LAST,
  55. FF_A_RUN_ALL = 255,
  56. FF_A_OP_MAX = 256
  57. };
  58. #if SPMC_AT_EL3
  59. extern void tsp_cpu_on_entry(void);
  60. #endif
  61. /*******************************************************************************
  62. * Test Functions.
  63. ******************************************************************************/
  64. /*******************************************************************************
  65. * Enable the TSP to forward the received message to another partition and ask
  66. * it to echo the value back in order to validate direct messages functionality.
  67. ******************************************************************************/
  68. static int ffa_test_relay(uint64_t arg0,
  69. uint64_t arg1,
  70. uint64_t arg2,
  71. uint64_t arg3,
  72. uint64_t arg4,
  73. uint64_t arg5,
  74. uint64_t arg6,
  75. uint64_t arg7)
  76. {
  77. smc_args_t ffa_forward_result;
  78. ffa_endpoint_id16_t receiver = arg5;
  79. ffa_forward_result = ffa_msg_send_direct_req(tsp_id,
  80. receiver,
  81. FF_A_ECHO_MESSAGE, arg4,
  82. 0, 0, 0);
  83. return ffa_forward_result._regs[3];
  84. }
  85. /*******************************************************************************
  86. * This function handles memory management tests, currently share and lend.
  87. * This test supports the use of FRAG_RX to use memory descriptors that do not
  88. * fit in a single 4KB buffer.
  89. ******************************************************************************/
  90. static int test_memory_send(ffa_endpoint_id16_t sender, uint64_t handle,
  91. ffa_mtd_flag32_t flags, bool multi_endpoint)
  92. {
  93. struct ffa_mtd *m;
  94. struct ffa_emad_v1_0 *receivers;
  95. struct ffa_comp_mrd *composite;
  96. int ret, status = 0;
  97. unsigned int mem_attrs;
  98. char *ptr;
  99. ffa_endpoint_id16_t source = sender;
  100. uint32_t total_length, recv_length = 0;
  101. /*
  102. * In the case that we're testing multiple endpoints choose a partition
  103. * ID that resides in the normal world so the SPMC won't detect it as
  104. * invalid.
  105. * TODO: Should get endpoint receiver id and flag as input from NWd.
  106. */
  107. uint32_t receiver_count = multi_endpoint ? 2 : 1;
  108. ffa_endpoint_id16_t test_receivers[2] = { tsp_id, 0x10 };
  109. /* Ensure that the sender ID resides in the normal world. */
  110. if (ffa_is_secure_world_id(sender)) {
  111. ERROR("Invalid sender ID 0x%x.\n", sender);
  112. return FFA_ERROR_DENIED;
  113. }
  114. if (!memory_retrieve(&mailbox, &m, handle, source, test_receivers,
  115. receiver_count, flags, &recv_length,
  116. &total_length)) {
  117. return FFA_ERROR_INVALID_PARAMETER;
  118. }
  119. receivers = (struct ffa_emad_v1_0 *)
  120. ((uint8_t *) m + m->emad_offset);
  121. while (total_length != recv_length) {
  122. smc_args_t ffa_return;
  123. uint32_t frag_length;
  124. ffa_return = ffa_mem_frag_rx(handle, recv_length);
  125. if (ffa_return._regs[0] == FFA_ERROR) {
  126. WARN("TSP: failed to resume mem with handle %lx\n",
  127. handle);
  128. return ffa_return._regs[2];
  129. }
  130. frag_length = ffa_return._regs[3];
  131. /* Validate frag_length is less than total_length and mailbox size. */
  132. if (frag_length > total_length ||
  133. frag_length > (mailbox.rxtx_page_count * PAGE_SIZE)) {
  134. ERROR("Invalid parameters!\n");
  135. return FFA_ERROR_INVALID_PARAMETER;
  136. }
  137. /* Validate frag_length is less than remaining mem_region_buffer size. */
  138. if (frag_length + recv_length >= REGION_BUF_SIZE) {
  139. ERROR("Out of memory!\n");
  140. return FFA_ERROR_INVALID_PARAMETER;
  141. }
  142. memcpy(&mem_region_buffer[recv_length], mailbox.rx_buffer,
  143. frag_length);
  144. if (ffa_rx_release()) {
  145. ERROR("Failed to release buffer!\n");
  146. return FFA_ERROR_DENIED;
  147. }
  148. recv_length += frag_length;
  149. assert(recv_length <= total_length);
  150. }
  151. composite = ffa_memory_region_get_composite(m, 0);
  152. if (composite == NULL) {
  153. WARN("Failed to get composite descriptor!\n");
  154. return FFA_ERROR_INVALID_PARAMETER;
  155. }
  156. VERBOSE("Address: %p; page_count: %x %lx\n",
  157. (void *)composite->address_range_array[0].address,
  158. composite->address_range_array[0].page_count, PAGE_SIZE);
  159. /* This test is only concerned with RW permissions. */
  160. if (ffa_get_data_access_attr(
  161. receivers[0].mapd.memory_access_permissions) != FFA_MEM_PERM_RW) {
  162. ERROR("Data permission in retrieve response %x does not match share/lend %x!\n",
  163. ffa_get_data_access_attr(receivers[0].mapd.memory_access_permissions),
  164. FFA_MEM_PERM_RW);
  165. return FFA_ERROR_INVALID_PARAMETER;
  166. }
  167. mem_attrs = MT_RW_DATA | MT_EXECUTE_NEVER;
  168. /* Only expecting to be sent memory from NWd so map accordingly. */
  169. mem_attrs |= MT_NS;
  170. for (int32_t i = 0; i < (int32_t)composite->address_range_count; i++) {
  171. size_t size = composite->address_range_array[i].page_count * PAGE_SIZE;
  172. ptr = (char *) composite->address_range_array[i].address;
  173. ret = mmap_add_dynamic_region(
  174. (uint64_t)ptr,
  175. (uint64_t)ptr,
  176. size, mem_attrs);
  177. if (ret != 0) {
  178. ERROR("Failed [%d] mmap_add_dynamic_region %u (%lx) (%lx) (%x)!\n",
  179. i, ret,
  180. (uint64_t)composite->address_range_array[i].address,
  181. size, mem_attrs);
  182. /* Remove mappings previously created in this transaction. */
  183. for (i--; i >= 0; i--) {
  184. ret = mmap_remove_dynamic_region(
  185. (uint64_t)composite->address_range_array[i].address,
  186. composite->address_range_array[i].page_count * PAGE_SIZE);
  187. if (ret != 0) {
  188. ERROR("Failed [%d] mmap_remove_dynamic_region!\n", i);
  189. panic();
  190. }
  191. }
  192. return FFA_ERROR_NO_MEMORY;
  193. }
  194. /* Increment memory region for validation purposes. */
  195. ++(*ptr);
  196. /*
  197. * Read initial magic number from memory region for
  198. * validation purposes.
  199. */
  200. if (!i) {
  201. status = *ptr;
  202. }
  203. }
  204. for (uint32_t i = 0U; i < composite->address_range_count; i++) {
  205. ret = mmap_remove_dynamic_region(
  206. (uint64_t)composite->address_range_array[i].address,
  207. composite->address_range_array[i].page_count * PAGE_SIZE);
  208. if (ret != 0) {
  209. ERROR("Failed [%d] mmap_remove_dynamic_region!\n", i);
  210. return FFA_ERROR_NO_MEMORY;
  211. }
  212. }
  213. if (!memory_relinquish((struct ffa_mem_relinquish_descriptor *)mailbox.tx_buffer,
  214. m->handle, tsp_id)) {
  215. ERROR("Failed to relinquish memory region!\n");
  216. return FFA_ERROR_INVALID_PARAMETER;
  217. }
  218. return status;
  219. }
  220. static smc_args_t *send_ffa_pm_success(void)
  221. {
  222. return set_smc_args(FFA_MSG_SEND_DIRECT_RESP_SMC32,
  223. ((tsp_id & FFA_DIRECT_MSG_ENDPOINT_ID_MASK)
  224. << FFA_DIRECT_MSG_SOURCE_SHIFT) | spmc_id,
  225. FFA_FWK_MSG_BIT |
  226. (FFA_PM_MSG_PM_RESP & FFA_FWK_MSG_MASK),
  227. 0, 0, 0, 0, 0);
  228. }
  229. /*******************************************************************************
  230. * This function performs any remaining book keeping in the test secure payload
  231. * before this cpu is turned off in response to a psci cpu_off request.
  232. ******************************************************************************/
  233. smc_args_t *tsp_cpu_off_main(uint64_t arg0,
  234. uint64_t arg1,
  235. uint64_t arg2,
  236. uint64_t arg3,
  237. uint64_t arg4,
  238. uint64_t arg5,
  239. uint64_t arg6,
  240. uint64_t arg7)
  241. {
  242. uint32_t linear_id = plat_my_core_pos();
  243. /*
  244. * This cpu is being turned off, so disable the timer to prevent the
  245. * secure timer interrupt from interfering with power down. A pending
  246. * interrupt will be lost but we do not care as we are turning off.
  247. */
  248. tsp_generic_timer_stop();
  249. /* Update this cpu's statistics. */
  250. tsp_stats[linear_id].smc_count++;
  251. tsp_stats[linear_id].eret_count++;
  252. tsp_stats[linear_id].cpu_off_count++;
  253. VERBOSE("TSP: cpu 0x%lx off request\n", read_mpidr());
  254. VERBOSE("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n",
  255. read_mpidr(),
  256. tsp_stats[linear_id].smc_count,
  257. tsp_stats[linear_id].eret_count,
  258. tsp_stats[linear_id].cpu_off_count);
  259. return send_ffa_pm_success();
  260. }
  261. /*******************************************************************************
  262. * This function performs any book keeping in the test secure payload before
  263. * this cpu's architectural state is saved in response to an earlier psci
  264. * cpu_suspend request.
  265. ******************************************************************************/
  266. smc_args_t *tsp_cpu_suspend_main(uint64_t arg0,
  267. uint64_t arg1,
  268. uint64_t arg2,
  269. uint64_t arg3,
  270. uint64_t arg4,
  271. uint64_t arg5,
  272. uint64_t arg6,
  273. uint64_t arg7)
  274. {
  275. uint32_t linear_id = plat_my_core_pos();
  276. /*
  277. * Save the time context and disable it to prevent the secure timer
  278. * interrupt from interfering with wakeup from the suspend state.
  279. */
  280. tsp_generic_timer_save();
  281. tsp_generic_timer_stop();
  282. /* Update this cpu's statistics. */
  283. tsp_stats[linear_id].smc_count++;
  284. tsp_stats[linear_id].eret_count++;
  285. tsp_stats[linear_id].cpu_suspend_count++;
  286. VERBOSE("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
  287. read_mpidr(),
  288. tsp_stats[linear_id].smc_count,
  289. tsp_stats[linear_id].eret_count,
  290. tsp_stats[linear_id].cpu_suspend_count);
  291. return send_ffa_pm_success();
  292. }
  293. /*******************************************************************************
  294. * This function performs any bookkeeping in the test secure payload after this
  295. * cpu's architectural state has been restored after wakeup from an earlier psci
  296. * cpu_suspend request.
  297. ******************************************************************************/
  298. smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
  299. uint64_t arg1,
  300. uint64_t arg2,
  301. uint64_t arg3,
  302. uint64_t arg4,
  303. uint64_t arg5,
  304. uint64_t arg6,
  305. uint64_t arg7)
  306. {
  307. uint32_t linear_id = plat_my_core_pos();
  308. /* Restore the generic timer context. */
  309. tsp_generic_timer_restore();
  310. /* Update this cpu's statistics. */
  311. tsp_stats[linear_id].smc_count++;
  312. tsp_stats[linear_id].eret_count++;
  313. tsp_stats[linear_id].cpu_resume_count++;
  314. VERBOSE("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n",
  315. read_mpidr(), max_off_pwrlvl);
  316. VERBOSE("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n",
  317. read_mpidr(),
  318. tsp_stats[linear_id].smc_count,
  319. tsp_stats[linear_id].eret_count,
  320. tsp_stats[linear_id].cpu_resume_count);
  321. return send_ffa_pm_success();
  322. }
  323. /*******************************************************************************
  324. * This function handles framework messages. Currently only PM.
  325. ******************************************************************************/
  326. static smc_args_t *handle_framework_message(uint64_t arg0,
  327. uint64_t arg1,
  328. uint64_t arg2,
  329. uint64_t arg3,
  330. uint64_t arg4,
  331. uint64_t arg5,
  332. uint64_t arg6,
  333. uint64_t arg7)
  334. {
  335. /* Check if it is a power management message from the SPMC. */
  336. if (ffa_endpoint_source(arg1) != spmc_id) {
  337. goto err;
  338. }
  339. /* Check if it is a PM request message. */
  340. if ((arg2 & FFA_FWK_MSG_MASK) == FFA_FWK_MSG_PSCI) {
  341. /* Check if it is a PSCI CPU_OFF request. */
  342. if (arg3 == PSCI_CPU_OFF) {
  343. return tsp_cpu_off_main(arg0, arg1, arg2, arg3,
  344. arg4, arg5, arg6, arg7);
  345. } else if (arg3 == PSCI_CPU_SUSPEND_AARCH64) {
  346. return tsp_cpu_suspend_main(arg0, arg1, arg2, arg3,
  347. arg4, arg5, arg6, arg7);
  348. }
  349. } else if ((arg2 & FFA_FWK_MSG_MASK) == FFA_PM_MSG_WB_REQ) {
  350. /* Check it is a PSCI Warm Boot request. */
  351. if (arg3 == FFA_WB_TYPE_NOTS2RAM) {
  352. return tsp_cpu_resume_main(arg0, arg1, arg2, arg3,
  353. arg4, arg5, arg6, arg7);
  354. }
  355. }
  356. err:
  357. ERROR("%s: Unknown framework message!\n", __func__);
  358. panic();
  359. }
  360. /*******************************************************************************
  361. * Handles partition messages. Exercised from the FF-A Test Driver.
  362. ******************************************************************************/
  363. static smc_args_t *handle_partition_message(uint64_t arg0,
  364. uint64_t arg1,
  365. uint64_t arg2,
  366. uint64_t arg3,
  367. uint64_t arg4,
  368. uint64_t arg5,
  369. uint64_t arg6,
  370. uint64_t arg7)
  371. {
  372. uint16_t sender = ffa_endpoint_source(arg1);
  373. uint16_t receiver = ffa_endpoint_destination(arg1);
  374. int status = -1;
  375. const bool multi_endpoint = true;
  376. switch (arg3) {
  377. case FF_A_MEMORY_SHARE:
  378. INFO("TSP Tests: Memory Share Request--\n");
  379. status = test_memory_send(sender, arg4, FFA_FLAG_SHARE_MEMORY, !multi_endpoint);
  380. break;
  381. case FF_A_MEMORY_LEND:
  382. INFO("TSP Tests: Memory Lend Request--\n");
  383. status = test_memory_send(sender, arg4, FFA_FLAG_LEND_MEMORY, !multi_endpoint);
  384. break;
  385. case FF_A_MEMORY_SHARE_MULTI_ENDPOINT:
  386. INFO("TSP Tests: Multi Endpoint Memory Share Request--\n");
  387. status = test_memory_send(sender, arg4, FFA_FLAG_SHARE_MEMORY, multi_endpoint);
  388. break;
  389. case FF_A_MEMORY_LEND_MULTI_ENDPOINT:
  390. INFO("TSP Tests: Multi Endpoint Memory Lend Request--\n");
  391. status = test_memory_send(sender, arg4, FFA_FLAG_LEND_MEMORY, multi_endpoint);
  392. break;
  393. case FF_A_RELAY_MESSAGE:
  394. INFO("TSP Tests: Relaying message--\n");
  395. status = ffa_test_relay(arg0, arg1, arg2, arg3, arg4,
  396. arg5, arg6, arg7);
  397. break;
  398. case FF_A_ECHO_MESSAGE:
  399. INFO("TSP Tests: echo message--\n");
  400. status = arg4;
  401. break;
  402. default:
  403. INFO("TSP Tests: Unknown request ID %d--\n", (int) arg3);
  404. }
  405. /* Swap the sender and receiver in the response. */
  406. return ffa_msg_send_direct_resp(receiver, sender, status, 0, 0, 0, 0);
  407. }
  408. /*******************************************************************************
  409. * This function implements the event loop for handling FF-A ABI invocations.
  410. ******************************************************************************/
  411. static smc_args_t *tsp_event_loop(uint64_t smc_fid,
  412. uint64_t arg1,
  413. uint64_t arg2,
  414. uint64_t arg3,
  415. uint64_t arg4,
  416. uint64_t arg5,
  417. uint64_t arg6,
  418. uint64_t arg7)
  419. {
  420. /* Panic if the SPMC did not forward an FF-A call. */
  421. if (!is_ffa_fid(smc_fid)) {
  422. ERROR("%s: Unknown SMC FID (0x%lx)\n", __func__, smc_fid);
  423. panic();
  424. }
  425. switch (smc_fid) {
  426. case FFA_INTERRUPT:
  427. /*
  428. * IRQs were enabled upon re-entry into the TSP. The interrupt
  429. * must have been handled by now. Return to the SPMC indicating
  430. * the same.
  431. */
  432. return set_smc_args(FFA_MSG_WAIT, 0, 0, 0, 0, 0, 0, 0);
  433. case FFA_MSG_SEND_DIRECT_REQ_SMC64:
  434. case FFA_MSG_SEND_DIRECT_REQ_SMC32:
  435. /* Check if a framework message, handle accordingly. */
  436. if ((arg2 & FFA_FWK_MSG_BIT)) {
  437. return handle_framework_message(smc_fid, arg1, arg2, arg3,
  438. arg4, arg5, arg6, arg7);
  439. }
  440. return handle_partition_message(smc_fid, arg1, arg2, arg3,
  441. arg4, arg5, arg6, arg7);
  442. }
  443. ERROR("%s: Unsupported FF-A FID (0x%lx)\n", __func__, smc_fid);
  444. panic();
  445. }
  446. static smc_args_t *tsp_loop(smc_args_t *args)
  447. {
  448. smc_args_t ret;
  449. do {
  450. /* --------------------------------------------
  451. * Mask FIQ interrupts to avoid preemption
  452. * in case EL3 SPMC delegates an IRQ next or a
  453. * managed exit. Lastly, unmask IRQs so that
  454. * they can be handled immediately upon re-entry.
  455. * ---------------------------------------------
  456. */
  457. write_daifset(DAIF_FIQ_BIT);
  458. write_daifclr(DAIF_IRQ_BIT);
  459. ret = smc_helper(args->_regs[0], args->_regs[1], args->_regs[2],
  460. args->_regs[3], args->_regs[4], args->_regs[5],
  461. args->_regs[6], args->_regs[7]);
  462. args = tsp_event_loop(ret._regs[0], ret._regs[1], ret._regs[2],
  463. ret._regs[3], ret._regs[4], ret._regs[5],
  464. ret._regs[6], ret._regs[7]);
  465. } while (1);
  466. /* Not Reached. */
  467. return NULL;
  468. }
  469. /*******************************************************************************
  470. * TSP main entry point where it gets the opportunity to initialize its secure
  471. * state/applications. Once the state is initialized, it must return to the
  472. * SPD with a pointer to the 'tsp_vector_table' jump table.
  473. ******************************************************************************/
  474. uint64_t tsp_main(void)
  475. {
  476. smc_args_t smc_args = {0};
  477. NOTICE("TSP: %s\n", version_string);
  478. NOTICE("TSP: %s\n", build_message);
  479. INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE);
  480. INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE);
  481. uint32_t linear_id = plat_my_core_pos();
  482. /* Initialize the platform. */
  483. tsp_platform_setup();
  484. /* Initialize secure/applications state here. */
  485. tsp_generic_timer_start();
  486. /* Register secondary entrypoint with the SPMC. */
  487. smc_args = smc_helper(FFA_SECONDARY_EP_REGISTER_SMC64,
  488. (uint64_t) tsp_cpu_on_entry,
  489. 0, 0, 0, 0, 0, 0);
  490. if (smc_args._regs[SMC_ARG0] != FFA_SUCCESS_SMC32) {
  491. ERROR("TSP could not register secondary ep (0x%lx)\n",
  492. smc_args._regs[2]);
  493. panic();
  494. }
  495. /* Get TSP's endpoint id. */
  496. smc_args = smc_helper(FFA_ID_GET, 0, 0, 0, 0, 0, 0, 0);
  497. if (smc_args._regs[SMC_ARG0] != FFA_SUCCESS_SMC32) {
  498. ERROR("TSP could not get own ID (0x%lx) on core%d\n",
  499. smc_args._regs[2], linear_id);
  500. panic();
  501. }
  502. tsp_id = smc_args._regs[2];
  503. INFO("TSP FF-A endpoint id = 0x%x\n", tsp_id);
  504. /* Get the SPMC ID. */
  505. smc_args = smc_helper(FFA_SPM_ID_GET, 0, 0, 0, 0, 0, 0, 0);
  506. if (smc_args._regs[SMC_ARG0] != FFA_SUCCESS_SMC32) {
  507. ERROR("TSP could not get SPMC ID (0x%lx) on core%d\n",
  508. smc_args._regs[2], linear_id);
  509. panic();
  510. }
  511. spmc_id = smc_args._regs[2];
  512. /* Call RXTX_MAP to map a 4k RX and TX buffer. */
  513. if (ffa_rxtx_map((uintptr_t) send_page,
  514. (uintptr_t) recv_page, 1)) {
  515. ERROR("TSP could not map it's RX/TX Buffers\n");
  516. panic();
  517. }
  518. mailbox.tx_buffer = send_page;
  519. mailbox.rx_buffer = recv_page;
  520. mailbox.rxtx_page_count = 1;
  521. /* Update this cpu's statistics. */
  522. tsp_stats[linear_id].smc_count++;
  523. tsp_stats[linear_id].eret_count++;
  524. tsp_stats[linear_id].cpu_on_count++;
  525. VERBOSE("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
  526. read_mpidr(),
  527. tsp_stats[linear_id].smc_count,
  528. tsp_stats[linear_id].eret_count,
  529. tsp_stats[linear_id].cpu_on_count);
  530. /* Tell SPMD that we are done initialising. */
  531. tsp_loop(set_smc_args(FFA_MSG_WAIT, 0, 0, 0, 0, 0, 0, 0));
  532. /* Not reached. */
  533. return 0;
  534. }
  535. /*******************************************************************************
  536. * This function performs any remaining book keeping in the test secure payload
  537. * after this cpu's architectural state has been setup in response to an earlier
  538. * psci cpu_on request.
  539. ******************************************************************************/
  540. smc_args_t *tsp_cpu_on_main(void)
  541. {
  542. uint32_t linear_id = plat_my_core_pos();
  543. /* Initialize secure/applications state here. */
  544. tsp_generic_timer_start();
  545. /* Update this cpu's statistics. */
  546. tsp_stats[linear_id].smc_count++;
  547. tsp_stats[linear_id].eret_count++;
  548. tsp_stats[linear_id].cpu_on_count++;
  549. VERBOSE("TSP: cpu 0x%lx turned on\n", read_mpidr());
  550. VERBOSE("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
  551. read_mpidr(),
  552. tsp_stats[linear_id].smc_count,
  553. tsp_stats[linear_id].eret_count,
  554. tsp_stats[linear_id].cpu_on_count);
  555. /* ---------------------------------------------
  556. * Jump to the main event loop to return to EL3
  557. * and be ready for the next request on this cpu.
  558. * ---------------------------------------------
  559. */
  560. return tsp_loop(set_smc_args(FFA_MSG_WAIT, 0, 0, 0, 0, 0, 0, 0));
  561. }