tspd_pm.c 8.08 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2016, 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 <arch_helpers.h>
8
9
#include <assert.h>
#include <bl_common.h>
10
11
#include <context_mgmt.h>
#include <debug.h>
12
#include <platform.h>
13
#include <tsp.h>
14
#include "tspd_private.h"
15
16
17
18
19

/*******************************************************************************
 * The target cpu is being turned on. Allow the TSPD/TSP to perform any actions
 * needed. Nothing at the moment.
 ******************************************************************************/
20
static void tspd_cpu_on_handler(u_register_t target_cpu)
21
22
23
24
25
26
27
{
}

/*******************************************************************************
 * This cpu is being turned off. Allow the TSPD/TSP to perform any actions
 * needed
 ******************************************************************************/
28
static int32_t tspd_cpu_off_handler(u_register_t unused)
29
30
{
	int32_t rc = 0;
31
	uint32_t linear_id = plat_my_core_pos();
32
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
33

34
	assert(tsp_vectors);
35
	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
36

37
38
39
40
41
42
	/*
	 * Abort any preempted SMC request before overwriting the SECURE
	 * context.
	 */
	tspd_abort_preempted_smc(tsp_ctx);

43
	/* Program the entry point and enter the TSP */
44
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_off_entry);
45
46
47
48
49
50
51
52
53
54
55
56
57
	rc = tspd_synchronous_sp_entry(tsp_ctx);

	/*
	 * Read the response from the TSP. A non-zero return means that
	 * something went wrong while communicating with the TSP.
	 */
	if (rc != 0)
		panic();

	/*
	 * Reset TSP's context for a fresh start when this cpu is turned on
	 * subsequently.
	 */
58
	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
59

60
	return 0;
61
62
63
64
65
66
}

/*******************************************************************************
 * This cpu is being suspended. S-EL1 state must have been saved in the
 * resident cpu (mpidr format) if it is a UP/UP migratable TSP.
 ******************************************************************************/
67
static void tspd_cpu_suspend_handler(u_register_t max_off_pwrlvl)
68
69
{
	int32_t rc = 0;
70
	uint32_t linear_id = plat_my_core_pos();
71
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
72

73
	assert(tsp_vectors);
74
	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
75

76
77
78
79
80
81
	/*
	 * Abort any preempted SMC request before overwriting the SECURE
	 * context.
	 */
	tspd_abort_preempted_smc(tsp_ctx);

82
	/* Program the entry point and enter the TSP */
83
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_suspend_entry);
84
85
86
87
88
89
	rc = tspd_synchronous_sp_entry(tsp_ctx);

	/*
	 * Read the response from the TSP. A non-zero return means that
	 * something went wrong while communicating with the TSP.
	 */
90
	if (rc)
91
92
93
		panic();

	/* Update its context to reflect the state the TSP is in */
94
	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_SUSPEND);
95
96
97
98
}

/*******************************************************************************
 * This cpu has been turned on. Enter the TSP to initialise S-EL1 and other bits
99
 * before passing control back to the Secure Monitor. Entry in S-EL1 is done
100
101
102
 * after initialising minimal architectural state that guarantees safe
 * execution.
 ******************************************************************************/
103
static void tspd_cpu_on_finish_handler(u_register_t unused)
104
105
{
	int32_t rc = 0;
106
	uint32_t linear_id = plat_my_core_pos();
107
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Vikram Kanigiri's avatar
Vikram Kanigiri committed
108
	entry_point_info_t tsp_on_entrypoint;
109

110
	assert(tsp_vectors);
111
	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_OFF);
112

Vikram Kanigiri's avatar
Vikram Kanigiri committed
113
	tspd_init_tsp_ep_state(&tsp_on_entrypoint,
114
				TSP_AARCH64,
Vikram Kanigiri's avatar
Vikram Kanigiri committed
115
				(uint64_t) &tsp_vectors->cpu_on_entry,
116
117
				tsp_ctx);

Vikram Kanigiri's avatar
Vikram Kanigiri committed
118
	/* Initialise this cpu's secure context */
119
	cm_init_my_context(&tsp_on_entrypoint);
Vikram Kanigiri's avatar
Vikram Kanigiri committed
120

121
#if TSP_NS_INTR_ASYNC_PREEMPT
122
123
	/*
	 * Disable the NS interrupt locally since it will be enabled globally
124
	 * within cm_init_my_context.
125
126
127
128
	 */
	disable_intr_rm_local(INTR_TYPE_NS, SECURE);
