esp_async_memcpy.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <sys/param.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/semphr.h"
  9. #include "hal/dma_types.h"
  10. #include "esp_check.h"
  11. #include "esp_heap_caps.h"
  12. #include "esp_log.h"
  13. #include "esp_async_memcpy.h"
  14. #include "esp_async_memcpy_impl.h"
  15. static const char *TAG = "async_memcpy";
  16. #define ALIGN_DOWN(val, align) ((val) & ~((align) - 1))
  17. /**
  18. * @brief Type of async mcp stream
  19. * mcp stream inherits DMA descriptor, besides that, it has a callback function member
  20. */
  21. typedef struct {
  22. dma_descriptor_t desc;
  23. async_memcpy_isr_cb_t cb;
  24. void *cb_args;
  25. } async_memcpy_stream_t;
  26. /**
  27. * @brief Type of async mcp driver context
  28. */
  29. typedef struct async_memcpy_context_t {
  30. async_memcpy_impl_t mcp_impl; // implementation layer
  31. portMUX_TYPE spinlock; // spinlock, prevent operating descriptors concurrently
  32. intr_handle_t intr_hdl; // interrupt handle
  33. uint32_t flags; // extra driver flags
  34. dma_descriptor_t *tx_desc; // pointer to the next free TX descriptor
  35. dma_descriptor_t *rx_desc; // pointer to the next free RX descriptor
  36. dma_descriptor_t *next_rx_desc_to_check; // pointer to the next RX descriptor to recycle
  37. uint32_t max_stream_num; // maximum number of streams
  38. size_t max_dma_buffer_size; // maximum DMA buffer size
  39. async_memcpy_stream_t *out_streams; // pointer to the first TX stream
  40. async_memcpy_stream_t *in_streams; // pointer to the first RX stream
  41. async_memcpy_stream_t streams_pool[0]; // stream pool (TX + RX), the size is configured during driver installation
  42. } async_memcpy_context_t;
  43. esp_err_t esp_async_memcpy_install(const async_memcpy_config_t *config, async_memcpy_t *asmcp)
  44. {
  45. esp_err_t ret = ESP_OK;
  46. async_memcpy_context_t *mcp_hdl = NULL;
  47. ESP_GOTO_ON_FALSE(config, ESP_ERR_INVALID_ARG, err, TAG, "configuration can't be null");
  48. ESP_GOTO_ON_FALSE(asmcp, ESP_ERR_INVALID_ARG, err, TAG, "can't assign mcp handle to null");
  49. // context memory size + stream pool size
  50. size_t total_malloc_size = sizeof(async_memcpy_context_t) + sizeof(async_memcpy_stream_t) * config->backlog * 2;
  51. // to work when cache is disabled, the driver handle should located in SRAM
  52. mcp_hdl = heap_caps_calloc(1, total_malloc_size, MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
  53. ESP_GOTO_ON_FALSE(mcp_hdl, ESP_ERR_NO_MEM, err, TAG, "allocate context memory failed");
  54. mcp_hdl->flags = config->flags;
  55. mcp_hdl->out_streams = mcp_hdl->streams_pool;
  56. mcp_hdl->in_streams = mcp_hdl->streams_pool + config->backlog;
  57. mcp_hdl->max_stream_num = config->backlog;
  58. // circle TX/RX descriptors
  59. for (size_t i = 0; i < mcp_hdl->max_stream_num; i++) {
  60. mcp_hdl->out_streams[i].desc.dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_CPU;
  61. mcp_hdl->out_streams[i].desc.next = &mcp_hdl->out_streams[i + 1].desc;
  62. mcp_hdl->in_streams[i].desc.dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_CPU;
  63. mcp_hdl->in_streams[i].desc.next = &mcp_hdl->in_streams[i + 1].desc;
  64. }
  65. mcp_hdl->out_streams[mcp_hdl->max_stream_num - 1].desc.next = &mcp_hdl->out_streams[0].desc;
  66. mcp_hdl->in_streams[mcp_hdl->max_stream_num - 1].desc.next = &mcp_hdl->in_streams[0].desc;
  67. mcp_hdl->tx_desc = &mcp_hdl->out_streams[0].desc;
  68. mcp_hdl->rx_desc = &mcp_hdl->in_streams[0].desc;
  69. mcp_hdl->next_rx_desc_to_check = &mcp_hdl->in_streams[0].desc;
  70. mcp_hdl->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  71. mcp_hdl->mcp_impl.sram_trans_align = config->sram_trans_align;
  72. mcp_hdl->mcp_impl.psram_trans_align = config->psram_trans_align;
  73. size_t trans_align = MAX(config->sram_trans_align, config->psram_trans_align);
  74. mcp_hdl->max_dma_buffer_size = trans_align ? ALIGN_DOWN(DMA_DESCRIPTOR_BUFFER_MAX_SIZE, trans_align) : DMA_DESCRIPTOR_BUFFER_MAX_SIZE;
  75. // initialize implementation layer
  76. ret = async_memcpy_impl_init(&mcp_hdl->mcp_impl);
  77. ESP_GOTO_ON_ERROR(ret, err, TAG, "DMA M2M init failed");
  78. ESP_LOGD(TAG, "installed memory to memory copy channel at %p", mcp_hdl);
  79. *asmcp = mcp_hdl;
  80. async_memcpy_impl_start(&mcp_hdl->mcp_impl, (intptr_t)&mcp_hdl->out_streams[0].desc, (intptr_t)&mcp_hdl->in_streams[0].desc);
  81. return ESP_OK;
  82. err:
  83. if (mcp_hdl) {
  84. free(mcp_hdl);
  85. }
  86. if (asmcp) {
  87. *asmcp = NULL;
  88. }
  89. return ret;
  90. }
  91. esp_err_t esp_async_memcpy_uninstall(async_memcpy_t asmcp)
  92. {
  93. esp_err_t ret = ESP_OK;
  94. ESP_GOTO_ON_FALSE(asmcp, ESP_ERR_INVALID_ARG, err, TAG, "mcp handle can't be null");
  95. async_memcpy_impl_stop(&asmcp->mcp_impl);
  96. async_memcpy_impl_deinit(&asmcp->mcp_impl);
  97. free(asmcp);
  98. err:
  99. return ret;
  100. }
  101. static int async_memcpy_prepare_receive(async_memcpy_t asmcp, void *buffer, size_t size, dma_descriptor_t **start_desc, dma_descriptor_t **end_desc)
  102. {
  103. uint32_t prepared_length = 0;
  104. uint8_t *buf = (uint8_t *)buffer;
  105. dma_descriptor_t *desc = asmcp->rx_desc; // descriptor iterator
  106. dma_descriptor_t *start = desc;
  107. dma_descriptor_t *end = desc;
  108. while (size > asmcp->max_dma_buffer_size) {
  109. if (desc->dw0.owner != DMA_DESCRIPTOR_BUFFER_OWNER_DMA) {
  110. desc->dw0.suc_eof = 0;
  111. desc->dw0.size = asmcp->max_dma_buffer_size;
  112. desc->buffer = &buf[prepared_length];
  113. desc = desc->next; // move to next descriptor
  114. prepared_length += asmcp->max_dma_buffer_size;
  115. size -= asmcp->max_dma_buffer_size;
  116. } else {
  117. // out of RX descriptors
  118. goto _exit;
  119. }
  120. }
  121. if (size) {
  122. if (desc->dw0.owner != DMA_DESCRIPTOR_BUFFER_OWNER_DMA) {
  123. end = desc; // the last descriptor used
  124. desc->dw0.suc_eof = 0;
  125. desc->dw0.size = size;
  126. desc->buffer = &buf[prepared_length];
  127. desc = desc->next; // move to next descriptor
  128. prepared_length += size;
  129. } else {
  130. // out of RX descriptors
  131. goto _exit;
  132. }
  133. }
  134. _exit:
  135. *start_desc = start;
  136. *end_desc = end;
  137. return prepared_length;
  138. }
  139. static int async_memcpy_prepare_transmit(async_memcpy_t asmcp, void *buffer, size_t len, dma_descriptor_t **start_desc, dma_descriptor_t **end_desc)
  140. {
  141. uint32_t prepared_length = 0;
  142. uint8_t *buf = (uint8_t *)buffer;
  143. dma_descriptor_t *desc = asmcp->tx_desc; // descriptor iterator
  144. dma_descriptor_t *start = desc;
  145. dma_descriptor_t *end = desc;
  146. while (len > asmcp->max_dma_buffer_size) {
  147. if (desc->dw0.owner != DMA_DESCRIPTOR_BUFFER_OWNER_DMA) {
  148. desc->dw0.suc_eof = 0; // not the end of the transaction
  149. desc->dw0.size = asmcp->max_dma_buffer_size;
  150. desc->dw0.length = asmcp->max_dma_buffer_size;
  151. desc->buffer = &buf[prepared_length];
  152. desc = desc->next; // move to next descriptor
  153. prepared_length += asmcp->max_dma_buffer_size;
  154. len -= asmcp->max_dma_buffer_size;
  155. } else {
  156. // out of TX descriptors
  157. goto _exit;
  158. }
  159. }
  160. if (len) {
  161. if (desc->dw0.owner != DMA_DESCRIPTOR_BUFFER_OWNER_DMA) {
  162. end = desc; // the last descriptor used
  163. desc->dw0.suc_eof = 1; // end of the transaction
  164. desc->dw0.size = len;
  165. desc->dw0.length = len;
  166. desc->buffer = &buf[prepared_length];
  167. desc = desc->next; // move to next descriptor
  168. prepared_length += len;
  169. } else {
  170. // out of TX descriptors
  171. goto _exit;
  172. }
  173. }
  174. *start_desc = start;
  175. *end_desc = end;
  176. _exit:
  177. return prepared_length;
  178. }
  179. static bool async_memcpy_get_next_rx_descriptor(async_memcpy_t asmcp, dma_descriptor_t *eof_desc, dma_descriptor_t **next_desc)
  180. {
  181. dma_descriptor_t *next = asmcp->next_rx_desc_to_check;
  182. // additional check, to avoid potential interrupt got triggered by mistake
  183. if (next->dw0.owner == DMA_DESCRIPTOR_BUFFER_OWNER_CPU) {
  184. asmcp->next_rx_desc_to_check = asmcp->next_rx_desc_to_check->next;
  185. *next_desc = next;
  186. // return if we need to continue
  187. return eof_desc == next ? false : true;
  188. }
  189. *next_desc = NULL;
  190. return false;
  191. }
  192. esp_err_t esp_async_memcpy(async_memcpy_t asmcp, void *dst, void *src, size_t n, async_memcpy_isr_cb_t cb_isr, void *cb_args)
  193. {
  194. esp_err_t ret = ESP_OK;
  195. dma_descriptor_t *rx_start_desc = NULL;
  196. dma_descriptor_t *rx_end_desc = NULL;
  197. dma_descriptor_t *tx_start_desc = NULL;
  198. dma_descriptor_t *tx_end_desc = NULL;
  199. size_t rx_prepared_size = 0;
  200. size_t tx_prepared_size = 0;
  201. ESP_GOTO_ON_FALSE(asmcp, ESP_ERR_INVALID_ARG, err, TAG, "mcp handle can't be null");
  202. ESP_GOTO_ON_FALSE(async_memcpy_impl_is_buffer_address_valid(&asmcp->mcp_impl, src, dst), ESP_ERR_INVALID_ARG, err, TAG, "buffer address not valid: %p -> %p", src, dst);
  203. ESP_GOTO_ON_FALSE(n <= asmcp->max_dma_buffer_size * asmcp->max_stream_num, ESP_ERR_INVALID_ARG, err, TAG, "buffer size too large");
  204. if (asmcp->mcp_impl.sram_trans_align) {
  205. ESP_GOTO_ON_FALSE(((n & (asmcp->mcp_impl.sram_trans_align - 1)) == 0), ESP_ERR_INVALID_ARG, err, TAG, "copy size should align to %d bytes", asmcp->mcp_impl.sram_trans_align);
  206. }
  207. if (asmcp->mcp_impl.psram_trans_align) {
  208. ESP_GOTO_ON_FALSE(((n & (asmcp->mcp_impl.psram_trans_align - 1)) == 0), ESP_ERR_INVALID_ARG, err, TAG, "copy size should align to %d bytes", asmcp->mcp_impl.psram_trans_align);
  209. }
  210. // Prepare TX and RX descriptor
  211. portENTER_CRITICAL_SAFE(&asmcp->spinlock);
  212. rx_prepared_size = async_memcpy_prepare_receive(asmcp, dst, n, &rx_start_desc, &rx_end_desc);
  213. tx_prepared_size = async_memcpy_prepare_transmit(asmcp, src, n, &tx_start_desc, &tx_end_desc);
  214. if (rx_start_desc && tx_start_desc && (rx_prepared_size == n) && (tx_prepared_size == n)) {
  215. // register user callback to the last descriptor
  216. async_memcpy_stream_t *mcp_stream = __containerof(rx_end_desc, async_memcpy_stream_t, desc);
  217. mcp_stream->cb = cb_isr;
  218. mcp_stream->cb_args = cb_args;
  219. // restart RX firstly
  220. dma_descriptor_t *desc = rx_start_desc;
  221. while (desc != rx_end_desc) {
  222. desc->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  223. desc = desc->next;
  224. }
  225. desc->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  226. asmcp->rx_desc = desc->next;
  227. // restart TX secondly
  228. desc = tx_start_desc;
  229. while (desc != tx_end_desc) {
  230. desc->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  231. desc = desc->next;
  232. }
  233. desc->dw0.owner = DMA_DESCRIPTOR_BUFFER_OWNER_DMA;
  234. asmcp->tx_desc = desc->next;
  235. async_memcpy_impl_restart(&asmcp->mcp_impl);
  236. }
  237. portEXIT_CRITICAL_SAFE(&asmcp->spinlock);
  238. // It's unlikely that we have space for rx descriptor but no space for tx descriptor
  239. // Both tx and rx descriptor should move in the same pace
  240. ESP_GOTO_ON_FALSE(rx_prepared_size == n, ESP_FAIL, err, TAG, "out of rx descriptor");
  241. ESP_GOTO_ON_FALSE(tx_prepared_size == n, ESP_FAIL, err, TAG, "out of tx descriptor");
  242. err:
  243. return ret;
  244. }
  245. IRAM_ATTR void async_memcpy_isr_on_rx_done_event(async_memcpy_impl_t *impl)
  246. {
  247. bool to_continue = false;
  248. async_memcpy_stream_t *in_stream = NULL;
  249. dma_descriptor_t *next_desc = NULL;
  250. async_memcpy_context_t *asmcp = __containerof(impl, async_memcpy_context_t, mcp_impl);
  251. // get the RX eof descriptor address
  252. dma_descriptor_t *eof = (dma_descriptor_t *)impl->rx_eof_addr;
  253. // traversal all unchecked descriptors
  254. do {
  255. portENTER_CRITICAL_ISR(&asmcp->spinlock);
  256. // There is an assumption that the usage of rx descriptors are in the same pace as tx descriptors (this is determined by M2M DMA working mechanism)
  257. // And once the rx descriptor is recycled, the corresponding tx desc is guaranteed to be returned by DMA
  258. to_continue = async_memcpy_get_next_rx_descriptor(asmcp, eof, &next_desc);
  259. portEXIT_CRITICAL_ISR(&asmcp->spinlock);
  260. if (next_desc) {
  261. in_stream = __containerof(next_desc, async_memcpy_stream_t, desc);
  262. // invoke user registered callback if available
  263. if (in_stream->cb) {
  264. async_memcpy_event_t e = {0};
  265. if (in_stream->cb(asmcp, &e, in_stream->cb_args)) {
  266. impl->isr_need_yield = true;
  267. }
  268. in_stream->cb = NULL;
  269. in_stream->cb_args = NULL;
  270. }
  271. }
  272. } while (to_continue);
  273. }