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