10b8762e9SThomas Hellstrom /**************************************************************************
20b8762e9SThomas Hellstrom  *
3931e09d8SMaaz Mombasawala  * Copyright (c) 2006-2022 VMware, Inc., Palo Alto, CA., USA
40b8762e9SThomas Hellstrom  * All Rights Reserved.
50b8762e9SThomas Hellstrom  *
60b8762e9SThomas Hellstrom  * Permission is hereby granted, free of charge, to any person obtaining a
70b8762e9SThomas Hellstrom  * copy of this software and associated documentation files (the
80b8762e9SThomas Hellstrom  * "Software"), to deal in the Software without restriction, including
90b8762e9SThomas Hellstrom  * without limitation the rights to use, copy, modify, merge, publish,
100b8762e9SThomas Hellstrom  * distribute, sub license, and/or sell copies of the Software, and to
110b8762e9SThomas Hellstrom  * permit persons to whom the Software is furnished to do so, subject to
120b8762e9SThomas Hellstrom  * the following conditions:
130b8762e9SThomas Hellstrom  *
140b8762e9SThomas Hellstrom  * The above copyright notice and this permission notice (including the
150b8762e9SThomas Hellstrom  * next paragraph) shall be included in all copies or substantial portions
160b8762e9SThomas Hellstrom  * of the Software.
170b8762e9SThomas Hellstrom  *
180b8762e9SThomas Hellstrom  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
190b8762e9SThomas Hellstrom  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
200b8762e9SThomas Hellstrom  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
210b8762e9SThomas Hellstrom  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
220b8762e9SThomas Hellstrom  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
230b8762e9SThomas Hellstrom  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
240b8762e9SThomas Hellstrom  * USE OR OTHER DEALINGS IN THE SOFTWARE.
250b8762e9SThomas Hellstrom  *
260b8762e9SThomas Hellstrom  **************************************************************************/
270b8762e9SThomas Hellstrom /*
280b8762e9SThomas Hellstrom  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
290b8762e9SThomas Hellstrom  */
300b8762e9SThomas Hellstrom /** @file ttm_object.h
310b8762e9SThomas Hellstrom  *
320b8762e9SThomas Hellstrom  * Base- and reference object implementation for the various
330b8762e9SThomas Hellstrom  * ttm objects. Implements reference counting, minimal security checks
340b8762e9SThomas Hellstrom  * and release on file close.
350b8762e9SThomas Hellstrom  */
360b8762e9SThomas Hellstrom 
370b8762e9SThomas Hellstrom #ifndef _TTM_OBJECT_H_
380b8762e9SThomas Hellstrom #define _TTM_OBJECT_H_
390b8762e9SThomas Hellstrom 
400b8762e9SThomas Hellstrom #include <linux/dma-buf.h>
41d5c1f011SSam Ravnborg #include <linux/kref.h>
42d5c1f011SSam Ravnborg #include <linux/list.h>
43d5c1f011SSam Ravnborg #include <linux/rcupdate.h>
44d5c1f011SSam Ravnborg 
45*13acb368SChristian König #include <drm/ttm/ttm_bo.h>
46*13acb368SChristian König 
470b8762e9SThomas Hellstrom /**
480b8762e9SThomas Hellstrom  * enum ttm_object_type
490b8762e9SThomas Hellstrom  *
500b8762e9SThomas Hellstrom  * One entry per ttm object type.
510b8762e9SThomas Hellstrom  * Device-specific types should use the
520b8762e9SThomas Hellstrom  * ttm_driver_typex types.
530b8762e9SThomas Hellstrom  */
540b8762e9SThomas Hellstrom 
550b8762e9SThomas Hellstrom enum ttm_object_type {
560b8762e9SThomas Hellstrom 	ttm_fence_type,
570b8762e9SThomas Hellstrom 	ttm_lock_type,
580b8762e9SThomas Hellstrom 	ttm_prime_type,
590b8762e9SThomas Hellstrom 	ttm_driver_type0 = 256,
600b8762e9SThomas Hellstrom 	ttm_driver_type1,
610b8762e9SThomas Hellstrom 	ttm_driver_type2,
620b8762e9SThomas Hellstrom 	ttm_driver_type3,
630b8762e9SThomas Hellstrom 	ttm_driver_type4,
640b8762e9SThomas Hellstrom 	ttm_driver_type5
650b8762e9SThomas Hellstrom };
660b8762e9SThomas Hellstrom 
670b8762e9SThomas Hellstrom struct ttm_object_file;
680b8762e9SThomas Hellstrom struct ttm_object_device;
690b8762e9SThomas Hellstrom 
700b8762e9SThomas Hellstrom /**
710b8762e9SThomas Hellstrom  * struct ttm_base_object
720b8762e9SThomas Hellstrom  *
730b8762e9SThomas Hellstrom  * @hash: hash entry for the per-device object hash.
740b8762e9SThomas Hellstrom  * @type: derived type this object is base class for.
750b8762e9SThomas Hellstrom  * @shareable: Other ttm_object_files can access this object.
760b8762e9SThomas Hellstrom  *
770b8762e9SThomas Hellstrom  * @tfile: Pointer to ttm_object_file of the creator.
780b8762e9SThomas Hellstrom  * NULL if the object was not created by a user request.
790b8762e9SThomas Hellstrom  * (kernel object).
800b8762e9SThomas Hellstrom  *
810b8762e9SThomas Hellstrom  * @refcount: Number of references to this object, not
820b8762e9SThomas Hellstrom  * including the hash entry. A reference to a base object can
830b8762e9SThomas Hellstrom  * only be held by a ref object.
840b8762e9SThomas Hellstrom  *
850b8762e9SThomas Hellstrom  * @refcount_release: A function to be called when there are
860b8762e9SThomas Hellstrom  * no more references to this object. This function should
870b8762e9SThomas Hellstrom  * destroy the object (or make sure destruction eventually happens),
880b8762e9SThomas Hellstrom  * and when it is called, the object has
890b8762e9SThomas Hellstrom  * already been taken out of the per-device hash. The parameter
900b8762e9SThomas Hellstrom  * "base" should be set to NULL by the function.
910b8762e9SThomas Hellstrom  *
920b8762e9SThomas Hellstrom  * @ref_obj_release: A function to be called when a reference object
930b8762e9SThomas Hellstrom  * with another ttm_ref_type than TTM_REF_USAGE is deleted.
940b8762e9SThomas Hellstrom  * This function may, for example, release a lock held by a user-space
950b8762e9SThomas Hellstrom  * process.
960b8762e9SThomas Hellstrom  *
970b8762e9SThomas Hellstrom  * This struct is intended to be used as a base struct for objects that
980b8762e9SThomas Hellstrom  * are visible to user-space. It provides a global name, race-safe
9905436815STom Rix  * access and refcounting, minimal access control and hooks for unref actions.
1000b8762e9SThomas Hellstrom  */
1010b8762e9SThomas Hellstrom 
1020b8762e9SThomas Hellstrom struct ttm_base_object {
1030b8762e9SThomas Hellstrom 	struct rcu_head rhead;
1040b8762e9SThomas Hellstrom 	struct ttm_object_file *tfile;
1050b8762e9SThomas Hellstrom 	struct kref refcount;
1060b8762e9SThomas Hellstrom 	void (*refcount_release) (struct ttm_base_object **base);
10776a9e07fSMaaz Mombasawala 	u64 handle;
108c7eae626SThomas Hellstrom 	enum ttm_object_type object_type;
109c7eae626SThomas Hellstrom 	u32 shareable;
1100b8762e9SThomas Hellstrom };
1110b8762e9SThomas Hellstrom 
1120b8762e9SThomas Hellstrom 
1130b8762e9SThomas Hellstrom /**
1140b8762e9SThomas Hellstrom  * struct ttm_prime_object - Modified base object that is prime-aware
1150b8762e9SThomas Hellstrom  *
1160b8762e9SThomas Hellstrom  * @base: struct ttm_base_object that we derive from
1170b8762e9SThomas Hellstrom  * @mutex: Mutex protecting the @dma_buf member.
1180b8762e9SThomas Hellstrom  * @size: Size of the dma_buf associated with this object
1190b8762e9SThomas Hellstrom  * @real_type: Type of the underlying object. Needed since we're setting
1200b8762e9SThomas Hellstrom  * the value of @base::object_type to ttm_prime_type
1210b8762e9SThomas Hellstrom  * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this
1220b8762e9SThomas Hellstrom  * object.
1230b8762e9SThomas Hellstrom  * @refcount_release: The underlying object's release method. Needed since
1240b8762e9SThomas Hellstrom  * we set @base::refcount_release to our own release method.
1250b8762e9SThomas Hellstrom  */
1260b8762e9SThomas Hellstrom 
1270b8762e9SThomas Hellstrom struct ttm_prime_object {
1280b8762e9SThomas Hellstrom 	struct ttm_base_object base;
1290b8762e9SThomas Hellstrom 	struct mutex mutex;
1300b8762e9SThomas Hellstrom 	size_t size;
1310b8762e9SThomas Hellstrom 	enum ttm_object_type real_type;
1320b8762e9SThomas Hellstrom 	struct dma_buf *dma_buf;
1330b8762e9SThomas Hellstrom 	void (*refcount_release) (struct ttm_base_object **);
1340b8762e9SThomas Hellstrom };
1350b8762e9SThomas Hellstrom 
1360b8762e9SThomas Hellstrom /**
1370b8762e9SThomas Hellstrom  * ttm_base_object_init
1380b8762e9SThomas Hellstrom  *
1390b8762e9SThomas Hellstrom  * @tfile: Pointer to a struct ttm_object_file.
1400b8762e9SThomas Hellstrom  * @base: The struct ttm_base_object to initialize.
14105436815STom Rix  * @shareable: This object is shareable with other applications.
1420b8762e9SThomas Hellstrom  * (different @tfile pointers.)
1430b8762e9SThomas Hellstrom  * @type: The object type.
1440b8762e9SThomas Hellstrom  * @refcount_release: See the struct ttm_base_object description.
1450b8762e9SThomas Hellstrom  * @ref_obj_release: See the struct ttm_base_object description.
1460b8762e9SThomas Hellstrom  *
1470b8762e9SThomas Hellstrom  * Initializes a struct ttm_base_object.
1480b8762e9SThomas Hellstrom  */
1490b8762e9SThomas Hellstrom 
1500b8762e9SThomas Hellstrom extern int ttm_base_object_init(struct ttm_object_file *tfile,
1510b8762e9SThomas Hellstrom 				struct ttm_base_object *base,
1520b8762e9SThomas Hellstrom 				bool shareable,
1530b8762e9SThomas Hellstrom 				enum ttm_object_type type,
1540b8762e9SThomas Hellstrom 				void (*refcount_release) (struct ttm_base_object
1558afa13a0SZack Rusin 							  **));
1560b8762e9SThomas Hellstrom 
1570b8762e9SThomas Hellstrom /**
1580b8762e9SThomas Hellstrom  * ttm_base_object_lookup
1590b8762e9SThomas Hellstrom  *
1600b8762e9SThomas Hellstrom  * @tfile: Pointer to a struct ttm_object_file.
1610b8762e9SThomas Hellstrom  * @key: Hash key
1620b8762e9SThomas Hellstrom  *
1630b8762e9SThomas Hellstrom  * Looks up a struct ttm_base_object with the key @key.
1640b8762e9SThomas Hellstrom  */
1650b8762e9SThomas Hellstrom 
1660b8762e9SThomas Hellstrom extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
16776a9e07fSMaaz Mombasawala 						      *tfile, uint64_t key);
1680b8762e9SThomas Hellstrom 
1690b8762e9SThomas Hellstrom /**
1700b8762e9SThomas Hellstrom  * ttm_base_object_lookup_for_ref
1710b8762e9SThomas Hellstrom  *
1720b8762e9SThomas Hellstrom  * @tdev: Pointer to a struct ttm_object_device.
1730b8762e9SThomas Hellstrom  * @key: Hash key
1740b8762e9SThomas Hellstrom  *
1750b8762e9SThomas Hellstrom  * Looks up a struct ttm_base_object with the key @key.
1760b8762e9SThomas Hellstrom  * This function should only be used when the struct tfile associated with the
1770b8762e9SThomas Hellstrom  * caller doesn't yet have a reference to the base object.
1780b8762e9SThomas Hellstrom  */
1790b8762e9SThomas Hellstrom 
1800b8762e9SThomas Hellstrom extern struct ttm_base_object *
18176a9e07fSMaaz Mombasawala ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint64_t key);
1820b8762e9SThomas Hellstrom 
1830b8762e9SThomas Hellstrom /**
1840b8762e9SThomas Hellstrom  * ttm_base_object_unref
1850b8762e9SThomas Hellstrom  *
1860b8762e9SThomas Hellstrom  * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
1870b8762e9SThomas Hellstrom  *
1880b8762e9SThomas Hellstrom  * Decrements the base object refcount and clears the pointer pointed to by
1890b8762e9SThomas Hellstrom  * p_base.
1900b8762e9SThomas Hellstrom  */
1910b8762e9SThomas Hellstrom 
1920b8762e9SThomas Hellstrom extern void ttm_base_object_unref(struct ttm_base_object **p_base);
1930b8762e9SThomas Hellstrom 
1940b8762e9SThomas Hellstrom /**
1950b8762e9SThomas Hellstrom  * ttm_ref_object_add.
1960b8762e9SThomas Hellstrom  *
1970b8762e9SThomas Hellstrom  * @tfile: A struct ttm_object_file representing the application owning the
1980b8762e9SThomas Hellstrom  * ref_object.
1990b8762e9SThomas Hellstrom  * @base: The base object to reference.
2000b8762e9SThomas Hellstrom  * @ref_type: The type of reference.
2010b8762e9SThomas Hellstrom  * @existed: Upon completion, indicates that an identical reference object
2020b8762e9SThomas Hellstrom  * already existed, and the refcount was upped on that object instead.
2030b8762e9SThomas Hellstrom  * @require_existed: Fail with -EPERM if an identical ref object didn't
2040b8762e9SThomas Hellstrom  * already exist.
2050b8762e9SThomas Hellstrom  *
2060b8762e9SThomas Hellstrom  * Checks that the base object is shareable and adds a ref object to it.
2070b8762e9SThomas Hellstrom  *
2080b8762e9SThomas Hellstrom  * Adding a ref object to a base object is basically like referencing the
2090b8762e9SThomas Hellstrom  * base object, but a user-space application holds the reference. When the
2100b8762e9SThomas Hellstrom  * file corresponding to @tfile is closed, all its reference objects are
2110b8762e9SThomas Hellstrom  * deleted. A reference object can have different types depending on what
2120b8762e9SThomas Hellstrom  * it's intended for. It can be refcounting to prevent object destruction,
2130b8762e9SThomas Hellstrom  * When user-space takes a lock, it can add a ref object to that lock to
2140b8762e9SThomas Hellstrom  * make sure the lock is released if the application dies. A ref object
2150b8762e9SThomas Hellstrom  * will hold a single reference on a base object.
2160b8762e9SThomas Hellstrom  */
2170b8762e9SThomas Hellstrom extern int ttm_ref_object_add(struct ttm_object_file *tfile,
2180b8762e9SThomas Hellstrom 			      struct ttm_base_object *base,
2198afa13a0SZack Rusin 			      bool *existed,
2200b8762e9SThomas Hellstrom 			      bool require_existed);
2210b8762e9SThomas Hellstrom 
2220b8762e9SThomas Hellstrom /**
2230b8762e9SThomas Hellstrom  * ttm_ref_object_base_unref
2240b8762e9SThomas Hellstrom  *
2250b8762e9SThomas Hellstrom  * @key: Key representing the base object.
2260b8762e9SThomas Hellstrom  * @ref_type: Ref type of the ref object to be dereferenced.
2270b8762e9SThomas Hellstrom  *
2280b8762e9SThomas Hellstrom  * Unreference a ref object with type @ref_type
2290b8762e9SThomas Hellstrom  * on the base object identified by @key. If there are no duplicate
2300b8762e9SThomas Hellstrom  * references, the ref object will be destroyed and the base object
2310b8762e9SThomas Hellstrom  * will be unreferenced.
2320b8762e9SThomas Hellstrom  */
2330b8762e9SThomas Hellstrom extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
2348afa13a0SZack Rusin 				     unsigned long key);
2350b8762e9SThomas Hellstrom 
2360b8762e9SThomas Hellstrom /**
2370b8762e9SThomas Hellstrom  * ttm_object_file_init - initialize a struct ttm_object file
2380b8762e9SThomas Hellstrom  *
2390b8762e9SThomas Hellstrom  * @tdev: A struct ttm_object device this file is initialized on.
2400b8762e9SThomas Hellstrom  *
2410b8762e9SThomas Hellstrom  * This is typically called by the file_ops::open function.
2420b8762e9SThomas Hellstrom  */
2430b8762e9SThomas Hellstrom 
2440b8762e9SThomas Hellstrom extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
24576a9e07fSMaaz Mombasawala 						    *tdev);
2460b8762e9SThomas Hellstrom 
2470b8762e9SThomas Hellstrom /**
2480b8762e9SThomas Hellstrom  * ttm_object_file_release - release data held by a ttm_object_file
2490b8762e9SThomas Hellstrom  *
2500b8762e9SThomas Hellstrom  * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
2510b8762e9SThomas Hellstrom  * *p_tfile will be set to NULL by this function.
2520b8762e9SThomas Hellstrom  *
2530b8762e9SThomas Hellstrom  * Releases all data associated by a ttm_object_file.
2540b8762e9SThomas Hellstrom  * Typically called from file_ops::release. The caller must
2550b8762e9SThomas Hellstrom  * ensure that there are no concurrent users of tfile.
2560b8762e9SThomas Hellstrom  */
2570b8762e9SThomas Hellstrom 
2580b8762e9SThomas Hellstrom extern void ttm_object_file_release(struct ttm_object_file **p_tfile);
2590b8762e9SThomas Hellstrom 
2600b8762e9SThomas Hellstrom /**
2610b8762e9SThomas Hellstrom  * ttm_object device init - initialize a struct ttm_object_device
2620b8762e9SThomas Hellstrom  *
2630b8762e9SThomas Hellstrom  * @ops: DMA buf ops for prime objects of this device.
2640b8762e9SThomas Hellstrom  *
2650b8762e9SThomas Hellstrom  * This function is typically called on device initialization to prepare
2660b8762e9SThomas Hellstrom  * data structures needed for ttm base and ref objects.
2670b8762e9SThomas Hellstrom  */
2680b8762e9SThomas Hellstrom 
2690b8762e9SThomas Hellstrom extern struct ttm_object_device *
270931e09d8SMaaz Mombasawala ttm_object_device_init(const struct dma_buf_ops *ops);
2710b8762e9SThomas Hellstrom 
2720b8762e9SThomas Hellstrom /**
2730b8762e9SThomas Hellstrom  * ttm_object_device_release - release data held by a ttm_object_device
2740b8762e9SThomas Hellstrom  *
2750b8762e9SThomas Hellstrom  * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
2760b8762e9SThomas Hellstrom  * *p_tdev will be set to NULL by this function.
2770b8762e9SThomas Hellstrom  *
2780b8762e9SThomas Hellstrom  * Releases all data associated by a ttm_object_device.
2790b8762e9SThomas Hellstrom  * Typically called from driver::unload before the destruction of the
2800b8762e9SThomas Hellstrom  * device private data structure.
2810b8762e9SThomas Hellstrom  */
2820b8762e9SThomas Hellstrom 
2830b8762e9SThomas Hellstrom extern void ttm_object_device_release(struct ttm_object_device **p_tdev);
2840b8762e9SThomas Hellstrom 
2850b8762e9SThomas Hellstrom #define ttm_base_object_kfree(__object, __base)\
2860b8762e9SThomas Hellstrom 	kfree_rcu(__object, __base.rhead)
2870b8762e9SThomas Hellstrom 
2880b8762e9SThomas Hellstrom extern int ttm_prime_object_init(struct ttm_object_file *tfile,
2890b8762e9SThomas Hellstrom 				 size_t size,
2900b8762e9SThomas Hellstrom 				 struct ttm_prime_object *prime,
2910b8762e9SThomas Hellstrom 				 bool shareable,
2920b8762e9SThomas Hellstrom 				 enum ttm_object_type type,
2930b8762e9SThomas Hellstrom 				 void (*refcount_release)
2948afa13a0SZack Rusin 				 (struct ttm_base_object **));
2950b8762e9SThomas Hellstrom 
2960b8762e9SThomas Hellstrom static inline enum ttm_object_type
ttm_base_object_type(struct ttm_base_object * base)2970b8762e9SThomas Hellstrom ttm_base_object_type(struct ttm_base_object *base)
2980b8762e9SThomas Hellstrom {
2990b8762e9SThomas Hellstrom 	return (base->object_type == ttm_prime_type) ?
3000b8762e9SThomas Hellstrom 		container_of(base, struct ttm_prime_object, base)->real_type :
3010b8762e9SThomas Hellstrom 		base->object_type;
3020b8762e9SThomas Hellstrom }
3030b8762e9SThomas Hellstrom extern int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
3040b8762e9SThomas Hellstrom 				  int fd, u32 *handle);
3050b8762e9SThomas Hellstrom extern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
3060b8762e9SThomas Hellstrom 				  uint32_t handle, uint32_t flags,
3070b8762e9SThomas Hellstrom 				  int *prime_fd);
3080b8762e9SThomas Hellstrom 
3090b8762e9SThomas Hellstrom #define ttm_prime_object_kfree(__obj, __prime)		\
3100b8762e9SThomas Hellstrom 	kfree_rcu(__obj, __prime.base.rhead)
311c7eae626SThomas Hellstrom 
ttm_bo_wait(struct ttm_buffer_object * bo,bool intr,bool no_wait)312e14c02e6SThomas Hellstrom static inline int ttm_bo_wait(struct ttm_buffer_object *bo, bool intr,
31376a9e07fSMaaz Mombasawala 			      bool no_wait)
314e14c02e6SThomas Hellstrom {
315e14c02e6SThomas Hellstrom 	struct ttm_operation_ctx ctx = { intr, no_wait };
316e14c02e6SThomas Hellstrom 
317e14c02e6SThomas Hellstrom 	return ttm_bo_wait_ctx(bo, &ctx);
318e14c02e6SThomas Hellstrom }
319e14c02e6SThomas Hellstrom 
320e14c02e6SThomas Hellstrom #endif
321e14c02e6SThomas Hellstrom