blob: c248c1d37268ac64cfed64e8b6bfba43b3d80332 [file] [log] [blame]
Dave Airlie22f579c2005-06-28 22:48:56 +10001/* via_irq.c
2 *
3 * Copyright 2004 BEAM Ltd.
4 * Copyright 2002 Tungsten Graphics, Inc.
5 * Copyright 2005 Thomas Hellstrom.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * BEAM LTD, TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * 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 * Terry Barnaby <terry1@beam.ltd.uk>
30 * Keith Whitwell <keith@tungstengraphics.com>
31 * Thomas Hellstrom <unichrome@shipmail.org>
32 *
33 * This code provides standard DRM access to the Via Unichrome / Pro Vertical blank
34 * interrupt, as well as an infrastructure to handle other interrupts of the chip.
35 * The refresh rate is also calculated for video playback sync purposes.
36 */
37
38#include "drmP.h"
39#include "drm.h"
40#include "via_drm.h"
41#include "via_drv.h"
42
43#define VIA_REG_INTERRUPT 0x200
44
45/* VIA_REG_INTERRUPT */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070046#define VIA_IRQ_GLOBAL (1 << 31)
Dave Airlie22f579c2005-06-28 22:48:56 +100047#define VIA_IRQ_VBLANK_ENABLE (1 << 19)
48#define VIA_IRQ_VBLANK_PENDING (1 << 3)
49#define VIA_IRQ_HQV0_ENABLE (1 << 11)
50#define VIA_IRQ_HQV1_ENABLE (1 << 25)
51#define VIA_IRQ_HQV0_PENDING (1 << 9)
52#define VIA_IRQ_HQV1_PENDING (1 << 10)
Dave Airlie92514242005-11-12 21:52:46 +110053#define VIA_IRQ_DMA0_DD_ENABLE (1 << 20)
54#define VIA_IRQ_DMA0_TD_ENABLE (1 << 21)
55#define VIA_IRQ_DMA1_DD_ENABLE (1 << 22)
56#define VIA_IRQ_DMA1_TD_ENABLE (1 << 23)
57#define VIA_IRQ_DMA0_DD_PENDING (1 << 4)
58#define VIA_IRQ_DMA0_TD_PENDING (1 << 5)
59#define VIA_IRQ_DMA1_DD_PENDING (1 << 6)
60#define VIA_IRQ_DMA1_TD_PENDING (1 << 7)
61
Dave Airlie22f579c2005-06-28 22:48:56 +100062
63/*
64 * Device-specific IRQs go here. This type might need to be extended with
65 * the register if there are multiple IRQ control registers.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100066 * Currently we activate the HQV interrupts of Unichrome Pro group A.
Dave Airlie22f579c2005-06-28 22:48:56 +100067 */
68
69static maskarray_t via_pro_group_a_irqs[] = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100070 {VIA_IRQ_HQV0_ENABLE, VIA_IRQ_HQV0_PENDING, 0x000003D0, 0x00008010,
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070071 0x00000000 },
Dave Airlieb5e89ed2005-09-25 14:28:13 +100072 {VIA_IRQ_HQV1_ENABLE, VIA_IRQ_HQV1_PENDING, 0x000013D0, 0x00008010,
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070073 0x00000000 },
Dave Airlie92514242005-11-12 21:52:46 +110074 {VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
75 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
76 {VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
77 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
Dave Airlieb5e89ed2005-09-25 14:28:13 +100078};
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070079static int via_num_pro_group_a = ARRAY_SIZE(via_pro_group_a_irqs);
Dave Airlie92514242005-11-12 21:52:46 +110080static int via_irqmap_pro_group_a[] = {0, 1, -1, 2, -1, 3};
Dave Airlie22f579c2005-06-28 22:48:56 +100081
Dave Airlie92514242005-11-12 21:52:46 +110082static maskarray_t via_unichrome_irqs[] = {
83 {VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
84 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
85 {VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
86 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008}
87};
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070088static int via_num_unichrome = ARRAY_SIZE(via_unichrome_irqs);
Dave Airlie92514242005-11-12 21:52:46 +110089static int via_irqmap_unichrome[] = {-1, -1, -1, 0, -1, 1};
Dave Airlie22f579c2005-06-28 22:48:56 +100090
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070091
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092static unsigned time_diff(struct timeval *now, struct timeval *then)
Dave Airlie22f579c2005-06-28 22:48:56 +100093{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100094 return (now->tv_usec >= then->tv_usec) ?
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070095 now->tv_usec - then->tv_usec :
96 1000000 - (then->tv_usec - now->tv_usec);
97}
98
99u32 via_get_vblank_counter(struct drm_device *dev, int crtc)
100{
101 drm_via_private_t *dev_priv = dev->dev_private;
102 if (crtc != 0)
103 return 0;
104
105 return atomic_read(&dev_priv->vbl_received);
Dave Airlie22f579c2005-06-28 22:48:56 +1000106}
107
108irqreturn_t via_driver_irq_handler(DRM_IRQ_ARGS)
109{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000110 struct drm_device *dev = (struct drm_device *) arg;
Dave Airlie22f579c2005-06-28 22:48:56 +1000111 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
112 u32 status;
113 int handled = 0;
114 struct timeval cur_vblank;
115 drm_via_irq_t *cur_irq = dev_priv->via_irqs;
116 int i;
117
118 status = VIA_READ(VIA_REG_INTERRUPT);
119 if (status & VIA_IRQ_VBLANK_PENDING) {
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700120 atomic_inc(&dev_priv->vbl_received);
121 if (!(atomic_read(&dev_priv->vbl_received) & 0x0F)) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000122 do_gettimeofday(&cur_vblank);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000123 if (dev_priv->last_vblank_valid) {
124 dev_priv->usec_per_vblank =
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700125 time_diff(&cur_vblank,
126 &dev_priv->last_vblank) >> 4;
Dave Airlie22f579c2005-06-28 22:48:56 +1000127 }
128 dev_priv->last_vblank = cur_vblank;
129 dev_priv->last_vblank_valid = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700131 if (!(atomic_read(&dev_priv->vbl_received) & 0xFF)) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000132 DRM_DEBUG("US per vblank is: %u\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000133 dev_priv->usec_per_vblank);
Dave Airlie22f579c2005-06-28 22:48:56 +1000134 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700135 drm_handle_vblank(dev, 0);
Dave Airlie22f579c2005-06-28 22:48:56 +1000136 handled = 1;
137 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000138
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000139 for (i = 0; i < dev_priv->num_irqs; ++i) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000140 if (status & cur_irq->pending_mask) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000141 atomic_inc(&cur_irq->irq_received);
142 DRM_WAKEUP(&cur_irq->irq_queue);
Dave Airlie22f579c2005-06-28 22:48:56 +1000143 handled = 1;
Dave Airlie92514242005-11-12 21:52:46 +1100144 if (dev_priv->irq_map[drm_via_irq_dma0_td] == i) {
145 via_dmablit_handler(dev, 0, 1);
146 } else if (dev_priv->irq_map[drm_via_irq_dma1_td] == i) {
147 via_dmablit_handler(dev, 1, 1);
148 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000149 }
150 cur_irq++;
151 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000152
Dave Airlie22f579c2005-06-28 22:48:56 +1000153 /* Acknowlege interrupts */
154 VIA_WRITE(VIA_REG_INTERRUPT, status);
155
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700156
Dave Airlie22f579c2005-06-28 22:48:56 +1000157 if (handled)
158 return IRQ_HANDLED;
159 else
160 return IRQ_NONE;
161}
162
163static __inline__ void viadrv_acknowledge_irqs(drm_via_private_t * dev_priv)
164{
165 u32 status;
166
167 if (dev_priv) {
168 /* Acknowlege interrupts */
169 status = VIA_READ(VIA_REG_INTERRUPT);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 VIA_WRITE(VIA_REG_INTERRUPT, status |
Dave Airlie22f579c2005-06-28 22:48:56 +1000171 dev_priv->irq_pending_mask);
172 }
173}
174
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700175int via_enable_vblank(struct drm_device *dev, int crtc)
Dave Airlie22f579c2005-06-28 22:48:56 +1000176{
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700177 drm_via_private_t *dev_priv = dev->dev_private;
178 u32 status;
Dave Airlie22f579c2005-06-28 22:48:56 +1000179
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700180 if (crtc != 0) {
181 DRM_ERROR("%s: bad crtc %d\n", __func__, crtc);
Dave Airlie22f579c2005-06-28 22:48:56 +1000182 return -EINVAL;
183 }
184
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700185 status = VIA_READ(VIA_REG_INTERRUPT);
186 VIA_WRITE(VIA_REG_INTERRUPT, status & VIA_IRQ_VBLANK_ENABLE);
Dave Airlie22f579c2005-06-28 22:48:56 +1000187
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700188 VIA_WRITE8(0x83d4, 0x11);
189 VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
Dave Airlie22f579c2005-06-28 22:48:56 +1000190
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700191 return 0;
192}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000193
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700194void via_disable_vblank(struct drm_device *dev, int crtc)
195{
196 drm_via_private_t *dev_priv = dev->dev_private;
197
198 VIA_WRITE8(0x83d4, 0x11);
199 VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);
200
201 if (crtc != 0)
202 DRM_ERROR("%s: bad crtc %d\n", __func__, crtc);
Dave Airlie22f579c2005-06-28 22:48:56 +1000203}
204
Dave Airliece60fe02006-02-02 19:21:38 +1100205static int
Dave Airlie84b1fd12007-07-11 15:53:27 +1000206via_driver_irq_wait(struct drm_device * dev, unsigned int irq, int force_sequence,
Dave Airlie22f579c2005-06-28 22:48:56 +1000207 unsigned int *sequence)
208{
209 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
210 unsigned int cur_irq_sequence;
Jayachandran Cd2532582006-04-10 23:18:28 -0700211 drm_via_irq_t *cur_irq;
Dave Airlie22f579c2005-06-28 22:48:56 +1000212 int ret = 0;
Dave Airlie86678df2006-04-05 18:10:11 +1000213 maskarray_t *masks;
Dave Airlie92514242005-11-12 21:52:46 +1100214 int real_irq;
Dave Airlie22f579c2005-06-28 22:48:56 +1000215
Márton Németh3e684ea2008-01-24 15:58:57 +1000216 DRM_DEBUG("\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000217
218 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000219 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000220 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000221 }
222
Dave Airlie92514242005-11-12 21:52:46 +1100223 if (irq >= drm_via_irq_num) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000224 DRM_ERROR("Trying to wait on unknown irq %d\n", irq);
Eric Anholt20caafa2007-08-25 19:22:43 +1000225 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000226 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227
Dave Airlie92514242005-11-12 21:52:46 +1100228 real_irq = dev_priv->irq_map[irq];
Dave Airlie22f579c2005-06-28 22:48:56 +1000229
Dave Airlie92514242005-11-12 21:52:46 +1100230 if (real_irq < 0) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000231 DRM_ERROR("Video IRQ %d not available on this hardware.\n",
232 irq);
Eric Anholt20caafa2007-08-25 19:22:43 +1000233 return -EINVAL;
Dave Airlie92514242005-11-12 21:52:46 +1100234 }
Dave Airlie86678df2006-04-05 18:10:11 +1000235
236 masks = dev_priv->irq_masks;
Jayachandran Cd2532582006-04-10 23:18:28 -0700237 cur_irq = dev_priv->via_irqs + real_irq;
Dave Airlie92514242005-11-12 21:52:46 +1100238
239 if (masks[real_irq][2] && !force_sequence) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000240 DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * DRM_HZ,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000241 ((VIA_READ(masks[irq][2]) & masks[irq][3]) ==
242 masks[irq][4]));
Dave Airlie22f579c2005-06-28 22:48:56 +1000243 cur_irq_sequence = atomic_read(&cur_irq->irq_received);
244 } else {
245 DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * DRM_HZ,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 (((cur_irq_sequence =
247 atomic_read(&cur_irq->irq_received)) -
248 *sequence) <= (1 << 23)));
Dave Airlie22f579c2005-06-28 22:48:56 +1000249 }
250 *sequence = cur_irq_sequence;
251 return ret;
252}
253
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700254
Dave Airlie22f579c2005-06-28 22:48:56 +1000255/*
256 * drm_dma.h hooks
257 */
258
Dave Airlie84b1fd12007-07-11 15:53:27 +1000259void via_driver_irq_preinstall(struct drm_device * dev)
Dave Airlie22f579c2005-06-28 22:48:56 +1000260{
261 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
262 u32 status;
Jayachandran Cd2532582006-04-10 23:18:28 -0700263 drm_via_irq_t *cur_irq;
Dave Airlie22f579c2005-06-28 22:48:56 +1000264 int i;
265
Márton Németh3e684ea2008-01-24 15:58:57 +1000266 DRM_DEBUG("dev_priv: %p\n", dev_priv);
Dave Airlie22f579c2005-06-28 22:48:56 +1000267 if (dev_priv) {
Jayachandran Cd2532582006-04-10 23:18:28 -0700268 cur_irq = dev_priv->via_irqs;
Dave Airlie22f579c2005-06-28 22:48:56 +1000269
270 dev_priv->irq_enable_mask = VIA_IRQ_VBLANK_ENABLE;
271 dev_priv->irq_pending_mask = VIA_IRQ_VBLANK_PENDING;
272
Thomas Hellstrom689692e2007-01-08 21:19:57 +1100273 if (dev_priv->chipset == VIA_PRO_GROUP_A ||
274 dev_priv->chipset == VIA_DX9_0) {
275 dev_priv->irq_masks = via_pro_group_a_irqs;
276 dev_priv->num_irqs = via_num_pro_group_a;
277 dev_priv->irq_map = via_irqmap_pro_group_a;
278 } else {
279 dev_priv->irq_masks = via_unichrome_irqs;
280 dev_priv->num_irqs = via_num_unichrome;
281 dev_priv->irq_map = via_irqmap_unichrome;
282 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283
284 for (i = 0; i < dev_priv->num_irqs; ++i) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000285 atomic_set(&cur_irq->irq_received, 0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286 cur_irq->enable_mask = dev_priv->irq_masks[i][0];
Dave Airlie22f579c2005-06-28 22:48:56 +1000287 cur_irq->pending_mask = dev_priv->irq_masks[i][1];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 DRM_INIT_WAITQUEUE(&cur_irq->irq_queue);
Dave Airlie22f579c2005-06-28 22:48:56 +1000289 dev_priv->irq_enable_mask |= cur_irq->enable_mask;
290 dev_priv->irq_pending_mask |= cur_irq->pending_mask;
291 cur_irq++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292
Dave Airlie22f579c2005-06-28 22:48:56 +1000293 DRM_DEBUG("Initializing IRQ %d\n", i);
294 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295
296 dev_priv->last_vblank_valid = 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000297
Dave Airlie92514242005-11-12 21:52:46 +1100298 /* Clear VSync interrupt regs */
Dave Airlie22f579c2005-06-28 22:48:56 +1000299 status = VIA_READ(VIA_REG_INTERRUPT);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300 VIA_WRITE(VIA_REG_INTERRUPT, status &
Dave Airlie22f579c2005-06-28 22:48:56 +1000301 ~(dev_priv->irq_enable_mask));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302
Dave Airlie22f579c2005-06-28 22:48:56 +1000303 /* Clear bits if they're already high */
304 viadrv_acknowledge_irqs(dev_priv);
305 }
306}
307
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700308int via_driver_irq_postinstall(struct drm_device *dev)
Dave Airlie22f579c2005-06-28 22:48:56 +1000309{
310 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
311 u32 status;
312
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700313 DRM_DEBUG("via_driver_irq_postinstall\n");
314 if (!dev_priv)
315 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000316
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700317 status = VIA_READ(VIA_REG_INTERRUPT);
318 VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_GLOBAL
319 | dev_priv->irq_enable_mask);
Dave Airlie22f579c2005-06-28 22:48:56 +1000320
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700321 /* Some magic, oh for some data sheets ! */
322 VIA_WRITE8(0x83d4, 0x11);
323 VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700325 return 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000326}
327
Dave Airlie84b1fd12007-07-11 15:53:27 +1000328void via_driver_irq_uninstall(struct drm_device * dev)
Dave Airlie22f579c2005-06-28 22:48:56 +1000329{
330 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
331 u32 status;
332
Márton Németh3e684ea2008-01-24 15:58:57 +1000333 DRM_DEBUG("\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000334 if (dev_priv) {
335
336 /* Some more magic, oh for some data sheets ! */
337
338 VIA_WRITE8(0x83d4, 0x11);
339 VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);
340
341 status = VIA_READ(VIA_REG_INTERRUPT);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 VIA_WRITE(VIA_REG_INTERRUPT, status &
Dave Airlie22f579c2005-06-28 22:48:56 +1000343 ~(VIA_IRQ_VBLANK_ENABLE | dev_priv->irq_enable_mask));
344 }
345}
346
Eric Anholtc153f452007-09-03 12:06:45 +1000347int via_wait_irq(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000348{
Eric Anholtc153f452007-09-03 12:06:45 +1000349 drm_via_irqwait_t *irqwait = data;
Dave Airlie22f579c2005-06-28 22:48:56 +1000350 struct timeval now;
351 int ret = 0;
352 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
353 drm_via_irq_t *cur_irq = dev_priv->via_irqs;
354 int force_sequence;
355
Eric Anholtc153f452007-09-03 12:06:45 +1000356 if (irqwait->request.irq >= dev_priv->num_irqs) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000357 DRM_ERROR("Trying to wait on unknown irq %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000358 irqwait->request.irq);
Eric Anholt20caafa2007-08-25 19:22:43 +1000359 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000360 }
361
Eric Anholtc153f452007-09-03 12:06:45 +1000362 cur_irq += irqwait->request.irq;
Dave Airlie22f579c2005-06-28 22:48:56 +1000363
Eric Anholtc153f452007-09-03 12:06:45 +1000364 switch (irqwait->request.type & ~VIA_IRQ_FLAGS_MASK) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000365 case VIA_IRQ_RELATIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700366 irqwait->request.sequence +=
367 atomic_read(&cur_irq->irq_received);
Eric Anholtc153f452007-09-03 12:06:45 +1000368 irqwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Dave Airlie22f579c2005-06-28 22:48:56 +1000369 case VIA_IRQ_ABSOLUTE:
370 break;
371 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000372 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000373 }
374
Eric Anholtc153f452007-09-03 12:06:45 +1000375 if (irqwait->request.type & VIA_IRQ_SIGNAL) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000376 DRM_ERROR("Signals on Via IRQs not implemented yet.\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000377 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000378 }
379
Eric Anholtc153f452007-09-03 12:06:45 +1000380 force_sequence = (irqwait->request.type & VIA_IRQ_FORCE_SEQUENCE);
Dave Airlie22f579c2005-06-28 22:48:56 +1000381
Eric Anholtc153f452007-09-03 12:06:45 +1000382 ret = via_driver_irq_wait(dev, irqwait->request.irq, force_sequence,
383 &irqwait->request.sequence);
Dave Airlie22f579c2005-06-28 22:48:56 +1000384 do_gettimeofday(&now);
Eric Anholtc153f452007-09-03 12:06:45 +1000385 irqwait->reply.tval_sec = now.tv_sec;
386 irqwait->reply.tval_usec = now.tv_usec;
Dave Airlie22f579c2005-06-28 22:48:56 +1000387
388 return ret;
389}