blob: b44d9d7db347b4e4dcbad7d96c40f3e4af36a477 [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/**
Christian König7faf9522017-08-10 13:01:48 -0400265* reservation_object_copy_fences - Copy all fences from src to dst.
266* @dst: the destination reservation object
267* @src: the source reservation object
268*
Christian König509536b2017-09-04 21:02:45 +0200269* Copy all fences from src to dst. dst-lock must be held.
Christian König7faf9522017-08-10 13:01:48 -0400270*/
271int reservation_object_copy_fences(struct reservation_object *dst,
272 struct reservation_object *src)
273{
274 struct reservation_object_list *src_list, *dst_list;
275 struct dma_fence *old, *new;
276 size_t size;
277 unsigned i;
278
Christian König509536b2017-09-04 21:02:45 +0200279 rcu_read_lock();
280 src_list = rcu_dereference(src->fence);
Christian König7faf9522017-08-10 13:01:48 -0400281
Christian König509536b2017-09-04 21:02:45 +0200282retry:
Christian König7faf9522017-08-10 13:01:48 -0400283 if (src_list) {
Christian König509536b2017-09-04 21:02:45 +0200284 unsigned shared_count = src_list->shared_count;
285
286 size = offsetof(typeof(*src_list), shared[shared_count]);
287 rcu_read_unlock();
288
Christian König7faf9522017-08-10 13:01:48 -0400289 dst_list = kmalloc(size, GFP_KERNEL);
290 if (!dst_list)
291 return -ENOMEM;
292
Christian König509536b2017-09-04 21:02:45 +0200293 rcu_read_lock();
294 src_list = rcu_dereference(src->fence);
295 if (!src_list || src_list->shared_count > shared_count) {
296 kfree(dst_list);
297 goto retry;
298 }
299
300 dst_list->shared_count = 0;
301 dst_list->shared_max = shared_count;
302 for (i = 0; i < src_list->shared_count; ++i) {
303 struct dma_fence *fence;
304
305 fence = rcu_dereference(src_list->shared[i]);
306 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
307 &fence->flags))
308 continue;
309
310 if (!dma_fence_get_rcu(fence)) {
311 kfree(dst_list);
312 src_list = rcu_dereference(src->fence);
313 goto retry;
314 }
315
316 if (dma_fence_is_signaled(fence)) {
317 dma_fence_put(fence);
318 continue;
319 }
320
321 dst_list->shared[dst_list->shared_count++] = fence;
322 }
Christian König7faf9522017-08-10 13:01:48 -0400323 } else {
324 dst_list = NULL;
325 }
326
Christian König509536b2017-09-04 21:02:45 +0200327 new = dma_fence_get_rcu_safe(&src->fence_excl);
328 rcu_read_unlock();
329
Christian König7faf9522017-08-10 13:01:48 -0400330 kfree(dst->staged);
331 dst->staged = NULL;
332
333 src_list = reservation_object_get_list(dst);
Christian König7faf9522017-08-10 13:01:48 -0400334 old = reservation_object_get_excl(dst);
Christian König7faf9522017-08-10 13:01:48 -0400335
336 preempt_disable();
337 write_seqcount_begin(&dst->seq);
338 /* write_seqcount_begin provides the necessary memory barrier */
339 RCU_INIT_POINTER(dst->fence_excl, new);
340 RCU_INIT_POINTER(dst->fence, dst_list);
341 write_seqcount_end(&dst->seq);
342 preempt_enable();
343
344 if (src_list)
345 kfree_rcu(src_list, rcu);
346 dma_fence_put(old);
347
348 return 0;
349}
350EXPORT_SYMBOL(reservation_object_copy_fences);
351
352/**
Rob Clarkdad6c392016-03-31 16:26:51 -0400353 * reservation_object_get_fences_rcu - Get an object's shared and exclusive
354 * fences without update side lock held
355 * @obj: the reservation object
356 * @pfence_excl: the returned exclusive fence (or NULL)
357 * @pshared_count: the number of shared fences returned
358 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
359 * the required size, and must be freed by caller)
360 *
361 * RETURNS
362 * Zero or -errno
363 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200364int reservation_object_get_fences_rcu(struct reservation_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100365 struct dma_fence **pfence_excl,
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200366 unsigned *pshared_count,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100367 struct dma_fence ***pshared)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200368{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100369 struct dma_fence **shared = NULL;
370 struct dma_fence *fence_excl;
Chris Wilsonfedf5412016-08-29 08:08:30 +0100371 unsigned int shared_count;
372 int ret = 1;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200373
Chris Wilsonfedf5412016-08-29 08:08:30 +0100374 do {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200375 struct reservation_object_list *fobj;
376 unsigned seq;
Chris Wilsonfedf5412016-08-29 08:08:30 +0100377 unsigned int i;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200378
Chris Wilsonfedf5412016-08-29 08:08:30 +0100379 shared_count = i = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200380
381 rcu_read_lock();
Chris Wilsonfedf5412016-08-29 08:08:30 +0100382 seq = read_seqcount_begin(&obj->seq);
383
384 fence_excl = rcu_dereference(obj->fence_excl);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100385 if (fence_excl && !dma_fence_get_rcu(fence_excl))
Chris Wilsonfedf5412016-08-29 08:08:30 +0100386 goto unlock;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200387
388 fobj = rcu_dereference(obj->fence);
389 if (fobj) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100390 struct dma_fence **nshared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200391 size_t sz = sizeof(*shared) * fobj->shared_max;
392
393 nshared = krealloc(shared, sz,
394 GFP_NOWAIT | __GFP_NOWARN);
395 if (!nshared) {
396 rcu_read_unlock();
397 nshared = krealloc(shared, sz, GFP_KERNEL);
398 if (nshared) {
399 shared = nshared;
400 continue;
401 }
402
403 ret = -ENOMEM;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200404 break;
405 }
406 shared = nshared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200407 shared_count = fobj->shared_count;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200408
409 for (i = 0; i < shared_count; ++i) {
Chris Wilsonfedf5412016-08-29 08:08:30 +0100410 shared[i] = rcu_dereference(fobj->shared[i]);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100411 if (!dma_fence_get_rcu(shared[i]))
Chris Wilsonfedf5412016-08-29 08:08:30 +0100412 break;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200413 }
Chris Wilsonfedf5412016-08-29 08:08:30 +0100414 }
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200415
Chris Wilsonfedf5412016-08-29 08:08:30 +0100416 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
417 while (i--)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100418 dma_fence_put(shared[i]);
419 dma_fence_put(fence_excl);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100420 goto unlock;
421 }
422
423 ret = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200424unlock:
425 rcu_read_unlock();
Chris Wilsonfedf5412016-08-29 08:08:30 +0100426 } while (ret);
427
428 if (!shared_count) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200429 kfree(shared);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100430 shared = NULL;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200431 }
Chris Wilsonfedf5412016-08-29 08:08:30 +0100432
433 *pshared_count = shared_count;
434 *pshared = shared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200435 *pfence_excl = fence_excl;
436
437 return ret;
438}
439EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
440
Rob Clarkdad6c392016-03-31 16:26:51 -0400441/**
442 * reservation_object_wait_timeout_rcu - Wait on reservation's objects
443 * shared and/or exclusive fences.
444 * @obj: the reservation object
445 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
446 * @intr: if true, do interruptible wait
447 * @timeout: timeout value in jiffies or zero to return immediately
448 *
449 * RETURNS
450 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
451 * greater than zer on success.
452 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200453long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
454 bool wait_all, bool intr,
455 unsigned long timeout)
456{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100457 struct dma_fence *fence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200458 unsigned seq, shared_count, i = 0;
Christian König06a66b52016-11-07 16:16:16 -0500459 long ret = timeout ? timeout : 1;
Jammy Zhoufb8b7d22015-01-21 18:35:47 +0800460
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200461retry:
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200462 shared_count = 0;
463 seq = read_seqcount_begin(&obj->seq);
464 rcu_read_lock();
465
Christian Königb88fa002017-08-10 13:01:49 -0400466 fence = rcu_dereference(obj->fence_excl);
467 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
468 if (!dma_fence_get_rcu(fence))
469 goto unlock_retry;
470
471 if (dma_fence_is_signaled(fence)) {
472 dma_fence_put(fence);
473 fence = NULL;
474 }
475
476 } else {
477 fence = NULL;
478 }
479
480 if (!fence && wait_all) {
Jagan Teki51366292015-05-21 01:09:31 +0530481 struct reservation_object_list *fobj =
482 rcu_dereference(obj->fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200483
484 if (fobj)
485 shared_count = fobj->shared_count;
486
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200487 for (i = 0; i < shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100488 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200489
Chris Wilsonf54d1862016-10-25 13:00:45 +0100490 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
491 &lfence->flags))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200492 continue;
493
Chris Wilsonf54d1862016-10-25 13:00:45 +0100494 if (!dma_fence_get_rcu(lfence))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200495 goto unlock_retry;
496
Chris Wilsonf54d1862016-10-25 13:00:45 +0100497 if (dma_fence_is_signaled(lfence)) {
498 dma_fence_put(lfence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200499 continue;
500 }
501
502 fence = lfence;
503 break;
504 }
505 }
506
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200507 rcu_read_unlock();
508 if (fence) {
Chris Wilson1cec20f2016-08-29 08:08:31 +0100509 if (read_seqcount_retry(&obj->seq, seq)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100510 dma_fence_put(fence);
Chris Wilson1cec20f2016-08-29 08:08:31 +0100511 goto retry;
512 }
513
Chris Wilsonf54d1862016-10-25 13:00:45 +0100514 ret = dma_fence_wait_timeout(fence, intr, ret);
515 dma_fence_put(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200516 if (ret > 0 && wait_all && (i + 1 < shared_count))
517 goto retry;
518 }
519 return ret;
520
521unlock_retry:
522 rcu_read_unlock();
523 goto retry;
524}
525EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
526
527
528static inline int
Chris Wilsonf54d1862016-10-25 13:00:45 +0100529reservation_object_test_signaled_single(struct dma_fence *passed_fence)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200530{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100531 struct dma_fence *fence, *lfence = passed_fence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200532 int ret = 1;
533
Chris Wilsonf54d1862016-10-25 13:00:45 +0100534 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
535 fence = dma_fence_get_rcu(lfence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200536 if (!fence)
537 return -1;
538
Chris Wilsonf54d1862016-10-25 13:00:45 +0100539 ret = !!dma_fence_is_signaled(fence);
540 dma_fence_put(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200541 }
542 return ret;
543}
544
Rob Clarkdad6c392016-03-31 16:26:51 -0400545/**
546 * reservation_object_test_signaled_rcu - Test if a reservation object's
547 * fences have been signaled.
548 * @obj: the reservation object
549 * @test_all: if true, test all fences, otherwise only test the exclusive
550 * fence
551 *
552 * RETURNS
553 * true if all fences signaled, else false
554 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200555bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
556 bool test_all)
557{
558 unsigned seq, shared_count;
Chris Wilsonb68d8372016-08-29 08:08:32 +0100559 int ret;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200560
Chris Wilsonb68d8372016-08-29 08:08:32 +0100561 rcu_read_lock();
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200562retry:
Chris Wilsonb68d8372016-08-29 08:08:32 +0100563 ret = true;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200564 shared_count = 0;
565 seq = read_seqcount_begin(&obj->seq);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200566
567 if (test_all) {
568 unsigned i;
569
Jagan Teki51366292015-05-21 01:09:31 +0530570 struct reservation_object_list *fobj =
571 rcu_dereference(obj->fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200572
573 if (fobj)
574 shared_count = fobj->shared_count;
575
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200576 for (i = 0; i < shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100577 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200578
579 ret = reservation_object_test_signaled_single(fence);
580 if (ret < 0)
Chris Wilsonb68d8372016-08-29 08:08:32 +0100581 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200582 else if (!ret)
583 break;
584 }
585
Chris Wilsonb68d8372016-08-29 08:08:32 +0100586 if (read_seqcount_retry(&obj->seq, seq))
587 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200588 }
589
590 if (!shared_count) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100591 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200592
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200593 if (fence_excl) {
Jagan Teki51366292015-05-21 01:09:31 +0530594 ret = reservation_object_test_signaled_single(
595 fence_excl);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200596 if (ret < 0)
Chris Wilsonb68d8372016-08-29 08:08:32 +0100597 goto retry;
598
599 if (read_seqcount_retry(&obj->seq, seq))
600 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200601 }
602 }
603
604 rcu_read_unlock();
605 return ret;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200606}
607EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);