drm: Merge the drm and drm2 implementations
[dragonfly.git] / sys / dev / drm / drm_irq.c
1 /*-
2  * Copyright 2003 Eric Anholt
3  * All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * ERIC ANHOLT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <anholt@FreeBSD.org>
25  *
26  * $FreeBSD: src/sys/dev/drm2/drm_irq.c,v 1.1 2012/05/22 11:07:44 kib Exp $
27  */
28
29 /** @file drm_irq.c
30  * Support code for handling setup/teardown of interrupt handlers and
31  * handing interrupt handlers off to the drivers.
32  */
33
34 #include "dev/drm/drmP.h"
35 #include "dev/drm/drm.h"
36
37 MALLOC_DEFINE(DRM_MEM_VBLANK, "drm_vblank", "DRM VBLANK Handling Data");
38
39 /* Access macro for slots in vblank timestamp ringbuffer. */
40 #define vblanktimestamp(dev, crtc, count) ( \
41         (dev)->_vblank_time[(crtc) * DRM_VBLANKTIME_RBSIZE + \
42         ((count) % DRM_VBLANKTIME_RBSIZE)])
43
44 /* Retry timestamp calculation up to 3 times to satisfy
45  * drm_timestamp_precision before giving up.
46  */
47 #define DRM_TIMESTAMP_MAXRETRIES 3
48
49 /* Threshold in nanoseconds for detection of redundant
50  * vblank irq in drm_handle_vblank(). 1 msec should be ok.
51  */
52 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
53
54 int drm_irq_by_busid(struct drm_device *dev, void *data,
55                      struct drm_file *file_priv)
56 {
57         struct drm_irq_busid *irq = data;
58
59         if ((irq->busnum >> 8) != dev->pci_domain ||
60             (irq->busnum & 0xff) != dev->pci_bus ||
61             irq->devnum != dev->pci_slot ||
62             irq->funcnum != dev->pci_func)
63                 return EINVAL;
64
65         irq->irq = dev->irq;
66
67         DRM_DEBUG("%d:%d:%d => IRQ %d\n",
68             irq->busnum, irq->devnum, irq->funcnum, irq->irq);
69
70         return 0;
71 }
72
73 int
74 drm_irq_install(struct drm_device *dev)
75 {
76         int retcode;
77
78         if (dev->irq == 0 || dev->dev_private == NULL)
79                 return (EINVAL);
80
81         DRM_DEBUG("irq=%d\n", dev->irq);
82
83         DRM_LOCK(dev);
84         if (dev->irq_enabled) {
85                 DRM_UNLOCK(dev);
86                 return EBUSY;
87         }
88         dev->irq_enabled = 1;
89
90         dev->context_flag = 0;
91
92         /* Before installing handler */
93         if (dev->driver->irq_preinstall)
94                 dev->driver->irq_preinstall(dev);
95         DRM_UNLOCK(dev);
96
97         /* Install handler */
98         retcode = bus_setup_intr(dev->device, dev->irqr, INTR_MPSAFE,
99             dev->driver->irq_handler, dev, &dev->irqh, &dev->irq_lock);
100         if (retcode != 0)
101                 goto err;
102
103         /* After installing handler */
104         DRM_LOCK(dev);
105         if (dev->driver->irq_postinstall)
106                 dev->driver->irq_postinstall(dev);
107         DRM_UNLOCK(dev);
108
109         return (0);
110 err:
111         device_printf(dev->device, "Error setting interrupt: %d\n", retcode);
112         dev->irq_enabled = 0;
113
114         return (retcode);
115 }
116
117 int drm_irq_uninstall(struct drm_device *dev)
118 {
119         int i;
120
121         if (!dev->irq_enabled)
122                 return EINVAL;
123
124         dev->irq_enabled = 0;
125
126         /*
127         * Wake up any waiters so they don't hang.
128         */
129         if (dev->num_crtcs) {
130                 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE);
131                 for (i = 0; i < dev->num_crtcs; i++) {
132                         wakeup(&dev->_vblank_count[i]);
133                         dev->vblank_enabled[i] = 0;
134                         dev->last_vblank[i] =
135                                 dev->driver->get_vblank_counter(dev, i);
136                 }
137                 lockmgr(&dev->vbl_lock, LK_RELEASE);
138         }
139
140         DRM_DEBUG("irq=%d\n", dev->irq);
141
142         if (dev->driver->irq_uninstall)
143                 dev->driver->irq_uninstall(dev);
144
145         DRM_UNLOCK(dev);
146         bus_teardown_intr(dev->device, dev->irqr, dev->irqh);
147         DRM_LOCK(dev);
148
149         return 0;
150 }
151
152 int drm_control(struct drm_device *dev, void *data, struct drm_file *file_priv)
153 {
154         struct drm_control *ctl = data;
155         int err;
156
157         switch (ctl->func) {
158         case DRM_INST_HANDLER:
159                 /* Handle drivers whose DRM used to require IRQ setup but the
160                  * no longer does.
161                  */
162                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
163                         return 0;
164                 if (drm_core_check_feature(dev, DRIVER_MODESET))
165                         return 0;
166                 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
167                     ctl->irq != dev->irq)
168                         return EINVAL;
169                 return drm_irq_install(dev);
170         case DRM_UNINST_HANDLER:
171                 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
172                         return 0;
173                 if (drm_core_check_feature(dev, DRIVER_MODESET))
174                         return 0;
175                 DRM_LOCK(dev);
176                 err = drm_irq_uninstall(dev);
177                 DRM_UNLOCK(dev);
178                 return err;
179         default:
180                 return EINVAL;
181         }
182 }
183
184 #define NSEC_PER_USEC   1000L
185 #define NSEC_PER_SEC    1000000000L
186
187 int64_t
188 timeval_to_ns(const struct timeval *tv)
189 {
190         return ((int64_t)tv->tv_sec * NSEC_PER_SEC) +
191                 tv->tv_usec * NSEC_PER_USEC;
192 }
193
194 struct timeval
195 ns_to_timeval(const int64_t nsec)
196 {
197         struct timeval tv;
198         uint32_t rem;
199
200         if (nsec == 0) {
201                 tv.tv_sec = 0;
202                 tv.tv_usec = 0;
203                 return (tv);
204         }
205
206         tv.tv_sec = nsec / NSEC_PER_SEC;
207         rem = nsec % NSEC_PER_SEC;
208         if (rem < 0) {
209                 tv.tv_sec--;
210                 rem += NSEC_PER_SEC;
211         }
212         tv.tv_usec = rem / 1000;
213         return (tv);
214 }
215
216 /*
217  * Clear vblank timestamp buffer for a crtc.
218  */
219 static void clear_vblank_timestamps(struct drm_device *dev, int crtc)
220 {
221         memset(&dev->_vblank_time[crtc * DRM_VBLANKTIME_RBSIZE], 0,
222                 DRM_VBLANKTIME_RBSIZE * sizeof(struct timeval));
223 }
224
225 static int64_t
226 abs64(int64_t x)
227 {
228
229         return (x < 0 ? -x : x);
230 }
231
232 /*
233  * Disable vblank irq's on crtc, make sure that last vblank count
234  * of hardware and corresponding consistent software vblank counter
235  * are preserved, even if there are any spurious vblank irq's after
236  * disable.
237  */
238 static void vblank_disable_and_save(struct drm_device *dev, int crtc)
239 {
240         u32 vblcount;
241         int64_t diff_ns;
242         int vblrc;
243         struct timeval tvblank;
244
245         /* Prevent vblank irq processing while disabling vblank irqs,
246          * so no updates of timestamps or count can happen after we've
247          * disabled. Needed to prevent races in case of delayed irq's.
248          */
249         lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
250
251         dev->driver->disable_vblank(dev, crtc);
252         dev->vblank_enabled[crtc] = 0;
253
254         /* No further vblank irq's will be processed after
255          * this point. Get current hardware vblank count and
256          * vblank timestamp, repeat until they are consistent.
257          *
258          * FIXME: There is still a race condition here and in
259          * drm_update_vblank_count() which can cause off-by-one
260          * reinitialization of software vblank counter. If gpu
261          * vblank counter doesn't increment exactly at the leading
262          * edge of a vblank interval, then we can lose 1 count if
263          * we happen to execute between start of vblank and the
264          * delayed gpu counter increment.
265          */
266         do {
267                 dev->last_vblank[crtc] = dev->driver->get_vblank_counter(dev, crtc);
268                 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
269         } while (dev->last_vblank[crtc] != dev->driver->get_vblank_counter(dev, crtc));
270
271         /* Compute time difference to stored timestamp of last vblank
272          * as updated by last invocation of drm_handle_vblank() in vblank irq.
273          */
274         vblcount = atomic_read(&dev->_vblank_count[crtc]);
275         diff_ns = timeval_to_ns(&tvblank) -
276                   timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
277
278         /* If there is at least 1 msec difference between the last stored
279          * timestamp and tvblank, then we are currently executing our
280          * disable inside a new vblank interval, the tvblank timestamp
281          * corresponds to this new vblank interval and the irq handler
282          * for this vblank didn't run yet and won't run due to our disable.
283          * Therefore we need to do the job of drm_handle_vblank() and
284          * increment the vblank counter by one to account for this vblank.
285          *
286          * Skip this step if there isn't any high precision timestamp
287          * available. In that case we can't account for this and just
288          * hope for the best.
289          */
290         if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
291                 atomic_inc(&dev->_vblank_count[crtc]);
292         }
293
294         /* Invalidate all timestamps while vblank irq's are off. */
295         clear_vblank_timestamps(dev, crtc);
296
297         lockmgr(&dev->vblank_time_lock, LK_RELEASE);
298 }
299
300 static void vblank_disable_fn(void * arg)
301 {
302         struct drm_device *dev = (struct drm_device *)arg;
303         int i;
304
305         if (!dev->vblank_disable_allowed)
306                 return;
307
308         for (i = 0; i < dev->num_crtcs; i++) {
309                 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE);
310                 if (atomic_read(&dev->vblank_refcount[i]) == 0 &&
311                     dev->vblank_enabled[i]) {
312                         DRM_DEBUG("disabling vblank on crtc %d\n", i);
313                         vblank_disable_and_save(dev, i);
314                 }
315                 lockmgr(&dev->vbl_lock, LK_RELEASE);
316         }
317 }
318
319 void drm_vblank_cleanup(struct drm_device *dev)
320 {
321         /* Bail if the driver didn't call drm_vblank_init() */
322         if (dev->num_crtcs == 0)
323                 return;
324
325         callout_stop(&dev->vblank_disable_callout);
326
327         vblank_disable_fn(dev);
328
329         drm_free(dev->_vblank_count, DRM_MEM_VBLANK);
330         drm_free(dev->vblank_refcount, DRM_MEM_VBLANK);
331         drm_free(dev->vblank_enabled, DRM_MEM_VBLANK);
332         drm_free(dev->last_vblank, DRM_MEM_VBLANK);
333         drm_free(dev->last_vblank_wait, DRM_MEM_VBLANK);
334         drm_free(dev->vblank_inmodeset, DRM_MEM_VBLANK);
335         drm_free(dev->_vblank_time, DRM_MEM_VBLANK);
336
337         dev->num_crtcs = 0;
338 }
339
340 int drm_vblank_init(struct drm_device *dev, int num_crtcs)
341 {
342         int i;
343
344         callout_init_mp(&dev->vblank_disable_callout);
345 #if 0
346         mtx_init(&dev->vbl_lock, "drmvbl", NULL, MTX_DEF);
347 #endif
348         lockinit(&dev->vblank_time_lock, "drmvtl", 0, LK_CANRECURSE);
349
350         dev->num_crtcs = num_crtcs;
351
352         dev->_vblank_count = kmalloc(sizeof(atomic_t) * num_crtcs,
353             DRM_MEM_VBLANK, M_WAITOK);
354         dev->vblank_refcount = kmalloc(sizeof(atomic_t) * num_crtcs,
355             DRM_MEM_VBLANK, M_WAITOK);
356         dev->vblank_enabled = kmalloc(num_crtcs * sizeof(int),
357             DRM_MEM_VBLANK, M_WAITOK | M_ZERO);
358         dev->last_vblank = kmalloc(num_crtcs * sizeof(u32),
359             DRM_MEM_VBLANK, M_WAITOK | M_ZERO);
360         dev->last_vblank_wait = kmalloc(num_crtcs * sizeof(u32),
361             DRM_MEM_VBLANK, M_WAITOK | M_ZERO);
362         dev->vblank_inmodeset = kmalloc(num_crtcs * sizeof(int),
363             DRM_MEM_VBLANK, M_WAITOK | M_ZERO);
364         dev->_vblank_time = kmalloc(num_crtcs * DRM_VBLANKTIME_RBSIZE *
365             sizeof(struct timeval), DRM_MEM_VBLANK, M_WAITOK | M_ZERO);
366         DRM_INFO("Supports vblank timestamp caching Rev 1 (10.10.2010).\n");
367
368         /* Driver specific high-precision vblank timestamping supported? */
369         if (dev->driver->get_vblank_timestamp)
370                 DRM_INFO("Driver supports precise vblank timestamp query.\n");
371         else
372                 DRM_INFO("No driver support for vblank timestamp query.\n");
373
374         /* Zero per-crtc vblank stuff */
375         for (i = 0; i < num_crtcs; i++) {
376                 atomic_set(&dev->_vblank_count[i], 0);
377                 atomic_set(&dev->vblank_refcount[i], 0);
378         }
379
380         dev->vblank_disable_allowed = 0;
381         return 0;
382 }
383
384 void
385 drm_calc_timestamping_constants(struct drm_crtc *crtc)
386 {
387         int64_t linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
388         uint64_t dotclock;
389
390         /* Dot clock in Hz: */
391         dotclock = (uint64_t) crtc->hwmode.clock * 1000;
392
393         /* Fields of interlaced scanout modes are only halve a frame duration.
394          * Double the dotclock to get halve the frame-/line-/pixelduration.
395          */
396         if (crtc->hwmode.flags & DRM_MODE_FLAG_INTERLACE)
397                 dotclock *= 2;
398
399         /* Valid dotclock? */
400         if (dotclock > 0) {
401                 /* Convert scanline length in pixels and video dot clock to
402                  * line duration, frame duration and pixel duration in
403                  * nanoseconds:
404                  */
405                 pixeldur_ns = (int64_t)1000000000 / dotclock;
406                 linedur_ns  = ((uint64_t)crtc->hwmode.crtc_htotal *
407                     1000000000) / dotclock;
408                 framedur_ns = (int64_t)crtc->hwmode.crtc_vtotal * linedur_ns;
409         } else
410                 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
411                           crtc->base.id);
412
413         crtc->pixeldur_ns = pixeldur_ns;
414         crtc->linedur_ns  = linedur_ns;
415         crtc->framedur_ns = framedur_ns;
416
417         DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
418                   crtc->base.id, crtc->hwmode.crtc_htotal,
419                   crtc->hwmode.crtc_vtotal, crtc->hwmode.crtc_vdisplay);
420         DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
421                   crtc->base.id, (int) dotclock/1000, (int) framedur_ns,
422                   (int) linedur_ns, (int) pixeldur_ns);
423 }
424
425 /**
426  * drm_calc_vbltimestamp_from_scanoutpos - helper routine for kms
427  * drivers. Implements calculation of exact vblank timestamps from
428  * given drm_display_mode timings and current video scanout position
429  * of a crtc. This can be called from within get_vblank_timestamp()
430  * implementation of a kms driver to implement the actual timestamping.
431  *
432  * Should return timestamps conforming to the OML_sync_control OpenML
433  * extension specification. The timestamp corresponds to the end of
434  * the vblank interval, aka start of scanout of topmost-leftmost display
435  * pixel in the following video frame.
436  *
437  * Requires support for optional dev->driver->get_scanout_position()
438  * in kms driver, plus a bit of setup code to provide a drm_display_mode
439  * that corresponds to the true scanout timing.
440  *
441  * The current implementation only handles standard video modes. It
442  * returns as no operation if a doublescan or interlaced video mode is
443  * active. Higher level code is expected to handle this.
444  *
445  * @dev: DRM device.
446  * @crtc: Which crtc's vblank timestamp to retrieve.
447  * @max_error: Desired maximum allowable error in timestamps (nanosecs).
448  *             On return contains true maximum error of timestamp.
449  * @vblank_time: Pointer to struct timeval which should receive the timestamp.
450  * @flags: Flags to pass to driver:
451  *         0 = Default.
452  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
453  * @refcrtc: drm_crtc* of crtc which defines scanout timing.
454  *
455  * Returns negative value on error, failure or if not supported in current
456  * video mode:
457  *
458  * -EINVAL   - Invalid crtc.
459  * -EAGAIN   - Temporary unavailable, e.g., called before initial modeset.
460  * -ENOTSUPP - Function not supported in current display mode.
461  * -EIO      - Failed, e.g., due to failed scanout position query.
462  *
463  * Returns or'ed positive status flags on success:
464  *
465  * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
466  * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
467  *
468  */
469 int
470 drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
471     int *max_error, struct timeval *vblank_time, unsigned flags,
472     struct drm_crtc *refcrtc)
473 {
474         struct timeval stime, raw_time;
475         struct drm_display_mode *mode;
476         int vbl_status, vtotal, vdisplay;
477         int vpos, hpos, i;
478         int64_t framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
479         bool invbl;
480
481         if (crtc < 0 || crtc >= dev->num_crtcs) {
482                 DRM_ERROR("Invalid crtc %d\n", crtc);
483                 return -EINVAL;
484         }
485
486         /* Scanout position query not supported? Should not happen. */
487         if (!dev->driver->get_scanout_position) {
488                 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
489                 return -EIO;
490         }
491
492         mode = &refcrtc->hwmode;
493         vtotal = mode->crtc_vtotal;
494         vdisplay = mode->crtc_vdisplay;
495
496         /* Durations of frames, lines, pixels in nanoseconds. */
497         framedur_ns = refcrtc->framedur_ns;
498         linedur_ns  = refcrtc->linedur_ns;
499         pixeldur_ns = refcrtc->pixeldur_ns;
500
501         /* If mode timing undefined, just return as no-op:
502          * Happens during initial modesetting of a crtc.
503          */
504         if (vtotal <= 0 || vdisplay <= 0 || framedur_ns == 0) {
505                 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
506                 return -EAGAIN;
507         }
508
509         /* Get current scanout position with system timestamp.
510          * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
511          * if single query takes longer than max_error nanoseconds.
512          *
513          * This guarantees a tight bound on maximum error if
514          * code gets preempted or delayed for some reason.
515          */
516         for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
517                 /* Disable preemption to make it very likely to
518                  * succeed in the first iteration.
519                  */
520                 crit_enter();
521
522                 /* Get system timestamp before query. */
523                 getmicrouptime(&stime);
524
525                 /* Get vertical and horizontal scanout pos. vpos, hpos. */
526                 vbl_status = dev->driver->get_scanout_position(dev, crtc, &vpos, &hpos);
527
528                 /* Get system timestamp after query. */
529                 getmicrouptime(&raw_time);
530
531                 crit_exit();
532
533                 /* Return as no-op if scanout query unsupported or failed. */
534                 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
535                         DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
536                                   crtc, vbl_status);
537                         return -EIO;
538                 }
539
540                 duration_ns = timeval_to_ns(&raw_time) - timeval_to_ns(&stime);
541
542                 /* Accept result with <  max_error nsecs timing uncertainty. */
543                 if (duration_ns <= (int64_t) *max_error)
544                         break;
545         }
546
547         /* Noisy system timing? */
548         if (i == DRM_TIMESTAMP_MAXRETRIES) {
549                 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
550                           crtc, (int) duration_ns/1000, *max_error/1000, i);
551         }
552
553         /* Return upper bound of timestamp precision error. */
554         *max_error = (int) duration_ns;
555
556         /* Check if in vblank area:
557          * vpos is >=0 in video scanout area, but negative
558          * within vblank area, counting down the number of lines until
559          * start of scanout.
560          */
561         invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
562
563         /* Convert scanout position into elapsed time at raw_time query
564          * since start of scanout at first display scanline. delta_ns
565          * can be negative if start of scanout hasn't happened yet.
566          */
567         delta_ns = (int64_t)vpos * linedur_ns + (int64_t)hpos * pixeldur_ns;
568
569         /* Is vpos outside nominal vblank area, but less than
570          * 1/100 of a frame height away from start of vblank?
571          * If so, assume this isn't a massively delayed vblank
572          * interrupt, but a vblank interrupt that fired a few
573          * microseconds before true start of vblank. Compensate
574          * by adding a full frame duration to the final timestamp.
575          * Happens, e.g., on ATI R500, R600.
576          *
577          * We only do this if DRM_CALLED_FROM_VBLIRQ.
578          */
579         if ((flags & DRM_CALLED_FROM_VBLIRQ) && !invbl &&
580             ((vdisplay - vpos) < vtotal / 100)) {
581                 delta_ns = delta_ns - framedur_ns;
582
583                 /* Signal this correction as "applied". */
584                 vbl_status |= 0x8;
585         }
586
587         /* Subtract time delta from raw timestamp to get final
588          * vblank_time timestamp for end of vblank.
589          */
590         *vblank_time = ns_to_timeval(timeval_to_ns(&raw_time) - delta_ns);
591
592         DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %jd.%jd -> %jd.%jd [e %d us, %d rep]\n",
593                   crtc, (int)vbl_status, hpos, vpos, (uintmax_t)raw_time.tv_sec,
594                   (uintmax_t)raw_time.tv_usec, (uintmax_t)vblank_time->tv_sec,
595                   (uintmax_t)vblank_time->tv_usec, (int)duration_ns/1000, i);
596
597         vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
598         if (invbl)
599                 vbl_status |= DRM_VBLANKTIME_INVBL;
600
601         return vbl_status;
602 }
603
604 /**
605  * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
606  * vblank interval.
607  *
608  * @dev: DRM device
609  * @crtc: which crtc's vblank timestamp to retrieve
610  * @tvblank: Pointer to target struct timeval which should receive the timestamp
611  * @flags: Flags to pass to driver:
612  *         0 = Default.
613  *         DRM_CALLED_FROM_VBLIRQ = If function is called from vbl irq handler.
614  *
615  * Fetches the system timestamp corresponding to the time of the most recent
616  * vblank interval on specified crtc. May call into kms-driver to
617  * compute the timestamp with a high-precision GPU specific method.
618  *
619  * Returns zero if timestamp originates from uncorrected do_gettimeofday()
620  * call, i.e., it isn't very precisely locked to the true vblank.
621  *
622  * Returns non-zero if timestamp is considered to be very precise.
623  */
624 u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
625                               struct timeval *tvblank, unsigned flags)
626 {
627         int ret = 0;
628
629         /* Define requested maximum error on timestamps (nanoseconds). */
630         int max_error = (int) drm_timestamp_precision * 1000;
631
632         /* Query driver if possible and precision timestamping enabled. */
633         if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
634                 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
635                                                         tvblank, flags);
636                 if (ret > 0)
637                         return (u32) ret;
638         }
639
640         /* GPU high precision timestamp query unsupported or failed.
641          * Return gettimeofday timestamp as best estimate.
642          */
643         microtime(tvblank);
644
645         return 0;
646 }
647
648 /**
649  * drm_vblank_count - retrieve "cooked" vblank counter value
650  * @dev: DRM device
651  * @crtc: which counter to retrieve
652  *
653  * Fetches the "cooked" vblank count value that represents the number of
654  * vblank events since the system was booted, including lost events due to
655  * modesetting activity.
656  */
657 u32 drm_vblank_count(struct drm_device *dev, int crtc)
658 {
659         return atomic_read(&dev->_vblank_count[crtc]);
660 }
661
662 /**
663  * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
664  * and the system timestamp corresponding to that vblank counter value.
665  *
666  * @dev: DRM device
667  * @crtc: which counter to retrieve
668  * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
669  *
670  * Fetches the "cooked" vblank count value that represents the number of
671  * vblank events since the system was booted, including lost events due to
672  * modesetting activity. Returns corresponding system timestamp of the time
673  * of the vblank interval that corresponds to the current value vblank counter
674  * value.
675  */
676 u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
677                               struct timeval *vblanktime)
678 {
679         u32 cur_vblank;
680
681         /* Read timestamp from slot of _vblank_time ringbuffer
682          * that corresponds to current vblank count. Retry if
683          * count has incremented during readout. This works like
684          * a seqlock.
685          */
686         do {
687                 cur_vblank = atomic_read(&dev->_vblank_count[crtc]);
688                 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
689                 cpu_lfence();
690         } while (cur_vblank != atomic_read(&dev->_vblank_count[crtc]));
691
692         return cur_vblank;
693 }
694
695 /**
696  * drm_update_vblank_count - update the master vblank counter
697  * @dev: DRM device
698  * @crtc: counter to update
699  *
700  * Call back into the driver to update the appropriate vblank counter
701  * (specified by @crtc).  Deal with wraparound, if it occurred, and
702  * update the last read value so we can deal with wraparound on the next
703  * call if necessary.
704  *
705  * Only necessary when going from off->on, to account for frames we
706  * didn't get an interrupt for.
707  *
708  * Note: caller must hold dev->vbl_lock since this reads & writes
709  * device vblank fields.
710  */
711 static void drm_update_vblank_count(struct drm_device *dev, int crtc)
712 {
713         u32 cur_vblank, diff, tslot, rc;
714         struct timeval t_vblank;
715
716         /*
717          * Interrupts were disabled prior to this call, so deal with counter
718          * wrap if needed.
719          * NOTE!  It's possible we lost a full dev->max_vblank_count events
720          * here if the register is small or we had vblank interrupts off for
721          * a long time.
722          *
723          * We repeat the hardware vblank counter & timestamp query until
724          * we get consistent results. This to prevent races between gpu
725          * updating its hardware counter while we are retrieving the
726          * corresponding vblank timestamp.
727          */
728         do {
729                 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
730                 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
731         } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
732
733         /* Deal with counter wrap */
734         diff = cur_vblank - dev->last_vblank[crtc];
735         if (cur_vblank < dev->last_vblank[crtc]) {
736                 diff += dev->max_vblank_count;
737
738                 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
739                           crtc, dev->last_vblank[crtc], cur_vblank, diff);
740         }
741
742         DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
743                   crtc, diff);
744
745         /* Reinitialize corresponding vblank timestamp if high-precision query
746          * available. Skip this step if query unsupported or failed. Will
747          * reinitialize delayed at next vblank interrupt in that case.
748          */
749         if (rc) {
750                 tslot = atomic_read(&dev->_vblank_count[crtc]) + diff;
751                 vblanktimestamp(dev, crtc, tslot) = t_vblank;
752         }
753
754         atomic_add(diff, &dev->_vblank_count[crtc]);
755 }
756
757 /**
758  * drm_vblank_get - get a reference count on vblank events
759  * @dev: DRM device
760  * @crtc: which CRTC to own
761  *
762  * Acquire a reference count on vblank events to avoid having them disabled
763  * while in use.
764  *
765  * RETURNS
766  * Zero on success, nonzero on failure.
767  */
768 int drm_vblank_get(struct drm_device *dev, int crtc)
769 {
770         int ret = 0;
771
772         lockmgr(&dev->vbl_lock, LK_EXCLUSIVE);
773         /* Going from 0->1 means we have to enable interrupts again */
774         if (atomic_fetchadd_int(&dev->vblank_refcount[crtc], 1) == 0) {
775                 lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
776                 if (!dev->vblank_enabled[crtc]) {
777                         /* Enable vblank irqs under vblank_time_lock protection.
778                          * All vblank count & timestamp updates are held off
779                          * until we are done reinitializing master counter and
780                          * timestamps. Filtercode in drm_handle_vblank() will
781                          * prevent double-accounting of same vblank interval.
782                          */
783                         ret = -dev->driver->enable_vblank(dev, crtc);
784                         DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n",
785                                   crtc, ret);
786                         if (ret)
787                                 atomic_dec(&dev->vblank_refcount[crtc]);
788                         else {
789                                 dev->vblank_enabled[crtc] = 1;
790                                 drm_update_vblank_count(dev, crtc);
791                         }
792                 }
793                 lockmgr(&dev->vblank_time_lock, LK_RELEASE);
794         } else {
795                 if (!dev->vblank_enabled[crtc]) {
796                         atomic_dec(&dev->vblank_refcount[crtc]);
797                         ret = EINVAL;
798                 }
799         }
800         lockmgr(&dev->vbl_lock, LK_RELEASE);
801
802         return ret;
803 }
804
805 /**
806  * drm_vblank_put - give up ownership of vblank events
807  * @dev: DRM device
808  * @crtc: which counter to give up
809  *
810  * Release ownership of a given vblank counter, turning off interrupts
811  * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
812  */
813 void drm_vblank_put(struct drm_device *dev, int crtc)
814 {
815         KASSERT(atomic_read(&dev->vblank_refcount[crtc]) != 0,
816             ("Too many drm_vblank_put for crtc %d", crtc));
817
818         /* Last user schedules interrupt disable */
819         if (atomic_fetchadd_int(&dev->vblank_refcount[crtc], -1) == 1 &&
820             (drm_vblank_offdelay > 0))
821                 callout_reset(&dev->vblank_disable_callout,
822                     (drm_vblank_offdelay * DRM_HZ) / 1000,
823                     vblank_disable_fn, dev);
824 }
825
826 void drm_vblank_off(struct drm_device *dev, int crtc)
827 {
828         struct drm_pending_vblank_event *e, *t;
829         struct timeval now;
830         unsigned int seq;
831
832         lockmgr(&dev->vbl_lock, LK_EXCLUSIVE);
833         vblank_disable_and_save(dev, crtc);
834         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
835         wakeup(&dev->_vblank_count[crtc]);
836
837         /* Send any queued vblank events, lest the natives grow disquiet */
838         seq = drm_vblank_count_and_time(dev, crtc, &now);
839         list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
840                 if (e->pipe != crtc)
841                         continue;
842                 DRM_DEBUG("Sending premature vblank event on disable: \
843                           wanted %d, current %d\n",
844                           e->event.sequence, seq);
845
846                 e->event.sequence = seq;
847                 e->event.tv_sec = now.tv_sec;
848                 e->event.tv_usec = now.tv_usec;
849                 drm_vblank_put(dev, e->pipe);
850                 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
851                 drm_event_wakeup(&e->base);
852         }
853
854         lockmgr(&dev->event_lock, LK_RELEASE);
855         lockmgr(&dev->vbl_lock, LK_RELEASE);
856 }
857
858 /**
859  * drm_vblank_pre_modeset - account for vblanks across mode sets
860  * @dev: DRM device
861  * @crtc: CRTC in question
862  * @post: post or pre mode set?
863  *
864  * Account for vblank events across mode setting events, which will likely
865  * reset the hardware frame counter.
866  */
867 void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
868 {
869         /* vblank is not initialized (IRQ not installed ?) */
870         if (!dev->num_crtcs)
871                 return;
872         /*
873          * To avoid all the problems that might happen if interrupts
874          * were enabled/disabled around or between these calls, we just
875          * have the kernel take a reference on the CRTC (just once though
876          * to avoid corrupting the count if multiple, mismatch calls occur),
877          * so that interrupts remain enabled in the interim.
878          */
879         if (!dev->vblank_inmodeset[crtc]) {
880                 dev->vblank_inmodeset[crtc] = 0x1;
881                 if (drm_vblank_get(dev, crtc) == 0)
882                         dev->vblank_inmodeset[crtc] |= 0x2;
883         }
884 }
885
886 void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
887 {
888
889         if (dev->vblank_inmodeset[crtc]) {
890                 lockmgr(&dev->vbl_lock, LK_EXCLUSIVE);
891                 dev->vblank_disable_allowed = 1;
892                 lockmgr(&dev->vbl_lock, LK_RELEASE);
893
894                 if (dev->vblank_inmodeset[crtc] & 0x2)
895                         drm_vblank_put(dev, crtc);
896
897                 dev->vblank_inmodeset[crtc] = 0;
898         }
899 }
900
901 /**
902  * drm_modeset_ctl - handle vblank event counter changes across mode switch
903  * @DRM_IOCTL_ARGS: standard ioctl arguments
904  *
905  * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
906  * ioctls around modesetting so that any lost vblank events are accounted for.
907  *
908  * Generally the counter will reset across mode sets.  If interrupts are
909  * enabled around this call, we don't have to do anything since the counter
910  * will have already been incremented.
911  */
912 int drm_modeset_ctl(struct drm_device *dev, void *data,
913                     struct drm_file *file_priv)
914 {
915         struct drm_modeset_ctl *modeset = data;
916         int ret = 0;
917         unsigned int crtc;
918
919         /* If drm_vblank_init() hasn't been called yet, just no-op */
920         if (!dev->num_crtcs)
921                 goto out;
922
923         crtc = modeset->crtc;
924         if (crtc >= dev->num_crtcs) {
925                 ret = -EINVAL;
926                 goto out;
927         }
928
929         switch (modeset->cmd) {
930         case _DRM_PRE_MODESET:
931                 drm_vblank_pre_modeset(dev, crtc);
932                 break;
933         case _DRM_POST_MODESET:
934                 drm_vblank_post_modeset(dev, crtc);
935                 break;
936         default:
937                 ret = -EINVAL;
938                 break;
939         }
940
941 out:
942         return ret;
943 }
944
945 static void
946 drm_vblank_event_destroy(struct drm_pending_event *e)
947 {
948
949         drm_free(e, DRM_MEM_VBLANK);
950 }
951
952 static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
953                                   union drm_wait_vblank *vblwait,
954                                   struct drm_file *file_priv)
955 {
956         struct drm_pending_vblank_event *e;
957         struct timeval now;
958         unsigned int seq;
959         int ret;
960
961         e = kmalloc(sizeof *e, DRM_MEM_VBLANK, M_WAITOK | M_ZERO);
962
963         e->pipe = pipe;
964         e->base.pid = curproc->p_pid;
965         e->event.base.type = DRM_EVENT_VBLANK;
966         e->event.base.length = sizeof e->event;
967         e->event.user_data = vblwait->request.signal;
968         e->base.event = &e->event.base;
969         e->base.file_priv = file_priv;
970         e->base.destroy = drm_vblank_event_destroy;
971
972         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
973
974         if (file_priv->event_space < sizeof e->event) {
975                 ret = EBUSY;
976                 goto err_unlock;
977         }
978
979         file_priv->event_space -= sizeof e->event;
980         seq = drm_vblank_count_and_time(dev, pipe, &now);
981
982         if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
983             (seq - vblwait->request.sequence) <= (1 << 23)) {
984                 vblwait->request.sequence = seq + 1;
985                 vblwait->reply.sequence = vblwait->request.sequence;
986         }
987
988         DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
989                   vblwait->request.sequence, seq, pipe);
990
991         e->event.sequence = vblwait->request.sequence;
992         if ((seq - vblwait->request.sequence) <= (1 << 23)) {
993                 e->event.sequence = seq;
994                 e->event.tv_sec = now.tv_sec;
995                 e->event.tv_usec = now.tv_usec;
996                 drm_vblank_put(dev, pipe);
997                 list_add_tail(&e->base.link, &e->base.file_priv->event_list);
998                 drm_event_wakeup(&e->base);
999                 vblwait->reply.sequence = seq;
1000         } else {
1001                 /* drm_handle_vblank_events will call drm_vblank_put */
1002                 list_add_tail(&e->base.link, &dev->vblank_event_list);
1003                 vblwait->reply.sequence = vblwait->request.sequence;
1004         }
1005
1006         lockmgr(&dev->event_lock, LK_RELEASE);
1007
1008         return 0;
1009
1010 err_unlock:
1011         lockmgr(&dev->event_lock, LK_RELEASE);
1012         drm_free(e, DRM_MEM_VBLANK);
1013         drm_vblank_put(dev, pipe);
1014         return ret;
1015 }
1016
1017 /**
1018  * Wait for VBLANK.
1019  *
1020  * \param inode device inode.
1021  * \param file_priv DRM file private.
1022  * \param cmd command.
1023  * \param data user argument, pointing to a drm_wait_vblank structure.
1024  * \return zero on success or a negative number on failure.
1025  *
1026  * This function enables the vblank interrupt on the pipe requested, then
1027  * sleeps waiting for the requested sequence number to occur, and drops
1028  * the vblank interrupt refcount afterwards. (vblank irq disable follows that
1029  * after a timeout with no further vblank waits scheduled).
1030  */
1031 int drm_wait_vblank(struct drm_device *dev, void *data,
1032                     struct drm_file *file_priv)
1033 {
1034         union drm_wait_vblank *vblwait = data;
1035         int ret = 0;
1036         unsigned int flags, seq, crtc, high_crtc;
1037
1038         if (/*(!drm_dev_to_irq(dev)) || */(!dev->irq_enabled))
1039                 return (EINVAL);
1040
1041         if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1042                 return (EINVAL);
1043
1044         if (vblwait->request.type &
1045             ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1046               _DRM_VBLANK_HIGH_CRTC_MASK)) {
1047                 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1048                           vblwait->request.type,
1049                           (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1050                            _DRM_VBLANK_HIGH_CRTC_MASK));
1051                 return (EINVAL);
1052         }
1053
1054         flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1055         high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1056         if (high_crtc)
1057                 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1058         else
1059                 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1060         if (crtc >= dev->num_crtcs)
1061                 return (EINVAL);
1062
1063         ret = drm_vblank_get(dev, crtc);
1064         if (ret) {
1065                 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
1066                 return (ret);
1067         }
1068         seq = drm_vblank_count(dev, crtc);
1069
1070         switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1071         case _DRM_VBLANK_RELATIVE:
1072                 vblwait->request.sequence += seq;
1073                 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1074         case _DRM_VBLANK_ABSOLUTE:
1075                 break;
1076         default:
1077                 ret = (EINVAL);
1078                 goto done;
1079         }
1080
1081         if (flags & _DRM_VBLANK_EVENT) {
1082                 /* must hold on to the vblank ref until the event fires
1083                  * drm_vblank_put will be called asynchronously
1084                  */
1085                 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
1086         }
1087
1088         if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1089             (seq - vblwait->request.sequence) <= (1<<23)) {
1090                 vblwait->request.sequence = seq + 1;
1091         }
1092
1093         dev->last_vblank_wait[crtc] = vblwait->request.sequence;
1094         lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
1095         while (((drm_vblank_count(dev, crtc) - vblwait->request.sequence) >
1096             (1 << 23)) && dev->irq_enabled) {
1097                 /*
1098                  * The wakeups from the drm_irq_uninstall() and
1099                  * drm_vblank_off() may be lost there since vbl_lock
1100                  * is not held.  Then, the timeout will wake us; the 3
1101                  * seconds delay should not be a problem for
1102                  * application when crtc is disabled or irq
1103                  * uninstalled anyway.
1104                  */
1105                 ret = lksleep(&dev->_vblank_count[crtc], &dev->vblank_time_lock,
1106                     PCATCH, "drmvbl", 3 * hz);
1107                 if (ret != 0)
1108                         break;
1109         }
1110         lockmgr(&dev->vblank_time_lock, LK_RELEASE);
1111         if (ret != EINTR) {
1112                 struct timeval now;
1113                 long reply_seq;
1114
1115                 reply_seq = drm_vblank_count_and_time(dev, crtc, &now);
1116                 vblwait->reply.sequence = reply_seq;
1117                 vblwait->reply.tval_sec = now.tv_sec;
1118                 vblwait->reply.tval_usec = now.tv_usec;
1119         }
1120
1121 done:
1122         drm_vblank_put(dev, crtc);
1123         return ret;
1124 }
1125
1126 void drm_handle_vblank_events(struct drm_device *dev, int crtc)
1127 {
1128         struct drm_pending_vblank_event *e, *t;
1129         struct timeval now;
1130         unsigned int seq;
1131
1132         seq = drm_vblank_count_and_time(dev, crtc, &now);
1133
1134         lockmgr(&dev->event_lock, LK_EXCLUSIVE);
1135
1136         list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1137                 if (e->pipe != crtc)
1138                         continue;
1139                 if ((seq - e->event.sequence) > (1<<23))
1140                         continue;
1141
1142                 e->event.sequence = seq;
1143                 e->event.tv_sec = now.tv_sec;
1144                 e->event.tv_usec = now.tv_usec;
1145                 drm_vblank_put(dev, e->pipe);
1146                 list_move_tail(&e->base.link, &e->base.file_priv->event_list);
1147                 drm_event_wakeup(&e->base);
1148         }
1149
1150         lockmgr(&dev->event_lock, LK_RELEASE);
1151 }
1152
1153 /**
1154  * drm_handle_vblank - handle a vblank event
1155  * @dev: DRM device
1156  * @crtc: where this event occurred
1157  *
1158  * Drivers should call this routine in their vblank interrupt handlers to
1159  * update the vblank counter and send any signals that may be pending.
1160  */
1161 bool drm_handle_vblank(struct drm_device *dev, int crtc)
1162 {
1163         u32 vblcount;
1164         int64_t diff_ns;
1165         struct timeval tvblank;
1166
1167         if (!dev->num_crtcs)
1168                 return false;
1169
1170         /* Need timestamp lock to prevent concurrent execution with
1171          * vblank enable/disable, as this would cause inconsistent
1172          * or corrupted timestamps and vblank counts.
1173          */
1174         lockmgr(&dev->vblank_time_lock, LK_EXCLUSIVE);
1175
1176         /* Vblank irq handling disabled. Nothing to do. */
1177         if (!dev->vblank_enabled[crtc]) {
1178                 lockmgr(&dev->vblank_time_lock, LK_RELEASE);
1179                 return false;
1180         }
1181
1182         /* Fetch corresponding timestamp for this vblank interval from
1183          * driver and store it in proper slot of timestamp ringbuffer.
1184          */
1185
1186         /* Get current timestamp and count. */
1187         vblcount = atomic_read(&dev->_vblank_count[crtc]);
1188         drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1189
1190         /* Compute time difference to timestamp of last vblank */
1191         diff_ns = timeval_to_ns(&tvblank) -
1192                   timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1193
1194         /* Update vblank timestamp and count if at least
1195          * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1196          * difference between last stored timestamp and current
1197          * timestamp. A smaller difference means basically
1198          * identical timestamps. Happens if this vblank has
1199          * been already processed and this is a redundant call,
1200          * e.g., due to spurious vblank interrupts. We need to
1201          * ignore those for accounting.
1202          */
1203         if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
1204                 /* Store new timestamp in ringbuffer. */
1205                 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
1206
1207                 /* Increment cooked vblank count. This also atomically commits
1208                  * the timestamp computed above.
1209                  */
1210                 atomic_inc(&dev->_vblank_count[crtc]);
1211         } else {
1212                 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1213                           crtc, (int) diff_ns);
1214         }
1215
1216         wakeup(&dev->_vblank_count[crtc]);
1217         drm_handle_vblank_events(dev, crtc);
1218
1219         lockmgr(&dev->vblank_time_lock, LK_RELEASE);
1220         return true;
1221 }