blob: ad8a0ac7182e9ead9d1498fd57b3dc28cdbb95de [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>
30 * Michel D�zer <michel@daenzer.net>
31 */
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
Dave Airlie84b1fd12007-07-11 15:53:27 +1000147int radeon_driver_vblank_do_wait(struct drm_device * dev, unsigned int *sequence,
Dave Airlieddbee332007-07-11 12:16:01 +1000148 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) {
157 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 return DRM_ERR(EINVAL);
159 }
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
168 return DRM_ERR(EINVAL);
169
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 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199int radeon_irq_emit(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 DRM_DEVICE;
202 drm_radeon_private_t *dev_priv = dev->dev_private;
203 drm_radeon_irq_emit_t emit;
204 int result;
205
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 LOCK_TEST_WITH_RETURN(dev, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000208 if (!dev_priv) {
209 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return DRM_ERR(EINVAL);
211 }
212
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 DRM_COPY_FROM_USER_IOCTL(emit, (drm_radeon_irq_emit_t __user *) data,
214 sizeof(emit));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 result = radeon_emit_irq(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 if (DRM_COPY_TO_USER(emit.irq_seq, &result, sizeof(int))) {
219 DRM_ERROR("copy_to_user\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return DRM_ERR(EFAULT);
221 }
222
223 return 0;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* Doesn't need the hardware lock.
227 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228int radeon_irq_wait(DRM_IOCTL_ARGS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 DRM_DEVICE;
231 drm_radeon_private_t *dev_priv = dev->dev_private;
232 drm_radeon_irq_wait_t irqwait;
233
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000234 if (!dev_priv) {
235 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return DRM_ERR(EINVAL);
237 }
238
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000239 DRM_COPY_FROM_USER_IOCTL(irqwait, (drm_radeon_irq_wait_t __user *) data,
240 sizeof(irqwait));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 return radeon_wait_irq(dev, irqwait.irq_seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Dave Airlie84b1fd12007-07-11 15:53:27 +1000245static void radeon_enable_interrupt(struct drm_device *dev)
Dave Airlieddbee332007-07-11 12:16:01 +1000246{
247 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
248
249 dev_priv->irq_enable_reg = RADEON_SW_INT_ENABLE;
250 if (dev_priv->vblank_crtc & DRM_RADEON_VBLANK_CRTC1)
251 dev_priv->irq_enable_reg |= RADEON_CRTC_VBLANK_MASK;
252
253 if (dev_priv->vblank_crtc & DRM_RADEON_VBLANK_CRTC2)
254 dev_priv->irq_enable_reg |= RADEON_CRTC2_VBLANK_MASK;
255
256 RADEON_WRITE(RADEON_GEN_INT_CNTL, dev_priv->irq_enable_reg);
257 dev_priv->irq_enabled = 1;
258}
259
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260/* drm_dma.h hooks
261*/
Dave Airlie84b1fd12007-07-11 15:53:27 +1000262void radeon_driver_irq_preinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000263{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000265 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267 /* Disable *all* interrupts */
268 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 /* Clear bits if they're already high */
Dave Airlie6921e332005-06-26 21:05:59 +1000271 radeon_acknowledge_irqs(dev_priv, (RADEON_SW_INT_TEST_ACK |
Dave Airlieddbee332007-07-11 12:16:01 +1000272 RADEON_CRTC_VBLANK_STAT |
273 RADEON_CRTC2_VBLANK_STAT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274}
275
Dave Airlie84b1fd12007-07-11 15:53:27 +1000276void radeon_driver_irq_postinstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 atomic_set(&dev_priv->swi_emitted, 0);
282 DRM_INIT_WAITQUEUE(&dev_priv->swi_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Dave Airlieddbee332007-07-11 12:16:01 +1000284 radeon_enable_interrupt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Dave Airlie84b1fd12007-07-11 15:53:27 +1000287void radeon_driver_irq_uninstall(struct drm_device * dev)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 drm_radeon_private_t *dev_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 (drm_radeon_private_t *) dev->dev_private;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if (!dev_priv)
292 return;
293
Dave Airlieddbee332007-07-11 12:16:01 +1000294 dev_priv->irq_enabled = 0;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* Disable *all* interrupts */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
Dave Airlieddbee332007-07-11 12:16:01 +1000299
300
Dave Airlie84b1fd12007-07-11 15:53:27 +1000301int radeon_vblank_crtc_get(struct drm_device *dev)
Dave Airlieddbee332007-07-11 12:16:01 +1000302{
303 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
304 u32 flag;
305 u32 value;
306
307 flag = RADEON_READ(RADEON_GEN_INT_CNTL);
308 value = 0;
309
310 if (flag & RADEON_CRTC_VBLANK_MASK)
311 value |= DRM_RADEON_VBLANK_CRTC1;
312
313 if (flag & RADEON_CRTC2_VBLANK_MASK)
314 value |= DRM_RADEON_VBLANK_CRTC2;
315 return value;
316}
317
Dave Airlie84b1fd12007-07-11 15:53:27 +1000318int radeon_vblank_crtc_set(struct drm_device *dev, int64_t value)
Dave Airlieddbee332007-07-11 12:16:01 +1000319{
320 drm_radeon_private_t *dev_priv = (drm_radeon_private_t *) dev->dev_private;
321 if (value & ~(DRM_RADEON_VBLANK_CRTC1 | DRM_RADEON_VBLANK_CRTC2)) {
322 DRM_ERROR("called with invalid crtc 0x%x\n", (unsigned int)value);
323 return DRM_ERR(EINVAL);
324 }
325 dev_priv->vblank_crtc = (unsigned int)value;
326 radeon_enable_interrupt(dev);
327 return 0;
328}