blob: eb419791d1b278a9e8b94392335a493d6cc26f75 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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 */
58static 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 */
74static 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 */
94static 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 */
115int 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.liu7f06c232015-07-30 18:28:12 +0800129 &ring->fence_drv.fence_queue.lock,
130 adev->fence_context + ring->idx,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 (*fence)->seq);
Chunming Zhou890ee232015-06-01 14:35:03 +0800132 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
133 (*fence)->seq,
134 AMDGPU_FENCE_FLAG_INT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
136 return 0;
137}
138
139/**
140 * amdgpu_fence_check_signaled - callback from fence_queue
141 *
142 * this function is called with fence_queue lock held, which is also used
143 * for the fence locking itself, so unlocked variants are used for
144 * fence_signal, and remove_wait_queue.
145 */
146static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
147{
148 struct amdgpu_fence *fence;
149 struct amdgpu_device *adev;
150 u64 seq;
151 int ret;
152
153 fence = container_of(wait, struct amdgpu_fence, fence_wake);
154 adev = fence->ring->adev;
155
156 /*
157 * We cannot use amdgpu_fence_process here because we're already
158 * in the waitqueue, in a call from wake_up_all.
159 */
160 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
161 if (seq >= fence->seq) {
162 ret = fence_signal_locked(&fence->base);
163 if (!ret)
164 FENCE_TRACE(&fence->base, "signaled from irq context\n");
165 else
166 FENCE_TRACE(&fence->base, "was already signaled\n");
167
monk.liu7f06c232015-07-30 18:28:12 +0800168 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 fence_put(&fence->base);
170 } else
171 FENCE_TRACE(&fence->base, "pending\n");
172 return 0;
173}
174
175/**
176 * amdgpu_fence_activity - check for fence activity
177 *
178 * @ring: pointer to struct amdgpu_ring
179 *
180 * Checks the current fence value and calculates the last
181 * signalled fence value. Returns true if activity occured
182 * on the ring, and the fence_queue should be waken up.
183 */
184static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
185{
186 uint64_t seq, last_seq, last_emitted;
187 unsigned count_loop = 0;
188 bool wake = false;
189
190 /* Note there is a scenario here for an infinite loop but it's
191 * very unlikely to happen. For it to happen, the current polling
192 * process need to be interrupted by another process and another
193 * process needs to update the last_seq btw the atomic read and
194 * xchg of the current process.
195 *
196 * More over for this to go in infinite loop there need to be
Jammy Zhou86c2b792015-05-13 22:52:42 +0800197 * continuously new fence signaled ie amdgpu_fence_read needs
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 * to return a different value each time for both the currently
199 * polling process and the other process that xchg the last_seq
200 * btw atomic read and xchg of the current process. And the
201 * value the other process set as last seq must be higher than
202 * the seq value we just read. Which means that current process
Jammy Zhou86c2b792015-05-13 22:52:42 +0800203 * need to be interrupted after amdgpu_fence_read and before
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204 * atomic xchg.
205 *
206 * To be even more safe we count the number of time we loop and
207 * we bail after 10 loop just accepting the fact that we might
208 * have temporarly set the last_seq not to the true real last
209 * seq but to an older one.
210 */
211 last_seq = atomic64_read(&ring->fence_drv.last_seq);
212 do {
213 last_emitted = ring->fence_drv.sync_seq[ring->idx];
214 seq = amdgpu_fence_read(ring);
215 seq |= last_seq & 0xffffffff00000000LL;
216 if (seq < last_seq) {
217 seq &= 0xffffffff;
218 seq |= last_emitted & 0xffffffff00000000LL;
219 }
220
221 if (seq <= last_seq || seq > last_emitted) {
222 break;
223 }
224 /* If we loop over we don't want to return without
225 * checking if a fence is signaled as it means that the
226 * seq we just read is different from the previous on.
227 */
228 wake = true;
229 last_seq = seq;
230 if ((count_loop++) > 10) {
231 /* We looped over too many time leave with the
232 * fact that we might have set an older fence
233 * seq then the current real last seq as signaled
234 * by the hw.
235 */
236 break;
237 }
238 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
239
240 if (seq < last_emitted)
241 amdgpu_fence_schedule_check(ring);
242
243 return wake;
244}
245
246/**
247 * amdgpu_fence_check_lockup - check for hardware lockup
248 *
249 * @work: delayed work item
250 *
251 * Checks for fence activity and if there is none probe
252 * the hardware if a lockup occured.
253 */
254static void amdgpu_fence_check_lockup(struct work_struct *work)
255{
256 struct amdgpu_fence_driver *fence_drv;
257 struct amdgpu_ring *ring;
258
259 fence_drv = container_of(work, struct amdgpu_fence_driver,
260 lockup_work.work);
261 ring = fence_drv->ring;
262
263 if (!down_read_trylock(&ring->adev->exclusive_lock)) {
264 /* just reschedule the check if a reset is going on */
265 amdgpu_fence_schedule_check(ring);
266 return;
267 }
268
monk.liu7f06c232015-07-30 18:28:12 +0800269 if (amdgpu_fence_activity(ring)) {
270 wake_up_all(&ring->fence_drv.fence_queue);
271 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272 else if (amdgpu_ring_is_lockup(ring)) {
273 /* good news we believe it's a lockup */
274 dev_warn(ring->adev->dev, "GPU lockup (current fence id "
275 "0x%016llx last fence id 0x%016llx on ring %d)\n",
276 (uint64_t)atomic64_read(&fence_drv->last_seq),
277 fence_drv->sync_seq[ring->idx], ring->idx);
278
279 /* remember that we need an reset */
280 ring->adev->needs_reset = true;
monk.liu7f06c232015-07-30 18:28:12 +0800281 wake_up_all(&ring->fence_drv.fence_queue);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282 }
283 up_read(&ring->adev->exclusive_lock);
284}
285
286/**
287 * amdgpu_fence_process - process a fence
288 *
289 * @adev: amdgpu_device pointer
290 * @ring: ring index the fence is associated with
291 *
292 * Checks the current fence value and wakes the fence queue
293 * if the sequence number has increased (all asics).
294 */
295void amdgpu_fence_process(struct amdgpu_ring *ring)
296{
297 uint64_t seq, last_seq, last_emitted;
298 unsigned count_loop = 0;
299 bool wake = false;
Chunming Zhou176e1ab2015-07-24 10:49:47 +0800300 unsigned long irqflags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400301
302 /* Note there is a scenario here for an infinite loop but it's
303 * very unlikely to happen. For it to happen, the current polling
304 * process need to be interrupted by another process and another
305 * process needs to update the last_seq btw the atomic read and
306 * xchg of the current process.
307 *
308 * More over for this to go in infinite loop there need to be
309 * continuously new fence signaled ie amdgpu_fence_read needs
310 * to return a different value each time for both the currently
311 * polling process and the other process that xchg the last_seq
312 * btw atomic read and xchg of the current process. And the
313 * value the other process set as last seq must be higher than
314 * the seq value we just read. Which means that current process
315 * need to be interrupted after amdgpu_fence_read and before
316 * atomic xchg.
317 *
318 * To be even more safe we count the number of time we loop and
319 * we bail after 10 loop just accepting the fact that we might
320 * have temporarly set the last_seq not to the true real last
321 * seq but to an older one.
322 */
Chunming Zhou176e1ab2015-07-24 10:49:47 +0800323 spin_lock_irqsave(&ring->fence_lock, irqflags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400324 last_seq = atomic64_read(&ring->fence_drv.last_seq);
325 do {
326 last_emitted = ring->fence_drv.sync_seq[ring->idx];
327 seq = amdgpu_fence_read(ring);
328 seq |= last_seq & 0xffffffff00000000LL;
329 if (seq < last_seq) {
330 seq &= 0xffffffff;
331 seq |= last_emitted & 0xffffffff00000000LL;
332 }
333
334 if (seq <= last_seq || seq > last_emitted) {
335 break;
336 }
337 /* If we loop over we don't want to return without
338 * checking if a fence is signaled as it means that the
339 * seq we just read is different from the previous on.
340 */
341 wake = true;
342 last_seq = seq;
343 if ((count_loop++) > 10) {
344 /* We looped over too many time leave with the
345 * fact that we might have set an older fence
346 * seq then the current real last seq as signaled
347 * by the hw.
348 */
349 break;
350 }
351 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
352
Chunming Zhou74846672015-08-04 11:30:09 +0800353 if (wake)
monk.liu7f06c232015-07-30 18:28:12 +0800354 wake_up_all(&ring->fence_drv.fence_queue);
Chunming Zhou176e1ab2015-07-24 10:49:47 +0800355 spin_unlock_irqrestore(&ring->fence_lock, irqflags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400356}
357
358/**
359 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
360 *
361 * @ring: ring the fence is associated with
362 * @seq: sequence number
363 *
364 * Check if the last signaled fence sequnce number is >= the requested
365 * sequence number (all asics).
366 * Returns true if the fence has signaled (current fence value
367 * is >= requested value) or false if it has not (current fence
368 * value is < the requested value. Helper function for
369 * amdgpu_fence_signaled().
370 */
371static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
372{
373 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
374 return true;
375
376 /* poll new last sequence at least once */
377 amdgpu_fence_process(ring);
378 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
379 return true;
380
381 return false;
382}
383
384static bool amdgpu_fence_is_signaled(struct fence *f)
385{
386 struct amdgpu_fence *fence = to_amdgpu_fence(f);
387 struct amdgpu_ring *ring = fence->ring;
388 struct amdgpu_device *adev = ring->adev;
389
390 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
391 return true;
392
393 if (down_read_trylock(&adev->exclusive_lock)) {
394 amdgpu_fence_process(ring);
395 up_read(&adev->exclusive_lock);
396
397 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
398 return true;
399 }
400 return false;
401}
402
403/**
404 * amdgpu_fence_enable_signaling - enable signalling on fence
405 * @fence: fence
406 *
407 * This function is called with fence_queue lock held, and adds a callback
408 * to fence_queue that checks if this fence is signaled, and if so it
409 * signals the fence and removes itself.
410 */
411static bool amdgpu_fence_enable_signaling(struct fence *f)
412{
413 struct amdgpu_fence *fence = to_amdgpu_fence(f);
414 struct amdgpu_ring *ring = fence->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415
416 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
417 return false;
418
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400419 fence->fence_wake.flags = 0;
420 fence->fence_wake.private = NULL;
421 fence->fence_wake.func = amdgpu_fence_check_signaled;
monk.liu7f06c232015-07-30 18:28:12 +0800422 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400423 fence_get(f);
424 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
425 return true;
426}
427
428/**
429 * amdgpu_fence_signaled - check if a fence has signaled
430 *
431 * @fence: amdgpu fence object
432 *
433 * Check if the requested fence has signaled (all asics).
434 * Returns true if the fence has signaled or false if it has not.
435 */
436bool amdgpu_fence_signaled(struct amdgpu_fence *fence)
437{
438 if (!fence)
439 return true;
440
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400441 if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400442 if (!fence_signal(&fence->base))
443 FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n");
444 return true;
445 }
446
447 return false;
448}
449
monk.liu7f06c232015-07-30 18:28:12 +0800450/*
451 * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
452 * @ring: ring to wait on for the seq number
453 * @seq: seq number wait for
454 * @intr: if interruptible
455 * @timeout: jiffies before time out
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400456 *
monk.liu7f06c232015-07-30 18:28:12 +0800457 * return value:
458 * 0: time out but seq not signaled, and gpu not hang
459 * X (X > 0): seq signaled and X means how many jiffies remains before time out
460 * -EDEADL: GPU hang before time out
461 * -ESYSRESTART: interrupted before seq signaled
462 * -EINVAL: some paramter is not valid
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 */
monk.liu7f06c232015-07-30 18:28:12 +0800464static long amdgpu_fence_ring_wait_seq_timeout(struct amdgpu_ring *ring, uint64_t seq,
465 bool intr, long timeout)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400466{
monk.liu7f06c232015-07-30 18:28:12 +0800467 struct amdgpu_device *adev = ring->adev;
468 long r = 0;
469 bool signaled = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470
monk.liu7f06c232015-07-30 18:28:12 +0800471 BUG_ON(!ring);
472 if (seq > ring->fence_drv.sync_seq[ring->idx])
473 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400474
monk.liu7f06c232015-07-30 18:28:12 +0800475 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
476 return timeout;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400477
monk.liu7f06c232015-07-30 18:28:12 +0800478 while (1) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400479 if (intr) {
monk.liu7f06c232015-07-30 18:28:12 +0800480 r = wait_event_interruptible_timeout(ring->fence_drv.fence_queue, (
481 (signaled = amdgpu_fence_seq_signaled(ring, seq))
482 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
483
484 if (r == -ERESTARTSYS) /* interrupted */
485 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486 } else {
monk.liu7f06c232015-07-30 18:28:12 +0800487 r = wait_event_timeout(ring->fence_drv.fence_queue, (
488 (signaled = amdgpu_fence_seq_signaled(ring, seq))
489 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490 }
491
monk.liu7f06c232015-07-30 18:28:12 +0800492 if (signaled) {
493 /* seq signaled */
494 if (timeout == MAX_SCHEDULE_TIMEOUT)
495 return timeout;
496 return (timeout - AMDGPU_FENCE_JIFFIES_TIMEOUT - r);
497 }
498 else if (adev->needs_reset) {
499 return -EDEADLK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 }
501
monk.liu7f06c232015-07-30 18:28:12 +0800502 /* check if it's a lockup */
503 if (amdgpu_ring_is_lockup(ring)) {
504 uint64_t last_seq = atomic64_read(&ring->fence_drv.last_seq);
505 /* ring lookup */
506 dev_warn(adev->dev, "GPU lockup (waiting for "
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 "0x%016llx last fence id 0x%016llx on"
508 " ring %d)\n",
monk.liu7f06c232015-07-30 18:28:12 +0800509 seq, last_seq, ring->idx);
510 wake_up_all(&ring->fence_drv.fence_queue);
511 return -EDEADLK;
512 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513
monk.liu7f06c232015-07-30 18:28:12 +0800514 if (timeout < MAX_SCHEDULE_TIMEOUT) {
515 timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
516 if (timeout < 1)
517 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400518 }
519 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520}
521
monk.liu7f06c232015-07-30 18:28:12 +0800522
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523/**
524 * amdgpu_fence_wait - wait for a fence to signal
525 *
526 * @fence: amdgpu fence object
527 * @intr: use interruptable sleep
528 *
529 * Wait for the requested fence to signal (all asics).
530 * @intr selects whether to use interruptable (true) or non-interruptable
531 * (false) sleep when waiting for the fence.
532 * Returns 0 if the fence has passed, error for all other cases.
533 */
534int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
535{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 long r;
537
monk.liu2e536082015-07-30 14:56:18 +0800538 r = fence_wait_timeout(&fence->base, intr, MAX_SCHEDULE_TIMEOUT);
539 if (r < 0)
540 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 return 0;
542}
543
544/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400545 * amdgpu_fence_wait_next - wait for the next fence to signal
546 *
547 * @adev: amdgpu device pointer
548 * @ring: ring index the fence is associated with
549 *
550 * Wait for the next fence on the requested ring to signal (all asics).
551 * Returns 0 if the next fence has passed, error for all other cases.
552 * Caller must hold ring lock.
553 */
554int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
555{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 long r;
557
monk.liu7f06c232015-07-30 18:28:12 +0800558 uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
559 if (seq >= ring->fence_drv.sync_seq[ring->idx])
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 return -ENOENT;
monk.liu7f06c232015-07-30 18:28:12 +0800561 r = amdgpu_fence_ring_wait_seq_timeout(ring, seq, false, MAX_SCHEDULE_TIMEOUT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 if (r < 0)
563 return r;
monk.liu7f06c232015-07-30 18:28:12 +0800564
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400565 return 0;
566}
567
568/**
569 * amdgpu_fence_wait_empty - wait for all fences to signal
570 *
571 * @adev: amdgpu device pointer
572 * @ring: ring index the fence is associated with
573 *
574 * Wait for all fences on the requested ring to signal (all asics).
575 * Returns 0 if the fences have passed, error for all other cases.
576 * Caller must hold ring lock.
577 */
578int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
579{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 long r;
581
monk.liu7f06c232015-07-30 18:28:12 +0800582 uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
583 if (!seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 return 0;
585
monk.liu7f06c232015-07-30 18:28:12 +0800586 r = amdgpu_fence_ring_wait_seq_timeout(ring, seq, false, MAX_SCHEDULE_TIMEOUT);
587
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 if (r < 0) {
589 if (r == -EDEADLK)
590 return -EDEADLK;
591
monk.liu7f06c232015-07-30 18:28:12 +0800592 dev_err(ring->adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
593 ring->idx, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400594 }
595 return 0;
596}
597
598/**
599 * amdgpu_fence_ref - take a ref on a fence
600 *
601 * @fence: amdgpu fence object
602 *
603 * Take a reference on a fence (all asics).
604 * Returns the fence.
605 */
606struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
607{
608 fence_get(&fence->base);
609 return fence;
610}
611
612/**
613 * amdgpu_fence_unref - remove a ref on a fence
614 *
615 * @fence: amdgpu fence object
616 *
617 * Remove a reference on a fence (all asics).
618 */
619void amdgpu_fence_unref(struct amdgpu_fence **fence)
620{
621 struct amdgpu_fence *tmp = *fence;
622
623 *fence = NULL;
624 if (tmp)
625 fence_put(&tmp->base);
626}
627
628/**
629 * amdgpu_fence_count_emitted - get the count of emitted fences
630 *
631 * @ring: ring the fence is associated with
632 *
633 * Get the number of fences emitted on the requested ring (all asics).
634 * Returns the number of emitted fences on the ring. Used by the
635 * dynpm code to ring track activity.
636 */
637unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
638{
639 uint64_t emitted;
640
641 /* We are not protected by ring lock when reading the last sequence
642 * but it's ok to report slightly wrong fence count here.
643 */
644 amdgpu_fence_process(ring);
645 emitted = ring->fence_drv.sync_seq[ring->idx]
646 - atomic64_read(&ring->fence_drv.last_seq);
647 /* to avoid 32bits warp around */
648 if (emitted > 0x10000000)
649 emitted = 0x10000000;
650
651 return (unsigned)emitted;
652}
653
654/**
655 * amdgpu_fence_need_sync - do we need a semaphore
656 *
657 * @fence: amdgpu fence object
658 * @dst_ring: which ring to check against
659 *
660 * Check if the fence needs to be synced against another ring
661 * (all asics). If so, we need to emit a semaphore.
662 * Returns true if we need to sync with another ring, false if
663 * not.
664 */
665bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
666 struct amdgpu_ring *dst_ring)
667{
668 struct amdgpu_fence_driver *fdrv;
669
670 if (!fence)
671 return false;
672
673 if (fence->ring == dst_ring)
674 return false;
675
676 /* we are protected by the ring mutex */
677 fdrv = &dst_ring->fence_drv;
678 if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
679 return false;
680
681 return true;
682}
683
684/**
685 * amdgpu_fence_note_sync - record the sync point
686 *
687 * @fence: amdgpu fence object
688 * @dst_ring: which ring to check against
689 *
690 * Note the sequence number at which point the fence will
691 * be synced with the requested ring (all asics).
692 */
693void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
694 struct amdgpu_ring *dst_ring)
695{
696 struct amdgpu_fence_driver *dst, *src;
697 unsigned i;
698
699 if (!fence)
700 return;
701
702 if (fence->ring == dst_ring)
703 return;
704
705 /* we are protected by the ring mutex */
706 src = &fence->ring->fence_drv;
707 dst = &dst_ring->fence_drv;
708 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
709 if (i == dst_ring->idx)
710 continue;
711
712 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
713 }
714}
715
716/**
717 * amdgpu_fence_driver_start_ring - make the fence driver
718 * ready for use on the requested ring.
719 *
720 * @ring: ring to start the fence driver on
721 * @irq_src: interrupt source to use for this ring
722 * @irq_type: interrupt type to use for this ring
723 *
724 * Make the fence driver ready for processing (all asics).
725 * Not all asics have all rings, so each asic will only
726 * start the fence driver on the rings it has.
727 * Returns 0 for success, errors for failure.
728 */
729int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
730 struct amdgpu_irq_src *irq_src,
731 unsigned irq_type)
732{
733 struct amdgpu_device *adev = ring->adev;
734 uint64_t index;
735
736 if (ring != &adev->uvd.ring) {
737 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
738 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
739 } else {
740 /* put fence directly behind firmware */
741 index = ALIGN(adev->uvd.fw->size, 8);
742 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
743 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
744 }
745 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
Chunming Zhouc6a40792015-06-01 14:14:32 +0800746 amdgpu_irq_get(adev, irq_src, irq_type);
747
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400748 ring->fence_drv.irq_src = irq_src;
749 ring->fence_drv.irq_type = irq_type;
Chunming Zhouc6a40792015-06-01 14:14:32 +0800750 ring->fence_drv.initialized = true;
751
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
753 "cpu addr 0x%p\n", ring->idx,
754 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
755 return 0;
756}
757
758/**
759 * amdgpu_fence_driver_init_ring - init the fence driver
760 * for the requested ring.
761 *
762 * @ring: ring to init the fence driver on
763 *
764 * Init the fence driver for the requested ring (all asics).
765 * Helper function for amdgpu_fence_driver_init().
766 */
767void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
768{
769 int i;
770
771 ring->fence_drv.cpu_addr = NULL;
772 ring->fence_drv.gpu_addr = 0;
773 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
774 ring->fence_drv.sync_seq[i] = 0;
775
776 atomic64_set(&ring->fence_drv.last_seq, 0);
777 ring->fence_drv.initialized = false;
778
779 INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
780 amdgpu_fence_check_lockup);
781 ring->fence_drv.ring = ring;
Alex Deucherb80d8472015-08-16 22:55:02 -0400782
783 if (amdgpu_enable_scheduler) {
784 ring->scheduler = amd_sched_create((void *)ring->adev,
Chunming Zhouc1b69ed2015-07-21 13:45:14 +0800785 &amdgpu_sched_ops,
Jammy Zhou4afcb302015-07-30 16:44:05 +0800786 ring->idx, 5, 0,
787 amdgpu_sched_hw_submission);
Alex Deucherb80d8472015-08-16 22:55:02 -0400788 if (!ring->scheduler)
789 DRM_ERROR("Failed to create scheduler on ring %d.\n",
790 ring->idx);
791 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400792}
793
794/**
795 * amdgpu_fence_driver_init - init the fence driver
796 * for all possible rings.
797 *
798 * @adev: amdgpu device pointer
799 *
800 * Init the fence driver for all possible rings (all asics).
801 * Not all asics have all rings, so each asic will only
802 * start the fence driver on the rings it has using
803 * amdgpu_fence_driver_start_ring().
804 * Returns 0 for success.
805 */
806int amdgpu_fence_driver_init(struct amdgpu_device *adev)
807{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400808 if (amdgpu_debugfs_fence_init(adev))
809 dev_err(adev->dev, "fence debugfs file creation failed\n");
810
811 return 0;
812}
813
814/**
815 * amdgpu_fence_driver_fini - tear down the fence driver
816 * for all possible rings.
817 *
818 * @adev: amdgpu device pointer
819 *
820 * Tear down the fence driver for all possible rings (all asics).
821 */
822void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
823{
824 int i, r;
825
826 mutex_lock(&adev->ring_lock);
827 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
828 struct amdgpu_ring *ring = adev->rings[i];
829 if (!ring || !ring->fence_drv.initialized)
830 continue;
831 r = amdgpu_fence_wait_empty(ring);
832 if (r) {
833 /* no need to trigger GPU reset as we are unloading */
834 amdgpu_fence_driver_force_completion(adev);
835 }
monk.liu7f06c232015-07-30 18:28:12 +0800836 wake_up_all(&ring->fence_drv.fence_queue);
Chunming Zhouc6a40792015-06-01 14:14:32 +0800837 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
838 ring->fence_drv.irq_type);
Alex Deucherb80d8472015-08-16 22:55:02 -0400839 if (ring->scheduler)
840 amd_sched_destroy(ring->scheduler);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841 ring->fence_drv.initialized = false;
842 }
843 mutex_unlock(&adev->ring_lock);
844}
845
846/**
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400847 * amdgpu_fence_driver_suspend - suspend the fence driver
848 * for all possible rings.
849 *
850 * @adev: amdgpu device pointer
851 *
852 * Suspend the fence driver for all possible rings (all asics).
853 */
854void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
855{
856 int i, r;
857
858 mutex_lock(&adev->ring_lock);
859 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
860 struct amdgpu_ring *ring = adev->rings[i];
861 if (!ring || !ring->fence_drv.initialized)
862 continue;
863
864 /* wait for gpu to finish processing current batch */
865 r = amdgpu_fence_wait_empty(ring);
866 if (r) {
867 /* delay GPU reset to resume */
868 amdgpu_fence_driver_force_completion(adev);
869 }
870
871 /* disable the interrupt */
872 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
873 ring->fence_drv.irq_type);
874 }
875 mutex_unlock(&adev->ring_lock);
876}
877
878/**
879 * amdgpu_fence_driver_resume - resume the fence driver
880 * for all possible rings.
881 *
882 * @adev: amdgpu device pointer
883 *
884 * Resume the fence driver for all possible rings (all asics).
885 * Not all asics have all rings, so each asic will only
886 * start the fence driver on the rings it has using
887 * amdgpu_fence_driver_start_ring().
888 * Returns 0 for success.
889 */
890void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
891{
892 int i;
893
894 mutex_lock(&adev->ring_lock);
895 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
896 struct amdgpu_ring *ring = adev->rings[i];
897 if (!ring || !ring->fence_drv.initialized)
898 continue;
899
900 /* enable the interrupt */
901 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
902 ring->fence_drv.irq_type);
903 }
904 mutex_unlock(&adev->ring_lock);
905}
906
907/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400908 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
909 *
910 * @adev: amdgpu device pointer
911 *
912 * In case of GPU reset failure make sure no process keep waiting on fence
913 * that will never complete.
914 */
915void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
916{
917 int i;
918
919 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
920 struct amdgpu_ring *ring = adev->rings[i];
921 if (!ring || !ring->fence_drv.initialized)
922 continue;
923
924 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
925 }
926}
927
928
929/*
930 * Fence debugfs
931 */
932#if defined(CONFIG_DEBUG_FS)
933static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
934{
935 struct drm_info_node *node = (struct drm_info_node *)m->private;
936 struct drm_device *dev = node->minor->dev;
937 struct amdgpu_device *adev = dev->dev_private;
938 int i, j;
939
940 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
941 struct amdgpu_ring *ring = adev->rings[i];
942 if (!ring || !ring->fence_drv.initialized)
943 continue;
944
945 amdgpu_fence_process(ring);
946
Christian König344c19f2015-06-02 15:47:16 +0200947 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948 seq_printf(m, "Last signaled fence 0x%016llx\n",
949 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
950 seq_printf(m, "Last emitted 0x%016llx\n",
951 ring->fence_drv.sync_seq[i]);
952
953 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
954 struct amdgpu_ring *other = adev->rings[j];
Christian König344c19f2015-06-02 15:47:16 +0200955 if (i != j && other && other->fence_drv.initialized &&
956 ring->fence_drv.sync_seq[j])
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400957 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
958 j, ring->fence_drv.sync_seq[j]);
959 }
960 }
961 return 0;
962}
963
964static struct drm_info_list amdgpu_debugfs_fence_list[] = {
965 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
966};
967#endif
968
969int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
970{
971#if defined(CONFIG_DEBUG_FS)
972 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
973#else
974 return 0;
975#endif
976}
977
978static const char *amdgpu_fence_get_driver_name(struct fence *fence)
979{
980 return "amdgpu";
981}
982
983static const char *amdgpu_fence_get_timeline_name(struct fence *f)
984{
985 struct amdgpu_fence *fence = to_amdgpu_fence(f);
986 return (const char *)fence->ring->name;
987}
988
989static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
990{
991 return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
992}
993
monk.liu332dfe92015-07-30 15:19:05 +0800994static inline bool amdgpu_test_signaled_any(struct amdgpu_fence **fences)
995{
996 int idx;
997 struct amdgpu_fence *fence;
998
999 idx = 0;
1000 for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
1001 fence = fences[idx];
1002 if (fence) {
1003 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
1004 return true;
1005 }
1006 }
1007 return false;
1008}
1009
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010struct amdgpu_wait_cb {
1011 struct fence_cb base;
1012 struct task_struct *task;
1013};
1014
1015static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
1016{
1017 struct amdgpu_wait_cb *wait =
1018 container_of(cb, struct amdgpu_wait_cb, base);
1019 wake_up_process(wait->task);
1020}
1021
1022static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
1023 signed long t)
1024{
monk.liue29551552015-07-30 18:26:18 +08001025 struct amdgpu_fence *array[AMDGPU_MAX_RINGS];
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001026 struct amdgpu_fence *fence = to_amdgpu_fence(f);
1027 struct amdgpu_device *adev = fence->ring->adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001028
monk.liue29551552015-07-30 18:26:18 +08001029 memset(&array[0], 0, sizeof(array));
1030 array[0] = fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001031
monk.liue29551552015-07-30 18:26:18 +08001032 return amdgpu_fence_wait_any(adev, array, intr, t);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001033}
1034
monk.liu332dfe92015-07-30 15:19:05 +08001035/* wait until any fence in array signaled */
1036signed long amdgpu_fence_wait_any(struct amdgpu_device *adev,
1037 struct amdgpu_fence **array, bool intr, signed long t)
1038{
1039 long idx = 0;
1040 struct amdgpu_wait_cb cb[AMDGPU_MAX_RINGS];
1041 struct amdgpu_fence *fence;
1042
1043 BUG_ON(!array);
1044
1045 for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
1046 fence = array[idx];
1047 if (fence) {
1048 cb[idx].task = current;
1049 if (fence_add_callback(&fence->base,
1050 &cb[idx].base, amdgpu_fence_wait_cb))
1051 return t; /* return if fence is already signaled */
1052 }
1053 }
1054
1055 while (t > 0) {
1056 if (intr)
1057 set_current_state(TASK_INTERRUPTIBLE);
1058 else
1059 set_current_state(TASK_UNINTERRUPTIBLE);
1060
1061 /*
1062 * amdgpu_test_signaled_any must be called after
1063 * set_current_state to prevent a race with wake_up_process
1064 */
1065 if (amdgpu_test_signaled_any(array))
1066 break;
1067
1068 if (adev->needs_reset) {
1069 t = -EDEADLK;
1070 break;
1071 }
1072
1073 t = schedule_timeout(t);
1074
1075 if (t > 0 && intr && signal_pending(current))
1076 t = -ERESTARTSYS;
1077 }
1078
1079 __set_current_state(TASK_RUNNING);
1080
1081 idx = 0;
1082 for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
1083 fence = array[idx];
1084 if (fence)
1085 fence_remove_callback(&fence->base, &cb[idx].base);
1086 }
1087
1088 return t;
1089}
1090
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091const struct fence_ops amdgpu_fence_ops = {
1092 .get_driver_name = amdgpu_fence_get_driver_name,
1093 .get_timeline_name = amdgpu_fence_get_timeline_name,
1094 .enable_signaling = amdgpu_fence_enable_signaling,
1095 .signaled = amdgpu_fence_is_signaled,
1096 .wait = amdgpu_fence_default_wait,
1097 .release = NULL,
1098};