#ifndef __DRM_GEM_H__1#define __DRM_GEM_H__23/*4* GEM Graphics Execution Manager Driver Interfaces5*6* Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.7* Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.8* Copyright (c) 2009-2010, Code Aurora Forum.9* All rights reserved.10* Copyright © 2014 Intel Corporation11* Daniel Vetter <[email protected]>12*13* Author: Rickard E. (Rik) Faith <[email protected]>14* Author: Gareth Hughes <[email protected]>15*16* Permission is hereby granted, free of charge, to any person obtaining a17* copy of this software and associated documentation files (the "Software"),18* to deal in the Software without restriction, including without limitation19* the rights to use, copy, modify, merge, publish, distribute, sublicense,20* and/or sell copies of the Software, and to permit persons to whom the21* Software is furnished to do so, subject to the following conditions:22*23* The above copyright notice and this permission notice (including the next24* paragraph) shall be included in all copies or substantial portions of the25* Software.26*27* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR28* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,29* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL30* VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR31* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,32* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR33* OTHER DEALINGS IN THE SOFTWARE.34*/3536#include <linux/kref.h>37#include <linux/dma-buf.h>38#include <linux/dma-resv.h>39#include <linux/list.h>40#include <linux/mutex.h>4142#include <drm/drm_vma_manager.h>4344struct iosys_map;45struct drm_gem_object;4647/**48* enum drm_gem_object_status - bitmask of object state for fdinfo reporting49* @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)50* @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace51* @DRM_GEM_OBJECT_ACTIVE: object is currently used by an active submission52*53* Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status54* and drm_show_fdinfo(). Note that an object can report DRM_GEM_OBJECT_PURGEABLE55* and be active or not resident, in which case drm_show_fdinfo() will not56* account for it as purgeable. So drivers do not need to check if the buffer57* is idle and resident to return this bit, i.e. userspace can mark a buffer as58* purgeable even while it is still busy on the GPU. It will not get reported in59* the puregeable stats until it becomes idle. The status gem object func does60* not need to consider this.61*/62enum drm_gem_object_status {63DRM_GEM_OBJECT_RESIDENT = BIT(0),64DRM_GEM_OBJECT_PURGEABLE = BIT(1),65DRM_GEM_OBJECT_ACTIVE = BIT(2),66};6768/**69* struct drm_gem_object_funcs - GEM object functions70*/71struct drm_gem_object_funcs {72/**73* @free:74*75* Deconstructor for drm_gem_objects.76*77* This callback is mandatory.78*/79void (*free)(struct drm_gem_object *obj);8081/**82* @open:83*84* Called upon GEM handle creation.85*86* This callback is optional.87*/88int (*open)(struct drm_gem_object *obj, struct drm_file *file);8990/**91* @close:92*93* Called upon GEM handle release.94*95* This callback is optional.96*/97void (*close)(struct drm_gem_object *obj, struct drm_file *file);9899/**100* @print_info:101*102* If driver subclasses struct &drm_gem_object, it can implement this103* optional hook for printing additional driver specific info.104*105* drm_printf_indent() should be used in the callback passing it the106* indent argument.107*108* This callback is called from drm_gem_print_info().109*110* This callback is optional.111*/112void (*print_info)(struct drm_printer *p, unsigned int indent,113const struct drm_gem_object *obj);114115/**116* @export:117*118* Export backing buffer as a &dma_buf.119* If this is not set drm_gem_prime_export() is used.120*121* This callback is optional.122*/123struct dma_buf *(*export)(struct drm_gem_object *obj, int flags);124125/**126* @pin:127*128* Pin backing buffer in memory, such that dma-buf importers can129* access it. Used by the drm_gem_map_attach() helper.130*131* This callback is optional.132*/133int (*pin)(struct drm_gem_object *obj);134135/**136* @unpin:137*138* Unpin backing buffer. Used by the drm_gem_map_detach() helper.139*140* This callback is optional.141*/142void (*unpin)(struct drm_gem_object *obj);143144/**145* @get_sg_table:146*147* Returns a Scatter-Gather table representation of the buffer.148* Used when exporting a buffer by the drm_gem_map_dma_buf() helper.149* Releasing is done by calling dma_unmap_sg_attrs() and sg_free_table()150* in drm_gem_unmap_buf(), therefore these helpers and this callback151* here cannot be used for sg tables pointing at driver private memory152* ranges.153*154* See also drm_prime_pages_to_sg().155*/156struct sg_table *(*get_sg_table)(struct drm_gem_object *obj);157158/**159* @vmap:160*161* Returns a virtual address for the buffer. Used by the162* drm_gem_dmabuf_vmap() helper. Called with a held GEM reservation163* lock.164*165* This callback is optional.166*/167int (*vmap)(struct drm_gem_object *obj, struct iosys_map *map);168169/**170* @vunmap:171*172* Releases the address previously returned by @vmap. Used by the173* drm_gem_dmabuf_vunmap() helper. Called with a held GEM reservation174* lock.175*176* This callback is optional.177*/178void (*vunmap)(struct drm_gem_object *obj, struct iosys_map *map);179180/**181* @mmap:182*183* Handle mmap() of the gem object, setup vma accordingly.184*185* This callback is optional.186*187* The callback is used by both drm_gem_mmap_obj() and188* drm_gem_prime_mmap(). When @mmap is present @vm_ops is not189* used, the @mmap callback must set vma->vm_ops instead.190*/191int (*mmap)(struct drm_gem_object *obj, struct vm_area_struct *vma);192193/**194* @evict:195*196* Evicts gem object out from memory. Used by the drm_gem_object_evict()197* helper. Returns 0 on success, -errno otherwise. Called with a held198* GEM reservation lock.199*200* This callback is optional.201*/202int (*evict)(struct drm_gem_object *obj);203204/**205* @status:206*207* The optional status callback can return additional object state208* which determines which stats the object is counted against. The209* callback is called under table_lock. Racing against object status210* change is "harmless", and the callback can expect to not race211* against object destruction.212*213* Called by drm_show_memory_stats().214*/215enum drm_gem_object_status (*status)(struct drm_gem_object *obj);216217/**218* @rss:219*220* Return resident size of the object in physical memory.221*222* Called by drm_show_memory_stats().223*/224size_t (*rss)(struct drm_gem_object *obj);225226/**227* @vm_ops:228*229* Virtual memory operations used with mmap.230*231* This is optional but necessary for mmap support.232*/233const struct vm_operations_struct *vm_ops;234};235236/**237* struct drm_gem_lru - A simple LRU helper238*239* A helper for tracking GEM objects in a given state, to aid in240* driver's shrinker implementation. Tracks the count of pages241* for lockless &shrinker.count_objects, and provides242* &drm_gem_lru_scan for driver's &shrinker.scan_objects243* implementation.244*/245struct drm_gem_lru {246/**247* @lock:248*249* Lock protecting movement of GEM objects between LRUs. All250* LRUs that the object can move between should be protected251* by the same lock.252*/253struct mutex *lock;254255/**256* @count:257*258* The total number of backing pages of the GEM objects in259* this LRU.260*/261long count;262263/**264* @list:265*266* The LRU list.267*/268struct list_head list;269};270271/**272* struct drm_gem_object - GEM buffer object273*274* This structure defines the generic parts for GEM buffer objects, which are275* mostly around handling mmap and userspace handles.276*277* Buffer objects are often abbreviated to BO.278*/279struct drm_gem_object {280/**281* @refcount:282*283* Reference count of this object284*285* Please use drm_gem_object_get() to acquire and drm_gem_object_put_locked()286* or drm_gem_object_put() to release a reference to a GEM287* buffer object.288*/289struct kref refcount;290291/**292* @handle_count:293*294* This is the GEM file_priv handle count of this object.295*296* Each handle also holds a reference. Note that when the handle_count297* drops to 0 any global names (e.g. the id in the flink namespace) will298* be cleared.299*300* Protected by &drm_device.object_name_lock.301*/302unsigned handle_count;303304/**305* @dev: DRM dev this object belongs to.306*/307struct drm_device *dev;308309/**310* @filp:311*312* SHMEM file node used as backing storage for swappable buffer objects.313* GEM also supports driver private objects with driver-specific backing314* storage (contiguous DMA memory, special reserved blocks). In this315* case @filp is NULL.316*/317struct file *filp;318319/**320* @vma_node:321*322* Mapping info for this object to support mmap. Drivers are supposed to323* allocate the mmap offset using drm_gem_create_mmap_offset(). The324* offset itself can be retrieved using drm_vma_node_offset_addr().325*326* Memory mapping itself is handled by drm_gem_mmap(), which also checks327* that userspace is allowed to access the object.328*/329struct drm_vma_offset_node vma_node;330331/**332* @size:333*334* Size of the object, in bytes. Immutable over the object's335* lifetime.336*/337size_t size;338339/**340* @name:341*342* Global name for this object, starts at 1. 0 means unnamed.343* Access is covered by &drm_device.object_name_lock. This is used by344* the GEM_FLINK and GEM_OPEN ioctls.345*/346int name;347348/**349* @dma_buf:350*351* dma-buf associated with this GEM object.352*353* Pointer to the dma-buf associated with this gem object (either354* through importing or exporting). We break the resulting reference355* loop when the last gem handle for this object is released.356*357* Protected by &drm_device.object_name_lock.358*/359struct dma_buf *dma_buf;360361/**362* @import_attach:363*364* dma-buf attachment backing this object.365*366* Any foreign dma_buf imported as a gem object has this set to the367* attachment point for the device. This is invariant over the lifetime368* of a gem object.369*370* The &drm_gem_object_funcs.free callback is responsible for371* cleaning up the dma_buf attachment and references acquired at import372* time.373*374* Note that the drm gem/prime core does not depend upon drivers setting375* this field any more. So for drivers where this doesn't make sense376* (e.g. virtual devices or a displaylink behind an usb bus) they can377* simply leave it as NULL.378*/379struct dma_buf_attachment *import_attach;380381/**382* @resv:383*384* Pointer to reservation object associated with the this GEM object.385*386* Normally (@resv == &@_resv) except for imported GEM objects.387*/388struct dma_resv *resv;389390/**391* @_resv:392*393* A reservation object for this GEM object.394*395* This is unused for imported GEM objects.396*/397struct dma_resv _resv;398399/**400* @gpuva: Fields used by GPUVM to manage mappings pointing to this GEM object.401*402* When DRM_GPUVM_IMMEDIATE_MODE is set, this list is protected by the403* mutex. Otherwise, the list is protected by the GEMs &dma_resv lock.404*405* Note that all entries in this list must agree on whether406* DRM_GPUVM_IMMEDIATE_MODE is set.407*/408struct {409/**410* @gpuva.list: list of GPUVM mappings attached to this GEM object.411*412* Drivers should lock list accesses with either the GEMs413* &dma_resv lock (&drm_gem_object.resv) or the414* &drm_gem_object.gpuva.lock mutex.415*/416struct list_head list;417418/**419* @gpuva.lock: lock protecting access to &drm_gem_object.gpuva.list420* when DRM_GPUVM_IMMEDIATE_MODE is used.421*422* Only used when DRM_GPUVM_IMMEDIATE_MODE is set. It should be423* safe to take this mutex during the fence signalling path, so424* do not allocate memory while holding this lock. Otherwise,425* the &dma_resv lock should be used.426*/427struct mutex lock;428} gpuva;429430/**431* @funcs:432*433* Optional GEM object functions. If this is set, it will be used instead of the434* corresponding &drm_driver GEM callbacks.435*436* New drivers should use this.437*438*/439const struct drm_gem_object_funcs *funcs;440441/**442* @lru_node:443*444* List node in a &drm_gem_lru.445*/446struct list_head lru_node;447448/**449* @lru:450*451* The current LRU list that the GEM object is on.452*/453struct drm_gem_lru *lru;454};455456/**457* DRM_GEM_FOPS - Default drm GEM file operations458*459* This macro provides a shorthand for setting the GEM file ops in the460* &file_operations structure. If all you need are the default ops, use461* DEFINE_DRM_GEM_FOPS instead.462*/463#define DRM_GEM_FOPS \464.open = drm_open,\465.release = drm_release,\466.unlocked_ioctl = drm_ioctl,\467.compat_ioctl = drm_compat_ioctl,\468.poll = drm_poll,\469.read = drm_read,\470.llseek = noop_llseek,\471.mmap = drm_gem_mmap, \472.fop_flags = FOP_UNSIGNED_OFFSET473474/**475* DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers476* @name: name for the generated structure477*478* This macro autogenerates a suitable &struct file_operations for GEM based479* drivers, which can be assigned to &drm_driver.fops. Note that this structure480* cannot be shared between drivers, because it contains a reference to the481* current module using THIS_MODULE.482*483* Note that the declaration is already marked as static - if you need a484* non-static version of this you're probably doing it wrong and will break the485* THIS_MODULE reference by accident.486*/487#define DEFINE_DRM_GEM_FOPS(name) \488static const struct file_operations name = {\489.owner = THIS_MODULE,\490DRM_GEM_FOPS,\491}492493void drm_gem_object_release(struct drm_gem_object *obj);494void drm_gem_object_free(struct kref *kref);495int drm_gem_object_init(struct drm_device *dev,496struct drm_gem_object *obj, size_t size);497int drm_gem_object_init_with_mnt(struct drm_device *dev,498struct drm_gem_object *obj, size_t size,499struct vfsmount *gemfs);500void drm_gem_private_object_init(struct drm_device *dev,501struct drm_gem_object *obj, size_t size);502void drm_gem_private_object_fini(struct drm_gem_object *obj);503void drm_gem_vm_open(struct vm_area_struct *vma);504void drm_gem_vm_close(struct vm_area_struct *vma);505int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,506struct vm_area_struct *vma);507int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);508509/**510* drm_gem_object_get - acquire a GEM buffer object reference511* @obj: GEM buffer object512*513* This function acquires an additional reference to @obj. It is illegal to514* call this without already holding a reference. No locks required.515*/516static inline void drm_gem_object_get(struct drm_gem_object *obj)517{518kref_get(&obj->refcount);519}520521__attribute__((nonnull))522static inline void523__drm_gem_object_put(struct drm_gem_object *obj)524{525kref_put(&obj->refcount, drm_gem_object_free);526}527528/**529* drm_gem_object_put - drop a GEM buffer object reference530* @obj: GEM buffer object531*532* This releases a reference to @obj.533*/534static inline void535drm_gem_object_put(struct drm_gem_object *obj)536{537if (obj)538__drm_gem_object_put(obj);539}540541int drm_gem_handle_create(struct drm_file *file_priv,542struct drm_gem_object *obj,543u32 *handlep);544int drm_gem_handle_delete(struct drm_file *filp, u32 handle);545546547void drm_gem_free_mmap_offset(struct drm_gem_object *obj);548int drm_gem_create_mmap_offset(struct drm_gem_object *obj);549int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);550551struct page **drm_gem_get_pages(struct drm_gem_object *obj);552void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,553bool dirty, bool accessed);554555void drm_gem_lock(struct drm_gem_object *obj);556void drm_gem_unlock(struct drm_gem_object *obj);557558int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map);559void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);560561int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,562int count, struct drm_gem_object ***objs_out);563struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);564long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,565bool wait_all, unsigned long timeout);566int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,567struct ww_acquire_ctx *acquire_ctx);568void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,569struct ww_acquire_ctx *acquire_ctx);570int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,571u32 handle, u64 *offset);572573void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);574void drm_gem_lru_remove(struct drm_gem_object *obj);575void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);576void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);577unsigned long578drm_gem_lru_scan(struct drm_gem_lru *lru,579unsigned int nr_to_scan,580unsigned long *remaining,581bool (*shrink)(struct drm_gem_object *obj, struct ww_acquire_ctx *ticket),582struct ww_acquire_ctx *ticket);583584int drm_gem_evict_locked(struct drm_gem_object *obj);585586/**587* drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats588*589* This helper should only be used for fdinfo shared memory stats to determine590* if a GEM object is shared.591*592* @obj: obj in question593*/594static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)595{596return (obj->handle_count > 1) || obj->dma_buf;597}598599/**600* drm_gem_is_imported() - Tests if GEM object's buffer has been imported601* @obj: the GEM object602*603* Returns:604* True if the GEM object's buffer has been imported, false otherwise605*/606static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)607{608return !!obj->import_attach;609}610611#ifdef CONFIG_LOCKDEP612#define drm_gem_gpuva_assert_lock_held(gpuvm, obj) \613lockdep_assert(drm_gpuvm_immediate_mode(gpuvm) ? \614lockdep_is_held(&(obj)->gpuva.lock) : \615dma_resv_held((obj)->resv))616#else617#define drm_gem_gpuva_assert_lock_held(gpuvm, obj) do {} while (0)618#endif619620/**621* drm_gem_gpuva_init() - initialize the gpuva list of a GEM object622* @obj: the &drm_gem_object623*624* This initializes the &drm_gem_object's &drm_gpuvm_bo list.625*626* Calling this function is only necessary for drivers intending to support the627* &drm_driver_feature DRIVER_GEM_GPUVA.628*629* See also drm_gem_gpuva_set_lock().630*/631static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)632{633INIT_LIST_HEAD(&obj->gpuva.list);634}635636/**637* drm_gem_for_each_gpuvm_bo() - iterator to walk over a list of &drm_gpuvm_bo638* @entry__: &drm_gpuvm_bo structure to assign to in each iteration step639* @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with640*641* This iterator walks over all &drm_gpuvm_bo structures associated with the642* &drm_gem_object.643*/644#define drm_gem_for_each_gpuvm_bo(entry__, obj__) \645list_for_each_entry(entry__, &(obj__)->gpuva.list, list.entry.gem)646647/**648* drm_gem_for_each_gpuvm_bo_safe() - iterator to safely walk over a list of649* &drm_gpuvm_bo650* @entry__: &drm_gpuvm_bostructure to assign to in each iteration step651* @next__: &next &drm_gpuvm_bo to store the next step652* @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with653*654* This iterator walks over all &drm_gpuvm_bo structures associated with the655* &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence656* it is save against removal of elements.657*/658#define drm_gem_for_each_gpuvm_bo_safe(entry__, next__, obj__) \659list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, list.entry.gem)660661#endif /* __DRM_GEM_H__ */662663664