Core vsync: Add flag DRM_VBLANK_NEXTONMISS.
[platform/upstream/libdrm.git] / linux-core / drm_irq.c
1 /**
2  * \file drm_irq.c
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.
42  *
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.
48  *
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  */
53 int drm_irq_by_busid(struct inode *inode, struct file *filp,
54                      unsigned int cmd, unsigned long arg)
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) != drm_get_pci_domain(dev) ||
68             (p.busnum & 0xff) != dev->pdev->bus->number ||
69             p.devnum != PCI_SLOT(dev->pdev->devfn) || p.funcnum != PCI_FUNC(dev->pdev->devfn))
70                 return -EINVAL;
71
72         p.irq = dev->irq;
73
74         DRM_DEBUG("%d:%d:%d => IRQ %d\n", p.busnum, p.devnum, p.funcnum, p.irq);
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  *
85  * Initializes the IRQ related data, and setups drm_device::vbl_queue. Installs the handler, calling the driver
86  * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
87  * before and after the installation.
88  */
89 static int drm_irq_install(drm_device_t * dev)
90 {
91         int ret;
92         unsigned long sh_flags = 0;
93
94         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
95                 return -EINVAL;
96
97         if (dev->irq == 0)
98                 return -EINVAL;
99
100         mutex_lock(&dev->struct_mutex);
101
102         /* Driver must have been initialized */
103         if (!dev->dev_private) {
104                 mutex_unlock(&dev->struct_mutex);
105                 return -EINVAL;
106         }
107
108         if (dev->irq_enabled) {
109                 mutex_unlock(&dev->struct_mutex);
110                 return -EBUSY;
111         }
112         dev->irq_enabled = 1;
113         mutex_unlock(&dev->struct_mutex);
114
115         DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
116
117         if (drm_core_check_feature(dev, DRIVER_IRQ_VBL)) {
118                 init_waitqueue_head(&dev->vbl_queue);
119
120                 spin_lock_init(&dev->vbl_lock);
121                 spin_lock_init(&dev->tasklet_lock);
122
123                 INIT_LIST_HEAD(&dev->vbl_sigs.head);
124                 INIT_LIST_HEAD(&dev->vbl_sigs2.head);
125
126                 dev->vbl_pending = 0;
127         }
128
129         /* Before installing handler */
130         dev->driver->irq_preinstall(dev);
131
132         /* Install handler */
133         if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
134                 sh_flags = SA_SHIRQ;
135
136         ret = request_irq(dev->irq, dev->driver->irq_handler,
137                           sh_flags, dev->devname, dev);
138         if (ret < 0) {
139                 mutex_lock(&dev->struct_mutex);
140                 dev->irq_enabled = 0;
141                 mutex_unlock(&dev->struct_mutex);
142                 return ret;
143         }
144
145         /* After installing handler */
146         dev->driver->irq_postinstall(dev);
147
148         return 0;
149 }
150
151 /**
152  * Uninstall the IRQ handler.
153  *
154  * \param dev DRM device.
155  *
156  * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
157  */
158 int drm_irq_uninstall(drm_device_t * dev)
159 {
160         int irq_enabled;
161
162         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
163                 return -EINVAL;
164
165         mutex_lock(&dev->struct_mutex);
166         irq_enabled = dev->irq_enabled;
167         dev->irq_enabled = 0;
168         mutex_unlock(&dev->struct_mutex);
169
170         if (!irq_enabled)
171                 return -EINVAL;
172
173         DRM_DEBUG("%s: irq=%d\n", __FUNCTION__, dev->irq);
174
175         dev->driver->irq_uninstall(dev);
176
177         free_irq(dev->irq, dev);
178
179         dev->locked_tasklet_func = NULL;
180
181         return 0;
182 }
183 EXPORT_SYMBOL(drm_irq_uninstall);
184
185 /**
186  * IRQ control ioctl.
187  *
188  * \param inode device inode.
189  * \param filp file pointer.
190  * \param cmd command.
191  * \param arg user argument, pointing to a drm_control structure.
192  * \return zero on success or a negative number on failure.
193  *
194  * Calls irq_install() or irq_uninstall() according to \p arg.
195  */
196 int drm_control(struct inode *inode, struct file *filp,
197                 unsigned int cmd, unsigned long arg)
198 {
199         drm_file_t *priv = filp->private_data;
200         drm_device_t *dev = priv->head->dev;
201         drm_control_t ctl;
202
203         /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
204
205         if (copy_from_user(&ctl, (drm_control_t __user *) arg, sizeof(ctl)))
206                 return -EFAULT;
207
208         switch (ctl.func) {
209         case DRM_INST_HANDLER:
210                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
211                         return 0;
212                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
213                     ctl.irq != dev->irq)
214                         return -EINVAL;
215                 return drm_irq_install(dev);
216         case DRM_UNINST_HANDLER:
217                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
218                         return 0;
219                 return drm_irq_uninstall(dev);
220         default:
221                 return -EINVAL;
222         }
223 }
224
225 /**
226  * Wait for VBLANK.
227  *
228  * \param inode device inode.
229  * \param filp file pointer.
230  * \param cmd command.
231  * \param data user argument, pointing to a drm_wait_vblank structure.
232  * \return zero on success or a negative number on failure.
233  *
234  * Verifies the IRQ is installed.
235  *
236  * If a signal is requested checks if this task has already scheduled the same signal
237  * for the same vblank sequence number - nothing to be done in
238  * that case. If the number of tasks waiting for the interrupt exceeds 100 the
239  * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
240  * task.
241  *
242  * If a signal is not requested, then calls vblank_wait().
243  */
244 int drm_wait_vblank(DRM_IOCTL_ARGS)
245 {
246         drm_file_t *priv = filp->private_data;
247         drm_device_t *dev = priv->head->dev;
248         drm_wait_vblank_t __user *argp = (void __user *)data;
249         drm_wait_vblank_t vblwait;
250         struct timeval now;
251         int ret = 0;
252         unsigned int flags, seq;
253
254         if ((!dev->irq) || (!dev->irq_enabled))
255                 return -EINVAL;
256
257         if (copy_from_user(&vblwait, argp, sizeof(vblwait)))
258                 return -EFAULT;
259
260         if (vblwait.request.type &
261             ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
262                 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
263                           vblwait.request.type,
264                           (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
265                 return -EINVAL;
266         }
267
268         flags = vblwait.request.type & _DRM_VBLANK_FLAGS_MASK;
269
270         if (!drm_core_check_feature(dev, (flags & _DRM_VBLANK_SECONDARY) ?
271                                     DRIVER_IRQ_VBL2 : DRIVER_IRQ_VBL))
272                 return -EINVAL;
273
274         seq = atomic_read((flags & _DRM_VBLANK_SECONDARY) ? &dev->vbl_received2
275                           : &dev->vbl_received);
276
277         switch (vblwait.request.type & _DRM_VBLANK_TYPES_MASK) {
278         case _DRM_VBLANK_RELATIVE:
279                 vblwait.request.sequence += seq;
280                 vblwait.request.type &= ~_DRM_VBLANK_RELATIVE;
281         case _DRM_VBLANK_ABSOLUTE:
282                 break;
283         default:
284                 return -EINVAL;
285         }
286
287         if ((flags & _DRM_VBLANK_NEXTONMISS) &&
288             (seq - vblwait.request.sequence) <= (1<<23)) {
289                 vblwait.request.sequence = seq + 1;
290         }
291
292         if (flags & _DRM_VBLANK_SIGNAL) {
293                 unsigned long irqflags;
294                 drm_vbl_sig_t *vbl_sigs = (flags & _DRM_VBLANK_SECONDARY)
295                                       ? &dev->vbl_sigs2 : &dev->vbl_sigs;
296                 drm_vbl_sig_t *vbl_sig;
297
298                 vblwait.reply.sequence = seq;
299
300                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
301
302                 /* Check if this task has already scheduled the same signal
303                  * for the same vblank sequence number; nothing to be done in
304                  * that case
305                  */
306                 list_for_each_entry(vbl_sig, &vbl_sigs->head, head) {
307                         if (vbl_sig->sequence == vblwait.request.sequence
308                             && vbl_sig->info.si_signo == vblwait.request.signal
309                             && vbl_sig->task == current) {
310                                 spin_unlock_irqrestore(&dev->vbl_lock,
311                                                        irqflags);
312                                 goto done;
313                         }
314                 }
315
316                 if (dev->vbl_pending >= 100) {
317                         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
318                         return -EBUSY;
319                 }
320
321                 dev->vbl_pending++;
322
323                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
324
325                 if (!
326                     (vbl_sig =
327                      drm_alloc(sizeof(drm_vbl_sig_t), DRM_MEM_DRIVER))) {
328                         return -ENOMEM;
329                 }
330
331                 memset((void *)vbl_sig, 0, sizeof(*vbl_sig));
332
333                 vbl_sig->sequence = vblwait.request.sequence;
334                 vbl_sig->info.si_signo = vblwait.request.signal;
335                 vbl_sig->task = current;
336
337                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
338
339                 list_add_tail((struct list_head *)vbl_sig, &vbl_sigs->head);
340
341                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
342         } else {
343                 if (flags & _DRM_VBLANK_SECONDARY) {
344                         if (dev->driver->vblank_wait2)
345                                 ret = dev->driver->vblank_wait2(dev, &vblwait.request.sequence);
346                 } else if (dev->driver->vblank_wait)
347                         ret =
348                             dev->driver->vblank_wait(dev,
349                                                      &vblwait.request.sequence);
350
351                 do_gettimeofday(&now);
352                 vblwait.reply.tval_sec = now.tv_sec;
353                 vblwait.reply.tval_usec = now.tv_usec;
354         }
355
356       done:
357         if (copy_to_user(argp, &vblwait, sizeof(vblwait)))
358                 return -EFAULT;
359
360         return ret;
361 }
362
363 /**
364  * Send the VBLANK signals.
365  *
366  * \param dev DRM device.
367  *
368  * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
369  *
370  * If a signal is not requested, then calls vblank_wait().
371  */
372 void drm_vbl_send_signals(drm_device_t * dev)
373 {
374         unsigned long flags;
375         int i;
376
377         spin_lock_irqsave(&dev->vbl_lock, flags);
378
379         for (i = 0; i < 2; i++) {
380                 struct list_head *list, *tmp;
381                 drm_vbl_sig_t *vbl_sig;
382                 drm_vbl_sig_t *vbl_sigs = i ? &dev->vbl_sigs2 : &dev->vbl_sigs;
383                 unsigned int vbl_seq = atomic_read(i ? &dev->vbl_received2 :
384                                                    &dev->vbl_received);
385
386                 list_for_each_safe(list, tmp, &vbl_sigs->head) {
387                         vbl_sig = list_entry(list, drm_vbl_sig_t, head);
388                         if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
389                                 vbl_sig->info.si_code = vbl_seq;
390                                 send_sig_info(vbl_sig->info.si_signo,
391                                               &vbl_sig->info, vbl_sig->task);
392
393                                 list_del(list);
394
395                                 drm_free(vbl_sig, sizeof(*vbl_sig),
396                                          DRM_MEM_DRIVER);
397
398                                 dev->vbl_pending--;
399                         }
400                 }
401         }
402
403         spin_unlock_irqrestore(&dev->vbl_lock, flags);
404 }
405 EXPORT_SYMBOL(drm_vbl_send_signals);
406
407 /**
408  * Tasklet wrapper function.
409  *
410  * \param data DRM device in disguise.
411  *
412  * Attempts to grab the HW lock and calls the driver callback on success. On
413  * failure, leave the lock marked as contended so the callback can be called
414  * from drm_unlock().
415  */
416 static void drm_locked_tasklet_func(unsigned long data)
417 {
418         drm_device_t *dev = (drm_device_t*)data;
419         unsigned int irqflags;
420
421         spin_lock_irqsave(&dev->tasklet_lock, irqflags);
422
423         if (!dev->locked_tasklet_func ||
424             !drm_lock_take(&dev->lock.hw_lock->lock,
425                            DRM_KERNEL_CONTEXT)) {
426                 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
427                 return;
428         }
429
430         dev->lock.lock_time = jiffies;
431         atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
432
433         dev->locked_tasklet_func(dev);
434
435         drm_lock_free(dev, &dev->lock.hw_lock->lock,
436                       DRM_KERNEL_CONTEXT);
437
438         dev->locked_tasklet_func = NULL;
439
440         spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
441 }
442
443 /**
444  * Schedule a tasklet to call back a driver hook with the HW lock held.
445  *
446  * \param dev DRM device.
447  * \param func Driver callback.
448  *
449  * This is intended for triggering actions that require the HW lock from an
450  * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
451  * completes. Note that the callback may be called from interrupt or process
452  * context, it must not make any assumptions about this. Also, the HW lock will
453  * be held with the kernel context or any client context.
454  */
455 void drm_locked_tasklet(drm_device_t *dev, void (*func)(drm_device_t*))
456 {
457         unsigned int irqflags;
458         static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
459
460         if (test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
461                 return;
462
463         spin_lock_irqsave(&dev->tasklet_lock, irqflags);
464
465         if (dev->locked_tasklet_func) {
466                 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
467                 return;
468         }
469
470         dev->locked_tasklet_func = func;
471
472         spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
473
474         drm_tasklet.data = (unsigned long)dev;
475
476         tasklet_hi_schedule(&drm_tasklet);
477 }
478 EXPORT_SYMBOL(drm_locked_tasklet);