blob: 009ff5f4d23136ed4d1b1d084e4699e06e7dd86c [file] [log] [blame]
Achin Gupta607084e2014-02-09 18:24:19 +00001/*
2 * Copyright (c) 2013-2014, ARM Limited and Contributors. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of ARM nor the names of its contributors may be used
15 * to endorse or promote products derived from this software without specific
16 * prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
Achin Gupta607084e2014-02-09 18:24:19 +000031#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010032#include <assert.h>
33#include <bl_common.h>
Achin Gupta607084e2014-02-09 18:24:19 +000034#include <context_mgmt.h>
Achin Gupta607084e2014-02-09 18:24:19 +000035#include <debug.h>
Dan Handleyed6ff952014-05-14 17:44:19 +010036#include <platform.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010037#include <tsp.h>
Dan Handley714a0d22014-04-09 13:13:04 +010038#include "tspd_private.h"
Achin Gupta607084e2014-02-09 18:24:19 +000039
40/*******************************************************************************
41 * The target cpu is being turned on. Allow the TSPD/TSP to perform any actions
42 * needed. Nothing at the moment.
43 ******************************************************************************/
44static void tspd_cpu_on_handler(uint64_t target_cpu)
45{
46}
47
48/*******************************************************************************
49 * This cpu is being turned off. Allow the TSPD/TSP to perform any actions
50 * needed
51 ******************************************************************************/
Soby Mathewf5121572014-09-30 11:19:51 +010052static int32_t tspd_cpu_off_handler(uint64_t unused)
Achin Gupta607084e2014-02-09 18:24:19 +000053{
54 int32_t rc = 0;
55 uint64_t mpidr = read_mpidr();
56 uint32_t linear_id = platform_get_core_pos(mpidr);
Dan Handleye2712bc2014-04-10 15:37:22 +010057 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta607084e2014-02-09 18:24:19 +000058
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010059 assert(tsp_vectors);
Achin Gupta18d6eaf2014-05-04 18:23:26 +010060 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
Achin Gupta607084e2014-02-09 18:24:19 +000061
62 /* Program the entry point and enter the TSP */
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010063 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_off_entry);
Achin Gupta607084e2014-02-09 18:24:19 +000064 rc = tspd_synchronous_sp_entry(tsp_ctx);
65
66 /*
67 * Read the response from the TSP. A non-zero return means that
68 * something went wrong while communicating with the TSP.
69 */
70 if (rc != 0)
71 panic();
72
73 /*
74 * Reset TSP's context for a fresh start when this cpu is turned on
75 * subsequently.
76 */
Achin Gupta18d6eaf2014-05-04 18:23:26 +010077 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_OFF);
Achin Gupta607084e2014-02-09 18:24:19 +000078
79 return 0;
80}
81
82/*******************************************************************************
83 * This cpu is being suspended. S-EL1 state must have been saved in the
84 * resident cpu (mpidr format) if it is a UP/UP migratable TSP.
85 ******************************************************************************/
Soby Mathewf5121572014-09-30 11:19:51 +010086static void tspd_cpu_suspend_handler(uint64_t unused)
Achin Gupta607084e2014-02-09 18:24:19 +000087{
88 int32_t rc = 0;
89 uint64_t mpidr = read_mpidr();
90 uint32_t linear_id = platform_get_core_pos(mpidr);
Dan Handleye2712bc2014-04-10 15:37:22 +010091 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta607084e2014-02-09 18:24:19 +000092
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010093 assert(tsp_vectors);
Achin Gupta18d6eaf2014-05-04 18:23:26 +010094 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
Achin Gupta607084e2014-02-09 18:24:19 +000095
Soby Mathewf5121572014-09-30 11:19:51 +010096 /* Program the entry point and enter the TSP */
Andrew Thoelke891c4ca2014-05-20 21:43:27 +010097 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_suspend_entry);
Achin Gupta607084e2014-02-09 18:24:19 +000098 rc = tspd_synchronous_sp_entry(tsp_ctx);
99
100 /*
101 * Read the response from the TSP. A non-zero return means that
102 * something went wrong while communicating with the TSP.
103 */
104 if (rc != 0)
105 panic();
106
107 /* Update its context to reflect the state the TSP is in */
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100108 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_SUSPEND);
Achin Gupta607084e2014-02-09 18:24:19 +0000109}
110
111/*******************************************************************************
112 * This cpu has been turned on. Enter the TSP to initialise S-EL1 and other bits
113 * before passing control back to the Secure Monitor. Entry in S-El1 is done
114 * after initialising minimal architectural state that guarantees safe
115 * execution.
116 ******************************************************************************/
Soby Mathewf5121572014-09-30 11:19:51 +0100117static void tspd_cpu_on_finish_handler(uint64_t unused)
Achin Gupta607084e2014-02-09 18:24:19 +0000118{
119 int32_t rc = 0;
120 uint64_t mpidr = read_mpidr();
121 uint32_t linear_id = platform_get_core_pos(mpidr);
Dan Handleye2712bc2014-04-10 15:37:22 +0100122 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100123 entry_point_info_t tsp_on_entrypoint;
Achin Gupta607084e2014-02-09 18:24:19 +0000124
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100125 assert(tsp_vectors);
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100126 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_OFF);
Achin Gupta607084e2014-02-09 18:24:19 +0000127
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100128 tspd_init_tsp_ep_state(&tsp_on_entrypoint,
Achin Gupta607084e2014-02-09 18:24:19 +0000129 TSP_AARCH64,
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100130 (uint64_t) &tsp_vectors->cpu_on_entry,
Achin Gupta607084e2014-02-09 18:24:19 +0000131 tsp_ctx);
132
Vikram Kanigiri9d70f0f2014-07-15 16:46:43 +0100133 /* Initialise this cpu's secure context */
134 cm_init_context(mpidr, &tsp_on_entrypoint);
135
Soby Mathew47903c02015-01-13 15:48:26 +0000136#if TSPD_ROUTE_IRQ_TO_EL3
137 /*
138 * Disable the NS interrupt locally since it will be enabled globally
139 * within cm_init_context.
140 */
141 disable_intr_rm_local(INTR_TYPE_NS, SECURE);
142#endif
143
Achin Gupta607084e2014-02-09 18:24:19 +0000144 /* Enter the TSP */
145 rc = tspd_synchronous_sp_entry(tsp_ctx);
146
147 /*
148 * Read the response from the TSP. A non-zero return means that
149 * something went wrong while communicating with the SP.
150 */
151 if (rc != 0)
152 panic();
153
154 /* Update its context to reflect the state the SP is in */
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100155 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
Achin Gupta607084e2014-02-09 18:24:19 +0000156}
157
158/*******************************************************************************
159 * This cpu has resumed from suspend. The SPD saved the TSP context when it
160 * completed the preceding suspend call. Use that context to program an entry
161 * into the TSP to allow it to do any remaining book keeping
162 ******************************************************************************/
163static void tspd_cpu_suspend_finish_handler(uint64_t suspend_level)
164{
165 int32_t rc = 0;
166 uint64_t mpidr = read_mpidr();
167 uint32_t linear_id = platform_get_core_pos(mpidr);
Dan Handleye2712bc2014-04-10 15:37:22 +0100168 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta607084e2014-02-09 18:24:19 +0000169
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100170 assert(tsp_vectors);
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100171 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_SUSPEND);
Achin Gupta607084e2014-02-09 18:24:19 +0000172
173 /* Program the entry point, suspend_level and enter the SP */
174 write_ctx_reg(get_gpregs_ctx(&tsp_ctx->cpu_ctx),
175 CTX_GPREG_X0,
176 suspend_level);
Andrew Thoelke891c4ca2014-05-20 21:43:27 +0100177 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->cpu_resume_entry);
Achin Gupta607084e2014-02-09 18:24:19 +0000178 rc = tspd_synchronous_sp_entry(tsp_ctx);
179
180 /*
181 * Read the response from the TSP. A non-zero return means that
182 * something went wrong while communicating with the TSP.
183 */
184 if (rc != 0)
185 panic();
186
187 /* Update its context to reflect the state the SP is in */
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100188 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
Achin Gupta607084e2014-02-09 18:24:19 +0000189}
190
191/*******************************************************************************
192 * Return the type of TSP the TSPD is dealing with. Report the current resident
193 * cpu (mpidr format) if it is a UP/UP migratable TSP.
194 ******************************************************************************/
195static int32_t tspd_cpu_migrate_info(uint64_t *resident_cpu)
196{
197 return TSP_MIGRATE_INFO;
198}
199
200/*******************************************************************************
Juan Castillo4dc4a472014-08-12 11:17:06 +0100201 * System is about to be switched off. Allow the TSPD/TSP to perform
202 * any actions needed.
203 ******************************************************************************/
204static void tspd_system_off(void)
205{
206 uint64_t mpidr = read_mpidr();
207 uint32_t linear_id = platform_get_core_pos(mpidr);
208 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
209
210 assert(tsp_vectors);
211 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
212
213 /* Program the entry point */
214 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_off_entry);
215
216 /* Enter the TSP. We do not care about the return value because we
217 * must continue the shutdown anyway */
218 tspd_synchronous_sp_entry(tsp_ctx);
219}
220
221/*******************************************************************************
222 * System is about to be reset. Allow the TSPD/TSP to perform
223 * any actions needed.
224 ******************************************************************************/
225static void tspd_system_reset(void)
226{
227 uint64_t mpidr = read_mpidr();
228 uint32_t linear_id = platform_get_core_pos(mpidr);
229 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
230
231 assert(tsp_vectors);
232 assert(get_tsp_pstate(tsp_ctx->state) == TSP_PSTATE_ON);
233
234 /* Program the entry point */
235 cm_set_elr_el3(SECURE, (uint64_t) &tsp_vectors->system_reset_entry);
236
237 /* Enter the TSP. We do not care about the return value because we
238 * must continue the reset anyway */
239 tspd_synchronous_sp_entry(tsp_ctx);
240}
241
242/*******************************************************************************
Achin Gupta607084e2014-02-09 18:24:19 +0000243 * Structure populated by the TSP Dispatcher to be given a chance to perform any
244 * TSP bookkeeping before PSCI executes a power mgmt. operation.
245 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +0100246const spd_pm_ops_t tspd_pm = {
Juan Castillo4dc4a472014-08-12 11:17:06 +0100247 .svc_on = tspd_cpu_on_handler,
248 .svc_off = tspd_cpu_off_handler,
249 .svc_suspend = tspd_cpu_suspend_handler,
250 .svc_on_finish = tspd_cpu_on_finish_handler,
251 .svc_suspend_finish = tspd_cpu_suspend_finish_handler,
252 .svc_migrate = NULL,
253 .svc_migrate_info = tspd_cpu_migrate_info,
254 .svc_system_off = tspd_system_off,
255 .svc_system_reset = tspd_system_reset
Achin Gupta607084e2014-02-09 18:24:19 +0000256};