tsp_main.c 14.3 KB
Newer Older
1
/*
Paul Beesley's avatar
Paul Beesley committed
2
 * Copyright (c) 2013-2019, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
 */

7
#include <platform_def.h>
8
9
10
11
12
13
14

#include <arch_helpers.h>
#include <bl32/tsp/tsp.h>
#include <common/bl_common.h>
#include <common/debug.h>
#include <lib/spinlock.h>
#include <plat/common/platform.h>
15
#include <platform_tsp.h>
16

17
#include "tsp_private.h"
18

19

20
21
22
23
24
25
26
27
28
/*******************************************************************************
 * Lock to control access to the console
 ******************************************************************************/
spinlock_t console_lock;

/*******************************************************************************
 * Per cpu data structure to populate parameters for an SMC in C code and use
 * a pointer to this structure in assembler code to populate x0-x7
 ******************************************************************************/
29
static tsp_args_t tsp_smc_args[PLATFORM_CORE_COUNT];
30
31
32
33

/*******************************************************************************
 * Per cpu data structure to keep track of TSP activity
 ******************************************************************************/
34
work_statistics_t tsp_stats[PLATFORM_CORE_COUNT];
35

36
/*******************************************************************************
37
38
39
 * The TSP memory footprint starts at address BL32_BASE and ends with the
 * linker symbol __BL32_END__. Use these addresses to compute the TSP image
 * size.
40
 ******************************************************************************/
41
#define BL32_TOTAL_LIMIT BL32_END
42
#define BL32_TOTAL_SIZE (BL32_TOTAL_LIMIT - (unsigned long) BL32_BASE)
43

44
static tsp_args_t *set_smc_args(uint64_t arg0,
45
46
47
48
49
50
51
52
53
			     uint64_t arg1,
			     uint64_t arg2,
			     uint64_t arg3,
			     uint64_t arg4,
			     uint64_t arg5,
			     uint64_t arg6,
			     uint64_t arg7)
{
	uint32_t linear_id;
54
	tsp_args_t *pcpu_smc_args;
55
56
57
58
59

	/*
	 * Return to Secure Monitor by raising an SMC. The results of the
	 * service are passed as an arguments to the SMC
	 */
60
	linear_id = plat_my_core_pos();
61
62
63
64
65
66
67
68
69
70
71
72
73
	pcpu_smc_args = &tsp_smc_args[linear_id];
	write_sp_arg(pcpu_smc_args, TSP_ARG0, arg0);
	write_sp_arg(pcpu_smc_args, TSP_ARG1, arg1);
	write_sp_arg(pcpu_smc_args, TSP_ARG2, arg2);
	write_sp_arg(pcpu_smc_args, TSP_ARG3, arg3);
	write_sp_arg(pcpu_smc_args, TSP_ARG4, arg4);
	write_sp_arg(pcpu_smc_args, TSP_ARG5, arg5);
	write_sp_arg(pcpu_smc_args, TSP_ARG6, arg6);
	write_sp_arg(pcpu_smc_args, TSP_ARG7, arg7);

	return pcpu_smc_args;
}

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*******************************************************************************
 * Setup function for TSP.
 ******************************************************************************/
void tsp_setup(void)
{
	/* Perform early platform-specific setup */
	tsp_early_platform_setup();

	/*
	 * Update pointer authentication key before the MMU is enabled. It is
	 * saved in the rodata section, that can be writen before enabling the
	 * MMU. This function must be called after the console is initialized
	 * in the early platform setup.
	 */
	bl_handle_pauth();

	/* Perform late platform-specific setup */
	tsp_plat_arch_setup();
}

94
95
96
/*******************************************************************************
 * TSP main entry point where it gets the opportunity to initialize its secure
 * state/applications. Once the state is initialized, it must return to the
97
 * SPD with a pointer to the 'tsp_vector_table' jump table.
98
99
100
 ******************************************************************************/
