blob: 4010aa6b4e53bd566145754bb63d0369e2f2a732 [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/**
88 * amdgpu_fence_schedule_check - schedule lockup check
89 *
90 * @ring: pointer to struct amdgpu_ring
91 *
92 * Queues a delayed work item to check for lockups.
93 */
94static void amdgpu_fence_schedule_check(struct amdgpu_ring *ring)
95{
96 /*
97 * Do not reset the timer here with mod_delayed_work,
98 * this can livelock in an interaction with TTM delayed destroy.
99 */
100 queue_delayed_work(system_power_efficient_wq,
101 &ring->fence_drv.lockup_work,
102 AMDGPU_FENCE_JIFFIES_TIMEOUT);
103}
104
105/**
106 * amdgpu_fence_emit - emit a fence on the requested ring
107 *
108 * @ring: ring the fence is associated with
109 * @owner: creator of the fence
110 * @fence: amdgpu fence object
111 *
112 * Emits a fence command on the requested ring (all asics).
113 * Returns 0 on success, -ENOMEM on failure.
114 */
115int amdgpu_fence_emit(struct amdgpu_ring *ring, void *owner,
116 struct amdgpu_fence **fence)
117{
118 struct amdgpu_device *adev = ring->adev;
119
120 /* we are protected by the ring emission mutex */
121 *fence = kmalloc(sizeof(struct amdgpu_fence), GFP_KERNEL);
122 if ((*fence) == NULL) {
123 return -ENOMEM;
124 }
125 (*fence)->seq = ++ring->fence_drv.sync_seq[ring->idx];
126 (*fence)->ring = ring;
127 (*fence)->owner = owner;
128 fence_init(&(*fence)->base, &amdgpu_fence_ops,
monk.liu7f06c232015-07-30 18:28:12 +0800129 &ring->fence_drv.fence_queue.lock,
130 adev->fence_context + ring->idx,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 (*fence)->seq);
Chunming Zhou890ee232015-06-01 14:35:03 +0800132 amdgpu_ring_emit_fence(ring, ring->fence_drv.gpu_addr,
133 (*fence)->seq,
134 AMDGPU_FENCE_FLAG_INT);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 trace_amdgpu_fence_emit(ring->adev->ddev, ring->idx, (*fence)->seq);
136 return 0;
137}
138
139/**
140 * amdgpu_fence_check_signaled - callback from fence_queue
141 *
142 * this function is called with fence_queue lock held, which is also used
143 * for the fence locking itself, so unlocked variants are used for
144 * fence_signal, and remove_wait_queue.
145 */
146static int amdgpu_fence_check_signaled(wait_queue_t *wait, unsigned mode, int flags, void *key)
147{
148 struct amdgpu_fence *fence;
149 struct amdgpu_device *adev;
150 u64 seq;
151 int ret;
152
153 fence = container_of(wait, struct amdgpu_fence, fence_wake);
154 adev = fence->ring->adev;
155
156 /*
157 * We cannot use amdgpu_fence_process here because we're already
158 * in the waitqueue, in a call from wake_up_all.
159 */
160 seq = atomic64_read(&fence->ring->fence_drv.last_seq);
161 if (seq >= fence->seq) {
162 ret = fence_signal_locked(&fence->base);
163 if (!ret)
164 FENCE_TRACE(&fence->base, "signaled from irq context\n");
165 else
166 FENCE_TRACE(&fence->base, "was already signaled\n");
167
monk.liu7f06c232015-07-30 18:28:12 +0800168 __remove_wait_queue(&fence->ring->fence_drv.fence_queue, &fence->fence_wake);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 fence_put(&fence->base);
170 } else
171 FENCE_TRACE(&fence->base, "pending\n");
172 return 0;
173}
174
175/**
176 * amdgpu_fence_activity - check for fence activity
177 *
178 * @ring: pointer to struct amdgpu_ring
179 *
180 * Checks the current fence value and calculates the last
181 * signalled fence value. Returns true if activity occured
182 * on the ring, and the fence_queue should be waken up.
183 */
184static bool amdgpu_fence_activity(struct amdgpu_ring *ring)
185{
186 uint64_t seq, last_seq, last_emitted;
187 unsigned count_loop = 0;
188 bool wake = false;
189
190 /* Note there is a scenario here for an infinite loop but it's
191 * very unlikely to happen. For it to happen, the current polling
192 * process need to be interrupted by another process and another
193 * process needs to update the last_seq btw the atomic read and
194 * xchg of the current process.
195 *
196 * More over for this to go in infinite loop there need to be
Jammy Zhou86c2b792015-05-13 22:52:42 +0800197 * continuously new fence signaled ie amdgpu_fence_read needs
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 * to return a different value each time for both the currently
199 * polling process and the other process that xchg the last_seq
200 * btw atomic read and xchg of the current process. And the
201 * value the other process set as last seq must be higher than
202 * the seq value we just read. Which means that current process
Jammy Zhou86c2b792015-05-13 22:52:42 +0800203 * need to be interrupted after amdgpu_fence_read and before
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204 * atomic xchg.
205 *
206 * To be even more safe we count the number of time we loop and
207 * we bail after 10 loop just accepting the fact that we might
208 * have temporarly set the last_seq not to the true real last
209 * seq but to an older one.
210 */
211 last_seq = atomic64_read(&ring->fence_drv.last_seq);
212 do {
213 last_emitted = ring->fence_drv.sync_seq[ring->idx];
214 seq = amdgpu_fence_read(ring);
215 seq |= last_seq & 0xffffffff00000000LL;
216 if (seq < last_seq) {
217 seq &= 0xffffffff;
218 seq |= last_emitted & 0xffffffff00000000LL;
219 }
220
221 if (seq <= last_seq || seq > last_emitted) {
222 break;
223 }
224 /* If we loop over we don't want to return without
225 * checking if a fence is signaled as it means that the
226 * seq we just read is different from the previous on.
227 */
228 wake = true;
229 last_seq = seq;
230 if ((count_loop++) > 10) {
231 /* We looped over too many time leave with the
232 * fact that we might have set an older fence
233 * seq then the current real last seq as signaled
234 * by the hw.
235 */
236 break;
237 }
238 } while (atomic64_xchg(&ring->fence_drv.last_seq, seq) > seq);
239
240 if (seq < last_emitted)
241 amdgpu_fence_schedule_check(ring);
242
243 return wake;
244}
245
246/**
247 * amdgpu_fence_check_lockup - check for hardware lockup
248 *
249 * @work: delayed work item
250 *
251 * Checks for fence activity and if there is none probe
252 * the hardware if a lockup occured.
253 */
254static void amdgpu_fence_check_lockup(struct work_struct *work)
255{
256 struct amdgpu_fence_driver *fence_drv;
257 struct amdgpu_ring *ring;
258
259 fence_drv = container_of(work, struct amdgpu_fence_driver,
260 lockup_work.work);
261 ring = fence_drv->ring;
262
263 if (!down_read_trylock(&ring->adev->exclusive_lock)) {
264 /* just reschedule the check if a reset is going on */
265 amdgpu_fence_schedule_check(ring);
266 return;
267 }
268
monk.liu7f06c232015-07-30 18:28:12 +0800269 if (amdgpu_fence_activity(ring)) {
270 wake_up_all(&ring->fence_drv.fence_queue);
271 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272 up_read(&ring->adev->exclusive_lock);
273}
274
275/**
276 * amdgpu_fence_process - process a fence
277 *
278 * @adev: amdgpu_device pointer
279 * @ring: ring index the fence is associated with
280 *
281 * Checks the current fence value and wakes the fence queue
282 * if the sequence number has increased (all asics).
283 */
284void amdgpu_fence_process(struct amdgpu_ring *ring)
285{
Christian König68ed3de2015-08-07 15:57:21 +0200286 if (amdgpu_fence_activity(ring))
monk.liu7f06c232015-07-30 18:28:12 +0800287 wake_up_all(&ring->fence_drv.fence_queue);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400288}
289
290/**
291 * amdgpu_fence_seq_signaled - check if a fence sequence number has signaled
292 *
293 * @ring: ring the fence is associated with
294 * @seq: sequence number
295 *
296 * Check if the last signaled fence sequnce number is >= the requested
297 * sequence number (all asics).
298 * Returns true if the fence has signaled (current fence value
299 * is >= requested value) or false if it has not (current fence
300 * value is < the requested value. Helper function for
301 * amdgpu_fence_signaled().
302 */
303static bool amdgpu_fence_seq_signaled(struct amdgpu_ring *ring, u64 seq)
304{
305 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
306 return true;
307
308 /* poll new last sequence at least once */
309 amdgpu_fence_process(ring);
310 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
311 return true;
312
313 return false;
314}
315
316static bool amdgpu_fence_is_signaled(struct fence *f)
317{
318 struct amdgpu_fence *fence = to_amdgpu_fence(f);
319 struct amdgpu_ring *ring = fence->ring;
320 struct amdgpu_device *adev = ring->adev;
321
322 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
323 return true;
324
325 if (down_read_trylock(&adev->exclusive_lock)) {
326 amdgpu_fence_process(ring);
327 up_read(&adev->exclusive_lock);
328
329 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
330 return true;
331 }
332 return false;
333}
334
335/**
336 * amdgpu_fence_enable_signaling - enable signalling on fence
337 * @fence: fence
338 *
339 * This function is called with fence_queue lock held, and adds a callback
340 * to fence_queue that checks if this fence is signaled, and if so it
341 * signals the fence and removes itself.
342 */
343static bool amdgpu_fence_enable_signaling(struct fence *f)
344{
345 struct amdgpu_fence *fence = to_amdgpu_fence(f);
346 struct amdgpu_ring *ring = fence->ring;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347
348 if (atomic64_read(&ring->fence_drv.last_seq) >= fence->seq)
349 return false;
350
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400351 fence->fence_wake.flags = 0;
352 fence->fence_wake.private = NULL;
353 fence->fence_wake.func = amdgpu_fence_check_signaled;
monk.liu7f06c232015-07-30 18:28:12 +0800354 __add_wait_queue(&ring->fence_drv.fence_queue, &fence->fence_wake);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400355 fence_get(f);
356 FENCE_TRACE(&fence->base, "armed on ring %i!\n", ring->idx);
357 return true;
358}
359
monk.liu7f06c232015-07-30 18:28:12 +0800360/*
361 * amdgpu_ring_wait_seq_timeout - wait for seq of the specific ring to signal
362 * @ring: ring to wait on for the seq number
363 * @seq: seq number wait for
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400364 *
monk.liu7f06c232015-07-30 18:28:12 +0800365 * return value:
Christian König00d2a2b2015-08-07 16:15:36 +0200366 * 0: seq signaled, and gpu not hang
367 * -EDEADL: GPU hang detected
monk.liu7f06c232015-07-30 18:28:12 +0800368 * -EINVAL: some paramter is not valid
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400369 */
Christian König00d2a2b2015-08-07 16:15:36 +0200370static int amdgpu_fence_ring_wait_seq(struct amdgpu_ring *ring, uint64_t seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400371{
monk.liu7f06c232015-07-30 18:28:12 +0800372 bool signaled = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400373
monk.liu7f06c232015-07-30 18:28:12 +0800374 BUG_ON(!ring);
375 if (seq > ring->fence_drv.sync_seq[ring->idx])
376 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377
monk.liu7f06c232015-07-30 18:28:12 +0800378 if (atomic64_read(&ring->fence_drv.last_seq) >= seq)
Christian König00d2a2b2015-08-07 16:15:36 +0200379 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380
Christian König00d2a2b2015-08-07 16:15:36 +0200381 wait_event(ring->fence_drv.fence_queue, (
Christian Königb7e4dad2015-09-01 10:50:26 +0200382 (signaled = amdgpu_fence_seq_signaled(ring, seq))));
monk.liu7f06c232015-07-30 18:28:12 +0800383
Christian König00d2a2b2015-08-07 16:15:36 +0200384 if (signaled)
385 return 0;
386 else
387 return -EDEADLK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400388}
389
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400390/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400391 * amdgpu_fence_wait_next - wait for the next fence to signal
392 *
393 * @adev: amdgpu device pointer
394 * @ring: ring index the fence is associated with
395 *
396 * Wait for the next fence on the requested ring to signal (all asics).
397 * Returns 0 if the next fence has passed, error for all other cases.
398 * Caller must hold ring lock.
399 */
400int amdgpu_fence_wait_next(struct amdgpu_ring *ring)
401{
monk.liu7f06c232015-07-30 18:28:12 +0800402 uint64_t seq = atomic64_read(&ring->fence_drv.last_seq) + 1ULL;
Christian König00d2a2b2015-08-07 16:15:36 +0200403
monk.liu7f06c232015-07-30 18:28:12 +0800404 if (seq >= ring->fence_drv.sync_seq[ring->idx])
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400405 return -ENOENT;
monk.liu7f06c232015-07-30 18:28:12 +0800406
Christian König00d2a2b2015-08-07 16:15:36 +0200407 return amdgpu_fence_ring_wait_seq(ring, seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400408}
409
410/**
411 * amdgpu_fence_wait_empty - wait for all fences to signal
412 *
413 * @adev: amdgpu device pointer
414 * @ring: ring index the fence is associated with
415 *
416 * Wait for all fences on the requested ring to signal (all asics).
417 * Returns 0 if the fences have passed, error for all other cases.
418 * Caller must hold ring lock.
419 */
420int amdgpu_fence_wait_empty(struct amdgpu_ring *ring)
421{
monk.liu7f06c232015-07-30 18:28:12 +0800422 uint64_t seq = ring->fence_drv.sync_seq[ring->idx];
Christian König00d2a2b2015-08-07 16:15:36 +0200423
monk.liu7f06c232015-07-30 18:28:12 +0800424 if (!seq)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425 return 0;
426
Christian König00d2a2b2015-08-07 16:15:36 +0200427 return amdgpu_fence_ring_wait_seq(ring, seq);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428}
429
430/**
431 * amdgpu_fence_ref - take a ref on a fence
432 *
433 * @fence: amdgpu fence object
434 *
435 * Take a reference on a fence (all asics).
436 * Returns the fence.
437 */
438struct amdgpu_fence *amdgpu_fence_ref(struct amdgpu_fence *fence)
439{
440 fence_get(&fence->base);
441 return fence;
442}
443
444/**
445 * amdgpu_fence_unref - remove a ref on a fence
446 *
447 * @fence: amdgpu fence object
448 *
449 * Remove a reference on a fence (all asics).
450 */
451void amdgpu_fence_unref(struct amdgpu_fence **fence)
452{
453 struct amdgpu_fence *tmp = *fence;
454
455 *fence = NULL;
456 if (tmp)
457 fence_put(&tmp->base);
458}
459
460/**
461 * amdgpu_fence_count_emitted - get the count of emitted fences
462 *
463 * @ring: ring the fence is associated with
464 *
465 * Get the number of fences emitted on the requested ring (all asics).
466 * Returns the number of emitted fences on the ring. Used by the
467 * dynpm code to ring track activity.
468 */
469unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring)
470{
471 uint64_t emitted;
472
473 /* We are not protected by ring lock when reading the last sequence
474 * but it's ok to report slightly wrong fence count here.
475 */
476 amdgpu_fence_process(ring);
477 emitted = ring->fence_drv.sync_seq[ring->idx]
478 - atomic64_read(&ring->fence_drv.last_seq);
479 /* to avoid 32bits warp around */
480 if (emitted > 0x10000000)
481 emitted = 0x10000000;
482
483 return (unsigned)emitted;
484}
485
486/**
487 * amdgpu_fence_need_sync - do we need a semaphore
488 *
489 * @fence: amdgpu fence object
490 * @dst_ring: which ring to check against
491 *
492 * Check if the fence needs to be synced against another ring
493 * (all asics). If so, we need to emit a semaphore.
494 * Returns true if we need to sync with another ring, false if
495 * not.
496 */
497bool amdgpu_fence_need_sync(struct amdgpu_fence *fence,
498 struct amdgpu_ring *dst_ring)
499{
500 struct amdgpu_fence_driver *fdrv;
501
502 if (!fence)
503 return false;
504
505 if (fence->ring == dst_ring)
506 return false;
507
508 /* we are protected by the ring mutex */
509 fdrv = &dst_ring->fence_drv;
510 if (fence->seq <= fdrv->sync_seq[fence->ring->idx])
511 return false;
512
513 return true;
514}
515
516/**
517 * amdgpu_fence_note_sync - record the sync point
518 *
519 * @fence: amdgpu fence object
520 * @dst_ring: which ring to check against
521 *
522 * Note the sequence number at which point the fence will
523 * be synced with the requested ring (all asics).
524 */
525void amdgpu_fence_note_sync(struct amdgpu_fence *fence,
526 struct amdgpu_ring *dst_ring)
527{
528 struct amdgpu_fence_driver *dst, *src;
529 unsigned i;
530
531 if (!fence)
532 return;
533
534 if (fence->ring == dst_ring)
535 return;
536
537 /* we are protected by the ring mutex */
538 src = &fence->ring->fence_drv;
539 dst = &dst_ring->fence_drv;
540 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
541 if (i == dst_ring->idx)
542 continue;
543
544 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
545 }
546}
547
548/**
549 * amdgpu_fence_driver_start_ring - make the fence driver
550 * ready for use on the requested ring.
551 *
552 * @ring: ring to start the fence driver on
553 * @irq_src: interrupt source to use for this ring
554 * @irq_type: interrupt type to use for this ring
555 *
556 * Make the fence driver ready for processing (all asics).
557 * Not all asics have all rings, so each asic will only
558 * start the fence driver on the rings it has.
559 * Returns 0 for success, errors for failure.
560 */
561int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
562 struct amdgpu_irq_src *irq_src,
563 unsigned irq_type)
564{
565 struct amdgpu_device *adev = ring->adev;
566 uint64_t index;
567
568 if (ring != &adev->uvd.ring) {
569 ring->fence_drv.cpu_addr = &adev->wb.wb[ring->fence_offs];
570 ring->fence_drv.gpu_addr = adev->wb.gpu_addr + (ring->fence_offs * 4);
571 } else {
572 /* put fence directly behind firmware */
573 index = ALIGN(adev->uvd.fw->size, 8);
574 ring->fence_drv.cpu_addr = adev->uvd.cpu_addr + index;
575 ring->fence_drv.gpu_addr = adev->uvd.gpu_addr + index;
576 }
577 amdgpu_fence_write(ring, atomic64_read(&ring->fence_drv.last_seq));
Chunming Zhouc6a40792015-06-01 14:14:32 +0800578 amdgpu_irq_get(adev, irq_src, irq_type);
579
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 ring->fence_drv.irq_src = irq_src;
581 ring->fence_drv.irq_type = irq_type;
Chunming Zhouc6a40792015-06-01 14:14:32 +0800582 ring->fence_drv.initialized = true;
583
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 dev_info(adev->dev, "fence driver on ring %d use gpu addr 0x%016llx, "
585 "cpu addr 0x%p\n", ring->idx,
586 ring->fence_drv.gpu_addr, ring->fence_drv.cpu_addr);
587 return 0;
588}
589
590/**
591 * amdgpu_fence_driver_init_ring - init the fence driver
592 * for the requested ring.
593 *
594 * @ring: ring to init the fence driver on
595 *
596 * Init the fence driver for the requested ring (all asics).
597 * Helper function for amdgpu_fence_driver_init().
598 */
Christian König4f839a22015-09-08 20:22:31 +0200599int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600{
Christian König4f839a22015-09-08 20:22:31 +0200601 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400602
603 ring->fence_drv.cpu_addr = NULL;
604 ring->fence_drv.gpu_addr = 0;
605 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
606 ring->fence_drv.sync_seq[i] = 0;
607
608 atomic64_set(&ring->fence_drv.last_seq, 0);
609 ring->fence_drv.initialized = false;
610
611 INIT_DELAYED_WORK(&ring->fence_drv.lockup_work,
612 amdgpu_fence_check_lockup);
613 ring->fence_drv.ring = ring;
Alex Deucherb80d8472015-08-16 22:55:02 -0400614
Christian König5ec92a72015-09-07 18:43:02 +0200615 init_waitqueue_head(&ring->fence_drv.fence_queue);
616
Alex Deucherb80d8472015-08-16 22:55:02 -0400617 if (amdgpu_enable_scheduler) {
Junwei Zhang2440ff22015-10-10 08:48:42 +0800618 long timeout = msecs_to_jiffies(amdgpu_lockup_timeout);
619 if (timeout == 0) {
620 /*
621 * FIXME:
622 * Delayed workqueue cannot use it directly,
623 * so the scheduler will not use delayed workqueue if
624 * MAX_SCHEDULE_TIMEOUT is set.
625 * Currently keep it simple and silly.
626 */
627 timeout = MAX_SCHEDULE_TIMEOUT;
628 }
Christian König4f839a22015-09-08 20:22:31 +0200629 r = amd_sched_init(&ring->sched, &amdgpu_sched_ops,
Junwei Zhang2440ff22015-10-10 08:48:42 +0800630 amdgpu_sched_hw_submission,
631 timeout, ring->name);
Christian König4f839a22015-09-08 20:22:31 +0200632 if (r) {
633 DRM_ERROR("Failed to create scheduler on ring %s.\n",
634 ring->name);
635 return r;
636 }
Alex Deucherb80d8472015-08-16 22:55:02 -0400637 }
Christian König4f839a22015-09-08 20:22:31 +0200638
639 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640}
641
642/**
643 * amdgpu_fence_driver_init - init the fence driver
644 * for all possible rings.
645 *
646 * @adev: amdgpu device pointer
647 *
648 * Init the fence driver for all possible rings (all asics).
649 * Not all asics have all rings, so each asic will only
650 * start the fence driver on the rings it has using
651 * amdgpu_fence_driver_start_ring().
652 * Returns 0 for success.
653 */
654int amdgpu_fence_driver_init(struct amdgpu_device *adev)
655{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656 if (amdgpu_debugfs_fence_init(adev))
657 dev_err(adev->dev, "fence debugfs file creation failed\n");
658
659 return 0;
660}
661
662/**
663 * amdgpu_fence_driver_fini - tear down the fence driver
664 * for all possible rings.
665 *
666 * @adev: amdgpu device pointer
667 *
668 * Tear down the fence driver for all possible rings (all asics).
669 */
670void amdgpu_fence_driver_fini(struct amdgpu_device *adev)
671{
672 int i, r;
673
674 mutex_lock(&adev->ring_lock);
675 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
676 struct amdgpu_ring *ring = adev->rings[i];
677 if (!ring || !ring->fence_drv.initialized)
678 continue;
679 r = amdgpu_fence_wait_empty(ring);
680 if (r) {
681 /* no need to trigger GPU reset as we are unloading */
682 amdgpu_fence_driver_force_completion(adev);
683 }
monk.liu7f06c232015-07-30 18:28:12 +0800684 wake_up_all(&ring->fence_drv.fence_queue);
Chunming Zhouc6a40792015-06-01 14:14:32 +0800685 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
686 ring->fence_drv.irq_type);
Christian König4f839a22015-09-08 20:22:31 +0200687 amd_sched_fini(&ring->sched);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688 ring->fence_drv.initialized = false;
689 }
690 mutex_unlock(&adev->ring_lock);
691}
692
693/**
Alex Deucher5ceb54c2015-08-05 12:41:48 -0400694 * amdgpu_fence_driver_suspend - suspend the fence driver
695 * for all possible rings.
696 *
697 * @adev: amdgpu device pointer
698 *
699 * Suspend the fence driver for all possible rings (all asics).
700 */
701void amdgpu_fence_driver_suspend(struct amdgpu_device *adev)
702{
703 int i, r;
704
705 mutex_lock(&adev->ring_lock);
706 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
707 struct amdgpu_ring *ring = adev->rings[i];
708 if (!ring || !ring->fence_drv.initialized)
709 continue;
710
711 /* wait for gpu to finish processing current batch */
712 r = amdgpu_fence_wait_empty(ring);
713 if (r) {
714 /* delay GPU reset to resume */
715 amdgpu_fence_driver_force_completion(adev);
716 }
717
718 /* disable the interrupt */
719 amdgpu_irq_put(adev, ring->fence_drv.irq_src,
720 ring->fence_drv.irq_type);
721 }
722 mutex_unlock(&adev->ring_lock);
723}
724
725/**
726 * amdgpu_fence_driver_resume - resume the fence driver
727 * for all possible rings.
728 *
729 * @adev: amdgpu device pointer
730 *
731 * Resume the fence driver for all possible rings (all asics).
732 * Not all asics have all rings, so each asic will only
733 * start the fence driver on the rings it has using
734 * amdgpu_fence_driver_start_ring().
735 * Returns 0 for success.
736 */
737void amdgpu_fence_driver_resume(struct amdgpu_device *adev)
738{
739 int i;
740
741 mutex_lock(&adev->ring_lock);
742 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
743 struct amdgpu_ring *ring = adev->rings[i];
744 if (!ring || !ring->fence_drv.initialized)
745 continue;
746
747 /* enable the interrupt */
748 amdgpu_irq_get(adev, ring->fence_drv.irq_src,
749 ring->fence_drv.irq_type);
750 }
751 mutex_unlock(&adev->ring_lock);
752}
753
754/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400755 * amdgpu_fence_driver_force_completion - force all fence waiter to complete
756 *
757 * @adev: amdgpu device pointer
758 *
759 * In case of GPU reset failure make sure no process keep waiting on fence
760 * that will never complete.
761 */
762void amdgpu_fence_driver_force_completion(struct amdgpu_device *adev)
763{
764 int i;
765
766 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
767 struct amdgpu_ring *ring = adev->rings[i];
768 if (!ring || !ring->fence_drv.initialized)
769 continue;
770
771 amdgpu_fence_write(ring, ring->fence_drv.sync_seq[i]);
772 }
773}
774
775
776/*
777 * Fence debugfs
778 */
779#if defined(CONFIG_DEBUG_FS)
780static int amdgpu_debugfs_fence_info(struct seq_file *m, void *data)
781{
782 struct drm_info_node *node = (struct drm_info_node *)m->private;
783 struct drm_device *dev = node->minor->dev;
784 struct amdgpu_device *adev = dev->dev_private;
785 int i, j;
786
787 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
788 struct amdgpu_ring *ring = adev->rings[i];
789 if (!ring || !ring->fence_drv.initialized)
790 continue;
791
792 amdgpu_fence_process(ring);
793
Christian König344c19f2015-06-02 15:47:16 +0200794 seq_printf(m, "--- ring %d (%s) ---\n", i, ring->name);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400795 seq_printf(m, "Last signaled fence 0x%016llx\n",
796 (unsigned long long)atomic64_read(&ring->fence_drv.last_seq));
797 seq_printf(m, "Last emitted 0x%016llx\n",
798 ring->fence_drv.sync_seq[i]);
799
800 for (j = 0; j < AMDGPU_MAX_RINGS; ++j) {
801 struct amdgpu_ring *other = adev->rings[j];
Christian König344c19f2015-06-02 15:47:16 +0200802 if (i != j && other && other->fence_drv.initialized &&
803 ring->fence_drv.sync_seq[j])
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 seq_printf(m, "Last sync to ring %d 0x%016llx\n",
805 j, ring->fence_drv.sync_seq[j]);
806 }
807 }
808 return 0;
809}
810
811static struct drm_info_list amdgpu_debugfs_fence_list[] = {
812 {"amdgpu_fence_info", &amdgpu_debugfs_fence_info, 0, NULL},
813};
814#endif
815
816int amdgpu_debugfs_fence_init(struct amdgpu_device *adev)
817{
818#if defined(CONFIG_DEBUG_FS)
819 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_fence_list, 1);
820#else
821 return 0;
822#endif
823}
824
825static const char *amdgpu_fence_get_driver_name(struct fence *fence)
826{
827 return "amdgpu";
828}
829
830static const char *amdgpu_fence_get_timeline_name(struct fence *f)
831{
832 struct amdgpu_fence *fence = to_amdgpu_fence(f);
833 return (const char *)fence->ring->name;
834}
835
836static inline bool amdgpu_test_signaled(struct amdgpu_fence *fence)
837{
838 return test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->base.flags);
839}
840
Chunming Zhou4ce98912015-08-19 16:41:19 +0800841static bool amdgpu_test_signaled_any(struct fence **fences, uint32_t count)
monk.liu332dfe92015-07-30 15:19:05 +0800842{
843 int idx;
Chunming Zhou4ce98912015-08-19 16:41:19 +0800844 struct fence *fence;
monk.liu332dfe92015-07-30 15:19:05 +0800845
Junwei Zhang1aa40512015-08-19 16:24:19 +0800846 for (idx = 0; idx < count; ++idx) {
monk.liu332dfe92015-07-30 15:19:05 +0800847 fence = fences[idx];
848 if (fence) {
Chunming Zhou4ce98912015-08-19 16:41:19 +0800849 if (test_bit(FENCE_FLAG_SIGNALED_BIT, &fence->flags))
monk.liu332dfe92015-07-30 15:19:05 +0800850 return true;
851 }
852 }
853 return false;
854}
855
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400856struct amdgpu_wait_cb {
857 struct fence_cb base;
858 struct task_struct *task;
859};
860
861static void amdgpu_fence_wait_cb(struct fence *fence, struct fence_cb *cb)
862{
863 struct amdgpu_wait_cb *wait =
864 container_of(cb, struct amdgpu_wait_cb, base);
865 wake_up_process(wait->task);
866}
867
868static signed long amdgpu_fence_default_wait(struct fence *f, bool intr,
869 signed long t)
870{
871 struct amdgpu_fence *fence = to_amdgpu_fence(f);
872 struct amdgpu_device *adev = fence->ring->adev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400873
Christian König8221d702015-09-02 12:14:57 -0400874 return amdgpu_fence_wait_any(adev, &f, 1, intr, t);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400875}
876
Junwei Zhang1aa40512015-08-19 16:24:19 +0800877/**
878 * Wait the fence array with timeout
879 *
880 * @adev: amdgpu device
881 * @array: the fence array with amdgpu fence pointer
882 * @count: the number of the fence array
Junwei Zhang1aa40512015-08-19 16:24:19 +0800883 * @intr: when sleep, set the current task interruptable or not
884 * @t: timeout to wait
885 *
Christian König8221d702015-09-02 12:14:57 -0400886 * It will return when any fence is signaled or timeout.
Junwei Zhang1aa40512015-08-19 16:24:19 +0800887 */
Christian König8221d702015-09-02 12:14:57 -0400888signed long amdgpu_fence_wait_any(struct amdgpu_device *adev,
889 struct fence **array, uint32_t count,
890 bool intr, signed long t)
monk.liu332dfe92015-07-30 15:19:05 +0800891{
Junwei Zhang1aa40512015-08-19 16:24:19 +0800892 struct amdgpu_wait_cb *cb;
Chunming Zhou4ce98912015-08-19 16:41:19 +0800893 struct fence *fence;
Christian König8221d702015-09-02 12:14:57 -0400894 unsigned idx;
monk.liu332dfe92015-07-30 15:19:05 +0800895
896 BUG_ON(!array);
897
Junwei Zhang1aa40512015-08-19 16:24:19 +0800898 cb = kcalloc(count, sizeof(struct amdgpu_wait_cb), GFP_KERNEL);
899 if (cb == NULL) {
900 t = -ENOMEM;
901 goto err_free_cb;
902 }
903
904 for (idx = 0; idx < count; ++idx) {
monk.liu332dfe92015-07-30 15:19:05 +0800905 fence = array[idx];
906 if (fence) {
907 cb[idx].task = current;
Chunming Zhou4ce98912015-08-19 16:41:19 +0800908 if (fence_add_callback(fence,
Junwei Zhang1aa40512015-08-19 16:24:19 +0800909 &cb[idx].base, amdgpu_fence_wait_cb)) {
910 /* The fence is already signaled */
Christian König8221d702015-09-02 12:14:57 -0400911 goto fence_rm_cb;
Junwei Zhang1aa40512015-08-19 16:24:19 +0800912 }
monk.liu332dfe92015-07-30 15:19:05 +0800913 }
914 }
915
916 while (t > 0) {
917 if (intr)
918 set_current_state(TASK_INTERRUPTIBLE);
919 else
920 set_current_state(TASK_UNINTERRUPTIBLE);
921
922 /*
923 * amdgpu_test_signaled_any must be called after
924 * set_current_state to prevent a race with wake_up_process
925 */
Christian König8221d702015-09-02 12:14:57 -0400926 if (amdgpu_test_signaled_any(array, count))
monk.liu332dfe92015-07-30 15:19:05 +0800927 break;
928
monk.liu332dfe92015-07-30 15:19:05 +0800929 t = schedule_timeout(t);
930
931 if (t > 0 && intr && signal_pending(current))
932 t = -ERESTARTSYS;
933 }
934
935 __set_current_state(TASK_RUNNING);
936
Junwei Zhang1aa40512015-08-19 16:24:19 +0800937fence_rm_cb:
938 for (idx = 0; idx < count; ++idx) {
monk.liu332dfe92015-07-30 15:19:05 +0800939 fence = array[idx];
Junwei Zhang113cd9d2015-08-26 09:34:59 +0800940 if (fence && cb[idx].base.func)
Chunming Zhou4ce98912015-08-19 16:41:19 +0800941 fence_remove_callback(fence, &cb[idx].base);
monk.liu332dfe92015-07-30 15:19:05 +0800942 }
943
Junwei Zhang1aa40512015-08-19 16:24:19 +0800944err_free_cb:
945 kfree(cb);
946
monk.liu332dfe92015-07-30 15:19:05 +0800947 return t;
948}
949
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950const struct fence_ops amdgpu_fence_ops = {
951 .get_driver_name = amdgpu_fence_get_driver_name,
952 .get_timeline_name = amdgpu_fence_get_timeline_name,
953 .enable_signaling = amdgpu_fence_enable_signaling,
954 .signaled = amdgpu_fence_is_signaled,
955 .wait = amdgpu_fence_default_wait,
956 .release = NULL,
957};