1 /*
2  * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 // #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
8 
9 #include <stdlib.h>
10 #include <sys/cdefs.h>
11 #include "sdkconfig.h"
12 #include "freertos/FreeRTOS.h"
13 #include "freertos/task.h"
14 #include "soc/soc_caps.h"
15 #include "soc/periph_defs.h"
16 #include "esp_intr_alloc.h"
17 #include "esp_log.h"
18 #include "esp_check.h"
19 #include "driver/periph_ctrl.h"
20 #include "esp_private/gdma.h"
21 #include "esp_heap_caps.h"
22 #include "hal/gdma_hal.h"
23 #include "hal/gdma_ll.h"
24 #include "soc/gdma_periph.h"
25 #include "soc/soc_memory_types.h"
26 
27 static const char *TAG = "gdma";
28 
29 #if CONFIG_GDMA_ISR_IRAM_SAFE
30 #define GDMA_INTR_ALLOC_FLAGS  (ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_INTRDISABLED)
31 #define GDMA_MEM_ALLOC_CAPS    (MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)
32 #else
33 #define GDMA_INTR_ALLOC_FLAGS  ESP_INTR_FLAG_INTRDISABLED
34 #define GDMA_MEM_ALLOC_CAPS    MALLOC_CAP_DEFAULT
35 #endif  // CONFIG_GDMA_ISR_IRAM_SAFE
36 
37 #if CONFIG_GDMA_CTRL_FUNC_IN_IRAM
38 #define GDMA_CTRL_FUNC_ATTR    IRAM_ATTR
39 #else
40 #define GDMA_CTRL_FUNC_ATTR
41 #endif // CONFIG_GDMA_CTRL_FUNC_IN_IRAM
42 
43 #define GDMA_INVALID_PERIPH_TRIG  (0x3F)
44 #define SEARCH_REQUEST_RX_CHANNEL (1 << 0)
45 #define SEARCH_REQUEST_TX_CHANNEL (1 << 1)
46 
47 typedef struct gdma_platform_t gdma_platform_t;
48 typedef struct gdma_group_t gdma_group_t;
49 typedef struct gdma_pair_t gdma_pair_t;
50 typedef struct gdma_channel_t gdma_channel_t;
51 typedef struct gdma_tx_channel_t gdma_tx_channel_t;
52 typedef struct gdma_rx_channel_t gdma_rx_channel_t;
53 
54 /**
55  * GDMA driver consists of there object class, namely: Group, Pair and Channel.
56  * Channel is allocated when user calls `gdma_new_channel`, its lifecycle is maintained by user.
57  * Pair and Group are all lazy allocated, their life cycles are maintained by this driver.
58  * We use reference count to track their life cycles, i.e. the driver will free their memory only when their reference count reached to 0.
59  *
60  * We don't use an all-in-one spin lock in this driver, instead, we created different spin locks at different level.
61  * For platform, it has a spinlock, which is used to protect the group handle slots and reference count of each group.
62  * For group, it has a spinlock, which is used to protect group level stuffs, e.g. hal object, pair handle slots and reference count of each pair.
63  * For pair, it has a spinlock, which is used to protect pair level stuffs, e.g. channel handle slots, occupy code.
64  */
65 
66 struct gdma_platform_t {
67     portMUX_TYPE spinlock;                 // platform level spinlock
68     gdma_group_t *groups[SOC_GDMA_GROUPS]; // array of GDMA group instances
69     int group_ref_counts[SOC_GDMA_GROUPS]; // reference count used to protect group install/uninstall
70 };
71 
72 struct gdma_group_t {
73     int group_id;           // Group ID, index from 0
74     gdma_hal_context_t hal; // HAL instance is at group level
75     portMUX_TYPE spinlock;  // group level spinlock
76     gdma_pair_t *pairs[SOC_GDMA_PAIRS_PER_GROUP];  // handles of GDMA pairs
77     int pair_ref_counts[SOC_GDMA_PAIRS_PER_GROUP]; // reference count used to protect pair install/uninstall
78 };
79 
80 struct gdma_pair_t {
81     gdma_group_t *group;        // which group the pair belongs to
82     int pair_id;                // Pair ID, index from 0
83     gdma_tx_channel_t *tx_chan; // pointer of tx channel in the pair
84     gdma_rx_channel_t *rx_chan; // pointer of rx channel in the pair
85     int occupy_code;            // each bit indicates which channel has been occupied (an occupied channel will be skipped during channel search)
86     portMUX_TYPE spinlock;      // pair level spinlock
87 };
88 
89 struct gdma_channel_t {
90     gdma_pair_t *pair;  // which pair the channel belongs to
91     intr_handle_t intr; // per-channel interrupt handle
92     gdma_channel_direction_t direction; // channel direction
93     int periph_id; // Peripheral instance ID, indicates which peripheral is connected to this GDMA channel
94     size_t sram_alignment;  // alignment for memory in SRAM
95     size_t psram_alignment; // alignment for memory in PSRAM
96     esp_err_t (*del)(gdma_channel_t *channel); // channel deletion function, it's polymorphic, see `gdma_del_tx_channel` or `gdma_del_rx_channel`
97 };
98 
99 struct gdma_tx_channel_t {
100     gdma_channel_t base; // GDMA channel, base class
101     void *user_data;     // user registered DMA event data
102     gdma_event_callback_t on_trans_eof; // TX EOF callback
103 };
104 
105 struct gdma_rx_channel_t {
106     gdma_channel_t base; // GDMA channel, base class
107     void *user_data;     // user registered DMA event data
108     gdma_event_callback_t on_recv_eof; // RX EOF callback
109 };
110 
111 static gdma_group_t *gdma_acquire_group_handle(int group_id);
112 static void gdma_release_group_handle(gdma_group_t *group);
113 static gdma_pair_t *gdma_acquire_pair_handle(gdma_group_t *group, int pair_id);
114 static void gdma_release_pair_handle(gdma_pair_t *pair);
115 static void gdma_uninstall_group(gdma_group_t *group);
116 static void gdma_uninstall_pair(gdma_pair_t *pair);
117 static esp_err_t gdma_del_tx_channel(gdma_channel_t *dma_channel);
118 static esp_err_t gdma_del_rx_channel(gdma_channel_t *dma_channel);
119 static esp_err_t gdma_install_rx_interrupt(gdma_rx_channel_t *rx_chan);
120 static esp_err_t gdma_install_tx_interrupt(gdma_tx_channel_t *tx_chan);
121 
122 // gdma driver platform
123 static gdma_platform_t s_platform = {
124     .spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED,
125     .groups = {} // groups will be lazy installed
126 };
127 
gdma_new_channel(const gdma_channel_alloc_config_t * config,gdma_channel_handle_t * ret_chan)128 esp_err_t gdma_new_channel(const gdma_channel_alloc_config_t *config, gdma_channel_handle_t *ret_chan)
129 {
130     esp_err_t ret = ESP_OK;
131     gdma_tx_channel_t *alloc_tx_channel = NULL;
132     gdma_rx_channel_t *alloc_rx_channel = NULL;
133     int search_code = 0;
134     gdma_pair_t *pair = NULL;
135     gdma_group_t *group = NULL;
136     ESP_GOTO_ON_FALSE(config && ret_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
137 
138     if (config->flags.reserve_sibling) {
139         search_code = SEARCH_REQUEST_RX_CHANNEL | SEARCH_REQUEST_TX_CHANNEL; // search for a pair of channels
140     }
141     if (config->direction == GDMA_CHANNEL_DIRECTION_TX) {
142         search_code |= SEARCH_REQUEST_TX_CHANNEL; // search TX only
143         alloc_tx_channel = heap_caps_calloc(1, sizeof(gdma_tx_channel_t), GDMA_MEM_ALLOC_CAPS);
144         ESP_GOTO_ON_FALSE(alloc_tx_channel, ESP_ERR_NO_MEM, err, TAG, "no mem for gdma tx channel");
145     } else if (config->direction == GDMA_CHANNEL_DIRECTION_RX) {
146         search_code |= SEARCH_REQUEST_RX_CHANNEL; // search RX only
147         alloc_rx_channel = heap_caps_calloc(1, sizeof(gdma_rx_channel_t), GDMA_MEM_ALLOC_CAPS);
148         ESP_GOTO_ON_FALSE(alloc_rx_channel, ESP_ERR_NO_MEM, err, TAG, "no mem for gdma rx channel");
149     }
150 
151     if (config->sibling_chan) {
152         pair = config->sibling_chan->pair;
153         ESP_GOTO_ON_FALSE(pair, ESP_ERR_INVALID_ARG, err, TAG, "invalid sibling channel");
154         ESP_GOTO_ON_FALSE(config->sibling_chan->direction != config->direction, ESP_ERR_INVALID_ARG, err, TAG, "sibling channel should have a different direction");
155         group = pair->group;
156         portENTER_CRITICAL(&group->spinlock);
157         group->pair_ref_counts[pair->pair_id]++; // channel obtains a reference to pair
158         portEXIT_CRITICAL(&group->spinlock);
159         goto search_done; // skip the search path below if user has specify a sibling channel
160     }
161 
162     for (int i = 0; i < SOC_GDMA_GROUPS && search_code; i++) { // loop to search group
163         group = gdma_acquire_group_handle(i);
164         for (int j = 0; j < SOC_GDMA_PAIRS_PER_GROUP && search_code && group; j++) { // loop to search pair
165             pair = gdma_acquire_pair_handle(group, j);
166             if (pair) {
167                 portENTER_CRITICAL(&pair->spinlock);
168                 if (!(search_code & pair->occupy_code)) { // pair has suitable position for acquired channel(s)
169                     pair->occupy_code |= search_code;
170                     search_code = 0; // exit search loop
171                 }
172                 portEXIT_CRITICAL(&pair->spinlock);
173                 if (!search_code) {
174                     portENTER_CRITICAL(&group->spinlock);
175                     group->pair_ref_counts[j]++; // channel obtains a reference to pair
176                     portEXIT_CRITICAL(&group->spinlock);
177                 }
178             }
179             gdma_release_pair_handle(pair);
180         } // loop used to search pair
181         gdma_release_group_handle(group);
182     } // loop used to search group
183     ESP_GOTO_ON_FALSE(search_code == 0, ESP_ERR_NOT_FOUND, err, TAG, "no free gdma channel, search code=%d", search_code);
184 
185 search_done:
186     // register TX channel
187     if (alloc_tx_channel) {
188         pair->tx_chan = alloc_tx_channel;
189         alloc_tx_channel->base.pair = pair;
190         alloc_tx_channel->base.direction = GDMA_CHANNEL_DIRECTION_TX;
191         alloc_tx_channel->base.periph_id = GDMA_INVALID_PERIPH_TRIG;
192         alloc_tx_channel->base.del = gdma_del_tx_channel; // set channel deletion function
193         *ret_chan = &alloc_tx_channel->base; // return the installed channel
194     }
195 
196     // register RX channel
197     if (alloc_rx_channel) {
198         pair->rx_chan = alloc_rx_channel;
199         alloc_rx_channel->base.pair = pair;
200         alloc_rx_channel->base.direction = GDMA_CHANNEL_DIRECTION_RX;
201         alloc_rx_channel->base.periph_id = GDMA_INVALID_PERIPH_TRIG;
202         alloc_rx_channel->base.del = gdma_del_rx_channel; // set channel deletion function
203         *ret_chan = &alloc_rx_channel->base; // return the installed channel
204     }
205 
206     ESP_LOGD(TAG, "new %s channel (%d,%d) at %p", (config->direction == GDMA_CHANNEL_DIRECTION_TX) ? "tx" : "rx",
207              group->group_id, pair->pair_id, *ret_chan);
208     return ESP_OK;
209 
210 err:
211     if (alloc_tx_channel) {
212         free(alloc_tx_channel);
213     }
214     if (alloc_rx_channel) {
215         free(alloc_rx_channel);
216     }
217     return ret;
218 }
219 
gdma_del_channel(gdma_channel_handle_t dma_chan)220 esp_err_t gdma_del_channel(gdma_channel_handle_t dma_chan)
221 {
222     esp_err_t ret = ESP_OK;
223     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
224 
225     ret = dma_chan->del(dma_chan); // call `gdma_del_tx_channel` or `gdma_del_rx_channel`
226 
227 err:
228     return ret;
229 }
230 
gdma_get_channel_id(gdma_channel_handle_t dma_chan,int * channel_id)231 esp_err_t gdma_get_channel_id(gdma_channel_handle_t dma_chan, int *channel_id)
232 {
233     esp_err_t ret = ESP_OK;
234     gdma_pair_t *pair = NULL;
235     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
236     pair = dma_chan->pair;
237     *channel_id = pair->pair_id;
238 err:
239     return ret;
240 }
241 
gdma_connect(gdma_channel_handle_t dma_chan,gdma_trigger_t trig_periph)242 esp_err_t gdma_connect(gdma_channel_handle_t dma_chan, gdma_trigger_t trig_periph)
243 {
244     esp_err_t ret = ESP_OK;
245     gdma_pair_t *pair = NULL;
246     gdma_group_t *group = NULL;
247     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
248     ESP_GOTO_ON_FALSE(dma_chan->periph_id == GDMA_INVALID_PERIPH_TRIG, ESP_ERR_INVALID_STATE, err, TAG, "channel is using by peripheral: %d", dma_chan->periph_id);
249     pair = dma_chan->pair;
250     group = pair->group;
251 
252     dma_chan->periph_id = trig_periph.instance_id;
253     // enable/disable m2m mode
254     gdma_ll_enable_m2m_mode(group->hal.dev, pair->pair_id, trig_periph.periph == GDMA_TRIG_PERIPH_M2M);
255 
256     if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
257         gdma_ll_tx_reset_channel(group->hal.dev, pair->pair_id); // reset channel
258         if (trig_periph.periph != GDMA_TRIG_PERIPH_M2M) {
259             gdma_ll_tx_connect_to_periph(group->hal.dev, pair->pair_id, trig_periph.instance_id);
260         }
261     } else {
262         gdma_ll_rx_reset_channel(group->hal.dev, pair->pair_id); // reset channel
263         if (trig_periph.periph != GDMA_TRIG_PERIPH_M2M) {
264             gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id, trig_periph.instance_id);
265         }
266     }
267 
268 err:
269     return ret;
270 }
271 
gdma_disconnect(gdma_channel_handle_t dma_chan)272 esp_err_t gdma_disconnect(gdma_channel_handle_t dma_chan)
273 {
274     esp_err_t ret = ESP_OK;
275     gdma_pair_t *pair = NULL;
276     gdma_group_t *group = NULL;
277     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
278     ESP_GOTO_ON_FALSE(dma_chan->periph_id != GDMA_INVALID_PERIPH_TRIG, ESP_ERR_INVALID_STATE, err, TAG, "no peripheral is connected to the channel");
279     pair = dma_chan->pair;
280     group = pair->group;
281 
282     dma_chan->periph_id = GDMA_INVALID_PERIPH_TRIG;
283     if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
284         gdma_ll_tx_connect_to_periph(group->hal.dev, pair->pair_id, GDMA_INVALID_PERIPH_TRIG);
285     } else {
286         gdma_ll_rx_connect_to_periph(group->hal.dev, pair->pair_id, GDMA_INVALID_PERIPH_TRIG);
287     }
288 
289 err:
290     return ret;
291 }
292 
gdma_set_transfer_ability(gdma_channel_handle_t dma_chan,const gdma_transfer_ability_t * ability)293 esp_err_t gdma_set_transfer_ability(gdma_channel_handle_t dma_chan, const gdma_transfer_ability_t *ability)
294 {
295     esp_err_t ret = ESP_OK;
296     gdma_pair_t *pair = NULL;
297     gdma_group_t *group = NULL;
298     bool en_burst = true;
299     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
300     pair = dma_chan->pair;
301     group = pair->group;
302     size_t sram_alignment = ability->sram_trans_align;
303     size_t psram_alignment = ability->psram_trans_align;
304     // alignment should be 2^n
305     ESP_GOTO_ON_FALSE((sram_alignment & (sram_alignment - 1)) == 0, ESP_ERR_INVALID_ARG, err, TAG, "invalid sram alignment: %zu", sram_alignment);
306 
307 #if SOC_GDMA_SUPPORT_PSRAM
308     int block_size_index = 0;
309     switch (psram_alignment) {
310     case 64: // 64 Bytes alignment
311         block_size_index = GDMA_LL_EXT_MEM_BK_SIZE_64B;
312         break;
313     case 32: // 32 Bytes alignment
314         block_size_index = GDMA_LL_EXT_MEM_BK_SIZE_32B;
315         break;
316     case 16: // 16 Bytes alignment
317         block_size_index = GDMA_LL_EXT_MEM_BK_SIZE_16B;
318         break;
319     case 0: // no alignment is requirement
320         block_size_index = GDMA_LL_EXT_MEM_BK_SIZE_16B;
321         psram_alignment = SOC_GDMA_PSRAM_MIN_ALIGN; // fall back to minimal alignment
322         break;
323     default:
324         ESP_GOTO_ON_FALSE(false, ESP_ERR_INVALID_ARG, err, TAG, "invalid psram alignment: %zu", psram_alignment);
325         break;
326     }
327 #endif // #if SOC_GDMA_SUPPORT_PSRAM
328 
329     if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
330         // TX channel can always enable burst mode, no matter data alignment
331         gdma_ll_tx_enable_data_burst(group->hal.dev, pair->pair_id, true);
332         gdma_ll_tx_enable_descriptor_burst(group->hal.dev, pair->pair_id, true);
333 #if SOC_GDMA_SUPPORT_PSRAM
334         gdma_ll_tx_set_block_size_psram(group->hal.dev, pair->pair_id, block_size_index);
335 #endif // #if SOC_GDMA_SUPPORT_PSRAM
336     } else {
337         // RX channel burst mode depends on specific data alignment
338         en_burst = sram_alignment >= 4;
339         gdma_ll_rx_enable_data_burst(group->hal.dev, pair->pair_id, en_burst);
340         gdma_ll_rx_enable_descriptor_burst(group->hal.dev, pair->pair_id, en_burst);
341 #if SOC_GDMA_SUPPORT_PSRAM
342         gdma_ll_rx_set_block_size_psram(group->hal.dev, pair->pair_id, block_size_index);
343 #endif // #if SOC_GDMA_SUPPORT_PSRAM
344     }
345 
346     dma_chan->sram_alignment = sram_alignment;
347     dma_chan->psram_alignment = psram_alignment;
348     ESP_LOGD(TAG, "%s channel (%d,%d), (%zu:%zu) bytes aligned, burst %s", dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX ? "tx" : "rx",
349              group->group_id, pair->pair_id, sram_alignment, psram_alignment, en_burst ? "enabled" : "disabled");
350 err:
351     return ret;
352 }
353 
gdma_apply_strategy(gdma_channel_handle_t dma_chan,const gdma_strategy_config_t * config)354 esp_err_t gdma_apply_strategy(gdma_channel_handle_t dma_chan, const gdma_strategy_config_t *config)
355 {
356     esp_err_t ret = ESP_OK;
357     gdma_pair_t *pair = NULL;
358     gdma_group_t *group = NULL;
359     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
360     pair = dma_chan->pair;
361     group = pair->group;
362 
363     if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
364         gdma_ll_tx_enable_owner_check(group->hal.dev, pair->pair_id, config->owner_check);
365         gdma_ll_tx_enable_auto_write_back(group->hal.dev, pair->pair_id, config->auto_update_desc);
366     } else {
367         gdma_ll_rx_enable_owner_check(group->hal.dev, pair->pair_id, config->owner_check);
368     }
369 
370 err:
371     return ret;
372 }
373 
gdma_register_tx_event_callbacks(gdma_channel_handle_t dma_chan,gdma_tx_event_callbacks_t * cbs,void * user_data)374 esp_err_t gdma_register_tx_event_callbacks(gdma_channel_handle_t dma_chan, gdma_tx_event_callbacks_t *cbs, void *user_data)
375 {
376     esp_err_t ret = ESP_OK;
377     gdma_pair_t *pair = NULL;
378     gdma_group_t *group = NULL;
379     ESP_GOTO_ON_FALSE(dma_chan && dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
380     pair = dma_chan->pair;
381     group = pair->group;
382     gdma_tx_channel_t *tx_chan = __containerof(dma_chan, gdma_tx_channel_t, base);
383 
384 #if CONFIG_GDMA_ISR_IRAM_SAFE
385     if (cbs->on_trans_eof) {
386         ESP_GOTO_ON_FALSE(esp_ptr_in_iram(cbs->on_trans_eof), ESP_ERR_INVALID_ARG, err, TAG, "on_trans_eof not in IRAM");
387     }
388     if (user_data) {
389         ESP_GOTO_ON_FALSE(esp_ptr_in_dram(user_data) ||
390                           esp_ptr_in_diram_dram(user_data) ||
391                           esp_ptr_in_rtc_dram_fast(user_data), ESP_ERR_INVALID_ARG, err, TAG, "user context not in DRAM");
392     }
393 #endif // CONFIG_GDMA_ISR_IRAM_SAFE
394 
395     // lazy install interrupt service
396     ESP_GOTO_ON_ERROR(gdma_install_tx_interrupt(tx_chan), err, TAG, "install interrupt service failed");
397 
398     // enable/disable GDMA interrupt events for TX channel
399     portENTER_CRITICAL(&pair->spinlock);
400     gdma_ll_tx_enable_interrupt(group->hal.dev, pair->pair_id, GDMA_LL_EVENT_TX_EOF, cbs->on_trans_eof != NULL);
401     portEXIT_CRITICAL(&pair->spinlock);
402 
403     tx_chan->on_trans_eof = cbs->on_trans_eof;
404     tx_chan->user_data = user_data;
405 
406     ESP_GOTO_ON_ERROR(esp_intr_enable(dma_chan->intr), err, TAG, "enable interrupt failed");
407 
408 err:
409     return ret;
410 }
411 
gdma_register_rx_event_callbacks(gdma_channel_handle_t dma_chan,gdma_rx_event_callbacks_t * cbs,void * user_data)412 esp_err_t gdma_register_rx_event_callbacks(gdma_channel_handle_t dma_chan, gdma_rx_event_callbacks_t *cbs, void *user_data)
413 {
414     esp_err_t ret = ESP_OK;
415     gdma_pair_t *pair = NULL;
416     gdma_group_t *group = NULL;
417     ESP_GOTO_ON_FALSE(dma_chan && dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
418     pair = dma_chan->pair;
419     group = pair->group;
420     gdma_rx_channel_t *rx_chan = __containerof(dma_chan, gdma_rx_channel_t, base);
421 
422 #if CONFIG_GDMA_ISR_IRAM_SAFE
423     if (cbs->on_recv_eof) {
424         ESP_GOTO_ON_FALSE(esp_ptr_in_iram(cbs->on_recv_eof), ESP_ERR_INVALID_ARG, err, TAG, "on_recv_eof not in IRAM");
425     }
426     if (user_data) {
427         ESP_GOTO_ON_FALSE(esp_ptr_in_dram(user_data) ||
428                           esp_ptr_in_diram_dram(user_data) ||
429                           esp_ptr_in_rtc_dram_fast(user_data), ESP_ERR_INVALID_ARG, err, TAG, "user context not in DRAM");
430     }
431 #endif // CONFIG_GDMA_ISR_IRAM_SAFE
432 
433     // lazy install interrupt service
434     ESP_GOTO_ON_ERROR(gdma_install_rx_interrupt(rx_chan), err, TAG, "install interrupt service failed");
435 
436     // enable/disable GDMA interrupt events for RX channel
437     portENTER_CRITICAL(&pair->spinlock);
438     gdma_ll_rx_enable_interrupt(group->hal.dev, pair->pair_id, GDMA_LL_EVENT_RX_SUC_EOF, cbs->on_recv_eof != NULL);
439     portEXIT_CRITICAL(&pair->spinlock);
440 
441     rx_chan->on_recv_eof = cbs->on_recv_eof;
442     rx_chan->user_data = user_data;
443 
444     ESP_GOTO_ON_ERROR(esp_intr_enable(dma_chan->intr), err, TAG, "enable interrupt failed");
445 
446 err:
447     return ret;
448 }
449 
gdma_start(gdma_channel_handle_t dma_chan,intptr_t desc_base_addr)450 GDMA_CTRL_FUNC_ATTR esp_err_t gdma_start(gdma_channel_handle_t dma_chan, intptr_t desc_base_addr)
451 {
452     esp_err_t ret = ESP_OK;
453     gdma_pair_t *pair = NULL;
454     gdma_group_t *group = NULL;
455     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
456     pair = dma_chan->pair;
457     group = pair->group;
458 
459     if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX) {
460         gdma_ll_rx_set_desc_addr(group->hal.dev, pair->pair_id, desc_base_addr);
461         gdma_ll_rx_start(group->hal.dev, pair->pair_id);
462     } else {
463         gdma_ll_tx_set_desc_addr(group->hal.dev, pair->pair_id, desc_base_addr);
464         gdma_ll_tx_start(group->hal.dev, pair->pair_id);
465     }
466 
467 err:
468     return ret;
469 }
470 
gdma_stop(gdma_channel_handle_t dma_chan)471 GDMA_CTRL_FUNC_ATTR esp_err_t gdma_stop(gdma_channel_handle_t dma_chan)
472 {
473     esp_err_t ret = ESP_OK;
474     gdma_pair_t *pair = NULL;
475     gdma_group_t *group = NULL;
476     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
477     pair = dma_chan->pair;
478     group = pair->group;
479 
480     if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX) {
481         gdma_ll_rx_stop(group->hal.dev, pair->pair_id);
482     } else {
483         gdma_ll_tx_stop(group->hal.dev, pair->pair_id);
484     }
485 
486 err:
487     return ret;
488 }
489 
gdma_append(gdma_channel_handle_t dma_chan)490 GDMA_CTRL_FUNC_ATTR esp_err_t gdma_append(gdma_channel_handle_t dma_chan)
491 {
492     esp_err_t ret = ESP_OK;
493     gdma_pair_t *pair = NULL;
494     gdma_group_t *group = NULL;
495     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
496     pair = dma_chan->pair;
497     group = pair->group;
498 
499     if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX) {
500         gdma_ll_rx_restart(group->hal.dev, pair->pair_id);
501     } else {
502         gdma_ll_tx_restart(group->hal.dev, pair->pair_id);
503     }
504 
505 err:
506     return ret;
507 }
508 
gdma_reset(gdma_channel_handle_t dma_chan)509 GDMA_CTRL_FUNC_ATTR esp_err_t gdma_reset(gdma_channel_handle_t dma_chan)
510 {
511     esp_err_t ret = ESP_OK;
512     gdma_pair_t *pair = NULL;
513     gdma_group_t *group = NULL;
514     ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
515     pair = dma_chan->pair;
516     group = pair->group;
517 
518     if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX) {
519         gdma_ll_rx_reset_channel(group->hal.dev, pair->pair_id);
520     } else {
521         gdma_ll_tx_reset_channel(group->hal.dev, pair->pair_id);
522     }
523 
524 err:
525     return ret;
526 }
527 
gdma_uninstall_group(gdma_group_t * group)528 static void gdma_uninstall_group(gdma_group_t *group)
529 {
530     int group_id = group->group_id;
531     bool do_deinitialize = false;
532 
533     portENTER_CRITICAL(&s_platform.spinlock);
534     s_platform.group_ref_counts[group_id]--;
535     if (s_platform.group_ref_counts[group_id] == 0) {
536         assert(s_platform.groups[group_id]);
537         do_deinitialize = true;
538         s_platform.groups[group_id] = NULL; // deregister from platfrom
539         gdma_ll_enable_clock(group->hal.dev, false);
540         periph_module_disable(gdma_periph_signals.groups[group_id].module);
541     }
542     portEXIT_CRITICAL(&s_platform.spinlock);
543 
544     if (do_deinitialize) {
545         free(group);
546         ESP_LOGD(TAG, "del group %d", group_id);
547     }
548 }
549 
gdma_acquire_group_handle(int group_id)550 static gdma_group_t *gdma_acquire_group_handle(int group_id)
551 {
552     bool new_group = false;
553     gdma_group_t *group = NULL;
554     gdma_group_t *pre_alloc_group = heap_caps_calloc(1, sizeof(gdma_group_t), GDMA_MEM_ALLOC_CAPS);
555     if (!pre_alloc_group) {
556         goto out;
557     }
558     portENTER_CRITICAL(&s_platform.spinlock);
559     if (!s_platform.groups[group_id]) {
560         new_group = true;
561         group = pre_alloc_group;
562         s_platform.groups[group_id] = group; // register to platform
563         group->group_id = group_id;
564         group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
565         periph_module_enable(gdma_periph_signals.groups[group_id].module); // enable APB to access GDMA registers
566         gdma_hal_init(&group->hal, group_id);       // initialize HAL context
567         gdma_ll_enable_clock(group->hal.dev, true); // enable gdma clock
568     } else {
569         group = s_platform.groups[group_id];
570     }
571     // someone acquired the group handle means we have a new object that refer to this group
572     s_platform.group_ref_counts[group_id]++;
573     portEXIT_CRITICAL(&s_platform.spinlock);
574 
575     if (new_group) {
576         ESP_LOGD(TAG, "new group (%d) at %p", group->group_id, group);
577     } else {
578         free(pre_alloc_group);
579     }
580 out:
581     return group;
582 }
583 
gdma_release_group_handle(gdma_group_t * group)584 static void gdma_release_group_handle(gdma_group_t *group)
585 {
586     if (group) {
587         gdma_uninstall_group(group);
588     }
589 }
590 
gdma_uninstall_pair(gdma_pair_t * pair)591 static void gdma_uninstall_pair(gdma_pair_t *pair)
592 {
593     gdma_group_t *group = pair->group;
594     int pair_id = pair->pair_id;
595     bool do_deinitialize = false;
596 
597     portENTER_CRITICAL(&group->spinlock);
598     group->pair_ref_counts[pair_id]--;
599     if (group->pair_ref_counts[pair_id] == 0) {
600         assert(group->pairs[pair_id]);
601         do_deinitialize = true;
602         group->pairs[pair_id] = NULL; // deregister from pair
603     }
604     portEXIT_CRITICAL(&group->spinlock);
605 
606     if (do_deinitialize) {
607         free(pair);
608         ESP_LOGD(TAG, "del pair (%d,%d)", group->group_id, pair_id);
609 
610         gdma_uninstall_group(group);
611     }
612 }
613 
gdma_acquire_pair_handle(gdma_group_t * group,int pair_id)614 static gdma_pair_t *gdma_acquire_pair_handle(gdma_group_t *group, int pair_id)
615 {
616     bool new_pair = false;
617     gdma_pair_t *pair = NULL;
618     gdma_pair_t *pre_alloc_pair = heap_caps_calloc(1, sizeof(gdma_pair_t), GDMA_MEM_ALLOC_CAPS);
619     if (!pre_alloc_pair) {
620         goto out;
621     }
622     portENTER_CRITICAL(&group->spinlock);
623     if (!group->pairs[pair_id]) {
624         new_pair = true;
625         pair = pre_alloc_pair;
626         group->pairs[pair_id] = pair; // register to group
627         pair->group = group;
628         pair->pair_id = pair_id;
629         pair->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
630     } else {
631         pair = group->pairs[pair_id];
632     }
633     // someone acquired the pair handle means we have a new object that refer to this pair
634     group->pair_ref_counts[pair_id]++;
635     portEXIT_CRITICAL(&group->spinlock);
636 
637     if (new_pair) {
638         portENTER_CRITICAL(&s_platform.spinlock);
639         s_platform.group_ref_counts[group->group_id]++; // pair obtains a reference to group
640         portEXIT_CRITICAL(&s_platform.spinlock);
641         ESP_LOGD(TAG, "new pair (%d,%d) at %p", group->group_id, pair->pair_id, pair);
642     } else {
643         free(pre_alloc_pair);
644     }
645 out:
646     return pair;
647 }
648 
gdma_release_pair_handle(gdma_pair_t * pair)649 static void gdma_release_pair_handle(gdma_pair_t *pair)
650 {
651     if (pair) {
652         gdma_uninstall_pair(pair);
653     }
654 }
655 
gdma_del_tx_channel(gdma_channel_t * dma_channel)656 static esp_err_t gdma_del_tx_channel(gdma_channel_t *dma_channel)
657 {
658     gdma_pair_t *pair = dma_channel->pair;
659     gdma_group_t *group = pair->group;
660     gdma_tx_channel_t *tx_chan = __containerof(dma_channel, gdma_tx_channel_t, base);
661     portENTER_CRITICAL(&pair->spinlock);
662     pair->tx_chan = NULL;
663     pair->occupy_code &= ~SEARCH_REQUEST_TX_CHANNEL;
664     portEXIT_CRITICAL(&pair->spinlock);
665 
666     if (dma_channel->intr) {
667         esp_intr_free(dma_channel->intr);
668         portENTER_CRITICAL(&pair->spinlock);
669         gdma_ll_tx_enable_interrupt(group->hal.dev, pair->pair_id, UINT32_MAX, false); // disable all interupt events
670         gdma_ll_tx_clear_interrupt_status(group->hal.dev, pair->pair_id, UINT32_MAX);  // clear all pending events
671         portEXIT_CRITICAL(&pair->spinlock);
672         ESP_LOGD(TAG, "uninstall interrupt service for tx channel (%d,%d)", group->group_id, pair->pair_id);
673     }
674 
675     ESP_LOGD(TAG, "del tx channel (%d,%d)", group->group_id, pair->pair_id);
676     free(tx_chan);
677     gdma_uninstall_pair(pair);
678     return ESP_OK;
679 }
680 
gdma_del_rx_channel(gdma_channel_t * dma_channel)681 static esp_err_t gdma_del_rx_channel(gdma_channel_t *dma_channel)
682 {
683     gdma_pair_t *pair = dma_channel->pair;
684     gdma_group_t *group = pair->group;
685     gdma_rx_channel_t *rx_chan = __containerof(dma_channel, gdma_rx_channel_t, base);
686     portENTER_CRITICAL(&pair->spinlock);
687     pair->rx_chan = NULL;
688     pair->occupy_code &= ~SEARCH_REQUEST_RX_CHANNEL;
689     portEXIT_CRITICAL(&pair->spinlock);
690 
691     if (dma_channel->intr) {
692         esp_intr_free(dma_channel->intr);
693         portENTER_CRITICAL(&pair->spinlock);
694         gdma_ll_rx_enable_interrupt(group->hal.dev, pair->pair_id, UINT32_MAX, false); // disable all interupt events
695         gdma_ll_rx_clear_interrupt_status(group->hal.dev, pair->pair_id, UINT32_MAX);  // clear all pending events
696         portEXIT_CRITICAL(&pair->spinlock);
697         ESP_LOGD(TAG, "uninstall interrupt service for rx channel (%d,%d)", group->group_id, pair->pair_id);
698     }
699 
700     ESP_LOGD(TAG, "del rx channel (%d,%d)", group->group_id, pair->pair_id);
701     free(rx_chan);
702     gdma_uninstall_pair(pair);
703     return ESP_OK;
704 }
705 
gdma_default_rx_isr(void * args)706 static void IRAM_ATTR gdma_default_rx_isr(void *args)
707 {
708     gdma_rx_channel_t *rx_chan = (gdma_rx_channel_t *)args;
709     gdma_pair_t *pair = rx_chan->base.pair;
710     gdma_group_t *group = pair->group;
711     bool need_yield = false;
712     // clear pending interrupt event
713     uint32_t intr_status = gdma_ll_rx_get_interrupt_status(group->hal.dev, pair->pair_id);
714     gdma_ll_rx_clear_interrupt_status(group->hal.dev, pair->pair_id, intr_status);
715 
716     if (intr_status & GDMA_LL_EVENT_RX_SUC_EOF) {
717         if (rx_chan && rx_chan->on_recv_eof) {
718             uint32_t eof_addr = gdma_ll_rx_get_success_eof_desc_addr(group->hal.dev, pair->pair_id);
719             gdma_event_data_t edata = {
720                 .rx_eof_desc_addr = eof_addr
721             };
722             if (rx_chan->on_recv_eof(&rx_chan->base, &edata, rx_chan->user_data)) {
723                 need_yield = true;
724             }
725         }
726     }
727 
728     if (need_yield) {
729         portYIELD_FROM_ISR();
730     }
731 }
732 
gdma_default_tx_isr(void * args)733 static void IRAM_ATTR gdma_default_tx_isr(void *args)
734 {
735     gdma_tx_channel_t *tx_chan = (gdma_tx_channel_t *)args;
736     gdma_pair_t *pair = tx_chan->base.pair;
737     gdma_group_t *group = pair->group;
738     bool need_yield = false;
739     // clear pending interrupt event
740     uint32_t intr_status = gdma_ll_tx_get_interrupt_status(group->hal.dev, pair->pair_id);
741     gdma_ll_tx_clear_interrupt_status(group->hal.dev, pair->pair_id, intr_status);
742 
743     if (intr_status & GDMA_LL_EVENT_TX_EOF) {
744         if (tx_chan && tx_chan->on_trans_eof) {
745             uint32_t eof_addr = gdma_ll_tx_get_eof_desc_addr(group->hal.dev, pair->pair_id);
746             gdma_event_data_t edata = {
747                 .tx_eof_desc_addr = eof_addr
748             };
749             if (tx_chan->on_trans_eof(&tx_chan->base, &edata, tx_chan->user_data)) {
750                 need_yield = true;
751             }
752         }
753     }
754 
755     if (need_yield) {
756         portYIELD_FROM_ISR();
757     }
758 }
759 
gdma_install_rx_interrupt(gdma_rx_channel_t * rx_chan)760 static esp_err_t gdma_install_rx_interrupt(gdma_rx_channel_t *rx_chan)
761 {
762     esp_err_t ret = ESP_OK;
763     gdma_pair_t *pair = rx_chan->base.pair;
764     gdma_group_t *group = pair->group;
765     // pre-alloc a interrupt handle, with handler disabled
766     int isr_flags = GDMA_INTR_ALLOC_FLAGS;
767 #if SOC_GDMA_TX_RX_SHARE_INTERRUPT
768     isr_flags |= ESP_INTR_FLAG_SHARED;
769 #endif
770     intr_handle_t intr = NULL;
771     ret = esp_intr_alloc_intrstatus(gdma_periph_signals.groups[group->group_id].pairs[pair->pair_id].rx_irq_id, isr_flags,
772                                     (uint32_t)gdma_ll_rx_get_interrupt_status_reg(group->hal.dev, pair->pair_id), GDMA_LL_RX_EVENT_MASK,
773                                     gdma_default_rx_isr, rx_chan, &intr);
774     ESP_GOTO_ON_ERROR(ret, err, TAG, "alloc interrupt failed");
775     rx_chan->base.intr = intr;
776 
777     portENTER_CRITICAL(&pair->spinlock);
778     gdma_ll_rx_enable_interrupt(group->hal.dev, pair->pair_id, UINT32_MAX, false); // disable all interupt events
779     gdma_ll_rx_clear_interrupt_status(group->hal.dev, pair->pair_id, UINT32_MAX);  // clear all pending events
780     portEXIT_CRITICAL(&pair->spinlock);
781     ESP_LOGD(TAG, "install interrupt service for rx channel (%d,%d)", group->group_id, pair->pair_id);
782 
783 err:
784     return ret;
785 }
786 
gdma_install_tx_interrupt(gdma_tx_channel_t * tx_chan)787 static esp_err_t gdma_install_tx_interrupt(gdma_tx_channel_t *tx_chan)
788 {
789     esp_err_t ret = ESP_OK;
790     gdma_pair_t *pair = tx_chan->base.pair;
791     gdma_group_t *group = pair->group;
792     // pre-alloc a interrupt handle, with handler disabled
793     int isr_flags = GDMA_INTR_ALLOC_FLAGS;
794 #if SOC_GDMA_TX_RX_SHARE_INTERRUPT
795     isr_flags |= ESP_INTR_FLAG_SHARED;
796 #endif
797     intr_handle_t intr = NULL;
798     ret = esp_intr_alloc_intrstatus(gdma_periph_signals.groups[group->group_id].pairs[pair->pair_id].tx_irq_id, isr_flags,
799                                     (uint32_t)gdma_ll_tx_get_interrupt_status_reg(group->hal.dev, pair->pair_id), GDMA_LL_TX_EVENT_MASK,
800                                     gdma_default_tx_isr, tx_chan, &intr);
801     ESP_GOTO_ON_ERROR(ret, err, TAG, "alloc interrupt failed");
802     tx_chan->base.intr = intr;
803 
804     portENTER_CRITICAL(&pair->spinlock);
805     gdma_ll_tx_enable_interrupt(group->hal.dev, pair->pair_id, UINT32_MAX, false); // disable all interupt events
806     gdma_ll_tx_clear_interrupt_status(group->hal.dev, pair->pair_id, UINT32_MAX);  // clear all pending events
807     portEXIT_CRITICAL(&pair->spinlock);
808     ESP_LOGD(TAG, "install interrupt service for tx channel (%d,%d)", group->group_id, pair->pair_id);
809 
810 err:
811     return ret;
812 }
813