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 #define FFA_PAGE_SIZE (4096) 41 #define FFA_RXTX_PAGE_COUNT_MASK 0x1F 42 43 /* Ensure that the page size used by TF-A is 4k aligned. */ 44 CASSERT((PAGE_SIZE % FFA_PAGE_SIZE) == 0, assert_aligned_page_size); 45 46 /* 47 * Runtime states of an execution context as per the FF-A v1.1 specification. 48 */ 49 enum sp_runtime_states { 50 RT_STATE_WAITING, 51 RT_STATE_RUNNING, 52 RT_STATE_PREEMPTED, 53 RT_STATE_BLOCKED 54 }; 55 56 /* 57 * Runtime model of an execution context as per the FF-A v1.1 specification. Its 58 * value is valid only if the execution context is not in the waiting state. 59 */ 60 enum sp_runtime_model { 61 RT_MODEL_DIR_REQ, 62 RT_MODEL_RUN, 63 RT_MODEL_INIT, 64 RT_MODEL_INTR 65 }; 66 67 enum sp_runtime_el { 68 EL1 = 0, 69 S_EL0, 70 S_EL1 71 }; 72 73 enum sp_execution_state { 74 SP_STATE_AARCH64 = 0, 75 SP_STATE_AARCH32 76 }; 77 78 enum mailbox_state { 79 /* There is no message in the mailbox. */ 80 MAILBOX_STATE_EMPTY, 81 82 /* There is a message that has been populated in the mailbox. */ 83 MAILBOX_STATE_FULL, 84 }; 85 86 struct mailbox { 87 enum mailbox_state state; 88 89 /* RX/TX Buffers. */ 90 void *rx_buffer; 91 const void *tx_buffer; 92 93 /* Size of RX/TX Buffer. */ 94 uint32_t rxtx_page_count; 95 96 /* Lock access to mailbox. */ 97 spinlock_t lock; 98 }; 99 100 /* 101 * Execution context members for an SP. This is a bit like struct 102 * vcpu in a hypervisor. 103 */ 104 struct sp_exec_ctx { 105 /* 106 * Store the stack address to restore C runtime context from after 107 * returning from a synchronous entry into the SP. 108 */ 109 uint64_t c_rt_ctx; 110 111 /* Space to maintain the architectural state of an SP. */ 112 cpu_context_t cpu_ctx; 113 114 /* Track the current runtime state of the SP. */ 115 enum sp_runtime_states rt_state; 116 117 /* Track the current runtime model of the SP. */ 118 enum sp_runtime_model rt_model; 119 }; 120 121 /* 122 * Structure to describe the cumulative properties of an SP. 123 */ 124 struct secure_partition_desc { 125 /* 126 * Execution contexts allocated to this endpoint. Ideally, 127 * we need as many contexts as there are physical cpus only 128 * for a S-EL1 SP which is MP-pinned. 129 */ 130 struct sp_exec_ctx ec[PLATFORM_CORE_COUNT]; 131 132 /* ID of the Secure Partition. */ 133 uint16_t sp_id; 134 135 /* Runtime EL. */ 136 enum sp_runtime_el runtime_el; 137 138 /* Partition UUID. */ 139 uint32_t uuid[4]; 140 141 /* Partition Properties. */ 142 uint32_t properties; 143 144 /* Supported FF-A Version. */ 145 uint32_t ffa_version; 146 147 /* Execution State. */ 148 enum sp_execution_state execution_state; 149 150 /* Mailbox tracking. */ 151 struct mailbox mailbox; 152 153 /* Secondary entrypoint. Only valid for a S-EL1 SP. */ 154 uintptr_t secondary_ep; 155 }; 156 157 /* 158 * This define identifies the only SP that will be initialised and participate 159 * in FF-A communication. The implementation leaves the door open for more SPs 160 * to be managed in future but for now it is reasonable to assume that either a 161 * single S-EL0 or a single S-EL1 SP will be supported. This define will be used 162 * to identify which SP descriptor to initialise and manage during SP runtime. 163 */ 164 #define ACTIVE_SP_DESC_INDEX 0 165 166 /* 167 * Structure to describe the cumulative properties of the Hypervisor and 168 * NS-Endpoints. 169 */ 170 struct ns_endpoint_desc { 171 /* 172 * ID of the NS-Endpoint or Hypervisor. 173 */ 174 uint16_t ns_ep_id; 175 176 /* 177 * Mailbox tracking. 178 */ 179 struct mailbox mailbox; 180 181 /* 182 * Supported FF-A Version 183 */ 184 uint32_t ffa_version; 185 }; 186 187 /** 188 * Holds information returned for each partition by the FFA_PARTITION_INFO_GET 189 * interface. 190 */ 191 struct ffa_partition_info_v1_0 { 192 uint16_t ep_id; 193 uint16_t execution_ctx_count; 194 uint32_t properties; 195 }; 196 197 /* Extended structure for v1.1. */ 198 struct ffa_partition_info_v1_1 { 199 uint16_t ep_id; 200 uint16_t execution_ctx_count; 201 uint32_t properties; 202 uint32_t uuid[4]; 203 }; 204 205 /* Setup Function for different SP types. */ 206 void spmc_sp_common_setup(struct secure_partition_desc *sp, 207 entry_point_info_t *ep_info); 208 void spmc_el1_sp_setup(struct secure_partition_desc *sp, 209 entry_point_info_t *ep_info); 210 void spmc_sp_common_ep_commit(struct secure_partition_desc *sp, 211 entry_point_info_t *ep_info); 212 213 /* 214 * Helper function to perform a synchronous entry into a SP. 215 */ 216 uint64_t spmc_sp_synchronous_entry(struct sp_exec_ctx *ec); 217 218 /* 219 * Helper function to obtain the descriptor of the current SP on a physical cpu. 220 */ 221 struct secure_partition_desc *spmc_get_current_sp_ctx(void); 222 223 /* 224 * Helper function to obtain the execution context of an SP on a 225 * physical cpu. 226 */ 227 struct sp_exec_ctx *spmc_get_sp_ec(struct secure_partition_desc *sp); 228 229 /* 230 * Helper function to obtain the index of the execution context of an SP on a 231 * physical cpu. 232 */ 233 unsigned int get_ec_index(struct secure_partition_desc *sp); 234 235 uint64_t spmc_ffa_error_return(void *handle, int error_code); 236 237 /* 238 * Ensure a partition ID does not clash and follows the secure world convention. 239 */ 240 bool is_ffa_secure_id_valid(uint16_t partition_id); 241 242 /* 243 * Helper function to obtain the array storing the EL3 244 * Logical Partition descriptors. 245 */ 246 struct el3_lp_desc *get_el3_lp_array(void); 247 248 /* 249 * Helper function to obtain the RX/TX buffer pair descriptor of the Hypervisor 250 * or OS kernel in the normal world or the last SP that was run. 251 */ 252 struct mailbox *spmc_get_mbox_desc(bool secure_origin); 253 254 #endif /* SPMC_H */ 255