spmc.h 7.3 KB

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