[intel] Quirk away MSI support on 945G/GM.
[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 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 drm_device *dev, void *data,
54                      struct drm_file *file_priv)
55 {
56         struct drm_irq_busid *p = data;
57
58         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
59                 return -EINVAL;
60
61         if ((p->busnum >> 8) != drm_get_pci_domain(dev) ||
62             (p->busnum & 0xff) != dev->pdev->bus->number ||
63             p->devnum != PCI_SLOT(dev->pdev->devfn) || p->funcnum != PCI_FUNC(dev->pdev->devfn))
64                 return -EINVAL;
65
66         p->irq = dev->pdev->irq;
67
68         DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
69                   p->irq);
70
71         return 0;
72 }
73
74 static void vblank_disable_fn(unsigned long arg)
75 {
76         struct drm_device *dev = (struct drm_device *)arg;
77         unsigned long irqflags;
78         int i;
79
80         for (i = 0; i < dev->num_crtcs; i++) {
81                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
82                 if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
83                     dev->vblank_enabled[i]) {
84                         dev->driver->disable_vblank(dev, i);
85                         dev->vblank_enabled[i] = 0;
86                 }
87                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
88         }
89 }
90
91 static void drm_vblank_cleanup(struct drm_device *dev)
92 {
93         /* Bail if the driver didn't call drm_vblank_init() */
94         if (dev->num_crtcs == 0)
95                 return;
96
97         del_timer(&dev->vblank_disable_timer);
98
99         vblank_disable_fn((unsigned long)dev);
100
101         drm_free(dev->vbl_queue, sizeof(*dev->vbl_queue) * dev->num_crtcs,
102                  DRM_MEM_DRIVER);
103         drm_free(dev->vbl_sigs, sizeof(*dev->vbl_sigs) * dev->num_crtcs,
104                  DRM_MEM_DRIVER);
105         drm_free(dev->_vblank_count, sizeof(*dev->_vblank_count) *
106                  dev->num_crtcs, DRM_MEM_DRIVER);
107         drm_free(dev->vblank_refcount, sizeof(*dev->vblank_refcount) *
108                  dev->num_crtcs, DRM_MEM_DRIVER);
109         drm_free(dev->vblank_enabled, sizeof(*dev->vblank_enabled) *
110                  dev->num_crtcs, DRM_MEM_DRIVER);
111         drm_free(dev->last_vblank, sizeof(*dev->last_vblank) * dev->num_crtcs,
112                  DRM_MEM_DRIVER);
113         drm_free(dev->vblank_premodeset, sizeof(*dev->vblank_premodeset) *
114                  dev->num_crtcs, DRM_MEM_DRIVER);
115         drm_free(dev->vblank_offset, sizeof(*dev->vblank_offset) * dev->num_crtcs,
116                  DRM_MEM_DRIVER);
117
118         dev->num_crtcs = 0;
119 }
120
121 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
122 {
123         int i, ret = -ENOMEM;
124
125         setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
126                     (unsigned long)dev);
127         init_timer_deferrable(&dev->vblank_disable_timer);
128         spin_lock_init(&dev->vbl_lock);
129         atomic_set(&dev->vbl_signal_pending, 0);
130         dev->num_crtcs = num_crtcs;
131
132         dev->vbl_queue = drm_alloc(sizeof(wait_queue_head_t) * num_crtcs,
133                                    DRM_MEM_DRIVER);
134         if (!dev->vbl_queue)
135                 goto err;
136
137         dev->vbl_sigs = drm_alloc(sizeof(struct list_head) * num_crtcs,
138                                   DRM_MEM_DRIVER);
139         if (!dev->vbl_sigs)
140                 goto err;
141
142         dev->_vblank_count = drm_alloc(sizeof(atomic_t) * num_crtcs,
143                                       DRM_MEM_DRIVER);
144         if (!dev->_vblank_count)
145                 goto err;
146
147         dev->vblank_refcount = drm_alloc(sizeof(atomic_t) * num_crtcs,
148                                          DRM_MEM_DRIVER);
149         if (!dev->vblank_refcount)
150                 goto err;
151
152         dev->vblank_enabled = drm_calloc(num_crtcs, sizeof(int),
153                                          DRM_MEM_DRIVER);
154         if (!dev->vblank_enabled)
155                 goto err;
156
157         dev->last_vblank = drm_calloc(num_crtcs, sizeof(u32), DRM_MEM_DRIVER);
158         if (!dev->last_vblank)
159                 goto err;
160
161         dev->vblank_premodeset = drm_calloc(num_crtcs, sizeof(u32),
162                                             DRM_MEM_DRIVER);
163         if (!dev->vblank_premodeset)
164                 goto err;
165
166         dev->vblank_offset = drm_calloc(num_crtcs, sizeof(u32), DRM_MEM_DRIVER);
167         if (!dev->vblank_offset)
168                 goto err;
169
170         /* Zero per-crtc vblank stuff */
171         for (i = 0; i < num_crtcs; i++) {
172                 init_waitqueue_head(&dev->vbl_queue[i]);
173                 INIT_LIST_HEAD(&dev->vbl_sigs[i]);
174                 atomic_set(&dev->_vblank_count[i], 0);
175                 atomic_set(&dev->vblank_refcount[i], 0);
176         }
177
178         return 0;
179
180 err:
181         drm_vblank_cleanup(dev);
182         return ret;
183 }
184 EXPORT_SYMBOL(drm_vblank_init);
185
186 /**
187  * Install IRQ handler.
188  *
189  * \param dev DRM device.
190  *
191  * Initializes the IRQ related data. Installs the handler, calling the driver
192  * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
193  * before and after the installation.
194  */
195 int drm_irq_install(struct drm_device * dev)
196 {
197         int ret = 0;
198         unsigned long sh_flags = 0;
199
200         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
201                 return -EINVAL;
202
203         if (dev->pdev->irq == 0)
204                 return -EINVAL;
205
206         mutex_lock(&dev->struct_mutex);
207
208         /* Driver must have been initialized */
209         if (!dev->dev_private) {
210                 mutex_unlock(&dev->struct_mutex);
211                 return -EINVAL;
212         }
213
214         if (dev->irq_enabled) {
215                 mutex_unlock(&dev->struct_mutex);
216                 return -EBUSY;
217         }
218         dev->irq_enabled = 1;
219         mutex_unlock(&dev->struct_mutex);
220
221         DRM_DEBUG("irq=%d\n", dev->pdev->irq);
222
223         /* Before installing handler */
224         dev->driver->irq_preinstall(dev);
225
226         /* Install handler */
227         if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
228                 sh_flags = IRQF_SHARED;
229
230         ret = request_irq(dev->pdev->irq, dev->driver->irq_handler,
231                           sh_flags, dev->devname, dev);
232         if (ret < 0) {
233                 mutex_lock(&dev->struct_mutex);
234                 dev->irq_enabled = 0;
235                 mutex_unlock(&dev->struct_mutex);
236                 return ret;
237         }
238         /* Expose the device irq to device drivers that want to export it for
239          * whatever reason.
240          */
241         dev->irq = dev->pdev->irq;
242
243         /* After installing handler */
244         ret = dev->driver->irq_postinstall(dev);
245         if (ret < 0) {
246                 mutex_lock(&dev->struct_mutex);
247                 dev->irq_enabled = 0;
248                 mutex_unlock(&dev->struct_mutex);
249         }
250
251         return ret;
252 }
253 EXPORT_SYMBOL(drm_irq_install);
254
255 /**
256  * Uninstall the IRQ handler.
257  *
258  * \param dev DRM device.
259  *
260  * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
261  */
262 int drm_irq_uninstall(struct drm_device * dev)
263 {
264         int irq_enabled;
265
266         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
267                 return -EINVAL;
268
269         mutex_lock(&dev->struct_mutex);
270         irq_enabled = dev->irq_enabled;
271         dev->irq_enabled = 0;
272         mutex_unlock(&dev->struct_mutex);
273
274         if (!irq_enabled)
275                 return -EINVAL;
276
277         DRM_DEBUG("irq=%d\n", dev->pdev->irq);
278
279         dev->driver->irq_uninstall(dev);
280
281         free_irq(dev->pdev->irq, dev);
282
283         drm_vblank_cleanup(dev);
284
285         dev->locked_tasklet_func = NULL;
286
287         return 0;
288 }
289 EXPORT_SYMBOL(drm_irq_uninstall);
290
291 /**
292  * IRQ control ioctl.
293  *
294  * \param inode device inode.
295  * \param file_priv DRM file private.
296  * \param cmd command.
297  * \param arg user argument, pointing to a drm_control structure.
298  * \return zero on success or a negative number on failure.
299  *
300  * Calls irq_install() or irq_uninstall() according to \p arg.
301  */
302 int drm_control(struct drm_device *dev, void *data,
303                 struct drm_file *file_priv)
304 {
305         struct drm_control *ctl = data;
306
307         /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
308
309
310         switch (ctl->func) {
311         case DRM_INST_HANDLER:
312                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
313                         return 0;
314                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
315                     ctl->irq != dev->pdev->irq)
316                         return -EINVAL;
317                 return drm_irq_install(dev);
318         case DRM_UNINST_HANDLER:
319                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
320                         return 0;
321                 return drm_irq_uninstall(dev);
322         default:
323                 return -EINVAL;
324         }
325 }
326
327 /**
328  * drm_vblank_count - retrieve "cooked" vblank counter value
329  * @dev: DRM device
330  * @crtc: which counter to retrieve
331  *
332  * Fetches the "cooked" vblank count value that represents the number of
333  * vblank events since the system was booted, including lost events due to
334  * modesetting activity.
335  */
336 u32 drm_vblank_count(struct drm_device *dev, int crtc)
337 {
338         return atomic_read(&dev->_vblank_count[crtc]) +
339                 dev->vblank_offset[crtc];
340 }
341 EXPORT_SYMBOL(drm_vblank_count);
342
343 /**
344  * drm_update_vblank_count - update the master vblank counter
345  * @dev: DRM device
346  * @crtc: counter to update
347  *
348  * Call back into the driver to update the appropriate vblank counter
349  * (specified by @crtc).  Deal with wraparound, if it occurred, and
350  * update the last read value so we can deal with wraparound on the next
351  * call if necessary.
352  */
353 void drm_update_vblank_count(struct drm_device *dev, int crtc)
354 {
355         unsigned long irqflags;
356         u32 cur_vblank, diff;
357
358         /*
359          * Interrupts were disabled prior to this call, so deal with counter
360          * wrap if needed.
361          * NOTE!  It's possible we lost a full dev->max_vblank_count events
362          * here if the register is small or we had vblank interrupts off for
363          * a long time.
364          */
365         cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
366         spin_lock_irqsave(&dev->vbl_lock, irqflags);
367         if (cur_vblank < dev->last_vblank[crtc]) {
368                 diff = dev->max_vblank_count -
369                         dev->last_vblank[crtc];
370                 diff += cur_vblank;
371         } else {
372                 diff = cur_vblank - dev->last_vblank[crtc];
373         }
374         dev->last_vblank[crtc] = cur_vblank;
375         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
376
377         atomic_add(diff, &dev->_vblank_count[crtc]);
378 }
379 EXPORT_SYMBOL(drm_update_vblank_count);
380
381 /**
382  * drm_vblank_get - get a reference count on vblank events
383  * @dev: DRM device
384  * @crtc: which CRTC to own
385  *
386  * Acquire a reference count on vblank events to avoid having them disabled
387  * while in use.  Note callers will probably want to update the master counter
388  * using drm_update_vblank_count() above before calling this routine so that
389  * wakeups occur on the right vblank event.
390  *
391  * RETURNS
392  * Zero on success, nonzero on failure.
393  */
394 int drm_vblank_get(struct drm_device *dev, int crtc)
395 {
396         unsigned long irqflags;
397         int ret = 0;
398
399         spin_lock_irqsave(&dev->vbl_lock, irqflags);    
400         /* Going from 0->1 means we have to enable interrupts again */
401         if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1 &&
402             !dev->vblank_enabled[crtc]) {
403                 ret = dev->driver->enable_vblank(dev, crtc);
404                 if (ret)
405                         atomic_dec(&dev->vblank_refcount[crtc]);
406                 else
407                         dev->vblank_enabled[crtc] = 1;
408         }
409         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
410
411         return ret;
412 }
413 EXPORT_SYMBOL(drm_vblank_get);
414
415 /**
416  * drm_vblank_put - give up ownership of vblank events
417  * @dev: DRM device
418  * @crtc: which counter to give up
419  *
420  * Release ownership of a given vblank counter, turning off interrupts
421  * if possible.
422  */
423 void drm_vblank_put(struct drm_device *dev, int crtc)
424 {
425         /* Last user schedules interrupt disable */
426         if (atomic_dec_and_test(&dev->vblank_refcount[crtc]))
427             mod_timer(&dev->vblank_disable_timer, jiffies + 5*DRM_HZ);
428 }
429 EXPORT_SYMBOL(drm_vblank_put);
430
431 /**
432  * drm_modeset_ctl - handle vblank event counter changes across mode switch
433  * @DRM_IOCTL_ARGS: standard ioctl arguments
434  *
435  * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
436  * ioctls around modesetting so that any lost vblank events are accounted for.
437  */
438 int drm_modeset_ctl(struct drm_device *dev, void *data,
439                     struct drm_file *file_priv)
440 {
441         struct drm_modeset_ctl *modeset = data;
442         int crtc, ret = 0;
443         u32 new;
444
445         crtc = modeset->crtc;
446         if (crtc >= dev->num_crtcs) {
447                 ret = -EINVAL;
448                 goto out;
449         }
450
451         switch (modeset->cmd) {
452         case _DRM_PRE_MODESET:
453                 dev->vblank_premodeset[crtc] =
454                         dev->driver->get_vblank_counter(dev, crtc);
455                 break;
456         case _DRM_POST_MODESET:
457                 new = dev->driver->get_vblank_counter(dev, crtc);
458                 dev->vblank_offset[crtc] = dev->vblank_premodeset[crtc] - new;
459                 break;
460         default:
461                 ret = -EINVAL;
462                 break;
463         }
464
465 out:
466         return ret;
467 }
468
469 /**
470  * Wait for VBLANK.
471  *
472  * \param inode device inode.
473  * \param file_priv DRM file private.
474  * \param cmd command.
475  * \param data user argument, pointing to a drm_wait_vblank structure.
476  * \return zero on success or a negative number on failure.
477  *
478  * Verifies the IRQ is installed.
479  *
480  * If a signal is requested checks if this task has already scheduled the same signal
481  * for the same vblank sequence number - nothing to be done in
482  * that case. If the number of tasks waiting for the interrupt exceeds 100 the
483  * function fails. Otherwise adds a new entry to drm_device::vbl_sigs for this
484  * task.
485  *
486  * If a signal is not requested, then calls vblank_wait().
487  */
488 int drm_wait_vblank(struct drm_device *dev, void *data,
489                     struct drm_file *file_priv)
490 {
491         union drm_wait_vblank *vblwait = data;
492         struct timeval now;
493         int ret = 0;
494         unsigned int flags, seq, crtc;
495
496         if ((!dev->pdev->irq) || (!dev->irq_enabled))
497                 return -EINVAL;
498
499         if (vblwait->request.type &
500             ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
501                 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
502                           vblwait->request.type,
503                           (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
504                 return -EINVAL;
505         }
506
507         flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
508         crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
509
510         if (crtc >= dev->num_crtcs)
511                 return -EINVAL;
512
513         drm_update_vblank_count(dev, crtc);
514         seq = drm_vblank_count(dev, crtc);
515
516         switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
517         case _DRM_VBLANK_RELATIVE:
518                 vblwait->request.sequence += seq;
519                 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
520         case _DRM_VBLANK_ABSOLUTE:
521                 break;
522         default:
523                 return -EINVAL;
524         }
525
526         if ((flags & _DRM_VBLANK_NEXTONMISS) &&
527             (seq - vblwait->request.sequence) <= (1<<23)) {
528                 vblwait->request.sequence = seq + 1;
529         }
530
531         if (flags & _DRM_VBLANK_SIGNAL) {
532                 unsigned long irqflags;
533                 struct list_head *vbl_sigs = &dev->vbl_sigs[crtc];
534                 struct drm_vbl_sig *vbl_sig;
535
536                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
537
538                 /* Check if this task has already scheduled the same signal
539                  * for the same vblank sequence number; nothing to be done in
540                  * that case
541                  */
542                 list_for_each_entry(vbl_sig, vbl_sigs, head) {
543                         if (vbl_sig->sequence == vblwait->request.sequence
544                             && vbl_sig->info.si_signo ==
545                             vblwait->request.signal
546                             && vbl_sig->task == current) {
547                                 spin_unlock_irqrestore(&dev->vbl_lock,
548                                                        irqflags);
549                                 vblwait->reply.sequence = seq;
550                                 goto done;
551                         }
552                 }
553
554                 if (atomic_read(&dev->vbl_signal_pending) >= 100) {
555                         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
556                         return -EBUSY;
557                 }
558
559                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
560
561                 vbl_sig = drm_calloc(1, sizeof(struct drm_vbl_sig),
562                                      DRM_MEM_DRIVER);
563                 if (!vbl_sig)
564                         return -ENOMEM;
565
566                 ret = drm_vblank_get(dev, crtc);
567                 if (ret) {
568                         drm_free(vbl_sig, sizeof(struct drm_vbl_sig),
569                                  DRM_MEM_DRIVER);
570                         return ret;
571                 }
572
573                 atomic_inc(&dev->vbl_signal_pending);
574
575                 vbl_sig->sequence = vblwait->request.sequence;
576                 vbl_sig->info.si_signo = vblwait->request.signal;
577                 vbl_sig->task = current;
578
579                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
580
581                 list_add_tail(&vbl_sig->head, vbl_sigs);
582
583                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
584
585                 vblwait->reply.sequence = seq;
586         } else {
587                 unsigned long cur_vblank;
588
589                 ret = drm_vblank_get(dev, crtc);
590                 if (ret)
591                         return ret;
592                 DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ,
593                             (((cur_vblank = drm_vblank_count(dev, crtc))
594                               - vblwait->request.sequence) <= (1 << 23)));
595                 drm_vblank_put(dev, crtc);
596                 do_gettimeofday(&now);
597
598                 vblwait->reply.tval_sec = now.tv_sec;
599                 vblwait->reply.tval_usec = now.tv_usec;
600                 vblwait->reply.sequence = cur_vblank;
601         }
602
603       done:
604         return ret;
605 }
606
607 /**
608  * Send the VBLANK signals.
609  *
610  * \param dev DRM device.
611  * \param crtc CRTC where the vblank event occurred
612  *
613  * Sends a signal for each task in drm_device::vbl_sigs and empties the list.
614  *
615  * If a signal is not requested, then calls vblank_wait().
616  */
617 static void drm_vbl_send_signals(struct drm_device * dev, int crtc)
618 {
619         struct drm_vbl_sig *vbl_sig, *tmp;
620         struct list_head *vbl_sigs;
621         unsigned int vbl_seq;
622         unsigned long flags;
623
624         spin_lock_irqsave(&dev->vbl_lock, flags);
625
626         vbl_sigs = &dev->vbl_sigs[crtc];
627         vbl_seq = drm_vblank_count(dev, crtc);
628
629         list_for_each_entry_safe(vbl_sig, tmp, vbl_sigs, head) {
630             if ((vbl_seq - vbl_sig->sequence) <= (1 << 23)) {
631                 vbl_sig->info.si_code = vbl_seq;
632                 send_sig_info(vbl_sig->info.si_signo,
633                               &vbl_sig->info, vbl_sig->task);
634
635                 list_del(&vbl_sig->head);
636
637                 drm_free(vbl_sig, sizeof(*vbl_sig),
638                          DRM_MEM_DRIVER);
639                 atomic_dec(&dev->vbl_signal_pending);
640                 drm_vblank_put(dev, crtc);
641             }
642         }
643
644         spin_unlock_irqrestore(&dev->vbl_lock, flags);
645 }
646
647 /**
648  * drm_handle_vblank - handle a vblank event
649  * @dev: DRM device
650  * @crtc: where this event occurred
651  *
652  * Drivers should call this routine in their vblank interrupt handlers to
653  * update the vblank counter and send any signals that may be pending.
654  */
655 void drm_handle_vblank(struct drm_device *dev, int crtc)
656 {
657         drm_update_vblank_count(dev, crtc);
658         DRM_WAKEUP(&dev->vbl_queue[crtc]);
659         drm_vbl_send_signals(dev, crtc);
660 }
661 EXPORT_SYMBOL(drm_handle_vblank);
662
663 /**
664  * Tasklet wrapper function.
665  *
666  * \param data DRM device in disguise.
667  *
668  * Attempts to grab the HW lock and calls the driver callback on success. On
669  * failure, leave the lock marked as contended so the callback can be called
670  * from drm_unlock().
671  */
672 static void drm_locked_tasklet_func(unsigned long data)
673 {
674         struct drm_device *dev = (struct drm_device *)data;
675         unsigned long irqflags;
676
677         spin_lock_irqsave(&dev->tasklet_lock, irqflags);
678
679         if (!dev->locked_tasklet_func ||
680             !drm_lock_take(&dev->lock,
681                            DRM_KERNEL_CONTEXT)) {
682                 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
683                 return;
684         }
685
686         dev->lock.lock_time = jiffies;
687         atomic_inc(&dev->counts[_DRM_STAT_LOCKS]);
688
689         dev->locked_tasklet_func(dev);
690
691         drm_lock_free(&dev->lock,
692                       DRM_KERNEL_CONTEXT);
693
694         dev->locked_tasklet_func = NULL;
695
696         spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
697 }
698
699 /**
700  * Schedule a tasklet to call back a driver hook with the HW lock held.
701  *
702  * \param dev DRM device.
703  * \param func Driver callback.
704  *
705  * This is intended for triggering actions that require the HW lock from an
706  * interrupt handler. The lock will be grabbed ASAP after the interrupt handler
707  * completes. Note that the callback may be called from interrupt or process
708  * context, it must not make any assumptions about this. Also, the HW lock will
709  * be held with the kernel context or any client context.
710  */
711 void drm_locked_tasklet(struct drm_device *dev, void (*func)(struct drm_device *))
712 {
713         unsigned long irqflags;
714         static DECLARE_TASKLET(drm_tasklet, drm_locked_tasklet_func, 0);
715
716         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ) ||
717             test_bit(TASKLET_STATE_SCHED, &drm_tasklet.state))
718                 return;
719
720         spin_lock_irqsave(&dev->tasklet_lock, irqflags);
721
722         if (dev->locked_tasklet_func) {
723                 spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
724                 return;
725         }
726
727         dev->locked_tasklet_func = func;
728
729         spin_unlock_irqrestore(&dev->tasklet_lock, irqflags);
730
731         drm_tasklet.data = (unsigned long)dev;
732
733         tasklet_hi_schedule(&drm_tasklet);
734 }
735 EXPORT_SYMBOL(drm_locked_tasklet);