#endif

129
130
131
132
133
134
135
136
137
138
139
	/* Enter the TSP */
	rc = tspd_synchronous_sp_entry(tsp_ctx);

	/*
	 * Read the response from the TSP. A non-zero return means that
	 * something went wrong while communicating with the SP.
	 */
	if (rc != 0)
		panic();

	/* Update its context to reflect the state the SP is in */
140
	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
141
142
143
144
145
146
147
}

/*******************************************************************************
 * This cpu has resumed from suspend. The SPD saved the TSP context when it
 * completed the preceding suspend call. Use that context to program an entry
 * into the TSP to allow it to do any remaining book keeping
 ******************************************************************************/
148
static void tspd_cpu_suspend_finish_handler(u_register_t max_off_pwrlvl)
149
150
{
	int32_t rc = 0;
151
	uint32_t linear_id = plat_my_core_pos();
152
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
153

154
	assert(tsp_vectors);
155
	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_SUSPEND);
156

157
	/* Program the entry point, max_off_pwrlvl and enter the SP */
158
159
	write_ctx_reg(get_gpregs_ctx(&tsp_ctx->cpu_ctx),
		      CTX_GPREG_X0,
160
		      max_off_pwrlvl);
161
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_resume_entry);
162
163
164
165
166
167
168
169
170
171
	rc = tspd_synchronous_sp_entry(tsp_ctx);

	/*
	 * Read the response from the TSP. A non-zero return means that
	 * something went wrong while communicating with the TSP.
	 */
	if (rc != 0)
		panic();

	/* Update its context to reflect the state the SP is in */
172
	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
173
174
175
176
177
178
}

/*******************************************************************************
 * Return the type of TSP the TSPD is dealing with. Report the current resident
 * cpu (mpidr format) if it is a UP/UP migratable TSP.
 ******************************************************************************/
179
static int32_t tspd_cpu_migrate_info(u_register_t *resident_cpu)
180
181
182
183
{
	return TSP_MIGRATE_INFO;
}

184
185
186
187
188
189
/*******************************************************************************
 * System is about to be switched off. Allow the TSPD/TSP to perform
 * any actions needed.
 ******************************************************************************/
static void tspd_system_off(void)
{
190
	uint32_t linear_id = plat_my_core_pos();
191
192
193
194
195
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];

	assert(tsp_vectors);
	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);

196
197
198
199
200
201
	/*
	 * Abort any preempted SMC request before overwriting the SECURE
	 * context.
	 */
	tspd_abort_preempted_smc(tsp_ctx);

202
203
204
205
206
207
208
209
210
211
212
213
214
215
	/* Program the entry point */
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_off_entry);

	/* Enter the TSP. We do not care about the return value because we
	 * must continue the shutdown anyway */
	tspd_synchronous_sp_entry(tsp_ctx);
}

/*******************************************************************************
 * System is about to be reset. Allow the TSPD/TSP to perform
 * any actions needed.
 ******************************************************************************/
static void tspd_system_reset(void)
{
216
	uint32_t linear_id = plat_my_core_pos();
217
218
219
220
221
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];

	assert(tsp_vectors);
	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);

222
223
224
225
226
227
	/*
	 * Abort any preempted SMC request before overwriting the SECURE
	 * context.
	 */
	tspd_abort_preempted_smc(tsp_ctx);

228
229
230
	/* Program the entry point */
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_reset_entry);

231
232
233
234
	/*
	 * Enter the TSP. We do not care about the return value because we
	 * must continue the reset anyway
	 */
235
236
237
	tspd_synchronous_sp_entry(tsp_ctx);
}

238
239
240
241
/*******************************************************************************
 * Structure populated by the TSP Dispatcher to be given a chance to perform any
 * TSP bookkeeping before PSCI executes a power mgmt.  operation.
 ******************************************************************************/
242
const spd_pm_ops_t tspd_pm = {
243
244
245
246
247
248
249
250
251
	.svc_on = tspd_cpu_on_handler,
	.svc_off = tspd_cpu_off_handler,
	.svc_suspend = tspd_cpu_suspend_handler,
	.svc_on_finish = tspd_cpu_on_finish_handler,
	.svc_suspend_finish = tspd_cpu_suspend_finish_handler,
	.svc_migrate = NULL,
	.svc_migrate_info = tspd_cpu_migrate_info,
	.svc_system_off = tspd_system_off,
	.svc_system_reset = tspd_system_reset
252
};