Home | History | Annotate | Line # | Download | only in linux
dma-resv.h revision 1.3
      1  1.2  riastrad /*
      2  1.3  riastrad  * Header file for reservations for dma-buf and ttm
      3  1.3  riastrad  *
      4  1.3  riastrad  * Copyright(C) 2011 Linaro Limited. All rights reserved.
      5  1.3  riastrad  * Copyright (C) 2012-2013 Canonical Ltd
      6  1.3  riastrad  * Copyright (C) 2012 Texas Instruments
      7  1.3  riastrad  *
      8  1.3  riastrad  * Authors:
      9  1.3  riastrad  * Rob Clark <robdclark (at) gmail.com>
     10  1.3  riastrad  * Maarten Lankhorst <maarten.lankhorst (at) canonical.com>
     11  1.3  riastrad  * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
     12  1.3  riastrad  *
     13  1.3  riastrad  * Based on bo.c which bears the following copyright notice,
     14  1.3  riastrad  * but is dual licensed:
     15  1.3  riastrad  *
     16  1.3  riastrad  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
     17  1.3  riastrad  * All Rights Reserved.
     18  1.3  riastrad  *
     19  1.3  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
     20  1.3  riastrad  * copy of this software and associated documentation files (the
     21  1.3  riastrad  * "Software"), to deal in the Software without restriction, including
     22  1.3  riastrad  * without limitation the rights to use, copy, modify, merge, publish,
     23  1.3  riastrad  * distribute, sub license, and/or sell copies of the Software, and to
     24  1.3  riastrad  * permit persons to whom the Software is furnished to do so, subject to
     25  1.3  riastrad  * the following conditions:
     26  1.3  riastrad  *
     27  1.3  riastrad  * The above copyright notice and this permission notice (including the
     28  1.3  riastrad  * next paragraph) shall be included in all copies or substantial portions
     29  1.3  riastrad  * of the Software.
     30  1.3  riastrad  *
     31  1.3  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     32  1.3  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     33  1.3  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
     34  1.3  riastrad  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
     35  1.3  riastrad  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
     36  1.3  riastrad  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
     37  1.3  riastrad  * USE OR OTHER DEALINGS IN THE SOFTWARE.
     38  1.2  riastrad  */
     39  1.3  riastrad #ifndef _LINUX_RESERVATION_H
     40  1.3  riastrad #define _LINUX_RESERVATION_H
     41  1.2  riastrad 
     42  1.2  riastrad #include <linux/ww_mutex.h>
     43  1.2  riastrad #include <linux/dma-fence.h>
     44  1.3  riastrad #include <linux/slab.h>
     45  1.3  riastrad #include <linux/seqlock.h>
     46  1.3  riastrad #include <linux/rcupdate.h>
     47  1.3  riastrad 
     48  1.3  riastrad extern struct ww_class reservation_ww_class;
     49  1.3  riastrad extern struct lock_class_key reservation_seqcount_class;
     50  1.3  riastrad extern const char reservation_seqcount_string[];
     51  1.3  riastrad 
     52  1.3  riastrad /**
     53  1.3  riastrad  * struct dma_resv_list - a list of shared fences
     54  1.3  riastrad  * @rcu: for internal use
     55  1.3  riastrad  * @shared_count: table of shared fences
     56  1.3  riastrad  * @shared_max: for growing shared fence table
     57  1.3  riastrad  * @shared: shared fence table
     58  1.3  riastrad  */
     59  1.3  riastrad struct dma_resv_list {
     60  1.3  riastrad 	struct rcu_head rcu;
     61  1.3  riastrad 	u32 shared_count, shared_max;
     62  1.3  riastrad 	struct dma_fence __rcu *shared[];
     63  1.3  riastrad };
     64  1.3  riastrad 
     65  1.3  riastrad /**
     66  1.3  riastrad  * struct dma_resv - a reservation object manages fences for a buffer
     67  1.3  riastrad  * @lock: update side lock
     68  1.3  riastrad  * @seq: sequence count for managing RCU read-side synchronization
     69  1.3  riastrad  * @fence_excl: the exclusive fence, if there is one currently
     70  1.3  riastrad  * @fence: list of current shared fences
     71  1.3  riastrad  */
     72  1.3  riastrad struct dma_resv {
     73  1.3  riastrad 	struct ww_mutex lock;
     74  1.3  riastrad 	seqcount_t seq;
     75  1.3  riastrad 
     76  1.3  riastrad 	struct dma_fence __rcu *fence_excl;
     77  1.3  riastrad 	struct dma_resv_list __rcu *fence;
     78  1.3  riastrad };
     79  1.3  riastrad 
     80  1.3  riastrad #define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base)
     81  1.3  riastrad #define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base)
     82  1.3  riastrad 
     83  1.3  riastrad /**
     84  1.3  riastrad  * dma_resv_get_list - get the reservation object's
     85  1.3  riastrad  * shared fence list, with update-side lock held
     86  1.3  riastrad  * @obj: the reservation object
     87  1.3  riastrad  *
     88  1.3  riastrad  * Returns the shared fence list.  Does NOT take references to
     89  1.3  riastrad  * the fence.  The obj->lock must be held.
     90  1.3  riastrad  */
     91  1.3  riastrad static inline struct dma_resv_list *dma_resv_get_list(struct dma_resv *obj)
     92  1.3  riastrad {
     93  1.3  riastrad 	return rcu_dereference_protected(obj->fence,
     94  1.3  riastrad 					 dma_resv_held(obj));
     95  1.3  riastrad }
     96  1.3  riastrad 
     97  1.3  riastrad /**
     98  1.3  riastrad  * dma_resv_lock - lock the reservation object
     99  1.3  riastrad  * @obj: the reservation object
    100  1.3  riastrad  * @ctx: the locking context
    101  1.3  riastrad  *
    102  1.3  riastrad  * Locks the reservation object for exclusive access and modification. Note,
    103  1.3  riastrad  * that the lock is only against other writers, readers will run concurrently
    104  1.3  riastrad  * with a writer under RCU. The seqlock is used to notify readers if they
    105  1.3  riastrad  * overlap with a writer.
    106  1.3  riastrad  *
    107  1.3  riastrad  * As the reservation object may be locked by multiple parties in an
    108  1.3  riastrad  * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
    109  1.3  riastrad  * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
    110  1.3  riastrad  * object may be locked by itself by passing NULL as @ctx.
    111  1.3  riastrad  */
    112  1.3  riastrad static inline int dma_resv_lock(struct dma_resv *obj,
    113  1.3  riastrad 				struct ww_acquire_ctx *ctx)
    114  1.3  riastrad {
    115  1.3  riastrad 	return ww_mutex_lock(&obj->lock, ctx);
    116  1.3  riastrad }
    117  1.3  riastrad 
    118  1.3  riastrad /**
    119  1.3  riastrad  * dma_resv_lock_interruptible - lock the reservation object
    120  1.3  riastrad  * @obj: the reservation object
    121  1.3  riastrad  * @ctx: the locking context
    122  1.3  riastrad  *
    123  1.3  riastrad  * Locks the reservation object interruptible for exclusive access and
    124  1.3  riastrad  * modification. Note, that the lock is only against other writers, readers
    125  1.3  riastrad  * will run concurrently with a writer under RCU. The seqlock is used to
    126  1.3  riastrad  * notify readers if they overlap with a writer.
    127  1.3  riastrad  *
    128  1.3  riastrad  * As the reservation object may be locked by multiple parties in an
    129  1.3  riastrad  * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
    130  1.3  riastrad  * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
    131  1.3  riastrad  * object may be locked by itself by passing NULL as @ctx.
    132  1.3  riastrad  */
    133  1.3  riastrad static inline int dma_resv_lock_interruptible(struct dma_resv *obj,
    134  1.3  riastrad 					      struct ww_acquire_ctx *ctx)
    135  1.3  riastrad {
    136  1.3  riastrad 	return ww_mutex_lock_interruptible(&obj->lock, ctx);
    137  1.3  riastrad }
    138  1.3  riastrad 
    139  1.3  riastrad /**
    140  1.3  riastrad  * dma_resv_lock_slow - slowpath lock the reservation object
    141  1.3  riastrad  * @obj: the reservation object
    142  1.3  riastrad  * @ctx: the locking context
    143  1.3  riastrad  *
    144  1.3  riastrad  * Acquires the reservation object after a die case. This function
    145  1.3  riastrad  * will sleep until the lock becomes available. See dma_resv_lock() as
    146  1.3  riastrad  * well.
    147  1.3  riastrad  */
    148  1.3  riastrad static inline void dma_resv_lock_slow(struct dma_resv *obj,
    149  1.3  riastrad 				      struct ww_acquire_ctx *ctx)
    150  1.3  riastrad {
    151  1.3  riastrad 	ww_mutex_lock_slow(&obj->lock, ctx);
    152  1.3  riastrad }
    153  1.3  riastrad 
    154  1.3  riastrad /**
    155  1.3  riastrad  * dma_resv_lock_slow_interruptible - slowpath lock the reservation
    156  1.3  riastrad  * object, interruptible
    157  1.3  riastrad  * @obj: the reservation object
    158  1.3  riastrad  * @ctx: the locking context
    159  1.3  riastrad  *
    160  1.3  riastrad  * Acquires the reservation object interruptible after a die case. This function
    161  1.3  riastrad  * will sleep until the lock becomes available. See
    162  1.3  riastrad  * dma_resv_lock_interruptible() as well.
    163  1.3  riastrad  */
    164  1.3  riastrad static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,
    165  1.3  riastrad 						   struct ww_acquire_ctx *ctx)
    166  1.3  riastrad {
    167  1.3  riastrad 	return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
    168  1.3  riastrad }
    169  1.3  riastrad 
    170  1.3  riastrad /**
    171  1.3  riastrad  * dma_resv_trylock - trylock the reservation object
    172  1.3  riastrad  * @obj: the reservation object
    173  1.3  riastrad  *
    174  1.3  riastrad  * Tries to lock the reservation object for exclusive access and modification.
    175  1.3  riastrad  * Note, that the lock is only against other writers, readers will run
    176  1.3  riastrad  * concurrently with a writer under RCU. The seqlock is used to notify readers
    177  1.3  riastrad  * if they overlap with a writer.
    178  1.3  riastrad  *
    179  1.3  riastrad  * Also note that since no context is provided, no deadlock protection is
    180  1.3  riastrad  * possible.
    181  1.3  riastrad  *
    182  1.3  riastrad  * Returns true if the lock was acquired, false otherwise.
    183  1.3  riastrad  */
    184  1.3  riastrad static inline bool __must_check dma_resv_trylock(struct dma_resv *obj)
    185  1.3  riastrad {
    186  1.3  riastrad 	return ww_mutex_trylock(&obj->lock);
    187  1.3  riastrad }
    188  1.3  riastrad 
    189  1.3  riastrad /**
    190  1.3  riastrad  * dma_resv_is_locked - is the reservation object locked
    191  1.3  riastrad  * @obj: the reservation object
    192  1.3  riastrad  *
    193  1.3  riastrad  * Returns true if the mutex is locked, false if unlocked.
    194  1.3  riastrad  */
    195  1.3  riastrad static inline bool dma_resv_is_locked(struct dma_resv *obj)
    196  1.3  riastrad {
    197  1.3  riastrad 	return ww_mutex_is_locked(&obj->lock);
    198  1.3  riastrad }
    199  1.3  riastrad 
    200  1.3  riastrad /**
    201  1.3  riastrad  * dma_resv_locking_ctx - returns the context used to lock the object
    202  1.3  riastrad  * @obj: the reservation object
    203  1.3  riastrad  *
    204  1.3  riastrad  * Returns the context used to lock a reservation object or NULL if no context
    205  1.3  riastrad  * was used or the object is not locked at all.
    206  1.3  riastrad  */
    207  1.3  riastrad static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj)
    208  1.3  riastrad {
    209  1.3  riastrad 	return READ_ONCE(obj->lock.ctx);
    210  1.3  riastrad }
    211  1.3  riastrad 
    212  1.3  riastrad /**
    213  1.3  riastrad  * dma_resv_unlock - unlock the reservation object
    214  1.3  riastrad  * @obj: the reservation object
    215  1.3  riastrad  *
    216  1.3  riastrad  * Unlocks the reservation object following exclusive access.
    217  1.3  riastrad  */
    218  1.3  riastrad static inline void dma_resv_unlock(struct dma_resv *obj)
    219  1.3  riastrad {
    220  1.3  riastrad #ifdef CONFIG_DEBUG_MUTEXES
    221  1.3  riastrad 	/* Test shared fence slot reservation */
    222  1.3  riastrad 	if (rcu_access_pointer(obj->fence)) {
    223  1.3  riastrad 		struct dma_resv_list *fence = dma_resv_get_list(obj);
    224  1.3  riastrad 
    225  1.3  riastrad 		fence->shared_max = fence->shared_count;
    226  1.3  riastrad 	}
    227  1.3  riastrad #endif
    228  1.3  riastrad 	ww_mutex_unlock(&obj->lock);
    229  1.3  riastrad }
    230  1.3  riastrad 
    231  1.3  riastrad /**
    232  1.3  riastrad  * dma_resv_get_excl - get the reservation object's
    233  1.3  riastrad  * exclusive fence, with update-side lock held
    234  1.3  riastrad  * @obj: the reservation object
    235  1.3  riastrad  *
    236  1.3  riastrad  * Returns the exclusive fence (if any).  Does NOT take a
    237  1.3  riastrad  * reference. Writers must hold obj->lock, readers may only
    238  1.3  riastrad  * hold a RCU read side lock.
    239  1.3  riastrad  *
    240  1.3  riastrad  * RETURNS
    241  1.3  riastrad  * The exclusive fence or NULL
    242  1.3  riastrad  */
    243  1.3  riastrad static inline struct dma_fence *
    244  1.3  riastrad dma_resv_get_excl(struct dma_resv *obj)
    245  1.3  riastrad {
    246  1.3  riastrad 	return rcu_dereference_protected(obj->fence_excl,
    247  1.3  riastrad 					 dma_resv_held(obj));
    248  1.3  riastrad }
    249  1.3  riastrad 
    250  1.3  riastrad /**
    251  1.3  riastrad  * dma_resv_get_excl_rcu - get the reservation object's
    252  1.3  riastrad  * exclusive fence, without lock held.
    253  1.3  riastrad  * @obj: the reservation object
    254  1.3  riastrad  *
    255  1.3  riastrad  * If there is an exclusive fence, this atomically increments it's
    256  1.3  riastrad  * reference count and returns it.
    257  1.3  riastrad  *
    258  1.3  riastrad  * RETURNS
    259  1.3  riastrad  * The exclusive fence or NULL if none
    260  1.3  riastrad  */
    261  1.3  riastrad static inline struct dma_fence *
    262  1.3  riastrad dma_resv_get_excl_rcu(struct dma_resv *obj)
    263  1.3  riastrad {
    264  1.3  riastrad 	struct dma_fence *fence;
    265  1.3  riastrad 
    266  1.3  riastrad 	if (!rcu_access_pointer(obj->fence_excl))
    267  1.3  riastrad 		return NULL;
    268  1.3  riastrad 
    269  1.3  riastrad 	rcu_read_lock();
    270  1.3  riastrad 	fence = dma_fence_get_rcu_safe(&obj->fence_excl);
    271  1.3  riastrad 	rcu_read_unlock();
    272  1.3  riastrad 
    273  1.3  riastrad 	return fence;
    274  1.3  riastrad }
    275  1.3  riastrad 
    276  1.3  riastrad void dma_resv_init(struct dma_resv *obj);
    277  1.3  riastrad void dma_resv_fini(struct dma_resv *obj);
    278  1.3  riastrad int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences);
    279  1.3  riastrad void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence);
    280  1.3  riastrad 
    281  1.3  riastrad void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence);
    282  1.3  riastrad 
    283  1.3  riastrad int dma_resv_get_fences_rcu(struct dma_resv *obj,
    284  1.3  riastrad 			    struct dma_fence **pfence_excl,
    285  1.3  riastrad 			    unsigned *pshared_count,
    286  1.3  riastrad 			    struct dma_fence ***pshared);
    287  1.3  riastrad 
    288  1.3  riastrad int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src);
    289  1.3  riastrad 
    290  1.3  riastrad long dma_resv_wait_timeout_rcu(struct dma_resv *obj, bool wait_all, bool intr,
    291  1.3  riastrad 			       unsigned long timeout);
    292  1.2  riastrad 
    293  1.3  riastrad bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all);
    294  1.2  riastrad 
    295  1.3  riastrad #endif /* _LINUX_RESERVATION_H */
    296