xref: /arm-trusted-firmware/services/spd/tspd/tspd_pm.c (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700Schasinglulu /*
2*91f16700Schasinglulu  * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
3*91f16700Schasinglulu  *
4*91f16700Schasinglulu  * SPDX-License-Identifier: BSD-3-Clause
5*91f16700Schasinglulu  */
6*91f16700Schasinglulu 
7*91f16700Schasinglulu #include <assert.h>
8*91f16700Schasinglulu 
9*91f16700Schasinglulu #include <arch_helpers.h>
10*91f16700Schasinglulu #include <bl32/tsp/tsp.h>
11*91f16700Schasinglulu #include <common/bl_common.h>
12*91f16700Schasinglulu #include <common/debug.h>
13*91f16700Schasinglulu #include <lib/el3_runtime/context_mgmt.h>
14*91f16700Schasinglulu #include <plat/common/platform.h>
15*91f16700Schasinglulu 
16*91f16700Schasinglulu #include "tspd_private.h"
17*91f16700Schasinglulu 
18*91f16700Schasinglulu /*******************************************************************************
19*91f16700Schasinglulu  * The target cpu is being turned on. Allow the TSPD/TSP to perform any actions
20*91f16700Schasinglulu  * needed. Nothing at the moment.
21*91f16700Schasinglulu  ******************************************************************************/
22*91f16700Schasinglulu static void tspd_cpu_on_handler(u_register_t target_cpu)
23*91f16700Schasinglulu {
24*91f16700Schasinglulu }
25*91f16700Schasinglulu 
26*91f16700Schasinglulu /*******************************************************************************
27*91f16700Schasinglulu  * This cpu is being turned off. Allow the TSPD/TSP to perform any actions
28*91f16700Schasinglulu  * needed
29*91f16700Schasinglulu  ******************************************************************************/
30*91f16700Schasinglulu static int32_t tspd_cpu_off_handler(u_register_t unused)
31*91f16700Schasinglulu {
32*91f16700Schasinglulu 	int32_t rc = 0;
33*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
34*91f16700Schasinglulu 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
35*91f16700Schasinglulu 
36*91f16700Schasinglulu 	assert(tsp_vectors);
37*91f16700Schasinglulu 	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
38*91f16700Schasinglulu 
39*91f16700Schasinglulu 	/*
40*91f16700Schasinglulu 	 * Abort any preempted SMC request before overwriting the SECURE
41*91f16700Schasinglulu 	 * context.
42*91f16700Schasinglulu 	 */
43*91f16700Schasinglulu 	tspd_abort_preempted_smc(tsp_ctx);
44*91f16700Schasinglulu 
45*91f16700Schasinglulu 	/* Program the entry point and enter the TSP */
46*91f16700Schasinglulu 	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_off_entry);
47*91f16700Schasinglulu 	rc = tspd_synchronous_sp_entry(tsp_ctx);
48*91f16700Schasinglulu 
49*91f16700Schasinglulu 	/*
50*91f16700Schasinglulu 	 * Read the response from the TSP. A non-zero return means that
51*91f16700Schasinglulu 	 * something went wrong while communicating with the TSP.
52*91f16700Schasinglulu 	 */
53*91f16700Schasinglulu 	if (rc != 0)
54*91f16700Schasinglulu 		panic();
55*91f16700Schasinglulu 
56*91f16700Schasinglulu 	/*
57*91f16700Schasinglulu 	 * Reset TSP's context for a fresh start when this cpu is turned on
58*91f16700Schasinglulu 	 * subsequently.
59*91f16700Schasinglulu 	 */
60*91f16700Schasinglulu 	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
61*91f16700Schasinglulu 
62*91f16700Schasinglulu 	return 0;
63*91f16700Schasinglulu }
64*91f16700Schasinglulu 
65*91f16700Schasinglulu /*******************************************************************************
66*91f16700Schasinglulu  * This cpu is being suspended. S-EL1 state must have been saved in the
67*91f16700Schasinglulu  * resident cpu (mpidr format) if it is a UP/UP migratable TSP.
68*91f16700Schasinglulu  ******************************************************************************/
69*91f16700Schasinglulu static void tspd_cpu_suspend_handler(u_register_t max_off_pwrlvl)
70*91f16700Schasinglulu {
71*91f16700Schasinglulu 	int32_t rc = 0;
72*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
73*91f16700Schasinglulu 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
74*91f16700Schasinglulu 
75*91f16700Schasinglulu 	assert(tsp_vectors);
76*91f16700Schasinglulu 	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
77*91f16700Schasinglulu 
78*91f16700Schasinglulu 	/*
79*91f16700Schasinglulu 	 * Abort any preempted SMC request before overwriting the SECURE
80*91f16700Schasinglulu 	 * context.
81*91f16700Schasinglulu 	 */
82*91f16700Schasinglulu 	tspd_abort_preempted_smc(tsp_ctx);
83*91f16700Schasinglulu 
84*91f16700Schasinglulu 	/* Program the entry point and enter the TSP */
85*91f16700Schasinglulu 	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_suspend_entry);
86*91f16700Schasinglulu 	rc = tspd_synchronous_sp_entry(tsp_ctx);
87*91f16700Schasinglulu 
88*91f16700Schasinglulu 	/*
89*91f16700Schasinglulu 	 * Read the response from the TSP. A non-zero return means that
90*91f16700Schasinglulu 	 * something went wrong while communicating with the TSP.
91*91f16700Schasinglulu 	 */
92*91f16700Schasinglulu 	if (rc)
93*91f16700Schasinglulu 		panic();
94*91f16700Schasinglulu 
95*91f16700Schasinglulu 	/* Update its context to reflect the state the TSP is in */
96*91f16700Schasinglulu 	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_SUSPEND);
97*91f16700Schasinglulu }
98*91f16700Schasinglulu 
99*91f16700Schasinglulu /*******************************************************************************
100*91f16700Schasinglulu  * This cpu has been turned on. Enter the TSP to initialise S-EL1 and other bits
101*91f16700Schasinglulu  * before passing control back to the Secure Monitor. Entry in S-EL1 is done
102*91f16700Schasinglulu  * after initialising minimal architectural state that guarantees safe
103*91f16700Schasinglulu  * execution.
104*91f16700Schasinglulu  ******************************************************************************/
105*91f16700Schasinglulu static void tspd_cpu_on_finish_handler(u_register_t unused)
106*91f16700Schasinglulu {
107*91f16700Schasinglulu 	int32_t rc = 0;
108*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
109*91f16700Schasinglulu 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
110*91f16700Schasinglulu 	entry_point_info_t tsp_on_entrypoint;
111*91f16700Schasinglulu 
112*91f16700Schasinglulu 	assert(tsp_vectors);
113*91f16700Schasinglulu 	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_OFF);
114*91f16700Schasinglulu 
115*91f16700Schasinglulu 	tspd_init_tsp_ep_state(&tsp_on_entrypoint,
116*91f16700Schasinglulu 				TSP_AARCH64,
117*91f16700Schasinglulu 				(uint64_t) &tsp_vectors->cpu_on_entry,
118*91f16700Schasinglulu 				tsp_ctx);
119*91f16700Schasinglulu 
120*91f16700Schasinglulu 	/* Initialise this cpu's secure context */
121*91f16700Schasinglulu 	cm_init_my_context(&tsp_on_entrypoint);
122*91f16700Schasinglulu 
123*91f16700Schasinglulu #if TSP_NS_INTR_ASYNC_PREEMPT
124*91f16700Schasinglulu 	/*
125*91f16700Schasinglulu 	 * Disable the NS interrupt locally since it will be enabled globally
126*91f16700Schasinglulu 	 * within cm_init_my_context.
127*91f16700Schasinglulu 	 */
128*91f16700Schasinglulu 	disable_intr_rm_local(INTR_TYPE_NS, SECURE);
129*91f16700Schasinglulu #endif
130*91f16700Schasinglulu 
131*91f16700Schasinglulu 	/* Enter the TSP */
132*91f16700Schasinglulu 	rc = tspd_synchronous_sp_entry(tsp_ctx);
133*91f16700Schasinglulu 
134*91f16700Schasinglulu 	/*
135*91f16700Schasinglulu 	 * Read the response from the TSP. A non-zero return means that
136*91f16700Schasinglulu 	 * something went wrong while communicating with the SP.
137*91f16700Schasinglulu 	 */
138*91f16700Schasinglulu 	if (rc != 0)
139*91f16700Schasinglulu 		panic();
140*91f16700Schasinglulu 
141*91f16700Schasinglulu 	/* Update its context to reflect the state the SP is in */
142*91f16700Schasinglulu 	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
143*91f16700Schasinglulu }
144*91f16700Schasinglulu 
145*91f16700Schasinglulu /*******************************************************************************
146*91f16700Schasinglulu  * This cpu has resumed from suspend. The SPD saved the TSP context when it
147*91f16700Schasinglulu  * completed the preceding suspend call. Use that context to program an entry
148*91f16700Schasinglulu  * into the TSP to allow it to do any remaining book keeping
149*91f16700Schasinglulu  ******************************************************************************/
150*91f16700Schasinglulu static void tspd_cpu_suspend_finish_handler(u_register_t max_off_pwrlvl)
151*91f16700Schasinglulu {
152*91f16700Schasinglulu 	int32_t rc = 0;
153*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
154*91f16700Schasinglulu 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
155*91f16700Schasinglulu 
156*91f16700Schasinglulu 	assert(tsp_vectors);
157*91f16700Schasinglulu 	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_SUSPEND);
158*91f16700Schasinglulu 
159*91f16700Schasinglulu 	/* Program the entry point, max_off_pwrlvl and enter the SP */
160*91f16700Schasinglulu 	write_ctx_reg(get_gpregs_ctx(&tsp_ctx->cpu_ctx),
161*91f16700Schasinglulu 		      CTX_GPREG_X0,
162*91f16700Schasinglulu 		      max_off_pwrlvl);
163*91f16700Schasinglulu 	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_resume_entry);
164*91f16700Schasinglulu 	rc = tspd_synchronous_sp_entry(tsp_ctx);
165*91f16700Schasinglulu 
166*91f16700Schasinglulu 	/*
167*91f16700Schasinglulu 	 * Read the response from the TSP. A non-zero return means that
168*91f16700Schasinglulu 	 * something went wrong while communicating with the TSP.
169*91f16700Schasinglulu 	 */
170*91f16700Schasinglulu 	if (rc != 0)
171*91f16700Schasinglulu 		panic();
172*91f16700Schasinglulu 
173*91f16700Schasinglulu 	/* Update its context to reflect the state the SP is in */
174*91f16700Schasinglulu 	set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
175*91f16700Schasinglulu }
176*91f16700Schasinglulu 
177*91f16700Schasinglulu /*******************************************************************************
178*91f16700Schasinglulu  * Return the type of TSP the TSPD is dealing with. Report the current resident
179*91f16700Schasinglulu  * cpu (mpidr format) if it is a UP/UP migratable TSP.
180*91f16700Schasinglulu  ******************************************************************************/
181*91f16700Schasinglulu static int32_t tspd_cpu_migrate_info(u_register_t *resident_cpu)
182*91f16700Schasinglulu {
183*91f16700Schasinglulu 	return TSP_MIGRATE_INFO;
184*91f16700Schasinglulu }
185*91f16700Schasinglulu 
186*91f16700Schasinglulu /*******************************************************************************
187*91f16700Schasinglulu  * System is about to be switched off. Allow the TSPD/TSP to perform
188*91f16700Schasinglulu  * any actions needed.
189*91f16700Schasinglulu  ******************************************************************************/
190*91f16700Schasinglulu static void tspd_system_off(void)
191*91f16700Schasinglulu {
192*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
193*91f16700Schasinglulu 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
194*91f16700Schasinglulu 
195*91f16700Schasinglulu 	assert(tsp_vectors);
196*91f16700Schasinglulu 	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
197*91f16700Schasinglulu 
198*91f16700Schasinglulu 	/*
199*91f16700Schasinglulu 	 * Abort any preempted SMC request before overwriting the SECURE
200*91f16700Schasinglulu 	 * context.
201*91f16700Schasinglulu 	 */
202*91f16700Schasinglulu 	tspd_abort_preempted_smc(tsp_ctx);
203*91f16700Schasinglulu 
204*91f16700Schasinglulu 	/* Program the entry point */
205*91f16700Schasinglulu 	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_off_entry);
206*91f16700Schasinglulu 
207*91f16700Schasinglulu 	/* Enter the TSP. We do not care about the return value because we
208*91f16700Schasinglulu 	 * must continue the shutdown anyway */
209*91f16700Schasinglulu 	tspd_synchronous_sp_entry(tsp_ctx);
210*91f16700Schasinglulu }
211*91f16700Schasinglulu 
212*91f16700Schasinglulu /*******************************************************************************
213*91f16700Schasinglulu  * System is about to be reset. Allow the TSPD/TSP to perform
214*91f16700Schasinglulu  * any actions needed.
215*91f16700Schasinglulu  ******************************************************************************/
216*91f16700Schasinglulu static void tspd_system_reset(void)
217*91f16700Schasinglulu {
218*91f16700Schasinglulu 	uint32_t linear_id = plat_my_core_pos();
219*91f16700Schasinglulu 	tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
220*91f16700Schasinglulu 
221*91f16700Schasinglulu 	assert(tsp_vectors);
222*91f16700Schasinglulu 	assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
223*91f16700Schasinglulu 
224*91f16700Schasinglulu 	/*
225*91f16700Schasinglulu 	 * Abort any preempted SMC request before overwriting the SECURE
226*91f16700Schasinglulu 	 * context.
227*91f16700Schasinglulu 	 */
228*91f16700Schasinglulu 	tspd_abort_preempted_smc(tsp_ctx);
229*91f16700Schasinglulu 
230*91f16700Schasinglulu 	/* Program the entry point */
231*91f16700Schasinglulu 	cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_reset_entry);
232*91f16700Schasinglulu 
233*91f16700Schasinglulu 	/*
234*91f16700Schasinglulu 	 * Enter the TSP. We do not care about the return value because we
235*91f16700Schasinglulu 	 * must continue the reset anyway
236*91f16700Schasinglulu 	 */
237*91f16700Schasinglulu 	tspd_synchronous_sp_entry(tsp_ctx);
238*91f16700Schasinglulu }
239*91f16700Schasinglulu 
240*91f16700Schasinglulu /*******************************************************************************
241*91f16700Schasinglulu  * Structure populated by the TSP Dispatcher to be given a chance to perform any
242*91f16700Schasinglulu  * TSP bookkeeping before PSCI executes a power mgmt.  operation.
243*91f16700Schasinglulu  ******************************************************************************/
244*91f16700Schasinglulu const spd_pm_ops_t tspd_pm = {
245*91f16700Schasinglulu 	.svc_on = tspd_cpu_on_handler,
246*91f16700Schasinglulu 	.svc_off = tspd_cpu_off_handler,
247*91f16700Schasinglulu 	.svc_suspend = tspd_cpu_suspend_handler,
248*91f16700Schasinglulu 	.svc_on_finish = tspd_cpu_on_finish_handler,
249*91f16700Schasinglulu 	.svc_suspend_finish = tspd_cpu_suspend_finish_handler,
250*91f16700Schasinglulu 	.svc_migrate = NULL,
251*91f16700Schasinglulu 	.svc_migrate_info = tspd_cpu_migrate_info,
252*91f16700Schasinglulu 	.svc_system_off = tspd_system_off,
253*91f16700Schasinglulu 	.svc_system_reset = tspd_system_reset
254*91f16700Schasinglulu };
255