1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * DMABUF Heaps Allocation Infrastructure 4 * 5 * Copyright (C) 2011 Google, Inc. 6 * Copyright (C) 2019 Linaro Ltd. 7 */ 8 9 #ifndef _DMA_HEAPS_H 10 #define _DMA_HEAPS_H 11 12 #include <linux/cdev.h> 13 #include <linux/types.h> 14 #include <uapi/linux/dma-heap.h> 15 16 struct dma_heap; 17 18 /** 19 * struct dma_heap_ops - ops to operate on a given heap 20 * @allocate: allocate dmabuf and return struct dma_buf ptr 21 * @get_pool_size: if heap maintains memory pools, get pool size in bytes 22 * 23 * allocate returns dmabuf on success, ERR_PTR(-errno) on error. 24 */ 25 struct dma_heap_ops { 26 struct dma_buf *(*allocate)(struct dma_heap *heap, 27 unsigned long len, 28 unsigned long fd_flags, 29 unsigned long heap_flags); 30 long (*get_pool_size)(struct dma_heap *heap); 31 #if IS_ENABLED(CONFIG_NO_GKI) 32 int (*get_phys)(struct dma_heap *heap, struct dma_heap_phys_data *phys); 33 #endif 34 }; 35 36 /** 37 * struct dma_heap_export_info - information needed to export a new dmabuf heap 38 * @name: used for debugging/device-node name 39 * @ops: ops struct for this heap 40 * @priv: heap exporter private data 41 * 42 * Information needed to export a new dmabuf heap. 43 */ 44 struct dma_heap_export_info { 45 const char *name; 46 const struct dma_heap_ops *ops; 47 void *priv; 48 }; 49 50 /** 51 * dma_heap_get_drvdata() - get per-heap driver data 52 * @heap: DMA-Heap to retrieve private data for 53 * 54 * Returns: 55 * The per-heap data for the heap. 56 */ 57 void *dma_heap_get_drvdata(struct dma_heap *heap); 58 59 /** 60 * dma_heap_get_dev() - get device struct for the heap 61 * @heap: DMA-Heap to retrieve device struct from 62 * 63 * Returns: 64 * The device struct for the heap. 65 */ 66 struct device *dma_heap_get_dev(struct dma_heap *heap); 67 68 /** 69 * dma_heap_get_name() - get heap name 70 * @heap: DMA-Heap to retrieve private data for 71 * 72 * Returns: 73 * The char* for the heap name. 74 */ 75 const char *dma_heap_get_name(struct dma_heap *heap); 76 77 /** 78 * dma_heap_add - adds a heap to dmabuf heaps 79 * @exp_info: information needed to register this heap 80 */ 81 struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info); 82 83 /** 84 * dma_heap_put - drops a reference to a dmabuf heaps, potentially freeing it 85 * @heap: heap pointer 86 */ 87 void dma_heap_put(struct dma_heap *heap); 88 89 /** 90 * dma_heap_find - Returns the registered dma_heap with the specified name 91 * @name: Name of the heap to find 92 * 93 * NOTE: dma_heaps returned from this function MUST be released 94 * using dma_heap_put() when the user is done. 95 */ 96 struct dma_heap *dma_heap_find(const char *name); 97 98 /** 99 * dma_heap_buffer_alloc - Allocate dma-buf from a dma_heap 100 * @heap: dma_heap to allocate from 101 * @len: size to allocate 102 * @fd_flags: flags to set on returned dma-buf fd 103 * @heap_flags: flags to pass to the dma heap 104 * 105 * This is for internal dma-buf allocations only. 106 */ 107 struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, 108 unsigned int fd_flags, 109 unsigned int heap_flags); 110 111 /** dma_heap_buffer_free - Free dma_buf allocated by dma_heap_buffer_alloc 112 * @dma_buf: dma_buf to free 113 * 114 * This is really only a simple wrapper to dma_buf_put() 115 */ 116 void dma_heap_buffer_free(struct dma_buf *); 117 118 /** 119 * dma_heap_bufferfd_alloc - Allocate dma-buf fd from a dma_heap 120 * @heap: dma_heap to allocate from 121 * @len: size to allocate 122 * @fd_flags: flags to set on returned dma-buf fd 123 * @heap_flags: flags to pass to the dma heap 124 */ 125 int dma_heap_bufferfd_alloc(struct dma_heap *heap, size_t len, 126 unsigned int fd_flags, 127 unsigned int heap_flags); 128 #endif /* _DMA_HEAPS_H */ 129