xref: /rk3399_ARM-atf/bl32/tsp/tsp_main.c (revision 758ccb802d4f2a5fe55ec936a21ad4ae8cbd7b4f)
17c88f3f6SAchin Gupta /*
20a33adc0SGovindraj Raja  * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
37c88f3f6SAchin Gupta  *
482cb2c1aSdp-arm  * SPDX-License-Identifier: BSD-3-Clause
57c88f3f6SAchin Gupta  */
67c88f3f6SAchin Gupta 
7ed108b56SAlexei Fedorov #include <assert.h>
84ce3e99aSScott Branden #include <inttypes.h>
94ce3e99aSScott Branden #include <stdint.h>
1009d40e0eSAntonio Nino Diaz 
11ed108b56SAlexei Fedorov #include <arch_features.h>
1209d40e0eSAntonio Nino Diaz #include <arch_helpers.h>
1309d40e0eSAntonio Nino Diaz #include <bl32/tsp/tsp.h>
1409d40e0eSAntonio Nino Diaz #include <common/bl_common.h>
15*758ccb80SChris Kay #include <common/build_message.h>
1609d40e0eSAntonio Nino Diaz #include <common/debug.h>
1709d40e0eSAntonio Nino Diaz #include <lib/spinlock.h>
1809d40e0eSAntonio Nino Diaz #include <plat/common/platform.h>
195a06bb7eSDan Handley #include <platform_tsp.h>
20da0af78aSDan Handley #include "tsp_private.h"
217c88f3f6SAchin Gupta 
224a8bfdb9SAchin Gupta #include <platform_def.h>
2367b6ff9fSAntonio Nino Diaz 
2467b6ff9fSAntonio Nino Diaz /*******************************************************************************
257c88f3f6SAchin Gupta  * TSP main entry point where it gets the opportunity to initialize its secure
267c88f3f6SAchin Gupta  * state/applications. Once the state is initialized, it must return to the
27399fb08fSAndrew Thoelke  * SPD with a pointer to the 'tsp_vector_table' jump table.
287c88f3f6SAchin Gupta  ******************************************************************************/
297c88f3f6SAchin Gupta uint64_t tsp_main(void)
307c88f3f6SAchin Gupta {
31*758ccb80SChris Kay 	NOTICE("TSP: %s\n", build_version_string);
326ad2e461SDan Handley 	NOTICE("TSP: %s\n", build_message);
33a604623cSSandrine Bailleux 	INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE);
34a604623cSSandrine Bailleux 	INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE);
356ad2e461SDan Handley 
36fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
377c88f3f6SAchin Gupta 
387c88f3f6SAchin Gupta 	/* Initialize the platform */
395a06bb7eSDan Handley 	tsp_platform_setup();
407c88f3f6SAchin Gupta 
417c88f3f6SAchin Gupta 	/* Initialize secure/applications state here */
42a20a81e5SAchin Gupta 	tsp_generic_timer_start();
437c88f3f6SAchin Gupta 
447c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
457c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
467c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
477c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_on_count++;
487c88f3f6SAchin Gupta 
49fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
50fd650ff6SSoby Mathew 	     read_mpidr(),
517c88f3f6SAchin Gupta 	     tsp_stats[linear_id].smc_count,
527c88f3f6SAchin Gupta 	     tsp_stats[linear_id].eret_count,
537c88f3f6SAchin Gupta 	     tsp_stats[linear_id].cpu_on_count);
54ae074b36SGovindraj Raja 
55ae074b36SGovindraj Raja 	console_flush();
56399fb08fSAndrew Thoelke 	return (uint64_t) &tsp_vector_table;
577c88f3f6SAchin Gupta }
587c88f3f6SAchin Gupta 
597c88f3f6SAchin Gupta /*******************************************************************************
607c88f3f6SAchin Gupta  * This function performs any remaining book keeping in the test secure payload
617c88f3f6SAchin Gupta  * after this cpu's architectural state has been setup in response to an earlier
627c88f3f6SAchin Gupta  * psci cpu_on request.
637c88f3f6SAchin Gupta  ******************************************************************************/
644a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_on_main(void)
657c88f3f6SAchin Gupta {
66fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
677c88f3f6SAchin Gupta 
68a20a81e5SAchin Gupta 	/* Initialize secure/applications state here */
69a20a81e5SAchin Gupta 	tsp_generic_timer_start();
70a20a81e5SAchin Gupta 
717c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
727c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
737c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
747c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_on_count++;
757c88f3f6SAchin Gupta 
76fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx turned on\n", read_mpidr());
77fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
78fd650ff6SSoby Mathew 		read_mpidr(),
797c88f3f6SAchin Gupta 		tsp_stats[linear_id].smc_count,
807c88f3f6SAchin Gupta 		tsp_stats[linear_id].eret_count,
817c88f3f6SAchin Gupta 		tsp_stats[linear_id].cpu_on_count);
827c88f3f6SAchin Gupta 	/* Indicate to the SPD that we have completed turned ourselves on */
837c88f3f6SAchin Gupta 	return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0);
847c88f3f6SAchin Gupta }
857c88f3f6SAchin Gupta 
867c88f3f6SAchin Gupta /*******************************************************************************
877c88f3f6SAchin Gupta  * This function performs any remaining book keeping in the test secure payload
887c88f3f6SAchin Gupta  * before this cpu is turned off in response to a psci cpu_off request.
897c88f3f6SAchin Gupta  ******************************************************************************/
904a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_off_main(uint64_t arg0,
917c88f3f6SAchin Gupta 			   uint64_t arg1,
927c88f3f6SAchin Gupta 			   uint64_t arg2,
937c88f3f6SAchin Gupta 			   uint64_t arg3,
947c88f3f6SAchin Gupta 			   uint64_t arg4,
957c88f3f6SAchin Gupta 			   uint64_t arg5,
967c88f3f6SAchin Gupta 			   uint64_t arg6,
977c88f3f6SAchin Gupta 			   uint64_t arg7)
987c88f3f6SAchin Gupta {
99fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
1007c88f3f6SAchin Gupta 
101a20a81e5SAchin Gupta 	/*
102a20a81e5SAchin Gupta 	 * This cpu is being turned off, so disable the timer to prevent the
103a20a81e5SAchin Gupta 	 * secure timer interrupt from interfering with power down. A pending
104a20a81e5SAchin Gupta 	 * interrupt will be lost but we do not care as we are turning off.
105a20a81e5SAchin Gupta 	 */
106a20a81e5SAchin Gupta 	tsp_generic_timer_stop();
107a20a81e5SAchin Gupta 
1087c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
1097c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
1107c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
1117c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_off_count++;
1127c88f3f6SAchin Gupta 
113fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx off request\n", read_mpidr());
114fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n",
115fd650ff6SSoby Mathew 		read_mpidr(),
1167c88f3f6SAchin Gupta 		tsp_stats[linear_id].smc_count,
1177c88f3f6SAchin Gupta 		tsp_stats[linear_id].eret_count,
1187c88f3f6SAchin Gupta 		tsp_stats[linear_id].cpu_off_count);
1197c88f3f6SAchin Gupta 
120607084eeSAchin Gupta 	/* Indicate to the SPD that we have completed this request */
1217c88f3f6SAchin Gupta 	return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
1227c88f3f6SAchin Gupta }
1237c88f3f6SAchin Gupta 
1247c88f3f6SAchin Gupta /*******************************************************************************
1257c88f3f6SAchin Gupta  * This function performs any book keeping in the test secure payload before
1267c88f3f6SAchin Gupta  * this cpu's architectural state is saved in response to an earlier psci
1277c88f3f6SAchin Gupta  * cpu_suspend request.
1287c88f3f6SAchin Gupta  ******************************************************************************/
1294a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_suspend_main(uint64_t arg0,
1307c88f3f6SAchin Gupta 			       uint64_t arg1,
1317c88f3f6SAchin Gupta 			       uint64_t arg2,
1327c88f3f6SAchin Gupta 			       uint64_t arg3,
1337c88f3f6SAchin Gupta 			       uint64_t arg4,
1347c88f3f6SAchin Gupta 			       uint64_t arg5,
1357c88f3f6SAchin Gupta 			       uint64_t arg6,
1367c88f3f6SAchin Gupta 			       uint64_t arg7)
1377c88f3f6SAchin Gupta {
138fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
1397c88f3f6SAchin Gupta 
140a20a81e5SAchin Gupta 	/*
141a20a81e5SAchin Gupta 	 * Save the time context and disable it to prevent the secure timer
142a20a81e5SAchin Gupta 	 * interrupt from interfering with wakeup from the suspend state.
143a20a81e5SAchin Gupta 	 */
144a20a81e5SAchin Gupta 	tsp_generic_timer_save();
145a20a81e5SAchin Gupta 	tsp_generic_timer_stop();
146a20a81e5SAchin Gupta 
1477c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
1487c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
1497c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
1507c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_suspend_count++;
1517c88f3f6SAchin Gupta 
152dad25049SSandrine Bailleux 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
153fd650ff6SSoby Mathew 		read_mpidr(),
1547c88f3f6SAchin Gupta 		tsp_stats[linear_id].smc_count,
1557c88f3f6SAchin Gupta 		tsp_stats[linear_id].eret_count,
1567c88f3f6SAchin Gupta 		tsp_stats[linear_id].cpu_suspend_count);
1577c88f3f6SAchin Gupta 
158607084eeSAchin Gupta 	/* Indicate to the SPD that we have completed this request */
1597c88f3f6SAchin Gupta 	return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0);
1607c88f3f6SAchin Gupta }
1617c88f3f6SAchin Gupta 
1627c88f3f6SAchin Gupta /*******************************************************************************
1637c88f3f6SAchin Gupta  * This function performs any book keeping in the test secure payload after this
1647c88f3f6SAchin Gupta  * cpu's architectural state has been restored after wakeup from an earlier psci
1657c88f3f6SAchin Gupta  * cpu_suspend request.
1667c88f3f6SAchin Gupta  ******************************************************************************/
1674a8bfdb9SAchin Gupta smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
1687c88f3f6SAchin Gupta 			      uint64_t arg1,
1697c88f3f6SAchin Gupta 			      uint64_t arg2,
1707c88f3f6SAchin Gupta 			      uint64_t arg3,
1717c88f3f6SAchin Gupta 			      uint64_t arg4,
1727c88f3f6SAchin Gupta 			      uint64_t arg5,
1737c88f3f6SAchin Gupta 			      uint64_t arg6,
1747c88f3f6SAchin Gupta 			      uint64_t arg7)
1757c88f3f6SAchin Gupta {
176fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
1777c88f3f6SAchin Gupta 
178a20a81e5SAchin Gupta 	/* Restore the generic timer context */
179a20a81e5SAchin Gupta 	tsp_generic_timer_restore();
180a20a81e5SAchin Gupta 
1817c88f3f6SAchin Gupta 	/* Update this cpu's statistics */
1827c88f3f6SAchin Gupta 	tsp_stats[linear_id].smc_count++;
1837c88f3f6SAchin Gupta 	tsp_stats[linear_id].eret_count++;
1847c88f3f6SAchin Gupta 	tsp_stats[linear_id].cpu_resume_count++;
1857c88f3f6SAchin Gupta 
1864ce3e99aSScott Branden 	INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n",
187f1054c93SAchin Gupta 	     read_mpidr(), max_off_pwrlvl);
188a16bc845SManish Pandey 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n",
189fd650ff6SSoby Mathew 		read_mpidr(),
1907c88f3f6SAchin Gupta 		tsp_stats[linear_id].smc_count,
1917c88f3f6SAchin Gupta 		tsp_stats[linear_id].eret_count,
192a16bc845SManish Pandey 		tsp_stats[linear_id].cpu_resume_count);
193607084eeSAchin Gupta 	/* Indicate to the SPD that we have completed this request */
1947c88f3f6SAchin Gupta 	return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
1957c88f3f6SAchin Gupta }
1967c88f3f6SAchin Gupta 
1977c88f3f6SAchin Gupta /*******************************************************************************
1987c88f3f6SAchin Gupta  * TSP fast smc handler. The secure monitor jumps to this function by
1997c88f3f6SAchin Gupta  * doing the ERET after populating X0-X7 registers. The arguments are received
2007c88f3f6SAchin Gupta  * in the function arguments in order. Once the service is rendered, this
201239b04faSSoby Mathew  * function returns to Secure Monitor by raising SMC.
2027c88f3f6SAchin Gupta  ******************************************************************************/
2034a8bfdb9SAchin Gupta smc_args_t *tsp_smc_handler(uint64_t func,
2047c88f3f6SAchin Gupta 			       uint64_t arg1,
2057c88f3f6SAchin Gupta 			       uint64_t arg2,
2067c88f3f6SAchin Gupta 			       uint64_t arg3,
2077c88f3f6SAchin Gupta 			       uint64_t arg4,
2087c88f3f6SAchin Gupta 			       uint64_t arg5,
2097c88f3f6SAchin Gupta 			       uint64_t arg6,
2107c88f3f6SAchin Gupta 			       uint64_t arg7)
2117c88f3f6SAchin Gupta {
212caff3c87SAlexei Fedorov 	uint128_t service_args;
213caff3c87SAlexei Fedorov 	uint64_t service_arg0;
214caff3c87SAlexei Fedorov 	uint64_t service_arg1;
215916a2c1eSAchin Gupta 	uint64_t results[2];
216fd650ff6SSoby Mathew 	uint32_t linear_id = plat_my_core_pos();
2174d482156SDaniel Boulby 	u_register_t dit;
2187c88f3f6SAchin Gupta 
219916a2c1eSAchin Gupta 	/* Update this cpu's statistics */
220916a2c1eSAchin Gupta 	tsp_stats[linear_id].smc_count++;
221916a2c1eSAchin Gupta 	tsp_stats[linear_id].eret_count++;
2227c88f3f6SAchin Gupta 
2234ce3e99aSScott Branden 	INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(),
22416292f54SDavid Cunado 		((func >> 31) & 1) == 1 ? "fast" : "yielding",
2256ad2e461SDan Handley 		func);
226fd650ff6SSoby Mathew 	INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(),
227916a2c1eSAchin Gupta 		tsp_stats[linear_id].smc_count,
228916a2c1eSAchin Gupta 		tsp_stats[linear_id].eret_count);
229916a2c1eSAchin Gupta 
230916a2c1eSAchin Gupta 	/* Render secure services and obtain results here */
2317c88f3f6SAchin Gupta 	results[0] = arg1;
2327c88f3f6SAchin Gupta 	results[1] = arg2;
2337c88f3f6SAchin Gupta 
2347c88f3f6SAchin Gupta 	/*
235caff3c87SAlexei Fedorov 	 * Request a service back from dispatcher/secure monitor.
236caff3c87SAlexei Fedorov 	 * This call returns and thereafter resumes execution.
2377c88f3f6SAchin Gupta 	 */
238caff3c87SAlexei Fedorov 	service_args = tsp_get_magic();
239caff3c87SAlexei Fedorov 	service_arg0 = (uint64_t)service_args;
240caff3c87SAlexei Fedorov 	service_arg1 = (uint64_t)(service_args >> 64U);
2417c88f3f6SAchin Gupta 
2429dd94382SJustin Chadwell 	/*
243c282384dSGovindraj Raja 	 * Write a dummy value to an MTE2 register, to simulate usage in the
2449dd94382SJustin Chadwell 	 * secure world
2459dd94382SJustin Chadwell 	 */
246c282384dSGovindraj Raja 	if (is_feat_mte2_supported()) {
2479dd94382SJustin Chadwell 		write_gcr_el1(0x99);
2488e397889SGovindraj Raja 	}
2499dd94382SJustin Chadwell 
2507c88f3f6SAchin Gupta 	/* Determine the function to perform based on the function ID */
251239b04faSSoby Mathew 	switch (TSP_BARE_FID(func)) {
252239b04faSSoby Mathew 	case TSP_ADD:
253caff3c87SAlexei Fedorov 		results[0] += service_arg0;
254caff3c87SAlexei Fedorov 		results[1] += service_arg1;
2557c88f3f6SAchin Gupta 		break;
256239b04faSSoby Mathew 	case TSP_SUB:
257caff3c87SAlexei Fedorov 		results[0] -= service_arg0;
258caff3c87SAlexei Fedorov 		results[1] -= service_arg1;
2597c88f3f6SAchin Gupta 		break;
260239b04faSSoby Mathew 	case TSP_MUL:
261caff3c87SAlexei Fedorov 		results[0] *= service_arg0;
262caff3c87SAlexei Fedorov 		results[1] *= service_arg1;
2637c88f3f6SAchin Gupta 		break;
264239b04faSSoby Mathew 	case TSP_DIV:
265caff3c87SAlexei Fedorov 		results[0] /= service_arg0 ? service_arg0 : 1;
266caff3c87SAlexei Fedorov 		results[1] /= service_arg1 ? service_arg1 : 1;
2677c88f3f6SAchin Gupta 		break;
2684d482156SDaniel Boulby 	case TSP_CHECK_DIT:
26988727fc3SAndre Przywara 		if (!is_feat_dit_supported()) {
2704d482156SDaniel Boulby 			ERROR("DIT not supported\n");
2714d482156SDaniel Boulby 			results[0] = 0;
2724d482156SDaniel Boulby 			results[1] = 0xffff;
2734d482156SDaniel Boulby 			break;
2744d482156SDaniel Boulby 		}
2754d482156SDaniel Boulby 		dit = read_dit();
2764d482156SDaniel Boulby 		results[0] = dit == service_arg0;
2774d482156SDaniel Boulby 		results[1] = dit;
2784d482156SDaniel Boulby 		/* Toggle the dit bit */
2794d482156SDaniel Boulby 		write_dit(service_arg0 != 0U ? 0 : DIT_BIT);
2804d482156SDaniel Boulby 		break;
2817c88f3f6SAchin Gupta 	default:
2827c88f3f6SAchin Gupta 		break;
2837c88f3f6SAchin Gupta 	}
2847c88f3f6SAchin Gupta 
285239b04faSSoby Mathew 	return set_smc_args(func, 0,
2867c88f3f6SAchin Gupta 			    results[0],
2877c88f3f6SAchin Gupta 			    results[1],
288239b04faSSoby Mathew 			    0, 0, 0, 0);
2897c88f3f6SAchin Gupta }
290