]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/drm/mga_irq.c
Merge DRM CVS as of 2005-12-02, adding i915 DRM support thanks to Alexey Popov,
[FreeBSD/FreeBSD.git] / sys / dev / drm / mga_irq.c
1 /* mga_irq.c -- IRQ handling for radeon -*- linux-c -*-
2  */
3 /*-
4  * Copyright (C) The Weather Channel, Inc.  2002.  All Rights Reserved.
5  *
6  * The Weather Channel (TM) funded Tungsten Graphics to develop the
7  * initial release of the Radeon 8500 driver under the XFree86 license.
8  * This notice must be preserved.
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice (including the next
18  * paragraph) shall be included in all copies or substantial portions of the
19  * Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  *
29  * Authors:
30  *    Keith Whitwell <keith@tungstengraphics.com>
31  *    Eric Anholt <anholt@FreeBSD.org>
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "dev/drm/drmP.h"
38 #include "dev/drm/drm.h"
39 #include "dev/drm/mga_drm.h"
40 #include "dev/drm/mga_drv.h"
41
42 irqreturn_t mga_driver_irq_handler(DRM_IRQ_ARGS)
43 {
44         drm_device_t *dev = (drm_device_t *) arg;
45         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
46         int status;
47         int handled = 0;
48
49         status = MGA_READ(MGA_STATUS);
50
51         /* VBLANK interrupt */
52         if (status & MGA_VLINEPEN) {
53                 MGA_WRITE(MGA_ICLEAR, MGA_VLINEICLR);
54                 atomic_inc(&dev->vbl_received);
55                 DRM_WAKEUP(&dev->vbl_queue);
56                 drm_vbl_send_signals(dev);
57                 handled = 1;
58         }
59
60         /* SOFTRAP interrupt */
61         if (status & MGA_SOFTRAPEN) {
62                 const u32 prim_start = MGA_READ(MGA_PRIMADDRESS);
63                 const u32 prim_end   = MGA_READ(MGA_PRIMEND);
64
65
66                 MGA_WRITE(MGA_ICLEAR, MGA_SOFTRAPICLR);
67
68                 /* In addition to clearing the interrupt-pending bit, we
69                  * have to write to MGA_PRIMEND to re-start the DMA operation.
70                  */
71                 if ( (prim_start & ~0x03) != (prim_end & ~0x03) ) {
72                         MGA_WRITE(MGA_PRIMEND, prim_end);
73                 }
74
75                 atomic_inc(&dev_priv->last_fence_retired);
76                 DRM_WAKEUP(&dev_priv->fence_queue);
77                 handled = 1;
78         }
79
80         if ( handled ) {
81                 return IRQ_HANDLED;
82         }
83         return IRQ_NONE;
84 }
85
86 int mga_driver_vblank_wait(drm_device_t * dev, unsigned int *sequence)
87 {
88         unsigned int cur_vblank;
89         int ret = 0;
90
91         /* Assume that the user has missed the current sequence number
92          * by about a day rather than she wants to wait for years
93          * using vertical blanks...
94          */
95         DRM_WAIT_ON(ret, dev->vbl_queue, 3 * DRM_HZ,
96                     (((cur_vblank = atomic_read(&dev->vbl_received))
97                       - *sequence) <= (1 << 23)));
98
99         *sequence = cur_vblank;
100
101         return ret;
102 }
103
104 int mga_driver_fence_wait(drm_device_t * dev, unsigned int *sequence)
105 {
106         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
107         unsigned int cur_fence;
108         int ret = 0;
109
110         /* Assume that the user has missed the current sequence number
111          * by about a day rather than she wants to wait for years
112          * using fences.
113          */
114         DRM_WAIT_ON(ret, dev_priv->fence_queue, 3 * DRM_HZ,
115                     (((cur_fence = atomic_read(&dev_priv->last_fence_retired))
116                       - *sequence) <= (1 << 23)));
117
118         *sequence = cur_fence;
119
120         return ret;
121 }
122
123 void mga_driver_irq_preinstall(drm_device_t * dev)
124 {
125         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
126
127         /* Disable *all* interrupts */
128         MGA_WRITE(MGA_IEN, 0);
129         /* Clear bits if they're already high */
130         MGA_WRITE(MGA_ICLEAR, ~0);
131 }
132
133 void mga_driver_irq_postinstall(drm_device_t * dev)
134 {
135         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
136
137         DRM_INIT_WAITQUEUE( &dev_priv->fence_queue );
138
139         /* Turn on vertical blank interrupt and soft trap interrupt. */
140         MGA_WRITE(MGA_IEN, MGA_VLINEIEN | MGA_SOFTRAPEN);
141 }
142
143 void mga_driver_irq_uninstall(drm_device_t * dev)
144 {
145         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
146         if (!dev_priv)
147                 return;
148
149         /* Disable *all* interrupts */
150         MGA_WRITE(MGA_IEN, 0);
151         
152         dev->irq_enabled = 0;
153 }