blob: d94b13ac290fada67eef7ef1a4f27914090eaca4 [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
Chunming Zhoub49c84a2015-11-05 11:28:28 +080050static struct kmem_cache *amdgpu_fence_slab;
51static atomic_t amdgpu_fence_slab_ref = ATOMIC_INIT(0);
52
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053/**
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
90/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091 * amdgpu_fence_emit - emit a fence on the requested ring
92 *
93 * @ring: ring the fence is associated with
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094 * @fence: amdgpu fence object
95 *
96 * Emits a fence command on the requested ring (all asics).
97 * Returns 0 on success, -ENOMEM on failure.
98 */
Christian König336d1f52016-02-16 10:57:10 +010099int amdgpu_fence_emit(struct amdgpu_ring *ring, struct amdgpu_fence **fence)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100{
101 struct amdgpu_device *adev = ring->adev;
102
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800103 *fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104 if ((*fence) == NULL) {
105 return -ENOMEM;
106 }
Christian König5907a0d2016-01-18 15:16:53 +0100107 (*fence)->seq = ++ring->fence_drv.sync_seq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 (*fence)->ring = ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109 fence_init(&(*fence)->base, &amdgpu_fence_ops,
monk.liu7f06c232015-07-30 18:28:12 +0800110 &ring->fence_drv.fence_queue.lock,
111 adev->fence_context + ring->idx,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112 (*fence)->seq);
Chunming Zhou890ee232015-06-01 14:35:03 +0800113 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
114 (*fence)->seq,
115 AMDGPU_FENCE_FLAG_INT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 return 0;
117}
118
119/**
Christian Königc2776af2015-11-03 13:27:39 +0100120 * amdgpu_fence_schedule_fallback - schedule fallback check
121 *
122 * @ring: pointer to struct amdgpu_ring
123 *
124 * Start a timer as fallback to our interrupts.
125 */
126static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
127{
128 mod_timer(&ring->fence_drv.fallback_timer,
129 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
130}
131
132/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 * amdgpu_fence_activity - check for fence activity
134 *
135 * @ring: pointer to struct amdgpu_ring
136 *
137 * Checks the current fence value and calculates the last
138 * signalled fence value. Returns true if activity occured
139 * on the ring, and the fence_queue should be waken up.
140 */
141static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
142{
143 uint64_t seq, last_seq, last_emitted;
144 unsigned count_loop = 0;
145 bool wake = false;
146
147 /* Note there is a scenario here for an infinite loop but it's
148 * very unlikely to happen. For it to happen, the current polling
149 * process need to be interrupted by another process and another
150 * process needs to update the last_seq btw the atomic read and
151 * xchg of the current process.
152 *
153 * More over for this to go in infinite loop there need to be
Jammy Zhou86c2b792015-05-13 22:52:42 +0800154 * continuously new fence signaled ie amdgpu_fence_read needs
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155 * to return a different value each time for both the currently
156 * polling process and the other process that xchg the last_seq
157 * btw atomic read and xchg of the current process. And the
158 * value the other process set as last seq must be higher than
159 * the seq value we just read. Which means that current process
Jammy Zhou86c2b792015-05-13 22:52:42 +0800160 * need to be interrupted after amdgpu_fence_read and before
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161 * atomic xchg.
162 *
163 * To be even more safe we count the number of time we loop and
164 * we bail after 10 loop just accepting the fact that we might
165 * have temporarly set the last_seq not to the true real last
166 * seq but to an older one.
167 */
168 last_seq = atomic64_read(&ring->fence_drv.last_seq);
169 do {
Christian König5907a0d2016-01-18 15:16:53 +0100170 last_emitted = ring->fence_drv.sync_seq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171 seq = amdgpu_fence_read(ring);
172 seq |= last_seq & 0xffffffff00000000LL;
173 if (seq < last_seq) {
174 seq &= 0xffffffff;
175 seq |= last_emitted & 0xffffffff00000000LL;
176 }
177
178 if (seq <= last_seq || seq > last_emitted) {
179 break;
180 }
181 /* If we loop over we don't want to return without
182 * checking if a fence is signaled as it means that the
183 * seq we just read is different from the previous on.
184 */
185 wake = true;
186 last_seq = seq;
187 if ((count_loop++) > 10) {
188 /* We looped over too many time leave with the
189 * fact that we might have set an older fence
190 * seq then the current real last seq as signaled
191 * by the hw.
192 */
193 break;
194 }
195 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
196
197 if (seq < last_emitted)
Christian Königc2776af2015-11-03 13:27:39 +0100198 amdgpu_fence_schedule_fallback(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199
200 return wake;
201}
202
203/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204 * amdgpu_fence_process - process a fence
205 *
206 * @adev: amdgpu_device pointer
207 * @ring: ring index the fence is associated with
208 *
209 * Checks the current fence value and wakes the fence queue
210 * if the sequence number has increased (all asics).
211 */
212void amdgpu_fence_process(struct amdgpu_ring *ring)
213{
Christian König68ed3de2015-08-07 15:57:21 +0200214 if (amdgpu_fence_activity(ring))
monk.liu7f06c232015-07-30 18:28:12 +0800215 wake_up_all(&ring->fence_drv.fence_queue);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216}
217
218/**
Christian Königc2776af2015-11-03 13:27:39 +0100219 * amdgpu_fence_fallback - fallback for hardware interrupts
220 *
221 * @work: delayed work item
222 *
223 * Checks for fence activity.
224 */
225static void amdgpu_fence_fallback(unsigned long arg)
226{
227 struct amdgpu_ring *ring = (void *)arg;
228
229 amdgpu_fence_process(ring);
230}
231
232/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
234 *
235 * @ring: ring the fence is associated with
236 * @seq: sequence number
237 *
238 * Check if the last signaled fence sequnce number is >= the requested
239 * sequence number (all asics).
240 * Returns true if the fence has signaled (current fence value
241 * is >= requested value) or false if it has not (current fence
242 * value is < the requested value. Helper function for
243 * amdgpu_fence_signaled().
244 */
245static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
246{
247 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
248 return true;
249
250 /* poll new last sequence at least once */
251 amdgpu_fence_process(ring);
252 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
253 return true;
254
255 return false;
256}
257
monk.liu7f06c232015-07-30 18:28:12 +0800258/*
Christian König9b389662016-02-11 14:42:33 +0100259 * amdgpu_ring_wait_seq - wait for seq of the specific ring to signal
monk.liu7f06c232015-07-30 18:28:12 +0800260 * @ring: ring to wait on for the seq number
261 * @seq: seq number wait for
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400262 *
monk.liu7f06c232015-07-30 18:28:12 +0800263 * return value:
Christian König00d2a2b2015-08-07 16:15:36 +0200264 * 0: seq signaled, and gpu not hang
monk.liu7f06c232015-07-30 18:28:12 +0800265 * -EINVAL: some paramter is not valid
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 */
Christian König00d2a2b2015-08-07 16:15:36 +0200267static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268{
monk.liu7f06c232015-07-30 18:28:12 +0800269 BUG_ON(!ring);
Christian König5907a0d2016-01-18 15:16:53 +0100270 if (seq > ring->fence_drv.sync_seq)
monk.liu7f06c232015-07-30 18:28:12 +0800271 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272
monk.liu7f06c232015-07-30 18:28:12 +0800273 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
Christian König00d2a2b2015-08-07 16:15:36 +0200274 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400275
Christian Königc2776af2015-11-03 13:27:39 +0100276 amdgpu_fence_schedule_fallback(ring);
Christian König9b389662016-02-11 14:42:33 +0100277 wait_event(ring->fence_drv.fence_queue,
278 amdgpu_fence_seq_signaled(ring, seq));
monk.liu7f06c232015-07-30 18:28:12 +0800279
Christian König9b389662016-02-11 14:42:33 +0100280 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281}
282
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284 * amdgpu_fence_wait_next - wait for the next fence to signal
285 *
286 * @adev: amdgpu device pointer
287 * @ring: ring index the fence is associated with
288 *
289 * Wait for the next fence on the requested ring to signal (all asics).
290 * Returns 0 if the next fence has passed, error for all other cases.
291 * Caller must hold ring lock.
292 */
293int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
294{
monk.liu7f06c232015-07-30 18:28:12 +0800295 uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
Christian König00d2a2b2015-08-07 16:15:36 +0200296
Christian König5907a0d2016-01-18 15:16:53 +0100297 if (seq >= ring->fence_drv.sync_seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298 return -ENOENT;
monk.liu7f06c232015-07-30 18:28:12 +0800299
Christian König00d2a2b2015-08-07 16:15:36 +0200300 return amdgpu_fence_ring_wait_seq(ring, seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400301}
302
303/**
304 * amdgpu_fence_wait_empty - wait for all fences to signal
305 *
306 * @adev: amdgpu device pointer
307 * @ring: ring index the fence is associated with
308 *
309 * Wait for all fences on the requested ring to signal (all asics).
310 * Returns 0 if the fences have passed, error for all other cases.
311 * Caller must hold ring lock.
312 */
313int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
314{
Christian König5907a0d2016-01-18 15:16:53 +0100315 uint64_t seq = ring->fence_drv.sync_seq;
Christian König00d2a2b2015-08-07 16:15:36 +0200316
monk.liu7f06c232015-07-30 18:28:12 +0800317 if (!seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400318 return 0;
319
Christian König00d2a2b2015-08-07 16:15:36 +0200320 return amdgpu_fence_ring_wait_seq(ring, seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400321}
322
323/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400324 * amdgpu_fence_count_emitted - get the count of emitted fences
325 *
326 * @ring: ring the fence is associated with
327 *
328 * Get the number of fences emitted on the requested ring (all asics).
329 * Returns the number of emitted fences on the ring. Used by the
330 * dynpm code to ring track activity.
331 */
332unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
333{
334 uint64_t emitted;
335
336 /* We are not protected by ring lock when reading the last sequence
337 * but it's ok to report slightly wrong fence count here.
338 */
339 amdgpu_fence_process(ring);
Christian König5907a0d2016-01-18 15:16:53 +0100340 emitted = ring->fence_drv.sync_seq
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400341 - atomic64_read(&ring->fence_drv.last_seq);
342 /* to avoid 32bits warp around */
343 if (emitted > 0x10000000)
344 emitted = 0x10000000;
345
346 return (unsigned)emitted;
347}
348
349/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400350 * amdgpu_fence_driver_start_ring - make the fence driver
351 * ready for use on the requested ring.
352 *
353 * @ring: ring to start the fence driver on
354 * @irq_src: interrupt source to use for this ring
355 * @irq_type: interrupt type to use for this ring
356 *
357 * Make the fence driver ready for processing (all asics).
358 * Not all asics have all rings, so each asic will only
359 * start the fence driver on the rings it has.
360 * Returns 0 for success, errors for failure.
361 */
362int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
363 struct amdgpu_irq_src *irq_src,
364 unsigned irq_type)
365{
366 struct amdgpu_device *adev = ring->adev;
367 uint64_t index;
368
369 if (ring != &adev->uvd.ring) {
370 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
371 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
372 } else {
373 /* put fence directly behind firmware */
374 index = ALIGN(adev->uvd.fw->size, 8);
375 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
376 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
377 }
378 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
Chunming Zhouc6a40792015-06-01 14:14:32 +0800379 amdgpu_irq_get(adev, irq_src, irq_type);
380
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381 ring->fence_drv.irq_src = irq_src;
382 ring->fence_drv.irq_type = irq_type;
Chunming Zhouc6a40792015-06-01 14:14:32 +0800383 ring->fence_drv.initialized = true;
384
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400385 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
386 "cpu addr 0x%p\n", ring->idx,
387 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
388 return 0;
389}
390
391/**
392 * amdgpu_fence_driver_init_ring - init the fence driver
393 * for the requested ring.
394 *
395 * @ring: ring to init the fence driver on
396 *
397 * Init the fence driver for the requested ring (all asics).
398 * Helper function for amdgpu_fence_driver_init().
399 */
Christian König4f839a22015-09-08 20:22:31 +0200400int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400401{
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800402 long timeout;
Christian König5907a0d2016-01-18 15:16:53 +0100403 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400404
405 ring->fence_drv.cpu_addr = NULL;
406 ring->fence_drv.gpu_addr = 0;
Christian König5907a0d2016-01-18 15:16:53 +0100407 ring->fence_drv.sync_seq = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400408 atomic64_set(&ring->fence_drv.last_seq, 0);
409 ring->fence_drv.initialized = false;
410
Christian Königc2776af2015-11-03 13:27:39 +0100411 setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
412 (unsigned long)ring);
Alex Deucherb80d8472015-08-16 22:55:02 -0400413
Christian König5ec92a72015-09-07 18:43:02 +0200414 init_waitqueue_head(&ring->fence_drv.fence_queue);
415
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800416 timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
417 if (timeout == 0) {
418 /*
419 * FIXME:
420 * Delayed workqueue cannot use it directly,
421 * so the scheduler will not use delayed workqueue if
422 * MAX_SCHEDULE_TIMEOUT is set.
423 * Currently keep it simple and silly.
424 */
425 timeout = MAX_SCHEDULE_TIMEOUT;
426 }
427 r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
428 amdgpu_sched_hw_submission,
429 timeout, ring->name);
430 if (r) {
431 DRM_ERROR("Failed to create scheduler on ring %s.\n",
432 ring->name);
433 return r;
Alex Deucherb80d8472015-08-16 22:55:02 -0400434 }
Christian König4f839a22015-09-08 20:22:31 +0200435
436 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437}
438
439/**
440 * amdgpu_fence_driver_init - init the fence driver
441 * for all possible rings.
442 *
443 * @adev: amdgpu device pointer
444 *
445 * Init the fence driver for all possible rings (all asics).
446 * Not all asics have all rings, so each asic will only
447 * start the fence driver on the rings it has using
448 * amdgpu_fence_driver_start_ring().
449 * Returns 0 for success.
450 */
451int amdgpu_fence_driver_init(struct amdgpu_device *adev)
452{
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800453 if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
454 amdgpu_fence_slab = kmem_cache_create(
455 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
456 SLAB_HWCACHE_ALIGN, NULL);
457 if (!amdgpu_fence_slab)
458 return -ENOMEM;
459 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460 if (amdgpu_debugfs_fence_init(adev))
461 dev_err(adev->dev, "fence debugfs file creation failed\n");
462
463 return 0;
464}
465
466/**
467 * amdgpu_fence_driver_fini - tear down the fence driver
468 * for all possible rings.
469 *
470 * @adev: amdgpu device pointer
471 *
472 * Tear down the fence driver for all possible rings (all asics).
473 */
474void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
475{
476 int i, r;
477
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800478 if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
479 kmem_cache_destroy(amdgpu_fence_slab);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400480 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
481 struct amdgpu_ring *ring = adev->rings[i];
Christian Königc2776af2015-11-03 13:27:39 +0100482
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400483 if (!ring || !ring->fence_drv.initialized)
484 continue;
485 r = amdgpu_fence_wait_empty(ring);
486 if (r) {
487 /* no need to trigger GPU reset as we are unloading */
488 amdgpu_fence_driver_force_completion(adev);
489 }
monk.liu7f06c232015-07-30 18:28:12 +0800490 wake_up_all(&ring->fence_drv.fence_queue);
Chunming Zhouc6a40792015-06-01 14:14:32 +0800491 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
492 ring->fence_drv.irq_type);
Christian König4f839a22015-09-08 20:22:31 +0200493 amd_sched_fini(&ring->sched);
Christian Königc2776af2015-11-03 13:27:39 +0100494 del_timer_sync(&ring->fence_drv.fallback_timer);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400495 ring->fence_drv.initialized = false;
496 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497}
498
499/**
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400500 * amdgpu_fence_driver_suspend - suspend the fence driver
501 * for all possible rings.
502 *
503 * @adev: amdgpu device pointer
504 *
505 * Suspend the fence driver for all possible rings (all asics).
506 */
507void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
508{
509 int i, r;
510
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400511 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
512 struct amdgpu_ring *ring = adev->rings[i];
513 if (!ring || !ring->fence_drv.initialized)
514 continue;
515
516 /* wait for gpu to finish processing current batch */
517 r = amdgpu_fence_wait_empty(ring);
518 if (r) {
519 /* delay GPU reset to resume */
520 amdgpu_fence_driver_force_completion(adev);
521 }
522
523 /* disable the interrupt */
524 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
525 ring->fence_drv.irq_type);
526 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400527}
528
529/**
530 * amdgpu_fence_driver_resume - resume the fence driver
531 * for all possible rings.
532 *
533 * @adev: amdgpu device pointer
534 *
535 * Resume the fence driver for all possible rings (all asics).
536 * Not all asics have all rings, so each asic will only
537 * start the fence driver on the rings it has using
538 * amdgpu_fence_driver_start_ring().
539 * Returns 0 for success.
540 */
541void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
542{
543 int i;
544
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400545 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
546 struct amdgpu_ring *ring = adev->rings[i];
547 if (!ring || !ring->fence_drv.initialized)
548 continue;
549
550 /* enable the interrupt */
551 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
552 ring->fence_drv.irq_type);
553 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400554}
555
556/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400557 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
558 *
559 * @adev: amdgpu device pointer
560 *
561 * In case of GPU reset failure make sure no process keep waiting on fence
562 * that will never complete.
563 */
564void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
565{
566 int i;
567
568 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
569 struct amdgpu_ring *ring = adev->rings[i];
570 if (!ring || !ring->fence_drv.initialized)
571 continue;
572
Christian König5907a0d2016-01-18 15:16:53 +0100573 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574 }
575}
576
Christian Königa95e2642015-11-03 12:21:57 +0100577/*
578 * Common fence implementation
579 */
580
581static const char *amdgpu_fence_get_driver_name(struct fence *fence)
582{
583 return "amdgpu";
584}
585
586static const char *amdgpu_fence_get_timeline_name(struct fence *f)
587{
588 struct amdgpu_fence *fence = to_amdgpu_fence(f);
589 return (const char *)fence->ring->name;
590}
591
592/**
593 * amdgpu_fence_is_signaled - test if fence is signaled
594 *
595 * @f: fence to test
596 *
597 * Test the fence sequence number if it is already signaled. If it isn't
598 * signaled start fence processing. Returns True if the fence is signaled.
599 */
600static bool amdgpu_fence_is_signaled(struct fence *f)
601{
602 struct amdgpu_fence *fence = to_amdgpu_fence(f);
603 struct amdgpu_ring *ring = fence->ring;
604
605 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
606 return true;
607
608 amdgpu_fence_process(ring);
609
610 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
611 return true;
612
613 return false;
614}
615
616/**
617 * amdgpu_fence_check_signaled - callback from fence_queue
618 *
619 * this function is called with fence_queue lock held, which is also used
620 * for the fence locking itself, so unlocked variants are used for
621 * fence_signal, and remove_wait_queue.
622 */
623static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
624{
625 struct amdgpu_fence *fence;
626 struct amdgpu_device *adev;
627 u64 seq;
628 int ret;
629
630 fence = container_of(wait, struct amdgpu_fence, fence_wake);
631 adev = fence->ring->adev;
632
633 /*
634 * We cannot use amdgpu_fence_process here because we're already
635 * in the waitqueue, in a call from wake_up_all.
636 */
637 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
638 if (seq >= fence->seq) {
639 ret = fence_signal_locked(&fence->base);
640 if (!ret)
641 FENCE_TRACE(&fence->base, "signaled from irq context\n");
642 else
643 FENCE_TRACE(&fence->base, "was already signaled\n");
644
645 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
646 fence_put(&fence->base);
647 } else
648 FENCE_TRACE(&fence->base, "pending\n");
649 return 0;
650}
651
652/**
653 * amdgpu_fence_enable_signaling - enable signalling on fence
654 * @fence: fence
655 *
656 * This function is called with fence_queue lock held, and adds a callback
657 * to fence_queue that checks if this fence is signaled, and if so it
658 * signals the fence and removes itself.
659 */
660static bool amdgpu_fence_enable_signaling(struct fence *f)
661{
662 struct amdgpu_fence *fence = to_amdgpu_fence(f);
663 struct amdgpu_ring *ring = fence->ring;
664
665 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
666 return false;
667
668 fence->fence_wake.flags = 0;
669 fence->fence_wake.private = NULL;
670 fence->fence_wake.func = amdgpu_fence_check_signaled;
671 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
672 fence_get(f);
Christian Königc2776af2015-11-03 13:27:39 +0100673 if (!timer_pending(&ring->fence_drv.fallback_timer))
674 amdgpu_fence_schedule_fallback(ring);
Christian Königa95e2642015-11-03 12:21:57 +0100675 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
676 return true;
677}
678
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800679static void amdgpu_fence_release(struct fence *f)
680{
681 struct amdgpu_fence *fence = to_amdgpu_fence(f);
682 kmem_cache_free(amdgpu_fence_slab, fence);
683}
684
Christian Königa95e2642015-11-03 12:21:57 +0100685const struct fence_ops amdgpu_fence_ops = {
686 .get_driver_name = amdgpu_fence_get_driver_name,
687 .get_timeline_name = amdgpu_fence_get_timeline_name,
688 .enable_signaling = amdgpu_fence_enable_signaling,
689 .signaled = amdgpu_fence_is_signaled,
690 .wait = fence_default_wait,
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800691 .release = amdgpu_fence_release,
Christian Königa95e2642015-11-03 12:21:57 +0100692};
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693
694/*
695 * Fence debugfs
696 */
697#if defined(CONFIG_DEBUG_FS)
698static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
699{
700 struct drm_info_node *node = (struct drm_info_node *)m->private;
701 struct drm_device *dev = node->minor->dev;
702 struct amdgpu_device *adev = dev->dev_private;
Christian König5907a0d2016-01-18 15:16:53 +0100703 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704
705 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
706 struct amdgpu_ring *ring = adev->rings[i];
707 if (!ring || !ring->fence_drv.initialized)
708 continue;
709
710 amdgpu_fence_process(ring);
711
Christian König344c19f2015-06-02 15:47:16 +0200712 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 seq_printf(m, "Last signaled fence 0x%016llx\n",
714 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
715 seq_printf(m, "Last emitted 0x%016llx\n",
Christian König5907a0d2016-01-18 15:16:53 +0100716 ring->fence_drv.sync_seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717 }
718 return 0;
719}
720
Alex Deucher18db89b2016-01-14 10:25:22 -0500721/**
722 * amdgpu_debugfs_gpu_reset - manually trigger a gpu reset
723 *
724 * Manually trigger a gpu reset at the next fence wait.
725 */
726static int amdgpu_debugfs_gpu_reset(struct seq_file *m, void *data)
727{
728 struct drm_info_node *node = (struct drm_info_node *) m->private;
729 struct drm_device *dev = node->minor->dev;
730 struct amdgpu_device *adev = dev->dev_private;
731
732 seq_printf(m, "gpu reset\n");
733 amdgpu_gpu_reset(adev);
734
735 return 0;
736}
737
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738static struct drm_info_list amdgpu_debugfs_fence_list[] = {
739 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
Alex Deucher18db89b2016-01-14 10:25:22 -0500740 {"amdgpu_gpu_reset", &amdgpu_debugfs_gpu_reset, 0, NULL}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741};
742#endif
743
744int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
745{
746#if defined(CONFIG_DEBUG_FS)
Alex Deucher18db89b2016-01-14 10:25:22 -0500747 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400748#else
749 return 0;
750#endif
751}
752