/**************************************************************************1*2* Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR18* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,19* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL20* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,21* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR22* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE23* USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/26/*27* Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>28*/29/** @file ttm_object.h30*31* Base- and reference object implementation for the various32* ttm objects. Implements reference counting, minimal security checks33* and release on file close.34*/3536#ifndef _TTM_OBJECT_H_37#define _TTM_OBJECT_H_3839#include <linux/list.h>40#include "drm_hashtab.h"41#include <linux/kref.h>42#include <ttm/ttm_memory.h>4344/**45* enum ttm_ref_type46*47* Describes what type of reference a ref object holds.48*49* TTM_REF_USAGE is a simple refcount on a base object.50*51* TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a52* buffer object.53*54* TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a55* buffer object.56*57*/5859enum ttm_ref_type {60TTM_REF_USAGE,61TTM_REF_SYNCCPU_READ,62TTM_REF_SYNCCPU_WRITE,63TTM_REF_NUM64};6566/**67* enum ttm_object_type68*69* One entry per ttm object type.70* Device-specific types should use the71* ttm_driver_typex types.72*/7374enum ttm_object_type {75ttm_fence_type,76ttm_buffer_type,77ttm_lock_type,78ttm_driver_type0 = 256,79ttm_driver_type1,80ttm_driver_type2,81ttm_driver_type3,82ttm_driver_type4,83ttm_driver_type584};8586struct ttm_object_file;87struct ttm_object_device;8889/**90* struct ttm_base_object91*92* @hash: hash entry for the per-device object hash.93* @type: derived type this object is base class for.94* @shareable: Other ttm_object_files can access this object.95*96* @tfile: Pointer to ttm_object_file of the creator.97* NULL if the object was not created by a user request.98* (kernel object).99*100* @refcount: Number of references to this object, not101* including the hash entry. A reference to a base object can102* only be held by a ref object.103*104* @refcount_release: A function to be called when there are105* no more references to this object. This function should106* destroy the object (or make sure destruction eventually happens),107* and when it is called, the object has108* already been taken out of the per-device hash. The parameter109* "base" should be set to NULL by the function.110*111* @ref_obj_release: A function to be called when a reference object112* with another ttm_ref_type than TTM_REF_USAGE is deleted.113* this function may, for example, release a lock held by a user-space114* process.115*116* This struct is intended to be used as a base struct for objects that117* are visible to user-space. It provides a global name, race-safe118* access and refcounting, minimal access contol and hooks for unref actions.119*/120121struct ttm_base_object {122struct drm_hash_item hash;123enum ttm_object_type object_type;124bool shareable;125struct ttm_object_file *tfile;126struct kref refcount;127void (*refcount_release) (struct ttm_base_object **base);128void (*ref_obj_release) (struct ttm_base_object *base,129enum ttm_ref_type ref_type);130};131132/**133* ttm_base_object_init134*135* @tfile: Pointer to a struct ttm_object_file.136* @base: The struct ttm_base_object to initialize.137* @shareable: This object is shareable with other applcations.138* (different @tfile pointers.)139* @type: The object type.140* @refcount_release: See the struct ttm_base_object description.141* @ref_obj_release: See the struct ttm_base_object description.142*143* Initializes a struct ttm_base_object.144*/145146extern int ttm_base_object_init(struct ttm_object_file *tfile,147struct ttm_base_object *base,148bool shareable,149enum ttm_object_type type,150void (*refcount_release) (struct ttm_base_object151**),152void (*ref_obj_release) (struct ttm_base_object153*,154enum ttm_ref_type155ref_type));156157/**158* ttm_base_object_lookup159*160* @tfile: Pointer to a struct ttm_object_file.161* @key: Hash key162*163* Looks up a struct ttm_base_object with the key @key.164* Also verifies that the object is visible to the application, by165* comparing the @tfile argument and checking the object shareable flag.166*/167168extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file169*tfile, uint32_t key);170171/**172* ttm_base_object_unref173*174* @p_base: Pointer to a pointer referncing a struct ttm_base_object.175*176* Decrements the base object refcount and clears the pointer pointed to by177* p_base.178*/179180extern void ttm_base_object_unref(struct ttm_base_object **p_base);181182/**183* ttm_ref_object_add.184*185* @tfile: A struct ttm_object_file representing the application owning the186* ref_object.187* @base: The base object to reference.188* @ref_type: The type of reference.189* @existed: Upon completion, indicates that an identical reference object190* already existed, and the refcount was upped on that object instead.191*192* Adding a ref object to a base object is basically like referencing the193* base object, but a user-space application holds the reference. When the194* file corresponding to @tfile is closed, all its reference objects are195* deleted. A reference object can have different types depending on what196* it's intended for. It can be refcounting to prevent object destruction,197* When user-space takes a lock, it can add a ref object to that lock to198* make sure the lock is released if the application dies. A ref object199* will hold a single reference on a base object.200*/201extern int ttm_ref_object_add(struct ttm_object_file *tfile,202struct ttm_base_object *base,203enum ttm_ref_type ref_type, bool *existed);204/**205* ttm_ref_object_base_unref206*207* @key: Key representing the base object.208* @ref_type: Ref type of the ref object to be dereferenced.209*210* Unreference a ref object with type @ref_type211* on the base object identified by @key. If there are no duplicate212* references, the ref object will be destroyed and the base object213* will be unreferenced.214*/215extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,216unsigned long key,217enum ttm_ref_type ref_type);218219/**220* ttm_object_file_init - initialize a struct ttm_object file221*222* @tdev: A struct ttm_object device this file is initialized on.223* @hash_order: Order of the hash table used to hold the reference objects.224*225* This is typically called by the file_ops::open function.226*/227228extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device229*tdev,230unsigned int hash_order);231232/**233* ttm_object_file_release - release data held by a ttm_object_file234*235* @p_tfile: Pointer to pointer to the ttm_object_file object to release.236* *p_tfile will be set to NULL by this function.237*238* Releases all data associated by a ttm_object_file.239* Typically called from file_ops::release. The caller must240* ensure that there are no concurrent users of tfile.241*/242243extern void ttm_object_file_release(struct ttm_object_file **p_tfile);244245/**246* ttm_object device init - initialize a struct ttm_object_device247*248* @hash_order: Order of hash table used to hash the base objects.249*250* This function is typically called on device initialization to prepare251* data structures needed for ttm base and ref objects.252*/253254extern struct ttm_object_device *ttm_object_device_init255(struct ttm_mem_global *mem_glob, unsigned int hash_order);256257/**258* ttm_object_device_release - release data held by a ttm_object_device259*260* @p_tdev: Pointer to pointer to the ttm_object_device object to release.261* *p_tdev will be set to NULL by this function.262*263* Releases all data associated by a ttm_object_device.264* Typically called from driver::unload before the destruction of the265* device private data structure.266*/267268extern void ttm_object_device_release(struct ttm_object_device **p_tdev);269270#endif271272273