2013-06-27 19:48:16 +08:00
|
|
|
/*
|
|
|
|
* Header file for reservations for dma-buf and ttm
|
|
|
|
*
|
|
|
|
* Copyright(C) 2011 Linaro Limited. All rights reserved.
|
|
|
|
* Copyright (C) 2012-2013 Canonical Ltd
|
|
|
|
* Copyright (C) 2012 Texas Instruments
|
|
|
|
*
|
|
|
|
* Authors:
|
2014-07-01 18:57:37 +08:00
|
|
|
* Rob Clark <robdclark@gmail.com>
|
2013-06-27 19:48:16 +08:00
|
|
|
* Maarten Lankhorst <maarten.lankhorst@canonical.com>
|
|
|
|
* Thomas Hellstrom <thellstrom-at-vmware-dot-com>
|
|
|
|
*
|
|
|
|
* Based on bo.c which bears the following copyright notice,
|
|
|
|
* but is dual licensed:
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
|
|
|
|
* All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sub license, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice (including the
|
|
|
|
* next paragraph) shall be included in all copies or substantial portions
|
|
|
|
* of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#ifndef _LINUX_RESERVATION_H
|
|
|
|
#define _LINUX_RESERVATION_H
|
|
|
|
|
2013-07-05 15:29:32 +08:00
|
|
|
#include <linux/ww_mutex.h>
|
2016-10-25 20:00:45 +08:00
|
|
|
#include <linux/dma-fence.h>
|
2014-07-01 18:57:37 +08:00
|
|
|
#include <linux/slab.h>
|
2014-07-01 18:58:00 +08:00
|
|
|
#include <linux/seqlock.h>
|
|
|
|
#include <linux/rcupdate.h>
|
2013-06-27 19:48:16 +08:00
|
|
|
|
|
|
|
extern struct ww_class reservation_ww_class;
|
|
|
|
|
2016-04-01 04:26:51 +08:00
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* struct dma_resv_list - a list of shared fences
|
2016-04-01 04:26:51 +08:00
|
|
|
* @rcu: for internal use
|
|
|
|
* @shared_count: table of shared fences
|
|
|
|
* @shared_max: for growing shared fence table
|
|
|
|
* @shared: shared fence table
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
struct dma_resv_list {
|
2014-07-01 18:58:00 +08:00
|
|
|
struct rcu_head rcu;
|
2014-07-01 18:57:54 +08:00
|
|
|
u32 shared_count, shared_max;
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence __rcu *shared[];
|
2014-07-01 18:57:54 +08:00
|
|
|
};
|
|
|
|
|
2016-04-01 04:26:51 +08:00
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* struct dma_resv - a reservation object manages fences for a buffer
|
2016-04-01 04:26:51 +08:00
|
|
|
* @lock: update side lock
|
|
|
|
* @seq: sequence count for managing RCU read-side synchronization
|
|
|
|
* @fence_excl: the exclusive fence, if there is one currently
|
|
|
|
* @fence: list of current shared fences
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
struct dma_resv {
|
2013-06-27 19:48:16 +08:00
|
|
|
struct ww_mutex lock;
|
2020-07-20 23:55:18 +08:00
|
|
|
seqcount_ww_mutex_t seq;
|
2014-07-01 18:57:37 +08:00
|
|
|
|
2016-10-25 20:00:45 +08:00
|
|
|
struct dma_fence __rcu *fence_excl;
|
2019-08-11 16:06:32 +08:00
|
|
|
struct dma_resv_list __rcu *fence;
|
2013-06-27 19:48:16 +08:00
|
|
|
};
|
|
|
|
|
2019-08-11 16:06:32 +08:00
|
|
|
#define dma_resv_held(obj) lockdep_is_held(&(obj)->lock.base)
|
|
|
|
#define dma_resv_assert_held(obj) lockdep_assert_held(&(obj)->lock.base)
|
2014-07-01 18:57:54 +08:00
|
|
|
|
2021-05-06 20:16:01 +08:00
|
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
|
|
void dma_resv_reset_shared_max(struct dma_resv *obj);
|
|
|
|
#else
|
|
|
|
static inline void dma_resv_reset_shared_max(struct dma_resv *obj) {}
|
|
|
|
#endif
|
|
|
|
|
2016-11-15 23:46:42 +08:00
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_lock - lock the reservation object
|
2016-11-15 23:46:42 +08:00
|
|
|
* @obj: the reservation object
|
|
|
|
* @ctx: the locking context
|
|
|
|
*
|
|
|
|
* Locks the reservation object for exclusive access and modification. Note,
|
|
|
|
* that the lock is only against other writers, readers will run concurrently
|
|
|
|
* with a writer under RCU. The seqlock is used to notify readers if they
|
|
|
|
* overlap with a writer.
|
|
|
|
*
|
|
|
|
* As the reservation object may be locked by multiple parties in an
|
|
|
|
* undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
|
|
|
|
* is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
|
|
|
|
* object may be locked by itself by passing NULL as @ctx.
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
static inline int dma_resv_lock(struct dma_resv *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
2016-11-15 23:46:42 +08:00
|
|
|
{
|
|
|
|
return ww_mutex_lock(&obj->lock, ctx);
|
|
|
|
}
|
|
|
|
|
2017-11-09 16:59:05 +08:00
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_lock_interruptible - lock the reservation object
|
2017-11-09 16:59:05 +08:00
|
|
|
* @obj: the reservation object
|
|
|
|
* @ctx: the locking context
|
|
|
|
*
|
|
|
|
* Locks the reservation object interruptible for exclusive access and
|
|
|
|
* modification. Note, that the lock is only against other writers, readers
|
|
|
|
* will run concurrently with a writer under RCU. The seqlock is used to
|
|
|
|
* notify readers if they overlap with a writer.
|
|
|
|
*
|
|
|
|
* As the reservation object may be locked by multiple parties in an
|
|
|
|
* undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
|
|
|
|
* is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
|
|
|
|
* object may be locked by itself by passing NULL as @ctx.
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
static inline int dma_resv_lock_interruptible(struct dma_resv *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
2017-11-09 16:59:05 +08:00
|
|
|
{
|
|
|
|
return ww_mutex_lock_interruptible(&obj->lock, ctx);
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:41:50 +08:00
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_lock_slow - slowpath lock the reservation object
|
2019-07-31 15:41:50 +08:00
|
|
|
* @obj: the reservation object
|
|
|
|
* @ctx: the locking context
|
|
|
|
*
|
|
|
|
* Acquires the reservation object after a die case. This function
|
2019-08-11 16:06:32 +08:00
|
|
|
* will sleep until the lock becomes available. See dma_resv_lock() as
|
2019-07-31 15:41:50 +08:00
|
|
|
* well.
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
static inline void dma_resv_lock_slow(struct dma_resv *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
2019-07-31 15:41:50 +08:00
|
|
|
{
|
|
|
|
ww_mutex_lock_slow(&obj->lock, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_lock_slow_interruptible - slowpath lock the reservation
|
2019-07-31 15:41:50 +08:00
|
|
|
* object, interruptible
|
|
|
|
* @obj: the reservation object
|
|
|
|
* @ctx: the locking context
|
|
|
|
*
|
|
|
|
* Acquires the reservation object interruptible after a die case. This function
|
|
|
|
* will sleep until the lock becomes available. See
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_lock_interruptible() as well.
|
2019-07-31 15:41:50 +08:00
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
static inline int dma_resv_lock_slow_interruptible(struct dma_resv *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
2019-07-31 15:41:50 +08:00
|
|
|
{
|
|
|
|
return ww_mutex_lock_slow_interruptible(&obj->lock, ctx);
|
|
|
|
}
|
2017-11-09 16:59:05 +08:00
|
|
|
|
2017-02-21 17:30:00 +08:00
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_trylock - trylock the reservation object
|
2017-02-21 17:30:00 +08:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Tries to lock the reservation object for exclusive access and modification.
|
|
|
|
* Note, that the lock is only against other writers, readers will run
|
|
|
|
* concurrently with a writer under RCU. The seqlock is used to notify readers
|
|
|
|
* if they overlap with a writer.
|
|
|
|
*
|
|
|
|
* Also note that since no context is provided, no deadlock protection is
|
|
|
|
* possible.
|
|
|
|
*
|
|
|
|
* Returns true if the lock was acquired, false otherwise.
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
static inline bool __must_check dma_resv_trylock(struct dma_resv *obj)
|
2017-02-21 17:30:00 +08:00
|
|
|
{
|
|
|
|
return ww_mutex_trylock(&obj->lock);
|
|
|
|
}
|
|
|
|
|
2019-07-31 15:41:50 +08:00
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_is_locked - is the reservation object locked
|
2019-07-31 15:41:50 +08:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Returns true if the mutex is locked, false if unlocked.
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
static inline bool dma_resv_is_locked(struct dma_resv *obj)
|
2019-07-31 15:41:50 +08:00
|
|
|
{
|
|
|
|
return ww_mutex_is_locked(&obj->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_locking_ctx - returns the context used to lock the object
|
2019-07-31 15:41:50 +08:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Returns the context used to lock a reservation object or NULL if no context
|
|
|
|
* was used or the object is not locked at all.
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
static inline struct ww_acquire_ctx *dma_resv_locking_ctx(struct dma_resv *obj)
|
2019-07-31 15:41:50 +08:00
|
|
|
{
|
|
|
|
return READ_ONCE(obj->lock.ctx);
|
|
|
|
}
|
|
|
|
|
2016-11-15 23:46:42 +08:00
|
|
|
/**
|
2019-08-11 16:06:32 +08:00
|
|
|
* dma_resv_unlock - unlock the reservation object
|
2016-11-15 23:46:42 +08:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Unlocks the reservation object following exclusive access.
|
|
|
|
*/
|
2019-08-11 16:06:32 +08:00
|
|
|
static inline void dma_resv_unlock(struct dma_resv *obj)
|
2016-11-15 23:46:42 +08:00
|
|
|
{
|
2021-05-06 20:16:01 +08:00
|
|
|
dma_resv_reset_shared_max(obj);
|
2016-11-15 23:46:42 +08:00
|
|
|
ww_mutex_unlock(&obj->lock);
|
|
|
|
}
|
|
|
|
|
2019-08-15 02:24:01 +08:00
|
|
|
/**
|
2021-06-28 08:40:12 +08:00
|
|
|
* dma_resv_excl_fence - return the object's exclusive fence
|
2019-08-15 02:24:01 +08:00
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
2021-05-10 22:14:09 +08:00
|
|
|
* Returns the exclusive fence (if any). Caller must either hold the objects
|
|
|
|
* through dma_resv_lock() or the RCU read side lock through rcu_read_lock(),
|
|
|
|
* or one of the variants of each
|
2019-08-15 02:24:01 +08:00
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* The exclusive fence or NULL
|
|
|
|
*/
|
|
|
|
static inline struct dma_fence *
|
2021-05-10 22:14:09 +08:00
|
|
|
dma_resv_excl_fence(struct dma_resv *obj)
|
2019-08-15 02:24:01 +08:00
|
|
|
{
|
2021-05-10 22:14:09 +08:00
|
|
|
return rcu_dereference_check(obj->fence_excl, dma_resv_held(obj));
|
2019-08-15 02:24:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-06-02 18:44:32 +08:00
|
|
|
* dma_resv_get_excl_unlocked - get the reservation object's
|
2019-08-15 02:24:01 +08:00
|
|
|
* exclusive fence, without lock held.
|
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* If there is an exclusive fence, this atomically increments it's
|
|
|
|
* reference count and returns it.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* The exclusive fence or NULL if none
|
|
|
|
*/
|
|
|
|
static inline struct dma_fence *
|
2021-06-02 18:44:32 +08:00
|
|
|
dma_resv_get_excl_unlocked(struct dma_resv *obj)
|
2019-08-15 02:24:01 +08:00
|
|
|
{
|
|
|
|
struct dma_fence *fence;
|
|
|
|
|
|
|
|
if (!rcu_access_pointer(obj->fence_excl))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
rcu_read_lock();
|
|
|
|
fence = dma_fence_get_rcu_safe(&obj->fence_excl);
|
|
|
|
rcu_read_unlock();
|
|
|
|
|
|
|
|
return fence;
|
|
|
|
}
|
|
|
|
|
2021-05-11 20:11:41 +08:00
|
|
|
/**
|
|
|
|
* dma_resv_shared_list - get the reservation object's shared fence list
|
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Returns the shared fence list. Caller must either hold the objects
|
|
|
|
* through dma_resv_lock() or the RCU read side lock through rcu_read_lock(),
|
|
|
|
* or one of the variants of each
|
|
|
|
*/
|
|
|
|
static inline struct dma_resv_list *dma_resv_shared_list(struct dma_resv *obj)
|
|
|
|
{
|
|
|
|
return rcu_dereference_check(obj->fence, dma_resv_held(obj));
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:06:32 +08:00
|
|
|
void dma_resv_init(struct dma_resv *obj);
|
|
|
|
void dma_resv_fini(struct dma_resv *obj);
|
|
|
|
int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences);
|
|
|
|
void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence);
|
|
|
|
void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence);
|
2021-06-02 19:01:15 +08:00
|
|
|
int dma_resv_get_fences(struct dma_resv *obj, struct dma_fence **pfence_excl,
|
|
|
|
unsigned *pshared_count, struct dma_fence ***pshared);
|
2019-08-11 16:06:32 +08:00
|
|
|
int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src);
|
2021-06-02 19:01:15 +08:00
|
|
|
long dma_resv_wait_timeout(struct dma_resv *obj, bool wait_all, bool intr,
|
|
|
|
unsigned long timeout);
|
|
|
|
bool dma_resv_test_signaled(struct dma_resv *obj, bool test_all);
|
2014-07-01 18:58:00 +08:00
|
|
|
|
2013-06-27 19:48:16 +08:00
|
|
|
#endif /* _LINUX_RESERVATION_H */
|