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/reservation.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 * reservation_object_list_alloc - allocate fence list
54 * @shared_max: number of fences we need space for
56 * Allocate a new reservation_object_list and make sure to correctly initialize
59 static struct reservation_object_list *
60 reservation_object_list_alloc(unsigned int shared_max)
62 struct reservation_object_list *list;
64 list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL);
68 list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) /
69 sizeof(*list->shared);
75 * reservation_object_list_free - free fence list
78 * Free a reservation_object_list and make sure to drop all references.
80 static void reservation_object_list_free(struct reservation_object_list *list)
87 for (i = 0; i < list->shared_count; ++i)
88 dma_fence_put(rcu_dereference_protected(list->shared[i], true));
94 * reservation_object_init - initialize a reservation object
95 * @obj: the reservation object
97 void reservation_object_init(struct reservation_object *obj)
99 ww_mutex_init(&obj->lock, &reservation_ww_class);
100 RCU_INIT_POINTER(obj->fence, NULL);
101 RCU_INIT_POINTER(obj->fence_excl, NULL);
103 EXPORT_SYMBOL(reservation_object_init);
106 * reservation_object_fini - destroys a reservation object
107 * @obj: the reservation object
109 void reservation_object_fini(struct reservation_object *obj)
111 struct reservation_object_list *fobj;
112 struct dma_fence *excl;
115 * This object should be dead and all references must have
116 * been released to it, so no need to be protected with rcu.
118 excl = rcu_dereference_protected(obj->fence_excl, 1);
122 fobj = rcu_dereference_protected(obj->fence, 1);
123 reservation_object_list_free(fobj);
124 ww_mutex_destroy(&obj->lock);
126 EXPORT_SYMBOL(reservation_object_fini);
129 * reservation_object_reserve_shared - Reserve space to add shared fences to
130 * a reservation_object.
131 * @obj: reservation object
132 * @num_fences: number of fences we want to add
134 * Should be called before reservation_object_add_shared_fence(). Must
135 * be called with obj->lock held.
138 * Zero for success, or -errno
140 int reservation_object_reserve_shared(struct reservation_object *obj,
141 unsigned int num_fences)
143 struct reservation_object_list *old, *new;
144 unsigned int i, j, k, max;
146 reservation_object_assert_held(obj);
148 old = reservation_object_get_list(obj);
150 if (old && old->shared_max) {
151 if ((old->shared_count + num_fences) <= old->shared_max)
154 max = max(old->shared_count + num_fences,
155 old->shared_max * 2);
160 new = reservation_object_list_alloc(max);
165 * no need to bump fence refcounts, rcu_read access
166 * requires the use of kref_get_unless_zero, and the
167 * references from the old struct are carried over to
170 for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) {
171 struct dma_fence *fence;
173 fence = rcu_dereference_protected(old->shared[i],
174 reservation_object_held(obj));
175 if (dma_fence_is_signaled(fence))
176 RCU_INIT_POINTER(new->shared[--k], fence);
178 RCU_INIT_POINTER(new->shared[j++], fence);
180 new->shared_count = j;
183 * We are not changing the effective set of fences here so can
184 * merely update the pointer to the new array; both existing
185 * readers and new readers will see exactly the same set of
186 * active (unsignaled) shared fences. Individual fences and the
187 * old array are protected by RCU and so will not vanish under
188 * the gaze of the rcu_read_lock() readers.
190 rcu_assign_pointer(obj->fence, new);
195 /* Drop the references to the signaled fences */
196 for (i = k; i < max; ++i) {
197 struct dma_fence *fence;
199 fence = rcu_dereference_protected(new->shared[i],
200 reservation_object_held(obj));
201 dma_fence_put(fence);
207 EXPORT_SYMBOL(reservation_object_reserve_shared);
210 * reservation_object_add_shared_fence - Add a fence to a shared slot
211 * @obj: the reservation object
212 * @fence: the shared fence to add
214 * Add a fence to a shared slot, obj->lock must be held, and
215 * reservation_object_reserve_shared() has been called.
217 void reservation_object_add_shared_fence(struct reservation_object *obj,
218 struct dma_fence *fence)
220 struct reservation_object_list *fobj;
221 struct dma_fence *old;
222 unsigned int i, count;
224 dma_fence_get(fence);
226 reservation_object_assert_held(obj);
228 fobj = reservation_object_get_list(obj);
229 count = fobj->shared_count;
231 for (i = 0; i < count; ++i) {
233 old = rcu_dereference_protected(fobj->shared[i],
234 reservation_object_held(obj));
235 if (old->context == fence->context ||
236 dma_fence_is_signaled(old))
240 BUG_ON(fobj->shared_count >= fobj->shared_max);
245 RCU_INIT_POINTER(fobj->shared[i], fence);
246 /* pointer update must be visible before we extend the shared_count */
247 smp_store_mb(fobj->shared_count, count);
250 EXPORT_SYMBOL(reservation_object_add_shared_fence);
253 * reservation_object_add_excl_fence - Add an exclusive fence.
254 * @obj: the reservation object
255 * @fence: the shared fence to add
257 * Add a fence to the exclusive slot. The obj->lock must be held.
259 void reservation_object_add_excl_fence(struct reservation_object *obj,
260 struct dma_fence *fence)
262 struct dma_fence *old_fence = reservation_object_get_excl(obj);
263 struct reservation_object_list *old;
266 reservation_object_assert_held(obj);
268 old = reservation_object_get_list(obj);
270 i = old->shared_count;
273 dma_fence_get(fence);
276 rcu_assign_pointer(obj->fence_excl, fence);
277 /* pointer update must be visible before we modify the shared_count */
279 smp_store_mb(old->shared_count, 0);
282 /* inplace update, no shared fences */
284 dma_fence_put(rcu_dereference_protected(old->shared[i],
285 reservation_object_held(obj)));
287 dma_fence_put(old_fence);
289 EXPORT_SYMBOL(reservation_object_add_excl_fence);
292 * reservation_object_copy_fences - Copy all fences from src to dst.
293 * @dst: the destination reservation object
294 * @src: the source reservation object
296 * Copy all fences from src to dst. dst-lock must be held.
298 int reservation_object_copy_fences(struct reservation_object *dst,
299 struct reservation_object *src)
301 struct reservation_object_list *src_list, *dst_list;
302 struct dma_fence *old, *new;
303 unsigned int i, shared_count;
305 reservation_object_assert_held(dst);
310 reservation_object_fences(src, &new, &src_list, &shared_count);
314 dst_list = reservation_object_list_alloc(shared_count);
319 reservation_object_fences(src, &new, &src_list, &shared_count);
320 if (!src_list || shared_count > dst_list->shared_max) {
325 dst_list->shared_count = 0;
326 for (i = 0; i < shared_count; ++i) {
327 struct dma_fence *fence;
329 fence = rcu_dereference(src_list->shared[i]);
330 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
334 if (!dma_fence_get_rcu(fence)) {
335 reservation_object_list_free(dst_list);
339 if (dma_fence_is_signaled(fence)) {
340 dma_fence_put(fence);
344 rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence);
350 if (new && !dma_fence_get_rcu(new)) {
351 reservation_object_list_free(dst_list);
356 src_list = reservation_object_get_list(dst);
357 old = reservation_object_get_excl(dst);
360 rcu_assign_pointer(dst->fence_excl, new);
361 rcu_assign_pointer(dst->fence, dst_list);
364 reservation_object_list_free(src_list);
369 EXPORT_SYMBOL(reservation_object_copy_fences);
372 * reservation_object_get_fences_rcu - Get an object's shared and exclusive
373 * fences without update side lock held
374 * @obj: the reservation object
375 * @pfence_excl: the returned exclusive fence (or NULL)
376 * @pshared_count: the number of shared fences returned
377 * @pshared: the array of shared fence ptrs returned (array is krealloc'd to
378 * the required size, and must be freed by caller)
380 * Retrieve all fences from the reservation object. If the pointer for the
381 * exclusive fence is not specified the fence is put into the array of the
382 * shared fences as well. Returns either zero or -ENOMEM.
384 int reservation_object_get_fences_rcu(struct reservation_object *obj,
385 struct dma_fence **pfence_excl,
386 unsigned *pshared_count,
387 struct dma_fence ***pshared)
389 struct dma_fence **shared = NULL;
390 struct dma_fence *fence_excl;
391 unsigned int shared_count;
395 struct reservation_object_list *fobj;
402 reservation_object_fences(obj, &fence_excl, &fobj,
405 if (fence_excl && !dma_fence_get_rcu(fence_excl))
409 sz += sizeof(*shared) * fobj->shared_max;
411 if (!pfence_excl && fence_excl)
412 sz += sizeof(*shared);
415 struct dma_fence **nshared;
417 nshared = krealloc(shared, sz,
418 GFP_NOWAIT | __GFP_NOWARN);
422 dma_fence_put(fence_excl);
425 nshared = krealloc(shared, sz, GFP_KERNEL);
435 for (i = 0; i < shared_count; ++i) {
436 shared[i] = rcu_dereference(fobj->shared[i]);
437 if (!dma_fence_get_rcu(shared[i]))
442 if (i != shared_count) {
444 dma_fence_put(shared[i]);
445 dma_fence_put(fence_excl);
455 *pfence_excl = fence_excl;
457 shared[++shared_count] = fence_excl;
464 *pshared_count = shared_count;
468 EXPORT_SYMBOL_GPL(reservation_object_get_fences_rcu);
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
479 * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or
480 * greater than zer on success.
482 long reservation_object_wait_timeout_rcu(struct reservation_object *obj,
483 bool wait_all, bool intr,
484 unsigned long timeout)
486 struct reservation_object_list *fobj;
487 struct dma_fence *fence;
488 unsigned shared_count;
489 long ret = timeout ? timeout : 1;
496 reservation_object_fences(obj, &fence, &fobj, &shared_count);
497 if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) {
498 if (!dma_fence_get_rcu(fence))
501 if (dma_fence_is_signaled(fence)) {
502 dma_fence_put(fence);
511 for (i = 0; !fence && i < shared_count; ++i) {
512 struct dma_fence *lfence = rcu_dereference(fobj->shared[i]);
514 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
518 if (!dma_fence_get_rcu(lfence))
521 if (dma_fence_is_signaled(lfence)) {
522 dma_fence_put(lfence);
533 ret = dma_fence_wait_timeout(fence, intr, ret);
534 dma_fence_put(fence);
535 if (ret > 0 && wait_all && (i + 1 < shared_count))
544 EXPORT_SYMBOL_GPL(reservation_object_wait_timeout_rcu);
548 reservation_object_test_signaled_single(struct dma_fence *passed_fence)
550 struct dma_fence *fence, *lfence = passed_fence;
553 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) {
554 fence = dma_fence_get_rcu(lfence);
558 ret = !!dma_fence_is_signaled(fence);
559 dma_fence_put(fence);
565 * reservation_object_test_signaled_rcu - Test if a reservation object's
566 * fences have been signaled.
567 * @obj: the reservation object
568 * @test_all: if true, test all fences, otherwise only test the exclusive
572 * true if all fences signaled, else false
574 bool reservation_object_test_signaled_rcu(struct reservation_object *obj,
577 struct reservation_object_list *fobj;
578 struct dma_fence *fence_excl;
579 unsigned shared_count;
586 reservation_object_fences(obj, &fence_excl, &fobj, &shared_count);
590 for (i = 0; i < shared_count; ++i) {
591 struct dma_fence *fence = rcu_dereference(fobj->shared[i]);
593 ret = reservation_object_test_signaled_single(fence);
601 if (!shared_count && fence_excl) {
602 ret = reservation_object_test_signaled_single(fence_excl);
610 EXPORT_SYMBOL_GPL(reservation_object_test_signaled_rcu);