dma-buf: dma-heap: add find/alloc function

add dma_heap_find/dma_heap_buffer_alloc
for kernel-space user

1. struct dma_heap *heap = dma_heap_find("system");
2. struct dma_buf *dmabuf = dma_heap_buffer_alloc(heap, ....);
3. dma_buf_attach/dma_buf_map_attachment
4. dma_buf_unmap_attachment/dma_buf_detach
5. dma_buf_put

Change-Id: I6d3e347698785c2b751bcef980607ed308270c76
Signed-off-by: Simon Xue <xxm@rock-chips.com>
This commit is contained in:
Simon Xue
2024-12-02 11:06:56 +08:00
committed by Tao Huang
parent 5dea796a40
commit a2ded8cab4
2 changed files with 58 additions and 2 deletions

View File

@@ -49,7 +49,44 @@ static dev_t dma_heap_devt;
static struct class *dma_heap_class;
static DEFINE_XARRAY_ALLOC(dma_heap_minors);
static int dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
struct dma_heap *dma_heap_find(const char *name)
{
struct dma_heap *h;
mutex_lock(&heap_list_lock);
list_for_each_entry(h, &heap_list, list) {
if (!strcmp(h->name, name)) {
mutex_unlock(&heap_list_lock);
return h;
}
}
mutex_unlock(&heap_list_lock);
return NULL;
}
EXPORT_SYMBOL_GPL(dma_heap_find);
struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
unsigned int fd_flags,
unsigned int heap_flags)
{
if (fd_flags & ~DMA_HEAP_VALID_FD_FLAGS)
return ERR_PTR(-EINVAL);
if (heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
return ERR_PTR(-EINVAL);
/*
* Allocations from all heaps have to begin
* and end on page boundaries.
*/
len = PAGE_ALIGN(len);
if (!len)
return ERR_PTR(-EINVAL);
return heap->ops->allocate(heap, len, fd_flags, heap_flags);
}
EXPORT_SYMBOL_GPL(dma_heap_buffer_alloc);
static int dma_heap_bufferfd_alloc(struct dma_heap *heap, size_t len,
unsigned int fd_flags,
unsigned int heap_flags)
{
@@ -108,7 +145,7 @@ static long dma_heap_ioctl_allocate(struct file *file, void *data)
if (heap_allocation->heap_flags & ~DMA_HEAP_VALID_HEAP_FLAGS)
return -EINVAL;
fd = dma_heap_buffer_alloc(heap, heap_allocation->len,
fd = dma_heap_bufferfd_alloc(heap, heap_allocation->len,
heap_allocation->fd_flags,
heap_allocation->heap_flags);
if (fd < 0)

View File

@@ -69,4 +69,23 @@ const char *dma_heap_get_name(struct dma_heap *heap);
*/
struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info);
/**
* dma_heap_find - Returns the registered dma_heap with the specified name
* @name: Name of the heap to find
*
*/
struct dma_heap *dma_heap_find(const char *name);
/**
* dma_heap_buffer_alloc - Allocate dma-buf from a dma_heap
* @heap: dma_heap to allocate from
* @len: size to allocate
* @fd_flags: flags to set on returned dma-buf fd
* @heap_flags: flags to pass to the dma heap
*
* This is for internal dma-buf allocations only.
*/
struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
unsigned int fd_flags,
unsigned int heap_flags);
#endif /* _DMA_HEAPS_H */