blob: 90fa0941e40fabadd8a8749c264c1a7afcd42157 [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 ******************************************************************************/
41#include <stdio.h>
42#include <string.h>
43#include <assert.h>
44#include <arch_helpers.h>
45#include <console.h>
46#include <platform.h>
Achin Gupta375f5382014-02-18 18:12:48 +000047#include <context_mgmt.h>
48#include <runtime_svc.h>
49#include <bl31.h>
50#include <tsp.h>
51#include <psci.h>
Achin Gupta375f5382014-02-18 18:12:48 +000052#include <debug.h>
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +000053#include <uuid.h>
Dan Handley714a0d22014-04-09 13:13:04 +010054#include "tspd_private.h"
Achin Gupta375f5382014-02-18 18:12:48 +000055
56/*******************************************************************************
57 * Single structure to hold information about the various entry points into the
58 * Secure Payload. It is initialised once on the primary core after a cold boot.
59 ******************************************************************************/
60entry_info *tsp_entry_info;
61
62/*******************************************************************************
63 * Array to keep track of per-cpu Secure Payload state
64 ******************************************************************************/
65tsp_context tspd_sp_context[TSPD_CORE_COUNT];
66
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000067
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +000068/* TSP UID */
69DEFINE_SVC_UUID(tsp_uuid,
70 0x5b3056a0, 0x3291, 0x427b, 0x98, 0x11,
71 0x71, 0x68, 0xca, 0x50, 0xf3, 0xfa);
72
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000073int32_t tspd_init(meminfo *bl32_meminfo);
74
75
Achin Gupta375f5382014-02-18 18:12:48 +000076/*******************************************************************************
77 * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
78 * (aarch32/aarch64) if not already known and initialises the context for entry
79 * into the SP for its initialisation.
80 ******************************************************************************/
81int32_t tspd_setup(void)
82{
83 el_change_info *image_info;
84 int32_t rc;
85 uint64_t mpidr = read_mpidr();
86 uint32_t linear_id;
87
88 linear_id = platform_get_core_pos(mpidr);
89
90 /*
91 * Get information about the Secure Payload (BL32) image. Its
92 * absence is a critical failure. TODO: Add support to
93 * conditionally include the SPD service
94 */
95 image_info = bl31_get_next_image_info(SECURE);
96 assert(image_info);
97
98 /*
Jeenu Viswambharan7f366602014-02-20 17:11:00 +000099 * If there's no valid entry point for SP, we return a non-zero value
100 * signalling failure initializing the service. We bail out without
101 * registering any handlers
102 */
103 if (!image_info->entrypoint)
104 return 1;
105
106 /*
Achin Gupta375f5382014-02-18 18:12:48 +0000107 * We could inspect the SP image and determine it's execution
108 * state i.e whether AArch32 or AArch64. Assuming it's AArch64
109 * for the time being.
110 */
111 rc = tspd_init_secure_context(image_info->entrypoint,
112 TSP_AARCH64,
113 mpidr,
114 &tspd_sp_context[linear_id]);
115 assert(rc == 0);
116
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000117 /*
118 * All TSPD initialization done. Now register our init function with
119 * BL31 for deferred invocation
120 */
121 bl31_register_bl32_init(&tspd_init);
122
Achin Gupta375f5382014-02-18 18:12:48 +0000123 return rc;
124}
125
126/*******************************************************************************
127 * This function passes control to the Secure Payload image (BL32) for the first
128 * time on the primary cpu after a cold boot. It assumes that a valid secure
129 * context has already been created by tspd_setup() which can be directly used.
130 * It also assumes that a valid non-secure context has been initialised by PSCI
131 * so it does not need to save and restore any non-secure state. This function
132 * performs a synchronous entry into the Secure payload. The SP passes control
133 * back to this routine through a SMC. It also passes the extents of memory made
134 * available to BL32 by BL31.
135 ******************************************************************************/
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000136int32_t tspd_init(meminfo *bl32_meminfo)
Achin Gupta375f5382014-02-18 18:12:48 +0000137{
138 uint64_t mpidr = read_mpidr();
139 uint32_t linear_id = platform_get_core_pos(mpidr);
140 uint64_t rc;
141 tsp_context *tsp_ctx = &tspd_sp_context[linear_id];
142
143 /*
144 * Arrange for passing a pointer to the meminfo structure
145 * describing the memory extents available to the secure
146 * payload.
147 * TODO: We are passing a pointer to BL31 internal memory
148 * whereas this structure should be copied to a communication
149 * buffer between the SP and SPD.
150 */
151 write_ctx_reg(get_gpregs_ctx(&tsp_ctx->cpu_ctx),
152 CTX_GPREG_X0,
153 (uint64_t) bl32_meminfo);
154
Achin Gupta607084e2014-02-09 18:24:19 +0000155 /*
156 * Arrange for an entry into the test secure payload. We expect an array
157 * of vectors in return
158 */
Achin Gupta375f5382014-02-18 18:12:48 +0000159 rc = tspd_synchronous_sp_entry(tsp_ctx);
160 assert(rc != 0);
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000161 if (rc) {
Achin Gupta375f5382014-02-18 18:12:48 +0000162 tsp_ctx->state = TSP_STATE_ON;
163
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000164 /*
165 * TSP has been successfully initialized. Register power
166 * managemnt hooks with PSCI
167 */
168 psci_register_spd_pm_hook(&tspd_pm);
169 }
170
Achin Gupta375f5382014-02-18 18:12:48 +0000171 return rc;
172}
173
Jeenu Viswambharan7f366602014-02-20 17:11:00 +0000174
Achin Gupta375f5382014-02-18 18:12:48 +0000175/*******************************************************************************
176 * This function is responsible for handling all SMCs in the Trusted OS/App
177 * range from the non-secure state as defined in the SMC Calling Convention
178 * Document. It is also responsible for communicating with the Secure payload
179 * to delegate work and return results back to the non-secure state. Lastly it
180 * will also return any information that the secure payload needs to do the
181 * work assigned to it.
182 ******************************************************************************/
183uint64_t tspd_smc_handler(uint32_t smc_fid,
184 uint64_t x1,
185 uint64_t x2,
186 uint64_t x3,
187 uint64_t x4,
188 void *cookie,
189 void *handle,
190 uint64_t flags)
191{
Achin Gupta916a2c12014-02-09 23:11:46 +0000192 cpu_context *ns_cpu_context;
193 gp_regs *ns_gp_regs;
Achin Gupta375f5382014-02-18 18:12:48 +0000194 unsigned long mpidr = read_mpidr();
195 uint32_t linear_id = platform_get_core_pos(mpidr), ns;
Achin Gupta916a2c12014-02-09 23:11:46 +0000196 tsp_context *tsp_ctx = &tspd_sp_context[linear_id];
Achin Gupta375f5382014-02-18 18:12:48 +0000197
198 /* Determine which security state this SMC originated from */
199 ns = is_caller_non_secure(flags);
200
201 switch (smc_fid) {
202
203 /*
204 * This function ID is used only by the SP to indicate it has
205 * finished initialising itself after a cold boot
206 */
207 case TSP_ENTRY_DONE:
208 if (ns)
209 SMC_RET1(handle, SMC_UNK);
210
211 /*
212 * Stash the SP entry points information. This is done
213 * only once on the primary cpu
214 */
215 assert(tsp_entry_info == NULL);
216 tsp_entry_info = (entry_info *) x1;
217
218 /*
219 * SP reports completion. The SPD must have initiated
220 * the original request through a synchronous entry
221 * into the SP. Jump back to the original C runtime
222 * context.
223 */
Achin Gupta916a2c12014-02-09 23:11:46 +0000224 tspd_synchronous_sp_exit(tsp_ctx, x1);
Achin Gupta375f5382014-02-18 18:12:48 +0000225
226 /* Should never reach here */
227 assert(0);
228
Achin Gupta607084e2014-02-09 18:24:19 +0000229 /*
230 * These function IDs is used only by the SP to indicate it has
231 * finished:
232 * 1. turning itself on in response to an earlier psci
233 * cpu_on request
234 * 2. resuming itself after an earlier psci cpu_suspend
235 * request.
236 */
237 case TSP_ON_DONE:
238 case TSP_RESUME_DONE:
239
240 /*
241 * These function IDs is used only by the SP to indicate it has
242 * finished:
243 * 1. suspending itself after an earlier psci cpu_suspend
244 * request.
245 * 2. turning itself off in response to an earlier psci
246 * cpu_off request.
247 */
248 case TSP_OFF_DONE:
249 case TSP_SUSPEND_DONE:
250 if (ns)
251 SMC_RET1(handle, SMC_UNK);
252
253 /*
254 * SP reports completion. The SPD must have initiated the
255 * original request through a synchronous entry into the SP.
256 * Jump back to the original C runtime context, and pass x1 as
257 * return value to the caller
258 */
Achin Gupta916a2c12014-02-09 23:11:46 +0000259 tspd_synchronous_sp_exit(tsp_ctx, x1);
Achin Gupta607084e2014-02-09 18:24:19 +0000260
261 /* Should never reach here */
262 assert(0);
263
Achin Gupta916a2c12014-02-09 23:11:46 +0000264 /*
265 * Request from non-secure client to perform an
266 * arithmetic operation or response from secure
267 * payload to an earlier request.
268 */
269 case TSP_FID_ADD:
270 case TSP_FID_SUB:
271 case TSP_FID_MUL:
272 case TSP_FID_DIV:
273 if (ns) {
274 /*
275 * This is a fresh request from the non-secure client.
276 * The parameters are in x1 and x2. Figure out which
277 * registers need to be preserved, save the non-secure
278 * state and send the request to the secure payload.
279 */
280 assert(handle == cm_get_context(mpidr, NON_SECURE));
281 cm_el1_sysregs_context_save(NON_SECURE);
282
283 /* Save x1 and x2 for use by TSP_GET_ARGS call below */
284 SMC_SET_GP(handle, CTX_GPREG_X1, x1);
285 SMC_SET_GP(handle, CTX_GPREG_X2, x2);
286
287 /*
288 * We are done stashing the non-secure context. Ask the
289 * secure payload to do the work now.
290 */
291
292 /*
293 * Verify if there is a valid context to use, copy the
294 * operation type and parameters to the secure context
295 * and jump to the fast smc entry point in the secure
296 * payload. Entry into S-EL1 will take place upon exit
297 * from this function.
298 */
299 assert(&tsp_ctx->cpu_ctx == cm_get_context(mpidr, SECURE));
300 set_aapcs_args7(&tsp_ctx->cpu_ctx, smc_fid, x1, x2, 0, 0,
301 0, 0, 0);
302 cm_set_el3_elr(SECURE, (uint64_t) tsp_entry_info->fast_smc_entry);
303 cm_el1_sysregs_context_restore(SECURE);
304 cm_set_next_eret_context(SECURE);
305
306 return smc_fid;
307 } else {
308 /*
309 * This is the result from the secure client of an
310 * earlier request. The results are in x1-x2. Copy it
311 * into the non-secure context, save the secure state
312 * and return to the non-secure state.
313 */
314 assert(handle == cm_get_context(mpidr, SECURE));
315 cm_el1_sysregs_context_save(SECURE);
316
317 /* Get a reference to the non-secure context */
318 ns_cpu_context = cm_get_context(mpidr, NON_SECURE);
319 assert(ns_cpu_context);
320 ns_gp_regs = get_gpregs_ctx(ns_cpu_context);
321
322 /* Restore non-secure state */
323 cm_el1_sysregs_context_restore(NON_SECURE);
324 cm_set_next_eret_context(NON_SECURE);
325
326 SMC_RET2(ns_gp_regs, x1, x2);
327 }
328
329 break;
330
331 /*
332 * This is a request from the secure payload for more arguments
333 * for an ongoing arithmetic operation requested by the
334 * non-secure world. Simply return the arguments from the non-
335 * secure client in the original call.
336 */
337 case TSP_GET_ARGS:
338 if (ns)
339 SMC_RET1(handle, SMC_UNK);
340
341 /* Get a reference to the non-secure context */
342 ns_cpu_context = cm_get_context(mpidr, NON_SECURE);
343 assert(ns_cpu_context);
344 ns_gp_regs = get_gpregs_ctx(ns_cpu_context);
345
346 SMC_RET2(handle, read_ctx_reg(ns_gp_regs, CTX_GPREG_X1),
347 read_ctx_reg(ns_gp_regs, CTX_GPREG_X2));
348
Jeenu Viswambharandf1ddb52014-02-28 11:23:35 +0000349 case TOS_CALL_COUNT:
350 /*
351 * Return the number of service function IDs implemented to
352 * provide service to non-secure
353 */
354 SMC_RET1(handle, TSP_NUM_FID);
355
356 case TOS_UID:
357 /* Return TSP UID to the caller */
358 SMC_UUID_RET(handle, tsp_uuid);
359
360 case TOS_CALL_VERSION:
361 /* Return the version of current implementation */
362 SMC_RET2(handle, TSP_VERSION_MAJOR, TSP_VERSION_MINOR);
363
Achin Gupta375f5382014-02-18 18:12:48 +0000364 default:
Achin Gupta607084e2014-02-09 18:24:19 +0000365 break;
Achin Gupta375f5382014-02-18 18:12:48 +0000366 }
367
Achin Gupta607084e2014-02-09 18:24:19 +0000368 SMC_RET1(handle, SMC_UNK);
Achin Gupta375f5382014-02-18 18:12:48 +0000369}
370
371/* Define a SPD runtime service descriptor */
372DECLARE_RT_SVC(
373 spd,
374
375 OEN_TOS_START,
376 OEN_TOS_END,
377 SMC_TYPE_FAST,
378 tspd_setup,
379 tspd_smc_handler
380);