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> 14 #include <services/el3_spmc_logical_sp.h> 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 27 /* ID 0 is reserved for the normal world entity, (Hypervisor or OS Kernel). */ 28 #define FFA_NWD_ID U(0) 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 40 /* FF-A Related helper macros. */ 41 #define FFA_ID_MASK U(0xFFFF) 42 #define FFA_PARTITION_ID_SHIFT U(16) 43 #define FFA_FEATURES_BIT31_MASK U(0x1u << 31) 44 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 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 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 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 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 160 /* Mailbox tracking. */ 161 struct mailbox mailbox; 162 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 /* 187 * Mailbox tracking. 188 */ 189 struct mailbox mailbox; 190 191 /* 192 * Supported FF-A Version 193 */ 194 uint32_t ffa_version; 195 }; 196 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 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 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 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 264 #endif /* SPMC_H */ 265