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