blob: 8c6b2edfe90f843dc9376b774c0dcfb58650b431 [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>
Jayanth Dodderi Chidanand6a45fa72024-09-11 13:29:07 +010014#include <bl32/tsp/tsp_el1_context.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <common/bl_common.h>
Chris Kay99b5b2e2024-03-08 16:08:31 +000016#include <common/build_message.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000017#include <common/debug.h>
18#include <lib/spinlock.h>
19#include <plat/common/platform.h>
Dan Handley4fd2f5c2014-08-04 11:41:20 +010020#include <platform_tsp.h>
Dan Handleye2c27f52014-08-01 17:58:27 +010021#include "tsp_private.h"
Achin Gupta7c88f3f2014-02-18 18:09:12 +000022
Achin Gupta6b4ec242021-10-04 20:13:36 +010023#include <platform_def.h>
Antonio Nino Diaze61ece02019-02-26 11:41:03 +000024
25/*******************************************************************************
Achin Gupta7c88f3f2014-02-18 18:09:12 +000026 * TSP main entry point where it gets the opportunity to initialize its secure
27 * state/applications. Once the state is initialized, it must return to the
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010028 * SPD with a pointer to the 'tsp_vector_table' jump table.
Achin Gupta7c88f3f2014-02-18 18:09:12 +000029 ******************************************************************************/
30uint64_t tsp_main(void)
31{
Chris Kay99b5b2e2024-03-08 16:08:31 +000032 NOTICE("TSP: %s\n", build_version_string);
Dan Handley91b624e2014-07-29 17:14:00 +010033 NOTICE("TSP: %s\n", build_message);
Sandrine Bailleuxbdba5e52016-06-16 14:24:26 +010034 INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE);
35 INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE);
Dan Handley91b624e2014-07-29 17:14:00 +010036
Soby Mathewda43b662015-07-08 21:45:46 +010037 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +000038
Achin Gupta7c88f3f2014-02-18 18:09:12 +000039 /* Initialize the platform */
Dan Handley4fd2f5c2014-08-04 11:41:20 +010040 tsp_platform_setup();
Achin Gupta7c88f3f2014-02-18 18:09:12 +000041
42 /* Initialize secure/applications state here */
Achin Guptabbc33f22014-05-09 13:33:42 +010043 tsp_generic_timer_start();
Achin Gupta7c88f3f2014-02-18 18:09:12 +000044
45 /* Update this cpu's statistics */
46 tsp_stats[linear_id].smc_count++;
47 tsp_stats[linear_id].eret_count++;
48 tsp_stats[linear_id].cpu_on_count++;
49
Soby Mathewda43b662015-07-08 21:45:46 +010050 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
51 read_mpidr(),
Achin Gupta7c88f3f2014-02-18 18:09:12 +000052 tsp_stats[linear_id].smc_count,
53 tsp_stats[linear_id].eret_count,
54 tsp_stats[linear_id].cpu_on_count);
Govindraj Raja55ca30d2023-05-22 13:22:42 -050055
56 console_flush();
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010057 return (uint64_t) &tsp_vector_table;
Achin Gupta7c88f3f2014-02-18 18:09:12 +000058}
59
60/*******************************************************************************
61 * This function performs any remaining book keeping in the test secure payload
62 * after this cpu's architectural state has been setup in response to an earlier
63 * psci cpu_on request.
64 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +010065smc_args_t *tsp_cpu_on_main(void)
Achin Gupta7c88f3f2014-02-18 18:09:12 +000066{
Soby Mathewda43b662015-07-08 21:45:46 +010067 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +000068
Achin Guptabbc33f22014-05-09 13:33:42 +010069 /* Initialize secure/applications state here */
70 tsp_generic_timer_start();
71
Achin Gupta7c88f3f2014-02-18 18:09:12 +000072 /* Update this cpu's statistics */
73 tsp_stats[linear_id].smc_count++;
74 tsp_stats[linear_id].eret_count++;
75 tsp_stats[linear_id].cpu_on_count++;
76
Soby Mathewda43b662015-07-08 21:45:46 +010077 INFO("TSP: cpu 0x%lx turned on\n", read_mpidr());
78 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
79 read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +010080 tsp_stats[linear_id].smc_count,
81 tsp_stats[linear_id].eret_count,
82 tsp_stats[linear_id].cpu_on_count);
Achin Gupta7c88f3f2014-02-18 18:09:12 +000083 /* Indicate to the SPD that we have completed turned ourselves on */
84 return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0);
85}
86
87/*******************************************************************************
88 * This function performs any remaining book keeping in the test secure payload
89 * before this cpu is turned off in response to a psci cpu_off request.
90 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +010091smc_args_t *tsp_cpu_off_main(uint64_t arg0,
Achin Gupta7c88f3f2014-02-18 18:09:12 +000092 uint64_t arg1,
93 uint64_t arg2,
94 uint64_t arg3,
95 uint64_t arg4,
96 uint64_t arg5,
97 uint64_t arg6,
98 uint64_t arg7)
99{
Soby Mathewda43b662015-07-08 21:45:46 +0100100 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000101
Achin Guptabbc33f22014-05-09 13:33:42 +0100102 /*
103 * This cpu is being turned off, so disable the timer to prevent the
104 * secure timer interrupt from interfering with power down. A pending
105 * interrupt will be lost but we do not care as we are turning off.
106 */
107 tsp_generic_timer_stop();
108
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000109 /* Update this cpu's statistics */
110 tsp_stats[linear_id].smc_count++;
111 tsp_stats[linear_id].eret_count++;
112 tsp_stats[linear_id].cpu_off_count++;
113
Soby Mathewda43b662015-07-08 21:45:46 +0100114 INFO("TSP: cpu 0x%lx off request\n", read_mpidr());
115 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n",
116 read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +0100117 tsp_stats[linear_id].smc_count,
118 tsp_stats[linear_id].eret_count,
119 tsp_stats[linear_id].cpu_off_count);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000120
Achin Gupta607084e2014-02-09 18:24:19 +0000121 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000122 return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
123}
124
125/*******************************************************************************
126 * This function performs any book keeping in the test secure payload before
127 * this cpu's architectural state is saved in response to an earlier psci
128 * cpu_suspend request.
129 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +0100130smc_args_t *tsp_cpu_suspend_main(uint64_t arg0,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000131 uint64_t arg1,
132 uint64_t arg2,
133 uint64_t arg3,
134 uint64_t arg4,
135 uint64_t arg5,
136 uint64_t arg6,
137 uint64_t arg7)
138{
Soby Mathewda43b662015-07-08 21:45:46 +0100139 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000140
Achin Guptabbc33f22014-05-09 13:33:42 +0100141 /*
142 * Save the time context and disable it to prevent the secure timer
143 * interrupt from interfering with wakeup from the suspend state.
144 */
145 tsp_generic_timer_save();
146 tsp_generic_timer_stop();
147
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000148 /* Update this cpu's statistics */
149 tsp_stats[linear_id].smc_count++;
150 tsp_stats[linear_id].eret_count++;
151 tsp_stats[linear_id].cpu_suspend_count++;
152
Sandrine Bailleux8723adf2015-02-05 15:42:31 +0000153 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
Soby Mathewda43b662015-07-08 21:45:46 +0100154 read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +0100155 tsp_stats[linear_id].smc_count,
156 tsp_stats[linear_id].eret_count,
157 tsp_stats[linear_id].cpu_suspend_count);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000158
Achin Gupta607084e2014-02-09 18:24:19 +0000159 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000160 return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0);
161}
162
163/*******************************************************************************
164 * This function performs any book keeping in the test secure payload after this
165 * cpu's architectural state has been restored after wakeup from an earlier psci
166 * cpu_suspend request.
167 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +0100168smc_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000169 uint64_t arg1,
170 uint64_t arg2,
171 uint64_t arg3,
172 uint64_t arg4,
173 uint64_t arg5,
174 uint64_t arg6,
175 uint64_t arg7)
176{
Soby Mathewda43b662015-07-08 21:45:46 +0100177 uint32_t linear_id = plat_my_core_pos();
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000178
Achin Guptabbc33f22014-05-09 13:33:42 +0100179 /* Restore the generic timer context */
180 tsp_generic_timer_restore();
181
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000182 /* Update this cpu's statistics */
183 tsp_stats[linear_id].smc_count++;
184 tsp_stats[linear_id].eret_count++;
185 tsp_stats[linear_id].cpu_resume_count++;
186
Scott Brandene5dcf982020-08-25 13:49:32 -0700187 INFO("TSP: cpu 0x%lx resumed. maximum off power level %" PRId64 "\n",
Achin Gupta9a0ff9b2015-09-07 20:43:27 +0100188 read_mpidr(), max_off_pwrlvl);
Manish Pandeyc4b47a22020-03-06 14:36:25 +0000189 INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu resume requests\n",
Soby Mathewda43b662015-07-08 21:45:46 +0100190 read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +0100191 tsp_stats[linear_id].smc_count,
192 tsp_stats[linear_id].eret_count,
Manish Pandeyc4b47a22020-03-06 14:36:25 +0000193 tsp_stats[linear_id].cpu_resume_count);
Achin Gupta607084e2014-02-09 18:24:19 +0000194 /* Indicate to the SPD that we have completed this request */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000195 return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
196}
197
198/*******************************************************************************
199 * TSP fast smc handler. The secure monitor jumps to this function by
200 * doing the ERET after populating X0-X7 registers. The arguments are received
201 * in the function arguments in order. Once the service is rendered, this
Soby Mathew9f71f702014-05-09 20:49:17 +0100202 * function returns to Secure Monitor by raising SMC.
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000203 ******************************************************************************/
Achin Gupta6b4ec242021-10-04 20:13:36 +0100204smc_args_t *tsp_smc_handler(uint64_t func,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000205 uint64_t arg1,
206 uint64_t arg2,
207 uint64_t arg3,
208 uint64_t arg4,
209 uint64_t arg5,
210 uint64_t arg6,
211 uint64_t arg7)
212{
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000213 uint128_t service_args;
214 uint64_t service_arg0;
215 uint64_t service_arg1;
Achin Gupta916a2c12014-02-09 23:11:46 +0000216 uint64_t results[2];
Soby Mathewda43b662015-07-08 21:45:46 +0100217 uint32_t linear_id = plat_my_core_pos();
Daniel Boulby60786e72021-10-22 11:37:34 +0100218 u_register_t dit;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000219
Achin Gupta916a2c12014-02-09 23:11:46 +0000220 /* Update this cpu's statistics */
221 tsp_stats[linear_id].smc_count++;
222 tsp_stats[linear_id].eret_count++;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000223
Scott Brandene5dcf982020-08-25 13:49:32 -0700224 INFO("TSP: cpu 0x%lx received %s smc 0x%" PRIx64 "\n", read_mpidr(),
David Cunado28f69ab2017-04-05 11:34:03 +0100225 ((func >> 31) & 1) == 1 ? "fast" : "yielding",
Dan Handley91b624e2014-07-29 17:14:00 +0100226 func);
Soby Mathewda43b662015-07-08 21:45:46 +0100227 INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(),
Dan Handley91b624e2014-07-29 17:14:00 +0100228 tsp_stats[linear_id].smc_count,
229 tsp_stats[linear_id].eret_count);
Achin Gupta916a2c12014-02-09 23:11:46 +0000230
231 /* Render secure services and obtain results here */
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000232 results[0] = arg1;
233 results[1] = arg2;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000234
235 /*
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000236 * Request a service back from dispatcher/secure monitor.
237 * This call returns and thereafter resumes execution.
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000238 */
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000239 service_args = tsp_get_magic();
240 service_arg0 = (uint64_t)service_args;
241 service_arg1 = (uint64_t)(service_args >> 64U);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000242
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100243 /*
Govindraj Rajac1be66f2024-03-07 14:42:20 -0600244 * Write a dummy value to an MTE2 register, to simulate usage in the
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100245 * secure world
246 */
Govindraj Rajac1be66f2024-03-07 14:42:20 -0600247 if (is_feat_mte2_supported()) {
Govindraj Rajad7b63ac2024-01-26 10:08:37 -0600248 write_gcr_el1(0x99);
249 }
Justin Chadwell1c7c13a2019-07-18 14:25:33 +0100250
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000251 /* Determine the function to perform based on the function ID */
Soby Mathew9f71f702014-05-09 20:49:17 +0100252 switch (TSP_BARE_FID(func)) {
253 case TSP_ADD:
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000254 results[0] += service_arg0;
255 results[1] += service_arg1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000256 break;
Soby Mathew9f71f702014-05-09 20:49:17 +0100257 case TSP_SUB:
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000258 results[0] -= service_arg0;
259 results[1] -= service_arg1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000260 break;
Soby Mathew9f71f702014-05-09 20:49:17 +0100261 case TSP_MUL:
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000262 results[0] *= service_arg0;
263 results[1] *= service_arg1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000264 break;
Soby Mathew9f71f702014-05-09 20:49:17 +0100265 case TSP_DIV:
Alexei Fedorov7d616ee2020-11-13 12:36:49 +0000266 results[0] /= service_arg0 ? service_arg0 : 1;
267 results[1] /= service_arg1 ? service_arg1 : 1;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000268 break;
Daniel Boulby60786e72021-10-22 11:37:34 +0100269 case TSP_CHECK_DIT:
Andre Przywara1f55c412023-01-26 16:47:52 +0000270 if (!is_feat_dit_supported()) {
Daniel Boulby60786e72021-10-22 11:37:34 +0100271 ERROR("DIT not supported\n");
Daniel Boulby60786e72021-10-22 11:37:34 +0100272 results[0] = 0;
273 results[1] = 0xffff;
274 break;
275 }
276 dit = read_dit();
277 results[0] = dit == service_arg0;
278 results[1] = dit;
279 /* Toggle the dit bit */
280 write_dit(service_arg0 != 0U ? 0 : DIT_BIT);
281 break;
Jayanth Dodderi Chidanand6a45fa72024-09-11 13:29:07 +0100282 case TSP_MODIFY_EL1_CTX:
283 /*
284 * Write dummy values to EL1 context registers, to simulate
285 * their usage in the secure world.
286 */
287 if (arg1 == TSP_CORRUPT_EL1_REGS) {
288 modify_el1_ctx_regs(TSP_CORRUPT_EL1_REGS);
289 } else {
290 modify_el1_ctx_regs(TSP_RESTORE_EL1_REGS);
291 }
292 break;
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000293 default:
294 break;
295 }
296
Soby Mathew9f71f702014-05-09 20:49:17 +0100297 return set_smc_args(func, 0,
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000298 results[0],
299 results[1],
Soby Mathew9f71f702014-05-09 20:49:17 +0100300 0, 0, 0, 0);
Achin Gupta7c88f3f2014-02-18 18:09:12 +0000301}