5 * GEM Graphics Execution Manager Driver Interfaces
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
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:
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
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.
37 #include <linux/kref.h>
38 #include <linux/dma-resv.h>
39 #include <linux/list.h>
40 #include <linux/mutex.h>
42 #include <drm/drm_vma_manager.h>
45 struct drm_gem_object;
48 * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
49 * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
50 * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
52 * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
53 * and drm_show_fdinfo(). Note that an object can DRM_GEM_OBJECT_PURGEABLE if
54 * it still active or not resident, in which case drm_show_fdinfo() will not
55 * account for it as purgeable. So drivers do not need to check if the buffer
56 * is idle and resident to return this bit. (Ie. userspace can mark a buffer
57 * as purgeable even while it is still busy on the GPU.. it does not _actually_
58 * become puregeable until it becomes idle. The status gem object func does
59 * not need to consider this.)
61 enum drm_gem_object_status {
62 DRM_GEM_OBJECT_RESIDENT = BIT(0),
63 DRM_GEM_OBJECT_PURGEABLE = BIT(1),
67 * struct drm_gem_object_funcs - GEM object functions
69 struct drm_gem_object_funcs {
73 * Deconstructor for drm_gem_objects.
75 * This callback is mandatory.
77 void (*free)(struct drm_gem_object *obj);
82 * Called upon GEM handle creation.
84 * This callback is optional.
86 int (*open)(struct drm_gem_object *obj, struct drm_file *file);
91 * Called upon GEM handle release.
93 * This callback is optional.
95 void (*close)(struct drm_gem_object *obj, struct drm_file *file);
100 * If driver subclasses struct &drm_gem_object, it can implement this
101 * optional hook for printing additional driver specific info.
103 * drm_printf_indent() should be used in the callback passing it the
106 * This callback is called from drm_gem_print_info().
108 * This callback is optional.
110 void (*print_info)(struct drm_printer *p, unsigned int indent,
111 const struct drm_gem_object *obj);
116 * Export backing buffer as a &dma_buf.
117 * If this is not set drm_gem_prime_export() is used.
119 * This callback is optional.
121 struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
126 * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper.
128 * This callback is optional.
130 int (*pin)(struct drm_gem_object *obj);
135 * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
137 * This callback is optional.
139 void (*unpin)(struct drm_gem_object *obj);
144 * Returns a Scatter-Gather table representation of the buffer.
145 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
146 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
147 * in drm_gem_unmap_buf(), therefore these helpers and this callback
148 * here cannot be used for sg tables pointing at driver private memory
151 * See also drm_prime_pages_to_sg().
153 struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
158 * Returns a virtual address for the buffer. Used by the
159 * drm_gem_dmabuf_vmap() helper.
161 * This callback is optional.
163 int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
168 * Releases the address previously returned by @vmap. Used by the
169 * drm_gem_dmabuf_vunmap() helper.
171 * This callback is optional.
173 void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
178 * Handle mmap() of the gem object, setup vma accordingly.
180 * This callback is optional.
182 * The callback is used by both drm_gem_mmap_obj() and
183 * drm_gem_prime_mmap(). When @mmap is present @vm_ops is not
184 * used, the @mmap callback must set vma->vm_ops instead.
186 int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
191 * Evicts gem object out from memory. Used by the drm_gem_object_evict()
192 * helper. Returns 0 on success, -errno otherwise.
194 * This callback is optional.
196 int (*evict)(struct drm_gem_object *obj);
201 * The optional status callback can return additional object state
202 * which determines which stats the object is counted against. The
203 * callback is called under table_lock. Racing against object status
204 * change is "harmless", and the callback can expect to not race
205 * against object destruction.
207 * Called by drm_show_memory_stats().
209 enum drm_gem_object_status (*status)(struct drm_gem_object *obj);
214 * Return resident size of the object in physical memory.
216 * Called by drm_show_memory_stats().
218 size_t (*rss)(struct drm_gem_object *obj);
223 * Virtual memory operations used with mmap.
225 * This is optional but necessary for mmap support.
227 const struct vm_operations_struct *vm_ops;
231 * struct drm_gem_lru - A simple LRU helper
233 * A helper for tracking GEM objects in a given state, to aid in
234 * driver's shrinker implementation. Tracks the count of pages
235 * for lockless &shrinker.count_objects, and provides
236 * &drm_gem_lru_scan for driver's &shrinker.scan_objects
243 * Lock protecting movement of GEM objects between LRUs. All
244 * LRUs that the object can move between should be protected
252 * The total number of backing pages of the GEM objects in
262 struct list_head list;
266 * struct drm_gem_object - GEM buffer object
268 * This structure defines the generic parts for GEM buffer objects, which are
269 * mostly around handling mmap and userspace handles.
271 * Buffer objects are often abbreviated to BO.
273 struct drm_gem_object {
277 * Reference count of this object
279 * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
280 * or drm_gem_object_put() to release a reference to a GEM
283 struct kref refcount;
288 * This is the GEM file_priv handle count of this object.
290 * Each handle also holds a reference. Note that when the handle_count
291 * drops to 0 any global names (e.g. the id in the flink namespace) will
294 * Protected by &drm_device.object_name_lock.
296 unsigned handle_count;
299 * @dev: DRM dev this object belongs to.
301 struct drm_device *dev;
306 * SHMEM file node used as backing storage for swappable buffer objects.
307 * GEM also supports driver private objects with driver-specific backing
308 * storage (contiguous DMA memory, special reserved blocks). In this
309 * case @filp is NULL.
316 * Mapping info for this object to support mmap. Drivers are supposed to
317 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
318 * offset itself can be retrieved using drm_vma_node_offset_addr().
320 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
321 * that userspace is allowed to access the object.
323 struct drm_vma_offset_node vma_node;
328 * Size of the object, in bytes. Immutable over the object's
336 * Global name for this object, starts at 1. 0 means unnamed.
337 * Access is covered by &drm_device.object_name_lock. This is used by
338 * the GEM_FLINK and GEM_OPEN ioctls.
345 * dma-buf associated with this GEM object.
347 * Pointer to the dma-buf associated with this gem object (either
348 * through importing or exporting). We break the resulting reference
349 * loop when the last gem handle for this object is released.
351 * Protected by &drm_device.object_name_lock.
353 struct dma_buf *dma_buf;
358 * dma-buf attachment backing this object.
360 * Any foreign dma_buf imported as a gem object has this set to the
361 * attachment point for the device. This is invariant over the lifetime
364 * The &drm_gem_object_funcs.free callback is responsible for
365 * cleaning up the dma_buf attachment and references acquired at import
368 * Note that the drm gem/prime core does not depend upon drivers setting
369 * this field any more. So for drivers where this doesn't make sense
370 * (e.g. virtual devices or a displaylink behind an usb bus) they can
371 * simply leave it as NULL.
373 struct dma_buf_attachment *import_attach;
378 * Pointer to reservation object associated with the this GEM object.
380 * Normally (@resv == &@_resv) except for imported GEM objects.
382 struct dma_resv *resv;
387 * A reservation object for this GEM object.
389 * This is unused for imported GEM objects.
391 struct dma_resv _resv;
396 * Provides the list of GPU VAs attached to this GEM object.
398 * Drivers should lock list accesses with the GEMs &dma_resv lock
399 * (&drm_gem_object.resv) or a custom lock if one is provided.
402 struct list_head list;
404 #ifdef CONFIG_LOCKDEP
405 struct lockdep_map *lock_dep_map;
412 * Optional GEM object functions. If this is set, it will be used instead of the
413 * corresponding &drm_driver GEM callbacks.
415 * New drivers should use this.
418 const struct drm_gem_object_funcs *funcs;
423 * List node in a &drm_gem_lru.
425 struct list_head lru_node;
430 * The current LRU list that the GEM object is on.
432 struct drm_gem_lru *lru;
436 * DRM_GEM_FOPS - Default drm GEM file operations
438 * This macro provides a shorthand for setting the GEM file ops in the
439 * &file_operations structure. If all you need are the default ops, use
440 * DEFINE_DRM_GEM_FOPS instead.
442 #define DRM_GEM_FOPS \
444 .release = drm_release,\
445 .unlocked_ioctl = drm_ioctl,\
446 .compat_ioctl = drm_compat_ioctl,\
449 .llseek = noop_llseek,\
453 * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
454 * @name: name for the generated structure
456 * This macro autogenerates a suitable &struct file_operations for GEM based
457 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
458 * cannot be shared between drivers, because it contains a reference to the
459 * current module using THIS_MODULE.
461 * Note that the declaration is already marked as static - if you need a
462 * non-static version of this you're probably doing it wrong and will break the
463 * THIS_MODULE reference by accident.
465 #define DEFINE_DRM_GEM_FOPS(name) \
466 static const struct file_operations name = {\
467 .owner = THIS_MODULE,\
471 void drm_gem_object_release(struct drm_gem_object *obj);
472 void drm_gem_object_free(struct kref *kref);
473 int drm_gem_object_init(struct drm_device *dev,
474 struct drm_gem_object *obj, size_t size);
475 void drm_gem_private_object_init(struct drm_device *dev,
476 struct drm_gem_object *obj, size_t size);
477 void drm_gem_private_object_fini(struct drm_gem_object *obj);
478 void drm_gem_vm_open(struct vm_area_struct *vma);
479 void drm_gem_vm_close(struct vm_area_struct *vma);
480 int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
481 struct vm_area_struct *vma);
482 int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
485 * drm_gem_object_get - acquire a GEM buffer object reference
486 * @obj: GEM buffer object
488 * This function acquires an additional reference to @obj. It is illegal to
489 * call this without already holding a reference. No locks required.
491 static inline void drm_gem_object_get(struct drm_gem_object *obj)
493 kref_get(&obj->refcount);
496 __attribute__((nonnull))
498 __drm_gem_object_put(struct drm_gem_object *obj)
500 kref_put(&obj->refcount, drm_gem_object_free);
504 * drm_gem_object_put - drop a GEM buffer object reference
505 * @obj: GEM buffer object
507 * This releases a reference to @obj.
510 drm_gem_object_put(struct drm_gem_object *obj)
513 __drm_gem_object_put(obj);
516 int drm_gem_handle_create(struct drm_file *file_priv,
517 struct drm_gem_object *obj,
519 int drm_gem_handle_delete(struct drm_file *filp, u32 handle);
522 void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
523 int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
524 int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
526 struct page **drm_gem_get_pages(struct drm_gem_object *obj);
527 void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
528 bool dirty, bool accessed);
530 void drm_gem_lock(struct drm_gem_object *obj);
531 void drm_gem_unlock(struct drm_gem_object *obj);
533 int drm_gem_vmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
534 void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
536 int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
537 int count, struct drm_gem_object ***objs_out);
538 struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
539 long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
540 bool wait_all, unsigned long timeout);
541 int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
542 struct ww_acquire_ctx *acquire_ctx);
543 void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
544 struct ww_acquire_ctx *acquire_ctx);
545 int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
546 u32 handle, u64 *offset);
548 void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
549 void drm_gem_lru_remove(struct drm_gem_object *obj);
550 void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
551 void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
552 unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru,
553 unsigned int nr_to_scan,
554 unsigned long *remaining,
555 bool (*shrink)(struct drm_gem_object *obj));
557 int drm_gem_evict(struct drm_gem_object *obj);
560 * drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats
562 * This helper should only be used for fdinfo shared memory stats to determine
563 * if a GEM object is shared.
565 * @obj: obj in question
567 static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)
569 return (obj->handle_count > 1) || obj->dma_buf;
572 #ifdef CONFIG_LOCKDEP
574 * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list.
575 * @obj: the &drm_gem_object
576 * @lock: the lock used to protect the gpuva list. The locking primitive
577 * must contain a dep_map field.
579 * Call this if you're not proctecting access to the gpuva list with the
580 * dma-resv lock, but with a custom lock.
582 #define drm_gem_gpuva_set_lock(obj, lock) \
583 if (!WARN((obj)->gpuva.lock_dep_map, \
584 "GEM GPUVA lock should be set only once.")) \
585 (obj)->gpuva.lock_dep_map = &(lock)->dep_map
586 #define drm_gem_gpuva_assert_lock_held(obj) \
587 lockdep_assert((obj)->gpuva.lock_dep_map ? \
588 lock_is_held((obj)->gpuva.lock_dep_map) : \
589 dma_resv_held((obj)->resv))
591 #define drm_gem_gpuva_set_lock(obj, lock) do {} while (0)
592 #define drm_gem_gpuva_assert_lock_held(obj) do {} while (0)
596 * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object
597 * @obj: the &drm_gem_object
599 * This initializes the &drm_gem_object's &drm_gpuvm_bo list.
601 * Calling this function is only necessary for drivers intending to support the
602 * &drm_driver_feature DRIVER_GEM_GPUVA.
604 * See also drm_gem_gpuva_set_lock().
606 static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)
608 INIT_LIST_HEAD(&obj->gpuva.list);
612 * drm_gem_for_each_gpuvm_bo() - iterator to walk over a list of &drm_gpuvm_bo
613 * @entry__: &drm_gpuvm_bo structure to assign to in each iteration step
614 * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
616 * This iterator walks over all &drm_gpuvm_bo structures associated with the
619 #define drm_gem_for_each_gpuvm_bo(entry__, obj__) \
620 list_for_each_entry(entry__, &(obj__)->gpuva.list, list.entry.gem)
623 * drm_gem_for_each_gpuvm_bo_safe() - iterator to safely walk over a list of
625 * @entry__: &drm_gpuvm_bostructure to assign to in each iteration step
626 * @next__: &next &drm_gpuvm_bo to store the next step
627 * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
629 * This iterator walks over all &drm_gpuvm_bo structures associated with the
630 * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence
631 * it is save against removal of elements.
633 #define drm_gem_for_each_gpuvm_bo_safe(entry__, next__, obj__) \
634 list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, list.entry.gem)
636 #endif /* __DRM_GEM_H__ */