Home | History | Annotate | Line # | Download | only in vmwgfx
      1 /*	$NetBSD: ttm_object.h,v 1.2 2021/12/18 23:45:45 riastradh Exp $	*/
      2 
      3 /**************************************************************************
      4  *
      5  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
      6  * All Rights Reserved.
      7  *
      8  * Permission is hereby granted, free of charge, to any person obtaining a
      9  * copy of this software and associated documentation files (the
     10  * "Software"), to deal in the Software without restriction, including
     11  * without limitation the rights to use, copy, modify, merge, publish,
     12  * distribute, sub license, and/or sell copies of the Software, and to
     13  * permit persons to whom the Software is furnished to do so, subject to
     14  * the following conditions:
     15  *
     16  * The above copyright notice and this permission notice (including the
     17  * next paragraph) shall be included in all copies or substantial portions
     18  * of the Software.
     19  *
     20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     22  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     23  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     24  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     25  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     26  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     27  *
     28  **************************************************************************/
     29 /*
     30  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
     31  */
     32 /** @file ttm_object.h
     33  *
     34  * Base- and reference object implementation for the various
     35  * ttm objects. Implements reference counting, minimal security checks
     36  * and release on file close.
     37  */
     38 
     39 #ifndef _TTM_OBJECT_H_
     40 #define _TTM_OBJECT_H_
     41 
     42 #include <linux/dma-buf.h>
     43 #include <linux/kref.h>
     44 #include <linux/list.h>
     45 #include <linux/rcupdate.h>
     46 
     47 #include <drm/drm_hashtab.h>
     48 #include <drm/ttm/ttm_memory.h>
     49 
     50 /**
     51  * enum ttm_ref_type
     52  *
     53  * Describes what type of reference a ref object holds.
     54  *
     55  * TTM_REF_USAGE is a simple refcount on a base object.
     56  *
     57  * TTM_REF_SYNCCPU_READ is a SYNCCPU_READ reference on a
     58  * buffer object.
     59  *
     60  * TTM_REF_SYNCCPU_WRITE is a SYNCCPU_WRITE reference on a
     61  * buffer object.
     62  *
     63  */
     64 
     65 enum ttm_ref_type {
     66 	TTM_REF_USAGE,
     67 	TTM_REF_SYNCCPU_READ,
     68 	TTM_REF_SYNCCPU_WRITE,
     69 	TTM_REF_NUM
     70 };
     71 
     72 /**
     73  * enum ttm_object_type
     74  *
     75  * One entry per ttm object type.
     76  * Device-specific types should use the
     77  * ttm_driver_typex types.
     78  */
     79 
     80 enum ttm_object_type {
     81 	ttm_fence_type,
     82 	ttm_buffer_type,
     83 	ttm_lock_type,
     84 	ttm_prime_type,
     85 	ttm_driver_type0 = 256,
     86 	ttm_driver_type1,
     87 	ttm_driver_type2,
     88 	ttm_driver_type3,
     89 	ttm_driver_type4,
     90 	ttm_driver_type5
     91 };
     92 
     93 struct ttm_object_file;
     94 struct ttm_object_device;
     95 
     96 /**
     97  * struct ttm_base_object
     98  *
     99  * @hash: hash entry for the per-device object hash.
    100  * @type: derived type this object is base class for.
    101  * @shareable: Other ttm_object_files can access this object.
    102  *
    103  * @tfile: Pointer to ttm_object_file of the creator.
    104  * NULL if the object was not created by a user request.
    105  * (kernel object).
    106  *
    107  * @refcount: Number of references to this object, not
    108  * including the hash entry. A reference to a base object can
    109  * only be held by a ref object.
    110  *
    111  * @refcount_release: A function to be called when there are
    112  * no more references to this object. This function should
    113  * destroy the object (or make sure destruction eventually happens),
    114  * and when it is called, the object has
    115  * already been taken out of the per-device hash. The parameter
    116  * "base" should be set to NULL by the function.
    117  *
    118  * @ref_obj_release: A function to be called when a reference object
    119  * with another ttm_ref_type than TTM_REF_USAGE is deleted.
    120  * This function may, for example, release a lock held by a user-space
    121  * process.
    122  *
    123  * This struct is intended to be used as a base struct for objects that
    124  * are visible to user-space. It provides a global name, race-safe
    125  * access and refcounting, minimal access contol and hooks for unref actions.
    126  */
    127 
    128 struct ttm_base_object {
    129 	struct rcu_head rhead;
    130 	struct ttm_object_file *tfile;
    131 	struct kref refcount;
    132 	void (*refcount_release) (struct ttm_base_object **base);
    133 	void (*ref_obj_release) (struct ttm_base_object *base,
    134 				 enum ttm_ref_type ref_type);
    135 	u32 handle;
    136 	enum ttm_object_type object_type;
    137 	u32 shareable;
    138 };
    139 
    140 
    141 /**
    142  * struct ttm_prime_object - Modified base object that is prime-aware
    143  *
    144  * @base: struct ttm_base_object that we derive from
    145  * @mutex: Mutex protecting the @dma_buf member.
    146  * @size: Size of the dma_buf associated with this object
    147  * @real_type: Type of the underlying object. Needed since we're setting
    148  * the value of @base::object_type to ttm_prime_type
    149  * @dma_buf: Non ref-coutned pointer to a struct dma_buf created from this
    150  * object.
    151  * @refcount_release: The underlying object's release method. Needed since
    152  * we set @base::refcount_release to our own release method.
    153  */
    154 
    155 struct ttm_prime_object {
    156 	struct ttm_base_object base;
    157 	struct mutex mutex;
    158 	size_t size;
    159 	enum ttm_object_type real_type;
    160 	struct dma_buf *dma_buf;
    161 	void (*refcount_release) (struct ttm_base_object **);
    162 };
    163 
    164 /**
    165  * ttm_base_object_init
    166  *
    167  * @tfile: Pointer to a struct ttm_object_file.
    168  * @base: The struct ttm_base_object to initialize.
    169  * @shareable: This object is shareable with other applcations.
    170  * (different @tfile pointers.)
    171  * @type: The object type.
    172  * @refcount_release: See the struct ttm_base_object description.
    173  * @ref_obj_release: See the struct ttm_base_object description.
    174  *
    175  * Initializes a struct ttm_base_object.
    176  */
    177 
    178 extern int ttm_base_object_init(struct ttm_object_file *tfile,
    179 				struct ttm_base_object *base,
    180 				bool shareable,
    181 				enum ttm_object_type type,
    182 				void (*refcount_release) (struct ttm_base_object
    183 							  **),
    184 				void (*ref_obj_release) (struct ttm_base_object
    185 							 *,
    186 							 enum ttm_ref_type
    187 							 ref_type));
    188 
    189 /**
    190  * ttm_base_object_lookup
    191  *
    192  * @tfile: Pointer to a struct ttm_object_file.
    193  * @key: Hash key
    194  *
    195  * Looks up a struct ttm_base_object with the key @key.
    196  */
    197 
    198 extern struct ttm_base_object *ttm_base_object_lookup(struct ttm_object_file
    199 						      *tfile, uint32_t key);
    200 
    201 /**
    202  * ttm_base_object_lookup_for_ref
    203  *
    204  * @tdev: Pointer to a struct ttm_object_device.
    205  * @key: Hash key
    206  *
    207  * Looks up a struct ttm_base_object with the key @key.
    208  * This function should only be used when the struct tfile associated with the
    209  * caller doesn't yet have a reference to the base object.
    210  */
    211 
    212 extern struct ttm_base_object *
    213 ttm_base_object_lookup_for_ref(struct ttm_object_device *tdev, uint32_t key);
    214 
    215 /**
    216  * ttm_base_object_unref
    217  *
    218  * @p_base: Pointer to a pointer referencing a struct ttm_base_object.
    219  *
    220  * Decrements the base object refcount and clears the pointer pointed to by
    221  * p_base.
    222  */
    223 
    224 extern void ttm_base_object_unref(struct ttm_base_object **p_base);
    225 
    226 /**
    227  * ttm_ref_object_add.
    228  *
    229  * @tfile: A struct ttm_object_file representing the application owning the
    230  * ref_object.
    231  * @base: The base object to reference.
    232  * @ref_type: The type of reference.
    233  * @existed: Upon completion, indicates that an identical reference object
    234  * already existed, and the refcount was upped on that object instead.
    235  * @require_existed: Fail with -EPERM if an identical ref object didn't
    236  * already exist.
    237  *
    238  * Checks that the base object is shareable and adds a ref object to it.
    239  *
    240  * Adding a ref object to a base object is basically like referencing the
    241  * base object, but a user-space application holds the reference. When the
    242  * file corresponding to @tfile is closed, all its reference objects are
    243  * deleted. A reference object can have different types depending on what
    244  * it's intended for. It can be refcounting to prevent object destruction,
    245  * When user-space takes a lock, it can add a ref object to that lock to
    246  * make sure the lock is released if the application dies. A ref object
    247  * will hold a single reference on a base object.
    248  */
    249 extern int ttm_ref_object_add(struct ttm_object_file *tfile,
    250 			      struct ttm_base_object *base,
    251 			      enum ttm_ref_type ref_type, bool *existed,
    252 			      bool require_existed);
    253 
    254 extern bool ttm_ref_object_exists(struct ttm_object_file *tfile,
    255 				  struct ttm_base_object *base);
    256 
    257 /**
    258  * ttm_ref_object_base_unref
    259  *
    260  * @key: Key representing the base object.
    261  * @ref_type: Ref type of the ref object to be dereferenced.
    262  *
    263  * Unreference a ref object with type @ref_type
    264  * on the base object identified by @key. If there are no duplicate
    265  * references, the ref object will be destroyed and the base object
    266  * will be unreferenced.
    267  */
    268 extern int ttm_ref_object_base_unref(struct ttm_object_file *tfile,
    269 				     unsigned long key,
    270 				     enum ttm_ref_type ref_type);
    271 
    272 /**
    273  * ttm_object_file_init - initialize a struct ttm_object file
    274  *
    275  * @tdev: A struct ttm_object device this file is initialized on.
    276  * @hash_order: Order of the hash table used to hold the reference objects.
    277  *
    278  * This is typically called by the file_ops::open function.
    279  */
    280 
    281 extern struct ttm_object_file *ttm_object_file_init(struct ttm_object_device
    282 						    *tdev,
    283 						    unsigned int hash_order);
    284 
    285 /**
    286  * ttm_object_file_release - release data held by a ttm_object_file
    287  *
    288  * @p_tfile: Pointer to pointer to the ttm_object_file object to release.
    289  * *p_tfile will be set to NULL by this function.
    290  *
    291  * Releases all data associated by a ttm_object_file.
    292  * Typically called from file_ops::release. The caller must
    293  * ensure that there are no concurrent users of tfile.
    294  */
    295 
    296 extern void ttm_object_file_release(struct ttm_object_file **p_tfile);
    297 
    298 /**
    299  * ttm_object device init - initialize a struct ttm_object_device
    300  *
    301  * @mem_glob: struct ttm_mem_global for memory accounting.
    302  * @hash_order: Order of hash table used to hash the base objects.
    303  * @ops: DMA buf ops for prime objects of this device.
    304  *
    305  * This function is typically called on device initialization to prepare
    306  * data structures needed for ttm base and ref objects.
    307  */
    308 
    309 extern struct ttm_object_device *
    310 ttm_object_device_init(struct ttm_mem_global *mem_glob,
    311 		       unsigned int hash_order,
    312 		       const struct dma_buf_ops *ops);
    313 
    314 /**
    315  * ttm_object_device_release - release data held by a ttm_object_device
    316  *
    317  * @p_tdev: Pointer to pointer to the ttm_object_device object to release.
    318  * *p_tdev will be set to NULL by this function.
    319  *
    320  * Releases all data associated by a ttm_object_device.
    321  * Typically called from driver::unload before the destruction of the
    322  * device private data structure.
    323  */
    324 
    325 extern void ttm_object_device_release(struct ttm_object_device **p_tdev);
    326 
    327 #define ttm_base_object_kfree(__object, __base)\
    328 	kfree_rcu(__object, __base.rhead)
    329 
    330 extern int ttm_prime_object_init(struct ttm_object_file *tfile,
    331 				 size_t size,
    332 				 struct ttm_prime_object *prime,
    333 				 bool shareable,
    334 				 enum ttm_object_type type,
    335 				 void (*refcount_release)
    336 				 (struct ttm_base_object **),
    337 				 void (*ref_obj_release)
    338 				 (struct ttm_base_object *,
    339 				  enum ttm_ref_type ref_type));
    340 
    341 static inline enum ttm_object_type
    342 ttm_base_object_type(struct ttm_base_object *base)
    343 {
    344 	return (base->object_type == ttm_prime_type) ?
    345 		container_of(base, struct ttm_prime_object, base)->real_type :
    346 		base->object_type;
    347 }
    348 extern int ttm_prime_fd_to_handle(struct ttm_object_file *tfile,
    349 				  int fd, u32 *handle);
    350 extern int ttm_prime_handle_to_fd(struct ttm_object_file *tfile,
    351 				  uint32_t handle, uint32_t flags,
    352 				  int *prime_fd);
    353 
    354 #define ttm_prime_object_kfree(__obj, __prime)		\
    355 	kfree_rcu(__obj, __prime.base.rhead)
    356 
    357 /*
    358  * Extra memory required by the base object's idr storage, which is allocated
    359  * separately from the base object itself. We estimate an on-average 128 bytes
    360  * per idr.
    361  */
    362 #define TTM_OBJ_EXTRA_SIZE 128
    363 
    364 struct ttm_base_object *
    365 ttm_base_object_noref_lookup(struct ttm_object_file *tfile, uint32_t key);
    366 
    367 /**
    368  * ttm_base_object_noref_release - release a base object pointer looked up
    369  * without reference
    370  *
    371  * Releases a base object pointer looked up with ttm_base_object_noref_lookup().
    372  */
    373 static inline void ttm_base_object_noref_release(void)
    374 {
    375 	__acquire(RCU);
    376 	rcu_read_unlock();
    377 }
    378 #endif
    379