blob: ac98964297cfffcf4939817629fbd51d17f32dd7 [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
David Howells760285e2012-10-02 18:01:07 +010038#include <drm/drmP.h>
39#include <drm/via_drm.h>
Dave Airlie22f579c2005-06-28 22:48:56 +100040#include "via_drv.h"
41
42#define VIA_REG_INTERRUPT 0x200
43
44/* VIA_REG_INTERRUPT */
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070045#define VIA_IRQ_GLOBAL (1 << 31)
Dave Airlie22f579c2005-06-28 22:48:56 +100046#define VIA_IRQ_VBLANK_ENABLE (1 << 19)
47#define VIA_IRQ_VBLANK_PENDING (1 << 3)
48#define VIA_IRQ_HQV0_ENABLE (1 << 11)
49#define VIA_IRQ_HQV1_ENABLE (1 << 25)
50#define VIA_IRQ_HQV0_PENDING (1 << 9)
51#define VIA_IRQ_HQV1_PENDING (1 << 10)
Dave Airlie92514242005-11-12 21:52:46 +110052#define VIA_IRQ_DMA0_DD_ENABLE (1 << 20)
53#define VIA_IRQ_DMA0_TD_ENABLE (1 << 21)
54#define VIA_IRQ_DMA1_DD_ENABLE (1 << 22)
55#define VIA_IRQ_DMA1_TD_ENABLE (1 << 23)
56#define VIA_IRQ_DMA0_DD_PENDING (1 << 4)
57#define VIA_IRQ_DMA0_TD_PENDING (1 << 5)
58#define VIA_IRQ_DMA1_DD_PENDING (1 << 6)
59#define VIA_IRQ_DMA1_TD_PENDING (1 << 7)
60
Dave Airlie22f579c2005-06-28 22:48:56 +100061
62/*
63 * Device-specific IRQs go here. This type might need to be extended with
64 * the register if there are multiple IRQ control registers.
Dave Airlieb5e89ed2005-09-25 14:28:13 +100065 * Currently we activate the HQV interrupts of Unichrome Pro group A.
Dave Airlie22f579c2005-06-28 22:48:56 +100066 */
67
68static maskarray_t via_pro_group_a_irqs[] = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +100069 {VIA_IRQ_HQV0_ENABLE, VIA_IRQ_HQV0_PENDING, 0x000003D0, 0x00008010,
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070070 0x00000000 },
Dave Airlieb5e89ed2005-09-25 14:28:13 +100071 {VIA_IRQ_HQV1_ENABLE, VIA_IRQ_HQV1_PENDING, 0x000013D0, 0x00008010,
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070072 0x00000000 },
Dave Airlie92514242005-11-12 21:52:46 +110073 {VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
74 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
75 {VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
76 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
Dave Airlieb5e89ed2005-09-25 14:28:13 +100077};
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070078static int via_num_pro_group_a = ARRAY_SIZE(via_pro_group_a_irqs);
Dave Airlie92514242005-11-12 21:52:46 +110079static int via_irqmap_pro_group_a[] = {0, 1, -1, 2, -1, 3};
Dave Airlie22f579c2005-06-28 22:48:56 +100080
Dave Airlie92514242005-11-12 21:52:46 +110081static maskarray_t via_unichrome_irqs[] = {
82 {VIA_IRQ_DMA0_TD_ENABLE, VIA_IRQ_DMA0_TD_PENDING, VIA_PCI_DMA_CSR0,
83 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008},
84 {VIA_IRQ_DMA1_TD_ENABLE, VIA_IRQ_DMA1_TD_PENDING, VIA_PCI_DMA_CSR1,
85 VIA_DMA_CSR_TA | VIA_DMA_CSR_TD, 0x00000008}
86};
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070087static int via_num_unichrome = ARRAY_SIZE(via_unichrome_irqs);
Dave Airlie92514242005-11-12 21:52:46 +110088static int via_irqmap_unichrome[] = {-1, -1, -1, 0, -1, 1};
Dave Airlie22f579c2005-06-28 22:48:56 +100089
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070090
Dave Airlieb5e89ed2005-09-25 14:28:13 +100091static unsigned time_diff(struct timeval *now, struct timeval *then)
Dave Airlie22f579c2005-06-28 22:48:56 +100092{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 return (now->tv_usec >= then->tv_usec) ?
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070094 now->tv_usec - then->tv_usec :
95 1000000 - (then->tv_usec - now->tv_usec);
96}
97
98u32 via_get_vblank_counter(struct drm_device *dev, int crtc)
99{
100 drm_via_private_t *dev_priv = dev->dev_private;
101 if (crtc != 0)
102 return 0;
103
104 return atomic_read(&dev_priv->vbl_received);
Dave Airlie22f579c2005-06-28 22:48:56 +1000105}
106
107irqreturn_t via_driver_irq_handler(DRM_IRQ_ARGS)
108{
Dave Airlie84b1fd12007-07-11 15:53:27 +1000109 struct drm_device *dev = (struct drm_device *) arg;
Dave Airlie22f579c2005-06-28 22:48:56 +1000110 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
111 u32 status;
112 int handled = 0;
113 struct timeval cur_vblank;
114 drm_via_irq_t *cur_irq = dev_priv->via_irqs;
115 int i;
116
117 status = VIA_READ(VIA_REG_INTERRUPT);
118 if (status & VIA_IRQ_VBLANK_PENDING) {
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700119 atomic_inc(&dev_priv->vbl_received);
120 if (!(atomic_read(&dev_priv->vbl_received) & 0x0F)) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000121 do_gettimeofday(&cur_vblank);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000122 if (dev_priv->last_vblank_valid) {
123 dev_priv->usec_per_vblank =
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700124 time_diff(&cur_vblank,
125 &dev_priv->last_vblank) >> 4;
Dave Airlie22f579c2005-06-28 22:48:56 +1000126 }
127 dev_priv->last_vblank = cur_vblank;
128 dev_priv->last_vblank_valid = 1;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000129 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700130 if (!(atomic_read(&dev_priv->vbl_received) & 0xFF)) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000131 DRM_DEBUG("US per vblank is: %u\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 dev_priv->usec_per_vblank);
Dave Airlie22f579c2005-06-28 22:48:56 +1000133 }
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700134 drm_handle_vblank(dev, 0);
Dave Airlie22f579c2005-06-28 22:48:56 +1000135 handled = 1;
136 }
Dave Airlie22f579c2005-06-28 22:48:56 +1000137
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000138 for (i = 0; i < dev_priv->num_irqs; ++i) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000139 if (status & cur_irq->pending_mask) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000140 atomic_inc(&cur_irq->irq_received);
141 DRM_WAKEUP(&cur_irq->irq_queue);
Dave Airlie22f579c2005-06-28 22:48:56 +1000142 handled = 1;
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200143 if (dev_priv->irq_map[drm_via_irq_dma0_td] == i)
Dave Airlie92514242005-11-12 21:52:46 +1100144 via_dmablit_handler(dev, 0, 1);
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200145 else if (dev_priv->irq_map[drm_via_irq_dma1_td] == i)
Dave Airlie92514242005-11-12 21:52:46 +1100146 via_dmablit_handler(dev, 1, 1);
Dave Airlie22f579c2005-06-28 22:48:56 +1000147 }
148 cur_irq++;
149 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000150
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800151 /* Acknowledge interrupts */
Dave Airlie22f579c2005-06-28 22:48:56 +1000152 VIA_WRITE(VIA_REG_INTERRUPT, status);
153
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700154
Dave Airlie22f579c2005-06-28 22:48:56 +1000155 if (handled)
156 return IRQ_HANDLED;
157 else
158 return IRQ_NONE;
159}
160
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200161static __inline__ void viadrv_acknowledge_irqs(drm_via_private_t *dev_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000162{
163 u32 status;
164
165 if (dev_priv) {
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800166 /* Acknowledge interrupts */
Dave Airlie22f579c2005-06-28 22:48:56 +1000167 status = VIA_READ(VIA_REG_INTERRUPT);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 VIA_WRITE(VIA_REG_INTERRUPT, status |
Dave Airlie22f579c2005-06-28 22:48:56 +1000169 dev_priv->irq_pending_mask);
170 }
171}
172
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700173int via_enable_vblank(struct drm_device *dev, int crtc)
Dave Airlie22f579c2005-06-28 22:48:56 +1000174{
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700175 drm_via_private_t *dev_priv = dev->dev_private;
176 u32 status;
Dave Airlie22f579c2005-06-28 22:48:56 +1000177
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700178 if (crtc != 0) {
179 DRM_ERROR("%s: bad crtc %d\n", __func__, crtc);
Dave Airlie22f579c2005-06-28 22:48:56 +1000180 return -EINVAL;
181 }
182
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700183 status = VIA_READ(VIA_REG_INTERRUPT);
Simon Farnsworth42dd8612009-07-10 11:25:16 +0100184 VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_VBLANK_ENABLE);
Dave Airlie22f579c2005-06-28 22:48:56 +1000185
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700186 VIA_WRITE8(0x83d4, 0x11);
187 VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
Dave Airlie22f579c2005-06-28 22:48:56 +1000188
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700189 return 0;
190}
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700192void via_disable_vblank(struct drm_device *dev, int crtc)
193{
194 drm_via_private_t *dev_priv = dev->dev_private;
Simon Farnsworth42dd8612009-07-10 11:25:16 +0100195 u32 status;
196
197 status = VIA_READ(VIA_REG_INTERRUPT);
198 VIA_WRITE(VIA_REG_INTERRUPT, status & ~VIA_IRQ_VBLANK_ENABLE);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700199
200 VIA_WRITE8(0x83d4, 0x11);
201 VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);
202
203 if (crtc != 0)
204 DRM_ERROR("%s: bad crtc %d\n", __func__, crtc);
Dave Airlie22f579c2005-06-28 22:48:56 +1000205}
206
Dave Airliece60fe02006-02-02 19:21:38 +1100207static int
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200208via_driver_irq_wait(struct drm_device *dev, unsigned int irq, int force_sequence,
Dave Airlie22f579c2005-06-28 22:48:56 +1000209 unsigned int *sequence)
210{
211 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
212 unsigned int cur_irq_sequence;
Jayachandran Cd2532582006-04-10 23:18:28 -0700213 drm_via_irq_t *cur_irq;
Dave Airlie22f579c2005-06-28 22:48:56 +1000214 int ret = 0;
Dave Airlie86678df2006-04-05 18:10:11 +1000215 maskarray_t *masks;
Dave Airlie92514242005-11-12 21:52:46 +1100216 int real_irq;
Dave Airlie22f579c2005-06-28 22:48:56 +1000217
Márton Németh3e684ea2008-01-24 15:58:57 +1000218 DRM_DEBUG("\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000219
220 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000221 DRM_ERROR("called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000222 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000223 }
224
Dave Airlie92514242005-11-12 21:52:46 +1100225 if (irq >= drm_via_irq_num) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000226 DRM_ERROR("Trying to wait on unknown irq %d\n", irq);
Eric Anholt20caafa2007-08-25 19:22:43 +1000227 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000228 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229
Dave Airlie92514242005-11-12 21:52:46 +1100230 real_irq = dev_priv->irq_map[irq];
Dave Airlie22f579c2005-06-28 22:48:56 +1000231
Dave Airlie92514242005-11-12 21:52:46 +1100232 if (real_irq < 0) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000233 DRM_ERROR("Video IRQ %d not available on this hardware.\n",
234 irq);
Eric Anholt20caafa2007-08-25 19:22:43 +1000235 return -EINVAL;
Dave Airlie92514242005-11-12 21:52:46 +1100236 }
Dave Airlie86678df2006-04-05 18:10:11 +1000237
238 masks = dev_priv->irq_masks;
Jayachandran Cd2532582006-04-10 23:18:28 -0700239 cur_irq = dev_priv->via_irqs + real_irq;
Dave Airlie92514242005-11-12 21:52:46 +1100240
241 if (masks[real_irq][2] && !force_sequence) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000242 DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * DRM_HZ,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 ((VIA_READ(masks[irq][2]) & masks[irq][3]) ==
244 masks[irq][4]));
Dave Airlie22f579c2005-06-28 22:48:56 +1000245 cur_irq_sequence = atomic_read(&cur_irq->irq_received);
246 } else {
247 DRM_WAIT_ON(ret, cur_irq->irq_queue, 3 * DRM_HZ,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000248 (((cur_irq_sequence =
249 atomic_read(&cur_irq->irq_received)) -
250 *sequence) <= (1 << 23)));
Dave Airlie22f579c2005-06-28 22:48:56 +1000251 }
252 *sequence = cur_irq_sequence;
253 return ret;
254}
255
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700256
Dave Airlie22f579c2005-06-28 22:48:56 +1000257/*
258 * drm_dma.h hooks
259 */
260
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200261void via_driver_irq_preinstall(struct drm_device *dev)
Dave Airlie22f579c2005-06-28 22:48:56 +1000262{
263 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
264 u32 status;
Jayachandran Cd2532582006-04-10 23:18:28 -0700265 drm_via_irq_t *cur_irq;
Dave Airlie22f579c2005-06-28 22:48:56 +1000266 int i;
267
Márton Németh3e684ea2008-01-24 15:58:57 +1000268 DRM_DEBUG("dev_priv: %p\n", dev_priv);
Dave Airlie22f579c2005-06-28 22:48:56 +1000269 if (dev_priv) {
Jayachandran Cd2532582006-04-10 23:18:28 -0700270 cur_irq = dev_priv->via_irqs;
Dave Airlie22f579c2005-06-28 22:48:56 +1000271
272 dev_priv->irq_enable_mask = VIA_IRQ_VBLANK_ENABLE;
273 dev_priv->irq_pending_mask = VIA_IRQ_VBLANK_PENDING;
274
Thomas Hellstrom689692e2007-01-08 21:19:57 +1100275 if (dev_priv->chipset == VIA_PRO_GROUP_A ||
276 dev_priv->chipset == VIA_DX9_0) {
277 dev_priv->irq_masks = via_pro_group_a_irqs;
278 dev_priv->num_irqs = via_num_pro_group_a;
279 dev_priv->irq_map = via_irqmap_pro_group_a;
280 } else {
281 dev_priv->irq_masks = via_unichrome_irqs;
282 dev_priv->num_irqs = via_num_unichrome;
283 dev_priv->irq_map = via_irqmap_unichrome;
284 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000285
286 for (i = 0; i < dev_priv->num_irqs; ++i) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000287 atomic_set(&cur_irq->irq_received, 0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 cur_irq->enable_mask = dev_priv->irq_masks[i][0];
Dave Airlie22f579c2005-06-28 22:48:56 +1000289 cur_irq->pending_mask = dev_priv->irq_masks[i][1];
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290 DRM_INIT_WAITQUEUE(&cur_irq->irq_queue);
Dave Airlie22f579c2005-06-28 22:48:56 +1000291 dev_priv->irq_enable_mask |= cur_irq->enable_mask;
292 dev_priv->irq_pending_mask |= cur_irq->pending_mask;
293 cur_irq++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000294
Dave Airlie22f579c2005-06-28 22:48:56 +1000295 DRM_DEBUG("Initializing IRQ %d\n", i);
296 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297
298 dev_priv->last_vblank_valid = 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000299
Dave Airlie92514242005-11-12 21:52:46 +1100300 /* Clear VSync interrupt regs */
Dave Airlie22f579c2005-06-28 22:48:56 +1000301 status = VIA_READ(VIA_REG_INTERRUPT);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000302 VIA_WRITE(VIA_REG_INTERRUPT, status &
Dave Airlie22f579c2005-06-28 22:48:56 +1000303 ~(dev_priv->irq_enable_mask));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304
Dave Airlie22f579c2005-06-28 22:48:56 +1000305 /* Clear bits if they're already high */
306 viadrv_acknowledge_irqs(dev_priv);
307 }
308}
309
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700310int via_driver_irq_postinstall(struct drm_device *dev)
Dave Airlie22f579c2005-06-28 22:48:56 +1000311{
312 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
313 u32 status;
314
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700315 DRM_DEBUG("via_driver_irq_postinstall\n");
316 if (!dev_priv)
317 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000318
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700319 status = VIA_READ(VIA_REG_INTERRUPT);
320 VIA_WRITE(VIA_REG_INTERRUPT, status | VIA_IRQ_GLOBAL
321 | dev_priv->irq_enable_mask);
Dave Airlie22f579c2005-06-28 22:48:56 +1000322
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700323 /* Some magic, oh for some data sheets ! */
324 VIA_WRITE8(0x83d4, 0x11);
325 VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) | 0x30);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700327 return 0;
Dave Airlie22f579c2005-06-28 22:48:56 +1000328}
329
Nicolas Kaiser58c1e852010-07-11 15:32:42 +0200330void via_driver_irq_uninstall(struct drm_device *dev)
Dave Airlie22f579c2005-06-28 22:48:56 +1000331{
332 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
333 u32 status;
334
Márton Németh3e684ea2008-01-24 15:58:57 +1000335 DRM_DEBUG("\n");
Dave Airlie22f579c2005-06-28 22:48:56 +1000336 if (dev_priv) {
337
338 /* Some more magic, oh for some data sheets ! */
339
340 VIA_WRITE8(0x83d4, 0x11);
341 VIA_WRITE8(0x83d5, VIA_READ8(0x83d5) & ~0x30);
342
343 status = VIA_READ(VIA_REG_INTERRUPT);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000344 VIA_WRITE(VIA_REG_INTERRUPT, status &
Dave Airlie22f579c2005-06-28 22:48:56 +1000345 ~(VIA_IRQ_VBLANK_ENABLE | dev_priv->irq_enable_mask));
346 }
347}
348
Eric Anholtc153f452007-09-03 12:06:45 +1000349int via_wait_irq(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie22f579c2005-06-28 22:48:56 +1000350{
Eric Anholtc153f452007-09-03 12:06:45 +1000351 drm_via_irqwait_t *irqwait = data;
Dave Airlie22f579c2005-06-28 22:48:56 +1000352 struct timeval now;
353 int ret = 0;
354 drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private;
355 drm_via_irq_t *cur_irq = dev_priv->via_irqs;
356 int force_sequence;
357
Eric Anholtc153f452007-09-03 12:06:45 +1000358 if (irqwait->request.irq >= dev_priv->num_irqs) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000359 DRM_ERROR("Trying to wait on unknown irq %d\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000360 irqwait->request.irq);
Eric Anholt20caafa2007-08-25 19:22:43 +1000361 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000362 }
363
Eric Anholtc153f452007-09-03 12:06:45 +1000364 cur_irq += irqwait->request.irq;
Dave Airlie22f579c2005-06-28 22:48:56 +1000365
Eric Anholtc153f452007-09-03 12:06:45 +1000366 switch (irqwait->request.type & ~VIA_IRQ_FLAGS_MASK) {
Dave Airlie22f579c2005-06-28 22:48:56 +1000367 case VIA_IRQ_RELATIVE:
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700368 irqwait->request.sequence +=
369 atomic_read(&cur_irq->irq_received);
Eric Anholtc153f452007-09-03 12:06:45 +1000370 irqwait->request.type &= ~_DRM_VBLANK_RELATIVE;
Dave Airlie22f579c2005-06-28 22:48:56 +1000371 case VIA_IRQ_ABSOLUTE:
372 break;
373 default:
Eric Anholt20caafa2007-08-25 19:22:43 +1000374 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000375 }
376
Eric Anholtc153f452007-09-03 12:06:45 +1000377 if (irqwait->request.type & VIA_IRQ_SIGNAL) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000378 DRM_ERROR("Signals on Via IRQs not implemented yet.\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000379 return -EINVAL;
Dave Airlie22f579c2005-06-28 22:48:56 +1000380 }
381
Eric Anholtc153f452007-09-03 12:06:45 +1000382 force_sequence = (irqwait->request.type & VIA_IRQ_FORCE_SEQUENCE);
Dave Airlie22f579c2005-06-28 22:48:56 +1000383
Eric Anholtc153f452007-09-03 12:06:45 +1000384 ret = via_driver_irq_wait(dev, irqwait->request.irq, force_sequence,
385 &irqwait->request.sequence);
Dave Airlie22f579c2005-06-28 22:48:56 +1000386 do_gettimeofday(&now);
Eric Anholtc153f452007-09-03 12:06:45 +1000387 irqwait->reply.tval_sec = now.tv_sec;
388 irqwait->reply.tval_usec = now.tv_usec;
Dave Airlie22f579c2005-06-28 22:48:56 +1000389
390 return ret;
391}