drm_irq.c revision 1.12 1 /* $NetBSD: drm_irq.c,v 1.12 2018/08/27 07:03:39 riastradh Exp $ */
2
3 /*
4 * drm_irq.c IRQ and vblank support
5 *
6 * \author Rickard E. (Rik) Faith <faith (at) valinux.com>
7 * \author Gareth Hughes <gareth (at) valinux.com>
8 */
9
10 /*
11 * Created: Fri Mar 19 14:30:16 1999 by faith (at) valinux.com
12 *
13 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
14 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
15 * All Rights Reserved.
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining a
18 * copy of this software and associated documentation files (the "Software"),
19 * to deal in the Software without restriction, including without limitation
20 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
21 * and/or sell copies of the Software, and to permit persons to whom the
22 * Software is furnished to do so, subject to the following conditions:
23 *
24 * The above copyright notice and this permission notice (including the next
25 * paragraph) shall be included in all copies or substantial portions of the
26 * Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
31 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
32 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
33 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
34 * OTHER DEALINGS IN THE SOFTWARE.
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: drm_irq.c,v 1.12 2018/08/27 07:03:39 riastradh Exp $");
39
40 #include <drm/drmP.h>
41 #include "drm_trace.h"
42 #include "drm_internal.h"
43
44 #include <linux/interrupt.h> /* For task queue support */
45 #include <linux/slab.h>
46
47 #include <linux/vgaarb.h>
48 #include <linux/export.h>
49 #include <linux/moduleparam.h>
50
51 #include <linux/atomic.h>
52 #include <linux/ktime.h>
53 #include <linux/math64.h>
54 #include <linux/preempt.h>
55 #include <linux/sched.h>
56
57 #include <asm/bug.h>
58 #include <asm/param.h>
59
60 #ifdef __NetBSD__ /* XXX hurk -- selnotify &c. */
61 #include <sys/poll.h>
62 #include <sys/select.h>
63 #endif
64
65 /* Access macro for slots in vblank timestamp ringbuffer. */
66 #define vblanktimestamp(dev, pipe, count) \
67 ((dev)->vblank[pipe].time[(count) % DRM_VBLANKTIME_RBSIZE])
68
69 /* Retry timestamp calculation up to 3 times to satisfy
70 * drm_timestamp_precision before giving up.
71 */
72 #define DRM_TIMESTAMP_MAXRETRIES 3
73
74 /* Threshold in nanoseconds for detection of redundant
75 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
76 */
77 #define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
78
79 static bool
80 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
81 struct timeval *tvblank, unsigned flags);
82
83 static unsigned int drm_timestamp_precision = 20; /* Default to 20 usecs. */
84
85 /*
86 * Default to use monotonic timestamps for wait-for-vblank and page-flip
87 * complete events.
88 */
89 unsigned int drm_timestamp_monotonic = 1;
90
91 static int drm_vblank_offdelay = 5000; /* Default to 5000 msecs. */
92
93 module_param_named(vblankoffdelay, drm_vblank_offdelay, int, 0600);
94 module_param_named(timestamp_precision_usec, drm_timestamp_precision, int, 0600);
95 module_param_named(timestamp_monotonic, drm_timestamp_monotonic, int, 0600);
96
97 static void store_vblank(struct drm_device *dev, unsigned int pipe,
98 u32 vblank_count_inc,
99 struct timeval *t_vblank, u32 last)
100 {
101 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
102 u32 tslot;
103
104 assert_spin_locked(&dev->vblank_time_lock);
105
106 vblank->last = last;
107
108 /* All writers hold the spinlock, but readers are serialized by
109 * the latching of vblank->count below.
110 */
111 tslot = vblank->count + vblank_count_inc;
112 vblanktimestamp(dev, pipe, tslot) = *t_vblank;
113
114 /*
115 * vblank timestamp updates are protected on the write side with
116 * vblank_time_lock, but on the read side done locklessly using a
117 * sequence-lock on the vblank counter. Ensure correct ordering using
118 * memory barrriers. We need the barrier both before and also after the
119 * counter update to synchronize with the next timestamp write.
120 * The read-side barriers for this are in drm_vblank_count_and_time.
121 */
122 smp_wmb();
123 vblank->count += vblank_count_inc;
124 smp_wmb();
125 }
126
127 /**
128 * drm_reset_vblank_timestamp - reset the last timestamp to the last vblank
129 * @dev: DRM device
130 * @pipe: index of CRTC for which to reset the timestamp
131 *
132 * Reset the stored timestamp for the current vblank count to correspond
133 * to the last vblank occurred.
134 *
135 * Only to be called from drm_vblank_on().
136 *
137 * Note: caller must hold dev->vbl_lock since this reads & writes
138 * device vblank fields.
139 */
140 static void drm_reset_vblank_timestamp(struct drm_device *dev, unsigned int pipe)
141 {
142 u32 cur_vblank;
143 bool rc;
144 struct timeval t_vblank;
145 int count = DRM_TIMESTAMP_MAXRETRIES;
146
147 spin_lock(&dev->vblank_time_lock);
148
149 /*
150 * sample the current counter to avoid random jumps
151 * when drm_vblank_enable() applies the diff
152 */
153 do {
154 cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
155 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, 0);
156 } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
157
158 /*
159 * Only reinitialize corresponding vblank timestamp if high-precision query
160 * available and didn't fail. Otherwise reinitialize delayed at next vblank
161 * interrupt and assign 0 for now, to mark the vblanktimestamp as invalid.
162 */
163 if (!rc)
164 t_vblank = (struct timeval) {0, 0};
165
166 /*
167 * +1 to make sure user will never see the same
168 * vblank counter value before and after a modeset
169 */
170 store_vblank(dev, pipe, 1, &t_vblank, cur_vblank);
171
172 spin_unlock(&dev->vblank_time_lock);
173 }
174
175 /**
176 * drm_update_vblank_count - update the master vblank counter
177 * @dev: DRM device
178 * @pipe: counter to update
179 *
180 * Call back into the driver to update the appropriate vblank counter
181 * (specified by @pipe). Deal with wraparound, if it occurred, and
182 * update the last read value so we can deal with wraparound on the next
183 * call if necessary.
184 *
185 * Only necessary when going from off->on, to account for frames we
186 * didn't get an interrupt for.
187 *
188 * Note: caller must hold dev->vbl_lock since this reads & writes
189 * device vblank fields.
190 */
191 static void drm_update_vblank_count(struct drm_device *dev, unsigned int pipe,
192 unsigned long flags)
193 {
194 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
195 u32 cur_vblank, diff;
196 bool rc;
197 struct timeval t_vblank;
198 int count = DRM_TIMESTAMP_MAXRETRIES;
199 int framedur_ns = vblank->framedur_ns;
200
201 /*
202 * Interrupts were disabled prior to this call, so deal with counter
203 * wrap if needed.
204 * NOTE! It's possible we lost a full dev->max_vblank_count + 1 events
205 * here if the register is small or we had vblank interrupts off for
206 * a long time.
207 *
208 * We repeat the hardware vblank counter & timestamp query until
209 * we get consistent results. This to prevent races between gpu
210 * updating its hardware counter while we are retrieving the
211 * corresponding vblank timestamp.
212 */
213 do {
214 cur_vblank = dev->driver->get_vblank_counter(dev, pipe);
215 rc = drm_get_last_vbltimestamp(dev, pipe, &t_vblank, flags);
216 } while (cur_vblank != dev->driver->get_vblank_counter(dev, pipe) && --count > 0);
217
218 if (dev->max_vblank_count != 0) {
219 /* trust the hw counter when it's around */
220 diff = (cur_vblank - vblank->last) & dev->max_vblank_count;
221 } else if (rc && framedur_ns) {
222 const struct timeval *t_old;
223 u64 diff_ns;
224
225 t_old = &vblanktimestamp(dev, pipe, vblank->count);
226 diff_ns = timeval_to_ns(&t_vblank) - timeval_to_ns(t_old);
227
228 /*
229 * Figure out how many vblanks we've missed based
230 * on the difference in the timestamps and the
231 * frame/field duration.
232 */
233 diff = DIV_ROUND_CLOSEST_ULL(diff_ns, framedur_ns);
234
235 if (diff == 0 && flags & DRM_CALLED_FROM_VBLIRQ)
236 DRM_DEBUG_VBL("crtc %u: Redundant vblirq ignored."
237 " diff_ns = %lld, framedur_ns = %d)\n",
238 pipe, (long long) diff_ns, framedur_ns);
239 } else {
240 /* some kind of default for drivers w/o accurate vbl timestamping */
241 diff = (flags & DRM_CALLED_FROM_VBLIRQ) != 0;
242 }
243
244 /*
245 * Within a drm_vblank_pre_modeset - drm_vblank_post_modeset
246 * interval? If so then vblank irqs keep running and it will likely
247 * happen that the hardware vblank counter is not trustworthy as it
248 * might reset at some point in that interval and vblank timestamps
249 * are not trustworthy either in that interval. Iow. this can result
250 * in a bogus diff >> 1 which must be avoided as it would cause
251 * random large forward jumps of the software vblank counter.
252 */
253 if (diff > 1 && (vblank->inmodeset & 0x2)) {
254 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u"
255 " due to pre-modeset.\n", pipe, diff);
256 diff = 1;
257 }
258
259 /*
260 * FIMXE: Need to replace this hack with proper seqlocks.
261 *
262 * Restrict the bump of the software vblank counter to a safe maximum
263 * value of +1 whenever there is the possibility that concurrent readers
264 * of vblank timestamps could be active at the moment, as the current
265 * implementation of the timestamp caching and updating is not safe
266 * against concurrent readers for calls to store_vblank() with a bump
267 * of anything but +1. A bump != 1 would very likely return corrupted
268 * timestamps to userspace, because the same slot in the cache could
269 * be concurrently written by store_vblank() and read by one of those
270 * readers without the read-retry logic detecting the collision.
271 *
272 * Concurrent readers can exist when we are called from the
273 * drm_vblank_off() or drm_vblank_on() functions and other non-vblank-
274 * irq callers. However, all those calls to us are happening with the
275 * vbl_lock locked to prevent drm_vblank_get(), so the vblank refcount
276 * can't increase while we are executing. Therefore a zero refcount at
277 * this point is safe for arbitrary counter bumps if we are called
278 * outside vblank irq, a non-zero count is not 100% safe. Unfortunately
279 * we must also accept a refcount of 1, as whenever we are called from
280 * drm_vblank_get() -> drm_vblank_enable() the refcount will be 1 and
281 * we must let that one pass through in order to not lose vblank counts
282 * during vblank irq off - which would completely defeat the whole
283 * point of this routine.
284 *
285 * Whenever we are called from vblank irq, we have to assume concurrent
286 * readers exist or can show up any time during our execution, even if
287 * the refcount is currently zero, as vblank irqs are usually only
288 * enabled due to the presence of readers, and because when we are called
289 * from vblank irq we can't hold the vbl_lock to protect us from sudden
290 * bumps in vblank refcount. Therefore also restrict bumps to +1 when
291 * called from vblank irq.
292 */
293 if ((diff > 1) && (atomic_read(&vblank->refcount) > 1 ||
294 (flags & DRM_CALLED_FROM_VBLIRQ))) {
295 DRM_DEBUG_VBL("clamping vblank bump to 1 on crtc %u: diffr=%u "
296 "refcount %u, vblirq %u\n", pipe, diff,
297 atomic_read(&vblank->refcount),
298 (flags & DRM_CALLED_FROM_VBLIRQ) != 0);
299 diff = 1;
300 }
301
302 DRM_DEBUG_VBL("updating vblank count on crtc %u:"
303 " current=%u, diff=%u, hw=%u hw_last=%u\n",
304 pipe, vblank->count, diff, cur_vblank, vblank->last);
305
306 if (diff == 0) {
307 WARN_ON_ONCE(cur_vblank != vblank->last);
308 return;
309 }
310
311 /*
312 * Only reinitialize corresponding vblank timestamp if high-precision query
313 * available and didn't fail, or we were called from the vblank interrupt.
314 * Otherwise reinitialize delayed at next vblank interrupt and assign 0
315 * for now, to mark the vblanktimestamp as invalid.
316 */
317 if (!rc && (flags & DRM_CALLED_FROM_VBLIRQ) == 0)
318 t_vblank = (struct timeval) {0, 0};
319
320 store_vblank(dev, pipe, diff, &t_vblank, cur_vblank);
321 }
322
323 /*
324 * Disable vblank irq's on crtc, make sure that last vblank count
325 * of hardware and corresponding consistent software vblank counter
326 * are preserved, even if there are any spurious vblank irq's after
327 * disable.
328 */
329 static void vblank_disable_and_save(struct drm_device *dev, unsigned int pipe)
330 {
331 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
332 unsigned long irqflags;
333
334 /* Prevent vblank irq processing while disabling vblank irqs,
335 * so no updates of timestamps or count can happen after we've
336 * disabled. Needed to prevent races in case of delayed irq's.
337 */
338 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
339
340 /*
341 * Only disable vblank interrupts if they're enabled. This avoids
342 * calling the ->disable_vblank() operation in atomic context with the
343 * hardware potentially runtime suspended.
344 */
345 if (vblank->enabled) {
346 dev->driver->disable_vblank(dev, pipe);
347 vblank->enabled = false;
348 }
349
350 /*
351 * Always update the count and timestamp to maintain the
352 * appearance that the counter has been ticking all along until
353 * this time. This makes the count account for the entire time
354 * between drm_vblank_on() and drm_vblank_off().
355 */
356 drm_update_vblank_count(dev, pipe, 0);
357
358 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
359 }
360
361 static void vblank_disable_fn(unsigned long arg)
362 {
363 struct drm_vblank_crtc *vblank = (void *)arg;
364 struct drm_device *dev = vblank->dev;
365 unsigned int pipe = vblank->pipe;
366 unsigned long irqflags;
367
368 if (!dev->vblank_disable_allowed)
369 return;
370
371 spin_lock_irqsave(&dev->vbl_lock, irqflags);
372 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
373 DRM_DEBUG("disabling vblank on crtc %u\n", pipe);
374 vblank_disable_and_save(dev, pipe);
375 }
376 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
377 }
378
379 /**
380 * drm_vblank_cleanup - cleanup vblank support
381 * @dev: DRM device
382 *
383 * This function cleans up any resources allocated in drm_vblank_init.
384 */
385 void drm_vblank_cleanup(struct drm_device *dev)
386 {
387 unsigned int pipe;
388
389 /* Bail if the driver didn't call drm_vblank_init() */
390 if (dev->num_crtcs == 0)
391 return;
392
393 for (pipe = 0; pipe < dev->num_crtcs; pipe++) {
394 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
395
396 WARN_ON(vblank->enabled &&
397 drm_core_check_feature(dev, DRIVER_MODESET));
398
399 del_timer_sync(&vblank->disable_timer);
400 #ifdef __NetBSD__
401 teardown_timer(&vblank->disable_timer);
402 #endif
403 }
404
405 #ifdef __NetBSD__
406 {
407 unsigned int i;
408 for (i = 0; i < dev->num_crtcs; i++)
409 DRM_DESTROY_WAITQUEUE(&dev->vblank[i].queue);
410 }
411 #endif
412
413 kfree(dev->vblank);
414
415 dev->num_crtcs = 0;
416
417 #ifdef __NetBSD__
418 spin_lock_destroy(&dev->vblank_time_lock);
419 spin_lock_destroy(&dev->vbl_lock);
420 #endif
421 }
422 EXPORT_SYMBOL(drm_vblank_cleanup);
423
424 /**
425 * drm_vblank_init - initialize vblank support
426 * @dev: DRM device
427 * @num_crtcs: number of CRTCs supported by @dev
428 *
429 * This function initializes vblank support for @num_crtcs display pipelines.
430 *
431 * Returns:
432 * Zero on success or a negative error code on failure.
433 */
434 int drm_vblank_init(struct drm_device *dev, unsigned int num_crtcs)
435 {
436 int ret = -ENOMEM;
437 unsigned int i;
438
439 spin_lock_init(&dev->vbl_lock);
440 spin_lock_init(&dev->vblank_time_lock);
441
442 dev->num_crtcs = num_crtcs;
443
444 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
445 if (!dev->vblank)
446 goto err;
447
448 for (i = 0; i < num_crtcs; i++) {
449 struct drm_vblank_crtc *vblank = &dev->vblank[i];
450
451 vblank->dev = dev;
452 vblank->pipe = i;
453 #ifdef __NetBSD__
454 DRM_INIT_WAITQUEUE(&vblank->queue, "drmvblnq");
455 #else
456 init_waitqueue_head(&vblank->queue);
457 #endif
458 setup_timer(&vblank->disable_timer, vblank_disable_fn,
459 (unsigned long)vblank);
460 }
461
462 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
463
464 /* Driver specific high-precision vblank timestamping supported? */
465 if (dev->driver->get_vblank_timestamp)
466 DRM_INFO("Driver supports precise vblank timestamp query.\n");
467 else
468 DRM_INFO("No driver support for vblank timestamp query.\n");
469
470 /* Must have precise timestamping for reliable vblank instant disable */
471 if (dev->vblank_disable_immediate && !dev->driver->get_vblank_timestamp) {
472 dev->vblank_disable_immediate = false;
473 DRM_INFO("Setting vblank_disable_immediate to false because "
474 "get_vblank_timestamp == NULL\n");
475 }
476
477 dev->vblank_disable_allowed = false;
478
479 return 0;
480
481 err:
482 dev->num_crtcs = 0;
483 return ret;
484 }
485 EXPORT_SYMBOL(drm_vblank_init);
486
487 static void drm_irq_vgaarb_nokms(void *cookie, bool state)
488 {
489 struct drm_device *dev = cookie;
490
491 if (dev->driver->vgaarb_irq) {
492 dev->driver->vgaarb_irq(dev, state);
493 return;
494 }
495
496 if (!dev->irq_enabled)
497 return;
498
499 if (state) {
500 if (dev->driver->irq_uninstall)
501 dev->driver->irq_uninstall(dev);
502 } else {
503 if (dev->driver->irq_preinstall)
504 dev->driver->irq_preinstall(dev);
505 if (dev->driver->irq_postinstall)
506 dev->driver->irq_postinstall(dev);
507 }
508 }
509
510 /**
511 * drm_irq_install - install IRQ handler
512 * @dev: DRM device
513 * @irq: IRQ number to install the handler for
514 *
515 * Initializes the IRQ related data. Installs the handler, calling the driver
516 * irq_preinstall() and irq_postinstall() functions before and after the
517 * installation.
518 *
519 * This is the simplified helper interface provided for drivers with no special
520 * needs. Drivers which need to install interrupt handlers for multiple
521 * interrupts must instead set drm_device->irq_enabled to signal the DRM core
522 * that vblank interrupts are available.
523 *
524 * Returns:
525 * Zero on success or a negative error code on failure.
526 */
527 #ifdef __NetBSD__
528 int drm_irq_install(struct drm_device *dev)
529 #else
530 int drm_irq_install(struct drm_device *dev, int irq)
531 #endif
532 {
533 int ret;
534 unsigned long sh_flags = 0;
535
536 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
537 return -EINVAL;
538
539 #ifndef __NetBSD__
540 if (irq == 0)
541 return -EINVAL;
542 #endif
543
544 /* Driver must have been initialized */
545 if (!dev->dev_private)
546 return -EINVAL;
547
548 if (dev->irq_enabled)
549 return -EBUSY;
550 dev->irq_enabled = true;
551
552 #ifndef __NetBSD__
553 DRM_DEBUG("irq=%d\n", irq);
554 #endif
555
556 /* Before installing handler */
557 if (dev->driver->irq_preinstall)
558 dev->driver->irq_preinstall(dev);
559
560 /* Install handler */
561 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
562 sh_flags = IRQF_SHARED;
563
564 #ifdef __NetBSD__
565 ret = (*dev->driver->request_irq)(dev, sh_flags);
566 #else
567 ret = request_irq(irq, dev->driver->irq_handler,
568 sh_flags, dev->driver->name, dev);
569 #endif
570
571 if (ret < 0) {
572 dev->irq_enabled = false;
573 return ret;
574 }
575
576 if (!drm_core_check_feature(dev, DRIVER_MODESET))
577 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
578
579 /* After installing handler */
580 if (dev->driver->irq_postinstall)
581 ret = dev->driver->irq_postinstall(dev);
582
583 if (ret < 0) {
584 dev->irq_enabled = false;
585 if (!drm_core_check_feature(dev, DRIVER_MODESET))
586 vga_client_register(dev->pdev, NULL, NULL, NULL);
587 #ifdef __NetBSD__
588 (*dev->driver->free_irq)(dev);
589 #else
590 free_irq(irq, dev);
591 #endif
592 } else {
593 #ifndef __NetBSD__
594 dev->irq = irq;
595 #endif
596 }
597
598 return ret;
599 }
600 EXPORT_SYMBOL(drm_irq_install);
601
602 /**
603 * drm_irq_uninstall - uninstall the IRQ handler
604 * @dev: DRM device
605 *
606 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
607 * This should only be called by drivers which used drm_irq_install() to set up
608 * their interrupt handler. Other drivers must only reset
609 * drm_device->irq_enabled to false.
610 *
611 * Note that for kernel modesetting drivers it is a bug if this function fails.
612 * The sanity checks are only to catch buggy user modesetting drivers which call
613 * the same function through an ioctl.
614 *
615 * Returns:
616 * Zero on success or a negative error code on failure.
617 */
618 int drm_irq_uninstall(struct drm_device *dev)
619 {
620 unsigned long irqflags;
621 bool irq_enabled;
622 int i;
623
624 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
625 return -EINVAL;
626
627 irq_enabled = dev->irq_enabled;
628 dev->irq_enabled = false;
629
630 /*
631 * Wake up any waiters so they don't hang. This is just to paper over
632 * isssues for UMS drivers which aren't in full control of their
633 * vblank/irq handling. KMS drivers must ensure that vblanks are all
634 * disabled when uninstalling the irq handler.
635 */
636 if (dev->num_crtcs) {
637 spin_lock_irqsave(&dev->vbl_lock, irqflags);
638 for (i = 0; i < dev->num_crtcs; i++) {
639 struct drm_vblank_crtc *vblank = &dev->vblank[i];
640
641 if (!vblank->enabled)
642 continue;
643
644 WARN_ON(drm_core_check_feature(dev, DRIVER_MODESET));
645
646 vblank_disable_and_save(dev, i);
647 #ifdef __NetBSD__
648 DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->vbl_lock);
649 #else
650 wake_up(&vblank->queue);
651 #endif
652 }
653 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
654 }
655
656 if (!irq_enabled)
657 return -EINVAL;
658
659 DRM_DEBUG("irq=%d\n", dev->irq);
660
661 if (!drm_core_check_feature(dev, DRIVER_MODESET))
662 vga_client_register(dev->pdev, NULL, NULL, NULL);
663
664 if (dev->driver->irq_uninstall)
665 dev->driver->irq_uninstall(dev);
666
667 #ifdef __NetBSD__
668 (*dev->driver->free_irq)(dev);
669 #else
670 free_irq(dev->irq, dev);
671 #endif
672
673 return 0;
674 }
675 EXPORT_SYMBOL(drm_irq_uninstall);
676
677 /*
678 * IRQ control ioctl.
679 *
680 * \param inode device inode.
681 * \param file_priv DRM file private.
682 * \param cmd command.
683 * \param arg user argument, pointing to a drm_control structure.
684 * \return zero on success or a negative number on failure.
685 *
686 * Calls irq_install() or irq_uninstall() according to \p arg.
687 */
688 int drm_control(struct drm_device *dev, void *data,
689 struct drm_file *file_priv)
690 {
691 struct drm_control *ctl = data;
692 int ret = 0, irq;
693
694 /* if we haven't irq we fallback for compatibility reasons -
695 * this used to be a separate function in drm_dma.h
696 */
697
698 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
699 return 0;
700 if (drm_core_check_feature(dev, DRIVER_MODESET))
701 return 0;
702 /* UMS was only ever support on pci devices. */
703 if (WARN_ON(!dev->pdev))
704 return -EINVAL;
705
706 switch (ctl->func) {
707 case DRM_INST_HANDLER:
708 #ifdef __NetBSD__
709 irq = ctl->irq;
710 #else
711 irq = dev->pdev->irq;
712 #endif
713
714 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
715 ctl->irq != irq)
716 return -EINVAL;
717 mutex_lock(&dev->struct_mutex);
718 #ifdef __NetBSD__
719 ret = drm_irq_install(dev);
720 #else
721 ret = drm_irq_install(dev, irq);
722 #endif
723 mutex_unlock(&dev->struct_mutex);
724
725 return ret;
726 case DRM_UNINST_HANDLER:
727 mutex_lock(&dev->struct_mutex);
728 ret = drm_irq_uninstall(dev);
729 mutex_unlock(&dev->struct_mutex);
730
731 return ret;
732 default:
733 return -EINVAL;
734 }
735 }
736
737 /**
738 * drm_calc_timestamping_constants - calculate vblank timestamp constants
739 * @crtc: drm_crtc whose timestamp constants should be updated.
740 * @mode: display mode containing the scanout timings
741 *
742 * Calculate and store various constants which are later
743 * needed by vblank and swap-completion timestamping, e.g,
744 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
745 * derived from CRTC's true scanout timing, so they take
746 * things like panel scaling or other adjustments into account.
747 */
748 void drm_calc_timestamping_constants(struct drm_crtc *crtc,
749 const struct drm_display_mode *mode)
750 {
751 struct drm_device *dev = crtc->dev;
752 unsigned int pipe = drm_crtc_index(crtc);
753 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
754 int linedur_ns = 0, framedur_ns = 0;
755 int dotclock = mode->crtc_clock;
756
757 if (!dev->num_crtcs)
758 return;
759
760 if (WARN_ON(pipe >= dev->num_crtcs))
761 return;
762
763 /* Valid dotclock? */
764 if (dotclock > 0) {
765 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
766
767 /*
768 * Convert scanline length in pixels and video
769 * dot clock to line duration and frame duration
770 * in nanoseconds:
771 */
772 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
773 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
774
775 /*
776 * Fields of interlaced scanout modes are only half a frame duration.
777 */
778 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
779 framedur_ns /= 2;
780 } else
781 DRM_ERROR("crtc %u: Can't calculate constants, dotclock = 0!\n",
782 crtc->base.id);
783
784 vblank->linedur_ns = linedur_ns;
785 vblank->framedur_ns = framedur_ns;
786
787 DRM_DEBUG("crtc %u: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
788 crtc->base.id, mode->crtc_htotal,
789 mode->crtc_vtotal, mode->crtc_vdisplay);
790 DRM_DEBUG("crtc %u: clock %d kHz framedur %d linedur %d\n",
791 crtc->base.id, dotclock, framedur_ns, linedur_ns);
792 }
793 EXPORT_SYMBOL(drm_calc_timestamping_constants);
794
795 /**
796 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
797 * @dev: DRM device
798 * @pipe: index of CRTC whose vblank timestamp to retrieve
799 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
800 * On return contains true maximum error of timestamp
801 * @vblank_time: Pointer to struct timeval which should receive the timestamp
802 * @flags: Flags to pass to driver:
803 * 0 = Default,
804 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
805 * @mode: mode which defines the scanout timings
806 *
807 * Implements calculation of exact vblank timestamps from given drm_display_mode
808 * timings and current video scanout position of a CRTC. This can be called from
809 * within get_vblank_timestamp() implementation of a kms driver to implement the
810 * actual timestamping.
811 *
812 * Should return timestamps conforming to the OML_sync_control OpenML
813 * extension specification. The timestamp corresponds to the end of
814 * the vblank interval, aka start of scanout of topmost-leftmost display
815 * pixel in the following video frame.
816 *
817 * Requires support for optional dev->driver->get_scanout_position()
818 * in kms driver, plus a bit of setup code to provide a drm_display_mode
819 * that corresponds to the true scanout timing.
820 *
821 * The current implementation only handles standard video modes. It
822 * returns as no operation if a doublescan or interlaced video mode is
823 * active. Higher level code is expected to handle this.
824 *
825 * Returns:
826 * Negative value on error, failure or if not supported in current
827 * video mode:
828 *
829 * -EINVAL - Invalid CRTC.
830 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
831 * -ENOTSUPP - Function not supported in current display mode.
832 * -EIO - Failed, e.g., due to failed scanout position query.
833 *
834 * Returns or'ed positive status flags on success:
835 *
836 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
837 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
838 *
839 */
840 int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev,
841 unsigned int pipe,
842 int *max_error,
843 struct timeval *vblank_time,
844 unsigned flags,
845 const struct drm_display_mode *mode)
846 {
847 struct timeval tv_etime;
848 ktime_t stime, etime;
849 unsigned int vbl_status;
850 int ret = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
851 int vpos, hpos, i;
852 int delta_ns, duration_ns;
853
854 if (pipe >= dev->num_crtcs) {
855 DRM_ERROR("Invalid crtc %u\n", pipe);
856 return -EINVAL;
857 }
858
859 /* Scanout position query not supported? Should not happen. */
860 if (!dev->driver->get_scanout_position) {
861 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
862 return -EIO;
863 }
864
865 /* If mode timing undefined, just return as no-op:
866 * Happens during initial modesetting of a crtc.
867 */
868 if (mode->crtc_clock == 0) {
869 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe);
870 return -EAGAIN;
871 }
872
873 /* Get current scanout position with system timestamp.
874 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
875 * if single query takes longer than max_error nanoseconds.
876 *
877 * This guarantees a tight bound on maximum error if
878 * code gets preempted or delayed for some reason.
879 */
880 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
881 /*
882 * Get vertical and horizontal scanout position vpos, hpos,
883 * and bounding timestamps stime, etime, pre/post query.
884 */
885 vbl_status = dev->driver->get_scanout_position(dev, pipe, flags,
886 &vpos, &hpos,
887 &stime, &etime,
888 mode);
889
890 /* Return as no-op if scanout query unsupported or failed. */
891 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
892 DRM_DEBUG("crtc %u : scanoutpos query failed [0x%x].\n",
893 pipe, vbl_status);
894 return -EIO;
895 }
896
897 /* Compute uncertainty in timestamp of scanout position query. */
898 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
899
900 /* Accept result with < max_error nsecs timing uncertainty. */
901 if (duration_ns <= *max_error)
902 break;
903 }
904
905 /* Noisy system timing? */
906 if (i == DRM_TIMESTAMP_MAXRETRIES) {
907 DRM_DEBUG("crtc %u: Noisy timestamp %d us > %d us [%d reps].\n",
908 pipe, duration_ns/1000, *max_error/1000, i);
909 }
910
911 /* Return upper bound of timestamp precision error. */
912 *max_error = duration_ns;
913
914 /* Check if in vblank area:
915 * vpos is >=0 in video scanout area, but negative
916 * within vblank area, counting down the number of lines until
917 * start of scanout.
918 */
919 if (vbl_status & DRM_SCANOUTPOS_IN_VBLANK)
920 ret |= DRM_VBLANKTIME_IN_VBLANK;
921
922 /* Convert scanout position into elapsed time at raw_time query
923 * since start of scanout at first display scanline. delta_ns
924 * can be negative if start of scanout hasn't happened yet.
925 */
926 delta_ns = div_s64(1000000LL * (vpos * mode->crtc_htotal + hpos),
927 mode->crtc_clock);
928
929 if (!drm_timestamp_monotonic)
930 etime = ktime_mono_to_real(etime);
931
932 /* save this only for debugging purposes */
933 tv_etime = ktime_to_timeval(etime);
934 /* Subtract time delta from raw timestamp to get final
935 * vblank_time timestamp for end of vblank.
936 */
937 if (delta_ns < 0)
938 etime = ktime_add_ns(etime, -delta_ns);
939 else
940 etime = ktime_sub_ns(etime, delta_ns);
941 *vblank_time = ktime_to_timeval(etime);
942
943 DRM_DEBUG_VBL("crtc %u : v 0x%x p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
944 pipe, vbl_status, hpos, vpos,
945 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
946 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
947 duration_ns/1000, i);
948
949 return ret;
950 }
951 EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
952
953 static struct timeval get_drm_timestamp(void)
954 {
955 ktime_t now;
956
957 now = drm_timestamp_monotonic ? ktime_get() : ktime_get_real();
958 return ktime_to_timeval(now);
959 }
960
961 /**
962 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
963 * vblank interval
964 * @dev: DRM device
965 * @pipe: index of CRTC whose vblank timestamp to retrieve
966 * @tvblank: Pointer to target struct timeval which should receive the timestamp
967 * @flags: Flags to pass to driver:
968 * 0 = Default,
969 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
970 *
971 * Fetches the system timestamp corresponding to the time of the most recent
972 * vblank interval on specified CRTC. May call into kms-driver to
973 * compute the timestamp with a high-precision GPU specific method.
974 *
975 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
976 * call, i.e., it isn't very precisely locked to the true vblank.
977 *
978 * Returns:
979 * True if timestamp is considered to be very precise, false otherwise.
980 */
981 static bool
982 drm_get_last_vbltimestamp(struct drm_device *dev, unsigned int pipe,
983 struct timeval *tvblank, unsigned flags)
984 {
985 int ret;
986
987 /* Define requested maximum error on timestamps (nanoseconds). */
988 int max_error = (int) drm_timestamp_precision * 1000;
989
990 /* Query driver if possible and precision timestamping enabled. */
991 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
992 ret = dev->driver->get_vblank_timestamp(dev, pipe, &max_error,
993 tvblank, flags);
994 if (ret > 0)
995 return true;
996 }
997
998 /* GPU high precision timestamp query unsupported or failed.
999 * Return current monotonic/gettimeofday timestamp as best estimate.
1000 */
1001 *tvblank = get_drm_timestamp();
1002
1003 return false;
1004 }
1005
1006 /**
1007 * drm_vblank_count - retrieve "cooked" vblank counter value
1008 * @dev: DRM device
1009 * @pipe: index of CRTC for which to retrieve the counter
1010 *
1011 * Fetches the "cooked" vblank count value that represents the number of
1012 * vblank events since the system was booted, including lost events due to
1013 * modesetting activity.
1014 *
1015 * This is the legacy version of drm_crtc_vblank_count().
1016 *
1017 * Returns:
1018 * The software vblank counter.
1019 */
1020 u32 drm_vblank_count(struct drm_device *dev, unsigned int pipe)
1021 {
1022 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1023
1024 if (WARN_ON(pipe >= dev->num_crtcs))
1025 return 0;
1026
1027 return vblank->count;
1028 }
1029 EXPORT_SYMBOL(drm_vblank_count);
1030
1031 /**
1032 * drm_crtc_vblank_count - retrieve "cooked" vblank counter value
1033 * @crtc: which counter to retrieve
1034 *
1035 * Fetches the "cooked" vblank count value that represents the number of
1036 * vblank events since the system was booted, including lost events due to
1037 * modesetting activity.
1038 *
1039 * This is the native KMS version of drm_vblank_count().
1040 *
1041 * Returns:
1042 * The software vblank counter.
1043 */
1044 u32 drm_crtc_vblank_count(struct drm_crtc *crtc)
1045 {
1046 return drm_vblank_count(crtc->dev, drm_crtc_index(crtc));
1047 }
1048 EXPORT_SYMBOL(drm_crtc_vblank_count);
1049
1050 /**
1051 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value and the
1052 * system timestamp corresponding to that vblank counter value.
1053 * @dev: DRM device
1054 * @pipe: index of CRTC whose counter to retrieve
1055 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
1056 *
1057 * Fetches the "cooked" vblank count value that represents the number of
1058 * vblank events since the system was booted, including lost events due to
1059 * modesetting activity. Returns corresponding system timestamp of the time
1060 * of the vblank interval that corresponds to the current vblank counter value.
1061 *
1062 * This is the legacy version of drm_crtc_vblank_count_and_time().
1063 */
1064 u32 drm_vblank_count_and_time(struct drm_device *dev, unsigned int pipe,
1065 struct timeval *vblanktime)
1066 {
1067 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1068 int count = DRM_TIMESTAMP_MAXRETRIES;
1069 u32 cur_vblank;
1070
1071 if (WARN_ON(pipe >= dev->num_crtcs))
1072 return 0;
1073
1074 /*
1075 * Vblank timestamps are read lockless. To ensure consistency the vblank
1076 * counter is rechecked and ordering is ensured using memory barriers.
1077 * This works like a seqlock. The write-side barriers are in store_vblank.
1078 */
1079 do {
1080 cur_vblank = vblank->count;
1081 smp_rmb();
1082 *vblanktime = vblanktimestamp(dev, pipe, cur_vblank);
1083 smp_rmb();
1084 } while (cur_vblank != vblank->count && --count > 0);
1085
1086 return cur_vblank;
1087 }
1088 EXPORT_SYMBOL(drm_vblank_count_and_time);
1089
1090 /**
1091 * drm_crtc_vblank_count_and_time - retrieve "cooked" vblank counter value
1092 * and the system timestamp corresponding to that vblank counter value
1093 * @crtc: which counter to retrieve
1094 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
1095 *
1096 * Fetches the "cooked" vblank count value that represents the number of
1097 * vblank events since the system was booted, including lost events due to
1098 * modesetting activity. Returns corresponding system timestamp of the time
1099 * of the vblank interval that corresponds to the current vblank counter value.
1100 *
1101 * This is the native KMS version of drm_vblank_count_and_time().
1102 */
1103 u32 drm_crtc_vblank_count_and_time(struct drm_crtc *crtc,
1104 struct timeval *vblanktime)
1105 {
1106 return drm_vblank_count_and_time(crtc->dev, drm_crtc_index(crtc),
1107 vblanktime);
1108 }
1109 EXPORT_SYMBOL(drm_crtc_vblank_count_and_time);
1110
1111 static void send_vblank_event(struct drm_device *dev,
1112 struct drm_pending_vblank_event *e,
1113 unsigned long seq, struct timeval *now)
1114 {
1115 assert_spin_locked(&dev->event_lock);
1116
1117 e->event.sequence = seq;
1118 e->event.tv_sec = now->tv_sec;
1119 e->event.tv_usec = now->tv_usec;
1120
1121 list_add_tail(&e->base.link,
1122 &e->base.file_priv->event_list);
1123 #ifdef __NetBSD__
1124 DRM_SPIN_WAKEUP_ONE(&e->base.file_priv->event_wait, &dev->event_lock);
1125 selnotify(&e->base.file_priv->event_selq, (POLLIN | POLLRDNORM),
1126 NOTE_SUBMIT);
1127 #else
1128 wake_up_interruptible(&e->base.file_priv->event_wait);
1129 #endif
1130 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
1131 e->event.sequence);
1132 }
1133
1134 /**
1135 * drm_arm_vblank_event - arm vblank event after pageflip
1136 * @dev: DRM device
1137 * @pipe: CRTC index
1138 * @e: the event to prepare to send
1139 *
1140 * A lot of drivers need to generate vblank events for the very next vblank
1141 * interrupt. For example when the page flip interrupt happens when the page
1142 * flip gets armed, but not when it actually executes within the next vblank
1143 * period. This helper function implements exactly the required vblank arming
1144 * behaviour.
1145 *
1146 * Caller must hold event lock. Caller must also hold a vblank reference for
1147 * the event @e, which will be dropped when the next vblank arrives.
1148 *
1149 * This is the legacy version of drm_crtc_arm_vblank_event().
1150 */
1151 void drm_arm_vblank_event(struct drm_device *dev, unsigned int pipe,
1152 struct drm_pending_vblank_event *e)
1153 {
1154 assert_spin_locked(&dev->event_lock);
1155
1156 e->pipe = pipe;
1157 e->event.sequence = drm_vblank_count(dev, pipe);
1158 list_add_tail(&e->base.link, &dev->vblank_event_list);
1159 }
1160 EXPORT_SYMBOL(drm_arm_vblank_event);
1161
1162 /**
1163 * drm_crtc_arm_vblank_event - arm vblank event after pageflip
1164 * @crtc: the source CRTC of the vblank event
1165 * @e: the event to send
1166 *
1167 * A lot of drivers need to generate vblank events for the very next vblank
1168 * interrupt. For example when the page flip interrupt happens when the page
1169 * flip gets armed, but not when it actually executes within the next vblank
1170 * period. This helper function implements exactly the required vblank arming
1171 * behaviour.
1172 *
1173 * Caller must hold event lock. Caller must also hold a vblank reference for
1174 * the event @e, which will be dropped when the next vblank arrives.
1175 *
1176 * This is the native KMS version of drm_arm_vblank_event().
1177 */
1178 void drm_crtc_arm_vblank_event(struct drm_crtc *crtc,
1179 struct drm_pending_vblank_event *e)
1180 {
1181 drm_arm_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
1182 }
1183 EXPORT_SYMBOL(drm_crtc_arm_vblank_event);
1184
1185 /**
1186 * drm_send_vblank_event - helper to send vblank event after pageflip
1187 * @dev: DRM device
1188 * @pipe: CRTC index
1189 * @e: the event to send
1190 *
1191 * Updates sequence # and timestamp on event, and sends it to userspace.
1192 * Caller must hold event lock.
1193 *
1194 * This is the legacy version of drm_crtc_send_vblank_event().
1195 */
1196 void drm_send_vblank_event(struct drm_device *dev, unsigned int pipe,
1197 struct drm_pending_vblank_event *e)
1198 {
1199 struct timeval now;
1200 unsigned int seq;
1201
1202 if (dev->num_crtcs > 0) {
1203 seq = drm_vblank_count_and_time(dev, pipe, &now);
1204 } else {
1205 seq = 0;
1206
1207 now = get_drm_timestamp();
1208 }
1209 e->pipe = pipe;
1210 send_vblank_event(dev, e, seq, &now);
1211 }
1212 EXPORT_SYMBOL(drm_send_vblank_event);
1213
1214 /**
1215 * drm_crtc_send_vblank_event - helper to send vblank event after pageflip
1216 * @crtc: the source CRTC of the vblank event
1217 * @e: the event to send
1218 *
1219 * Updates sequence # and timestamp on event, and sends it to userspace.
1220 * Caller must hold event lock.
1221 *
1222 * This is the native KMS version of drm_send_vblank_event().
1223 */
1224 void drm_crtc_send_vblank_event(struct drm_crtc *crtc,
1225 struct drm_pending_vblank_event *e)
1226 {
1227 drm_send_vblank_event(crtc->dev, drm_crtc_index(crtc), e);
1228 }
1229 EXPORT_SYMBOL(drm_crtc_send_vblank_event);
1230
1231 /**
1232 * drm_vblank_enable - enable the vblank interrupt on a CRTC
1233 * @dev: DRM device
1234 * @pipe: CRTC index
1235 *
1236 * Returns:
1237 * Zero on success or a negative error code on failure.
1238 */
1239 static int drm_vblank_enable(struct drm_device *dev, unsigned int pipe)
1240 {
1241 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1242 int ret = 0;
1243
1244 assert_spin_locked(&dev->vbl_lock);
1245
1246 spin_lock(&dev->vblank_time_lock);
1247
1248 if (!vblank->enabled) {
1249 /*
1250 * Enable vblank irqs under vblank_time_lock protection.
1251 * All vblank count & timestamp updates are held off
1252 * until we are done reinitializing master counter and
1253 * timestamps. Filtercode in drm_handle_vblank() will
1254 * prevent double-accounting of same vblank interval.
1255 */
1256 ret = dev->driver->enable_vblank(dev, pipe);
1257 DRM_DEBUG("enabling vblank on crtc %u, ret: %d\n", pipe, ret);
1258 if (ret)
1259 atomic_dec(&vblank->refcount);
1260 else {
1261 vblank->enabled = true;
1262 drm_update_vblank_count(dev, pipe, 0);
1263 }
1264 }
1265
1266 spin_unlock(&dev->vblank_time_lock);
1267
1268 return ret;
1269 }
1270
1271 /**
1272 * drm_vblank_get - get a reference count on vblank events
1273 * @dev: DRM device
1274 * @pipe: index of CRTC to own
1275 *
1276 * Acquire a reference count on vblank events to avoid having them disabled
1277 * while in use.
1278 *
1279 * This is the legacy version of drm_crtc_vblank_get().
1280 *
1281 * Returns:
1282 * Zero on success or a negative error code on failure.
1283 */
1284 int drm_vblank_get(struct drm_device *dev, unsigned int pipe)
1285 {
1286 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1287 unsigned long irqflags;
1288 int ret = 0;
1289
1290 if (!dev->num_crtcs)
1291 return -EINVAL;
1292
1293 if (WARN_ON(pipe >= dev->num_crtcs))
1294 return -EINVAL;
1295
1296 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1297 /* Going from 0->1 means we have to enable interrupts again */
1298 if (atomic_add_return(1, &vblank->refcount) == 1) {
1299 ret = drm_vblank_enable(dev, pipe);
1300 } else {
1301 if (!vblank->enabled) {
1302 atomic_dec(&vblank->refcount);
1303 ret = -EINVAL;
1304 }
1305 }
1306 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1307
1308 return ret;
1309 }
1310 EXPORT_SYMBOL(drm_vblank_get);
1311
1312 /**
1313 * drm_crtc_vblank_get - get a reference count on vblank events
1314 * @crtc: which CRTC to own
1315 *
1316 * Acquire a reference count on vblank events to avoid having them disabled
1317 * while in use.
1318 *
1319 * This is the native kms version of drm_vblank_get().
1320 *
1321 * Returns:
1322 * Zero on success or a negative error code on failure.
1323 */
1324 int drm_crtc_vblank_get(struct drm_crtc *crtc)
1325 {
1326 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
1327 }
1328 EXPORT_SYMBOL(drm_crtc_vblank_get);
1329
1330 /**
1331 * drm_vblank_put - release ownership of vblank events
1332 * @dev: DRM device
1333 * @pipe: index of CRTC to release
1334 *
1335 * Release ownership of a given vblank counter, turning off interrupts
1336 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1337 *
1338 * This is the legacy version of drm_crtc_vblank_put().
1339 */
1340 void drm_vblank_put(struct drm_device *dev, unsigned int pipe)
1341 {
1342 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1343
1344 if (WARN_ON(pipe >= dev->num_crtcs))
1345 return;
1346
1347 if (WARN_ON(atomic_read(&vblank->refcount) == 0))
1348 return;
1349
1350 /* Last user schedules interrupt disable */
1351 if (atomic_dec_and_test(&vblank->refcount)) {
1352 if (drm_vblank_offdelay == 0)
1353 return;
1354 else if (drm_vblank_offdelay < 0)
1355 vblank_disable_fn((unsigned long)vblank);
1356 else if (!dev->vblank_disable_immediate)
1357 mod_timer(&vblank->disable_timer,
1358 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1359 }
1360 }
1361 EXPORT_SYMBOL(drm_vblank_put);
1362
1363 /**
1364 * drm_crtc_vblank_put - give up ownership of vblank events
1365 * @crtc: which counter to give up
1366 *
1367 * Release ownership of a given vblank counter, turning off interrupts
1368 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1369 *
1370 * This is the native kms version of drm_vblank_put().
1371 */
1372 void drm_crtc_vblank_put(struct drm_crtc *crtc)
1373 {
1374 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1375 }
1376 EXPORT_SYMBOL(drm_crtc_vblank_put);
1377
1378 /**
1379 * drm_wait_one_vblank - wait for one vblank
1380 * @dev: DRM device
1381 * @pipe: CRTC index
1382 *
1383 * This waits for one vblank to pass on @pipe, using the irq driver interfaces.
1384 * It is a failure to call this when the vblank irq for @pipe is disabled, e.g.
1385 * due to lack of driver support or because the crtc is off.
1386 */
1387 void drm_wait_one_vblank(struct drm_device *dev, unsigned int pipe)
1388 {
1389 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1390 int ret;
1391 u32 last;
1392
1393 if (WARN_ON(pipe >= dev->num_crtcs))
1394 return;
1395
1396 ret = drm_vblank_get(dev, pipe);
1397 if (WARN(ret, "vblank not available on crtc %i, ret=%i\n", pipe, ret))
1398 return;
1399
1400 #ifdef __NetBSD__
1401 spin_lock(&dev->vbl_lock);
1402 last = drm_vblank_count(dev, pipe);
1403 DRM_SPIN_TIMED_WAIT_UNTIL(ret, &vblank->queue, &dev->vbl_lock,
1404 msecs_to_jiffies(100),
1405 last != drm_vblank_count(dev, pipe));
1406 spin_unlock(&dev->vbl_lock);
1407 #else
1408 last = drm_vblank_count(dev, pipe);
1409
1410 ret = wait_event_timeout(vblank->queue,
1411 last != drm_vblank_count(dev, pipe),
1412 msecs_to_jiffies(100));
1413 #endif
1414
1415 WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1416
1417 drm_vblank_put(dev, pipe);
1418 }
1419 EXPORT_SYMBOL(drm_wait_one_vblank);
1420
1421 /**
1422 * drm_crtc_wait_one_vblank - wait for one vblank
1423 * @crtc: DRM crtc
1424 *
1425 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1426 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1427 * due to lack of driver support or because the crtc is off.
1428 */
1429 void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1430 {
1431 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1432 }
1433 EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1434
1435 /**
1436 * drm_vblank_off - disable vblank events on a CRTC
1437 * @dev: DRM device
1438 * @pipe: CRTC index
1439 *
1440 * Drivers can use this function to shut down the vblank interrupt handling when
1441 * disabling a crtc. This function ensures that the latest vblank frame count is
1442 * stored so that drm_vblank_on() can restore it again.
1443 *
1444 * Drivers must use this function when the hardware vblank counter can get
1445 * reset, e.g. when suspending.
1446 *
1447 * This is the legacy version of drm_crtc_vblank_off().
1448 */
1449 void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
1450 {
1451 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1452 struct drm_pending_vblank_event *e, *t;
1453 struct timeval now;
1454 unsigned long irqflags;
1455 unsigned int seq;
1456
1457 if (WARN_ON(pipe >= dev->num_crtcs))
1458 return;
1459
1460 spin_lock_irqsave(&dev->event_lock, irqflags);
1461
1462 spin_lock(&dev->vbl_lock);
1463 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1464 pipe, vblank->enabled, vblank->inmodeset);
1465
1466 /* Avoid redundant vblank disables without previous drm_vblank_on(). */
1467 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1468 vblank_disable_and_save(dev, pipe);
1469
1470 #ifdef __NetBSD__
1471 DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->vbl_lock);
1472 #else
1473 wake_up(&vblank->queue);
1474 #endif
1475
1476 /*
1477 * Prevent subsequent drm_vblank_get() from re-enabling
1478 * the vblank interrupt by bumping the refcount.
1479 */
1480 if (!vblank->inmodeset) {
1481 atomic_inc(&vblank->refcount);
1482 vblank->inmodeset = 1;
1483 }
1484 spin_unlock(&dev->vbl_lock);
1485
1486 /* Send any queued vblank events, lest the natives grow disquiet */
1487 seq = drm_vblank_count_and_time(dev, pipe, &now);
1488
1489 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1490 if (e->pipe != pipe)
1491 continue;
1492 DRM_DEBUG("Sending premature vblank event on disable: "
1493 "wanted %d, current %d\n",
1494 e->event.sequence, seq);
1495 list_del(&e->base.link);
1496 drm_vblank_put(dev, pipe);
1497 send_vblank_event(dev, e, seq, &now);
1498 }
1499 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1500 }
1501 EXPORT_SYMBOL(drm_vblank_off);
1502
1503 /**
1504 * drm_crtc_vblank_off - disable vblank events on a CRTC
1505 * @crtc: CRTC in question
1506 *
1507 * Drivers can use this function to shut down the vblank interrupt handling when
1508 * disabling a crtc. This function ensures that the latest vblank frame count is
1509 * stored so that drm_vblank_on can restore it again.
1510 *
1511 * Drivers must use this function when the hardware vblank counter can get
1512 * reset, e.g. when suspending.
1513 *
1514 * This is the native kms version of drm_vblank_off().
1515 */
1516 void drm_crtc_vblank_off(struct drm_crtc *crtc)
1517 {
1518 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1519 }
1520 EXPORT_SYMBOL(drm_crtc_vblank_off);
1521
1522 /**
1523 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1524 * @crtc: CRTC in question
1525 *
1526 * Drivers can use this function to reset the vblank state to off at load time.
1527 * Drivers should use this together with the drm_crtc_vblank_off() and
1528 * drm_crtc_vblank_on() functions. The difference compared to
1529 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1530 * and hence doesn't need to call any driver hooks.
1531 */
1532 void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1533 {
1534 struct drm_device *dev = crtc->dev;
1535 unsigned long irqflags;
1536 unsigned int pipe = drm_crtc_index(crtc);
1537 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1538
1539 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1540 /*
1541 * Prevent subsequent drm_vblank_get() from enabling the vblank
1542 * interrupt by bumping the refcount.
1543 */
1544 if (!vblank->inmodeset) {
1545 atomic_inc(&vblank->refcount);
1546 vblank->inmodeset = 1;
1547 }
1548 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1549
1550 WARN_ON(!list_empty(&dev->vblank_event_list));
1551 }
1552 EXPORT_SYMBOL(drm_crtc_vblank_reset);
1553
1554 /**
1555 * drm_vblank_on - enable vblank events on a CRTC
1556 * @dev: DRM device
1557 * @pipe: CRTC index
1558 *
1559 * This functions restores the vblank interrupt state captured with
1560 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1561 * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1562 * in driver load code to reflect the current hardware state of the crtc.
1563 *
1564 * This is the legacy version of drm_crtc_vblank_on().
1565 */
1566 void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
1567 {
1568 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1569 unsigned long irqflags;
1570
1571 if (WARN_ON(pipe >= dev->num_crtcs))
1572 return;
1573
1574 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1575 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1576 pipe, vblank->enabled, vblank->inmodeset);
1577
1578 /* Drop our private "prevent drm_vblank_get" refcount */
1579 if (vblank->inmodeset) {
1580 atomic_dec(&vblank->refcount);
1581 vblank->inmodeset = 0;
1582 }
1583
1584 drm_reset_vblank_timestamp(dev, pipe);
1585
1586 /*
1587 * re-enable interrupts if there are users left, or the
1588 * user wishes vblank interrupts to be enabled all the time.
1589 */
1590 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1591 WARN_ON(drm_vblank_enable(dev, pipe));
1592 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1593 }
1594 EXPORT_SYMBOL(drm_vblank_on);
1595
1596 /**
1597 * drm_crtc_vblank_on - enable vblank events on a CRTC
1598 * @crtc: CRTC in question
1599 *
1600 * This functions restores the vblank interrupt state captured with
1601 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1602 * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1603 * in driver load code to reflect the current hardware state of the crtc.
1604 *
1605 * This is the native kms version of drm_vblank_on().
1606 */
1607 void drm_crtc_vblank_on(struct drm_crtc *crtc)
1608 {
1609 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1610 }
1611 EXPORT_SYMBOL(drm_crtc_vblank_on);
1612
1613 /**
1614 * drm_vblank_pre_modeset - account for vblanks across mode sets
1615 * @dev: DRM device
1616 * @pipe: CRTC index
1617 *
1618 * Account for vblank events across mode setting events, which will likely
1619 * reset the hardware frame counter.
1620 *
1621 * This is done by grabbing a temporary vblank reference to ensure that the
1622 * vblank interrupt keeps running across the modeset sequence. With this the
1623 * software-side vblank frame counting will ensure that there are no jumps or
1624 * discontinuities.
1625 *
1626 * Unfortunately this approach is racy and also doesn't work when the vblank
1627 * interrupt stops running, e.g. across system suspend resume. It is therefore
1628 * highly recommended that drivers use the newer drm_vblank_off() and
1629 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1630 * using "cooked" software vblank frame counters and not relying on any hardware
1631 * counters.
1632 *
1633 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1634 * again.
1635 */
1636 void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
1637 {
1638 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1639
1640 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1641 if (!dev->num_crtcs)
1642 return;
1643
1644 if (WARN_ON(pipe >= dev->num_crtcs))
1645 return;
1646
1647 /*
1648 * To avoid all the problems that might happen if interrupts
1649 * were enabled/disabled around or between these calls, we just
1650 * have the kernel take a reference on the CRTC (just once though
1651 * to avoid corrupting the count if multiple, mismatch calls occur),
1652 * so that interrupts remain enabled in the interim.
1653 */
1654 if (!vblank->inmodeset) {
1655 vblank->inmodeset = 0x1;
1656 if (drm_vblank_get(dev, pipe) == 0)
1657 vblank->inmodeset |= 0x2;
1658 }
1659 }
1660 EXPORT_SYMBOL(drm_vblank_pre_modeset);
1661
1662 /**
1663 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1664 * @dev: DRM device
1665 * @pipe: CRTC index
1666 *
1667 * This function again drops the temporary vblank reference acquired in
1668 * drm_vblank_pre_modeset.
1669 */
1670 void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
1671 {
1672 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1673 unsigned long irqflags;
1674
1675 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1676 if (!dev->num_crtcs)
1677 return;
1678
1679 if (WARN_ON(pipe >= dev->num_crtcs))
1680 return;
1681
1682 if (vblank->inmodeset) {
1683 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1684 dev->vblank_disable_allowed = true;
1685 drm_reset_vblank_timestamp(dev, pipe);
1686 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1687
1688 if (vblank->inmodeset & 0x2)
1689 drm_vblank_put(dev, pipe);
1690
1691 vblank->inmodeset = 0;
1692 }
1693 }
1694 EXPORT_SYMBOL(drm_vblank_post_modeset);
1695
1696 /*
1697 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1698 * @DRM_IOCTL_ARGS: standard ioctl arguments
1699 *
1700 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1701 * ioctls around modesetting so that any lost vblank events are accounted for.
1702 *
1703 * Generally the counter will reset across mode sets. If interrupts are
1704 * enabled around this call, we don't have to do anything since the counter
1705 * will have already been incremented.
1706 */
1707 int drm_modeset_ctl(struct drm_device *dev, void *data,
1708 struct drm_file *file_priv)
1709 {
1710 struct drm_modeset_ctl *modeset = data;
1711 unsigned int pipe;
1712
1713 /* If drm_vblank_init() hasn't been called yet, just no-op */
1714 if (!dev->num_crtcs)
1715 return 0;
1716
1717 /* KMS drivers handle this internally */
1718 if (drm_core_check_feature(dev, DRIVER_MODESET))
1719 return 0;
1720
1721 pipe = modeset->crtc;
1722 if (pipe >= dev->num_crtcs)
1723 return -EINVAL;
1724
1725 switch (modeset->cmd) {
1726 case _DRM_PRE_MODESET:
1727 drm_vblank_pre_modeset(dev, pipe);
1728 break;
1729 case _DRM_POST_MODESET:
1730 drm_vblank_post_modeset(dev, pipe);
1731 break;
1732 default:
1733 return -EINVAL;
1734 }
1735
1736 return 0;
1737 }
1738
1739 static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
1740 union drm_wait_vblank *vblwait,
1741 struct drm_file *file_priv)
1742 {
1743 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1744 struct drm_pending_vblank_event *e;
1745 struct timeval now;
1746 unsigned long flags;
1747 unsigned int seq;
1748 int ret;
1749
1750 e = kzalloc(sizeof(*e), GFP_KERNEL);
1751 if (e == NULL) {
1752 ret = -ENOMEM;
1753 goto err_put;
1754 }
1755
1756 e->pipe = pipe;
1757 #ifdef __NetBSD__
1758 e->base.pid = curproc->p_pid;
1759 #else
1760 e->base.pid = current->pid;
1761 #endif
1762 e->event.base.type = DRM_EVENT_VBLANK;
1763 e->event.base.length = sizeof(e->event);
1764 e->event.user_data = vblwait->request.signal;
1765 e->base.event = &e->event.base;
1766 e->base.file_priv = file_priv;
1767 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1768
1769 spin_lock_irqsave(&dev->event_lock, flags);
1770
1771 /*
1772 * drm_vblank_off() might have been called after we called
1773 * drm_vblank_get(). drm_vblank_off() holds event_lock
1774 * around the vblank disable, so no need for further locking.
1775 * The reference from drm_vblank_get() protects against
1776 * vblank disable from another source.
1777 */
1778 if (!vblank->enabled) {
1779 ret = -EINVAL;
1780 goto err_unlock;
1781 }
1782
1783 if (file_priv->event_space < sizeof(e->event)) {
1784 ret = -EBUSY;
1785 goto err_unlock;
1786 }
1787
1788 file_priv->event_space -= sizeof(e->event);
1789 seq = drm_vblank_count_and_time(dev, pipe, &now);
1790
1791 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1792 (seq - vblwait->request.sequence) <= (1 << 23)) {
1793 vblwait->request.sequence = seq + 1;
1794 vblwait->reply.sequence = vblwait->request.sequence;
1795 }
1796
1797 DRM_DEBUG("event on vblank count %d, current %d, crtc %u\n",
1798 vblwait->request.sequence, seq, pipe);
1799
1800 #ifdef __NetBSD__
1801 trace_drm_vblank_event_queued(curproc->p_pid, pipe,
1802 vblwait->request.sequence);
1803 #else
1804 trace_drm_vblank_event_queued(current->pid, pipe,
1805 vblwait->request.sequence);
1806 #endif
1807
1808 e->event.sequence = vblwait->request.sequence;
1809 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1810 drm_vblank_put(dev, pipe);
1811 send_vblank_event(dev, e, seq, &now);
1812 vblwait->reply.sequence = seq;
1813 } else {
1814 /* drm_handle_vblank_events will call drm_vblank_put */
1815 list_add_tail(&e->base.link, &dev->vblank_event_list);
1816 vblwait->reply.sequence = vblwait->request.sequence;
1817 }
1818
1819 spin_unlock_irqrestore(&dev->event_lock, flags);
1820
1821 return 0;
1822
1823 err_unlock:
1824 spin_unlock_irqrestore(&dev->event_lock, flags);
1825 kfree(e);
1826 err_put:
1827 drm_vblank_put(dev, pipe);
1828 return ret;
1829 }
1830
1831 /*
1832 * Wait for VBLANK.
1833 *
1834 * \param inode device inode.
1835 * \param file_priv DRM file private.
1836 * \param cmd command.
1837 * \param data user argument, pointing to a drm_wait_vblank structure.
1838 * \return zero on success or a negative number on failure.
1839 *
1840 * This function enables the vblank interrupt on the pipe requested, then
1841 * sleeps waiting for the requested sequence number to occur, and drops
1842 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
1843 * after a timeout with no further vblank waits scheduled).
1844 */
1845 int drm_wait_vblank(struct drm_device *dev, void *data,
1846 struct drm_file *file_priv)
1847 {
1848 struct drm_vblank_crtc *vblank;
1849 union drm_wait_vblank *vblwait = data;
1850 int ret;
1851 unsigned int flags, seq, pipe, high_pipe;
1852
1853 if (!dev->irq_enabled)
1854 return -EINVAL;
1855
1856 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1857 return -EINVAL;
1858
1859 if (vblwait->request.type &
1860 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1861 _DRM_VBLANK_HIGH_CRTC_MASK)) {
1862 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1863 vblwait->request.type,
1864 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1865 _DRM_VBLANK_HIGH_CRTC_MASK));
1866 return -EINVAL;
1867 }
1868
1869 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1870 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1871 if (high_pipe)
1872 pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1873 else
1874 pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1875 if (pipe >= dev->num_crtcs)
1876 return -EINVAL;
1877
1878 vblank = &dev->vblank[pipe];
1879
1880 ret = drm_vblank_get(dev, pipe);
1881 if (ret) {
1882 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
1883 return ret;
1884 }
1885 seq = drm_vblank_count(dev, pipe);
1886
1887 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1888 case _DRM_VBLANK_RELATIVE:
1889 vblwait->request.sequence += seq;
1890 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1891 case _DRM_VBLANK_ABSOLUTE:
1892 break;
1893 default:
1894 ret = -EINVAL;
1895 goto done;
1896 }
1897
1898 if (flags & _DRM_VBLANK_EVENT) {
1899 /* must hold on to the vblank ref until the event fires
1900 * drm_vblank_put will be called asynchronously
1901 */
1902 return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
1903 }
1904
1905 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1906 (seq - vblwait->request.sequence) <= (1<<23)) {
1907 vblwait->request.sequence = seq + 1;
1908 }
1909
1910 DRM_DEBUG("waiting on vblank count %d, crtc %u\n",
1911 vblwait->request.sequence, pipe);
1912 vblank->last_wait = vblwait->request.sequence;
1913 #ifdef __NetBSD__
1914 {
1915 unsigned long irqflags;
1916
1917 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1918 DRM_SPIN_WAIT_ON(ret, &vblank->queue, &dev->vbl_lock,
1919 3 * HZ,
1920 (((drm_vblank_count(dev, pipe) -
1921 vblwait->request.sequence) <= (1 << 23)) ||
1922 !vblank->enabled ||
1923 !dev->irq_enabled));
1924 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1925 }
1926 #else
1927 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
1928 (((drm_vblank_count(dev, pipe) -
1929 vblwait->request.sequence) <= (1 << 23)) ||
1930 !vblank->enabled ||
1931 !dev->irq_enabled));
1932 #endif
1933
1934 if (ret != -EINTR) {
1935 struct timeval now;
1936
1937 vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
1938 vblwait->reply.tval_sec = now.tv_sec;
1939 vblwait->reply.tval_usec = now.tv_usec;
1940
1941 DRM_DEBUG("returning %d to client\n",
1942 vblwait->reply.sequence);
1943 } else {
1944 DRM_DEBUG("vblank wait interrupted by signal\n");
1945 }
1946
1947 done:
1948 drm_vblank_put(dev, pipe);
1949 return ret;
1950 }
1951
1952 static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1953 {
1954 struct drm_pending_vblank_event *e, *t;
1955 struct timeval now;
1956 unsigned int seq;
1957
1958 assert_spin_locked(&dev->event_lock);
1959
1960 seq = drm_vblank_count_and_time(dev, pipe, &now);
1961
1962 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1963 if (e->pipe != pipe)
1964 continue;
1965 if ((seq - e->event.sequence) > (1<<23))
1966 continue;
1967
1968 DRM_DEBUG("vblank event on %d, current %d\n",
1969 e->event.sequence, seq);
1970
1971 list_del(&e->base.link);
1972 drm_vblank_put(dev, pipe);
1973 send_vblank_event(dev, e, seq, &now);
1974 }
1975
1976 trace_drm_vblank_event(pipe, seq);
1977 }
1978
1979 /**
1980 * drm_handle_vblank - handle a vblank event
1981 * @dev: DRM device
1982 * @pipe: index of CRTC where this event occurred
1983 *
1984 * Drivers should call this routine in their vblank interrupt handlers to
1985 * update the vblank counter and send any signals that may be pending.
1986 *
1987 * This is the legacy version of drm_crtc_handle_vblank().
1988 */
1989 bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1990 {
1991 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1992 unsigned long irqflags;
1993 #ifdef __NetBSD__ /* XXX vblank locking */
1994 unsigned long irqflags_vbl_lock;
1995 #endif
1996
1997 if (WARN_ON_ONCE(!dev->num_crtcs))
1998 return false;
1999
2000 if (WARN_ON(pipe >= dev->num_crtcs))
2001 return false;
2002
2003 spin_lock_irqsave(&dev->event_lock, irqflags);
2004 #ifdef __NetBSD__ /* XXX vblank locking */
2005 spin_lock_irqsave(&dev->vbl_lock, irqflags_vbl_lock);
2006 #endif
2007
2008 /* Need timestamp lock to prevent concurrent execution with
2009 * vblank enable/disable, as this would cause inconsistent
2010 * or corrupted timestamps and vblank counts.
2011 */
2012 spin_lock(&dev->vblank_time_lock);
2013
2014 /* Vblank irq handling disabled. Nothing to do. */
2015 if (!vblank->enabled) {
2016 spin_unlock(&dev->vblank_time_lock);
2017 spin_unlock_irqrestore(&dev->event_lock, irqflags);
2018 #ifdef __NetBSD__ /* XXX vblank locking */
2019 spin_unlock_irqrestore(&dev->vbl_lock, irqflags_vbl_lock);
2020 #endif
2021 return false;
2022 }
2023
2024 drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
2025
2026 spin_unlock(&dev->vblank_time_lock);
2027
2028 #ifdef __NetBSD__
2029 DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->vbl_lock);
2030 #else
2031 wake_up(&vblank->queue);
2032 #endif
2033 drm_handle_vblank_events(dev, pipe);
2034
2035 /* With instant-off, we defer disabling the interrupt until after
2036 * we finish processing the following vblank. The disable has to
2037 * be last (after drm_handle_vblank_events) so that the timestamp
2038 * is always accurate.
2039 */
2040 if (dev->vblank_disable_immediate &&
2041 drm_vblank_offdelay > 0 &&
2042 !atomic_read(&vblank->refcount))
2043 vblank_disable_fn((unsigned long)vblank);
2044
2045 spin_unlock_irqrestore(&dev->event_lock, irqflags);
2046 #ifdef __NetBSD__ /* XXX vblank locking */
2047 spin_unlock_irqrestore(&dev->vbl_lock, irqflags_vbl_lock);
2048 #endif
2049
2050 return true;
2051 }
2052 EXPORT_SYMBOL(drm_handle_vblank);
2053
2054 /**
2055 * drm_crtc_handle_vblank - handle a vblank event
2056 * @crtc: where this event occurred
2057 *
2058 * Drivers should call this routine in their vblank interrupt handlers to
2059 * update the vblank counter and send any signals that may be pending.
2060 *
2061 * This is the native KMS version of drm_handle_vblank().
2062 *
2063 * Returns:
2064 * True if the event was successfully handled, false on failure.
2065 */
2066 bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
2067 {
2068 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
2069 }
2070 EXPORT_SYMBOL(drm_crtc_handle_vblank);
2071
2072 /**
2073 * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
2074 * @dev: DRM device
2075 * @pipe: CRTC for which to read the counter
2076 *
2077 * Drivers can plug this into the .get_vblank_counter() function if
2078 * there is no useable hardware frame counter available.
2079 *
2080 * Returns:
2081 * 0
2082 */
2083 u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
2084 {
2085 return 0;
2086 }
2087 EXPORT_SYMBOL(drm_vblank_no_hw_counter);
2088