upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / gpu / 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 #include "drm_trace.h"
38
39 #include <linux/interrupt.h>    /* For task queue support */
40 #include <linux/slab.h>
41
42 #include <linux/vgaarb.h>
43 /**
44  * Get interrupt from bus id.
45  *
46  * \param inode device inode.
47  * \param file_priv DRM file private.
48  * \param cmd command.
49  * \param arg user argument, pointing to a drm_irq_busid structure.
50  * \return zero on success or a negative number on failure.
51  *
52  * Finds the PCI device with the specified bus id and gets its IRQ number.
53  * This IOCTL is deprecated, and will now return EINVAL for any busid not equal
54  * to that of the device that this DRM instance attached to.
55  */
56 int drm_irq_by_busid(struct drm_device *dev, void *data,
57                      struct drm_file *file_priv)
58 {
59         struct drm_irq_busid *p = data;
60
61         if (drm_core_check_feature(dev, DRIVER_USE_PLATFORM_DEVICE))
62                 return -EINVAL;
63
64         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
65                 return -EINVAL;
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->pdev->irq;
73
74         DRM_DEBUG("%d:%d:%d => IRQ %d\n", p->busnum, p->devnum, p->funcnum,
75                   p->irq);
76
77         return 0;
78 }
79
80 static void vblank_disable_fn(unsigned long arg)
81 {
82         struct drm_device *dev = (struct drm_device *)arg;
83         unsigned long irqflags;
84         int i;
85
86         if (!dev->vblank_disable_allowed)
87                 return;
88
89         for (i = 0; i < dev->num_crtcs; i++) {
90                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
91                 if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
92                     dev->vblank_enabled[i]) {
93                         DRM_DEBUG("disabling vblank on crtc %d\n", i);
94                         dev->last_vblank[i] =
95                                 dev->driver->get_vblank_counter(dev, i);
96                         dev->driver->disable_vblank(dev, i);
97                         dev->vblank_enabled[i] = 0;
98                 }
99                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
100         }
101 }
102
103 void drm_vblank_cleanup(struct drm_device *dev)
104 {
105         /* Bail if the driver didn't call drm_vblank_init() */
106         if (dev->num_crtcs == 0)
107                 return;
108
109         del_timer(&dev->vblank_disable_timer);
110
111         vblank_disable_fn((unsigned long)dev);
112
113         kfree(dev->vbl_queue);
114         kfree(dev->_vblank_count);
115         kfree(dev->vblank_refcount);
116         kfree(dev->vblank_enabled);
117         kfree(dev->last_vblank);
118         kfree(dev->last_vblank_wait);
119         kfree(dev->vblank_inmodeset);
120
121         dev->num_crtcs = 0;
122 }
123 EXPORT_SYMBOL(drm_vblank_cleanup);
124
125 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
126 {
127         int i, ret = -ENOMEM;
128
129         setup_timer(&dev->vblank_disable_timer, vblank_disable_fn,
130                     (unsigned long)dev);
131         spin_lock_init(&dev->vbl_lock);
132         dev->num_crtcs = num_crtcs;
133
134         dev->vbl_queue = kmalloc(sizeof(wait_queue_head_t) * num_crtcs,
135                                  GFP_KERNEL);
136         if (!dev->vbl_queue)
137                 goto err;
138
139         dev->_vblank_count = kmalloc(sizeof(atomic_t) * num_crtcs, GFP_KERNEL);
140         if (!dev->_vblank_count)
141                 goto err;
142
143         dev->vblank_refcount = kmalloc(sizeof(atomic_t) * num_crtcs,
144                                        GFP_KERNEL);
145         if (!dev->vblank_refcount)
146                 goto err;
147
148         dev->vblank_enabled = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
149         if (!dev->vblank_enabled)
150                 goto err;
151
152         dev->last_vblank = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
153         if (!dev->last_vblank)
154                 goto err;
155
156         dev->last_vblank_wait = kcalloc(num_crtcs, sizeof(u32), GFP_KERNEL);
157         if (!dev->last_vblank_wait)
158                 goto err;
159
160         dev->vblank_inmodeset = kcalloc(num_crtcs, sizeof(int), GFP_KERNEL);
161         if (!dev->vblank_inmodeset)
162                 goto err;
163
164         /* Zero per-crtc vblank stuff */
165         for (i = 0; i < num_crtcs; i++) {
166                 init_waitqueue_head(&dev->vbl_queue[i]);
167                 atomic_set(&dev->_vblank_count[i], 0);
168                 atomic_set(&dev->vblank_refcount[i], 0);
169         }
170
171         dev->vblank_disable_allowed = 0;
172         return 0;
173
174 err:
175         drm_vblank_cleanup(dev);
176         return ret;
177 }
178 EXPORT_SYMBOL(drm_vblank_init);
179
180 static void drm_irq_vgaarb_nokms(void *cookie, bool state)
181 {
182         struct drm_device *dev = cookie;
183
184         if (dev->driver->vgaarb_irq) {
185                 dev->driver->vgaarb_irq(dev, state);
186                 return;
187         }
188
189         if (!dev->irq_enabled)
190                 return;
191
192         if (state)
193                 dev->driver->irq_uninstall(dev);
194         else {
195                 dev->driver->irq_preinstall(dev);
196                 dev->driver->irq_postinstall(dev);
197         }
198 }
199
200 /**
201  * Install IRQ handler.
202  *
203  * \param dev DRM device.
204  *
205  * Initializes the IRQ related data. Installs the handler, calling the driver
206  * \c drm_driver_irq_preinstall() and \c drm_driver_irq_postinstall() functions
207  * before and after the installation.
208  */
209 int drm_irq_install(struct drm_device *dev)
210 {
211         int ret = 0;
212         unsigned long sh_flags = 0;
213         char *irqname;
214
215         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
216                 return -EINVAL;
217
218         if (drm_dev_to_irq(dev) == 0)
219                 return -EINVAL;
220
221         mutex_lock(&dev->struct_mutex);
222
223         /* Driver must have been initialized */
224         if (!dev->dev_private) {
225                 mutex_unlock(&dev->struct_mutex);
226                 return -EINVAL;
227         }
228
229         if (dev->irq_enabled) {
230                 mutex_unlock(&dev->struct_mutex);
231                 return -EBUSY;
232         }
233         dev->irq_enabled = 1;
234         mutex_unlock(&dev->struct_mutex);
235
236         DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
237
238         /* Before installing handler */
239         dev->driver->irq_preinstall(dev);
240
241         /* Install handler */
242         if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
243                 sh_flags = IRQF_SHARED;
244
245         if (dev->devname)
246                 irqname = dev->devname;
247         else
248                 irqname = dev->driver->name;
249
250         ret = request_irq(drm_dev_to_irq(dev), dev->driver->irq_handler,
251                           sh_flags, irqname, dev);
252
253         if (ret < 0) {
254                 mutex_lock(&dev->struct_mutex);
255                 dev->irq_enabled = 0;
256                 mutex_unlock(&dev->struct_mutex);
257                 return ret;
258         }
259
260         if (!drm_core_check_feature(dev, DRIVER_MODESET))
261                 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
262
263         /* After installing handler */
264         ret = dev->driver->irq_postinstall(dev);
265         if (ret < 0) {
266                 mutex_lock(&dev->struct_mutex);
267                 dev->irq_enabled = 0;
268                 mutex_unlock(&dev->struct_mutex);
269         }
270
271         return ret;
272 }
273 EXPORT_SYMBOL(drm_irq_install);
274
275 /**
276  * Uninstall the IRQ handler.
277  *
278  * \param dev DRM device.
279  *
280  * Calls the driver's \c drm_driver_irq_uninstall() function, and stops the irq.
281  */
282 int drm_irq_uninstall(struct drm_device * dev)
283 {
284         unsigned long irqflags;
285         int irq_enabled, i;
286
287         if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
288                 return -EINVAL;
289
290         mutex_lock(&dev->struct_mutex);
291         irq_enabled = dev->irq_enabled;
292         dev->irq_enabled = 0;
293         mutex_unlock(&dev->struct_mutex);
294
295         /*
296          * Wake up any waiters so they don't hang.
297          */
298         spin_lock_irqsave(&dev->vbl_lock, irqflags);
299         for (i = 0; i < dev->num_crtcs; i++) {
300                 DRM_WAKEUP(&dev->vbl_queue[i]);
301                 dev->vblank_enabled[i] = 0;
302                 dev->last_vblank[i] = dev->driver->get_vblank_counter(dev, i);
303         }
304         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
305
306         if (!irq_enabled)
307                 return -EINVAL;
308
309         DRM_DEBUG("irq=%d\n", drm_dev_to_irq(dev));
310
311         if (!drm_core_check_feature(dev, DRIVER_MODESET))
312                 vga_client_register(dev->pdev, NULL, NULL, NULL);
313
314         dev->driver->irq_uninstall(dev);
315
316         free_irq(drm_dev_to_irq(dev), dev);
317
318         return 0;
319 }
320 EXPORT_SYMBOL(drm_irq_uninstall);
321
322 /**
323  * IRQ control ioctl.
324  *
325  * \param inode device inode.
326  * \param file_priv DRM file private.
327  * \param cmd command.
328  * \param arg user argument, pointing to a drm_control structure.
329  * \return zero on success or a negative number on failure.
330  *
331  * Calls irq_install() or irq_uninstall() according to \p arg.
332  */
333 int drm_control(struct drm_device *dev, void *data,
334                 struct drm_file *file_priv)
335 {
336         struct drm_control *ctl = data;
337
338         /* if we haven't irq we fallback for compatibility reasons - this used to be a separate function in drm_dma.h */
339
340
341         switch (ctl->func) {
342         case DRM_INST_HANDLER:
343                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
344                         return 0;
345                 if (drm_core_check_feature(dev, DRIVER_MODESET))
346                         return 0;
347                 /* 
348                  * Currently ARM DRI, there is no way get Xserver to know the
349                  * platform irq number.
350                  *
351                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
352                     ctl->irq != drm_dev_to_irq(dev))
353                         return -EINVAL;*/
354                 return drm_irq_install(dev);
355         case DRM_UNINST_HANDLER:
356                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
357                         return 0;
358                 if (drm_core_check_feature(dev, DRIVER_MODESET))
359                         return 0;
360                 return drm_irq_uninstall(dev);
361         default:
362                 return -EINVAL;
363         }
364 }
365
366 /**
367  * drm_vblank_count - retrieve "cooked" vblank counter value
368  * @dev: DRM device
369  * @crtc: which counter to retrieve
370  *
371  * Fetches the "cooked" vblank count value that represents the number of
372  * vblank events since the system was booted, including lost events due to
373  * modesetting activity.
374  */
375 u32 drm_vblank_count(struct drm_device *dev, int crtc)
376 {
377         return atomic_read(&dev->_vblank_count[crtc]);
378 }
379 EXPORT_SYMBOL(drm_vblank_count);
380
381 /**
382  * drm_update_vblank_count - update the master vblank counter
383  * @dev: DRM device
384  * @crtc: counter to update
385  *
386  * Call back into the driver to update the appropriate vblank counter
387  * (specified by @crtc).  Deal with wraparound, if it occurred, and
388  * update the last read value so we can deal with wraparound on the next
389  * call if necessary.
390  *
391  * Only necessary when going from off->on, to account for frames we
392  * didn't get an interrupt for.
393  *
394  * Note: caller must hold dev->vbl_lock since this reads & writes
395  * device vblank fields.
396  */
397 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
398 {
399         u32 cur_vblank, diff;
400
401         /*
402          * Interrupts were disabled prior to this call, so deal with counter
403          * wrap if needed.
404          * NOTE!  It's possible we lost a full dev->max_vblank_count events
405          * here if the register is small or we had vblank interrupts off for
406          * a long time.
407          */
408         cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
409         diff = cur_vblank - dev->last_vblank[crtc];
410         if (cur_vblank < dev->last_vblank[crtc]) {
411                 diff += dev->max_vblank_count;
412
413                 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
414                           crtc, dev->last_vblank[crtc], cur_vblank, diff);
415         }
416
417         DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
418                   crtc, diff);
419
420         atomic_add(diff, &dev->_vblank_count[crtc]);
421 }
422
423 /**
424  * drm_vblank_get - get a reference count on vblank events
425  * @dev: DRM device
426  * @crtc: which CRTC to own
427  *
428  * Acquire a reference count on vblank events to avoid having them disabled
429  * while in use.
430  *
431  * RETURNS
432  * Zero on success, nonzero on failure.
433  */
434 int drm_vblank_get(struct drm_device *dev, int crtc)
435 {
436         unsigned long irqflags;
437         int ret = 0;
438
439         spin_lock_irqsave(&dev->vbl_lock, irqflags);
440         /* Going from 0->1 means we have to enable interrupts again */
441         if (atomic_add_return(1, &dev->vblank_refcount[crtc]) == 1) {
442                 if (!dev->vblank_enabled[crtc]) {
443                         ret = dev->driver->enable_vblank(dev, crtc);
444                         DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
445                         if (ret)
446                                 atomic_dec(&dev->vblank_refcount[crtc]);
447                         else {
448                                 dev->vblank_enabled[crtc] = 1;
449                                 drm_update_vblank_count(dev, crtc);
450                         }
451                 }
452         } else {
453                 if (!dev->vblank_enabled[crtc]) {
454                         atomic_dec(&dev->vblank_refcount[crtc]);
455                         ret = -EINVAL;
456                 }
457         }
458         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
459
460         return ret;
461 }
462 EXPORT_SYMBOL(drm_vblank_get);
463
464 /**
465  * drm_vblank_put - give up ownership of vblank events
466  * @dev: DRM device
467  * @crtc: which counter to give up
468  *
469  * Release ownership of a given vblank counter, turning off interrupts
470  * if possible.
471  */
472 void drm_vblank_put(struct drm_device *dev, int crtc)
473 {
474         BUG_ON (atomic_read (&dev->vblank_refcount[crtc]) == 0);
475
476         /* Last user schedules interrupt disable */
477         if (atomic_dec_and_test(&dev->vblank_refcount[crtc]))
478                 mod_timer(&dev->vblank_disable_timer, jiffies + 5*DRM_HZ);
479 }
480 EXPORT_SYMBOL(drm_vblank_put);
481
482 void drm_vblank_off(struct drm_device *dev, int crtc)
483 {
484         unsigned long irqflags;
485
486         spin_lock_irqsave(&dev->vbl_lock, irqflags);
487         dev->driver->disable_vblank(dev, crtc);
488         DRM_WAKEUP(&dev->vbl_queue[crtc]);
489         dev->vblank_enabled[crtc] = 0;
490         dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
491         spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
492 }
493 EXPORT_SYMBOL(drm_vblank_off);
494
495 /**
496  * drm_vblank_pre_modeset - account for vblanks across mode sets
497  * @dev: DRM device
498  * @crtc: CRTC in question
499  * @post: post or pre mode set?
500  *
501  * Account for vblank events across mode setting events, which will likely
502  * reset the hardware frame counter.
503  */
504 void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
505 {
506         /* vblank is not initialized (IRQ not installed ?) */
507         if (!dev->num_crtcs)
508                 return;
509         /*
510          * To avoid all the problems that might happen if interrupts
511          * were enabled/disabled around or between these calls, we just
512          * have the kernel take a reference on the CRTC (just once though
513          * to avoid corrupting the count if multiple, mismatch calls occur),
514          * so that interrupts remain enabled in the interim.
515          */
516         if (!dev->vblank_inmodeset[crtc]) {
517                 dev->vblank_inmodeset[crtc] = 0x1;
518                 if (drm_vblank_get(dev, crtc) == 0)
519                         dev->vblank_inmodeset[crtc] |= 0x2;
520         }
521 }
522 EXPORT_SYMBOL(drm_vblank_pre_modeset);
523
524 void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
525 {
526         unsigned long irqflags;
527
528         if (dev->vblank_inmodeset[crtc]) {
529                 spin_lock_irqsave(&dev->vbl_lock, irqflags);
530                 dev->vblank_disable_allowed = 1;
531                 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
532
533                 if (dev->vblank_inmodeset[crtc] & 0x2)
534                         drm_vblank_put(dev, crtc);
535
536                 dev->vblank_inmodeset[crtc] = 0;
537         }
538 }
539 EXPORT_SYMBOL(drm_vblank_post_modeset);
540
541 /**
542  * drm_modeset_ctl - handle vblank event counter changes across mode switch
543  * @DRM_IOCTL_ARGS: standard ioctl arguments
544  *
545  * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
546  * ioctls around modesetting so that any lost vblank events are accounted for.
547  *
548  * Generally the counter will reset across mode sets.  If interrupts are
549  * enabled around this call, we don't have to do anything since the counter
550  * will have already been incremented.
551  */
552 int drm_modeset_ctl(struct drm_device *dev, void *data,
553                     struct drm_file *file_priv)
554 {
555         struct drm_modeset_ctl *modeset = data;
556         int crtc, ret = 0;
557
558         /* If drm_vblank_init() hasn't been called yet, just no-op */
559         if (!dev->num_crtcs)
560                 goto out;
561
562         crtc = modeset->crtc;
563         if (crtc >= dev->num_crtcs) {
564                 ret = -EINVAL;
565                 goto out;
566         }
567
568         switch (modeset->cmd) {
569         case _DRM_PRE_MODESET:
570                 drm_vblank_pre_modeset(dev, crtc);
571                 break;
572         case _DRM_POST_MODESET:
573                 drm_vblank_post_modeset(dev, crtc);
574                 break;
575         default:
576                 ret = -EINVAL;
577                 break;
578         }
579
580 out:
581         return ret;
582 }
583
584 static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
585                                   union drm_wait_vblank *vblwait,
586                                   struct drm_file *file_priv)
587 {
588         struct drm_pending_vblank_event *e;
589         struct timeval now;
590         unsigned long flags;
591         unsigned int seq;
592
593         e = kzalloc(sizeof *e, GFP_KERNEL);
594         if (e == NULL)
595                 return -ENOMEM;
596
597         e->pipe = pipe;
598         e->base.pid = current->pid;
599         e->event.base.type = DRM_EVENT_VBLANK;
600         e->event.base.length = sizeof e->event;
601         e->event.user_data = vblwait->request.signal;
602         e->base.event = &e->event.base;
603         e->base.file_priv = file_priv;
604         e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
605
606         do_gettimeofday(&now);
607
608         spin_lock_irqsave(&dev->event_lock, flags);
609
610         if (file_priv->event_space < sizeof e->event) {
611                 spin_unlock_irqrestore(&dev->event_lock, flags);
612                 kfree(e);
613                 return -ENOMEM;
614         }
615
616         file_priv->event_space -= sizeof e->event;
617         seq = drm_vblank_count(dev, pipe);
618         if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
619             (seq - vblwait->request.sequence) <= (1 << 23)) {
620                 vblwait->request.sequence = seq + 1;
621                 vblwait->reply.sequence = vblwait->request.sequence;
622         }
623
624         DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
625                   vblwait->request.sequence, seq, pipe);
626
627         trace_drm_vblank_event_queued(current->pid, pipe,
628                                       vblwait->request.sequence);
629
630         e->event.sequence = vblwait->request.sequence;
631         if ((seq - vblwait->request.sequence) <= (1 << 23)) {
632                 e->event.tv_sec = now.tv_sec;
633                 e->event.tv_usec = now.tv_usec;
634                 drm_vblank_put(dev, e->pipe);
635                 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
636                 wake_up_interruptible(&e->base.file_priv->event_wait);
637                 trace_drm_vblank_event_delivered(current->pid, pipe,
638                                                  vblwait->request.sequence);
639         } else {
640                 list_add_tail(&e->base.link, &dev->vblank_event_list);
641         }
642
643         spin_unlock_irqrestore(&dev->event_lock, flags);
644
645         return 0;
646 }
647
648 /**
649  * Wait for VBLANK.
650  *
651  * \param inode device inode.
652  * \param file_priv DRM file private.
653  * \param cmd command.
654  * \param data user argument, pointing to a drm_wait_vblank structure.
655  * \return zero on success or a negative number on failure.
656  *
657  * This function enables the vblank interrupt on the pipe requested, then
658  * sleeps waiting for the requested sequence number to occur, and drops
659  * the vblank interrupt refcount afterwards. (vblank irq disable follows that
660  * after a timeout with no further vblank waits scheduled).
661  */
662 int drm_wait_vblank(struct drm_device *dev, void *data,
663                     struct drm_file *file_priv)
664 {
665         union drm_wait_vblank *vblwait = data;
666         int ret = 0;
667         unsigned int flags, seq, crtc;
668
669         if ((!drm_dev_to_irq(dev)) || (!dev->irq_enabled))
670                 return -EINVAL;
671
672         if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
673                 return -EINVAL;
674
675         if (vblwait->request.type &
676             ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK)) {
677                 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
678                           vblwait->request.type,
679                           (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK));
680                 return -EINVAL;
681         }
682
683         flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
684         crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
685
686         if (crtc >= dev->num_crtcs)
687                 return -EINVAL;
688
689         ret = drm_vblank_get(dev, crtc);
690         if (ret) {
691                 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
692                 return ret;
693         }
694         seq = drm_vblank_count(dev, crtc);
695
696         switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
697         case _DRM_VBLANK_RELATIVE:
698                 vblwait->request.sequence += seq;
699                 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
700         case _DRM_VBLANK_ABSOLUTE:
701                 break;
702         default:
703                 ret = -EINVAL;
704                 goto done;
705         }
706
707         if (flags & _DRM_VBLANK_EVENT)
708                 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
709
710         if ((flags & _DRM_VBLANK_NEXTONMISS) &&
711             (seq - vblwait->request.sequence) <= (1<<23)) {
712                 vblwait->request.sequence = seq + 1;
713         }
714
715         DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
716                   vblwait->request.sequence, crtc);
717         dev->last_vblank_wait[crtc] = vblwait->request.sequence;
718         DRM_WAIT_ON(ret, dev->vbl_queue[crtc], 3 * DRM_HZ,
719                     (((drm_vblank_count(dev, crtc) -
720                        vblwait->request.sequence) <= (1 << 23)) ||
721                      !dev->irq_enabled));
722
723         if (ret != -EINTR) {
724                 struct timeval now;
725
726                 do_gettimeofday(&now);
727
728                 vblwait->reply.tval_sec = now.tv_sec;
729                 vblwait->reply.tval_usec = now.tv_usec;
730                 vblwait->reply.sequence = drm_vblank_count(dev, crtc);
731                 DRM_DEBUG("returning %d to client\n",
732                           vblwait->reply.sequence);
733         } else {
734                 DRM_DEBUG("vblank wait interrupted by signal\n");
735         }
736
737 done:
738         drm_vblank_put(dev, crtc);
739         return ret;
740 }
741
742 void drm_handle_vblank_events(struct drm_device *dev, int crtc)
743 {
744         struct drm_pending_vblank_event *e, *t;
745         struct timeval now;
746         unsigned long flags;
747         unsigned int seq;
748
749         do_gettimeofday(&now);
750         seq = drm_vblank_count(dev, crtc);
751
752         spin_lock_irqsave(&dev->event_lock, flags);
753
754         list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
755                 if (e->pipe != crtc)
756                         continue;
757                 if ((seq - e->event.sequence) > (1<<23))
758                         continue;
759
760                 DRM_DEBUG("vblank event on %d, current %d\n",
761                           e->event.sequence, seq);
762
763                 e->event.sequence = seq;
764                 e->event.tv_sec = now.tv_sec;
765                 e->event.tv_usec = now.tv_usec;
766                 drm_vblank_put(dev, e->pipe);
767                 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
768                 wake_up_interruptible(&e->base.file_priv->event_wait);
769                 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
770                                                  e->event.sequence);
771         }
772
773         spin_unlock_irqrestore(&dev->event_lock, flags);
774
775         trace_drm_vblank_event(crtc, seq);
776 }
777
778 /**
779  * drm_handle_vblank - handle a vblank event
780  * @dev: DRM device
781  * @crtc: where this event occurred
782  *
783  * Drivers should call this routine in their vblank interrupt handlers to
784  * update the vblank counter and send any signals that may be pending.
785  */
786 void drm_handle_vblank(struct drm_device *dev, int crtc)
787 {
788         if (!dev->num_crtcs)
789                 return;
790
791         atomic_inc(&dev->_vblank_count[crtc]);
792         DRM_WAKEUP(&dev->vbl_queue[crtc]);
793         drm_handle_vblank_events(dev, crtc);
794 }
795 EXPORT_SYMBOL(drm_handle_vblank);