xref: /OK3568_Linux_fs/kernel/include/linux/rk-dma-heap.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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  * Copyright (C) 2022 Rockchip Electronics Co. Ltd.
8  * Author: Simon Xue <xxm@rock-chips.com>
9  */
10 
11 #ifndef _RK_DMA_HEAPS_H_
12 #define _RK_DMA_HEAPS_H_
13 #include <linux/dma-buf.h>
14 
15 struct rk_dma_heap;
16 
17 #if defined(CONFIG_DMABUF_HEAPS_ROCKCHIP)
18 int rk_dma_heap_cma_setup(void);
19 
20 /**
21  * rk_dma_heap_set_dev - set heap dev dma param
22  * @heap: DMA-Heap to retrieve private data for
23  *
24  * Returns:
25  * Zero on success, ERR_PTR(-errno) on error
26  */
27 int rk_dma_heap_set_dev(struct device *heap_dev);
28 
29 /**
30  * rk_dma_heap_find - Returns the registered dma_heap with the specified name
31  * @name: Name of the heap to find
32  *
33  * NOTE: dma_heaps returned from this function MUST be released
34  * using rk_dma_heap_put() when the user is done.
35  */
36 struct rk_dma_heap *rk_dma_heap_find(const char *name);
37 
38 /** rk_dma_heap_buffer_free - Free dma_buf allocated by rk_dma_heap_buffer_alloc
39  * @dma_buf:	dma_buf to free
40  *
41  * This is really only a simple wrapper to dma_buf_put()
42  */
43 void rk_dma_heap_buffer_free(struct dma_buf *dmabuf);
44 
45 /**
46  * rk_dma_heap_buffer_alloc - Allocate dma-buf from a dma_heap
47  * @heap:	dma_heap to allocate from
48  * @len:	size to allocate
49  * @fd_flags:	flags to set on returned dma-buf fd
50  * @heap_flags:	flags to pass to the dma heap
51  *
52  * This is for internal dma-buf allocations only.
53  */
54 struct dma_buf *rk_dma_heap_buffer_alloc(struct rk_dma_heap *heap, size_t len,
55 					 unsigned int fd_flags,
56 					 unsigned int heap_flags,
57 					 const char *name);
58 
59 /**
60  * rk_dma_heap_bufferfd_alloc - Allocate dma-buf fd from a dma_heap
61  * @heap:	dma_heap to allocate from
62  * @len:	size to allocate
63  * @fd_flags:	flags to set on returned dma-buf fd
64  * @heap_flags:	flags to pass to the dma heap
65  */
66 int rk_dma_heap_bufferfd_alloc(struct rk_dma_heap *heap, size_t len,
67 			       unsigned int fd_flags,
68 			       unsigned int heap_flags,
69 			       const char *name);
70 
71 /**
72  * rk_dma_heap_alloc_contig_pages - Allocate contiguous pages from a dma_heap
73  * @heap:	dma_heap to allocate from
74  * @len:	size to allocate
75  * @name:	the name who allocate
76  */
77 struct page *rk_dma_heap_alloc_contig_pages(struct rk_dma_heap *heap,
78 					    size_t len, const char *name);
79 
80 /**
81  * rk_dma_heap_free_contig_pages - Free contiguous pages to a dma_heap
82  * @heap:	dma_heap to free to
83  * @pages:	pages to free to
84  * @len:	size to free
85  * @name:	the name who allocate
86  */
87 void rk_dma_heap_free_contig_pages(struct rk_dma_heap *heap, struct page *pages,
88 				   size_t len, const char *name);
89 
90 #else
rk_dma_heap_cma_setup(void)91 static inline int rk_dma_heap_cma_setup(void)
92 {
93 	return -ENODEV;
94 }
95 
rk_dma_heap_set_dev(struct device * heap_dev)96 static inline int rk_dma_heap_set_dev(struct device *heap_dev)
97 {
98 	return -ENODEV;
99 }
100 
rk_dma_heap_find(const char * name)101 static inline struct rk_dma_heap *rk_dma_heap_find(const char *name)
102 {
103 	return NULL;
104 }
105 
rk_dma_heap_buffer_free(struct dma_buf * dmabuf)106 static inline void rk_dma_heap_buffer_free(struct dma_buf *dmabuf)
107 {
108 }
109 
rk_dma_heap_buffer_alloc(struct rk_dma_heap * heap,size_t len,unsigned int fd_flags,unsigned int heap_flags,const char * name)110 static inline struct dma_buf *rk_dma_heap_buffer_alloc(struct rk_dma_heap *heap, size_t len,
111 						       unsigned int fd_flags,
112 						       unsigned int heap_flags,
113 						       const char *name)
114 {
115 	return NULL;
116 }
117 
rk_dma_heap_bufferfd_alloc(struct rk_dma_heap * heap,size_t len,unsigned int fd_flags,unsigned int heap_flags,const char * name)118 static inline int rk_dma_heap_bufferfd_alloc(struct rk_dma_heap *heap, size_t len,
119 					     unsigned int fd_flags,
120 					     unsigned int heap_flags,
121 					     const char *name)
122 {
123 	return -ENODEV;
124 }
125 
rk_dma_heap_alloc_contig_pages(struct rk_dma_heap * heap,size_t len,const char * name)126 static inline struct page *rk_dma_heap_alloc_contig_pages(struct rk_dma_heap *heap,
127 							  size_t len, const char *name)
128 {
129 	return NULL;
130 }
131 
rk_dma_heap_free_contig_pages(struct rk_dma_heap * heap,struct page * pages,size_t len,const char * name)132 static inline void rk_dma_heap_free_contig_pages(struct rk_dma_heap *heap, struct page *pages,
133 						 size_t len, const char *name)
134 {
135 }
136 #endif
137 #endif /* _DMA_HEAPS_H */
138