blob: 507d6b747a13139cf3aeeb8e0a4f8339a950eaab [file] [log] [blame]
Dave Airlie94bb5982006-12-19 17:49:08 +11001/* radeon_irq.c -- IRQ handling for radeon -*- linux-c -*- */
2/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) The Weather Channel, Inc. 2002. All Rights Reserved.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * The Weather Channel (TM) funded Tungsten Graphics to develop the
6 * initial release of the Radeon 8500 driver under the XFree86 license.
7 * This notice must be preserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 * Keith Whitwell <keith@tungstengraphics.com>
Jan Engelhardt96de0e22007-10-19 23:21:04 +020030 * Michel Dänzer <michel@daenzer.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 */
32
33#include "drmP.h"
34#include "drm.h"
35#include "radeon_drm.h"
36#include "radeon_drv.h"
37
Jesse Barnesac741ab2008-04-22 16:03:07 +100038static void radeon_irq_set_state(struct drm_device *dev, u32 mask, int state)
Dave Airlie6921e332005-06-26 21:05:59 +100039{
Jesse Barnesac741ab2008-04-22 16:03:07 +100040 drm_radeon_private_t *dev_priv = dev->dev_private;
41
42 if (state)
43 dev_priv->irq_enable_reg |= mask;
44 else
45 dev_priv->irq_enable_reg &= ~mask;
46
47 RADEON_WRITE(RADEON_GEN_INT_CNTL, dev_priv->irq_enable_reg);
48}
49
50int radeon_enable_vblank(struct drm_device *dev, int crtc)
51{
52 switch (crtc) {
53 case 0:
54 radeon_irq_set_state(dev, RADEON_CRTC_VBLANK_MASK, 1);
55 break;
56 case 1:
57 radeon_irq_set_state(dev, RADEON_CRTC2_VBLANK_MASK, 1);
58 break;
59 default:
60 DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
61 crtc);
62 return EINVAL;
63 }
64
65 return 0;
66}
67
68void radeon_disable_vblank(struct drm_device *dev, int crtc)
69{
70 switch (crtc) {
71 case 0:
72 radeon_irq_set_state(dev, RADEON_CRTC_VBLANK_MASK, 0);
73 break;
74 case 1:
75 radeon_irq_set_state(dev, RADEON_CRTC2_VBLANK_MASK, 0);
76 break;
77 default:
78 DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
79 crtc);
80 break;
81 }
82}
83
84static __inline__ u32 radeon_acknowledge_irqs(drm_radeon_private_t * dev_priv)
85{
86 u32 irqs = RADEON_READ(RADEON_GEN_INT_STATUS) &
87 (RADEON_SW_INT_TEST | RADEON_CRTC_VBLANK_STAT |
88 RADEON_CRTC2_VBLANK_STAT);
89
Dave Airlie6921e332005-06-26 21:05:59 +100090 if (irqs)
91 RADEON_WRITE(RADEON_GEN_INT_STATUS, irqs);
Jesse Barnesac741ab2008-04-22 16:03:07 +100092
Dave Airlie6921e332005-06-26 21:05:59 +100093 return irqs;
94}
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096/* Interrupts - Used for device synchronization and flushing in the
97 * following circumstances:
98 *
99 * - Exclusive FB access with hw idle:
100 * - Wait for GUI Idle (?) interrupt, then do normal flush.
101 *
102 * - Frame throttling, NV_fence:
103 * - Drop marker irq's into command stream ahead of time.
104 * - Wait on irq's with lock *not held*
105 * - Check each for termination condition
106 *
107 * - Internally in cp_getbuffer, etc:
108 * - as above, but wait with lock held???
109 *
110 * NOTE: These functions are misleadingly named -- the irq's aren't
111 * tied to dma at all, this is just a hangover from dri prehistory.
112 */
113
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114irqreturn_t radeon_driver_irq_handler(DRM_IRQ_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000116 struct drm_device *dev = (struct drm_device *) arg;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000117 drm_radeon_private_t *dev_priv =
118 (drm_radeon_private_t *) dev->dev_private;
119 u32 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* Only consider the bits we're interested in - others could be used
122 * outside the DRM
123 */
Jesse Barnesac741ab2008-04-22 16:03:07 +1000124 stat = radeon_acknowledge_irqs(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!stat)
126 return IRQ_NONE;
127
Dave Airlieddbee332007-07-11 12:16:01 +1000128 stat &= dev_priv->irq_enable_reg;
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 /* SW interrupt */
Jesse Barnesac741ab2008-04-22 16:03:07 +1000131 if (stat & RADEON_SW_INT_TEST)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 DRM_WAKEUP(&dev_priv->swi_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 /* VBLANK interrupt */
Jesse Barnesac741ab2008-04-22 16:03:07 +1000135 if (stat & RADEON_CRTC_VBLANK_STAT)
136 drm_handle_vblank(dev, 0);
137 if (stat & RADEON_CRTC2_VBLANK_STAT)
138 drm_handle_vblank(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return IRQ_HANDLED;
141}
142
Dave Airlie84b1fd12007-07-11 15:53:27 +1000143static int radeon_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 drm_radeon_private_t *dev_priv = dev->dev_private;
146 unsigned int ret;
147 RING_LOCALS;
148
149 atomic_inc(&dev_priv->swi_emitted);
150 ret = atomic_read(&dev_priv->swi_emitted);
151
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152 BEGIN_RING(4);
153 OUT_RING_REG(RADEON_LAST_SWI_REG, ret);
154 OUT_RING_REG(RADEON_GEN_INT_STATUS, RADEON_SW_INT_FIRE);
155 ADVANCE_RING();
156 COMMIT_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
158 return ret;
159}
160
Dave Airlie84b1fd12007-07-11 15:53:27 +1000161static int radeon_wait_irq(struct drm_device * dev, int swi_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163 drm_radeon_private_t *dev_priv =
164 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 int ret = 0;
166
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000167 if (RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr)
168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
171
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 DRM_WAIT_ON(ret, dev_priv->swi_queue, 3 * DRM_HZ,
173 RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 return ret;
176}
177
Jesse Barnesac741ab2008-04-22 16:03:07 +1000178u32 radeon_get_vblank_counter(struct drm_device *dev, int crtc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Jesse Barnesac741ab2008-04-22 16:03:07 +1000180 drm_radeon_private_t *dev_priv = dev->dev_private;
181 u32 crtc_cnt_reg, crtc_status_reg;
182
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000184 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000185 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
Jesse Barnesac741ab2008-04-22 16:03:07 +1000188 if (crtc == 0) {
189 crtc_cnt_reg = RADEON_CRTC_CRNT_FRAME;
190 crtc_status_reg = RADEON_CRTC_STATUS;
191 } else if (crtc == 1) {
192 crtc_cnt_reg = RADEON_CRTC2_CRNT_FRAME;
193 crtc_status_reg = RADEON_CRTC2_STATUS;
194 } else {
Eric Anholt20caafa2007-08-25 19:22:43 +1000195 return -EINVAL;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000196 }
Dave Airlieddbee332007-07-11 12:16:01 +1000197
Jesse Barnesac741ab2008-04-22 16:03:07 +1000198 return RADEON_READ(crtc_cnt_reg) + (RADEON_READ(crtc_status_reg) & 1);
Dave Airlieddbee332007-07-11 12:16:01 +1000199}
200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/* Needs the lock as it touches the ring.
202 */
Eric Anholtc153f452007-09-03 12:06:45 +1000203int radeon_irq_emit(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000206 drm_radeon_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 int result;
208
Eric Anholt6c340ea2007-08-25 20:23:09 +1000209 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000211 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000212 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000213 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 result = radeon_emit_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Eric Anholtc153f452007-09-03 12:06:45 +1000218 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000220 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
223 return 0;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* Doesn't need the hardware lock.
227 */
Eric Anholtc153f452007-09-03 12:06:45 +1000228int radeon_irq_wait(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000231 drm_radeon_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000233 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000234 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000235 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 }
237
Eric Anholtc153f452007-09-03 12:06:45 +1000238 return radeon_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/* drm_dma.h hooks
242*/
Dave Airlie84b1fd12007-07-11 15:53:27 +1000243void radeon_driver_irq_preinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000248 /* Disable *all* interrupts */
249 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
251 /* Clear bits if they're already high */
Jesse Barnesac741ab2008-04-22 16:03:07 +1000252 radeon_acknowledge_irqs(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Jesse Barnesac741ab2008-04-22 16:03:07 +1000255int radeon_driver_irq_postinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 (drm_radeon_private_t *) dev->dev_private;
Jesse Barnesac741ab2008-04-22 16:03:07 +1000259 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000261 atomic_set(&dev_priv->swi_emitted, 0);
262 DRM_INIT_WAITQUEUE(&dev_priv->swi_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Jesse Barnesac741ab2008-04-22 16:03:07 +1000264 ret = drm_vblank_init(dev, 2);
265 if (ret)
266 return ret;
267
268 dev->max_vblank_count = 0x001fffff;
269
270 radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
271
272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Dave Airlie84b1fd12007-07-11 15:53:27 +1000275void radeon_driver_irq_uninstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (!dev_priv)
280 return;
281
Dave Airlieddbee332007-07-11 12:16:01 +1000282 dev_priv->irq_enabled = 0;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 /* Disable *all* interrupts */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
Dave Airlieddbee332007-07-11 12:16:01 +1000287
288
Dave Airlie84b1fd12007-07-11 15:53:27 +1000289int radeon_vblank_crtc_get(struct drm_device *dev)
Dave Airlieddbee332007-07-11 12:16:01 +1000290{
291 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
292 u32 flag;
293 u32 value;
294
295 flag = RADEON_READ(RADEON_GEN_INT_CNTL);
296 value = 0;
297
298 if (flag & RADEON_CRTC_VBLANK_MASK)
299 value |= DRM_RADEON_VBLANK_CRTC1;
300
301 if (flag & RADEON_CRTC2_VBLANK_MASK)
302 value |= DRM_RADEON_VBLANK_CRTC2;
303 return value;
304}
305
Dave Airlie84b1fd12007-07-11 15:53:27 +1000306int radeon_vblank_crtc_set(struct drm_device *dev, int64_t value)
Dave Airlieddbee332007-07-11 12:16:01 +1000307{
308 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
309 if (value & ~(DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) {
310 DRM_ERROR("called with invalid crtc 0x%x\n", (unsigned int)value);
Eric Anholt20caafa2007-08-25 19:22:43 +1000311 return -EINVAL;
Dave Airlieddbee332007-07-11 12:16:01 +1000312 }
313 dev_priv->vblank_crtc = (unsigned int)value;
Dave Airlieddbee332007-07-11 12:16:01 +1000314 return 0;
315}