reservation: update api and add some helpers
[linux-2.6-block.git] / drivers / dma-buf / reservation.c
1 /*
2  * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst)
3  *
4  * Based on bo.c which bears the following copyright notice,
5  * but is dual licensed:
6  *
7  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
8  * All Rights Reserved.
9  *
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:
17  *
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.
21  *
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.
29  *
30  **************************************************************************/
31 /*
32  * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
33  */
34
35 #include <linux/reservation.h>
36 #include <linux/export.h>
37
38 DEFINE_WW_CLASS(reservation_ww_class);
39 EXPORT_SYMBOL(reservation_ww_class);
40
41 /*
42  * Reserve space to add a shared fence to a reservation_object,
43  * must be called with obj->lock held.
44  */
45 int reservation_object_reserve_shared(struct reservation_object *obj)
46 {
47         struct reservation_object_list *fobj, *old;
48         u32 max;
49
50         old = reservation_object_get_list(obj);
51
52         if (old && old->shared_max) {
53                 if (old->shared_count < old->shared_max) {
54                         /* perform an in-place update */
55                         kfree(obj->staged);
56                         obj->staged = NULL;
57                         return 0;
58                 } else
59                         max = old->shared_max * 2;
60         } else
61                 max = 4;
62
63         /*
64          * resize obj->staged or allocate if it doesn't exist,
65          * noop if already correct size
66          */
67         fobj = krealloc(obj->staged, offsetof(typeof(*fobj), shared[max]),
68                         GFP_KERNEL);
69         if (!fobj)
70                 return -ENOMEM;
71
72         obj->staged = fobj;
73         fobj->shared_max = max;
74         return 0;
75 }
76 EXPORT_SYMBOL(reservation_object_reserve_shared);
77
78 static void
79 reservation_object_add_shared_inplace(struct reservation_object *obj,
80                                       struct reservation_object_list *fobj,
81                                       struct fence *fence)
82 {
83         u32 i;
84
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];
88
89                         fence_get(fence);
90
91                         fobj->shared[i] = fence;
92
93                         fence_put(old_fence);
94                         return;
95                 }
96         }
97
98         fence_get(fence);
99         fobj->shared[fobj->shared_count] = fence;
100         /*
101          * make the new fence visible before incrementing
102          * fobj->shared_count
103          */
104         smp_wmb();
105         fobj->shared_count++;
106 }
107
108 static void
109 reservation_object_add_shared_replace(struct reservation_object *obj,
110                                       struct reservation_object_list *old,
111                                       struct reservation_object_list *fobj,
112                                       struct fence *fence)
113 {
114         unsigned i;
115
116         fence_get(fence);
117
118         if (!old) {
119                 fobj->shared[0] = fence;
120                 fobj->shared_count = 1;
121                 goto done;
122         }
123
124         /*
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
128          * the new.
129          */
130         fobj->shared_count = old->shared_count;
131
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;
136                         fence = NULL;
137                 } else
138                         fobj->shared[i] = old->shared[i];
139         }
140         if (fence)
141                 fobj->shared[fobj->shared_count++] = fence;
142
143 done:
144         obj->fence = fobj;
145         kfree(old);
146 }
147
148 /*
149  * Add a fence to a shared slot, obj->lock must be held, and
150  * reservation_object_reserve_shared_fence has been called.
151  */
152 void reservation_object_add_shared_fence(struct reservation_object *obj,
153                                          struct fence *fence)
154 {
155         struct reservation_object_list *old, *fobj = obj->staged;
156
157         old = reservation_object_get_list(obj);
158         obj->staged = NULL;
159
160         if (!fobj) {
161                 BUG_ON(old->shared_count == old->shared_max);
162                 reservation_object_add_shared_inplace(obj, old, fence);
163         } else
164                 reservation_object_add_shared_replace(obj, old, fobj, fence);
165 }
166 EXPORT_SYMBOL(reservation_object_add_shared_fence);
167
168 void reservation_object_add_excl_fence(struct reservation_object *obj,
169                                        struct fence *fence)
170 {
171         struct fence *old_fence = obj->fence_excl;
172         struct reservation_object_list *old;
173         u32 i = 0;
174
175         old = reservation_object_get_list(obj);
176         if (old) {
177                 i = old->shared_count;
178                 old->shared_count = 0;
179         }
180
181         if (fence)
182                 fence_get(fence);
183
184         obj->fence_excl = fence;
185
186         /* inplace update, no shared fences */
187         while (i--)
188                 fence_put(old->shared[i]);
189
190         if (old_fence)
191                 fence_put(old_fence);
192 }
193 EXPORT_SYMBOL(reservation_object_add_excl_fence);