Andrew F. Davis | c02a81f | 2019-12-03 17:26:37 +0000 | [diff] [blame] | 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 | |
| 15 | struct dma_heap; |
| 16 | |
| 17 | /** |
| 18 | * struct dma_heap_ops - ops to operate on a given heap |
John Stultz | 349e836 | 2020-08-14 22:42:09 +0000 | [diff] [blame] | 19 | * @allocate: allocate dmabuf and return struct dma_buf ptr |
Hridya Valsaraju | 9419dd1 | 2021-01-31 20:38:27 -0800 | [diff] [blame] | 20 | * @get_pool_size: if heap maintains memory pools, get pool size in bytes |
Andrew F. Davis | c02a81f | 2019-12-03 17:26:37 +0000 | [diff] [blame] | 21 | * |
John Stultz | 349e836 | 2020-08-14 22:42:09 +0000 | [diff] [blame] | 22 | * allocate returns dmabuf on success, ERR_PTR(-errno) on error. |
Andrew F. Davis | c02a81f | 2019-12-03 17:26:37 +0000 | [diff] [blame] | 23 | */ |
| 24 | struct dma_heap_ops { |
John Stultz | 349e836 | 2020-08-14 22:42:09 +0000 | [diff] [blame] | 25 | struct dma_buf *(*allocate)(struct dma_heap *heap, |
Andrew F. Davis | c02a81f | 2019-12-03 17:26:37 +0000 | [diff] [blame] | 26 | unsigned long len, |
| 27 | unsigned long fd_flags, |
| 28 | unsigned long heap_flags); |
Hridya Valsaraju | 9419dd1 | 2021-01-31 20:38:27 -0800 | [diff] [blame] | 29 | long (*get_pool_size)(struct dma_heap *heap); |
Andrew F. Davis | c02a81f | 2019-12-03 17:26:37 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * struct dma_heap_export_info - information needed to export a new dmabuf heap |
| 34 | * @name: used for debugging/device-node name |
| 35 | * @ops: ops struct for this heap |
| 36 | * @priv: heap exporter private data |
| 37 | * |
| 38 | * Information needed to export a new dmabuf heap. |
| 39 | */ |
| 40 | struct dma_heap_export_info { |
| 41 | const char *name; |
| 42 | const struct dma_heap_ops *ops; |
| 43 | void *priv; |
| 44 | }; |
| 45 | |
| 46 | /** |
| 47 | * dma_heap_get_drvdata() - get per-heap driver data |
| 48 | * @heap: DMA-Heap to retrieve private data for |
| 49 | * |
| 50 | * Returns: |
| 51 | * The per-heap data for the heap. |
| 52 | */ |
| 53 | void *dma_heap_get_drvdata(struct dma_heap *heap); |
| 54 | |
| 55 | /** |
John Stultz | c8bdd32 | 2020-05-07 20:08:13 +0000 | [diff] [blame] | 56 | * dma_heap_get_dev() - get device struct for the heap |
| 57 | * @heap: DMA-Heap to retrieve device struct from |
| 58 | * |
| 59 | * Returns: |
| 60 | * The device struct for the heap. |
| 61 | */ |
| 62 | struct device *dma_heap_get_dev(struct dma_heap *heap); |
| 63 | |
| 64 | /** |
John Stultz | e09764e | 2021-02-06 05:28:51 +0000 | [diff] [blame] | 65 | * dma_heap_get_name() - get heap name |
| 66 | * @heap: DMA-Heap to retrieve private data for |
| 67 | * |
| 68 | * Returns: |
| 69 | * The char* for the heap name. |
| 70 | */ |
| 71 | const char *dma_heap_get_name(struct dma_heap *heap); |
| 72 | |
| 73 | /** |
Andrew F. Davis | c02a81f | 2019-12-03 17:26:37 +0000 | [diff] [blame] | 74 | * dma_heap_add - adds a heap to dmabuf heaps |
| 75 | * @exp_info: information needed to register this heap |
| 76 | */ |
| 77 | struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info); |
| 78 | |
John Stultz | c700bdd | 2020-07-25 02:53:27 +0000 | [diff] [blame] | 79 | /** |
| 80 | * dma_heap_put - drops a reference to a dmabuf heaps, potentially freeing it |
| 81 | * @heap: heap pointer |
| 82 | */ |
| 83 | void dma_heap_put(struct dma_heap *heap); |
| 84 | |
John Stultz | 8e1ec97 | 2020-05-05 18:40:52 +0000 | [diff] [blame] | 85 | /** |
| 86 | * dma_heap_find - Returns the registered dma_heap with the specified name |
| 87 | * @name: Name of the heap to find |
| 88 | * |
| 89 | * NOTE: dma_heaps returned from this function MUST be released |
| 90 | * using dma_heap_put() when the user is done. |
| 91 | */ |
| 92 | struct dma_heap *dma_heap_find(const char *name); |
| 93 | |
| 94 | /** |
| 95 | * dma_heap_buffer_alloc - Allocate dma-buf from a dma_heap |
| 96 | * @heap: dma_heap to allocate from |
| 97 | * @len: size to allocate |
| 98 | * @fd_flags: flags to set on returned dma-buf fd |
| 99 | * @heap_flags: flags to pass to the dma heap |
| 100 | * |
| 101 | * This is for internal dma-buf allocations only. |
| 102 | */ |
| 103 | struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len, |
| 104 | unsigned int fd_flags, |
| 105 | unsigned int heap_flags); |
| 106 | |
| 107 | /** dma_heap_buffer_free - Free dma_buf allocated by dma_heap_buffer_alloc |
| 108 | * @dma_buf: dma_buf to free |
| 109 | * |
| 110 | * This is really only a simple wrapper to dma_buf_put() |
| 111 | */ |
| 112 | void dma_heap_buffer_free(struct dma_buf *); |
| 113 | |
| 114 | /** |
| 115 | * dma_heap_bufferfd_alloc - Allocate dma-buf fd from a dma_heap |
| 116 | * @heap: dma_heap to allocate from |
| 117 | * @len: size to allocate |
| 118 | * @fd_flags: flags to set on returned dma-buf fd |
| 119 | * @heap_flags: flags to pass to the dma heap |
| 120 | */ |
| 121 | int dma_heap_bufferfd_alloc(struct dma_heap *heap, size_t len, |
| 122 | unsigned int fd_flags, |
| 123 | unsigned int heap_flags); |
Andrew F. Davis | c02a81f | 2019-12-03 17:26:37 +0000 | [diff] [blame] | 124 | #endif /* _DMA_HEAPS_H */ |