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 <assert.h>
8

9
#include <arch_features.h>
10
11
12
13
14
15
#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>
16
#include <platform_def.h>
17
#include <platform_tsp.h>
18

19
#include "tsp_private.h"
20

21

22
23
24
25
26
27
28
29
30
/*******************************************************************************
 * 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
 ******************************************************************************/
31
static tsp_args_t tsp_smc_args[PLATFORM_CORE_COUNT];
32
33
34
35

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

38
/*******************************************************************************
39
40
41
 * 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.
42
 ******************************************************************************/
43
#define BL32_TOTAL_LIMIT BL32_END
44
#define BL32_TOTAL_SIZE (BL32_TOTAL_LIMIT - (unsigned long) BL32_BASE)
45

46
static tsp_args_t *set_smc_args(uint64_t arg0,
47
48
49
50
51
52
53
54
55
			     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;
56
	tsp_args_t *pcpu_smc_args;
57
58
59
60
61

	/*
	 * Return to Secure Monitor by raising an SMC. The results of the
	 * service are passed as an arguments to the SMC
	 */
62
	linear_id = plat_my_core_pos();
63
64
65
66
67
68
69
70
71
72
73
74
75
	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;
}

76
77
78
79
80
81
82
83
84
85
/*******************************************************************************
 * Setup function for TSP.
 ******************************************************************************/
void tsp_setup(void)
{
	/* Perform early platform-specific setup */
	tsp_early_platform_setup();

	/* Perform late platform-specific setup */
	tsp_plat_arch_setup();
86
87
88
89
90
91
92
93

#if ENABLE_PAUTH
	/*
	 * Assert that the ARMv8.3-PAuth registers are present or an access
	 * fault will be triggered when they are being saved or restored.
	 */
	assert(is_armv8_3_pauth_present());
#endif /* ENABLE_PAUTH */
94
95
}

96
97
98
/*******************************************************************************
 * 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
99
 * SPD with a pointer to the 'tsp_vector_table' jump table.
100
101
102
 ******************************************************************************/
uint64_t tsp_main(void)
{
Dan Handley's avatar
Dan Handley committed
103
104
	NOTICE("TSP: %s\n", version_string);
	NOTICE("TSP: %s\n", build_message);
105
106
	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
107

108
	uint32_t linear_id = plat_my_core_pos();
109
110

	/* Initialize the platform */
111
	tsp_platform_setup();
112
113

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

	/* 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
121
#if LOG_LEVEL >= LOG_LEVEL_INFO
122
	spin_lock(&console_lock);
123
124
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
	     read_mpidr(),
125
126
127
128
	     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
129
#endif
130
	return (uint64_t) &tsp_vector_table;
131
132
133
134
135
136
137
}

/*******************************************************************************
 * 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.
 ******************************************************************************/
138
tsp_args_t *tsp_cpu_on_main(void)
139
{
140
	uint32_t linear_id = plat_my_core_pos();
141

142
143
144
	/* Initialize secure/applications state here */
	tsp_generic_timer_start();

145
146
147
148
149
	/* 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
150
#if LOG_LEVEL >= LOG_LEVEL_INFO
151
	spin_lock(&console_lock);
152
153
154
	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
155
156
157
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_on_count);
158
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
159
#endif
160
161
162
163
164
165
166
167
	/* 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.
 ******************************************************************************/
168
tsp_args_t *tsp_cpu_off_main(uint64_t arg0,
169
170
171
172
173
174
175
176
			   uint64_t arg1,
			   uint64_t arg2,
			   uint64_t arg3,
			   uint64_t arg4,
			   uint64_t arg5,
			   uint64_t arg6,
			   uint64_t arg7)
{
177
	uint32_t linear_id = plat_my_core_pos();
178

179
180
181
182
183
184
185
	/*
	 * 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();

186
187
188
189
190
	/* 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
191
#if LOG_LEVEL >= LOG_LEVEL_INFO
192
	spin_lock(&console_lock);
193
194
195
	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
196
197
198
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_off_count);
199
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
200
#endif
201

202
	/* Indicate to the SPD that we have completed this request */
203
204
205
206
207
208
209
210
	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.
 ******************************************************************************/
211
tsp_args_t *tsp_cpu_suspend_main(uint64_t arg0,
212
213
214
215
216
217
218
219
			       uint64_t arg1,
			       uint64_t arg2,
			       uint64_t arg3,
			       uint64_t arg4,
			       uint64_t arg5,
			       uint64_t arg6,
			       uint64_t arg7)
{
220
	uint32_t linear_id = plat_my_core_pos();
221

222
223
224
225
226
227
228
	/*
	 * 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();

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

244
	/* Indicate to the SPD that we have completed this request */
245
246
247
248
249
250
251
252
	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.
 ******************************************************************************/
253
tsp_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
254
255
256
257
258
259
260
261
			      uint64_t arg1,
			      uint64_t arg2,
			      uint64_t arg3,
			      uint64_t arg4,
			      uint64_t arg5,
			      uint64_t arg6,
			      uint64_t arg7)
{
262
	uint32_t linear_id = plat_my_core_pos();
263

264
265
266
	/* Restore the generic timer context */
	tsp_generic_timer_restore();

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

287
288
289
290
291
292
293
294
295
296
297
298
299
/*******************************************************************************
 * 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)
{
300
	uint32_t linear_id = plat_my_core_pos();
301
302
303
304
305
306
307

	/* 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);
308
309
	INFO("TSP: cpu 0x%lx SYSTEM_OFF request\n", read_mpidr());
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
	     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)
{
332
	uint32_t linear_id = plat_my_core_pos();
333
334
335
336
337
338
339

	/* 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);
340
341
	INFO("TSP: cpu 0x%lx SYSTEM_RESET request\n", read_mpidr());
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
342
343
344
345
346
347
348
349
350
	     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);
}

351
352
353
354
/*******************************************************************************
 * 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
355
 * function returns to Secure Monitor by raising SMC.
356
 ******************************************************************************/
357
tsp_args_t *tsp_smc_handler(uint64_t func,
358
359
360
361
362
363
364
365
			       uint64_t arg1,
			       uint64_t arg2,
			       uint64_t arg3,
			       uint64_t arg4,
			       uint64_t arg5,
			       uint64_t arg6,
			       uint64_t arg7)
{
366
367
	uint64_t results[2];
	uint64_t service_args[2];
368
	uint32_t linear_id = plat_my_core_pos();
369

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

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

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

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

391
392
393
394
395
396
397
398
#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

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

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

427
/*******************************************************************************
Paul Beesley's avatar
Paul Beesley committed
428
 * TSP smc abort handler. This function is called when aborting a preempted
429
 * yielding SMC request. It should cleanup all resources owned by the SMC
430
431
432
433
434
435
436
437
438
439
440
441
442
443
 * 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);
}