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