blob: c484516d34582b536027d8b66b572a693a7ab545 [file] [log] [blame]
Jens Wiklanderc2888862014-08-04 15:39:58 +02001/*
Raymond Mao5fe9abb2023-10-04 09:36:21 -07002 * Copyright (c) 2013-2023, 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 ******************************************************************************/
Jeffrey Kardatzke7e6b09a2022-10-03 15:50:21 -070021#define OPTEE_PSTATE_OFF 1
22#define OPTEE_PSTATE_ON 2
23#define OPTEE_PSTATE_SUSPEND 3
24#define OPTEE_PSTATE_UNKNOWN 0
Jens Wiklanderc2888862014-08-04 15:39:58 +020025#define OPTEE_PSTATE_SHIFT 0
26#define OPTEE_PSTATE_MASK 0x3
27#define get_optee_pstate(state) ((state >> OPTEE_PSTATE_SHIFT) & \
28 OPTEE_PSTATE_MASK)
29#define clr_optee_pstate(state) (state &= ~(OPTEE_PSTATE_MASK \
30 << OPTEE_PSTATE_SHIFT))
31#define set_optee_pstate(st, pst) do { \
32 clr_optee_pstate(st); \
33 st |= (pst & OPTEE_PSTATE_MASK) << \
34 OPTEE_PSTATE_SHIFT; \
35 } while (0)
36
37
38/*******************************************************************************
39 * OPTEE execution state information i.e. aarch32 or aarch64
40 ******************************************************************************/
41#define OPTEE_AARCH32 MODE_RW_32
42#define OPTEE_AARCH64 MODE_RW_64
43
44/*******************************************************************************
45 * The OPTEED should know the type of OPTEE
46 ******************************************************************************/
47#define OPTEE_TYPE_UP PSCI_TOS_NOT_UP_MIG_CAP
48#define OPTEE_TYPE_UPM PSCI_TOS_UP_MIG_CAP
49#define OPTEE_TYPE_MP PSCI_TOS_NOT_PRESENT_MP
50
51/*******************************************************************************
52 * OPTEE migrate type information as known to the OPTEED. We assume that
53 * the OPTEED is dealing with an MP Secure Payload.
54 ******************************************************************************/
55#define OPTEE_MIGRATE_INFO OPTEE_TYPE_MP
56
57/*******************************************************************************
58 * Number of cpus that the present on this platform. TODO: Rely on a topology
59 * tree to determine this in the future to avoid assumptions about mpidr
60 * allocation
61 ******************************************************************************/
62#define OPTEED_CORE_COUNT PLATFORM_CORE_COUNT
63
64/*******************************************************************************
65 * Constants that allow assembler code to preserve callee-saved registers of the
66 * C runtime context while performing a security state switch.
67 ******************************************************************************/
68#define OPTEED_C_RT_CTX_X19 0x0
69#define OPTEED_C_RT_CTX_X20 0x8
70#define OPTEED_C_RT_CTX_X21 0x10
71#define OPTEED_C_RT_CTX_X22 0x18
72#define OPTEED_C_RT_CTX_X23 0x20
73#define OPTEED_C_RT_CTX_X24 0x28
74#define OPTEED_C_RT_CTX_X25 0x30
75#define OPTEED_C_RT_CTX_X26 0x38
76#define OPTEED_C_RT_CTX_X27 0x40
77#define OPTEED_C_RT_CTX_X28 0x48
78#define OPTEED_C_RT_CTX_X29 0x50
79#define OPTEED_C_RT_CTX_X30 0x58
80#define OPTEED_C_RT_CTX_SIZE 0x60
81#define OPTEED_C_RT_CTX_ENTRIES (OPTEED_C_RT_CTX_SIZE >> DWORD_SHIFT)
82
Julius Werner53456fc2019-07-09 13:49:11 -070083#ifndef __ASSEMBLER__
Jens Wiklanderc2888862014-08-04 15:39:58 +020084
Jens Wiklanderc2888862014-08-04 15:39:58 +020085#include <stdint.h>
86
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000087#include <lib/cassert.h>
88
Jens Wiklanderc2888862014-08-04 15:39:58 +020089typedef uint32_t optee_vector_isn_t;
90
91typedef struct optee_vectors {
David Cunadoc8833ea2017-04-16 17:15:08 +010092 optee_vector_isn_t yield_smc_entry;
Jens Wiklanderc2888862014-08-04 15:39:58 +020093 optee_vector_isn_t fast_smc_entry;
94 optee_vector_isn_t cpu_on_entry;
95 optee_vector_isn_t cpu_off_entry;
96 optee_vector_isn_t cpu_resume_entry;
97 optee_vector_isn_t cpu_suspend_entry;
98 optee_vector_isn_t fiq_entry;
99 optee_vector_isn_t system_off_entry;
100 optee_vector_isn_t system_reset_entry;
101} optee_vectors_t;
102
103/*
104 * The number of arguments to save during a SMC call for OPTEE.
105 * Currently only x1 and x2 are used by OPTEE.
106 */
107#define OPTEE_NUM_ARGS 0x2
108
109/* AArch64 callee saved general purpose register context structure. */
110DEFINE_REG_STRUCT(c_rt_regs, OPTEED_C_RT_CTX_ENTRIES);
111
112/*
113 * Compile time assertion to ensure that both the compiler and linker
114 * have the same double word aligned view of the size of the C runtime
115 * register context.
116 */
Elyes Haouas183638f2023-02-13 10:05:41 +0100117CASSERT(OPTEED_C_RT_CTX_SIZE == sizeof(c_rt_regs_t),
Jens Wiklanderc2888862014-08-04 15:39:58 +0200118 assert_spd_c_rt_regs_size_mismatch);
119
120/*******************************************************************************
121 * Structure which helps the OPTEED to maintain the per-cpu state of OPTEE.
122 * 'state' - collection of flags to track OPTEE state e.g. on/off
123 * 'mpidr' - mpidr to associate a context with a cpu
124 * 'c_rt_ctx' - stack address to restore C runtime context from after
125 * returning from a synchronous entry into OPTEE.
126 * 'cpu_ctx' - space to maintain OPTEE architectural state
127 ******************************************************************************/
128typedef struct optee_context {
129 uint32_t state;
130 uint64_t mpidr;
131 uint64_t c_rt_ctx;
132 cpu_context_t cpu_ctx;
133} optee_context_t;
134
135/* OPTEED power management handlers */
136extern const spd_pm_ops_t opteed_pm;
137
138/*******************************************************************************
139 * Forward declarations
140 ******************************************************************************/
141struct optee_vectors;
142
143/*******************************************************************************
144 * Function & Data prototypes
145 ******************************************************************************/
146uint64_t opteed_enter_sp(uint64_t *c_rt_ctx);
147void __dead2 opteed_exit_sp(uint64_t c_rt_ctx, uint64_t ret);
148uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx);
149void __dead2 opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret);
Sandrine Bailleuxb3b6e222018-07-11 12:44:22 +0200150void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point,
Raymond Mao5fe9abb2023-10-04 09:36:21 -0700151 uint32_t rw, uint64_t pc, uint64_t arg0,
152 uint64_t arg1, uint64_t arg2, uint64_t arg3,
Jens Wiklanderc2888862014-08-04 15:39:58 +0200153 optee_context_t *optee_ctx);
Jeffrey Kardatzke7e6b09a2022-10-03 15:50:21 -0700154void opteed_cpu_on_finish_handler(u_register_t unused);
Jens Wiklanderc2888862014-08-04 15:39:58 +0200155
156extern optee_context_t opteed_sp_context[OPTEED_CORE_COUNT];
157extern uint32_t opteed_rw;
Daniel Boulbyc5259cc2018-05-15 11:41:55 +0100158extern struct optee_vectors *optee_vector_table;
Julius Werner53456fc2019-07-09 13:49:11 -0700159#endif /*__ASSEMBLER__*/
Jens Wiklanderc2888862014-08-04 15:39:58 +0200160
Antonio Nino Diaz5eb88372018-11-08 10:20:19 +0000161#endif /* OPTEED_PRIVATE_H */