drm: Use an include directory hierarchy similar to the Linux one
[dragonfly.git] / sys / dev / drm / mga / 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 <drm/drmP.h>
35 #include "mga_drm.h"
36 #include "mga_drv.h"
37
38 u32 mga_get_vblank_counter(struct drm_device *dev, int crtc)
39 {
40         drm_mga_private_t *const dev_priv = 
41                 (drm_mga_private_t *) dev->dev_private;
42
43         if (crtc != 0) {
44                 return 0;
45         }
46
47
48         return atomic_read(&dev_priv->vbl_received);
49 }
50
51
52 irqreturn_t mga_driver_irq_handler(DRM_IRQ_ARGS)
53 {
54         struct drm_device *dev = (struct drm_device *) arg;
55         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
56         int status;
57         int handled = 0;
58
59         status = MGA_READ(MGA_STATUS);
60
61         /* VBLANK interrupt */
62         if (status & MGA_VLINEPEN) {
63                 MGA_WRITE(MGA_ICLEAR, MGA_VLINEICLR);
64                 atomic_inc(&dev_priv->vbl_received);
65                 drm_handle_vblank(dev, 0);
66                 handled = 1;
67         }
68
69         /* SOFTRAP interrupt */
70         if (status & MGA_SOFTRAPEN) {
71                 const u32 prim_start = MGA_READ(MGA_PRIMADDRESS);
72                 const u32 prim_end = MGA_READ(MGA_PRIMEND);
73
74
75                 MGA_WRITE(MGA_ICLEAR, MGA_SOFTRAPICLR);
76
77                 /* In addition to clearing the interrupt-pending bit, we
78                  * have to write to MGA_PRIMEND to re-start the DMA operation.
79                  */
80                 if ((prim_start & ~0x03) != (prim_end & ~0x03)) {
81                         MGA_WRITE(MGA_PRIMEND, prim_end);
82                 }
83
84                 atomic_inc(&dev_priv->last_fence_retired);
85                 DRM_WAKEUP(&dev_priv->fence_queue);
86                 handled = 1;
87         }
88
89         if (handled)
90                 return IRQ_HANDLED;
91         return IRQ_NONE;
92 }
93
94 int mga_enable_vblank(struct drm_device *dev, int crtc)
95 {
96         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
97
98         if (crtc != 0) {
99                 DRM_ERROR("tried to enable vblank on non-existent crtc %d\n",
100                           crtc);
101                 return 0;
102         }
103
104         MGA_WRITE(MGA_IEN, MGA_VLINEIEN | MGA_SOFTRAPEN);
105         return 0;
106 }
107
108
109 void mga_disable_vblank(struct drm_device *dev, int crtc)
110 {
111         if (crtc != 0) {
112                 DRM_ERROR("tried to disable vblank on non-existent crtc %d\n",
113                           crtc);
114         }
115
116         /* Do *NOT* disable the vertical refresh interrupt.  MGA doesn't have
117          * a nice hardware counter that tracks the number of refreshes when
118          * the interrupt is disabled, and the kernel doesn't know the refresh
119          * rate to calculate an estimate.
120          */
121         /* MGA_WRITE(MGA_IEN, MGA_VLINEIEN | MGA_SOFTRAPEN); */
122 }
123
124 int mga_driver_fence_wait(struct drm_device * dev, unsigned int *sequence)
125 {
126         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
127         unsigned int cur_fence;
128         int ret = 0;
129
130         /* Assume that the user has missed the current sequence number
131          * by about a day rather than she wants to wait for years
132          * using fences.
133          */
134         DRM_WAIT_ON(ret, dev_priv->fence_queue, 3 * DRM_HZ,
135                     (((cur_fence = atomic_read(&dev_priv->last_fence_retired))
136                       - *sequence) <= (1 << 23)));
137
138         if (ret == -ERESTART)
139                 DRM_DEBUG("restarting syscall\n");
140
141         *sequence = cur_fence;
142
143         return ret;
144 }
145
146 void mga_driver_irq_preinstall(struct drm_device * dev)
147 {
148         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
149
150         /* Disable *all* interrupts */
151         MGA_WRITE(MGA_IEN, 0);
152         /* Clear bits if they're already high */
153         MGA_WRITE(MGA_ICLEAR, ~0);
154 }
155
156 int mga_driver_irq_postinstall(struct drm_device * dev)
157 {
158         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
159
160         DRM_INIT_WAITQUEUE(&dev_priv->fence_queue);
161
162         /* Turn on soft trap interrupt.  Vertical blank interrupts are enabled
163          * in mga_enable_vblank.
164          */
165         MGA_WRITE(MGA_IEN, MGA_SOFTRAPEN);
166         return 0;
167 }
168
169 void mga_driver_irq_uninstall(struct drm_device * dev)
170 {
171         drm_mga_private_t *dev_priv = (drm_mga_private_t *) dev->dev_private;
172         if (!dev_priv)
173                 return;
174
175         /* Disable *all* interrupts */
176         MGA_WRITE(MGA_IEN, 0);
177
178         dev->irq_enabled = 0;
179 }