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
20 struct drm_gem_cma_object {
21 struct drm_gem_object base;
25 /* For objects with DMA memory allocated by GEM CMA */
29 #define to_drm_gem_cma_obj(gem_obj) \
30 container_of(gem_obj, struct drm_gem_cma_object, base)
33 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
34 .get_unmapped_area = drm_gem_cma_get_unmapped_area,
36 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
40 * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
41 * @name: name for the generated structure
43 * This macro autogenerates a suitable &struct file_operations for CMA based
44 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
45 * cannot be shared between drivers, because it contains a reference to the
46 * current module using THIS_MODULE.
48 * Note that the declaration is already marked as static - if you need a
49 * non-static version of this you're probably doing it wrong and will break the
50 * THIS_MODULE reference by accident.
52 #define DEFINE_DRM_GEM_CMA_FOPS(name) \
53 static const struct file_operations name = {\
54 .owner = THIS_MODULE,\
56 .release = drm_release,\
57 .unlocked_ioctl = drm_ioctl,\
58 .compat_ioctl = drm_compat_ioctl,\
61 .llseek = noop_llseek,\
62 .mmap = drm_gem_mmap,\
63 DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
67 void drm_gem_cma_free_object(struct drm_gem_object *gem_obj);
69 /* create memory region for DRM framebuffer */
70 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
71 struct drm_device *drm,
72 struct drm_mode_create_dumb *args);
74 /* create memory region for DRM framebuffer */
75 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
76 struct drm_device *drm,
77 struct drm_mode_create_dumb *args);
79 /* allocate physical memory */
80 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
83 extern const struct vm_operations_struct drm_gem_cma_vm_ops;
86 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
93 void drm_gem_cma_print_info(struct drm_printer *p, unsigned int indent,
94 const struct drm_gem_object *obj);
96 struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_object *obj);
97 struct drm_gem_object *
98 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
99 struct dma_buf_attachment *attach,
100 struct sg_table *sgt);
101 int drm_gem_cma_vmap(struct drm_gem_object *obj, struct dma_buf_map *map);
102 int drm_gem_cma_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
105 * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE - CMA GEM driver operations
106 * @dumb_create_func: callback function for .dumb_create
108 * This macro provides a shortcut for setting the default GEM operations in the
109 * &drm_driver structure.
111 * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS for drivers that
112 * override the default implementation of &struct rm_driver.dumb_create. Use
113 * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
114 * on imported buffers should use
115 * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.
117 #define DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \
118 .dumb_create = (dumb_create_func), \
119 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
120 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
121 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, \
122 .gem_prime_mmap = drm_gem_prime_mmap
125 * DRM_GEM_CMA_DRIVER_OPS - CMA GEM driver operations
127 * This macro provides a shortcut for setting the default GEM operations in the
128 * &drm_driver structure.
130 * Drivers that come with their own implementation of
131 * &struct drm_driver.dumb_create should use
132 * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use
133 * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
134 * on imported buffers should use DRM_GEM_CMA_DRIVER_OPS_VMAP instead.
136 #define DRM_GEM_CMA_DRIVER_OPS \
137 DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
140 * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - CMA GEM driver operations
141 * ensuring a virtual address
143 * @dumb_create_func: callback function for .dumb_create
145 * This macro provides a shortcut for setting the default GEM operations in the
146 * &drm_driver structure for drivers that need the virtual address also on
149 * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS_VMAP for drivers that
150 * override the default implementation of &struct drm_driver.dumb_create. Use
151 * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
152 * virtual address on imported buffers should use
153 * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.
155 #define DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \
156 .dumb_create = dumb_create_func, \
157 .prime_handle_to_fd = drm_gem_prime_handle_to_fd, \
158 .prime_fd_to_handle = drm_gem_prime_fd_to_handle, \
159 .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table_vmap, \
160 .gem_prime_mmap = drm_gem_prime_mmap
163 * DRM_GEM_CMA_DRIVER_OPS_VMAP - CMA GEM driver operations ensuring a virtual
164 * address on the buffer
166 * This macro provides a shortcut for setting the default GEM operations in the
167 * &drm_driver structure for drivers that need the virtual address also on
170 * Drivers that come with their own implementation of
171 * &struct drm_driver.dumb_create should use
172 * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use
173 * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
174 * virtual address on imported buffers should use DRM_GEM_CMA_DRIVER_OPS
177 #define DRM_GEM_CMA_DRIVER_OPS_VMAP \
178 DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
180 struct drm_gem_object *
181 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *drm,
182 struct dma_buf_attachment *attach,
183 struct sg_table *sgt);
185 #endif /* __DRM_GEM_CMA_HELPER_H__ */