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
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
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/dma-resv.h>
36 #include <linux/export.h>
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
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);
59 * dma_resv_list_alloc - allocate fence list
60 * @shared_max: number of fences we need space for
62 * Allocate a new dma_resv_list and make sure to correctly initialize
65 static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max)
67 struct dma_resv_list *list;
69 list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL);
73 list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) /
74 sizeof(*list->shared);
80 * dma_resv_list_free - free fence list
83 * Free a dma_resv_list and make sure to drop all references.
85 static void dma_resv_list_free(struct dma_resv_list *list)
92 for (i = 0; i < list->shared_count; ++i)
93 dma_fence_put(rcu_dereference_protected(list->shared[i], true));
99 * dma_resv_init - initialize a reservation object
100 * @obj: the reservation object
102 void dma_resv_init(struct dma_resv *obj)
104 ww_mutex_init(&obj->lock, &reservation_ww_class);
106 __seqcount_init(&obj->seq, reservation_seqcount_string,
107 &reservation_seqcount_class);
108 RCU_INIT_POINTER(obj->fence, NULL);
109 RCU_INIT_POINTER(obj->fence_excl, NULL);
111 EXPORT_SYMBOL(dma_resv_init);
114 * dma_resv_fini - destroys a reservation object
115 * @obj: the reservation object
117 void dma_resv_fini(struct dma_resv *obj)
119 struct dma_resv_list *fobj;
120 struct dma_fence *excl;
123 * This object should be dead and all references must have
124 * been released to it, so no need to be protected with rcu.
126 excl = rcu_dereference_protected(obj->fence_excl, 1);
130 fobj = rcu_dereference_protected(obj->fence, 1);
131 dma_resv_list_free(fobj);
132 ww_mutex_destroy(&obj->lock);
134 EXPORT_SYMBOL(dma_resv_fini);
137 * dma_resv_reserve_shared - Reserve space to add shared fences to
139 * @obj: reservation object
140 * @num_fences: number of fences we want to add
142 * Should be called before dma_resv_add_shared_fence(). Must
143 * be called with obj->lock held.
146 * Zero for success, or -errno
148 int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences)
150 struct dma_resv_list *old, *new;
151 unsigned int i, j, k, max;
153 dma_resv_assert_held(obj);
155 old = dma_resv_get_list(obj);
157 if (old && old->shared_max) {
158 if ((old->shared_count + num_fences) <= old->shared_max)
161 max = max(old->shared_count + num_fences,
162 old->shared_max * 2);
164 max = max(4ul, roundup_pow_of_two(num_fences));
167 new = dma_resv_list_alloc(max);
172 * no need to bump fence refcounts, rcu_read access
173 * requires the use of kref_get_unless_zero, and the
174 * references from the old struct are carried over to
177 for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
178 struct dma_fence *fence;
180 fence = rcu_dereference_protected(old->shared[i],
182 if (dma_fence_is_signaled(fence))
183 RCU_INIT_POINTER(new->shared[--k], fence);
185 RCU_INIT_POINTER(new->shared[j++], fence);
187 new->shared_count = j;
190 * We are not changing the effective set of fences here so can
191 * merely update the pointer to the new array; both existing
192 * readers and new readers will see exactly the same set of
193 * active (unsignaled) shared fences. Individual fences and the
194 * old array are protected by RCU and so will not vanish under
195 * the gaze of the rcu_read_lock() readers.
197 rcu_assign_pointer(obj->fence, new);
202 /* Drop the references to the signaled fences */
203 for (i = k; i < max; ++i) {
204 struct dma_fence *fence;
206 fence = rcu_dereference_protected(new->shared[i],
208 dma_fence_put(fence);
214 EXPORT_SYMBOL(dma_resv_reserve_shared);
217 * dma_resv_add_shared_fence - Add a fence to a shared slot
218 * @obj: the reservation object
219 * @fence: the shared fence to add
221 * Add a fence to a shared slot, obj->lock must be held, and
222 * dma_resv_reserve_shared() has been called.
224 void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence)
226 struct dma_resv_list *fobj;
227 struct dma_fence *old;
228 unsigned int i, count;
230 dma_fence_get(fence);
232 dma_resv_assert_held(obj);
234 fobj = dma_resv_get_list(obj);
235 count = fobj->shared_count;
238 write_seqcount_begin(&obj->seq);
240 for (i = 0; i < count; ++i) {
242 old = rcu_dereference_protected(fobj->shared[i],
244 if (old->context == fence->context ||
245 dma_fence_is_signaled(old))
249 BUG_ON(fobj->shared_count >= fobj->shared_max);
254 RCU_INIT_POINTER(fobj->shared[i], fence);
255 /* pointer update must be visible before we extend the shared_count */
256 smp_store_mb(fobj->shared_count, count);
258 write_seqcount_end(&obj->seq);
262 EXPORT_SYMBOL(dma_resv_add_shared_fence);
265 * dma_resv_add_excl_fence - Add an exclusive fence.
266 * @obj: the reservation object
267 * @fence: the shared fence to add
269 * Add a fence to the exclusive slot. The obj->lock must be held.
271 void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
273 struct dma_fence *old_fence = dma_resv_get_excl(obj);
274 struct dma_resv_list *old;
277 dma_resv_assert_held(obj);
279 old = dma_resv_get_list(obj);
281 i = old->shared_count;
284 dma_fence_get(fence);
287 write_seqcount_begin(&obj->seq);
288 /* write_seqcount_begin provides the necessary memory barrier */
289 RCU_INIT_POINTER(obj->fence_excl, fence);
291 old->shared_count = 0;
292 write_seqcount_end(&obj->seq);
295 /* inplace update, no shared fences */
297 dma_fence_put(rcu_dereference_protected(old->shared[i],
298 dma_resv_held(obj)));
300 dma_fence_put(old_fence);
302 EXPORT_SYMBOL(dma_resv_add_excl_fence);
305 * dma_resv_copy_fences - Copy all fences from src to dst.
306 * @dst: the destination reservation object
307 * @src: the source reservation object
309 * Copy all fences from src to dst. dst-lock must be held.
311 int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
313 struct dma_resv_list *src_list, *dst_list;
314 struct dma_fence *old, *new;
317 dma_resv_assert_held(dst);
320 src_list = rcu_dereference(src->fence);
324 unsigned shared_count = src_list->shared_count;
328 dst_list = dma_resv_list_alloc(shared_count);
333 src_list = rcu_dereference(src->fence);
334 if (!src_list || src_list->shared_count > shared_count) {
339 dst_list->shared_count = 0;
340 for (i = 0; i < src_list->shared_count; ++i) {
341 struct dma_fence *fence;
343 fence = rcu_dereference(src_list->shared[i]);
344 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
348 if (!dma_fence_get_rcu(fence)) {
349 dma_resv_list_free(dst_list);
350 src_list = rcu_dereference(src->fence);
354 if (dma_fence_is_signaled(fence)) {
355 dma_fence_put(fence);
359 rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
365 new = dma_fence_get_rcu_safe(&src->fence_excl);
368 src_list = dma_resv_get_list(dst);
369 old = dma_resv_get_excl(dst);
372 write_seqcount_begin(&dst->seq);
373 /* write_seqcount_begin provides the necessary memory barrier */
374 RCU_INIT_POINTER(dst->fence_excl, new);
375 RCU_INIT_POINTER(dst->fence, dst_list);
376 write_seqcount_end(&dst->seq);
379 dma_resv_list_free(src_list);
384 EXPORT_SYMBOL(dma_resv_copy_fences);
387 * dma_resv_get_fences_rcu - Get an object's shared and exclusive
388 * fences without update side lock held
389 * @obj: the reservation object
390 * @pfence_excl: the returned exclusive fence (or NULL)
391 * @pshared_count: the number of shared fences returned
392 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
393 * the required size, and must be freed by caller)
395 * Retrieve all fences from the reservation object. If the pointer for the
396 * exclusive fence is not specified the fence is put into the array of the
397 * shared fences as well. Returns either zero or -ENOMEM.
399 int dma_resv_get_fences_rcu(struct dma_resv *obj,
400 struct dma_fence **pfence_excl,
401 unsigned *pshared_count,
402 struct dma_fence ***pshared)
404 struct dma_fence **shared = NULL;
405 struct dma_fence *fence_excl;
406 unsigned int shared_count;
410 struct dma_resv_list *fobj;
414 shared_count = i = 0;
417 seq = read_seqcount_begin(&obj->seq);
419 fence_excl = rcu_dereference(obj->fence_excl);
420 if (fence_excl && !dma_fence_get_rcu(fence_excl))
423 fobj = rcu_dereference(obj->fence);
425 sz += sizeof(*shared) * fobj->shared_max;
427 if (!pfence_excl && fence_excl)
428 sz += sizeof(*shared);
431 struct dma_fence **nshared;
433 nshared = krealloc(shared, sz,
434 GFP_NOWAIT | __GFP_NOWARN);
438 dma_fence_put(fence_excl);
441 nshared = krealloc(shared, sz, GFP_KERNEL);
451 shared_count = fobj ? fobj->shared_count : 0;
452 for (i = 0; i < shared_count; ++i) {
453 shared[i] = rcu_dereference(fobj->shared[i]);
454 if (!dma_fence_get_rcu(shared[i]))
459 if (i != shared_count || read_seqcount_retry(&obj->seq, seq)) {
461 dma_fence_put(shared[i]);
462 dma_fence_put(fence_excl);
472 *pfence_excl = fence_excl;
474 shared[shared_count++] = fence_excl;
481 *pshared_count = shared_count;
485 EXPORT_SYMBOL_GPL(dma_resv_get_fences_rcu);
488 * dma_resv_wait_timeout_rcu - Wait on reservation's objects
489 * shared and/or exclusive fences.
490 * @obj: the reservation object
491 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
492 * @intr: if true, do interruptible wait
493 * @timeout: timeout value in jiffies or zero to return immediately
496 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
497 * greater than zer on success.
499 long dma_resv_wait_timeout_rcu(struct dma_resv *obj,
500 bool wait_all, bool intr,
501 unsigned long timeout)
503 struct dma_fence *fence;
504 unsigned seq, shared_count;
505 long ret = timeout ? timeout : 1;
510 seq = read_seqcount_begin(&obj->seq);
514 fence = rcu_dereference(obj->fence_excl);
515 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
516 if (!dma_fence_get_rcu(fence))
519 if (dma_fence_is_signaled(fence)) {
520 dma_fence_put(fence);
529 struct dma_resv_list *fobj = rcu_dereference(obj->fence);
532 shared_count = fobj->shared_count;
534 for (i = 0; !fence && i < shared_count; ++i) {
535 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
537 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
541 if (!dma_fence_get_rcu(lfence))
544 if (dma_fence_is_signaled(lfence)) {
545 dma_fence_put(lfence);
556 if (read_seqcount_retry(&obj->seq, seq)) {
557 dma_fence_put(fence);
561 ret = dma_fence_wait_timeout(fence, intr, ret);
562 dma_fence_put(fence);
563 if (ret > 0 && wait_all && (i + 1 < shared_count))
572 EXPORT_SYMBOL_GPL(dma_resv_wait_timeout_rcu);
575 static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence)
577 struct dma_fence *fence, *lfence = passed_fence;
580 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
581 fence = dma_fence_get_rcu(lfence);
585 ret = !!dma_fence_is_signaled(fence);
586 dma_fence_put(fence);
592 * dma_resv_test_signaled_rcu - Test if a reservation object's
593 * fences have been signaled.
594 * @obj: the reservation object
595 * @test_all: if true, test all fences, otherwise only test the exclusive
599 * true if all fences signaled, else false
601 bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all)
603 unsigned seq, shared_count;
610 seq = read_seqcount_begin(&obj->seq);
615 struct dma_resv_list *fobj = rcu_dereference(obj->fence);
618 shared_count = fobj->shared_count;
620 for (i = 0; i < shared_count; ++i) {
621 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
623 ret = dma_resv_test_signaled_single(fence);
630 if (read_seqcount_retry(&obj->seq, seq))
635 struct dma_fence *fence_excl = rcu_dereference(obj->fence_excl);
638 ret = dma_resv_test_signaled_single(fence_excl);
642 if (read_seqcount_retry(&obj->seq, seq))
650 EXPORT_SYMBOL_GPL(dma_resv_test_signaled_rcu);