2 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
4 * Based on bo.c which bears the following copyright notice,
5 * but is dual licensed:
7 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the
12 * "Software"), to deal in the Software without restriction, including
13 * without limitation the rights to use, copy, modify, merge, publish,
14 * distribute, sub license, and/or sell copies of the Software, and to
15 * permit persons to whom the Software is furnished to do so, subject to
16 * the following conditions:
18 * The above copyright notice and this permission notice (including the
19 * next paragraph) shall be included in all copies or substantial portions
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
25 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
26 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
27 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
28 * USE OR OTHER DEALINGS IN THE SOFTWARE.
30 **************************************************************************/
32 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
35 #include <linux/reservation.h>
36 #include <linux/export.h>
39 * DOC: Reservation Object Overview
41 * The reservation object provides a mechanism to manage shared and
42 * exclusive fences associated with a buffer. A reservation object
43 * can have attached one exclusive fence (normally associated with
44 * write operations) or N shared fences (read operations). The RCU
45 * mechanism is used to protect read access to fences from locked
49 DEFINE_WW_CLASS(reservation_ww_class);
50 EXPORT_SYMBOL(reservation_ww_class);
52 struct lock_class_key reservation_seqcount_class;
53 EXPORT_SYMBOL(reservation_seqcount_class);
55 const char reservation_seqcount_string[] = "reservation_seqcount";
56 EXPORT_SYMBOL(reservation_seqcount_string);
59 * reservation_object_reserve_shared - Reserve space to add a shared
60 * fence to a reservation_object.
61 * @obj: reservation object
63 * Should be called before reservation_object_add_shared_fence(). Must
64 * be called with obj->lock held.
67 * Zero for success, or -errno
69 int reservation_object_reserve_shared(struct reservation_object *obj)
71 struct reservation_object_list *fobj, *old;
74 old = reservation_object_get_list(obj);
76 if (old && old->shared_max) {
77 if (old->shared_count < old->shared_max) {
78 /* perform an in-place update */
83 max = old->shared_max * 2;
88 * resize obj->staged or allocate if it doesn't exist,
89 * noop if already correct size
91 fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
97 fobj->shared_max = max;
100 EXPORT_SYMBOL(reservation_object_reserve_shared);
103 reservation_object_add_shared_inplace(struct reservation_object *obj,
104 struct reservation_object_list *fobj,
105 struct dma_fence *fence)
109 dma_fence_get(fence);
112 write_seqcount_begin(&obj->seq);
114 for (i = 0; i < fobj->shared_count; ++i) {
115 struct dma_fence *old_fence;
117 old_fence = rcu_dereference_protected(fobj->shared[i],
118 reservation_object_held(obj));
120 if (old_fence->context == fence->context) {
121 /* memory barrier is added by write_seqcount_begin */
122 RCU_INIT_POINTER(fobj->shared[i], fence);
123 write_seqcount_end(&obj->seq);
126 dma_fence_put(old_fence);
132 * memory barrier is added by write_seqcount_begin,
133 * fobj->shared_count is protected by this lock too
135 RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
136 fobj->shared_count++;
138 write_seqcount_end(&obj->seq);
143 reservation_object_add_shared_replace(struct reservation_object *obj,
144 struct reservation_object_list *old,
145 struct reservation_object_list *fobj,
146 struct dma_fence *fence)
149 struct dma_fence *old_fence = NULL;
151 dma_fence_get(fence);
154 RCU_INIT_POINTER(fobj->shared[0], fence);
155 fobj->shared_count = 1;
160 * no need to bump fence refcounts, rcu_read access
161 * requires the use of kref_get_unless_zero, and the
162 * references from the old struct are carried over to
165 fobj->shared_count = old->shared_count;
167 for (i = 0; i < old->shared_count; ++i) {
168 struct dma_fence *check;
170 check = rcu_dereference_protected(old->shared[i],
171 reservation_object_held(obj));
173 if (!old_fence && check->context == fence->context) {
175 RCU_INIT_POINTER(fobj->shared[i], fence);
177 RCU_INIT_POINTER(fobj->shared[i], check);
180 RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
181 fobj->shared_count++;
186 write_seqcount_begin(&obj->seq);
188 * RCU_INIT_POINTER can be used here,
189 * seqcount provides the necessary barriers
191 RCU_INIT_POINTER(obj->fence, fobj);
192 write_seqcount_end(&obj->seq);
199 dma_fence_put(old_fence);
203 * reservation_object_add_shared_fence - Add a fence to a shared slot
204 * @obj: the reservation object
205 * @fence: the shared fence to add
207 * Add a fence to a shared slot, obj->lock must be held, and
208 * reservation_object_reserve_shared() has been called.
210 void reservation_object_add_shared_fence(struct reservation_object *obj,
211 struct dma_fence *fence)
213 struct reservation_object_list *old, *fobj = obj->staged;
215 old = reservation_object_get_list(obj);
219 BUG_ON(old->shared_count >= old->shared_max);
220 reservation_object_add_shared_inplace(obj, old, fence);
222 reservation_object_add_shared_replace(obj, old, fobj, fence);
224 EXPORT_SYMBOL(reservation_object_add_shared_fence);
227 * reservation_object_add_excl_fence - Add an exclusive fence.
228 * @obj: the reservation object
229 * @fence: the shared fence to add
231 * Add a fence to the exclusive slot. The obj->lock must be held.
233 void reservation_object_add_excl_fence(struct reservation_object *obj,
234 struct dma_fence *fence)
236 struct dma_fence *old_fence = reservation_object_get_excl(obj);
237 struct reservation_object_list *old;
240 old = reservation_object_get_list(obj);
242 i = old->shared_count;
245 dma_fence_get(fence);
248 write_seqcount_begin(&obj->seq);
249 /* write_seqcount_begin provides the necessary memory barrier */
250 RCU_INIT_POINTER(obj->fence_excl, fence);
252 old->shared_count = 0;
253 write_seqcount_end(&obj->seq);
256 /* inplace update, no shared fences */
258 dma_fence_put(rcu_dereference_protected(old->shared[i],
259 reservation_object_held(obj)));
262 dma_fence_put(old_fence);
264 EXPORT_SYMBOL(reservation_object_add_excl_fence);
267 * reservation_object_get_fences_rcu - Get an object's shared and exclusive
268 * fences without update side lock held
269 * @obj: the reservation object
270 * @pfence_excl: the returned exclusive fence (or NULL)
271 * @pshared_count: the number of shared fences returned
272 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
273 * the required size, and must be freed by caller)
278 int reservation_object_get_fences_rcu(struct reservation_object *obj,
279 struct dma_fence **pfence_excl,
280 unsigned *pshared_count,
281 struct dma_fence ***pshared)
283 struct dma_fence **shared = NULL;
284 struct dma_fence *fence_excl;
285 unsigned int shared_count;
289 struct reservation_object_list *fobj;
293 shared_count = i = 0;
296 seq = read_seqcount_begin(&obj->seq);
298 fence_excl = rcu_dereference(obj->fence_excl);
299 if (fence_excl && !dma_fence_get_rcu(fence_excl))
302 fobj = rcu_dereference(obj->fence);
304 struct dma_fence **nshared;
305 size_t sz = sizeof(*shared) * fobj->shared_max;
307 nshared = krealloc(shared, sz,
308 GFP_NOWAIT | __GFP_NOWARN);
311 nshared = krealloc(shared, sz, GFP_KERNEL);
321 shared_count = fobj->shared_count;
323 for (i = 0; i < shared_count; ++i) {
324 shared[i] = rcu_dereference(fobj->shared[i]);
325 if (!dma_fence_get_rcu(shared[i]))
330 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
332 dma_fence_put(shared[i]);
333 dma_fence_put(fence_excl);
347 *pshared_count = shared_count;
349 *pfence_excl = fence_excl;
353 EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
356 * reservation_object_wait_timeout_rcu - Wait on reservation's objects
357 * shared and/or exclusive fences.
358 * @obj: the reservation object
359 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
360 * @intr: if true, do interruptible wait
361 * @timeout: timeout value in jiffies or zero to return immediately
364 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
365 * greater than zer on success.
367 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
368 bool wait_all, bool intr,
369 unsigned long timeout)
371 struct dma_fence *fence;
372 unsigned seq, shared_count, i = 0;
373 long ret = timeout ? timeout : 1;
378 seq = read_seqcount_begin(&obj->seq);
382 struct reservation_object_list *fobj =
383 rcu_dereference(obj->fence);
386 shared_count = fobj->shared_count;
388 for (i = 0; i < shared_count; ++i) {
389 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
391 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
395 if (!dma_fence_get_rcu(lfence))
398 if (dma_fence_is_signaled(lfence)) {
399 dma_fence_put(lfence);
409 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
412 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
413 &fence_excl->flags)) {
414 if (!dma_fence_get_rcu(fence_excl))
417 if (dma_fence_is_signaled(fence_excl))
418 dma_fence_put(fence_excl);
426 if (read_seqcount_retry(&obj->seq, seq)) {
427 dma_fence_put(fence);
431 ret = dma_fence_wait_timeout(fence, intr, ret);
432 dma_fence_put(fence);
433 if (ret > 0 && wait_all && (i + 1 < shared_count))
442 EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
446 reservation_object_test_signaled_single(struct dma_fence *passed_fence)
448 struct dma_fence *fence, *lfence = passed_fence;
451 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
452 fence = dma_fence_get_rcu(lfence);
456 ret = !!dma_fence_is_signaled(fence);
457 dma_fence_put(fence);
463 * reservation_object_test_signaled_rcu - Test if a reservation object's
464 * fences have been signaled.
465 * @obj: the reservation object
466 * @test_all: if true, test all fences, otherwise only test the exclusive
470 * true if all fences signaled, else false
472 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
475 unsigned seq, shared_count;
482 seq = read_seqcount_begin(&obj->seq);
487 struct reservation_object_list *fobj =
488 rcu_dereference(obj->fence);
491 shared_count = fobj->shared_count;
493 for (i = 0; i < shared_count; ++i) {
494 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
496 ret = reservation_object_test_signaled_single(fence);
503 if (read_seqcount_retry(&obj->seq, seq))
508 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
511 ret = reservation_object_test_signaled_single(
516 if (read_seqcount_retry(&obj->seq, seq))
524 EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);