Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Advanced Micro Devices, Inc. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 6 | * copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sub license, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
| 12 | * |
| 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 16 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 17 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 18 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 19 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | * |
| 21 | * The above copyright notice and this permission notice (including the |
| 22 | * next paragraph) shall be included in all copies or substantial portions |
| 23 | * of the Software. |
| 24 | * |
| 25 | */ |
| 26 | /* |
| 27 | * Authors: |
| 28 | * Christian König <christian.koenig@amd.com> |
| 29 | */ |
| 30 | |
| 31 | #include <drm/drmP.h> |
| 32 | #include "amdgpu.h" |
| 33 | #include "amdgpu_trace.h" |
| 34 | |
Christian König | f91b3a6 | 2015-08-20 14:47:40 +0800 | [diff] [blame] | 35 | struct amdgpu_sync_entry { |
| 36 | struct hlist_node node; |
| 37 | struct fence *fence; |
| 38 | }; |
| 39 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 40 | /** |
| 41 | * amdgpu_sync_create - zero init sync object |
| 42 | * |
| 43 | * @sync: sync object to initialize |
| 44 | * |
| 45 | * Just clear the sync object for now. |
| 46 | */ |
| 47 | void amdgpu_sync_create(struct amdgpu_sync *sync) |
| 48 | { |
Christian König | f91b3a6 | 2015-08-20 14:47:40 +0800 | [diff] [blame] | 49 | hash_init(sync->fences); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 50 | sync->last_vm_update = NULL; |
| 51 | } |
| 52 | |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 53 | static bool amdgpu_sync_same_dev(struct amdgpu_device *adev, struct fence *f) |
| 54 | { |
| 55 | struct amdgpu_fence *a_fence = to_amdgpu_fence(f); |
| 56 | struct amd_sched_fence *s_fence = to_amd_sched_fence(f); |
| 57 | |
| 58 | if (a_fence) |
| 59 | return a_fence->ring->adev == adev; |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 60 | |
| 61 | if (s_fence) { |
| 62 | struct amdgpu_ring *ring; |
| 63 | |
| 64 | ring = container_of(s_fence->sched, struct amdgpu_ring, sched); |
| 65 | return ring->adev == adev; |
| 66 | } |
| 67 | |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 68 | return false; |
| 69 | } |
| 70 | |
| 71 | static bool amdgpu_sync_test_owner(struct fence *f, void *owner) |
| 72 | { |
| 73 | struct amdgpu_fence *a_fence = to_amdgpu_fence(f); |
| 74 | struct amd_sched_fence *s_fence = to_amd_sched_fence(f); |
| 75 | if (s_fence) |
| 76 | return s_fence->owner == owner; |
| 77 | if (a_fence) |
| 78 | return a_fence->owner == owner; |
| 79 | return false; |
| 80 | } |
| 81 | |
Christian König | 2423386 | 2015-10-22 10:53:16 +0200 | [diff] [blame] | 82 | static void amdgpu_sync_keep_later(struct fence **keep, struct fence *fence) |
| 83 | { |
| 84 | if (*keep && fence_is_later(*keep, fence)) |
| 85 | return; |
| 86 | |
| 87 | fence_put(*keep); |
| 88 | *keep = fence_get(fence); |
| 89 | } |
| 90 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 91 | /** |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 92 | * amdgpu_sync_fence - remember to sync to this fence |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 93 | * |
| 94 | * @sync: sync object to add fence to |
| 95 | * @fence: fence to sync to |
| 96 | * |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 97 | */ |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 98 | int amdgpu_sync_fence(struct amdgpu_device *adev, struct amdgpu_sync *sync, |
| 99 | struct fence *f) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 100 | { |
Christian König | f91b3a6 | 2015-08-20 14:47:40 +0800 | [diff] [blame] | 101 | struct amdgpu_sync_entry *e; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 102 | |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 103 | if (!f) |
| 104 | return 0; |
| 105 | |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 106 | if (amdgpu_sync_same_dev(adev, f) && |
Christian König | 2423386 | 2015-10-22 10:53:16 +0200 | [diff] [blame] | 107 | amdgpu_sync_test_owner(f, AMDGPU_FENCE_OWNER_VM)) |
| 108 | amdgpu_sync_keep_later(&sync->last_vm_update, f); |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 109 | |
Christian König | 046c12c | 2016-01-18 14:49:45 +0100 | [diff] [blame] | 110 | hash_for_each_possible(sync->fences, e, node, f->context) { |
| 111 | if (unlikely(e->fence->context != f->context)) |
| 112 | continue; |
Christian König | 2423386 | 2015-10-22 10:53:16 +0200 | [diff] [blame] | 113 | |
Christian König | 046c12c | 2016-01-18 14:49:45 +0100 | [diff] [blame] | 114 | amdgpu_sync_keep_later(&e->fence, f); |
Christian König | f91b3a6 | 2015-08-20 14:47:40 +0800 | [diff] [blame] | 115 | return 0; |
| 116 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 117 | |
Christian König | 046c12c | 2016-01-18 14:49:45 +0100 | [diff] [blame] | 118 | e = kmalloc(sizeof(struct amdgpu_sync_entry), GFP_KERNEL); |
| 119 | if (!e) |
| 120 | return -ENOMEM; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 121 | |
Christian König | 046c12c | 2016-01-18 14:49:45 +0100 | [diff] [blame] | 122 | hash_add(sync->fences, &e->node, f->context); |
| 123 | e->fence = fence_get(f); |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 124 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Chunming Zhou | 423a948 | 2015-08-24 16:59:54 +0800 | [diff] [blame] | 127 | static void *amdgpu_sync_get_owner(struct fence *f) |
| 128 | { |
| 129 | struct amdgpu_fence *a_fence = to_amdgpu_fence(f); |
| 130 | struct amd_sched_fence *s_fence = to_amd_sched_fence(f); |
| 131 | |
| 132 | if (s_fence) |
| 133 | return s_fence->owner; |
| 134 | else if (a_fence) |
| 135 | return a_fence->owner; |
| 136 | return AMDGPU_FENCE_OWNER_UNDEFINED; |
| 137 | } |
| 138 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 139 | /** |
Chunming Zhou | 2f4b940 | 2016-01-15 11:05:21 +0800 | [diff] [blame] | 140 | * amdgpu_sync_resv - sync to a reservation object |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 141 | * |
| 142 | * @sync: sync object to add fences from reservation object to |
| 143 | * @resv: reservation object with embedded fence |
| 144 | * @shared: true if we should only sync to the exclusive fence |
| 145 | * |
Chunming Zhou | 2f4b940 | 2016-01-15 11:05:21 +0800 | [diff] [blame] | 146 | * Sync to the fence |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 147 | */ |
| 148 | int amdgpu_sync_resv(struct amdgpu_device *adev, |
| 149 | struct amdgpu_sync *sync, |
| 150 | struct reservation_object *resv, |
| 151 | void *owner) |
| 152 | { |
| 153 | struct reservation_object_list *flist; |
| 154 | struct fence *f; |
Chunming Zhou | 423a948 | 2015-08-24 16:59:54 +0800 | [diff] [blame] | 155 | void *fence_owner; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 156 | unsigned i; |
| 157 | int r = 0; |
| 158 | |
Jammy Zhou | 4b09530 | 2015-05-12 23:17:19 +0800 | [diff] [blame] | 159 | if (resv == NULL) |
| 160 | return -EINVAL; |
| 161 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 162 | /* always sync to the exclusive fence */ |
| 163 | f = reservation_object_get_excl(resv); |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 164 | r = amdgpu_sync_fence(adev, sync, f); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 165 | |
| 166 | flist = reservation_object_get_list(resv); |
| 167 | if (!flist || r) |
| 168 | return r; |
| 169 | |
| 170 | for (i = 0; i < flist->shared_count; ++i) { |
| 171 | f = rcu_dereference_protected(flist->shared[i], |
| 172 | reservation_object_held(resv)); |
Chunming Zhou | 423a948 | 2015-08-24 16:59:54 +0800 | [diff] [blame] | 173 | if (amdgpu_sync_same_dev(adev, f)) { |
Christian König | 1d3897e | 2015-07-27 15:40:35 +0200 | [diff] [blame] | 174 | /* VM updates are only interesting |
| 175 | * for other VM updates and moves. |
| 176 | */ |
Chunming Zhou | 423a948 | 2015-08-24 16:59:54 +0800 | [diff] [blame] | 177 | fence_owner = amdgpu_sync_get_owner(f); |
Christian König | 7a91d6c | 2015-10-27 17:28:24 +0100 | [diff] [blame] | 178 | if ((owner != AMDGPU_FENCE_OWNER_UNDEFINED) && |
| 179 | (fence_owner != AMDGPU_FENCE_OWNER_UNDEFINED) && |
Christian König | 1d3897e | 2015-07-27 15:40:35 +0200 | [diff] [blame] | 180 | ((owner == AMDGPU_FENCE_OWNER_VM) != |
Chunming Zhou | 423a948 | 2015-08-24 16:59:54 +0800 | [diff] [blame] | 181 | (fence_owner == AMDGPU_FENCE_OWNER_VM))) |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 182 | continue; |
| 183 | |
Christian König | 1d3897e | 2015-07-27 15:40:35 +0200 | [diff] [blame] | 184 | /* Ignore fence from the same owner as |
| 185 | * long as it isn't undefined. |
| 186 | */ |
| 187 | if (owner != AMDGPU_FENCE_OWNER_UNDEFINED && |
Chunming Zhou | 423a948 | 2015-08-24 16:59:54 +0800 | [diff] [blame] | 188 | fence_owner == owner) |
Christian König | 1d3897e | 2015-07-27 15:40:35 +0200 | [diff] [blame] | 189 | continue; |
| 190 | } |
| 191 | |
Christian König | 91e1a52 | 2015-07-06 22:06:40 +0200 | [diff] [blame] | 192 | r = amdgpu_sync_fence(adev, sync, f); |
| 193 | if (r) |
| 194 | break; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 195 | } |
| 196 | return r; |
| 197 | } |
| 198 | |
Christian König | e61235d | 2015-08-25 11:05:36 +0200 | [diff] [blame] | 199 | struct fence *amdgpu_sync_get_fence(struct amdgpu_sync *sync) |
| 200 | { |
| 201 | struct amdgpu_sync_entry *e; |
| 202 | struct hlist_node *tmp; |
| 203 | struct fence *f; |
| 204 | int i; |
| 205 | |
| 206 | hash_for_each_safe(sync->fences, i, tmp, e, node) { |
| 207 | |
| 208 | f = e->fence; |
| 209 | |
| 210 | hash_del(&e->node); |
| 211 | kfree(e); |
| 212 | |
| 213 | if (!fence_is_signaled(f)) |
| 214 | return f; |
| 215 | |
| 216 | fence_put(f); |
| 217 | } |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Christian König | f91b3a6 | 2015-08-20 14:47:40 +0800 | [diff] [blame] | 221 | int amdgpu_sync_wait(struct amdgpu_sync *sync) |
| 222 | { |
| 223 | struct amdgpu_sync_entry *e; |
| 224 | struct hlist_node *tmp; |
| 225 | int i, r; |
| 226 | |
| 227 | hash_for_each_safe(sync->fences, i, tmp, e, node) { |
| 228 | r = fence_wait(e->fence, false); |
| 229 | if (r) |
| 230 | return r; |
| 231 | |
| 232 | hash_del(&e->node); |
| 233 | fence_put(e->fence); |
| 234 | kfree(e); |
| 235 | } |
Christian König | 3daea9e3d | 2015-09-05 11:12:27 +0200 | [diff] [blame] | 236 | |
Christian König | f91b3a6 | 2015-08-20 14:47:40 +0800 | [diff] [blame] | 237 | return 0; |
| 238 | } |
| 239 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 240 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 241 | * amdgpu_sync_free - free the sync object |
| 242 | * |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 243 | * @sync: sync object to use |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 244 | * |
Chunming Zhou | 2f4b940 | 2016-01-15 11:05:21 +0800 | [diff] [blame] | 245 | * Free the sync object. |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 246 | */ |
Christian König | 8a8f0b4 | 2016-02-03 15:11:39 +0100 | [diff] [blame^] | 247 | void amdgpu_sync_free(struct amdgpu_sync *sync) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 248 | { |
Christian König | f91b3a6 | 2015-08-20 14:47:40 +0800 | [diff] [blame] | 249 | struct amdgpu_sync_entry *e; |
| 250 | struct hlist_node *tmp; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 251 | unsigned i; |
| 252 | |
Christian König | f91b3a6 | 2015-08-20 14:47:40 +0800 | [diff] [blame] | 253 | hash_for_each_safe(sync->fences, i, tmp, e, node) { |
| 254 | hash_del(&e->node); |
| 255 | fence_put(e->fence); |
| 256 | kfree(e); |
| 257 | } |
| 258 | |
Chunming Zhou | 3c62338 | 2015-08-20 18:33:59 +0800 | [diff] [blame] | 259 | fence_put(sync->last_vm_update); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 260 | } |