uint64_t tsp_main(void)
{
Dan Handley's avatar
Dan Handley committed
101
102
	NOTICE("TSP: %s\n", version_string);
	NOTICE("TSP: %s\n", build_message);
103
104
	INFO("TSP: Total memory base : 0x%lx\n", (unsigned long) BL32_BASE);
	INFO("TSP: Total memory size : 0x%lx bytes\n", BL32_TOTAL_SIZE);
Dan Handley's avatar
Dan Handley committed
105

106
	uint32_t linear_id = plat_my_core_pos();
107
108

	/* Initialize the platform */
109
	tsp_platform_setup();
110
111

	/* Initialize secure/applications state here */
112
	tsp_generic_timer_start();
113
114
115
116
117
118

	/* Update this cpu's statistics */
	tsp_stats[linear_id].smc_count++;
	tsp_stats[linear_id].eret_count++;
	tsp_stats[linear_id].cpu_on_count++;

Dan Handley's avatar
Dan Handley committed
119
#if LOG_LEVEL >= LOG_LEVEL_INFO
120
	spin_lock(&console_lock);
121
122
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
	     read_mpidr(),
123
124
125
126
	     tsp_stats[linear_id].smc_count,
	     tsp_stats[linear_id].eret_count,
	     tsp_stats[linear_id].cpu_on_count);
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
127
#endif
128
	return (uint64_t) &tsp_vector_table;
129
130
131
132
133
134
135
}

/*******************************************************************************
 * This function performs any remaining book keeping in the test secure payload
 * after this cpu's architectural state has been setup in response to an earlier
 * psci cpu_on request.
 ******************************************************************************/
136
tsp_args_t *tsp_cpu_on_main(void)
137
{
138
	uint32_t linear_id = plat_my_core_pos();
139

140
141
142
	/* Initialize secure/applications state here */
	tsp_generic_timer_start();

143
144
145
146
147
	/* Update this cpu's statistics */
	tsp_stats[linear_id].smc_count++;
	tsp_stats[linear_id].eret_count++;
	tsp_stats[linear_id].cpu_on_count++;

Dan Handley's avatar
Dan Handley committed
148
#if LOG_LEVEL >= LOG_LEVEL_INFO
149
	spin_lock(&console_lock);
150
151
152
	INFO("TSP: cpu 0x%lx turned on\n", read_mpidr());
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
		read_mpidr(),
Dan Handley's avatar
Dan Handley committed
153
154
155
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_on_count);
156
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
157
#endif
158
159
160
161
162
163
164
165
	/* Indicate to the SPD that we have completed turned ourselves on */
	return set_smc_args(TSP_ON_DONE, 0, 0, 0, 0, 0, 0, 0);
}

/*******************************************************************************
 * This function performs any remaining book keeping in the test secure payload
 * before this cpu is turned off in response to a psci cpu_off request.
 ******************************************************************************/
166
tsp_args_t *tsp_cpu_off_main(uint64_t arg0,
167
168
169
170
171
172
173
174
			   uint64_t arg1,
			   uint64_t arg2,
			   uint64_t arg3,
			   uint64_t arg4,
			   uint64_t arg5,
			   uint64_t arg6,
			   uint64_t arg7)
{
175
	uint32_t linear_id = plat_my_core_pos();
176

177
178
179
180
181
182
183
	/*
	 * This cpu is being turned off, so disable the timer to prevent the
	 * secure timer interrupt from interfering with power down. A pending
	 * interrupt will be lost but we do not care as we are turning off.
	 */
	tsp_generic_timer_stop();

184
185
186
187
188
	/* Update this cpu's statistics */
	tsp_stats[linear_id].smc_count++;
	tsp_stats[linear_id].eret_count++;
	tsp_stats[linear_id].cpu_off_count++;

Dan Handley's avatar
Dan Handley committed
189
#if LOG_LEVEL >= LOG_LEVEL_INFO
190
	spin_lock(&console_lock);
191
192
193
	INFO("TSP: cpu 0x%lx off request\n", read_mpidr());
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu off requests\n",
		read_mpidr(),
Dan Handley's avatar
Dan Handley committed
194
195
196
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_off_count);
197
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
198
#endif
199

200
	/* Indicate to the SPD that we have completed this request */
201
202
203
204
205
206
207
208
	return set_smc_args(TSP_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
}

/*******************************************************************************
 * This function performs any book keeping in the test secure payload before
 * this cpu's architectural state is saved in response to an earlier psci
 * cpu_suspend request.
 ******************************************************************************/
