1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __DRM_GEM_CMA_HELPER_H__
3 #define __DRM_GEM_CMA_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_cma_object - GEM object backed by CMA memory allocations
13  * @base: base GEM object
14  * @paddr: physical 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_cma_object {
22 	struct drm_gem_object base;
23 	dma_addr_t paddr;
24 	struct sg_table *sgt;
25 
26 	/* For objects with DMA memory allocated by GEM CMA */
27 	void *vaddr;
28 
29 	bool map_noncoherent;
30 };
31 
32 #define to_drm_gem_cma_obj(gem_obj) \
33 	container_of(gem_obj, struct drm_gem_cma_object, base)
34 
35 #ifndef CONFIG_MMU
36 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
37 	.get_unmapped_area	= drm_gem_cma_get_unmapped_area,
38 #else
39 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
40 #endif
41 
42 /**
43  * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
44  * @name: name for the generated structure
45  *
46  * This macro autogenerates a suitable &struct file_operations for CMA based
47  * drivers, which can be assigned to &drm_driver.fops. Note that this structure
48  * cannot be shared between drivers, because it contains a reference to the
49  * current module using THIS_MODULE.
50  *
51  * Note that the declaration is already marked as static - if you need a
52  * non-static version of this you're probably doing it wrong and will break the
53  * THIS_MODULE reference by accident.
54  */
55 #define DEFINE_DRM_GEM_CMA_FOPS(name) \
56 	static const struct file_operations name = {\
57 		.owner		= THIS_MODULE,\
58 		.open		= drm_open,\
59 		.release	= drm_release,\
60 		.unlocked_ioctl	= drm_ioctl,\
61 		.compat_ioctl	= drm_compat_ioctl,\
62 		.poll		= drm_poll,\
63 		.read		= drm_read,\
64 		.llseek		= noop_llseek,\
65 		.mmap		= drm_gem_mmap,\
66 		DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
67 	}
68 
69 /* free GEM object */
70 void drm_gem_cma_free_object(struct drm_gem_object *gem_obj);
71 
72 /* create memory region for DRM framebuffer */
73 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
74 				     struct drm_device *drm,
75 				     struct drm_mode_create_dumb *args);
76 
77 /* create memory region for DRM framebuffer */
78 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
79 			    struct drm_device *drm,
80 			    struct drm_mode_create_dumb *args);
81 
82 /* allocate physical memory */
83 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
84 					      size_t size);
85 
86 extern const struct vm_operations_struct drm_gem_cma_vm_ops;
87 
88 #ifndef CONFIG_MMU
89 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
90 					    unsigned long addr,
91 					    unsigned long len,
92 					    unsigned long pgoff,
93 					    unsigned long flags);
94 #endif
95 
96 void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
97 			    const struct drm_gem_object *obj);
98 
99 struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_object *obj);
100 struct drm_gem_object *
101 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
102 				  struct dma_buf_attachment *attach,
103 				  struct sg_table *sgt);
104 int drm_gem_cma_vmap(struct drm_gem_object *obj, struct dma_buf_map *map);
105 int drm_gem_cma_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
106 
107 /**
108  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE - CMA GEM driver operations
109  * @dumb_create_func: callback function for .dumb_create
110  *
111  * This macro provides a shortcut for setting the default GEM operations in the
112  * &drm_driver structure.
113  *
114  * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS for drivers that
115  * override the default implementation of &struct rm_driver.dumb_create. Use
116  * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
117  * on imported buffers should use
118  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.
119  */
120 #define DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \
121 	.dumb_create		= (dumb_create_func), \
122 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd, \
123 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle, \
124 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, \
125 	.gem_prime_mmap		= drm_gem_prime_mmap
126 
127 /**
128  * DRM_GEM_CMA_DRIVER_OPS - CMA GEM driver operations
129  *
130  * This macro provides a shortcut for setting the default GEM operations in the
131  * &drm_driver structure.
132  *
133  * Drivers that come with their own implementation of
134  * &struct drm_driver.dumb_create should use
135  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use
136  * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
137  * on imported buffers should use DRM_GEM_CMA_DRIVER_OPS_VMAP instead.
138  */
139 #define DRM_GEM_CMA_DRIVER_OPS \
140 	DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
141 
142 /**
143  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - CMA GEM driver operations
144  *                                                ensuring a virtual address
145  *                                                on the buffer
146  * @dumb_create_func: callback function for .dumb_create
147  *
148  * This macro provides a shortcut for setting the default GEM operations in the
149  * &drm_driver structure for drivers that need the virtual address also on
150  * imported buffers.
151  *
152  * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS_VMAP for drivers that
153  * override the default implementation of &struct drm_driver.dumb_create. Use
154  * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
155  * virtual address on imported buffers should use
156  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.
157  */
158 #define DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \
159 	.dumb_create		= dumb_create_func, \
160 	.prime_handle_to_fd	= drm_gem_prime_handle_to_fd, \
161 	.prime_fd_to_handle	= drm_gem_prime_fd_to_handle, \
162 	.gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table_vmap, \
163 	.gem_prime_mmap		= drm_gem_prime_mmap
164 
165 /**
166  * DRM_GEM_CMA_DRIVER_OPS_VMAP - CMA GEM driver operations ensuring a virtual
167  *                               address on the buffer
168  *
169  * This macro provides a shortcut for setting the default GEM operations in the
170  * &drm_driver structure for drivers that need the virtual address also on
171  * imported buffers.
172  *
173  * Drivers that come with their own implementation of
174  * &struct drm_driver.dumb_create should use
175  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use
176  * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
177  * virtual address on imported buffers should use DRM_GEM_CMA_DRIVER_OPS
178  * instead.
179  */
180 #define DRM_GEM_CMA_DRIVER_OPS_VMAP \
181 	DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
182 
183 struct drm_gem_object *
184 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *drm,
185 				       struct dma_buf_attachment *attach,
186 				       struct sg_table *sgt);
187 
188 #endif /* __DRM_GEM_CMA_HELPER_H__ */
189