blob: fabb01e8c8d5abddfb0198db4753140fe48d5f8f [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
Christian König364beb22016-02-16 17:39:39 +010094 * @f: resulting fence object
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095 *
96 * Emits a fence command on the requested ring (all asics).
97 * Returns 0 on success, -ENOMEM on failure.
98 */
Christian König364beb22016-02-16 17:39:39 +010099int amdgpu_fence_emit(struct amdgpu_ring *ring, struct fence **f)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100{
101 struct amdgpu_device *adev = ring->adev;
Christian König364beb22016-02-16 17:39:39 +0100102 struct amdgpu_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400103
Christian König364beb22016-02-16 17:39:39 +0100104 fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
105 if (fence == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 return -ENOMEM;
Christian König364beb22016-02-16 17:39:39 +0100107
108 fence->seq = ++ring->fence_drv.sync_seq;
109 fence->ring = ring;
110 fence_init(&fence->base, &amdgpu_fence_ops,
111 &ring->fence_drv.fence_queue.lock,
112 adev->fence_context + ring->idx,
113 fence->seq);
Chunming Zhou890ee232015-06-01 14:35:03 +0800114 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
Christian König364beb22016-02-16 17:39:39 +0100115 fence->seq, AMDGPU_FENCE_FLAG_INT);
116 *f = &fence->base;
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 {
Christian König5907a0d2016-01-18 15:16:53 +0100171 last_emitted = ring->fence_drv.sync_seq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172 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/*
Christian König9b389662016-02-11 14:42:33 +0100260 * amdgpu_ring_wait_seq - wait for seq of the specific ring to signal
monk.liu7f06c232015-07-30 18:28:12 +0800261 * @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
monk.liu7f06c232015-07-30 18:28:12 +0800266 * -EINVAL: some paramter is not valid
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400267 */
Christian König00d2a2b2015-08-07 16:15:36 +0200268static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269{
monk.liu7f06c232015-07-30 18:28:12 +0800270 BUG_ON(!ring);
Christian König5907a0d2016-01-18 15:16:53 +0100271 if (seq > ring->fence_drv.sync_seq)
monk.liu7f06c232015-07-30 18:28:12 +0800272 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400273
monk.liu7f06c232015-07-30 18:28:12 +0800274 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
Christian König00d2a2b2015-08-07 16:15:36 +0200275 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400276
Christian Königc2776af2015-11-03 13:27:39 +0100277 amdgpu_fence_schedule_fallback(ring);
Christian König9b389662016-02-11 14:42:33 +0100278 wait_event(ring->fence_drv.fence_queue,
279 amdgpu_fence_seq_signaled(ring, seq));
monk.liu7f06c232015-07-30 18:28:12 +0800280
Christian König9b389662016-02-11 14:42:33 +0100281 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400282}
283
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400285 * amdgpu_fence_wait_empty - wait for all fences to signal
286 *
287 * @adev: amdgpu device pointer
288 * @ring: ring index the fence is associated with
289 *
290 * Wait for all fences on the requested ring to signal (all asics).
291 * Returns 0 if the fences have passed, error for all other cases.
292 * Caller must hold ring lock.
293 */
294int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
295{
Christian König5907a0d2016-01-18 15:16:53 +0100296 uint64_t seq = ring->fence_drv.sync_seq;
Christian König00d2a2b2015-08-07 16:15:36 +0200297
monk.liu7f06c232015-07-30 18:28:12 +0800298 if (!seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400299 return 0;
300
Christian König00d2a2b2015-08-07 16:15:36 +0200301 return amdgpu_fence_ring_wait_seq(ring, seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400302}
303
304/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400305 * amdgpu_fence_count_emitted - get the count of emitted fences
306 *
307 * @ring: ring the fence is associated with
308 *
309 * Get the number of fences emitted on the requested ring (all asics).
310 * Returns the number of emitted fences on the ring. Used by the
311 * dynpm code to ring track activity.
312 */
313unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
314{
315 uint64_t emitted;
316
317 /* We are not protected by ring lock when reading the last sequence
318 * but it's ok to report slightly wrong fence count here.
319 */
320 amdgpu_fence_process(ring);
Christian König5907a0d2016-01-18 15:16:53 +0100321 emitted = ring->fence_drv.sync_seq
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400322 - atomic64_read(&ring->fence_drv.last_seq);
323 /* to avoid 32bits warp around */
324 if (emitted > 0x10000000)
325 emitted = 0x10000000;
326
327 return (unsigned)emitted;
328}
329
330/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400331 * amdgpu_fence_driver_start_ring - make the fence driver
332 * ready for use on the requested ring.
333 *
334 * @ring: ring to start the fence driver on
335 * @irq_src: interrupt source to use for this ring
336 * @irq_type: interrupt type to use for this ring
337 *
338 * Make the fence driver ready for processing (all asics).
339 * Not all asics have all rings, so each asic will only
340 * start the fence driver on the rings it has.
341 * Returns 0 for success, errors for failure.
342 */
343int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
344 struct amdgpu_irq_src *irq_src,
345 unsigned irq_type)
346{
347 struct amdgpu_device *adev = ring->adev;
348 uint64_t index;
349
350 if (ring != &adev->uvd.ring) {
351 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
352 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
353 } else {
354 /* put fence directly behind firmware */
355 index = ALIGN(adev->uvd.fw->size, 8);
356 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
357 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
358 }
359 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
Chunming Zhouc6a40792015-06-01 14:14:32 +0800360 amdgpu_irq_get(adev, irq_src, irq_type);
361
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400362 ring->fence_drv.irq_src = irq_src;
363 ring->fence_drv.irq_type = irq_type;
Chunming Zhouc6a40792015-06-01 14:14:32 +0800364 ring->fence_drv.initialized = true;
365
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400366 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
367 "cpu addr 0x%p\n", ring->idx,
368 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
369 return 0;
370}
371
372/**
373 * amdgpu_fence_driver_init_ring - init the fence driver
374 * for the requested ring.
375 *
376 * @ring: ring to init the fence driver on
377 *
378 * Init the fence driver for the requested ring (all asics).
379 * Helper function for amdgpu_fence_driver_init().
380 */
Christian König4f839a22015-09-08 20:22:31 +0200381int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400382{
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800383 long timeout;
Christian König5907a0d2016-01-18 15:16:53 +0100384 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400385
386 ring->fence_drv.cpu_addr = NULL;
387 ring->fence_drv.gpu_addr = 0;
Christian König5907a0d2016-01-18 15:16:53 +0100388 ring->fence_drv.sync_seq = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400389 atomic64_set(&ring->fence_drv.last_seq, 0);
390 ring->fence_drv.initialized = false;
391
Christian Königc2776af2015-11-03 13:27:39 +0100392 setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
393 (unsigned long)ring);
Alex Deucherb80d8472015-08-16 22:55:02 -0400394
Christian König5ec92a72015-09-07 18:43:02 +0200395 init_waitqueue_head(&ring->fence_drv.fence_queue);
396
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800397 timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
398 if (timeout == 0) {
399 /*
400 * FIXME:
401 * Delayed workqueue cannot use it directly,
402 * so the scheduler will not use delayed workqueue if
403 * MAX_SCHEDULE_TIMEOUT is set.
404 * Currently keep it simple and silly.
405 */
406 timeout = MAX_SCHEDULE_TIMEOUT;
407 }
408 r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
409 amdgpu_sched_hw_submission,
410 timeout, ring->name);
411 if (r) {
412 DRM_ERROR("Failed to create scheduler on ring %s.\n",
413 ring->name);
414 return r;
Alex Deucherb80d8472015-08-16 22:55:02 -0400415 }
Christian König4f839a22015-09-08 20:22:31 +0200416
417 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400418}
419
420/**
421 * amdgpu_fence_driver_init - init the fence driver
422 * for all possible rings.
423 *
424 * @adev: amdgpu device pointer
425 *
426 * Init the fence driver for all possible rings (all asics).
427 * Not all asics have all rings, so each asic will only
428 * start the fence driver on the rings it has using
429 * amdgpu_fence_driver_start_ring().
430 * Returns 0 for success.
431 */
432int amdgpu_fence_driver_init(struct amdgpu_device *adev)
433{
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800434 if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
435 amdgpu_fence_slab = kmem_cache_create(
436 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
437 SLAB_HWCACHE_ALIGN, NULL);
438 if (!amdgpu_fence_slab)
439 return -ENOMEM;
440 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400441 if (amdgpu_debugfs_fence_init(adev))
442 dev_err(adev->dev, "fence debugfs file creation failed\n");
443
444 return 0;
445}
446
447/**
448 * amdgpu_fence_driver_fini - tear down the fence driver
449 * for all possible rings.
450 *
451 * @adev: amdgpu device pointer
452 *
453 * Tear down the fence driver for all possible rings (all asics).
454 */
455void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
456{
457 int i, r;
458
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800459 if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
460 kmem_cache_destroy(amdgpu_fence_slab);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400461 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
462 struct amdgpu_ring *ring = adev->rings[i];
Christian Königc2776af2015-11-03 13:27:39 +0100463
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 if (!ring || !ring->fence_drv.initialized)
465 continue;
466 r = amdgpu_fence_wait_empty(ring);
467 if (r) {
468 /* no need to trigger GPU reset as we are unloading */
469 amdgpu_fence_driver_force_completion(adev);
470 }
monk.liu7f06c232015-07-30 18:28:12 +0800471 wake_up_all(&ring->fence_drv.fence_queue);
Chunming Zhouc6a40792015-06-01 14:14:32 +0800472 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
473 ring->fence_drv.irq_type);
Christian König4f839a22015-09-08 20:22:31 +0200474 amd_sched_fini(&ring->sched);
Christian Königc2776af2015-11-03 13:27:39 +0100475 del_timer_sync(&ring->fence_drv.fallback_timer);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400476 ring->fence_drv.initialized = false;
477 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400478}
479
480/**
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400481 * amdgpu_fence_driver_suspend - suspend the fence driver
482 * for all possible rings.
483 *
484 * @adev: amdgpu device pointer
485 *
486 * Suspend the fence driver for all possible rings (all asics).
487 */
488void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
489{
490 int i, r;
491
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400492 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
493 struct amdgpu_ring *ring = adev->rings[i];
494 if (!ring || !ring->fence_drv.initialized)
495 continue;
496
497 /* wait for gpu to finish processing current batch */
498 r = amdgpu_fence_wait_empty(ring);
499 if (r) {
500 /* delay GPU reset to resume */
501 amdgpu_fence_driver_force_completion(adev);
502 }
503
504 /* disable the interrupt */
505 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
506 ring->fence_drv.irq_type);
507 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400508}
509
510/**
511 * amdgpu_fence_driver_resume - resume the fence driver
512 * for all possible rings.
513 *
514 * @adev: amdgpu device pointer
515 *
516 * Resume the fence driver for all possible rings (all asics).
517 * Not all asics have all rings, so each asic will only
518 * start the fence driver on the rings it has using
519 * amdgpu_fence_driver_start_ring().
520 * Returns 0 for success.
521 */
522void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
523{
524 int i;
525
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400526 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
527 struct amdgpu_ring *ring = adev->rings[i];
528 if (!ring || !ring->fence_drv.initialized)
529 continue;
530
531 /* enable the interrupt */
532 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
533 ring->fence_drv.irq_type);
534 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400535}
536
537/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400538 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
539 *
540 * @adev: amdgpu device pointer
541 *
542 * In case of GPU reset failure make sure no process keep waiting on fence
543 * that will never complete.
544 */
545void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
546{
547 int i;
548
549 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
550 struct amdgpu_ring *ring = adev->rings[i];
551 if (!ring || !ring->fence_drv.initialized)
552 continue;
553
Christian König5907a0d2016-01-18 15:16:53 +0100554 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555 }
556}
557
Christian Königa95e2642015-11-03 12:21:57 +0100558/*
559 * Common fence implementation
560 */
561
562static const char *amdgpu_fence_get_driver_name(struct fence *fence)
563{
564 return "amdgpu";
565}
566
567static const char *amdgpu_fence_get_timeline_name(struct fence *f)
568{
569 struct amdgpu_fence *fence = to_amdgpu_fence(f);
570 return (const char *)fence->ring->name;
571}
572
573/**
574 * amdgpu_fence_is_signaled - test if fence is signaled
575 *
576 * @f: fence to test
577 *
578 * Test the fence sequence number if it is already signaled. If it isn't
579 * signaled start fence processing. Returns True if the fence is signaled.
580 */
581static bool amdgpu_fence_is_signaled(struct fence *f)
582{
583 struct amdgpu_fence *fence = to_amdgpu_fence(f);
584 struct amdgpu_ring *ring = fence->ring;
585
586 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
587 return true;
588
589 amdgpu_fence_process(ring);
590
591 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
592 return true;
593
594 return false;
595}
596
597/**
598 * amdgpu_fence_check_signaled - callback from fence_queue
599 *
600 * this function is called with fence_queue lock held, which is also used
601 * for the fence locking itself, so unlocked variants are used for
602 * fence_signal, and remove_wait_queue.
603 */
604static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
605{
606 struct amdgpu_fence *fence;
607 struct amdgpu_device *adev;
608 u64 seq;
609 int ret;
610
611 fence = container_of(wait, struct amdgpu_fence, fence_wake);
612 adev = fence->ring->adev;
613
614 /*
615 * We cannot use amdgpu_fence_process here because we're already
616 * in the waitqueue, in a call from wake_up_all.
617 */
618 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
619 if (seq >= fence->seq) {
620 ret = fence_signal_locked(&fence->base);
621 if (!ret)
622 FENCE_TRACE(&fence->base, "signaled from irq context\n");
623 else
624 FENCE_TRACE(&fence->base, "was already signaled\n");
625
626 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
627 fence_put(&fence->base);
628 } else
629 FENCE_TRACE(&fence->base, "pending\n");
630 return 0;
631}
632
633/**
634 * amdgpu_fence_enable_signaling - enable signalling on fence
635 * @fence: fence
636 *
637 * This function is called with fence_queue lock held, and adds a callback
638 * to fence_queue that checks if this fence is signaled, and if so it
639 * signals the fence and removes itself.
640 */
641static bool amdgpu_fence_enable_signaling(struct fence *f)
642{
643 struct amdgpu_fence *fence = to_amdgpu_fence(f);
644 struct amdgpu_ring *ring = fence->ring;
645
646 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
647 return false;
648
649 fence->fence_wake.flags = 0;
650 fence->fence_wake.private = NULL;
651 fence->fence_wake.func = amdgpu_fence_check_signaled;
652 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
653 fence_get(f);
Christian Königc2776af2015-11-03 13:27:39 +0100654 if (!timer_pending(&ring->fence_drv.fallback_timer))
655 amdgpu_fence_schedule_fallback(ring);
Christian Königa95e2642015-11-03 12:21:57 +0100656 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
657 return true;
658}
659
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800660static void amdgpu_fence_release(struct fence *f)
661{
662 struct amdgpu_fence *fence = to_amdgpu_fence(f);
663 kmem_cache_free(amdgpu_fence_slab, fence);
664}
665
Christian Königa95e2642015-11-03 12:21:57 +0100666const struct fence_ops amdgpu_fence_ops = {
667 .get_driver_name = amdgpu_fence_get_driver_name,
668 .get_timeline_name = amdgpu_fence_get_timeline_name,
669 .enable_signaling = amdgpu_fence_enable_signaling,
670 .signaled = amdgpu_fence_is_signaled,
671 .wait = fence_default_wait,
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800672 .release = amdgpu_fence_release,
Christian Königa95e2642015-11-03 12:21:57 +0100673};
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400674
675/*
676 * Fence debugfs
677 */
678#if defined(CONFIG_DEBUG_FS)
679static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
680{
681 struct drm_info_node *node = (struct drm_info_node *)m->private;
682 struct drm_device *dev = node->minor->dev;
683 struct amdgpu_device *adev = dev->dev_private;
Christian König5907a0d2016-01-18 15:16:53 +0100684 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685
686 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
687 struct amdgpu_ring *ring = adev->rings[i];
688 if (!ring || !ring->fence_drv.initialized)
689 continue;
690
691 amdgpu_fence_process(ring);
692
Christian König344c19f2015-06-02 15:47:16 +0200693 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 seq_printf(m, "Last signaled fence 0x%016llx\n",
695 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
696 seq_printf(m, "Last emitted 0x%016llx\n",
Christian König5907a0d2016-01-18 15:16:53 +0100697 ring->fence_drv.sync_seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698 }
699 return 0;
700}
701
Alex Deucher18db89b2016-01-14 10:25:22 -0500702/**
703 * amdgpu_debugfs_gpu_reset - manually trigger a gpu reset
704 *
705 * Manually trigger a gpu reset at the next fence wait.
706 */
707static int amdgpu_debugfs_gpu_reset(struct seq_file *m, void *data)
708{
709 struct drm_info_node *node = (struct drm_info_node *) m->private;
710 struct drm_device *dev = node->minor->dev;
711 struct amdgpu_device *adev = dev->dev_private;
712
713 seq_printf(m, "gpu reset\n");
714 amdgpu_gpu_reset(adev);
715
716 return 0;
717}
718
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719static struct drm_info_list amdgpu_debugfs_fence_list[] = {
720 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
Alex Deucher18db89b2016-01-14 10:25:22 -0500721 {"amdgpu_gpu_reset", &amdgpu_debugfs_gpu_reset, 0, NULL}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722};
723#endif
724
725int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
726{
727#if defined(CONFIG_DEBUG_FS)
Alex Deucher18db89b2016-01-14 10:25:22 -0500728 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729#else
730 return 0;
731#endif
732}
733