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