18c2ecf20Sopenharmony_ci/**************************************************************************
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
48c2ecf20Sopenharmony_ci * All Rights Reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
78c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the
88c2ecf20Sopenharmony_ci * "Software"), to deal in the Software without restriction, including
98c2ecf20Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish,
108c2ecf20Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to
118c2ecf20Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to
128c2ecf20Sopenharmony_ci * the following conditions:
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the
158c2ecf20Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions
168c2ecf20Sopenharmony_ci * of the Software.
178c2ecf20Sopenharmony_ci *
188c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
198c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
208c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
218c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
228c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
238c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
248c2ecf20Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
258c2ecf20Sopenharmony_ci *
268c2ecf20Sopenharmony_ci **************************************************************************/
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
298c2ecf20Sopenharmony_ci */
308c2ecf20Sopenharmony_ci/** @file ttm_object.h
318c2ecf20Sopenharmony_ci *
328c2ecf20Sopenharmony_ci * Base- and reference object implementation for the various
338c2ecf20Sopenharmony_ci * ttm objects. Implements reference counting, minimal security checks
348c2ecf20Sopenharmony_ci * and release on file close.
358c2ecf20Sopenharmony_ci */
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifndef _TTM_OBJECT_H_
388c2ecf20Sopenharmony_ci#define _TTM_OBJECT_H_
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#include <linux/dma-buf.h>
418c2ecf20Sopenharmony_ci#include <linux/kref.h>
428c2ecf20Sopenharmony_ci#include <linux/list.h>
438c2ecf20Sopenharmony_ci#include <linux/rcupdate.h>
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#include <drm/drm_hashtab.h>
468c2ecf20Sopenharmony_ci#include <drm/ttm/ttm_memory.h>
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci/**
498c2ecf20Sopenharmony_ci * enum ttm_ref_type
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * Describes what type of reference a ref object holds.
528c2ecf20Sopenharmony_ci *
538c2ecf20Sopenharmony_ci * TTM_REF_USAGE is a simple refcount on a base object.
548c2ecf20Sopenharmony_ci *
558c2ecf20Sopenharmony_ci * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a
568c2ecf20Sopenharmony_ci * buffer object.
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a
598c2ecf20Sopenharmony_ci * buffer object.
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci */
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_cienum ttm_ref_type {
648c2ecf20Sopenharmony_ci	TTM_REF_USAGE,
658c2ecf20Sopenharmony_ci	TTM_REF_SYNCCPU_READ,
668c2ecf20Sopenharmony_ci	TTM_REF_SYNCCPU_WRITE,
678c2ecf20Sopenharmony_ci	TTM_REF_NUM
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci/**
718c2ecf20Sopenharmony_ci * enum ttm_object_type
728c2ecf20Sopenharmony_ci *
738c2ecf20Sopenharmony_ci * One entry per ttm object type.
748c2ecf20Sopenharmony_ci * Device-specific types should use the
758c2ecf20Sopenharmony_ci * ttm_driver_typex types.
768c2ecf20Sopenharmony_ci */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_cienum ttm_object_type {
798c2ecf20Sopenharmony_ci	ttm_fence_type,
808c2ecf20Sopenharmony_ci	ttm_buffer_type,
818c2ecf20Sopenharmony_ci	ttm_lock_type,
828c2ecf20Sopenharmony_ci	ttm_prime_type,
838c2ecf20Sopenharmony_ci	ttm_driver_type0 = 256,
848c2ecf20Sopenharmony_ci	ttm_driver_type1,
858c2ecf20Sopenharmony_ci	ttm_driver_type2,
868c2ecf20Sopenharmony_ci	ttm_driver_type3,
878c2ecf20Sopenharmony_ci	ttm_driver_type4,
888c2ecf20Sopenharmony_ci	ttm_driver_type5
898c2ecf20Sopenharmony_ci};
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistruct ttm_object_file;
928c2ecf20Sopenharmony_cistruct ttm_object_device;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/**
958c2ecf20Sopenharmony_ci * struct ttm_base_object
968c2ecf20Sopenharmony_ci *
978c2ecf20Sopenharmony_ci * @hash: hash entry for the per-device object hash.
988c2ecf20Sopenharmony_ci * @type: derived type this object is base class for.
998c2ecf20Sopenharmony_ci * @shareable: Other ttm_object_files can access this object.
1008c2ecf20Sopenharmony_ci *
1018c2ecf20Sopenharmony_ci * @tfile: Pointer to ttm_object_file of the creator.
1028c2ecf20Sopenharmony_ci * NULL if the object was not created by a user request.
1038c2ecf20Sopenharmony_ci * (kernel object).
1048c2ecf20Sopenharmony_ci *
1058c2ecf20Sopenharmony_ci * @refcount: Number of references to this object, not
1068c2ecf20Sopenharmony_ci * including the hash entry. A reference to a base object can
1078c2ecf20Sopenharmony_ci * only be held by a ref object.
1088c2ecf20Sopenharmony_ci *
1098c2ecf20Sopenharmony_ci * @refcount_release: A function to be called when there are
1108c2ecf20Sopenharmony_ci * no more references to this object. This function should
1118c2ecf20Sopenharmony_ci * destroy the object (or make sure destruction eventually happens),
1128c2ecf20Sopenharmony_ci * and when it is called, the object has
1138c2ecf20Sopenharmony_ci * already been taken out of the per-device hash. The parameter
1148c2ecf20Sopenharmony_ci * "base" should be set to NULL by the function.
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci * @ref_obj_release: A function to be called when a reference object
1178c2ecf20Sopenharmony_ci * with another ttm_ref_type than TTM_REF_USAGE is deleted.
1188c2ecf20Sopenharmony_ci * This function may, for example, release a lock held by a user-space
1198c2ecf20Sopenharmony_ci * process.
1208c2ecf20Sopenharmony_ci *
1218c2ecf20Sopenharmony_ci * This struct is intended to be used as a base struct for objects that
1228c2ecf20Sopenharmony_ci * are visible to user-space. It provides a global name, race-safe
1238c2ecf20Sopenharmony_ci * access and refcounting, minimal access contol and hooks for unref actions.
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistruct ttm_base_object {
1278c2ecf20Sopenharmony_ci	struct rcu_head rhead;
1288c2ecf20Sopenharmony_ci	struct ttm_object_file *tfile;
1298c2ecf20Sopenharmony_ci	struct kref refcount;
1308c2ecf20Sopenharmony_ci	void (*refcount_release) (struct ttm_base_object **base);
1318c2ecf20Sopenharmony_ci	void (*ref_obj_release) (struct ttm_base_object *base,
1328c2ecf20Sopenharmony_ci				 enum ttm_ref_type ref_type);
1338c2ecf20Sopenharmony_ci	u32 handle;
1348c2ecf20Sopenharmony_ci	enum ttm_object_type object_type;
1358c2ecf20Sopenharmony_ci	u32 shareable;
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci/**
1408c2ecf20Sopenharmony_ci * struct ttm_prime_object - Modified base object that is prime-aware
1418c2ecf20Sopenharmony_ci *
1428c2ecf20Sopenharmony_ci * @base: struct ttm_base_object that we derive from
1438c2ecf20Sopenharmony_ci * @mutex: Mutex protecting the @dma_buf member.
1448c2ecf20Sopenharmony_ci * @size: Size of the dma_buf associated with this object
1458c2ecf20Sopenharmony_ci * @real_type: Type of the underlying object. Needed since we're setting
1468c2ecf20Sopenharmony_ci * the value of @base::object_type to ttm_prime_type
1478c2ecf20Sopenharmony_ci * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this
1488c2ecf20Sopenharmony_ci * object.
1498c2ecf20Sopenharmony_ci * @refcount_release: The underlying object's release method. Needed since
1508c2ecf20Sopenharmony_ci * we set @base::refcount_release to our own release method.
1518c2ecf20Sopenharmony_ci */
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_cistruct ttm_prime_object {
1548c2ecf20Sopenharmony_ci	struct ttm_base_object base;
1558c2ecf20Sopenharmony_ci	struct mutex mutex;
1568c2ecf20Sopenharmony_ci	size_t size;
1578c2ecf20Sopenharmony_ci	enum ttm_object_type real_type;
1588c2ecf20Sopenharmony_ci	struct dma_buf *dma_buf;
1598c2ecf20Sopenharmony_ci	void (*refcount_release) (struct ttm_base_object **);
1608c2ecf20Sopenharmony_ci};
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/**
1638c2ecf20Sopenharmony_ci * ttm_base_object_init
1648c2ecf20Sopenharmony_ci *
1658c2ecf20Sopenharmony_ci * @tfile: Pointer to a struct ttm_object_file.
1668c2ecf20Sopenharmony_ci * @base: The struct ttm_base_object to initialize.
1678c2ecf20Sopenharmony_ci * @shareable: This object is shareable with other applcations.
1688c2ecf20Sopenharmony_ci * (different @tfile pointers.)
1698c2ecf20Sopenharmony_ci * @type: The object type.
1708c2ecf20Sopenharmony_ci * @refcount_release: See the struct ttm_base_object description.
1718c2ecf20Sopenharmony_ci * @ref_obj_release: See the struct ttm_base_object description.
1728c2ecf20Sopenharmony_ci *
1738c2ecf20Sopenharmony_ci * Initializes a struct ttm_base_object.
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ciextern int ttm_base_object_init(struct ttm_object_file *tfile,
1778c2ecf20Sopenharmony_ci				struct ttm_base_object *base,
1788c2ecf20Sopenharmony_ci				bool shareable,
1798c2ecf20Sopenharmony_ci				enum ttm_object_type type,
1808c2ecf20Sopenharmony_ci				void (*refcount_release) (struct ttm_base_object
1818c2ecf20Sopenharmony_ci							  **),
1828c2ecf20Sopenharmony_ci				void (*ref_obj_release) (struct ttm_base_object
1838c2ecf20Sopenharmony_ci							 *,
1848c2ecf20Sopenharmony_ci							 enum ttm_ref_type
1858c2ecf20Sopenharmony_ci							 ref_type));
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/**
1888c2ecf20Sopenharmony_ci * ttm_base_object_lookup
1898c2ecf20Sopenharmony_ci *
1908c2ecf20Sopenharmony_ci * @tfile: Pointer to a struct ttm_object_file.
1918c2ecf20Sopenharmony_ci * @key: Hash key
1928c2ecf20Sopenharmony_ci *
1938c2ecf20Sopenharmony_ci * Looks up a struct ttm_base_object with the key @key.
1948c2ecf20Sopenharmony_ci */
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ciextern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
1978c2ecf20Sopenharmony_ci						      *tfile, uint32_t key);
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci/**
2008c2ecf20Sopenharmony_ci * ttm_base_object_lookup_for_ref
2018c2ecf20Sopenharmony_ci *
2028c2ecf20Sopenharmony_ci * @tdev: Pointer to a struct ttm_object_device.
2038c2ecf20Sopenharmony_ci * @key: Hash key
2048c2ecf20Sopenharmony_ci *
2058c2ecf20Sopenharmony_ci * Looks up a struct ttm_base_object with the key @key.
2068c2ecf20Sopenharmony_ci * This function should only be used when the struct tfile associated with the
2078c2ecf20Sopenharmony_ci * caller doesn't yet have a reference to the base object.
2088c2ecf20Sopenharmony_ci */
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ciextern struct ttm_base_object *
2118c2ecf20Sopenharmony_cittm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci/**
2148c2ecf20Sopenharmony_ci * ttm_base_object_unref
2158c2ecf20Sopenharmony_ci *
2168c2ecf20Sopenharmony_ci * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
2178c2ecf20Sopenharmony_ci *
2188c2ecf20Sopenharmony_ci * Decrements the base object refcount and clears the pointer pointed to by
2198c2ecf20Sopenharmony_ci * p_base.
2208c2ecf20Sopenharmony_ci */
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ciextern void ttm_base_object_unref(struct ttm_base_object **p_base);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci/**
2258c2ecf20Sopenharmony_ci * ttm_ref_object_add.
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * @tfile: A struct ttm_object_file representing the application owning the
2288c2ecf20Sopenharmony_ci * ref_object.
2298c2ecf20Sopenharmony_ci * @base: The base object to reference.
2308c2ecf20Sopenharmony_ci * @ref_type: The type of reference.
2318c2ecf20Sopenharmony_ci * @existed: Upon completion, indicates that an identical reference object
2328c2ecf20Sopenharmony_ci * already existed, and the refcount was upped on that object instead.
2338c2ecf20Sopenharmony_ci * @require_existed: Fail with -EPERM if an identical ref object didn't
2348c2ecf20Sopenharmony_ci * already exist.
2358c2ecf20Sopenharmony_ci *
2368c2ecf20Sopenharmony_ci * Checks that the base object is shareable and adds a ref object to it.
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci * Adding a ref object to a base object is basically like referencing the
2398c2ecf20Sopenharmony_ci * base object, but a user-space application holds the reference. When the
2408c2ecf20Sopenharmony_ci * file corresponding to @tfile is closed, all its reference objects are
2418c2ecf20Sopenharmony_ci * deleted. A reference object can have different types depending on what
2428c2ecf20Sopenharmony_ci * it's intended for. It can be refcounting to prevent object destruction,
2438c2ecf20Sopenharmony_ci * When user-space takes a lock, it can add a ref object to that lock to
2448c2ecf20Sopenharmony_ci * make sure the lock is released if the application dies. A ref object
2458c2ecf20Sopenharmony_ci * will hold a single reference on a base object.
2468c2ecf20Sopenharmony_ci */
2478c2ecf20Sopenharmony_ciextern int ttm_ref_object_add(struct ttm_object_file *tfile,
2488c2ecf20Sopenharmony_ci			      struct ttm_base_object *base,
2498c2ecf20Sopenharmony_ci			      enum ttm_ref_type ref_type, bool *existed,
2508c2ecf20Sopenharmony_ci			      bool require_existed);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ciextern bool ttm_ref_object_exists(struct ttm_object_file *tfile,
2538c2ecf20Sopenharmony_ci				  struct ttm_base_object *base);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci/**
2568c2ecf20Sopenharmony_ci * ttm_ref_object_base_unref
2578c2ecf20Sopenharmony_ci *
2588c2ecf20Sopenharmony_ci * @key: Key representing the base object.
2598c2ecf20Sopenharmony_ci * @ref_type: Ref type of the ref object to be dereferenced.
2608c2ecf20Sopenharmony_ci *
2618c2ecf20Sopenharmony_ci * Unreference a ref object with type @ref_type
2628c2ecf20Sopenharmony_ci * on the base object identified by @key. If there are no duplicate
2638c2ecf20Sopenharmony_ci * references, the ref object will be destroyed and the base object
2648c2ecf20Sopenharmony_ci * will be unreferenced.
2658c2ecf20Sopenharmony_ci */
2668c2ecf20Sopenharmony_ciextern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
2678c2ecf20Sopenharmony_ci				     unsigned long key,
2688c2ecf20Sopenharmony_ci				     enum ttm_ref_type ref_type);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/**
2718c2ecf20Sopenharmony_ci * ttm_object_file_init - initialize a struct ttm_object file
2728c2ecf20Sopenharmony_ci *
2738c2ecf20Sopenharmony_ci * @tdev: A struct ttm_object device this file is initialized on.
2748c2ecf20Sopenharmony_ci * @hash_order: Order of the hash table used to hold the reference objects.
2758c2ecf20Sopenharmony_ci *
2768c2ecf20Sopenharmony_ci * This is typically called by the file_ops::open function.
2778c2ecf20Sopenharmony_ci */
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ciextern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
2808c2ecf20Sopenharmony_ci						    *tdev,
2818c2ecf20Sopenharmony_ci						    unsigned int hash_order);
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci/**
2848c2ecf20Sopenharmony_ci * ttm_object_file_release - release data held by a ttm_object_file
2858c2ecf20Sopenharmony_ci *
2868c2ecf20Sopenharmony_ci * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
2878c2ecf20Sopenharmony_ci * *p_tfile will be set to NULL by this function.
2888c2ecf20Sopenharmony_ci *
2898c2ecf20Sopenharmony_ci * Releases all data associated by a ttm_object_file.
2908c2ecf20Sopenharmony_ci * Typically called from file_ops::release. The caller must
2918c2ecf20Sopenharmony_ci * ensure that there are no concurrent users of tfile.
2928c2ecf20Sopenharmony_ci */
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ciextern void ttm_object_file_release(struct ttm_object_file **p_tfile);
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci/**
2978c2ecf20Sopenharmony_ci * ttm_object device init - initialize a struct ttm_object_device
2988c2ecf20Sopenharmony_ci *
2998c2ecf20Sopenharmony_ci * @mem_glob: struct ttm_mem_global for memory accounting.
3008c2ecf20Sopenharmony_ci * @hash_order: Order of hash table used to hash the base objects.
3018c2ecf20Sopenharmony_ci * @ops: DMA buf ops for prime objects of this device.
3028c2ecf20Sopenharmony_ci *
3038c2ecf20Sopenharmony_ci * This function is typically called on device initialization to prepare
3048c2ecf20Sopenharmony_ci * data structures needed for ttm base and ref objects.
3058c2ecf20Sopenharmony_ci */
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ciextern struct ttm_object_device *
3088c2ecf20Sopenharmony_cittm_object_device_init(struct ttm_mem_global *mem_glob,
3098c2ecf20Sopenharmony_ci		       unsigned int hash_order,
3108c2ecf20Sopenharmony_ci		       const struct dma_buf_ops *ops);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci/**
3138c2ecf20Sopenharmony_ci * ttm_object_device_release - release data held by a ttm_object_device
3148c2ecf20Sopenharmony_ci *
3158c2ecf20Sopenharmony_ci * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
3168c2ecf20Sopenharmony_ci * *p_tdev will be set to NULL by this function.
3178c2ecf20Sopenharmony_ci *
3188c2ecf20Sopenharmony_ci * Releases all data associated by a ttm_object_device.
3198c2ecf20Sopenharmony_ci * Typically called from driver::unload before the destruction of the
3208c2ecf20Sopenharmony_ci * device private data structure.
3218c2ecf20Sopenharmony_ci */
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ciextern void ttm_object_device_release(struct ttm_object_device **p_tdev);
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci#define ttm_base_object_kfree(__object, __base)\
3268c2ecf20Sopenharmony_ci	kfree_rcu(__object, __base.rhead)
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ciextern int ttm_prime_object_init(struct ttm_object_file *tfile,
3298c2ecf20Sopenharmony_ci				 size_t size,
3308c2ecf20Sopenharmony_ci				 struct ttm_prime_object *prime,
3318c2ecf20Sopenharmony_ci				 bool shareable,
3328c2ecf20Sopenharmony_ci				 enum ttm_object_type type,
3338c2ecf20Sopenharmony_ci				 void (*refcount_release)
3348c2ecf20Sopenharmony_ci				 (struct ttm_base_object **),
3358c2ecf20Sopenharmony_ci				 void (*ref_obj_release)
3368c2ecf20Sopenharmony_ci				 (struct ttm_base_object *,
3378c2ecf20Sopenharmony_ci				  enum ttm_ref_type ref_type));
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic inline enum ttm_object_type
3408c2ecf20Sopenharmony_cittm_base_object_type(struct ttm_base_object *base)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	return (base->object_type == ttm_prime_type) ?
3438c2ecf20Sopenharmony_ci		container_of(base, struct ttm_prime_object, base)->real_type :
3448c2ecf20Sopenharmony_ci		base->object_type;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ciextern int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
3478c2ecf20Sopenharmony_ci				  int fd, u32 *handle);
3488c2ecf20Sopenharmony_ciextern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
3498c2ecf20Sopenharmony_ci				  uint32_t handle, uint32_t flags,
3508c2ecf20Sopenharmony_ci				  int *prime_fd);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci#define ttm_prime_object_kfree(__obj, __prime)		\
3538c2ecf20Sopenharmony_ci	kfree_rcu(__obj, __prime.base.rhead)
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci/*
3568c2ecf20Sopenharmony_ci * Extra memory required by the base object's idr storage, which is allocated
3578c2ecf20Sopenharmony_ci * separately from the base object itself. We estimate an on-average 128 bytes
3588c2ecf20Sopenharmony_ci * per idr.
3598c2ecf20Sopenharmony_ci */
3608c2ecf20Sopenharmony_ci#define TTM_OBJ_EXTRA_SIZE 128
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistruct ttm_base_object *
3638c2ecf20Sopenharmony_cittm_base_object_noref_lookup(struct ttm_object_file *tfile, uint32_t key);
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci/**
3668c2ecf20Sopenharmony_ci * ttm_base_object_noref_release - release a base object pointer looked up
3678c2ecf20Sopenharmony_ci * without reference
3688c2ecf20Sopenharmony_ci *
3698c2ecf20Sopenharmony_ci * Releases a base object pointer looked up with ttm_base_object_noref_lookup().
3708c2ecf20Sopenharmony_ci */
3718c2ecf20Sopenharmony_cistatic inline void ttm_base_object_noref_release(void)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	__acquire(RCU);
3748c2ecf20Sopenharmony_ci	rcu_read_unlock();
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci#endif
377