blob: ae38d69043420ca1113a723f871fba5fc8c4d2a7 [file] [log] [blame]
Achin Gupta7c88f3f2014-02-18 18:09:12 +00001/*
Govindraj Raja24d3a4e2023-12-21 13:57:49 -06002 * Copyright (c) 2013-2024, Arm Limited and Contributors. All rights reserved.
Achin Gupta7c88f3f2014-02-18 18:09:12 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta7c88f3f2014-02-18 18:09:12 +00005 */
6
Alexei Fedorovf41355c2019-09-13 14:11:59 +01007#include <assert.h>
Scott Brandene5dcf982020-08-25 13:49:32 -07008#include <inttypes.h>
9#include <stdint.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
Alexei Fedorovf41355c2019-09-13 14:11:59 +010011#include <arch_features.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012#include <arch_helpers.h>
13#include <bl32/tsp/tsp.h>
14#include <common/bl_common.h>
Chris Kay99b5b2e2024-03-08 16:08:31 +000015#include <common/build_message.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016#include <common/debug.h>
17#include <lib/spinlock.h>
18#include <plat/common/platform.h>
Dan Handley4fd2f5c2014-08-04 11:41:20 +010019#include <platform_tsp.h>
Dan Handleye2c27f52014-08-01 17:58:27 +010020#include "tsp_private.h"
Achin Gupta7c88f3f2014-02-18 18:09:12 +000021
Achin Gupta6b4ec242021-10-04 20:13:36 +010022#include <platform_def.h>
Antonio Nino Diaze61ece02019-02-26 11:41:03 +000023
24/*******************************************************************************
Achin Gupta7c88f3f2014-02-18 18:09:12 +000025 * TSP main entry point where it gets the opportunity to initialize its secure
26 * state/applications. Once the state is initialized, it must return to the
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010027 * SPD with a pointer to the 'tsp_vector_table' jump table.
Achin Gupta7c88f3f2014-02-18 18:09:12 +000028 ******************************************************************************/
29uint64_t tsp_main(void)
30{
Chris Kay99b5b2e2024-03-08 16:08:31 +000031 NOTICE("TSP: %s\n", build_version_string);
Dan Handley91b624e2014-07-29 17:14:00 +010032 NOTICE("TSP: %s\n", build_message);
Sandrine Bailleuxbdba5e52016-06-16 14:24:26 +010033 INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE);
34 INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE);
Dan Handley91b624e2014-07-29 17:14:00 +010035
Soby Mathewda43b662015-07-08 21:45:46 +010036 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +000037
Achin Gupta7c88f3f2014-02-18 18:09:12 +000038 /* Initialize the platform */
Dan Handley4fd2f5c2014-08-04 11:41:20 +010039 tsp_platform_setup();
Achin Gupta7c88f3f2014-02-18 18:09:12 +000040
41 /* Initialize secure/applications state here */
Achin Guptabbc33f22014-05-09 13:33:42 +010042 tsp_generic_timer_start();
Achin Gupta7c88f3f2014-02-18 18:09:12 +000043
44 /* Update this cpu's statistics */
45 tsp_stats[linear_id].smc_count++;
46 tsp_stats[linear_id].eret_count++;
47 tsp_stats[linear_id].cpu_on_count++;
48
Soby Mathewda43b662015-07-08 21:45:46 +010049 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
50 read_mpidr(),
Achin Gupta7c88f3f2014-02-18 18:09:12 +000051 tsp_stats[linear_id].smc_count,
52 tsp_stats[linear_id].eret_count,
53 tsp_stats[linear_id].cpu_on_count);
Govindraj Raja55ca30d2023-05-22 13:22:42 -050054
55 console_flush();
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010056 return (uint64_t) &tsp_vector_table;
Achin Gupta7c88f3f2014-02-18 18:09:12 +000057}
58
59/*******************************************************************************
60 * This function performs any remaining book keeping in the test secure payload
61 * after this cpu's architectural state has been setup in response to an earlier
62 * psci cpu_on request.
63 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +010064smc_args_t *tsp_cpu_on_main(void)
Achin Gupta7c88f3f2014-02-18 18:09:12 +000065{
Soby Mathewda43b662015-07-08 21:45:46 +010066 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +000067
Achin Guptabbc33f22014-05-09 13:33:42 +010068 /* Initialize secure/applications state here */
69 tsp_generic_timer_start();
70
Achin Gupta7c88f3f2014-02-18 18:09:12 +000071 /* Update this cpu's statistics */
72 tsp_stats[linear_id].smc_count++;
73 tsp_stats[linear_id].eret_count++;
74 tsp_stats[linear_id].cpu_on_count++;
75
Soby Mathewda43b662015-07-08 21:45:46 +010076 INFO("TSP: cpu 0x%lx turned on\n", read_mpidr());
77 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
78 read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +010079 tsp_stats[linear_id].smc_count,
80 tsp_stats[linear_id].eret_count,
81 tsp_stats[linear_id].cpu_on_count);
Achin Gupta7c88f3f2014-02-18 18:09:12 +000082 /* Indicate to the SPD that we have completed turned ourselves on */
83 return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0);
84}
85
86/*******************************************************************************
87 * This function performs any remaining book keeping in the test secure payload
88 * before this cpu is turned off in response to a psci cpu_off request.
89 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +010090smc_args_t *tsp_cpu_off_main(uint64_t arg0,
Achin Gupta7c88f3f2014-02-18 18:09:12 +000091 uint64_t arg1,
92 uint64_t arg2,
93 uint64_t arg3,
94 uint64_t arg4,
95 uint64_t arg5,
96 uint64_t arg6,
97 uint64_t arg7)
98{
Soby Mathewda43b662015-07-08 21:45:46 +010099 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000100
Achin Guptabbc33f22014-05-09 13:33:42 +0100101 /*
102 * This cpu is being turned off, so disable the timer to prevent the
103 * secure timer interrupt from interfering with power down. A pending
104 * interrupt will be lost but we do not care as we are turning off.
105 */
106 tsp_generic_timer_stop();
107
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000108 /* Update this cpu's statistics */
109 tsp_stats[linear_id].smc_count++;
110 tsp_stats[linear_id].eret_count++;
111 tsp_stats[linear_id].cpu_off_count++;
112
Soby Mathewda43b662015-07-08 21:45:46 +0100113 INFO("TSP: cpu 0x%lx off request\n", read_mpidr());
114 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n",
115 read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +0100116 tsp_stats[linear_id].smc_count,
117 tsp_stats[linear_id].eret_count,
118 tsp_stats[linear_id].cpu_off_count);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000119
Achin Gupta607084e2014-02-09 18:24:19 +0000120 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000121 return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
122}
123
124/*******************************************************************************
125 * This function performs any book keeping in the test secure payload before
126 * this cpu's architectural state is saved in response to an earlier psci
127 * cpu_suspend request.
128 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +0100129smc_args_t *tsp_cpu_suspend_main(uint64_t arg0,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000130 uint64_t arg1,
131 uint64_t arg2,
132 uint64_t arg3,
133 uint64_t arg4,
134 uint64_t arg5,
135 uint64_t arg6,
136 uint64_t arg7)
137{
Soby Mathewda43b662015-07-08 21:45:46 +0100138 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000139
Achin Guptabbc33f22014-05-09 13:33:42 +0100140 /*
141 * Save the time context and disable it to prevent the secure timer
142 * interrupt from interfering with wakeup from the suspend state.
143 */
144 tsp_generic_timer_save();
145 tsp_generic_timer_stop();
146
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000147 /* Update this cpu's statistics */
148 tsp_stats[linear_id].smc_count++;
149 tsp_stats[linear_id].eret_count++;
150 tsp_stats[linear_id].cpu_suspend_count++;
151
Sandrine Bailleux8723adf2015-02-05 15:42:31 +0000152 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
Soby Mathewda43b662015-07-08 21:45:46 +0100153 read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +0100154 tsp_stats[linear_id].smc_count,
155 tsp_stats[linear_id].eret_count,
156 tsp_stats[linear_id].cpu_suspend_count);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000157
Achin Gupta607084e2014-02-09 18:24:19 +0000158 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000159 return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0);
160}
161
162/*******************************************************************************
163 * This function performs any book keeping in the test secure payload after this
164 * cpu's architectural state has been restored after wakeup from an earlier psci
165 * cpu_suspend request.
166 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +0100167smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000168 uint64_t arg1,
169 uint64_t arg2,
170 uint64_t arg3,
171 uint64_t arg4,
172 uint64_t arg5,
173 uint64_t arg6,
174 uint64_t arg7)
175{
Soby Mathewda43b662015-07-08 21:45:46 +0100176 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000177
Achin Guptabbc33f22014-05-09 13:33:42 +0100178 /* Restore the generic timer context */
179 tsp_generic_timer_restore();
180
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000181 /* Update this cpu's statistics */
182 tsp_stats[linear_id].smc_count++;
183 tsp_stats[linear_id].eret_count++;
184 tsp_stats[linear_id].cpu_resume_count++;
185
Scott Brandene5dcf982020-08-25 13:49:32 -0700186 INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n",
Achin Gupta9a0ff9b2015-09-07 20:43:27 +0100187 read_mpidr(), max_off_pwrlvl);
Manish Pandeyc4b47a22020-03-06 14:36:25 +0000188 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n",
Soby Mathewda43b662015-07-08 21:45:46 +0100189 read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +0100190 tsp_stats[linear_id].smc_count,
191 tsp_stats[linear_id].eret_count,
Manish Pandeyc4b47a22020-03-06 14:36:25 +0000192 tsp_stats[linear_id].cpu_resume_count);
Achin Gupta607084e2014-02-09 18:24:19 +0000193 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000194 return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
195}
196
197/*******************************************************************************
198 * TSP fast smc handler. The secure monitor jumps to this function by
199 * doing the ERET after populating X0-X7 registers. The arguments are received
200 * in the function arguments in order. Once the service is rendered, this
Soby Mathew9f71f702014-05-09 20:49:17 +0100201 * function returns to Secure Monitor by raising SMC.
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000202 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +0100203smc_args_t *tsp_smc_handler(uint64_t func,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000204 uint64_t arg1,
205 uint64_t arg2,
206 uint64_t arg3,
207 uint64_t arg4,
208 uint64_t arg5,
209 uint64_t arg6,
210 uint64_t arg7)
211{
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000212 uint128_t service_args;
213 uint64_t service_arg0;
214 uint64_t service_arg1;
Achin Gupta916a2c12014-02-09 23:11:46 +0000215 uint64_t results[2];
Soby Mathewda43b662015-07-08 21:45:46 +0100216 uint32_t linear_id = plat_my_core_pos();
Daniel Boulby60786e72021-10-22 11:37:34 +0100217 u_register_t dit;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000218
Achin Gupta916a2c12014-02-09 23:11:46 +0000219 /* Update this cpu's statistics */
220 tsp_stats[linear_id].smc_count++;
221 tsp_stats[linear_id].eret_count++;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000222
Scott Brandene5dcf982020-08-25 13:49:32 -0700223 INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(),
David Cunado28f69ab2017-04-05 11:34:03 +0100224 ((func >> 31) & 1) == 1 ? "fast" : "yielding",
Dan Handley91b624e2014-07-29 17:14:00 +0100225 func);
Soby Mathewda43b662015-07-08 21:45:46 +0100226 INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +0100227 tsp_stats[linear_id].smc_count,
228 tsp_stats[linear_id].eret_count);
Achin Gupta916a2c12014-02-09 23:11:46 +0000229
230 /* Render secure services and obtain results here */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000231 results[0] = arg1;
232 results[1] = arg2;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000233
234 /*
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000235 * Request a service back from dispatcher/secure monitor.
236 * This call returns and thereafter resumes execution.
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000237 */
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000238 service_args = tsp_get_magic();
239 service_arg0 = (uint64_t)service_args;
240 service_arg1 = (uint64_t)(service_args >> 64U);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000241
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100242 /*
Govindraj Rajac1be66f2024-03-07 14:42:20 -0600243 * Write a dummy value to an MTE2 register, to simulate usage in the
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100244 * secure world
245 */
Govindraj Rajac1be66f2024-03-07 14:42:20 -0600246 if (is_feat_mte2_supported()) {
Govindraj Rajad7b63ac2024-01-26 10:08:37 -0600247 write_gcr_el1(0x99);
248 }
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100249
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000250 /* Determine the function to perform based on the function ID */
Soby Mathew9f71f702014-05-09 20:49:17 +0100251 switch (TSP_BARE_FID(func)) {
252 case TSP_ADD:
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000253 results[0] += service_arg0;
254 results[1] += service_arg1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000255 break;
Soby Mathew9f71f702014-05-09 20:49:17 +0100256 case TSP_SUB:
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000257 results[0] -= service_arg0;
258 results[1] -= service_arg1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000259 break;
Soby Mathew9f71f702014-05-09 20:49:17 +0100260 case TSP_MUL:
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000261 results[0] *= service_arg0;
262 results[1] *= service_arg1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000263 break;
Soby Mathew9f71f702014-05-09 20:49:17 +0100264 case TSP_DIV:
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000265 results[0] /= service_arg0 ? service_arg0 : 1;
266 results[1] /= service_arg1 ? service_arg1 : 1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000267 break;
Daniel Boulby60786e72021-10-22 11:37:34 +0100268 case TSP_CHECK_DIT:
Andre Przywara1f55c412023-01-26 16:47:52 +0000269 if (!is_feat_dit_supported()) {
Daniel Boulby60786e72021-10-22 11:37:34 +0100270 ERROR("DIT not supported\n");
Daniel Boulby60786e72021-10-22 11:37:34 +0100271 results[0] = 0;
272 results[1] = 0xffff;
273 break;
274 }
275 dit = read_dit();
276 results[0] = dit == service_arg0;
277 results[1] = dit;
278 /* Toggle the dit bit */
279 write_dit(service_arg0 != 0U ? 0 : DIT_BIT);
280 break;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000281 default:
282 break;
283 }
284
Soby Mathew9f71f702014-05-09 20:49:17 +0100285 return set_smc_args(func, 0,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000286 results[0],
287 results[1],
Soby Mathew9f71f702014-05-09 20:49:17 +0100288 0, 0, 0, 0);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000289}