xref: /linux/include/drm/drm_gem.h (revision 2c142b63c8ee982cdfdba49a616027c266294838)
1 #ifndef __DRM_GEM_H__
2 #define __DRM_GEM_H__
3 
4 /*
5  * GEM Graphics Execution Manager Driver Interfaces
6  *
7  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
8  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
9  * Copyright (c) 2009-2010, Code Aurora Forum.
10  * All rights reserved.
11  * Copyright © 2014 Intel Corporation
12  *   Daniel Vetter <daniel.vetter@ffwll.ch>
13  *
14  * Author: Rickard E. (Rik) Faith <faith@valinux.com>
15  * Author: Gareth Hughes <gareth@valinux.com>
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining a
18  * copy of this software and associated documentation files (the "Software"),
19  * to deal in the Software without restriction, including without limitation
20  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21  * and/or sell copies of the Software, and to permit persons to whom the
22  * Software is furnished to do so, subject to the following conditions:
23  *
24  * The above copyright notice and this permission notice (including the next
25  * paragraph) shall be included in all copies or substantial portions of the
26  * Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
31  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34  * OTHER DEALINGS IN THE SOFTWARE.
35  */
36 
37 #include <linux/kref.h>
38 #include <linux/dma-buf.h>
39 #include <linux/dma-resv.h>
40 #include <linux/list.h>
41 #include <linux/mutex.h>
42 
43 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
44 #include <drm/drm_device.h>
45 #endif
46 #include <drm/drm_vma_manager.h>
47 
48 struct iosys_map;
49 struct drm_gem_object;
50 
51 /**
52  * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
53  * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
54  * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
55  * @DRM_GEM_OBJECT_ACTIVE: object is currently used by an active submission
56  *
57  * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
58  * and drm_show_fdinfo().  Note that an object can report DRM_GEM_OBJECT_PURGEABLE
59  * and be active or not resident, in which case drm_show_fdinfo() will not
60  * account for it as purgeable.  So drivers do not need to check if the buffer
61  * is idle and resident to return this bit, i.e. userspace can mark a buffer as
62  * purgeable even while it is still busy on the GPU. It will not get reported in
63  * the puregeable stats until it becomes idle.  The status gem object func does
64  * not need to consider this.
65  */
66 enum drm_gem_object_status {
67 	DRM_GEM_OBJECT_RESIDENT  = BIT(0),
68 	DRM_GEM_OBJECT_PURGEABLE = BIT(1),
69 	DRM_GEM_OBJECT_ACTIVE    = BIT(2),
70 };
71 
72 /**
73  * struct drm_gem_object_funcs - GEM object functions
74  */
75 struct drm_gem_object_funcs {
76 	/**
77 	 * @free:
78 	 *
79 	 * Deconstructor for drm_gem_objects.
80 	 *
81 	 * This callback is mandatory.
82 	 */
83 	void (*free)(struct drm_gem_object *obj);
84 
85 	/**
86 	 * @open:
87 	 *
88 	 * Called upon GEM handle creation.
89 	 *
90 	 * This callback is optional.
91 	 */
92 	int (*open)(struct drm_gem_object *obj, struct drm_file *file);
93 
94 	/**
95 	 * @close:
96 	 *
97 	 * Called upon GEM handle release.
98 	 *
99 	 * This callback is optional.
100 	 */
101 	void (*close)(struct drm_gem_object *obj, struct drm_file *file);
102 
103 	/**
104 	 * @print_info:
105 	 *
106 	 * If driver subclasses struct &drm_gem_object, it can implement this
107 	 * optional hook for printing additional driver specific info.
108 	 *
109 	 * drm_printf_indent() should be used in the callback passing it the
110 	 * indent argument.
111 	 *
112 	 * This callback is called from drm_gem_print_info().
113 	 *
114 	 * This callback is optional.
115 	 */
116 	void (*print_info)(struct drm_printer *p, unsigned int indent,
117 			   const struct drm_gem_object *obj);
118 
119 	/**
120 	 * @export:
121 	 *
122 	 * Export backing buffer as a &dma_buf.
123 	 * If this is not set drm_gem_prime_export() is used.
124 	 *
125 	 * This callback is optional.
126 	 */
127 	struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
128 
129 	/**
130 	 * @pin:
131 	 *
132 	 * Pin backing buffer in memory, such that dma-buf importers can
133 	 * access it. Used by the drm_gem_map_attach() helper.
134 	 *
135 	 * This callback is optional.
136 	 */
137 	int (*pin)(struct drm_gem_object *obj);
138 
139 	/**
140 	 * @unpin:
141 	 *
142 	 * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
143 	 *
144 	 * This callback is optional.
145 	 */
146 	void (*unpin)(struct drm_gem_object *obj);
147 
148 	/**
149 	 * @get_sg_table:
150 	 *
151 	 * Returns a Scatter-Gather table representation of the buffer.
152 	 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
153 	 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
154 	 * in drm_gem_unmap_buf(), therefore these helpers and this callback
155 	 * here cannot be used for sg tables pointing at driver private memory
156 	 * ranges.
157 	 *
158 	 * See also drm_prime_pages_to_sg().
159 	 */
160 	struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
161 
162 	/**
163 	 * @vmap:
164 	 *
165 	 * Returns a virtual address for the buffer. Used by the
166 	 * drm_gem_dmabuf_vmap() helper. Called with a held GEM reservation
167 	 * lock.
168 	 *
169 	 * This callback is optional.
170 	 */
171 	int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
172 
173 	/**
174 	 * @vunmap:
175 	 *
176 	 * Releases the address previously returned by @vmap. Used by the
177 	 * drm_gem_dmabuf_vunmap() helper. Called with a held GEM reservation
178 	 * lock.
179 	 *
180 	 * This callback is optional.
181 	 */
182 	void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
183 
184 	/**
185 	 * @mmap:
186 	 *
187 	 * Handle mmap() of the gem object, setup vma accordingly.
188 	 *
189 	 * This callback is optional.
190 	 *
191 	 * The callback is used by both drm_gem_mmap_obj() and
192 	 * drm_gem_prime_mmap().  When @mmap is present @vm_ops is not
193 	 * used, the @mmap callback must set vma->vm_ops instead.
194 	 */
195 	int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
196 
197 	/**
198 	 * @evict:
199 	 *
200 	 * Evicts gem object out from memory. Used by the drm_gem_object_evict()
201 	 * helper. Returns 0 on success, -errno otherwise. Called with a held
202 	 * GEM reservation lock.
203 	 *
204 	 * This callback is optional.
205 	 */
206 	int (*evict)(struct drm_gem_object *obj);
207 
208 	/**
209 	 * @status:
210 	 *
211 	 * The optional status callback can return additional object state
212 	 * which determines which stats the object is counted against.  The
213 	 * callback is called under table_lock.  Racing against object status
214 	 * change is "harmless", and the callback can expect to not race
215 	 * against object destruction.
216 	 *
217 	 * Called by drm_show_memory_stats().
218 	 */
219 	enum drm_gem_object_status (*status)(struct drm_gem_object *obj);
220 
221 	/**
222 	 * @rss:
223 	 *
224 	 * Return resident size of the object in physical memory.
225 	 *
226 	 * Called by drm_show_memory_stats().
227 	 */
228 	size_t (*rss)(struct drm_gem_object *obj);
229 
230 	/**
231 	 * @vm_ops:
232 	 *
233 	 * Virtual memory operations used with mmap.
234 	 *
235 	 * This is optional but necessary for mmap support.
236 	 */
237 	const struct vm_operations_struct *vm_ops;
238 };
239 
240 /**
241  * struct drm_gem_lru - A simple LRU helper
242  *
243  * A helper for tracking GEM objects in a given state, to aid in
244  * driver's shrinker implementation.  Tracks the count of pages
245  * for lockless &shrinker.count_objects, and provides
246  * &drm_gem_lru_scan for driver's &shrinker.scan_objects
247  * implementation.
248  *
249  * Any access to this kind of object must be done with
250  * drm_device::gem_lru_mutex held.
251  */
252 struct drm_gem_lru {
253 	/**
254 	 * @count:
255 	 *
256 	 * The total number of backing pages of the GEM objects in
257 	 * this LRU.
258 	 */
259 	long count;
260 
261 	/**
262 	 * @list:
263 	 *
264 	 * The LRU list.
265 	 */
266 	struct list_head list;
267 };
268 
269 /**
270  * struct drm_gem_object - GEM buffer object
271  *
272  * This structure defines the generic parts for GEM buffer objects, which are
273  * mostly around handling mmap and userspace handles.
274  *
275  * Buffer objects are often abbreviated to BO.
276  */
277 struct drm_gem_object {
278 	/**
279 	 * @refcount:
280 	 *
281 	 * Reference count of this object
282 	 *
283 	 * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
284 	 * or drm_gem_object_put() to release a reference to a GEM
285 	 * buffer object.
286 	 */
287 	struct kref refcount;
288 
289 	/**
290 	 * @handle_count:
291 	 *
292 	 * This is the GEM file_priv handle count of this object.
293 	 *
294 	 * Each handle also holds a reference. Note that when the handle_count
295 	 * drops to 0 any global names (e.g. the id in the flink namespace) will
296 	 * be cleared.
297 	 *
298 	 * Protected by &drm_device.object_name_lock.
299 	 */
300 	unsigned handle_count;
301 
302 	/**
303 	 * @dev: DRM dev this object belongs to.
304 	 */
305 	struct drm_device *dev;
306 
307 	/**
308 	 * @filp:
309 	 *
310 	 * SHMEM file node used as backing storage for swappable buffer objects.
311 	 * GEM also supports driver private objects with driver-specific backing
312 	 * storage (contiguous DMA memory, special reserved blocks). In this
313 	 * case @filp is NULL.
314 	 */
315 	struct file *filp;
316 
317 	/**
318 	 * @vma_node:
319 	 *
320 	 * Mapping info for this object to support mmap. Drivers are supposed to
321 	 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
322 	 * offset itself can be retrieved using drm_vma_node_offset_addr().
323 	 *
324 	 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
325 	 * that userspace is allowed to access the object.
326 	 */
327 	struct drm_vma_offset_node vma_node;
328 
329 	/**
330 	 * @size:
331 	 *
332 	 * Size of the object, in bytes.  Immutable over the object's
333 	 * lifetime.
334 	 */
335 	size_t size;
336 
337 	/**
338 	 * @name:
339 	 *
340 	 * Global name for this object, starts at 1. 0 means unnamed.
341 	 * Access is covered by &drm_device.object_name_lock. This is used by
342 	 * the GEM_FLINK and GEM_OPEN ioctls.
343 	 */
344 	int name;
345 
346 	/**
347 	 * @dma_buf:
348 	 *
349 	 * dma-buf associated with this GEM object.
350 	 *
351 	 * Pointer to the dma-buf associated with this gem object (either
352 	 * through importing or exporting). We break the resulting reference
353 	 * loop when the last gem handle for this object is released.
354 	 *
355 	 * Protected by &drm_device.object_name_lock.
356 	 */
357 	struct dma_buf *dma_buf;
358 
359 	/**
360 	 * @import_attach:
361 	 *
362 	 * dma-buf attachment backing this object.
363 	 *
364 	 * Any foreign dma_buf imported as a gem object has this set to the
365 	 * attachment point for the device. This is invariant over the lifetime
366 	 * of a gem object.
367 	 *
368 	 * The &drm_gem_object_funcs.free callback is responsible for
369 	 * cleaning up the dma_buf attachment and references acquired at import
370 	 * time.
371 	 *
372 	 * Note that the drm gem/prime core does not depend upon drivers setting
373 	 * this field any more. So for drivers where this doesn't make sense
374 	 * (e.g. virtual devices or a displaylink behind an usb bus) they can
375 	 * simply leave it as NULL.
376 	 */
377 	struct dma_buf_attachment *import_attach;
378 
379 	/**
380 	 * @resv:
381 	 *
382 	 * Pointer to reservation object associated with the this GEM object.
383 	 *
384 	 * Normally (@resv == &@_resv) except for imported GEM objects.
385 	 */
386 	struct dma_resv *resv;
387 
388 	/**
389 	 * @_resv:
390 	 *
391 	 * A reservation object for this GEM object.
392 	 *
393 	 * This is unused for imported GEM objects.
394 	 */
395 	struct dma_resv _resv;
396 
397 	/**
398 	 * @gpuva: Fields used by GPUVM to manage mappings pointing to this GEM object.
399 	 *
400 	 * When DRM_GPUVM_IMMEDIATE_MODE is set, this list is protected by the
401 	 * mutex. Otherwise, the list is protected by the GEMs &dma_resv lock.
402 	 *
403 	 * Note that all entries in this list must agree on whether
404 	 * DRM_GPUVM_IMMEDIATE_MODE is set.
405 	 */
406 	struct {
407 		/**
408 		 * @gpuva.list: list of GPUVM mappings attached to this GEM object.
409 		 *
410 		 * Drivers should lock list accesses with either the GEMs
411 		 * &dma_resv lock (&drm_gem_object.resv) or the
412 		 * &drm_gem_object.gpuva.lock mutex.
413 		 */
414 		struct list_head list;
415 
416 		/**
417 		 * @gpuva.lock: lock protecting access to &drm_gem_object.gpuva.list
418 		 * when DRM_GPUVM_IMMEDIATE_MODE is used.
419 		 *
420 		 * Only used when DRM_GPUVM_IMMEDIATE_MODE is set. It should be
421 		 * safe to take this mutex during the fence signalling path, so
422 		 * do not allocate memory while holding this lock. Otherwise,
423 		 * the &dma_resv lock should be used.
424 		 */
425 		struct mutex lock;
426 	} gpuva;
427 
428 	/**
429 	 * @funcs:
430 	 *
431 	 * Optional GEM object functions. If this is set, it will be used instead of the
432 	 * corresponding &drm_driver GEM callbacks.
433 	 *
434 	 * New drivers should use this.
435 	 *
436 	 */
437 	const struct drm_gem_object_funcs *funcs;
438 
439 	/**
440 	 * @lru_node:
441 	 *
442 	 * List node in a &drm_gem_lru.
443 	 */
444 	struct list_head lru_node;
445 
446 	/**
447 	 * @lru:
448 	 *
449 	 * The current LRU list that the GEM object is on.
450 	 *
451 	 * Access to this field must be done with drm_device::gem_lru_mutex
452 	 * held.
453 	 */
454 	struct drm_gem_lru *lru;
455 };
456 
457 /**
458  * DRM_GEM_FOPS - Default drm GEM file operations
459  *
460  * This macro provides a shorthand for setting the GEM file ops in the
461  * &file_operations structure.  If all you need are the default ops, use
462  * DEFINE_DRM_GEM_FOPS instead.
463  */
464 #define DRM_GEM_FOPS \
465 	.open		= drm_open,\
466 	.release	= drm_release,\
467 	.unlocked_ioctl	= drm_ioctl,\
468 	.compat_ioctl	= drm_compat_ioctl,\
469 	.poll		= drm_poll,\
470 	.read		= drm_read,\
471 	.llseek		= noop_llseek,\
472 	.get_unmapped_area	= drm_gem_get_unmapped_area,\
473 	.mmap		= drm_gem_mmap, \
474 	.fop_flags	= FOP_UNSIGNED_OFFSET
475 
476 /**
477  * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
478  * @name: name for the generated structure
479  *
480  * This macro autogenerates a suitable &struct file_operations for GEM based
481  * drivers, which can be assigned to &drm_driver.fops. Note that this structure
482  * cannot be shared between drivers, because it contains a reference to the
483  * current module using THIS_MODULE.
484  *
485  * Note that the declaration is already marked as static - if you need a
486  * non-static version of this you're probably doing it wrong and will break the
487  * THIS_MODULE reference by accident.
488  */
489 #define DEFINE_DRM_GEM_FOPS(name) \
490 	static const struct file_operations name = {\
491 		.owner		= THIS_MODULE,\
492 		DRM_GEM_FOPS,\
493 	}
494 
495 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
496 int drm_gem_huge_mnt_create(struct drm_device *dev, const char *value);
497 #else
drm_gem_huge_mnt_create(struct drm_device * dev,const char * value)498 static inline int drm_gem_huge_mnt_create(struct drm_device *dev,
499 					  const char *value)
500 {
501 	return 0;
502 }
503 #endif
504 
505 /**
506  * drm_gem_get_huge_mnt - Get the huge tmpfs mountpoint used by a DRM device
507  * @dev: DRM device
508  *
509  * This function gets the huge tmpfs mountpoint used by DRM device @dev. A huge
510  * tmpfs mountpoint is used instead of `shm_mnt` after a successful call to
511  * drm_gem_huge_mnt_create() when CONFIG_TRANSPARENT_HUGEPAGE is enabled.
512  *
513  * Returns:
514  * The huge tmpfs mountpoint in use, NULL otherwise.
515  */
drm_gem_get_huge_mnt(struct drm_device * dev)516 static inline struct vfsmount *drm_gem_get_huge_mnt(struct drm_device *dev)
517 {
518 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
519 	return dev->huge_mnt;
520 #else
521 	return NULL;
522 #endif
523 }
524 
525 void drm_gem_object_release(struct drm_gem_object *obj);
526 void drm_gem_object_free(struct kref *kref);
527 int drm_gem_object_init(struct drm_device *dev,
528 			struct drm_gem_object *obj, size_t size);
529 void drm_gem_private_object_init(struct drm_device *dev,
530 				 struct drm_gem_object *obj, size_t size);
531 void drm_gem_private_object_fini(struct drm_gem_object *obj);
532 void drm_gem_vm_open(struct vm_area_struct *vma);
533 void drm_gem_vm_close(struct vm_area_struct *vma);
534 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
535 		     struct vm_area_struct *vma);
536 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
537 
538 #ifdef CONFIG_MMU
539 unsigned long drm_gem_get_unmapped_area(struct file *filp, unsigned long uaddr,
540 					unsigned long len, unsigned long pgoff,
541 					unsigned long flags);
542 #else
543 #define drm_gem_get_unmapped_area NULL
544 #endif
545 
546 /**
547  * drm_gem_object_get - acquire a GEM buffer object reference
548  * @obj: GEM buffer object
549  *
550  * This function acquires an additional reference to @obj. It is illegal to
551  * call this without already holding a reference. No locks required.
552  */
drm_gem_object_get(struct drm_gem_object * obj)553 static inline void drm_gem_object_get(struct drm_gem_object *obj)
554 {
555 	kref_get(&obj->refcount);
556 }
557 
558 __attribute__((nonnull))
559 static inline void
__drm_gem_object_put(struct drm_gem_object * obj)560 __drm_gem_object_put(struct drm_gem_object *obj)
561 {
562 	kref_put(&obj->refcount, drm_gem_object_free);
563 }
564 
565 /**
566  * drm_gem_object_put - drop a GEM buffer object reference
567  * @obj: GEM buffer object
568  *
569  * This releases a reference to @obj.
570  */
571 static inline void
drm_gem_object_put(struct drm_gem_object * obj)572 drm_gem_object_put(struct drm_gem_object *obj)
573 {
574 	if (obj)
575 		__drm_gem_object_put(obj);
576 }
577 
578 int drm_gem_handle_create(struct drm_file *file_priv,
579 			  struct drm_gem_object *obj,
580 			  u32 *handlep);
581 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
582 
583 
584 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
585 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
586 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
587 
588 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
589 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
590 		bool dirty, bool accessed);
591 
592 void drm_gem_lock(struct drm_gem_object *obj);
593 void drm_gem_unlock(struct drm_gem_object *obj);
594 
595 int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map);
596 void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);
597 
598 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
599 			   int count, struct drm_gem_object ***objs_out);
600 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
601 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
602 				    bool wait_all, unsigned long timeout);
603 int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
604 			      struct ww_acquire_ctx *acquire_ctx);
605 void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
606 				 struct ww_acquire_ctx *acquire_ctx);
607 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
608 			    u32 handle, u64 *offset);
609 
610 void drm_gem_lru_init(struct drm_gem_lru *lru);
611 void drm_gem_lru_remove(struct drm_gem_object *obj);
612 void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
613 void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
614 unsigned long
615 drm_gem_lru_scan(struct drm_device *dev,
616 		 struct drm_gem_lru *lru,
617 		 unsigned int nr_to_scan,
618 		 unsigned long *remaining,
619 		 bool (*shrink)(struct drm_gem_object *obj, struct ww_acquire_ctx *ticket),
620 		 struct ww_acquire_ctx *ticket);
621 
622 int drm_gem_evict_locked(struct drm_gem_object *obj);
623 
624 /**
625  * drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats
626  *
627  * This helper should only be used for fdinfo shared memory stats to determine
628  * if a GEM object is shared.
629  *
630  * @obj: obj in question
631  */
drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object * obj)632 static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)
633 {
634 	return (obj->handle_count > 1) || obj->dma_buf;
635 }
636 
637 /**
638  * drm_gem_is_imported() - Tests if GEM object's buffer has been imported
639  * @obj: the GEM object
640  *
641  * Returns:
642  * True if the GEM object's buffer has been imported, false otherwise
643  */
drm_gem_is_imported(const struct drm_gem_object * obj)644 static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)
645 {
646 	return !!obj->import_attach;
647 }
648 
649 #ifdef CONFIG_LOCKDEP
650 #define drm_gem_gpuva_assert_lock_held(gpuvm, obj) \
651 	lockdep_assert(drm_gpuvm_immediate_mode(gpuvm) ? \
652 		       lockdep_is_held(&(obj)->gpuva.lock) : \
653 		       dma_resv_held((obj)->resv))
654 #else
655 #define drm_gem_gpuva_assert_lock_held(gpuvm, obj) do {} while (0)
656 #endif
657 
658 /**
659  * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object
660  * @obj: the &drm_gem_object
661  *
662  * This initializes the &drm_gem_object's &drm_gpuvm_bo list.
663  *
664  * Calling this function is only necessary for drivers intending to support the
665  * &drm_driver_feature DRIVER_GEM_GPUVA.
666  *
667  * See also drm_gem_gpuva_set_lock().
668  */
drm_gem_gpuva_init(struct drm_gem_object * obj)669 static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)
670 {
671 	INIT_LIST_HEAD(&obj->gpuva.list);
672 }
673 
674 /**
675  * drm_gem_for_each_gpuvm_bo() - iterator to walk over a list of &drm_gpuvm_bo
676  * @entry__: &drm_gpuvm_bo structure to assign to in each iteration step
677  * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
678  *
679  * This iterator walks over all &drm_gpuvm_bo structures associated with the
680  * &drm_gem_object.
681  */
682 #define drm_gem_for_each_gpuvm_bo(entry__, obj__) \
683 	list_for_each_entry(entry__, &(obj__)->gpuva.list, list.entry.gem)
684 
685 /**
686  * drm_gem_for_each_gpuvm_bo_safe() - iterator to safely walk over a list of
687  * &drm_gpuvm_bo
688  * @entry__: &drm_gpuvm_bostructure to assign to in each iteration step
689  * @next__: &next &drm_gpuvm_bo to store the next step
690  * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
691  *
692  * This iterator walks over all &drm_gpuvm_bo structures associated with the
693  * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence
694  * it is save against removal of elements.
695  */
696 #define drm_gem_for_each_gpuvm_bo_safe(entry__, next__, obj__) \
697 	list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, list.entry.gem)
698 
699 #endif /* __DRM_GEM_H__ */
700