]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/char/drm/drm_irq.c
[PATCH] irq-flags: drivers/char: Use the new IRQF_ constants
[mirror_ubuntu-bionic-kernel.git] / drivers / char / drm / drm_irq.c
CommitLineData
1da177e4 1/**
b5e89ed5 2 * \file drm_irq.c
1da177e4
LT
3 * IRQ support
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9/*
10 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36#include "drmP.h"
37
38#include <linux/interrupt.h> /* For task queue support */
39
40/**
41 * Get interrupt from bus id.
b5e89ed5 42 *
1da177e4
LT
43 * \param inode device inode.
44 * \param filp file pointer.
45 * \param cmd command.
46 * \param arg user argument, pointing to a drm_irq_busid structure.
47 * \return zero on success or a negative number on failure.
b5e89ed5 48 *
1da177e4
LT
49 * Finds the PCI device with the specified bus id and gets its IRQ number.
50 * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
51 * to that of the device that this DRM instance attached to.
52 */
53int drm_irq_by_busid(struct inode *inode, struct file *filp,
b5e89ed5 54 unsigned int cmd, unsigned long arg)
1da177e4
LT
55{
56 drm_file_t *priv = filp->private_data;
57 drm_device_t *dev = priv->head->dev;
58 drm_irq_busid_t __user *argp = (void __user *)arg;
59 drm_irq_busid_t p;
60
61 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
62 return -EINVAL;
63
64 if (copy_from_user(&p, argp, sizeof(p)))
65 return -EFAULT;
66
67 if ((p.busnum >> 8) != dev->pci_domain ||
68 (p.busnum & 0xff) != dev->pci_bus ||
b5e89ed5 69 p.devnum != dev->pci_slot || p.funcnum != dev->pci_func)
1da177e4
LT
70 return -EINVAL;
71
72 p.irq = dev->irq;
73
b5e89ed5 74 DRM_DEBUG("%d:%d:%d => IRQ %d\n", p.busnum, p.devnum, p.funcnum, p.irq);
1da177e4
LT
75 if (copy_to_user(argp, &p, sizeof(p)))
76 return -EFAULT;
77 return 0;
78}
79
80/**
81 * Install IRQ handler.
82 *
83 * \param dev DRM device.
84 * \param irq IRQ number.
85 *
86 * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
87 * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
88 * before and after the installation.
89 */
b5e89ed5 90static int drm_irq_install(drm_device_t * dev)
1da177e4
LT
91{
92 int ret;
b5e89ed5 93 unsigned long sh_flags = 0;
1da177e4
LT
94
95 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
96 return -EINVAL;
97
b5e89ed5 98 if (dev->irq == 0)
1da177e4
LT
99 return -EINVAL;
100
30e2fb18 101 mutex_lock(&dev->struct_mutex);
1da177e4
LT
102
103 /* Driver must have been initialized */
b5e89ed5 104 if (!dev->dev_private) {
30e2fb18 105 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
106 return -EINVAL;
107 }
108
b5e89ed5 109 if (dev->irq_enabled) {
30e2fb18 110 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
111 return -EBUSY;
112 }
113 dev->irq_enabled = 1;
30e2fb18 114 mutex_unlock(&dev->struct_mutex);
1da177e4 115
b5e89ed5 116 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
1da177e4
LT
117
118 if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
119 init_waitqueue_head(&dev->vbl_queue);
b5e89ed5
DA
120
121 spin_lock_init(&dev->vbl_lock);
122
123 INIT_LIST_HEAD(&dev->vbl_sigs.head);
124
1da177e4
LT
125 dev->vbl_pending = 0;
126 }
127
b5e89ed5 128 /* Before installing handler */
1da177e4
LT
129 dev->driver->irq_preinstall(dev);
130
b5e89ed5 131 /* Install handler */
1da177e4
LT
132 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
133 sh_flags = SA_SHIRQ;
b5e89ed5
DA
134
135 ret = request_irq(dev->irq, dev->driver->irq_handler,
136 sh_flags, dev->devname, dev);
137 if (ret < 0) {
30e2fb18 138 mutex_lock(&dev->struct_mutex);
1da177e4 139 dev->irq_enabled = 0;
30e2fb18 140 mutex_unlock(&dev->struct_mutex);
1da177e4
LT
141 return ret;
142 }
143
b5e89ed5 144 /* After installing handler */
1da177e4
LT
145 dev->driver->irq_postinstall(dev);
146
147 return 0;
148}
149
150/**
151 * Uninstall the IRQ handler.
152 *
153 * \param dev DRM device.
154 *
155 * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
156 */
b5e89ed5 157int drm_irq_uninstall(drm_device_t * dev)
1da177e4
LT
158{
159 int irq_enabled;
160
161 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
162 return -EINVAL;
163
30e2fb18 164 mutex_lock(&dev->struct_mutex);
1da177e4
LT
165 irq_enabled = dev->irq_enabled;
166 dev->irq_enabled = 0;
30e2fb18 167 mutex_unlock(&dev->struct_mutex);
1da177e4 168
b5e89ed5 169 if (!irq_enabled)
1da177e4
LT
170 return -EINVAL;
171
b5e89ed5 172 DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
1da177e4
LT
173
174 dev->driver->irq_uninstall(dev);
175
b5e89ed5 176 free_irq(dev->irq, dev);
1da177e4
LT
177
178 return 0;
179}
b5e89ed5 180
1da177e4
LT
181EXPORT_SYMBOL(drm_irq_uninstall);
182
183/**
184 * IRQ control ioctl.
185 *
186 * \param inode device inode.
187 * \param filp file pointer.
188 * \param cmd command.
189 * \param arg user argument, pointing to a drm_control structure.
190 * \return zero on success or a negative number on failure.
191 *
192 * Calls irq_install() or irq_uninstall() according to \p arg.
193 */
b5e89ed5
DA
194int drm_control(struct inode *inode, struct file *filp,
195 unsigned int cmd, unsigned long arg)
1da177e4
LT
196{
197 drm_file_t *priv = filp->private_data;
198 drm_device_t *dev = priv->head->dev;
199 drm_control_t ctl;
b5e89ed5 200
1da177e4
LT
201 /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
202
b5e89ed5 203 if (copy_from_user(&ctl, (drm_control_t __user *) arg, sizeof(ctl)))
1da177e4
LT
204 return -EFAULT;
205
b5e89ed5 206 switch (ctl.func) {
1da177e4
LT
207 case DRM_INST_HANDLER:
208 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
209 return 0;
210 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
211 ctl.irq != dev->irq)
212 return -EINVAL;
b5e89ed5 213 return drm_irq_install(dev);
1da177e4
LT
214 case DRM_UNINST_HANDLER:
215 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
216 return 0;
b5e89ed5 217 return drm_irq_uninstall(dev);
1da177e4
LT
218 default:
219 return -EINVAL;
220 }
221}
222
223/**
224 * Wait for VBLANK.
225 *
226 * \param inode device inode.
227 * \param filp file pointer.
228 * \param cmd command.
229 * \param data user argument, pointing to a drm_wait_vblank structure.
230 * \return zero on success or a negative number on failure.
231 *
b5e89ed5 232 * Verifies the IRQ is installed.
1da177e4
LT
233 *
234 * If a signal is requested checks if this task has already scheduled the same signal
235 * for the same vblank sequence number - nothing to be done in
236 * that case. If the number of tasks waiting for the interrupt exceeds 100 the
237 * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
238 * task.
239 *
240 * If a signal is not requested, then calls vblank_wait().
241 */
b5e89ed5 242int drm_wait_vblank(DRM_IOCTL_ARGS)
1da177e4
LT
243{
244 drm_file_t *priv = filp->private_data;
245 drm_device_t *dev = priv->head->dev;
246 drm_wait_vblank_t __user *argp = (void __user *)data;
247 drm_wait_vblank_t vblwait;
248 struct timeval now;
249 int ret = 0;
250 unsigned int flags;
251
252 if (!drm_core_check_feature(dev, DRIVER_IRQ_VBL))
253 return -EINVAL;
254
255 if (!dev->irq)
256 return -EINVAL;
257
b5e89ed5 258 DRM_COPY_FROM_USER_IOCTL(vblwait, argp, sizeof(vblwait));
1da177e4 259
b5e89ed5 260 switch (vblwait.request.type & ~_DRM_VBLANK_FLAGS_MASK) {
1da177e4 261 case _DRM_VBLANK_RELATIVE:
b5e89ed5 262 vblwait.request.sequence += atomic_read(&dev->vbl_received);
1da177e4
LT
263 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
264 case _DRM_VBLANK_ABSOLUTE:
265 break;
266 default:
267 return -EINVAL;
268 }
269
270 flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
b5e89ed5
DA
271
272 if (flags & _DRM_VBLANK_SIGNAL) {
1da177e4
LT
273 unsigned long irqflags;
274 drm_vbl_sig_t *vbl_sig;
1da177e4 275
b5e89ed5
DA
276 vblwait.reply.sequence = atomic_read(&dev->vbl_received);
277
278 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1da177e4
LT
279
280 /* Check if this task has already scheduled the same signal
281 * for the same vblank sequence number; nothing to be done in
282 * that case
283 */
b5e89ed5 284 list_for_each_entry(vbl_sig, &dev->vbl_sigs.head, head) {
1da177e4
LT
285 if (vbl_sig->sequence == vblwait.request.sequence
286 && vbl_sig->info.si_signo == vblwait.request.signal
b5e89ed5
DA
287 && vbl_sig->task == current) {
288 spin_unlock_irqrestore(&dev->vbl_lock,
289 irqflags);
1da177e4
LT
290 goto done;
291 }
292 }
293
b5e89ed5
DA
294 if (dev->vbl_pending >= 100) {
295 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1da177e4
LT
296 return -EBUSY;
297 }
298
299 dev->vbl_pending++;
300
b5e89ed5 301 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1da177e4 302
b5e89ed5
DA
303 if (!
304 (vbl_sig =
305 drm_alloc(sizeof(drm_vbl_sig_t), DRM_MEM_DRIVER))) {
1da177e4
LT
306 return -ENOMEM;
307 }
308
b5e89ed5 309 memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
1da177e4
LT
310
311 vbl_sig->sequence = vblwait.request.sequence;
312 vbl_sig->info.si_signo = vblwait.request.signal;
313 vbl_sig->task = current;
314
b5e89ed5 315 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1da177e4 316
b5e89ed5 317 list_add_tail((struct list_head *)vbl_sig, &dev->vbl_sigs.head);
1da177e4 318
b5e89ed5 319 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1da177e4
LT
320 } else {
321 if (dev->driver->vblank_wait)
b5e89ed5
DA
322 ret =
323 dev->driver->vblank_wait(dev,
324 &vblwait.request.sequence);
1da177e4 325
b5e89ed5 326 do_gettimeofday(&now);
1da177e4
LT
327 vblwait.reply.tval_sec = now.tv_sec;
328 vblwait.reply.tval_usec = now.tv_usec;
329 }
330
b5e89ed5
DA
331 done:
332 DRM_COPY_TO_USER_IOCTL(argp, vblwait, sizeof(vblwait));
1da177e4
LT
333
334 return ret;
335}
336
337/**
338 * Send the VBLANK signals.
339 *
340 * \param dev DRM device.
341 *
342 * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
343 *
344 * If a signal is not requested, then calls vblank_wait().
345 */
b5e89ed5 346void drm_vbl_send_signals(drm_device_t * dev)
1da177e4
LT
347{
348 struct list_head *list, *tmp;
349 drm_vbl_sig_t *vbl_sig;
b5e89ed5 350 unsigned int vbl_seq = atomic_read(&dev->vbl_received);
1da177e4
LT
351 unsigned long flags;
352
b5e89ed5 353 spin_lock_irqsave(&dev->vbl_lock, flags);
1da177e4 354
b5e89ed5
DA
355 list_for_each_safe(list, tmp, &dev->vbl_sigs.head) {
356 vbl_sig = list_entry(list, drm_vbl_sig_t, head);
357 if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
1da177e4 358 vbl_sig->info.si_code = vbl_seq;
b5e89ed5
DA
359 send_sig_info(vbl_sig->info.si_signo, &vbl_sig->info,
360 vbl_sig->task);
1da177e4 361
b5e89ed5 362 list_del(list);
1da177e4 363
b5e89ed5 364 drm_free(vbl_sig, sizeof(*vbl_sig), DRM_MEM_DRIVER);
1da177e4
LT
365
366 dev->vbl_pending--;
367 }
368 }
369
b5e89ed5 370 spin_unlock_irqrestore(&dev->vbl_lock, flags);
1da177e4 371}
1da177e4 372
b5e89ed5 373EXPORT_SYMBOL(drm_vbl_send_signals);