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);
53 * dma_resv_list_alloc - allocate fence list
54 * @shared_max: number of fences we need space for
56 * Allocate a new dma_resv_list and make sure to correctly initialize
59 static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max)
61 struct dma_resv_list *list;
63 list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL);
67 list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) /
68 sizeof(*list->shared);
74 * dma_resv_list_free - free fence list
77 * Free a dma_resv_list and make sure to drop all references.
79 static void dma_resv_list_free(struct dma_resv_list *list)
86 for (i = 0; i < list->shared_count; ++i)
87 dma_fence_put(rcu_dereference_protected(list->shared[i], true));
93 * dma_resv_init - initialize a reservation object
94 * @obj: the reservation object
96 void dma_resv_init(struct dma_resv *obj)
98 ww_mutex_init(&obj->lock, &reservation_ww_class);
99 RCU_INIT_POINTER(obj->fence, NULL);
100 RCU_INIT_POINTER(obj->fence_excl, NULL);
102 EXPORT_SYMBOL(dma_resv_init);
105 * dma_resv_fini - destroys a reservation object
106 * @obj: the reservation object
108 void dma_resv_fini(struct dma_resv *obj)
110 struct dma_resv_list *fobj;
111 struct dma_fence *excl;
114 * This object should be dead and all references must have
115 * been released to it, so no need to be protected with rcu.
117 excl = rcu_dereference_protected(obj->fence_excl, 1);
121 fobj = rcu_dereference_protected(obj->fence, 1);
122 dma_resv_list_free(fobj);
123 ww_mutex_destroy(&obj->lock);
125 EXPORT_SYMBOL(dma_resv_fini);
128 * dma_resv_reserve_shared - Reserve space to add shared fences to
130 * @obj: reservation object
131 * @num_fences: number of fences we want to add
133 * Should be called before dma_resv_add_shared_fence(). Must
134 * be called with obj->lock held.
137 * Zero for success, or -errno
139 int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences)
141 struct dma_resv_list *old, *new;
142 unsigned int i, j, k, max;
144 dma_resv_assert_held(obj);
146 old = dma_resv_get_list(obj);
148 if (old && old->shared_max) {
149 if ((old->shared_count + num_fences) <= old->shared_max)
152 max = max(old->shared_count + num_fences,
153 old->shared_max * 2);
158 new = dma_resv_list_alloc(max);
163 * no need to bump fence refcounts, rcu_read access
164 * requires the use of kref_get_unless_zero, and the
165 * references from the old struct are carried over to
168 for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
169 struct dma_fence *fence;
171 fence = rcu_dereference_protected(old->shared[i],
173 if (dma_fence_is_signaled(fence))
174 RCU_INIT_POINTER(new->shared[--k], fence);
176 RCU_INIT_POINTER(new->shared[j++], fence);
178 new->shared_count = j;
181 * We are not changing the effective set of fences here so can
182 * merely update the pointer to the new array; both existing
183 * readers and new readers will see exactly the same set of
184 * active (unsignaled) shared fences. Individual fences and the
185 * old array are protected by RCU and so will not vanish under
186 * the gaze of the rcu_read_lock() readers.
188 rcu_assign_pointer(obj->fence, new);
193 /* Drop the references to the signaled fences */
194 for (i = k; i < max; ++i) {
195 struct dma_fence *fence;
197 fence = rcu_dereference_protected(new->shared[i],
199 dma_fence_put(fence);
205 EXPORT_SYMBOL(dma_resv_reserve_shared);
208 * dma_resv_add_shared_fence - Add a fence to a shared slot
209 * @obj: the reservation object
210 * @fence: the shared fence to add
212 * Add a fence to a shared slot, obj->lock must be held, and
213 * dma_resv_reserve_shared() has been called.
215 void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence)
217 struct dma_resv_list *fobj;
218 struct dma_fence *old;
219 unsigned int i, count;
221 dma_fence_get(fence);
223 dma_resv_assert_held(obj);
225 fobj = dma_resv_get_list(obj);
226 count = fobj->shared_count;
228 for (i = 0; i < count; ++i) {
230 old = rcu_dereference_protected(fobj->shared[i],
232 if (old->context == fence->context ||
233 dma_fence_is_signaled(old))
237 BUG_ON(fobj->shared_count >= fobj->shared_max);
242 RCU_INIT_POINTER(fobj->shared[i], fence);
243 /* pointer update must be visible before we extend the shared_count */
244 smp_store_mb(fobj->shared_count, count);
247 EXPORT_SYMBOL(dma_resv_add_shared_fence);
250 * dma_resv_add_excl_fence - Add an exclusive fence.
251 * @obj: the reservation object
252 * @fence: the shared fence to add
254 * Add a fence to the exclusive slot. The obj->lock must be held.
256 void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence)
258 struct dma_fence *old_fence = dma_resv_get_excl(obj);
259 struct dma_resv_list *old;
262 dma_resv_assert_held(obj);
264 old = dma_resv_get_list(obj);
266 i = old->shared_count;
269 dma_fence_get(fence);
272 rcu_assign_pointer(obj->fence_excl, fence);
273 /* pointer update must be visible before we modify the shared_count */
275 smp_store_mb(old->shared_count, 0);
278 /* inplace update, no shared fences */
280 dma_fence_put(rcu_dereference_protected(old->shared[i],
281 dma_resv_held(obj)));
283 dma_fence_put(old_fence);
285 EXPORT_SYMBOL(dma_resv_add_excl_fence);
288 * dma_resv_copy_fences - Copy all fences from src to dst.
289 * @dst: the destination reservation object
290 * @src: the source reservation object
292 * Copy all fences from src to dst. dst-lock must be held.
294 int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src)
296 struct dma_resv_list *src_list, *dst_list;
297 struct dma_fence *old, *new;
298 unsigned int i, shared_count;
300 dma_resv_assert_held(dst);
305 dma_resv_fences(src, &new, &src_list, &shared_count);
309 dst_list = dma_resv_list_alloc(shared_count);
314 dma_resv_fences(src, &new, &src_list, &shared_count);
315 if (!src_list || shared_count > dst_list->shared_max) {
320 dst_list->shared_count = 0;
321 for (i = 0; i < shared_count; ++i) {
322 struct dma_fence *fence;
324 fence = rcu_dereference(src_list->shared[i]);
325 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
329 if (!dma_fence_get_rcu(fence)) {
330 dma_resv_list_free(dst_list);
334 if (dma_fence_is_signaled(fence)) {
335 dma_fence_put(fence);
339 rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
345 if (new && !dma_fence_get_rcu(new)) {
346 dma_resv_list_free(dst_list);
351 src_list = dma_resv_get_list(dst);
352 old = dma_resv_get_excl(dst);
355 rcu_assign_pointer(dst->fence_excl, new);
356 rcu_assign_pointer(dst->fence, dst_list);
359 dma_resv_list_free(src_list);
364 EXPORT_SYMBOL(dma_resv_copy_fences);
367 * dma_resv_get_fences_rcu - Get an object's shared and exclusive
368 * fences without update side lock held
369 * @obj: the reservation object
370 * @pfence_excl: the returned exclusive fence (or NULL)
371 * @pshared_count: the number of shared fences returned
372 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
373 * the required size, and must be freed by caller)
375 * Retrieve all fences from the reservation object. If the pointer for the
376 * exclusive fence is not specified the fence is put into the array of the
377 * shared fences as well. Returns either zero or -ENOMEM.
379 int dma_resv_get_fences_rcu(struct dma_resv *obj,
380 struct dma_fence **pfence_excl,
381 unsigned *pshared_count,
382 struct dma_fence ***pshared)
384 struct dma_fence **shared = NULL;
385 struct dma_fence *fence_excl;
386 unsigned int shared_count;
390 struct dma_resv_list *fobj;
397 dma_resv_fences(obj, &fence_excl, &fobj,
400 if (fence_excl && !dma_fence_get_rcu(fence_excl))
404 sz += sizeof(*shared) * fobj->shared_max;
406 if (!pfence_excl && fence_excl)
407 sz += sizeof(*shared);
410 struct dma_fence **nshared;
412 nshared = krealloc(shared, sz,
413 GFP_NOWAIT | __GFP_NOWARN);
417 dma_fence_put(fence_excl);
420 nshared = krealloc(shared, sz, GFP_KERNEL);
430 for (i = 0; i < shared_count; ++i) {
431 shared[i] = rcu_dereference(fobj->shared[i]);
432 if (!dma_fence_get_rcu(shared[i]))
437 if (i != shared_count) {
439 dma_fence_put(shared[i]);
440 dma_fence_put(fence_excl);
450 *pfence_excl = fence_excl;
452 shared[++shared_count] = fence_excl;
459 *pshared_count = shared_count;
463 EXPORT_SYMBOL_GPL(dma_resv_get_fences_rcu);
466 * dma_resv_wait_timeout_rcu - Wait on reservation's objects
467 * shared and/or exclusive fences.
468 * @obj: the reservation object
469 * @wait_all: if true, wait on all fences, else wait on just exclusive fence
470 * @intr: if true, do interruptible wait
471 * @timeout: timeout value in jiffies or zero to return immediately
474 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
475 * greater than zer on success.
477 long dma_resv_wait_timeout_rcu(struct dma_resv *obj,
478 bool wait_all, bool intr,
479 unsigned long timeout)
481 struct dma_resv_list *fobj;
482 struct dma_fence *fence;
483 unsigned shared_count;
484 long ret = timeout ? timeout : 1;
491 dma_resv_fences(obj, &fence, &fobj, &shared_count);
492 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
493 if (!dma_fence_get_rcu(fence))
496 if (dma_fence_is_signaled(fence)) {
497 dma_fence_put(fence);
506 for (i = 0; !fence && i < shared_count; ++i) {
507 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
509 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
513 if (!dma_fence_get_rcu(lfence))
516 if (dma_fence_is_signaled(lfence)) {
517 dma_fence_put(lfence);
528 ret = dma_fence_wait_timeout(fence, intr, ret);
529 dma_fence_put(fence);
530 if (ret > 0 && wait_all && (i + 1 < shared_count))
539 EXPORT_SYMBOL_GPL(dma_resv_wait_timeout_rcu);
542 static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence)
544 struct dma_fence *fence, *lfence = passed_fence;
547 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
548 fence = dma_fence_get_rcu(lfence);
552 ret = !!dma_fence_is_signaled(fence);
553 dma_fence_put(fence);
559 * dma_resv_test_signaled_rcu - Test if a reservation object's
560 * fences have been signaled.
561 * @obj: the reservation object
562 * @test_all: if true, test all fences, otherwise only test the exclusive
566 * true if all fences signaled, else false
568 bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all)
570 struct dma_resv_list *fobj;
571 struct dma_fence *fence_excl;
572 unsigned shared_count;
579 dma_resv_fences(obj, &fence_excl, &fobj, &shared_count);
583 for (i = 0; i < shared_count; ++i) {
584 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
586 ret = dma_resv_test_signaled_single(fence);
594 if (!shared_count && fence_excl) {
595 ret = dma_resv_test_signaled_single(fence_excl);
603 EXPORT_SYMBOL_GPL(dma_resv_test_signaled_rcu);