blob: 009af3814b6f12ec4dcf34aff23cffff79e0d5db [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
Dave Airlieb5e89ed2005-09-25 14:28:13 +100038static __inline__ u32 radeon_acknowledge_irqs(drm_radeon_private_t * dev_priv,
39 u32 mask)
Dave Airlie6921e332005-06-26 21:05:59 +100040{
41 u32 irqs = RADEON_READ(RADEON_GEN_INT_STATUS) & mask;
42 if (irqs)
43 RADEON_WRITE(RADEON_GEN_INT_STATUS, irqs);
44 return irqs;
45}
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* Interrupts - Used for device synchronization and flushing in the
48 * following circumstances:
49 *
50 * - Exclusive FB access with hw idle:
51 * - Wait for GUI Idle (?) interrupt, then do normal flush.
52 *
53 * - Frame throttling, NV_fence:
54 * - Drop marker irq's into command stream ahead of time.
55 * - Wait on irq's with lock *not held*
56 * - Check each for termination condition
57 *
58 * - Internally in cp_getbuffer, etc:
59 * - as above, but wait with lock held???
60 *
61 * NOTE: These functions are misleadingly named -- the irq's aren't
62 * tied to dma at all, this is just a hangover from dri prehistory.
63 */
64
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065irqreturn_t radeon_driver_irq_handler(DRM_IRQ_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Dave Airlie84b1fd12007-07-11 15:53:27 +100067 struct drm_device *dev = (struct drm_device *) arg;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100068 drm_radeon_private_t *dev_priv =
69 (drm_radeon_private_t *) dev->dev_private;
70 u32 stat;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 /* Only consider the bits we're interested in - others could be used
73 * outside the DRM
74 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100075 stat = radeon_acknowledge_irqs(dev_priv, (RADEON_SW_INT_TEST_ACK |
Dave Airlieddbee332007-07-11 12:16:01 +100076 RADEON_CRTC_VBLANK_STAT |
77 RADEON_CRTC2_VBLANK_STAT));
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 if (!stat)
79 return IRQ_NONE;
80
Dave Airlieddbee332007-07-11 12:16:01 +100081 stat &= dev_priv->irq_enable_reg;
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 /* SW interrupt */
84 if (stat & RADEON_SW_INT_TEST) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085 DRM_WAKEUP(&dev_priv->swi_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 }
87
88 /* VBLANK interrupt */
Dave Airlieddbee332007-07-11 12:16:01 +100089 if (stat & (RADEON_CRTC_VBLANK_STAT|RADEON_CRTC2_VBLANK_STAT)) {
90 int vblank_crtc = dev_priv->vblank_crtc;
91
92 if ((vblank_crtc &
93 (DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) ==
94 (DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) {
95 if (stat & RADEON_CRTC_VBLANK_STAT)
96 atomic_inc(&dev->vbl_received);
97 if (stat & RADEON_CRTC2_VBLANK_STAT)
98 atomic_inc(&dev->vbl_received2);
99 } else if (((stat & RADEON_CRTC_VBLANK_STAT) &&
100 (vblank_crtc & DRM_RADEON_VBLANK_CRTC1)) ||
101 ((stat & RADEON_CRTC2_VBLANK_STAT) &&
102 (vblank_crtc & DRM_RADEON_VBLANK_CRTC2)))
103 atomic_inc(&dev->vbl_received);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 DRM_WAKEUP(&dev->vbl_queue);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000106 drm_vbl_send_signals(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return IRQ_HANDLED;
110}
111
Dave Airlie84b1fd12007-07-11 15:53:27 +1000112static int radeon_emit_irq(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 drm_radeon_private_t *dev_priv = dev->dev_private;
115 unsigned int ret;
116 RING_LOCALS;
117
118 atomic_inc(&dev_priv->swi_emitted);
119 ret = atomic_read(&dev_priv->swi_emitted);
120
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000121 BEGIN_RING(4);
122 OUT_RING_REG(RADEON_LAST_SWI_REG, ret);
123 OUT_RING_REG(RADEON_GEN_INT_STATUS, RADEON_SW_INT_FIRE);
124 ADVANCE_RING();
125 COMMIT_RING();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 return ret;
128}
129
Dave Airlie84b1fd12007-07-11 15:53:27 +1000130static int radeon_wait_irq(struct drm_device * dev, int swi_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 drm_radeon_private_t *dev_priv =
133 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 int ret = 0;
135
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000136 if (RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr)
137 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
140
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 DRM_WAIT_ON(ret, dev_priv->swi_queue, 3 * DRM_HZ,
142 RADEON_READ(RADEON_LAST_SWI_REG) >= swi_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 return ret;
145}
146
Adrian Bunkf75a71f2007-10-16 01:28:46 -0700147static int radeon_driver_vblank_do_wait(struct drm_device * dev,
148 unsigned int *sequence, int crtc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150 drm_radeon_private_t *dev_priv =
151 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 unsigned int cur_vblank;
153 int ret = 0;
Dave Airlieddbee332007-07-11 12:16:01 +1000154 int ack = 0;
155 atomic_t *counter;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000157 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000158 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
Dave Airlieddbee332007-07-11 12:16:01 +1000161 if (crtc == DRM_RADEON_VBLANK_CRTC1) {
162 counter = &dev->vbl_received;
163 ack |= RADEON_CRTC_VBLANK_STAT;
164 } else if (crtc == DRM_RADEON_VBLANK_CRTC2) {
165 counter = &dev->vbl_received2;
166 ack |= RADEON_CRTC2_VBLANK_STAT;
167 } else
Eric Anholt20caafa2007-08-25 19:22:43 +1000168 return -EINVAL;
Dave Airlieddbee332007-07-11 12:16:01 +1000169
170 radeon_acknowledge_irqs(dev_priv, ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
173
174 /* Assume that the user has missed the current sequence number
175 * by about a day rather than she wants to wait for years
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000176 * using vertical blanks...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
Dave Airlieddbee332007-07-11 12:16:01 +1000179 (((cur_vblank = atomic_read(counter))
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180 - *sequence) <= (1 << 23)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 *sequence = cur_vblank;
183
184 return ret;
185}
186
Dave Airlie84b1fd12007-07-11 15:53:27 +1000187int radeon_driver_vblank_wait(struct drm_device *dev, unsigned int *sequence)
Dave Airlieddbee332007-07-11 12:16:01 +1000188{
189 return radeon_driver_vblank_do_wait(dev, sequence, DRM_RADEON_VBLANK_CRTC1);
190}
191
Dave Airlie84b1fd12007-07-11 15:53:27 +1000192int radeon_driver_vblank_wait2(struct drm_device *dev, unsigned int *sequence)
Dave Airlieddbee332007-07-11 12:16:01 +1000193{
194 return radeon_driver_vblank_do_wait(dev, sequence, DRM_RADEON_VBLANK_CRTC2);
195}
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* Needs the lock as it touches the ring.
198 */
Eric Anholtc153f452007-09-03 12:06:45 +1000199int radeon_irq_emit(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000202 drm_radeon_irq_emit_t *emit = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int result;
204
Eric Anholt6c340ea2007-08-25 20:23:09 +1000205 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000208 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000209 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000212 result = radeon_emit_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Eric Anholtc153f452007-09-03 12:06:45 +1000214 if (DRM_COPY_TO_USER(emit->irq_seq, &result, sizeof(int))) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000215 DRM_ERROR("copy_to_user\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000216 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
219 return 0;
220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/* Doesn't need the hardware lock.
223 */
Eric Anholtc153f452007-09-03 12:06:45 +1000224int radeon_irq_wait(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000227 drm_radeon_irq_wait_t *irqwait = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000230 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000231 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Eric Anholtc153f452007-09-03 12:06:45 +1000234 return radeon_wait_irq(dev, irqwait->irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Dave Airlie84b1fd12007-07-11 15:53:27 +1000237static void radeon_enable_interrupt(struct drm_device *dev)
Dave Airlieddbee332007-07-11 12:16:01 +1000238{
239 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
240
241 dev_priv->irq_enable_reg = RADEON_SW_INT_ENABLE;
242 if (dev_priv->vblank_crtc & DRM_RADEON_VBLANK_CRTC1)
243 dev_priv->irq_enable_reg |= RADEON_CRTC_VBLANK_MASK;
244
245 if (dev_priv->vblank_crtc & DRM_RADEON_VBLANK_CRTC2)
246 dev_priv->irq_enable_reg |= RADEON_CRTC2_VBLANK_MASK;
247
248 RADEON_WRITE(RADEON_GEN_INT_CNTL, dev_priv->irq_enable_reg);
249 dev_priv->irq_enabled = 1;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/* drm_dma.h hooks
253*/
Dave Airlie84b1fd12007-07-11 15:53:27 +1000254void radeon_driver_irq_preinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000255{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000257 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000259 /* Disable *all* interrupts */
260 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 /* Clear bits if they're already high */
Dave Airlie6921e332005-06-26 21:05:59 +1000263 radeon_acknowledge_irqs(dev_priv, (RADEON_SW_INT_TEST_ACK |
Dave Airlieddbee332007-07-11 12:16:01 +1000264 RADEON_CRTC_VBLANK_STAT |
265 RADEON_CRTC2_VBLANK_STAT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266}
267
Dave Airlie84b1fd12007-07-11 15:53:27 +1000268void radeon_driver_irq_postinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000271 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000273 atomic_set(&dev_priv->swi_emitted, 0);
274 DRM_INIT_WAITQUEUE(&dev_priv->swi_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Dave Airlieddbee332007-07-11 12:16:01 +1000276 radeon_enable_interrupt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Dave Airlie84b1fd12007-07-11 15:53:27 +1000279void radeon_driver_irq_uninstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!dev_priv)
284 return;
285
Dave Airlieddbee332007-07-11 12:16:01 +1000286 dev_priv->irq_enabled = 0;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 /* Disable *all* interrupts */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000289 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290}
Dave Airlieddbee332007-07-11 12:16:01 +1000291
292
Dave Airlie84b1fd12007-07-11 15:53:27 +1000293int radeon_vblank_crtc_get(struct drm_device *dev)
Dave Airlieddbee332007-07-11 12:16:01 +1000294{
295 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
296 u32 flag;
297 u32 value;
298
299 flag = RADEON_READ(RADEON_GEN_INT_CNTL);
300 value = 0;
301
302 if (flag & RADEON_CRTC_VBLANK_MASK)
303 value |= DRM_RADEON_VBLANK_CRTC1;
304
305 if (flag & RADEON_CRTC2_VBLANK_MASK)
306 value |= DRM_RADEON_VBLANK_CRTC2;
307 return value;
308}
309
Dave Airlie84b1fd12007-07-11 15:53:27 +1000310int radeon_vblank_crtc_set(struct drm_device *dev, int64_t value)
Dave Airlieddbee332007-07-11 12:16:01 +1000311{
312 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
313 if (value & ~(DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) {
314 DRM_ERROR("called with invalid crtc 0x%x\n", (unsigned int)value);
Eric Anholt20caafa2007-08-25 19:22:43 +1000315 return -EINVAL;
Dave Airlieddbee332007-07-11 12:16:01 +1000316 }
317 dev_priv->vblank_crtc = (unsigned int)value;
318 radeon_enable_interrupt(dev);
319 return 0;
320}