blob: 1b0bc07d0c01e82191633cb34102c46a42032cc9 [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,
129 &adev->fence_queue.lock, adev->fence_context + ring->idx,
130 (*fence)->seq);
Chunming Zhou890ee232015-06-01 14:35:03 +0800131 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
132 (*fence)->seq,
133 AMDGPU_FENCE_FLAG_INT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
135 return 0;
136}
137
138/**
139 * amdgpu_fence_check_signaled - callback from fence_queue
140 *
141 * this function is called with fence_queue lock held, which is also used
142 * for the fence locking itself, so unlocked variants are used for
143 * fence_signal, and remove_wait_queue.
144 */
145static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
146{
147 struct amdgpu_fence *fence;
148 struct amdgpu_device *adev;
149 u64 seq;
150 int ret;
151
152 fence = container_of(wait, struct amdgpu_fence, fence_wake);
153 adev = fence->ring->adev;
154
155 /*
156 * We cannot use amdgpu_fence_process here because we're already
157 * in the waitqueue, in a call from wake_up_all.
158 */
159 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
160 if (seq >= fence->seq) {
161 ret = fence_signal_locked(&fence->base);
162 if (!ret)
163 FENCE_TRACE(&fence->base, "signaled from irq context\n");
164 else
165 FENCE_TRACE(&fence->base, "was already signaled\n");
166
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167 __remove_wait_queue(&adev->fence_queue, &fence->fence_wake);
168 fence_put(&fence->base);
169 } else
170 FENCE_TRACE(&fence->base, "pending\n");
171 return 0;
172}
173
174/**
175 * amdgpu_fence_activity - check for fence activity
176 *
177 * @ring: pointer to struct amdgpu_ring
178 *
179 * Checks the current fence value and calculates the last
180 * signalled fence value. Returns true if activity occured
181 * on the ring, and the fence_queue should be waken up.
182 */
183static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
184{
185 uint64_t seq, last_seq, last_emitted;
186 unsigned count_loop = 0;
187 bool wake = false;
188
189 /* Note there is a scenario here for an infinite loop but it's
190 * very unlikely to happen. For it to happen, the current polling
191 * process need to be interrupted by another process and another
192 * process needs to update the last_seq btw the atomic read and
193 * xchg of the current process.
194 *
195 * More over for this to go in infinite loop there need to be
Jammy Zhou86c2b792015-05-13 22:52:42 +0800196 * continuously new fence signaled ie amdgpu_fence_read needs
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197 * to return a different value each time for both the currently
198 * polling process and the other process that xchg the last_seq
199 * btw atomic read and xchg of the current process. And the
200 * value the other process set as last seq must be higher than
201 * the seq value we just read. Which means that current process
Jammy Zhou86c2b792015-05-13 22:52:42 +0800202 * need to be interrupted after amdgpu_fence_read and before
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400203 * atomic xchg.
204 *
205 * To be even more safe we count the number of time we loop and
206 * we bail after 10 loop just accepting the fact that we might
207 * have temporarly set the last_seq not to the true real last
208 * seq but to an older one.
209 */
210 last_seq = atomic64_read(&ring->fence_drv.last_seq);
211 do {
212 last_emitted = ring->fence_drv.sync_seq[ring->idx];
213 seq = amdgpu_fence_read(ring);
214 seq |= last_seq & 0xffffffff00000000LL;
215 if (seq < last_seq) {
216 seq &= 0xffffffff;
217 seq |= last_emitted & 0xffffffff00000000LL;
218 }
219
220 if (seq <= last_seq || seq > last_emitted) {
221 break;
222 }
223 /* If we loop over we don't want to return without
224 * checking if a fence is signaled as it means that the
225 * seq we just read is different from the previous on.
226 */
227 wake = true;
228 last_seq = seq;
229 if ((count_loop++) > 10) {
230 /* We looped over too many time leave with the
231 * fact that we might have set an older fence
232 * seq then the current real last seq as signaled
233 * by the hw.
234 */
235 break;
236 }
237 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
238
239 if (seq < last_emitted)
240 amdgpu_fence_schedule_check(ring);
241
242 return wake;
243}
244
245/**
246 * amdgpu_fence_check_lockup - check for hardware lockup
247 *
248 * @work: delayed work item
249 *
250 * Checks for fence activity and if there is none probe
251 * the hardware if a lockup occured.
252 */
253static void amdgpu_fence_check_lockup(struct work_struct *work)
254{
255 struct amdgpu_fence_driver *fence_drv;
256 struct amdgpu_ring *ring;
257
258 fence_drv = container_of(work, struct amdgpu_fence_driver,
259 lockup_work.work);
260 ring = fence_drv->ring;
261
262 if (!down_read_trylock(&ring->adev->exclusive_lock)) {
263 /* just reschedule the check if a reset is going on */
264 amdgpu_fence_schedule_check(ring);
265 return;
266 }
267
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268 if (amdgpu_fence_activity(ring))
269 wake_up_all(&ring->adev->fence_queue);
270 else if (amdgpu_ring_is_lockup(ring)) {
271 /* good news we believe it's a lockup */
272 dev_warn(ring->adev->dev, "GPU lockup (current fence id "
273 "0x%016llx last fence id 0x%016llx on ring %d)\n",
274 (uint64_t)atomic64_read(&fence_drv->last_seq),
275 fence_drv->sync_seq[ring->idx], ring->idx);
276
277 /* remember that we need an reset */
278 ring->adev->needs_reset = true;
279 wake_up_all(&ring->adev->fence_queue);
280 }
281 up_read(&ring->adev->exclusive_lock);
282}
283
284/**
285 * amdgpu_fence_process - process a fence
286 *
287 * @adev: amdgpu_device pointer
288 * @ring: ring index the fence is associated with
289 *
290 * Checks the current fence value and wakes the fence queue
291 * if the sequence number has increased (all asics).
292 */
293void amdgpu_fence_process(struct amdgpu_ring *ring)
294{
295 uint64_t seq, last_seq, last_emitted;
296 unsigned count_loop = 0;
297 bool wake = false;
298
299 /* Note there is a scenario here for an infinite loop but it's
300 * very unlikely to happen. For it to happen, the current polling
301 * process need to be interrupted by another process and another
302 * process needs to update the last_seq btw the atomic read and
303 * xchg of the current process.
304 *
305 * More over for this to go in infinite loop there need to be
306 * continuously new fence signaled ie amdgpu_fence_read needs
307 * to return a different value each time for both the currently
308 * polling process and the other process that xchg the last_seq
309 * btw atomic read and xchg of the current process. And the
310 * value the other process set as last seq must be higher than
311 * the seq value we just read. Which means that current process
312 * need to be interrupted after amdgpu_fence_read and before
313 * atomic xchg.
314 *
315 * To be even more safe we count the number of time we loop and
316 * we bail after 10 loop just accepting the fact that we might
317 * have temporarly set the last_seq not to the true real last
318 * seq but to an older one.
319 */
320 last_seq = atomic64_read(&ring->fence_drv.last_seq);
321 do {
322 last_emitted = ring->fence_drv.sync_seq[ring->idx];
323 seq = amdgpu_fence_read(ring);
324 seq |= last_seq & 0xffffffff00000000LL;
325 if (seq < last_seq) {
326 seq &= 0xffffffff;
327 seq |= last_emitted & 0xffffffff00000000LL;
328 }
329
330 if (seq <= last_seq || seq > last_emitted) {
331 break;
332 }
333 /* If we loop over we don't want to return without
334 * checking if a fence is signaled as it means that the
335 * seq we just read is different from the previous on.
336 */
337 wake = true;
338 last_seq = seq;
339 if ((count_loop++) > 10) {
340 /* We looped over too many time leave with the
341 * fact that we might have set an older fence
342 * seq then the current real last seq as signaled
343 * by the hw.
344 */
345 break;
346 }
347 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
348
349 if (wake)
350 wake_up_all(&ring->adev->fence_queue);
351}
352
353/**
354 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
355 *
356 * @ring: ring the fence is associated with
357 * @seq: sequence number
358 *
359 * Check if the last signaled fence sequnce number is >= the requested
360 * sequence number (all asics).
361 * Returns true if the fence has signaled (current fence value
362 * is >= requested value) or false if it has not (current fence
363 * value is < the requested value. Helper function for
364 * amdgpu_fence_signaled().
365 */
366static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
367{
368 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
369 return true;
370
371 /* poll new last sequence at least once */
372 amdgpu_fence_process(ring);
373 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
374 return true;
375
376 return false;
377}
378
379static bool amdgpu_fence_is_signaled(struct fence *f)
380{
381 struct amdgpu_fence *fence = to_amdgpu_fence(f);
382 struct amdgpu_ring *ring = fence->ring;
383 struct amdgpu_device *adev = ring->adev;
384
385 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
386 return true;
387
388 if (down_read_trylock(&adev->exclusive_lock)) {
389 amdgpu_fence_process(ring);
390 up_read(&adev->exclusive_lock);
391
392 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
393 return true;
394 }
395 return false;
396}
397
398/**
399 * amdgpu_fence_enable_signaling - enable signalling on fence
400 * @fence: fence
401 *
402 * This function is called with fence_queue lock held, and adds a callback
403 * to fence_queue that checks if this fence is signaled, and if so it
404 * signals the fence and removes itself.
405 */
406static bool amdgpu_fence_enable_signaling(struct fence *f)
407{
408 struct amdgpu_fence *fence = to_amdgpu_fence(f);
409 struct amdgpu_ring *ring = fence->ring;
410 struct amdgpu_device *adev = ring->adev;
411
412 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
413 return false;
414
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415 fence->fence_wake.flags = 0;
416 fence->fence_wake.private = NULL;
417 fence->fence_wake.func = amdgpu_fence_check_signaled;
418 __add_wait_queue(&adev->fence_queue, &fence->fence_wake);
419 fence_get(f);
420 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
421 return true;
422}
423
424/**
425 * amdgpu_fence_signaled - check if a fence has signaled
426 *
427 * @fence: amdgpu fence object
428 *
429 * Check if the requested fence has signaled (all asics).
430 * Returns true if the fence has signaled or false if it has not.
431 */
432bool amdgpu_fence_signaled(struct amdgpu_fence *fence)
433{
434 if (!fence)
435 return true;
436
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437 if (amdgpu_fence_seq_signaled(fence->ring, fence->seq)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400438 if (!fence_signal(&fence->base))
439 FENCE_TRACE(&fence->base, "signaled from amdgpu_fence_signaled\n");
440 return true;
441 }
442
443 return false;
444}
445
446/**
447 * amdgpu_fence_any_seq_signaled - check if any sequence number is signaled
448 *
449 * @adev: amdgpu device pointer
450 * @seq: sequence numbers
451 *
452 * Check if the last signaled fence sequnce number is >= the requested
453 * sequence number (all asics).
454 * Returns true if any has signaled (current value is >= requested value)
455 * or false if it has not. Helper function for amdgpu_fence_wait_seq.
456 */
457static bool amdgpu_fence_any_seq_signaled(struct amdgpu_device *adev, u64 *seq)
458{
459 unsigned i;
460
461 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
462 if (!adev->rings[i] || !seq[i])
463 continue;
464
465 if (amdgpu_fence_seq_signaled(adev->rings[i], seq[i]))
466 return true;
467 }
468
469 return false;
470}
471
472/**
473 * amdgpu_fence_wait_seq_timeout - wait for a specific sequence numbers
474 *
475 * @adev: amdgpu device pointer
476 * @target_seq: sequence number(s) we want to wait for
477 * @intr: use interruptable sleep
478 * @timeout: maximum time to wait, or MAX_SCHEDULE_TIMEOUT for infinite wait
479 *
480 * Wait for the requested sequence number(s) to be written by any ring
481 * (all asics). Sequnce number array is indexed by ring id.
482 * @intr selects whether to use interruptable (true) or non-interruptable
483 * (false) sleep when waiting for the sequence number. Helper function
484 * for amdgpu_fence_wait_*().
485 * Returns remaining time if the sequence number has passed, 0 when
486 * the wait timeout, or an error for all other cases.
487 * -EDEADLK is returned when a GPU lockup has been detected.
488 */
Christian König03507c42015-06-19 17:00:19 +0200489static long amdgpu_fence_wait_seq_timeout(struct amdgpu_device *adev,
490 u64 *target_seq, bool intr,
491 long timeout)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400492{
493 uint64_t last_seq[AMDGPU_MAX_RINGS];
494 bool signaled;
monk.liu332300b2015-06-08 14:48:15 +0800495 int i;
496 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497
Jack Xiao25f45e62015-06-04 12:18:27 +0800498 if (timeout == 0) {
499 return amdgpu_fence_any_seq_signaled(adev, target_seq);
500 }
501
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400502 while (!amdgpu_fence_any_seq_signaled(adev, target_seq)) {
503
504 /* Save current sequence values, used to check for GPU lockups */
505 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
506 struct amdgpu_ring *ring = adev->rings[i];
507
508 if (!ring || !target_seq[i])
509 continue;
510
511 last_seq[i] = atomic64_read(&ring->fence_drv.last_seq);
512 trace_amdgpu_fence_wait_begin(adev->ddev, i, target_seq[i]);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 }
514
515 if (intr) {
516 r = wait_event_interruptible_timeout(adev->fence_queue, (
517 (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
518 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
519 } else {
520 r = wait_event_timeout(adev->fence_queue, (
521 (signaled = amdgpu_fence_any_seq_signaled(adev, target_seq))
522 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
523 }
524
525 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
526 struct amdgpu_ring *ring = adev->rings[i];
527
528 if (!ring || !target_seq[i])
529 continue;
530
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400531 trace_amdgpu_fence_wait_end(adev->ddev, i, target_seq[i]);
532 }
533
534 if (unlikely(r < 0))
535 return r;
536
537 if (unlikely(!signaled)) {
538
539 if (adev->needs_reset)
540 return -EDEADLK;
541
542 /* we were interrupted for some reason and fence
543 * isn't signaled yet, resume waiting */
544 if (r)
545 continue;
546
547 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
548 struct amdgpu_ring *ring = adev->rings[i];
549
550 if (!ring || !target_seq[i])
551 continue;
552
553 if (last_seq[i] != atomic64_read(&ring->fence_drv.last_seq))
554 break;
555 }
556
557 if (i != AMDGPU_MAX_RINGS)
558 continue;
559
560 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
561 if (!adev->rings[i] || !target_seq[i])
562 continue;
563
564 if (amdgpu_ring_is_lockup(adev->rings[i]))
565 break;
566 }
567
568 if (i < AMDGPU_MAX_RINGS) {
569 /* good news we believe it's a lockup */
570 dev_warn(adev->dev, "GPU lockup (waiting for "
571 "0x%016llx last fence id 0x%016llx on"
572 " ring %d)\n",
573 target_seq[i], last_seq[i], i);
574
575 /* remember that we need an reset */
576 adev->needs_reset = true;
577 wake_up_all(&adev->fence_queue);
578 return -EDEADLK;
579 }
580
581 if (timeout < MAX_SCHEDULE_TIMEOUT) {
582 timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
583 if (timeout <= 0) {
584 return 0;
585 }
586 }
587 }
588 }
589 return timeout;
590}
591
592/**
593 * amdgpu_fence_wait - wait for a fence to signal
594 *
595 * @fence: amdgpu fence object
596 * @intr: use interruptable sleep
597 *
598 * Wait for the requested fence to signal (all asics).
599 * @intr selects whether to use interruptable (true) or non-interruptable
600 * (false) sleep when waiting for the fence.
601 * Returns 0 if the fence has passed, error for all other cases.
602 */
603int amdgpu_fence_wait(struct amdgpu_fence *fence, bool intr)
604{
605 uint64_t seq[AMDGPU_MAX_RINGS] = {};
606 long r;
607
608 seq[fence->ring->idx] = fence->seq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400609 r = amdgpu_fence_wait_seq_timeout(fence->ring->adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
610 if (r < 0) {
611 return r;
612 }
613
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 r = fence_signal(&fence->base);
615 if (!r)
616 FENCE_TRACE(&fence->base, "signaled from fence_wait\n");
617 return 0;
618}
619
620/**
621 * amdgpu_fence_wait_any - wait for a fence to signal on any ring
622 *
623 * @adev: amdgpu device pointer
624 * @fences: amdgpu fence object(s)
625 * @intr: use interruptable sleep
626 *
627 * Wait for any requested fence to signal (all asics). Fence
628 * array is indexed by ring id. @intr selects whether to use
629 * interruptable (true) or non-interruptable (false) sleep when
630 * waiting for the fences. Used by the suballocator.
631 * Returns 0 if any fence has passed, error for all other cases.
632 */
633int amdgpu_fence_wait_any(struct amdgpu_device *adev,
634 struct amdgpu_fence **fences,
635 bool intr)
636{
637 uint64_t seq[AMDGPU_MAX_RINGS];
638 unsigned i, num_rings = 0;
639 long r;
640
641 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
642 seq[i] = 0;
643
644 if (!fences[i]) {
645 continue;
646 }
647
648 seq[i] = fences[i]->seq;
649 ++num_rings;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650 }
651
652 /* nothing to wait for ? */
653 if (num_rings == 0)
654 return -ENOENT;
655
656 r = amdgpu_fence_wait_seq_timeout(adev, seq, intr, MAX_SCHEDULE_TIMEOUT);
657 if (r < 0) {
658 return r;
659 }
660 return 0;
661}
662
663/**
664 * amdgpu_fence_wait_next - wait for the next fence to signal
665 *
666 * @adev: amdgpu device pointer
667 * @ring: ring index the fence is associated with
668 *
669 * Wait for the next fence on the requested ring to signal (all asics).
670 * Returns 0 if the next fence has passed, error for all other cases.
671 * Caller must hold ring lock.
672 */
673int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
674{
675 uint64_t seq[AMDGPU_MAX_RINGS] = {};
676 long r;
677
678 seq[ring->idx] = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
679 if (seq[ring->idx] >= ring->fence_drv.sync_seq[ring->idx]) {
680 /* nothing to wait for, last_seq is
681 already the last emited fence */
682 return -ENOENT;
683 }
684 r = amdgpu_fence_wait_seq_timeout(ring->adev, seq, false, MAX_SCHEDULE_TIMEOUT);
685 if (r < 0)
686 return r;
687 return 0;
688}
689
690/**
691 * amdgpu_fence_wait_empty - wait for all fences to signal
692 *
693 * @adev: amdgpu device pointer
694 * @ring: ring index the fence is associated with
695 *
696 * Wait for all fences on the requested ring to signal (all asics).
697 * Returns 0 if the fences have passed, error for all other cases.
698 * Caller must hold ring lock.
699 */
700int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
701{
702 struct amdgpu_device *adev = ring->adev;
703 uint64_t seq[AMDGPU_MAX_RINGS] = {};
704 long r;
705
706 seq[ring->idx] = ring->fence_drv.sync_seq[ring->idx];
707 if (!seq[ring->idx])
708 return 0;
709
710 r = amdgpu_fence_wait_seq_timeout(adev, seq, false, MAX_SCHEDULE_TIMEOUT);
711 if (r < 0) {
712 if (r == -EDEADLK)
713 return -EDEADLK;
714
715 dev_err(adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
716 ring->idx, r);
717 }
718 return 0;
719}
720
721/**
722 * amdgpu_fence_ref - take a ref on a fence
723 *
724 * @fence: amdgpu fence object
725 *
726 * Take a reference on a fence (all asics).
727 * Returns the fence.
728 */
729struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
730{
731 fence_get(&fence->base);
732 return fence;
733}
734
735/**
736 * amdgpu_fence_unref - remove a ref on a fence
737 *
738 * @fence: amdgpu fence object
739 *
740 * Remove a reference on a fence (all asics).
741 */
742void amdgpu_fence_unref(struct amdgpu_fence **fence)
743{
744 struct amdgpu_fence *tmp = *fence;
745
746 *fence = NULL;
747 if (tmp)
748 fence_put(&tmp->base);
749}
750
751/**
752 * amdgpu_fence_count_emitted - get the count of emitted fences
753 *
754 * @ring: ring the fence is associated with
755 *
756 * Get the number of fences emitted on the requested ring (all asics).
757 * Returns the number of emitted fences on the ring. Used by the
758 * dynpm code to ring track activity.
759 */
760unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
761{
762 uint64_t emitted;
763
764 /* We are not protected by ring lock when reading the last sequence
765 * but it's ok to report slightly wrong fence count here.
766 */
767 amdgpu_fence_process(ring);
768 emitted = ring->fence_drv.sync_seq[ring->idx]
769 - atomic64_read(&ring->fence_drv.last_seq);
770 /* to avoid 32bits warp around */
771 if (emitted > 0x10000000)
772 emitted = 0x10000000;
773
774 return (unsigned)emitted;
775}
776
777/**
778 * amdgpu_fence_need_sync - do we need a semaphore
779 *
780 * @fence: amdgpu fence object
781 * @dst_ring: which ring to check against
782 *
783 * Check if the fence needs to be synced against another ring
784 * (all asics). If so, we need to emit a semaphore.
785 * Returns true if we need to sync with another ring, false if
786 * not.
787 */
788bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
789 struct amdgpu_ring *dst_ring)
790{
791 struct amdgpu_fence_driver *fdrv;
792
793 if (!fence)
794 return false;
795
796 if (fence->ring == dst_ring)
797 return false;
798
799 /* we are protected by the ring mutex */
800 fdrv = &dst_ring->fence_drv;
801 if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
802 return false;
803
804 return true;
805}
806
807/**
808 * amdgpu_fence_note_sync - record the sync point
809 *
810 * @fence: amdgpu fence object
811 * @dst_ring: which ring to check against
812 *
813 * Note the sequence number at which point the fence will
814 * be synced with the requested ring (all asics).
815 */
816void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
817 struct amdgpu_ring *dst_ring)
818{
819 struct amdgpu_fence_driver *dst, *src;
820 unsigned i;
821
822 if (!fence)
823 return;
824
825 if (fence->ring == dst_ring)
826 return;
827
828 /* we are protected by the ring mutex */
829 src = &fence->ring->fence_drv;
830 dst = &dst_ring->fence_drv;
831 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
832 if (i == dst_ring->idx)
833 continue;
834
835 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
836 }
837}
838
839/**
840 * amdgpu_fence_driver_start_ring - make the fence driver
841 * ready for use on the requested ring.
842 *
843 * @ring: ring to start the fence driver on
844 * @irq_src: interrupt source to use for this ring
845 * @irq_type: interrupt type to use for this ring
846 *
847 * Make the fence driver ready for processing (all asics).
848 * Not all asics have all rings, so each asic will only
849 * start the fence driver on the rings it has.
850 * Returns 0 for success, errors for failure.
851 */
852int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
853 struct amdgpu_irq_src *irq_src,
854 unsigned irq_type)
855{
856 struct amdgpu_device *adev = ring->adev;
857 uint64_t index;
858
859 if (ring != &adev->uvd.ring) {
860 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
861 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
862 } else {
863 /* put fence directly behind firmware */
864 index = ALIGN(adev->uvd.fw->size, 8);
865 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
866 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
867 }
868 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
Chunming Zhouc6a40792015-06-01 14:14:32 +0800869 amdgpu_irq_get(adev, irq_src, irq_type);
870
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400871 ring->fence_drv.irq_src = irq_src;
872 ring->fence_drv.irq_type = irq_type;
Chunming Zhouc6a40792015-06-01 14:14:32 +0800873 ring->fence_drv.initialized = true;
874
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
876 "cpu addr 0x%p\n", ring->idx,
877 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
878 return 0;
879}
880
881/**
882 * amdgpu_fence_driver_init_ring - init the fence driver
883 * for the requested ring.
884 *
885 * @ring: ring to init the fence driver on
886 *
887 * Init the fence driver for the requested ring (all asics).
888 * Helper function for amdgpu_fence_driver_init().
889 */
890void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
891{
892 int i;
893
894 ring->fence_drv.cpu_addr = NULL;
895 ring->fence_drv.gpu_addr = 0;
896 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
897 ring->fence_drv.sync_seq[i] = 0;
898
899 atomic64_set(&ring->fence_drv.last_seq, 0);
900 ring->fence_drv.initialized = false;
901
902 INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
903 amdgpu_fence_check_lockup);
904 ring->fence_drv.ring = ring;
905}
906
907/**
908 * amdgpu_fence_driver_init - init the fence driver
909 * for all possible rings.
910 *
911 * @adev: amdgpu device pointer
912 *
913 * Init the fence driver for all possible rings (all asics).
914 * Not all asics have all rings, so each asic will only
915 * start the fence driver on the rings it has using
916 * amdgpu_fence_driver_start_ring().
917 * Returns 0 for success.
918 */
919int amdgpu_fence_driver_init(struct amdgpu_device *adev)
920{
921 init_waitqueue_head(&adev->fence_queue);
922 if (amdgpu_debugfs_fence_init(adev))
923 dev_err(adev->dev, "fence debugfs file creation failed\n");
924
925 return 0;
926}
927
928/**
929 * amdgpu_fence_driver_fini - tear down the fence driver
930 * for all possible rings.
931 *
932 * @adev: amdgpu device pointer
933 *
934 * Tear down the fence driver for all possible rings (all asics).
935 */
936void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
937{
938 int i, r;
939
940 mutex_lock(&adev->ring_lock);
941 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
942 struct amdgpu_ring *ring = adev->rings[i];
943 if (!ring || !ring->fence_drv.initialized)
944 continue;
945 r = amdgpu_fence_wait_empty(ring);
946 if (r) {
947 /* no need to trigger GPU reset as we are unloading */
948 amdgpu_fence_driver_force_completion(adev);
949 }
950 wake_up_all(&adev->fence_queue);
Chunming Zhouc6a40792015-06-01 14:14:32 +0800951 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
952 ring->fence_drv.irq_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400953 ring->fence_drv.initialized = false;
954 }
955 mutex_unlock(&adev->ring_lock);
956}
957
958/**
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400959 * amdgpu_fence_driver_suspend - suspend the fence driver
960 * for all possible rings.
961 *
962 * @adev: amdgpu device pointer
963 *
964 * Suspend the fence driver for all possible rings (all asics).
965 */
966void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
967{
968 int i, r;
969
970 mutex_lock(&adev->ring_lock);
971 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
972 struct amdgpu_ring *ring = adev->rings[i];
973 if (!ring || !ring->fence_drv.initialized)
974 continue;
975
976 /* wait for gpu to finish processing current batch */
977 r = amdgpu_fence_wait_empty(ring);
978 if (r) {
979 /* delay GPU reset to resume */
980 amdgpu_fence_driver_force_completion(adev);
981 }
982
983 /* disable the interrupt */
984 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
985 ring->fence_drv.irq_type);
986 }
987 mutex_unlock(&adev->ring_lock);
988}
989
990/**
991 * amdgpu_fence_driver_resume - resume the fence driver
992 * for all possible rings.
993 *
994 * @adev: amdgpu device pointer
995 *
996 * Resume the fence driver for all possible rings (all asics).
997 * Not all asics have all rings, so each asic will only
998 * start the fence driver on the rings it has using
999 * amdgpu_fence_driver_start_ring().
1000 * Returns 0 for success.
1001 */
1002void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
1003{
1004 int i;
1005
1006 mutex_lock(&adev->ring_lock);
1007 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1008 struct amdgpu_ring *ring = adev->rings[i];
1009 if (!ring || !ring->fence_drv.initialized)
1010 continue;
1011
1012 /* enable the interrupt */
1013 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
1014 ring->fence_drv.irq_type);
1015 }
1016 mutex_unlock(&adev->ring_lock);
1017}
1018
1019/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001020 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
1021 *
1022 * @adev: amdgpu device pointer
1023 *
1024 * In case of GPU reset failure make sure no process keep waiting on fence
1025 * that will never complete.
1026 */
1027void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
1028{
1029 int i;
1030
1031 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
1032 struct amdgpu_ring *ring = adev->rings[i];
1033 if (!ring || !ring->fence_drv.initialized)
1034 continue;
1035
1036 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
1037 }
1038}
1039
1040
1041/*
1042 * Fence debugfs
1043 */
1044#if defined(CONFIG_DEBUG_FS)
1045static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
1046{
1047 struct drm_info_node *node = (struct drm_info_node *)m->private;
1048 struct drm_device *dev = node->minor->dev;
1049 struct amdgpu_device *adev = dev->dev_private;
1050 int i, j;
1051
1052 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1053 struct amdgpu_ring *ring = adev->rings[i];
1054 if (!ring || !ring->fence_drv.initialized)
1055 continue;
1056
1057 amdgpu_fence_process(ring);
1058
Christian König344c19f2015-06-02 15:47:16 +02001059 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001060 seq_printf(m, "Last signaled fence 0x%016llx\n",
1061 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
1062 seq_printf(m, "Last emitted 0x%016llx\n",
1063 ring->fence_drv.sync_seq[i]);
1064
1065 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
1066 struct amdgpu_ring *other = adev->rings[j];
Christian König344c19f2015-06-02 15:47:16 +02001067 if (i != j && other && other->fence_drv.initialized &&
1068 ring->fence_drv.sync_seq[j])
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001069 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
1070 j, ring->fence_drv.sync_seq[j]);
1071 }
1072 }
1073 return 0;
1074}
1075
1076static struct drm_info_list amdgpu_debugfs_fence_list[] = {
1077 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
1078};
1079#endif
1080
1081int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
1082{
1083#if defined(CONFIG_DEBUG_FS)
1084 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
1085#else
1086 return 0;
1087#endif
1088}
1089
1090static const char *amdgpu_fence_get_driver_name(struct fence *fence)
1091{
1092 return "amdgpu";
1093}
1094
1095static const char *amdgpu_fence_get_timeline_name(struct fence *f)
1096{
1097 struct amdgpu_fence *fence = to_amdgpu_fence(f);
1098 return (const char *)fence->ring->name;
1099}
1100
1101static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
1102{
1103 return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
1104}
1105
1106struct amdgpu_wait_cb {
1107 struct fence_cb base;
1108 struct task_struct *task;
1109};
1110
1111static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
1112{
1113 struct amdgpu_wait_cb *wait =
1114 container_of(cb, struct amdgpu_wait_cb, base);
1115 wake_up_process(wait->task);
1116}
1117
1118static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
1119 signed long t)
1120{
1121 struct amdgpu_fence *fence = to_amdgpu_fence(f);
1122 struct amdgpu_device *adev = fence->ring->adev;
1123 struct amdgpu_wait_cb cb;
1124
1125 cb.task = current;
1126
1127 if (fence_add_callback(f, &cb.base, amdgpu_fence_wait_cb))
1128 return t;
1129
1130 while (t > 0) {
1131 if (intr)
1132 set_current_state(TASK_INTERRUPTIBLE);
1133 else
1134 set_current_state(TASK_UNINTERRUPTIBLE);
1135
1136 /*
1137 * amdgpu_test_signaled must be called after
1138 * set_current_state to prevent a race with wake_up_process
1139 */
1140 if (amdgpu_test_signaled(fence))
1141 break;
1142
1143 if (adev->needs_reset) {
1144 t = -EDEADLK;
1145 break;
1146 }
1147
1148 t = schedule_timeout(t);
1149
1150 if (t > 0 && intr && signal_pending(current))
1151 t = -ERESTARTSYS;
1152 }
1153
1154 __set_current_state(TASK_RUNNING);
1155 fence_remove_callback(f, &cb.base);
1156
1157 return t;
1158}
1159
1160const struct fence_ops amdgpu_fence_ops = {
1161 .get_driver_name = amdgpu_fence_get_driver_name,
1162 .get_timeline_name = amdgpu_fence_get_timeline_name,
1163 .enable_signaling = amdgpu_fence_enable_signaling,
1164 .signaled = amdgpu_fence_is_signaled,
1165 .wait = amdgpu_fence_default_wait,
1166 .release = NULL,
1167};