xref: /rk3399_ARM-atf/bl32/tsp/tsp_main.c (revision ed108b56051de5da8024568a06781ce287e86c78)
17c88f3f6SAchin Gupta /*
28aabea33SPaul Beesley  * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
37c88f3f6SAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57c88f3f6SAchin Gupta  */
67c88f3f6SAchin Gupta 
7*ed108b56SAlexei Fedorov #include <assert.h>
809d40e0eSAntonio Nino Diaz 
9*ed108b56SAlexei Fedorov #include <arch_features.h>
1009d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1109d40e0eSAntonio Nino Diaz #include <bl32/tsp/tsp.h>
1209d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
1309d40e0eSAntonio Nino Diaz #include <common/debug.h>
1409d40e0eSAntonio Nino Diaz #include <lib/spinlock.h>
1509d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
16*ed108b56SAlexei Fedorov #include <platform_def.h>
175a06bb7eSDan Handley #include <platform_tsp.h>
1809d40e0eSAntonio Nino Diaz 
19da0af78aSDan Handley #include "tsp_private.h"
207c88f3f6SAchin Gupta 
216871c5d3SVikram Kanigiri 
226871c5d3SVikram Kanigiri /*******************************************************************************
237c88f3f6SAchin Gupta  * Lock to control access to the console
247c88f3f6SAchin Gupta  ******************************************************************************/
257c88f3f6SAchin Gupta spinlock_t console_lock;
267c88f3f6SAchin Gupta 
277c88f3f6SAchin Gupta /*******************************************************************************
287c88f3f6SAchin Gupta  * Per cpu data structure to populate parameters for an SMC in C code and use
297c88f3f6SAchin Gupta  * a pointer to this structure in assembler code to populate x0-x7
307c88f3f6SAchin Gupta  ******************************************************************************/
31fb037bfbSDan Handley static tsp_args_t tsp_smc_args[PLATFORM_CORE_COUNT];
327c88f3f6SAchin Gupta 
337c88f3f6SAchin Gupta /*******************************************************************************
347c88f3f6SAchin Gupta  * Per cpu data structure to keep track of TSP activity
357c88f3f6SAchin Gupta  ******************************************************************************/
366cf89021SAchin Gupta work_statistics_t tsp_stats[PLATFORM_CORE_COUNT];
377c88f3f6SAchin Gupta 
387c88f3f6SAchin Gupta /*******************************************************************************
39a604623cSSandrine Bailleux  * The TSP memory footprint starts at address BL32_BASE and ends with the
40a604623cSSandrine Bailleux  * linker symbol __BL32_END__. Use these addresses to compute the TSP image
41a604623cSSandrine Bailleux  * size.
426871c5d3SVikram Kanigiri  ******************************************************************************/
43f6605337SAntonio Nino Diaz #define BL32_TOTAL_LIMIT BL32_END
44a604623cSSandrine Bailleux #define BL32_TOTAL_SIZE (BL32_TOTAL_LIMIT - (unsigned long) BL32_BASE)
456871c5d3SVikram Kanigiri 
46fb037bfbSDan Handley static tsp_args_t *set_smc_args(uint64_t arg0,
477c88f3f6SAchin Gupta 			     uint64_t arg1,
487c88f3f6SAchin Gupta 			     uint64_t arg2,
497c88f3f6SAchin Gupta 			     uint64_t arg3,
507c88f3f6SAchin Gupta 			     uint64_t arg4,
517c88f3f6SAchin Gupta 			     uint64_t arg5,
527c88f3f6SAchin Gupta 			     uint64_t arg6,
537c88f3f6SAchin Gupta 			     uint64_t arg7)
547c88f3f6SAchin Gupta {
557c88f3f6SAchin Gupta 	uint32_t linear_id;
56fb037bfbSDan Handley 	tsp_args_t *pcpu_smc_args;
577c88f3f6SAchin Gupta 
587c88f3f6SAchin Gupta 	/*
597c88f3f6SAchin Gupta 	 * Return to Secure Monitor by raising an SMC. The results of the
607c88f3f6SAchin Gupta 	 * service are passed as an arguments to the SMC
617c88f3f6SAchin Gupta 	 */
62fd650ff6SSoby Mathew 	linear_id = plat_my_core_pos();
637c88f3f6SAchin Gupta 	pcpu_smc_args = &tsp_smc_args[linear_id];
647c88f3f6SAchin Gupta 	write_sp_arg(pcpu_smc_args, TSP_ARG0, arg0);
657c88f3f6SAchin Gupta 	write_sp_arg(pcpu_smc_args, TSP_ARG1, arg1);
667c88f3f6SAchin Gupta 	write_sp_arg(pcpu_smc_args, TSP_ARG2, arg2);
677c88f3f6SAchin Gupta 	write_sp_arg(pcpu_smc_args, TSP_ARG3, arg3);
687c88f3f6SAchin Gupta 	write_sp_arg(pcpu_smc_args, TSP_ARG4, arg4);
697c88f3f6SAchin Gupta 	write_sp_arg(pcpu_smc_args, TSP_ARG5, arg5);
707c88f3f6SAchin Gupta 	write_sp_arg(pcpu_smc_args, TSP_ARG6, arg6);
717c88f3f6SAchin Gupta 	write_sp_arg(pcpu_smc_args, TSP_ARG7, arg7);
727c88f3f6SAchin Gupta 
737c88f3f6SAchin Gupta 	return pcpu_smc_args;
747c88f3f6SAchin Gupta }
757c88f3f6SAchin Gupta 
767c88f3f6SAchin Gupta /*******************************************************************************
7767b6ff9fSAntonio Nino Diaz  * Setup function for TSP.
7867b6ff9fSAntonio Nino Diaz  ******************************************************************************/
7967b6ff9fSAntonio Nino Diaz void tsp_setup(void)
8067b6ff9fSAntonio Nino Diaz {
8167b6ff9fSAntonio Nino Diaz 	/* Perform early platform-specific setup */
8267b6ff9fSAntonio Nino Diaz 	tsp_early_platform_setup();
8367b6ff9fSAntonio Nino Diaz 
8467b6ff9fSAntonio Nino Diaz 	/* Perform late platform-specific setup */
8567b6ff9fSAntonio Nino Diaz 	tsp_plat_arch_setup();
86*ed108b56SAlexei Fedorov 
87*ed108b56SAlexei Fedorov #if ENABLE_PAUTH
88*ed108b56SAlexei Fedorov 	/*
89*ed108b56SAlexei Fedorov 	 * Assert that the ARMv8.3-PAuth registers are present or an access
90*ed108b56SAlexei Fedorov 	 * fault will be triggered when they are being saved or restored.
91*ed108b56SAlexei Fedorov 	 */
92*ed108b56SAlexei Fedorov 	assert(is_armv8_3_pauth_present());
93*ed108b56SAlexei Fedorov #endif /* ENABLE_PAUTH */
9467b6ff9fSAntonio Nino Diaz }
9567b6ff9fSAntonio Nino Diaz 
9667b6ff9fSAntonio Nino Diaz /*******************************************************************************
977c88f3f6SAchin Gupta  * TSP main entry point where it gets the opportunity to initialize its secure
987c88f3f6SAchin Gupta  * state/applications. Once the state is initialized, it must return to the
99399fb08fSAndrew Thoelke  * SPD with a pointer to the 'tsp_vector_table' jump table.
1007c88f3f6SAchin Gupta  ******************************************************************************/
1017c88f3f6SAchin Gupta uint64_t tsp_main(void)
1027c88f3f6SAchin Gupta {
1036ad2e461SDan Handley 	NOTICE("TSP: %s\n", version_string);
1046ad2e461SDan Handley 	NOTICE("TSP: %s\n", build_message);
105a604623cSSandrine Bailleux 	INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE);
106a604623cSSandrine Bailleux 	INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE);
1076ad2e461SDan Handley 
108fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
1097c88f3f6SAchin Gupta 
1107c88f3f6SAchin Gupta 	/* Initialize the platform */
1115a06bb7eSDan Handley 	tsp_platform_setup();
1127c88f3f6SAchin Gupta 
1137c88f3f6SAchin Gupta 	/* Initialize secure/applications state here */
114a20a81e5SAchin Gupta 	tsp_generic_timer_start();
1157c88f3f6SAchin Gupta 
1167c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
1177c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
1187c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
1197c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_on_count++;
1207c88f3f6SAchin Gupta 
1216ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO
1227c88f3f6SAchin Gupta 	spin_lock(&console_lock);
123fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
124fd650ff6SSoby Mathew 	     read_mpidr(),
1257c88f3f6SAchin Gupta 	     tsp_stats[linear_id].smc_count,
1267c88f3f6SAchin Gupta 	     tsp_stats[linear_id].eret_count,
1277c88f3f6SAchin Gupta 	     tsp_stats[linear_id].cpu_on_count);
1287c88f3f6SAchin Gupta 	spin_unlock(&console_lock);
1296ad2e461SDan Handley #endif
130399fb08fSAndrew Thoelke 	return (uint64_t) &tsp_vector_table;
1317c88f3f6SAchin Gupta }
1327c88f3f6SAchin Gupta 
1337c88f3f6SAchin Gupta /*******************************************************************************
1347c88f3f6SAchin Gupta  * This function performs any remaining book keeping in the test secure payload
1357c88f3f6SAchin Gupta  * after this cpu's architectural state has been setup in response to an earlier
1367c88f3f6SAchin Gupta  * psci cpu_on request.
1377c88f3f6SAchin Gupta  ******************************************************************************/
138fb037bfbSDan Handley tsp_args_t *tsp_cpu_on_main(void)
1397c88f3f6SAchin Gupta {
140fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
1417c88f3f6SAchin Gupta 
142a20a81e5SAchin Gupta 	/* Initialize secure/applications state here */
143a20a81e5SAchin Gupta 	tsp_generic_timer_start();
144a20a81e5SAchin Gupta 
1457c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
1467c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
1477c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
1487c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_on_count++;
1497c88f3f6SAchin Gupta 
1506ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO
1517c88f3f6SAchin Gupta 	spin_lock(&console_lock);
152fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx turned on\n", read_mpidr());
153fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
154fd650ff6SSoby Mathew 		read_mpidr(),
1557c88f3f6SAchin Gupta 		tsp_stats[linear_id].smc_count,
1567c88f3f6SAchin Gupta 		tsp_stats[linear_id].eret_count,
1577c88f3f6SAchin Gupta 		tsp_stats[linear_id].cpu_on_count);
1587c88f3f6SAchin Gupta 	spin_unlock(&console_lock);
1596ad2e461SDan Handley #endif
1607c88f3f6SAchin Gupta 	/* Indicate to the SPD that we have completed turned ourselves on */
1617c88f3f6SAchin Gupta 	return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0);
1627c88f3f6SAchin Gupta }
1637c88f3f6SAchin Gupta 
1647c88f3f6SAchin Gupta /*******************************************************************************
1657c88f3f6SAchin Gupta  * This function performs any remaining book keeping in the test secure payload
1667c88f3f6SAchin Gupta  * before this cpu is turned off in response to a psci cpu_off request.
1677c88f3f6SAchin Gupta  ******************************************************************************/
168fb037bfbSDan Handley tsp_args_t *tsp_cpu_off_main(uint64_t arg0,
1697c88f3f6SAchin Gupta 			   uint64_t arg1,
1707c88f3f6SAchin Gupta 			   uint64_t arg2,
1717c88f3f6SAchin Gupta 			   uint64_t arg3,
1727c88f3f6SAchin Gupta 			   uint64_t arg4,
1737c88f3f6SAchin Gupta 			   uint64_t arg5,
1747c88f3f6SAchin Gupta 			   uint64_t arg6,
1757c88f3f6SAchin Gupta 			   uint64_t arg7)
1767c88f3f6SAchin Gupta {
177fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
1787c88f3f6SAchin Gupta 
179a20a81e5SAchin Gupta 	/*
180a20a81e5SAchin Gupta 	 * This cpu is being turned off, so disable the timer to prevent the
181a20a81e5SAchin Gupta 	 * secure timer interrupt from interfering with power down. A pending
182a20a81e5SAchin Gupta 	 * interrupt will be lost but we do not care as we are turning off.
183a20a81e5SAchin Gupta 	 */
184a20a81e5SAchin Gupta 	tsp_generic_timer_stop();
185a20a81e5SAchin Gupta 
1867c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
1877c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
1887c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
1897c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_off_count++;
1907c88f3f6SAchin Gupta 
1916ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO
1927c88f3f6SAchin Gupta 	spin_lock(&console_lock);
193fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx off request\n", read_mpidr());
194fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n",
195fd650ff6SSoby Mathew 		read_mpidr(),
1967c88f3f6SAchin Gupta 		tsp_stats[linear_id].smc_count,
1977c88f3f6SAchin Gupta 		tsp_stats[linear_id].eret_count,
1987c88f3f6SAchin Gupta 		tsp_stats[linear_id].cpu_off_count);
1997c88f3f6SAchin Gupta 	spin_unlock(&console_lock);
2006ad2e461SDan Handley #endif
2017c88f3f6SAchin Gupta 
202607084eeSAchin Gupta 	/* Indicate to the SPD that we have completed this request */
2037c88f3f6SAchin Gupta 	return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
2047c88f3f6SAchin Gupta }
2057c88f3f6SAchin Gupta 
2067c88f3f6SAchin Gupta /*******************************************************************************
2077c88f3f6SAchin Gupta  * This function performs any book keeping in the test secure payload before
2087c88f3f6SAchin Gupta  * this cpu's architectural state is saved in response to an earlier psci
2097c88f3f6SAchin Gupta  * cpu_suspend request.
2107c88f3f6SAchin Gupta  ******************************************************************************/
21131244d74SSoby Mathew tsp_args_t *tsp_cpu_suspend_main(uint64_t arg0,
2127c88f3f6SAchin Gupta 			       uint64_t arg1,
2137c88f3f6SAchin Gupta 			       uint64_t arg2,
2147c88f3f6SAchin Gupta 			       uint64_t arg3,
2157c88f3f6SAchin Gupta 			       uint64_t arg4,
2167c88f3f6SAchin Gupta 			       uint64_t arg5,
2177c88f3f6SAchin Gupta 			       uint64_t arg6,
2187c88f3f6SAchin Gupta 			       uint64_t arg7)
2197c88f3f6SAchin Gupta {
220fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
2217c88f3f6SAchin Gupta 
222a20a81e5SAchin Gupta 	/*
223a20a81e5SAchin Gupta 	 * Save the time context and disable it to prevent the secure timer
224a20a81e5SAchin Gupta 	 * interrupt from interfering with wakeup from the suspend state.
225a20a81e5SAchin Gupta 	 */
226a20a81e5SAchin Gupta 	tsp_generic_timer_save();
227a20a81e5SAchin Gupta 	tsp_generic_timer_stop();
228a20a81e5SAchin Gupta 
2297c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
2307c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
2317c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
2327c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_suspend_count++;
2337c88f3f6SAchin Gupta 
2346ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO
2357c88f3f6SAchin Gupta 	spin_lock(&console_lock);
236dad25049SSandrine Bailleux 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
237fd650ff6SSoby Mathew 		read_mpidr(),
2387c88f3f6SAchin Gupta 		tsp_stats[linear_id].smc_count,
2397c88f3f6SAchin Gupta 		tsp_stats[linear_id].eret_count,
2407c88f3f6SAchin Gupta 		tsp_stats[linear_id].cpu_suspend_count);
2417c88f3f6SAchin Gupta 	spin_unlock(&console_lock);
2426ad2e461SDan Handley #endif
2437c88f3f6SAchin Gupta 
244607084eeSAchin Gupta 	/* Indicate to the SPD that we have completed this request */
2457c88f3f6SAchin Gupta 	return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0);
2467c88f3f6SAchin Gupta }
2477c88f3f6SAchin Gupta 
2487c88f3f6SAchin Gupta /*******************************************************************************
2497c88f3f6SAchin Gupta  * This function performs any book keeping in the test secure payload after this
2507c88f3f6SAchin Gupta  * cpu's architectural state has been restored after wakeup from an earlier psci
2517c88f3f6SAchin Gupta  * cpu_suspend request.
2527c88f3f6SAchin Gupta  ******************************************************************************/
253f1054c93SAchin Gupta tsp_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
2547c88f3f6SAchin Gupta 			      uint64_t arg1,
2557c88f3f6SAchin Gupta 			      uint64_t arg2,
2567c88f3f6SAchin Gupta 			      uint64_t arg3,
2577c88f3f6SAchin Gupta 			      uint64_t arg4,
2587c88f3f6SAchin Gupta 			      uint64_t arg5,
2597c88f3f6SAchin Gupta 			      uint64_t arg6,
2607c88f3f6SAchin Gupta 			      uint64_t arg7)
2617c88f3f6SAchin Gupta {
262fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
2637c88f3f6SAchin Gupta 
264a20a81e5SAchin Gupta 	/* Restore the generic timer context */
265a20a81e5SAchin Gupta 	tsp_generic_timer_restore();
266a20a81e5SAchin Gupta 
2677c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
2687c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
2697c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
2707c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_resume_count++;
2717c88f3f6SAchin Gupta 
2726ad2e461SDan Handley #if LOG_LEVEL >= LOG_LEVEL_INFO
2737c88f3f6SAchin Gupta 	spin_lock(&console_lock);
2740a2d5b43SMasahiro Yamada 	INFO("TSP: cpu 0x%lx resumed. maximum off power level %lld\n",
275f1054c93SAchin Gupta 	     read_mpidr(), max_off_pwrlvl);
276dad25049SSandrine Bailleux 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
277fd650ff6SSoby Mathew 		read_mpidr(),
2787c88f3f6SAchin Gupta 		tsp_stats[linear_id].smc_count,
2797c88f3f6SAchin Gupta 		tsp_stats[linear_id].eret_count,
2807c88f3f6SAchin Gupta 		tsp_stats[linear_id].cpu_suspend_count);
2817c88f3f6SAchin Gupta 	spin_unlock(&console_lock);
2826ad2e461SDan Handley #endif
283607084eeSAchin Gupta 	/* Indicate to the SPD that we have completed this request */
2847c88f3f6SAchin Gupta 	return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
2857c88f3f6SAchin Gupta }
2867c88f3f6SAchin Gupta 
2877c88f3f6SAchin Gupta /*******************************************************************************
288d5f13093SJuan Castillo  * This function performs any remaining bookkeeping in the test secure payload
289d5f13093SJuan Castillo  * before the system is switched off (in response to a psci SYSTEM_OFF request)
290d5f13093SJuan Castillo  ******************************************************************************/
291d5f13093SJuan Castillo tsp_args_t *tsp_system_off_main(uint64_t arg0,
292d5f13093SJuan Castillo 				uint64_t arg1,
293d5f13093SJuan Castillo 				uint64_t arg2,
294d5f13093SJuan Castillo 				uint64_t arg3,
295d5f13093SJuan Castillo 				uint64_t arg4,
296d5f13093SJuan Castillo 				uint64_t arg5,
297d5f13093SJuan Castillo 				uint64_t arg6,
298d5f13093SJuan Castillo 				uint64_t arg7)
299d5f13093SJuan Castillo {
300fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
301d5f13093SJuan Castillo 
302d5f13093SJuan Castillo 	/* Update this cpu's statistics */
303d5f13093SJuan Castillo 	tsp_stats[linear_id].smc_count++;
304d5f13093SJuan Castillo 	tsp_stats[linear_id].eret_count++;
305d5f13093SJuan Castillo 
306d5f13093SJuan Castillo #if LOG_LEVEL >= LOG_LEVEL_INFO
307d5f13093SJuan Castillo 	spin_lock(&console_lock);
308fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx SYSTEM_OFF request\n", read_mpidr());
309fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
310d5f13093SJuan Castillo 	     tsp_stats[linear_id].smc_count,
311d5f13093SJuan Castillo 	     tsp_stats[linear_id].eret_count);
312d5f13093SJuan Castillo 	spin_unlock(&console_lock);
313d5f13093SJuan Castillo #endif
314d5f13093SJuan Castillo 
315d5f13093SJuan Castillo 	/* Indicate to the SPD that we have completed this request */
316d5f13093SJuan Castillo 	return set_smc_args(TSP_SYSTEM_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
317d5f13093SJuan Castillo }
318d5f13093SJuan Castillo 
319d5f13093SJuan Castillo /*******************************************************************************
320d5f13093SJuan Castillo  * This function performs any remaining bookkeeping in the test secure payload
321d5f13093SJuan Castillo  * before the system is reset (in response to a psci SYSTEM_RESET request)
322d5f13093SJuan Castillo  ******************************************************************************/
323d5f13093SJuan Castillo tsp_args_t *tsp_system_reset_main(uint64_t arg0,
324d5f13093SJuan Castillo 				uint64_t arg1,
325d5f13093SJuan Castillo 				uint64_t arg2,
326d5f13093SJuan Castillo 				uint64_t arg3,
327d5f13093SJuan Castillo 				uint64_t arg4,
328d5f13093SJuan Castillo 				uint64_t arg5,
329d5f13093SJuan Castillo 				uint64_t arg6,
330d5f13093SJuan Castillo 				uint64_t arg7)
331d5f13093SJuan Castillo {
332fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
333d5f13093SJuan Castillo 
334d5f13093SJuan Castillo 	/* Update this cpu's statistics */
335d5f13093SJuan Castillo 	tsp_stats[linear_id].smc_count++;
336d5f13093SJuan Castillo 	tsp_stats[linear_id].eret_count++;
337d5f13093SJuan Castillo 
338d5f13093SJuan Castillo #if LOG_LEVEL >= LOG_LEVEL_INFO
339d5f13093SJuan Castillo 	spin_lock(&console_lock);
340fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx SYSTEM_RESET request\n", read_mpidr());
341fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
342d5f13093SJuan Castillo 	     tsp_stats[linear_id].smc_count,
343d5f13093SJuan Castillo 	     tsp_stats[linear_id].eret_count);
344d5f13093SJuan Castillo 	spin_unlock(&console_lock);
345d5f13093SJuan Castillo #endif
346d5f13093SJuan Castillo 
347d5f13093SJuan Castillo 	/* Indicate to the SPD that we have completed this request */
348d5f13093SJuan Castillo 	return set_smc_args(TSP_SYSTEM_RESET_DONE, 0, 0, 0, 0, 0, 0, 0);
349d5f13093SJuan Castillo }
350d5f13093SJuan Castillo 
351d5f13093SJuan Castillo /*******************************************************************************
3527c88f3f6SAchin Gupta  * TSP fast smc handler. The secure monitor jumps to this function by
3537c88f3f6SAchin Gupta  * doing the ERET after populating X0-X7 registers. The arguments are received
3547c88f3f6SAchin Gupta  * in the function arguments in order. Once the service is rendered, this
355239b04faSSoby Mathew  * function returns to Secure Monitor by raising SMC.
3567c88f3f6SAchin Gupta  ******************************************************************************/
357239b04faSSoby Mathew tsp_args_t *tsp_smc_handler(uint64_t func,
3587c88f3f6SAchin Gupta 			       uint64_t arg1,
3597c88f3f6SAchin Gupta 			       uint64_t arg2,
3607c88f3f6SAchin Gupta 			       uint64_t arg3,
3617c88f3f6SAchin Gupta 			       uint64_t arg4,
3627c88f3f6SAchin Gupta 			       uint64_t arg5,
3637c88f3f6SAchin Gupta 			       uint64_t arg6,
3647c88f3f6SAchin Gupta 			       uint64_t arg7)
3657c88f3f6SAchin Gupta {
366916a2c1eSAchin Gupta 	uint64_t results[2];
367916a2c1eSAchin Gupta 	uint64_t service_args[2];
368fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
3697c88f3f6SAchin Gupta 
370916a2c1eSAchin Gupta 	/* Update this cpu's statistics */
371916a2c1eSAchin Gupta 	tsp_stats[linear_id].smc_count++;
372916a2c1eSAchin Gupta 	tsp_stats[linear_id].eret_count++;
3737c88f3f6SAchin Gupta 
3740a2d5b43SMasahiro Yamada 	INFO("TSP: cpu 0x%lx received %s smc 0x%llx\n", read_mpidr(),
37516292f54SDavid Cunado 		((func >> 31) & 1) == 1 ? "fast" : "yielding",
3766ad2e461SDan Handley 		func);
377fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(),
378916a2c1eSAchin Gupta 		tsp_stats[linear_id].smc_count,
379916a2c1eSAchin Gupta 		tsp_stats[linear_id].eret_count);
380916a2c1eSAchin Gupta 
381916a2c1eSAchin Gupta 	/* Render secure services and obtain results here */
3827c88f3f6SAchin Gupta 	results[0] = arg1;
3837c88f3f6SAchin Gupta 	results[1] = arg2;
3847c88f3f6SAchin Gupta 
3857c88f3f6SAchin Gupta 	/*
3867c88f3f6SAchin Gupta 	 * Request a service back from dispatcher/secure monitor. This call
3878aabea33SPaul Beesley 	 * return and thereafter resume execution
3887c88f3f6SAchin Gupta 	 */
3897c88f3f6SAchin Gupta 	tsp_get_magic(service_args);
3907c88f3f6SAchin Gupta 
3919dd94382SJustin Chadwell #if CTX_INCLUDE_MTE_REGS
3929dd94382SJustin Chadwell 	/*
3939dd94382SJustin Chadwell 	 * Write a dummy value to an MTE register, to simulate usage in the
3949dd94382SJustin Chadwell 	 * secure world
3959dd94382SJustin Chadwell 	 */
3969dd94382SJustin Chadwell 	write_gcr_el1(0x99);
3979dd94382SJustin Chadwell #endif
3989dd94382SJustin Chadwell 
3997c88f3f6SAchin Gupta 	/* Determine the function to perform based on the function ID */
400239b04faSSoby Mathew 	switch (TSP_BARE_FID(func)) {
401239b04faSSoby Mathew 	case TSP_ADD:
4027c88f3f6SAchin Gupta 		results[0] += service_args[0];
4037c88f3f6SAchin Gupta 		results[1] += service_args[1];
4047c88f3f6SAchin Gupta 		break;
405239b04faSSoby Mathew 	case TSP_SUB:
4067c88f3f6SAchin Gupta 		results[0] -= service_args[0];
4077c88f3f6SAchin Gupta 		results[1] -= service_args[1];
4087c88f3f6SAchin Gupta 		break;
409239b04faSSoby Mathew 	case TSP_MUL:
4107c88f3f6SAchin Gupta 		results[0] *= service_args[0];
4117c88f3f6SAchin Gupta 		results[1] *= service_args[1];
4127c88f3f6SAchin Gupta 		break;
413239b04faSSoby Mathew 	case TSP_DIV:
4147c88f3f6SAchin Gupta 		results[0] /= service_args[0] ? service_args[0] : 1;
4157c88f3f6SAchin Gupta 		results[1] /= service_args[1] ? service_args[1] : 1;
4167c88f3f6SAchin Gupta 		break;
4177c88f3f6SAchin Gupta 	default:
4187c88f3f6SAchin Gupta 		break;
4197c88f3f6SAchin Gupta 	}
4207c88f3f6SAchin Gupta 
421239b04faSSoby Mathew 	return set_smc_args(func, 0,
4227c88f3f6SAchin Gupta 			    results[0],
4237c88f3f6SAchin Gupta 			    results[1],
424239b04faSSoby Mathew 			    0, 0, 0, 0);
4257c88f3f6SAchin Gupta }
4267c88f3f6SAchin Gupta 
4273df6012aSDouglas Raillard /*******************************************************************************
4288aabea33SPaul Beesley  * TSP smc abort handler. This function is called when aborting a preempted
42916292f54SDavid Cunado  * yielding SMC request. It should cleanup all resources owned by the SMC
4303df6012aSDouglas Raillard  * handler such as locks or dynamically allocated memory so following SMC
4313df6012aSDouglas Raillard  * request are executed in a clean environment.
4323df6012aSDouglas Raillard  ******************************************************************************/
4333df6012aSDouglas Raillard tsp_args_t *tsp_abort_smc_handler(uint64_t func,
4343df6012aSDouglas Raillard 				  uint64_t arg1,
4353df6012aSDouglas Raillard 				  uint64_t arg2,
4363df6012aSDouglas Raillard 				  uint64_t arg3,
4373df6012aSDouglas Raillard 				  uint64_t arg4,
4383df6012aSDouglas Raillard 				  uint64_t arg5,
4393df6012aSDouglas Raillard 				  uint64_t arg6,
4403df6012aSDouglas Raillard 				  uint64_t arg7)
4413df6012aSDouglas Raillard {
4423df6012aSDouglas Raillard 	return set_smc_args(TSP_ABORT_DONE, 0, 0, 0, 0, 0, 0, 0);
4433df6012aSDouglas Raillard }
444