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 <asm/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 int radeon_fence_emit(struct radeon_device
*rdev
, struct radeon_fence
*fence
)
45 unsigned long irq_flags
;
47 write_lock_irqsave(&rdev
->fence_drv
.lock
, irq_flags
);
49 write_unlock_irqrestore(&rdev
->fence_drv
.lock
, irq_flags
);
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
57 WREG32(rdev
->fence_drv
.scratch_reg
, fence
->seq
);
59 radeon_fence_ring_emit(rdev
, fence
);
61 trace_radeon_fence_emit(rdev
->ddev
, fence
->seq
);
63 list_move_tail(&fence
->list
, &rdev
->fence_drv
.emited
);
64 write_unlock_irqrestore(&rdev
->fence_drv
.lock
, irq_flags
);
68 static bool radeon_fence_poll_locked(struct radeon_device
*rdev
)
70 struct radeon_fence
*fence
;
71 struct list_head
*i
, *n
;
74 unsigned long cjiffies
;
76 if (rdev
->wb
.enabled
) {
78 if (rdev
->wb
.use_event
)
79 scratch_index
= R600_WB_EVENT_OFFSET
+ rdev
->fence_drv
.scratch_reg
- rdev
->scratch
.reg_base
;
81 scratch_index
= RADEON_WB_SCRATCH_OFFSET
+ rdev
->fence_drv
.scratch_reg
- rdev
->scratch
.reg_base
;
82 seq
= rdev
->wb
.wb
[scratch_index
/4];
84 seq
= RREG32(rdev
->fence_drv
.scratch_reg
);
85 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
;
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
;
97 /* the 500ms timeout is elapsed we should test
100 rdev
->fence_drv
.last_timeout
= 1;
103 /* wrap around update last jiffies, we will just wait
106 rdev
->fence_drv
.last_jiffies
= cjiffies
;
111 list_for_each(i
, &rdev
->fence_drv
.emited
) {
112 fence
= list_entry(i
, struct radeon_fence
, list
);
113 if (fence
->seq
== seq
) {
118 /* all fence previous to this one are considered as signaled */
123 list_move_tail(i
, &rdev
->fence_drv
.signaled
);
124 fence
= list_entry(i
, struct radeon_fence
, list
);
125 fence
->signaled
= true;
127 } while (i
!= &rdev
->fence_drv
.emited
);
133 static void radeon_fence_destroy(struct kref
*kref
)
135 unsigned long irq_flags
;
136 struct radeon_fence
*fence
;
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
);
146 int radeon_fence_create(struct radeon_device
*rdev
, struct radeon_fence
**fence
)
148 unsigned long irq_flags
;
150 *fence
= kmalloc(sizeof(struct radeon_fence
), GFP_KERNEL
);
151 if ((*fence
) == NULL
) {
154 kref_init(&((*fence
)->kref
));
155 (*fence
)->rdev
= rdev
;
156 (*fence
)->emited
= false;
157 (*fence
)->signaled
= false;
159 INIT_LIST_HEAD(&(*fence
)->list
);
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
);
168 bool radeon_fence_signaled(struct radeon_fence
*fence
)
170 unsigned long irq_flags
;
171 bool signaled
= false;
176 if (fence
->rdev
->gpu_lockup
)
179 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
) {
185 if (!fence
->emited
) {
186 WARN(1, "Querying an unemited fence : %p !\n", fence
);
190 radeon_fence_poll_locked(fence
->rdev
);
191 signaled
= fence
->signaled
;
193 write_unlock_irqrestore(&fence
->rdev
->fence_drv
.lock
, irq_flags
);
197 int radeon_fence_wait(struct radeon_fence
*fence
, bool intr
)
199 struct radeon_device
*rdev
;
200 unsigned long irq_flags
, timeout
;
205 WARN(1, "Querying an invalid fence : %p !\n", fence
);
209 if (radeon_fence_signaled(fence
)) {
212 timeout
= rdev
->fence_drv
.last_timeout
;
214 /* save current sequence used to check for GPU lockup */
215 seq
= rdev
->fence_drv
.last_seq
;
216 trace_radeon_fence_wait_begin(rdev
->ddev
, seq
);
218 radeon_irq_kms_sw_irq_get(rdev
);
219 r
= wait_event_interruptible_timeout(rdev
->fence_drv
.queue
,
220 radeon_fence_signaled(fence
), timeout
);
221 radeon_irq_kms_sw_irq_put(rdev
);
222 if (unlikely(r
< 0)) {
226 radeon_irq_kms_sw_irq_get(rdev
);
227 r
= wait_event_timeout(rdev
->fence_drv
.queue
,
228 radeon_fence_signaled(fence
), timeout
);
229 radeon_irq_kms_sw_irq_put(rdev
);
231 trace_radeon_fence_wait_end(rdev
->ddev
, seq
);
232 if (unlikely(!radeon_fence_signaled(fence
))) {
233 /* we were interrupted for some reason and fence isn't
234 * isn't signaled yet, resume wait
240 /* don't protect read access to rdev->fence_drv.last_seq
241 * if we experiencing a lockup the value doesn't change
243 if (seq
== rdev
->fence_drv
.last_seq
&& radeon_gpu_is_lockup(rdev
)) {
244 /* good news we believe it's a lockup */
245 WARN(1, "GPU lockup (waiting for 0x%08X last fence id 0x%08X)\n",
247 /* FIXME: what should we do ? marking everyone
248 * as signaled for now
250 rdev
->gpu_lockup
= true;
251 r
= radeon_gpu_reset(rdev
);
254 WREG32(rdev
->fence_drv
.scratch_reg
, fence
->seq
);
255 rdev
->gpu_lockup
= false;
257 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
);
267 int radeon_fence_wait_next(struct radeon_device
*rdev
)
269 unsigned long irq_flags
;
270 struct radeon_fence
*fence
;
273 if (rdev
->gpu_lockup
) {
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
);
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
);
290 int radeon_fence_wait_last(struct radeon_device
*rdev
)
292 unsigned long irq_flags
;
293 struct radeon_fence
*fence
;
296 if (rdev
->gpu_lockup
) {
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
);
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
);
313 struct radeon_fence
*radeon_fence_ref(struct radeon_fence
*fence
)
315 kref_get(&fence
->kref
);
319 void radeon_fence_unref(struct radeon_fence
**fence
)
321 struct radeon_fence
*tmp
= *fence
;
325 kref_put(&tmp
->kref
, radeon_fence_destroy
);
329 void radeon_fence_process(struct radeon_device
*rdev
)
331 unsigned long irq_flags
;
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
);
338 wake_up_all(&rdev
->fence_drv
.queue
);
342 int radeon_fence_driver_init(struct radeon_device
*rdev
)
344 unsigned long irq_flags
;
347 write_lock_irqsave(&rdev
->fence_drv
.lock
, irq_flags
);
348 r
= radeon_scratch_get(rdev
, &rdev
->fence_drv
.scratch_reg
);
350 dev_err(rdev
->dev
, "fence failed to get scratch register\n");
351 write_unlock_irqrestore(&rdev
->fence_drv
.lock
, irq_flags
);
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
);
359 init_waitqueue_head(&rdev
->fence_drv
.queue
);
360 rdev
->fence_drv
.initialized
= true;
361 write_unlock_irqrestore(&rdev
->fence_drv
.lock
, irq_flags
);
362 if (radeon_debugfs_fence_init(rdev
)) {
363 dev_err(rdev
->dev
, "fence debugfs file creation failed\n");
368 void radeon_fence_driver_fini(struct radeon_device
*rdev
)
370 unsigned long irq_flags
;
372 if (!rdev
->fence_drv
.initialized
)
374 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
);
378 rdev
->fence_drv
.initialized
= false;
385 #if defined(CONFIG_DEBUG_FS)
386 static int radeon_debugfs_fence_info(struct seq_file
*m
, void *data
)
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
;
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",
404 static struct drm_info_list radeon_debugfs_fence_list
[] = {
405 {"radeon_fence_info", &radeon_debugfs_fence_info
, 0, NULL
},
409 int radeon_debugfs_fence_init(struct radeon_device
*rdev
)
411 #if defined(CONFIG_DEBUG_FS)
412 return radeon_debugfs_add_files(rdev
, radeon_debugfs_fence_list
, 1);