162306a36Sopenharmony_ci#ifndef __DRM_GEM_H__
262306a36Sopenharmony_ci#define __DRM_GEM_H__
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci/*
562306a36Sopenharmony_ci * GEM Graphics Execution Manager Driver Interfaces
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
862306a36Sopenharmony_ci * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
962306a36Sopenharmony_ci * Copyright (c) 2009-2010, Code Aurora Forum.
1062306a36Sopenharmony_ci * All rights reserved.
1162306a36Sopenharmony_ci * Copyright © 2014 Intel Corporation
1262306a36Sopenharmony_ci *   Daniel Vetter <daniel.vetter@ffwll.ch>
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * Author: Rickard E. (Rik) Faith <faith@valinux.com>
1562306a36Sopenharmony_ci * Author: Gareth Hughes <gareth@valinux.com>
1662306a36Sopenharmony_ci *
1762306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
1862306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
1962306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
2062306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
2162306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
2262306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
2362306a36Sopenharmony_ci *
2462306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the next
2562306a36Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
2662306a36Sopenharmony_ci * Software.
2762306a36Sopenharmony_ci *
2862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2962306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3062306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
3162306a36Sopenharmony_ci * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
3262306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
3362306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
3462306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
3562306a36Sopenharmony_ci */
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci#include <linux/kref.h>
3862306a36Sopenharmony_ci#include <linux/dma-resv.h>
3962306a36Sopenharmony_ci#include <linux/list.h>
4062306a36Sopenharmony_ci#include <linux/mutex.h>
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#include <drm/drm_vma_manager.h>
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cistruct iosys_map;
4562306a36Sopenharmony_cistruct drm_gem_object;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/**
4862306a36Sopenharmony_ci * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
4962306a36Sopenharmony_ci * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
5062306a36Sopenharmony_ci * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
5162306a36Sopenharmony_ci *
5262306a36Sopenharmony_ci * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
5362306a36Sopenharmony_ci * and drm_show_fdinfo().  Note that an object can DRM_GEM_OBJECT_PURGEABLE if
5462306a36Sopenharmony_ci * it still active or not resident, in which case drm_show_fdinfo() will not
5562306a36Sopenharmony_ci * account for it as purgeable.  So drivers do not need to check if the buffer
5662306a36Sopenharmony_ci * is idle and resident to return this bit.  (Ie. userspace can mark a buffer
5762306a36Sopenharmony_ci * as purgeable even while it is still busy on the GPU.. it does not _actually_
5862306a36Sopenharmony_ci * become puregeable until it becomes idle.  The status gem object func does
5962306a36Sopenharmony_ci * not need to consider this.)
6062306a36Sopenharmony_ci */
6162306a36Sopenharmony_cienum drm_gem_object_status {
6262306a36Sopenharmony_ci	DRM_GEM_OBJECT_RESIDENT  = BIT(0),
6362306a36Sopenharmony_ci	DRM_GEM_OBJECT_PURGEABLE = BIT(1),
6462306a36Sopenharmony_ci};
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci/**
6762306a36Sopenharmony_ci * struct drm_gem_object_funcs - GEM object functions
6862306a36Sopenharmony_ci */
6962306a36Sopenharmony_cistruct drm_gem_object_funcs {
7062306a36Sopenharmony_ci	/**
7162306a36Sopenharmony_ci	 * @free:
7262306a36Sopenharmony_ci	 *
7362306a36Sopenharmony_ci	 * Deconstructor for drm_gem_objects.
7462306a36Sopenharmony_ci	 *
7562306a36Sopenharmony_ci	 * This callback is mandatory.
7662306a36Sopenharmony_ci	 */
7762306a36Sopenharmony_ci	void (*free)(struct drm_gem_object *obj);
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	/**
8062306a36Sopenharmony_ci	 * @open:
8162306a36Sopenharmony_ci	 *
8262306a36Sopenharmony_ci	 * Called upon GEM handle creation.
8362306a36Sopenharmony_ci	 *
8462306a36Sopenharmony_ci	 * This callback is optional.
8562306a36Sopenharmony_ci	 */
8662306a36Sopenharmony_ci	int (*open)(struct drm_gem_object *obj, struct drm_file *file);
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci	/**
8962306a36Sopenharmony_ci	 * @close:
9062306a36Sopenharmony_ci	 *
9162306a36Sopenharmony_ci	 * Called upon GEM handle release.
9262306a36Sopenharmony_ci	 *
9362306a36Sopenharmony_ci	 * This callback is optional.
9462306a36Sopenharmony_ci	 */
9562306a36Sopenharmony_ci	void (*close)(struct drm_gem_object *obj, struct drm_file *file);
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/**
9862306a36Sopenharmony_ci	 * @print_info:
9962306a36Sopenharmony_ci	 *
10062306a36Sopenharmony_ci	 * If driver subclasses struct &drm_gem_object, it can implement this
10162306a36Sopenharmony_ci	 * optional hook for printing additional driver specific info.
10262306a36Sopenharmony_ci	 *
10362306a36Sopenharmony_ci	 * drm_printf_indent() should be used in the callback passing it the
10462306a36Sopenharmony_ci	 * indent argument.
10562306a36Sopenharmony_ci	 *
10662306a36Sopenharmony_ci	 * This callback is called from drm_gem_print_info().
10762306a36Sopenharmony_ci	 *
10862306a36Sopenharmony_ci	 * This callback is optional.
10962306a36Sopenharmony_ci	 */
11062306a36Sopenharmony_ci	void (*print_info)(struct drm_printer *p, unsigned int indent,
11162306a36Sopenharmony_ci			   const struct drm_gem_object *obj);
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	/**
11462306a36Sopenharmony_ci	 * @export:
11562306a36Sopenharmony_ci	 *
11662306a36Sopenharmony_ci	 * Export backing buffer as a &dma_buf.
11762306a36Sopenharmony_ci	 * If this is not set drm_gem_prime_export() is used.
11862306a36Sopenharmony_ci	 *
11962306a36Sopenharmony_ci	 * This callback is optional.
12062306a36Sopenharmony_ci	 */
12162306a36Sopenharmony_ci	struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci	/**
12462306a36Sopenharmony_ci	 * @pin:
12562306a36Sopenharmony_ci	 *
12662306a36Sopenharmony_ci	 * Pin backing buffer in memory. Used by the drm_gem_map_attach() helper.
12762306a36Sopenharmony_ci	 *
12862306a36Sopenharmony_ci	 * This callback is optional.
12962306a36Sopenharmony_ci	 */
13062306a36Sopenharmony_ci	int (*pin)(struct drm_gem_object *obj);
13162306a36Sopenharmony_ci
13262306a36Sopenharmony_ci	/**
13362306a36Sopenharmony_ci	 * @unpin:
13462306a36Sopenharmony_ci	 *
13562306a36Sopenharmony_ci	 * Unpin backing buffer. Used by the drm_gem_map_detach() helper.
13662306a36Sopenharmony_ci	 *
13762306a36Sopenharmony_ci	 * This callback is optional.
13862306a36Sopenharmony_ci	 */
13962306a36Sopenharmony_ci	void (*unpin)(struct drm_gem_object *obj);
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	/**
14262306a36Sopenharmony_ci	 * @get_sg_table:
14362306a36Sopenharmony_ci	 *
14462306a36Sopenharmony_ci	 * Returns a Scatter-Gather table representation of the buffer.
14562306a36Sopenharmony_ci	 * Used when exporting a buffer by the drm_gem_map_dma_buf() helper.
14662306a36Sopenharmony_ci	 * Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()
14762306a36Sopenharmony_ci	 * in drm_gem_unmap_buf(), therefore these helpers and this callback
14862306a36Sopenharmony_ci	 * here cannot be used for sg tables pointing at driver private memory
14962306a36Sopenharmony_ci	 * ranges.
15062306a36Sopenharmony_ci	 *
15162306a36Sopenharmony_ci	 * See also drm_prime_pages_to_sg().
15262306a36Sopenharmony_ci	 */
15362306a36Sopenharmony_ci	struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci	/**
15662306a36Sopenharmony_ci	 * @vmap:
15762306a36Sopenharmony_ci	 *
15862306a36Sopenharmony_ci	 * Returns a virtual address for the buffer. Used by the
15962306a36Sopenharmony_ci	 * drm_gem_dmabuf_vmap() helper.
16062306a36Sopenharmony_ci	 *
16162306a36Sopenharmony_ci	 * This callback is optional.
16262306a36Sopenharmony_ci	 */
16362306a36Sopenharmony_ci	int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);
16462306a36Sopenharmony_ci
16562306a36Sopenharmony_ci	/**
16662306a36Sopenharmony_ci	 * @vunmap:
16762306a36Sopenharmony_ci	 *
16862306a36Sopenharmony_ci	 * Releases the address previously returned by @vmap. Used by the
16962306a36Sopenharmony_ci	 * drm_gem_dmabuf_vunmap() helper.
17062306a36Sopenharmony_ci	 *
17162306a36Sopenharmony_ci	 * This callback is optional.
17262306a36Sopenharmony_ci	 */
17362306a36Sopenharmony_ci	void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	/**
17662306a36Sopenharmony_ci	 * @mmap:
17762306a36Sopenharmony_ci	 *
17862306a36Sopenharmony_ci	 * Handle mmap() of the gem object, setup vma accordingly.
17962306a36Sopenharmony_ci	 *
18062306a36Sopenharmony_ci	 * This callback is optional.
18162306a36Sopenharmony_ci	 *
18262306a36Sopenharmony_ci	 * The callback is used by both drm_gem_mmap_obj() and
18362306a36Sopenharmony_ci	 * drm_gem_prime_mmap().  When @mmap is present @vm_ops is not
18462306a36Sopenharmony_ci	 * used, the @mmap callback must set vma->vm_ops instead.
18562306a36Sopenharmony_ci	 */
18662306a36Sopenharmony_ci	int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci	/**
18962306a36Sopenharmony_ci	 * @evict:
19062306a36Sopenharmony_ci	 *
19162306a36Sopenharmony_ci	 * Evicts gem object out from memory. Used by the drm_gem_object_evict()
19262306a36Sopenharmony_ci	 * helper. Returns 0 on success, -errno otherwise.
19362306a36Sopenharmony_ci	 *
19462306a36Sopenharmony_ci	 * This callback is optional.
19562306a36Sopenharmony_ci	 */
19662306a36Sopenharmony_ci	int (*evict)(struct drm_gem_object *obj);
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci	/**
19962306a36Sopenharmony_ci	 * @status:
20062306a36Sopenharmony_ci	 *
20162306a36Sopenharmony_ci	 * The optional status callback can return additional object state
20262306a36Sopenharmony_ci	 * which determines which stats the object is counted against.  The
20362306a36Sopenharmony_ci	 * callback is called under table_lock.  Racing against object status
20462306a36Sopenharmony_ci	 * change is "harmless", and the callback can expect to not race
20562306a36Sopenharmony_ci	 * against object destruction.
20662306a36Sopenharmony_ci	 *
20762306a36Sopenharmony_ci	 * Called by drm_show_memory_stats().
20862306a36Sopenharmony_ci	 */
20962306a36Sopenharmony_ci	enum drm_gem_object_status (*status)(struct drm_gem_object *obj);
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	/**
21262306a36Sopenharmony_ci	 * @vm_ops:
21362306a36Sopenharmony_ci	 *
21462306a36Sopenharmony_ci	 * Virtual memory operations used with mmap.
21562306a36Sopenharmony_ci	 *
21662306a36Sopenharmony_ci	 * This is optional but necessary for mmap support.
21762306a36Sopenharmony_ci	 */
21862306a36Sopenharmony_ci	const struct vm_operations_struct *vm_ops;
21962306a36Sopenharmony_ci};
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci/**
22262306a36Sopenharmony_ci * struct drm_gem_lru - A simple LRU helper
22362306a36Sopenharmony_ci *
22462306a36Sopenharmony_ci * A helper for tracking GEM objects in a given state, to aid in
22562306a36Sopenharmony_ci * driver's shrinker implementation.  Tracks the count of pages
22662306a36Sopenharmony_ci * for lockless &shrinker.count_objects, and provides
22762306a36Sopenharmony_ci * &drm_gem_lru_scan for driver's &shrinker.scan_objects
22862306a36Sopenharmony_ci * implementation.
22962306a36Sopenharmony_ci */
23062306a36Sopenharmony_cistruct drm_gem_lru {
23162306a36Sopenharmony_ci	/**
23262306a36Sopenharmony_ci	 * @lock:
23362306a36Sopenharmony_ci	 *
23462306a36Sopenharmony_ci	 * Lock protecting movement of GEM objects between LRUs.  All
23562306a36Sopenharmony_ci	 * LRUs that the object can move between should be protected
23662306a36Sopenharmony_ci	 * by the same lock.
23762306a36Sopenharmony_ci	 */
23862306a36Sopenharmony_ci	struct mutex *lock;
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	/**
24162306a36Sopenharmony_ci	 * @count:
24262306a36Sopenharmony_ci	 *
24362306a36Sopenharmony_ci	 * The total number of backing pages of the GEM objects in
24462306a36Sopenharmony_ci	 * this LRU.
24562306a36Sopenharmony_ci	 */
24662306a36Sopenharmony_ci	long count;
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	/**
24962306a36Sopenharmony_ci	 * @list:
25062306a36Sopenharmony_ci	 *
25162306a36Sopenharmony_ci	 * The LRU list.
25262306a36Sopenharmony_ci	 */
25362306a36Sopenharmony_ci	struct list_head list;
25462306a36Sopenharmony_ci};
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci/**
25762306a36Sopenharmony_ci * struct drm_gem_object - GEM buffer object
25862306a36Sopenharmony_ci *
25962306a36Sopenharmony_ci * This structure defines the generic parts for GEM buffer objects, which are
26062306a36Sopenharmony_ci * mostly around handling mmap and userspace handles.
26162306a36Sopenharmony_ci *
26262306a36Sopenharmony_ci * Buffer objects are often abbreviated to BO.
26362306a36Sopenharmony_ci */
26462306a36Sopenharmony_cistruct drm_gem_object {
26562306a36Sopenharmony_ci	/**
26662306a36Sopenharmony_ci	 * @refcount:
26762306a36Sopenharmony_ci	 *
26862306a36Sopenharmony_ci	 * Reference count of this object
26962306a36Sopenharmony_ci	 *
27062306a36Sopenharmony_ci	 * Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()
27162306a36Sopenharmony_ci	 * or drm_gem_object_put() to release a reference to a GEM
27262306a36Sopenharmony_ci	 * buffer object.
27362306a36Sopenharmony_ci	 */
27462306a36Sopenharmony_ci	struct kref refcount;
27562306a36Sopenharmony_ci
27662306a36Sopenharmony_ci	/**
27762306a36Sopenharmony_ci	 * @handle_count:
27862306a36Sopenharmony_ci	 *
27962306a36Sopenharmony_ci	 * This is the GEM file_priv handle count of this object.
28062306a36Sopenharmony_ci	 *
28162306a36Sopenharmony_ci	 * Each handle also holds a reference. Note that when the handle_count
28262306a36Sopenharmony_ci	 * drops to 0 any global names (e.g. the id in the flink namespace) will
28362306a36Sopenharmony_ci	 * be cleared.
28462306a36Sopenharmony_ci	 *
28562306a36Sopenharmony_ci	 * Protected by &drm_device.object_name_lock.
28662306a36Sopenharmony_ci	 */
28762306a36Sopenharmony_ci	unsigned handle_count;
28862306a36Sopenharmony_ci
28962306a36Sopenharmony_ci	/**
29062306a36Sopenharmony_ci	 * @dev: DRM dev this object belongs to.
29162306a36Sopenharmony_ci	 */
29262306a36Sopenharmony_ci	struct drm_device *dev;
29362306a36Sopenharmony_ci
29462306a36Sopenharmony_ci	/**
29562306a36Sopenharmony_ci	 * @filp:
29662306a36Sopenharmony_ci	 *
29762306a36Sopenharmony_ci	 * SHMEM file node used as backing storage for swappable buffer objects.
29862306a36Sopenharmony_ci	 * GEM also supports driver private objects with driver-specific backing
29962306a36Sopenharmony_ci	 * storage (contiguous DMA memory, special reserved blocks). In this
30062306a36Sopenharmony_ci	 * case @filp is NULL.
30162306a36Sopenharmony_ci	 */
30262306a36Sopenharmony_ci	struct file *filp;
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci	/**
30562306a36Sopenharmony_ci	 * @vma_node:
30662306a36Sopenharmony_ci	 *
30762306a36Sopenharmony_ci	 * Mapping info for this object to support mmap. Drivers are supposed to
30862306a36Sopenharmony_ci	 * allocate the mmap offset using drm_gem_create_mmap_offset(). The
30962306a36Sopenharmony_ci	 * offset itself can be retrieved using drm_vma_node_offset_addr().
31062306a36Sopenharmony_ci	 *
31162306a36Sopenharmony_ci	 * Memory mapping itself is handled by drm_gem_mmap(), which also checks
31262306a36Sopenharmony_ci	 * that userspace is allowed to access the object.
31362306a36Sopenharmony_ci	 */
31462306a36Sopenharmony_ci	struct drm_vma_offset_node vma_node;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	/**
31762306a36Sopenharmony_ci	 * @size:
31862306a36Sopenharmony_ci	 *
31962306a36Sopenharmony_ci	 * Size of the object, in bytes.  Immutable over the object's
32062306a36Sopenharmony_ci	 * lifetime.
32162306a36Sopenharmony_ci	 */
32262306a36Sopenharmony_ci	size_t size;
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci	/**
32562306a36Sopenharmony_ci	 * @name:
32662306a36Sopenharmony_ci	 *
32762306a36Sopenharmony_ci	 * Global name for this object, starts at 1. 0 means unnamed.
32862306a36Sopenharmony_ci	 * Access is covered by &drm_device.object_name_lock. This is used by
32962306a36Sopenharmony_ci	 * the GEM_FLINK and GEM_OPEN ioctls.
33062306a36Sopenharmony_ci	 */
33162306a36Sopenharmony_ci	int name;
33262306a36Sopenharmony_ci
33362306a36Sopenharmony_ci	/**
33462306a36Sopenharmony_ci	 * @dma_buf:
33562306a36Sopenharmony_ci	 *
33662306a36Sopenharmony_ci	 * dma-buf associated with this GEM object.
33762306a36Sopenharmony_ci	 *
33862306a36Sopenharmony_ci	 * Pointer to the dma-buf associated with this gem object (either
33962306a36Sopenharmony_ci	 * through importing or exporting). We break the resulting reference
34062306a36Sopenharmony_ci	 * loop when the last gem handle for this object is released.
34162306a36Sopenharmony_ci	 *
34262306a36Sopenharmony_ci	 * Protected by &drm_device.object_name_lock.
34362306a36Sopenharmony_ci	 */
34462306a36Sopenharmony_ci	struct dma_buf *dma_buf;
34562306a36Sopenharmony_ci
34662306a36Sopenharmony_ci	/**
34762306a36Sopenharmony_ci	 * @import_attach:
34862306a36Sopenharmony_ci	 *
34962306a36Sopenharmony_ci	 * dma-buf attachment backing this object.
35062306a36Sopenharmony_ci	 *
35162306a36Sopenharmony_ci	 * Any foreign dma_buf imported as a gem object has this set to the
35262306a36Sopenharmony_ci	 * attachment point for the device. This is invariant over the lifetime
35362306a36Sopenharmony_ci	 * of a gem object.
35462306a36Sopenharmony_ci	 *
35562306a36Sopenharmony_ci	 * The &drm_gem_object_funcs.free callback is responsible for
35662306a36Sopenharmony_ci	 * cleaning up the dma_buf attachment and references acquired at import
35762306a36Sopenharmony_ci	 * time.
35862306a36Sopenharmony_ci	 *
35962306a36Sopenharmony_ci	 * Note that the drm gem/prime core does not depend upon drivers setting
36062306a36Sopenharmony_ci	 * this field any more. So for drivers where this doesn't make sense
36162306a36Sopenharmony_ci	 * (e.g. virtual devices or a displaylink behind an usb bus) they can
36262306a36Sopenharmony_ci	 * simply leave it as NULL.
36362306a36Sopenharmony_ci	 */
36462306a36Sopenharmony_ci	struct dma_buf_attachment *import_attach;
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	/**
36762306a36Sopenharmony_ci	 * @resv:
36862306a36Sopenharmony_ci	 *
36962306a36Sopenharmony_ci	 * Pointer to reservation object associated with the this GEM object.
37062306a36Sopenharmony_ci	 *
37162306a36Sopenharmony_ci	 * Normally (@resv == &@_resv) except for imported GEM objects.
37262306a36Sopenharmony_ci	 */
37362306a36Sopenharmony_ci	struct dma_resv *resv;
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	/**
37662306a36Sopenharmony_ci	 * @_resv:
37762306a36Sopenharmony_ci	 *
37862306a36Sopenharmony_ci	 * A reservation object for this GEM object.
37962306a36Sopenharmony_ci	 *
38062306a36Sopenharmony_ci	 * This is unused for imported GEM objects.
38162306a36Sopenharmony_ci	 */
38262306a36Sopenharmony_ci	struct dma_resv _resv;
38362306a36Sopenharmony_ci
38462306a36Sopenharmony_ci	/**
38562306a36Sopenharmony_ci	 * @gpuva:
38662306a36Sopenharmony_ci	 *
38762306a36Sopenharmony_ci	 * Provides the list of GPU VAs attached to this GEM object.
38862306a36Sopenharmony_ci	 *
38962306a36Sopenharmony_ci	 * Drivers should lock list accesses with the GEMs &dma_resv lock
39062306a36Sopenharmony_ci	 * (&drm_gem_object.resv) or a custom lock if one is provided.
39162306a36Sopenharmony_ci	 */
39262306a36Sopenharmony_ci	struct {
39362306a36Sopenharmony_ci		struct list_head list;
39462306a36Sopenharmony_ci
39562306a36Sopenharmony_ci#ifdef CONFIG_LOCKDEP
39662306a36Sopenharmony_ci		struct lockdep_map *lock_dep_map;
39762306a36Sopenharmony_ci#endif
39862306a36Sopenharmony_ci	} gpuva;
39962306a36Sopenharmony_ci
40062306a36Sopenharmony_ci	/**
40162306a36Sopenharmony_ci	 * @funcs:
40262306a36Sopenharmony_ci	 *
40362306a36Sopenharmony_ci	 * Optional GEM object functions. If this is set, it will be used instead of the
40462306a36Sopenharmony_ci	 * corresponding &drm_driver GEM callbacks.
40562306a36Sopenharmony_ci	 *
40662306a36Sopenharmony_ci	 * New drivers should use this.
40762306a36Sopenharmony_ci	 *
40862306a36Sopenharmony_ci	 */
40962306a36Sopenharmony_ci	const struct drm_gem_object_funcs *funcs;
41062306a36Sopenharmony_ci
41162306a36Sopenharmony_ci	/**
41262306a36Sopenharmony_ci	 * @lru_node:
41362306a36Sopenharmony_ci	 *
41462306a36Sopenharmony_ci	 * List node in a &drm_gem_lru.
41562306a36Sopenharmony_ci	 */
41662306a36Sopenharmony_ci	struct list_head lru_node;
41762306a36Sopenharmony_ci
41862306a36Sopenharmony_ci	/**
41962306a36Sopenharmony_ci	 * @lru:
42062306a36Sopenharmony_ci	 *
42162306a36Sopenharmony_ci	 * The current LRU list that the GEM object is on.
42262306a36Sopenharmony_ci	 */
42362306a36Sopenharmony_ci	struct drm_gem_lru *lru;
42462306a36Sopenharmony_ci};
42562306a36Sopenharmony_ci
42662306a36Sopenharmony_ci/**
42762306a36Sopenharmony_ci * DRM_GEM_FOPS - Default drm GEM file operations
42862306a36Sopenharmony_ci *
42962306a36Sopenharmony_ci * This macro provides a shorthand for setting the GEM file ops in the
43062306a36Sopenharmony_ci * &file_operations structure.  If all you need are the default ops, use
43162306a36Sopenharmony_ci * DEFINE_DRM_GEM_FOPS instead.
43262306a36Sopenharmony_ci */
43362306a36Sopenharmony_ci#define DRM_GEM_FOPS \
43462306a36Sopenharmony_ci	.open		= drm_open,\
43562306a36Sopenharmony_ci	.release	= drm_release,\
43662306a36Sopenharmony_ci	.unlocked_ioctl	= drm_ioctl,\
43762306a36Sopenharmony_ci	.compat_ioctl	= drm_compat_ioctl,\
43862306a36Sopenharmony_ci	.poll		= drm_poll,\
43962306a36Sopenharmony_ci	.read		= drm_read,\
44062306a36Sopenharmony_ci	.llseek		= noop_llseek,\
44162306a36Sopenharmony_ci	.mmap		= drm_gem_mmap
44262306a36Sopenharmony_ci
44362306a36Sopenharmony_ci/**
44462306a36Sopenharmony_ci * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
44562306a36Sopenharmony_ci * @name: name for the generated structure
44662306a36Sopenharmony_ci *
44762306a36Sopenharmony_ci * This macro autogenerates a suitable &struct file_operations for GEM based
44862306a36Sopenharmony_ci * drivers, which can be assigned to &drm_driver.fops. Note that this structure
44962306a36Sopenharmony_ci * cannot be shared between drivers, because it contains a reference to the
45062306a36Sopenharmony_ci * current module using THIS_MODULE.
45162306a36Sopenharmony_ci *
45262306a36Sopenharmony_ci * Note that the declaration is already marked as static - if you need a
45362306a36Sopenharmony_ci * non-static version of this you're probably doing it wrong and will break the
45462306a36Sopenharmony_ci * THIS_MODULE reference by accident.
45562306a36Sopenharmony_ci */
45662306a36Sopenharmony_ci#define DEFINE_DRM_GEM_FOPS(name) \
45762306a36Sopenharmony_ci	static const struct file_operations name = {\
45862306a36Sopenharmony_ci		.owner		= THIS_MODULE,\
45962306a36Sopenharmony_ci		DRM_GEM_FOPS,\
46062306a36Sopenharmony_ci	}
46162306a36Sopenharmony_ci
46262306a36Sopenharmony_civoid drm_gem_object_release(struct drm_gem_object *obj);
46362306a36Sopenharmony_civoid drm_gem_object_free(struct kref *kref);
46462306a36Sopenharmony_ciint drm_gem_object_init(struct drm_device *dev,
46562306a36Sopenharmony_ci			struct drm_gem_object *obj, size_t size);
46662306a36Sopenharmony_civoid drm_gem_private_object_init(struct drm_device *dev,
46762306a36Sopenharmony_ci				 struct drm_gem_object *obj, size_t size);
46862306a36Sopenharmony_civoid drm_gem_private_object_fini(struct drm_gem_object *obj);
46962306a36Sopenharmony_civoid drm_gem_vm_open(struct vm_area_struct *vma);
47062306a36Sopenharmony_civoid drm_gem_vm_close(struct vm_area_struct *vma);
47162306a36Sopenharmony_ciint drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
47262306a36Sopenharmony_ci		     struct vm_area_struct *vma);
47362306a36Sopenharmony_ciint drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);
47462306a36Sopenharmony_ci
47562306a36Sopenharmony_ci/**
47662306a36Sopenharmony_ci * drm_gem_object_get - acquire a GEM buffer object reference
47762306a36Sopenharmony_ci * @obj: GEM buffer object
47862306a36Sopenharmony_ci *
47962306a36Sopenharmony_ci * This function acquires an additional reference to @obj. It is illegal to
48062306a36Sopenharmony_ci * call this without already holding a reference. No locks required.
48162306a36Sopenharmony_ci */
48262306a36Sopenharmony_cistatic inline void drm_gem_object_get(struct drm_gem_object *obj)
48362306a36Sopenharmony_ci{
48462306a36Sopenharmony_ci	kref_get(&obj->refcount);
48562306a36Sopenharmony_ci}
48662306a36Sopenharmony_ci
48762306a36Sopenharmony_ci__attribute__((nonnull))
48862306a36Sopenharmony_cistatic inline void
48962306a36Sopenharmony_ci__drm_gem_object_put(struct drm_gem_object *obj)
49062306a36Sopenharmony_ci{
49162306a36Sopenharmony_ci	kref_put(&obj->refcount, drm_gem_object_free);
49262306a36Sopenharmony_ci}
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_ci/**
49562306a36Sopenharmony_ci * drm_gem_object_put - drop a GEM buffer object reference
49662306a36Sopenharmony_ci * @obj: GEM buffer object
49762306a36Sopenharmony_ci *
49862306a36Sopenharmony_ci * This releases a reference to @obj.
49962306a36Sopenharmony_ci */
50062306a36Sopenharmony_cistatic inline void
50162306a36Sopenharmony_cidrm_gem_object_put(struct drm_gem_object *obj)
50262306a36Sopenharmony_ci{
50362306a36Sopenharmony_ci	if (obj)
50462306a36Sopenharmony_ci		__drm_gem_object_put(obj);
50562306a36Sopenharmony_ci}
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ciint drm_gem_handle_create(struct drm_file *file_priv,
50862306a36Sopenharmony_ci			  struct drm_gem_object *obj,
50962306a36Sopenharmony_ci			  u32 *handlep);
51062306a36Sopenharmony_ciint drm_gem_handle_delete(struct drm_file *filp, u32 handle);
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci
51362306a36Sopenharmony_civoid drm_gem_free_mmap_offset(struct drm_gem_object *obj);
51462306a36Sopenharmony_ciint drm_gem_create_mmap_offset(struct drm_gem_object *obj);
51562306a36Sopenharmony_ciint drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_cistruct page **drm_gem_get_pages(struct drm_gem_object *obj);
51862306a36Sopenharmony_civoid drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
51962306a36Sopenharmony_ci		bool dirty, bool accessed);
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_ciint drm_gem_vmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
52262306a36Sopenharmony_civoid drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
52362306a36Sopenharmony_ci
52462306a36Sopenharmony_ciint drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
52562306a36Sopenharmony_ci			   int count, struct drm_gem_object ***objs_out);
52662306a36Sopenharmony_cistruct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
52762306a36Sopenharmony_cilong drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
52862306a36Sopenharmony_ci				    bool wait_all, unsigned long timeout);
52962306a36Sopenharmony_ciint drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
53062306a36Sopenharmony_ci			      struct ww_acquire_ctx *acquire_ctx);
53162306a36Sopenharmony_civoid drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
53262306a36Sopenharmony_ci				 struct ww_acquire_ctx *acquire_ctx);
53362306a36Sopenharmony_ciint drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
53462306a36Sopenharmony_ci			    u32 handle, u64 *offset);
53562306a36Sopenharmony_ci
53662306a36Sopenharmony_civoid drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
53762306a36Sopenharmony_civoid drm_gem_lru_remove(struct drm_gem_object *obj);
53862306a36Sopenharmony_civoid drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
53962306a36Sopenharmony_civoid drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
54062306a36Sopenharmony_ciunsigned long drm_gem_lru_scan(struct drm_gem_lru *lru,
54162306a36Sopenharmony_ci			       unsigned int nr_to_scan,
54262306a36Sopenharmony_ci			       unsigned long *remaining,
54362306a36Sopenharmony_ci			       bool (*shrink)(struct drm_gem_object *obj));
54462306a36Sopenharmony_ci
54562306a36Sopenharmony_ciint drm_gem_evict(struct drm_gem_object *obj);
54662306a36Sopenharmony_ci
54762306a36Sopenharmony_ci#ifdef CONFIG_LOCKDEP
54862306a36Sopenharmony_ci/**
54962306a36Sopenharmony_ci * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list.
55062306a36Sopenharmony_ci * @obj: the &drm_gem_object
55162306a36Sopenharmony_ci * @lock: the lock used to protect the gpuva list. The locking primitive
55262306a36Sopenharmony_ci * must contain a dep_map field.
55362306a36Sopenharmony_ci *
55462306a36Sopenharmony_ci * Call this if you're not proctecting access to the gpuva list with the
55562306a36Sopenharmony_ci * dma-resv lock, but with a custom lock.
55662306a36Sopenharmony_ci */
55762306a36Sopenharmony_ci#define drm_gem_gpuva_set_lock(obj, lock) \
55862306a36Sopenharmony_ci	if (!WARN((obj)->gpuva.lock_dep_map, \
55962306a36Sopenharmony_ci		  "GEM GPUVA lock should be set only once.")) \
56062306a36Sopenharmony_ci		(obj)->gpuva.lock_dep_map = &(lock)->dep_map
56162306a36Sopenharmony_ci#define drm_gem_gpuva_assert_lock_held(obj) \
56262306a36Sopenharmony_ci	lockdep_assert((obj)->gpuva.lock_dep_map ? \
56362306a36Sopenharmony_ci		       lock_is_held((obj)->gpuva.lock_dep_map) : \
56462306a36Sopenharmony_ci		       dma_resv_held((obj)->resv))
56562306a36Sopenharmony_ci#else
56662306a36Sopenharmony_ci#define drm_gem_gpuva_set_lock(obj, lock) do {} while (0)
56762306a36Sopenharmony_ci#define drm_gem_gpuva_assert_lock_held(obj) do {} while (0)
56862306a36Sopenharmony_ci#endif
56962306a36Sopenharmony_ci
57062306a36Sopenharmony_ci/**
57162306a36Sopenharmony_ci * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object
57262306a36Sopenharmony_ci * @obj: the &drm_gem_object
57362306a36Sopenharmony_ci *
57462306a36Sopenharmony_ci * This initializes the &drm_gem_object's &drm_gpuva list.
57562306a36Sopenharmony_ci *
57662306a36Sopenharmony_ci * Calling this function is only necessary for drivers intending to support the
57762306a36Sopenharmony_ci * &drm_driver_feature DRIVER_GEM_GPUVA.
57862306a36Sopenharmony_ci *
57962306a36Sopenharmony_ci * See also drm_gem_gpuva_set_lock().
58062306a36Sopenharmony_ci */
58162306a36Sopenharmony_cistatic inline void drm_gem_gpuva_init(struct drm_gem_object *obj)
58262306a36Sopenharmony_ci{
58362306a36Sopenharmony_ci	INIT_LIST_HEAD(&obj->gpuva.list);
58462306a36Sopenharmony_ci}
58562306a36Sopenharmony_ci
58662306a36Sopenharmony_ci/**
58762306a36Sopenharmony_ci * drm_gem_for_each_gpuva() - iternator to walk over a list of gpuvas
58862306a36Sopenharmony_ci * @entry__: &drm_gpuva structure to assign to in each iteration step
58962306a36Sopenharmony_ci * @obj__: the &drm_gem_object the &drm_gpuvas to walk are associated with
59062306a36Sopenharmony_ci *
59162306a36Sopenharmony_ci * This iterator walks over all &drm_gpuva structures associated with the
59262306a36Sopenharmony_ci * &drm_gpuva_manager.
59362306a36Sopenharmony_ci */
59462306a36Sopenharmony_ci#define drm_gem_for_each_gpuva(entry__, obj__) \
59562306a36Sopenharmony_ci	list_for_each_entry(entry__, &(obj__)->gpuva.list, gem.entry)
59662306a36Sopenharmony_ci
59762306a36Sopenharmony_ci/**
59862306a36Sopenharmony_ci * drm_gem_for_each_gpuva_safe() - iternator to safely walk over a list of
59962306a36Sopenharmony_ci * gpuvas
60062306a36Sopenharmony_ci * @entry__: &drm_gpuva structure to assign to in each iteration step
60162306a36Sopenharmony_ci * @next__: &next &drm_gpuva to store the next step
60262306a36Sopenharmony_ci * @obj__: the &drm_gem_object the &drm_gpuvas to walk are associated with
60362306a36Sopenharmony_ci *
60462306a36Sopenharmony_ci * This iterator walks over all &drm_gpuva structures associated with the
60562306a36Sopenharmony_ci * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence
60662306a36Sopenharmony_ci * it is save against removal of elements.
60762306a36Sopenharmony_ci */
60862306a36Sopenharmony_ci#define drm_gem_for_each_gpuva_safe(entry__, next__, obj__) \
60962306a36Sopenharmony_ci	list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, gem.entry)
61062306a36Sopenharmony_ci
61162306a36Sopenharmony_ci#endif /* __DRM_GEM_H__ */
612