Files
2026-08-24 10:03:24 +08:00

96 lines
2.0 KiB
C

// SPDX-License-Identifier: (GPL-2.0-only OR MIT)
/*
* Copyright (C) 2025 Amlogic, Inc. All rights reserved
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <asm/types.h>
#include <string.h>
#include <errno.h>
#include "dmabuf.h"
#include "dma-direction.h"
#include "dma-heap.h"
#define PAGE_ALIGN(x) (((x) + 4095) & ~4095)
#define E_DEBUG(fmt, args...) printf(fmt, ## args)
int dmabuf_dev_open(char *name)
{
int ret, fd;
char buf[256];
ret = snprintf(buf, 256, "%s/%s", DEVPATH, name);
if (ret < 0) {
E_DEBUG("snprintf failed!\n");
return ret;
}
fd = open(buf, O_RDWR);
/* if errno == 2, which means there is no heap_codecmm, using ion replace
*/
if (fd < 0) {
if (errno != 2)
E_DEBUG("open %s failed, %s!\n", buf, strerror(errno));
}
return fd;
}
static int dmabuf_heap_alloc_fdflags(int fd, size_t len, unsigned int fd_flags,
unsigned int heap_flags, int *dmabuf_fd)
{
struct dma_heap_allocation_data data = {
.len = len,
.fd = 0,
.fd_flags = fd_flags,
.heap_flags = heap_flags,
};
int ret;
if (!dmabuf_fd)
return -EINVAL;
ret = ioctl(fd, DMA_HEAP_IOCTL_ALLOC, &data);
if (ret < 0)
return ret;
*dmabuf_fd = (int)data.fd;
return ret;
}
int dmabuf_heap_alloc(int fd, size_t len, unsigned int flags,
int *dmabuf_fd)
{
return dmabuf_heap_alloc_fdflags(fd, len, O_RDWR | O_CLOEXEC, flags,
dmabuf_fd);
}
void dmabuf_sync(int fd, int start_stop)
{
struct dma_buf_sync sync = {
.flags = start_stop | DMA_BUF_SYNC_RW,
};
int ret;
ret = ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
if (ret)
E_DEBUG("sync failed %d\n", errno);
}
void dmabuf_heap_close(int heap_fd)
{
if (heap_fd >= 0)
close(heap_fd);
}
void dmabuf_dev_close(int fd)
{
if (fd > 0)
close(fd);
}