blob: b95ee8fbaec2870db6664dafb2c1fc88b53d1914 [file] [log] [blame]
Achin Gupta607084e2014-02-09 18:24:19 +00001/*
Douglas Raillardf2129652016-11-24 15:43:19 +00002 * Copyright (c) 2013-2016, ARM Limited and Contributors. All rights reserved.
Achin Gupta607084e2014-02-09 18:24:19 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Achin Gupta607084e2014-02-09 18:24:19 +00005 */
6
Dan Handley2bd4ef22014-04-09 13:14:54 +01007#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
9#include <arch_helpers.h>
10#include <bl32/tsp/tsp.h>
11#include <common/bl_common.h>
12#include <common/debug.h>
13#include <lib/el3_runtime/context_mgmt.h>
14#include <plat/common/platform.h>
15
Dan Handley714a0d22014-04-09 13:13:04 +010016#include "tspd_private.h"
Achin Gupta607084e2014-02-09 18:24:19 +000017
18/*******************************************************************************
19 * The target cpu is being turned on. Allow the TSPD/TSP to perform any actions
20 * needed. Nothing at the moment.
21 ******************************************************************************/
Masahiro Yamada5ac9d962018-04-19 01:18:48 +090022static void tspd_cpu_on_handler(u_register_t target_cpu)
Achin Gupta607084e2014-02-09 18:24:19 +000023{
24}
25
26/*******************************************************************************
27 * This cpu is being turned off. Allow the TSPD/TSP to perform any actions
28 * needed
29 ******************************************************************************/
Masahiro Yamada5ac9d962018-04-19 01:18:48 +090030static int32_t tspd_cpu_off_handler(u_register_t unused)
Achin Gupta607084e2014-02-09 18:24:19 +000031{
32 int32_t rc = 0;
Soby Mathewda43b662015-07-08 21:45:46 +010033 uint32_t linear_id = plat_my_core_pos();
Dan Handleye2712bc2014-04-10 15:37:22 +010034 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta607084e2014-02-09 18:24:19 +000035
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010036 assert(tsp_vectors);
Achin Gupta18d6eaf2014-05-04 18:23:26 +010037 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
Achin Gupta607084e2014-02-09 18:24:19 +000038
Douglas Raillardf2129652016-11-24 15:43:19 +000039 /*
40 * Abort any preempted SMC request before overwriting the SECURE
41 * context.
42 */
43 tspd_abort_preempted_smc(tsp_ctx);
44
Achin Gupta607084e2014-02-09 18:24:19 +000045 /* Program the entry point and enter the TSP */
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010046 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_off_entry);
Achin Gupta607084e2014-02-09 18:24:19 +000047 rc = tspd_synchronous_sp_entry(tsp_ctx);
48
49 /*
50 * Read the response from the TSP. A non-zero return means that
51 * something went wrong while communicating with the TSP.
52 */
53 if (rc != 0)
54 panic();
55
56 /*
57 * Reset TSP's context for a fresh start when this cpu is turned on
58 * subsequently.
59 */
Achin Gupta18d6eaf2014-05-04 18:23:26 +010060 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
Achin Gupta607084e2014-02-09 18:24:19 +000061
Douglas Raillardf2129652016-11-24 15:43:19 +000062 return 0;
Achin Gupta607084e2014-02-09 18:24:19 +000063}
64
65/*******************************************************************************
66 * This cpu is being suspended. S-EL1 state must have been saved in the
67 * resident cpu (mpidr format) if it is a UP/UP migratable TSP.
68 ******************************************************************************/
Masahiro Yamada5ac9d962018-04-19 01:18:48 +090069static void tspd_cpu_suspend_handler(u_register_t max_off_pwrlvl)
Achin Gupta607084e2014-02-09 18:24:19 +000070{
71 int32_t rc = 0;
Soby Mathewda43b662015-07-08 21:45:46 +010072 uint32_t linear_id = plat_my_core_pos();
Dan Handleye2712bc2014-04-10 15:37:22 +010073 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta607084e2014-02-09 18:24:19 +000074
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010075 assert(tsp_vectors);
Achin Gupta18d6eaf2014-05-04 18:23:26 +010076 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
Achin Gupta607084e2014-02-09 18:24:19 +000077
Douglas Raillardf2129652016-11-24 15:43:19 +000078 /*
79 * Abort any preempted SMC request before overwriting the SECURE
80 * context.
81 */
82 tspd_abort_preempted_smc(tsp_ctx);
83
Soby Mathewf5121572014-09-30 11:19:51 +010084 /* Program the entry point and enter the TSP */
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010085 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_suspend_entry);
Achin Gupta607084e2014-02-09 18:24:19 +000086 rc = tspd_synchronous_sp_entry(tsp_ctx);
87
88 /*
89 * Read the response from the TSP. A non-zero return means that
90 * something went wrong while communicating with the TSP.
91 */
Douglas Raillardf2129652016-11-24 15:43:19 +000092 if (rc)
Achin Gupta607084e2014-02-09 18:24:19 +000093 panic();
94
95 /* Update its context to reflect the state the TSP is in */
Achin Gupta18d6eaf2014-05-04 18:23:26 +010096 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_SUSPEND);
Achin Gupta607084e2014-02-09 18:24:19 +000097}
98
99/*******************************************************************************
100 * This cpu has been turned on. Enter the TSP to initialise S-EL1 and other bits
Douglas Raillardf2129652016-11-24 15:43:19 +0000101 * before passing control back to the Secure Monitor. Entry in S-EL1 is done
Achin Gupta607084e2014-02-09 18:24:19 +0000102 * after initialising minimal architectural state that guarantees safe
103 * execution.
104 ******************************************************************************/
Masahiro Yamada5ac9d962018-04-19 01:18:48 +0900105static void tspd_cpu_on_finish_handler(u_register_t unused)
Achin Gupta607084e2014-02-09 18:24:19 +0000106{
107 int32_t rc = 0;
Soby Mathewda43b662015-07-08 21:45:46 +0100108 uint32_t linear_id = plat_my_core_pos();
Dan Handleye2712bc2014-04-10 15:37:22 +0100109 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100110 entry_point_info_t tsp_on_entrypoint;
Achin Gupta607084e2014-02-09 18:24:19 +0000111
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100112 assert(tsp_vectors);
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100113 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_OFF);
Achin Gupta607084e2014-02-09 18:24:19 +0000114
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100115 tspd_init_tsp_ep_state(&tsp_on_entrypoint,
Achin Gupta607084e2014-02-09 18:24:19 +0000116 TSP_AARCH64,
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100117 (uint64_t) &tsp_vectors->cpu_on_entry,
Achin Gupta607084e2014-02-09 18:24:19 +0000118 tsp_ctx);
119
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100120 /* Initialise this cpu's secure context */
Soby Mathewda43b662015-07-08 21:45:46 +0100121 cm_init_my_context(&tsp_on_entrypoint);
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100122
Soby Mathewbec98512015-09-03 18:29:38 +0100123#if TSP_NS_INTR_ASYNC_PREEMPT
Soby Mathew47903c02015-01-13 15:48:26 +0000124 /*
125 * Disable the NS interrupt locally since it will be enabled globally
Soby Mathewda43b662015-07-08 21:45:46 +0100126 * within cm_init_my_context.
Soby Mathew47903c02015-01-13 15:48:26 +0000127 */
128 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
129#endif
130
Achin Gupta607084e2014-02-09 18:24:19 +0000131 /* Enter the TSP */
132 rc = tspd_synchronous_sp_entry(tsp_ctx);
133
134 /*
135 * Read the response from the TSP. A non-zero return means that
136 * something went wrong while communicating with the SP.
137 */
138 if (rc != 0)
139 panic();
140
141 /* Update its context to reflect the state the SP is in */
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100142 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
Achin Gupta607084e2014-02-09 18:24:19 +0000143}
144
145/*******************************************************************************
146 * This cpu has resumed from suspend. The SPD saved the TSP context when it
147 * completed the preceding suspend call. Use that context to program an entry
148 * into the TSP to allow it to do any remaining book keeping
149 ******************************************************************************/
Masahiro Yamada5ac9d962018-04-19 01:18:48 +0900150static void tspd_cpu_suspend_finish_handler(u_register_t max_off_pwrlvl)
Achin Gupta607084e2014-02-09 18:24:19 +0000151{
152 int32_t rc = 0;
Soby Mathewda43b662015-07-08 21:45:46 +0100153 uint32_t linear_id = plat_my_core_pos();
Dan Handleye2712bc2014-04-10 15:37:22 +0100154 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta607084e2014-02-09 18:24:19 +0000155
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100156 assert(tsp_vectors);
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100157 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_SUSPEND);
Achin Gupta607084e2014-02-09 18:24:19 +0000158
Achin Gupta9a0ff9b2015-09-07 20:43:27 +0100159 /* Program the entry point, max_off_pwrlvl and enter the SP */
Achin Gupta607084e2014-02-09 18:24:19 +0000160 write_ctx_reg(get_gpregs_ctx(&tsp_ctx->cpu_ctx),
161 CTX_GPREG_X0,
Achin Gupta9a0ff9b2015-09-07 20:43:27 +0100162 max_off_pwrlvl);
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100163 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_resume_entry);
Achin Gupta607084e2014-02-09 18:24:19 +0000164 rc = tspd_synchronous_sp_entry(tsp_ctx);
165
166 /*
167 * Read the response from the TSP. A non-zero return means that
168 * something went wrong while communicating with the TSP.
169 */
170 if (rc != 0)
171 panic();
172
173 /* Update its context to reflect the state the SP is in */
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100174 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
Achin Gupta607084e2014-02-09 18:24:19 +0000175}
176
177/*******************************************************************************
178 * Return the type of TSP the TSPD is dealing with. Report the current resident
179 * cpu (mpidr format) if it is a UP/UP migratable TSP.
180 ******************************************************************************/
Masahiro Yamada5ac9d962018-04-19 01:18:48 +0900181static int32_t tspd_cpu_migrate_info(u_register_t *resident_cpu)
Achin Gupta607084e2014-02-09 18:24:19 +0000182{
183 return TSP_MIGRATE_INFO;
184}
185
186/*******************************************************************************
Juan Castillo4dc4a472014-08-12 11:17:06 +0100187 * System is about to be switched off. Allow the TSPD/TSP to perform
188 * any actions needed.
189 ******************************************************************************/
190static void tspd_system_off(void)
191{
Soby Mathewda43b662015-07-08 21:45:46 +0100192 uint32_t linear_id = plat_my_core_pos();
Juan Castillo4dc4a472014-08-12 11:17:06 +0100193 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
194
195 assert(tsp_vectors);
196 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
197
Douglas Raillardf2129652016-11-24 15:43:19 +0000198 /*
199 * Abort any preempted SMC request before overwriting the SECURE
200 * context.
201 */
202 tspd_abort_preempted_smc(tsp_ctx);
203
Juan Castillo4dc4a472014-08-12 11:17:06 +0100204 /* Program the entry point */
205 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_off_entry);
206
207 /* Enter the TSP. We do not care about the return value because we
208 * must continue the shutdown anyway */
209 tspd_synchronous_sp_entry(tsp_ctx);
210}
211
212/*******************************************************************************
213 * System is about to be reset. Allow the TSPD/TSP to perform
214 * any actions needed.
215 ******************************************************************************/
216static void tspd_system_reset(void)
217{
Soby Mathewda43b662015-07-08 21:45:46 +0100218 uint32_t linear_id = plat_my_core_pos();
Juan Castillo4dc4a472014-08-12 11:17:06 +0100219 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
220
221 assert(tsp_vectors);
222 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
223
Douglas Raillardf2129652016-11-24 15:43:19 +0000224 /*
225 * Abort any preempted SMC request before overwriting the SECURE
226 * context.
227 */
228 tspd_abort_preempted_smc(tsp_ctx);
229
Juan Castillo4dc4a472014-08-12 11:17:06 +0100230 /* Program the entry point */
231 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_reset_entry);
232
Douglas Raillardf2129652016-11-24 15:43:19 +0000233 /*
234 * Enter the TSP. We do not care about the return value because we
235 * must continue the reset anyway
236 */
Juan Castillo4dc4a472014-08-12 11:17:06 +0100237 tspd_synchronous_sp_entry(tsp_ctx);
238}
239
240/*******************************************************************************
Achin Gupta607084e2014-02-09 18:24:19 +0000241 * Structure populated by the TSP Dispatcher to be given a chance to perform any
242 * TSP bookkeeping before PSCI executes a power mgmt. operation.
243 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +0100244const spd_pm_ops_t tspd_pm = {
Juan Castillo4dc4a472014-08-12 11:17:06 +0100245 .svc_on = tspd_cpu_on_handler,
246 .svc_off = tspd_cpu_off_handler,
247 .svc_suspend = tspd_cpu_suspend_handler,
248 .svc_on_finish = tspd_cpu_on_finish_handler,
249 .svc_suspend_finish = tspd_cpu_suspend_finish_handler,
250 .svc_migrate = NULL,
251 .svc_migrate_info = tspd_cpu_migrate_info,
252 .svc_system_off = tspd_system_off,
253 .svc_system_reset = tspd_system_reset
Achin Gupta607084e2014-02-09 18:24:19 +0000254};