2013-06-27 18:48:16 +07: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 17:57:37 +07:00
|
|
|
* Rob Clark <robdclark@gmail.com>
|
2013-06-27 18:48:16 +07: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 14:29:32 +07:00
|
|
|
#include <linux/ww_mutex.h>
|
2016-10-25 19:00:45 +07:00
|
|
|
#include <linux/dma-fence.h>
|
2014-07-01 17:57:37 +07:00
|
|
|
#include <linux/slab.h>
|
2014-07-01 17:58:00 +07:00
|
|
|
#include <linux/seqlock.h>
|
|
|
|
#include <linux/rcupdate.h>
|
2013-06-27 18:48:16 +07:00
|
|
|
|
|
|
|
extern struct ww_class reservation_ww_class;
|
2014-07-01 17:58:00 +07:00
|
|
|
extern struct lock_class_key reservation_seqcount_class;
|
|
|
|
extern const char reservation_seqcount_string[];
|
2013-06-27 18:48:16 +07:00
|
|
|
|
2016-04-01 03:26:51 +07:00
|
|
|
/**
|
|
|
|
* struct reservation_object_list - a list of shared fences
|
|
|
|
* @rcu: for internal use
|
|
|
|
* @shared_count: table of shared fences
|
|
|
|
* @shared_max: for growing shared fence table
|
|
|
|
* @shared: shared fence table
|
|
|
|
*/
|
2014-07-01 17:57:54 +07:00
|
|
|
struct reservation_object_list {
|
2014-07-01 17:58:00 +07:00
|
|
|
struct rcu_head rcu;
|
2014-07-01 17:57:54 +07:00
|
|
|
u32 shared_count, shared_max;
|
2016-10-25 19:00:45 +07:00
|
|
|
struct dma_fence __rcu *shared[];
|
2014-07-01 17:57:54 +07:00
|
|
|
};
|
|
|
|
|
2016-04-01 03:26:51 +07:00
|
|
|
/**
|
|
|
|
* struct reservation_object - a reservation object manages fences for a buffer
|
|
|
|
* @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
|
|
|
|
*/
|
2013-06-27 18:48:16 +07:00
|
|
|
struct reservation_object {
|
|
|
|
struct ww_mutex lock;
|
2014-07-01 17:58:00 +07:00
|
|
|
seqcount_t seq;
|
2014-07-01 17:57:37 +07:00
|
|
|
|
2016-10-25 19:00:45 +07:00
|
|
|
struct dma_fence __rcu *fence_excl;
|
2014-07-01 17:58:00 +07:00
|
|
|
struct reservation_object_list __rcu *fence;
|
2013-06-27 18:48:16 +07:00
|
|
|
};
|
|
|
|
|
2014-07-01 17:58:00 +07:00
|
|
|
#define reservation_object_held(obj) lockdep_is_held(&(obj)->lock.base)
|
2014-07-01 17:57:54 +07:00
|
|
|
#define reservation_object_assert_held(obj) \
|
|
|
|
lockdep_assert_held(&(obj)->lock.base)
|
|
|
|
|
2016-04-01 03:26:51 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_init - initialize a reservation object
|
|
|
|
* @obj: the reservation object
|
|
|
|
*/
|
2013-06-27 18:48:16 +07:00
|
|
|
static inline void
|
|
|
|
reservation_object_init(struct reservation_object *obj)
|
|
|
|
{
|
|
|
|
ww_mutex_init(&obj->lock, &reservation_ww_class);
|
2014-07-01 17:57:37 +07:00
|
|
|
|
2014-07-01 17:58:00 +07:00
|
|
|
__seqcount_init(&obj->seq, reservation_seqcount_string, &reservation_seqcount_class);
|
|
|
|
RCU_INIT_POINTER(obj->fence, NULL);
|
|
|
|
RCU_INIT_POINTER(obj->fence_excl, NULL);
|
2013-06-27 18:48:16 +07:00
|
|
|
}
|
|
|
|
|
2016-04-01 03:26:51 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_fini - destroys a reservation object
|
|
|
|
* @obj: the reservation object
|
|
|
|
*/
|
2013-06-27 18:48:16 +07:00
|
|
|
static inline void
|
|
|
|
reservation_object_fini(struct reservation_object *obj)
|
|
|
|
{
|
2014-07-01 17:57:37 +07:00
|
|
|
int i;
|
2014-07-01 17:57:54 +07:00
|
|
|
struct reservation_object_list *fobj;
|
2016-10-25 19:00:45 +07:00
|
|
|
struct dma_fence *excl;
|
2014-07-01 17:57:37 +07:00
|
|
|
|
2014-07-01 17:57:54 +07:00
|
|
|
/*
|
|
|
|
* This object should be dead and all references must have
|
2014-07-01 17:58:00 +07:00
|
|
|
* been released to it, so no need to be protected with rcu.
|
2014-07-01 17:57:54 +07:00
|
|
|
*/
|
2014-07-01 17:58:00 +07:00
|
|
|
excl = rcu_dereference_protected(obj->fence_excl, 1);
|
|
|
|
if (excl)
|
2016-10-25 19:00:45 +07:00
|
|
|
dma_fence_put(excl);
|
2014-07-01 17:57:54 +07:00
|
|
|
|
2014-07-01 17:58:00 +07:00
|
|
|
fobj = rcu_dereference_protected(obj->fence, 1);
|
2014-07-01 17:57:54 +07:00
|
|
|
if (fobj) {
|
|
|
|
for (i = 0; i < fobj->shared_count; ++i)
|
2016-10-25 19:00:45 +07:00
|
|
|
dma_fence_put(rcu_dereference_protected(fobj->shared[i], 1));
|
2014-07-01 17:57:54 +07:00
|
|
|
|
|
|
|
kfree(fobj);
|
|
|
|
}
|
2014-07-01 17:57:37 +07:00
|
|
|
|
2013-06-27 18:48:16 +07:00
|
|
|
ww_mutex_destroy(&obj->lock);
|
|
|
|
}
|
|
|
|
|
2016-04-01 03:26:51 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_get_list - get the reservation object's
|
|
|
|
* shared fence list, with update-side lock held
|
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Returns the shared fence list. Does NOT take references to
|
|
|
|
* the fence. The obj->lock must be held.
|
|
|
|
*/
|
2014-07-01 17:57:54 +07:00
|
|
|
static inline struct reservation_object_list *
|
|
|
|
reservation_object_get_list(struct reservation_object *obj)
|
|
|
|
{
|
2014-07-01 17:58:00 +07:00
|
|
|
return rcu_dereference_protected(obj->fence,
|
|
|
|
reservation_object_held(obj));
|
2014-07-01 17:57:54 +07:00
|
|
|
}
|
|
|
|
|
2016-11-15 22:46:42 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_lock - lock the reservation object
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
static inline int
|
|
|
|
reservation_object_lock(struct reservation_object *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
|
|
|
{
|
|
|
|
return ww_mutex_lock(&obj->lock, ctx);
|
|
|
|
}
|
|
|
|
|
2017-11-09 15:59:05 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_lock_interruptible - lock the reservation object
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
static inline int
|
|
|
|
reservation_object_lock_interruptible(struct reservation_object *obj,
|
|
|
|
struct ww_acquire_ctx *ctx)
|
|
|
|
{
|
|
|
|
return ww_mutex_lock_interruptible(&obj->lock, ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-21 16:30:00 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_trylock - trylock the reservation object
|
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
static inline bool __must_check
|
|
|
|
reservation_object_trylock(struct reservation_object *obj)
|
|
|
|
{
|
|
|
|
return ww_mutex_trylock(&obj->lock);
|
|
|
|
}
|
|
|
|
|
2016-11-15 22:46:42 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_unlock - unlock the reservation object
|
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Unlocks the reservation object following exclusive access.
|
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
reservation_object_unlock(struct reservation_object *obj)
|
|
|
|
{
|
2018-10-04 19:45:17 +07:00
|
|
|
#ifdef CONFIG_DEBUG_MUTEXES
|
|
|
|
/* Test shared fence slot reservation */
|
|
|
|
if (obj->fence)
|
|
|
|
obj->fence->shared_max = obj->fence->shared_count;
|
|
|
|
#endif
|
2016-11-15 22:46:42 +07:00
|
|
|
ww_mutex_unlock(&obj->lock);
|
|
|
|
}
|
|
|
|
|
2016-04-01 03:26:51 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_get_excl - get the reservation object's
|
|
|
|
* exclusive fence, with update-side lock held
|
|
|
|
* @obj: the reservation object
|
|
|
|
*
|
|
|
|
* Returns the exclusive fence (if any). Does NOT take a
|
|
|
|
* reference. The obj->lock must be held.
|
|
|
|
*
|
|
|
|
* RETURNS
|
|
|
|
* The exclusive fence or NULL
|
|
|
|
*/
|
2016-10-25 19:00:45 +07:00
|
|
|
static inline struct dma_fence *
|
2014-07-01 17:57:54 +07:00
|
|
|
reservation_object_get_excl(struct reservation_object *obj)
|
|
|
|
{
|
2014-07-01 17:58:00 +07:00
|
|
|
return rcu_dereference_protected(obj->fence_excl,
|
|
|
|
reservation_object_held(obj));
|
2014-07-01 17:57:54 +07:00
|
|
|
}
|
|
|
|
|
2016-04-01 03:26:51 +07:00
|
|
|
/**
|
|
|
|
* reservation_object_get_excl_rcu - get the reservation object's
|
|
|
|
* 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
|
|
|
|
*/
|
2016-10-25 19:00:45 +07:00
|
|
|
static inline struct dma_fence *
|
2016-04-01 03:23:51 +07:00
|
|
|
reservation_object_get_excl_rcu(struct reservation_object *obj)
|
|
|
|
{
|
2016-10-25 19:00:45 +07:00
|
|
|
struct dma_fence *fence;
|
2016-11-14 18:55:40 +07:00
|
|
|
|
|
|
|
if (!rcu_access_pointer(obj->fence_excl))
|
|
|
|
return NULL;
|
|
|
|
|
2016-04-01 03:23:51 +07:00
|
|
|
rcu_read_lock();
|
2016-11-14 18:55:40 +07:00
|
|
|
fence = dma_fence_get_rcu_safe(&obj->fence_excl);
|
2016-04-01 03:23:51 +07:00
|
|
|
rcu_read_unlock();
|
2016-11-14 18:55:40 +07:00
|
|
|
|
2016-04-01 03:23:51 +07:00
|
|
|
return fence;
|
|
|
|
}
|
|
|
|
|
2018-09-19 21:12:25 +07:00
|
|
|
int reservation_object_reserve_shared(struct reservation_object *obj,
|
|
|
|
unsigned int num_fences);
|
2014-07-01 17:57:54 +07:00
|
|
|
void reservation_object_add_shared_fence(struct reservation_object *obj,
|
2016-10-25 19:00:45 +07:00
|
|
|
struct dma_fence *fence);
|
2014-07-01 17:57:54 +07:00
|
|
|
|
|
|
|
void reservation_object_add_excl_fence(struct reservation_object *obj,
|
2016-10-25 19:00:45 +07:00
|
|
|
struct dma_fence *fence);
|
2014-07-01 17:57:54 +07:00
|
|
|
|
2014-07-01 17:58:00 +07:00
|
|
|
int reservation_object_get_fences_rcu(struct reservation_object *obj,
|
2016-10-25 19:00:45 +07:00
|
|
|
struct dma_fence **pfence_excl,
|
2014-07-01 17:58:00 +07:00
|
|
|
unsigned *pshared_count,
|
2016-10-25 19:00:45 +07:00
|
|
|
struct dma_fence ***pshared);
|
2014-07-01 17:58:00 +07:00
|
|
|
|
2017-08-11 00:01:48 +07:00
|
|
|
int reservation_object_copy_fences(struct reservation_object *dst,
|
|
|
|
struct reservation_object *src);
|
|
|
|
|
2014-07-01 17:58:00 +07:00
|
|
|
long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
|
|
|
|
bool wait_all, bool intr,
|
|
|
|
unsigned long timeout);
|
|
|
|
|
|
|
|
bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
|
|
|
|
bool test_all);
|
|
|
|
|
2013-06-27 18:48:16 +07:00
|
|
|
#endif /* _LINUX_RESERVATION_H */
|