Merge tag 'fuse-update-5.15' of git://git.kernel.org/pub/scm/linux/kernel/git/mszered...
[linux-2.6-microblaze.git] / drivers / gpu / drm / drm_irq.c
1 /*
2  * drm_irq.c IRQ and vblank support
3  *
4  * \author Rickard E. (Rik) Faith <faith@valinux.com>
5  * \author Gareth Hughes <gareth@valinux.com>
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.
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
54
55 #include <linux/export.h>
56 #include <linux/interrupt.h>    /* For task queue support */
57 #include <linux/pci.h>
58 #include <linux/vgaarb.h>
59
60 #include <drm/drm.h>
61 #include <drm/drm_device.h>
62 #include <drm/drm_drv.h>
63 #include <drm/drm_legacy.h>
64 #include <drm/drm_print.h>
65 #include <drm/drm_vblank.h>
66
67 #include "drm_internal.h"
68
69 #if IS_ENABLED(CONFIG_DRM_LEGACY)
70 static int drm_legacy_irq_install(struct drm_device *dev, int irq)
71 {
72         int ret;
73         unsigned long sh_flags = 0;
74
75         if (irq == 0)
76                 return -EINVAL;
77
78         if (dev->irq_enabled)
79                 return -EBUSY;
80         dev->irq_enabled = true;
81
82         DRM_DEBUG("irq=%d\n", irq);
83
84         /* Before installing handler */
85         if (dev->driver->irq_preinstall)
86                 dev->driver->irq_preinstall(dev);
87
88         /* PCI devices require shared interrupts. */
89         if (dev_is_pci(dev->dev))
90                 sh_flags = IRQF_SHARED;
91
92         ret = request_irq(irq, dev->driver->irq_handler,
93                           sh_flags, dev->driver->name, dev);
94
95         if (ret < 0) {
96                 dev->irq_enabled = false;
97                 return ret;
98         }
99
100         /* After installing handler */
101         if (dev->driver->irq_postinstall)
102                 ret = dev->driver->irq_postinstall(dev);
103
104         if (ret < 0) {
105                 dev->irq_enabled = false;
106                 if (drm_core_check_feature(dev, DRIVER_LEGACY))
107                         vga_client_unregister(to_pci_dev(dev->dev));
108                 free_irq(irq, dev);
109         } else {
110                 dev->irq = irq;
111         }
112
113         return ret;
114 }
115
116 int drm_legacy_irq_uninstall(struct drm_device *dev)
117 {
118         unsigned long irqflags;
119         bool irq_enabled;
120         int i;
121
122         irq_enabled = dev->irq_enabled;
123         dev->irq_enabled = false;
124
125         /*
126          * Wake up any waiters so they don't hang. This is just to paper over
127          * issues for UMS drivers which aren't in full control of their
128          * vblank/irq handling. KMS drivers must ensure that vblanks are all
129          * disabled when uninstalling the irq handler.
130          */
131         if (drm_dev_has_vblank(dev)) {
132                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
133                 for (i = 0; i < dev->num_crtcs; i++) {
134                         struct drm_vblank_crtc *vblank = &dev->vblank[i];
135
136                         if (!vblank->enabled)
137                                 continue;
138
139                         WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
140
141                         drm_vblank_disable_and_save(dev, i);
142                         wake_up(&vblank->queue);
143                 }
144                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
145         }
146
147         if (!irq_enabled)
148                 return -EINVAL;
149
150         DRM_DEBUG("irq=%d\n", dev->irq);
151
152         if (drm_core_check_feature(dev, DRIVER_LEGACY))
153                 vga_client_unregister(to_pci_dev(dev->dev));
154
155         if (dev->driver->irq_uninstall)
156                 dev->driver->irq_uninstall(dev);
157
158         free_irq(dev->irq, dev);
159
160         return 0;
161 }
162 EXPORT_SYMBOL(drm_legacy_irq_uninstall);
163
164 int drm_legacy_irq_control(struct drm_device *dev, void *data,
165                            struct drm_file *file_priv)
166 {
167         struct drm_control *ctl = data;
168         int ret = 0, irq;
169         struct pci_dev *pdev;
170
171         /* if we haven't irq we fallback for compatibility reasons -
172          * this used to be a separate function in drm_dma.h
173          */
174
175         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
176                 return 0;
177         if (!drm_core_check_feature(dev, DRIVER_LEGACY))
178                 return 0;
179         /* UMS was only ever supported on pci devices. */
180         if (WARN_ON(!dev_is_pci(dev->dev)))
181                 return -EINVAL;
182
183         switch (ctl->func) {
184         case DRM_INST_HANDLER:
185                 pdev = to_pci_dev(dev->dev);
186                 irq = pdev->irq;
187
188                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
189                     ctl->irq != irq)
190                         return -EINVAL;
191                 mutex_lock(&dev->struct_mutex);
192                 ret = drm_legacy_irq_install(dev, irq);
193                 mutex_unlock(&dev->struct_mutex);
194
195                 return ret;
196         case DRM_UNINST_HANDLER:
197                 mutex_lock(&dev->struct_mutex);
198                 ret = drm_legacy_irq_uninstall(dev);
199                 mutex_unlock(&dev->struct_mutex);
200
201                 return ret;
202         default:
203                 return -EINVAL;
204         }
205 }
206 #endif