Linux 6.9-rc1
[linux-2.6-microblaze.git] / drm / drm_gem_cma_helper.h
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  */
20 struct drm_gem_cma_object {
21         struct drm_gem_object base;
22         dma_addr_t paddr;
23         struct sg_table *sgt;
24
25         /* For objects with DMA memory allocated by GEM CMA */
26         void *vaddr;
27 };
28
29 #define to_drm_gem_cma_obj(gem_obj) \
30         container_of(gem_obj, struct drm_gem_cma_object, base)
31
32 #ifndef CONFIG_MMU
33 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
34         .get_unmapped_area      = drm_gem_cma_get_unmapped_area,
35 #else
36 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
37 #endif
38
39 /**
40  * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
41  * @name: name for the generated structure
42  *
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.
47  *
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.
51  */
52 #define DEFINE_DRM_GEM_CMA_FOPS(name) \
53         static const struct file_operations name = {\
54                 .owner          = THIS_MODULE,\
55                 .open           = drm_open,\
56                 .release        = drm_release,\
57                 .unlocked_ioctl = drm_ioctl,\
58                 .compat_ioctl   = drm_compat_ioctl,\
59                 .poll           = drm_poll,\
60                 .read           = drm_read,\
61                 .llseek         = noop_llseek,\
62                 .mmap           = drm_gem_cma_mmap,\
63                 DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
64         }
65
66 /* free GEM object */
67 void drm_gem_cma_free_object(struct drm_gem_object *gem_obj);
68
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);
73
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);
78
79 /* set vm_flags and we can change the VM attribute to other one at here */
80 int drm_gem_cma_mmap(struct file *filp, struct vm_area_struct *vma);
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_prime_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_prime_mmap(struct drm_gem_object *obj,
105                            struct vm_area_struct *vma);
106 int drm_gem_cma_prime_vmap(struct drm_gem_object *obj, struct dma_buf_map *map);
107
108 /**
109  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE - CMA GEM driver operations
110  * @dumb_create_func: callback function for .dumb_create
111  *
112  * This macro provides a shortcut for setting the default GEM operations in the
113  * &drm_driver structure.
114  *
115  * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS for drivers that
116  * override the default implementation of &struct rm_driver.dumb_create. Use
117  * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
118  * on imported buffers should use
119  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.
120  */
121 #define DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \
122         .dumb_create            = (dumb_create_func), \
123         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd, \
124         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle, \
125         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, \
126         .gem_prime_mmap         = drm_gem_cma_prime_mmap
127
128 /**
129  * DRM_GEM_CMA_DRIVER_OPS - CMA GEM driver operations
130  *
131  * This macro provides a shortcut for setting the default GEM operations in the
132  * &drm_driver structure.
133  *
134  * Drivers that come with their own implementation of
135  * &struct drm_driver.dumb_create should use
136  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use
137  * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
138  * on imported buffers should use DRM_GEM_CMA_DRIVER_OPS_VMAP instead.
139  */
140 #define DRM_GEM_CMA_DRIVER_OPS \
141         DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
142
143 /**
144  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - CMA GEM driver operations
145  *                                                ensuring a virtual address
146  *                                                on the buffer
147  * @dumb_create_func: callback function for .dumb_create
148  *
149  * This macro provides a shortcut for setting the default GEM operations in the
150  * &drm_driver structure for drivers that need the virtual address also on
151  * imported buffers.
152  *
153  * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS_VMAP for drivers that
154  * override the default implementation of &struct drm_driver.dumb_create. Use
155  * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
156  * virtual address on imported buffers should use
157  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.
158  */
159 #define DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \
160         .dumb_create            = dumb_create_func, \
161         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd, \
162         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle, \
163         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table_vmap, \
164         .gem_prime_mmap         = drm_gem_prime_mmap
165
166 /**
167  * DRM_GEM_CMA_DRIVER_OPS_VMAP - CMA GEM driver operations ensuring a virtual
168  *                               address on the buffer
169  *
170  * This macro provides a shortcut for setting the default GEM operations in the
171  * &drm_driver structure for drivers that need the virtual address also on
172  * imported buffers.
173  *
174  * Drivers that come with their own implementation of
175  * &struct drm_driver.dumb_create should use
176  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use
177  * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
178  * virtual address on imported buffers should use DRM_GEM_CMA_DRIVER_OPS
179  * instead.
180  */
181 #define DRM_GEM_CMA_DRIVER_OPS_VMAP \
182         DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
183
184 struct drm_gem_object *
185 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *drm,
186                                        struct dma_buf_attachment *attach,
187                                        struct sg_table *sgt);
188
189 #endif /* __DRM_GEM_CMA_HELPER_H__ */