162306a36Sopenharmony_ci/************************************************************************** 262306a36Sopenharmony_ci * 362306a36Sopenharmony_ci * Copyright (c) 2006-2022 VMware, Inc., Palo Alto, CA., USA 462306a36Sopenharmony_ci * All Rights Reserved. 562306a36Sopenharmony_ci * 662306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 762306a36Sopenharmony_ci * copy of this software and associated documentation files (the 862306a36Sopenharmony_ci * "Software"), to deal in the Software without restriction, including 962306a36Sopenharmony_ci * without limitation the rights to use, copy, modify, merge, publish, 1062306a36Sopenharmony_ci * distribute, sub license, and/or sell copies of the Software, and to 1162306a36Sopenharmony_ci * permit persons to whom the Software is furnished to do so, subject to 1262306a36Sopenharmony_ci * the following conditions: 1362306a36Sopenharmony_ci * 1462306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the 1562306a36Sopenharmony_ci * next paragraph) shall be included in all copies or substantial portions 1662306a36Sopenharmony_ci * of the Software. 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1962306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2062306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL 2162306a36Sopenharmony_ci * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, 2262306a36Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 2362306a36Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 2462306a36Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci **************************************************************************/ 2762306a36Sopenharmony_ci/* 2862306a36Sopenharmony_ci * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> 2962306a36Sopenharmony_ci */ 3062306a36Sopenharmony_ci/** @file ttm_object.h 3162306a36Sopenharmony_ci * 3262306a36Sopenharmony_ci * Base- and reference object implementation for the various 3362306a36Sopenharmony_ci * ttm objects. Implements reference counting, minimal security checks 3462306a36Sopenharmony_ci * and release on file close. 3562306a36Sopenharmony_ci */ 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci#ifndef _TTM_OBJECT_H_ 3862306a36Sopenharmony_ci#define _TTM_OBJECT_H_ 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci#include <linux/dma-buf.h> 4162306a36Sopenharmony_ci#include <linux/kref.h> 4262306a36Sopenharmony_ci#include <linux/list.h> 4362306a36Sopenharmony_ci#include <linux/rcupdate.h> 4462306a36Sopenharmony_ci 4562306a36Sopenharmony_ci#include <drm/ttm/ttm_bo.h> 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/** 4862306a36Sopenharmony_ci * enum ttm_object_type 4962306a36Sopenharmony_ci * 5062306a36Sopenharmony_ci * One entry per ttm object type. 5162306a36Sopenharmony_ci * Device-specific types should use the 5262306a36Sopenharmony_ci * ttm_driver_typex types. 5362306a36Sopenharmony_ci */ 5462306a36Sopenharmony_ci 5562306a36Sopenharmony_cienum ttm_object_type { 5662306a36Sopenharmony_ci ttm_fence_type, 5762306a36Sopenharmony_ci ttm_lock_type, 5862306a36Sopenharmony_ci ttm_prime_type, 5962306a36Sopenharmony_ci ttm_driver_type0 = 256, 6062306a36Sopenharmony_ci ttm_driver_type1, 6162306a36Sopenharmony_ci ttm_driver_type2, 6262306a36Sopenharmony_ci ttm_driver_type3, 6362306a36Sopenharmony_ci ttm_driver_type4, 6462306a36Sopenharmony_ci ttm_driver_type5 6562306a36Sopenharmony_ci}; 6662306a36Sopenharmony_ci 6762306a36Sopenharmony_cistruct ttm_object_file; 6862306a36Sopenharmony_cistruct ttm_object_device; 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci/** 7162306a36Sopenharmony_ci * struct ttm_base_object 7262306a36Sopenharmony_ci * 7362306a36Sopenharmony_ci * @hash: hash entry for the per-device object hash. 7462306a36Sopenharmony_ci * @type: derived type this object is base class for. 7562306a36Sopenharmony_ci * @shareable: Other ttm_object_files can access this object. 7662306a36Sopenharmony_ci * 7762306a36Sopenharmony_ci * @tfile: Pointer to ttm_object_file of the creator. 7862306a36Sopenharmony_ci * NULL if the object was not created by a user request. 7962306a36Sopenharmony_ci * (kernel object). 8062306a36Sopenharmony_ci * 8162306a36Sopenharmony_ci * @refcount: Number of references to this object, not 8262306a36Sopenharmony_ci * including the hash entry. A reference to a base object can 8362306a36Sopenharmony_ci * only be held by a ref object. 8462306a36Sopenharmony_ci * 8562306a36Sopenharmony_ci * @refcount_release: A function to be called when there are 8662306a36Sopenharmony_ci * no more references to this object. This function should 8762306a36Sopenharmony_ci * destroy the object (or make sure destruction eventually happens), 8862306a36Sopenharmony_ci * and when it is called, the object has 8962306a36Sopenharmony_ci * already been taken out of the per-device hash. The parameter 9062306a36Sopenharmony_ci * "base" should be set to NULL by the function. 9162306a36Sopenharmony_ci * 9262306a36Sopenharmony_ci * @ref_obj_release: A function to be called when a reference object 9362306a36Sopenharmony_ci * with another ttm_ref_type than TTM_REF_USAGE is deleted. 9462306a36Sopenharmony_ci * This function may, for example, release a lock held by a user-space 9562306a36Sopenharmony_ci * process. 9662306a36Sopenharmony_ci * 9762306a36Sopenharmony_ci * This struct is intended to be used as a base struct for objects that 9862306a36Sopenharmony_ci * are visible to user-space. It provides a global name, race-safe 9962306a36Sopenharmony_ci * access and refcounting, minimal access control and hooks for unref actions. 10062306a36Sopenharmony_ci */ 10162306a36Sopenharmony_ci 10262306a36Sopenharmony_cistruct ttm_base_object { 10362306a36Sopenharmony_ci struct rcu_head rhead; 10462306a36Sopenharmony_ci struct ttm_object_file *tfile; 10562306a36Sopenharmony_ci struct kref refcount; 10662306a36Sopenharmony_ci void (*refcount_release) (struct ttm_base_object **base); 10762306a36Sopenharmony_ci u64 handle; 10862306a36Sopenharmony_ci enum ttm_object_type object_type; 10962306a36Sopenharmony_ci u32 shareable; 11062306a36Sopenharmony_ci}; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci/** 11462306a36Sopenharmony_ci * struct ttm_prime_object - Modified base object that is prime-aware 11562306a36Sopenharmony_ci * 11662306a36Sopenharmony_ci * @base: struct ttm_base_object that we derive from 11762306a36Sopenharmony_ci * @mutex: Mutex protecting the @dma_buf member. 11862306a36Sopenharmony_ci * @size: Size of the dma_buf associated with this object 11962306a36Sopenharmony_ci * @real_type: Type of the underlying object. Needed since we're setting 12062306a36Sopenharmony_ci * the value of @base::object_type to ttm_prime_type 12162306a36Sopenharmony_ci * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this 12262306a36Sopenharmony_ci * object. 12362306a36Sopenharmony_ci * @refcount_release: The underlying object's release method. Needed since 12462306a36Sopenharmony_ci * we set @base::refcount_release to our own release method. 12562306a36Sopenharmony_ci */ 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_cistruct ttm_prime_object { 12862306a36Sopenharmony_ci struct ttm_base_object base; 12962306a36Sopenharmony_ci struct mutex mutex; 13062306a36Sopenharmony_ci size_t size; 13162306a36Sopenharmony_ci enum ttm_object_type real_type; 13262306a36Sopenharmony_ci struct dma_buf *dma_buf; 13362306a36Sopenharmony_ci void (*refcount_release) (struct ttm_base_object **); 13462306a36Sopenharmony_ci}; 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci/** 13762306a36Sopenharmony_ci * ttm_base_object_init 13862306a36Sopenharmony_ci * 13962306a36Sopenharmony_ci * @tfile: Pointer to a struct ttm_object_file. 14062306a36Sopenharmony_ci * @base: The struct ttm_base_object to initialize. 14162306a36Sopenharmony_ci * @shareable: This object is shareable with other applications. 14262306a36Sopenharmony_ci * (different @tfile pointers.) 14362306a36Sopenharmony_ci * @type: The object type. 14462306a36Sopenharmony_ci * @refcount_release: See the struct ttm_base_object description. 14562306a36Sopenharmony_ci * @ref_obj_release: See the struct ttm_base_object description. 14662306a36Sopenharmony_ci * 14762306a36Sopenharmony_ci * Initializes a struct ttm_base_object. 14862306a36Sopenharmony_ci */ 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ciextern int ttm_base_object_init(struct ttm_object_file *tfile, 15162306a36Sopenharmony_ci struct ttm_base_object *base, 15262306a36Sopenharmony_ci bool shareable, 15362306a36Sopenharmony_ci enum ttm_object_type type, 15462306a36Sopenharmony_ci void (*refcount_release) (struct ttm_base_object 15562306a36Sopenharmony_ci **)); 15662306a36Sopenharmony_ci 15762306a36Sopenharmony_ci/** 15862306a36Sopenharmony_ci * ttm_base_object_lookup 15962306a36Sopenharmony_ci * 16062306a36Sopenharmony_ci * @tfile: Pointer to a struct ttm_object_file. 16162306a36Sopenharmony_ci * @key: Hash key 16262306a36Sopenharmony_ci * 16362306a36Sopenharmony_ci * Looks up a struct ttm_base_object with the key @key. 16462306a36Sopenharmony_ci */ 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ciextern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file 16762306a36Sopenharmony_ci *tfile, uint64_t key); 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci/** 17062306a36Sopenharmony_ci * ttm_base_object_lookup_for_ref 17162306a36Sopenharmony_ci * 17262306a36Sopenharmony_ci * @tdev: Pointer to a struct ttm_object_device. 17362306a36Sopenharmony_ci * @key: Hash key 17462306a36Sopenharmony_ci * 17562306a36Sopenharmony_ci * Looks up a struct ttm_base_object with the key @key. 17662306a36Sopenharmony_ci * This function should only be used when the struct tfile associated with the 17762306a36Sopenharmony_ci * caller doesn't yet have a reference to the base object. 17862306a36Sopenharmony_ci */ 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ciextern struct ttm_base_object * 18162306a36Sopenharmony_cittm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint64_t key); 18262306a36Sopenharmony_ci 18362306a36Sopenharmony_ci/** 18462306a36Sopenharmony_ci * ttm_base_object_unref 18562306a36Sopenharmony_ci * 18662306a36Sopenharmony_ci * @p_base: Pointer to a pointer referencing a struct ttm_base_object. 18762306a36Sopenharmony_ci * 18862306a36Sopenharmony_ci * Decrements the base object refcount and clears the pointer pointed to by 18962306a36Sopenharmony_ci * p_base. 19062306a36Sopenharmony_ci */ 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ciextern void ttm_base_object_unref(struct ttm_base_object **p_base); 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_ci/** 19562306a36Sopenharmony_ci * ttm_ref_object_add. 19662306a36Sopenharmony_ci * 19762306a36Sopenharmony_ci * @tfile: A struct ttm_object_file representing the application owning the 19862306a36Sopenharmony_ci * ref_object. 19962306a36Sopenharmony_ci * @base: The base object to reference. 20062306a36Sopenharmony_ci * @ref_type: The type of reference. 20162306a36Sopenharmony_ci * @existed: Upon completion, indicates that an identical reference object 20262306a36Sopenharmony_ci * already existed, and the refcount was upped on that object instead. 20362306a36Sopenharmony_ci * @require_existed: Fail with -EPERM if an identical ref object didn't 20462306a36Sopenharmony_ci * already exist. 20562306a36Sopenharmony_ci * 20662306a36Sopenharmony_ci * Checks that the base object is shareable and adds a ref object to it. 20762306a36Sopenharmony_ci * 20862306a36Sopenharmony_ci * Adding a ref object to a base object is basically like referencing the 20962306a36Sopenharmony_ci * base object, but a user-space application holds the reference. When the 21062306a36Sopenharmony_ci * file corresponding to @tfile is closed, all its reference objects are 21162306a36Sopenharmony_ci * deleted. A reference object can have different types depending on what 21262306a36Sopenharmony_ci * it's intended for. It can be refcounting to prevent object destruction, 21362306a36Sopenharmony_ci * When user-space takes a lock, it can add a ref object to that lock to 21462306a36Sopenharmony_ci * make sure the lock is released if the application dies. A ref object 21562306a36Sopenharmony_ci * will hold a single reference on a base object. 21662306a36Sopenharmony_ci */ 21762306a36Sopenharmony_ciextern int ttm_ref_object_add(struct ttm_object_file *tfile, 21862306a36Sopenharmony_ci struct ttm_base_object *base, 21962306a36Sopenharmony_ci bool *existed, 22062306a36Sopenharmony_ci bool require_existed); 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci/** 22362306a36Sopenharmony_ci * ttm_ref_object_base_unref 22462306a36Sopenharmony_ci * 22562306a36Sopenharmony_ci * @key: Key representing the base object. 22662306a36Sopenharmony_ci * @ref_type: Ref type of the ref object to be dereferenced. 22762306a36Sopenharmony_ci * 22862306a36Sopenharmony_ci * Unreference a ref object with type @ref_type 22962306a36Sopenharmony_ci * on the base object identified by @key. If there are no duplicate 23062306a36Sopenharmony_ci * references, the ref object will be destroyed and the base object 23162306a36Sopenharmony_ci * will be unreferenced. 23262306a36Sopenharmony_ci */ 23362306a36Sopenharmony_ciextern int ttm_ref_object_base_unref(struct ttm_object_file *tfile, 23462306a36Sopenharmony_ci unsigned long key); 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci/** 23762306a36Sopenharmony_ci * ttm_object_file_init - initialize a struct ttm_object file 23862306a36Sopenharmony_ci * 23962306a36Sopenharmony_ci * @tdev: A struct ttm_object device this file is initialized on. 24062306a36Sopenharmony_ci * 24162306a36Sopenharmony_ci * This is typically called by the file_ops::open function. 24262306a36Sopenharmony_ci */ 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ciextern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device 24562306a36Sopenharmony_ci *tdev); 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ci/** 24862306a36Sopenharmony_ci * ttm_object_file_release - release data held by a ttm_object_file 24962306a36Sopenharmony_ci * 25062306a36Sopenharmony_ci * @p_tfile: Pointer to pointer to the ttm_object_file object to release. 25162306a36Sopenharmony_ci * *p_tfile will be set to NULL by this function. 25262306a36Sopenharmony_ci * 25362306a36Sopenharmony_ci * Releases all data associated by a ttm_object_file. 25462306a36Sopenharmony_ci * Typically called from file_ops::release. The caller must 25562306a36Sopenharmony_ci * ensure that there are no concurrent users of tfile. 25662306a36Sopenharmony_ci */ 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ciextern void ttm_object_file_release(struct ttm_object_file **p_tfile); 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci/** 26162306a36Sopenharmony_ci * ttm_object device init - initialize a struct ttm_object_device 26262306a36Sopenharmony_ci * 26362306a36Sopenharmony_ci * @ops: DMA buf ops for prime objects of this device. 26462306a36Sopenharmony_ci * 26562306a36Sopenharmony_ci * This function is typically called on device initialization to prepare 26662306a36Sopenharmony_ci * data structures needed for ttm base and ref objects. 26762306a36Sopenharmony_ci */ 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ciextern struct ttm_object_device * 27062306a36Sopenharmony_cittm_object_device_init(const struct dma_buf_ops *ops); 27162306a36Sopenharmony_ci 27262306a36Sopenharmony_ci/** 27362306a36Sopenharmony_ci * ttm_object_device_release - release data held by a ttm_object_device 27462306a36Sopenharmony_ci * 27562306a36Sopenharmony_ci * @p_tdev: Pointer to pointer to the ttm_object_device object to release. 27662306a36Sopenharmony_ci * *p_tdev will be set to NULL by this function. 27762306a36Sopenharmony_ci * 27862306a36Sopenharmony_ci * Releases all data associated by a ttm_object_device. 27962306a36Sopenharmony_ci * Typically called from driver::unload before the destruction of the 28062306a36Sopenharmony_ci * device private data structure. 28162306a36Sopenharmony_ci */ 28262306a36Sopenharmony_ci 28362306a36Sopenharmony_ciextern void ttm_object_device_release(struct ttm_object_device **p_tdev); 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci#define ttm_base_object_kfree(__object, __base)\ 28662306a36Sopenharmony_ci kfree_rcu(__object, __base.rhead) 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ciextern int ttm_prime_object_init(struct ttm_object_file *tfile, 28962306a36Sopenharmony_ci size_t size, 29062306a36Sopenharmony_ci struct ttm_prime_object *prime, 29162306a36Sopenharmony_ci bool shareable, 29262306a36Sopenharmony_ci enum ttm_object_type type, 29362306a36Sopenharmony_ci void (*refcount_release) 29462306a36Sopenharmony_ci (struct ttm_base_object **)); 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_cistatic inline enum ttm_object_type 29762306a36Sopenharmony_cittm_base_object_type(struct ttm_base_object *base) 29862306a36Sopenharmony_ci{ 29962306a36Sopenharmony_ci return (base->object_type == ttm_prime_type) ? 30062306a36Sopenharmony_ci container_of(base, struct ttm_prime_object, base)->real_type : 30162306a36Sopenharmony_ci base->object_type; 30262306a36Sopenharmony_ci} 30362306a36Sopenharmony_ciextern int ttm_prime_fd_to_handle(struct ttm_object_file *tfile, 30462306a36Sopenharmony_ci int fd, u32 *handle); 30562306a36Sopenharmony_ciextern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile, 30662306a36Sopenharmony_ci uint32_t handle, uint32_t flags, 30762306a36Sopenharmony_ci int *prime_fd); 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ci#define ttm_prime_object_kfree(__obj, __prime) \ 31062306a36Sopenharmony_ci kfree_rcu(__obj, __prime.base.rhead) 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_cistatic inline int ttm_bo_wait(struct ttm_buffer_object *bo, bool intr, 31362306a36Sopenharmony_ci bool no_wait) 31462306a36Sopenharmony_ci{ 31562306a36Sopenharmony_ci struct ttm_operation_ctx ctx = { intr, no_wait }; 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ci return ttm_bo_wait_ctx(bo, &ctx); 31862306a36Sopenharmony_ci} 31962306a36Sopenharmony_ci 32062306a36Sopenharmony_ci#endif 321