drm/amdgpu: remove duplicate amdgpu_fence_process implementation
[deliverable/linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_fence.c
CommitLineData
d38ceaf9
AD
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 */
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,
7f06c236 129 &ring->fence_drv.fence_queue.lock,
130 adev->fence_context + ring->idx,
d38ceaf9 131 (*fence)->seq);
890ee23f
CZ
132 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
133 (*fence)->seq,
134 AMDGPU_FENCE_FLAG_INT);
d38ceaf9
AD
135 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
7f06c236 168 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
d38ceaf9
AD
169 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
86c2b790 197 * continuously new fence signaled ie amdgpu_fence_read needs
d38ceaf9
AD
198 * 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
86c2b790 203 * need to be interrupted after amdgpu_fence_read and before
d38ceaf9
AD
204 * 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
7f06c236 269 if (amdgpu_fence_activity(ring)) {
270 wake_up_all(&ring->fence_drv.fence_queue);
271 }
d38ceaf9
AD
272 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;
7f06c236 281 wake_up_all(&ring->fence_drv.fence_queue);
d38ceaf9
AD
282 }
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{
68ed3de4 297 if (amdgpu_fence_activity(ring))
7f06c236 298 wake_up_all(&ring->fence_drv.fence_queue);
d38ceaf9
AD
299}
300
301/**
302 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
303 *
304 * @ring: ring the fence is associated with
305 * @seq: sequence number
306 *
307 * Check if the last signaled fence sequnce number is >= the requested
308 * sequence number (all asics).
309 * Returns true if the fence has signaled (current fence value
310 * is >= requested value) or false if it has not (current fence
311 * value is < the requested value. Helper function for
312 * amdgpu_fence_signaled().
313 */
314static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
315{
316 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
317 return true;
318
319 /* poll new last sequence at least once */
320 amdgpu_fence_process(ring);
321 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
322 return true;
323
324 return false;
325}
326
327static bool amdgpu_fence_is_signaled(struct fence *f)
328{
329 struct amdgpu_fence *fence = to_amdgpu_fence(f);
330 struct amdgpu_ring *ring = fence->ring;
331 struct amdgpu_device *adev = ring->adev;
332
333 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
334 return true;
335
336 if (down_read_trylock(&adev->exclusive_lock)) {
337 amdgpu_fence_process(ring);
338 up_read(&adev->exclusive_lock);
339
340 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
341 return true;
342 }
343 return false;
344}
345
346/**
347 * amdgpu_fence_enable_signaling - enable signalling on fence
348 * @fence: fence
349 *
350 * This function is called with fence_queue lock held, and adds a callback
351 * to fence_queue that checks if this fence is signaled, and if so it
352 * signals the fence and removes itself.
353 */
354static bool amdgpu_fence_enable_signaling(struct fence *f)
355{
356 struct amdgpu_fence *fence = to_amdgpu_fence(f);
357 struct amdgpu_ring *ring = fence->ring;
d38ceaf9
AD
358
359 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
360 return false;
361
d38ceaf9
AD
362 fence->fence_wake.flags = 0;
363 fence->fence_wake.private = NULL;
364 fence->fence_wake.func = amdgpu_fence_check_signaled;
7f06c236 365 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
d38ceaf9
AD
366 fence_get(f);
367 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
368 return true;
369}
370
7f06c236 371/*
372 * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
373 * @ring: ring to wait on for the seq number
374 * @seq: seq number wait for
375 * @intr: if interruptible
376 * @timeout: jiffies before time out
377 *
378 * return value:
379 * 0: time out but seq not signaled, and gpu not hang
380 * X (X > 0): seq signaled and X means how many jiffies remains before time out
381 * -EDEADL: GPU hang before time out
382 * -ESYSRESTART: interrupted before seq signaled
383 * -EINVAL: some paramter is not valid
d38ceaf9 384 */
7f06c236 385static long amdgpu_fence_ring_wait_seq_timeout(struct amdgpu_ring *ring, uint64_t seq,
386 bool intr, long timeout)
d38ceaf9 387{
7f06c236 388 struct amdgpu_device *adev = ring->adev;
389 long r = 0;
390 bool signaled = false;
d38ceaf9 391
7f06c236 392 BUG_ON(!ring);
393 if (seq > ring->fence_drv.sync_seq[ring->idx])
394 return -EINVAL;
d38ceaf9 395
7f06c236 396 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
397 return timeout;
d38ceaf9 398
7f06c236 399 while (1) {
d38ceaf9 400 if (intr) {
7f06c236 401 r = wait_event_interruptible_timeout(ring->fence_drv.fence_queue, (
402 (signaled = amdgpu_fence_seq_signaled(ring, seq))
403 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
404
405 if (r == -ERESTARTSYS) /* interrupted */
406 return r;
d38ceaf9 407 } else {
7f06c236 408 r = wait_event_timeout(ring->fence_drv.fence_queue, (
409 (signaled = amdgpu_fence_seq_signaled(ring, seq))
410 || adev->needs_reset), AMDGPU_FENCE_JIFFIES_TIMEOUT);
d38ceaf9
AD
411 }
412
7f06c236 413 if (signaled) {
414 /* seq signaled */
415 if (timeout == MAX_SCHEDULE_TIMEOUT)
416 return timeout;
417 return (timeout - AMDGPU_FENCE_JIFFIES_TIMEOUT - r);
418 }
419 else if (adev->needs_reset) {
420 return -EDEADLK;
d38ceaf9
AD
421 }
422
7f06c236 423 /* check if it's a lockup */
424 if (amdgpu_ring_is_lockup(ring)) {
425 uint64_t last_seq = atomic64_read(&ring->fence_drv.last_seq);
426 /* ring lookup */
427 dev_warn(adev->dev, "GPU lockup (waiting for "
d38ceaf9
AD
428 "0x%016llx last fence id 0x%016llx on"
429 " ring %d)\n",
7f06c236 430 seq, last_seq, ring->idx);
431 wake_up_all(&ring->fence_drv.fence_queue);
432 return -EDEADLK;
433 }
d38ceaf9 434
7f06c236 435 if (timeout < MAX_SCHEDULE_TIMEOUT) {
436 timeout -= AMDGPU_FENCE_JIFFIES_TIMEOUT;
437 if (timeout < 1)
438 return 0;
d38ceaf9
AD
439 }
440 }
d38ceaf9
AD
441}
442
d38ceaf9
AD
443/**
444 * amdgpu_fence_wait_next - wait for the next fence to signal
445 *
446 * @adev: amdgpu device pointer
447 * @ring: ring index the fence is associated with
448 *
449 * Wait for the next fence on the requested ring to signal (all asics).
450 * Returns 0 if the next fence has passed, error for all other cases.
451 * Caller must hold ring lock.
452 */
453int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
454{
d38ceaf9
AD
455 long r;
456
7f06c236 457 uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
458 if (seq >= ring->fence_drv.sync_seq[ring->idx])
d38ceaf9 459 return -ENOENT;
7f06c236 460 r = amdgpu_fence_ring_wait_seq_timeout(ring, seq, false, MAX_SCHEDULE_TIMEOUT);
d38ceaf9
AD
461 if (r < 0)
462 return r;
7f06c236 463
d38ceaf9
AD
464 return 0;
465}
466
467/**
468 * amdgpu_fence_wait_empty - wait for all fences to signal
469 *
470 * @adev: amdgpu device pointer
471 * @ring: ring index the fence is associated with
472 *
473 * Wait for all fences on the requested ring to signal (all asics).
474 * Returns 0 if the fences have passed, error for all other cases.
475 * Caller must hold ring lock.
476 */
477int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
478{
d38ceaf9
AD
479 long r;
480
7f06c236 481 uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
482 if (!seq)
d38ceaf9
AD
483 return 0;
484
7f06c236 485 r = amdgpu_fence_ring_wait_seq_timeout(ring, seq, false, MAX_SCHEDULE_TIMEOUT);
486
d38ceaf9
AD
487 if (r < 0) {
488 if (r == -EDEADLK)
489 return -EDEADLK;
490
7f06c236 491 dev_err(ring->adev->dev, "error waiting for ring[%d] to become idle (%ld)\n",
492 ring->idx, r);
d38ceaf9
AD
493 }
494 return 0;
495}
496
497/**
498 * amdgpu_fence_ref - take a ref on a fence
499 *
500 * @fence: amdgpu fence object
501 *
502 * Take a reference on a fence (all asics).
503 * Returns the fence.
504 */
505struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
506{
507 fence_get(&fence->base);
508 return fence;
509}
510
511/**
512 * amdgpu_fence_unref - remove a ref on a fence
513 *
514 * @fence: amdgpu fence object
515 *
516 * Remove a reference on a fence (all asics).
517 */
518void amdgpu_fence_unref(struct amdgpu_fence **fence)
519{
520 struct amdgpu_fence *tmp = *fence;
521
522 *fence = NULL;
523 if (tmp)
524 fence_put(&tmp->base);
525}
526
527/**
528 * amdgpu_fence_count_emitted - get the count of emitted fences
529 *
530 * @ring: ring the fence is associated with
531 *
532 * Get the number of fences emitted on the requested ring (all asics).
533 * Returns the number of emitted fences on the ring. Used by the
534 * dynpm code to ring track activity.
535 */
536unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
537{
538 uint64_t emitted;
539
540 /* We are not protected by ring lock when reading the last sequence
541 * but it's ok to report slightly wrong fence count here.
542 */
543 amdgpu_fence_process(ring);
544 emitted = ring->fence_drv.sync_seq[ring->idx]
545 - atomic64_read(&ring->fence_drv.last_seq);
546 /* to avoid 32bits warp around */
547 if (emitted > 0x10000000)
548 emitted = 0x10000000;
549
550 return (unsigned)emitted;
551}
552
553/**
554 * amdgpu_fence_need_sync - do we need a semaphore
555 *
556 * @fence: amdgpu fence object
557 * @dst_ring: which ring to check against
558 *
559 * Check if the fence needs to be synced against another ring
560 * (all asics). If so, we need to emit a semaphore.
561 * Returns true if we need to sync with another ring, false if
562 * not.
563 */
564bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
565 struct amdgpu_ring *dst_ring)
566{
567 struct amdgpu_fence_driver *fdrv;
568
569 if (!fence)
570 return false;
571
572 if (fence->ring == dst_ring)
573 return false;
574
575 /* we are protected by the ring mutex */
576 fdrv = &dst_ring->fence_drv;
577 if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
578 return false;
579
580 return true;
581}
582
583/**
584 * amdgpu_fence_note_sync - record the sync point
585 *
586 * @fence: amdgpu fence object
587 * @dst_ring: which ring to check against
588 *
589 * Note the sequence number at which point the fence will
590 * be synced with the requested ring (all asics).
591 */
592void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
593 struct amdgpu_ring *dst_ring)
594{
595 struct amdgpu_fence_driver *dst, *src;
596 unsigned i;
597
598 if (!fence)
599 return;
600
601 if (fence->ring == dst_ring)
602 return;
603
604 /* we are protected by the ring mutex */
605 src = &fence->ring->fence_drv;
606 dst = &dst_ring->fence_drv;
607 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
608 if (i == dst_ring->idx)
609 continue;
610
611 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
612 }
613}
614
615/**
616 * amdgpu_fence_driver_start_ring - make the fence driver
617 * ready for use on the requested ring.
618 *
619 * @ring: ring to start the fence driver on
620 * @irq_src: interrupt source to use for this ring
621 * @irq_type: interrupt type to use for this ring
622 *
623 * Make the fence driver ready for processing (all asics).
624 * Not all asics have all rings, so each asic will only
625 * start the fence driver on the rings it has.
626 * Returns 0 for success, errors for failure.
627 */
628int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
629 struct amdgpu_irq_src *irq_src,
630 unsigned irq_type)
631{
632 struct amdgpu_device *adev = ring->adev;
633 uint64_t index;
634
635 if (ring != &adev->uvd.ring) {
636 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
637 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
638 } else {
639 /* put fence directly behind firmware */
640 index = ALIGN(adev->uvd.fw->size, 8);
641 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
642 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
643 }
644 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
c6a4079b
CZ
645 amdgpu_irq_get(adev, irq_src, irq_type);
646
d38ceaf9
AD
647 ring->fence_drv.irq_src = irq_src;
648 ring->fence_drv.irq_type = irq_type;
c6a4079b
CZ
649 ring->fence_drv.initialized = true;
650
d38ceaf9
AD
651 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
652 "cpu addr 0x%p\n", ring->idx,
653 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
654 return 0;
655}
656
657/**
658 * amdgpu_fence_driver_init_ring - init the fence driver
659 * for the requested ring.
660 *
661 * @ring: ring to init the fence driver on
662 *
663 * Init the fence driver for the requested ring (all asics).
664 * Helper function for amdgpu_fence_driver_init().
665 */
666void amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
667{
668 int i;
669
670 ring->fence_drv.cpu_addr = NULL;
671 ring->fence_drv.gpu_addr = 0;
672 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
673 ring->fence_drv.sync_seq[i] = 0;
674
675 atomic64_set(&ring->fence_drv.last_seq, 0);
676 ring->fence_drv.initialized = false;
677
678 INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
679 amdgpu_fence_check_lockup);
680 ring->fence_drv.ring = ring;
b80d8475
AD
681
682 if (amdgpu_enable_scheduler) {
683 ring->scheduler = amd_sched_create((void *)ring->adev,
c1b69ed0 684 &amdgpu_sched_ops,
4afcb303
JZ
685 ring->idx, 5, 0,
686 amdgpu_sched_hw_submission);
b80d8475
AD
687 if (!ring->scheduler)
688 DRM_ERROR("Failed to create scheduler on ring %d.\n",
689 ring->idx);
690 }
d38ceaf9
AD
691}
692
693/**
694 * amdgpu_fence_driver_init - init the fence driver
695 * for all possible rings.
696 *
697 * @adev: amdgpu device pointer
698 *
699 * Init the fence driver for all possible rings (all asics).
700 * Not all asics have all rings, so each asic will only
701 * start the fence driver on the rings it has using
702 * amdgpu_fence_driver_start_ring().
703 * Returns 0 for success.
704 */
705int amdgpu_fence_driver_init(struct amdgpu_device *adev)
706{
d38ceaf9
AD
707 if (amdgpu_debugfs_fence_init(adev))
708 dev_err(adev->dev, "fence debugfs file creation failed\n");
709
710 return 0;
711}
712
713/**
714 * amdgpu_fence_driver_fini - tear down the fence driver
715 * for all possible rings.
716 *
717 * @adev: amdgpu device pointer
718 *
719 * Tear down the fence driver for all possible rings (all asics).
720 */
721void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
722{
723 int i, r;
724
725 mutex_lock(&adev->ring_lock);
726 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
727 struct amdgpu_ring *ring = adev->rings[i];
728 if (!ring || !ring->fence_drv.initialized)
729 continue;
730 r = amdgpu_fence_wait_empty(ring);
731 if (r) {
732 /* no need to trigger GPU reset as we are unloading */
733 amdgpu_fence_driver_force_completion(adev);
734 }
7f06c236 735 wake_up_all(&ring->fence_drv.fence_queue);
c6a4079b
CZ
736 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
737 ring->fence_drv.irq_type);
b80d8475
AD
738 if (ring->scheduler)
739 amd_sched_destroy(ring->scheduler);
d38ceaf9
AD
740 ring->fence_drv.initialized = false;
741 }
742 mutex_unlock(&adev->ring_lock);
743}
744
5ceb54c6
AD
745/**
746 * amdgpu_fence_driver_suspend - suspend the fence driver
747 * for all possible rings.
748 *
749 * @adev: amdgpu device pointer
750 *
751 * Suspend the fence driver for all possible rings (all asics).
752 */
753void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
754{
755 int i, r;
756
757 mutex_lock(&adev->ring_lock);
758 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
759 struct amdgpu_ring *ring = adev->rings[i];
760 if (!ring || !ring->fence_drv.initialized)
761 continue;
762
763 /* wait for gpu to finish processing current batch */
764 r = amdgpu_fence_wait_empty(ring);
765 if (r) {
766 /* delay GPU reset to resume */
767 amdgpu_fence_driver_force_completion(adev);
768 }
769
770 /* disable the interrupt */
771 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
772 ring->fence_drv.irq_type);
773 }
774 mutex_unlock(&adev->ring_lock);
775}
776
777/**
778 * amdgpu_fence_driver_resume - resume the fence driver
779 * for all possible rings.
780 *
781 * @adev: amdgpu device pointer
782 *
783 * Resume the fence driver for all possible rings (all asics).
784 * Not all asics have all rings, so each asic will only
785 * start the fence driver on the rings it has using
786 * amdgpu_fence_driver_start_ring().
787 * Returns 0 for success.
788 */
789void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
790{
791 int i;
792
793 mutex_lock(&adev->ring_lock);
794 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
795 struct amdgpu_ring *ring = adev->rings[i];
796 if (!ring || !ring->fence_drv.initialized)
797 continue;
798
799 /* enable the interrupt */
800 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
801 ring->fence_drv.irq_type);
802 }
803 mutex_unlock(&adev->ring_lock);
804}
805
d38ceaf9
AD
806/**
807 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
808 *
809 * @adev: amdgpu device pointer
810 *
811 * In case of GPU reset failure make sure no process keep waiting on fence
812 * that will never complete.
813 */
814void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
815{
816 int i;
817
818 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
819 struct amdgpu_ring *ring = adev->rings[i];
820 if (!ring || !ring->fence_drv.initialized)
821 continue;
822
823 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
824 }
825}
826
827
828/*
829 * Fence debugfs
830 */
831#if defined(CONFIG_DEBUG_FS)
832static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
833{
834 struct drm_info_node *node = (struct drm_info_node *)m->private;
835 struct drm_device *dev = node->minor->dev;
836 struct amdgpu_device *adev = dev->dev_private;
837 int i, j;
838
839 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
840 struct amdgpu_ring *ring = adev->rings[i];
841 if (!ring || !ring->fence_drv.initialized)
842 continue;
843
844 amdgpu_fence_process(ring);
845
344c19f9 846 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
d38ceaf9
AD
847 seq_printf(m, "Last signaled fence 0x%016llx\n",
848 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
849 seq_printf(m, "Last emitted 0x%016llx\n",
850 ring->fence_drv.sync_seq[i]);
851
852 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
853 struct amdgpu_ring *other = adev->rings[j];
344c19f9
CK
854 if (i != j && other && other->fence_drv.initialized &&
855 ring->fence_drv.sync_seq[j])
d38ceaf9
AD
856 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
857 j, ring->fence_drv.sync_seq[j]);
858 }
859 }
860 return 0;
861}
862
863static struct drm_info_list amdgpu_debugfs_fence_list[] = {
864 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
865};
866#endif
867
868int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
869{
870#if defined(CONFIG_DEBUG_FS)
871 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
872#else
873 return 0;
874#endif
875}
876
877static const char *amdgpu_fence_get_driver_name(struct fence *fence)
878{
879 return "amdgpu";
880}
881
882static const char *amdgpu_fence_get_timeline_name(struct fence *f)
883{
884 struct amdgpu_fence *fence = to_amdgpu_fence(f);
885 return (const char *)fence->ring->name;
886}
887
888static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
889{
890 return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
891}
892
332dfe90 893static inline bool amdgpu_test_signaled_any(struct amdgpu_fence **fences)
894{
895 int idx;
896 struct amdgpu_fence *fence;
897
898 idx = 0;
899 for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
900 fence = fences[idx];
901 if (fence) {
902 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags))
903 return true;
904 }
905 }
906 return false;
907}
908
d38ceaf9
AD
909struct amdgpu_wait_cb {
910 struct fence_cb base;
911 struct task_struct *task;
912};
913
914static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
915{
916 struct amdgpu_wait_cb *wait =
917 container_of(cb, struct amdgpu_wait_cb, base);
918 wake_up_process(wait->task);
919}
920
921static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
922 signed long t)
923{
e2955155 924 struct amdgpu_fence *array[AMDGPU_MAX_RINGS];
d38ceaf9
AD
925 struct amdgpu_fence *fence = to_amdgpu_fence(f);
926 struct amdgpu_device *adev = fence->ring->adev;
d38ceaf9 927
e2955155 928 memset(&array[0], 0, sizeof(array));
929 array[0] = fence;
d38ceaf9 930
e2955155 931 return amdgpu_fence_wait_any(adev, array, intr, t);
d38ceaf9
AD
932}
933
332dfe90 934/* wait until any fence in array signaled */
935signed long amdgpu_fence_wait_any(struct amdgpu_device *adev,
936 struct amdgpu_fence **array, bool intr, signed long t)
937{
938 long idx = 0;
939 struct amdgpu_wait_cb cb[AMDGPU_MAX_RINGS];
940 struct amdgpu_fence *fence;
941
942 BUG_ON(!array);
943
944 for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
945 fence = array[idx];
946 if (fence) {
947 cb[idx].task = current;
948 if (fence_add_callback(&fence->base,
949 &cb[idx].base, amdgpu_fence_wait_cb))
950 return t; /* return if fence is already signaled */
951 }
952 }
953
954 while (t > 0) {
955 if (intr)
956 set_current_state(TASK_INTERRUPTIBLE);
957 else
958 set_current_state(TASK_UNINTERRUPTIBLE);
959
960 /*
961 * amdgpu_test_signaled_any must be called after
962 * set_current_state to prevent a race with wake_up_process
963 */
964 if (amdgpu_test_signaled_any(array))
965 break;
966
967 if (adev->needs_reset) {
968 t = -EDEADLK;
969 break;
970 }
971
972 t = schedule_timeout(t);
973
974 if (t > 0 && intr && signal_pending(current))
975 t = -ERESTARTSYS;
976 }
977
978 __set_current_state(TASK_RUNNING);
979
980 idx = 0;
981 for (idx = 0; idx < AMDGPU_MAX_RINGS; ++idx) {
982 fence = array[idx];
983 if (fence)
984 fence_remove_callback(&fence->base, &cb[idx].base);
985 }
986
987 return t;
988}
989
d38ceaf9
AD
990const struct fence_ops amdgpu_fence_ops = {
991 .get_driver_name = amdgpu_fence_get_driver_name,
992 .get_timeline_name = amdgpu_fence_get_timeline_name,
993 .enable_signaling = amdgpu_fence_enable_signaling,
994 .signaled = amdgpu_fence_is_signaled,
995 .wait = amdgpu_fence_default_wait,
996 .release = NULL,
997};
This page took 0.080363 seconds and 5 git commands to generate.