nrelease - fix/improve livecd
[dragonfly.git] / sys / dev / drm / drm_irq.c
CommitLineData
20c04ff7
FT
1/*
2 * drm_irq.c IRQ and vblank support
782e40d3
FT
3 *
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
3f2dd94a
FT
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the next
15 * paragraph) shall be included in all copies or substantial portions of the
16 * Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
782e40d3
FT
25 */
26
27/*
28 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
29 *
30 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
31 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7f3c3d6f
HT
32 * All Rights Reserved.
33 *
34 * Permission is hereby granted, free of charge, to any person obtaining a
35 * copy of this software and associated documentation files (the "Software"),
36 * to deal in the Software without restriction, including without limitation
37 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38 * and/or sell copies of the Software, and to permit persons to whom the
39 * Software is furnished to do so, subject to the following conditions:
40 *
41 * The above copyright notice and this permission notice (including the next
42 * paragraph) shall be included in all copies or substantial portions of the
43 * Software.
44 *
45 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
782e40d3
FT
48 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
49 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
50 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
51 * OTHER DEALINGS IN THE SOFTWARE.
7f3c3d6f
HT
52 */
53
3f2dd94a 54#include <drm/drm_irq.h>
18e26a6d 55#include <drm/drmP.h>
7f3c3d6f 56
183e2373 57#include <linux/interrupt.h> /* For task queue support */
ba55f2f5 58
183e2373 59#include <linux/vgaarb.h>
ba55f2f5
FT
60#include <linux/export.h>
61
3f2dd94a 62#include "drm_internal.h"
5718399f 63
20c04ff7 64/**
3f2dd94a 65 * DOC: irq helpers
20c04ff7 66 *
3f2dd94a
FT
67 * The DRM core provides very simple support helpers to enable IRQ handling on a
68 * device through the drm_irq_install() and drm_irq_uninstall() functions. This
69 * only supports devices with a single interrupt on the main device stored in
70 * &drm_device.dev and set as the device paramter in drm_dev_alloc().
20c04ff7 71 *
3f2dd94a
FT
72 * These IRQ helpers are strictly optional. Drivers which roll their own only
73 * need to set &drm_device.irq_enabled to signal the DRM core that vblank
74 * interrupts are working. Since these helpers don't automatically clean up the
75 * requested interrupt like e.g. devm_request_irq() they're not really
76 * recommended.
20c04ff7 77 */
5718399f 78
ea2ce9e4 79/**
20c04ff7
FT
80 * drm_irq_install - install IRQ handler
81 * @dev: DRM device
82 * @irq: IRQ number to install the handler for
ea2ce9e4
FT
83 *
84 * Initializes the IRQ related data. Installs the handler, calling the driver
3f2dd94a
FT
85 * &drm_driver.irq_preinstall and &drm_driver.irq_postinstall functions before
86 * and after the installation.
20c04ff7
FT
87 *
88 * This is the simplified helper interface provided for drivers with no special
89 * needs. Drivers which need to install interrupt handlers for multiple
a85cb24f 90 * interrupts must instead set &drm_device.irq_enabled to signal the DRM core
20c04ff7
FT
91 * that vblank interrupts are available.
92 *
3f2dd94a
FT
93 * @irq must match the interrupt number that would be passed to request_irq(),
94 * if called directly instead of using this helper function.
95 *
96 * &drm_driver.irq_handler is called to handle the registered interrupt.
97 *
20c04ff7
FT
98 * Returns:
99 * Zero on success or a negative error code on failure.
ea2ce9e4 100 */
ba55f2f5 101int drm_irq_install(struct drm_device *dev, int irq)
03ad0861 102{
ea2ce9e4 103 int ret;
183e2373 104 unsigned long sh_flags = 0;
03ad0861 105
ea2ce9e4
FT
106 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
107 return -EINVAL;
03ad0861 108
ba55f2f5 109 if (irq == 0)
ea2ce9e4 110 return -EINVAL;
03ad0861 111
ea2ce9e4 112 /* Driver must have been initialized */
ba55f2f5 113 if (!dev->dev_private)
ea2ce9e4 114 return -EINVAL;
ea2ce9e4 115
ba55f2f5 116 if (dev->irq_enabled)
ea2ce9e4 117 return -EBUSY;
20c04ff7 118 dev->irq_enabled = true;
03ad0861 119
ba55f2f5 120 DRM_DEBUG("irq=%d\n", irq);
03ad0861
FT
121
122 /* Before installing handler */
123 if (dev->driver->irq_preinstall)
124 dev->driver->irq_preinstall(dev);
03ad0861
FT
125
126 /* Install handler */
183e2373
FT
127 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
128 sh_flags = IRQF_SHARED;
ea2ce9e4 129
183e2373
FT
130 ret = request_irq(irq, dev->driver->irq_handler,
131 sh_flags, dev->driver->name, dev);
132
133 if (ret < 0) {
20c04ff7 134 dev->irq_enabled = false;
ea2ce9e4
FT
135 return ret;
136 }
03ad0861
FT
137
138 /* After installing handler */
03ad0861 139 if (dev->driver->irq_postinstall)
ea2ce9e4 140 ret = dev->driver->irq_postinstall(dev);
03ad0861 141
ea2ce9e4 142 if (ret < 0) {
20c04ff7 143 dev->irq_enabled = false;
3f2dd94a
FT
144#if 0
145 if (drm_core_check_feature(dev, DRIVER_LEGACY))
146 vga_client_register(dev->pdev, NULL, NULL, NULL);
147#endif
183e2373 148 free_irq(irq, dev);
ba55f2f5
FT
149 } else {
150 dev->irq = irq;
ea2ce9e4 151 }
03ad0861 152
ea2ce9e4 153 return ret;
03ad0861 154}
ea2ce9e4 155EXPORT_SYMBOL(drm_irq_install);
03ad0861 156
ea2ce9e4 157/**
20c04ff7
FT
158 * drm_irq_uninstall - uninstall the IRQ handler
159 * @dev: DRM device
160 *
3f2dd94a
FT
161 * Calls the driver's &drm_driver.irq_uninstall function and unregisters the IRQ
162 * handler. This should only be called by drivers which used drm_irq_install()
163 * to set up their interrupt handler. Other drivers must only reset
a85cb24f 164 * &drm_device.irq_enabled to false.
ea2ce9e4 165 *
20c04ff7
FT
166 * Note that for kernel modesetting drivers it is a bug if this function fails.
167 * The sanity checks are only to catch buggy user modesetting drivers which call
168 * the same function through an ioctl.
ea2ce9e4 169 *
20c04ff7
FT
170 * Returns:
171 * Zero on success or a negative error code on failure.
ea2ce9e4 172 */
03ad0861
FT
173int drm_irq_uninstall(struct drm_device *dev)
174{
93fbfa9a 175 unsigned long irqflags;
782e40d3
FT
176 bool irq_enabled;
177 int i;
03ad0861 178
ea2ce9e4
FT
179 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
180 return -EINVAL;
03ad0861 181
ea2ce9e4 182 irq_enabled = dev->irq_enabled;
782e40d3 183 dev->irq_enabled = false;
03ad0861
FT
184
185 /*
19c468b4 186 * Wake up any waiters so they don't hang. This is just to paper over
1dedbd3b 187 * issues for UMS drivers which aren't in full control of their
19c468b4
FT
188 * vblank/irq handling. KMS drivers must ensure that vblanks are all
189 * disabled when uninstalling the irq handler.
ea2ce9e4 190 */
03ad0861 191 if (dev->num_crtcs) {
93fbfa9a 192 spin_lock_irqsave(&dev->vbl_lock, irqflags);
03ad0861 193 for (i = 0; i < dev->num_crtcs; i++) {
20c04ff7
FT
194 struct drm_vblank_crtc *vblank = &dev->vblank[i];
195
19c468b4
FT
196 if (!vblank->enabled)
197 continue;
198
199 WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
200
3f2dd94a 201 drm_vblank_disable_and_save(dev, i);
20c04ff7 202 wake_up(&vblank->queue);
03ad0861 203 }
93fbfa9a 204 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
03ad0861
FT
205 }
206
ea2ce9e4
FT
207 if (!irq_enabled)
208 return -EINVAL;
209
03ad0861
FT
210 DRM_DEBUG("irq=%d\n", dev->irq);
211
a85cb24f
FT
212#if 0
213 if (drm_core_check_feature(dev, DRIVER_LEGACY))
214 vga_client_register(dev->pdev, NULL, NULL, NULL);
215#endif
216
03ad0861
FT
217 if (dev->driver->irq_uninstall)
218 dev->driver->irq_uninstall(dev);
219
183e2373 220 free_irq(dev->irq, dev);
03ad0861
FT
221
222 return 0;
223}
ea2ce9e4 224EXPORT_SYMBOL(drm_irq_uninstall);
03ad0861 225
a85cb24f
FT
226int drm_legacy_irq_control(struct drm_device *dev, void *data,
227 struct drm_file *file_priv)
03ad0861
FT
228{
229 struct drm_control *ctl = data;
ba55f2f5 230 int ret = 0, irq;
ea2ce9e4
FT
231
232 /* if we haven't irq we fallback for compatibility reasons -
233 * this used to be a separate function in drm_dma.h
234 */
235
ba55f2f5
FT
236 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
237 return 0;
1dedbd3b 238 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
ba55f2f5 239 return 0;
1dedbd3b 240 /* UMS was only ever supported on pci devices. */
ba55f2f5
FT
241 if (WARN_ON(!dev->pdev))
242 return -EINVAL;
03ad0861
FT
243
244 switch (ctl->func) {
245 case DRM_INST_HANDLER:
d4d73b30 246 irq = dev->pdev->irq;
ba55f2f5 247
03ad0861 248 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
ba55f2f5 249 ctl->irq != irq)
ea2ce9e4 250 return -EINVAL;
ba55f2f5
FT
251 mutex_lock(&dev->struct_mutex);
252 ret = drm_irq_install(dev, irq);
253 mutex_unlock(&dev->struct_mutex);
254
255 return ret;
03ad0861 256 case DRM_UNINST_HANDLER:
ba55f2f5
FT
257 mutex_lock(&dev->struct_mutex);
258 ret = drm_irq_uninstall(dev);
259 mutex_unlock(&dev->struct_mutex);
260
261 return ret;
03ad0861 262 default:
ea2ce9e4 263 return -EINVAL;
03ad0861
FT
264 }
265}