Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 1 | /* |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 2 | * Copyright (C) 2012-2014 Canonical Ltd (Maarten Lankhorst) |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 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 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 35 | #include <linux/dma-resv.h> |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 36 | #include <linux/export.h> |
| 37 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 38 | /** |
| 39 | * DOC: Reservation Object Overview |
| 40 | * |
| 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 |
| 46 | * write-side updates. |
| 47 | */ |
| 48 | |
Thomas Hellstrom | 08295b3 | 2018-06-15 10:17:38 +0200 | [diff] [blame] | 49 | DEFINE_WD_CLASS(reservation_ww_class); |
Maarten Lankhorst | 786d725 | 2013-06-27 13:48:16 +0200 | [diff] [blame] | 50 | EXPORT_SYMBOL(reservation_ww_class); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 51 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 52 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 53 | * dma_resv_list_alloc - allocate fence list |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 54 | * @shared_max: number of fences we need space for |
| 55 | * |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 56 | * Allocate a new dma_resv_list and make sure to correctly initialize |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 57 | * shared_max. |
| 58 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 59 | static struct dma_resv_list *dma_resv_list_alloc(unsigned int shared_max) |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 60 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 61 | struct dma_resv_list *list; |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 62 | |
| 63 | list = kmalloc(offsetof(typeof(*list), shared[shared_max]), GFP_KERNEL); |
| 64 | if (!list) |
| 65 | return NULL; |
| 66 | |
| 67 | list->shared_max = (ksize(list) - offsetof(typeof(*list), shared)) / |
| 68 | sizeof(*list->shared); |
| 69 | |
| 70 | return list; |
| 71 | } |
| 72 | |
| 73 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 74 | * dma_resv_list_free - free fence list |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 75 | * @list: list to free |
| 76 | * |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 77 | * Free a dma_resv_list and make sure to drop all references. |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 78 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 79 | static void dma_resv_list_free(struct dma_resv_list *list) |
Christian König | 96e9549 | 2019-08-06 13:33:12 +0200 | [diff] [blame] | 80 | { |
| 81 | unsigned int i; |
| 82 | |
| 83 | if (!list) |
| 84 | return; |
| 85 | |
| 86 | for (i = 0; i < list->shared_count; ++i) |
| 87 | dma_fence_put(rcu_dereference_protected(list->shared[i], true)); |
| 88 | |
| 89 | kfree_rcu(list, rcu); |
| 90 | } |
| 91 | |
| 92 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 93 | * dma_resv_init - initialize a reservation object |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 94 | * @obj: the reservation object |
| 95 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 96 | void dma_resv_init(struct dma_resv *obj) |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 97 | { |
| 98 | ww_mutex_init(&obj->lock, &reservation_ww_class); |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 99 | RCU_INIT_POINTER(obj->fence, NULL); |
| 100 | RCU_INIT_POINTER(obj->fence_excl, NULL); |
| 101 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 102 | EXPORT_SYMBOL(dma_resv_init); |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 103 | |
| 104 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 105 | * dma_resv_fini - destroys a reservation object |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 106 | * @obj: the reservation object |
| 107 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 108 | void dma_resv_fini(struct dma_resv *obj) |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 109 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 110 | struct dma_resv_list *fobj; |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 111 | struct dma_fence *excl; |
| 112 | |
| 113 | /* |
| 114 | * This object should be dead and all references must have |
| 115 | * been released to it, so no need to be protected with rcu. |
| 116 | */ |
| 117 | excl = rcu_dereference_protected(obj->fence_excl, 1); |
| 118 | if (excl) |
| 119 | dma_fence_put(excl); |
| 120 | |
| 121 | fobj = rcu_dereference_protected(obj->fence, 1); |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 122 | dma_resv_list_free(fobj); |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 123 | ww_mutex_destroy(&obj->lock); |
| 124 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 125 | EXPORT_SYMBOL(dma_resv_fini); |
Christian König | 8735f16 | 2019-06-26 16:31:46 +0200 | [diff] [blame] | 126 | |
| 127 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 128 | * dma_resv_reserve_shared - Reserve space to add shared fences to |
| 129 | * a dma_resv. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 130 | * @obj: reservation object |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame] | 131 | * @num_fences: number of fences we want to add |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 132 | * |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 133 | * Should be called before dma_resv_add_shared_fence(). Must |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 134 | * be called with obj->lock held. |
| 135 | * |
| 136 | * RETURNS |
| 137 | * Zero for success, or -errno |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 138 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 139 | int dma_resv_reserve_shared(struct dma_resv *obj, unsigned int num_fences) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 140 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 141 | struct dma_resv_list *old, *new; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 142 | unsigned int i, j, k, max; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 143 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 144 | dma_resv_assert_held(obj); |
Lucas Stach | 547c713 | 2017-06-13 10:26:46 +0200 | [diff] [blame] | 145 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 146 | old = dma_resv_get_list(obj); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 147 | |
| 148 | if (old && old->shared_max) { |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame] | 149 | if ((old->shared_count + num_fences) <= old->shared_max) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 150 | return 0; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 151 | else |
Christian König | ca05359 | 2018-09-19 16:12:25 +0200 | [diff] [blame] | 152 | max = max(old->shared_count + num_fences, |
| 153 | old->shared_max * 2); |
Christian König | ca25fe5 | 2017-11-14 15:24:36 +0100 | [diff] [blame] | 154 | } else { |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 155 | max = 4; |
Christian König | ca25fe5 | 2017-11-14 15:24:36 +0100 | [diff] [blame] | 156 | } |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 157 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 158 | new = dma_resv_list_alloc(max); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 159 | if (!new) |
| 160 | return -ENOMEM; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 161 | |
| 162 | /* |
| 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 |
| 166 | * the new. |
| 167 | */ |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 168 | for (i = 0, j = 0, k = max; i < (old ? old->shared_count : 0); ++i) { |
| 169 | struct dma_fence *fence; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 170 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 171 | fence = rcu_dereference_protected(old->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 172 | dma_resv_held(obj)); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 173 | if (dma_fence_is_signaled(fence)) |
| 174 | RCU_INIT_POINTER(new->shared[--k], fence); |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 175 | else |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 176 | RCU_INIT_POINTER(new->shared[j++], fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 177 | } |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 178 | new->shared_count = j; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 179 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 180 | /* |
Chris Wilson | 30fe7b0 | 2019-07-12 09:03:14 +0100 | [diff] [blame] | 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. |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 187 | */ |
Chris Wilson | 30fe7b0 | 2019-07-12 09:03:14 +0100 | [diff] [blame] | 188 | rcu_assign_pointer(obj->fence, new); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 189 | |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 190 | if (!old) |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 191 | return 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 192 | |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 193 | /* Drop the references to the signaled fences */ |
Chris Wilson | 94eb1e1 | 2019-07-12 09:03:13 +0100 | [diff] [blame] | 194 | for (i = k; i < max; ++i) { |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 195 | struct dma_fence *fence; |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 196 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 197 | fence = rcu_dereference_protected(new->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 198 | dma_resv_held(obj)); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 199 | dma_fence_put(fence); |
Christian König | 4d9c62e | 2017-11-14 15:24:35 +0100 | [diff] [blame] | 200 | } |
| 201 | kfree_rcu(old, rcu); |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 202 | |
| 203 | return 0; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 204 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 205 | EXPORT_SYMBOL(dma_resv_reserve_shared); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 206 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 207 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 208 | * dma_resv_add_shared_fence - Add a fence to a shared slot |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 209 | * @obj: the reservation object |
| 210 | * @fence: the shared fence to add |
| 211 | * |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 212 | * Add a fence to a shared slot, obj->lock must be held, and |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 213 | * dma_resv_reserve_shared() has been called. |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 214 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 215 | void dma_resv_add_shared_fence(struct dma_resv *obj, struct dma_fence *fence) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 216 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 217 | struct dma_resv_list *fobj; |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 218 | struct dma_fence *old; |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 219 | unsigned int i, count; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 220 | |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 221 | dma_fence_get(fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 222 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 223 | dma_resv_assert_held(obj); |
Lucas Stach | 547c713 | 2017-06-13 10:26:46 +0200 | [diff] [blame] | 224 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 225 | fobj = dma_resv_get_list(obj); |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 226 | count = fobj->shared_count; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 227 | |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 228 | for (i = 0; i < count; ++i) { |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 229 | |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 230 | old = rcu_dereference_protected(fobj->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 231 | dma_resv_held(obj)); |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 232 | if (old->context == fence->context || |
| 233 | dma_fence_is_signaled(old)) |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 234 | goto replace; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | BUG_ON(fobj->shared_count >= fobj->shared_max); |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 238 | old = NULL; |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 239 | count++; |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 240 | |
| 241 | replace: |
Christian König | 27836b6 | 2018-08-08 16:01:22 +0200 | [diff] [blame] | 242 | RCU_INIT_POINTER(fobj->shared[i], fence); |
Chris Wilson | a590d0f | 2018-10-26 09:03:02 +0100 | [diff] [blame] | 243 | /* pointer update must be visible before we extend the shared_count */ |
| 244 | smp_store_mb(fobj->shared_count, count); |
Christian König | 93505ee | 2019-08-05 11:14:27 +0200 | [diff] [blame] | 245 | dma_fence_put(old); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 246 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 247 | EXPORT_SYMBOL(dma_resv_add_shared_fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 248 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 249 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 250 | * dma_resv_add_excl_fence - Add an exclusive fence. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 251 | * @obj: the reservation object |
| 252 | * @fence: the shared fence to add |
| 253 | * |
| 254 | * Add a fence to the exclusive slot. The obj->lock must be held. |
| 255 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 256 | void dma_resv_add_excl_fence(struct dma_resv *obj, struct dma_fence *fence) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 257 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 258 | struct dma_fence *old_fence = dma_resv_get_excl(obj); |
| 259 | struct dma_resv_list *old; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 260 | u32 i = 0; |
| 261 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 262 | dma_resv_assert_held(obj); |
Lucas Stach | 547c713 | 2017-06-13 10:26:46 +0200 | [diff] [blame] | 263 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 264 | old = dma_resv_get_list(obj); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 265 | if (old) |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 266 | i = old->shared_count; |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 267 | |
| 268 | if (fence) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 269 | dma_fence_get(fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 270 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 271 | preempt_disable(); |
Christian König | 5d344f5 | 2019-08-05 14:24:55 +0200 | [diff] [blame] | 272 | rcu_assign_pointer(obj->fence_excl, fence); |
| 273 | /* pointer update must be visible before we modify the shared_count */ |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 274 | if (old) |
Christian König | 5d344f5 | 2019-08-05 14:24:55 +0200 | [diff] [blame] | 275 | smp_store_mb(old->shared_count, 0); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 276 | preempt_enable(); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 277 | |
| 278 | /* inplace update, no shared fences */ |
| 279 | while (i--) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 280 | dma_fence_put(rcu_dereference_protected(old->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 281 | dma_resv_held(obj))); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 282 | |
Christian König | f3e31b7 | 2017-08-07 17:32:21 -0400 | [diff] [blame] | 283 | dma_fence_put(old_fence); |
Maarten Lankhorst | 04a5faa | 2014-07-01 12:57:54 +0200 | [diff] [blame] | 284 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 285 | EXPORT_SYMBOL(dma_resv_add_excl_fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 286 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 287 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 288 | * dma_resv_copy_fences - Copy all fences from src to dst. |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 289 | * @dst: the destination reservation object |
| 290 | * @src: the source reservation object |
| 291 | * |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 292 | * Copy all fences from src to dst. dst-lock must be held. |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 293 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 294 | int dma_resv_copy_fences(struct dma_resv *dst, struct dma_resv *src) |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 295 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 296 | struct dma_resv_list *src_list, *dst_list; |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 297 | struct dma_fence *old, *new; |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 298 | unsigned int i, shared_count; |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 299 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 300 | dma_resv_assert_held(dst); |
Lucas Stach | 547c713 | 2017-06-13 10:26:46 +0200 | [diff] [blame] | 301 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 302 | rcu_read_lock(); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 303 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 304 | retry: |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 305 | dma_resv_fences(src, &new, &src_list, &shared_count); |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 306 | if (shared_count) { |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 307 | rcu_read_unlock(); |
| 308 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 309 | dst_list = dma_resv_list_alloc(shared_count); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 310 | if (!dst_list) |
| 311 | return -ENOMEM; |
| 312 | |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 313 | rcu_read_lock(); |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 314 | dma_resv_fences(src, &new, &src_list, &shared_count); |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 315 | if (!src_list || shared_count > dst_list->shared_max) { |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 316 | kfree(dst_list); |
| 317 | goto retry; |
| 318 | } |
| 319 | |
| 320 | dst_list->shared_count = 0; |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 321 | for (i = 0; i < shared_count; ++i) { |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 322 | struct dma_fence *fence; |
| 323 | |
| 324 | fence = rcu_dereference(src_list->shared[i]); |
| 325 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 326 | &fence->flags)) |
| 327 | continue; |
| 328 | |
| 329 | if (!dma_fence_get_rcu(fence)) { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 330 | dma_resv_list_free(dst_list); |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 331 | goto retry; |
| 332 | } |
| 333 | |
| 334 | if (dma_fence_is_signaled(fence)) { |
| 335 | dma_fence_put(fence); |
| 336 | continue; |
| 337 | } |
| 338 | |
Ville Syrjälä | ad46d7b | 2017-11-02 22:03:36 +0200 | [diff] [blame] | 339 | rcu_assign_pointer(dst_list->shared[dst_list->shared_count++], fence); |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 340 | } |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 341 | } else { |
| 342 | dst_list = NULL; |
| 343 | } |
| 344 | |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 345 | if (new && !dma_fence_get_rcu(new)) { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 346 | dma_resv_list_free(dst_list); |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 347 | goto retry; |
| 348 | } |
Christian König | 39e16ba | 2017-09-04 21:02:45 +0200 | [diff] [blame] | 349 | rcu_read_unlock(); |
| 350 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 351 | src_list = dma_resv_get_list(dst); |
| 352 | old = dma_resv_get_excl(dst); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 353 | |
| 354 | preempt_disable(); |
Christian König | 5d344f5 | 2019-08-05 14:24:55 +0200 | [diff] [blame] | 355 | rcu_assign_pointer(dst->fence_excl, new); |
| 356 | rcu_assign_pointer(dst->fence, dst_list); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 357 | preempt_enable(); |
| 358 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 359 | dma_resv_list_free(src_list); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 360 | dma_fence_put(old); |
| 361 | |
| 362 | return 0; |
| 363 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 364 | EXPORT_SYMBOL(dma_resv_copy_fences); |
Christian König | 7faf952 | 2017-08-10 13:01:48 -0400 | [diff] [blame] | 365 | |
| 366 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 367 | * dma_resv_get_fences_rcu - Get an object's shared and exclusive |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 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) |
| 374 | * |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 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. |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 378 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 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) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 383 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 384 | struct dma_fence **shared = NULL; |
| 385 | struct dma_fence *fence_excl; |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 386 | unsigned int shared_count; |
| 387 | int ret = 1; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 388 | |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 389 | do { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 390 | struct dma_resv_list *fobj; |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 391 | unsigned int i; |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 392 | size_t sz = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 393 | |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 394 | i = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 395 | |
| 396 | rcu_read_lock(); |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 397 | dma_resv_fences(obj, &fence_excl, &fobj, |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 398 | &shared_count); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 399 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 400 | if (fence_excl && !dma_fence_get_rcu(fence_excl)) |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 401 | goto unlock; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 402 | |
Christian König | a35f2f3 | 2018-01-10 13:53:41 +0100 | [diff] [blame] | 403 | if (fobj) |
| 404 | sz += sizeof(*shared) * fobj->shared_max; |
| 405 | |
| 406 | if (!pfence_excl && fence_excl) |
| 407 | sz += sizeof(*shared); |
| 408 | |
| 409 | if (sz) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 410 | struct dma_fence **nshared; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 411 | |
| 412 | nshared = krealloc(shared, sz, |
| 413 | GFP_NOWAIT | __GFP_NOWARN); |
| 414 | if (!nshared) { |
| 415 | rcu_read_unlock(); |
Chris Wilson | f5b07b0 | 2019-06-04 13:53:23 +0100 | [diff] [blame] | 416 | |
| 417 | dma_fence_put(fence_excl); |
| 418 | fence_excl = NULL; |
| 419 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 420 | nshared = krealloc(shared, sz, GFP_KERNEL); |
| 421 | if (nshared) { |
| 422 | shared = nshared; |
| 423 | continue; |
| 424 | } |
| 425 | |
| 426 | ret = -ENOMEM; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 427 | break; |
| 428 | } |
| 429 | shared = nshared; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 430 | for (i = 0; i < shared_count; ++i) { |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 431 | shared[i] = rcu_dereference(fobj->shared[i]); |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 432 | if (!dma_fence_get_rcu(shared[i])) |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 433 | break; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 434 | } |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 435 | } |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 436 | |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 437 | if (i != shared_count) { |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 438 | while (i--) |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 439 | dma_fence_put(shared[i]); |
| 440 | dma_fence_put(fence_excl); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 441 | goto unlock; |
| 442 | } |
| 443 | |
| 444 | ret = 0; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 445 | unlock: |
| 446 | rcu_read_unlock(); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 447 | } while (ret); |
| 448 | |
Christian König | b8c036d | 2019-08-05 11:49:20 +0200 | [diff] [blame] | 449 | if (pfence_excl) |
| 450 | *pfence_excl = fence_excl; |
| 451 | else if (fence_excl) |
| 452 | shared[++shared_count] = fence_excl; |
| 453 | |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 454 | if (!shared_count) { |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 455 | kfree(shared); |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 456 | shared = NULL; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 457 | } |
Chris Wilson | fedf541 | 2016-08-29 08:08:30 +0100 | [diff] [blame] | 458 | |
| 459 | *pshared_count = shared_count; |
| 460 | *pshared = shared; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 461 | return ret; |
| 462 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 463 | EXPORT_SYMBOL_GPL(dma_resv_get_fences_rcu); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 464 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 465 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 466 | * dma_resv_wait_timeout_rcu - Wait on reservation's objects |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 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 |
| 472 | * |
| 473 | * RETURNS |
| 474 | * Returns -ERESTARTSYS if interrupted, 0 if the wait timed out, or |
| 475 | * greater than zer on success. |
| 476 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 477 | long dma_resv_wait_timeout_rcu(struct dma_resv *obj, |
| 478 | bool wait_all, bool intr, |
| 479 | unsigned long timeout) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 480 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 481 | struct dma_resv_list *fobj; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 482 | struct dma_fence *fence; |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 483 | unsigned shared_count; |
Christian König | 06a66b5 | 2016-11-07 16:16:16 -0500 | [diff] [blame] | 484 | long ret = timeout ? timeout : 1; |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 485 | int i; |
Jammy Zhou | fb8b7d2 | 2015-01-21 18:35:47 +0800 | [diff] [blame] | 486 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 487 | retry: |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 488 | rcu_read_lock(); |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 489 | i = -1; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 490 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 491 | dma_resv_fences(obj, &fence, &fobj, &shared_count); |
Christian König | b88fa00 | 2017-08-10 13:01:49 -0400 | [diff] [blame] | 492 | if (fence && !test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &fence->flags)) { |
| 493 | if (!dma_fence_get_rcu(fence)) |
| 494 | goto unlock_retry; |
| 495 | |
| 496 | if (dma_fence_is_signaled(fence)) { |
| 497 | dma_fence_put(fence); |
| 498 | fence = NULL; |
| 499 | } |
| 500 | |
| 501 | } else { |
| 502 | fence = NULL; |
| 503 | } |
| 504 | |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 505 | if (wait_all) { |
Christian König | 5bffee8 | 2018-01-22 21:00:03 +0100 | [diff] [blame] | 506 | for (i = 0; !fence && i < shared_count; ++i) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 507 | struct dma_fence *lfence = rcu_dereference(fobj->shared[i]); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 508 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 509 | if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, |
| 510 | &lfence->flags)) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 511 | continue; |
| 512 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 513 | if (!dma_fence_get_rcu(lfence)) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 514 | goto unlock_retry; |
| 515 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 516 | if (dma_fence_is_signaled(lfence)) { |
| 517 | dma_fence_put(lfence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 518 | continue; |
| 519 | } |
| 520 | |
| 521 | fence = lfence; |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 526 | rcu_read_unlock(); |
| 527 | if (fence) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 528 | ret = dma_fence_wait_timeout(fence, intr, ret); |
| 529 | dma_fence_put(fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 530 | if (ret > 0 && wait_all && (i + 1 < shared_count)) |
| 531 | goto retry; |
| 532 | } |
| 533 | return ret; |
| 534 | |
| 535 | unlock_retry: |
| 536 | rcu_read_unlock(); |
| 537 | goto retry; |
| 538 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 539 | EXPORT_SYMBOL_GPL(dma_resv_wait_timeout_rcu); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 540 | |
| 541 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 542 | static inline int dma_resv_test_signaled_single(struct dma_fence *passed_fence) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 543 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 544 | struct dma_fence *fence, *lfence = passed_fence; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 545 | int ret = 1; |
| 546 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 547 | if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT, &lfence->flags)) { |
| 548 | fence = dma_fence_get_rcu(lfence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 549 | if (!fence) |
| 550 | return -1; |
| 551 | |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 552 | ret = !!dma_fence_is_signaled(fence); |
| 553 | dma_fence_put(fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 554 | } |
| 555 | return ret; |
| 556 | } |
| 557 | |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 558 | /** |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 559 | * dma_resv_test_signaled_rcu - Test if a reservation object's |
Rob Clark | dad6c39 | 2016-03-31 16:26:51 -0400 | [diff] [blame] | 560 | * fences have been signaled. |
| 561 | * @obj: the reservation object |
| 562 | * @test_all: if true, test all fences, otherwise only test the exclusive |
| 563 | * fence |
| 564 | * |
| 565 | * RETURNS |
| 566 | * true if all fences signaled, else false |
| 567 | */ |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 568 | bool dma_resv_test_signaled_rcu(struct dma_resv *obj, bool test_all) |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 569 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 570 | struct dma_resv_list *fobj; |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 571 | struct dma_fence *fence_excl; |
| 572 | unsigned shared_count; |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 573 | int ret; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 574 | |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 575 | rcu_read_lock(); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 576 | retry: |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 577 | ret = true; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 578 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 579 | dma_resv_fences(obj, &fence_excl, &fobj, &shared_count); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 580 | if (test_all) { |
| 581 | unsigned i; |
| 582 | |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 583 | for (i = 0; i < shared_count; ++i) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 584 | struct dma_fence *fence = rcu_dereference(fobj->shared[i]); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 585 | |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 586 | ret = dma_resv_test_signaled_single(fence); |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 587 | if (ret < 0) |
Chris Wilson | b68d837 | 2016-08-29 08:08:32 +0100 | [diff] [blame] | 588 | goto retry; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 589 | else if (!ret) |
| 590 | break; |
| 591 | } |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 592 | } |
| 593 | |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 594 | if (!shared_count && fence_excl) { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 595 | ret = dma_resv_test_signaled_single(fence_excl); |
Christian König | 67c97fb | 2019-08-06 14:19:33 +0200 | [diff] [blame] | 596 | if (ret < 0) |
| 597 | goto retry; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | rcu_read_unlock(); |
| 601 | return ret; |
Maarten Lankhorst | 3c3b177 | 2014-07-01 12:58:00 +0200 | [diff] [blame] | 602 | } |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame^] | 603 | EXPORT_SYMBOL_GPL(dma_resv_test_signaled_rcu); |