drm: misc cleanup
[sfrench/cifs-2.6.git] / drivers / char / drm / i915_irq.c
1 /* i915_dma.c -- DMA support for the I915 -*- linux-c -*-
2  */
3 /**************************************************************************
4  *
5  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sub license, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject to
14  * the following conditions:
15  *
16  * The above copyright notice and this permission notice (including the
17  * next paragraph) shall be included in all copies or substantial portions
18  * of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
24  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *
28  **************************************************************************/
29
30 #include "drmP.h"
31 #include "drm.h"
32 #include "i915_drm.h"
33 #include "i915_drv.h"
34
35 #define USER_INT_FLAG 0x2
36 #define MAX_NOPID ((u32)~0)
37 #define READ_BREADCRUMB(dev_priv)  (((u32*)(dev_priv->hw_status_page))[5])
38
39 irqreturn_t i915_driver_irq_handler(DRM_IRQ_ARGS)
40 {
41         drm_device_t *dev = (drm_device_t *) arg;
42         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
43         u16 temp;
44
45         temp = I915_READ16(I915REG_INT_IDENTITY_R);
46         temp &= USER_INT_FLAG;
47
48         DRM_DEBUG("%s flag=%08x\n", __FUNCTION__, temp);
49
50         if (temp == 0)
51                 return IRQ_NONE;
52
53         I915_WRITE16(I915REG_INT_IDENTITY_R, temp);
54         DRM_WAKEUP(&dev_priv->irq_queue);
55
56         return IRQ_HANDLED;
57 }
58
59 static int i915_emit_irq(drm_device_t * dev)
60 {
61         drm_i915_private_t *dev_priv = dev->dev_private;
62         u32 ret;
63         RING_LOCALS;
64
65         i915_kernel_lost_context(dev);
66
67         DRM_DEBUG("%s\n", __FUNCTION__);
68
69         ret = dev_priv->counter;
70
71         BEGIN_LP_RING(2);
72         OUT_RING(0);
73         OUT_RING(GFX_OP_USER_INTERRUPT);
74         ADVANCE_LP_RING();
75
76         return ret;
77 }
78
79 static int i915_wait_irq(drm_device_t * dev, int irq_nr)
80 {
81         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
82         int ret = 0;
83
84         DRM_DEBUG("%s irq_nr=%d breadcrumb=%d\n", __FUNCTION__, irq_nr,
85                   READ_BREADCRUMB(dev_priv));
86
87         if (READ_BREADCRUMB(dev_priv) >= irq_nr)
88                 return 0;
89
90         dev_priv->sarea_priv->perf_boxes |= I915_BOX_WAIT;
91
92         DRM_WAIT_ON(ret, dev_priv->irq_queue, 3 * DRM_HZ,
93                     READ_BREADCRUMB(dev_priv) >= irq_nr);
94
95         if (ret == DRM_ERR(EBUSY)) {
96                 DRM_ERROR("%s: EBUSY -- rec: %d emitted: %d\n",
97                           __FUNCTION__,
98                           READ_BREADCRUMB(dev_priv), (int)dev_priv->counter);
99         }
100
101         dev_priv->sarea_priv->last_dispatch = READ_BREADCRUMB(dev_priv);
102         return ret;
103 }
104
105 /* Needs the lock as it touches the ring.
106  */
107 int i915_irq_emit(DRM_IOCTL_ARGS)
108 {
109         DRM_DEVICE;
110         drm_i915_private_t *dev_priv = dev->dev_private;
111         drm_i915_irq_emit_t emit;
112         int result;
113
114         LOCK_TEST_WITH_RETURN(dev, filp);
115
116         if (!dev_priv) {
117                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
118                 return DRM_ERR(EINVAL);
119         }
120
121         DRM_COPY_FROM_USER_IOCTL(emit, (drm_i915_irq_emit_t __user *) data,
122                                  sizeof(emit));
123
124         result = i915_emit_irq(dev);
125
126         if (DRM_COPY_TO_USER(emit.irq_seq, &result, sizeof(int))) {
127                 DRM_ERROR("copy_to_user\n");
128                 return DRM_ERR(EFAULT);
129         }
130
131         return 0;
132 }
133
134 /* Doesn't need the hardware lock.
135  */
136 int i915_irq_wait(DRM_IOCTL_ARGS)
137 {
138         DRM_DEVICE;
139         drm_i915_private_t *dev_priv = dev->dev_private;
140         drm_i915_irq_wait_t irqwait;
141
142         if (!dev_priv) {
143                 DRM_ERROR("%s called with no initialization\n", __FUNCTION__);
144                 return DRM_ERR(EINVAL);
145         }
146
147         DRM_COPY_FROM_USER_IOCTL(irqwait, (drm_i915_irq_wait_t __user *) data,
148                                  sizeof(irqwait));
149
150         return i915_wait_irq(dev, irqwait.irq_seq);
151 }
152
153 /* drm_dma.h hooks
154 */
155 void i915_driver_irq_preinstall(drm_device_t * dev)
156 {
157         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
158
159         I915_WRITE16(I915REG_HWSTAM, 0xfffe);
160         I915_WRITE16(I915REG_INT_MASK_R, 0x0);
161         I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
162 }
163
164 void i915_driver_irq_postinstall(drm_device_t * dev)
165 {
166         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
167
168         I915_WRITE16(I915REG_INT_ENABLE_R, USER_INT_FLAG);
169         DRM_INIT_WAITQUEUE(&dev_priv->irq_queue);
170 }
171
172 void i915_driver_irq_uninstall(drm_device_t * dev)
173 {
174         drm_i915_private_t *dev_priv = (drm_i915_private_t *) dev->dev_private;
175         if (!dev_priv)
176                 return;
177
178         I915_WRITE16(I915REG_HWSTAM, 0xffff);
179         I915_WRITE16(I915REG_INT_MASK_R, 0xffff);
180         I915_WRITE16(I915REG_INT_ENABLE_R, 0x0);
181 }