drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / dma-buf / reservation.c
blob4447e13d1e891881f8ff6046ae442333b2293e30
1 /*
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
8 * All Rights Reserved.
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
20 * of the Software.
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>
38 /**
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
46 * write-side updates.
49 DEFINE_WD_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);
58 /**
59 * reservation_object_reserve_shared - Reserve space to add shared fences to
60 * a reservation_object.
61 * @obj: reservation object
62 * @num_fences: number of fences we want to add
64 * Should be called before reservation_object_add_shared_fence(). Must
65 * be called with obj->lock held.
67 * RETURNS
68 * Zero for success, or -errno
70 int reservation_object_reserve_shared(struct reservation_object *obj,
71 unsigned int num_fences)
73 struct reservation_object_list *old, *new;
74 unsigned int i, j, k, max;
76 reservation_object_assert_held(obj);
78 old = reservation_object_get_list(obj);
80 if (old && old->shared_max) {
81 if ((old->shared_count + num_fences) <= old->shared_max)
82 return 0;
83 else
84 max = max(old->shared_count + num_fences,
85 old->shared_max * 2);
86 } else {
87 max = 4;
90 new = kmalloc(offsetof(typeof(*new), shared[max]), GFP_KERNEL);
91 if (!new)
92 return -ENOMEM;
95 * no need to bump fence refcounts, rcu_read access
96 * requires the use of kref_get_unless_zero, and the
97 * references from the old struct are carried over to
98 * the new.
100 for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
101 struct dma_fence *fence;
103 fence = rcu_dereference_protected(old->shared[i],
104 reservation_object_held(obj));
105 if (dma_fence_is_signaled(fence))
106 RCU_INIT_POINTER(new->shared[--k], fence);
107 else
108 RCU_INIT_POINTER(new->shared[j++], fence);
110 new->shared_count = j;
111 new->shared_max = max;
113 preempt_disable();
114 write_seqcount_begin(&obj->seq);
116 * RCU_INIT_POINTER can be used here,
117 * seqcount provides the necessary barriers
119 RCU_INIT_POINTER(obj->fence, new);
120 write_seqcount_end(&obj->seq);
121 preempt_enable();
123 if (!old)
124 return 0;
126 /* Drop the references to the signaled fences */
127 for (i = k; i < new->shared_max; ++i) {
128 struct dma_fence *fence;
130 fence = rcu_dereference_protected(new->shared[i],
131 reservation_object_held(obj));
132 dma_fence_put(fence);
134 kfree_rcu(old, rcu);
136 return 0;
138 EXPORT_SYMBOL(reservation_object_reserve_shared);
141 * reservation_object_add_shared_fence - Add a fence to a shared slot
142 * @obj: the reservation object
143 * @fence: the shared fence to add
145 * Add a fence to a shared slot, obj->lock must be held, and
146 * reservation_object_reserve_shared() has been called.
148 void reservation_object_add_shared_fence(struct reservation_object *obj,
149 struct dma_fence *fence)
151 struct reservation_object_list *fobj;
152 unsigned int i, count;
154 dma_fence_get(fence);
156 reservation_object_assert_held(obj);
158 fobj = reservation_object_get_list(obj);
159 count = fobj->shared_count;
161 preempt_disable();
162 write_seqcount_begin(&obj->seq);
164 for (i = 0; i < count; ++i) {
165 struct dma_fence *old_fence;
167 old_fence = rcu_dereference_protected(fobj->shared[i],
168 reservation_object_held(obj));
169 if (old_fence->context == fence->context ||
170 dma_fence_is_signaled(old_fence)) {
171 dma_fence_put(old_fence);
172 goto replace;
176 BUG_ON(fobj->shared_count >= fobj->shared_max);
177 count++;
179 replace:
180 RCU_INIT_POINTER(fobj->shared[i], fence);
181 /* pointer update must be visible before we extend the shared_count */
182 smp_store_mb(fobj->shared_count, count);
184 write_seqcount_end(&obj->seq);
185 preempt_enable();
187 EXPORT_SYMBOL(reservation_object_add_shared_fence);
190 * reservation_object_add_excl_fence - Add an exclusive fence.
191 * @obj: the reservation object
192 * @fence: the shared fence to add
194 * Add a fence to the exclusive slot. The obj->lock must be held.
196 void reservation_object_add_excl_fence(struct reservation_object *obj,
197 struct dma_fence *fence)
199 struct dma_fence *old_fence = reservation_object_get_excl(obj);
200 struct reservation_object_list *old;
201 u32 i = 0;
203 reservation_object_assert_held(obj);
205 old = reservation_object_get_list(obj);
206 if (old)
207 i = old->shared_count;
209 if (fence)
210 dma_fence_get(fence);
212 preempt_disable();
213 write_seqcount_begin(&obj->seq);
214 /* write_seqcount_begin provides the necessary memory barrier */
215 RCU_INIT_POINTER(obj->fence_excl, fence);
216 if (old)
217 old->shared_count = 0;
218 write_seqcount_end(&obj->seq);
219 preempt_enable();
221 /* inplace update, no shared fences */
222 while (i--)
223 dma_fence_put(rcu_dereference_protected(old->shared[i],
224 reservation_object_held(obj)));
226 dma_fence_put(old_fence);
228 EXPORT_SYMBOL(reservation_object_add_excl_fence);
231 * reservation_object_copy_fences - Copy all fences from src to dst.
232 * @dst: the destination reservation object
233 * @src: the source reservation object
235 * Copy all fences from src to dst. dst-lock must be held.
237 int reservation_object_copy_fences(struct reservation_object *dst,
238 struct reservation_object *src)
240 struct reservation_object_list *src_list, *dst_list;
241 struct dma_fence *old, *new;
242 size_t size;
243 unsigned i;
245 reservation_object_assert_held(dst);
247 rcu_read_lock();
248 src_list = rcu_dereference(src->fence);
250 retry:
251 if (src_list) {
252 unsigned shared_count = src_list->shared_count;
254 size = offsetof(typeof(*src_list), shared[shared_count]);
255 rcu_read_unlock();
257 dst_list = kmalloc(size, GFP_KERNEL);
258 if (!dst_list)
259 return -ENOMEM;
261 rcu_read_lock();
262 src_list = rcu_dereference(src->fence);
263 if (!src_list || src_list->shared_count > shared_count) {
264 kfree(dst_list);
265 goto retry;
268 dst_list->shared_count = 0;
269 dst_list->shared_max = shared_count;
270 for (i = 0; i < src_list->shared_count; ++i) {
271 struct dma_fence *fence;
273 fence = rcu_dereference(src_list->shared[i]);
274 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
275 &fence->flags))
276 continue;
278 if (!dma_fence_get_rcu(fence)) {
279 kfree(dst_list);
280 src_list = rcu_dereference(src->fence);
281 goto retry;
284 if (dma_fence_is_signaled(fence)) {
285 dma_fence_put(fence);
286 continue;
289 rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
291 } else {
292 dst_list = NULL;
295 new = dma_fence_get_rcu_safe(&src->fence_excl);
296 rcu_read_unlock();
298 src_list = reservation_object_get_list(dst);
299 old = reservation_object_get_excl(dst);
301 preempt_disable();
302 write_seqcount_begin(&dst->seq);
303 /* write_seqcount_begin provides the necessary memory barrier */
304 RCU_INIT_POINTER(dst->fence_excl, new);
305 RCU_INIT_POINTER(dst->fence, dst_list);
306 write_seqcount_end(&dst->seq);
307 preempt_enable();
309 if (src_list)
310 kfree_rcu(src_list, rcu);
311 dma_fence_put(old);
313 return 0;
315 EXPORT_SYMBOL(reservation_object_copy_fences);
318 * reservation_object_get_fences_rcu - Get an object's shared and exclusive
319 * fences without update side lock held
320 * @obj: the reservation object
321 * @pfence_excl: the returned exclusive fence (or NULL)
322 * @pshared_count: the number of shared fences returned
323 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
324 * the required size, and must be freed by caller)
326 * Retrieve all fences from the reservation object. If the pointer for the
327 * exclusive fence is not specified the fence is put into the array of the
328 * shared fences as well. Returns either zero or -ENOMEM.
330 int reservation_object_get_fences_rcu(struct reservation_object *obj,
331 struct dma_fence **pfence_excl,
332 unsigned *pshared_count,
333 struct dma_fence ***pshared)
335 struct dma_fence **shared = NULL;
336 struct dma_fence *fence_excl;
337 unsigned int shared_count;
338 int ret = 1;
340 do {
341 struct reservation_object_list *fobj;
342 unsigned int i, seq;
343 size_t sz = 0;
345 shared_count = i = 0;
347 rcu_read_lock();
348 seq = read_seqcount_begin(&obj->seq);
350 fence_excl = rcu_dereference(obj->fence_excl);
351 if (fence_excl && !dma_fence_get_rcu(fence_excl))
352 goto unlock;
354 fobj = rcu_dereference(obj->fence);
355 if (fobj)
356 sz += sizeof(*shared) * fobj->shared_max;
358 if (!pfence_excl && fence_excl)
359 sz += sizeof(*shared);
361 if (sz) {
362 struct dma_fence **nshared;
364 nshared = krealloc(shared, sz,
365 GFP_NOWAIT | __GFP_NOWARN);
366 if (!nshared) {
367 rcu_read_unlock();
369 dma_fence_put(fence_excl);
370 fence_excl = NULL;
372 nshared = krealloc(shared, sz, GFP_KERNEL);
373 if (nshared) {
374 shared = nshared;
375 continue;
378 ret = -ENOMEM;
379 break;
381 shared = nshared;
382 shared_count = fobj ? fobj->shared_count : 0;
383 for (i = 0; i < shared_count; ++i) {
384 shared[i] = rcu_dereference(fobj->shared[i]);
385 if (!dma_fence_get_rcu(shared[i]))
386 break;
389 if (!pfence_excl && fence_excl) {
390 shared[i] = fence_excl;
391 fence_excl = NULL;
392 ++i;
393 ++shared_count;
397 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
398 while (i--)
399 dma_fence_put(shared[i]);
400 dma_fence_put(fence_excl);
401 goto unlock;
404 ret = 0;
405 unlock:
406 rcu_read_unlock();
407 } while (ret);
409 if (!shared_count) {
410 kfree(shared);
411 shared = NULL;
414 *pshared_count = shared_count;
415 *pshared = shared;
416 if (pfence_excl)
417 *pfence_excl = fence_excl;
419 return ret;
421 EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
424 * reservation_object_wait_timeout_rcu - Wait on reservation's objects
425 * shared and/or exclusive fences.
426 * @obj: the reservation object
427 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
428 * @intr: if true, do interruptible wait
429 * @timeout: timeout value in jiffies or zero to return immediately
431 * RETURNS
432 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
433 * greater than zer on success.
435 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
436 bool wait_all, bool intr,
437 unsigned long timeout)
439 struct dma_fence *fence;
440 unsigned seq, shared_count;
441 long ret = timeout ? timeout : 1;
442 int i;
444 retry:
445 shared_count = 0;
446 seq = read_seqcount_begin(&obj->seq);
447 rcu_read_lock();
448 i = -1;
450 fence = rcu_dereference(obj->fence_excl);
451 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
452 if (!dma_fence_get_rcu(fence))
453 goto unlock_retry;
455 if (dma_fence_is_signaled(fence)) {
456 dma_fence_put(fence);
457 fence = NULL;
460 } else {
461 fence = NULL;
464 if (wait_all) {
465 struct reservation_object_list *fobj =
466 rcu_dereference(obj->fence);
468 if (fobj)
469 shared_count = fobj->shared_count;
471 for (i = 0; !fence && i < shared_count; ++i) {
472 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
474 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
475 &lfence->flags))
476 continue;
478 if (!dma_fence_get_rcu(lfence))
479 goto unlock_retry;
481 if (dma_fence_is_signaled(lfence)) {
482 dma_fence_put(lfence);
483 continue;
486 fence = lfence;
487 break;
491 rcu_read_unlock();
492 if (fence) {
493 if (read_seqcount_retry(&obj->seq, seq)) {
494 dma_fence_put(fence);
495 goto retry;
498 ret = dma_fence_wait_timeout(fence, intr, ret);
499 dma_fence_put(fence);
500 if (ret > 0 && wait_all && (i + 1 < shared_count))
501 goto retry;
503 return ret;
505 unlock_retry:
506 rcu_read_unlock();
507 goto retry;
509 EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
512 static inline int
513 reservation_object_test_signaled_single(struct dma_fence *passed_fence)
515 struct dma_fence *fence, *lfence = passed_fence;
516 int ret = 1;
518 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
519 fence = dma_fence_get_rcu(lfence);
520 if (!fence)
521 return -1;
523 ret = !!dma_fence_is_signaled(fence);
524 dma_fence_put(fence);
526 return ret;
530 * reservation_object_test_signaled_rcu - Test if a reservation object's
531 * fences have been signaled.
532 * @obj: the reservation object
533 * @test_all: if true, test all fences, otherwise only test the exclusive
534 * fence
536 * RETURNS
537 * true if all fences signaled, else false
539 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
540 bool test_all)
542 unsigned seq, shared_count;
543 int ret;
545 rcu_read_lock();
546 retry:
547 ret = true;
548 shared_count = 0;
549 seq = read_seqcount_begin(&obj->seq);
551 if (test_all) {
552 unsigned i;
554 struct reservation_object_list *fobj =
555 rcu_dereference(obj->fence);
557 if (fobj)
558 shared_count = fobj->shared_count;
560 for (i = 0; i < shared_count; ++i) {
561 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
563 ret = reservation_object_test_signaled_single(fence);
564 if (ret < 0)
565 goto retry;
566 else if (!ret)
567 break;
570 if (read_seqcount_retry(&obj->seq, seq))
571 goto retry;
574 if (!shared_count) {
575 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
577 if (fence_excl) {
578 ret = reservation_object_test_signaled_single(
579 fence_excl);
580 if (ret < 0)
581 goto retry;
583 if (read_seqcount_retry(&obj->seq, seq))
584 goto retry;
588 rcu_read_unlock();
589 return ret;
591 EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);