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