drm/amd/display: Add plane capabilities to dc_caps
[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
3ed4351a 54#include <drm/drm_irq.h>
760285e7 55#include <drm/drmP.h>
1da177e4
LT
56
57#include <linux/interrupt.h> /* For task queue support */
58
28d52043 59#include <linux/vgaarb.h>
2d1a8a48 60#include <linux/export.h>
27641c3f 61
3ed4351a 62#include "drm_internal.h"
0a3e67a4 63
16584b20
DV
64/**
65 * DOC: irq helpers
66 *
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().
71 *
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.
77 */
78
1da177e4 79/**
f5752b38
DV
80 * drm_irq_install - install IRQ handler
81 * @dev: DRM device
82 * @irq: IRQ number to install the handler for
1da177e4 83 *
0a3e67a4 84 * Initializes the IRQ related data. Installs the handler, calling the driver
16584b20
DV
85 * &drm_driver.irq_preinstall and &drm_driver.irq_postinstall functions before
86 * and after the installation.
f5752b38
DV
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
ef40cbf9 90 * interrupts must instead set &drm_device.irq_enabled to signal the DRM core
f5752b38
DV
91 * that vblank interrupts are available.
92 *
16584b20
DV
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 *
f5752b38
DV
98 * Returns:
99 * Zero on success or a negative error code on failure.
1da177e4 100 */
bb0f1b5c 101int drm_irq_install(struct drm_device *dev, int irq)
1da177e4 102{
4a1b0714 103 int ret;
b5e89ed5 104 unsigned long sh_flags = 0;
1da177e4 105
7c1a38e3 106 if (irq == 0)
1da177e4
LT
107 return -EINVAL;
108
1da177e4 109 /* Driver must have been initialized */
e090c53b 110 if (!dev->dev_private)
1da177e4 111 return -EINVAL;
1da177e4 112
e090c53b 113 if (dev->irq_enabled)
1da177e4 114 return -EBUSY;
4423843c 115 dev->irq_enabled = true;
1da177e4 116
7c1a38e3 117 DRM_DEBUG("irq=%d\n", irq);
1da177e4 118
b5e89ed5 119 /* Before installing handler */
5037f8ac
JS
120 if (dev->driver->irq_preinstall)
121 dev->driver->irq_preinstall(dev);
1da177e4 122
1ff49481
DV
123 /* PCI devices require shared interrupts. */
124 if (dev->pdev)
935f6e3a 125 sh_flags = IRQF_SHARED;
b5e89ed5 126
7c1a38e3 127 ret = request_irq(irq, dev->driver->irq_handler,
5829d183 128 sh_flags, dev->driver->name, dev);
9bfbd5cb 129
b5e89ed5 130 if (ret < 0) {
4423843c 131 dev->irq_enabled = false;
1da177e4
LT
132 return ret;
133 }
134
b5e89ed5 135 /* After installing handler */
5037f8ac
JS
136 if (dev->driver->irq_postinstall)
137 ret = dev->driver->irq_postinstall(dev);
138
0a3e67a4 139 if (ret < 0) {
4423843c 140 dev->irq_enabled = false;
fa538645 141 if (drm_core_check_feature(dev, DRIVER_LEGACY))
e1c44acc 142 vga_client_register(dev->pdev, NULL, NULL, NULL);
7c1a38e3
DV
143 free_irq(irq, dev);
144 } else {
145 dev->irq = irq;
0a3e67a4 146 }
1da177e4 147
0a3e67a4 148 return ret;
1da177e4 149}
0a3e67a4 150EXPORT_SYMBOL(drm_irq_install);
1da177e4
LT
151
152/**
f5752b38
DV
153 * drm_irq_uninstall - uninstall the IRQ handler
154 * @dev: DRM device
155 *
16584b20
DV
156 * Calls the driver's &drm_driver.irq_uninstall function and unregisters the IRQ
157 * handler. This should only be called by drivers which used drm_irq_install()
158 * to set up their interrupt handler. Other drivers must only reset
ef40cbf9 159 * &drm_device.irq_enabled to false.
1da177e4 160 *
f5752b38
DV
161 * Note that for kernel modesetting drivers it is a bug if this function fails.
162 * The sanity checks are only to catch buggy user modesetting drivers which call
163 * the same function through an ioctl.
1da177e4 164 *
f5752b38
DV
165 * Returns:
166 * Zero on success or a negative error code on failure.
1da177e4 167 */
27641c3f 168int drm_irq_uninstall(struct drm_device *dev)
1da177e4 169{
dc1336ff 170 unsigned long irqflags;
4423843c
VS
171 bool irq_enabled;
172 int i;
1da177e4 173
1da177e4 174 irq_enabled = dev->irq_enabled;
4423843c 175 dev->irq_enabled = false;
1da177e4 176
dc1336ff 177 /*
3bff93d6 178 * Wake up any waiters so they don't hang. This is just to paper over
c78d1ca2 179 * issues for UMS drivers which aren't in full control of their
3bff93d6
DV
180 * vblank/irq handling. KMS drivers must ensure that vblanks are all
181 * disabled when uninstalling the irq handler.
dc1336ff 182 */
bde4889a
BS
183 if (dev->num_crtcs) {
184 spin_lock_irqsave(&dev->vbl_lock, irqflags);
185 for (i = 0; i < dev->num_crtcs; i++) {
8a51d5be
VS
186 struct drm_vblank_crtc *vblank = &dev->vblank[i];
187
3bff93d6
DV
188 if (!vblank->enabled)
189 continue;
190
191 WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
192
3ed4351a 193 drm_vblank_disable_and_save(dev, i);
8a51d5be 194 wake_up(&vblank->queue);
bde4889a
BS
195 }
196 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
dc1336ff 197 }
dc1336ff 198
b5e89ed5 199 if (!irq_enabled)
1da177e4
LT
200 return -EINVAL;
201
7c1a38e3 202 DRM_DEBUG("irq=%d\n", dev->irq);
1da177e4 203
fa538645 204 if (drm_core_check_feature(dev, DRIVER_LEGACY))
28d52043
DA
205 vga_client_register(dev->pdev, NULL, NULL, NULL);
206
5037f8ac
JS
207 if (dev->driver->irq_uninstall)
208 dev->driver->irq_uninstall(dev);
1da177e4 209
7c1a38e3 210 free_irq(dev->irq, dev);
1da177e4
LT
211
212 return 0;
213}
214EXPORT_SYMBOL(drm_irq_uninstall);
215
25a9939c
DV
216int drm_legacy_irq_control(struct drm_device *dev, void *data,
217 struct drm_file *file_priv)
1da177e4 218{
c153f45f 219 struct drm_control *ctl = data;
a319c1a4 220 int ret = 0, irq;
b5e89ed5 221
27641c3f
MK
222 /* if we haven't irq we fallback for compatibility reasons -
223 * this used to be a separate function in drm_dma.h
224 */
1da177e4 225
22471cf6
DV
226 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
227 return 0;
fa538645 228 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
22471cf6 229 return 0;
c78d1ca2 230 /* UMS was only ever supported on pci devices. */
22471cf6
DV
231 if (WARN_ON(!dev->pdev))
232 return -EINVAL;
1da177e4 233
c153f45f 234 switch (ctl->func) {
1da177e4 235 case DRM_INST_HANDLER:
a319c1a4
DV
236 irq = dev->pdev->irq;
237
1da177e4 238 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
a319c1a4 239 ctl->irq != irq)
1da177e4 240 return -EINVAL;
e090c53b 241 mutex_lock(&dev->struct_mutex);
bb0f1b5c 242 ret = drm_irq_install(dev, irq);
e090c53b
DV
243 mutex_unlock(&dev->struct_mutex);
244
245 return ret;
1da177e4 246 case DRM_UNINST_HANDLER:
e090c53b
DV
247 mutex_lock(&dev->struct_mutex);
248 ret = drm_irq_uninstall(dev);
249 mutex_unlock(&dev->struct_mutex);
250
251 return ret;
1da177e4
LT
252 default:
253 return -EINVAL;
254 }
255}