blob: 6909cf65e10ac46e584a3fd752241915170481f6 [file] [log] [blame]
Achin Gupta375f5382014-02-18 18:12:48 +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
31
32/*******************************************************************************
33 * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
34 * plug-in component to the Secure Monitor, registered as a runtime service. The
35 * SPD is expected to be a functional extension of the Secure Payload (SP) that
36 * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
37 * the Trusted OS/Applications range to the dispatcher. The SPD will either
38 * handle the request locally or delegate it to the Secure Payload. It is also
39 * responsible for initialising and maintaining communication with the SP.
40 ******************************************************************************/
Achin Gupta375f5382014-02-18 18:12:48 +000041#include <arch_helpers.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010042#include <assert.h>
43#include <bl_common.h>
44#include <bl31.h>
Achin Gupta375f5382014-02-18 18:12:48 +000045#include <context_mgmt.h>
46#include <runtime_svc.h>
Dan Handley2bd4ef22014-04-09 13:14:54 +010047#include <stddef.h>
Achin Gupta375f5382014-02-18 18:12:48 +000048#include <tsp.h>
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +000049#include <uuid.h>
Dan Handley714a0d22014-04-09 13:13:04 +010050#include "tspd_private.h"
Achin Gupta375f5382014-02-18 18:12:48 +000051
52/*******************************************************************************
53 * Single structure to hold information about the various entry points into the
54 * Secure Payload. It is initialised once on the primary core after a cold boot.
55 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010056entry_info_t *tsp_entry_info;
Achin Gupta375f5382014-02-18 18:12:48 +000057
58/*******************************************************************************
59 * Array to keep track of per-cpu Secure Payload state
60 ******************************************************************************/
Dan Handleye2712bc2014-04-10 15:37:22 +010061tsp_context_t tspd_sp_context[TSPD_CORE_COUNT];
Achin Gupta375f5382014-02-18 18:12:48 +000062
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000063
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +000064/* TSP UID */
65DEFINE_SVC_UUID(tsp_uuid,
66 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11,
67 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa);
68
Vikram Kanigirid8c9d262014-05-16 18:48:12 +010069int32_t tspd_init(void);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000070
71
Achin Gupta375f5382014-02-18 18:12:48 +000072/*******************************************************************************
73 * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
74 * (aarch32/aarch64) if not already known and initialises the context for entry
75 * into the SP for its initialisation.
76 ******************************************************************************/
77int32_t tspd_setup(void)
78{
Vikram Kanigirida567432014-04-15 18:08:08 +010079 entry_point_info_t *image_info;
Achin Gupta375f5382014-02-18 18:12:48 +000080 int32_t rc;
81 uint64_t mpidr = read_mpidr();
82 uint32_t linear_id;
83
84 linear_id = platform_get_core_pos(mpidr);
85
86 /*
87 * Get information about the Secure Payload (BL32) image. Its
88 * absence is a critical failure. TODO: Add support to
89 * conditionally include the SPD service
90 */
91 image_info = bl31_get_next_image_info(SECURE);
92 assert(image_info);
93
94 /*
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000095 * If there's no valid entry point for SP, we return a non-zero value
96 * signalling failure initializing the service. We bail out without
97 * registering any handlers
98 */
Vikram Kanigirida567432014-04-15 18:08:08 +010099 if (!image_info->pc)
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000100 return 1;
101
102 /*
Achin Gupta375f5382014-02-18 18:12:48 +0000103 * We could inspect the SP image and determine it's execution
104 * state i.e whether AArch32 or AArch64. Assuming it's AArch64
105 * for the time being.
106 */
Vikram Kanigirida567432014-04-15 18:08:08 +0100107 rc = tspd_init_secure_context(image_info->pc,
Achin Gupta375f5382014-02-18 18:12:48 +0000108 TSP_AARCH64,
109 mpidr,
110 &tspd_sp_context[linear_id]);
111 assert(rc == 0);
112
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000113 /*
114 * All TSPD initialization done. Now register our init function with
115 * BL31 for deferred invocation
116 */
117 bl31_register_bl32_init(&tspd_init);
118
Achin Gupta375f5382014-02-18 18:12:48 +0000119 return rc;
120}
121
122/*******************************************************************************
123 * This function passes control to the Secure Payload image (BL32) for the first
124 * time on the primary cpu after a cold boot. It assumes that a valid secure
125 * context has already been created by tspd_setup() which can be directly used.
126 * It also assumes that a valid non-secure context has been initialised by PSCI
127 * so it does not need to save and restore any non-secure state. This function
128 * performs a synchronous entry into the Secure payload. The SP passes control
Vikram Kanigirid8c9d262014-05-16 18:48:12 +0100129 * back to this routine through a SMC.
Achin Gupta375f5382014-02-18 18:12:48 +0000130 ******************************************************************************/
Vikram Kanigirid8c9d262014-05-16 18:48:12 +0100131int32_t tspd_init(void)
Achin Gupta375f5382014-02-18 18:12:48 +0000132{
133 uint64_t mpidr = read_mpidr();
134 uint32_t linear_id = platform_get_core_pos(mpidr);
135 uint64_t rc;
Dan Handleye2712bc2014-04-10 15:37:22 +0100136 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta375f5382014-02-18 18:12:48 +0000137
138 /*
Achin Gupta607084e2014-02-09 18:24:19 +0000139 * Arrange for an entry into the test secure payload. We expect an array
140 * of vectors in return
141 */
Achin Gupta375f5382014-02-18 18:12:48 +0000142 rc = tspd_synchronous_sp_entry(tsp_ctx);
143 assert(rc != 0);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000144 if (rc) {
Achin Gupta18d6eaf2014-05-04 18:23:26 +0100145 set_tsp_pstate(tsp_ctx->state, TSP_PSTATE_ON);
Achin Gupta375f5382014-02-18 18:12:48 +0000146
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000147 /*
148 * TSP has been successfully initialized. Register power
149 * managemnt hooks with PSCI
150 */
151 psci_register_spd_pm_hook(&tspd_pm);
152 }
153
Achin Gupta375f5382014-02-18 18:12:48 +0000154 return rc;
155}
156
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000157
Achin Gupta375f5382014-02-18 18:12:48 +0000158/*******************************************************************************
159 * This function is responsible for handling all SMCs in the Trusted OS/App
160 * range from the non-secure state as defined in the SMC Calling Convention
161 * Document. It is also responsible for communicating with the Secure payload
162 * to delegate work and return results back to the non-secure state. Lastly it
163 * will also return any information that the secure payload needs to do the
164 * work assigned to it.
165 ******************************************************************************/
166uint64_t tspd_smc_handler(uint32_t smc_fid,
167 uint64_t x1,
168 uint64_t x2,
169 uint64_t x3,
170 uint64_t x4,
171 void *cookie,
172 void *handle,
173 uint64_t flags)
174{
Dan Handleye2712bc2014-04-10 15:37:22 +0100175 cpu_context_t *ns_cpu_context;
176 gp_regs_t *ns_gp_regs;
Achin Gupta375f5382014-02-18 18:12:48 +0000177 unsigned long mpidr = read_mpidr();
178 uint32_t linear_id = platform_get_core_pos(mpidr), ns;
Dan Handleye2712bc2014-04-10 15:37:22 +0100179 tsp_context_t *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta375f5382014-02-18 18:12:48 +0000180
181 /* Determine which security state this SMC originated from */
182 ns = is_caller_non_secure(flags);
183
184 switch (smc_fid) {
185
186 /*
187 * This function ID is used only by the SP to indicate it has
188 * finished initialising itself after a cold boot
189 */
190 case TSP_ENTRY_DONE:
191 if (ns)
192 SMC_RET1(handle, SMC_UNK);
193
194 /*
195 * Stash the SP entry points information. This is done
196 * only once on the primary cpu
197 */
198 assert(tsp_entry_info == NULL);
Dan Handleye2712bc2014-04-10 15:37:22 +0100199 tsp_entry_info = (entry_info_t *) x1;
Achin Gupta375f5382014-02-18 18:12:48 +0000200
201 /*
202 * SP reports completion. The SPD must have initiated
203 * the original request through a synchronous entry
204 * into the SP. Jump back to the original C runtime
205 * context.
206 */
Achin Gupta916a2c12014-02-09 23:11:46 +0000207 tspd_synchronous_sp_exit(tsp_ctx, x1);
Achin Gupta375f5382014-02-18 18:12:48 +0000208
209 /* Should never reach here */
210 assert(0);
211
Achin Gupta607084e2014-02-09 18:24:19 +0000212 /*
213 * These function IDs is used only by the SP to indicate it has
214 * finished:
215 * 1. turning itself on in response to an earlier psci
216 * cpu_on request
217 * 2. resuming itself after an earlier psci cpu_suspend
218 * request.
219 */
220 case TSP_ON_DONE:
221 case TSP_RESUME_DONE:
222
223 /*
224 * These function IDs is used only by the SP to indicate it has
225 * finished:
226 * 1. suspending itself after an earlier psci cpu_suspend
227 * request.
228 * 2. turning itself off in response to an earlier psci
229 * cpu_off request.
230 */
231 case TSP_OFF_DONE:
232 case TSP_SUSPEND_DONE:
233 if (ns)
234 SMC_RET1(handle, SMC_UNK);
235
236 /*
237 * SP reports completion. The SPD must have initiated the
238 * original request through a synchronous entry into the SP.
239 * Jump back to the original C runtime context, and pass x1 as
240 * return value to the caller
241 */
Achin Gupta916a2c12014-02-09 23:11:46 +0000242 tspd_synchronous_sp_exit(tsp_ctx, x1);
Achin Gupta607084e2014-02-09 18:24:19 +0000243
244 /* Should never reach here */
245 assert(0);
246
Achin Gupta916a2c12014-02-09 23:11:46 +0000247 /*
248 * Request from non-secure client to perform an
249 * arithmetic operation or response from secure
250 * payload to an earlier request.
251 */
252 case TSP_FID_ADD:
253 case TSP_FID_SUB:
254 case TSP_FID_MUL:
255 case TSP_FID_DIV:
256 if (ns) {
257 /*
258 * This is a fresh request from the non-secure client.
259 * The parameters are in x1 and x2. Figure out which
260 * registers need to be preserved, save the non-secure
261 * state and send the request to the secure payload.
262 */
263 assert(handle == cm_get_context(mpidr, NON_SECURE));
264 cm_el1_sysregs_context_save(NON_SECURE);
265
266 /* Save x1 and x2 for use by TSP_GET_ARGS call below */
267 SMC_SET_GP(handle, CTX_GPREG_X1, x1);
268 SMC_SET_GP(handle, CTX_GPREG_X2, x2);
269
270 /*
271 * We are done stashing the non-secure context. Ask the
272 * secure payload to do the work now.
273 */
274
275 /*
276 * Verify if there is a valid context to use, copy the
277 * operation type and parameters to the secure context
278 * and jump to the fast smc entry point in the secure
279 * payload. Entry into S-EL1 will take place upon exit
280 * from this function.
281 */
282 assert(&tsp_ctx->cpu_ctx == cm_get_context(mpidr, SECURE));
283 set_aapcs_args7(&tsp_ctx->cpu_ctx, smc_fid, x1, x2, 0, 0,
284 0, 0, 0);
Achin Gupta27b895e2014-05-04 18:38:28 +0100285 cm_set_elr_el3(SECURE, (uint64_t) tsp_entry_info->fast_smc_entry);
Achin Gupta916a2c12014-02-09 23:11:46 +0000286 cm_el1_sysregs_context_restore(SECURE);
287 cm_set_next_eret_context(SECURE);
288
289 return smc_fid;
290 } else {
291 /*
292 * This is the result from the secure client of an
293 * earlier request. The results are in x1-x2. Copy it
294 * into the non-secure context, save the secure state
295 * and return to the non-secure state.
296 */
297 assert(handle == cm_get_context(mpidr, SECURE));
298 cm_el1_sysregs_context_save(SECURE);
299
300 /* Get a reference to the non-secure context */
301 ns_cpu_context = cm_get_context(mpidr, NON_SECURE);
302 assert(ns_cpu_context);
303 ns_gp_regs = get_gpregs_ctx(ns_cpu_context);
304
305 /* Restore non-secure state */
306 cm_el1_sysregs_context_restore(NON_SECURE);
307 cm_set_next_eret_context(NON_SECURE);
308
309 SMC_RET2(ns_gp_regs, x1, x2);
310 }
311
312 break;
313
314 /*
315 * This is a request from the secure payload for more arguments
316 * for an ongoing arithmetic operation requested by the
317 * non-secure world. Simply return the arguments from the non-
318 * secure client in the original call.
319 */
320 case TSP_GET_ARGS:
321 if (ns)
322 SMC_RET1(handle, SMC_UNK);
323
324 /* Get a reference to the non-secure context */
325 ns_cpu_context = cm_get_context(mpidr, NON_SECURE);
326 assert(ns_cpu_context);
327 ns_gp_regs = get_gpregs_ctx(ns_cpu_context);
328
329 SMC_RET2(handle, read_ctx_reg(ns_gp_regs, CTX_GPREG_X1),
330 read_ctx_reg(ns_gp_regs, CTX_GPREG_X2));
331
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +0000332 case TOS_CALL_COUNT:
333 /*
334 * Return the number of service function IDs implemented to
335 * provide service to non-secure
336 */
337 SMC_RET1(handle, TSP_NUM_FID);
338
339 case TOS_UID:
340 /* Return TSP UID to the caller */
341 SMC_UUID_RET(handle, tsp_uuid);
342
343 case TOS_CALL_VERSION:
344 /* Return the version of current implementation */
345 SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR);
346
Achin Gupta375f5382014-02-18 18:12:48 +0000347 default:
Achin Gupta607084e2014-02-09 18:24:19 +0000348 break;
Achin Gupta375f5382014-02-18 18:12:48 +0000349 }
350
Achin Gupta607084e2014-02-09 18:24:19 +0000351 SMC_RET1(handle, SMC_UNK);
Achin Gupta375f5382014-02-18 18:12:48 +0000352}
353
354/* Define a SPD runtime service descriptor */
355DECLARE_RT_SVC(
356 spd,
357
358 OEN_TOS_START,
359 OEN_TOS_END,
360 SMC_TYPE_FAST,
361 tspd_setup,
362 tspd_smc_handler
363);