radeon_fence.c revision 1.5.6.2 1 /*
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 (at) 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 "radeon_reg.h"
39 #include "radeon.h"
40 #include "radeon_trace.h"
41
42 /*
43 * Fences
44 * Fences mark an event in the GPUs pipeline and are used
45 * for GPU/CPU synchronization. When the fence is written,
46 * it is expected that all buffers associated with that fence
47 * are no longer in use by the associated ring on the GPU and
48 * that the the relevant GPU caches have been flushed. Whether
49 * we use a scratch register or memory location depends on the asic
50 * and whether writeback is enabled.
51 */
52
53 /**
54 * radeon_fence_write - write a fence value
55 *
56 * @rdev: radeon_device pointer
57 * @seq: sequence number to write
58 * @ring: ring index the fence is associated with
59 *
60 * Writes a fence value to memory or a scratch register (all asics).
61 */
62 static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
63 {
64 struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
65 if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
66 if (drv->cpu_addr) {
67 *drv->cpu_addr = cpu_to_le32(seq);
68 }
69 } else {
70 WREG32(drv->scratch_reg, seq);
71 }
72 }
73
74 /**
75 * radeon_fence_read - read a fence value
76 *
77 * @rdev: radeon_device pointer
78 * @ring: ring index the fence is associated with
79 *
80 * Reads a fence value from memory or a scratch register (all asics).
81 * Returns the value of the fence read from memory or register.
82 */
83 static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
84 {
85 struct radeon_fence_driver *drv = &rdev->fence_drv[ring];
86 u32 seq = 0;
87
88 if (likely(rdev->wb.enabled || !drv->scratch_reg)) {
89 if (drv->cpu_addr) {
90 seq = le32_to_cpu(*drv->cpu_addr);
91 } else {
92 seq = lower_32_bits(atomic64_read(&drv->last_seq));
93 }
94 } else {
95 seq = RREG32(drv->scratch_reg);
96 }
97 return seq;
98 }
99
100 /**
101 * radeon_fence_emit - emit a fence on the requested ring
102 *
103 * @rdev: radeon_device pointer
104 * @fence: radeon fence object
105 * @ring: ring index the fence is associated with
106 *
107 * Emits a fence command on the requested ring (all asics).
108 * Returns 0 on success, -ENOMEM on failure.
109 */
110 int radeon_fence_emit(struct radeon_device *rdev,
111 struct radeon_fence **fence,
112 int ring)
113 {
114 /* we are protected by the ring emission mutex */
115 *fence = kmalloc(sizeof(struct radeon_fence), GFP_KERNEL);
116 if ((*fence) == NULL) {
117 return -ENOMEM;
118 }
119 kref_init(&((*fence)->kref));
120 (*fence)->rdev = rdev;
121 (*fence)->seq = ++rdev->fence_drv[ring].sync_seq[ring];
122 (*fence)->ring = ring;
123 radeon_fence_ring_emit(rdev, ring, *fence);
124 trace_radeon_fence_emit(rdev->ddev, ring, (*fence)->seq);
125 return 0;
126 }
127
128 /**
129 * radeon_fence_process - process a fence
130 *
131 * @rdev: radeon_device pointer
132 * @ring: ring index the fence is associated with
133 *
134 * Checks the current fence value and wakes the fence queue
135 * if the sequence number has increased (all asics).
136 */
137 static void radeon_fence_process_locked(struct radeon_device *rdev, int ring)
138 {
139 uint64_t seq, last_seq, last_emitted;
140 unsigned count_loop = 0;
141 bool wake = false;
142
143 BUG_ON(!spin_is_locked(&rdev->fence_lock));
144
145 /* Note there is a scenario here for an infinite loop but it's
146 * very unlikely to happen. For it to happen, the current polling
147 * process need to be interrupted by another process and another
148 * process needs to update the last_seq btw the atomic read and
149 * xchg of the current process.
150 *
151 * More over for this to go in infinite loop there need to be
152 * continuously new fence signaled ie radeon_fence_read needs
153 * to return a different value each time for both the currently
154 * polling process and the other process that xchg the last_seq
155 * btw atomic read and xchg of the current process. And the
156 * value the other process set as last seq must be higher than
157 * the seq value we just read. Which means that current process
158 * need to be interrupted after radeon_fence_read and before
159 * atomic xchg.
160 *
161 * To be even more safe we count the number of time we loop and
162 * we bail after 10 loop just accepting the fact that we might
163 * have temporarly set the last_seq not to the true real last
164 * seq but to an older one.
165 */
166 last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
167 do {
168 last_emitted = rdev->fence_drv[ring].sync_seq[ring];
169 seq = radeon_fence_read(rdev, ring);
170 seq |= last_seq & 0xffffffff00000000LL;
171 if (seq < last_seq) {
172 seq &= 0xffffffff;
173 seq |= last_emitted & 0xffffffff00000000LL;
174 }
175
176 if (seq <= last_seq || seq > last_emitted) {
177 break;
178 }
179 /* If we loop over we don't want to return without
180 * checking if a fence is signaled as it means that the
181 * seq we just read is different from the previous on.
182 */
183 wake = true;
184 last_seq = seq;
185 if ((count_loop++) > 10) {
186 /* We looped over too many time leave with the
187 * fact that we might have set an older fence
188 * seq then the current real last seq as signaled
189 * by the hw.
190 */
191 break;
192 }
193 } while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
194
195 if (wake)
196 #ifdef __NetBSD__
197 DRM_SPIN_WAKEUP_ALL(&rdev->fence_queue, &rdev->fence_lock);
198 #else
199 wake_up_all(&rdev->fence_queue);
200 #endif
201 }
202
203 void radeon_fence_process(struct radeon_device *rdev, int ring)
204 {
205
206 spin_lock(&rdev->fence_lock);
207 radeon_fence_process_locked(rdev, ring);
208 spin_unlock(&rdev->fence_lock);
209 }
210
211 /**
212 * radeon_fence_destroy - destroy a fence
213 *
214 * @kref: fence kref
215 *
216 * Frees the fence object (all asics).
217 */
218 static void radeon_fence_destroy(struct kref *kref)
219 {
220 struct radeon_fence *fence;
221
222 fence = container_of(kref, struct radeon_fence, kref);
223 kfree(fence);
224 }
225
226 /**
227 * radeon_fence_seq_signaled - check if a fence sequence number has signaled
228 *
229 * @rdev: radeon device pointer
230 * @seq: sequence number
231 * @ring: ring index the fence is associated with
232 *
233 * Check if the last signaled fence sequnce number is >= the requested
234 * sequence number (all asics).
235 * Returns true if the fence has signaled (current fence value
236 * is >= requested value) or false if it has not (current fence
237 * value is < the requested value. Helper function for
238 * radeon_fence_signaled().
239 */
240 static bool radeon_fence_seq_signaled(struct radeon_device *rdev,
241 u64 seq, unsigned ring)
242 {
243 BUG_ON(!spin_is_locked(&rdev->fence_lock));
244 if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
245 return true;
246 }
247 /* poll new last sequence at least once */
248 radeon_fence_process_locked(rdev, ring);
249 if (atomic64_read(&rdev->fence_drv[ring].last_seq) >= seq) {
250 return true;
251 }
252 return false;
253 }
254
255 /**
256 * radeon_fence_signaled - check if a fence has signaled
257 *
258 * @fence: radeon fence object
259 *
260 * Check if the requested fence has signaled (all asics).
261 * Returns true if the fence has signaled or false if it has not.
262 */
263 bool radeon_fence_signaled(struct radeon_fence *fence)
264 {
265 if (!fence) {
266 return true;
267 }
268 spin_lock(&fence->rdev->fence_lock);
269 if (fence->seq == RADEON_FENCE_SIGNALED_SEQ) {
270 spin_unlock(&fence->rdev->fence_lock);
271 return true;
272 }
273 if (radeon_fence_seq_signaled(fence->rdev, fence->seq, fence->ring)) {
274 fence->seq = RADEON_FENCE_SIGNALED_SEQ;
275 spin_unlock(&fence->rdev->fence_lock);
276 return true;
277 }
278 spin_unlock(&fence->rdev->fence_lock);
279 return false;
280 }
281
282 /**
283 * radeon_fence_any_seq_signaled - check if any sequence number is signaled
284 *
285 * @rdev: radeon device pointer
286 * @seq: sequence numbers
287 *
288 * Check if the last signaled fence sequnce number is >= the requested
289 * sequence number (all asics).
290 * Returns true if any has signaled (current value is >= requested value)
291 * or false if it has not. Helper function for radeon_fence_wait_seq.
292 */
293 static bool radeon_fence_any_seq_signaled(struct radeon_device *rdev, u64 *seq)
294 {
295 unsigned i;
296
297 BUG_ON(!spin_is_locked(&rdev->fence_lock));
298
299 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
300 if (seq[i] && radeon_fence_seq_signaled(rdev, seq[i], i))
301 return true;
302 }
303 return false;
304 }
305
306 /**
307 * radeon_fence_wait_seq - wait for a specific sequence numbers
308 *
309 * @rdev: radeon device pointer
310 * @target_seq: sequence number(s) we want to wait for
311 * @intr: use interruptable sleep
312 *
313 * Wait for the requested sequence number(s) to be written by any ring
314 * (all asics). Sequnce number array is indexed by ring id.
315 * @intr selects whether to use interruptable (true) or non-interruptable
316 * (false) sleep when waiting for the sequence number. Helper function
317 * for radeon_fence_wait_*().
318 * Returns 0 if the sequence number has passed, error for all other cases.
319 * -EDEADLK is returned when a GPU lockup has been detected.
320 */
321 static int radeon_fence_wait_seq(struct radeon_device *rdev, u64 *target_seq,
322 bool intr)
323 {
324 uint64_t last_seq[RADEON_NUM_RINGS];
325 bool signaled;
326 int i, r;
327
328 spin_lock(&rdev->fence_lock);
329 while (!radeon_fence_any_seq_signaled(rdev, target_seq)) {
330
331 /* Save current sequence values, used to check for GPU lockups */
332 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
333 if (!target_seq[i])
334 continue;
335
336 last_seq[i] = atomic64_read(&rdev->fence_drv[i].last_seq);
337 trace_radeon_fence_wait_begin(rdev->ddev, i, target_seq[i]);
338 radeon_irq_kms_sw_irq_get(rdev, i);
339 }
340
341 #ifdef __NetBSD__
342 if (intr)
343 DRM_SPIN_TIMED_WAIT_UNTIL(r, &rdev->fence_queue,
344 &rdev->fence_lock, RADEON_FENCE_JIFFIES_TIMEOUT,
345 ((signaled = radeon_fence_any_seq_signaled(rdev,
346 target_seq))
347 || rdev->needs_reset));
348 else
349 DRM_SPIN_TIMED_WAIT_NOINTR_UNTIL(r, &rdev->fence_queue,
350 &rdev->fence_lock, RADEON_FENCE_JIFFIES_TIMEOUT,
351 ((signaled = radeon_fence_any_seq_signaled(rdev,
352 target_seq))
353 || rdev->needs_reset));
354 #else
355 if (intr) {
356 r = wait_event_interruptible_timeout(rdev->fence_queue, (
357 (signaled = radeon_fence_any_seq_signaled(rdev, target_seq))
358 || rdev->needs_reset), RADEON_FENCE_JIFFIES_TIMEOUT);
359 } else {
360 r = wait_event_timeout(rdev->fence_queue, (
361 (signaled = radeon_fence_any_seq_signaled(rdev, target_seq))
362 || rdev->needs_reset), RADEON_FENCE_JIFFIES_TIMEOUT);
363 }
364 #endif
365
366 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
367 if (!target_seq[i])
368 continue;
369
370 radeon_irq_kms_sw_irq_put(rdev, i);
371 trace_radeon_fence_wait_end(rdev->ddev, i, target_seq[i]);
372 }
373
374 if (unlikely(r < 0))
375 goto out;
376
377 if (unlikely(!signaled)) {
378 if (rdev->needs_reset) {
379 r = -EDEADLK;
380 goto out;
381 }
382
383 /* we were interrupted for some reason and fence
384 * isn't signaled yet, resume waiting */
385 if (r)
386 continue;
387
388 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
389 if (!target_seq[i])
390 continue;
391
392 if (last_seq[i] != atomic64_read(&rdev->fence_drv[i].last_seq))
393 break;
394 }
395
396 if (i != RADEON_NUM_RINGS)
397 continue;
398
399 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
400 if (!target_seq[i])
401 continue;
402
403 if (radeon_ring_is_lockup(rdev, i, &rdev->ring[i]))
404 break;
405 }
406
407 if (i < RADEON_NUM_RINGS) {
408 /* good news we believe it's a lockup */
409 dev_warn(rdev->dev, "GPU lockup (waiting for "
410 "0x%016"PRIx64" last fence id 0x%016"PRIx64" on"
411 " ring %d)\n",
412 target_seq[i], last_seq[i], i);
413
414 /* remember that we need an reset */
415 rdev->needs_reset = true;
416 #ifdef __NetBSD__
417 DRM_SPIN_WAKEUP_ALL(&rdev->fence_queue,
418 &rdev->fence_lock);
419 #else
420 wake_up_all(&rdev->fence_queue);
421 #endif
422 r = -EDEADLK;
423 goto out;
424 }
425 }
426 }
427 out: spin_unlock(&rdev->fence_lock);
428 return 0;
429 }
430
431 /**
432 * radeon_fence_wait - wait for a fence to signal
433 *
434 * @fence: radeon fence object
435 * @intr: use interruptable sleep
436 *
437 * Wait for the requested fence to signal (all asics).
438 * @intr selects whether to use interruptable (true) or non-interruptable
439 * (false) sleep when waiting for the fence.
440 * Returns 0 if the fence has passed, error for all other cases.
441 */
442 int radeon_fence_wait(struct radeon_fence *fence, bool intr)
443 {
444 uint64_t seq[RADEON_NUM_RINGS] = {};
445 int r;
446
447 if (fence == NULL) {
448 WARN(1, "Querying an invalid fence : %p !\n", fence);
449 return -EINVAL;
450 }
451
452 seq[fence->ring] = fence->seq;
453 if (seq[fence->ring] == RADEON_FENCE_SIGNALED_SEQ)
454 return 0;
455
456 r = radeon_fence_wait_seq(fence->rdev, seq, intr);
457 if (r)
458 return r;
459
460 fence->seq = RADEON_FENCE_SIGNALED_SEQ;
461 return 0;
462 }
463
464 /**
465 * radeon_fence_wait_any - wait for a fence to signal on any ring
466 *
467 * @rdev: radeon device pointer
468 * @fences: radeon fence object(s)
469 * @intr: use interruptable sleep
470 *
471 * Wait for any requested fence to signal (all asics). Fence
472 * array is indexed by ring id. @intr selects whether to use
473 * interruptable (true) or non-interruptable (false) sleep when
474 * waiting for the fences. Used by the suballocator.
475 * Returns 0 if any fence has passed, error for all other cases.
476 */
477 int radeon_fence_wait_any(struct radeon_device *rdev,
478 struct radeon_fence **fences,
479 bool intr)
480 {
481 uint64_t seq[RADEON_NUM_RINGS];
482 unsigned i, num_rings = 0;
483 int r;
484
485 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
486 seq[i] = 0;
487
488 if (!fences[i]) {
489 continue;
490 }
491
492 seq[i] = fences[i]->seq;
493 ++num_rings;
494
495 /* test if something was allready signaled */
496 if (seq[i] == RADEON_FENCE_SIGNALED_SEQ)
497 return 0;
498 }
499
500 /* nothing to wait for ? */
501 if (num_rings == 0)
502 return -ENOENT;
503
504 r = radeon_fence_wait_seq(rdev, seq, intr);
505 if (r) {
506 return r;
507 }
508 return 0;
509 }
510
511 /**
512 * radeon_fence_wait_next - wait for the next fence to signal
513 *
514 * @rdev: radeon device pointer
515 * @ring: ring index the fence is associated with
516 *
517 * Wait for the next fence on the requested ring to signal (all asics).
518 * Returns 0 if the next fence has passed, error for all other cases.
519 * Caller must hold ring lock.
520 */
521 int radeon_fence_wait_next(struct radeon_device *rdev, int ring)
522 {
523 uint64_t seq[RADEON_NUM_RINGS] = {};
524
525 seq[ring] = atomic64_read(&rdev->fence_drv[ring].last_seq) + 1ULL;
526 if (seq[ring] >= rdev->fence_drv[ring].sync_seq[ring]) {
527 /* nothing to wait for, last_seq is
528 already the last emited fence */
529 return -ENOENT;
530 }
531 return radeon_fence_wait_seq(rdev, seq, false);
532 }
533
534 /**
535 * radeon_fence_wait_empty - wait for all fences to signal
536 *
537 * @rdev: radeon device pointer
538 * @ring: ring index the fence is associated with
539 *
540 * Wait for all fences on the requested ring to signal (all asics).
541 * Returns 0 if the fences have passed, error for all other cases.
542 * Caller must hold ring lock.
543 */
544 int radeon_fence_wait_empty(struct radeon_device *rdev, int ring)
545 {
546 uint64_t seq[RADEON_NUM_RINGS] = {};
547 int r;
548
549 seq[ring] = rdev->fence_drv[ring].sync_seq[ring];
550 if (!seq[ring])
551 return 0;
552
553 r = radeon_fence_wait_seq(rdev, seq, false);
554 if (r) {
555 if (r == -EDEADLK)
556 return -EDEADLK;
557
558 dev_err(rdev->dev, "error waiting for ring[%d] to become idle (%d)\n",
559 ring, r);
560 }
561 return 0;
562 }
563
564 /**
565 * radeon_fence_ref - take a ref on a fence
566 *
567 * @fence: radeon fence object
568 *
569 * Take a reference on a fence (all asics).
570 * Returns the fence.
571 */
572 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
573 {
574 kref_get(&fence->kref);
575 return fence;
576 }
577
578 /**
579 * radeon_fence_unref - remove a ref on a fence
580 *
581 * @fence: radeon fence object
582 *
583 * Remove a reference on a fence (all asics).
584 */
585 void radeon_fence_unref(struct radeon_fence **fence)
586 {
587 struct radeon_fence *tmp = *fence;
588
589 *fence = NULL;
590 if (tmp) {
591 kref_put(&tmp->kref, radeon_fence_destroy);
592 }
593 }
594
595 /**
596 * radeon_fence_count_emitted - get the count of emitted fences
597 *
598 * @rdev: radeon device pointer
599 * @ring: ring index the fence is associated with
600 *
601 * Get the number of fences emitted on the requested ring (all asics).
602 * Returns the number of emitted fences on the ring. Used by the
603 * dynpm code to ring track activity.
604 */
605 unsigned radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
606 {
607 uint64_t emitted;
608
609 /* We are not protected by ring lock when reading the last sequence
610 * but it's ok to report slightly wrong fence count here.
611 */
612 radeon_fence_process(rdev, ring);
613 emitted = rdev->fence_drv[ring].sync_seq[ring]
614 - atomic64_read(&rdev->fence_drv[ring].last_seq);
615 /* to avoid 32bits warp around */
616 if (emitted > 0x10000000) {
617 emitted = 0x10000000;
618 }
619 return (unsigned)emitted;
620 }
621
622 /**
623 * radeon_fence_need_sync - do we need a semaphore
624 *
625 * @fence: radeon fence object
626 * @dst_ring: which ring to check against
627 *
628 * Check if the fence needs to be synced against another ring
629 * (all asics). If so, we need to emit a semaphore.
630 * Returns true if we need to sync with another ring, false if
631 * not.
632 */
633 bool radeon_fence_need_sync(struct radeon_fence *fence, int dst_ring)
634 {
635 struct radeon_fence_driver *fdrv;
636
637 if (!fence) {
638 return false;
639 }
640
641 if (fence->ring == dst_ring) {
642 return false;
643 }
644
645 /* we are protected by the ring mutex */
646 fdrv = &fence->rdev->fence_drv[dst_ring];
647 if (fence->seq <= fdrv->sync_seq[fence->ring]) {
648 return false;
649 }
650
651 return true;
652 }
653
654 /**
655 * radeon_fence_note_sync - record the sync point
656 *
657 * @fence: radeon fence object
658 * @dst_ring: which ring to check against
659 *
660 * Note the sequence number at which point the fence will
661 * be synced with the requested ring (all asics).
662 */
663 void radeon_fence_note_sync(struct radeon_fence *fence, int dst_ring)
664 {
665 struct radeon_fence_driver *dst, *src;
666 unsigned i;
667
668 if (!fence) {
669 return;
670 }
671
672 if (fence->ring == dst_ring) {
673 return;
674 }
675
676 /* we are protected by the ring mutex */
677 src = &fence->rdev->fence_drv[fence->ring];
678 dst = &fence->rdev->fence_drv[dst_ring];
679 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
680 if (i == dst_ring) {
681 continue;
682 }
683 dst->sync_seq[i] = max(dst->sync_seq[i], src->sync_seq[i]);
684 }
685 }
686
687 /**
688 * radeon_fence_driver_start_ring - make the fence driver
689 * ready for use on the requested ring.
690 *
691 * @rdev: radeon device pointer
692 * @ring: ring index to start the fence driver on
693 *
694 * Make the fence driver ready for processing (all asics).
695 * Not all asics have all rings, so each asic will only
696 * start the fence driver on the rings it has.
697 * Returns 0 for success, errors for failure.
698 */
699 int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
700 {
701 uint64_t index;
702 int r;
703
704 radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
705 if (rdev->wb.use_event || !radeon_ring_supports_scratch_reg(rdev, &rdev->ring[ring])) {
706 rdev->fence_drv[ring].scratch_reg = 0;
707 if (ring != R600_RING_TYPE_UVD_INDEX) {
708 index = R600_WB_EVENT_OFFSET + ring * 4;
709 rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
710 rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr +
711 index;
712
713 } else {
714 /* put fence directly behind firmware */
715 #ifdef __NetBSD__ /* XXX ALIGN means something else. */
716 index = round_up(rdev->uvd_fw->size, 8);
717 #else
718 index = ALIGN(rdev->uvd_fw->size, 8);
719 #endif
720 rdev->fence_drv[ring].cpu_addr = (uint32_t *)((uint8_t *)rdev->uvd.cpu_addr + index);
721 rdev->fence_drv[ring].gpu_addr = rdev->uvd.gpu_addr + index;
722 }
723
724 } else {
725 r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
726 if (r) {
727 dev_err(rdev->dev, "fence failed to get scratch register\n");
728 return r;
729 }
730 index = RADEON_WB_SCRATCH_OFFSET +
731 rdev->fence_drv[ring].scratch_reg -
732 rdev->scratch.reg_base;
733 rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
734 rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
735 }
736 radeon_fence_write(rdev, atomic64_read(&rdev->fence_drv[ring].last_seq), ring);
737 rdev->fence_drv[ring].initialized = true;
738 dev_info(rdev->dev, "fence driver on ring %d use gpu addr 0x%016"PRIx64" and cpu addr 0x%p\n",
739 ring, rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
740 return 0;
741 }
742
743 /**
744 * radeon_fence_driver_init_ring - init the fence driver
745 * for the requested ring.
746 *
747 * @rdev: radeon device pointer
748 * @ring: ring index to start the fence driver on
749 *
750 * Init the fence driver for the requested ring (all asics).
751 * Helper function for radeon_fence_driver_init().
752 */
753 static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
754 {
755 int i;
756
757 rdev->fence_drv[ring].scratch_reg = -1;
758 rdev->fence_drv[ring].cpu_addr = NULL;
759 rdev->fence_drv[ring].gpu_addr = 0;
760 for (i = 0; i < RADEON_NUM_RINGS; ++i)
761 rdev->fence_drv[ring].sync_seq[i] = 0;
762 atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
763 rdev->fence_drv[ring].initialized = false;
764 }
765
766 /**
767 * radeon_fence_driver_init - init the fence driver
768 * for all possible rings.
769 *
770 * @rdev: radeon device pointer
771 *
772 * Init the fence driver for all possible rings (all asics).
773 * Not all asics have all rings, so each asic will only
774 * start the fence driver on the rings it has using
775 * radeon_fence_driver_start_ring().
776 * Returns 0 for success.
777 */
778 int radeon_fence_driver_init(struct radeon_device *rdev)
779 {
780 int ring;
781
782 #ifdef __NetBSD__
783 spin_lock_init(&rdev->fence_lock);
784 DRM_INIT_WAITQUEUE(&rdev->fence_queue, "radfence");
785 #else
786 init_waitqueue_head(&rdev->fence_queue);
787 #endif
788 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
789 radeon_fence_driver_init_ring(rdev, ring);
790 }
791 if (radeon_debugfs_fence_init(rdev)) {
792 dev_err(rdev->dev, "fence debugfs file creation failed\n");
793 }
794 return 0;
795 }
796
797 /**
798 * radeon_fence_driver_fini - tear down the fence driver
799 * for all possible rings.
800 *
801 * @rdev: radeon device pointer
802 *
803 * Tear down the fence driver for all possible rings (all asics).
804 */
805 void radeon_fence_driver_fini(struct radeon_device *rdev)
806 {
807 int ring, r;
808
809 mutex_lock(&rdev->ring_lock);
810 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
811 if (!rdev->fence_drv[ring].initialized)
812 continue;
813 r = radeon_fence_wait_empty(rdev, ring);
814 if (r) {
815 /* no need to trigger GPU reset as we are unloading */
816 radeon_fence_driver_force_completion(rdev);
817 }
818 #ifdef __NetBSD__
819 spin_lock(&rdev->fence_lock);
820 DRM_SPIN_WAKEUP_ALL(&rdev->fence_queue, &rdev->fence_lock);
821 spin_unlock(&rdev->fence_lock);
822 #else
823 wake_up_all(&rdev->fence_queue);
824 #endif
825 radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
826 rdev->fence_drv[ring].initialized = false;
827 }
828 mutex_unlock(&rdev->ring_lock);
829
830 #ifdef __NetBSD__
831 DRM_DESTROY_WAITQUEUE(&rdev->fence_queue);
832 spin_lock_destroy(&rdev->fence_lock);
833 #endif
834 }
835
836 /**
837 * radeon_fence_driver_force_completion - force all fence waiter to complete
838 *
839 * @rdev: radeon device pointer
840 *
841 * In case of GPU reset failure make sure no process keep waiting on fence
842 * that will never complete.
843 */
844 void radeon_fence_driver_force_completion(struct radeon_device *rdev)
845 {
846 int ring;
847
848 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
849 if (!rdev->fence_drv[ring].initialized)
850 continue;
851 radeon_fence_write(rdev, rdev->fence_drv[ring].sync_seq[ring], ring);
852 }
853 }
854
855
856 /*
857 * Fence debugfs
858 */
859 #if defined(CONFIG_DEBUG_FS)
860 static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
861 {
862 struct drm_info_node *node = (struct drm_info_node *)m->private;
863 struct drm_device *dev = node->minor->dev;
864 struct radeon_device *rdev = dev->dev_private;
865 int i, j;
866
867 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
868 if (!rdev->fence_drv[i].initialized)
869 continue;
870
871 radeon_fence_process(rdev, i);
872
873 seq_printf(m, "--- ring %d ---\n", i);
874 seq_printf(m, "Last signaled fence 0x%016llx\n",
875 (unsigned long long)atomic64_read(&rdev->fence_drv[i].last_seq));
876 seq_printf(m, "Last emitted 0x%016"PRIx64"\n",
877 rdev->fence_drv[i].sync_seq[i]);
878
879 for (j = 0; j < RADEON_NUM_RINGS; ++j) {
880 if (i != j && rdev->fence_drv[j].initialized)
881 seq_printf(m, "Last sync to ring %d 0x%016"PRIx64"\n",
882 j, rdev->fence_drv[i].sync_seq[j]);
883 }
884 }
885 return 0;
886 }
887
888 static struct drm_info_list radeon_debugfs_fence_list[] = {
889 {"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
890 };
891 #endif
892
893 int radeon_debugfs_fence_init(struct radeon_device *rdev)
894 {
895 #if defined(CONFIG_DEBUG_FS)
896 return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 1);
897 #else
898 return 0;
899 #endif
900 }
901