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