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