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