13253c27bSkettenis /*
23253c27bSkettenis * drm_irq.c IRQ and vblank support
3746fbbdbSjsg *
4746fbbdbSjsg * \author Rickard E. (Rik) Faith <faith@valinux.com>
5746fbbdbSjsg * \author Gareth Hughes <gareth@valinux.com>
67f4dd379Sjsg *
77f4dd379Sjsg * Permission is hereby granted, free of charge, to any person obtaining a
87f4dd379Sjsg * copy of this software and associated documentation files (the "Software"),
97f4dd379Sjsg * to deal in the Software without restriction, including without limitation
107f4dd379Sjsg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
117f4dd379Sjsg * and/or sell copies of the Software, and to permit persons to whom the
127f4dd379Sjsg * Software is furnished to do so, subject to the following conditions:
137f4dd379Sjsg *
147f4dd379Sjsg * The above copyright notice and this permission notice (including the next
157f4dd379Sjsg * paragraph) shall be included in all copies or substantial portions of the
167f4dd379Sjsg * Software.
177f4dd379Sjsg *
187f4dd379Sjsg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
197f4dd379Sjsg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
207f4dd379Sjsg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
217f4dd379Sjsg * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
227f4dd379Sjsg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
237f4dd379Sjsg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
247f4dd379Sjsg * OTHER DEALINGS IN THE SOFTWARE.
25746fbbdbSjsg */
26746fbbdbSjsg
27746fbbdbSjsg /*
28746fbbdbSjsg * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
29746fbbdbSjsg *
30746fbbdbSjsg * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
31746fbbdbSjsg * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
32a9412f7dSoga * All Rights Reserved.
33a9412f7dSoga *
34a9412f7dSoga * Permission is hereby granted, free of charge, to any person obtaining a
35a9412f7dSoga * copy of this software and associated documentation files (the "Software"),
36a9412f7dSoga * to deal in the Software without restriction, including without limitation
37a9412f7dSoga * the rights to use, copy, modify, merge, publish, distribute, sublicense,
38a9412f7dSoga * and/or sell copies of the Software, and to permit persons to whom the
39a9412f7dSoga * Software is furnished to do so, subject to the following conditions:
40a9412f7dSoga *
41a9412f7dSoga * The above copyright notice and this permission notice (including the next
42a9412f7dSoga * paragraph) shall be included in all copies or substantial portions of the
43a9412f7dSoga * Software.
44a9412f7dSoga *
45a9412f7dSoga * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46a9412f7dSoga * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47a9412f7dSoga * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48746fbbdbSjsg * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
49746fbbdbSjsg * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
50746fbbdbSjsg * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
51746fbbdbSjsg * OTHER DEALINGS IN THE SOFTWARE.
52a9412f7dSoga */
53a9412f7dSoga
541fdfe5baSkettenis
553253c27bSkettenis #include <linux/export.h>
56c349dbc7Sjsg #include <linux/interrupt.h> /* For task queue support */
57c349dbc7Sjsg #include <linux/pci.h>
58c349dbc7Sjsg #include <linux/vgaarb.h>
59c349dbc7Sjsg
60c349dbc7Sjsg #include <drm/drm.h>
61c349dbc7Sjsg #include <drm/drm_device.h>
62c349dbc7Sjsg #include <drm/drm_drv.h>
63*5ca02815Sjsg #include <drm/drm_legacy.h>
64c349dbc7Sjsg #include <drm/drm_print.h>
65c349dbc7Sjsg #include <drm/drm_vblank.h>
66a9412f7dSoga
677f4dd379Sjsg #include "drm_internal.h"
683253c27bSkettenis
drm_legacy_irq_install(struct drm_device * dev,int irq)69*5ca02815Sjsg static int drm_legacy_irq_install(struct drm_device *dev, int irq)
70a9412f7dSoga {
71bb145c75Soga int ret;
721fdfe5baSkettenis unsigned long sh_flags = 0;
73a9412f7dSoga
743253c27bSkettenis if (irq == 0)
751fdfe5baSkettenis return -EINVAL;
76a9412f7dSoga
773253c27bSkettenis if (dev->irq_enabled)
78dd5ac294Skettenis return -EBUSY;
791fdfe5baSkettenis dev->irq_enabled = true;
80a9412f7dSoga
813253c27bSkettenis DRM_DEBUG("irq=%d\n", irq);
821fdfe5baSkettenis
831fdfe5baSkettenis /* Before installing handler */
84746fbbdbSjsg if (dev->driver->irq_preinstall)
85746fbbdbSjsg dev->driver->irq_preinstall(dev);
861fdfe5baSkettenis
87c349dbc7Sjsg /* PCI devices require shared interrupts. */
88*5ca02815Sjsg if (dev_is_pci(dev->dev))
891fdfe5baSkettenis sh_flags = IRQF_SHARED;
901fdfe5baSkettenis
913253c27bSkettenis ret = request_irq(irq, dev->driver->irq_handler,
923253c27bSkettenis sh_flags, dev->driver->name, dev);
931fdfe5baSkettenis
941fdfe5baSkettenis if (ret < 0) {
951fdfe5baSkettenis dev->irq_enabled = false;
961fdfe5baSkettenis return ret;
97746fbbdbSjsg }
98a9412f7dSoga
991fdfe5baSkettenis /* After installing handler */
1001fdfe5baSkettenis if (dev->driver->irq_postinstall)
1011fdfe5baSkettenis ret = dev->driver->irq_postinstall(dev);
1021fdfe5baSkettenis
1031fdfe5baSkettenis if (ret < 0) {
1041fdfe5baSkettenis dev->irq_enabled = false;
1057f4dd379Sjsg if (drm_core_check_feature(dev, DRIVER_LEGACY))
106*5ca02815Sjsg vga_client_unregister(to_pci_dev(dev->dev));
1073253c27bSkettenis free_irq(irq, dev);
1083253c27bSkettenis } else {
1093253c27bSkettenis dev->irq = irq;
1101fdfe5baSkettenis }
1111fdfe5baSkettenis
112dd5ac294Skettenis return ret;
113a9412f7dSoga }
114a9412f7dSoga
drm_legacy_irq_uninstall(struct drm_device * dev)115*5ca02815Sjsg int drm_legacy_irq_uninstall(struct drm_device *dev)
116a9412f7dSoga {
117c1268e5aSjsg unsigned long irqflags;
1181fdfe5baSkettenis bool irq_enabled;
1193e8408dbSoga int i;
120a9412f7dSoga
1211fdfe5baSkettenis irq_enabled = dev->irq_enabled;
1221fdfe5baSkettenis dev->irq_enabled = false;
123a9412f7dSoga
1243e8408dbSoga /*
1253253c27bSkettenis * Wake up any waiters so they don't hang. This is just to paper over
1267f4dd379Sjsg * issues for UMS drivers which aren't in full control of their
1273253c27bSkettenis * vblank/irq handling. KMS drivers must ensure that vblanks are all
1283253c27bSkettenis * disabled when uninstalling the irq handler.
1293e8408dbSoga */
130ad8b1aafSjsg if (drm_dev_has_vblank(dev)) {
131c1268e5aSjsg spin_lock_irqsave(&dev->vbl_lock, irqflags);
132746fbbdbSjsg for (i = 0; i < dev->num_crtcs; i++) {
1333253c27bSkettenis struct drm_vblank_crtc *vblank = &dev->vblank[i];
1343253c27bSkettenis
1353253c27bSkettenis if (!vblank->enabled)
1363253c27bSkettenis continue;
1373253c27bSkettenis
1383253c27bSkettenis WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
1393253c27bSkettenis
1407f4dd379Sjsg drm_vblank_disable_and_save(dev, i);
1413253c27bSkettenis wake_up(&vblank->queue);
1423e8408dbSoga }
143c1268e5aSjsg spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
144a058ee16Soga }
1453e8408dbSoga
1461fdfe5baSkettenis if (!irq_enabled)
1471fdfe5baSkettenis return -EINVAL;
148a9412f7dSoga
1493253c27bSkettenis DRM_DEBUG("irq=%d\n", dev->irq);
1501fdfe5baSkettenis
1517f4dd379Sjsg if (drm_core_check_feature(dev, DRIVER_LEGACY))
152*5ca02815Sjsg vga_client_unregister(to_pci_dev(dev->dev));
1531fdfe5baSkettenis
1541fdfe5baSkettenis if (dev->driver->irq_uninstall)
155e6d11d11Soga dev->driver->irq_uninstall(dev);
156a9412f7dSoga
1573253c27bSkettenis free_irq(dev->irq, dev);
1581fdfe5baSkettenis
159dd5ac294Skettenis return 0;
160a9412f7dSoga }
161*5ca02815Sjsg EXPORT_SYMBOL(drm_legacy_irq_uninstall);
162a9412f7dSoga
drm_legacy_irq_control(struct drm_device * dev,void * data,struct drm_file * file_priv)1637f4dd379Sjsg int drm_legacy_irq_control(struct drm_device *dev, void *data,
164d7873f4eSjsg struct drm_file *file_priv)
165a9412f7dSoga {
16682894d78Soga struct drm_control *ctl = data;
1673253c27bSkettenis int ret = 0, irq;
168*5ca02815Sjsg struct pci_dev *pdev;
169a253d7a3Soga
1701fdfe5baSkettenis /* if we haven't irq we fallback for compatibility reasons -
1711fdfe5baSkettenis * this used to be a separate function in drm_dma.h
1721fdfe5baSkettenis */
1731fdfe5baSkettenis
1743253c27bSkettenis if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
1753253c27bSkettenis return 0;
1767f4dd379Sjsg if (!drm_core_check_feature(dev, DRIVER_LEGACY))
1773253c27bSkettenis return 0;
1787f4dd379Sjsg /* UMS was only ever supported on pci devices. */
179*5ca02815Sjsg if (WARN_ON(!dev_is_pci(dev->dev)))
1803253c27bSkettenis return -EINVAL;
181a9412f7dSoga
182a9412f7dSoga switch (ctl->func) {
183a9412f7dSoga case DRM_INST_HANDLER:
184*5ca02815Sjsg pdev = to_pci_dev(dev->dev);
185*5ca02815Sjsg irq = pdev->irq;
1863253c27bSkettenis
187a9412f7dSoga if (dev->if_version < DRM_IF_VERSION(1, 2) &&
1883253c27bSkettenis ctl->irq != irq)
189dd5ac294Skettenis return -EINVAL;
1903253c27bSkettenis mutex_lock(&dev->struct_mutex);
191*5ca02815Sjsg ret = drm_legacy_irq_install(dev, irq);
1923253c27bSkettenis mutex_unlock(&dev->struct_mutex);
1933253c27bSkettenis
1943253c27bSkettenis return ret;
195a9412f7dSoga case DRM_UNINST_HANDLER:
1963253c27bSkettenis mutex_lock(&dev->struct_mutex);
197*5ca02815Sjsg ret = drm_legacy_irq_uninstall(dev);
1983253c27bSkettenis mutex_unlock(&dev->struct_mutex);
1993253c27bSkettenis
2003253c27bSkettenis return ret;
201a9412f7dSoga default:
202dd5ac294Skettenis return -EINVAL;
203a9412f7dSoga }
204a9412f7dSoga }
205