tsp_main.c 13.6 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
74
75
76
	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;
}

/*******************************************************************************
 * 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
77
 * SPD with a pointer to the 'tsp_vector_table' jump table.
78
79
80
 ******************************************************************************/
uint64_t tsp_main(void)
{
Dan Handley's avatar
Dan Handley committed
81
82
	NOTICE("TSP: %s\n", version_string);
	NOTICE("TSP: %s\n", build_message);
83
84
	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
85

86
	uint32_t linear_id = plat_my_core_pos();
87
88

	/* Initialize the platform */
89
	tsp_platform_setup();
90
91

	/* Initialize secure/applications state here */
92
	tsp_generic_timer_start();
93
94
95
96
97
98

	/* 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
99
#if LOG_LEVEL >= LOG_LEVEL_INFO
100
	spin_lock(&console_lock);
101
102
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu on requests\n",
	     read_mpidr(),
103
104
105
106
	     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
107
#endif
108
	return (uint64_t) &tsp_vector_table;
109
110
111
112
113
114
115
}

/*******************************************************************************
 * 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.
 ******************************************************************************/
116
tsp_args_t *tsp_cpu_on_main(void)
117
{
118
	uint32_t linear_id = plat_my_core_pos();
119

120
121
122
	/* Initialize secure/applications state here */
	tsp_generic_timer_start();

123
124
125
126
127
	/* 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
128
#if LOG_LEVEL >= LOG_LEVEL_INFO
129
	spin_lock(&console_lock);
130
131
132
	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
133
134
135
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_on_count);
136
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
137
#endif
138
139
140
141
142
143
144
145
	/* 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.
 ******************************************************************************/
146
tsp_args_t *tsp_cpu_off_main(uint64_t arg0,
147
148
149
150
151
152
153
154
			   uint64_t arg1,
			   uint64_t arg2,
			   uint64_t arg3,
			   uint64_t arg4,
			   uint64_t arg5,
			   uint64_t arg6,
			   uint64_t arg7)
{
155
	uint32_t linear_id = plat_my_core_pos();
156

157
158
159
160
161
162
163
	/*
	 * 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();

164
165
166
167
168
	/* 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
169
#if LOG_LEVEL >= LOG_LEVEL_INFO
170
	spin_lock(&console_lock);
171
172
173
	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
174
175
176
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_off_count);
177
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
178
#endif
179

180
	/* Indicate to the SPD that we have completed this request */
181
182
183
184
185
186
187
188
	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.
 ******************************************************************************/
189
tsp_args_t *tsp_cpu_suspend_main(uint64_t arg0,
190
191
192
193
194
195
196
197
			       uint64_t arg1,
			       uint64_t arg2,
			       uint64_t arg3,
			       uint64_t arg4,
			       uint64_t arg5,
			       uint64_t arg6,
			       uint64_t arg7)
{
198
	uint32_t linear_id = plat_my_core_pos();
199

200
201
202
203
204
205
206
	/*
	 * 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();

207
208
209
210
211
	/* 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
212
#if LOG_LEVEL >= LOG_LEVEL_INFO
213
	spin_lock(&console_lock);
214
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
215
		read_mpidr(),
Dan Handley's avatar
Dan Handley committed
216
217
218
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_suspend_count);
219
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
220
#endif
221

222
	/* Indicate to the SPD that we have completed this request */
223
224
225
226
227
228
229
230
	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.
 ******************************************************************************/
231
tsp_args_t *tsp_cpu_resume_main(uint64_t max_off_pwrlvl,
232
233
234
235
236
237
238
239
			      uint64_t arg1,
			      uint64_t arg2,
			      uint64_t arg3,
			      uint64_t arg4,
			      uint64_t arg5,
			      uint64_t arg6,
			      uint64_t arg7)
{
240
	uint32_t linear_id = plat_my_core_pos();
241

242
243
244
	/* Restore the generic timer context */
	tsp_generic_timer_restore();

245
246
247
248
249
	/* 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
250
#if LOG_LEVEL >= LOG_LEVEL_INFO
251
	spin_lock(&console_lock);
252
	INFO("TSP: cpu 0x%lx resumed. maximum off power level %lld\n",
253
	     read_mpidr(), max_off_pwrlvl);
254
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets %d cpu suspend requests\n",
255
		read_mpidr(),
Dan Handley's avatar
Dan Handley committed
256
257
258
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count,
		tsp_stats[linear_id].cpu_suspend_count);
259
	spin_unlock(&console_lock);
Dan Handley's avatar
Dan Handley committed
260
#endif
261
	/* Indicate to the SPD that we have completed this request */
262
263
264
	return set_smc_args(TSP_RESUME_DONE, 0, 0, 0, 0, 0, 0, 0);
}

265
266
267
268
269
270
271
272
273
274
275
276
277
/*******************************************************************************
 * 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)
{
278
	uint32_t linear_id = plat_my_core_pos();
279
280
281
282
283
284
285

	/* 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);
286
287
	INFO("TSP: cpu 0x%lx SYSTEM_OFF request\n", read_mpidr());
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
	     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)
{
310
	uint32_t linear_id = plat_my_core_pos();
311
312
313
314
315
316
317

	/* 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);
318
319
	INFO("TSP: cpu 0x%lx SYSTEM_RESET request\n", read_mpidr());
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets requests\n", read_mpidr(),
320
321
322
323
324
325
326
327
328
	     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);
}

329
330
331
332
/*******************************************************************************
 * 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
333
 * function returns to Secure Monitor by raising SMC.
334
 ******************************************************************************/
335
tsp_args_t *tsp_smc_handler(uint64_t func,
336
337
338
339
340
341
342
343
			       uint64_t arg1,
			       uint64_t arg2,
			       uint64_t arg3,
			       uint64_t arg4,
			       uint64_t arg5,
			       uint64_t arg6,
			       uint64_t arg7)
{
344
345
	uint64_t results[2];
	uint64_t service_args[2];
346
	uint32_t linear_id = plat_my_core_pos();
347

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

352
	INFO("TSP: cpu 0x%lx received %s smc 0x%llx\n", read_mpidr(),
353
		((func >> 31) & 1) == 1 ? "fast" : "yielding",
Dan Handley's avatar
Dan Handley committed
354
		func);
355
	INFO("TSP: cpu 0x%lx: %d smcs, %d erets\n", read_mpidr(),
Dan Handley's avatar
Dan Handley committed
356
357
		tsp_stats[linear_id].smc_count,
		tsp_stats[linear_id].eret_count);
358

359
	/* Render secure services and obtain results here */
360
361
362
363
364
	results[0] = arg1;
	results[1] = arg2;

	/*
	 * Request a service back from dispatcher/secure monitor. This call
Paul Beesley's avatar
Paul Beesley committed
365
	 * return and thereafter resume execution
366
367
368
369
	 */
	tsp_get_magic(service_args);

	/* Determine the function to perform based on the function ID */
370
371
	switch (TSP_BARE_FID(func)) {
	case TSP_ADD:
372
373
374
		results[0] += service_args[0];
		results[1] += service_args[1];
		break;
375
	case TSP_SUB:
376
377
378
		results[0] -= service_args[0];
		results[1] -= service_args[1];
		break;
379
	case TSP_MUL:
380
381
382
		results[0] *= service_args[0];
		results[1] *= service_args[1];
		break;
383
	case TSP_DIV:
384
385
386
387
388
389
390
		results[0] /= service_args[0] ? service_args[0] : 1;
		results[1] /= service_args[1] ? service_args[1] : 1;
		break;
	default:
		break;
	}

391
	return set_smc_args(func, 0,
392
393
			    results[0],
			    results[1],
394
			    0, 0, 0, 0);
395
396
}

397
/*******************************************************************************
Paul Beesley's avatar
Paul Beesley committed
398
 * TSP smc abort handler. This function is called when aborting a preempted
399
 * yielding SMC request. It should cleanup all resources owned by the SMC
400
401
402
403
404
405
406
407
408
409
410
411
412
413
 * 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);
}