Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 1 | /* |
Jeffrey Kardatzke | 7e6b09a | 2022-10-03 15:50:21 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013-2023, ARM Limited and Contributors. All rights reserved. |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 7 | #ifndef OPTEED_PRIVATE_H |
| 8 | #define OPTEED_PRIVATE_H |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 9 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <platform_def.h> |
| 11 | |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 12 | #include <arch.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | #include <bl31/interrupt_mgmt.h> |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 14 | #include <context.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 15 | #include <lib/psci/psci.h> |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 16 | |
| 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 Kardatzke | 7e6b09a | 2022-10-03 15:50:21 -0700 | [diff] [blame] | 21 | #define OPTEE_PSTATE_OFF 1 |
| 22 | #define OPTEE_PSTATE_ON 2 |
| 23 | #define OPTEE_PSTATE_SUSPEND 3 |
| 24 | #define OPTEE_PSTATE_UNKNOWN 0 |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 25 | #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 Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 83 | #ifndef __ASSEMBLER__ |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 84 | |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 85 | #include <stdint.h> |
| 86 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 87 | #include <lib/cassert.h> |
| 88 | |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 89 | typedef uint32_t optee_vector_isn_t; |
| 90 | |
| 91 | typedef struct optee_vectors { |
David Cunado | c8833ea | 2017-04-16 17:15:08 +0100 | [diff] [blame] | 92 | optee_vector_isn_t yield_smc_entry; |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 93 | 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. */ |
| 110 | DEFINE_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 | */ |
| 117 | CASSERT(OPTEED_C_RT_CTX_SIZE == sizeof(c_rt_regs_t), \ |
| 118 | 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 | ******************************************************************************/ |
| 128 | typedef 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 */ |
| 136 | extern const spd_pm_ops_t opteed_pm; |
| 137 | |
| 138 | /******************************************************************************* |
| 139 | * Forward declarations |
| 140 | ******************************************************************************/ |
| 141 | struct optee_vectors; |
| 142 | |
| 143 | /******************************************************************************* |
| 144 | * Function & Data prototypes |
| 145 | ******************************************************************************/ |
| 146 | uint64_t opteed_enter_sp(uint64_t *c_rt_ctx); |
| 147 | void __dead2 opteed_exit_sp(uint64_t c_rt_ctx, uint64_t ret); |
| 148 | uint64_t opteed_synchronous_sp_entry(optee_context_t *optee_ctx); |
| 149 | void __dead2 opteed_synchronous_sp_exit(optee_context_t *optee_ctx, uint64_t ret); |
Sandrine Bailleux | b3b6e22 | 2018-07-11 12:44:22 +0200 | [diff] [blame] | 150 | void opteed_init_optee_ep_state(struct entry_point_info *optee_entry_point, |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 151 | uint32_t rw, |
| 152 | uint64_t pc, |
Edison Ai | 5d685d3 | 2017-07-18 16:52:26 +0800 | [diff] [blame] | 153 | uint64_t pageable_part, |
| 154 | uint64_t mem_limit, |
Jens Wiklander | ce6cd16 | 2017-08-24 13:16:22 +0200 | [diff] [blame] | 155 | uint64_t dt_addr, |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 156 | optee_context_t *optee_ctx); |
Jeffrey Kardatzke | 7e6b09a | 2022-10-03 15:50:21 -0700 | [diff] [blame] | 157 | void opteed_cpu_on_finish_handler(u_register_t unused); |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 158 | |
| 159 | extern optee_context_t opteed_sp_context[OPTEED_CORE_COUNT]; |
| 160 | extern uint32_t opteed_rw; |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 161 | extern struct optee_vectors *optee_vector_table; |
Julius Werner | 53456fc | 2019-07-09 13:49:11 -0700 | [diff] [blame] | 162 | #endif /*__ASSEMBLER__*/ |
Jens Wiklander | c288886 | 2014-08-04 15:39:58 +0200 | [diff] [blame] | 163 | |
Antonio Nino Diaz | 5eb8837 | 2018-11-08 10:20:19 +0000 | [diff] [blame] | 164 | #endif /* OPTEED_PRIVATE_H */ |