Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #ifndef SPMC_H |
| 8 | #define SPMC_H |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <lib/psci/psci.h> |
| 13 | #include <lib/spinlock.h> |
Marc Bonnici | 9a29704 | 2022-02-14 17:06:09 +0000 | [diff] [blame] | 14 | #include <services/el3_spmc_logical_sp.h> |
Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 15 | #include "spm_common.h" |
| 16 | |
| 17 | /* |
| 18 | * Ranges of FF-A IDs for Normal world and Secure world components. The |
| 19 | * convention matches that used by other SPMCs i.e. Hafnium and OP-TEE. |
| 20 | */ |
| 21 | #define FFA_NWD_ID_BASE 0x0 |
| 22 | #define FFA_NWD_ID_LIMIT 0x7FFF |
| 23 | #define FFA_SWD_ID_BASE 0x8000 |
| 24 | #define FFA_SWD_ID_LIMIT SPMD_DIRECT_MSG_ENDPOINT_ID - 1 |
| 25 | #define FFA_SWD_ID_MASK 0x8000 |
| 26 | |
Marc Bonnici | 8eb1520 | 2021-11-29 17:05:33 +0000 | [diff] [blame] | 27 | /* ID 0 is reserved for the normal world entity, (Hypervisor or OS Kernel). */ |
| 28 | #define FFA_NWD_ID U(0) |
Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 29 | /* First ID is reserved for the SPMC */ |
| 30 | #define FFA_SPMC_ID U(FFA_SWD_ID_BASE) |
| 31 | /* SP IDs are allocated after the SPMC ID */ |
| 32 | #define FFA_SP_ID_BASE (FFA_SPMC_ID + 1) |
| 33 | /* Align with Hafnium implementation */ |
| 34 | #define INV_SP_ID 0x7FFF |
| 35 | |
| 36 | /* FF-A warm boot types. */ |
| 37 | #define FFA_WB_TYPE_S2RAM 0 |
| 38 | #define FFA_WB_TYPE_NOTS2RAM 1 |
| 39 | |
Marc Bonnici | d4bb245 | 2021-12-13 11:08:59 +0000 | [diff] [blame] | 40 | /* FF-A Related helper macros. */ |
Marc Bonnici | 764e667 | 2021-08-31 17:57:04 +0100 | [diff] [blame] | 41 | #define FFA_ID_MASK U(0xFFFF) |
| 42 | #define FFA_PARTITION_ID_SHIFT U(16) |
Marc Bonnici | d4bb245 | 2021-12-13 11:08:59 +0000 | [diff] [blame] | 43 | #define FFA_FEATURES_BIT31_MASK U(0x1u << 31) |
| 44 | |
Marc Bonnici | 764e667 | 2021-08-31 17:57:04 +0100 | [diff] [blame] | 45 | #define FFA_RUN_EP_ID(ep_vcpu_ids) \ |
| 46 | ((ep_vcpu_ids >> FFA_PARTITION_ID_SHIFT) & FFA_ID_MASK) |
| 47 | #define FFA_RUN_VCPU_ID(ep_vcpu_ids) \ |
| 48 | (ep_vcpu_ids & FFA_ID_MASK) |
| 49 | |
Marc Bonnici | 0cf1a15 | 2021-08-25 12:09:37 +0100 | [diff] [blame] | 50 | #define FFA_PAGE_SIZE (4096) |
| 51 | #define FFA_RXTX_PAGE_COUNT_MASK 0x1F |
| 52 | |
| 53 | /* Ensure that the page size used by TF-A is 4k aligned. */ |
| 54 | CASSERT((PAGE_SIZE % FFA_PAGE_SIZE) == 0, assert_aligned_page_size); |
| 55 | |
Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 56 | /* |
| 57 | * Runtime states of an execution context as per the FF-A v1.1 specification. |
| 58 | */ |
| 59 | enum sp_runtime_states { |
| 60 | RT_STATE_WAITING, |
| 61 | RT_STATE_RUNNING, |
| 62 | RT_STATE_PREEMPTED, |
| 63 | RT_STATE_BLOCKED |
| 64 | }; |
| 65 | |
| 66 | /* |
| 67 | * Runtime model of an execution context as per the FF-A v1.1 specification. Its |
| 68 | * value is valid only if the execution context is not in the waiting state. |
| 69 | */ |
| 70 | enum sp_runtime_model { |
| 71 | RT_MODEL_DIR_REQ, |
| 72 | RT_MODEL_RUN, |
| 73 | RT_MODEL_INIT, |
| 74 | RT_MODEL_INTR |
| 75 | }; |
| 76 | |
| 77 | enum sp_runtime_el { |
| 78 | EL1 = 0, |
| 79 | S_EL0, |
| 80 | S_EL1 |
| 81 | }; |
| 82 | |
| 83 | enum sp_execution_state { |
| 84 | SP_STATE_AARCH64 = 0, |
| 85 | SP_STATE_AARCH32 |
| 86 | }; |
| 87 | |
Marc Bonnici | ecc460a | 2021-09-02 13:18:41 +0100 | [diff] [blame] | 88 | enum mailbox_state { |
| 89 | /* There is no message in the mailbox. */ |
| 90 | MAILBOX_STATE_EMPTY, |
| 91 | |
| 92 | /* There is a message that has been populated in the mailbox. */ |
| 93 | MAILBOX_STATE_FULL, |
| 94 | }; |
| 95 | |
| 96 | struct mailbox { |
| 97 | enum mailbox_state state; |
| 98 | |
| 99 | /* RX/TX Buffers. */ |
| 100 | void *rx_buffer; |
| 101 | const void *tx_buffer; |
| 102 | |
| 103 | /* Size of RX/TX Buffer. */ |
| 104 | uint32_t rxtx_page_count; |
| 105 | |
| 106 | /* Lock access to mailbox. */ |
| 107 | spinlock_t lock; |
| 108 | }; |
| 109 | |
Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 110 | /* |
| 111 | * Execution context members for an SP. This is a bit like struct |
| 112 | * vcpu in a hypervisor. |
| 113 | */ |
| 114 | struct sp_exec_ctx { |
| 115 | /* |
| 116 | * Store the stack address to restore C runtime context from after |
| 117 | * returning from a synchronous entry into the SP. |
| 118 | */ |
| 119 | uint64_t c_rt_ctx; |
| 120 | |
| 121 | /* Space to maintain the architectural state of an SP. */ |
| 122 | cpu_context_t cpu_ctx; |
| 123 | |
| 124 | /* Track the current runtime state of the SP. */ |
| 125 | enum sp_runtime_states rt_state; |
| 126 | |
| 127 | /* Track the current runtime model of the SP. */ |
| 128 | enum sp_runtime_model rt_model; |
| 129 | }; |
| 130 | |
| 131 | /* |
| 132 | * Structure to describe the cumulative properties of an SP. |
| 133 | */ |
| 134 | struct secure_partition_desc { |
| 135 | /* |
| 136 | * Execution contexts allocated to this endpoint. Ideally, |
| 137 | * we need as many contexts as there are physical cpus only |
| 138 | * for a S-EL1 SP which is MP-pinned. |
| 139 | */ |
| 140 | struct sp_exec_ctx ec[PLATFORM_CORE_COUNT]; |
| 141 | |
| 142 | /* ID of the Secure Partition. */ |
| 143 | uint16_t sp_id; |
| 144 | |
| 145 | /* Runtime EL. */ |
| 146 | enum sp_runtime_el runtime_el; |
| 147 | |
| 148 | /* Partition UUID. */ |
| 149 | uint32_t uuid[4]; |
| 150 | |
| 151 | /* Partition Properties. */ |
| 152 | uint32_t properties; |
| 153 | |
| 154 | /* Supported FF-A Version. */ |
| 155 | uint32_t ffa_version; |
| 156 | |
| 157 | /* Execution State. */ |
| 158 | enum sp_execution_state execution_state; |
| 159 | |
Marc Bonnici | ecc460a | 2021-09-02 13:18:41 +0100 | [diff] [blame] | 160 | /* Mailbox tracking. */ |
| 161 | struct mailbox mailbox; |
| 162 | |
Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 163 | /* Secondary entrypoint. Only valid for a S-EL1 SP. */ |
| 164 | uintptr_t secondary_ep; |
| 165 | }; |
| 166 | |
| 167 | /* |
| 168 | * This define identifies the only SP that will be initialised and participate |
| 169 | * in FF-A communication. The implementation leaves the door open for more SPs |
| 170 | * to be managed in future but for now it is reasonable to assume that either a |
| 171 | * single S-EL0 or a single S-EL1 SP will be supported. This define will be used |
| 172 | * to identify which SP descriptor to initialise and manage during SP runtime. |
| 173 | */ |
| 174 | #define ACTIVE_SP_DESC_INDEX 0 |
| 175 | |
| 176 | /* |
| 177 | * Structure to describe the cumulative properties of the Hypervisor and |
| 178 | * NS-Endpoints. |
| 179 | */ |
| 180 | struct ns_endpoint_desc { |
| 181 | /* |
| 182 | * ID of the NS-Endpoint or Hypervisor. |
| 183 | */ |
| 184 | uint16_t ns_ep_id; |
| 185 | |
| 186 | /* |
Marc Bonnici | ecc460a | 2021-09-02 13:18:41 +0100 | [diff] [blame] | 187 | * Mailbox tracking. |
| 188 | */ |
| 189 | struct mailbox mailbox; |
| 190 | |
| 191 | /* |
| 192 | * Supported FF-A Version |
Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 193 | */ |
| 194 | uint32_t ffa_version; |
| 195 | }; |
| 196 | |
Marc Bonnici | 37dd8e1 | 2021-08-17 18:00:07 +0100 | [diff] [blame] | 197 | /** |
| 198 | * Holds information returned for each partition by the FFA_PARTITION_INFO_GET |
| 199 | * interface. |
| 200 | */ |
| 201 | struct ffa_partition_info_v1_0 { |
| 202 | uint16_t ep_id; |
| 203 | uint16_t execution_ctx_count; |
| 204 | uint32_t properties; |
| 205 | }; |
| 206 | |
| 207 | /* Extended structure for v1.1. */ |
| 208 | struct ffa_partition_info_v1_1 { |
| 209 | uint16_t ep_id; |
| 210 | uint16_t execution_ctx_count; |
| 211 | uint32_t properties; |
| 212 | uint32_t uuid[4]; |
| 213 | }; |
| 214 | |
Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 215 | /* Setup Function for different SP types. */ |
| 216 | void spmc_sp_common_setup(struct secure_partition_desc *sp, |
| 217 | entry_point_info_t *ep_info); |
| 218 | void spmc_el1_sp_setup(struct secure_partition_desc *sp, |
| 219 | entry_point_info_t *ep_info); |
| 220 | void spmc_sp_common_ep_commit(struct secure_partition_desc *sp, |
| 221 | entry_point_info_t *ep_info); |
| 222 | |
| 223 | /* |
| 224 | * Helper function to perform a synchronous entry into a SP. |
| 225 | */ |
| 226 | uint64_t spmc_sp_synchronous_entry(struct sp_exec_ctx *ec); |
| 227 | |
| 228 | /* |
| 229 | * Helper function to obtain the descriptor of the current SP on a physical cpu. |
| 230 | */ |
| 231 | struct secure_partition_desc *spmc_get_current_sp_ctx(void); |
| 232 | |
| 233 | /* |
| 234 | * Helper function to obtain the execution context of an SP on a |
| 235 | * physical cpu. |
| 236 | */ |
| 237 | struct sp_exec_ctx *spmc_get_sp_ec(struct secure_partition_desc *sp); |
| 238 | |
| 239 | /* |
| 240 | * Helper function to obtain the index of the execution context of an SP on a |
| 241 | * physical cpu. |
| 242 | */ |
| 243 | unsigned int get_ec_index(struct secure_partition_desc *sp); |
| 244 | |
| 245 | uint64_t spmc_ffa_error_return(void *handle, int error_code); |
| 246 | |
| 247 | /* |
| 248 | * Ensure a partition ID does not clash and follows the secure world convention. |
| 249 | */ |
| 250 | bool is_ffa_secure_id_valid(uint16_t partition_id); |
| 251 | |
Marc Bonnici | 9a29704 | 2022-02-14 17:06:09 +0000 | [diff] [blame] | 252 | /* |
| 253 | * Helper function to obtain the array storing the EL3 |
| 254 | * Logical Partition descriptors. |
| 255 | */ |
| 256 | struct el3_lp_desc *get_el3_lp_array(void); |
| 257 | |
Marc Bonnici | a2cfa61 | 2021-11-24 10:33:48 +0000 | [diff] [blame] | 258 | /* |
| 259 | * Helper function to obtain the RX/TX buffer pair descriptor of the Hypervisor |
| 260 | * or OS kernel in the normal world or the last SP that was run. |
| 261 | */ |
| 262 | struct mailbox *spmc_get_mbox_desc(bool secure_origin); |
| 263 | |
Marc Bonnici | 8e1a755 | 2021-12-01 17:57:04 +0000 | [diff] [blame] | 264 | #endif /* SPMC_H */ |