mhu_wrapper_v2_x.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2022, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stddef.h>
  8. #include <stdint.h>
  9. #include <string.h>
  10. #include <drivers/arm/mhu.h>
  11. #include "mhu_v2_x.h"
  12. #define MHU_NOTIFY_VALUE (1234u)
  13. /*
  14. * MHU devices for host:
  15. * HSE: Host to Secure Enclave (sender device)
  16. * SEH: Secure Enclave to Host (receiver device)
  17. */
  18. struct mhu_v2_x_dev_t MHU1_HSE_DEV = {0, MHU_V2_X_SENDER_FRAME};
  19. struct mhu_v2_x_dev_t MHU1_SEH_DEV = {0, MHU_V2_X_RECEIVER_FRAME};
  20. static enum mhu_error_t error_mapping_to_mhu_error_t(enum mhu_v2_x_error_t err)
  21. {
  22. switch (err) {
  23. case MHU_V_2_X_ERR_NONE:
  24. return MHU_ERR_NONE;
  25. case MHU_V_2_X_ERR_NOT_INIT:
  26. return MHU_ERR_NOT_INIT;
  27. case MHU_V_2_X_ERR_ALREADY_INIT:
  28. return MHU_ERR_ALREADY_INIT;
  29. case MHU_V_2_X_ERR_UNSUPPORTED_VERSION:
  30. return MHU_ERR_UNSUPPORTED_VERSION;
  31. case MHU_V_2_X_ERR_INVALID_ARG:
  32. return MHU_ERR_INVALID_ARG;
  33. case MHU_V_2_X_ERR_GENERAL:
  34. return MHU_ERR_GENERAL;
  35. default:
  36. return MHU_ERR_GENERAL;
  37. }
  38. }
  39. static enum mhu_v2_x_error_t signal_and_wait_for_clear(void)
  40. {
  41. enum mhu_v2_x_error_t err;
  42. struct mhu_v2_x_dev_t *dev = &MHU1_HSE_DEV;
  43. uint32_t val = MHU_NOTIFY_VALUE;
  44. /* Using the last channel for notifications */
  45. uint32_t channel_notify = mhu_v2_x_get_num_channel_implemented(dev) - 1;
  46. err = mhu_v2_x_channel_send(dev, channel_notify, val);
  47. if (err != MHU_V_2_X_ERR_NONE) {
  48. return err;
  49. }
  50. do {
  51. err = mhu_v2_x_channel_poll(dev, channel_notify, &val);
  52. if (err != MHU_V_2_X_ERR_NONE) {
  53. break;
  54. }
  55. } while (val != 0);
  56. return err;
  57. }
  58. static enum mhu_v2_x_error_t wait_for_signal(void)
  59. {
  60. enum mhu_v2_x_error_t err;
  61. struct mhu_v2_x_dev_t *dev = &MHU1_SEH_DEV;
  62. uint32_t val = 0;
  63. /* Using the last channel for notifications */
  64. uint32_t channel_notify = mhu_v2_x_get_num_channel_implemented(dev) - 1;
  65. do {
  66. err = mhu_v2_x_channel_receive(dev, channel_notify, &val);
  67. if (err != MHU_V_2_X_ERR_NONE) {
  68. break;
  69. }
  70. } while (val != MHU_NOTIFY_VALUE);
  71. return err;
  72. }
  73. static enum mhu_v2_x_error_t clear_and_wait_for_next_signal(void)
  74. {
  75. enum mhu_v2_x_error_t err;
  76. struct mhu_v2_x_dev_t *dev = &MHU1_SEH_DEV;
  77. uint32_t num_channels = mhu_v2_x_get_num_channel_implemented(dev);
  78. uint32_t i;
  79. /* Clear all channels */
  80. for (i = 0; i < num_channels; ++i) {
  81. err = mhu_v2_x_channel_clear(dev, i);
  82. if (err != MHU_V_2_X_ERR_NONE) {
  83. return err;
  84. }
  85. }
  86. return wait_for_signal();
  87. }
  88. enum mhu_error_t mhu_init_sender(uintptr_t mhu_sender_base)
  89. {
  90. enum mhu_v2_x_error_t err;
  91. assert(mhu_sender_base != (uintptr_t)NULL);
  92. MHU1_HSE_DEV.base = mhu_sender_base;
  93. err = mhu_v2_x_driver_init(&MHU1_HSE_DEV, MHU_REV_READ_FROM_HW);
  94. return error_mapping_to_mhu_error_t(err);
  95. }
  96. enum mhu_error_t mhu_init_receiver(uintptr_t mhu_receiver_base)
  97. {
  98. enum mhu_v2_x_error_t err;
  99. uint32_t num_channels, i;
  100. assert(mhu_receiver_base != (uintptr_t)NULL);
  101. MHU1_SEH_DEV.base = mhu_receiver_base;
  102. err = mhu_v2_x_driver_init(&MHU1_SEH_DEV, MHU_REV_READ_FROM_HW);
  103. if (err != MHU_V_2_X_ERR_NONE) {
  104. return error_mapping_to_mhu_error_t(err);
  105. }
  106. num_channels = mhu_v2_x_get_num_channel_implemented(&MHU1_SEH_DEV);
  107. /* Mask all channels except the notifying channel */
  108. for (i = 0; i < (num_channels - 1); ++i) {
  109. err = mhu_v2_x_channel_mask_set(&MHU1_SEH_DEV, i, UINT32_MAX);
  110. if (err != MHU_V_2_X_ERR_NONE) {
  111. return error_mapping_to_mhu_error_t(err);
  112. }
  113. }
  114. /* The last channel is used for notifications */
  115. err = mhu_v2_x_channel_mask_clear(
  116. &MHU1_SEH_DEV, (num_channels - 1), UINT32_MAX);
  117. return error_mapping_to_mhu_error_t(err);
  118. }
  119. /*
  120. * Public function. See mhu.h
  121. *
  122. * The basic steps of transferring a message:
  123. * 1. Initiate MHU transfer.
  124. * 2. Send over the size of the payload on Channel 1. It is the very first
  125. * 4 Bytes of the transfer. Continue with Channel 2.
  126. * 3. Send over the payload, writing the channels one after the other
  127. * (4 Bytes each). The last available channel is reserved for controlling
  128. * the transfer.
  129. * When the last channel is reached or no more data is left, STOP.
  130. * 4. Notify the receiver using the last channel and wait for acknowledge.
  131. * If there is still data to transfer, jump to step 3. Otherwise, proceed.
  132. * 5. Close MHU transfer.
  133. *
  134. */
  135. enum mhu_error_t mhu_send_data(const uint8_t *send_buffer, size_t size)
  136. {
  137. enum mhu_v2_x_error_t err;
  138. struct mhu_v2_x_dev_t *dev = &MHU1_HSE_DEV;
  139. uint32_t num_channels = mhu_v2_x_get_num_channel_implemented(dev);
  140. uint32_t chan = 0;
  141. uint32_t i;
  142. uint32_t *p;
  143. /* For simplicity, require the send_buffer to be 4-byte aligned */
  144. if ((uintptr_t)send_buffer & 0x3U) {
  145. return MHU_ERR_INVALID_ARG;
  146. }
  147. err = mhu_v2_x_initiate_transfer(dev);
  148. if (err != MHU_V_2_X_ERR_NONE) {
  149. return error_mapping_to_mhu_error_t(err);
  150. }
  151. /* First send over the size of the actual message */
  152. err = mhu_v2_x_channel_send(dev, chan, (uint32_t)size);
  153. if (err != MHU_V_2_X_ERR_NONE) {
  154. return error_mapping_to_mhu_error_t(err);
  155. }
  156. chan++;
  157. p = (uint32_t *)send_buffer;
  158. for (i = 0; i < size; i += 4) {
  159. err = mhu_v2_x_channel_send(dev, chan, *p++);
  160. if (err != MHU_V_2_X_ERR_NONE) {
  161. return error_mapping_to_mhu_error_t(err);
  162. }
  163. if (++chan == (num_channels - 1)) {
  164. err = signal_and_wait_for_clear();
  165. if (err != MHU_V_2_X_ERR_NONE) {
  166. return error_mapping_to_mhu_error_t(err);
  167. }
  168. chan = 0;
  169. }
  170. }
  171. /* Signal the end of transfer.
  172. * It's not required to send a signal when the message was
  173. * perfectly-aligned (num_channels - 1 channels were used in the last
  174. * round) preventing it from signaling twice at the end of transfer.
  175. */
  176. if (chan != 0) {
  177. err = signal_and_wait_for_clear();
  178. if (err != MHU_V_2_X_ERR_NONE) {
  179. return error_mapping_to_mhu_error_t(err);
  180. }
  181. }
  182. err = mhu_v2_x_close_transfer(dev);
  183. return error_mapping_to_mhu_error_t(err);
  184. }
  185. /*
  186. * Public function. See mhu.h
  187. *
  188. * The basic steps of receiving a message:
  189. * 1. Read the size of the payload from Channel 1. It is the very first
  190. * 4 Bytes of the transfer. Continue with Channel 2.
  191. * 2. Receive the payload, read the channels one after the other
  192. * (4 Bytes each). The last available channel is reserved for controlling
  193. * the transfer.
  194. * When the last channel is reached clear all the channels
  195. * (also sending an acknowledge on the last channel).
  196. * 3. If there is still data to receive wait for a notification on the last
  197. * channel and jump to step 2 as soon as it arrived. Otherwise, proceed.
  198. * 4. End of transfer.
  199. *
  200. */
  201. enum mhu_error_t mhu_receive_data(uint8_t *receive_buffer, size_t *size)
  202. {
  203. enum mhu_v2_x_error_t err;
  204. struct mhu_v2_x_dev_t *dev = &MHU1_SEH_DEV;
  205. uint32_t num_channels = mhu_v2_x_get_num_channel_implemented(dev);
  206. uint32_t chan = 0;
  207. uint32_t message_len;
  208. uint32_t i;
  209. uint32_t *p;
  210. /* For simplicity, require:
  211. * - the receive_buffer to be 4-byte aligned,
  212. * - the buffer size to be a multiple of 4.
  213. */
  214. if (((uintptr_t)receive_buffer & 0x3U) || (*size & 0x3U)) {
  215. return MHU_ERR_INVALID_ARG;
  216. }
  217. /* Busy wait for incoming reply */
  218. err = wait_for_signal();
  219. if (err != MHU_V_2_X_ERR_NONE) {
  220. return error_mapping_to_mhu_error_t(err);
  221. }
  222. /* The first word is the length of the actual message */
  223. err = mhu_v2_x_channel_receive(dev, chan, &message_len);
  224. if (err != MHU_V_2_X_ERR_NONE) {
  225. return error_mapping_to_mhu_error_t(err);
  226. }
  227. chan++;
  228. if (message_len > *size) {
  229. /* Message buffer too small */
  230. *size = message_len;
  231. return MHU_ERR_BUFFER_TOO_SMALL;
  232. }
  233. p = (uint32_t *)receive_buffer;
  234. for (i = 0; i < message_len; i += 4) {
  235. err = mhu_v2_x_channel_receive(dev, chan, p++);
  236. if (err != MHU_V_2_X_ERR_NONE) {
  237. return error_mapping_to_mhu_error_t(err);
  238. }
  239. /* Only wait for next transfer if there is still missing data */
  240. if (++chan == (num_channels - 1) && (message_len - i) > 4) {
  241. /* Busy wait for next transfer */
  242. err = clear_and_wait_for_next_signal();
  243. if (err != MHU_V_2_X_ERR_NONE) {
  244. return error_mapping_to_mhu_error_t(err);
  245. }
  246. chan = 0;
  247. }
  248. }
  249. /* Clear all channels */
  250. for (i = 0; i < num_channels; ++i) {
  251. err = mhu_v2_x_channel_clear(dev, i);
  252. if (err != MHU_V_2_X_ERR_NONE) {
  253. return error_mapping_to_mhu_error_t(err);
  254. }
  255. }
  256. *size = message_len;
  257. return MHU_ERR_NONE;
  258. }
  259. size_t mhu_get_max_message_size(void)
  260. {
  261. struct mhu_v2_x_dev_t *dev = &MHU1_SEH_DEV;
  262. uint32_t num_channels = mhu_v2_x_get_num_channel_implemented(dev);
  263. assert(num_channels != 0);
  264. /*
  265. * Returns only usable size of memory. As one channel is specifically
  266. * used to inform about the size of payload, discard it from avialable
  267. * memory size.
  268. */
  269. return (num_channels - 1) * sizeof(uint32_t);
  270. }