linux/include/drm/drm_gem.h

#ifndef __DRM_GEM_H__
#define __DRM_GEM_H__

/*
 * GEM Graphics Execution Manager Driver Interfaces
 *
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * Copyright (c) 2009-2010, Code Aurora Forum.
 * All rights reserved.
 * Copyright © 2014 Intel Corporation
 *   Daniel Vetter <[email protected]>
 *
 * Author: Rickard E. (Rik) Faith <[email protected]>
 * Author: Gareth Hughes <[email protected]>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include <linux/kref.h>
#include <linux/dma-resv.h>
#include <linux/list.h>
#include <linux/mutex.h>

#include <drm/drm_vma_manager.h>

struct iosys_map;
struct drm_gem_object;

/**
 * enum drm_gem_object_status - bitmask of object state for fdinfo reporting
 * @DRM_GEM_OBJECT_RESIDENT: object is resident in memory (ie. not unpinned)
 * @DRM_GEM_OBJECT_PURGEABLE: object marked as purgeable by userspace
 *
 * Bitmask of status used for fdinfo memory stats, see &drm_gem_object_funcs.status
 * and drm_show_fdinfo().  Note that an object can DRM_GEM_OBJECT_PURGEABLE if
 * it still active or not resident, in which case drm_show_fdinfo() will not
 * account for it as purgeable.  So drivers do not need to check if the buffer
 * is idle and resident to return this bit.  (Ie. userspace can mark a buffer
 * as purgeable even while it is still busy on the GPU.. it does not _actually_
 * become puregeable until it becomes idle.  The status gem object func does
 * not need to consider this.)
 */
enum drm_gem_object_status {};

/**
 * struct drm_gem_object_funcs - GEM object functions
 */
struct drm_gem_object_funcs {};

/**
 * struct drm_gem_lru - A simple LRU helper
 *
 * A helper for tracking GEM objects in a given state, to aid in
 * driver's shrinker implementation.  Tracks the count of pages
 * for lockless &shrinker.count_objects, and provides
 * &drm_gem_lru_scan for driver's &shrinker.scan_objects
 * implementation.
 */
struct drm_gem_lru {};

/**
 * struct drm_gem_object - GEM buffer object
 *
 * This structure defines the generic parts for GEM buffer objects, which are
 * mostly around handling mmap and userspace handles.
 *
 * Buffer objects are often abbreviated to BO.
 */
struct drm_gem_object {};

/**
 * DRM_GEM_FOPS - Default drm GEM file operations
 *
 * This macro provides a shorthand for setting the GEM file ops in the
 * &file_operations structure.  If all you need are the default ops, use
 * DEFINE_DRM_GEM_FOPS instead.
 */
#define DRM_GEM_FOPS

/**
 * DEFINE_DRM_GEM_FOPS() - macro to generate file operations for GEM drivers
 * @name: name for the generated structure
 *
 * This macro autogenerates a suitable &struct file_operations for GEM based
 * drivers, which can be assigned to &drm_driver.fops. Note that this structure
 * cannot be shared between drivers, because it contains a reference to the
 * current module using THIS_MODULE.
 *
 * Note that the declaration is already marked as static - if you need a
 * non-static version of this you're probably doing it wrong and will break the
 * THIS_MODULE reference by accident.
 */
#define DEFINE_DRM_GEM_FOPS(name)

void drm_gem_object_release(struct drm_gem_object *obj);
void drm_gem_object_free(struct kref *kref);
int drm_gem_object_init(struct drm_device *dev,
			struct drm_gem_object *obj, size_t size);
void drm_gem_private_object_init(struct drm_device *dev,
				 struct drm_gem_object *obj, size_t size);
void drm_gem_private_object_fini(struct drm_gem_object *obj);
void drm_gem_vm_open(struct vm_area_struct *vma);
void drm_gem_vm_close(struct vm_area_struct *vma);
int drm_gem_mmap_obj(struct drm_gem_object *obj, unsigned long obj_size,
		     struct vm_area_struct *vma);
int drm_gem_mmap(struct file *filp, struct vm_area_struct *vma);

/**
 * drm_gem_object_get - acquire a GEM buffer object reference
 * @obj: GEM buffer object
 *
 * This function acquires an additional reference to @obj. It is illegal to
 * call this without already holding a reference. No locks required.
 */
static inline void drm_gem_object_get(struct drm_gem_object *obj)
{}

__attribute__((nonnull))
static inline void
__drm_gem_object_put(struct drm_gem_object *obj)
{}

/**
 * drm_gem_object_put - drop a GEM buffer object reference
 * @obj: GEM buffer object
 *
 * This releases a reference to @obj.
 */
static inline void
drm_gem_object_put(struct drm_gem_object *obj)
{}

int drm_gem_handle_create(struct drm_file *file_priv,
			  struct drm_gem_object *obj,
			  u32 *handlep);
