// dma-buf 分配器实现 — ioctl + mmap
// 参考: cam_stream/src/core/dma_alloc.cpp (RK3588), 增加多 heap 路径探测
#include "common/dma_buffer.h"

#include <cerrno>
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>

// ---- dma_heap 分配 ioctl (内核 UAPI 稳定接口) ----
struct dma_heap_allocation_data {
    unsigned long long len;
    unsigned int       fd;
    unsigned int       fd_flags;
    unsigned long long heap_flags;
};
#define DMA_HEAP_IOC_MAGIC   'H'
#define DMA_HEAP_IOCTL_ALLOC _IOWR(DMA_HEAP_IOC_MAGIC, 0x0, struct dma_heap_allocation_data)

// ---- dma_buf cache sync ioctl ----
struct dma_buf_sync {
    unsigned long long flags;
};
#define DMA_BUF_SYNC_READ  (1 << 0)
#define DMA_BUF_SYNC_WRITE (2 << 0)
#define DMA_BUF_SYNC_START (0 << 2)
#define DMA_BUF_SYNC_END   (1 << 2)
#define DMA_BUF_BASE       'b'
#define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)

// 按优先级探测: RV1106 板厂 CMA heap -> 标准内核 heap
static const char* kHeapCandidates[] = {
    "/dev/rk_dma_heap/rk-dma-heap-cma",   // RV1106 / Rockchip 4.19+ 固件
    "/dev/dma_heap/system-uncached",      // 标准内核 (5.x+)
    "/dev/dma_heap/system",
};

const char* dma_heap_auto() {
    for (const char* p : kHeapCandidates) {
        if (access(p, R_OK | W_OK) == 0) return p;
    }
    return nullptr;
}

bool dma_buf_alloc(const char* heap_path, size_t size, DmaBuffer* out) {
    if (!heap_path || !out || size == 0) return false;
    *out = DmaBuffer{};

    int heap_fd = open(heap_path, O_RDWR | O_CLOEXEC);
    if (heap_fd < 0) {
        fprintf(stderr, "[DMA] open %s failed: %s\n", heap_path, strerror(errno));
        return false;
    }

    struct dma_heap_allocation_data data = {};
    data.len      = size;
    data.fd_flags = O_CLOEXEC | O_RDWR;

    int ret = ioctl(heap_fd, DMA_HEAP_IOCTL_ALLOC, &data);
    close(heap_fd);
    if (ret < 0) {
        fprintf(stderr, "[DMA] alloc %zu bytes from %s failed: %s\n", size, heap_path,
                strerror(errno));
        return false;
    }

    void* va = mmap(nullptr, data.len, PROT_READ | PROT_WRITE, MAP_SHARED, data.fd, 0);
    if (va == MAP_FAILED) {
        fprintf(stderr, "[DMA] mmap failed: %s\n", strerror(errno));
        close(data.fd);
        return false;
    }

    out->fd   = data.fd;
    out->va   = va;
    out->size = data.len;
    return true;
}

bool dma_buf_alloc_auto(size_t size, DmaBuffer* out) {
    const char* heap = dma_heap_auto();
    if (!heap) {
        fprintf(stderr, "[DMA] no usable dma_heap found\n");
        return false;
    }
    return dma_buf_alloc(heap, size, out);
}

void dma_buf_free(DmaBuffer* buf) {
    if (!buf) return;
    if (buf->va && buf->va != MAP_FAILED) munmap(buf->va, buf->size);
    if (buf->fd >= 0) close(buf->fd);
    *buf = DmaBuffer{};
}

int dma_buf_sync_start(int fd) {
    struct dma_buf_sync sync = {};
    sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE;
    return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
}

int dma_buf_sync_end(int fd) {
    struct dma_buf_sync sync = {};
    sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE;
    return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
}
