blob: f5dd74499f821aea3a30dac7b32a6540c0b35ede [file] [log] [blame]
Varun Wadekar3d4e6a52015-03-13 14:01:03 +05301/*
2 * Copyright (c) 2015, 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 * This is the Secure Payload Dispatcher (SPD). The dispatcher is meant to be a
33 * plug-in component to the Secure Monitor, registered as a runtime service. The
34 * SPD is expected to be a functional extension of the Secure Payload (SP) that
35 * executes in Secure EL1. The Secure Monitor will delegate all SMCs targeting
36 * the Trusted OS/Applications range to the dispatcher. The SPD will either
37 * handle the request locally or delegate it to the Secure Payload. It is also
38 * responsible for initialising and maintaining communication with the SP.
39 ******************************************************************************/
40#include <arch_helpers.h>
41#include <assert.h>
42#include <bl_common.h>
43#include <bl31.h>
44#include <context_mgmt.h>
45#include <debug.h>
46#include <errno.h>
47#include <platform.h>
48#include <runtime_svc.h>
49#include <stddef.h>
50#include <tlk.h>
51#include <uuid.h>
52#include "tlkd_private.h"
53
54extern const spd_pm_ops_t tlkd_pm_ops;
55
56/*******************************************************************************
Varun Wadekara70dec32015-08-26 12:49:03 +053057 * Per-cpu Secure Payload state
Varun Wadekar3d4e6a52015-03-13 14:01:03 +053058 ******************************************************************************/
Varun Wadekara70dec32015-08-26 12:49:03 +053059tlk_context_t tlk_ctx;
Varun Wadekar3d4e6a52015-03-13 14:01:03 +053060
61/* TLK UID: RFC-4122 compliant UUID (version-5, sha-1) */
62DEFINE_SVC_UUID(tlk_uuid,
63 0xbd11e9c9, 0x2bba, 0x52ee, 0xb1, 0x72,
64 0x46, 0x1f, 0xba, 0x97, 0x7f, 0x63);
65
66int32_t tlkd_init(void);
67
Varun Wadekar3d4e6a52015-03-13 14:01:03 +053068/*******************************************************************************
69 * Secure Payload Dispatcher setup. The SPD finds out the SP entrypoint and type
70 * (aarch32/aarch64) if not already known and initialises the context for entry
71 * into the SP for its initialisation.
72 ******************************************************************************/
73int32_t tlkd_setup(void)
74{
75 entry_point_info_t *tlk_ep_info;
76
77 /*
78 * Get information about the Secure Payload (BL32) image. Its
79 * absence is a critical failure.
80 */
81 tlk_ep_info = bl31_plat_get_next_image_ep_info(SECURE);
82 if (!tlk_ep_info) {
83 WARN("No SP provided. Booting device without SP"
84 " initialization. SMC`s destined for SP"
85 " will return SMC_UNK\n");
86 return 1;
87 }
88
89 /*
90 * If there's no valid entry point for SP, we return a non-zero value
91 * signalling failure initializing the service. We bail out without
92 * registering any handlers
93 */
94 if (!tlk_ep_info->pc)
95 return 1;
96
97 /*
98 * Inspect the SP image's SPSR and determine it's execution state
99 * i.e whether AArch32 or AArch64.
100 */
101 tlkd_init_tlk_ep_state(tlk_ep_info,
102 (tlk_ep_info->spsr >> MODE_RW_SHIFT) & MODE_RW_MASK,
103 tlk_ep_info->pc,
104 &tlk_ctx);
105
106 /*
107 * All TLK SPD initialization done. Now register our init function
108 * with BL31 for deferred invocation
109 */
110 bl31_register_bl32_init(&tlkd_init);
111
112 return 0;
113}
114
115/*******************************************************************************
116 * This function passes control to the Secure Payload image (BL32) for the first
117 * time on the primary cpu after a cold boot. It assumes that a valid secure
118 * context has already been created by tlkd_setup() which can be directly
119 * used. This function performs a synchronous entry into the Secure payload.
120 * The SP passes control back to this routine through a SMC.
121 ******************************************************************************/
122int32_t tlkd_init(void)
123{
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530124 entry_point_info_t *tlk_entry_point;
125
126 /*
127 * Get information about the Secure Payload (BL32) image. Its
128 * absence is a critical failure.
129 */
130 tlk_entry_point = bl31_plat_get_next_image_ep_info(SECURE);
131 assert(tlk_entry_point);
132
Soby Mathewda43b662015-07-08 21:45:46 +0100133 cm_init_my_context(tlk_entry_point);
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530134
135 /*
136 * Arrange for an entry into the test secure payload.
137 */
138 return tlkd_synchronous_sp_entry(&tlk_ctx);
139}
140
141/*******************************************************************************
142 * This function is responsible for handling all SMCs in the Trusted OS/App
143 * range from the non-secure state as defined in the SMC Calling Convention
144 * Document. It is also responsible for communicating with the Secure payload
145 * to delegate work and return results back to the non-secure state. Lastly it
146 * will also return any information that the secure payload needs to do the
147 * work assigned to it.
148 ******************************************************************************/
149uint64_t tlkd_smc_handler(uint32_t smc_fid,
150 uint64_t x1,
151 uint64_t x2,
152 uint64_t x3,
153 uint64_t x4,
154 void *cookie,
155 void *handle,
156 uint64_t flags)
157{
Varun Wadekara97535f2015-03-13 14:19:11 +0530158 cpu_context_t *ns_cpu_context;
Varun Wadekarebfeae92015-04-02 14:57:47 +0530159 gp_regs_t *gp_regs;
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530160 uint32_t ns;
Varun Wadekarebfeae92015-04-02 14:57:47 +0530161 uint64_t par;
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530162
163 /* Passing a NULL context is a critical programming error */
164 assert(handle);
165
Varun Wadekarb539b6c2015-03-13 15:18:20 +0530166 /* These SMCs are only supported by CPU0 */
167 if ((read_mpidr() & MPIDR_CPU_MASK) != 0)
168 SMC_RET1(handle, SMC_UNK);
169
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530170 /* Determine which security state this SMC originated from */
171 ns = is_caller_non_secure(flags);
172
173 switch (smc_fid) {
174
175 /*
Varun Wadekar968c0292015-03-13 15:10:54 +0530176 * This function ID is used by SP to indicate that it was
177 * preempted by a non-secure world IRQ.
178 */
179 case TLK_PREEMPTED:
180
181 if (ns)
182 SMC_RET1(handle, SMC_UNK);
183
184 assert(handle == cm_get_context(SECURE));
185 cm_el1_sysregs_context_save(SECURE);
186
187 /* Get a reference to the non-secure context */
188 ns_cpu_context = cm_get_context(NON_SECURE);
189 assert(ns_cpu_context);
190
191 /*
192 * Restore non-secure state. There is no need to save the
193 * secure system register context since the SP was supposed
194 * to preserve it during S-EL1 interrupt handling.
195 */
196 cm_el1_sysregs_context_restore(NON_SECURE);
197 cm_set_next_eret_context(NON_SECURE);
198
Varun Wadekarebfeae92015-04-02 14:57:47 +0530199 SMC_RET1(ns_cpu_context, x1);
Varun Wadekar968c0292015-03-13 15:10:54 +0530200
201 /*
202 * Request from non secure world to resume the preempted
203 * Standard SMC call.
204 */
205 case TLK_RESUME_FID:
206
207 /* RESUME should be invoked only by normal world */
208 if (!ns)
209 SMC_RET1(handle, SMC_UNK);
210
211 /*
212 * This is a resume request from the non-secure client.
213 * save the non-secure state and send the request to
214 * the secure payload.
215 */
216 assert(handle == cm_get_context(NON_SECURE));
217
218 /* Check if we are already preempted before resume */
219 if (!get_std_smc_active_flag(tlk_ctx.state))
220 SMC_RET1(handle, SMC_UNK);
221
222 cm_el1_sysregs_context_save(NON_SECURE);
223
224 /*
225 * We are done stashing the non-secure context. Ask the
226 * secure payload to do the work now.
227 */
228
229 /* We just need to return to the preempted point in
230 * SP and the execution will resume as normal.
231 */
232 cm_el1_sysregs_context_restore(SECURE);
233 cm_set_next_eret_context(SECURE);
234 SMC_RET0(handle);
235
236 /*
Varun Wadekara97535f2015-03-13 14:19:11 +0530237 * This is a request from the non-secure context to:
238 *
239 * a. register shared memory with the SP for storing it's
240 * activity logs.
241 * b. register shared memory with the SP for passing args
242 * required for maintaining sessions with the Trusted
243 * Applications.
Varun Wadekarb539b6c2015-03-13 15:18:20 +0530244 * c. open/close sessions
245 * d. issue commands to the Trusted Apps
Varun Wadekara97535f2015-03-13 14:19:11 +0530246 */
247 case TLK_REGISTER_LOGBUF:
248 case TLK_REGISTER_REQBUF:
Varun Wadekarb539b6c2015-03-13 15:18:20 +0530249 case TLK_OPEN_TA_SESSION:
250 case TLK_CLOSE_TA_SESSION:
251 case TLK_TA_LAUNCH_OP:
252 case TLK_TA_SEND_EVENT:
253
Varun Wadekarebfeae92015-04-02 14:57:47 +0530254 if (!ns)
Varun Wadekara97535f2015-03-13 14:19:11 +0530255 SMC_RET1(handle, SMC_UNK);
256
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(NON_SECURE));
264
265 /* Check if we are already preempted */
266 if (get_std_smc_active_flag(tlk_ctx.state))
267 SMC_RET1(handle, SMC_UNK);
268
269 cm_el1_sysregs_context_save(NON_SECURE);
270
271 /*
272 * Verify if there is a valid context to use.
273 */
274 assert(&tlk_ctx.cpu_ctx == cm_get_context(SECURE));
275
276 /*
277 * Mark the SP state as active.
278 */
279 set_std_smc_active_flag(tlk_ctx.state);
280
Varun Wadekara97535f2015-03-13 14:19:11 +0530281 /*
282 * We are done stashing the non-secure context. Ask the
283 * secure payload to do the work now.
284 */
285 cm_el1_sysregs_context_restore(SECURE);
286 cm_set_next_eret_context(SECURE);
Varun Wadekarebfeae92015-04-02 14:57:47 +0530287
288 /*
289 * TLK is a 32-bit Trusted OS and so expects the SMC
290 * arguments via r0-r7. TLK expects the monitor frame
291 * registers to be 64-bits long. Hence, we pass x0 in
292 * r0-r1, x1 in r2-r3, x3 in r4-r5 and x4 in r6-r7.
293 *
294 * As smc_fid is a uint32 value, r1 contains 0.
295 */
296 gp_regs = get_gpregs_ctx(&tlk_ctx.cpu_ctx);
297 write_ctx_reg(gp_regs, CTX_GPREG_X4, (uint32_t)x2);
298 write_ctx_reg(gp_regs, CTX_GPREG_X5, (uint32_t)(x2 >> 32));
299 write_ctx_reg(gp_regs, CTX_GPREG_X6, (uint32_t)x3);
300 write_ctx_reg(gp_regs, CTX_GPREG_X7, (uint32_t)(x3 >> 32));
301 SMC_RET4(&tlk_ctx.cpu_ctx, smc_fid, 0, (uint32_t)x1,
302 (uint32_t)(x1 >> 32));
Varun Wadekara97535f2015-03-13 14:19:11 +0530303
304 /*
Varun Wadekarebfeae92015-04-02 14:57:47 +0530305 * Translate NS/EL1-S virtual addresses.
306 *
307 * x1 = virtual address
308 * x3 = type (NS/S)
309 *
310 * Returns PA:lo in r0, PA:hi in r1.
Varun Wadekar97625e32015-03-13 14:59:03 +0530311 */
312 case TLK_VA_TRANSLATE:
Varun Wadekarebfeae92015-04-02 14:57:47 +0530313
314 /* Should be invoked only by secure world */
315 if (ns)
Varun Wadekar97625e32015-03-13 14:59:03 +0530316 SMC_RET1(handle, SMC_UNK);
317
Varun Wadekarebfeae92015-04-02 14:57:47 +0530318 /* NS virtual addresses are 64-bit long */
319 if (x3 & TLK_TRANSLATE_NS_VADDR)
320 x1 = (uint32_t)x1 | (x2 << 32);
Varun Wadekar97625e32015-03-13 14:59:03 +0530321
Varun Wadekarebfeae92015-04-02 14:57:47 +0530322 if (!x1)
323 SMC_RET1(handle, SMC_UNK);
Varun Wadekar97625e32015-03-13 14:59:03 +0530324
Varun Wadekarebfeae92015-04-02 14:57:47 +0530325 /*
326 * TODO: Sanity check x1. This would require platform
327 * support.
328 */
Varun Wadekar97625e32015-03-13 14:59:03 +0530329
Varun Wadekarebfeae92015-04-02 14:57:47 +0530330 /* virtual address and type: ns/s */
331 par = tlkd_va_translate(x1, x3);
332
333 /* return physical address in r0-r1 */
334 SMC_RET4(handle, (uint32_t)par, (uint32_t)(par >> 32), 0, 0);
Varun Wadekar97625e32015-03-13 14:59:03 +0530335
336 /*
Varun Wadekara97535f2015-03-13 14:19:11 +0530337 * This is a request from the SP to mark completion of
338 * a standard function ID.
339 */
340 case TLK_REQUEST_DONE:
Varun Wadekarebfeae92015-04-02 14:57:47 +0530341 if (ns)
Varun Wadekara97535f2015-03-13 14:19:11 +0530342 SMC_RET1(handle, SMC_UNK);
343
344 /*
345 * Mark the SP state as inactive.
346 */
347 clr_std_smc_active_flag(tlk_ctx.state);
348
349 /* Get a reference to the non-secure context */
350 ns_cpu_context = cm_get_context(NON_SECURE);
351 assert(ns_cpu_context);
352
353 /*
354 * This is a request completion SMC and we must switch to
355 * the non-secure world to pass the result.
356 */
357 cm_el1_sysregs_context_save(SECURE);
358
359 /*
360 * We are done stashing the secure context. Switch to the
361 * non-secure context and return the result.
362 */
363 cm_el1_sysregs_context_restore(NON_SECURE);
364 cm_set_next_eret_context(NON_SECURE);
Varun Wadekarebfeae92015-04-02 14:57:47 +0530365 SMC_RET1(ns_cpu_context, x1);
Varun Wadekara97535f2015-03-13 14:19:11 +0530366
367 /*
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530368 * This function ID is used only by the SP to indicate it has
369 * finished initialising itself after a cold boot
370 */
371 case TLK_ENTRY_DONE:
Varun Wadekarebfeae92015-04-02 14:57:47 +0530372 if (ns)
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530373 SMC_RET1(handle, SMC_UNK);
374
375 /*
376 * SP has been successfully initialized. Register power
377 * managemnt hooks with PSCI
378 */
379 psci_register_spd_pm_hook(&tlkd_pm_ops);
380
381 /*
382 * TLK reports completion. The SPD must have initiated
383 * the original request through a synchronous entry
384 * into the SP. Jump back to the original C runtime
385 * context.
386 */
Varun Wadekarebfeae92015-04-02 14:57:47 +0530387 tlkd_synchronous_sp_exit(&tlk_ctx, x1);
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530388
389 /*
Varun Wadekara70dec32015-08-26 12:49:03 +0530390 * These function IDs are used only by TLK to indicate it has
391 * finished:
392 * 1. suspending itself after an earlier psci cpu_suspend
393 * request.
394 * 2. resuming itself after an earlier psci cpu_suspend
395 * request.
396 * 3. powering down after an earlier psci system_off/system_reset
397 * request.
398 */
399 case TLK_SUSPEND_DONE:
400 case TLK_RESUME_DONE:
401 case TLK_SYSTEM_OFF_DONE:
402
403 if (ns)
404 SMC_RET1(handle, SMC_UNK);
405
406 /*
407 * TLK reports completion. TLKD must have initiated the
408 * original request through a synchronous entry into the SP.
409 * Jump back to the original C runtime context, and pass x1 as
410 * return value to the caller
411 */
412 tlkd_synchronous_sp_exit(&tlk_ctx, x1);
413
414 /*
Varun Wadekar3d4e6a52015-03-13 14:01:03 +0530415 * Return the number of service function IDs implemented to
416 * provide service to non-secure
417 */
418 case TOS_CALL_COUNT:
419 SMC_RET1(handle, TLK_NUM_FID);
420
421 /*
422 * Return TLK's UID to the caller
423 */
424 case TOS_UID:
425 SMC_UUID_RET(handle, tlk_uuid);
426
427 /*
428 * Return the version of current implementation
429 */
430 case TOS_CALL_VERSION:
431 SMC_RET2(handle, TLK_VERSION_MAJOR, TLK_VERSION_MINOR);
432
433 default:
434 break;
435 }
436
437 SMC_RET1(handle, SMC_UNK);
438}
439
440/* Define a SPD runtime service descriptor for fast SMC calls */
441DECLARE_RT_SVC(
442 tlkd_tos_fast,
443
444 OEN_TOS_START,
445 OEN_TOS_END,
446 SMC_TYPE_FAST,
447 tlkd_setup,
448 tlkd_smc_handler
449);
450
451/* Define a SPD runtime service descriptor for standard SMC calls */
452DECLARE_RT_SVC(
453 tlkd_tos_std,
454
455 OEN_TOS_START,
456 OEN_TOS_END,
457 SMC_TYPE_STD,
458 NULL,
459 tlkd_smc_handler
460);
Varun Wadekarb539b6c2015-03-13 15:18:20 +0530461
462/* Define a SPD runtime service descriptor for fast SMC calls */
463DECLARE_RT_SVC(
464 tlkd_tap_fast,
465
466 OEN_TAP_START,
467 OEN_TAP_END,
468 SMC_TYPE_FAST,
469 NULL,
470 tlkd_smc_handler
471);
472
473/* Define a SPD runtime service descriptor for standard SMC calls */
474DECLARE_RT_SVC(
475 tlkd_tap_std,
476
477 OEN_TAP_START,
478 OEN_TAP_END,
479 SMC_TYPE_STD,
480 NULL,
481 tlkd_smc_handler
482);