2 * Copyright 2009 Jerome Glisse.
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:
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.
21 * The above copyright notice and this permission notice (including the
22 * next paragraph) shall be included in all copies or substantial portions
28 * Jerome Glisse <glisse@freedesktop.org>
31 #include <linux/seq_file.h>
32 #include <linux/atomic.h>
33 #include <linux/wait.h>
34 #include <linux/list.h>
35 #include <linux/kref.h>
36 #include <linux/slab.h>
39 #include "radeon_reg.h"
41 #include "radeon_trace.h"
43 static void radeon_fence_write(struct radeon_device *rdev, u32 seq, int ring)
45 if (rdev->wb.enabled) {
46 *rdev->fence_drv[ring].cpu_addr = cpu_to_le32(seq);
48 WREG32(rdev->fence_drv[ring].scratch_reg, seq);
52 static u32 radeon_fence_read(struct radeon_device *rdev, int ring)
56 if (rdev->wb.enabled) {
57 seq = le32_to_cpu(*rdev->fence_drv[ring].cpu_addr);
59 seq = RREG32(rdev->fence_drv[ring].scratch_reg);
64 int radeon_fence_emit(struct radeon_device *rdev, struct radeon_fence *fence)
66 unsigned long irq_flags;
68 write_lock_irqsave(&rdev->fence_lock, irq_flags);
69 if (fence->seq && fence->seq < RADEON_FENCE_NOTEMITED_SEQ) {
70 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
73 /* we are protected by the ring emission mutex */
74 fence->seq = ++rdev->fence_drv[fence->ring].seq;
75 radeon_fence_ring_emit(rdev, fence->ring, fence);
76 trace_radeon_fence_emit(rdev->ddev, fence->seq);
77 /* are we the first fence on a previusly idle ring? */
78 if (list_empty(&rdev->fence_drv[fence->ring].emitted)) {
79 rdev->fence_drv[fence->ring].last_activity = jiffies;
81 list_move_tail(&fence->list, &rdev->fence_drv[fence->ring].emitted);
82 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
86 static bool radeon_fence_poll_locked(struct radeon_device *rdev, int ring)
88 struct radeon_fence *fence;
89 struct list_head *i, *n;
90 uint64_t seq, last_seq;
91 unsigned count_loop = 0;
94 /* Note there is a scenario here for an infinite loop but it's
95 * very unlikely to happen. For it to happen, the current polling
96 * process need to be interrupted by another process and another
97 * process needs to update the last_seq btw the atomic read and
98 * xchg of the current process.
100 * More over for this to go in infinite loop there need to be
101 * continuously new fence signaled ie radeon_fence_read needs
102 * to return a different value each time for both the currently
103 * polling process and the other process that xchg the last_seq
104 * btw atomic read and xchg of the current process. And the
105 * value the other process set as last seq must be higher than
106 * the seq value we just read. Which means that current process
107 * need to be interrupted after radeon_fence_read and before
110 * To be even more safe we count the number of time we loop and
111 * we bail after 10 loop just accepting the fact that we might
112 * have temporarly set the last_seq not to the true real last
113 * seq but to an older one.
115 last_seq = atomic64_read(&rdev->fence_drv[ring].last_seq);
117 seq = radeon_fence_read(rdev, ring);
118 seq |= last_seq & 0xffffffff00000000LL;
119 if (seq < last_seq) {
120 seq += 0x100000000LL;
123 if (!wake && seq == last_seq) {
126 /* If we loop over we don't want to return without
127 * checking if a fence is signaled as it means that the
128 * seq we just read is different from the previous on.
131 if ((count_loop++) > 10) {
132 /* We looped over too many time leave with the
133 * fact that we might have set an older fence
134 * seq then the current real last seq as signaled
140 } while (atomic64_xchg(&rdev->fence_drv[ring].last_seq, seq) > seq);
142 /* reset wake to false */
144 rdev->fence_drv[ring].last_activity = jiffies;
147 list_for_each(i, &rdev->fence_drv[ring].emitted) {
148 fence = list_entry(i, struct radeon_fence, list);
149 if (fence->seq == seq) {
154 /* all fence previous to this one are considered as signaled */
159 list_move_tail(i, &rdev->fence_drv[ring].signaled);
160 fence = list_entry(i, struct radeon_fence, list);
161 fence->seq = RADEON_FENCE_SIGNALED_SEQ;
163 } while (i != &rdev->fence_drv[ring].emitted);
169 static void radeon_fence_destroy(struct kref *kref)
171 unsigned long irq_flags;
172 struct radeon_fence *fence;
174 fence = container_of(kref, struct radeon_fence, kref);
175 write_lock_irqsave(&fence->rdev->fence_lock, irq_flags);
176 list_del(&fence->list);
177 fence->seq = RADEON_FENCE_NOTEMITED_SEQ;
178 write_unlock_irqrestore(&fence->rdev->fence_lock, irq_flags);
179 if (fence->semaphore)
180 radeon_semaphore_free(fence->rdev, fence->semaphore);
184 int radeon_fence_create(struct radeon_device *rdev,
185 struct radeon_fence **fence,
188 *fence = kmalloc(sizeof(struct radeon_fence), GFP_KERNEL);
189 if ((*fence) == NULL) {
192 kref_init(&((*fence)->kref));
193 (*fence)->rdev = rdev;
194 (*fence)->seq = RADEON_FENCE_NOTEMITED_SEQ;
195 (*fence)->ring = ring;
196 (*fence)->semaphore = NULL;
197 INIT_LIST_HEAD(&(*fence)->list);
201 bool radeon_fence_signaled(struct radeon_fence *fence)
203 unsigned long irq_flags;
204 bool signaled = false;
209 write_lock_irqsave(&fence->rdev->fence_lock, irq_flags);
210 signaled = (fence->seq == RADEON_FENCE_SIGNALED_SEQ);
211 /* if we are shuting down report all fence as signaled */
212 if (fence->rdev->shutdown) {
215 if (fence->seq == RADEON_FENCE_NOTEMITED_SEQ) {
216 WARN(1, "Querying an unemitted fence : %p !\n", fence);
220 radeon_fence_poll_locked(fence->rdev, fence->ring);
221 signaled = (fence->seq == RADEON_FENCE_SIGNALED_SEQ);
223 write_unlock_irqrestore(&fence->rdev->fence_lock, irq_flags);
227 int radeon_fence_wait(struct radeon_fence *fence, bool intr)
229 struct radeon_device *rdev;
230 unsigned long irq_flags, timeout, last_activity;
236 WARN(1, "Querying an invalid fence : %p !\n", fence);
241 signaled = radeon_fence_signaled(fence);
243 read_lock_irqsave(&rdev->fence_lock, irq_flags);
244 timeout = jiffies - RADEON_FENCE_JIFFIES_TIMEOUT;
245 if (time_after(rdev->fence_drv[fence->ring].last_activity, timeout)) {
246 /* the normal case, timeout is somewhere before last_activity */
247 timeout = rdev->fence_drv[fence->ring].last_activity - timeout;
249 /* either jiffies wrapped around, or no fence was signaled in the last 500ms
250 * anyway we will just wait for the minimum amount and then check for a lockup */
253 /* save current sequence value used to check for GPU lockups */
254 seq = atomic64_read(&rdev->fence_drv[fence->ring].last_seq);
255 /* Save current last activity valuee, used to check for GPU lockups */
256 last_activity = rdev->fence_drv[fence->ring].last_activity;
257 read_unlock_irqrestore(&rdev->fence_lock, irq_flags);
259 trace_radeon_fence_wait_begin(rdev->ddev, seq);
260 radeon_irq_kms_sw_irq_get(rdev, fence->ring);
262 r = wait_event_interruptible_timeout(
263 rdev->fence_drv[fence->ring].queue,
264 (signaled = radeon_fence_signaled(fence)), timeout);
266 r = wait_event_timeout(
267 rdev->fence_drv[fence->ring].queue,
268 (signaled = radeon_fence_signaled(fence)), timeout);
270 radeon_irq_kms_sw_irq_put(rdev, fence->ring);
271 if (unlikely(r < 0)) {
274 trace_radeon_fence_wait_end(rdev->ddev, seq);
276 if (unlikely(!signaled)) {
277 /* we were interrupted for some reason and fence
278 * isn't signaled yet, resume waiting */
283 write_lock_irqsave(&rdev->fence_lock, irq_flags);
284 /* test if somebody else has already decided that this is a lockup */
285 if (last_activity != rdev->fence_drv[fence->ring].last_activity) {
286 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
290 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
292 if (radeon_ring_is_lockup(rdev, fence->ring, &rdev->ring[fence->ring])) {
293 /* good news we believe it's a lockup */
294 dev_warn(rdev->dev, "GPU lockup (waiting for 0x%016llx last fence id 0x%016llx)\n",
297 /* change last activity so nobody else think there is a lockup */
298 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
299 rdev->fence_drv[i].last_activity = jiffies;
302 /* mark the ring as not ready any more */
303 rdev->ring[fence->ring].ready = false;
311 int radeon_fence_wait_next(struct radeon_device *rdev, int ring)
313 unsigned long irq_flags;
314 struct radeon_fence *fence;
317 write_lock_irqsave(&rdev->fence_lock, irq_flags);
318 if (!rdev->ring[ring].ready) {
319 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
322 if (list_empty(&rdev->fence_drv[ring].emitted)) {
323 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
326 fence = list_entry(rdev->fence_drv[ring].emitted.next,
327 struct radeon_fence, list);
328 radeon_fence_ref(fence);
329 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
330 r = radeon_fence_wait(fence, false);
331 radeon_fence_unref(&fence);
335 int radeon_fence_wait_empty(struct radeon_device *rdev, int ring)
337 unsigned long irq_flags;
338 struct radeon_fence *fence;
341 write_lock_irqsave(&rdev->fence_lock, irq_flags);
342 if (!rdev->ring[ring].ready) {
343 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
346 if (list_empty(&rdev->fence_drv[ring].emitted)) {
347 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
350 fence = list_entry(rdev->fence_drv[ring].emitted.prev,
351 struct radeon_fence, list);
352 radeon_fence_ref(fence);
353 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
354 r = radeon_fence_wait(fence, false);
355 radeon_fence_unref(&fence);
359 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
361 kref_get(&fence->kref);
365 void radeon_fence_unref(struct radeon_fence **fence)
367 struct radeon_fence *tmp = *fence;
371 kref_put(&tmp->kref, radeon_fence_destroy);
375 void radeon_fence_process(struct radeon_device *rdev, int ring)
377 unsigned long irq_flags;
380 write_lock_irqsave(&rdev->fence_lock, irq_flags);
381 wake = radeon_fence_poll_locked(rdev, ring);
382 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
384 wake_up_all(&rdev->fence_drv[ring].queue);
388 int radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
390 unsigned long irq_flags;
391 int not_processed = 0;
393 read_lock_irqsave(&rdev->fence_lock, irq_flags);
394 if (!rdev->fence_drv[ring].initialized) {
395 read_unlock_irqrestore(&rdev->fence_lock, irq_flags);
399 if (!list_empty(&rdev->fence_drv[ring].emitted)) {
400 struct list_head *ptr;
401 list_for_each(ptr, &rdev->fence_drv[ring].emitted) {
402 /* count up to 3, that's enought info */
403 if (++not_processed >= 3)
407 read_unlock_irqrestore(&rdev->fence_lock, irq_flags);
408 return not_processed;
411 int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
413 unsigned long irq_flags;
417 write_lock_irqsave(&rdev->fence_lock, irq_flags);
418 radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
419 if (rdev->wb.use_event) {
420 rdev->fence_drv[ring].scratch_reg = 0;
421 index = R600_WB_EVENT_OFFSET + ring * 4;
423 r = radeon_scratch_get(rdev, &rdev->fence_drv[ring].scratch_reg);
425 dev_err(rdev->dev, "fence failed to get scratch register\n");
426 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
429 index = RADEON_WB_SCRATCH_OFFSET +
430 rdev->fence_drv[ring].scratch_reg -
431 rdev->scratch.reg_base;
433 rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
434 rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
435 radeon_fence_write(rdev, rdev->fence_drv[ring].seq, ring);
436 rdev->fence_drv[ring].initialized = true;
437 DRM_INFO("fence driver on ring %d use gpu addr 0x%016llx and cpu addr 0x%p\n",
438 ring, rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
439 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
443 static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
445 rdev->fence_drv[ring].scratch_reg = -1;
446 rdev->fence_drv[ring].cpu_addr = NULL;
447 rdev->fence_drv[ring].gpu_addr = 0;
448 rdev->fence_drv[ring].seq = 0;
449 atomic64_set(&rdev->fence_drv[ring].last_seq, 0);
450 INIT_LIST_HEAD(&rdev->fence_drv[ring].emitted);
451 INIT_LIST_HEAD(&rdev->fence_drv[ring].signaled);
452 init_waitqueue_head(&rdev->fence_drv[ring].queue);
453 rdev->fence_drv[ring].initialized = false;
456 int radeon_fence_driver_init(struct radeon_device *rdev)
458 unsigned long irq_flags;
461 write_lock_irqsave(&rdev->fence_lock, irq_flags);
462 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
463 radeon_fence_driver_init_ring(rdev, ring);
465 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
466 if (radeon_debugfs_fence_init(rdev)) {
467 dev_err(rdev->dev, "fence debugfs file creation failed\n");
472 void radeon_fence_driver_fini(struct radeon_device *rdev)
474 unsigned long irq_flags;
477 for (ring = 0; ring < RADEON_NUM_RINGS; ring++) {
478 if (!rdev->fence_drv[ring].initialized)
480 radeon_fence_wait_empty(rdev, ring);
481 wake_up_all(&rdev->fence_drv[ring].queue);
482 write_lock_irqsave(&rdev->fence_lock, irq_flags);
483 radeon_scratch_free(rdev, rdev->fence_drv[ring].scratch_reg);
484 write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
485 rdev->fence_drv[ring].initialized = false;
493 #if defined(CONFIG_DEBUG_FS)
494 static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
496 struct drm_info_node *node = (struct drm_info_node *)m->private;
497 struct drm_device *dev = node->minor->dev;
498 struct radeon_device *rdev = dev->dev_private;
499 struct radeon_fence *fence;
502 for (i = 0; i < RADEON_NUM_RINGS; ++i) {
503 if (!rdev->fence_drv[i].initialized)
506 seq_printf(m, "--- ring %d ---\n", i);
507 seq_printf(m, "Last signaled fence 0x%016lx\n",
508 atomic64_read(&rdev->fence_drv[i].last_seq));
509 if (!list_empty(&rdev->fence_drv[i].emitted)) {
510 fence = list_entry(rdev->fence_drv[i].emitted.prev,
511 struct radeon_fence, list);
512 seq_printf(m, "Last emitted fence %p with 0x%016llx\n",
519 static struct drm_info_list radeon_debugfs_fence_list[] = {
520 {"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
524 int radeon_debugfs_fence_init(struct radeon_device *rdev)
526 #if defined(CONFIG_DEBUG_FS)
527 return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 1);