blob: 1f822943657017b3684d6204ae19c76abd95ae6a [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
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 <asm/atomic.h>
33#include <linux/wait.h>
34#include <linux/list.h>
35#include <linux/kref.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020037#include "drmP.h"
38#include "drm.h"
39#include "radeon_reg.h"
40#include "radeon.h"
Dave Airlie99ee7fa2010-11-23 11:47:49 +100041#include "radeon_trace.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020042
43int radeon_fence_emit(struct radeon_device *rdev, struct radeon_fence *fence)
44{
45 unsigned long irq_flags;
46
47 write_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
48 if (fence->emited) {
49 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
50 return 0;
51 }
52 fence->seq = atomic_add_return(1, &rdev->fence_drv.seq);
53 if (!rdev->cp.ready) {
54 /* FIXME: cp is not running assume everythings is done right
55 * away
56 */
57 WREG32(rdev->fence_drv.scratch_reg, fence->seq);
Jerome Glisse3ce0a232009-09-08 10:10:24 +100058 } else
Jerome Glisse771fe6b2009-06-05 14:42:42 +020059 radeon_fence_ring_emit(rdev, fence);
Jerome Glisse3ce0a232009-09-08 10:10:24 +100060
Dave Airlie99ee7fa2010-11-23 11:47:49 +100061 trace_radeon_fence_emit(rdev->ddev, fence->seq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020062 fence->emited = true;
Nicolas Kaiser3409fc12011-03-16 17:08:04 +010063 list_move_tail(&fence->list, &rdev->fence_drv.emited);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020064 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
65 return 0;
66}
67
68static bool radeon_fence_poll_locked(struct radeon_device *rdev)
69{
70 struct radeon_fence *fence;
71 struct list_head *i, *n;
72 uint32_t seq;
73 bool wake = false;
Jerome Glisse225758d2010-03-09 14:45:10 +000074 unsigned long cjiffies;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020075
Alex Deucher724c80e2010-08-27 18:25:25 -040076 if (rdev->wb.enabled) {
Alex Deucherd0f8a852010-09-04 05:04:34 -040077 u32 scratch_index;
78 if (rdev->wb.use_event)
79 scratch_index = R600_WB_EVENT_OFFSET + rdev->fence_drv.scratch_reg - rdev->scratch.reg_base;
80 else
81 scratch_index = RADEON_WB_SCRATCH_OFFSET + rdev->fence_drv.scratch_reg - rdev->scratch.reg_base;
Michel Dänzerdc66b322011-04-07 16:17:47 +020082 seq = le32_to_cpu(rdev->wb.wb[scratch_index/4]);
Alex Deucher724c80e2010-08-27 18:25:25 -040083 } else
84 seq = RREG32(rdev->fence_drv.scratch_reg);
Jerome Glisse225758d2010-03-09 14:45:10 +000085 if (seq != rdev->fence_drv.last_seq) {
86 rdev->fence_drv.last_seq = seq;
87 rdev->fence_drv.last_jiffies = jiffies;
88 rdev->fence_drv.last_timeout = RADEON_FENCE_JIFFIES_TIMEOUT;
89 } else {
90 cjiffies = jiffies;
91 if (time_after(cjiffies, rdev->fence_drv.last_jiffies)) {
92 cjiffies -= rdev->fence_drv.last_jiffies;
93 if (time_after(rdev->fence_drv.last_timeout, cjiffies)) {
94 /* update the timeout */
95 rdev->fence_drv.last_timeout -= cjiffies;
96 } else {
97 /* the 500ms timeout is elapsed we should test
98 * for GPU lockup
99 */
100 rdev->fence_drv.last_timeout = 1;
101 }
102 } else {
103 /* wrap around update last jiffies, we will just wait
104 * a little longer
105 */
106 rdev->fence_drv.last_jiffies = cjiffies;
107 }
108 return false;
109 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200110 n = NULL;
111 list_for_each(i, &rdev->fence_drv.emited) {
112 fence = list_entry(i, struct radeon_fence, list);
113 if (fence->seq == seq) {
114 n = i;
115 break;
116 }
117 }
118 /* all fence previous to this one are considered as signaled */
119 if (n) {
120 i = n;
121 do {
122 n = i->prev;
Nicolas Kaiser3409fc12011-03-16 17:08:04 +0100123 list_move_tail(i, &rdev->fence_drv.signaled);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200124 fence = list_entry(i, struct radeon_fence, list);
125 fence->signaled = true;
126 i = n;
127 } while (i != &rdev->fence_drv.emited);
128 wake = true;
129 }
130 return wake;
131}
132
133static void radeon_fence_destroy(struct kref *kref)
134{
135 unsigned long irq_flags;
136 struct radeon_fence *fence;
137
138 fence = container_of(kref, struct radeon_fence, kref);
139 write_lock_irqsave(&fence->rdev->fence_drv.lock, irq_flags);
140 list_del(&fence->list);
141 fence->emited = false;
142 write_unlock_irqrestore(&fence->rdev->fence_drv.lock, irq_flags);
143 kfree(fence);
144}
145
146int radeon_fence_create(struct radeon_device *rdev, struct radeon_fence **fence)
147{
148 unsigned long irq_flags;
149
150 *fence = kmalloc(sizeof(struct radeon_fence), GFP_KERNEL);
151 if ((*fence) == NULL) {
152 return -ENOMEM;
153 }
154 kref_init(&((*fence)->kref));
155 (*fence)->rdev = rdev;
156 (*fence)->emited = false;
157 (*fence)->signaled = false;
158 (*fence)->seq = 0;
159 INIT_LIST_HEAD(&(*fence)->list);
160
161 write_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
162 list_add_tail(&(*fence)->list, &rdev->fence_drv.created);
163 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
164 return 0;
165}
166
167
168bool radeon_fence_signaled(struct radeon_fence *fence)
169{
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200170 unsigned long irq_flags;
171 bool signaled = false;
172
Darren Jenkins3655d542009-12-30 12:20:05 +1100173 if (!fence)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200174 return true;
Darren Jenkins3655d542009-12-30 12:20:05 +1100175
176 if (fence->rdev->gpu_lockup)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200177 return true;
Darren Jenkins3655d542009-12-30 12:20:05 +1100178
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200179 write_lock_irqsave(&fence->rdev->fence_drv.lock, irq_flags);
180 signaled = fence->signaled;
181 /* if we are shuting down report all fence as signaled */
182 if (fence->rdev->shutdown) {
183 signaled = true;
184 }
185 if (!fence->emited) {
186 WARN(1, "Querying an unemited fence : %p !\n", fence);
187 signaled = true;
188 }
189 if (!signaled) {
190 radeon_fence_poll_locked(fence->rdev);
191 signaled = fence->signaled;
192 }
193 write_unlock_irqrestore(&fence->rdev->fence_drv.lock, irq_flags);
194 return signaled;
195}
196
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000197int radeon_fence_wait(struct radeon_fence *fence, bool intr)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200198{
199 struct radeon_device *rdev;
Jerome Glisse225758d2010-03-09 14:45:10 +0000200 unsigned long irq_flags, timeout;
201 u32 seq;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200202 int r;
203
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200204 if (fence == NULL) {
205 WARN(1, "Querying an invalid fence : %p !\n", fence);
206 return 0;
207 }
208 rdev = fence->rdev;
209 if (radeon_fence_signaled(fence)) {
210 return 0;
211 }
Jerome Glisse225758d2010-03-09 14:45:10 +0000212 timeout = rdev->fence_drv.last_timeout;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200213retry:
Jerome Glisse225758d2010-03-09 14:45:10 +0000214 /* save current sequence used to check for GPU lockup */
215 seq = rdev->fence_drv.last_seq;
Dave Airlie99ee7fa2010-11-23 11:47:49 +1000216 trace_radeon_fence_wait_begin(rdev->ddev, seq);
Jerome Glisse3ce0a232009-09-08 10:10:24 +1000217 if (intr) {
Dave Airlie1614f8b2009-12-01 16:04:56 +1000218 radeon_irq_kms_sw_irq_get(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200219 r = wait_event_interruptible_timeout(rdev->fence_drv.queue,
220 radeon_fence_signaled(fence), timeout);
Dave Airlie1614f8b2009-12-01 16:04:56 +1000221 radeon_irq_kms_sw_irq_put(rdev);
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000222 if (unlikely(r < 0)) {
Thomas Hellstrom5cc6fba2009-12-07 18:36:19 +0100223 return r;
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000224 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200225 } else {
Dave Airlie1614f8b2009-12-01 16:04:56 +1000226 radeon_irq_kms_sw_irq_get(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200227 r = wait_event_timeout(rdev->fence_drv.queue,
228 radeon_fence_signaled(fence), timeout);
Dave Airlie1614f8b2009-12-01 16:04:56 +1000229 radeon_irq_kms_sw_irq_put(rdev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200230 }
Dave Airlie99ee7fa2010-11-23 11:47:49 +1000231 trace_radeon_fence_wait_end(rdev->ddev, seq);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200232 if (unlikely(!radeon_fence_signaled(fence))) {
Jerome Glisse225758d2010-03-09 14:45:10 +0000233 /* we were interrupted for some reason and fence isn't
234 * isn't signaled yet, resume wait
235 */
236 if (r) {
237 timeout = r;
238 goto retry;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200239 }
Jerome Glisse225758d2010-03-09 14:45:10 +0000240 /* don't protect read access to rdev->fence_drv.last_seq
241 * if we experiencing a lockup the value doesn't change
242 */
243 if (seq == rdev->fence_drv.last_seq && radeon_gpu_is_lockup(rdev)) {
244 /* good news we believe it's a lockup */
Joe Perchesfce7d612010-10-30 21:08:30 +0000245 WARN(1, "GPU lockup (waiting for 0x%08X last fence id 0x%08X)\n",
246 fence->seq, seq);
Jerome Glisse225758d2010-03-09 14:45:10 +0000247 /* FIXME: what should we do ? marking everyone
248 * as signaled for now
249 */
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000250 rdev->gpu_lockup = true;
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000251 r = radeon_gpu_reset(rdev);
252 if (r)
253 return r;
Jerome Glissea1e9ada2010-04-26 22:23:42 +0200254 WREG32(rdev->fence_drv.scratch_reg, fence->seq);
Jerome Glisse90aca4d2010-03-09 14:45:12 +0000255 rdev->gpu_lockup = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200256 }
Jerome Glisse225758d2010-03-09 14:45:10 +0000257 timeout = RADEON_FENCE_JIFFIES_TIMEOUT;
258 write_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
259 rdev->fence_drv.last_timeout = RADEON_FENCE_JIFFIES_TIMEOUT;
260 rdev->fence_drv.last_jiffies = jiffies;
261 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200262 goto retry;
263 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200264 return 0;
265}
266
267int radeon_fence_wait_next(struct radeon_device *rdev)
268{
269 unsigned long irq_flags;
270 struct radeon_fence *fence;
271 int r;
272
273 if (rdev->gpu_lockup) {
274 return 0;
275 }
276 write_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
277 if (list_empty(&rdev->fence_drv.emited)) {
278 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
279 return 0;
280 }
281 fence = list_entry(rdev->fence_drv.emited.next,
282 struct radeon_fence, list);
283 radeon_fence_ref(fence);
284 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
285 r = radeon_fence_wait(fence, false);
286 radeon_fence_unref(&fence);
287 return r;
288}
289
290int radeon_fence_wait_last(struct radeon_device *rdev)
291{
292 unsigned long irq_flags;
293 struct radeon_fence *fence;
294 int r;
295
296 if (rdev->gpu_lockup) {
297 return 0;
298 }
299 write_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
300 if (list_empty(&rdev->fence_drv.emited)) {
301 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
302 return 0;
303 }
304 fence = list_entry(rdev->fence_drv.emited.prev,
305 struct radeon_fence, list);
306 radeon_fence_ref(fence);
307 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
308 r = radeon_fence_wait(fence, false);
309 radeon_fence_unref(&fence);
310 return r;
311}
312
313struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
314{
315 kref_get(&fence->kref);
316 return fence;
317}
318
319void radeon_fence_unref(struct radeon_fence **fence)
320{
321 struct radeon_fence *tmp = *fence;
322
323 *fence = NULL;
324 if (tmp) {
Paul Bollecdb650a2011-02-27 01:34:08 +0100325 kref_put(&tmp->kref, radeon_fence_destroy);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200326 }
327}
328
329void radeon_fence_process(struct radeon_device *rdev)
330{
331 unsigned long irq_flags;
332 bool wake;
333
334 write_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
335 wake = radeon_fence_poll_locked(rdev);
336 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
337 if (wake) {
338 wake_up_all(&rdev->fence_drv.queue);
339 }
340}
341
342int radeon_fence_driver_init(struct radeon_device *rdev)
343{
344 unsigned long irq_flags;
345 int r;
346
347 write_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
348 r = radeon_scratch_get(rdev, &rdev->fence_drv.scratch_reg);
349 if (r) {
Jerome Glisse0a0c7592009-12-11 20:36:19 +0100350 dev_err(rdev->dev, "fence failed to get scratch register\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
352 return r;
353 }
354 WREG32(rdev->fence_drv.scratch_reg, 0);
355 atomic_set(&rdev->fence_drv.seq, 0);
356 INIT_LIST_HEAD(&rdev->fence_drv.created);
357 INIT_LIST_HEAD(&rdev->fence_drv.emited);
358 INIT_LIST_HEAD(&rdev->fence_drv.signaled);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200359 init_waitqueue_head(&rdev->fence_drv.queue);
Jerome Glisse0a0c7592009-12-11 20:36:19 +0100360 rdev->fence_drv.initialized = true;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200361 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
362 if (radeon_debugfs_fence_init(rdev)) {
Jerome Glisse0a0c7592009-12-11 20:36:19 +0100363 dev_err(rdev->dev, "fence debugfs file creation failed\n");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200364 }
365 return 0;
366}
367
368void radeon_fence_driver_fini(struct radeon_device *rdev)
369{
370 unsigned long irq_flags;
371
Jerome Glisse0a0c7592009-12-11 20:36:19 +0100372 if (!rdev->fence_drv.initialized)
373 return;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200374 wake_up_all(&rdev->fence_drv.queue);
375 write_lock_irqsave(&rdev->fence_drv.lock, irq_flags);
376 radeon_scratch_free(rdev, rdev->fence_drv.scratch_reg);
377 write_unlock_irqrestore(&rdev->fence_drv.lock, irq_flags);
Jerome Glisse0a0c7592009-12-11 20:36:19 +0100378 rdev->fence_drv.initialized = false;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200379}
380
381
382/*
383 * Fence debugfs
384 */
385#if defined(CONFIG_DEBUG_FS)
386static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
387{
388 struct drm_info_node *node = (struct drm_info_node *)m->private;
389 struct drm_device *dev = node->minor->dev;
390 struct radeon_device *rdev = dev->dev_private;
391 struct radeon_fence *fence;
392
393 seq_printf(m, "Last signaled fence 0x%08X\n",
394 RREG32(rdev->fence_drv.scratch_reg));
395 if (!list_empty(&rdev->fence_drv.emited)) {
396 fence = list_entry(rdev->fence_drv.emited.prev,
397 struct radeon_fence, list);
398 seq_printf(m, "Last emited fence %p with 0x%08X\n",
399 fence, fence->seq);
400 }
401 return 0;
402}
403
404static struct drm_info_list radeon_debugfs_fence_list[] = {
405 {"radeon_fence_info", &radeon_debugfs_fence_info, 0, NULL},
406};
407#endif
408
409int radeon_debugfs_fence_init(struct radeon_device *rdev)
410{
411#if defined(CONFIG_DEBUG_FS)
412 return radeon_debugfs_add_files(rdev, radeon_debugfs_fence_list, 1);
413#else
414 return 0;
415#endif
416}