209
tsp_args_t *tsp_cpu_suspend_main(uint64_t arg0,
210
211
212
213
214
215
216
217
			       uint64_t arg1,
			       uint64_t arg2,
			       uint64_t arg3,
			       uint64_t arg4,
			       uint64_t arg5,
			       uint64_t arg6,
			       uint64_t arg7)
{
218
	uint32_t linear_id = plat_my_core_pos();
219

220
221
222
223
224
225
226
	/*
	 * Save the time context and disable it to prevent the secure timer
	 * interrupt from interfering with wakeup from the suspend state.
	 */
	tsp_generic_timer_save();
	tsp_generic_timer_stop();

227
228
229
230
231
	/* Update this cpu's statistics */
	tsp_stats[linear_id].smc_count++;
	tsp_stats[linear_id].eret_count++;
	tsp_stats[linear_id].cpu_suspend_count++;

Dan Handley's avatar
Dan Handley committed
232
#if LOG_LEVEL >= LOG_LEVEL_INFO
233
	spin_lock(&console_lock);
234
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
235
		read_mpidr(),
Dan Handley's avatar
Dan Handley committed
236
237
238
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_suspend_count);
239
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
240
#endif
241

242
	/* Indicate to the SPD that we have completed this request */
243
244
245
246
247
248
249
250
	return set_smc_args(TSP_SUSPEND_DONE, 0, 0, 0, 0, 0, 0, 0);
}

/*******************************************************************************
 * This function performs any book keeping in the test secure payload after this
 * cpu's architectural state has been restored after wakeup from an earlier psci
 * cpu_suspend request.
 ******************************************************************************/
251
tsp_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
252
253
254
255
256
257
258
259
			      uint64_t arg1,
			      uint64_t arg2,
			      uint64_t arg3,
			      uint64_t arg4,
			      uint64_t arg5,
			      uint64_t arg6,
			      uint64_t arg7)
{
260
	uint32_t linear_id = plat_my_core_pos();
261

262
263
264
	/* Restore the generic timer context */
	tsp_generic_timer_restore();

265
266
267
268
269
	/* Update this cpu's statistics */
	tsp_stats[linear_id].smc_count++;
	tsp_stats[linear_id].eret_count++;
	tsp_stats[linear_id].cpu_resume_count++;

Dan Handley's avatar
Dan Handley committed
270
#if LOG_LEVEL >= LOG_LEVEL_INFO
271
	spin_lock(&console_lock);
272
	INFO("TSP: cpu 0x%lx resumed. maximum off power level %lld\n",
273
	     read_mpidr(), max_off_pwrlvl);
274
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
275
		read_mpidr(),
Dan Handley's avatar
Dan Handley committed
276
277
278
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_suspend_count);
279
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
280
#endif
281
	/* Indicate to the SPD that we have completed this request */
282
283
284
	return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
}

285
286
287
288
289
290
291
292
293
294
295
296
297
/*******************************************************************************
 * This function performs any remaining bookkeeping in the test secure payload
 * before the system is switched off (in response to a psci SYSTEM_OFF request)
 ******************************************************************************/
tsp_args_t *tsp_system_off_main(uint64_t arg0,
				uint64_t arg1,
				uint64_t arg2,
				uint64_t arg3,
				uint64_t arg4,
				uint64_t arg5,
				uint64_t arg6,
				uint64_t arg7)
{
298
	uint32_t linear_id = plat_my_core_pos();
299
300
301
302
303
304
305

	/* Update this cpu's statistics */
	tsp_stats[linear_id].smc_count++;
	tsp_stats[linear_id].eret_count++;

#if LOG_LEVEL >= LOG_LEVEL_INFO
	spin_lock(&console_lock);
306
307
	INFO("TSP: cpu 0x%lx SYSTEM_OFF request\n", read_mpidr());
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
	     tsp_stats[linear_id].smc_count,
	     tsp_stats[linear_id].eret_count);
	spin_unlock(&console_lock);
#endif

	/* Indicate to the SPD that we have completed this request */
	return set_smc_args(TSP_SYSTEM_OFF_DONE, 0, 0, 0, 0, 0, 0, 0);
}

/*******************************************************************************
 * This function performs any remaining bookkeeping in the test secure payload
 * before the system is reset (in response to a psci SYSTEM_RESET request)
 ******************************************************************************/
