blob: 02f38cc9f4682b12f727c0d96ad6706b971c0ef0 [file] [log] [blame]
Daniel Vetterf5752b32014-05-08 16:41:51 +02001/*
2 * drm_irq.c IRQ and vblank support
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
Daniel Vetter3ed43512017-05-31 11:21:46 +02006 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 */
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
Daniel Vetter3ed43512017-05-31 11:21:46 +020054#include <drm/drm_irq.h>
David Howells760285e2012-10-02 18:01:07 +010055#include <drm/drmP.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57#include <linux/interrupt.h> /* For task queue support */
58
Dave Airlie28d52042009-09-21 14:33:58 +100059#include <linux/vgaarb.h>
Paul Gortmaker2d1a8a42011-08-30 18:16:33 -040060#include <linux/export.h>
Mario Kleiner27641c32010-10-23 04:20:23 +020061
Daniel Vetter3ed43512017-05-31 11:21:46 +020062#include "drm_internal.h"
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/**
Daniel Vetter16584b22017-05-31 11:22:53 +020065 * 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
79/**
Daniel Vetterf5752b32014-05-08 16:41:51 +020080 * drm_irq_install - install IRQ handler
81 * @dev: DRM device
82 * @irq: IRQ number to install the handler for
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 *
Jesse Barnes0a3e67a2008-09-30 12:14:26 -070084 * Initializes the IRQ related data. Installs the handler, calling the driver
Daniel Vetter16584b22017-05-31 11:22:53 +020085 * &drm_driver.irq_preinstall and &drm_driver.irq_postinstall functions before
86 * and after the installation.
Daniel Vetterf5752b32014-05-08 16:41:51 +020087 *
88 * This is the simplified helper interface provided for drivers with no special
89 * needs. Drivers which need to install interrupt handlers for multiple
Daniel Vetteref40cbf92017-01-25 07:26:47 +010090 * interrupts must instead set &drm_device.irq_enabled to signal the DRM core
Daniel Vetterf5752b32014-05-08 16:41:51 +020091 * that vblank interrupts are available.
92 *
Daniel Vetter16584b22017-05-31 11:22:53 +020093 * @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 *
Daniel Vetterf5752b32014-05-08 16:41:51 +020098 * Returns:
99 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 */
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100101int drm_irq_install(struct drm_device *dev, int irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Laurent Pinchart4a1b0712012-05-17 13:27:21 +0200103 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000104 unsigned long sh_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100106 if (irq == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return -EINVAL;
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 /* Driver must have been initialized */
Daniel Vettere090c532013-11-03 20:27:05 +0100110 if (!dev->dev_private)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Daniel Vettere090c532013-11-03 20:27:05 +0100113 if (dev->irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EBUSY;
Ville Syrjälä44238432013-10-04 14:53:37 +0300115 dev->irq_enabled = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100117 DRM_DEBUG("irq=%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000119 /* Before installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000120 if (dev->driver->irq_preinstall)
121 dev->driver->irq_preinstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Daniel Vetter1ff49482019-01-29 11:42:48 +0100123 /* PCI devices require shared interrupts. */
124 if (dev->pdev)
Thomas Gleixner935f6e32006-07-01 19:29:34 -0700125 sh_flags = IRQF_SHARED;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000126
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100127 ret = request_irq(irq, dev->driver->irq_handler,
Daniel Vetter5829d182013-11-03 21:48:48 +0100128 sh_flags, dev->driver->name, dev);
Jesse Barnes9bfbd5c2008-09-15 15:00:33 -0700129
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000130 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300131 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return ret;
133 }
134
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000135 /* After installing handler */
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000136 if (dev->driver->irq_postinstall)
137 ret = dev->driver->irq_postinstall(dev);
138
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700139 if (ret < 0) {
Ville Syrjälä44238432013-10-04 14:53:37 +0300140 dev->irq_enabled = false;
Daniel Vetterfa538642016-08-03 21:11:10 +0200141 if (drm_core_check_feature(dev, DRIVER_LEGACY))
Joonyoung Shime1c44ac2011-08-04 05:41:00 +0000142 vga_client_register(dev->pdev, NULL, NULL, NULL);
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100143 free_irq(irq, dev);
144 } else {
145 dev->irq = irq;
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700148 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700150EXPORT_SYMBOL(drm_irq_install);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
152/**
Daniel Vetterf5752b32014-05-08 16:41:51 +0200153 * drm_irq_uninstall - uninstall the IRQ handler
154 * @dev: DRM device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 *
Daniel Vetter16584b22017-05-31 11:22:53 +0200156 * 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
Daniel Vetteref40cbf92017-01-25 07:26:47 +0100159 * &drm_device.irq_enabled to false.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 *
Daniel Vetterf5752b32014-05-08 16:41:51 +0200161 * 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.
164 *
165 * Returns:
166 * Zero on success or a negative error code on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 */
Mario Kleiner27641c32010-10-23 04:20:23 +0200168int drm_irq_uninstall(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800170 unsigned long irqflags;
Ville Syrjälä44238432013-10-04 14:53:37 +0300171 bool irq_enabled;
172 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 irq_enabled = dev->irq_enabled;
Ville Syrjälä44238432013-10-04 14:53:37 +0300175 dev->irq_enabled = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800177 /*
Daniel Vetter3bff93d2015-02-22 15:11:19 +0100178 * Wake up any waiters so they don't hang. This is just to paper over
Frank Binnsc78d1ca2016-06-24 11:15:37 +0100179 * issues for UMS drivers which aren't in full control of their
Daniel Vetter3bff93d2015-02-22 15:11:19 +0100180 * vblank/irq handling. KMS drivers must ensure that vblanks are all
181 * disabled when uninstalling the irq handler.
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800182 */
Ben Skeggsbde48892011-07-04 12:52:27 +1000183 if (dev->num_crtcs) {
184 spin_lock_irqsave(&dev->vbl_lock, irqflags);
185 for (i = 0; i < dev->num_crtcs; i++) {
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300186 struct drm_vblank_crtc *vblank = &dev->vblank[i];
187
Daniel Vetter3bff93d2015-02-22 15:11:19 +0100188 if (!vblank->enabled)
189 continue;
190
191 WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
192
Daniel Vetter3ed43512017-05-31 11:21:46 +0200193 drm_vblank_disable_and_save(dev, i);
Ville Syrjälä8a51d5b2014-08-06 14:49:50 +0300194 wake_up(&vblank->queue);
Ben Skeggsbde48892011-07-04 12:52:27 +1000195 }
196 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800197 }
Jesse Barnesdc1336f2009-01-06 10:21:24 -0800198
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000199 if (!irq_enabled)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return -EINVAL;
201
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100202 DRM_DEBUG("irq=%d\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Daniel Vetterfa538642016-08-03 21:11:10 +0200204 if (drm_core_check_feature(dev, DRIVER_LEGACY))
Dave Airlie28d52042009-09-21 14:33:58 +1000205 vga_client_register(dev->pdev, NULL, NULL, NULL);
206
Joonyoung Shim5037f8a2011-08-04 05:41:01 +0000207 if (dev->driver->irq_uninstall)
208 dev->driver->irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Daniel Vetter7c1a38e2013-12-16 11:21:15 +0100210 free_irq(dev->irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 return 0;
213}
214EXPORT_SYMBOL(drm_irq_uninstall);
215
Dave Airlie61ae2272019-04-18 17:10:40 +1000216#if IS_ENABLED(CONFIG_DRM_LEGACY)
Daniel Vetter25a99392016-12-14 00:08:02 +0100217int drm_legacy_irq_control(struct drm_device *dev, void *data,
218 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
Eric Anholtc153f452007-09-03 12:06:45 +1000220 struct drm_control *ctl = data;
Daniel Vettera319c1a2013-11-03 21:09:15 +0100221 int ret = 0, irq;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000222
Mario Kleiner27641c32010-10-23 04:20:23 +0200223 /* if we haven't irq we fallback for compatibility reasons -
224 * this used to be a separate function in drm_dma.h
225 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Daniel Vetter22471cf2013-11-03 20:02:50 +0100227 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
228 return 0;
Daniel Vetterfa538642016-08-03 21:11:10 +0200229 if (!drm_core_check_feature(dev, DRIVER_LEGACY))
Daniel Vetter22471cf2013-11-03 20:02:50 +0100230 return 0;
Frank Binnsc78d1ca2016-06-24 11:15:37 +0100231 /* UMS was only ever supported on pci devices. */
Daniel Vetter22471cf2013-11-03 20:02:50 +0100232 if (WARN_ON(!dev->pdev))
233 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Eric Anholtc153f452007-09-03 12:06:45 +1000235 switch (ctl->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 case DRM_INST_HANDLER:
Daniel Vettera319c1a2013-11-03 21:09:15 +0100237 irq = dev->pdev->irq;
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
Daniel Vettera319c1a2013-11-03 21:09:15 +0100240 ctl->irq != irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return -EINVAL;
Daniel Vettere090c532013-11-03 20:27:05 +0100242 mutex_lock(&dev->struct_mutex);
Daniel Vetterbb0f1b52013-11-03 21:09:27 +0100243 ret = drm_irq_install(dev, irq);
Daniel Vettere090c532013-11-03 20:27:05 +0100244 mutex_unlock(&dev->struct_mutex);
245
246 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 case DRM_UNINST_HANDLER:
Daniel Vettere090c532013-11-03 20:27:05 +0100248 mutex_lock(&dev->struct_mutex);
249 ret = drm_irq_uninstall(dev);
250 mutex_unlock(&dev->struct_mutex);
251
252 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 default:
254 return -EINVAL;
255 }
256}
Dave Airlie61ae2272019-04-18 17:10:40 +1000257#endif