blob: 5079f7054a2f67aa712bcf6c89fa4e98cb1835b9 [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>
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070030 * Michel D�zer <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 Barnes0a3e67a2008-09-30 12:14:26 -070038void radeon_irq_set_state(struct drm_device *dev, u32 mask, int state)
Dave Airlie6921e332005-06-26 21:05:59 +100039{
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070040 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
50static void r500_vbl_irq_set_state(struct drm_device *dev, u32 mask, int state)
51{
52 drm_radeon_private_t *dev_priv = dev->dev_private;
53
54 if (state)
55 dev_priv->r500_disp_irq_reg |= mask;
56 else
57 dev_priv->r500_disp_irq_reg &= ~mask;
58
59 RADEON_WRITE(R500_DxMODE_INT_MASK, dev_priv->r500_disp_irq_reg);
60}
61
62int radeon_enable_vblank(struct drm_device *dev, int crtc)
63{
64 drm_radeon_private_t *dev_priv = dev->dev_private;
65
66 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS690) {
67 switch (crtc) {
68 case 0:
69 r500_vbl_irq_set_state(dev, R500_D1MODE_INT_MASK, 1);
70 break;
71 case 1:
72 r500_vbl_irq_set_state(dev, R500_D2MODE_INT_MASK, 1);
73 break;
74 default:
75 DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
76 crtc);
77 return EINVAL;
78 }
79 } else {
80 switch (crtc) {
81 case 0:
82 radeon_irq_set_state(dev, RADEON_CRTC_VBLANK_MASK, 1);
83 break;
84 case 1:
85 radeon_irq_set_state(dev, RADEON_CRTC2_VBLANK_MASK, 1);
86 break;
87 default:
88 DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
89 crtc);
90 return EINVAL;
91 }
92 }
93
94 return 0;
95}
96
97void radeon_disable_vblank(struct drm_device *dev, int crtc)
98{
99 drm_radeon_private_t *dev_priv = dev->dev_private;
100
101 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS690) {
102 switch (crtc) {
103 case 0:
104 r500_vbl_irq_set_state(dev, R500_D1MODE_INT_MASK, 0);
105 break;
106 case 1:
107 r500_vbl_irq_set_state(dev, R500_D2MODE_INT_MASK, 0);
108 break;
109 default:
110 DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
111 crtc);
112 break;
113 }
114 } else {
115 switch (crtc) {
116 case 0:
117 radeon_irq_set_state(dev, RADEON_CRTC_VBLANK_MASK, 0);
118 break;
119 case 1:
120 radeon_irq_set_state(dev, RADEON_CRTC2_VBLANK_MASK, 0);
121 break;
122 default:
123 DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
124 crtc);
125 break;
126 }
127 }
128}
129
130static inline u32 radeon_acknowledge_irqs(drm_radeon_private_t *dev_priv, u32 *r500_disp_int)
131{
132 u32 irqs = RADEON_READ(RADEON_GEN_INT_STATUS);
133 u32 irq_mask = RADEON_SW_INT_TEST;
134
135 *r500_disp_int = 0;
136 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS690) {
137 /* vbl interrupts in a different place */
138
139 if (irqs & R500_DISPLAY_INT_STATUS) {
140 /* if a display interrupt */
141 u32 disp_irq;
142
143 disp_irq = RADEON_READ(R500_DISP_INTERRUPT_STATUS);
144
145 *r500_disp_int = disp_irq;
146 if (disp_irq & R500_D1_VBLANK_INTERRUPT)
147 RADEON_WRITE(R500_D1MODE_VBLANK_STATUS, R500_VBLANK_ACK);
148 if (disp_irq & R500_D2_VBLANK_INTERRUPT)
149 RADEON_WRITE(R500_D2MODE_VBLANK_STATUS, R500_VBLANK_ACK);
150 }
151 irq_mask |= R500_DISPLAY_INT_STATUS;
152 } else
153 irq_mask |= RADEON_CRTC_VBLANK_STAT | RADEON_CRTC2_VBLANK_STAT;
154
155 irqs &= irq_mask;
156
Dave Airlie6921e332005-06-26 21:05:59 +1000157 if (irqs)
158 RADEON_WRITE(RADEON_GEN_INT_STATUS, irqs);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700159
Dave Airlie6921e332005-06-26 21:05:59 +1000160 return irqs;
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/* Interrupts - Used for device synchronization and flushing in the
164 * following circumstances:
165 *
166 * - Exclusive FB access with hw idle:
167 * - Wait for GUI Idle (?) interrupt, then do normal flush.
168 *
169 * - Frame throttling, NV_fence:
170 * - Drop marker irq's into command stream ahead of time.
171 * - Wait on irq's with lock *not held*
172 * - Check each for termination condition
173 *
174 * - Internally in cp_getbuffer, etc:
175 * - as above, but wait with lock held???
176 *
177 * NOTE: These functions are misleadingly named -- the irq's aren't
178 * tied to dma at all, this is just a hangover from dri prehistory.
179 */
180
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000181irqreturn_t radeon_driver_irq_handler(DRM_IRQ_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000183 struct drm_device *dev = (struct drm_device *) arg;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000184 drm_radeon_private_t *dev_priv =
185 (drm_radeon_private_t *) dev->dev_private;
186 u32 stat;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700187 u32 r500_disp_int;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* Only consider the bits we're interested in - others could be used
190 * outside the DRM
191 */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700192 stat = radeon_acknowledge_irqs(dev_priv, &r500_disp_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (!stat)
194 return IRQ_NONE;
195
Dave Airlieddbee332007-07-11 12:16:01 +1000196 stat &= dev_priv->irq_enable_reg;
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* SW interrupt */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700199 if (stat & RADEON_SW_INT_TEST)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000200 DRM_WAKEUP(&dev_priv->swi_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /* VBLANK interrupt */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700203 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS690) {
204 if (r500_disp_int & R500_D1_VBLANK_INTERRUPT)
205 drm_handle_vblank(dev, 0);
206 if (r500_disp_int & R500_D2_VBLANK_INTERRUPT)
207 drm_handle_vblank(dev, 1);
208 } else {
209 if (stat & RADEON_CRTC_VBLANK_STAT)
210 drm_handle_vblank(dev, 0);
211 if (stat & RADEON_CRTC2_VBLANK_STAT)
212 drm_handle_vblank(dev, 1);
Dave Airlieaf6061a2008-05-07 12:15:39 +1000213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return IRQ_HANDLED;
215}
216
Dave Airlie84b1fd12007-07-11 15:53:27 +1000217static int radeon_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
219 drm_radeon_private_t *dev_priv = dev->dev_private;
220 unsigned int ret;
221 RING_LOCALS;
222
223 atomic_inc(&dev_priv->swi_emitted);
224 ret = atomic_read(&dev_priv->swi_emitted);
225
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 BEGIN_RING(4);
227 OUT_RING_REG(RADEON_LAST_SWI_REG, ret);
228 OUT_RING_REG(RADEON_GEN_INT_STATUS, RADEON_SW_INT_FIRE);
229 ADVANCE_RING();
230 COMMIT_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 return ret;
233}
234
Dave Airlie84b1fd12007-07-11 15:53:27 +1000235static int radeon_wait_irq(struct drm_device * dev, int swi_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 drm_radeon_private_t *dev_priv =
238 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 int ret = 0;
240
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 if (RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr)
242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
245
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 DRM_WAIT_ON(ret, dev_priv->swi_queue, 3 * DRM_HZ,
247 RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 return ret;
250}
251
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700252u32 radeon_get_vblank_counter(struct drm_device *dev, int crtc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700254 drm_radeon_private_t *dev_priv = dev->dev_private;
255
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000257 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000258 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700261 if (crtc < 0 || crtc > 1) {
262 DRM_ERROR("Invalid crtc %d\n", crtc);
Eric Anholt20caafa2007-08-25 19:22:43 +1000263 return -EINVAL;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700264 }
Dave Airlieddbee332007-07-11 12:16:01 +1000265
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700266 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS690) {
267 if (crtc == 0)
268 return RADEON_READ(R500_D1CRTC_FRAME_COUNT);
269 else
270 return RADEON_READ(R500_D2CRTC_FRAME_COUNT);
271 } else {
272 if (crtc == 0)
273 return RADEON_READ(RADEON_CRTC_CRNT_FRAME);
274 else
275 return RADEON_READ(RADEON_CRTC2_CRNT_FRAME);
276 }
Dave Airlieddbee332007-07-11 12:16:01 +1000277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279/* Needs the lock as it touches the ring.
280 */
Eric Anholtc153f452007-09-03 12:06:45 +1000281int radeon_irq_emit(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000284 drm_radeon_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 int result;
286
Eric Anholt6c340ea2007-08-25 20:23:09 +1000287 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000290 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000291 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 }
293
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294 result = radeon_emit_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Eric Anholtc153f452007-09-03 12:06:45 +1000296 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000298 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 return 0;
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/* Doesn't need the hardware lock.
305 */
Eric Anholtc153f452007-09-03 12:06:45 +1000306int radeon_irq_wait(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000309 drm_radeon_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000311 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000312 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000313 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 }
315
Eric Anholtc153f452007-09-03 12:06:45 +1000316 return radeon_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319/* drm_dma.h hooks
320*/
Dave Airlie84b1fd12007-07-11 15:53:27 +1000321void radeon_driver_irq_preinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 (drm_radeon_private_t *) dev->dev_private;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700325 u32 dummy;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 /* Disable *all* interrupts */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700328 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS690)
329 RADEON_WRITE(R500_DxMODE_INT_MASK, 0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000330 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 /* Clear bits if they're already high */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700333 radeon_acknowledge_irqs(dev_priv, &dummy);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700336int radeon_driver_irq_postinstall(struct drm_device *dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 (drm_radeon_private_t *) dev->dev_private;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700340 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 atomic_set(&dev_priv->swi_emitted, 0);
343 DRM_INIT_WAITQUEUE(&dev_priv->swi_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700345 ret = drm_vblank_init(dev, 2);
346 if (ret)
347 return ret;
348
349 dev->max_vblank_count = 0x001fffff;
350
351 radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
352
353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Dave Airlie84b1fd12007-07-11 15:53:27 +1000356void radeon_driver_irq_uninstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000357{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000359 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 if (!dev_priv)
361 return;
362
Dave Airlieddbee332007-07-11 12:16:01 +1000363 dev_priv->irq_enabled = 0;
364
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700365 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS690)
366 RADEON_WRITE(R500_DxMODE_INT_MASK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 /* Disable *all* interrupts */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000368 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
Dave Airlieddbee332007-07-11 12:16:01 +1000370
371
Dave Airlie84b1fd12007-07-11 15:53:27 +1000372int radeon_vblank_crtc_get(struct drm_device *dev)
Dave Airlieddbee332007-07-11 12:16:01 +1000373{
374 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
Dave Airlieddbee332007-07-11 12:16:01 +1000375
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700376 return dev_priv->vblank_crtc;
Dave Airlieddbee332007-07-11 12:16:01 +1000377}
378
Dave Airlie84b1fd12007-07-11 15:53:27 +1000379int radeon_vblank_crtc_set(struct drm_device *dev, int64_t value)
Dave Airlieddbee332007-07-11 12:16:01 +1000380{
381 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
382 if (value & ~(DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) {
383 DRM_ERROR("called with invalid crtc 0x%x\n", (unsigned int)value);
Eric Anholt20caafa2007-08-25 19:22:43 +1000384 return -EINVAL;
Dave Airlieddbee332007-07-11 12:16:01 +1000385 }
386 dev_priv->vblank_crtc = (unsigned int)value;
Dave Airlieddbee332007-07-11 12:16:01 +1000387 return 0;
388}