1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __DRM_GEM_CMA_HELPER_H__
3 #define __DRM_GEM_CMA_HELPER_H__
5 #include <drm/drm_file.h>
6 #include <drm/drm_ioctl.h>
7 #include <drm/drm_gem.h>
9 struct drm_mode_create_dumb;
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
18 * @vaddr: kernel virtual address of the backing memory
19 * @map_noncoherent: if true, the GEM object is backed by non-coherent memory
21 struct drm_gem_cma_object {
22 struct drm_gem_object base;
26 /* For objects with DMA memory allocated by GEM CMA */
32 #define to_drm_gem_cma_obj(gem_obj) \
33 container_of(gem_obj, struct drm_gem_cma_object, base)
36 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
37 .get_unmapped_area = drm_gem_cma_get_unmapped_area,
39 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
43 * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
44 * @name: name for the generated structure
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.
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.
55 #define DEFINE_DRM_GEM_CMA_FOPS(name) \
56 static const struct file_operations name = {\
57 .owner = THIS_MODULE,\
59 .release = drm_release,\
60 .unlocked_ioctl = drm_ioctl,\
61 .compat_ioctl = drm_compat_ioctl,\
64 .llseek = noop_llseek,\
65 .mmap = drm_gem_mmap,\
66 DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
70 void drm_gem_cma_free_object(struct drm_gem_object *gem_obj);
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);
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);
82 /* allocate physical memory */
83 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
86 extern const struct vm_operations_struct drm_gem_cma_vm_ops;
89 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
96 void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
97 const struct drm_gem_object *obj);
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);
108 * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE - CMA GEM driver operations
109 * @dumb_create_func: callback function for .dumb_create
111 * This macro provides a shortcut for setting the default GEM operations in the
112 * &drm_driver structure.
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.
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
128 * DRM_GEM_CMA_DRIVER_OPS - CMA GEM driver operations
130 * This macro provides a shortcut for setting the default GEM operations in the
131 * &drm_driver structure.
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.
139 #define DRM_GEM_CMA_DRIVER_OPS \
140 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
143 * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - CMA GEM driver operations
144 * ensuring a virtual address
146 * @dumb_create_func: callback function for .dumb_create
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
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.
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
166 * DRM_GEM_CMA_DRIVER_OPS_VMAP - CMA GEM driver operations ensuring a virtual
167 * address on the buffer
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
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
180 #define DRM_GEM_CMA_DRIVER_OPS_VMAP \
181 DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
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);
188 #endif /* __DRM_GEM_CMA_HELPER_H__ */