2 * Copyright 2009 Jerome Glisse.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Jerome Glisse <glisse@freedesktop.org>
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>
39 #include "amdgpu_trace.h"
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.
54 struct amdgpu_ring *ring;
57 wait_queue_t fence_wake;
60 static struct kmem_cache *amdgpu_fence_slab;
61 static atomic_t amdgpu_fence_slab_ref = ATOMIC_INIT(0);
66 static const struct fence_ops amdgpu_fence_ops;
67 static inline struct amdgpu_fence *to_amdgpu_fence(struct fence *f)
69 struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
71 if (__f->base.ops == &amdgpu_fence_ops)
78 * amdgpu_fence_write - write a fence value
80 * @ring: ring the fence is associated with
81 * @seq: sequence number to write
83 * Writes a fence value to memory (all asics).
85 static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
87 struct amdgpu_fence_driver *drv = &ring->fence_drv;
90 *drv->cpu_addr = cpu_to_le32(seq);
94 * amdgpu_fence_read - read a fence value
96 * @ring: ring the fence is associated with
98 * Reads a fence value from memory (all asics).
99 * Returns the value of the fence read from memory.
101 static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
103 struct amdgpu_fence_driver *drv = &ring->fence_drv;
107 seq = le32_to_cpu(*drv->cpu_addr);
109 seq = lower_32_bits(atomic64_read(&drv->last_seq));
115 * amdgpu_fence_emit - emit a fence on the requested ring
117 * @ring: ring the fence is associated with
118 * @f: resulting fence object
120 * Emits a fence command on the requested ring (all asics).
121 * Returns 0 on success, -ENOMEM on failure.
123 int amdgpu_fence_emit(struct amdgpu_ring *ring, struct fence **f)
125 struct amdgpu_device *adev = ring->adev;
126 struct amdgpu_fence *fence;
128 fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
132 fence->seq = ++ring->fence_drv.sync_seq;
134 fence_init(&fence->base, &amdgpu_fence_ops,
135 &ring->fence_drv.fence_queue.lock,
136 adev->fence_context + ring->idx,
138 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
139 fence->seq, AMDGPU_FENCE_FLAG_INT);
145 * amdgpu_fence_schedule_fallback - schedule fallback check
147 * @ring: pointer to struct amdgpu_ring
149 * Start a timer as fallback to our interrupts.
151 static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
153 mod_timer(&ring->fence_drv.fallback_timer,
154 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
158 * amdgpu_fence_activity - check for fence activity
160 * @ring: pointer to struct amdgpu_ring
162 * Checks the current fence value and calculates the last
163 * signalled fence value. Returns true if activity occured
164 * on the ring, and the fence_queue should be waken up.
166 static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
168 uint64_t seq, last_seq, last_emitted;
171 last_seq = atomic64_read(&ring->fence_drv.last_seq);
173 last_emitted = ring->fence_drv.sync_seq;
174 seq = amdgpu_fence_read(ring);
175 seq |= last_seq & 0xffffffff00000000LL;
176 if (seq < last_seq) {
178 seq |= last_emitted & 0xffffffff00000000LL;
181 if (seq <= last_seq || seq > last_emitted)
184 /* If we loop over we don't want to return without
185 * checking if a fence is signaled as it means that the
186 * seq we just read is different from the previous on.
191 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
193 if (seq < last_emitted)
194 amdgpu_fence_schedule_fallback(ring);
200 * amdgpu_fence_process - process a fence
202 * @adev: amdgpu_device pointer
203 * @ring: ring index the fence is associated with
205 * Checks the current fence value and wakes the fence queue
206 * if the sequence number has increased (all asics).
208 void amdgpu_fence_process(struct amdgpu_ring *ring)
210 if (amdgpu_fence_activity(ring))
211 wake_up_all(&ring->fence_drv.fence_queue);
215 * amdgpu_fence_fallback - fallback for hardware interrupts
217 * @work: delayed work item
219 * Checks for fence activity.
221 static void amdgpu_fence_fallback(unsigned long arg)
223 struct amdgpu_ring *ring = (void *)arg;
225 amdgpu_fence_process(ring);
229 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
231 * @ring: ring the fence is associated with
232 * @seq: sequence number
234 * Check if the last signaled fence sequnce number is >= the requested
235 * sequence number (all asics).
236 * Returns true if the fence has signaled (current fence value
237 * is >= requested value) or false if it has not (current fence
238 * value is < the requested value. Helper function for
239 * amdgpu_fence_signaled().
241 static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
243 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
246 /* poll new last sequence at least once */
247 amdgpu_fence_process(ring);
248 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
255 * amdgpu_ring_wait_seq - wait for seq of the specific ring to signal
256 * @ring: ring to wait on for the seq number
257 * @seq: seq number wait for
260 * 0: seq signaled, and gpu not hang
261 * -EINVAL: some paramter is not valid
263 static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
266 if (seq > ring->fence_drv.sync_seq)
269 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
272 amdgpu_fence_schedule_fallback(ring);
273 wait_event(ring->fence_drv.fence_queue,
274 amdgpu_fence_seq_signaled(ring, seq));
280 * amdgpu_fence_wait_empty - wait for all fences to signal
282 * @adev: amdgpu device pointer
283 * @ring: ring index the fence is associated with
285 * Wait for all fences on the requested ring to signal (all asics).
286 * Returns 0 if the fences have passed, error for all other cases.
287 * Caller must hold ring lock.
289 int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
291 uint64_t seq = ring->fence_drv.sync_seq;
296 return amdgpu_fence_ring_wait_seq(ring, seq);
300 * amdgpu_fence_count_emitted - get the count of emitted fences
302 * @ring: ring the fence is associated with
304 * Get the number of fences emitted on the requested ring (all asics).
305 * Returns the number of emitted fences on the ring. Used by the
306 * dynpm code to ring track activity.
308 unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
312 /* We are not protected by ring lock when reading the last sequence
313 * but it's ok to report slightly wrong fence count here.
315 amdgpu_fence_process(ring);
316 emitted = ring->fence_drv.sync_seq
317 - atomic64_read(&ring->fence_drv.last_seq);
318 /* to avoid 32bits warp around */
319 if (emitted > 0x10000000)
320 emitted = 0x10000000;
322 return (unsigned)emitted;
326 * amdgpu_fence_driver_start_ring - make the fence driver
327 * ready for use on the requested ring.
329 * @ring: ring to start the fence driver on
330 * @irq_src: interrupt source to use for this ring
331 * @irq_type: interrupt type to use for this ring
333 * Make the fence driver ready for processing (all asics).
334 * Not all asics have all rings, so each asic will only
335 * start the fence driver on the rings it has.
336 * Returns 0 for success, errors for failure.
338 int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
339 struct amdgpu_irq_src *irq_src,
342 struct amdgpu_device *adev = ring->adev;
345 if (ring != &adev->uvd.ring) {
346 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
347 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
349 /* put fence directly behind firmware */
350 index = ALIGN(adev->uvd.fw->size, 8);
351 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
352 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
354 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
355 amdgpu_irq_get(adev, irq_src, irq_type);
357 ring->fence_drv.irq_src = irq_src;
358 ring->fence_drv.irq_type = irq_type;
359 ring->fence_drv.initialized = true;
361 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
362 "cpu addr 0x%p\n", ring->idx,
363 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
368 * amdgpu_fence_driver_init_ring - init the fence driver
369 * for the requested ring.
371 * @ring: ring to init the fence driver on
373 * Init the fence driver for the requested ring (all asics).
374 * Helper function for amdgpu_fence_driver_init().
376 int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
381 ring->fence_drv.cpu_addr = NULL;
382 ring->fence_drv.gpu_addr = 0;
383 ring->fence_drv.sync_seq = 0;
384 atomic64_set(&ring->fence_drv.last_seq, 0);
385 ring->fence_drv.initialized = false;
387 setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
388 (unsigned long)ring);
390 init_waitqueue_head(&ring->fence_drv.fence_queue);
392 timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
396 * Delayed workqueue cannot use it directly,
397 * so the scheduler will not use delayed workqueue if
398 * MAX_SCHEDULE_TIMEOUT is set.
399 * Currently keep it simple and silly.
401 timeout = MAX_SCHEDULE_TIMEOUT;
403 r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
404 amdgpu_sched_hw_submission,
405 timeout, ring->name);
407 DRM_ERROR("Failed to create scheduler on ring %s.\n",
416 * amdgpu_fence_driver_init - init the fence driver
417 * for all possible rings.
419 * @adev: amdgpu device pointer
421 * Init the fence driver for all possible rings (all asics).
422 * Not all asics have all rings, so each asic will only
423 * start the fence driver on the rings it has using
424 * amdgpu_fence_driver_start_ring().
425 * Returns 0 for success.
427 int amdgpu_fence_driver_init(struct amdgpu_device *adev)
429 if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
430 amdgpu_fence_slab = kmem_cache_create(
431 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
432 SLAB_HWCACHE_ALIGN, NULL);
433 if (!amdgpu_fence_slab)
436 if (amdgpu_debugfs_fence_init(adev))
437 dev_err(adev->dev, "fence debugfs file creation failed\n");
443 * amdgpu_fence_driver_fini - tear down the fence driver
444 * for all possible rings.
446 * @adev: amdgpu device pointer
448 * Tear down the fence driver for all possible rings (all asics).
450 void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
454 if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
455 kmem_cache_destroy(amdgpu_fence_slab);
456 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
457 struct amdgpu_ring *ring = adev->rings[i];
459 if (!ring || !ring->fence_drv.initialized)
461 r = amdgpu_fence_wait_empty(ring);
463 /* no need to trigger GPU reset as we are unloading */
464 amdgpu_fence_driver_force_completion(adev);
466 wake_up_all(&ring->fence_drv.fence_queue);
467 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
468 ring->fence_drv.irq_type);
469 amd_sched_fini(&ring->sched);
470 del_timer_sync(&ring->fence_drv.fallback_timer);
471 ring->fence_drv.initialized = false;
476 * amdgpu_fence_driver_suspend - suspend the fence driver
477 * for all possible rings.
479 * @adev: amdgpu device pointer
481 * Suspend the fence driver for all possible rings (all asics).
483 void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
487 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
488 struct amdgpu_ring *ring = adev->rings[i];
489 if (!ring || !ring->fence_drv.initialized)
492 /* wait for gpu to finish processing current batch */
493 r = amdgpu_fence_wait_empty(ring);
495 /* delay GPU reset to resume */
496 amdgpu_fence_driver_force_completion(adev);
499 /* disable the interrupt */
500 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
501 ring->fence_drv.irq_type);
506 * amdgpu_fence_driver_resume - resume the fence driver
507 * for all possible rings.
509 * @adev: amdgpu device pointer
511 * Resume the fence driver for all possible rings (all asics).
512 * Not all asics have all rings, so each asic will only
513 * start the fence driver on the rings it has using
514 * amdgpu_fence_driver_start_ring().
515 * Returns 0 for success.
517 void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
521 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
522 struct amdgpu_ring *ring = adev->rings[i];
523 if (!ring || !ring->fence_drv.initialized)
526 /* enable the interrupt */
527 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
528 ring->fence_drv.irq_type);
533 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
535 * @adev: amdgpu device pointer
537 * In case of GPU reset failure make sure no process keep waiting on fence
538 * that will never complete.
540 void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
544 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
545 struct amdgpu_ring *ring = adev->rings[i];
546 if (!ring || !ring->fence_drv.initialized)
549 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
554 * Common fence implementation
557 static const char *amdgpu_fence_get_driver_name(struct fence *fence)
562 static const char *amdgpu_fence_get_timeline_name(struct fence *f)
564 struct amdgpu_fence *fence = to_amdgpu_fence(f);
565 return (const char *)fence->ring->name;
569 * amdgpu_fence_is_signaled - test if fence is signaled
573 * Test the fence sequence number if it is already signaled. If it isn't
574 * signaled start fence processing. Returns True if the fence is signaled.
576 static bool amdgpu_fence_is_signaled(struct fence *f)
578 struct amdgpu_fence *fence = to_amdgpu_fence(f);
579 struct amdgpu_ring *ring = fence->ring;
581 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
584 amdgpu_fence_process(ring);
586 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
593 * amdgpu_fence_check_signaled - callback from fence_queue
595 * this function is called with fence_queue lock held, which is also used
596 * for the fence locking itself, so unlocked variants are used for
597 * fence_signal, and remove_wait_queue.
599 static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
601 struct amdgpu_fence *fence;
602 struct amdgpu_device *adev;
606 fence = container_of(wait, struct amdgpu_fence, fence_wake);
607 adev = fence->ring->adev;
610 * We cannot use amdgpu_fence_process here because we're already
611 * in the waitqueue, in a call from wake_up_all.
613 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
614 if (seq >= fence->seq) {
615 ret = fence_signal_locked(&fence->base);
617 FENCE_TRACE(&fence->base, "signaled from irq context\n");
619 FENCE_TRACE(&fence->base, "was already signaled\n");
621 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
622 fence_put(&fence->base);
624 FENCE_TRACE(&fence->base, "pending\n");
629 * amdgpu_fence_enable_signaling - enable signalling on fence
632 * This function is called with fence_queue lock held, and adds a callback
633 * to fence_queue that checks if this fence is signaled, and if so it
634 * signals the fence and removes itself.
636 static bool amdgpu_fence_enable_signaling(struct fence *f)
638 struct amdgpu_fence *fence = to_amdgpu_fence(f);
639 struct amdgpu_ring *ring = fence->ring;
641 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
644 fence->fence_wake.flags = 0;
645 fence->fence_wake.private = NULL;
646 fence->fence_wake.func = amdgpu_fence_check_signaled;
647 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
649 if (!timer_pending(&ring->fence_drv.fallback_timer))
650 amdgpu_fence_schedule_fallback(ring);
651 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
655 static void amdgpu_fence_release(struct fence *f)
657 struct amdgpu_fence *fence = to_amdgpu_fence(f);
658 kmem_cache_free(amdgpu_fence_slab, fence);
661 static const struct fence_ops amdgpu_fence_ops = {
662 .get_driver_name = amdgpu_fence_get_driver_name,
663 .get_timeline_name = amdgpu_fence_get_timeline_name,
664 .enable_signaling = amdgpu_fence_enable_signaling,
665 .signaled = amdgpu_fence_is_signaled,
666 .wait = fence_default_wait,
667 .release = amdgpu_fence_release,
673 #if defined(CONFIG_DEBUG_FS)
674 static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
676 struct drm_info_node *node = (struct drm_info_node *)m->private;
677 struct drm_device *dev = node->minor->dev;
678 struct amdgpu_device *adev = dev->dev_private;
681 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
682 struct amdgpu_ring *ring = adev->rings[i];
683 if (!ring || !ring->fence_drv.initialized)
686 amdgpu_fence_process(ring);
688 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
689 seq_printf(m, "Last signaled fence 0x%016llx\n",
690 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
691 seq_printf(m, "Last emitted 0x%016llx\n",
692 ring->fence_drv.sync_seq);
698 * amdgpu_debugfs_gpu_reset - manually trigger a gpu reset
700 * Manually trigger a gpu reset at the next fence wait.
702 static int amdgpu_debugfs_gpu_reset(struct seq_file *m, void *data)
704 struct drm_info_node *node = (struct drm_info_node *) m->private;
705 struct drm_device *dev = node->minor->dev;
706 struct amdgpu_device *adev = dev->dev_private;
708 seq_printf(m, "gpu reset\n");
709 amdgpu_gpu_reset(adev);
714 static struct drm_info_list amdgpu_debugfs_fence_list[] = {
715 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
716 {"amdgpu_gpu_reset", &amdgpu_debugfs_gpu_reset, 0, NULL}
720 int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
722 #if defined(CONFIG_DEBUG_FS)
723 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);