dt-bindings: mtd: ingenic: Use standard ecc-engine property
[linux/fpc-iii.git] / drivers / dma-buf / reservation.c
blobc1618335ca9944e11c537eb7bd2c13ed3dd93b2b
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 old = reservation_object_get_list(obj);
78 if (old && old->shared_max) {
79 if ((old->shared_count + num_fences) <= old->shared_max)
80 return 0;
81 else
82 max = max(old->shared_count + num_fences,
83 old->shared_max * 2);
84 } else {
85 max = 4;
88 new = kmalloc(offsetof(typeof(*new), shared[max]), GFP_KERNEL);
89 if (!new)
90 return -ENOMEM;
93 * no need to bump fence refcounts, rcu_read access
94 * requires the use of kref_get_unless_zero, and the
95 * references from the old struct are carried over to
96 * the new.
98 for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
99 struct dma_fence *fence;
101 fence = rcu_dereference_protected(old->shared[i],
102 reservation_object_held(obj));
103 if (dma_fence_is_signaled(fence))
104 RCU_INIT_POINTER(new->shared[--k], fence);
105 else
106 RCU_INIT_POINTER(new->shared[j++], fence);
108 new->shared_count = j;
109 new->shared_max = max;
111 preempt_disable();
112 write_seqcount_begin(&obj->seq);
114 * RCU_INIT_POINTER can be used here,
115 * seqcount provides the necessary barriers
117 RCU_INIT_POINTER(obj->fence, new);
118 write_seqcount_end(&obj->seq);
119 preempt_enable();
121 if (!old)
122 return 0;
124 /* Drop the references to the signaled fences */
125 for (i = k; i < new->shared_max; ++i) {
126 struct dma_fence *fence;
128 fence = rcu_dereference_protected(new->shared[i],
129 reservation_object_held(obj));
130 dma_fence_put(fence);
132 kfree_rcu(old, rcu);
134 return 0;
136 EXPORT_SYMBOL(reservation_object_reserve_shared);
139 * reservation_object_add_shared_fence - Add a fence to a shared slot
140 * @obj: the reservation object
141 * @fence: the shared fence to add
143 * Add a fence to a shared slot, obj->lock must be held, and
144 * reservation_object_reserve_shared() has been called.
146 void reservation_object_add_shared_fence(struct reservation_object *obj,
147 struct dma_fence *fence)
149 struct reservation_object_list *fobj;
150 unsigned int i, count;
152 dma_fence_get(fence);
154 fobj = reservation_object_get_list(obj);
155 count = fobj->shared_count;
157 preempt_disable();
158 write_seqcount_begin(&obj->seq);
160 for (i = 0; i < count; ++i) {
161 struct dma_fence *old_fence;
163 old_fence = rcu_dereference_protected(fobj->shared[i],
164 reservation_object_held(obj));
165 if (old_fence->context == fence->context ||
166 dma_fence_is_signaled(old_fence)) {
167 dma_fence_put(old_fence);
168 goto replace;
172 BUG_ON(fobj->shared_count >= fobj->shared_max);
173 count++;
175 replace:
176 RCU_INIT_POINTER(fobj->shared[i], fence);
177 /* pointer update must be visible before we extend the shared_count */
178 smp_store_mb(fobj->shared_count, count);
180 write_seqcount_end(&obj->seq);
181 preempt_enable();
183 EXPORT_SYMBOL(reservation_object_add_shared_fence);
186 * reservation_object_add_excl_fence - Add an exclusive fence.
187 * @obj: the reservation object
188 * @fence: the shared fence to add
190 * Add a fence to the exclusive slot. The obj->lock must be held.
192 void reservation_object_add_excl_fence(struct reservation_object *obj,
193 struct dma_fence *fence)
195 struct dma_fence *old_fence = reservation_object_get_excl(obj);
196 struct reservation_object_list *old;
197 u32 i = 0;
199 old = reservation_object_get_list(obj);
200 if (old)
201 i = old->shared_count;
203 if (fence)
204 dma_fence_get(fence);
206 preempt_disable();
207 write_seqcount_begin(&obj->seq);
208 /* write_seqcount_begin provides the necessary memory barrier */
209 RCU_INIT_POINTER(obj->fence_excl, fence);
210 if (old)
211 old->shared_count = 0;
212 write_seqcount_end(&obj->seq);
213 preempt_enable();
215 /* inplace update, no shared fences */
216 while (i--)
217 dma_fence_put(rcu_dereference_protected(old->shared[i],
218 reservation_object_held(obj)));
220 dma_fence_put(old_fence);
222 EXPORT_SYMBOL(reservation_object_add_excl_fence);
225 * reservation_object_copy_fences - Copy all fences from src to dst.
226 * @dst: the destination reservation object
227 * @src: the source reservation object
229 * Copy all fences from src to dst. dst-lock must be held.
231 int reservation_object_copy_fences(struct reservation_object *dst,
232 struct reservation_object *src)
234 struct reservation_object_list *src_list, *dst_list;
235 struct dma_fence *old, *new;
236 size_t size;
237 unsigned i;
239 rcu_read_lock();
240 src_list = rcu_dereference(src->fence);
242 retry:
243 if (src_list) {
244 unsigned shared_count = src_list->shared_count;
246 size = offsetof(typeof(*src_list), shared[shared_count]);
247 rcu_read_unlock();
249 dst_list = kmalloc(size, GFP_KERNEL);
250 if (!dst_list)
251 return -ENOMEM;
253 rcu_read_lock();
254 src_list = rcu_dereference(src->fence);
255 if (!src_list || src_list->shared_count > shared_count) {
256 kfree(dst_list);
257 goto retry;
260 dst_list->shared_count = 0;
261 dst_list->shared_max = shared_count;
262 for (i = 0; i < src_list->shared_count; ++i) {
263 struct dma_fence *fence;
265 fence = rcu_dereference(src_list->shared[i]);
266 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
267 &fence->flags))
268 continue;
270 if (!dma_fence_get_rcu(fence)) {
271 kfree(dst_list);
272 src_list = rcu_dereference(src->fence);
273 goto retry;
276 if (dma_fence_is_signaled(fence)) {
277 dma_fence_put(fence);
278 continue;
281 rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
283 } else {
284 dst_list = NULL;
287 new = dma_fence_get_rcu_safe(&src->fence_excl);
288 rcu_read_unlock();
290 src_list = reservation_object_get_list(dst);
291 old = reservation_object_get_excl(dst);
293 preempt_disable();
294 write_seqcount_begin(&dst->seq);
295 /* write_seqcount_begin provides the necessary memory barrier */
296 RCU_INIT_POINTER(dst->fence_excl, new);
297 RCU_INIT_POINTER(dst->fence, dst_list);
298 write_seqcount_end(&dst->seq);
299 preempt_enable();
301 if (src_list)
302 kfree_rcu(src_list, rcu);
303 dma_fence_put(old);
305 return 0;
307 EXPORT_SYMBOL(reservation_object_copy_fences);
310 * reservation_object_get_fences_rcu - Get an object's shared and exclusive
311 * fences without update side lock held
312 * @obj: the reservation object
313 * @pfence_excl: the returned exclusive fence (or NULL)
314 * @pshared_count: the number of shared fences returned
315 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
316 * the required size, and must be freed by caller)
318 * Retrieve all fences from the reservation object. If the pointer for the
319 * exclusive fence is not specified the fence is put into the array of the
320 * shared fences as well. Returns either zero or -ENOMEM.
322 int reservation_object_get_fences_rcu(struct reservation_object *obj,
323 struct dma_fence **pfence_excl,
324 unsigned *pshared_count,
325 struct dma_fence ***pshared)
327 struct dma_fence **shared = NULL;
328 struct dma_fence *fence_excl;
329 unsigned int shared_count;
330 int ret = 1;
332 do {
333 struct reservation_object_list *fobj;
334 unsigned int i, seq;
335 size_t sz = 0;
337 shared_count = i = 0;
339 rcu_read_lock();
340 seq = read_seqcount_begin(&obj->seq);
342 fence_excl = rcu_dereference(obj->fence_excl);
343 if (fence_excl && !dma_fence_get_rcu(fence_excl))
344 goto unlock;
346 fobj = rcu_dereference(obj->fence);
347 if (fobj)
348 sz += sizeof(*shared) * fobj->shared_max;
350 if (!pfence_excl && fence_excl)
351 sz += sizeof(*shared);
353 if (sz) {
354 struct dma_fence **nshared;
356 nshared = krealloc(shared, sz,
357 GFP_NOWAIT | __GFP_NOWARN);
358 if (!nshared) {
359 rcu_read_unlock();
360 nshared = krealloc(shared, sz, GFP_KERNEL);
361 if (nshared) {
362 shared = nshared;
363 continue;
366 ret = -ENOMEM;
367 break;
369 shared = nshared;
370 shared_count = fobj ? fobj->shared_count : 0;
371 for (i = 0; i < shared_count; ++i) {
372 shared[i] = rcu_dereference(fobj->shared[i]);
373 if (!dma_fence_get_rcu(shared[i]))
374 break;
377 if (!pfence_excl && fence_excl) {
378 shared[i] = fence_excl;
379 fence_excl = NULL;
380 ++i;
381 ++shared_count;
385 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
386 while (i--)
387 dma_fence_put(shared[i]);
388 dma_fence_put(fence_excl);
389 goto unlock;
392 ret = 0;
393 unlock:
394 rcu_read_unlock();
395 } while (ret);
397 if (!shared_count) {
398 kfree(shared);
399 shared = NULL;
402 *pshared_count = shared_count;
403 *pshared = shared;
404 if (pfence_excl)
405 *pfence_excl = fence_excl;
407 return ret;
409 EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
412 * reservation_object_wait_timeout_rcu - Wait on reservation's objects
413 * shared and/or exclusive fences.
414 * @obj: the reservation object
415 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
416 * @intr: if true, do interruptible wait
417 * @timeout: timeout value in jiffies or zero to return immediately
419 * RETURNS
420 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
421 * greater than zer on success.
423 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
424 bool wait_all, bool intr,
425 unsigned long timeout)
427 struct dma_fence *fence;
428 unsigned seq, shared_count;
429 long ret = timeout ? timeout : 1;
430 int i;
432 retry:
433 shared_count = 0;
434 seq = read_seqcount_begin(&obj->seq);
435 rcu_read_lock();
436 i = -1;
438 fence = rcu_dereference(obj->fence_excl);
439 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
440 if (!dma_fence_get_rcu(fence))
441 goto unlock_retry;
443 if (dma_fence_is_signaled(fence)) {
444 dma_fence_put(fence);
445 fence = NULL;
448 } else {
449 fence = NULL;
452 if (wait_all) {
453 struct reservation_object_list *fobj =
454 rcu_dereference(obj->fence);
456 if (fobj)
457 shared_count = fobj->shared_count;
459 for (i = 0; !fence && i < shared_count; ++i) {
460 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
462 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
463 &lfence->flags))
464 continue;
466 if (!dma_fence_get_rcu(lfence))
467 goto unlock_retry;
469 if (dma_fence_is_signaled(lfence)) {
470 dma_fence_put(lfence);
471 continue;
474 fence = lfence;
475 break;
479 rcu_read_unlock();
480 if (fence) {
481 if (read_seqcount_retry(&obj->seq, seq)) {
482 dma_fence_put(fence);
483 goto retry;
486 ret = dma_fence_wait_timeout(fence, intr, ret);
487 dma_fence_put(fence);
488 if (ret > 0 && wait_all && (i + 1 < shared_count))
489 goto retry;
491 return ret;
493 unlock_retry:
494 rcu_read_unlock();
495 goto retry;
497 EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
500 static inline int
501 reservation_object_test_signaled_single(struct dma_fence *passed_fence)
503 struct dma_fence *fence, *lfence = passed_fence;
504 int ret = 1;
506 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
507 fence = dma_fence_get_rcu(lfence);
508 if (!fence)
509 return -1;
511 ret = !!dma_fence_is_signaled(fence);
512 dma_fence_put(fence);
514 return ret;
518 * reservation_object_test_signaled_rcu - Test if a reservation object's
519 * fences have been signaled.
520 * @obj: the reservation object
521 * @test_all: if true, test all fences, otherwise only test the exclusive
522 * fence
524 * RETURNS
525 * true if all fences signaled, else false
527 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
528 bool test_all)
530 unsigned seq, shared_count;
531 int ret;
533 rcu_read_lock();
534 retry:
535 ret = true;
536 shared_count = 0;
537 seq = read_seqcount_begin(&obj->seq);
539 if (test_all) {
540 unsigned i;
542 struct reservation_object_list *fobj =
543 rcu_dereference(obj->fence);
545 if (fobj)
546 shared_count = fobj->shared_count;
548 for (i = 0; i < shared_count; ++i) {
549 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
551 ret = reservation_object_test_signaled_single(fence);
552 if (ret < 0)
553 goto retry;
554 else if (!ret)
555 break;
558 if (read_seqcount_retry(&obj->seq, seq))
559 goto retry;
562 if (!shared_count) {
563 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
565 if (fence_excl) {
566 ret = reservation_object_test_signaled_single(
567 fence_excl);
568 if (ret < 0)
569 goto retry;
571 if (read_seqcount_retry(&obj->seq, seq))
572 goto retry;
576 rcu_read_unlock();
577 return ret;
579 EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);