blob: 257fce3563196d1a09ff57eb04d4c7bae2dd9048 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2009 Jerome Glisse.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
16 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
17 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
19 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
23 * of the Software.
24 *
25 */
26/*
27 * Authors:
28 * Jerome Glisse <glisse@freedesktop.org>
29 * Dave Airlie
30 */
31#include <linux/seq_file.h>
32#include <linux/atomic.h>
33#include <linux/wait.h>
34#include <linux/kref.h>
35#include <linux/slab.h>
36#include <linux/firmware.h>
37#include <drm/drmP.h>
38#include "amdgpu.h"
39#include "amdgpu_trace.h"
40
41/*
42 * Fences
43 * Fences mark an event in the GPUs pipeline and are used
44 * for GPU/CPU synchronization. When the fence is written,
45 * it is expected that all buffers associated with that fence
46 * are no longer in use by the associated ring on the GPU and
47 * that the the relevant GPU caches have been flushed.
48 */
49
50/**
51 * amdgpu_fence_write - write a fence value
52 *
53 * @ring: ring the fence is associated with
54 * @seq: sequence number to write
55 *
56 * Writes a fence value to memory (all asics).
57 */
58static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
59{
60 struct amdgpu_fence_driver *drv = &ring->fence_drv;
61
62 if (drv->cpu_addr)
63 *drv->cpu_addr = cpu_to_le32(seq);
64}
65
66/**
67 * amdgpu_fence_read - read a fence value
68 *
69 * @ring: ring the fence is associated with
70 *
71 * Reads a fence value from memory (all asics).
72 * Returns the value of the fence read from memory.
73 */
74static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
75{
76 struct amdgpu_fence_driver *drv = &ring->fence_drv;
77 u32 seq = 0;
78
79 if (drv->cpu_addr)
80 seq = le32_to_cpu(*drv->cpu_addr);
81 else
82 seq = lower_32_bits(atomic64_read(&drv->last_seq));
83
84 return seq;
85}
86
87/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088 * amdgpu_fence_emit - emit a fence on the requested ring
89 *
90 * @ring: ring the fence is associated with
91 * @owner: creator of the fence
92 * @fence: amdgpu fence object
93 *
94 * Emits a fence command on the requested ring (all asics).
95 * Returns 0 on success, -ENOMEM on failure.
96 */
97int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
98 struct amdgpu_fence **fence)
99{
100 struct amdgpu_device *adev = ring->adev;
101
102 /* we are protected by the ring emission mutex */
103 *fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL);
104 if ((*fence) == NULL) {
105 return -ENOMEM;
106 }
107 (*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx];
108 (*fence)->ring = ring;
109 (*fence)->owner = owner;
110 fence_init(&(*fence)->base, &amdgpu_fence_ops,
monk.liu7f06c232015-07-30 18:28:12 +0800111 &ring->fence_drv.fence_queue.lock,
112 adev->fence_context + ring->idx,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113 (*fence)->seq);
Chunming Zhou890ee232015-06-01 14:35:03 +0800114 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
115 (*fence)->seq,
116 AMDGPU_FENCE_FLAG_INT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117 return 0;
118}
119
120/**
Christian Königc2776af2015-11-03 13:27:39 +0100121 * amdgpu_fence_schedule_fallback - schedule fallback check
122 *
123 * @ring: pointer to struct amdgpu_ring
124 *
125 * Start a timer as fallback to our interrupts.
126 */
127static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
128{
129 mod_timer(&ring->fence_drv.fallback_timer,
130 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
131}
132
133/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 * amdgpu_fence_activity - check for fence activity
135 *
136 * @ring: pointer to struct amdgpu_ring
137 *
138 * Checks the current fence value and calculates the last
139 * signalled fence value. Returns true if activity occured
140 * on the ring, and the fence_queue should be waken up.
141 */
142static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
143{
144 uint64_t seq, last_seq, last_emitted;
145 unsigned count_loop = 0;
146 bool wake = false;
147
148 /* Note there is a scenario here for an infinite loop but it's
149 * very unlikely to happen. For it to happen, the current polling
150 * process need to be interrupted by another process and another
151 * process needs to update the last_seq btw the atomic read and
152 * xchg of the current process.
153 *
154 * More over for this to go in infinite loop there need to be
Jammy Zhou86c2b792015-05-13 22:52:42 +0800155 * continuously new fence signaled ie amdgpu_fence_read needs
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 * to return a different value each time for both the currently
157 * polling process and the other process that xchg the last_seq
158 * btw atomic read and xchg of the current process. And the
159 * value the other process set as last seq must be higher than
160 * the seq value we just read. Which means that current process
Jammy Zhou86c2b792015-05-13 22:52:42 +0800161 * need to be interrupted after amdgpu_fence_read and before
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400162 * atomic xchg.
163 *
164 * To be even more safe we count the number of time we loop and
165 * we bail after 10 loop just accepting the fact that we might
166 * have temporarly set the last_seq not to the true real last
167 * seq but to an older one.
168 */
169 last_seq = atomic64_read(&ring->fence_drv.last_seq);
170 do {
171 last_emitted = ring->fence_drv.sync_seq[ring->idx];
172 seq = amdgpu_fence_read(ring);
173 seq |= last_seq & 0xffffffff00000000LL;
174 if (seq < last_seq) {
175 seq &= 0xffffffff;
176 seq |= last_emitted & 0xffffffff00000000LL;
177 }
178
179 if (seq <= last_seq || seq > last_emitted) {
180 break;
181 }
182 /* If we loop over we don't want to return without
183 * checking if a fence is signaled as it means that the
184 * seq we just read is different from the previous on.
185 */
186 wake = true;
187 last_seq = seq;
188 if ((count_loop++) > 10) {
189 /* We looped over too many time leave with the
190 * fact that we might have set an older fence
191 * seq then the current real last seq as signaled
192 * by the hw.
193 */
194 break;
195 }
196 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
197
198 if (seq < last_emitted)
Christian Königc2776af2015-11-03 13:27:39 +0100199 amdgpu_fence_schedule_fallback(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400200
201 return wake;
202}
203
204/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400205 * amdgpu_fence_process - process a fence
206 *
207 * @adev: amdgpu_device pointer
208 * @ring: ring index the fence is associated with
209 *
210 * Checks the current fence value and wakes the fence queue
211 * if the sequence number has increased (all asics).
212 */
213void amdgpu_fence_process(struct amdgpu_ring *ring)
214{
Christian König68ed3de2015-08-07 15:57:21 +0200215 if (amdgpu_fence_activity(ring))
monk.liu7f06c232015-07-30 18:28:12 +0800216 wake_up_all(&ring->fence_drv.fence_queue);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400217}
218
219/**
Christian Königc2776af2015-11-03 13:27:39 +0100220 * amdgpu_fence_fallback - fallback for hardware interrupts
221 *
222 * @work: delayed work item
223 *
224 * Checks for fence activity.
225 */
226static void amdgpu_fence_fallback(unsigned long arg)
227{
228 struct amdgpu_ring *ring = (void *)arg;
229
230 amdgpu_fence_process(ring);
231}
232
233/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
235 *
236 * @ring: ring the fence is associated with
237 * @seq: sequence number
238 *
239 * Check if the last signaled fence sequnce number is >= the requested
240 * sequence number (all asics).
241 * Returns true if the fence has signaled (current fence value
242 * is >= requested value) or false if it has not (current fence
243 * value is < the requested value. Helper function for
244 * amdgpu_fence_signaled().
245 */
246static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
247{
248 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
249 return true;
250
251 /* poll new last sequence at least once */
252 amdgpu_fence_process(ring);
253 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
254 return true;
255
256 return false;
257}
258
monk.liu7f06c232015-07-30 18:28:12 +0800259/*
260 * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
261 * @ring: ring to wait on for the seq number
262 * @seq: seq number wait for
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400263 *
monk.liu7f06c232015-07-30 18:28:12 +0800264 * return value:
Christian König00d2a2b2015-08-07 16:15:36 +0200265 * 0: seq signaled, and gpu not hang
266 * -EDEADL: GPU hang detected
monk.liu7f06c232015-07-30 18:28:12 +0800267 * -EINVAL: some paramter is not valid
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268 */
Christian König00d2a2b2015-08-07 16:15:36 +0200269static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270{
monk.liu7f06c232015-07-30 18:28:12 +0800271 bool signaled = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272
monk.liu7f06c232015-07-30 18:28:12 +0800273 BUG_ON(!ring);
274 if (seq > ring->fence_drv.sync_seq[ring->idx])
275 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400276
monk.liu7f06c232015-07-30 18:28:12 +0800277 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
Christian König00d2a2b2015-08-07 16:15:36 +0200278 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400279
Christian Königc2776af2015-11-03 13:27:39 +0100280 amdgpu_fence_schedule_fallback(ring);
Christian König00d2a2b2015-08-07 16:15:36 +0200281 wait_event(ring->fence_drv.fence_queue, (
Christian Königb7e4dad2015-09-01 10:50:26 +0200282 (signaled = amdgpu_fence_seq_signaled(ring, seq))));
monk.liu7f06c232015-07-30 18:28:12 +0800283
Christian König00d2a2b2015-08-07 16:15:36 +0200284 if (signaled)
285 return 0;
286 else
287 return -EDEADLK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400288}
289
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400290/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400291 * amdgpu_fence_wait_next - wait for the next fence to signal
292 *
293 * @adev: amdgpu device pointer
294 * @ring: ring index the fence is associated with
295 *
296 * Wait for the next fence on the requested ring to signal (all asics).
297 * Returns 0 if the next fence has passed, error for all other cases.
298 * Caller must hold ring lock.
299 */
300int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
301{
monk.liu7f06c232015-07-30 18:28:12 +0800302 uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
Christian König00d2a2b2015-08-07 16:15:36 +0200303
monk.liu7f06c232015-07-30 18:28:12 +0800304 if (seq >= ring->fence_drv.sync_seq[ring->idx])
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400305 return -ENOENT;
monk.liu7f06c232015-07-30 18:28:12 +0800306
Christian König00d2a2b2015-08-07 16:15:36 +0200307 return amdgpu_fence_ring_wait_seq(ring, seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400308}
309
310/**
311 * amdgpu_fence_wait_empty - wait for all fences to signal
312 *
313 * @adev: amdgpu device pointer
314 * @ring: ring index the fence is associated with
315 *
316 * Wait for all fences on the requested ring to signal (all asics).
317 * Returns 0 if the fences have passed, error for all other cases.
318 * Caller must hold ring lock.
319 */
320int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
321{
monk.liu7f06c232015-07-30 18:28:12 +0800322 uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
Christian König00d2a2b2015-08-07 16:15:36 +0200323
monk.liu7f06c232015-07-30 18:28:12 +0800324 if (!seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400325 return 0;
326
Christian König00d2a2b2015-08-07 16:15:36 +0200327 return amdgpu_fence_ring_wait_seq(ring, seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400328}
329
330/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400331 * amdgpu_fence_count_emitted - get the count of emitted fences
332 *
333 * @ring: ring the fence is associated with
334 *
335 * Get the number of fences emitted on the requested ring (all asics).
336 * Returns the number of emitted fences on the ring. Used by the
337 * dynpm code to ring track activity.
338 */
339unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
340{
341 uint64_t emitted;
342
343 /* We are not protected by ring lock when reading the last sequence
344 * but it's ok to report slightly wrong fence count here.
345 */
346 amdgpu_fence_process(ring);
347 emitted = ring->fence_drv.sync_seq[ring->idx]
348 - atomic64_read(&ring->fence_drv.last_seq);
349 /* to avoid 32bits warp around */
350 if (emitted > 0x10000000)
351 emitted = 0x10000000;
352
353 return (unsigned)emitted;
354}
355
356/**
357 * amdgpu_fence_need_sync - do we need a semaphore
358 *
359 * @fence: amdgpu fence object
360 * @dst_ring: which ring to check against
361 *
362 * Check if the fence needs to be synced against another ring
363 * (all asics). If so, we need to emit a semaphore.
364 * Returns true if we need to sync with another ring, false if
365 * not.
366 */
367bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
368 struct amdgpu_ring *dst_ring)
369{
370 struct amdgpu_fence_driver *fdrv;
371
372 if (!fence)
373 return false;
374
375 if (fence->ring == dst_ring)
376 return false;
377
378 /* we are protected by the ring mutex */
379 fdrv = &dst_ring->fence_drv;
380 if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
381 return false;
382
383 return true;
384}
385
386/**
387 * amdgpu_fence_note_sync - record the sync point
388 *
389 * @fence: amdgpu fence object
390 * @dst_ring: which ring to check against
391 *
392 * Note the sequence number at which point the fence will
393 * be synced with the requested ring (all asics).
394 */
395void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
396 struct amdgpu_ring *dst_ring)
397{
398 struct amdgpu_fence_driver *dst, *src;
399 unsigned i;
400
401 if (!fence)
402 return;
403
404 if (fence->ring == dst_ring)
405 return;
406
407 /* we are protected by the ring mutex */
408 src = &fence->ring->fence_drv;
409 dst = &dst_ring->fence_drv;
410 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
411 if (i == dst_ring->idx)
412 continue;
413
414 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
415 }
416}
417
418/**
419 * amdgpu_fence_driver_start_ring - make the fence driver
420 * ready for use on the requested ring.
421 *
422 * @ring: ring to start the fence driver on
423 * @irq_src: interrupt source to use for this ring
424 * @irq_type: interrupt type to use for this ring
425 *
426 * Make the fence driver ready for processing (all asics).
427 * Not all asics have all rings, so each asic will only
428 * start the fence driver on the rings it has.
429 * Returns 0 for success, errors for failure.
430 */
431int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
432 struct amdgpu_irq_src *irq_src,
433 unsigned irq_type)
434{
435 struct amdgpu_device *adev = ring->adev;
436 uint64_t index;
437
438 if (ring != &adev->uvd.ring) {
439 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
440 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
441 } else {
442 /* put fence directly behind firmware */
443 index = ALIGN(adev->uvd.fw->size, 8);
444 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
445 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
446 }
447 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
Chunming Zhouc6a40792015-06-01 14:14:32 +0800448 amdgpu_irq_get(adev, irq_src, irq_type);
449
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400450 ring->fence_drv.irq_src = irq_src;
451 ring->fence_drv.irq_type = irq_type;
Chunming Zhouc6a40792015-06-01 14:14:32 +0800452 ring->fence_drv.initialized = true;
453
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
455 "cpu addr 0x%p\n", ring->idx,
456 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
457 return 0;
458}
459
460/**
461 * amdgpu_fence_driver_init_ring - init the fence driver
462 * for the requested ring.
463 *
464 * @ring: ring to init the fence driver on
465 *
466 * Init the fence driver for the requested ring (all asics).
467 * Helper function for amdgpu_fence_driver_init().
468 */
Christian König4f839a22015-09-08 20:22:31 +0200469int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470{
Christian König4f839a22015-09-08 20:22:31 +0200471 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472
473 ring->fence_drv.cpu_addr = NULL;
474 ring->fence_drv.gpu_addr = 0;
475 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
476 ring->fence_drv.sync_seq[i] = 0;
477
478 atomic64_set(&ring->fence_drv.last_seq, 0);
479 ring->fence_drv.initialized = false;
480
Christian Königc2776af2015-11-03 13:27:39 +0100481 setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
482 (unsigned long)ring);
Alex Deucherb80d8472015-08-16 22:55:02 -0400483
Christian König5ec92a72015-09-07 18:43:02 +0200484 init_waitqueue_head(&ring->fence_drv.fence_queue);
485
Alex Deucherb80d8472015-08-16 22:55:02 -0400486 if (amdgpu_enable_scheduler) {
Junwei Zhang2440ff22015-10-10 08:48:42 +0800487 long timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
488 if (timeout == 0) {
489 /*
490 * FIXME:
491 * Delayed workqueue cannot use it directly,
492 * so the scheduler will not use delayed workqueue if
493 * MAX_SCHEDULE_TIMEOUT is set.
494 * Currently keep it simple and silly.
495 */
496 timeout = MAX_SCHEDULE_TIMEOUT;
497 }
Christian König4f839a22015-09-08 20:22:31 +0200498 r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
Junwei Zhang2440ff22015-10-10 08:48:42 +0800499 amdgpu_sched_hw_submission,
500 timeout, ring->name);
Christian König4f839a22015-09-08 20:22:31 +0200501 if (r) {
502 DRM_ERROR("Failed to create scheduler on ring %s.\n",
503 ring->name);
504 return r;
505 }
Alex Deucherb80d8472015-08-16 22:55:02 -0400506 }
Christian König4f839a22015-09-08 20:22:31 +0200507
508 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509}
510
511/**
512 * amdgpu_fence_driver_init - init the fence driver
513 * for all possible rings.
514 *
515 * @adev: amdgpu device pointer
516 *
517 * Init the fence driver for all possible rings (all asics).
518 * Not all asics have all rings, so each asic will only
519 * start the fence driver on the rings it has using
520 * amdgpu_fence_driver_start_ring().
521 * Returns 0 for success.
522 */
523int amdgpu_fence_driver_init(struct amdgpu_device *adev)
524{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525 if (amdgpu_debugfs_fence_init(adev))
526 dev_err(adev->dev, "fence debugfs file creation failed\n");
527
528 return 0;
529}
530
531/**
532 * amdgpu_fence_driver_fini - tear down the fence driver
533 * for all possible rings.
534 *
535 * @adev: amdgpu device pointer
536 *
537 * Tear down the fence driver for all possible rings (all asics).
538 */
539void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
540{
541 int i, r;
542
543 mutex_lock(&adev->ring_lock);
544 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
545 struct amdgpu_ring *ring = adev->rings[i];
Christian Königc2776af2015-11-03 13:27:39 +0100546
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547 if (!ring || !ring->fence_drv.initialized)
548 continue;
549 r = amdgpu_fence_wait_empty(ring);
550 if (r) {
551 /* no need to trigger GPU reset as we are unloading */
552 amdgpu_fence_driver_force_completion(adev);
553 }
monk.liu7f06c232015-07-30 18:28:12 +0800554 wake_up_all(&ring->fence_drv.fence_queue);
Chunming Zhouc6a40792015-06-01 14:14:32 +0800555 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
556 ring->fence_drv.irq_type);
Christian König4f839a22015-09-08 20:22:31 +0200557 amd_sched_fini(&ring->sched);
Christian Königc2776af2015-11-03 13:27:39 +0100558 del_timer_sync(&ring->fence_drv.fallback_timer);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559 ring->fence_drv.initialized = false;
560 }
561 mutex_unlock(&adev->ring_lock);
562}
563
564/**
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400565 * amdgpu_fence_driver_suspend - suspend the fence driver
566 * for all possible rings.
567 *
568 * @adev: amdgpu device pointer
569 *
570 * Suspend the fence driver for all possible rings (all asics).
571 */
572void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
573{
574 int i, r;
575
576 mutex_lock(&adev->ring_lock);
577 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
578 struct amdgpu_ring *ring = adev->rings[i];
579 if (!ring || !ring->fence_drv.initialized)
580 continue;
581
582 /* wait for gpu to finish processing current batch */
583 r = amdgpu_fence_wait_empty(ring);
584 if (r) {
585 /* delay GPU reset to resume */
586 amdgpu_fence_driver_force_completion(adev);
587 }
588
589 /* disable the interrupt */
590 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
591 ring->fence_drv.irq_type);
592 }
593 mutex_unlock(&adev->ring_lock);
594}
595
596/**
597 * amdgpu_fence_driver_resume - resume the fence driver
598 * for all possible rings.
599 *
600 * @adev: amdgpu device pointer
601 *
602 * Resume the fence driver for all possible rings (all asics).
603 * Not all asics have all rings, so each asic will only
604 * start the fence driver on the rings it has using
605 * amdgpu_fence_driver_start_ring().
606 * Returns 0 for success.
607 */
608void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
609{
610 int i;
611
612 mutex_lock(&adev->ring_lock);
613 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
614 struct amdgpu_ring *ring = adev->rings[i];
615 if (!ring || !ring->fence_drv.initialized)
616 continue;
617
618 /* enable the interrupt */
619 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
620 ring->fence_drv.irq_type);
621 }
622 mutex_unlock(&adev->ring_lock);
623}
624
625/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
627 *
628 * @adev: amdgpu device pointer
629 *
630 * In case of GPU reset failure make sure no process keep waiting on fence
631 * that will never complete.
632 */
633void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
634{
635 int i;
636
637 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
638 struct amdgpu_ring *ring = adev->rings[i];
639 if (!ring || !ring->fence_drv.initialized)
640 continue;
641
642 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
643 }
644}
645
Christian Königa95e2642015-11-03 12:21:57 +0100646/*
647 * Common fence implementation
648 */
649
650static const char *amdgpu_fence_get_driver_name(struct fence *fence)
651{
652 return "amdgpu";
653}
654
655static const char *amdgpu_fence_get_timeline_name(struct fence *f)
656{
657 struct amdgpu_fence *fence = to_amdgpu_fence(f);
658 return (const char *)fence->ring->name;
659}
660
661/**
662 * amdgpu_fence_is_signaled - test if fence is signaled
663 *
664 * @f: fence to test
665 *
666 * Test the fence sequence number if it is already signaled. If it isn't
667 * signaled start fence processing. Returns True if the fence is signaled.
668 */
669static bool amdgpu_fence_is_signaled(struct fence *f)
670{
671 struct amdgpu_fence *fence = to_amdgpu_fence(f);
672 struct amdgpu_ring *ring = fence->ring;
673
674 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
675 return true;
676
677 amdgpu_fence_process(ring);
678
679 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
680 return true;
681
682 return false;
683}
684
685/**
686 * amdgpu_fence_check_signaled - callback from fence_queue
687 *
688 * this function is called with fence_queue lock held, which is also used
689 * for the fence locking itself, so unlocked variants are used for
690 * fence_signal, and remove_wait_queue.
691 */
692static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
693{
694 struct amdgpu_fence *fence;
695 struct amdgpu_device *adev;
696 u64 seq;
697 int ret;
698
699 fence = container_of(wait, struct amdgpu_fence, fence_wake);
700 adev = fence->ring->adev;
701
702 /*
703 * We cannot use amdgpu_fence_process here because we're already
704 * in the waitqueue, in a call from wake_up_all.
705 */
706 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
707 if (seq >= fence->seq) {
708 ret = fence_signal_locked(&fence->base);
709 if (!ret)
710 FENCE_TRACE(&fence->base, "signaled from irq context\n");
711 else
712 FENCE_TRACE(&fence->base, "was already signaled\n");
713
714 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
715 fence_put(&fence->base);
716 } else
717 FENCE_TRACE(&fence->base, "pending\n");
718 return 0;
719}
720
721/**
722 * amdgpu_fence_enable_signaling - enable signalling on fence
723 * @fence: fence
724 *
725 * This function is called with fence_queue lock held, and adds a callback
726 * to fence_queue that checks if this fence is signaled, and if so it
727 * signals the fence and removes itself.
728 */
729static bool amdgpu_fence_enable_signaling(struct fence *f)
730{
731 struct amdgpu_fence *fence = to_amdgpu_fence(f);
732 struct amdgpu_ring *ring = fence->ring;
733
734 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
735 return false;
736
737 fence->fence_wake.flags = 0;
738 fence->fence_wake.private = NULL;
739 fence->fence_wake.func = amdgpu_fence_check_signaled;
740 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
741 fence_get(f);
Christian Königc2776af2015-11-03 13:27:39 +0100742 if (!timer_pending(&ring->fence_drv.fallback_timer))
743 amdgpu_fence_schedule_fallback(ring);
Christian Königa95e2642015-11-03 12:21:57 +0100744 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
745 return true;
746}
747
748const struct fence_ops amdgpu_fence_ops = {
749 .get_driver_name = amdgpu_fence_get_driver_name,
750 .get_timeline_name = amdgpu_fence_get_timeline_name,
751 .enable_signaling = amdgpu_fence_enable_signaling,
752 .signaled = amdgpu_fence_is_signaled,
753 .wait = fence_default_wait,
754 .release = NULL,
755};
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756
757/*
758 * Fence debugfs
759 */
760#if defined(CONFIG_DEBUG_FS)
761static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
762{
763 struct drm_info_node *node = (struct drm_info_node *)m->private;
764 struct drm_device *dev = node->minor->dev;
765 struct amdgpu_device *adev = dev->dev_private;
766 int i, j;
767
768 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
769 struct amdgpu_ring *ring = adev->rings[i];
770 if (!ring || !ring->fence_drv.initialized)
771 continue;
772
773 amdgpu_fence_process(ring);
774
Christian König344c19f2015-06-02 15:47:16 +0200775 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400776 seq_printf(m, "Last signaled fence 0x%016llx\n",
777 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
778 seq_printf(m, "Last emitted 0x%016llx\n",
779 ring->fence_drv.sync_seq[i]);
780
781 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
782 struct amdgpu_ring *other = adev->rings[j];
Christian König344c19f2015-06-02 15:47:16 +0200783 if (i != j && other && other->fence_drv.initialized &&
784 ring->fence_drv.sync_seq[j])
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400785 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
786 j, ring->fence_drv.sync_seq[j]);
787 }
788 }
789 return 0;
790}
791
792static struct drm_info_list amdgpu_debugfs_fence_list[] = {
793 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
794};
795#endif
796
797int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
798{
799#if defined(CONFIG_DEBUG_FS)
800 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
801#else
802 return 0;
803#endif
804}
805