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