Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2009 Jerome Glisse. |
| 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 | * Jerome Glisse <glisse@freedesktop.org> |
| 29 | * Dave Airlie |
| 30 | */ |
| 31 | #include <linux/seq_file.h> |
| 32 | #include <linux/atomic.h> |
| 33 | #include <linux/wait.h> |
| 34 | #include <linux/kref.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/firmware.h> |
| 37 | #include <drm/drmP.h> |
| 38 | #include "amdgpu.h" |
| 39 | #include "amdgpu_trace.h" |
| 40 | |
| 41 | /* |
| 42 | * Fences |
| 43 | * Fences mark an event in the GPUs pipeline and are used |
| 44 | * for GPU/CPU synchronization. When the fence is written, |
| 45 | * it is expected that all buffers associated with that fence |
| 46 | * are no longer in use by the associated ring on the GPU and |
| 47 | * that the the relevant GPU caches have been flushed. |
| 48 | */ |
| 49 | |
| 50 | /** |
| 51 | * amdgpu_fence_write - write a fence value |
| 52 | * |
| 53 | * @ring: ring the fence is associated with |
| 54 | * @seq: sequence number to write |
| 55 | * |
| 56 | * Writes a fence value to memory (all asics). |
| 57 | */ |
| 58 | static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq) |
| 59 | { |
| 60 | struct amdgpu_fence_driver *drv = &ring->fence_drv; |
| 61 | |
| 62 | if (drv->cpu_addr) |
| 63 | *drv->cpu_addr = cpu_to_le32(seq); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * amdgpu_fence_read - read a fence value |
| 68 | * |
| 69 | * @ring: ring the fence is associated with |
| 70 | * |
| 71 | * Reads a fence value from memory (all asics). |
| 72 | * Returns the value of the fence read from memory. |
| 73 | */ |
| 74 | static u32 amdgpu_fence_read(struct amdgpu_ring *ring) |
| 75 | { |
| 76 | struct amdgpu_fence_driver *drv = &ring->fence_drv; |
| 77 | u32 seq = 0; |
| 78 | |
| 79 | if (drv->cpu_addr) |
| 80 | seq = le32_to_cpu(*drv->cpu_addr); |
| 81 | else |
| 82 | seq = lower_32_bits(atomic64_read(&drv->last_seq)); |
| 83 | |
| 84 | return seq; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * amdgpu_fence_schedule_check - schedule lockup check |
| 89 | * |
| 90 | * @ring: pointer to struct amdgpu_ring |
| 91 | * |
| 92 | * Queues a delayed work item to check for lockups. |
| 93 | */ |
| 94 | static void amdgpu_fence_schedule_check(struct amdgpu_ring *ring) |
| 95 | { |
| 96 | /* |
| 97 | * Do not reset the timer here with mod_delayed_work, |
| 98 | * this can livelock in an interaction with TTM delayed destroy. |
| 99 | */ |
| 100 | queue_delayed_work(system_power_efficient_wq, |
| 101 | &ring->fence_drv.lockup_work, |
| 102 | AMDGPU_FENCE_JIFFIES_TIMEOUT); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * amdgpu_fence_emit - emit a fence on the requested ring |
| 107 | * |
| 108 | * @ring: ring the fence is associated with |
| 109 | * @owner: creator of the fence |
| 110 | * @fence: amdgpu fence object |
| 111 | * |
| 112 | * Emits a fence command on the requested ring (all asics). |
| 113 | * Returns 0 on success, -ENOMEM on failure. |
| 114 | */ |
| 115 | int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner, |
| 116 | struct amdgpu_fence **fence) |
| 117 | { |
| 118 | struct amdgpu_device *adev = ring->adev; |
| 119 | |
| 120 | /* we are protected by the ring emission mutex */ |
| 121 | *fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL); |
| 122 | if ((*fence) == NULL) { |
| 123 | return -ENOMEM; |
| 124 | } |
| 125 | (*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx]; |
| 126 | (*fence)->ring = ring; |
| 127 | (*fence)->owner = owner; |
| 128 | fence_init(&(*fence)->base, &amdgpu_fence_ops, |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 129 | &ring->fence_drv.fence_queue.lock, |
| 130 | adev->fence_context + ring->idx, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 131 | (*fence)->seq); |
Chunming Zhou | 890ee23 | 2015-06-01 14:35:03 +0800 | [diff] [blame] | 132 | amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr, |
| 133 | (*fence)->seq, |
| 134 | AMDGPU_FENCE_FLAG_INT); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 139 | * amdgpu_fence_activity - check for fence activity |
| 140 | * |
| 141 | * @ring: pointer to struct amdgpu_ring |
| 142 | * |
| 143 | * Checks the current fence value and calculates the last |
| 144 | * signalled fence value. Returns true if activity occured |
| 145 | * on the ring, and the fence_queue should be waken up. |
| 146 | */ |
| 147 | static bool amdgpu_fence_activity(struct amdgpu_ring *ring) |
| 148 | { |
| 149 | uint64_t seq, last_seq, last_emitted; |
| 150 | unsigned count_loop = 0; |
| 151 | bool wake = false; |
| 152 | |
| 153 | /* Note there is a scenario here for an infinite loop but it's |
| 154 | * very unlikely to happen. For it to happen, the current polling |
| 155 | * process need to be interrupted by another process and another |
| 156 | * process needs to update the last_seq btw the atomic read and |
| 157 | * xchg of the current process. |
| 158 | * |
| 159 | * More over for this to go in infinite loop there need to be |
Jammy Zhou | 86c2b79 | 2015-05-13 22:52:42 +0800 | [diff] [blame] | 160 | * continuously new fence signaled ie amdgpu_fence_read needs |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 161 | * to return a different value each time for both the currently |
| 162 | * polling process and the other process that xchg the last_seq |
| 163 | * btw atomic read and xchg of the current process. And the |
| 164 | * value the other process set as last seq must be higher than |
| 165 | * the seq value we just read. Which means that current process |
Jammy Zhou | 86c2b79 | 2015-05-13 22:52:42 +0800 | [diff] [blame] | 166 | * need to be interrupted after amdgpu_fence_read and before |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 167 | * atomic xchg. |
| 168 | * |
| 169 | * To be even more safe we count the number of time we loop and |
| 170 | * we bail after 10 loop just accepting the fact that we might |
| 171 | * have temporarly set the last_seq not to the true real last |
| 172 | * seq but to an older one. |
| 173 | */ |
| 174 | last_seq = atomic64_read(&ring->fence_drv.last_seq); |
| 175 | do { |
| 176 | last_emitted = ring->fence_drv.sync_seq[ring->idx]; |
| 177 | seq = amdgpu_fence_read(ring); |
| 178 | seq |= last_seq & 0xffffffff00000000LL; |
| 179 | if (seq < last_seq) { |
| 180 | seq &= 0xffffffff; |
| 181 | seq |= last_emitted & 0xffffffff00000000LL; |
| 182 | } |
| 183 | |
| 184 | if (seq <= last_seq || seq > last_emitted) { |
| 185 | break; |
| 186 | } |
| 187 | /* If we loop over we don't want to return without |
| 188 | * checking if a fence is signaled as it means that the |
| 189 | * seq we just read is different from the previous on. |
| 190 | */ |
| 191 | wake = true; |
| 192 | last_seq = seq; |
| 193 | if ((count_loop++) > 10) { |
| 194 | /* We looped over too many time leave with the |
| 195 | * fact that we might have set an older fence |
| 196 | * seq then the current real last seq as signaled |
| 197 | * by the hw. |
| 198 | */ |
| 199 | break; |
| 200 | } |
| 201 | } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq); |
| 202 | |
| 203 | if (seq < last_emitted) |
| 204 | amdgpu_fence_schedule_check(ring); |
| 205 | |
| 206 | return wake; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * amdgpu_fence_check_lockup - check for hardware lockup |
| 211 | * |
| 212 | * @work: delayed work item |
| 213 | * |
| 214 | * Checks for fence activity and if there is none probe |
| 215 | * the hardware if a lockup occured. |
| 216 | */ |
| 217 | static void amdgpu_fence_check_lockup(struct work_struct *work) |
| 218 | { |
| 219 | struct amdgpu_fence_driver *fence_drv; |
| 220 | struct amdgpu_ring *ring; |
| 221 | |
| 222 | fence_drv = container_of(work, struct amdgpu_fence_driver, |
| 223 | lockup_work.work); |
| 224 | ring = fence_drv->ring; |
| 225 | |
Christian König | 0c418f1 | 2015-09-01 15:13:53 +0200 | [diff] [blame] | 226 | if (amdgpu_fence_activity(ring)) |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 227 | wake_up_all(&ring->fence_drv.fence_queue); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /** |
| 231 | * amdgpu_fence_process - process a fence |
| 232 | * |
| 233 | * @adev: amdgpu_device pointer |
| 234 | * @ring: ring index the fence is associated with |
| 235 | * |
| 236 | * Checks the current fence value and wakes the fence queue |
| 237 | * if the sequence number has increased (all asics). |
| 238 | */ |
| 239 | void amdgpu_fence_process(struct amdgpu_ring *ring) |
| 240 | { |
Christian König | 68ed3de | 2015-08-07 15:57:21 +0200 | [diff] [blame] | 241 | if (amdgpu_fence_activity(ring)) |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 242 | wake_up_all(&ring->fence_drv.fence_queue); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | /** |
| 246 | * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled |
| 247 | * |
| 248 | * @ring: ring the fence is associated with |
| 249 | * @seq: sequence number |
| 250 | * |
| 251 | * Check if the last signaled fence sequnce number is >= the requested |
| 252 | * sequence number (all asics). |
| 253 | * Returns true if the fence has signaled (current fence value |
| 254 | * is >= requested value) or false if it has not (current fence |
| 255 | * value is < the requested value. Helper function for |
| 256 | * amdgpu_fence_signaled(). |
| 257 | */ |
| 258 | static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq) |
| 259 | { |
| 260 | if (atomic64_read(&ring->fence_drv.last_seq) >= seq) |
| 261 | return true; |
| 262 | |
| 263 | /* poll new last sequence at least once */ |
| 264 | amdgpu_fence_process(ring); |
| 265 | if (atomic64_read(&ring->fence_drv.last_seq) >= seq) |
| 266 | return true; |
| 267 | |
| 268 | return false; |
| 269 | } |
| 270 | |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 271 | /* |
| 272 | * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal |
| 273 | * @ring: ring to wait on for the seq number |
| 274 | * @seq: seq number wait for |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 275 | * |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 276 | * return value: |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 277 | * 0: seq signaled, and gpu not hang |
| 278 | * -EDEADL: GPU hang detected |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 279 | * -EINVAL: some paramter is not valid |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 280 | */ |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 281 | static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 282 | { |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 283 | bool signaled = false; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 284 | |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 285 | BUG_ON(!ring); |
| 286 | if (seq > ring->fence_drv.sync_seq[ring->idx]) |
| 287 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 288 | |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 289 | if (atomic64_read(&ring->fence_drv.last_seq) >= seq) |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 290 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 291 | |
Christian König | 24372447 | 2015-11-03 11:26:42 +0100 | [diff] [blame] | 292 | amdgpu_fence_schedule_check(ring); |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 293 | wait_event(ring->fence_drv.fence_queue, ( |
Christian König | b7e4dad | 2015-09-01 10:50:26 +0200 | [diff] [blame] | 294 | (signaled = amdgpu_fence_seq_signaled(ring, seq)))); |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 295 | |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 296 | if (signaled) |
| 297 | return 0; |
| 298 | else |
| 299 | return -EDEADLK; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 302 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 303 | * amdgpu_fence_wait_next - wait for the next fence to signal |
| 304 | * |
| 305 | * @adev: amdgpu device pointer |
| 306 | * @ring: ring index the fence is associated with |
| 307 | * |
| 308 | * Wait for the next fence on the requested ring to signal (all asics). |
| 309 | * Returns 0 if the next fence has passed, error for all other cases. |
| 310 | * Caller must hold ring lock. |
| 311 | */ |
| 312 | int amdgpu_fence_wait_next(struct amdgpu_ring *ring) |
| 313 | { |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 314 | uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL; |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 315 | |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 316 | if (seq >= ring->fence_drv.sync_seq[ring->idx]) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 317 | return -ENOENT; |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 318 | |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 319 | return amdgpu_fence_ring_wait_seq(ring, seq); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | /** |
| 323 | * amdgpu_fence_wait_empty - wait for all fences to signal |
| 324 | * |
| 325 | * @adev: amdgpu device pointer |
| 326 | * @ring: ring index the fence is associated with |
| 327 | * |
| 328 | * Wait for all fences on the requested ring to signal (all asics). |
| 329 | * Returns 0 if the fences have passed, error for all other cases. |
| 330 | * Caller must hold ring lock. |
| 331 | */ |
| 332 | int amdgpu_fence_wait_empty(struct amdgpu_ring *ring) |
| 333 | { |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 334 | uint64_t seq = ring->fence_drv.sync_seq[ring->idx]; |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 335 | |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 336 | if (!seq) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 337 | return 0; |
| 338 | |
Christian König | 00d2a2b | 2015-08-07 16:15:36 +0200 | [diff] [blame] | 339 | return amdgpu_fence_ring_wait_seq(ring, seq); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 343 | * amdgpu_fence_count_emitted - get the count of emitted fences |
| 344 | * |
| 345 | * @ring: ring the fence is associated with |
| 346 | * |
| 347 | * Get the number of fences emitted on the requested ring (all asics). |
| 348 | * Returns the number of emitted fences on the ring. Used by the |
| 349 | * dynpm code to ring track activity. |
| 350 | */ |
| 351 | unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring) |
| 352 | { |
| 353 | uint64_t emitted; |
| 354 | |
| 355 | /* We are not protected by ring lock when reading the last sequence |
| 356 | * but it's ok to report slightly wrong fence count here. |
| 357 | */ |
| 358 | amdgpu_fence_process(ring); |
| 359 | emitted = ring->fence_drv.sync_seq[ring->idx] |
| 360 | - atomic64_read(&ring->fence_drv.last_seq); |
| 361 | /* to avoid 32bits warp around */ |
| 362 | if (emitted > 0x10000000) |
| 363 | emitted = 0x10000000; |
| 364 | |
| 365 | return (unsigned)emitted; |
| 366 | } |
| 367 | |
| 368 | /** |
| 369 | * amdgpu_fence_need_sync - do we need a semaphore |
| 370 | * |
| 371 | * @fence: amdgpu fence object |
| 372 | * @dst_ring: which ring to check against |
| 373 | * |
| 374 | * Check if the fence needs to be synced against another ring |
| 375 | * (all asics). If so, we need to emit a semaphore. |
| 376 | * Returns true if we need to sync with another ring, false if |
| 377 | * not. |
| 378 | */ |
| 379 | bool amdgpu_fence_need_sync(struct amdgpu_fence *fence, |
| 380 | struct amdgpu_ring *dst_ring) |
| 381 | { |
| 382 | struct amdgpu_fence_driver *fdrv; |
| 383 | |
| 384 | if (!fence) |
| 385 | return false; |
| 386 | |
| 387 | if (fence->ring == dst_ring) |
| 388 | return false; |
| 389 | |
| 390 | /* we are protected by the ring mutex */ |
| 391 | fdrv = &dst_ring->fence_drv; |
| 392 | if (fence->seq <= fdrv->sync_seq[fence->ring->idx]) |
| 393 | return false; |
| 394 | |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * amdgpu_fence_note_sync - record the sync point |
| 400 | * |
| 401 | * @fence: amdgpu fence object |
| 402 | * @dst_ring: which ring to check against |
| 403 | * |
| 404 | * Note the sequence number at which point the fence will |
| 405 | * be synced with the requested ring (all asics). |
| 406 | */ |
| 407 | void amdgpu_fence_note_sync(struct amdgpu_fence *fence, |
| 408 | struct amdgpu_ring *dst_ring) |
| 409 | { |
| 410 | struct amdgpu_fence_driver *dst, *src; |
| 411 | unsigned i; |
| 412 | |
| 413 | if (!fence) |
| 414 | return; |
| 415 | |
| 416 | if (fence->ring == dst_ring) |
| 417 | return; |
| 418 | |
| 419 | /* we are protected by the ring mutex */ |
| 420 | src = &fence->ring->fence_drv; |
| 421 | dst = &dst_ring->fence_drv; |
| 422 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
| 423 | if (i == dst_ring->idx) |
| 424 | continue; |
| 425 | |
| 426 | dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * amdgpu_fence_driver_start_ring - make the fence driver |
| 432 | * ready for use on the requested ring. |
| 433 | * |
| 434 | * @ring: ring to start the fence driver on |
| 435 | * @irq_src: interrupt source to use for this ring |
| 436 | * @irq_type: interrupt type to use for this ring |
| 437 | * |
| 438 | * Make the fence driver ready for processing (all asics). |
| 439 | * Not all asics have all rings, so each asic will only |
| 440 | * start the fence driver on the rings it has. |
| 441 | * Returns 0 for success, errors for failure. |
| 442 | */ |
| 443 | int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring, |
| 444 | struct amdgpu_irq_src *irq_src, |
| 445 | unsigned irq_type) |
| 446 | { |
| 447 | struct amdgpu_device *adev = ring->adev; |
| 448 | uint64_t index; |
| 449 | |
| 450 | if (ring != &adev->uvd.ring) { |
| 451 | ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs]; |
| 452 | ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4); |
| 453 | } else { |
| 454 | /* put fence directly behind firmware */ |
| 455 | index = ALIGN(adev->uvd.fw->size, 8); |
| 456 | ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index; |
| 457 | ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index; |
| 458 | } |
| 459 | amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq)); |
Chunming Zhou | c6a4079 | 2015-06-01 14:14:32 +0800 | [diff] [blame] | 460 | amdgpu_irq_get(adev, irq_src, irq_type); |
| 461 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 462 | ring->fence_drv.irq_src = irq_src; |
| 463 | ring->fence_drv.irq_type = irq_type; |
Chunming Zhou | c6a4079 | 2015-06-01 14:14:32 +0800 | [diff] [blame] | 464 | ring->fence_drv.initialized = true; |
| 465 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 466 | dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, " |
| 467 | "cpu addr 0x%p\n", ring->idx, |
| 468 | ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * amdgpu_fence_driver_init_ring - init the fence driver |
| 474 | * for the requested ring. |
| 475 | * |
| 476 | * @ring: ring to init the fence driver on |
| 477 | * |
| 478 | * Init the fence driver for the requested ring (all asics). |
| 479 | * Helper function for amdgpu_fence_driver_init(). |
| 480 | */ |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 481 | int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 482 | { |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 483 | int i, r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 484 | |
| 485 | ring->fence_drv.cpu_addr = NULL; |
| 486 | ring->fence_drv.gpu_addr = 0; |
| 487 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) |
| 488 | ring->fence_drv.sync_seq[i] = 0; |
| 489 | |
| 490 | atomic64_set(&ring->fence_drv.last_seq, 0); |
| 491 | ring->fence_drv.initialized = false; |
| 492 | |
| 493 | INIT_DELAYED_WORK(&ring->fence_drv.lockup_work, |
| 494 | amdgpu_fence_check_lockup); |
| 495 | ring->fence_drv.ring = ring; |
Alex Deucher | b80d847 | 2015-08-16 22:55:02 -0400 | [diff] [blame] | 496 | |
Christian König | 5ec92a7 | 2015-09-07 18:43:02 +0200 | [diff] [blame] | 497 | init_waitqueue_head(&ring->fence_drv.fence_queue); |
| 498 | |
Alex Deucher | b80d847 | 2015-08-16 22:55:02 -0400 | [diff] [blame] | 499 | if (amdgpu_enable_scheduler) { |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 500 | long timeout = msecs_to_jiffies(amdgpu_lockup_timeout); |
| 501 | if (timeout == 0) { |
| 502 | /* |
| 503 | * FIXME: |
| 504 | * Delayed workqueue cannot use it directly, |
| 505 | * so the scheduler will not use delayed workqueue if |
| 506 | * MAX_SCHEDULE_TIMEOUT is set. |
| 507 | * Currently keep it simple and silly. |
| 508 | */ |
| 509 | timeout = MAX_SCHEDULE_TIMEOUT; |
| 510 | } |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 511 | r = amd_sched_init(&ring->sched, &amdgpu_sched_ops, |
Junwei Zhang | 2440ff2 | 2015-10-10 08:48:42 +0800 | [diff] [blame] | 512 | amdgpu_sched_hw_submission, |
| 513 | timeout, ring->name); |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 514 | if (r) { |
| 515 | DRM_ERROR("Failed to create scheduler on ring %s.\n", |
| 516 | ring->name); |
| 517 | return r; |
| 518 | } |
Alex Deucher | b80d847 | 2015-08-16 22:55:02 -0400 | [diff] [blame] | 519 | } |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 520 | |
| 521 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | /** |
| 525 | * amdgpu_fence_driver_init - init the fence driver |
| 526 | * for all possible rings. |
| 527 | * |
| 528 | * @adev: amdgpu device pointer |
| 529 | * |
| 530 | * Init the fence driver for all possible rings (all asics). |
| 531 | * Not all asics have all rings, so each asic will only |
| 532 | * start the fence driver on the rings it has using |
| 533 | * amdgpu_fence_driver_start_ring(). |
| 534 | * Returns 0 for success. |
| 535 | */ |
| 536 | int amdgpu_fence_driver_init(struct amdgpu_device *adev) |
| 537 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 538 | if (amdgpu_debugfs_fence_init(adev)) |
| 539 | dev_err(adev->dev, "fence debugfs file creation failed\n"); |
| 540 | |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | /** |
| 545 | * amdgpu_fence_driver_fini - tear down the fence driver |
| 546 | * for all possible rings. |
| 547 | * |
| 548 | * @adev: amdgpu device pointer |
| 549 | * |
| 550 | * Tear down the fence driver for all possible rings (all asics). |
| 551 | */ |
| 552 | void amdgpu_fence_driver_fini(struct amdgpu_device *adev) |
| 553 | { |
| 554 | int i, r; |
| 555 | |
| 556 | mutex_lock(&adev->ring_lock); |
| 557 | for (i = 0; i < AMDGPU_MAX_RINGS; i++) { |
| 558 | struct amdgpu_ring *ring = adev->rings[i]; |
| 559 | if (!ring || !ring->fence_drv.initialized) |
| 560 | continue; |
| 561 | r = amdgpu_fence_wait_empty(ring); |
| 562 | if (r) { |
| 563 | /* no need to trigger GPU reset as we are unloading */ |
| 564 | amdgpu_fence_driver_force_completion(adev); |
| 565 | } |
monk.liu | 7f06c23 | 2015-07-30 18:28:12 +0800 | [diff] [blame] | 566 | wake_up_all(&ring->fence_drv.fence_queue); |
Chunming Zhou | c6a4079 | 2015-06-01 14:14:32 +0800 | [diff] [blame] | 567 | amdgpu_irq_put(adev, ring->fence_drv.irq_src, |
| 568 | ring->fence_drv.irq_type); |
Christian König | 4f839a2 | 2015-09-08 20:22:31 +0200 | [diff] [blame] | 569 | amd_sched_fini(&ring->sched); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 570 | ring->fence_drv.initialized = false; |
| 571 | } |
| 572 | mutex_unlock(&adev->ring_lock); |
| 573 | } |
| 574 | |
| 575 | /** |
Alex Deucher | 5ceb54c | 2015-08-05 12:41:48 -0400 | [diff] [blame] | 576 | * amdgpu_fence_driver_suspend - suspend the fence driver |
| 577 | * for all possible rings. |
| 578 | * |
| 579 | * @adev: amdgpu device pointer |
| 580 | * |
| 581 | * Suspend the fence driver for all possible rings (all asics). |
| 582 | */ |
| 583 | void amdgpu_fence_driver_suspend(struct amdgpu_device *adev) |
| 584 | { |
| 585 | int i, r; |
| 586 | |
| 587 | mutex_lock(&adev->ring_lock); |
| 588 | for (i = 0; i < AMDGPU_MAX_RINGS; i++) { |
| 589 | struct amdgpu_ring *ring = adev->rings[i]; |
| 590 | if (!ring || !ring->fence_drv.initialized) |
| 591 | continue; |
| 592 | |
| 593 | /* wait for gpu to finish processing current batch */ |
| 594 | r = amdgpu_fence_wait_empty(ring); |
| 595 | if (r) { |
| 596 | /* delay GPU reset to resume */ |
| 597 | amdgpu_fence_driver_force_completion(adev); |
| 598 | } |
| 599 | |
| 600 | /* disable the interrupt */ |
| 601 | amdgpu_irq_put(adev, ring->fence_drv.irq_src, |
| 602 | ring->fence_drv.irq_type); |
| 603 | } |
| 604 | mutex_unlock(&adev->ring_lock); |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * amdgpu_fence_driver_resume - resume the fence driver |
| 609 | * for all possible rings. |
| 610 | * |
| 611 | * @adev: amdgpu device pointer |
| 612 | * |
| 613 | * Resume the fence driver for all possible rings (all asics). |
| 614 | * Not all asics have all rings, so each asic will only |
| 615 | * start the fence driver on the rings it has using |
| 616 | * amdgpu_fence_driver_start_ring(). |
| 617 | * Returns 0 for success. |
| 618 | */ |
| 619 | void amdgpu_fence_driver_resume(struct amdgpu_device *adev) |
| 620 | { |
| 621 | int i; |
| 622 | |
| 623 | mutex_lock(&adev->ring_lock); |
| 624 | for (i = 0; i < AMDGPU_MAX_RINGS; i++) { |
| 625 | struct amdgpu_ring *ring = adev->rings[i]; |
| 626 | if (!ring || !ring->fence_drv.initialized) |
| 627 | continue; |
| 628 | |
| 629 | /* enable the interrupt */ |
| 630 | amdgpu_irq_get(adev, ring->fence_drv.irq_src, |
| 631 | ring->fence_drv.irq_type); |
| 632 | } |
| 633 | mutex_unlock(&adev->ring_lock); |
| 634 | } |
| 635 | |
| 636 | /** |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 637 | * amdgpu_fence_driver_force_completion - force all fence waiter to complete |
| 638 | * |
| 639 | * @adev: amdgpu device pointer |
| 640 | * |
| 641 | * In case of GPU reset failure make sure no process keep waiting on fence |
| 642 | * that will never complete. |
| 643 | */ |
| 644 | void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev) |
| 645 | { |
| 646 | int i; |
| 647 | |
| 648 | for (i = 0; i < AMDGPU_MAX_RINGS; i++) { |
| 649 | struct amdgpu_ring *ring = adev->rings[i]; |
| 650 | if (!ring || !ring->fence_drv.initialized) |
| 651 | continue; |
| 652 | |
| 653 | amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]); |
| 654 | } |
| 655 | } |
| 656 | |
Christian König | a95e264 | 2015-11-03 12:21:57 +0100 | [diff] [blame] | 657 | /* |
| 658 | * Common fence implementation |
| 659 | */ |
| 660 | |
| 661 | static const char *amdgpu_fence_get_driver_name(struct fence *fence) |
| 662 | { |
| 663 | return "amdgpu"; |
| 664 | } |
| 665 | |
| 666 | static const char *amdgpu_fence_get_timeline_name(struct fence *f) |
| 667 | { |
| 668 | struct amdgpu_fence *fence = to_amdgpu_fence(f); |
| 669 | return (const char *)fence->ring->name; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * amdgpu_fence_is_signaled - test if fence is signaled |
| 674 | * |
| 675 | * @f: fence to test |
| 676 | * |
| 677 | * Test the fence sequence number if it is already signaled. If it isn't |
| 678 | * signaled start fence processing. Returns True if the fence is signaled. |
| 679 | */ |
| 680 | static bool amdgpu_fence_is_signaled(struct fence *f) |
| 681 | { |
| 682 | struct amdgpu_fence *fence = to_amdgpu_fence(f); |
| 683 | struct amdgpu_ring *ring = fence->ring; |
| 684 | |
| 685 | if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) |
| 686 | return true; |
| 687 | |
| 688 | amdgpu_fence_process(ring); |
| 689 | |
| 690 | if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) |
| 691 | return true; |
| 692 | |
| 693 | return false; |
| 694 | } |
| 695 | |
| 696 | /** |
| 697 | * amdgpu_fence_check_signaled - callback from fence_queue |
| 698 | * |
| 699 | * this function is called with fence_queue lock held, which is also used |
| 700 | * for the fence locking itself, so unlocked variants are used for |
| 701 | * fence_signal, and remove_wait_queue. |
| 702 | */ |
| 703 | static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key) |
| 704 | { |
| 705 | struct amdgpu_fence *fence; |
| 706 | struct amdgpu_device *adev; |
| 707 | u64 seq; |
| 708 | int ret; |
| 709 | |
| 710 | fence = container_of(wait, struct amdgpu_fence, fence_wake); |
| 711 | adev = fence->ring->adev; |
| 712 | |
| 713 | /* |
| 714 | * We cannot use amdgpu_fence_process here because we're already |
| 715 | * in the waitqueue, in a call from wake_up_all. |
| 716 | */ |
| 717 | seq = atomic64_read(&fence->ring->fence_drv.last_seq); |
| 718 | if (seq >= fence->seq) { |
| 719 | ret = fence_signal_locked(&fence->base); |
| 720 | if (!ret) |
| 721 | FENCE_TRACE(&fence->base, "signaled from irq context\n"); |
| 722 | else |
| 723 | FENCE_TRACE(&fence->base, "was already signaled\n"); |
| 724 | |
| 725 | __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake); |
| 726 | fence_put(&fence->base); |
| 727 | } else |
| 728 | FENCE_TRACE(&fence->base, "pending\n"); |
| 729 | return 0; |
| 730 | } |
| 731 | |
| 732 | /** |
| 733 | * amdgpu_fence_enable_signaling - enable signalling on fence |
| 734 | * @fence: fence |
| 735 | * |
| 736 | * This function is called with fence_queue lock held, and adds a callback |
| 737 | * to fence_queue that checks if this fence is signaled, and if so it |
| 738 | * signals the fence and removes itself. |
| 739 | */ |
| 740 | static bool amdgpu_fence_enable_signaling(struct fence *f) |
| 741 | { |
| 742 | struct amdgpu_fence *fence = to_amdgpu_fence(f); |
| 743 | struct amdgpu_ring *ring = fence->ring; |
| 744 | |
| 745 | if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq) |
| 746 | return false; |
| 747 | |
| 748 | fence->fence_wake.flags = 0; |
| 749 | fence->fence_wake.private = NULL; |
| 750 | fence->fence_wake.func = amdgpu_fence_check_signaled; |
| 751 | __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake); |
| 752 | fence_get(f); |
| 753 | amdgpu_fence_schedule_check(ring); |
| 754 | FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx); |
| 755 | return true; |
| 756 | } |
| 757 | |
| 758 | const struct fence_ops amdgpu_fence_ops = { |
| 759 | .get_driver_name = amdgpu_fence_get_driver_name, |
| 760 | .get_timeline_name = amdgpu_fence_get_timeline_name, |
| 761 | .enable_signaling = amdgpu_fence_enable_signaling, |
| 762 | .signaled = amdgpu_fence_is_signaled, |
| 763 | .wait = fence_default_wait, |
| 764 | .release = NULL, |
| 765 | }; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 766 | |
| 767 | /* |
| 768 | * Fence debugfs |
| 769 | */ |
| 770 | #if defined(CONFIG_DEBUG_FS) |
| 771 | static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data) |
| 772 | { |
| 773 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
| 774 | struct drm_device *dev = node->minor->dev; |
| 775 | struct amdgpu_device *adev = dev->dev_private; |
| 776 | int i, j; |
| 777 | |
| 778 | for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { |
| 779 | struct amdgpu_ring *ring = adev->rings[i]; |
| 780 | if (!ring || !ring->fence_drv.initialized) |
| 781 | continue; |
| 782 | |
| 783 | amdgpu_fence_process(ring); |
| 784 | |
Christian König | 344c19f | 2015-06-02 15:47:16 +0200 | [diff] [blame] | 785 | seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 786 | seq_printf(m, "Last signaled fence 0x%016llx\n", |
| 787 | (unsigned long long)atomic64_read(&ring->fence_drv.last_seq)); |
| 788 | seq_printf(m, "Last emitted 0x%016llx\n", |
| 789 | ring->fence_drv.sync_seq[i]); |
| 790 | |
| 791 | for (j = 0; j < AMDGPU_MAX_RINGS; ++j) { |
| 792 | struct amdgpu_ring *other = adev->rings[j]; |
Christian König | 344c19f | 2015-06-02 15:47:16 +0200 | [diff] [blame] | 793 | if (i != j && other && other->fence_drv.initialized && |
| 794 | ring->fence_drv.sync_seq[j]) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 795 | seq_printf(m, "Last sync to ring %d 0x%016llx\n", |
| 796 | j, ring->fence_drv.sync_seq[j]); |
| 797 | } |
| 798 | } |
| 799 | return 0; |
| 800 | } |
| 801 | |
| 802 | static struct drm_info_list amdgpu_debugfs_fence_list[] = { |
| 803 | {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL}, |
| 804 | }; |
| 805 | #endif |
| 806 | |
| 807 | int amdgpu_debugfs_fence_init(struct amdgpu_device *adev) |
| 808 | { |
| 809 | #if defined(CONFIG_DEBUG_FS) |
| 810 | return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1); |
| 811 | #else |
| 812 | return 0; |
| 813 | #endif |
| 814 | } |
| 815 | |