Merge tag 'for-linus-5.11-rc5-tag' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / gpu / drm / drm_irq.c
CommitLineData
f5752b38
DV
1/*
2 * drm_irq.c IRQ and vblank support
1da177e4
LT
3 *
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
3ed4351a
DV
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.
1da177e4
LT
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.
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
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.
52 */
53
1da177e4 54
0500c04e 55#include <linux/export.h>
1da177e4 56#include <linux/interrupt.h> /* For task queue support */
0500c04e 57#include <linux/pci.h>
28d52043 58#include <linux/vgaarb.h>
0500c04e
SR
59
60#include <drm/drm.h>
61#include <drm/drm_device.h>
62#include <drm/drm_drv.h>
63#include <drm/drm_irq.h>
64#include <drm/drm_print.h>
65#include <drm/drm_vblank.h>
27641c3f 66
3ed4351a 67#include "drm_internal.h"
0a3e67a4 68
16584b20
DV
69/**
70 * DOC: irq helpers
71 *
72 * The DRM core provides very simple support helpers to enable IRQ handling on a
73 * device through the drm_irq_install() and drm_irq_uninstall() functions. This
74 * only supports devices with a single interrupt on the main device stored in
75 * &drm_device.dev and set as the device paramter in drm_dev_alloc().
76 *
77 * These IRQ helpers are strictly optional. Drivers which roll their own only
78 * need to set &drm_device.irq_enabled to signal the DRM core that vblank
79 * interrupts are working. Since these helpers don't automatically clean up the
80 * requested interrupt like e.g. devm_request_irq() they're not really
81 * recommended.
82 */
83
1da177e4 84/**
f5752b38
DV
85 * drm_irq_install - install IRQ handler
86 * @dev: DRM device
87 * @irq: IRQ number to install the handler for
1da177e4 88 *
0a3e67a4 89 * Initializes the IRQ related data. Installs the handler, calling the driver
16584b20
DV
90 * &drm_driver.irq_preinstall and &drm_driver.irq_postinstall functions before
91 * and after the installation.
f5752b38
DV
92 *
93 * This is the simplified helper interface provided for drivers with no special
94 * needs. Drivers which need to install interrupt handlers for multiple
ef40cbf9 95 * interrupts must instead set &drm_device.irq_enabled to signal the DRM core
f5752b38
DV
96 * that vblank interrupts are available.
97 *
16584b20
DV
98 * @irq must match the interrupt number that would be passed to request_irq(),
99 * if called directly instead of using this helper function.
100 *
101 * &drm_driver.irq_handler is called to handle the registered interrupt.
102 *
f5752b38
DV
103 * Returns:
104 * Zero on success or a negative error code on failure.
1da177e4 105 */
bb0f1b5c 106int drm_irq_install(struct drm_device *dev, int irq)
1da177e4 107{
4a1b0714 108 int ret;
b5e89ed5 109 unsigned long sh_flags = 0;
1da177e4 110
7c1a38e3 111 if (irq == 0)
1da177e4
LT
112 return -EINVAL;
113
e090c53b 114 if (dev->irq_enabled)
1da177e4 115 return -EBUSY;
4423843c 116 dev->irq_enabled = true;
1da177e4 117
7c1a38e3 118 DRM_DEBUG("irq=%d\n", irq);
1da177e4 119
b5e89ed5 120 /* Before installing handler */
5037f8ac
JS
121 if (dev->driver->irq_preinstall)
122 dev->driver->irq_preinstall(dev);
1da177e4 123
1ff49481
DV
124 /* PCI devices require shared interrupts. */
125 if (dev->pdev)
935f6e3a 126 sh_flags = IRQF_SHARED;
b5e89ed5 127
7c1a38e3 128 ret = request_irq(irq, dev->driver->irq_handler,
5829d183 129 sh_flags, dev->driver->name, dev);
9bfbd5cb 130
b5e89ed5 131 if (ret < 0) {
4423843c 132 dev->irq_enabled = false;
1da177e4
LT
133 return ret;
134 }
135
b5e89ed5 136 /* After installing handler */
5037f8ac
JS
137 if (dev->driver->irq_postinstall)
138 ret = dev->driver->irq_postinstall(dev);
139
0a3e67a4 140 if (ret < 0) {
4423843c 141 dev->irq_enabled = false;
fa538645 142 if (drm_core_check_feature(dev, DRIVER_LEGACY))
e1c44acc 143 vga_client_register(dev->pdev, NULL, NULL, NULL);
7c1a38e3
DV
144 free_irq(irq, dev);
145 } else {
146 dev->irq = irq;
0a3e67a4 147 }
1da177e4 148
0a3e67a4 149 return ret;
1da177e4 150}
0a3e67a4 151EXPORT_SYMBOL(drm_irq_install);
1da177e4
LT
152
153/**
f5752b38
DV
154 * drm_irq_uninstall - uninstall the IRQ handler
155 * @dev: DRM device
156 *
16584b20
DV
157 * Calls the driver's &drm_driver.irq_uninstall function and unregisters the IRQ
158 * handler. This should only be called by drivers which used drm_irq_install()
159 * to set up their interrupt handler. Other drivers must only reset
ef40cbf9 160 * &drm_device.irq_enabled to false.
1da177e4 161 *
f5752b38
DV
162 * Note that for kernel modesetting drivers it is a bug if this function fails.
163 * The sanity checks are only to catch buggy user modesetting drivers which call
164 * the same function through an ioctl.
1da177e4 165 *
f5752b38
DV
166 * Returns:
167 * Zero on success or a negative error code on failure.
1da177e4 168 */
27641c3f 169int drm_irq_uninstall(struct drm_device *dev)
1da177e4 170{
dc1336ff 171 unsigned long irqflags;
4423843c
VS
172 bool irq_enabled;
173 int i;
1da177e4 174
1da177e4 175 irq_enabled = dev->irq_enabled;
4423843c 176 dev->irq_enabled = false;
1da177e4 177
dc1336ff 178 /*
3bff93d6 179 * Wake up any waiters so they don't hang. This is just to paper over
c78d1ca2 180 * issues for UMS drivers which aren't in full control of their
3bff93d6
DV
181 * vblank/irq handling. KMS drivers must ensure that vblanks are all
182 * disabled when uninstalling the irq handler.
dc1336ff 183 */
6015002e 184 if (drm_dev_has_vblank(dev)) {
bde4889a
BS
185 spin_lock_irqsave(&dev->vbl_lock, irqflags);
186 for (i = 0; i < dev->num_crtcs; i++) {
8a51d5be
VS
187 struct drm_vblank_crtc *vblank = &dev->vblank[i];
188
3bff93d6
DV
189 if (!vblank->enabled)
190 continue;
191
192 WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
193
3ed4351a 194 drm_vblank_disable_and_save(dev, i);
8a51d5be 195 wake_up(&vblank->queue);
bde4889a
BS
196 }
197 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
dc1336ff 198 }
dc1336ff 199
b5e89ed5 200 if (!irq_enabled)
1da177e4
LT
201 return -EINVAL;
202
7c1a38e3 203 DRM_DEBUG("irq=%d\n", dev->irq);
1da177e4 204
fa538645 205 if (drm_core_check_feature(dev, DRIVER_LEGACY))
28d52043
DA
206 vga_client_register(dev->pdev, NULL, NULL, NULL);
207
5037f8ac
JS
208 if (dev->driver->irq_uninstall)
209 dev->driver->irq_uninstall(dev);
1da177e4 210
7c1a38e3 211 free_irq(dev->irq, dev);
1da177e4
LT
212
213 return 0;
214}
215EXPORT_SYMBOL(drm_irq_uninstall);
216
61ae2270 217#if IS_ENABLED(CONFIG_DRM_LEGACY)
25a9939c
DV
218int drm_legacy_irq_control(struct drm_device *dev, void *data,
219 struct drm_file *file_priv)
1da177e4 220{
c153f45f 221 struct drm_control *ctl = data;
a319c1a4 222 int ret = 0, irq;
b5e89ed5 223
27641c3f
MK
224 /* if we haven't irq we fallback for compatibility reasons -
225 * this used to be a separate function in drm_dma.h
226 */
1da177e4 227
22471cf6
DV
228 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
229 return 0;
fa538645 230 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
22471cf6 231 return 0;
c78d1ca2 232 /* UMS was only ever supported on pci devices. */
22471cf6
DV
233 if (WARN_ON(!dev->pdev))
234 return -EINVAL;
1da177e4 235
c153f45f 236 switch (ctl->func) {
1da177e4 237 case DRM_INST_HANDLER:
a319c1a4
DV
238 irq = dev->pdev->irq;
239
1da177e4 240 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
a319c1a4 241 ctl->irq != irq)
1da177e4 242 return -EINVAL;
e090c53b 243 mutex_lock(&dev->struct_mutex);
bb0f1b5c 244 ret = drm_irq_install(dev, irq);
e090c53b
DV
245 mutex_unlock(&dev->struct_mutex);
246
247 return ret;
1da177e4 248 case DRM_UNINST_HANDLER:
e090c53b
DV
249 mutex_lock(&dev->struct_mutex);
250 ret = drm_irq_uninstall(dev);
251 mutex_unlock(&dev->struct_mutex);
252
253 return ret;
1da177e4
LT
254 default:
255 return -EINVAL;
256 }
257}
61ae2270 258#endif