blob: 461afa9febd47cd615163af194fb9e37f4bad86c [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{
Christian Königca25fe52017-11-14 15:24:36 +0100107 struct dma_fence *signaled = NULL;
108 u32 i, signaled_idx;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200109
Chris Wilsonf54d1862016-10-25 13:00:45 +0100110 dma_fence_get(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200111
112 preempt_disable();
113 write_seqcount_begin(&obj->seq);
114
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200115 for (i = 0; i < fobj->shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100116 struct dma_fence *old_fence;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200117
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200118 old_fence = rcu_dereference_protected(fobj->shared[i],
119 reservation_object_held(obj));
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200120
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200121 if (old_fence->context == fence->context) {
122 /* memory barrier is added by write_seqcount_begin */
123 RCU_INIT_POINTER(fobj->shared[i], fence);
124 write_seqcount_end(&obj->seq);
125 preempt_enable();
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200126
Chris Wilsonf54d1862016-10-25 13:00:45 +0100127 dma_fence_put(old_fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200128 return;
129 }
Christian Königca25fe52017-11-14 15:24:36 +0100130
131 if (!signaled && dma_fence_is_signaled(old_fence)) {
132 signaled = old_fence;
133 signaled_idx = i;
134 }
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200135 }
136
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200137 /*
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200138 * memory barrier is added by write_seqcount_begin,
139 * fobj->shared_count is protected by this lock too
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200140 */
Christian Königca25fe52017-11-14 15:24:36 +0100141 if (signaled) {
142 RCU_INIT_POINTER(fobj->shared[signaled_idx], fence);
143 } else {
144 RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
145 fobj->shared_count++;
146 }
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200147
148 write_seqcount_end(&obj->seq);
149 preempt_enable();
Christian Königca25fe52017-11-14 15:24:36 +0100150
151 dma_fence_put(signaled);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200152}
153
154static void
155reservation_object_add_shared_replace(struct reservation_object *obj,
156 struct reservation_object_list *old,
157 struct reservation_object_list *fobj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100158 struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200159{
Christian König4d9c62e2017-11-14 15:24:35 +0100160 unsigned i, j, k;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200161
Chris Wilsonf54d1862016-10-25 13:00:45 +0100162 dma_fence_get(fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200163
164 if (!old) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200165 RCU_INIT_POINTER(fobj->shared[0], fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200166 fobj->shared_count = 1;
167 goto done;
168 }
169
170 /*
171 * no need to bump fence refcounts, rcu_read access
172 * requires the use of kref_get_unless_zero, and the
173 * references from the old struct are carried over to
174 * the new.
175 */
Christian König4d9c62e2017-11-14 15:24:35 +0100176 for (i = 0, j = 0, k = fobj->shared_max; i < old->shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100177 struct dma_fence *check;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200178
179 check = rcu_dereference_protected(old->shared[i],
180 reservation_object_held(obj));
181
Christian König4d9c62e2017-11-14 15:24:35 +0100182 if (check->context == fence->context ||
183 dma_fence_is_signaled(check))
184 RCU_INIT_POINTER(fobj->shared[--k], check);
185 else
186 RCU_INIT_POINTER(fobj->shared[j++], check);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200187 }
Christian König4d9c62e2017-11-14 15:24:35 +0100188 fobj->shared_count = j;
189 RCU_INIT_POINTER(fobj->shared[fobj->shared_count], fence);
190 fobj->shared_count++;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200191
192done:
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200193 preempt_disable();
194 write_seqcount_begin(&obj->seq);
195 /*
196 * RCU_INIT_POINTER can be used here,
197 * seqcount provides the necessary barriers
198 */
199 RCU_INIT_POINTER(obj->fence, fobj);
200 write_seqcount_end(&obj->seq);
201 preempt_enable();
202
Christian König4d9c62e2017-11-14 15:24:35 +0100203 if (!old)
204 return;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200205
Christian König4d9c62e2017-11-14 15:24:35 +0100206 /* Drop the references to the signaled fences */
207 for (i = k; i < fobj->shared_max; ++i) {
208 struct dma_fence *f;
209
210 f = rcu_dereference_protected(fobj->shared[i],
211 reservation_object_held(obj));
212 dma_fence_put(f);
213 }
214 kfree_rcu(old, rcu);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200215}
216
Rob Clarkdad6c392016-03-31 16:26:51 -0400217/**
218 * reservation_object_add_shared_fence - Add a fence to a shared slot
219 * @obj: the reservation object
220 * @fence: the shared fence to add
221 *
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200222 * Add a fence to a shared slot, obj->lock must be held, and
Rob Clarkf5bef0b2016-08-19 16:55:34 -0400223 * reservation_object_reserve_shared() has been called.
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200224 */
225void reservation_object_add_shared_fence(struct reservation_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100226 struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200227{
228 struct reservation_object_list *old, *fobj = obj->staged;
229
230 old = reservation_object_get_list(obj);
231 obj->staged = NULL;
232
233 if (!fobj) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200234 BUG_ON(old->shared_count >= old->shared_max);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200235 reservation_object_add_shared_inplace(obj, old, fence);
236 } else
237 reservation_object_add_shared_replace(obj, old, fobj, fence);
238}
239EXPORT_SYMBOL(reservation_object_add_shared_fence);
240
Rob Clarkdad6c392016-03-31 16:26:51 -0400241/**
242 * reservation_object_add_excl_fence - Add an exclusive fence.
243 * @obj: the reservation object
244 * @fence: the shared fence to add
245 *
246 * Add a fence to the exclusive slot. The obj->lock must be held.
247 */
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200248void reservation_object_add_excl_fence(struct reservation_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100249 struct dma_fence *fence)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200250{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100251 struct dma_fence *old_fence = reservation_object_get_excl(obj);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200252 struct reservation_object_list *old;
253 u32 i = 0;
254
255 old = reservation_object_get_list(obj);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200256 if (old)
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200257 i = old->shared_count;
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200258
259 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100260 dma_fence_get(fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200261
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200262 preempt_disable();
263 write_seqcount_begin(&obj->seq);
264 /* write_seqcount_begin provides the necessary memory barrier */
265 RCU_INIT_POINTER(obj->fence_excl, fence);
266 if (old)
267 old->shared_count = 0;
268 write_seqcount_end(&obj->seq);
269 preempt_enable();
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200270
271 /* inplace update, no shared fences */
272 while (i--)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100273 dma_fence_put(rcu_dereference_protected(old->shared[i],
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200274 reservation_object_held(obj)));
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200275
Christian Königf3e31b72017-08-07 17:32:21 -0400276 dma_fence_put(old_fence);
Maarten Lankhorst04a5faa2014-07-01 12:57:54 +0200277}
278EXPORT_SYMBOL(reservation_object_add_excl_fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200279
Rob Clarkdad6c392016-03-31 16:26:51 -0400280/**
Christian König7faf9522017-08-10 13:01:48 -0400281* reservation_object_copy_fences - Copy all fences from src to dst.
282* @dst: the destination reservation object
283* @src: the source reservation object
284*
Christian König39e16ba2017-09-04 21:02:45 +0200285* Copy all fences from src to dst. dst-lock must be held.
Christian König7faf9522017-08-10 13:01:48 -0400286*/
287int reservation_object_copy_fences(struct reservation_object *dst,
288 struct reservation_object *src)
289{
290 struct reservation_object_list *src_list, *dst_list;
291 struct dma_fence *old, *new;
292 size_t size;
293 unsigned i;
294
Christian König39e16ba2017-09-04 21:02:45 +0200295 rcu_read_lock();
296 src_list = rcu_dereference(src->fence);
Christian König7faf9522017-08-10 13:01:48 -0400297
Christian König39e16ba2017-09-04 21:02:45 +0200298retry:
Christian König7faf9522017-08-10 13:01:48 -0400299 if (src_list) {
Christian König39e16ba2017-09-04 21:02:45 +0200300 unsigned shared_count = src_list->shared_count;
301
302 size = offsetof(typeof(*src_list), shared[shared_count]);
303 rcu_read_unlock();
304
Christian König7faf9522017-08-10 13:01:48 -0400305 dst_list = kmalloc(size, GFP_KERNEL);
306 if (!dst_list)
307 return -ENOMEM;
308
Christian König39e16ba2017-09-04 21:02:45 +0200309 rcu_read_lock();
310 src_list = rcu_dereference(src->fence);
311 if (!src_list || src_list->shared_count > shared_count) {
312 kfree(dst_list);
313 goto retry;
314 }
315
316 dst_list->shared_count = 0;
317 dst_list->shared_max = shared_count;
318 for (i = 0; i < src_list->shared_count; ++i) {
319 struct dma_fence *fence;
320
321 fence = rcu_dereference(src_list->shared[i]);
322 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
323 &fence->flags))
324 continue;
325
326 if (!dma_fence_get_rcu(fence)) {
327 kfree(dst_list);
328 src_list = rcu_dereference(src->fence);
329 goto retry;
330 }
331
332 if (dma_fence_is_signaled(fence)) {
333 dma_fence_put(fence);
334 continue;
335 }
336
Ville Syrjäläad46d7b2017-11-02 22:03:36 +0200337 rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
Christian König39e16ba2017-09-04 21:02:45 +0200338 }
Christian König7faf9522017-08-10 13:01:48 -0400339 } else {
340 dst_list = NULL;
341 }
342
Christian König39e16ba2017-09-04 21:02:45 +0200343 new = dma_fence_get_rcu_safe(&src->fence_excl);
344 rcu_read_unlock();
345
Christian König7faf9522017-08-10 13:01:48 -0400346 kfree(dst->staged);
347 dst->staged = NULL;
348
349 src_list = reservation_object_get_list(dst);
Christian König7faf9522017-08-10 13:01:48 -0400350 old = reservation_object_get_excl(dst);
Christian König7faf9522017-08-10 13:01:48 -0400351
352 preempt_disable();
353 write_seqcount_begin(&dst->seq);
354 /* write_seqcount_begin provides the necessary memory barrier */
355 RCU_INIT_POINTER(dst->fence_excl, new);
356 RCU_INIT_POINTER(dst->fence, dst_list);
357 write_seqcount_end(&dst->seq);
358 preempt_enable();
359
360 if (src_list)
361 kfree_rcu(src_list, rcu);
362 dma_fence_put(old);
363
364 return 0;
365}
366EXPORT_SYMBOL(reservation_object_copy_fences);
367
368/**
Rob Clarkdad6c392016-03-31 16:26:51 -0400369 * reservation_object_get_fences_rcu - Get an object's shared and exclusive
370 * fences without update side lock held
371 * @obj: the reservation object
372 * @pfence_excl: the returned exclusive fence (or NULL)
373 * @pshared_count: the number of shared fences returned
374 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
375 * the required size, and must be freed by caller)
376 *
Christian Königa35f2f32018-01-10 13:53:41 +0100377 * Retrieve all fences from the reservation object. If the pointer for the
378 * exclusive fence is not specified the fence is put into the array of the
379 * shared fences as well. Returns either zero or -ENOMEM.
Rob Clarkdad6c392016-03-31 16:26:51 -0400380 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200381int reservation_object_get_fences_rcu(struct reservation_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100382 struct dma_fence **pfence_excl,
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200383 unsigned *pshared_count,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100384 struct dma_fence ***pshared)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200385{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100386 struct dma_fence **shared = NULL;
387 struct dma_fence *fence_excl;
Chris Wilsonfedf5412016-08-29 08:08:30 +0100388 unsigned int shared_count;
389 int ret = 1;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200390
Chris Wilsonfedf5412016-08-29 08:08:30 +0100391 do {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200392 struct reservation_object_list *fobj;
Christian Königa35f2f32018-01-10 13:53:41 +0100393 unsigned int i, seq;
394 size_t sz = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200395
Chris Wilsonfedf5412016-08-29 08:08:30 +0100396 shared_count = i = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200397
398 rcu_read_lock();
Chris Wilsonfedf5412016-08-29 08:08:30 +0100399 seq = read_seqcount_begin(&obj->seq);
400
401 fence_excl = rcu_dereference(obj->fence_excl);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100402 if (fence_excl && !dma_fence_get_rcu(fence_excl))
Chris Wilsonfedf5412016-08-29 08:08:30 +0100403 goto unlock;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200404
405 fobj = rcu_dereference(obj->fence);
Christian Königa35f2f32018-01-10 13:53:41 +0100406 if (fobj)
407 sz += sizeof(*shared) * fobj->shared_max;
408
409 if (!pfence_excl && fence_excl)
410 sz += sizeof(*shared);
411
412 if (sz) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100413 struct dma_fence **nshared;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200414
415 nshared = krealloc(shared, sz,
416 GFP_NOWAIT | __GFP_NOWARN);
417 if (!nshared) {
418 rcu_read_unlock();
419 nshared = krealloc(shared, sz, GFP_KERNEL);
420 if (nshared) {
421 shared = nshared;
422 continue;
423 }
424
425 ret = -ENOMEM;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200426 break;
427 }
428 shared = nshared;
Christian Königa35f2f32018-01-10 13:53:41 +0100429 shared_count = fobj ? fobj->shared_count : 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200430 for (i = 0; i < shared_count; ++i) {
Chris Wilsonfedf5412016-08-29 08:08:30 +0100431 shared[i] = rcu_dereference(fobj->shared[i]);
Chris Wilsonf54d1862016-10-25 13:00:45 +0100432 if (!dma_fence_get_rcu(shared[i]))
Chris Wilsonfedf5412016-08-29 08:08:30 +0100433 break;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200434 }
Christian Königa35f2f32018-01-10 13:53:41 +0100435
436 if (!pfence_excl && fence_excl) {
437 shared[i] = fence_excl;
438 fence_excl = NULL;
439 ++i;
440 ++shared_count;
441 }
Chris Wilsonfedf5412016-08-29 08:08:30 +0100442 }
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200443
Chris Wilsonfedf5412016-08-29 08:08:30 +0100444 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
445 while (i--)
Chris Wilsonf54d1862016-10-25 13:00:45 +0100446 dma_fence_put(shared[i]);
447 dma_fence_put(fence_excl);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100448 goto unlock;
449 }
450
451 ret = 0;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200452unlock:
453 rcu_read_unlock();
Chris Wilsonfedf5412016-08-29 08:08:30 +0100454 } while (ret);
455
456 if (!shared_count) {
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200457 kfree(shared);
Chris Wilsonfedf5412016-08-29 08:08:30 +0100458 shared = NULL;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200459 }
Chris Wilsonfedf5412016-08-29 08:08:30 +0100460
461 *pshared_count = shared_count;
462 *pshared = shared;
Christian Königa35f2f32018-01-10 13:53:41 +0100463 if (pfence_excl)
464 *pfence_excl = fence_excl;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200465
466 return ret;
467}
468EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
469
Rob Clarkdad6c392016-03-31 16:26:51 -0400470/**
471 * reservation_object_wait_timeout_rcu - Wait on reservation's objects
472 * shared and/or exclusive fences.
473 * @obj: the reservation object
474 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
475 * @intr: if true, do interruptible wait
476 * @timeout: timeout value in jiffies or zero to return immediately
477 *
478 * RETURNS
479 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
480 * greater than zer on success.
481 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200482long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
483 bool wait_all, bool intr,
484 unsigned long timeout)
485{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100486 struct dma_fence *fence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200487 unsigned seq, shared_count, i = 0;
Christian König06a66b52016-11-07 16:16:16 -0500488 long ret = timeout ? timeout : 1;
Jammy Zhoufb8b7d22015-01-21 18:35:47 +0800489
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200490retry:
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200491 shared_count = 0;
492 seq = read_seqcount_begin(&obj->seq);
493 rcu_read_lock();
494
Christian Königb88fa002017-08-10 13:01:49 -0400495 fence = rcu_dereference(obj->fence_excl);
496 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
497 if (!dma_fence_get_rcu(fence))
498 goto unlock_retry;
499
500 if (dma_fence_is_signaled(fence)) {
501 dma_fence_put(fence);
502 fence = NULL;
503 }
504
505 } else {
506 fence = NULL;
507 }
508
509 if (!fence && wait_all) {
Jagan Teki51366292015-05-21 01:09:31 +0530510 struct reservation_object_list *fobj =
511 rcu_dereference(obj->fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200512
513 if (fobj)
514 shared_count = fobj->shared_count;
515
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200516 for (i = 0; i < shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100517 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200518
Chris Wilsonf54d1862016-10-25 13:00:45 +0100519 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
520 &lfence->flags))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200521 continue;
522
Chris Wilsonf54d1862016-10-25 13:00:45 +0100523 if (!dma_fence_get_rcu(lfence))
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200524 goto unlock_retry;
525
Chris Wilsonf54d1862016-10-25 13:00:45 +0100526 if (dma_fence_is_signaled(lfence)) {
527 dma_fence_put(lfence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200528 continue;
529 }
530
531 fence = lfence;
532 break;
533 }
534 }
535
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200536 rcu_read_unlock();
537 if (fence) {
Chris Wilson1cec20f2016-08-29 08:08:31 +0100538 if (read_seqcount_retry(&obj->seq, seq)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100539 dma_fence_put(fence);
Chris Wilson1cec20f2016-08-29 08:08:31 +0100540 goto retry;
541 }
542
Chris Wilsonf54d1862016-10-25 13:00:45 +0100543 ret = dma_fence_wait_timeout(fence, intr, ret);
544 dma_fence_put(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200545 if (ret > 0 && wait_all && (i + 1 < shared_count))
546 goto retry;
547 }
548 return ret;
549
550unlock_retry:
551 rcu_read_unlock();
552 goto retry;
553}
554EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
555
556
557static inline int
Chris Wilsonf54d1862016-10-25 13:00:45 +0100558reservation_object_test_signaled_single(struct dma_fence *passed_fence)
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200559{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100560 struct dma_fence *fence, *lfence = passed_fence;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200561 int ret = 1;
562
Chris Wilsonf54d1862016-10-25 13:00:45 +0100563 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
564 fence = dma_fence_get_rcu(lfence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200565 if (!fence)
566 return -1;
567
Chris Wilsonf54d1862016-10-25 13:00:45 +0100568 ret = !!dma_fence_is_signaled(fence);
569 dma_fence_put(fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200570 }
571 return ret;
572}
573
Rob Clarkdad6c392016-03-31 16:26:51 -0400574/**
575 * reservation_object_test_signaled_rcu - Test if a reservation object's
576 * fences have been signaled.
577 * @obj: the reservation object
578 * @test_all: if true, test all fences, otherwise only test the exclusive
579 * fence
580 *
581 * RETURNS
582 * true if all fences signaled, else false
583 */
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200584bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
585 bool test_all)
586{
587 unsigned seq, shared_count;
Chris Wilsonb68d8372016-08-29 08:08:32 +0100588 int ret;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200589
Chris Wilsonb68d8372016-08-29 08:08:32 +0100590 rcu_read_lock();
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200591retry:
Chris Wilsonb68d8372016-08-29 08:08:32 +0100592 ret = true;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200593 shared_count = 0;
594 seq = read_seqcount_begin(&obj->seq);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200595
596 if (test_all) {
597 unsigned i;
598
Jagan Teki51366292015-05-21 01:09:31 +0530599 struct reservation_object_list *fobj =
600 rcu_dereference(obj->fence);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200601
602 if (fobj)
603 shared_count = fobj->shared_count;
604
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200605 for (i = 0; i < shared_count; ++i) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100606 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200607
608 ret = reservation_object_test_signaled_single(fence);
609 if (ret < 0)
Chris Wilsonb68d8372016-08-29 08:08:32 +0100610 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200611 else if (!ret)
612 break;
613 }
614
Chris Wilsonb68d8372016-08-29 08:08:32 +0100615 if (read_seqcount_retry(&obj->seq, seq))
616 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200617 }
618
619 if (!shared_count) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100620 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200621
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200622 if (fence_excl) {
Jagan Teki51366292015-05-21 01:09:31 +0530623 ret = reservation_object_test_signaled_single(
624 fence_excl);
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200625 if (ret < 0)
Chris Wilsonb68d8372016-08-29 08:08:32 +0100626 goto retry;
627
628 if (read_seqcount_retry(&obj->seq, seq))
629 goto retry;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200630 }
631 }
632
633 rcu_read_unlock();
634 return ret;
Maarten Lankhorst3c3b1772014-07-01 12:58:00 +0200635}
636EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);