]> Git Repo - linux.git/blob - include/drm/drm_gem_cma_helper.h
slab: remove __alloc_size attribute from __kmalloc_track_caller
[linux.git] / include / 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  * @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 struct drm_gem_cma_object *drm_gem_cma_create(struct drm_device *drm,
36                                               size_t size);
37 void drm_gem_cma_free(struct drm_gem_cma_object *cma_obj);
38 void drm_gem_cma_print_info(const struct drm_gem_cma_object *cma_obj,
39                             struct drm_printer *p, unsigned int indent);
40 struct sg_table *drm_gem_cma_get_sg_table(struct drm_gem_cma_object *cma_obj);
41 int drm_gem_cma_vmap(struct drm_gem_cma_object *cma_obj, struct dma_buf_map *map);
42 int drm_gem_cma_mmap(struct drm_gem_cma_object *cma_obj, struct vm_area_struct *vma);
43
44 extern const struct vm_operations_struct drm_gem_cma_vm_ops;
45
46 /*
47  * GEM object functions
48  */
49
50 /**
51  * drm_gem_cma_object_free - GEM object function for drm_gem_cma_free()
52  * @obj: GEM object to free
53  *
54  * This function wraps drm_gem_cma_free_object(). Drivers that employ the CMA helpers
55  * should use it as their &drm_gem_object_funcs.free handler.
56  */
57 static inline void drm_gem_cma_object_free(struct drm_gem_object *obj)
58 {
59         struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
60
61         drm_gem_cma_free(cma_obj);
62 }
63
64 /**
65  * drm_gem_cma_object_print_info() - Print &drm_gem_cma_object info for debugfs
66  * @p: DRM printer
67  * @indent: Tab indentation level
68  * @obj: GEM object
69  *
70  * This function wraps drm_gem_cma_print_info(). Drivers that employ the CMA helpers
71  * should use this function as their &drm_gem_object_funcs.print_info handler.
72  */
73 static inline void drm_gem_cma_object_print_info(struct drm_printer *p, unsigned int indent,
74                                                  const struct drm_gem_object *obj)
75 {
76         const struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
77
78         drm_gem_cma_print_info(cma_obj, p, indent);
79 }
80
81 /**
82  * drm_gem_cma_object_get_sg_table - GEM object function for drm_gem_cma_get_sg_table()
83  * @obj: GEM object
84  *
85  * This function wraps drm_gem_cma_get_sg_table(). Drivers that employ the CMA helpers should
86  * use it as their &drm_gem_object_funcs.get_sg_table handler.
87  *
88  * Returns:
89  * A pointer to the scatter/gather table of pinned pages or NULL on failure.
90  */
91 static inline struct sg_table *drm_gem_cma_object_get_sg_table(struct drm_gem_object *obj)
92 {
93         struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
94
95         return drm_gem_cma_get_sg_table(cma_obj);
96 }
97
98 /*
99  * drm_gem_cma_object_vmap - GEM object function for drm_gem_cma_vmap()
100  * @obj: GEM object
101  * @map: Returns the kernel virtual address of the CMA GEM object's backing store.
102  *
103  * This function wraps drm_gem_cma_vmap(). Drivers that employ the CMA helpers should
104  * use it as their &drm_gem_object_funcs.vmap handler.
105  *
106  * Returns:
107  * 0 on success or a negative error code on failure.
108  */
109 static inline int drm_gem_cma_object_vmap(struct drm_gem_object *obj, struct dma_buf_map *map)
110 {
111         struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
112
113         return drm_gem_cma_vmap(cma_obj, map);
114 }
115
116 /**
117  * drm_gem_cma_object_mmap - GEM object function for drm_gem_cma_mmap()
118  * @obj: GEM object
119  * @vma: VMA for the area to be mapped
120  *
121  * This function wraps drm_gem_cma_mmap(). Drivers that employ the cma helpers should
122  * use it as their &drm_gem_object_funcs.mmap handler.
123  *
124  * Returns:
125  * 0 on success or a negative error code on failure.
126  */
127 static inline int drm_gem_cma_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
128 {
129         struct drm_gem_cma_object *cma_obj = to_drm_gem_cma_obj(obj);
130
131         return drm_gem_cma_mmap(cma_obj, vma);
132 }
133
134 /*
135  * Driver ops
136  */
137
138 /* create memory region for DRM framebuffer */
139 int drm_gem_cma_dumb_create_internal(struct drm_file *file_priv,
140                                      struct drm_device *drm,
141                                      struct drm_mode_create_dumb *args);
142
143 /* create memory region for DRM framebuffer */
144 int drm_gem_cma_dumb_create(struct drm_file *file_priv,
145                             struct drm_device *drm,
146                             struct drm_mode_create_dumb *args);
147
148 struct drm_gem_object *
149 drm_gem_cma_prime_import_sg_table(struct drm_device *dev,
150                                   struct dma_buf_attachment *attach,
151                                   struct sg_table *sgt);
152
153 /**
154  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE - CMA GEM driver operations
155  * @dumb_create_func: callback function for .dumb_create
156  *
157  * This macro provides a shortcut for setting the default GEM operations in the
158  * &drm_driver structure.
159  *
160  * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS for drivers that
161  * override the default implementation of &struct rm_driver.dumb_create. Use
162  * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
163  * on imported buffers should use
164  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead.
165  */
166 #define DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(dumb_create_func) \
167         .dumb_create            = (dumb_create_func), \
168         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd, \
169         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle, \
170         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table, \
171         .gem_prime_mmap         = drm_gem_prime_mmap
172
173 /**
174  * DRM_GEM_CMA_DRIVER_OPS - CMA GEM driver operations
175  *
176  * This macro provides a shortcut for setting the default GEM operations in the
177  * &drm_driver structure.
178  *
179  * Drivers that come with their own implementation of
180  * &struct drm_driver.dumb_create should use
181  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead. Use
182  * DRM_GEM_CMA_DRIVER_OPS if possible. Drivers that require a virtual address
183  * on imported buffers should use DRM_GEM_CMA_DRIVER_OPS_VMAP instead.
184  */
185 #define DRM_GEM_CMA_DRIVER_OPS \
186         DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
187
188 /**
189  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE - CMA GEM driver operations
190  *                                                ensuring a virtual address
191  *                                                on the buffer
192  * @dumb_create_func: callback function for .dumb_create
193  *
194  * This macro provides a shortcut for setting the default GEM operations in the
195  * &drm_driver structure for drivers that need the virtual address also on
196  * imported buffers.
197  *
198  * This macro is a variant of DRM_GEM_CMA_DRIVER_OPS_VMAP for drivers that
199  * override the default implementation of &struct drm_driver.dumb_create. Use
200  * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
201  * virtual address on imported buffers should use
202  * DRM_GEM_CMA_DRIVER_OPS_WITH_DUMB_CREATE() instead.
203  */
204 #define DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(dumb_create_func) \
205         .dumb_create            = dumb_create_func, \
206         .prime_handle_to_fd     = drm_gem_prime_handle_to_fd, \
207         .prime_fd_to_handle     = drm_gem_prime_fd_to_handle, \
208         .gem_prime_import_sg_table = drm_gem_cma_prime_import_sg_table_vmap, \
209         .gem_prime_mmap         = drm_gem_prime_mmap
210
211 /**
212  * DRM_GEM_CMA_DRIVER_OPS_VMAP - CMA GEM driver operations ensuring a virtual
213  *                               address on the buffer
214  *
215  * This macro provides a shortcut for setting the default GEM operations in the
216  * &drm_driver structure for drivers that need the virtual address also on
217  * imported buffers.
218  *
219  * Drivers that come with their own implementation of
220  * &struct drm_driver.dumb_create should use
221  * DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE() instead. Use
222  * DRM_GEM_CMA_DRIVER_OPS_VMAP if possible. Drivers that do not require a
223  * virtual address on imported buffers should use DRM_GEM_CMA_DRIVER_OPS
224  * instead.
225  */
226 #define DRM_GEM_CMA_DRIVER_OPS_VMAP \
227         DRM_GEM_CMA_DRIVER_OPS_VMAP_WITH_DUMB_CREATE(drm_gem_cma_dumb_create)
228
229 struct drm_gem_object *
230 drm_gem_cma_prime_import_sg_table_vmap(struct drm_device *drm,
231                                        struct dma_buf_attachment *attach,
232                                        struct sg_table *sgt);
233
234 /*
235  * File ops
236  */
237
238 #ifndef CONFIG_MMU
239 unsigned long drm_gem_cma_get_unmapped_area(struct file *filp,
240                                             unsigned long addr,
241                                             unsigned long len,
242                                             unsigned long pgoff,
243                                             unsigned long flags);
244 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
245         .get_unmapped_area      = drm_gem_cma_get_unmapped_area,
246 #else
247 #define DRM_GEM_CMA_UNMAPPED_AREA_FOPS
248 #endif
249
250 /**
251  * DEFINE_DRM_GEM_CMA_FOPS() - macro to generate file operations for CMA drivers
252  * @name: name for the generated structure
253  *
254  * This macro autogenerates a suitable &struct file_operations for CMA based
255  * drivers, which can be assigned to &drm_driver.fops. Note that this structure
256  * cannot be shared between drivers, because it contains a reference to the
257  * current module using THIS_MODULE.
258  *
259  * Note that the declaration is already marked as static - if you need a
260  * non-static version of this you're probably doing it wrong and will break the
261  * THIS_MODULE reference by accident.
262  */
263 #define DEFINE_DRM_GEM_CMA_FOPS(name) \
264         static const struct file_operations name = {\
265                 .owner          = THIS_MODULE,\
266                 .open           = drm_open,\
267                 .release        = drm_release,\
268                 .unlocked_ioctl = drm_ioctl,\
269                 .compat_ioctl   = drm_compat_ioctl,\
270                 .poll           = drm_poll,\
271                 .read           = drm_read,\
272                 .llseek         = noop_llseek,\
273                 .mmap           = drm_gem_mmap,\
274                 DRM_GEM_CMA_UNMAPPED_AREA_FOPS \
275         }
276
277 #endif /* __DRM_GEM_CMA_HELPER_H__ */
This page took 0.049316 seconds and 4 git commands to generate.