int drm_gem_handle_delete(struct drm_file *filp, u32 handle);


void drm_gem_free_mmap_offset(struct drm_gem_object *obj);
int drm_gem_create_mmap_offset(struct drm_gem_object *obj);
int drm_gem_create_mmap_offset_size(struct drm_gem_object *obj, size_t size);

struct page **drm_gem_get_pages(struct drm_gem_object *obj);
void drm_gem_put_pages(struct drm_gem_object *obj, struct page **pages,
		bool dirty, bool accessed);

void drm_gem_lock(struct drm_gem_object *obj);
void drm_gem_unlock(struct drm_gem_object *obj);

int drm_gem_vmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);
void drm_gem_vunmap_unlocked(struct drm_gem_object *obj, struct iosys_map *map);

int drm_gem_objects_lookup(struct drm_file *filp, void __user *bo_handles,
			   int count, struct drm_gem_object ***objs_out);
struct drm_gem_object *drm_gem_object_lookup(struct drm_file *filp, u32 handle);
long drm_gem_dma_resv_wait(struct drm_file *filep, u32 handle,
				    bool wait_all, unsigned long timeout);
int drm_gem_lock_reservations(struct drm_gem_object **objs, int count,
			      struct ww_acquire_ctx *acquire_ctx);
void drm_gem_unlock_reservations(struct drm_gem_object **objs, int count,
				 struct ww_acquire_ctx *acquire_ctx);
int drm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
			    u32 handle, u64 *offset);

void drm_gem_lru_init(struct drm_gem_lru *lru, struct mutex *lock);
void drm_gem_lru_remove(struct drm_gem_object *obj);
void drm_gem_lru_move_tail_locked(struct drm_gem_lru *lru, struct drm_gem_object *obj);
void drm_gem_lru_move_tail(struct drm_gem_lru *lru, struct drm_gem_object *obj);
unsigned long drm_gem_lru_scan(struct drm_gem_lru *lru,
			       unsigned int nr_to_scan,
			       unsigned long *remaining,
			       bool (*shrink)(struct drm_gem_object *obj));

int drm_gem_evict(struct drm_gem_object *obj);

/**
 * drm_gem_object_is_shared_for_memory_stats - helper for shared memory stats
 *
 * This helper should only be used for fdinfo shared memory stats to determine
 * if a GEM object is shared.
 *
 * @obj: obj in question
 */
static inline bool drm_gem_object_is_shared_for_memory_stats(struct drm_gem_object *obj)
{}

#ifdef CONFIG_LOCKDEP
/**
 * drm_gem_gpuva_set_lock() - Set the lock protecting accesses to the gpuva list.
 * @obj: the &drm_gem_object
 * @lock: the lock used to protect the gpuva list. The locking primitive
 * must contain a dep_map field.
 *
 * Call this if you're not proctecting access to the gpuva list with the
 * dma-resv lock, but with a custom lock.
 */
#define drm_gem_gpuva_set_lock(obj, lock)
#define drm_gem_gpuva_assert_lock_held(obj)
#else
#define drm_gem_gpuva_set_lock
#define drm_gem_gpuva_assert_lock_held
#endif

/**
 * drm_gem_gpuva_init() - initialize the gpuva list of a GEM object
 * @obj: the &drm_gem_object
 *
 * This initializes the &drm_gem_object's &drm_gpuvm_bo list.
 *
 * Calling this function is only necessary for drivers intending to support the
 * &drm_driver_feature DRIVER_GEM_GPUVA.
 *
 * See also drm_gem_gpuva_set_lock().
 */
static inline void drm_gem_gpuva_init(struct drm_gem_object *obj)
{}

/**
 * drm_gem_for_each_gpuvm_bo() - iterator to walk over a list of &drm_gpuvm_bo
 * @entry__: &drm_gpuvm_bo structure to assign to in each iteration step
 * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
 *
 * This iterator walks over all &drm_gpuvm_bo structures associated with the
 * &drm_gem_object.
 */
#define drm_gem_for_each_gpuvm_bo(entry__, obj__)

/**
 * drm_gem_for_each_gpuvm_bo_safe() - iterator to safely walk over a list of
 * &drm_gpuvm_bo
 * @entry__: &drm_gpuvm_bostructure to assign to in each iteration step
 * @next__: &next &drm_gpuvm_bo to store the next step
 * @obj__: the &drm_gem_object the &drm_gpuvm_bo to walk are associated with
 *
 * This iterator walks over all &drm_gpuvm_bo structures associated with the
 * &drm_gem_object. It is implemented with list_for_each_entry_safe(), hence
 * it is save against removal of elements.
 */
#define drm_gem_for_each_gpuvm_bo_safe(entry__, next__, obj__)

#endif /* __DRM_GEM_H__ */