tsp_args_t *tsp_system_reset_main(uint64_t arg0,
				uint64_t arg1,
				uint64_t arg2,
				uint64_t arg3,
				uint64_t arg4,
				uint64_t arg5,
				uint64_t arg6,
				uint64_t arg7)
{
330
	uint32_t linear_id = plat_my_core_pos();
331
332
333
334
335
336
337

	/* Update this cpu's statistics */
	tsp_stats[linear_id].smc_count++;
	tsp_stats[linear_id].eret_count++;

#if LOG_LEVEL >= LOG_LEVEL_INFO
	spin_lock(&console_lock);
338
339
	INFO("TSP: cpu 0x%lx SYSTEM_RESET request\n", read_mpidr());
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
340
341
342
343
344
345
346
347
348
	     tsp_stats[linear_id].smc_count,
	     tsp_stats[linear_id].eret_count);
	spin_unlock(&console_lock);
#endif

	/* Indicate to the SPD that we have completed this request */
	return set_smc_args(TSP_SYSTEM_RESET_DONE, 0, 0, 0, 0, 0, 0, 0);
}

349
350
351
352
/*******************************************************************************
 * TSP fast smc handler. The secure monitor jumps to this function by
 * doing the ERET after populating X0-X7 registers. The arguments are received
 * in the function arguments in order. Once the service is rendered, this
353
 * function returns to Secure Monitor by raising SMC.
354
 ******************************************************************************/
355
tsp_args_t *tsp_smc_handler(uint64_t func,
356
357
358
359
360
361
362
363
			       uint64_t arg1,
			       uint64_t arg2,
			       uint64_t arg3,
			       uint64_t arg4,
			       uint64_t arg5,
			       uint64_t arg6,
			       uint64_t arg7)
{
364
365
	uint64_t results[2];
	uint64_t service_args[2];
366
	uint32_t linear_id = plat_my_core_pos();
367

368
369
370
371
	/* Update this cpu's statistics */
	tsp_stats[linear_id].smc_count++;
	tsp_stats[linear_id].eret_count++;

372
	INFO("TSP: cpu 0x%lx received %s smc 0x%llx\n", read_mpidr(),
373
		((func >> 31) & 1) == 1 ? "fast" : "yielding",
Dan Handley's avatar
Dan Handley committed
374
		func);
375
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(),
Dan Handley's avatar
Dan Handley committed
376
377
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count);
378

379
	/* Render secure services and obtain results here */
380
381
382
383
384
	results[0] = arg1;
	results[1] = arg2;

	/*
	 * Request a service back from dispatcher/secure monitor. This call
Paul Beesley's avatar
Paul Beesley committed
385
	 * return and thereafter resume execution
386
387
388
	 */
	tsp_get_magic(service_args);

389
390
391
392
393
394
395
396
#if CTX_INCLUDE_MTE_REGS
	/*
	 * Write a dummy value to an MTE register, to simulate usage in the
	 * secure world
	 */
	write_gcr_el1(0x99);
#endif

397
	/* Determine the function to perform based on the function ID */
398
399
	switch (TSP_BARE_FID(func)) {
	case TSP_ADD:
400
401
402
		results[0] += service_args[0];
		results[1] += service_args[1];
		break;
403
	case TSP_SUB:
404
405
406
		results[0] -= service_args[0];
		results[1] -= service_args[1];
		break;
407
	case TSP_MUL:
408
409
410
		results[0] *= service_args[0];
		results[1] *= service_args[1];
		break;
411
	case TSP_DIV:
412
413
414
415
416
417
418
		results[0] /= service_args[0] ? service_args[0] : 1;
		results[1] /= service_args[1] ? service_args[1] : 1;
		break;
	default:
		break;
	}

419
	return set_smc_args(func, 0,
420
421
			    results[0],
			    results[1],
422
			    0, 0, 0, 0);
423
424
}

425
/*******************************************************************************
Paul Beesley's avatar
Paul Beesley committed
426
 * TSP smc abort handler. This function is called when aborting a preempted
427
 * yielding SMC request. It should cleanup all resources owned by the SMC
428
429
430
431
432
433
434
435
436
437
438
439
440
441
 * handler such as locks or dynamically allocated memory so following SMC
 * request are executed in a clean environment.
 ******************************************************************************/
tsp_args_t *tsp_abort_smc_handler(uint64_t func,
				  uint64_t arg1,
				  uint64_t arg2,
				  uint64_t arg3,
				  uint64_t arg4,
				  uint64_t arg5,
				  uint64_t arg6,
				  uint64_t arg7)
{
	return set_smc_args(TSP_ABORT_DONE, 0, 0, 0, 0, 0, 0, 0);
}