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