ffa_helpers.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2022, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <common/debug.h>
  7. #include "ffa_helpers.h"
  8. #include <services/ffa_svc.h>
  9. #include "tsp_private.h"
  10. /*******************************************************************************
  11. * Wrapper function to send a direct request.
  12. ******************************************************************************/
  13. smc_args_t ffa_msg_send_direct_req(ffa_endpoint_id16_t sender,
  14. ffa_endpoint_id16_t receiver,
  15. uint32_t arg3,
  16. uint32_t arg4,
  17. uint32_t arg5,
  18. uint32_t arg6,
  19. uint32_t arg7)
  20. {
  21. uint32_t src_dst_ids = (sender << FFA_DIRECT_MSG_SOURCE_SHIFT) |
  22. (receiver << FFA_DIRECT_MSG_DESTINATION_SHIFT);
  23. /* Send Direct Request. */
  24. return smc_helper(FFA_MSG_SEND_DIRECT_REQ_SMC64, src_dst_ids,
  25. 0, arg3, arg4, arg5, arg6, arg7);
  26. }
  27. /*******************************************************************************
  28. * Wrapper function to send a direct response.
  29. ******************************************************************************/
  30. smc_args_t *ffa_msg_send_direct_resp(ffa_endpoint_id16_t sender,
  31. ffa_endpoint_id16_t receiver,
  32. uint32_t arg3,
  33. uint32_t arg4,
  34. uint32_t arg5,
  35. uint32_t arg6,
  36. uint32_t arg7)
  37. {
  38. uint32_t src_dst_ids = (sender << FFA_DIRECT_MSG_SOURCE_SHIFT) |
  39. (receiver << FFA_DIRECT_MSG_DESTINATION_SHIFT);
  40. return set_smc_args(FFA_MSG_SEND_DIRECT_RESP_SMC64, src_dst_ids,
  41. 0, arg3, arg4, arg5, arg6, arg7);
  42. }
  43. /*******************************************************************************
  44. * Memory Management Helpers.
  45. ******************************************************************************/
  46. /**
  47. * Initialises the header of the given `ffa_mtd`, not including the
  48. * composite memory region offset.
  49. */
  50. static void ffa_memory_region_init_header(
  51. struct ffa_mtd *memory_region, ffa_endpoint_id16_t sender,
  52. ffa_mem_attr16_t attributes, ffa_mtd_flag32_t flags,
  53. uint64_t handle, uint64_t tag, ffa_endpoint_id16_t *receivers,
  54. uint32_t receiver_count, ffa_mem_perm8_t permissions)
  55. {
  56. struct ffa_emad_v1_0 *emad;
  57. memory_region->emad_offset = sizeof(struct ffa_mtd);
  58. memory_region->emad_size = sizeof(struct ffa_emad_v1_0);
  59. emad = (struct ffa_emad_v1_0 *)
  60. ((uint8_t *) memory_region +
  61. memory_region->emad_offset);
  62. memory_region->sender_id = sender;
  63. memory_region->memory_region_attributes = attributes;
  64. memory_region->reserved_36_39 = 0;
  65. memory_region->flags = flags;
  66. memory_region->handle = handle;
  67. memory_region->tag = tag;
  68. memory_region->reserved_40_47 = 0;
  69. memory_region->emad_count = receiver_count;
  70. for (uint32_t i = 0U; i < receiver_count; i++) {
  71. emad[i].mapd.endpoint_id = receivers[i];
  72. emad[i].mapd.memory_access_permissions = permissions;
  73. emad[i].mapd.flags = 0;
  74. emad[i].comp_mrd_offset = 0;
  75. emad[i].reserved_8_15 = 0;
  76. }
  77. }
  78. /**
  79. * Initialises the given `ffa_mtd` to be used for an
  80. * `FFA_MEM_RETRIEVE_REQ` by the receiver of a memory transaction.
  81. * TODO: Support differing attributes per receiver.
  82. *
  83. * Returns the size of the descriptor written.
  84. */
  85. static uint32_t ffa_memory_retrieve_request_init(
  86. struct ffa_mtd *memory_region, uint64_t handle,
  87. ffa_endpoint_id16_t sender, ffa_endpoint_id16_t *receivers, uint32_t receiver_count,
  88. uint64_t tag, ffa_mtd_flag32_t flags,
  89. ffa_mem_perm8_t permissions,
  90. ffa_mem_attr16_t attributes)
  91. {
  92. ffa_memory_region_init_header(memory_region, sender, attributes, flags,
  93. handle, tag, receivers,
  94. receiver_count, permissions);
  95. return sizeof(struct ffa_mtd) +
  96. memory_region->emad_count * sizeof(struct ffa_emad_v1_0);
  97. }
  98. /* Relinquish access to memory region. */
  99. bool ffa_mem_relinquish(void)
  100. {
  101. smc_args_t ret;
  102. ret = smc_helper(FFA_MEM_RELINQUISH, 0, 0, 0, 0, 0, 0, 0);
  103. if (ffa_func_id(ret) != FFA_SUCCESS_SMC32) {
  104. ERROR("%s failed to relinquish memory! error: (%x) %x\n",
  105. __func__, ffa_func_id(ret), ffa_error_code(ret));
  106. return false;
  107. }
  108. return true;
  109. }
  110. /* Retrieve memory shared by another partition. */
  111. smc_args_t ffa_mem_retrieve_req(uint32_t descriptor_length,
  112. uint32_t fragment_length)
  113. {
  114. return smc_helper(FFA_MEM_RETRIEVE_REQ_SMC32,
  115. descriptor_length,
  116. fragment_length,
  117. 0, 0, 0, 0, 0);
  118. }
  119. /* Retrieve the next memory descriptor fragment. */
  120. smc_args_t ffa_mem_frag_rx(uint64_t handle, uint32_t recv_length)
  121. {
  122. return smc_helper(FFA_MEM_FRAG_RX,
  123. FFA_MEM_HANDLE_LOW(handle),
  124. FFA_MEM_HANDLE_HIGH(handle),
  125. recv_length,
  126. 0, 0, 0, 0);
  127. }
  128. bool memory_retrieve(struct mailbox *mb,
  129. struct ffa_mtd **retrieved,
  130. uint64_t handle, ffa_endpoint_id16_t sender,
  131. ffa_endpoint_id16_t *receivers, uint32_t receiver_count,
  132. ffa_mtd_flag32_t flags, uint32_t *frag_length,
  133. uint32_t *total_length)
  134. {
  135. smc_args_t ret;
  136. uint32_t descriptor_size;
  137. struct ffa_mtd *memory_region;
  138. if (retrieved == NULL || mb == NULL) {
  139. ERROR("Invalid parameters!\n");
  140. return false;
  141. }
  142. memory_region = (struct ffa_mtd *)mb->tx_buffer;
  143. /* Clear TX buffer. */
  144. memset(memory_region, 0, PAGE_SIZE);
  145. /* Clear local buffer. */
  146. memset(mem_region_buffer, 0, REGION_BUF_SIZE);
  147. descriptor_size = ffa_memory_retrieve_request_init(
  148. memory_region, handle, sender, receivers, receiver_count, 0, flags,
  149. FFA_MEM_PERM_RW | FFA_MEM_PERM_NX,
  150. FFA_MEM_ATTR_NORMAL_MEMORY_CACHED_WB |
  151. FFA_MEM_ATTR_INNER_SHAREABLE);
  152. ret = ffa_mem_retrieve_req(descriptor_size, descriptor_size);
  153. if (ffa_func_id(ret) == FFA_ERROR) {
  154. ERROR("Couldn't retrieve the memory page. Error: %x\n",
  155. ffa_error_code(ret));
  156. return false;
  157. }
  158. /*
  159. * Following total_size and fragment_size are useful to keep track
  160. * of the state of transaction. When the sum of all fragment_size of all
  161. * fragments is equal to total_size, the memory transaction has been
  162. * completed.
  163. */
  164. *total_length = ret._regs[1];
  165. *frag_length = ret._regs[2];
  166. /* Validate frag_length is less than total_length and mailbox size. */
  167. if (*frag_length == 0U || *total_length == 0U ||
  168. *frag_length > *total_length || *frag_length > (mb->rxtx_page_count * PAGE_SIZE)) {
  169. ERROR("Invalid parameters!\n");
  170. return false;
  171. }
  172. /* Copy response to local buffer. */
  173. memcpy(mem_region_buffer, mb->rx_buffer, *frag_length);
  174. if (ffa_rx_release()) {
  175. ERROR("Failed to release buffer!\n");
  176. return false;
  177. }
  178. *retrieved = (struct ffa_mtd *) mem_region_buffer;
  179. if ((*retrieved)->emad_count > MAX_MEM_SHARE_RECIPIENTS) {
  180. VERBOSE("SPMC memory sharing supports max of %u receivers!\n",
  181. MAX_MEM_SHARE_RECIPIENTS);
  182. return false;
  183. }
  184. /*
  185. * We are sharing memory from the normal world therefore validate the NS
  186. * bit was set by the SPMC.
  187. */
  188. if (((*retrieved)->memory_region_attributes & FFA_MEM_ATTR_NS_BIT) == 0U) {
  189. ERROR("SPMC has not set the NS bit! 0x%x\n",
  190. (*retrieved)->memory_region_attributes);
  191. return false;
  192. }
  193. VERBOSE("Memory Descriptor Retrieved!\n");
  194. return true;
  195. }
  196. /* Relinquish the memory region. */
  197. bool memory_relinquish(struct ffa_mem_relinquish_descriptor *m, uint64_t handle,
  198. ffa_endpoint_id16_t id)
  199. {
  200. ffa_mem_relinquish_init(m, handle, 0, id);
  201. return ffa_mem_relinquish();
  202. }
  203. /* Query SPMC that the rx buffer of the partition can be released. */
  204. bool ffa_rx_release(void)
  205. {
  206. smc_args_t ret;
  207. ret = smc_helper(FFA_RX_RELEASE, 0, 0, 0, 0, 0, 0, 0);
  208. return ret._regs[SMC_ARG0] != FFA_SUCCESS_SMC32;
  209. }
  210. /* Map the provided buffers with the SPMC. */
  211. bool ffa_rxtx_map(uintptr_t send, uintptr_t recv, uint32_t pages)
  212. {
  213. smc_args_t ret;
  214. ret = smc_helper(FFA_RXTX_MAP_SMC64, send, recv, pages, 0, 0, 0, 0);
  215. return ret._regs[0] != FFA_SUCCESS_SMC32;
  216. }