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>
38 DEFINE_WW_CLASS(reservation_ww_class);
39 EXPORT_SYMBOL(reservation_ww_class);
42 * Reserve space to add a shared fence to a reservation_object,
43 * must be called with obj->lock held.
45 int reservation_object_reserve_shared(struct reservation_object *obj)
47 struct reservation_object_list *fobj, *old;
50 old = reservation_object_get_list(obj);
52 if (old && old->shared_max) {
53 if (old->shared_count < old->shared_max) {
54 /* perform an in-place update */
59 max = old->shared_max * 2;
64 * resize obj->staged or allocate if it doesn't exist,
65 * noop if already correct size
67 fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
73 fobj->shared_max = max;
76 EXPORT_SYMBOL(reservation_object_reserve_shared);
79 reservation_object_add_shared_inplace(struct reservation_object *obj,
80 struct reservation_object_list *fobj,
85 for (i = 0; i < fobj->shared_count; ++i) {
86 if (fobj->shared[i]->context == fence->context) {
87 struct fence *old_fence = fobj->shared[i];
91 fobj->shared[i] = fence;
99 fobj->shared[fobj->shared_count] = fence;
101 * make the new fence visible before incrementing
105 fobj->shared_count++;
109 reservation_object_add_shared_replace(struct reservation_object *obj,
110 struct reservation_object_list *old,
111 struct reservation_object_list *fobj,
119 fobj->shared[0] = fence;
120 fobj->shared_count = 1;
125 * no need to bump fence refcounts, rcu_read access
126 * requires the use of kref_get_unless_zero, and the
127 * references from the old struct are carried over to
130 fobj->shared_count = old->shared_count;
132 for (i = 0; i < old->shared_count; ++i) {
133 if (fence && old->shared[i]->context == fence->context) {
134 fence_put(old->shared[i]);
135 fobj->shared[i] = fence;
138 fobj->shared[i] = old->shared[i];
141 fobj->shared[fobj->shared_count++] = fence;
149 * Add a fence to a shared slot, obj->lock must be held, and
150 * reservation_object_reserve_shared_fence has been called.
152 void reservation_object_add_shared_fence(struct reservation_object *obj,
155 struct reservation_object_list *old, *fobj = obj->staged;
157 old = reservation_object_get_list(obj);
161 BUG_ON(old->shared_count == old->shared_max);
162 reservation_object_add_shared_inplace(obj, old, fence);
164 reservation_object_add_shared_replace(obj, old, fobj, fence);
166 EXPORT_SYMBOL(reservation_object_add_shared_fence);
168 void reservation_object_add_excl_fence(struct reservation_object *obj,
171 struct fence *old_fence = obj->fence_excl;
172 struct reservation_object_list *old;
175 old = reservation_object_get_list(obj);
177 i = old->shared_count;
178 old->shared_count = 0;
184 obj->fence_excl = fence;
186 /* inplace update, no shared fences */
188 fence_put(old->shared[i]);
191 fence_put(old_fence);
193 EXPORT_SYMBOL(reservation_object_add_excl_fence);