drm_gem.h revision 1.10 1 /* $NetBSD: drm_gem.h,v 1.10 2021/12/19 09:48:14 riastradh Exp $ */
2
3 #ifndef __DRM_GEM_H__
4 #define __DRM_GEM_H__
5
6 /*
7 * GEM Graphics Execution Manager Driver Interfaces
8 *
9 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
10 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
11 * Copyright (c) 2009-2010, Code Aurora Forum.
12 * All rights reserved.
13 * Copyright 2014 Intel Corporation
14 * Daniel Vetter <daniel.vetter (at) ffwll.ch>
15 *
16 * Author: Rickard E. (Rik) Faith <faith (at) valinux.com>
17 * Author: Gareth Hughes <gareth (at) valinux.com>
18 *
19 * Permission is hereby granted, free of charge, to any person obtaining a
20 * copy of this software and associated documentation files (the "Software"),
21 * to deal in the Software without restriction, including without limitation
22 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
23 * and/or sell copies of the Software, and to permit persons to whom the
24 * Software is furnished to do so, subject to the following conditions:
25 *
26 * The above copyright notice and this permission notice (including the next
27 * paragraph) shall be included in all copies or substantial portions of the
28 * Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
33 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
34 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
35 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36 * OTHER DEALINGS IN THE SOFTWARE.
37 */
38
39 #ifdef __NetBSD__
40 #include <uvm/uvm.h>
41 #endif
42
43 #include <linux/types.h>
44 #include <linux/kref.h>
45 #include <linux/dma-resv.h>
46
47 #include <drm/drm_vma_manager.h>
48 #undef free
49
50 struct drm_gem_object;
51 struct xarray;
52
53 /**
54 * struct drm_gem_object_funcs - GEM object functions
55 */
56 struct drm_gem_object_funcs {
57 /**
58 * @free:
59 *
60 * Deconstructor for drm_gem_objects.
61 *
62 * This callback is mandatory.
63 */
64 void (*free)(struct drm_gem_object *obj);
65
66 /**
67 * @open:
68 *
69 * Called upon GEM handle creation.
70 *
71 * This callback is optional.
72 */
73 int (*open)(struct drm_gem_object *obj, struct drm_file *file);
74
75 /**
76 * @close:
77 *
78 * Called upon GEM handle release.
79 *
80 * This callback is optional.
81 */
82 void (*close)(struct drm_gem_object *obj, struct drm_file *file);
83
84 /**
85 * @print_info:
86 *
87 * If driver subclasses struct &drm_gem_object, it can implement this
88 * optional hook for printing additional driver specific info.
89 *
90 * drm_printf_indent() should be used in the callback passing it the
91 * indent argument.
92 *
93 * This callback is called from drm_gem_print_info().
94 *
95 * This callback is optional.
96 */
97 void (*print_info)(struct drm_printer *p, unsigned int indent,
98 const struct drm_gem_object *obj);
99
100 /**
101 * @export:
102 *
103 * Export backing buffer as a &dma_buf.
104 * If this is not set drm_gem_prime_export() is used.
105 *
106 * This callback is optional.
107 */
108 struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
109
110 /**
111 * @pin:
112 *
113 * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper.
114 *
115 * This callback is optional.
116 */
117 int (*pin)(struct drm_gem_object *obj);
118
119 /**
120 * @unpin:
121 *
122 * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
123 *
124 * This callback is optional.
125 */
126 void (*unpin)(struct drm_gem_object *obj);
127
128 /**
129 * @get_sg_table:
130 *
131 * Returns a Scatter-Gather table representation of the buffer.
132 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
133 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
134 * in drm_gem_unmap_buf(), therefore these helpers and this callback
135 * here cannot be used for sg tables pointing at driver private memory
136 * ranges.
137 *
138 * See also drm_prime_pages_to_sg().
139 */
140 struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
141
142 /**
143 * @vmap:
144 *
145 * Returns a virtual address for the buffer. Used by the
146 * drm_gem_dmabuf_vmap() helper.
147 *
148 * This callback is optional.
149 */
150 void *(*vmap)(struct drm_gem_object *obj);
151
152 /**
153 * @vunmap:
154 *
155 * Releases the the address previously returned by @vmap. Used by the
156 * drm_gem_dmabuf_vunmap() helper.
157 *
158 * This callback is optional.
159 */
160 void (*vunmap)(struct drm_gem_object *obj, void *vaddr);
161
162 /**
163 * @mmap:
164 *
165 * Handle mmap() of the gem object, setup vma accordingly.
166 *
167 * This callback is optional.
168 *
169 * The callback is used by by both drm_gem_mmap_obj() and
170 * drm_gem_prime_mmap(). When @mmap is present @vm_ops is not
171 * used, the @mmap callback must set vma->vm_ops instead.
172 */
173 #ifdef __NetBSD__
174 int (*mmap)(struct drm_device *, off_t, size_t, int, struct uvm_object **,
175 voff_t *, struct file *);
176 #else
177 int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
178 #endif
179
180 /**
181 * @vm_ops:
182 *
183 * Virtual memory operations used with mmap.
184 *
185 * This is optional but necessary for mmap support.
186 */
187 const struct vm_operations_struct *vm_ops;
188 };
189
190 /**
191 * struct drm_gem_object - GEM buffer object
192 *
193 * This structure defines the generic parts for GEM buffer objects, which are
194 * mostly around handling mmap and userspace handles.
195 *
196 * Buffer objects are often abbreviated to BO.
197 */
198 struct drm_gem_object {
199 /**
200 * @refcount:
201 *
202 * Reference count of this object
203 *
204 * Please use drm_gem_object_get() to acquire and drm_gem_object_put()
205 * or drm_gem_object_put_unlocked() to release a reference to a GEM
206 * buffer object.
207 */
208 struct kref refcount;
209
210 /**
211 * @handle_count:
212 *
213 * This is the GEM file_priv handle count of this object.
214 *
215 * Each handle also holds a reference. Note that when the handle_count
216 * drops to 0 any global names (e.g. the id in the flink namespace) will
217 * be cleared.
218 *
219 * Protected by &drm_device.object_name_lock.
220 */
221 unsigned handle_count;
222
223 /**
224 * @dev: DRM dev this object belongs to.
225 */
226 struct drm_device *dev;
227
228 #ifdef __NetBSD__
229 /* UVM anonymous object for shared memory mappings. */
230 struct uvm_object *filp;
231
232 /* UVM object with custom pager ops for device memory mappings. */
233 struct uvm_object gemo_uvmobj;
234 #else
235 /**
236 * @filp:
237 *
238 * SHMEM file node used as backing storage for swappable buffer objects.
239 * GEM also supports driver private objects with driver-specific backing
240 * storage (contiguous CMA memory, special reserved blocks). In this
241 * case @filp is NULL.
242 */
243 struct file *filp;
244 #endif
245
246 /**
247 * @vma_node:
248 *
249 * Mapping info for this object to support mmap. Drivers are supposed to
250 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
251 * offset itself can be retrieved using drm_vma_node_offset_addr().
252 *
253 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
254 * that userspace is allowed to access the object.
255 */
256 struct drm_vma_offset_node vma_node;
257
258 /**
259 * @size:
260 *
261 * Size of the object, in bytes. Immutable over the object's
262 * lifetime.
263 */
264 size_t size;
265
266 /**
267 * @name:
268 *
269 * Global name for this object, starts at 1. 0 means unnamed.
270 * Access is covered by &drm_device.object_name_lock. This is used by
271 * the GEM_FLINK and GEM_OPEN ioctls.
272 */
273 int name;
274
275 /**
276 * @dma_buf:
277 *
278 * dma-buf associated with this GEM object.
279 *
280 * Pointer to the dma-buf associated with this gem object (either
281 * through importing or exporting). We break the resulting reference
282 * loop when the last gem handle for this object is released.
283 *
284 * Protected by &drm_device.object_name_lock.
285 */
286 struct dma_buf *dma_buf;
287
288 /**
289 * @import_attach:
290 *
291 * dma-buf attachment backing this object.
292 *
293 * Any foreign dma_buf imported as a gem object has this set to the
294 * attachment point for the device. This is invariant over the lifetime
295 * of a gem object.
296 *
297 * The &drm_driver.gem_free_object callback is responsible for cleaning
298 * up the dma_buf attachment and references acquired at import time.
299 *
300 * Note that the drm gem/prime core does not depend upon drivers setting
301 * this field any more. So for drivers where this doesn't make sense
302 * (e.g. virtual devices or a displaylink behind an usb bus) they can
303 * simply leave it as NULL.
304 */
305 struct dma_buf_attachment *import_attach;
306
307 /**
308 * @resv:
309 *
310 * Pointer to reservation object associated with the this GEM object.
311 *
312 * Normally (@resv == &@_resv) except for imported GEM objects.
313 */
314 struct dma_resv *resv;
315
316 /**
317 * @_resv:
318 *
319 * A reservation object for this GEM object.
320 *
321 * This is unused for imported GEM objects.
322 */
323 struct dma_resv _resv;
324
325 /**
326 * @funcs:
327 *
328 * Optional GEM object functions. If this is set, it will be used instead of the
329 * corresponding &drm_driver GEM callbacks.
330 *
331 * New drivers should use this.
332 *
333 */
334 const struct drm_gem_object_funcs *funcs;
335 };
336
337 /**
338 * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
339 * @name: name for the generated structure
340 *
341 * This macro autogenerates a suitable &struct file_operations for GEM based
342 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
343 * cannot be shared between drivers, because it contains a reference to the
344 * current module using THIS_MODULE.
345 *
346 * Note that the declaration is already marked as static - if you need a
347 * non-static version of this you're probably doing it wrong and will break the
348 * THIS_MODULE reference by accident.
349 */
350 #define DEFINE_DRM_GEM_FOPS(name) \
351 static const struct file_operations name = {\
352 .owner = THIS_MODULE,\
353 .open = drm_open,\
354 .release = drm_release,\
355 .unlocked_ioctl = drm_ioctl,\
356 .compat_ioctl = drm_compat_ioctl,\
357 .poll = drm_poll,\
358 .read = drm_read,\
359 .llseek = noop_llseek,\
360 .mmap = drm_gem_mmap,\
361 }
362
363 void drm_gem_object_release(struct drm_gem_object *obj);
364 void drm_gem_object_free(struct kref *kref);
365 int drm_gem_object_init(struct drm_device *dev,
366 struct drm_gem_object *obj, size_t size);
367 void drm_gem_private_object_init(struct drm_device *dev,
368 struct drm_gem_object *obj, size_t size);
369 #ifdef __NetBSD__
370 void drm_gem_pager_reference(struct uvm_object *);
371 void drm_gem_pager_detach(struct uvm_object *);
372 int drm_gem_mmap_object(struct drm_device *, off_t, size_t, int,
373 struct uvm_object **, voff_t *, struct file *);
374 int drm_gem_or_legacy_mmap_object(struct drm_device *, off_t, size_t, int,
375 struct uvm_object **, voff_t *, struct file *);
376 #else
377 void drm_gem_vm_open(struct vm_area_struct *vma);
378 void drm_gem_vm_close(struct vm_area_struct *vma);
379 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
380 struct vm_area_struct *vma);
381 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
382 #endif
383
384 /**
385 * drm_gem_object_get - acquire a GEM buffer object reference
386 * @obj: GEM buffer object
387 *
388 * This function acquires an additional reference to @obj. It is illegal to
389 * call this without already holding a reference. No locks required.
390 */
391 static inline void drm_gem_object_get(struct drm_gem_object *obj)
392 {
393 kref_get(&obj->refcount);
394 }
395
396 /**
397 * __drm_gem_object_put - raw function to release a GEM buffer object reference
398 * @obj: GEM buffer object
399 *
400 * This function is meant to be used by drivers which are not encumbered with
401 * &drm_device.struct_mutex legacy locking and which are using the
402 * gem_free_object_unlocked callback. It avoids all the locking checks and
403 * locking overhead of drm_gem_object_put() and drm_gem_object_put_unlocked().
404 *
405 * Drivers should never call this directly in their code. Instead they should
406 * wrap it up into a ``driver_gem_object_put(struct driver_gem_object *obj)``
407 * wrapper function, and use that. Shared code should never call this, to
408 * avoid breaking drivers by accident which still depend upon
409 * &drm_device.struct_mutex locking.
410 */
411 static inline void
412 __drm_gem_object_put(struct drm_gem_object *obj)
413 {
414 kref_put(&obj->refcount, drm_gem_object_free);
415 }
416
417 void drm_gem_object_put_unlocked(struct drm_gem_object *obj);
418 void drm_gem_object_put(struct drm_gem_object *obj);
419
420 int drm_gem_handle_create(struct drm_file *file_priv,
421 struct drm_gem_object *obj,
422 u32 *handlep);
423 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
424
425
426 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
427 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
428 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
429
430 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
431 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
432 bool dirty, bool accessed);
433
434 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
435 int count, struct drm_gem_object ***objs_out);
436 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
437 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
438 bool wait_all, unsigned long timeout);
439 int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
440 struct ww_acquire_ctx *acquire_ctx);
441 void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
442 struct ww_acquire_ctx *acquire_ctx);
443 int drm_gem_fence_array_add(struct xarray *fence_array,
444 struct dma_fence *fence);
445 int drm_gem_fence_array_add_implicit(struct xarray *fence_array,
446 struct drm_gem_object *obj,
447 bool write);
448 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
449 u32 handle, u64 *offset);
450 int drm_gem_dumb_destroy(struct drm_file *file,
451 struct drm_device *dev,
452 uint32_t handle);
453
454 #endif /* __DRM_GEM_H__ */
455