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