1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __DRM_GEM_DMA_HELPER_H__
3 #define __DRM_GEM_DMA_HELPER_H__
4 
5 #include <drm/drm_file.h>
6 #include <drm/drm_ioctl.h>
7 #include <drm/drm_gem.h>
8 
9 struct drm_mode_create_dumb;
10 
11 /**
12  * struct drm_gem_dma_object - GEM object backed by DMA memory allocations
13  * @base: base GEM object
14  * @dma_addr: DMA address of the backing memory
15  * @sgt: scatter/gather table for imported PRIME buffers. The table can have
16  *       more than one entry but they are guaranteed to have contiguous
17  *       DMA addresses.
18  * @vaddr: kernel virtual address of the backing memory
19  * @map_noncoherent: if true, the GEM object is backed by non-coherent memory
20  */
21 struct drm_gem_dma_object {
22 	struct drm_gem_object base;
23 	dma_addr_t dma_addr;
24 	struct sg_table *sgt;
25 
26 	/* For objects with DMA memory allocated by GEM DMA */
27 	void *vaddr;
28 
29 	bool map_noncoherent;
30 };
31 
32 #define to_drm_gem_dma_obj(gem_obj) \
33 	container_of(gem_obj, struct drm_gem_dma_object, base)
34 
35 struct drm_gem_dma_object *drm_gem_dma_create(struct drm_device *drm,
36 					      size_t size);
37 void drm_gem_dma_free(struct drm_gem_dma_object *dma_obj);
38 void drm_gem_dma_print_info(const struct drm_gem_dma_object *dma_obj,
39 			    struct drm_printer *p, unsigned int indent);
40 struct sg_table *drm_gem_dma_get_sg_table(struct drm_gem_dma_object *dma_obj);
41 int drm_gem_dma_vmap(struct drm_gem_dma_object *dma_obj,
42 		     struct iosys_map *map);
43 int drm_gem_dma_mmap(struct drm_gem_dma_object *dma_obj, struct vm_area_struct *vma);
44 
45 extern const struct vm_operations_struct drm_gem_dma_vm_ops;
46 
47 /*
48  * GEM object functions
49  */
50 
51 /**
52  * drm_gem_dma_object_free - GEM object function for drm_gem_dma_free()
53  * @obj: GEM object to free
54  *
55  * This function wraps drm_gem_dma_free_object(). Drivers that employ the DMA helpers
56  * should use it as their &drm_gem_object_funcs.free handler.
57  */
drm_gem_dma_object_free(struct drm_gem_object * obj)58 static inline void drm_gem_dma_object_free(struct drm_gem_object *obj)
59 {
60 	struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
61 
62 	drm_gem_dma_free(dma_obj);
63 }
64 
65 /**
66  * drm_gem_dma_object_print_info() - Print &drm_gem_dma_object info for debugfs
67  * @p: DRM printer
68  * @indent: Tab indentation level
69  * @obj: GEM object
70  *
71  * This function wraps drm_gem_dma_print_info(). Drivers that employ the DMA helpers
72  * should use this function as their &drm_gem_object_funcs.print_info handler.
73  */
drm_gem_dma_object_print_info(struct drm_printer * p,unsigned int indent,const struct drm_gem_object * obj)74 static inline void drm_gem_dma_object_print_info(struct drm_printer *p, unsigned int indent,
75 						 const struct drm_gem_object *obj)
76 {
77 	const struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
78 
79 	drm_gem_dma_print_info(dma_obj, p, indent);
80 }
81 
82 /**
83  * drm_gem_dma_object_get_sg_table - GEM object function for drm_gem_dma_get_sg_table()
84  * @obj: GEM object
85  *
86  * This function wraps drm_gem_dma_get_sg_table(). Drivers that employ the DMA helpers should
87  * use it as their &drm_gem_object_funcs.get_sg_table handler.
88  *
89  * Returns:
90  * A pointer to the scatter/gather table of pinned pages or NULL on failure.
91  */
drm_gem_dma_object_get_sg_table(struct drm_gem_object * obj)92 static inline struct sg_table *drm_gem_dma_object_get_sg_table(struct drm_gem_object *obj)
93 {
94 	struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
95 
96 	return drm_gem_dma_get_sg_table(dma_obj);
97 }
98 
99 /*
100  * drm_gem_dma_object_vmap - GEM object function for drm_gem_dma_vmap()
101  * @obj: GEM object
102  * @map: Returns the kernel virtual address of the DMA GEM object's backing store.
103  *
104  * This function wraps drm_gem_dma_vmap(). Drivers that employ the DMA helpers should
105  * use it as their &drm_gem_object_funcs.vmap handler.
106  *
107  * Returns:
108  * 0 on success or a negative error code on failure.
109  */
drm_gem_dma_object_vmap(struct drm_gem_object * obj,struct iosys_map * map)110 static inline int drm_gem_dma_object_vmap(struct drm_gem_object *obj,
111 					  struct iosys_map *map)
112 {
113 	struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
114 
115 	return drm_gem_dma_vmap(dma_obj, map);
116 }
117 
118 /**
119  * drm_gem_dma_object_mmap - GEM object function for drm_gem_dma_mmap()
120  * @obj: GEM object
121  * @vma: VMA for the area to be mapped
122  *
123  * This function wraps drm_gem_dma_mmap(). Drivers that employ the dma helpers should
124  * use it as their &drm_gem_object_funcs.mmap handler.
125  *
126  * Returns:
127  * 0 on success or a negative error code on failure.
128  */
drm_gem_dma_object_mmap(struct drm_gem_object * obj,struct vm_area_struct * vma)129 static inline int drm_gem_dma_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
130 {
131 	struct drm_gem_dma_object *dma_obj = to_drm_gem_dma_obj(obj);
132 
133 	return drm_gem_dma_mmap(dma_obj, vma);
134 }
135 
136 /*
137  * Driver ops
138  */
139 
140 /* create memory region for DRM framebuffer */
141 int drm_gem_dma_dumb_create_internal(struct drm_file *file_priv,
142 				     struct drm_device *drm,
143 				     struct drm_mode_create_dumb *args);
144 
145 /* create memory region for DRM framebuffer */
146 int drm_gem_dma_dumb_create(struct drm_file *file_priv,
147 			    struct drm_device *drm,
148 			    struct drm_mode_create_dumb *args);
149 
150 struct drm_gem_object *
151 drm_gem_dma_prime_import_sg_table(struct drm_device *dev,
152 				  struct dma_buf_attachment *attach,
153 				  struct sg_table *sgt);
154 
155 /**
156  * DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE - DMA GEM driver operations
157  * @dumb_create_func: callback function for .dumb_create
158  *
159  * This macro provides a shortcut for setting the default GEM operations in the
160  * &drm_driver structure.
161  *
162  * This macro is a variant of DRM_GEM_DMA_DRIVER_OPS for drivers that
163  * override the default implementation of &struct rm_driver.dumb_create. Use
164  * DRM_GEM_DMA_DRIVER_OPS if possible. Drivers that require a virtual address
165  * on imported buffers should use
166  * DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.
167  */
168 #define DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \
169 	.dumb_create		= (dumb_create_func), \
170 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd, \
171 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle, \
172 	.gem_prime_import_sg_table = drm_gem_dma_prime_import_sg_table, \
173 	.gem_prime_mmap		= drm_gem_prime_mmap
174 
175 /**
176  * DRM_GEM_DMA_DRIVER_OPS - DMA GEM driver operations
177  *
178  * This macro provides a shortcut for setting the default GEM operations in the
179  * &drm_driver structure.
180  *
181  * Drivers that come with their own implementation of
182  * &struct drm_driver.dumb_create should use
183  * DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use
184  * DRM_GEM_DMA_DRIVER_OPS if possible. Drivers that require a virtual address
185  * on imported buffers should use DRM_GEM_DMA_DRIVER_OPS_VMAP instead.
186  */
187 #define DRM_GEM_DMA_DRIVER_OPS \
188 	DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_dma_dumb_create)
189 
190 /**
191  * DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - DMA GEM driver operations
192  *                                                ensuring a virtual address
193  *                                                on the buffer
194  * @dumb_create_func: callback function for .dumb_create
195  *
196  * This macro provides a shortcut for setting the default GEM operations in the
197  * &drm_driver structure for drivers that need the virtual address also on
198  * imported buffers.
199  *
200  * This macro is a variant of DRM_GEM_DMA_DRIVER_OPS_VMAP for drivers that
201  * override the default implementation of &struct drm_driver.dumb_create. Use
202  * DRM_GEM_DMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
203  * virtual address on imported buffers should use
204  * DRM_GEM_DMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.
205  */
206 #define DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \
207 	.dumb_create		= dumb_create_func, \
208 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd, \
209 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle, \
210 	.gem_prime_import_sg_table = drm_gem_dma_prime_import_sg_table_vmap, \
211 	.gem_prime_mmap		= drm_gem_prime_mmap
212 
213 /**
214  * DRM_GEM_DMA_DRIVER_OPS_VMAP - DMA GEM driver operations ensuring a virtual
215  *                               address on the buffer
216  *
217  * This macro provides a shortcut for setting the default GEM operations in the
218  * &drm_driver structure for drivers that need the virtual address also on
219  * imported buffers.
220  *
221  * Drivers that come with their own implementation of
222  * &struct drm_driver.dumb_create should use
223  * DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use
224  * DRM_GEM_DMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
225  * virtual address on imported buffers should use DRM_GEM_DMA_DRIVER_OPS
226  * instead.
227  */
228 #define DRM_GEM_DMA_DRIVER_OPS_VMAP \
229 	DRM_GEM_DMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_dma_dumb_create)
230 
231 struct drm_gem_object *
232 drm_gem_dma_prime_import_sg_table_vmap(struct drm_device *drm,
233 				       struct dma_buf_attachment *attach,
234 				       struct sg_table *sgt);
235 
236 /*
237  * File ops
238  */
239 
240 #ifndef CONFIG_MMU
241 unsigned long drm_gem_dma_get_unmapped_area(struct file *filp,
242 					    unsigned long addr,
243 					    unsigned long len,
244 					    unsigned long pgoff,
245 					    unsigned long flags);
246 #define DRM_GEM_DMA_UNMAPPED_AREA_FOPS \
247 	.get_unmapped_area	= drm_gem_dma_get_unmapped_area,
248 #else
249 #define DRM_GEM_DMA_UNMAPPED_AREA_FOPS
250 #endif
251 
252 /**
253  * DEFINE_DRM_GEM_DMA_FOPS() - macro to generate file operations for DMA drivers
254  * @name: name for the generated structure
255  *
256  * This macro autogenerates a suitable &struct file_operations for DMA based
257  * drivers, which can be assigned to &drm_driver.fops. Note that this structure
258  * cannot be shared between drivers, because it contains a reference to the
259  * current module using THIS_MODULE.
260  *
261  * Note that the declaration is already marked as static - if you need a
262  * non-static version of this you're probably doing it wrong and will break the
263  * THIS_MODULE reference by accident.
264  */
265 #define DEFINE_DRM_GEM_DMA_FOPS(name) \
266 	static const struct file_operations name = {\
267 		.owner		= THIS_MODULE,\
268 		.open		= drm_open,\
269 		.release	= drm_release,\
270 		.unlocked_ioctl	= drm_ioctl,\
271 		.compat_ioctl	= drm_compat_ioctl,\
272 		.poll		= drm_poll,\
273 		.read		= drm_read,\
274 		.llseek		= noop_llseek,\
275 		.mmap		= drm_gem_mmap,\
276 		DRM_GEM_DMA_UNMAPPED_AREA_FOPS \
277 	}
278 
279 #endif /* __DRM_GEM_DMA_HELPER_H__ */
280