drm_irq.c revision 1.11 1 /* $NetBSD: drm_irq.c,v 1.11 2018/08/27 07:03:25 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.11 2018/08/27 07:03:25 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 last = drm_vblank_count(dev, pipe);
1401
1402 ret = wait_event_timeout(vblank->queue,
1403 last != drm_vblank_count(dev, pipe),
1404 msecs_to_jiffies(100));
1405
1406 WARN(ret == 0, "vblank wait timed out on crtc %i\n", pipe);
1407
1408 drm_vblank_put(dev, pipe);
1409 }
1410 EXPORT_SYMBOL(drm_wait_one_vblank);
1411
1412 /**
1413 * drm_crtc_wait_one_vblank - wait for one vblank
1414 * @crtc: DRM crtc
1415 *
1416 * This waits for one vblank to pass on @crtc, using the irq driver interfaces.
1417 * It is a failure to call this when the vblank irq for @crtc is disabled, e.g.
1418 * due to lack of driver support or because the crtc is off.
1419 */
1420 void drm_crtc_wait_one_vblank(struct drm_crtc *crtc)
1421 {
1422 drm_wait_one_vblank(crtc->dev, drm_crtc_index(crtc));
1423 }
1424 EXPORT_SYMBOL(drm_crtc_wait_one_vblank);
1425
1426 /**
1427 * drm_vblank_off - disable vblank events on a CRTC
1428 * @dev: DRM device
1429 * @pipe: CRTC index
1430 *
1431 * Drivers can use this function to shut down the vblank interrupt handling when
1432 * disabling a crtc. This function ensures that the latest vblank frame count is
1433 * stored so that drm_vblank_on() can restore it again.
1434 *
1435 * Drivers must use this function when the hardware vblank counter can get
1436 * reset, e.g. when suspending.
1437 *
1438 * This is the legacy version of drm_crtc_vblank_off().
1439 */
1440 void drm_vblank_off(struct drm_device *dev, unsigned int pipe)
1441 {
1442 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1443 struct drm_pending_vblank_event *e, *t;
1444 struct timeval now;
1445 unsigned long irqflags;
1446 unsigned int seq;
1447
1448 if (WARN_ON(pipe >= dev->num_crtcs))
1449 return;
1450
1451 spin_lock_irqsave(&dev->event_lock, irqflags);
1452
1453 spin_lock(&dev->vbl_lock);
1454 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1455 pipe, vblank->enabled, vblank->inmodeset);
1456
1457 /* Avoid redundant vblank disables without previous drm_vblank_on(). */
1458 if (drm_core_check_feature(dev, DRIVER_ATOMIC) || !vblank->inmodeset)
1459 vblank_disable_and_save(dev, pipe);
1460
1461 #ifdef __NetBSD__
1462 DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->vbl_lock);
1463 #else
1464 wake_up(&vblank->queue);
1465 #endif
1466
1467 /*
1468 * Prevent subsequent drm_vblank_get() from re-enabling
1469 * the vblank interrupt by bumping the refcount.
1470 */
1471 if (!vblank->inmodeset) {
1472 atomic_inc(&vblank->refcount);
1473 vblank->inmodeset = 1;
1474 }
1475 spin_unlock(&dev->vbl_lock);
1476
1477 /* Send any queued vblank events, lest the natives grow disquiet */
1478 seq = drm_vblank_count_and_time(dev, pipe, &now);
1479
1480 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1481 if (e->pipe != pipe)
1482 continue;
1483 DRM_DEBUG("Sending premature vblank event on disable: "
1484 "wanted %d, current %d\n",
1485 e->event.sequence, seq);
1486 list_del(&e->base.link);
1487 drm_vblank_put(dev, pipe);
1488 send_vblank_event(dev, e, seq, &now);
1489 }
1490 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1491 }
1492 EXPORT_SYMBOL(drm_vblank_off);
1493
1494 /**
1495 * drm_crtc_vblank_off - disable vblank events on a CRTC
1496 * @crtc: CRTC in question
1497 *
1498 * Drivers can use this function to shut down the vblank interrupt handling when
1499 * disabling a crtc. This function ensures that the latest vblank frame count is
1500 * stored so that drm_vblank_on can restore it again.
1501 *
1502 * Drivers must use this function when the hardware vblank counter can get
1503 * reset, e.g. when suspending.
1504 *
1505 * This is the native kms version of drm_vblank_off().
1506 */
1507 void drm_crtc_vblank_off(struct drm_crtc *crtc)
1508 {
1509 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1510 }
1511 EXPORT_SYMBOL(drm_crtc_vblank_off);
1512
1513 /**
1514 * drm_crtc_vblank_reset - reset vblank state to off on a CRTC
1515 * @crtc: CRTC in question
1516 *
1517 * Drivers can use this function to reset the vblank state to off at load time.
1518 * Drivers should use this together with the drm_crtc_vblank_off() and
1519 * drm_crtc_vblank_on() functions. The difference compared to
1520 * drm_crtc_vblank_off() is that this function doesn't save the vblank counter
1521 * and hence doesn't need to call any driver hooks.
1522 */
1523 void drm_crtc_vblank_reset(struct drm_crtc *crtc)
1524 {
1525 struct drm_device *dev = crtc->dev;
1526 unsigned long irqflags;
1527 unsigned int pipe = drm_crtc_index(crtc);
1528 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1529
1530 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1531 /*
1532 * Prevent subsequent drm_vblank_get() from enabling the vblank
1533 * interrupt by bumping the refcount.
1534 */
1535 if (!vblank->inmodeset) {
1536 atomic_inc(&vblank->refcount);
1537 vblank->inmodeset = 1;
1538 }
1539 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1540
1541 WARN_ON(!list_empty(&dev->vblank_event_list));
1542 }
1543 EXPORT_SYMBOL(drm_crtc_vblank_reset);
1544
1545 /**
1546 * drm_vblank_on - enable vblank events on a CRTC
1547 * @dev: DRM device
1548 * @pipe: CRTC index
1549 *
1550 * This functions restores the vblank interrupt state captured with
1551 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1552 * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1553 * in driver load code to reflect the current hardware state of the crtc.
1554 *
1555 * This is the legacy version of drm_crtc_vblank_on().
1556 */
1557 void drm_vblank_on(struct drm_device *dev, unsigned int pipe)
1558 {
1559 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1560 unsigned long irqflags;
1561
1562 if (WARN_ON(pipe >= dev->num_crtcs))
1563 return;
1564
1565 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1566 DRM_DEBUG_VBL("crtc %d, vblank enabled %d, inmodeset %d\n",
1567 pipe, vblank->enabled, vblank->inmodeset);
1568
1569 /* Drop our private "prevent drm_vblank_get" refcount */
1570 if (vblank->inmodeset) {
1571 atomic_dec(&vblank->refcount);
1572 vblank->inmodeset = 0;
1573 }
1574
1575 drm_reset_vblank_timestamp(dev, pipe);
1576
1577 /*
1578 * re-enable interrupts if there are users left, or the
1579 * user wishes vblank interrupts to be enabled all the time.
1580 */
1581 if (atomic_read(&vblank->refcount) != 0 || drm_vblank_offdelay == 0)
1582 WARN_ON(drm_vblank_enable(dev, pipe));
1583 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1584 }
1585 EXPORT_SYMBOL(drm_vblank_on);
1586
1587 /**
1588 * drm_crtc_vblank_on - enable vblank events on a CRTC
1589 * @crtc: CRTC in question
1590 *
1591 * This functions restores the vblank interrupt state captured with
1592 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1593 * drm_vblank_off() can be unbalanced and so can also be unconditionally called
1594 * in driver load code to reflect the current hardware state of the crtc.
1595 *
1596 * This is the native kms version of drm_vblank_on().
1597 */
1598 void drm_crtc_vblank_on(struct drm_crtc *crtc)
1599 {
1600 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1601 }
1602 EXPORT_SYMBOL(drm_crtc_vblank_on);
1603
1604 /**
1605 * drm_vblank_pre_modeset - account for vblanks across mode sets
1606 * @dev: DRM device
1607 * @pipe: CRTC index
1608 *
1609 * Account for vblank events across mode setting events, which will likely
1610 * reset the hardware frame counter.
1611 *
1612 * This is done by grabbing a temporary vblank reference to ensure that the
1613 * vblank interrupt keeps running across the modeset sequence. With this the
1614 * software-side vblank frame counting will ensure that there are no jumps or
1615 * discontinuities.
1616 *
1617 * Unfortunately this approach is racy and also doesn't work when the vblank
1618 * interrupt stops running, e.g. across system suspend resume. It is therefore
1619 * highly recommended that drivers use the newer drm_vblank_off() and
1620 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1621 * using "cooked" software vblank frame counters and not relying on any hardware
1622 * counters.
1623 *
1624 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1625 * again.
1626 */
1627 void drm_vblank_pre_modeset(struct drm_device *dev, unsigned int pipe)
1628 {
1629 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1630
1631 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1632 if (!dev->num_crtcs)
1633 return;
1634
1635 if (WARN_ON(pipe >= dev->num_crtcs))
1636 return;
1637
1638 /*
1639 * To avoid all the problems that might happen if interrupts
1640 * were enabled/disabled around or between these calls, we just
1641 * have the kernel take a reference on the CRTC (just once though
1642 * to avoid corrupting the count if multiple, mismatch calls occur),
1643 * so that interrupts remain enabled in the interim.
1644 */
1645 if (!vblank->inmodeset) {
1646 vblank->inmodeset = 0x1;
1647 if (drm_vblank_get(dev, pipe) == 0)
1648 vblank->inmodeset |= 0x2;
1649 }
1650 }
1651 EXPORT_SYMBOL(drm_vblank_pre_modeset);
1652
1653 /**
1654 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1655 * @dev: DRM device
1656 * @pipe: CRTC index
1657 *
1658 * This function again drops the temporary vblank reference acquired in
1659 * drm_vblank_pre_modeset.
1660 */
1661 void drm_vblank_post_modeset(struct drm_device *dev, unsigned int pipe)
1662 {
1663 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1664 unsigned long irqflags;
1665
1666 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1667 if (!dev->num_crtcs)
1668 return;
1669
1670 if (WARN_ON(pipe >= dev->num_crtcs))
1671 return;
1672
1673 if (vblank->inmodeset) {
1674 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1675 dev->vblank_disable_allowed = true;
1676 drm_reset_vblank_timestamp(dev, pipe);
1677 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1678
1679 if (vblank->inmodeset & 0x2)
1680 drm_vblank_put(dev, pipe);
1681
1682 vblank->inmodeset = 0;
1683 }
1684 }
1685 EXPORT_SYMBOL(drm_vblank_post_modeset);
1686
1687 /*
1688 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1689 * @DRM_IOCTL_ARGS: standard ioctl arguments
1690 *
1691 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1692 * ioctls around modesetting so that any lost vblank events are accounted for.
1693 *
1694 * Generally the counter will reset across mode sets. If interrupts are
1695 * enabled around this call, we don't have to do anything since the counter
1696 * will have already been incremented.
1697 */
1698 int drm_modeset_ctl(struct drm_device *dev, void *data,
1699 struct drm_file *file_priv)
1700 {
1701 struct drm_modeset_ctl *modeset = data;
1702 unsigned int pipe;
1703
1704 /* If drm_vblank_init() hasn't been called yet, just no-op */
1705 if (!dev->num_crtcs)
1706 return 0;
1707
1708 /* KMS drivers handle this internally */
1709 if (drm_core_check_feature(dev, DRIVER_MODESET))
1710 return 0;
1711
1712 pipe = modeset->crtc;
1713 if (pipe >= dev->num_crtcs)
1714 return -EINVAL;
1715
1716 switch (modeset->cmd) {
1717 case _DRM_PRE_MODESET:
1718 drm_vblank_pre_modeset(dev, pipe);
1719 break;
1720 case _DRM_POST_MODESET:
1721 drm_vblank_post_modeset(dev, pipe);
1722 break;
1723 default:
1724 return -EINVAL;
1725 }
1726
1727 return 0;
1728 }
1729
1730 static int drm_queue_vblank_event(struct drm_device *dev, unsigned int pipe,
1731 union drm_wait_vblank *vblwait,
1732 struct drm_file *file_priv)
1733 {
1734 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1735 struct drm_pending_vblank_event *e;
1736 struct timeval now;
1737 unsigned long flags;
1738 unsigned int seq;
1739 int ret;
1740
1741 e = kzalloc(sizeof(*e), GFP_KERNEL);
1742 if (e == NULL) {
1743 ret = -ENOMEM;
1744 goto err_put;
1745 }
1746
1747 e->pipe = pipe;
1748 #ifdef __NetBSD__
1749 e->base.pid = curproc->p_pid;
1750 #else
1751 e->base.pid = current->pid;
1752 #endif
1753 e->event.base.type = DRM_EVENT_VBLANK;
1754 e->event.base.length = sizeof(e->event);
1755 e->event.user_data = vblwait->request.signal;
1756 e->base.event = &e->event.base;
1757 e->base.file_priv = file_priv;
1758 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1759
1760 spin_lock_irqsave(&dev->event_lock, flags);
1761
1762 /*
1763 * drm_vblank_off() might have been called after we called
1764 * drm_vblank_get(). drm_vblank_off() holds event_lock
1765 * around the vblank disable, so no need for further locking.
1766 * The reference from drm_vblank_get() protects against
1767 * vblank disable from another source.
1768 */
1769 if (!vblank->enabled) {
1770 ret = -EINVAL;
1771 goto err_unlock;
1772 }
1773
1774 if (file_priv->event_space < sizeof(e->event)) {
1775 ret = -EBUSY;
1776 goto err_unlock;
1777 }
1778
1779 file_priv->event_space -= sizeof(e->event);
1780 seq = drm_vblank_count_and_time(dev, pipe, &now);
1781
1782 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1783 (seq - vblwait->request.sequence) <= (1 << 23)) {
1784 vblwait->request.sequence = seq + 1;
1785 vblwait->reply.sequence = vblwait->request.sequence;
1786 }
1787
1788 DRM_DEBUG("event on vblank count %d, current %d, crtc %u\n",
1789 vblwait->request.sequence, seq, pipe);
1790
1791 #ifdef __NetBSD__
1792 trace_drm_vblank_event_queued(curproc->p_pid, pipe,
1793 vblwait->request.sequence);
1794 #else
1795 trace_drm_vblank_event_queued(current->pid, pipe,
1796 vblwait->request.sequence);
1797 #endif
1798
1799 e->event.sequence = vblwait->request.sequence;
1800 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
1801 drm_vblank_put(dev, pipe);
1802 send_vblank_event(dev, e, seq, &now);
1803 vblwait->reply.sequence = seq;
1804 } else {
1805 /* drm_handle_vblank_events will call drm_vblank_put */
1806 list_add_tail(&e->base.link, &dev->vblank_event_list);
1807 vblwait->reply.sequence = vblwait->request.sequence;
1808 }
1809
1810 spin_unlock_irqrestore(&dev->event_lock, flags);
1811
1812 return 0;
1813
1814 err_unlock:
1815 spin_unlock_irqrestore(&dev->event_lock, flags);
1816 kfree(e);
1817 err_put:
1818 drm_vblank_put(dev, pipe);
1819 return ret;
1820 }
1821
1822 /*
1823 * Wait for VBLANK.
1824 *
1825 * \param inode device inode.
1826 * \param file_priv DRM file private.
1827 * \param cmd command.
1828 * \param data user argument, pointing to a drm_wait_vblank structure.
1829 * \return zero on success or a negative number on failure.
1830 *
1831 * This function enables the vblank interrupt on the pipe requested, then
1832 * sleeps waiting for the requested sequence number to occur, and drops
1833 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
1834 * after a timeout with no further vblank waits scheduled).
1835 */
1836 int drm_wait_vblank(struct drm_device *dev, void *data,
1837 struct drm_file *file_priv)
1838 {
1839 struct drm_vblank_crtc *vblank;
1840 union drm_wait_vblank *vblwait = data;
1841 int ret;
1842 unsigned int flags, seq, pipe, high_pipe;
1843
1844 if (!dev->irq_enabled)
1845 return -EINVAL;
1846
1847 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1848 return -EINVAL;
1849
1850 if (vblwait->request.type &
1851 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1852 _DRM_VBLANK_HIGH_CRTC_MASK)) {
1853 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
1854 vblwait->request.type,
1855 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1856 _DRM_VBLANK_HIGH_CRTC_MASK));
1857 return -EINVAL;
1858 }
1859
1860 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
1861 high_pipe = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1862 if (high_pipe)
1863 pipe = high_pipe >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1864 else
1865 pipe = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
1866 if (pipe >= dev->num_crtcs)
1867 return -EINVAL;
1868
1869 vblank = &dev->vblank[pipe];
1870
1871 ret = drm_vblank_get(dev, pipe);
1872 if (ret) {
1873 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
1874 return ret;
1875 }
1876 seq = drm_vblank_count(dev, pipe);
1877
1878 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1879 case _DRM_VBLANK_RELATIVE:
1880 vblwait->request.sequence += seq;
1881 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1882 case _DRM_VBLANK_ABSOLUTE:
1883 break;
1884 default:
1885 ret = -EINVAL;
1886 goto done;
1887 }
1888
1889 if (flags & _DRM_VBLANK_EVENT) {
1890 /* must hold on to the vblank ref until the event fires
1891 * drm_vblank_put will be called asynchronously
1892 */
1893 return drm_queue_vblank_event(dev, pipe, vblwait, file_priv);
1894 }
1895
1896 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
1897 (seq - vblwait->request.sequence) <= (1<<23)) {
1898 vblwait->request.sequence = seq + 1;
1899 }
1900
1901 DRM_DEBUG("waiting on vblank count %d, crtc %u\n",
1902 vblwait->request.sequence, pipe);
1903 vblank->last_wait = vblwait->request.sequence;
1904 #ifdef __NetBSD__
1905 {
1906 unsigned long irqflags;
1907
1908 spin_lock_irqsave(&dev->vbl_lock, irqflags);
1909 DRM_SPIN_WAIT_ON(ret, &vblank->queue, &dev->vbl_lock,
1910 3 * HZ,
1911 (((drm_vblank_count(dev, pipe) -
1912 vblwait->request.sequence) <= (1 << 23)) ||
1913 !vblank->enabled ||
1914 !dev->irq_enabled));
1915 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1916 }
1917 #else
1918 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
1919 (((drm_vblank_count(dev, pipe) -
1920 vblwait->request.sequence) <= (1 << 23)) ||
1921 !vblank->enabled ||
1922 !dev->irq_enabled));
1923 #endif
1924
1925 if (ret != -EINTR) {
1926 struct timeval now;
1927
1928 vblwait->reply.sequence = drm_vblank_count_and_time(dev, pipe, &now);
1929 vblwait->reply.tval_sec = now.tv_sec;
1930 vblwait->reply.tval_usec = now.tv_usec;
1931
1932 DRM_DEBUG("returning %d to client\n",
1933 vblwait->reply.sequence);
1934 } else {
1935 DRM_DEBUG("vblank wait interrupted by signal\n");
1936 }
1937
1938 done:
1939 drm_vblank_put(dev, pipe);
1940 return ret;
1941 }
1942
1943 static void drm_handle_vblank_events(struct drm_device *dev, unsigned int pipe)
1944 {
1945 struct drm_pending_vblank_event *e, *t;
1946 struct timeval now;
1947 unsigned int seq;
1948
1949 assert_spin_locked(&dev->event_lock);
1950
1951 seq = drm_vblank_count_and_time(dev, pipe, &now);
1952
1953 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1954 if (e->pipe != pipe)
1955 continue;
1956 if ((seq - e->event.sequence) > (1<<23))
1957 continue;
1958
1959 DRM_DEBUG("vblank event on %d, current %d\n",
1960 e->event.sequence, seq);
1961
1962 list_del(&e->base.link);
1963 drm_vblank_put(dev, pipe);
1964 send_vblank_event(dev, e, seq, &now);
1965 }
1966
1967 trace_drm_vblank_event(pipe, seq);
1968 }
1969
1970 /**
1971 * drm_handle_vblank - handle a vblank event
1972 * @dev: DRM device
1973 * @pipe: index of CRTC where this event occurred
1974 *
1975 * Drivers should call this routine in their vblank interrupt handlers to
1976 * update the vblank counter and send any signals that may be pending.
1977 *
1978 * This is the legacy version of drm_crtc_handle_vblank().
1979 */
1980 bool drm_handle_vblank(struct drm_device *dev, unsigned int pipe)
1981 {
1982 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
1983 unsigned long irqflags;
1984 #ifdef __NetBSD__ /* XXX vblank locking */
1985 unsigned long irqflags_vbl_lock;
1986 #endif
1987
1988 if (WARN_ON_ONCE(!dev->num_crtcs))
1989 return false;
1990
1991 if (WARN_ON(pipe >= dev->num_crtcs))
1992 return false;
1993
1994 spin_lock_irqsave(&dev->event_lock, irqflags);
1995 #ifdef __NetBSD__ /* XXX vblank locking */
1996 spin_lock_irqsave(&dev->vbl_lock, irqflags_vbl_lock);
1997 #endif
1998
1999 /* Need timestamp lock to prevent concurrent execution with
2000 * vblank enable/disable, as this would cause inconsistent
2001 * or corrupted timestamps and vblank counts.
2002 */
2003 spin_lock(&dev->vblank_time_lock);
2004
2005 /* Vblank irq handling disabled. Nothing to do. */
2006 if (!vblank->enabled) {
2007 spin_unlock(&dev->vblank_time_lock);
2008 spin_unlock_irqrestore(&dev->event_lock, irqflags);
2009 #ifdef __NetBSD__ /* XXX vblank locking */
2010 spin_unlock_irqrestore(&dev->vbl_lock, irqflags_vbl_lock);
2011 #endif
2012 return false;
2013 }
2014
2015 drm_update_vblank_count(dev, pipe, DRM_CALLED_FROM_VBLIRQ);
2016
2017 spin_unlock(&dev->vblank_time_lock);
2018
2019 #ifdef __NetBSD__
2020 DRM_SPIN_WAKEUP_ONE(&vblank->queue, &dev->vbl_lock);
2021 #else
2022 wake_up(&vblank->queue);
2023 #endif
2024 drm_handle_vblank_events(dev, pipe);
2025
2026 /* With instant-off, we defer disabling the interrupt until after
2027 * we finish processing the following vblank. The disable has to
2028 * be last (after drm_handle_vblank_events) so that the timestamp
2029 * is always accurate.
2030 */
2031 if (dev->vblank_disable_immediate &&
2032 drm_vblank_offdelay > 0 &&
2033 !atomic_read(&vblank->refcount))
2034 vblank_disable_fn((unsigned long)vblank);
2035
2036 spin_unlock_irqrestore(&dev->event_lock, irqflags);
2037 #ifdef __NetBSD__ /* XXX vblank locking */
2038 spin_unlock_irqrestore(&dev->vbl_lock, irqflags_vbl_lock);
2039 #endif
2040
2041 return true;
2042 }
2043 EXPORT_SYMBOL(drm_handle_vblank);
2044
2045 /**
2046 * drm_crtc_handle_vblank - handle a vblank event
2047 * @crtc: where this event occurred
2048 *
2049 * Drivers should call this routine in their vblank interrupt handlers to
2050 * update the vblank counter and send any signals that may be pending.
2051 *
2052 * This is the native KMS version of drm_handle_vblank().
2053 *
2054 * Returns:
2055 * True if the event was successfully handled, false on failure.
2056 */
2057 bool drm_crtc_handle_vblank(struct drm_crtc *crtc)
2058 {
2059 return drm_handle_vblank(crtc->dev, drm_crtc_index(crtc));
2060 }
2061 EXPORT_SYMBOL(drm_crtc_handle_vblank);
2062
2063 /**
2064 * drm_vblank_no_hw_counter - "No hw counter" implementation of .get_vblank_counter()
2065 * @dev: DRM device
2066 * @pipe: CRTC for which to read the counter
2067 *
2068 * Drivers can plug this into the .get_vblank_counter() function if
2069 * there is no useable hardware frame counter available.
2070 *
2071 * Returns:
2072 * 0
2073 */
2074 u32 drm_vblank_no_hw_counter(struct drm_device *dev, unsigned int pipe)
2075 {
2076 return 0;
2077 }
2078 EXPORT_SYMBOL(drm_vblank_no_hw_counter);
2079