1#include "hex-dma.h"
 2
 3#include <stdbool.h>
 4#include <stdlib.h>
 5#include <string.h>
 6
 7#pragma clang diagnostic ignored "-Wunused-function"
 8
 9static inline uint32_t pow2_ceil(uint32_t x) {
10    if (x <= 1) {
11        return 1;
12    }
13    int p = 2;
14    x--;
15    while (x >>= 1) {
16        p <<= 1;
17    }
18    return p;
19}
20
21dma_queue * dma_queue_create(size_t capacity) {
22    dma_queue * q = (dma_queue *) memalign(32, sizeof(dma_queue));
23    if (q == NULL) {
24        FARF(ERROR, "%s: failed to allocate DMA queue\n", __FUNCTION__);
25        return NULL;
26    }
27
28    capacity = pow2_ceil(capacity);
29
30    memset(q, 0, sizeof(dma_queue));
31    q->capacity = capacity;
32    q->idx_mask = capacity - 1;
33
34    q->desc = (hexagon_udma_descriptor_type1_t *) memalign(64, capacity * sizeof(hexagon_udma_descriptor_type1_t));
35    memset(q->desc, 0, capacity * sizeof(hexagon_udma_descriptor_type1_t));
36
37    q->dptr = (dma_ptr *) memalign(4, capacity * sizeof(dma_ptr));
38    memset(q->dptr, 0, capacity * sizeof(dma_ptr));
39
40    q->tail = &q->desc[capacity - 1];
41
42    if (!q->desc && !q->dptr) {
43        FARF(ERROR, "%s: failed to allocate DMA queue items\n", __FUNCTION__);
44        return NULL;
45    }
46
47    FARF(HIGH, "dma-queue: capacity %u\n", capacity);
48
49    return q;
50}
51
52void dma_queue_delete(dma_queue * q) {
53    if (!q) {
54        return;
55    }
56    free(q->desc);
57    free(q->dptr);
58    free(q);
59}
60
61void dma_queue_flush(dma_queue * q) {
62    while (dma_queue_pop(q).dst != NULL) ;
63}