blob: 847b9c5c1a0c93ec1d1caf5bbdd7a082be9fdc4e [file] [log] [blame]
Jens Wiklanderc2888862014-08-04 15:39:58 +02001/*
David Cunadoc8833ea2017-04-16 17:15:08 +01002 * Copyright (c) 2013-2017, ARM Limited and Contributors. All rights reserved.
Jens Wiklanderc2888862014-08-04 15:39:58 +02003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Jens Wiklanderc2888862014-08-04 15:39:58 +02005 */
6
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +00007#ifndef OPTEED_PRIVATE_H
8#define OPTEED_PRIVATE_H
Jens Wiklanderc2888862014-08-04 15:39:58 +02009
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010#include <platform_def.h>
11
Jens Wiklanderc2888862014-08-04 15:39:58 +020012#include <arch.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013#include <bl31/interrupt_mgmt.h>
Jens Wiklanderc2888862014-08-04 15:39:58 +020014#include <context.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000015#include <lib/psci/psci.h>
Jens Wiklanderc2888862014-08-04 15:39:58 +020016
17/*******************************************************************************
18 * OPTEE PM state information e.g. OPTEE is suspended, uninitialised etc
19 * and macros to access the state information in the per-cpu 'state' flags
20 ******************************************************************************/
21#define OPTEE_PSTATE_OFF 0
22#define OPTEE_PSTATE_ON 1
23#define OPTEE_PSTATE_SUSPEND 2
24#define OPTEE_PSTATE_SHIFT 0
25#define OPTEE_PSTATE_MASK 0x3
26#define get_optee_pstate(state) ((state >> OPTEE_PSTATE_SHIFT) & \
27 OPTEE_PSTATE_MASK)
28#define clr_optee_pstate(state) (state &= ~(OPTEE_PSTATE_MASK \
29 << OPTEE_PSTATE_SHIFT))
30#define set_optee_pstate(st, pst) do { \
31 clr_optee_pstate(st); \
32 st |= (pst & OPTEE_PSTATE_MASK) << \
33 OPTEE_PSTATE_SHIFT; \
34 } while (0)
35
36
37/*******************************************************************************
38 * OPTEE execution state information i.e. aarch32 or aarch64
39 ******************************************************************************/
40#define OPTEE_AARCH32 MODE_RW_32
41#define OPTEE_AARCH64 MODE_RW_64
42
43/*******************************************************************************
44 * The OPTEED should know the type of OPTEE
45 ******************************************************************************/
46#define OPTEE_TYPE_UP PSCI_TOS_NOT_UP_MIG_CAP
47#define OPTEE_TYPE_UPM PSCI_TOS_UP_MIG_CAP
48#define OPTEE_TYPE_MP PSCI_TOS_NOT_PRESENT_MP
49
50/*******************************************************************************
51 * OPTEE migrate type information as known to the OPTEED. We assume that
52 * the OPTEED is dealing with an MP Secure Payload.
53 ******************************************************************************/
54#define OPTEE_MIGRATE_INFO OPTEE_TYPE_MP
55
56/*******************************************************************************
57 * Number of cpus that the present on this platform. TODO: Rely on a topology
58 * tree to determine this in the future to avoid assumptions about mpidr
59 * allocation
60 ******************************************************************************/
61#define OPTEED_CORE_COUNT PLATFORM_CORE_COUNT
62
63/*******************************************************************************
64 * Constants that allow assembler code to preserve callee-saved registers of the
65 * C runtime context while performing a security state switch.
66 ******************************************************************************/
67#define OPTEED_C_RT_CTX_X19 0x0
68#define OPTEED_C_RT_CTX_X20 0x8
69#define OPTEED_C_RT_CTX_X21 0x10
70#define OPTEED_C_RT_CTX_X22 0x18
71#define OPTEED_C_RT_CTX_X23 0x20
72#define OPTEED_C_RT_CTX_X24 0x28
73#define OPTEED_C_RT_CTX_X25 0x30
74#define OPTEED_C_RT_CTX_X26 0x38
75#define OPTEED_C_RT_CTX_X27 0x40
76#define OPTEED_C_RT_CTX_X28 0x48
77#define OPTEED_C_RT_CTX_X29 0x50
78#define OPTEED_C_RT_CTX_X30 0x58
79#define OPTEED_C_RT_CTX_SIZE 0x60
80#define OPTEED_C_RT_CTX_ENTRIES (OPTEED_C_RT_CTX_SIZE >> DWORD_SHIFT)
81
82#ifndef __ASSEMBLY__
83
Jens Wiklanderc2888862014-08-04 15:39:58 +020084#include <stdint.h>
85
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000086#include <lib/cassert.h>
87
Jens Wiklanderc2888862014-08-04 15:39:58 +020088typedef uint32_t optee_vector_isn_t;
89
90typedef struct optee_vectors {
David Cunadoc8833ea2017-04-16 17:15:08 +010091 optee_vector_isn_t yield_smc_entry;
Jens Wiklanderc2888862014-08-04 15:39:58 +020092 optee_vector_isn_t fast_smc_entry;
93 optee_vector_isn_t cpu_on_entry;
94 optee_vector_isn_t cpu_off_entry;
95 optee_vector_isn_t cpu_resume_entry;
96 optee_vector_isn_t cpu_suspend_entry;
97 optee_vector_isn_t fiq_entry;
98 optee_vector_isn_t system_off_entry;
99 optee_vector_isn_t system_reset_entry;
100} optee_vectors_t;
101
102/*
103 * The number of arguments to save during a SMC call for OPTEE.
104 * Currently only x1 and x2 are used by OPTEE.
105 */
106#define OPTEE_NUM_ARGS 0x2
107
108/* AArch64 callee saved general purpose register context structure. */
109DEFINE_REG_STRUCT(c_rt_regs, OPTEED_C_RT_CTX_ENTRIES);
110
111/*
112 * Compile time assertion to ensure that both the compiler and linker
113 * have the same double word aligned view of the size of the C runtime
114 * register context.
115 */
116CASSERT(OPTEED_C_RT_CTX_SIZE == sizeof(c_rt_regs_t), \
117 assert_spd_c_rt_regs_size_mismatch);
118
119/*******************************************************************************
120 * Structure which helps the OPTEED to maintain the per-cpu state of OPTEE.
121 * 'state' - collection of flags to track OPTEE state e.g. on/off
122 * 'mpidr' - mpidr to associate a context with a cpu
123 * 'c_rt_ctx' - stack address to restore C runtime context from after
124 * returning from a synchronous entry into OPTEE.
125 * 'cpu_ctx' - space to maintain OPTEE architectural state
126 ******************************************************************************/
127typedef struct optee_context {
128 uint32_t state;
129 uint64_t mpidr;
130 uint64_t c_rt_ctx;
131 cpu_context_t cpu_ctx;
132} optee_context_t;
133
134/* OPTEED power management handlers */
135extern const spd_pm_ops_t opteed_pm;
136
137/*******************************************************************************
138 * Forward declarations
139 ******************************************************************************/
140struct optee_vectors;
141
142/*******************************************************************************
143 * Function & Data prototypes
144 ******************************************************************************/
145uint64_t opteed_enter_sp(uint64_t *c_rt_ctx);
146void __dead2 opteed_exit_sp(uint64_t c_rt_ctx, uint64_t ret);
147uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx);
148void __dead2 opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret);
Sandrine Bailleuxb3b6e222018-07-11 12:44:22 +0200149void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point,
Jens Wiklanderc2888862014-08-04 15:39:58 +0200150 uint32_t rw,
151 uint64_t pc,
Edison Ai5d685d32017-07-18 16:52:26 +0800152 uint64_t pageable_part,
153 uint64_t mem_limit,
Jens Wiklanderce6cd162017-08-24 13:16:22 +0200154 uint64_t dt_addr,
Jens Wiklanderc2888862014-08-04 15:39:58 +0200155 optee_context_t *optee_ctx);
156
157extern optee_context_t opteed_sp_context[OPTEED_CORE_COUNT];
158extern uint32_t opteed_rw;
Daniel Boulbyc5259cc2018-05-15 11:41:55 +0100159extern struct optee_vectors *optee_vector_table;
Jens Wiklanderc2888862014-08-04 15:39:58 +0200160#endif /*__ASSEMBLY__*/
161
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000162#endif /* OPTEED_PRIVATE_H */