blob: ec73ab1c177dc471da21bbba990a735ce00df408 [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
Christian König22e5a2f2016-03-11 15:12:53 +010050struct amdgpu_fence {
51 struct fence base;
52
53 /* RB, DMA, etc. */
54 struct amdgpu_ring *ring;
55 uint64_t seq;
56
57 wait_queue_t fence_wake;
58};
59
Chunming Zhoub49c84a2015-11-05 11:28:28 +080060static struct kmem_cache *amdgpu_fence_slab;
61static atomic_t amdgpu_fence_slab_ref = ATOMIC_INIT(0);
62
Christian König22e5a2f2016-03-11 15:12:53 +010063/*
64 * Cast helper
65 */
66static const struct fence_ops amdgpu_fence_ops;
67static inline struct amdgpu_fence *to_amdgpu_fence(struct fence *f)
68{
69 struct amdgpu_fence *__f = container_of(f, struct amdgpu_fence, base);
70
71 if (__f->base.ops == &amdgpu_fence_ops)
72 return __f;
73
74 return NULL;
75}
76
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077/**
78 * amdgpu_fence_write - write a fence value
79 *
80 * @ring: ring the fence is associated with
81 * @seq: sequence number to write
82 *
83 * Writes a fence value to memory (all asics).
84 */
85static void amdgpu_fence_write(struct amdgpu_ring *ring, u32 seq)
86{
87 struct amdgpu_fence_driver *drv = &ring->fence_drv;
88
89 if (drv->cpu_addr)
90 *drv->cpu_addr = cpu_to_le32(seq);
91}
92
93/**
94 * amdgpu_fence_read - read a fence value
95 *
96 * @ring: ring the fence is associated with
97 *
98 * Reads a fence value from memory (all asics).
99 * Returns the value of the fence read from memory.
100 */
101static u32 amdgpu_fence_read(struct amdgpu_ring *ring)
102{
103 struct amdgpu_fence_driver *drv = &ring->fence_drv;
104 u32 seq = 0;
105
106 if (drv->cpu_addr)
107 seq = le32_to_cpu(*drv->cpu_addr);
108 else
109 seq = lower_32_bits(atomic64_read(&drv->last_seq));
110
111 return seq;
112}
113
114/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 * amdgpu_fence_emit - emit a fence on the requested ring
116 *
117 * @ring: ring the fence is associated with
Christian König364beb22016-02-16 17:39:39 +0100118 * @f: resulting fence object
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 *
120 * Emits a fence command on the requested ring (all asics).
121 * Returns 0 on success, -ENOMEM on failure.
122 */
Christian König364beb22016-02-16 17:39:39 +0100123int amdgpu_fence_emit(struct amdgpu_ring *ring, struct fence **f)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124{
125 struct amdgpu_device *adev = ring->adev;
Christian König364beb22016-02-16 17:39:39 +0100126 struct amdgpu_fence *fence;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127
Christian König364beb22016-02-16 17:39:39 +0100128 fence = kmem_cache_alloc(amdgpu_fence_slab, GFP_KERNEL);
129 if (fence == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130 return -ENOMEM;
Christian König364beb22016-02-16 17:39:39 +0100131
132 fence->seq = ++ring->fence_drv.sync_seq;
133 fence->ring = ring;
134 fence_init(&fence->base, &amdgpu_fence_ops,
135 &ring->fence_drv.fence_queue.lock,
136 adev->fence_context + ring->idx,
137 fence->seq);
Chunming Zhou890ee232015-06-01 14:35:03 +0800138 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
Christian König364beb22016-02-16 17:39:39 +0100139 fence->seq, AMDGPU_FENCE_FLAG_INT);
140 *f = &fence->base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141 return 0;
142}
143
144/**
Christian Königc2776af2015-11-03 13:27:39 +0100145 * amdgpu_fence_schedule_fallback - schedule fallback check
146 *
147 * @ring: pointer to struct amdgpu_ring
148 *
149 * Start a timer as fallback to our interrupts.
150 */
151static void amdgpu_fence_schedule_fallback(struct amdgpu_ring *ring)
152{
153 mod_timer(&ring->fence_drv.fallback_timer,
154 jiffies + AMDGPU_FENCE_JIFFIES_TIMEOUT);
155}
156
157/**
Christian Königca08e042016-03-11 17:57:56 +0100158 * amdgpu_fence_process - check for fence activity
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159 *
160 * @ring: pointer to struct amdgpu_ring
161 *
162 * Checks the current fence value and calculates the last
Christian Königca08e042016-03-11 17:57:56 +0100163 * signalled fence value. Wakes the fence queue if the
164 * sequence number has increased.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 */
Christian Königca08e042016-03-11 17:57:56 +0100166void amdgpu_fence_process(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167{
168 uint64_t seq, last_seq, last_emitted;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 bool wake = false;
170
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171 last_seq = atomic64_read(&ring->fence_drv.last_seq);
172 do {
Christian König5907a0d2016-01-18 15:16:53 +0100173 last_emitted = ring->fence_drv.sync_seq;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174 seq = amdgpu_fence_read(ring);
175 seq |= last_seq & 0xffffffff00000000LL;
176 if (seq < last_seq) {
177 seq &= 0xffffffff;
178 seq |= last_emitted & 0xffffffff00000000LL;
179 }
180
Christian Königd9713ef2016-03-11 17:49:58 +0100181 if (seq <= last_seq || seq > last_emitted)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400182 break;
Christian Königd9713ef2016-03-11 17:49:58 +0100183
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400184 /* 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.
187 */
188 wake = true;
189 last_seq = seq;
Christian Königd9713ef2016-03-11 17:49:58 +0100190
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
192
193 if (seq < last_emitted)
Christian Königc2776af2015-11-03 13:27:39 +0100194 amdgpu_fence_schedule_fallback(ring);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195
Christian Königca08e042016-03-11 17:57:56 +0100196 if (wake)
monk.liu7f06c232015-07-30 18:28:12 +0800197 wake_up_all(&ring->fence_drv.fence_queue);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198}
199
200/**
Christian Königc2776af2015-11-03 13:27:39 +0100201 * amdgpu_fence_fallback - fallback for hardware interrupts
202 *
203 * @work: delayed work item
204 *
205 * Checks for fence activity.
206 */
207static void amdgpu_fence_fallback(unsigned long arg)
208{
209 struct amdgpu_ring *ring = (void *)arg;
210
211 amdgpu_fence_process(ring);
212}
213
214/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400215 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
216 *
217 * @ring: ring the fence is associated with
218 * @seq: sequence number
219 *
220 * Check if the last signaled fence sequnce number is >= the requested
221 * sequence number (all asics).
222 * Returns true if the fence has signaled (current fence value
223 * is >= requested value) or false if it has not (current fence
224 * value is < the requested value. Helper function for
225 * amdgpu_fence_signaled().
226 */
227static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
228{
229 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
230 return true;
231
232 /* poll new last sequence at least once */
233 amdgpu_fence_process(ring);
234 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
235 return true;
236
237 return false;
238}
239
monk.liu7f06c232015-07-30 18:28:12 +0800240/*
Christian König9b389662016-02-11 14:42:33 +0100241 * amdgpu_ring_wait_seq - wait for seq of the specific ring to signal
monk.liu7f06c232015-07-30 18:28:12 +0800242 * @ring: ring to wait on for the seq number
243 * @seq: seq number wait for
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400244 *
monk.liu7f06c232015-07-30 18:28:12 +0800245 * return value:
Christian König00d2a2b2015-08-07 16:15:36 +0200246 * 0: seq signaled, and gpu not hang
monk.liu7f06c232015-07-30 18:28:12 +0800247 * -EINVAL: some paramter is not valid
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400248 */
Christian König00d2a2b2015-08-07 16:15:36 +0200249static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250{
monk.liu7f06c232015-07-30 18:28:12 +0800251 BUG_ON(!ring);
Christian König5907a0d2016-01-18 15:16:53 +0100252 if (seq > ring->fence_drv.sync_seq)
monk.liu7f06c232015-07-30 18:28:12 +0800253 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254
monk.liu7f06c232015-07-30 18:28:12 +0800255 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
Christian König00d2a2b2015-08-07 16:15:36 +0200256 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257
Christian Königc2776af2015-11-03 13:27:39 +0100258 amdgpu_fence_schedule_fallback(ring);
Christian König9b389662016-02-11 14:42:33 +0100259 wait_event(ring->fence_drv.fence_queue,
260 amdgpu_fence_seq_signaled(ring, seq));
monk.liu7f06c232015-07-30 18:28:12 +0800261
Christian König9b389662016-02-11 14:42:33 +0100262 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400263}
264
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 * amdgpu_fence_wait_empty - wait for all fences to signal
267 *
268 * @adev: amdgpu device pointer
269 * @ring: ring index the fence is associated with
270 *
271 * Wait for all fences on the requested ring to signal (all asics).
272 * Returns 0 if the fences have passed, error for all other cases.
273 * Caller must hold ring lock.
274 */
275int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
276{
Christian König5907a0d2016-01-18 15:16:53 +0100277 uint64_t seq = ring->fence_drv.sync_seq;
Christian König00d2a2b2015-08-07 16:15:36 +0200278
monk.liu7f06c232015-07-30 18:28:12 +0800279 if (!seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400280 return 0;
281
Christian König00d2a2b2015-08-07 16:15:36 +0200282 return amdgpu_fence_ring_wait_seq(ring, seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283}
284
285/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400286 * amdgpu_fence_count_emitted - get the count of emitted fences
287 *
288 * @ring: ring the fence is associated with
289 *
290 * Get the number of fences emitted on the requested ring (all asics).
291 * Returns the number of emitted fences on the ring. Used by the
292 * dynpm code to ring track activity.
293 */
294unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
295{
296 uint64_t emitted;
297
298 /* We are not protected by ring lock when reading the last sequence
299 * but it's ok to report slightly wrong fence count here.
300 */
301 amdgpu_fence_process(ring);
Christian König5907a0d2016-01-18 15:16:53 +0100302 emitted = ring->fence_drv.sync_seq
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303 - atomic64_read(&ring->fence_drv.last_seq);
304 /* to avoid 32bits warp around */
305 if (emitted > 0x10000000)
306 emitted = 0x10000000;
307
308 return (unsigned)emitted;
309}
310
311/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400312 * amdgpu_fence_driver_start_ring - make the fence driver
313 * ready for use on the requested ring.
314 *
315 * @ring: ring to start the fence driver on
316 * @irq_src: interrupt source to use for this ring
317 * @irq_type: interrupt type to use for this ring
318 *
319 * Make the fence driver ready for processing (all asics).
320 * Not all asics have all rings, so each asic will only
321 * start the fence driver on the rings it has.
322 * Returns 0 for success, errors for failure.
323 */
324int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
325 struct amdgpu_irq_src *irq_src,
326 unsigned irq_type)
327{
328 struct amdgpu_device *adev = ring->adev;
329 uint64_t index;
330
331 if (ring != &adev->uvd.ring) {
332 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
333 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
334 } else {
335 /* put fence directly behind firmware */
336 index = ALIGN(adev->uvd.fw->size, 8);
337 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
338 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
339 }
340 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
Chunming Zhouc6a40792015-06-01 14:14:32 +0800341 amdgpu_irq_get(adev, irq_src, irq_type);
342
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400343 ring->fence_drv.irq_src = irq_src;
344 ring->fence_drv.irq_type = irq_type;
Chunming Zhouc6a40792015-06-01 14:14:32 +0800345 ring->fence_drv.initialized = true;
346
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
348 "cpu addr 0x%p\n", ring->idx,
349 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
350 return 0;
351}
352
353/**
354 * amdgpu_fence_driver_init_ring - init the fence driver
355 * for the requested ring.
356 *
357 * @ring: ring to init the fence driver on
358 *
359 * Init the fence driver for the requested ring (all asics).
360 * Helper function for amdgpu_fence_driver_init().
361 */
Christian König4f839a22015-09-08 20:22:31 +0200362int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400363{
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800364 long timeout;
Christian König5907a0d2016-01-18 15:16:53 +0100365 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400366
367 ring->fence_drv.cpu_addr = NULL;
368 ring->fence_drv.gpu_addr = 0;
Christian König5907a0d2016-01-18 15:16:53 +0100369 ring->fence_drv.sync_seq = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400370 atomic64_set(&ring->fence_drv.last_seq, 0);
371 ring->fence_drv.initialized = false;
372
Christian Königc2776af2015-11-03 13:27:39 +0100373 setup_timer(&ring->fence_drv.fallback_timer, amdgpu_fence_fallback,
374 (unsigned long)ring);
Alex Deucherb80d8472015-08-16 22:55:02 -0400375
Christian König5ec92a72015-09-07 18:43:02 +0200376 init_waitqueue_head(&ring->fence_drv.fence_queue);
377
Chunming Zhoucadf97b2016-01-15 11:25:00 +0800378 timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
379 if (timeout == 0) {
380 /*
381 * FIXME:
382 * Delayed workqueue cannot use it directly,
383 * so the scheduler will not use delayed workqueue if
384 * MAX_SCHEDULE_TIMEOUT is set.
385 * Currently keep it simple and silly.
386 */
387 timeout = MAX_SCHEDULE_TIMEOUT;
388 }
389 r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
390 amdgpu_sched_hw_submission,
391 timeout, ring->name);
392 if (r) {
393 DRM_ERROR("Failed to create scheduler on ring %s.\n",
394 ring->name);
395 return r;
Alex Deucherb80d8472015-08-16 22:55:02 -0400396 }
Christian König4f839a22015-09-08 20:22:31 +0200397
398 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400399}
400
401/**
402 * amdgpu_fence_driver_init - init the fence driver
403 * for all possible rings.
404 *
405 * @adev: amdgpu device pointer
406 *
407 * Init the fence driver for all possible rings (all asics).
408 * Not all asics have all rings, so each asic will only
409 * start the fence driver on the rings it has using
410 * amdgpu_fence_driver_start_ring().
411 * Returns 0 for success.
412 */
413int amdgpu_fence_driver_init(struct amdgpu_device *adev)
414{
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800415 if (atomic_inc_return(&amdgpu_fence_slab_ref) == 1) {
416 amdgpu_fence_slab = kmem_cache_create(
417 "amdgpu_fence", sizeof(struct amdgpu_fence), 0,
418 SLAB_HWCACHE_ALIGN, NULL);
419 if (!amdgpu_fence_slab)
420 return -ENOMEM;
421 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400422 if (amdgpu_debugfs_fence_init(adev))
423 dev_err(adev->dev, "fence debugfs file creation failed\n");
424
425 return 0;
426}
427
428/**
429 * amdgpu_fence_driver_fini - tear down the fence driver
430 * for all possible rings.
431 *
432 * @adev: amdgpu device pointer
433 *
434 * Tear down the fence driver for all possible rings (all asics).
435 */
436void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
437{
438 int i, r;
439
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800440 if (atomic_dec_and_test(&amdgpu_fence_slab_ref))
441 kmem_cache_destroy(amdgpu_fence_slab);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400442 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
443 struct amdgpu_ring *ring = adev->rings[i];
Christian Königc2776af2015-11-03 13:27:39 +0100444
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445 if (!ring || !ring->fence_drv.initialized)
446 continue;
447 r = amdgpu_fence_wait_empty(ring);
448 if (r) {
449 /* no need to trigger GPU reset as we are unloading */
450 amdgpu_fence_driver_force_completion(adev);
451 }
monk.liu7f06c232015-07-30 18:28:12 +0800452 wake_up_all(&ring->fence_drv.fence_queue);
Chunming Zhouc6a40792015-06-01 14:14:32 +0800453 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
454 ring->fence_drv.irq_type);
Christian König4f839a22015-09-08 20:22:31 +0200455 amd_sched_fini(&ring->sched);
Christian Königc2776af2015-11-03 13:27:39 +0100456 del_timer_sync(&ring->fence_drv.fallback_timer);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457 ring->fence_drv.initialized = false;
458 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400459}
460
461/**
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400462 * amdgpu_fence_driver_suspend - suspend the fence driver
463 * for all possible rings.
464 *
465 * @adev: amdgpu device pointer
466 *
467 * Suspend the fence driver for all possible rings (all asics).
468 */
469void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
470{
471 int i, r;
472
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400473 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
474 struct amdgpu_ring *ring = adev->rings[i];
475 if (!ring || !ring->fence_drv.initialized)
476 continue;
477
478 /* wait for gpu to finish processing current batch */
479 r = amdgpu_fence_wait_empty(ring);
480 if (r) {
481 /* delay GPU reset to resume */
482 amdgpu_fence_driver_force_completion(adev);
483 }
484
485 /* disable the interrupt */
486 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
487 ring->fence_drv.irq_type);
488 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400489}
490
491/**
492 * amdgpu_fence_driver_resume - resume the fence driver
493 * for all possible rings.
494 *
495 * @adev: amdgpu device pointer
496 *
497 * Resume the fence driver for all possible rings (all asics).
498 * Not all asics have all rings, so each asic will only
499 * start the fence driver on the rings it has using
500 * amdgpu_fence_driver_start_ring().
501 * Returns 0 for success.
502 */
503void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
504{
505 int i;
506
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400507 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
508 struct amdgpu_ring *ring = adev->rings[i];
509 if (!ring || !ring->fence_drv.initialized)
510 continue;
511
512 /* enable the interrupt */
513 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
514 ring->fence_drv.irq_type);
515 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400516}
517
518/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400519 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
520 *
521 * @adev: amdgpu device pointer
522 *
523 * In case of GPU reset failure make sure no process keep waiting on fence
524 * that will never complete.
525 */
526void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
527{
528 int i;
529
530 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
531 struct amdgpu_ring *ring = adev->rings[i];
532 if (!ring || !ring->fence_drv.initialized)
533 continue;
534
Christian König5907a0d2016-01-18 15:16:53 +0100535 amdgpu_fence_write(ring, ring->fence_drv.sync_seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 }
537}
538
Christian Königa95e2642015-11-03 12:21:57 +0100539/*
540 * Common fence implementation
541 */
542
543static const char *amdgpu_fence_get_driver_name(struct fence *fence)
544{
545 return "amdgpu";
546}
547
548static const char *amdgpu_fence_get_timeline_name(struct fence *f)
549{
550 struct amdgpu_fence *fence = to_amdgpu_fence(f);
551 return (const char *)fence->ring->name;
552}
553
554/**
555 * amdgpu_fence_is_signaled - test if fence is signaled
556 *
557 * @f: fence to test
558 *
559 * Test the fence sequence number if it is already signaled. If it isn't
560 * signaled start fence processing. Returns True if the fence is signaled.
561 */
562static bool amdgpu_fence_is_signaled(struct fence *f)
563{
564 struct amdgpu_fence *fence = to_amdgpu_fence(f);
565 struct amdgpu_ring *ring = fence->ring;
566
567 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
568 return true;
569
570 amdgpu_fence_process(ring);
571
572 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
573 return true;
574
575 return false;
576}
577
578/**
579 * amdgpu_fence_check_signaled - callback from fence_queue
580 *
581 * this function is called with fence_queue lock held, which is also used
582 * for the fence locking itself, so unlocked variants are used for
583 * fence_signal, and remove_wait_queue.
584 */
585static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
586{
587 struct amdgpu_fence *fence;
588 struct amdgpu_device *adev;
589 u64 seq;
590 int ret;
591
592 fence = container_of(wait, struct amdgpu_fence, fence_wake);
593 adev = fence->ring->adev;
594
595 /*
596 * We cannot use amdgpu_fence_process here because we're already
597 * in the waitqueue, in a call from wake_up_all.
598 */
599 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
600 if (seq >= fence->seq) {
601 ret = fence_signal_locked(&fence->base);
602 if (!ret)
603 FENCE_TRACE(&fence->base, "signaled from irq context\n");
604 else
605 FENCE_TRACE(&fence->base, "was already signaled\n");
606
607 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
608 fence_put(&fence->base);
609 } else
610 FENCE_TRACE(&fence->base, "pending\n");
611 return 0;
612}
613
614/**
615 * amdgpu_fence_enable_signaling - enable signalling on fence
616 * @fence: fence
617 *
618 * This function is called with fence_queue lock held, and adds a callback
619 * to fence_queue that checks if this fence is signaled, and if so it
620 * signals the fence and removes itself.
621 */
622static bool amdgpu_fence_enable_signaling(struct fence *f)
623{
624 struct amdgpu_fence *fence = to_amdgpu_fence(f);
625 struct amdgpu_ring *ring = fence->ring;
626
627 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
628 return false;
629
630 fence->fence_wake.flags = 0;
631 fence->fence_wake.private = NULL;
632 fence->fence_wake.func = amdgpu_fence_check_signaled;
633 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
634 fence_get(f);
Christian Königc2776af2015-11-03 13:27:39 +0100635 if (!timer_pending(&ring->fence_drv.fallback_timer))
636 amdgpu_fence_schedule_fallback(ring);
Christian Königa95e2642015-11-03 12:21:57 +0100637 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
638 return true;
639}
640
Christian Königb4413532016-03-15 13:40:17 +0100641/**
642 * amdgpu_fence_free - free up the fence memory
643 *
644 * @rcu: RCU callback head
645 *
646 * Free up the fence memory after the RCU grace period.
647 */
648static void amdgpu_fence_free(struct rcu_head *rcu)
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800649{
Christian Königb4413532016-03-15 13:40:17 +0100650 struct fence *f = container_of(rcu, struct fence, rcu);
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800651 struct amdgpu_fence *fence = to_amdgpu_fence(f);
652 kmem_cache_free(amdgpu_fence_slab, fence);
653}
654
Christian Königb4413532016-03-15 13:40:17 +0100655/**
656 * amdgpu_fence_release - callback that fence can be freed
657 *
658 * @fence: fence
659 *
660 * This function is called when the reference count becomes zero.
661 * It just RCU schedules freeing up the fence.
662 */
663static void amdgpu_fence_release(struct fence *f)
664{
665 call_rcu(&f->rcu, amdgpu_fence_free);
666}
667
Christian König22e5a2f2016-03-11 15:12:53 +0100668static const struct fence_ops amdgpu_fence_ops = {
Christian Königa95e2642015-11-03 12:21:57 +0100669 .get_driver_name = amdgpu_fence_get_driver_name,
670 .get_timeline_name = amdgpu_fence_get_timeline_name,
671 .enable_signaling = amdgpu_fence_enable_signaling,
672 .signaled = amdgpu_fence_is_signaled,
673 .wait = fence_default_wait,
Chunming Zhoub49c84a2015-11-05 11:28:28 +0800674 .release = amdgpu_fence_release,
Christian Königa95e2642015-11-03 12:21:57 +0100675};
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676
677/*
678 * Fence debugfs
679 */
680#if defined(CONFIG_DEBUG_FS)
681static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
682{
683 struct drm_info_node *node = (struct drm_info_node *)m->private;
684 struct drm_device *dev = node->minor->dev;
685 struct amdgpu_device *adev = dev->dev_private;
Christian König5907a0d2016-01-18 15:16:53 +0100686 int i;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687
688 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
689 struct amdgpu_ring *ring = adev->rings[i];
690 if (!ring || !ring->fence_drv.initialized)
691 continue;
692
693 amdgpu_fence_process(ring);
694
Christian König344c19f2015-06-02 15:47:16 +0200695 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 seq_printf(m, "Last signaled fence 0x%016llx\n",
697 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
698 seq_printf(m, "Last emitted 0x%016llx\n",
Christian König5907a0d2016-01-18 15:16:53 +0100699 ring->fence_drv.sync_seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 }
701 return 0;
702}
703
Alex Deucher18db89b2016-01-14 10:25:22 -0500704/**
705 * amdgpu_debugfs_gpu_reset - manually trigger a gpu reset
706 *
707 * Manually trigger a gpu reset at the next fence wait.
708 */
709static int amdgpu_debugfs_gpu_reset(struct seq_file *m, void *data)
710{
711 struct drm_info_node *node = (struct drm_info_node *) m->private;
712 struct drm_device *dev = node->minor->dev;
713 struct amdgpu_device *adev = dev->dev_private;
714
715 seq_printf(m, "gpu reset\n");
716 amdgpu_gpu_reset(adev);
717
718 return 0;
719}
720
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400721static struct drm_info_list amdgpu_debugfs_fence_list[] = {
722 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
Alex Deucher18db89b2016-01-14 10:25:22 -0500723 {"amdgpu_gpu_reset", &amdgpu_debugfs_gpu_reset, 0, NULL}
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724};
725#endif
726
727int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
728{
729#if defined(CONFIG_DEBUG_FS)
Alex Deucher18db89b2016-01-14 10:25:22 -0500730 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 2);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731#else
732 return 0;
733#endif
734}
735