blob: 87f8f57475007b836370f1c0b28172595d8d032c [file] [log] [blame]
Maarten Lankhorst786d7252013-06-27 13:48:16 +02001/*
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +02002 * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
Maarten Lankhorst786d7252013-06-27 13:48:16 +02003 *
4 * Based on bo.c which bears the following copyright notice,
5 * but is dual licensed:
6 *
7 * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
8 * All Rights Reserved.
9 *
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:
17 *
18 * The above copyright notice and this permission notice (including the
19 * next paragraph) shall be included in all copies or substantial portions
20 * of the Software.
21 *
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.
29 *
30 **************************************************************************/
31/*
32 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
33 */
34
35#include <linux/reservation.h>
36#include <linux/export.h>
37
Rob Clarkdad6c392016-03-31 16:26:51 -040038/**
39 * DOC: Reservation Object Overview
40 *
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
46 * write-side updates.
47 */
48
Maarten Lankhorst786d7252013-06-27 13:48:16 +020049DEFINE_WW_CLASS(reservation_ww_class);
50EXPORT_SYMBOL(reservation_ww_class);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +020051
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +020052struct lock_class_key reservation_seqcount_class;
53EXPORT_SYMBOL(reservation_seqcount_class);
54
55const char reservation_seqcount_string[] = "reservation_seqcount";
56EXPORT_SYMBOL(reservation_seqcount_string);
Rob Clarkdad6c392016-03-31 16:26:51 -040057
58/**
59 * reservation_object_reserve_shared - Reserve space to add a shared
60 * fence to a reservation_object.
61 * @obj: reservation object
62 *
63 * Should be called before reservation_object_add_shared_fence(). Must
64 * be called with obj->lock held.
65 *
66 * RETURNS
67 * Zero for success, or -errno
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +020068 */
69int reservation_object_reserve_shared(struct reservation_object *obj)
70{
71 struct reservation_object_list *fobj, *old;
72 u32 max;
73
74 old = reservation_object_get_list(obj);
75
76 if (old && old->shared_max) {
77 if (old->shared_count < old->shared_max) {
78 /* perform an in-place update */
79 kfree(obj->staged);
80 obj->staged = NULL;
81 return 0;
82 } else
83 max = old->shared_max * 2;
84 } else
85 max = 4;
86
87 /*
88 * resize obj->staged or allocate if it doesn't exist,
89 * noop if already correct size
90 */
91 fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
92 GFP_KERNEL);
93 if (!fobj)
94 return -ENOMEM;
95
96 obj->staged = fobj;
97 fobj->shared_max = max;
98 return 0;
99}
100EXPORT_SYMBOL(reservation_object_reserve_shared);
101
102static void
103reservation_object_add_shared_inplace(struct reservation_object *obj,
104 struct reservation_object_list *fobj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100105 struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200106{
107 u32 i;
108
Chris Wilsonf54d1862016-10-25 13:00:45 +0100109 dma_fence_get(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200110
111 preempt_disable();
112 write_seqcount_begin(&obj->seq);
113
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200114 for (i = 0; i < fobj->shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100115 struct dma_fence *old_fence;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200116
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200117 old_fence = rcu_dereference_protected(fobj->shared[i],
118 reservation_object_held(obj));
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200119
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200120 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);
124 preempt_enable();
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200125
Chris Wilsonf54d1862016-10-25 13:00:45 +0100126 dma_fence_put(old_fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200127 return;
128 }
129 }
130
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200131 /*
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200132 * memory barrier is added by write_seqcount_begin,
133 * fobj->shared_count is protected by this lock too
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200134 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200135 RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200136 fobj->shared_count++;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200137
138 write_seqcount_end(&obj->seq);
139 preempt_enable();
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200140}
141
142static void
143reservation_object_add_shared_replace(struct reservation_object *obj,
144 struct reservation_object_list *old,
145 struct reservation_object_list *fobj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100146 struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200147{
148 unsigned i;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100149 struct dma_fence *old_fence = NULL;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200150
Chris Wilsonf54d1862016-10-25 13:00:45 +0100151 dma_fence_get(fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200152
153 if (!old) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200154 RCU_INIT_POINTER(fobj->shared[0], fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200155 fobj->shared_count = 1;
156 goto done;
157 }
158
159 /*
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
163 * the new.
164 */
165 fobj->shared_count = old->shared_count;
166
167 for (i = 0; i < old->shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100168 struct dma_fence *check;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200169
170 check = rcu_dereference_protected(old->shared[i],
171 reservation_object_held(obj));
172
173 if (!old_fence && check->context == fence->context) {
174 old_fence = check;
175 RCU_INIT_POINTER(fobj->shared[i], fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200176 } else
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200177 RCU_INIT_POINTER(fobj->shared[i], check);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200178 }
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200179 if (!old_fence) {
180 RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
181 fobj->shared_count++;
182 }
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200183
184done:
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200185 preempt_disable();
186 write_seqcount_begin(&obj->seq);
187 /*
188 * RCU_INIT_POINTER can be used here,
189 * seqcount provides the necessary barriers
190 */
191 RCU_INIT_POINTER(obj->fence, fobj);
192 write_seqcount_end(&obj->seq);
193 preempt_enable();
194
195 if (old)
196 kfree_rcu(old, rcu);
197
Christian Königf3e31b72017-08-07 17:32:21 -0400198 dma_fence_put(old_fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200199}
200
Rob Clarkdad6c392016-03-31 16:26:51 -0400201/**
202 * reservation_object_add_shared_fence - Add a fence to a shared slot
203 * @obj: the reservation object
204 * @fence: the shared fence to add
205 *
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200206 * Add a fence to a shared slot, obj->lock must be held, and
Rob Clarkf5bef0b2016-08-19 16:55:34 -0400207 * reservation_object_reserve_shared() has been called.
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200208 */
209void reservation_object_add_shared_fence(struct reservation_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100210 struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200211{
212 struct reservation_object_list *old, *fobj = obj->staged;
213
214 old = reservation_object_get_list(obj);
215 obj->staged = NULL;
216
217 if (!fobj) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200218 BUG_ON(old->shared_count >= old->shared_max);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200219 reservation_object_add_shared_inplace(obj, old, fence);
220 } else
221 reservation_object_add_shared_replace(obj, old, fobj, fence);
222}
223EXPORT_SYMBOL(reservation_object_add_shared_fence);
224
Rob Clarkdad6c392016-03-31 16:26:51 -0400225/**
226 * reservation_object_add_excl_fence - Add an exclusive fence.
227 * @obj: the reservation object
228 * @fence: the shared fence to add
229 *
230 * Add a fence to the exclusive slot. The obj->lock must be held.
231 */
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200232void reservation_object_add_excl_fence(struct reservation_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100233 struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200234{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100235 struct dma_fence *old_fence = reservation_object_get_excl(obj);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200236 struct reservation_object_list *old;
237 u32 i = 0;
238
239 old = reservation_object_get_list(obj);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200240 if (old)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200241 i = old->shared_count;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200242
243 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100244 dma_fence_get(fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200245
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200246 preempt_disable();
247 write_seqcount_begin(&obj->seq);
248 /* write_seqcount_begin provides the necessary memory barrier */
249 RCU_INIT_POINTER(obj->fence_excl, fence);
250 if (old)
251 old->shared_count = 0;
252 write_seqcount_end(&obj->seq);
253 preempt_enable();
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200254
255 /* inplace update, no shared fences */
256 while (i--)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100257 dma_fence_put(rcu_dereference_protected(old->shared[i],
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200258 reservation_object_held(obj)));
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200259
Christian Königf3e31b72017-08-07 17:32:21 -0400260 dma_fence_put(old_fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200261}
262EXPORT_SYMBOL(reservation_object_add_excl_fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200263
Rob Clarkdad6c392016-03-31 16:26:51 -0400264/**
265 * reservation_object_get_fences_rcu - Get an object's shared and exclusive
266 * fences without update side lock held
267 * @obj: the reservation object
268 * @pfence_excl: the returned exclusive fence (or NULL)
269 * @pshared_count: the number of shared fences returned
270 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
271 * the required size, and must be freed by caller)
272 *
273 * RETURNS
274 * Zero or -errno
275 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200276int reservation_object_get_fences_rcu(struct reservation_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100277 struct dma_fence **pfence_excl,
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200278 unsigned *pshared_count,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100279 struct dma_fence ***pshared)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200280{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100281 struct dma_fence **shared = NULL;
282 struct dma_fence *fence_excl;
Chris Wilsonfedf5412016-08-29 08:08:30 +0100283 unsigned int shared_count;
284 int ret = 1;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200285
Chris Wilsonfedf5412016-08-29 08:08:30 +0100286 do {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200287 struct reservation_object_list *fobj;
288 unsigned seq;
Chris Wilsonfedf5412016-08-29 08:08:30 +0100289 unsigned int i;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200290
Chris Wilsonfedf5412016-08-29 08:08:30 +0100291 shared_count = i = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200292
293 rcu_read_lock();
Chris Wilsonfedf5412016-08-29 08:08:30 +0100294 seq = read_seqcount_begin(&obj->seq);
295
296 fence_excl = rcu_dereference(obj->fence_excl);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100297 if (fence_excl && !dma_fence_get_rcu(fence_excl))
Chris Wilsonfedf5412016-08-29 08:08:30 +0100298 goto unlock;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200299
300 fobj = rcu_dereference(obj->fence);
301 if (fobj) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100302 struct dma_fence **nshared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200303 size_t sz = sizeof(*shared) * fobj->shared_max;
304
305 nshared = krealloc(shared, sz,
306 GFP_NOWAIT | __GFP_NOWARN);
307 if (!nshared) {
308 rcu_read_unlock();
309 nshared = krealloc(shared, sz, GFP_KERNEL);
310 if (nshared) {
311 shared = nshared;
312 continue;
313 }
314
315 ret = -ENOMEM;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200316 break;
317 }
318 shared = nshared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200319 shared_count = fobj->shared_count;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200320
321 for (i = 0; i < shared_count; ++i) {
Chris Wilsonfedf5412016-08-29 08:08:30 +0100322 shared[i] = rcu_dereference(fobj->shared[i]);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100323 if (!dma_fence_get_rcu(shared[i]))
Chris Wilsonfedf5412016-08-29 08:08:30 +0100324 break;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200325 }
Chris Wilsonfedf5412016-08-29 08:08:30 +0100326 }
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200327
Chris Wilsonfedf5412016-08-29 08:08:30 +0100328 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
329 while (i--)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100330 dma_fence_put(shared[i]);
331 dma_fence_put(fence_excl);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100332 goto unlock;
333 }
334
335 ret = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200336unlock:
337 rcu_read_unlock();
Chris Wilsonfedf5412016-08-29 08:08:30 +0100338 } while (ret);
339
340 if (!shared_count) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200341 kfree(shared);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100342 shared = NULL;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200343 }
Chris Wilsonfedf5412016-08-29 08:08:30 +0100344
345 *pshared_count = shared_count;
346 *pshared = shared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200347 *pfence_excl = fence_excl;
348
349 return ret;
350}
351EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
352
Rob Clarkdad6c392016-03-31 16:26:51 -0400353/**
354 * reservation_object_wait_timeout_rcu - Wait on reservation's objects
355 * shared and/or exclusive fences.
356 * @obj: the reservation object
357 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
358 * @intr: if true, do interruptible wait
359 * @timeout: timeout value in jiffies or zero to return immediately
360 *
361 * RETURNS
362 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
363 * greater than zer on success.
364 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200365long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
366 bool wait_all, bool intr,
367 unsigned long timeout)
368{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100369 struct dma_fence *fence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200370 unsigned seq, shared_count, i = 0;
Christian König06a66b52016-11-07 16:16:16 -0500371 long ret = timeout ? timeout : 1;
Jammy Zhoufb8b7d22015-01-21 18:35:47 +0800372
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200373retry:
374 fence = NULL;
375 shared_count = 0;
376 seq = read_seqcount_begin(&obj->seq);
377 rcu_read_lock();
378
379 if (wait_all) {
Jagan Teki51366292015-05-21 01:09:31 +0530380 struct reservation_object_list *fobj =
381 rcu_dereference(obj->fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200382
383 if (fobj)
384 shared_count = fobj->shared_count;
385
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200386 for (i = 0; i < shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100387 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200388
Chris Wilsonf54d1862016-10-25 13:00:45 +0100389 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
390 &lfence->flags))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200391 continue;
392
Chris Wilsonf54d1862016-10-25 13:00:45 +0100393 if (!dma_fence_get_rcu(lfence))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200394 goto unlock_retry;
395
Chris Wilsonf54d1862016-10-25 13:00:45 +0100396 if (dma_fence_is_signaled(lfence)) {
397 dma_fence_put(lfence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200398 continue;
399 }
400
401 fence = lfence;
402 break;
403 }
404 }
405
406 if (!shared_count) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100407 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200408
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200409 if (fence_excl &&
Chris Wilsonf54d1862016-10-25 13:00:45 +0100410 !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
411 &fence_excl->flags)) {
412 if (!dma_fence_get_rcu(fence_excl))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200413 goto unlock_retry;
414
Chris Wilsonf54d1862016-10-25 13:00:45 +0100415 if (dma_fence_is_signaled(fence_excl))
416 dma_fence_put(fence_excl);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200417 else
418 fence = fence_excl;
419 }
420 }
421
422 rcu_read_unlock();
423 if (fence) {
Chris Wilson1cec20f2016-08-29 08:08:31 +0100424 if (read_seqcount_retry(&obj->seq, seq)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100425 dma_fence_put(fence);
Chris Wilson1cec20f2016-08-29 08:08:31 +0100426 goto retry;
427 }
428
Chris Wilsonf54d1862016-10-25 13:00:45 +0100429 ret = dma_fence_wait_timeout(fence, intr, ret);
430 dma_fence_put(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200431 if (ret > 0 && wait_all && (i + 1 < shared_count))
432 goto retry;
433 }
434 return ret;
435
436unlock_retry:
437 rcu_read_unlock();
438 goto retry;
439}
440EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
441
442
443static inline int
Chris Wilsonf54d1862016-10-25 13:00:45 +0100444reservation_object_test_signaled_single(struct dma_fence *passed_fence)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200445{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100446 struct dma_fence *fence, *lfence = passed_fence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200447 int ret = 1;
448
Chris Wilsonf54d1862016-10-25 13:00:45 +0100449 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
450 fence = dma_fence_get_rcu(lfence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200451 if (!fence)
452 return -1;
453
Chris Wilsonf54d1862016-10-25 13:00:45 +0100454 ret = !!dma_fence_is_signaled(fence);
455 dma_fence_put(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200456 }
457 return ret;
458}
459
Rob Clarkdad6c392016-03-31 16:26:51 -0400460/**
461 * reservation_object_test_signaled_rcu - Test if a reservation object's
462 * fences have been signaled.
463 * @obj: the reservation object
464 * @test_all: if true, test all fences, otherwise only test the exclusive
465 * fence
466 *
467 * RETURNS
468 * true if all fences signaled, else false
469 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200470bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
471 bool test_all)
472{
473 unsigned seq, shared_count;
Chris Wilsonb68d8372016-08-29 08:08:32 +0100474 int ret;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200475
Chris Wilsonb68d8372016-08-29 08:08:32 +0100476 rcu_read_lock();
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200477retry:
Chris Wilsonb68d8372016-08-29 08:08:32 +0100478 ret = true;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200479 shared_count = 0;
480 seq = read_seqcount_begin(&obj->seq);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200481
482 if (test_all) {
483 unsigned i;
484
Jagan Teki51366292015-05-21 01:09:31 +0530485 struct reservation_object_list *fobj =
486 rcu_dereference(obj->fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200487
488 if (fobj)
489 shared_count = fobj->shared_count;
490
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200491 for (i = 0; i < shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100492 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200493
494 ret = reservation_object_test_signaled_single(fence);
495 if (ret < 0)
Chris Wilsonb68d8372016-08-29 08:08:32 +0100496 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200497 else if (!ret)
498 break;
499 }
500
Chris Wilsonb68d8372016-08-29 08:08:32 +0100501 if (read_seqcount_retry(&obj->seq, seq))
502 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200503 }
504
505 if (!shared_count) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100506 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200507
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200508 if (fence_excl) {
Jagan Teki51366292015-05-21 01:09:31 +0530509 ret = reservation_object_test_signaled_single(
510 fence_excl);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200511 if (ret < 0)
Chris Wilsonb68d8372016-08-29 08:08:32 +0100512 goto retry;
513
514 if (read_seqcount_retry(&obj->seq, seq))
515 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200516 }
517 }
518
519 rcu_read_unlock();
520 return ret;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200521}
522EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);