#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:401*402* Provides the list of GPU VAs attached to this GEM object.403*404* Drivers should lock list accesses with the GEMs &dma_resv lock405* (&drm_gem_object.resv) or a custom lock if one is provided.406*/407struct {408struct list_head list;409410#ifdef CONFIG_LOCKDEP411struct lockdep_map *lock_dep_map;412#endif413} gpuva;414415/**416* @funcs:417*418* Optional GEM object functions. If this is set, it will be used instead of the419* corresponding &drm_driver GEM callbacks.420*421* New drivers should use this.422*423*/424const struct drm_gem_object_funcs *funcs;425426/**427* @lru_node:428*429* List node in a &drm_gem_lru.430*/431struct list_head lru_node;432433/**434* @lru:435*436* The current LRU list that the GEM object is on.437*/438struct drm_gem_lru *lru;439};440441/**442* DRM_GEM_FOPS - Default drm GEM file operations443*444* This macro provides a shorthand for setting the GEM file ops in the445* &file_operations structure. If all you need are the default ops, use446* DEFINE_DRM_GEM_FOPS instead.447*/448#define DRM_GEM_FOPS \449.open = drm_open,\450.release = drm_release,\451.unlocked_ioctl = drm_ioctl,\452.compat_ioctl = drm_compat_ioctl,\453.poll = drm_poll,\454.read = drm_read,\455.llseek = noop_llseek,\456.mmap = drm_gem_mmap, \457.fop_flags = FOP_UNSIGNED_OFFSET458459/**460* DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers461* @name: name for the generated structure462*463* This macro autogenerates a suitable &struct file_operations for GEM based464* drivers, which can be assigned to &drm_driver.fops. Note that this structure465* cannot be shared between drivers, because it contains a reference to the466* current module using THIS_MODULE.467*468* Note that the declaration is already marked as static - if you need a469* non-static version of this you're probably doing it wrong and will break the470* THIS_MODULE reference by accident.471*/472#define DEFINE_DRM_GEM_FOPS(name) \473static const struct file_operations name = {\474.owner = THIS_MODULE,\475DRM_GEM_FOPS,\476}477478void drm_gem_object_release(struct drm_gem_object *obj);479void drm_gem_object_free(struct kref *kref);480int drm_gem_object_init(struct drm_device *dev,481struct drm_gem_object *obj, size_t size);482int drm_gem_object_init_with_mnt(struct drm_device *dev,483struct drm_gem_object *obj, size_t size,484struct vfsmount *gemfs);485void drm_gem_private_object_init(struct drm_device *dev,486struct drm_gem_object *obj, size_t size);487void drm_gem_private_object_fini(struct drm_gem_object *obj);488void drm_gem_vm_open(struct vm_area_struct *vma);489void drm_gem_vm_close(struct vm_area_struct *vma);490int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,491struct vm_area_struct *vma);492int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);493494/**495* drm_gem_object_get - acquire a GEM buffer object reference496* @obj: GEM buffer object497*498* This function acquires an additional reference to @obj. It is illegal to499* call this without already holding a reference. No locks required.500*/501static inline void drm_gem_object_get(struct drm_gem_object *obj)502{503kref_get(&obj->refcount);504}505506__attribute__((nonnull))507static inline void508__drm_gem_object_put(struct drm_gem_object *obj)509{510kref_put(&obj->refcount, drm_gem_object_free);511}512513/**514* drm_gem_object_put - drop a GEM buffer object reference515* @obj: GEM buffer object516*517* This releases a reference to @obj.518*/519static inline void520drm_gem_object_put(struct drm_gem_object *obj)521{522if (obj)523__drm_gem_object_put(obj);524}525526int drm_gem_handle_create(struct drm_file *file_priv,527struct drm_gem_object *obj,528u32 *handlep);529int drm_gem_handle_delete(struct drm_file *filp, u32 handle);530531532void drm_gem_free_mmap_offset(struct drm_gem_object *obj);533int drm_gem_create_mmap_offset(struct drm_gem_object *obj);534int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);535536struct page **drm_gem_get_pages(struct drm_gem_object *obj);537void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,538bool dirty, bool accessed);539540void drm_gem_lock(struct drm_gem_object *obj);541void drm_gem_unlock(struct drm_gem_object *obj);542543int drm_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map);544void drm_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map);545546int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,547int count, struct drm_gem_object ***objs_out);548struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);549long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,550bool wait_all, unsigned long timeout);551int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,552struct ww_acquire_ctx *acquire_ctx);553void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,554struct ww_acquire_ctx *acquire_ctx);555int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,556u32 handle, u64 *offset);557558void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);559void drm_gem_lru_remove(struct drm_gem_object *obj);560void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);561void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);562unsigned long563drm_gem_lru_scan(struct drm_gem_lru *lru,564unsigned int nr_to_scan,565unsigned long *remaining,566bool (*shrink)(struct drm_gem_object *obj, struct ww_acquire_ctx *ticket),567struct ww_acquire_ctx *ticket);568569int drm_gem_evict_locked(struct drm_gem_object *obj);570571/**572* drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats573*574* This helper should only be used for fdinfo shared memory stats to determine575* if a GEM object is shared.576*577* @obj: obj in question578*/579static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)580{581return (obj->handle_count > 1) || obj->dma_buf;582}583584/**585* drm_gem_is_imported() - Tests if GEM object's buffer has been imported586* @obj: the GEM object587*588* Returns:589* True if the GEM object's buffer has been imported, false otherwise590*/591static inline bool drm_gem_is_imported(const struct drm_gem_object *obj)592{593return !!obj->import_attach;594}595596#ifdef CONFIG_LOCKDEP597/**598* drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list.599* @obj: the &drm_gem_object600* @lock: the lock used to protect the gpuva list. The locking primitive601* must contain a dep_map field.602*603* Call this if you're not proctecting access to the gpuva list with the604* dma-resv lock, but with a custom lock.605*/606#define drm_gem_gpuva_set_lock(obj, lock) \607if (!WARN((obj)->gpuva.lock_dep_map, \608"GEM GPUVA lock should be set only once.")) \609(obj)->gpuva.lock_dep_map = &(lock)->dep_map610#define drm_gem_gpuva_assert_lock_held(obj) \611lockdep_assert((obj)->gpuva.lock_dep_map ? \612lock_is_held((obj)->gpuva.lock_dep_map) : \613dma_resv_held((obj)->resv))614#else615#define drm_gem_gpuva_set_lock(obj, lock) do {} while (0)616#define drm_gem_gpuva_assert_lock_held(obj) do {} while (0)617#endif618619/**620* drm_gem_gpuva_init() - initialize the gpuva list of a GEM object621* @obj: the &drm_gem_object622*623* This initializes the &drm_gem_object's &drm_gpuvm_bo list.624*625* Calling this function is only necessary for drivers intending to support the626* &drm_driver_feature DRIVER_GEM_GPUVA.627*628* See also drm_gem_gpuva_set_lock().629*/630static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)631{632INIT_LIST_HEAD(&obj->gpuva.list);633}634635/**636* drm_gem_for_each_gpuvm_bo() - iterator to walk over a list of &drm_gpuvm_bo637* @entry__: &drm_gpuvm_bo structure to assign to in each iteration step638* @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with639*640* This iterator walks over all &drm_gpuvm_bo structures associated with the641* &drm_gem_object.642*/643#define drm_gem_for_each_gpuvm_bo(entry__, obj__) \644list_for_each_entry(entry__, &(obj__)->gpuva.list, list.entry.gem)645646/**647* drm_gem_for_each_gpuvm_bo_safe() - iterator to safely walk over a list of648* &drm_gpuvm_bo649* @entry__: &drm_gpuvm_bostructure to assign to in each iteration step650* @next__: &next &drm_gpuvm_bo to store the next step651* @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with652*653* This iterator walks over all &drm_gpuvm_bo structures associated with the654* &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence655* it is save against removal of elements.656*/657#define drm_gem_for_each_gpuvm_bo_safe(entry__, next__, obj__) \658list_for_each_entry_safe(entry__, next__, &(obj__)->gpuva.list, list.entry.gem)659660#endif /* __DRM_GEM_H__ */661662663