tspd_pm.c 9.46 KB
Newer Older
1
/*
2
 * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <arch_helpers.h>
32
33
#include <assert.h>
#include <bl_common.h>
34
35
#include <context_mgmt.h>
#include <debug.h>
36
#include <platform.h>
37
#include <tsp.h>
38
#include "tspd_private.h"
39
40
41
42
43
44
45
46
47
48
49
50
51

/*******************************************************************************
 * The target cpu is being turned on. Allow the TSPD/TSP to perform any actions
 * needed. Nothing at the moment.
 ******************************************************************************/
static void tspd_cpu_on_handler(uint64_t target_cpu)
{
}

/*******************************************************************************
 * This cpu is being turned off. Allow the TSPD/TSP to perform any actions
 * needed
 ******************************************************************************/
52
static int32_t tspd_cpu_off_handler(uint64_t unused)
53
54
{
	int32_t rc = 0;
55
	uint32_t linear_id = plat_my_core_pos();
56
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
57

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

61
62
63
64
65
66
	/*
	 * Abort any preempted SMC request before overwriting the SECURE
	 * context.
	 */
	tspd_abort_preempted_smc(tsp_ctx);

67
	/* Program the entry point and enter the TSP */
68
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_off_entry);
69
70
71
72
73
74
75
76
77
78
79
80
81
	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.
	 */
82
	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
83

84
	return 0;
85
86
87
88
89
90
}

/*******************************************************************************
 * 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.
 ******************************************************************************/
91
static void tspd_cpu_suspend_handler(uint64_t max_off_pwrlvl)
92
93
{
	int32_t rc = 0;
94
	uint32_t linear_id = plat_my_core_pos();
95
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
96

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

100
101
102
103
104
105
	/*
	 * Abort any preempted SMC request before overwriting the SECURE
	 * context.
	 */
	tspd_abort_preempted_smc(tsp_ctx);

106
	/* Program the entry point and enter the TSP */
107
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_suspend_entry);
108
109
110
111
112
113
	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.
	 */
114
	if (rc)
115
116
117
		panic();

	/* Update its context to reflect the state the TSP is in */
118
	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_SUSPEND);
119
120
121
122
}

/*******************************************************************************
 * This cpu has been turned on. Enter the TSP to initialise S-EL1 and other bits
123
 * before passing control back to the Secure Monitor. Entry in S-EL1 is done
124
125
126
 * after initialising minimal architectural state that guarantees safe
 * execution.
 ******************************************************************************/
127
static void tspd_cpu_on_finish_handler(uint64_t unused)
128
129
{
	int32_t rc = 0;
130
	uint32_t linear_id = plat_my_core_pos();
131
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Vikram Kanigiri's avatar
Vikram Kanigiri committed
132
	entry_point_info_t tsp_on_entrypoint;
133

134
	assert(tsp_vectors);
135
	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_OFF);
136

Vikram Kanigiri's avatar
Vikram Kanigiri committed
137
	tspd_init_tsp_ep_state(&tsp_on_entrypoint,
138
				TSP_AARCH64,
Vikram Kanigiri's avatar
Vikram Kanigiri committed
139
				(uint64_t) &tsp_vectors->cpu_on_entry,
140
141
				tsp_ctx);

Vikram Kanigiri's avatar
Vikram Kanigiri committed
142
	/* Initialise this cpu's secure context */
143
	cm_init_my_context(&tsp_on_entrypoint);
Vikram Kanigiri's avatar
Vikram Kanigiri committed
144

145
#if TSP_NS_INTR_ASYNC_PREEMPT
146
147
	/*
	 * Disable the NS interrupt locally since it will be enabled globally
148
	 * within cm_init_my_context.
149
150
151
152
	 */
	disable_intr_rm_local(INTR_TYPE_NS, SECURE);
#endif

153
154
155
156
157
158
159
160
161
162
163
	/* 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 */
164
	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
165
166
167
168
169
170
171
}

/*******************************************************************************
 * 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
 ******************************************************************************/
172
static void tspd_cpu_suspend_finish_handler(uint64_t max_off_pwrlvl)
173
174
{
	int32_t rc = 0;
175
	uint32_t linear_id = plat_my_core_pos();
176
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
177

178
	assert(tsp_vectors);
179
	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_SUSPEND);
180

181
	/* Program the entry point, max_off_pwrlvl and enter the SP */
182
183
	write_ctx_reg(get_gpregs_ctx(&tsp_ctx->cpu_ctx),
		      CTX_GPREG_X0,
184
		      max_off_pwrlvl);
185
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_resume_entry);
186
187
188
189
190
191
192
193
194
195
	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 */
196
	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
197
198
199
200
201
202
203
204
205
206
207
}

/*******************************************************************************
 * 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.
 ******************************************************************************/
static int32_t tspd_cpu_migrate_info(uint64_t *resident_cpu)
{
	return TSP_MIGRATE_INFO;
}

208
209
210
211
212
213
/*******************************************************************************
 * System is about to be switched off. Allow the TSPD/TSP to perform
 * any actions needed.
 ******************************************************************************/
static void tspd_system_off(void)
{
214
	uint32_t linear_id = plat_my_core_pos();
215
216
217
218
219
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];

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

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

226
227
228
229
230
231
232
233
234
235
236
237
238
239
	/* 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)
{
240
	uint32_t linear_id = plat_my_core_pos();
241
242
243
244
245
	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];

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

246
247
248
249
250
251
	/*
	 * Abort any preempted SMC request before overwriting the SECURE
	 * context.
	 */
	tspd_abort_preempted_smc(tsp_ctx);

252
253
254
	/* Program the entry point */
	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_reset_entry);

255
256
257
258
	/*
	 * Enter the TSP. We do not care about the return value because we
	 * must continue the reset anyway
	 */
259
260
261
	tspd_synchronous_sp_entry(tsp_ctx);
}

262
263
264
265
/*******************************************************************************
 * Structure populated by the TSP Dispatcher to be given a chance to perform any
 * TSP bookkeeping before PSCI executes a power mgmt.  operation.
 ******************************************************************************/
266
const spd_pm_ops_t tspd_pm = {
267
268
269
270
271
272
273
274
275
	.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
276
};