945dd82e2ea3e5905ae8f9ec93b10d9f5cc277f5
[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_irq.h>
64 #include <drm/drm_print.h>
65 #include <drm/drm_vblank.h>
66
67 #include "drm_internal.h"
68
69 /**
70  * DOC: irq helpers
71  *
72  * The DRM core provides very simple support helpers to enable IRQ handling on a
73  * device through the drm_irq_install() and drm_irq_uninstall() functions. This
74  * only supports devices with a single interrupt on the main device stored in
75  * &drm_device.dev and set as the device paramter in drm_dev_alloc().
76  *
77  * These IRQ helpers are strictly optional. Since these helpers don't automatically
78  * clean up the requested interrupt like e.g. devm_request_irq() they're not really
79  * recommended.
80  */
81
82 /**
83  * drm_irq_install - install IRQ handler
84  * @dev: DRM device
85  * @irq: IRQ number to install the handler for
86  *
87  * Initializes the IRQ related data. Installs the handler, calling the driver
88  * &drm_driver.irq_preinstall and &drm_driver.irq_postinstall functions before
89  * and after the installation.
90  *
91  * This is the simplified helper interface provided for drivers with no special
92  * needs.
93  *
94  * @irq must match the interrupt number that would be passed to request_irq(),
95  * if called directly instead of using this helper function.
96  *
97  * &drm_driver.irq_handler is called to handle the registered interrupt.
98  *
99  * Returns:
100  * Zero on success or a negative error code on failure.
101  */
102 int drm_irq_install(struct drm_device *dev, int irq)
103 {
104         int ret;
105         unsigned long sh_flags = 0;
106
107         if (irq == 0)
108                 return -EINVAL;
109
110         if (dev->irq_enabled)
111                 return -EBUSY;
112         dev->irq_enabled = true;
113
114         DRM_DEBUG("irq=%d\n", irq);
115
116         /* Before installing handler */
117         if (dev->driver->irq_preinstall)
118                 dev->driver->irq_preinstall(dev);
119
120         /* PCI devices require shared interrupts. */
121         if (dev_is_pci(dev->dev))
122                 sh_flags = IRQF_SHARED;
123
124         ret = request_irq(irq, dev->driver->irq_handler,
125                           sh_flags, dev->driver->name, dev);
126
127         if (ret < 0) {
128                 dev->irq_enabled = false;
129                 return ret;
130         }
131
132         /* After installing handler */
133         if (dev->driver->irq_postinstall)
134                 ret = dev->driver->irq_postinstall(dev);
135
136         if (ret < 0) {
137                 dev->irq_enabled = false;
138                 if (drm_core_check_feature(dev, DRIVER_LEGACY))
139                         vga_client_register(to_pci_dev(dev->dev), NULL, NULL, NULL);
140                 free_irq(irq, dev);
141         } else {
142                 dev->irq = irq;
143         }
144
145         return ret;
146 }
147 EXPORT_SYMBOL(drm_irq_install);
148
149 /**
150  * drm_irq_uninstall - uninstall the IRQ handler
151  * @dev: DRM device
152  *
153  * Calls the driver's &drm_driver.irq_uninstall function and unregisters the IRQ
154  * handler.  This should only be called by drivers which used drm_irq_install()
155  * to set up their interrupt handler.
156  *
157  * Note that for kernel modesetting drivers it is a bug if this function fails.
158  * The sanity checks are only to catch buggy user modesetting drivers which call
159  * the same function through an ioctl.
160  *
161  * Returns:
162  * Zero on success or a negative error code on failure.
163  */
164 int drm_irq_uninstall(struct drm_device *dev)
165 {
166         unsigned long irqflags;
167         bool irq_enabled;
168         int i;
169
170         irq_enabled = dev->irq_enabled;
171         dev->irq_enabled = false;
172
173         /*
174          * Wake up any waiters so they don't hang. This is just to paper over
175          * issues for UMS drivers which aren't in full control of their
176          * vblank/irq handling. KMS drivers must ensure that vblanks are all
177          * disabled when uninstalling the irq handler.
178          */
179         if (drm_dev_has_vblank(dev)) {
180                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
181                 for (i = 0; i < dev->num_crtcs; i++) {
182                         struct drm_vblank_crtc *vblank = &dev->vblank[i];
183
184                         if (!vblank->enabled)
185                                 continue;
186
187                         WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
188
189                         drm_vblank_disable_and_save(dev, i);
190                         wake_up(&vblank->queue);
191                 }
192                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
193         }
194
195         if (!irq_enabled)
196                 return -EINVAL;
197
198         DRM_DEBUG("irq=%d\n", dev->irq);
199
200         if (drm_core_check_feature(dev, DRIVER_LEGACY))
201                 vga_client_register(to_pci_dev(dev->dev), NULL, NULL, NULL);
202
203         if (dev->driver->irq_uninstall)
204                 dev->driver->irq_uninstall(dev);
205
206         free_irq(dev->irq, dev);
207
208         return 0;
209 }
210 EXPORT_SYMBOL(drm_irq_uninstall);
211
212 static void devm_drm_irq_uninstall(void *data)
213 {
214         drm_irq_uninstall(data);
215 }
216
217 /**
218  * devm_drm_irq_install - install IRQ handler
219  * @dev: DRM device
220  * @irq: IRQ number to install the handler for
221  *
222  * devm_drm_irq_install is a  help function of drm_irq_install.
223  *
224  * if the driver uses devm_drm_irq_install, there is no need
225  * to call drm_irq_uninstall when the drm module get unloaded,
226  * as this will done automagically.
227  *
228  * Returns:
229  * Zero on success or a negative error code on failure.
230  */
231 int devm_drm_irq_install(struct drm_device *dev, int irq)
232 {
233         int ret;
234
235         ret = drm_irq_install(dev, irq);
236         if (ret)
237                 return ret;
238
239         return devm_add_action_or_reset(dev->dev,
240                                         devm_drm_irq_uninstall, dev);
241 }
242 EXPORT_SYMBOL(devm_drm_irq_install);
243
244 #if IS_ENABLED(CONFIG_DRM_LEGACY)
245 int drm_legacy_irq_control(struct drm_device *dev, void *data,
246                            struct drm_file *file_priv)
247 {
248         struct drm_control *ctl = data;
249         int ret = 0, irq;
250         struct pci_dev *pdev;
251
252         /* if we haven't irq we fallback for compatibility reasons -
253          * this used to be a separate function in drm_dma.h
254          */
255
256         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
257                 return 0;
258         if (!drm_core_check_feature(dev, DRIVER_LEGACY))
259                 return 0;
260         /* UMS was only ever supported on pci devices. */
261         if (WARN_ON(!dev_is_pci(dev->dev)))
262                 return -EINVAL;
263
264         switch (ctl->func) {
265         case DRM_INST_HANDLER:
266                 pdev = to_pci_dev(dev->dev);
267                 irq = pdev->irq;
268
269                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
270                     ctl->irq != irq)
271                         return -EINVAL;
272                 mutex_lock(&dev->struct_mutex);
273                 ret = drm_irq_install(dev, irq);
274                 mutex_unlock(&dev->struct_mutex);
275
276                 return ret;
277         case DRM_UNINST_HANDLER:
278                 mutex_lock(&dev->struct_mutex);
279                 ret = drm_irq_uninstall(dev);
280                 mutex_unlock(&dev->struct_mutex);
281
282                 return ret;
283         default:
284                 return -EINVAL;
285         }
286 }
287 #endif