14cd92098Szrj /*
24cd92098Szrj * Header file for reservations for dma-buf and ttm
34cd92098Szrj *
44cd92098Szrj * Copyright(C) 2011 Linaro Limited. All rights reserved.
54cd92098Szrj * Copyright (C) 2012-2013 Canonical Ltd
64cd92098Szrj * Copyright (C) 2012 Texas Instruments
74cd92098Szrj *
84cd92098Szrj * Authors:
99a12856eSFrançois Tigeot * Rob Clark <robdclark@gmail.com>
104cd92098Szrj * Maarten Lankhorst <maarten.lankhorst@canonical.com>
114cd92098Szrj * Thomas Hellstrom <thellstrom-at-vmware-dot-com>
124cd92098Szrj *
134cd92098Szrj * Based on bo.c which bears the following copyright notice,
144cd92098Szrj * but is dual licensed:
154cd92098Szrj *
164cd92098Szrj * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
174cd92098Szrj * All Rights Reserved.
184cd92098Szrj *
194cd92098Szrj * Permission is hereby granted, free of charge, to any person obtaining a
204cd92098Szrj * copy of this software and associated documentation files (the
214cd92098Szrj * "Software"), to deal in the Software without restriction, including
224cd92098Szrj * without limitation the rights to use, copy, modify, merge, publish,
234cd92098Szrj * distribute, sub license, and/or sell copies of the Software, and to
244cd92098Szrj * permit persons to whom the Software is furnished to do so, subject to
254cd92098Szrj * the following conditions:
264cd92098Szrj *
274cd92098Szrj * The above copyright notice and this permission notice (including the
284cd92098Szrj * next paragraph) shall be included in all copies or substantial portions
294cd92098Szrj * of the Software.
304cd92098Szrj *
314cd92098Szrj * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
324cd92098Szrj * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
334cd92098Szrj * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
344cd92098Szrj * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
354cd92098Szrj * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
364cd92098Szrj * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
374cd92098Szrj * USE OR OTHER DEALINGS IN THE SOFTWARE.
384cd92098Szrj */
394cd92098Szrj #ifndef _LINUX_RESERVATION_H
404cd92098Szrj #define _LINUX_RESERVATION_H
414cd92098Szrj
424cd92098Szrj #include <linux/ww_mutex.h>
436559babbSFrançois Tigeot #include <linux/dma-fence.h>
443306aed3SFrançois Tigeot #include <linux/slab.h>
453306aed3SFrançois Tigeot #include <linux/seqlock.h>
4694a02576SFrançois Tigeot #include <linux/rcupdate.h>
474cd92098Szrj
484cd92098Szrj extern struct ww_class reservation_ww_class;
493306aed3SFrançois Tigeot extern struct lock_class_key reservation_seqcount_class;
503306aed3SFrançois Tigeot extern const char reservation_seqcount_string[];
514cd92098Szrj
529a12856eSFrançois Tigeot /**
539a12856eSFrançois Tigeot * struct reservation_object_list - a list of shared fences
549a12856eSFrançois Tigeot * @rcu: for internal use
559a12856eSFrançois Tigeot * @shared_count: table of shared fences
569a12856eSFrançois Tigeot * @shared_max: for growing shared fence table
579a12856eSFrançois Tigeot * @shared: shared fence table
589a12856eSFrançois Tigeot */
599a12856eSFrançois Tigeot struct reservation_object_list {
609a12856eSFrançois Tigeot struct rcu_head rcu;
619a12856eSFrançois Tigeot u32 shared_count, shared_max;
626559babbSFrançois Tigeot struct dma_fence __rcu *shared[];
639a12856eSFrançois Tigeot };
649a12856eSFrançois Tigeot
659a12856eSFrançois Tigeot /**
669a12856eSFrançois Tigeot * struct reservation_object - a reservation object manages fences for a buffer
679a12856eSFrançois Tigeot * @lock: update side lock
689a12856eSFrançois Tigeot * @seq: sequence count for managing RCU read-side synchronization
699a12856eSFrançois Tigeot * @fence_excl: the exclusive fence, if there is one currently
709a12856eSFrançois Tigeot * @fence: list of current shared fences
719a12856eSFrançois Tigeot * @staged: staged copy of shared fences for RCU updates
729a12856eSFrançois Tigeot */
734cd92098Szrj struct reservation_object {
744cd92098Szrj struct ww_mutex lock;
753306aed3SFrançois Tigeot seqcount_t seq;
763306aed3SFrançois Tigeot
776559babbSFrançois Tigeot struct dma_fence __rcu *fence_excl;
789a12856eSFrançois Tigeot struct reservation_object_list __rcu *fence;
799a12856eSFrançois Tigeot struct reservation_object_list *staged;
804cd92098Szrj };
814cd92098Szrj
823306aed3SFrançois Tigeot #define reservation_object_held(obj) lockdep_is_held(&(obj)->lock.base)
833306aed3SFrançois Tigeot #define reservation_object_assert_held(obj) \
843306aed3SFrançois Tigeot lockdep_assert_held(&(obj)->lock.base)
853306aed3SFrançois Tigeot
863306aed3SFrançois Tigeot /**
873306aed3SFrançois Tigeot * reservation_object_init - initialize a reservation object
883306aed3SFrançois Tigeot * @obj: the reservation object
893306aed3SFrançois Tigeot */
903306aed3SFrançois Tigeot static inline void
reservation_object_init(struct reservation_object * obj)913306aed3SFrançois Tigeot reservation_object_init(struct reservation_object *obj)
923306aed3SFrançois Tigeot {
933306aed3SFrançois Tigeot ww_mutex_init(&obj->lock, &reservation_ww_class);
943306aed3SFrançois Tigeot
953306aed3SFrançois Tigeot __seqcount_init(&obj->seq, reservation_seqcount_string, &reservation_seqcount_class);
969a12856eSFrançois Tigeot RCU_INIT_POINTER(obj->fence, NULL);
979a12856eSFrançois Tigeot RCU_INIT_POINTER(obj->fence_excl, NULL);
989a12856eSFrançois Tigeot obj->staged = NULL;
993306aed3SFrançois Tigeot }
1003306aed3SFrançois Tigeot
1013306aed3SFrançois Tigeot /**
1023306aed3SFrançois Tigeot * reservation_object_fini - destroys a reservation object
1033306aed3SFrançois Tigeot * @obj: the reservation object
1043306aed3SFrançois Tigeot */
1053306aed3SFrançois Tigeot static inline void
reservation_object_fini(struct reservation_object * obj)1063306aed3SFrançois Tigeot reservation_object_fini(struct reservation_object *obj)
1073306aed3SFrançois Tigeot {
1089a12856eSFrançois Tigeot int i;
1099a12856eSFrançois Tigeot struct reservation_object_list *fobj;
1106559babbSFrançois Tigeot struct dma_fence *excl;
1119a12856eSFrançois Tigeot
1129a12856eSFrançois Tigeot /*
1139a12856eSFrançois Tigeot * This object should be dead and all references must have
1149a12856eSFrançois Tigeot * been released to it, so no need to be protected with rcu.
1159a12856eSFrançois Tigeot */
1169a12856eSFrançois Tigeot excl = rcu_dereference_protected(obj->fence_excl, 1);
1179a12856eSFrançois Tigeot if (excl)
1186559babbSFrançois Tigeot dma_fence_put(excl);
1199a12856eSFrançois Tigeot
1209a12856eSFrançois Tigeot fobj = rcu_dereference_protected(obj->fence, 1);
1219a12856eSFrançois Tigeot if (fobj) {
1229a12856eSFrançois Tigeot for (i = 0; i < fobj->shared_count; ++i)
1236559babbSFrançois Tigeot dma_fence_put(rcu_dereference_protected(fobj->shared[i], 1));
1249a12856eSFrançois Tigeot
1259a12856eSFrançois Tigeot kfree(fobj);
1269a12856eSFrançois Tigeot }
1279a12856eSFrançois Tigeot kfree(obj->staged);
1289a12856eSFrançois Tigeot
1293306aed3SFrançois Tigeot ww_mutex_destroy(&obj->lock);
1303306aed3SFrançois Tigeot }
1313306aed3SFrançois Tigeot
1329a12856eSFrançois Tigeot /**
1339a12856eSFrançois Tigeot * reservation_object_get_list - get the reservation object's
1349a12856eSFrançois Tigeot * shared fence list, with update-side lock held
1359a12856eSFrançois Tigeot * @obj: the reservation object
1369a12856eSFrançois Tigeot *
1379a12856eSFrançois Tigeot * Returns the shared fence list. Does NOT take references to
1389a12856eSFrançois Tigeot * the fence. The obj->lock must be held.
1399a12856eSFrançois Tigeot */
1409a12856eSFrançois Tigeot static inline struct reservation_object_list *
reservation_object_get_list(struct reservation_object * obj)1419a12856eSFrançois Tigeot reservation_object_get_list(struct reservation_object *obj)
1429a12856eSFrançois Tigeot {
1439a12856eSFrançois Tigeot return rcu_dereference_protected(obj->fence,
1449a12856eSFrançois Tigeot reservation_object_held(obj));
1459a12856eSFrançois Tigeot }
1469a12856eSFrançois Tigeot
1479a12856eSFrançois Tigeot /**
148a85cb24fSFrançois Tigeot * reservation_object_lock - lock the reservation object
149a85cb24fSFrançois Tigeot * @obj: the reservation object
150a85cb24fSFrançois Tigeot * @ctx: the locking context
151a85cb24fSFrançois Tigeot *
152a85cb24fSFrançois Tigeot * Locks the reservation object for exclusive access and modification. Note,
153a85cb24fSFrançois Tigeot * that the lock is only against other writers, readers will run concurrently
154a85cb24fSFrançois Tigeot * with a writer under RCU. The seqlock is used to notify readers if they
155a85cb24fSFrançois Tigeot * overlap with a writer.
156a85cb24fSFrançois Tigeot *
157a85cb24fSFrançois Tigeot * As the reservation object may be locked by multiple parties in an
158a85cb24fSFrançois Tigeot * undefined order, a #ww_acquire_ctx is passed to unwind if a cycle
159a85cb24fSFrançois Tigeot * is detected. See ww_mutex_lock() and ww_acquire_init(). A reservation
160a85cb24fSFrançois Tigeot * object may be locked by itself by passing NULL as @ctx.
161a85cb24fSFrançois Tigeot */
162a85cb24fSFrançois Tigeot static inline int
reservation_object_lock(struct reservation_object * obj,struct ww_acquire_ctx * ctx)163a85cb24fSFrançois Tigeot reservation_object_lock(struct reservation_object *obj,
164a85cb24fSFrançois Tigeot struct ww_acquire_ctx *ctx)
165a85cb24fSFrançois Tigeot {
166a85cb24fSFrançois Tigeot return ww_mutex_lock(&obj->lock, ctx);
167a85cb24fSFrançois Tigeot }
168a85cb24fSFrançois Tigeot
169a85cb24fSFrançois Tigeot /**
170a85cb24fSFrançois Tigeot * reservation_object_trylock - trylock the reservation object
171a85cb24fSFrançois Tigeot * @obj: the reservation object
172a85cb24fSFrançois Tigeot *
173a85cb24fSFrançois Tigeot * Tries to lock the reservation object for exclusive access and modification.
174a85cb24fSFrançois Tigeot * Note, that the lock is only against other writers, readers will run
175a85cb24fSFrançois Tigeot * concurrently with a writer under RCU. The seqlock is used to notify readers
176a85cb24fSFrançois Tigeot * if they overlap with a writer.
177a85cb24fSFrançois Tigeot *
178a85cb24fSFrançois Tigeot * Also note that since no context is provided, no deadlock protection is
179a85cb24fSFrançois Tigeot * possible.
180a85cb24fSFrançois Tigeot *
181a85cb24fSFrançois Tigeot * Returns true if the lock was acquired, false otherwise.
182a85cb24fSFrançois Tigeot */
183a85cb24fSFrançois Tigeot static inline bool __must_check
reservation_object_trylock(struct reservation_object * obj)184a85cb24fSFrançois Tigeot reservation_object_trylock(struct reservation_object *obj)
185a85cb24fSFrançois Tigeot {
186a85cb24fSFrançois Tigeot return ww_mutex_trylock(&obj->lock);
187a85cb24fSFrançois Tigeot }
188a85cb24fSFrançois Tigeot
189a85cb24fSFrançois Tigeot /**
190a85cb24fSFrançois Tigeot * reservation_object_unlock - unlock the reservation object
191a85cb24fSFrançois Tigeot * @obj: the reservation object
192a85cb24fSFrançois Tigeot *
193a85cb24fSFrançois Tigeot * Unlocks the reservation object following exclusive access.
194a85cb24fSFrançois Tigeot */
195a85cb24fSFrançois Tigeot static inline void
reservation_object_unlock(struct reservation_object * obj)196a85cb24fSFrançois Tigeot reservation_object_unlock(struct reservation_object *obj)
197a85cb24fSFrançois Tigeot {
198a85cb24fSFrançois Tigeot ww_mutex_unlock(&obj->lock);
199a85cb24fSFrançois Tigeot }
200a85cb24fSFrançois Tigeot
201a85cb24fSFrançois Tigeot /**
2029a12856eSFrançois Tigeot * reservation_object_get_excl - get the reservation object's
2039a12856eSFrançois Tigeot * exclusive fence, with update-side lock held
2049a12856eSFrançois Tigeot * @obj: the reservation object
2059a12856eSFrançois Tigeot *
2069a12856eSFrançois Tigeot * Returns the exclusive fence (if any). Does NOT take a
2079a12856eSFrançois Tigeot * reference. The obj->lock must be held.
2089a12856eSFrançois Tigeot *
2099a12856eSFrançois Tigeot * RETURNS
2109a12856eSFrançois Tigeot * The exclusive fence or NULL
2119a12856eSFrançois Tigeot */
2126559babbSFrançois Tigeot static inline struct dma_fence *
reservation_object_get_excl(struct reservation_object * obj)2139a12856eSFrançois Tigeot reservation_object_get_excl(struct reservation_object *obj)
2149a12856eSFrançois Tigeot {
2159a12856eSFrançois Tigeot return rcu_dereference_protected(obj->fence_excl,
2169a12856eSFrançois Tigeot reservation_object_held(obj));
2179a12856eSFrançois Tigeot }
2189a12856eSFrançois Tigeot
2199a12856eSFrançois Tigeot /**
2209a12856eSFrançois Tigeot * reservation_object_get_excl_rcu - get the reservation object's
2219a12856eSFrançois Tigeot * exclusive fence, without lock held.
2229a12856eSFrançois Tigeot * @obj: the reservation object
2239a12856eSFrançois Tigeot *
2249a12856eSFrançois Tigeot * If there is an exclusive fence, this atomically increments it's
2259a12856eSFrançois Tigeot * reference count and returns it.
2269a12856eSFrançois Tigeot *
2279a12856eSFrançois Tigeot * RETURNS
2289a12856eSFrançois Tigeot * The exclusive fence or NULL if none
2299a12856eSFrançois Tigeot */
2306559babbSFrançois Tigeot static inline struct dma_fence *
reservation_object_get_excl_rcu(struct reservation_object * obj)2319a12856eSFrançois Tigeot reservation_object_get_excl_rcu(struct reservation_object *obj)
2329a12856eSFrançois Tigeot {
2336559babbSFrançois Tigeot struct dma_fence *fence;
234a85cb24fSFrançois Tigeot
235a85cb24fSFrançois Tigeot if (!rcu_access_pointer(obj->fence_excl))
236a85cb24fSFrançois Tigeot return NULL;
237a85cb24fSFrançois Tigeot
2389a12856eSFrançois Tigeot rcu_read_lock();
239a85cb24fSFrançois Tigeot fence = dma_fence_get_rcu_safe(&obj->fence_excl);
2409a12856eSFrançois Tigeot rcu_read_unlock();
241a85cb24fSFrançois Tigeot
2429a12856eSFrançois Tigeot return fence;
2439a12856eSFrançois Tigeot }
2449a12856eSFrançois Tigeot
2459a12856eSFrançois Tigeot int reservation_object_reserve_shared(struct reservation_object *obj);
2469a12856eSFrançois Tigeot void reservation_object_add_shared_fence(struct reservation_object *obj,
2476559babbSFrançois Tigeot struct dma_fence *fence);
2489a12856eSFrançois Tigeot
2499a12856eSFrançois Tigeot void reservation_object_add_excl_fence(struct reservation_object *obj,
2506559babbSFrançois Tigeot struct dma_fence *fence);
2519a12856eSFrançois Tigeot
2529a12856eSFrançois Tigeot int reservation_object_get_fences_rcu(struct reservation_object *obj,
2536559babbSFrançois Tigeot struct dma_fence **pfence_excl,
2549a12856eSFrançois Tigeot unsigned *pshared_count,
2556559babbSFrançois Tigeot struct dma_fence ***pshared);
2569a12856eSFrançois Tigeot
257*3f2dd94aSFrançois Tigeot int reservation_object_copy_fences(struct reservation_object *dst,
258*3f2dd94aSFrançois Tigeot struct reservation_object *src);
259*3f2dd94aSFrançois Tigeot
2609a12856eSFrançois Tigeot long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
2619a12856eSFrançois Tigeot bool wait_all, bool intr,
2629a12856eSFrançois Tigeot unsigned long timeout);
2639a12856eSFrançois Tigeot
2649a12856eSFrançois Tigeot bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
2659a12856eSFrançois Tigeot bool test_all);
2669a12856eSFrançois Tigeot
2674cd92098Szrj #endif /* _LINUX_RESERVATION_H */
268