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