drm/i915: Update scanline_offset only for active crtcs
[deliverable/linux.git] / drivers / gpu / drm / drm_irq.c
CommitLineData
f5752b38
DV
1/*
2 * drm_irq.c IRQ and vblank support
1da177e4
LT
3 *
4 * \author Rickard E. (Rik) Faith <faith@valinux.com>
5 * \author Gareth Hughes <gareth@valinux.com>
6 */
7
8/*
9 * Created: Fri Mar 19 14:30:16 1999 by faith@valinux.com
10 *
11 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
12 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
13 * All Rights Reserved.
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining a
16 * copy of this software and associated documentation files (the "Software"),
17 * to deal in the Software without restriction, including without limitation
18 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
19 * and/or sell copies of the Software, and to permit persons to whom the
20 * Software is furnished to do so, subject to the following conditions:
21 *
22 * The above copyright notice and this permission notice (including the next
23 * paragraph) shall be included in all copies or substantial portions of the
24 * Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
29 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
30 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
31 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
32 * OTHER DEALINGS IN THE SOFTWARE.
33 */
34
760285e7 35#include <drm/drmP.h>
ac2874b9 36#include "drm_trace.h"
1da177e4
LT
37
38#include <linux/interrupt.h> /* For task queue support */
5a0e3ad6 39#include <linux/slab.h>
1da177e4 40
28d52043 41#include <linux/vgaarb.h>
2d1a8a48 42#include <linux/export.h>
27641c3f
MK
43
44/* Access macro for slots in vblank timestamp ringbuffer. */
5380e929
VS
45#define vblanktimestamp(dev, crtc, count) \
46 ((dev)->vblank[crtc].time[(count) % DRM_VBLANKTIME_RBSIZE])
27641c3f
MK
47
48/* Retry timestamp calculation up to 3 times to satisfy
49 * drm_timestamp_precision before giving up.
50 */
51#define DRM_TIMESTAMP_MAXRETRIES 3
52
53/* Threshold in nanoseconds for detection of redundant
54 * vblank irq in drm_handle_vblank(). 1 msec should be ok.
55 */
56#define DRM_REDUNDANT_VBLIRQ_THRESH_NS 1000000
57
13b030af
VS
58/**
59 * drm_update_vblank_count - update the master vblank counter
60 * @dev: DRM device
61 * @crtc: counter to update
62 *
63 * Call back into the driver to update the appropriate vblank counter
64 * (specified by @crtc). Deal with wraparound, if it occurred, and
65 * update the last read value so we can deal with wraparound on the next
66 * call if necessary.
67 *
68 * Only necessary when going from off->on, to account for frames we
69 * didn't get an interrupt for.
70 *
71 * Note: caller must hold dev->vbl_lock since this reads & writes
72 * device vblank fields.
73 */
74static void drm_update_vblank_count(struct drm_device *dev, int crtc)
75{
8a51d5be 76 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
13b030af
VS
77 u32 cur_vblank, diff, tslot, rc;
78 struct timeval t_vblank;
79
80 /*
81 * Interrupts were disabled prior to this call, so deal with counter
82 * wrap if needed.
83 * NOTE! It's possible we lost a full dev->max_vblank_count events
84 * here if the register is small or we had vblank interrupts off for
85 * a long time.
86 *
87 * We repeat the hardware vblank counter & timestamp query until
88 * we get consistent results. This to prevent races between gpu
89 * updating its hardware counter while we are retrieving the
90 * corresponding vblank timestamp.
91 */
92 do {
93 cur_vblank = dev->driver->get_vblank_counter(dev, crtc);
94 rc = drm_get_last_vbltimestamp(dev, crtc, &t_vblank, 0);
95 } while (cur_vblank != dev->driver->get_vblank_counter(dev, crtc));
96
97 /* Deal with counter wrap */
8a51d5be
VS
98 diff = cur_vblank - vblank->last;
99 if (cur_vblank < vblank->last) {
13b030af
VS
100 diff += dev->max_vblank_count;
101
102 DRM_DEBUG("last_vblank[%d]=0x%x, cur_vblank=0x%x => diff=0x%x\n",
8a51d5be 103 crtc, vblank->last, cur_vblank, diff);
13b030af
VS
104 }
105
106 DRM_DEBUG("enabling vblank interrupts on crtc %d, missed %d\n",
107 crtc, diff);
108
109 /* Reinitialize corresponding vblank timestamp if high-precision query
110 * available. Skip this step if query unsupported or failed. Will
111 * reinitialize delayed at next vblank interrupt in that case.
112 */
113 if (rc) {
8a51d5be 114 tslot = atomic_read(&vblank->count) + diff;
13b030af
VS
115 vblanktimestamp(dev, crtc, tslot) = t_vblank;
116 }
117
118 smp_mb__before_atomic();
8a51d5be 119 atomic_add(diff, &vblank->count);
13b030af
VS
120 smp_mb__after_atomic();
121}
122
27641c3f
MK
123/*
124 * Disable vblank irq's on crtc, make sure that last vblank count
125 * of hardware and corresponding consistent software vblank counter
126 * are preserved, even if there are any spurious vblank irq's after
127 * disable.
128 */
129static void vblank_disable_and_save(struct drm_device *dev, int crtc)
130{
8a51d5be 131 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
27641c3f
MK
132 unsigned long irqflags;
133 u32 vblcount;
134 s64 diff_ns;
135 int vblrc;
136 struct timeval tvblank;
0355cf3a 137 int count = DRM_TIMESTAMP_MAXRETRIES;
27641c3f
MK
138
139 /* Prevent vblank irq processing while disabling vblank irqs,
140 * so no updates of timestamps or count can happen after we've
141 * disabled. Needed to prevent races in case of delayed irq's.
27641c3f 142 */
27641c3f
MK
143 spin_lock_irqsave(&dev->vblank_time_lock, irqflags);
144
812e7465
VS
145 /*
146 * If the vblank interrupt was already disbled update the count
147 * and timestamp to maintain the appearance that the counter
148 * has been ticking all along until this time. This makes the
149 * count account for the entire time between drm_vblank_on() and
150 * drm_vblank_off().
151 */
8a51d5be 152 if (!vblank->enabled) {
812e7465
VS
153 drm_update_vblank_count(dev, crtc);
154 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
155 return;
156 }
157
27641c3f 158 dev->driver->disable_vblank(dev, crtc);
8a51d5be 159 vblank->enabled = false;
27641c3f
MK
160
161 /* No further vblank irq's will be processed after
162 * this point. Get current hardware vblank count and
163 * vblank timestamp, repeat until they are consistent.
164 *
165 * FIXME: There is still a race condition here and in
166 * drm_update_vblank_count() which can cause off-by-one
167 * reinitialization of software vblank counter. If gpu
168 * vblank counter doesn't increment exactly at the leading
169 * edge of a vblank interval, then we can lose 1 count if
170 * we happen to execute between start of vblank and the
171 * delayed gpu counter increment.
172 */
173 do {
8a51d5be 174 vblank->last = dev->driver->get_vblank_counter(dev, crtc);
27641c3f 175 vblrc = drm_get_last_vbltimestamp(dev, crtc, &tvblank, 0);
8a51d5be 176 } while (vblank->last != dev->driver->get_vblank_counter(dev, crtc) && (--count) && vblrc);
0355cf3a
EE
177
178 if (!count)
179 vblrc = 0;
27641c3f
MK
180
181 /* Compute time difference to stored timestamp of last vblank
182 * as updated by last invocation of drm_handle_vblank() in vblank irq.
183 */
8a51d5be 184 vblcount = atomic_read(&vblank->count);
27641c3f
MK
185 diff_ns = timeval_to_ns(&tvblank) -
186 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
187
188 /* If there is at least 1 msec difference between the last stored
189 * timestamp and tvblank, then we are currently executing our
190 * disable inside a new vblank interval, the tvblank timestamp
191 * corresponds to this new vblank interval and the irq handler
192 * for this vblank didn't run yet and won't run due to our disable.
193 * Therefore we need to do the job of drm_handle_vblank() and
194 * increment the vblank counter by one to account for this vblank.
195 *
196 * Skip this step if there isn't any high precision timestamp
197 * available. In that case we can't account for this and just
198 * hope for the best.
199 */
bc215128 200 if ((vblrc > 0) && (abs64(diff_ns) > 1000000)) {
8a51d5be 201 atomic_inc(&vblank->count);
4e857c58 202 smp_mb__after_atomic();
bc215128 203 }
27641c3f 204
27641c3f 205 spin_unlock_irqrestore(&dev->vblank_time_lock, irqflags);
27641c3f
MK
206}
207
0a3e67a4
JB
208static void vblank_disable_fn(unsigned long arg)
209{
e69595c2
VS
210 struct drm_vblank_crtc *vblank = (void *)arg;
211 struct drm_device *dev = vblank->dev;
0a3e67a4 212 unsigned long irqflags;
e69595c2 213 int crtc = vblank->crtc;
0a3e67a4
JB
214
215 if (!dev->vblank_disable_allowed)
216 return;
217
e69595c2
VS
218 spin_lock_irqsave(&dev->vbl_lock, irqflags);
219 if (atomic_read(&vblank->refcount) == 0 && vblank->enabled) {
220 DRM_DEBUG("disabling vblank on crtc %d\n", crtc);
221 vblank_disable_and_save(dev, crtc);
0a3e67a4 222 }
e69595c2 223 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
0a3e67a4
JB
224}
225
f5752b38
DV
226/**
227 * drm_vblank_cleanup - cleanup vblank support
228 * @dev: DRM device
229 *
230 * This function cleans up any resources allocated in drm_vblank_init.
231 */
52440211 232void drm_vblank_cleanup(struct drm_device *dev)
0a3e67a4 233{
e69595c2
VS
234 int crtc;
235
0a3e67a4
JB
236 /* Bail if the driver didn't call drm_vblank_init() */
237 if (dev->num_crtcs == 0)
238 return;
239
e69595c2 240 for (crtc = 0; crtc < dev->num_crtcs; crtc++) {
8a51d5be
VS
241 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
242
243 del_timer_sync(&vblank->disable_timer);
244 vblank_disable_fn((unsigned long)vblank);
e69595c2 245 }
0a3e67a4 246
5380e929 247 kfree(dev->vblank);
0a3e67a4
JB
248
249 dev->num_crtcs = 0;
250}
e77cef9c 251EXPORT_SYMBOL(drm_vblank_cleanup);
0a3e67a4 252
f5752b38
DV
253/**
254 * drm_vblank_init - initialize vblank support
255 * @dev: drm_device
256 * @num_crtcs: number of crtcs supported by @dev
257 *
258 * This function initializes vblank support for @num_crtcs display pipelines.
259 *
260 * Returns:
261 * Zero on success or a negative error code on failure.
262 */
0a3e67a4
JB
263int drm_vblank_init(struct drm_device *dev, int num_crtcs)
264{
265 int i, ret = -ENOMEM;
266
0a3e67a4 267 spin_lock_init(&dev->vbl_lock);
27641c3f
MK
268 spin_lock_init(&dev->vblank_time_lock);
269
0a3e67a4
JB
270 dev->num_crtcs = num_crtcs;
271
5380e929
VS
272 dev->vblank = kcalloc(num_crtcs, sizeof(*dev->vblank), GFP_KERNEL);
273 if (!dev->vblank)
0a3e67a4
JB
274 goto err;
275
e69595c2 276 for (i = 0; i < num_crtcs; i++) {
8a51d5be
VS
277 struct drm_vblank_crtc *vblank = &dev->vblank[i];
278
279 vblank->dev = dev;
280 vblank->crtc = i;
281 init_waitqueue_head(&vblank->queue);
282 setup_timer(&vblank->disable_timer, vblank_disable_fn,
283 (unsigned long)vblank);
e69595c2 284 }
27641c3f 285
8f6fce03 286 DRM_INFO("Supports vblank timestamp caching Rev 2 (21.10.2013).\n");
27641c3f
MK
287
288 /* Driver specific high-precision vblank timestamping supported? */
289 if (dev->driver->get_vblank_timestamp)
290 DRM_INFO("Driver supports precise vblank timestamp query.\n");
291 else
292 DRM_INFO("No driver support for vblank timestamp query.\n");
293
ba0bf120
VS
294 dev->vblank_disable_allowed = false;
295
0a3e67a4
JB
296 return 0;
297
298err:
299 drm_vblank_cleanup(dev);
300 return ret;
301}
302EXPORT_SYMBOL(drm_vblank_init);
303
28d52043
DA
304static void drm_irq_vgaarb_nokms(void *cookie, bool state)
305{
306 struct drm_device *dev = cookie;
307
308 if (dev->driver->vgaarb_irq) {
309 dev->driver->vgaarb_irq(dev, state);
310 return;
311 }
312
313 if (!dev->irq_enabled)
314 return;
315
5037f8ac
JS
316 if (state) {
317 if (dev->driver->irq_uninstall)
318 dev->driver->irq_uninstall(dev);
319 } else {
320 if (dev->driver->irq_preinstall)
321 dev->driver->irq_preinstall(dev);
322 if (dev->driver->irq_postinstall)
323 dev->driver->irq_postinstall(dev);
28d52043
DA
324 }
325}
326
1da177e4 327/**
f5752b38
DV
328 * drm_irq_install - install IRQ handler
329 * @dev: DRM device
330 * @irq: IRQ number to install the handler for
1da177e4 331 *
0a3e67a4 332 * Initializes the IRQ related data. Installs the handler, calling the driver
f5752b38
DV
333 * irq_preinstall() and irq_postinstall() functions before and after the
334 * installation.
335 *
336 * This is the simplified helper interface provided for drivers with no special
337 * needs. Drivers which need to install interrupt handlers for multiple
338 * interrupts must instead set drm_device->irq_enabled to signal the DRM core
339 * that vblank interrupts are available.
340 *
341 * Returns:
342 * Zero on success or a negative error code on failure.
1da177e4 343 */
bb0f1b5c 344int drm_irq_install(struct drm_device *dev, int irq)
1da177e4 345{
4a1b0714 346 int ret;
b5e89ed5 347 unsigned long sh_flags = 0;
1da177e4
LT
348
349 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
350 return -EINVAL;
351
7c1a38e3 352 if (irq == 0)
1da177e4
LT
353 return -EINVAL;
354
1da177e4 355 /* Driver must have been initialized */
e090c53b 356 if (!dev->dev_private)
1da177e4 357 return -EINVAL;
1da177e4 358
e090c53b 359 if (dev->irq_enabled)
1da177e4 360 return -EBUSY;
4423843c 361 dev->irq_enabled = true;
1da177e4 362
7c1a38e3 363 DRM_DEBUG("irq=%d\n", irq);
1da177e4 364
b5e89ed5 365 /* Before installing handler */
5037f8ac
JS
366 if (dev->driver->irq_preinstall)
367 dev->driver->irq_preinstall(dev);
1da177e4 368
b5e89ed5 369 /* Install handler */
1da177e4 370 if (drm_core_check_feature(dev, DRIVER_IRQ_SHARED))
935f6e3a 371 sh_flags = IRQF_SHARED;
b5e89ed5 372
7c1a38e3 373 ret = request_irq(irq, dev->driver->irq_handler,
5829d183 374 sh_flags, dev->driver->name, dev);
9bfbd5cb 375
b5e89ed5 376 if (ret < 0) {
4423843c 377 dev->irq_enabled = false;
1da177e4
LT
378 return ret;
379 }
380
28d52043
DA
381 if (!drm_core_check_feature(dev, DRIVER_MODESET))
382 vga_client_register(dev->pdev, (void *)dev, drm_irq_vgaarb_nokms, NULL);
383
b5e89ed5 384 /* After installing handler */
5037f8ac
JS
385 if (dev->driver->irq_postinstall)
386 ret = dev->driver->irq_postinstall(dev);
387
0a3e67a4 388 if (ret < 0) {
4423843c 389 dev->irq_enabled = false;
e1c44acc
JS
390 if (!drm_core_check_feature(dev, DRIVER_MODESET))
391 vga_client_register(dev->pdev, NULL, NULL, NULL);
7c1a38e3
DV
392 free_irq(irq, dev);
393 } else {
394 dev->irq = irq;
0a3e67a4 395 }
1da177e4 396
0a3e67a4 397 return ret;
1da177e4 398}
0a3e67a4 399EXPORT_SYMBOL(drm_irq_install);
1da177e4
LT
400
401/**
f5752b38
DV
402 * drm_irq_uninstall - uninstall the IRQ handler
403 * @dev: DRM device
404 *
405 * Calls the driver's irq_uninstall() function and unregisters the IRQ handler.
406 * This should only be called by drivers which used drm_irq_install() to set up
407 * their interrupt handler. Other drivers must only reset
408 * drm_device->irq_enabled to false.
1da177e4 409 *
f5752b38
DV
410 * Note that for kernel modesetting drivers it is a bug if this function fails.
411 * The sanity checks are only to catch buggy user modesetting drivers which call
412 * the same function through an ioctl.
1da177e4 413 *
f5752b38
DV
414 * Returns:
415 * Zero on success or a negative error code on failure.
1da177e4 416 */
27641c3f 417int drm_irq_uninstall(struct drm_device *dev)
1da177e4 418{
dc1336ff 419 unsigned long irqflags;
4423843c
VS
420 bool irq_enabled;
421 int i;
1da177e4
LT
422
423 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
424 return -EINVAL;
425
1da177e4 426 irq_enabled = dev->irq_enabled;
4423843c 427 dev->irq_enabled = false;
1da177e4 428
dc1336ff
JB
429 /*
430 * Wake up any waiters so they don't hang.
431 */
bde4889a
BS
432 if (dev->num_crtcs) {
433 spin_lock_irqsave(&dev->vbl_lock, irqflags);
434 for (i = 0; i < dev->num_crtcs; i++) {
8a51d5be
VS
435 struct drm_vblank_crtc *vblank = &dev->vblank[i];
436
437 wake_up(&vblank->queue);
438 vblank->enabled = false;
439 vblank->last =
bde4889a
BS
440 dev->driver->get_vblank_counter(dev, i);
441 }
442 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
dc1336ff 443 }
dc1336ff 444
b5e89ed5 445 if (!irq_enabled)
1da177e4
LT
446 return -EINVAL;
447
7c1a38e3 448 DRM_DEBUG("irq=%d\n", dev->irq);
1da177e4 449
28d52043
DA
450 if (!drm_core_check_feature(dev, DRIVER_MODESET))
451 vga_client_register(dev->pdev, NULL, NULL, NULL);
452
5037f8ac
JS
453 if (dev->driver->irq_uninstall)
454 dev->driver->irq_uninstall(dev);
1da177e4 455
7c1a38e3 456 free_irq(dev->irq, dev);
1da177e4
LT
457
458 return 0;
459}
460EXPORT_SYMBOL(drm_irq_uninstall);
461
f5752b38 462/*
1da177e4
LT
463 * IRQ control ioctl.
464 *
465 * \param inode device inode.
6c340eac 466 * \param file_priv DRM file private.
1da177e4
LT
467 * \param cmd command.
468 * \param arg user argument, pointing to a drm_control structure.
469 * \return zero on success or a negative number on failure.
470 *
471 * Calls irq_install() or irq_uninstall() according to \p arg.
472 */
c153f45f
EA
473int drm_control(struct drm_device *dev, void *data,
474 struct drm_file *file_priv)
1da177e4 475{
c153f45f 476 struct drm_control *ctl = data;
a319c1a4 477 int ret = 0, irq;
b5e89ed5 478
27641c3f
MK
479 /* if we haven't irq we fallback for compatibility reasons -
480 * this used to be a separate function in drm_dma.h
481 */
1da177e4 482
22471cf6
DV
483 if (!drm_core_check_feature(dev, DRIVER_HAVE_IRQ))
484 return 0;
485 if (drm_core_check_feature(dev, DRIVER_MODESET))
486 return 0;
487 /* UMS was only ever support on pci devices. */
488 if (WARN_ON(!dev->pdev))
489 return -EINVAL;
1da177e4 490
c153f45f 491 switch (ctl->func) {
1da177e4 492 case DRM_INST_HANDLER:
a319c1a4
DV
493 irq = dev->pdev->irq;
494
1da177e4 495 if (dev->if_version < DRM_IF_VERSION(1, 2) &&
a319c1a4 496 ctl->irq != irq)
1da177e4 497 return -EINVAL;
e090c53b 498 mutex_lock(&dev->struct_mutex);
bb0f1b5c 499 ret = drm_irq_install(dev, irq);
e090c53b
DV
500 mutex_unlock(&dev->struct_mutex);
501
502 return ret;
1da177e4 503 case DRM_UNINST_HANDLER:
e090c53b
DV
504 mutex_lock(&dev->struct_mutex);
505 ret = drm_irq_uninstall(dev);
506 mutex_unlock(&dev->struct_mutex);
507
508 return ret;
1da177e4
LT
509 default:
510 return -EINVAL;
511 }
512}
513
27641c3f 514/**
f5752b38
DV
515 * drm_calc_timestamping_constants - calculate vblank timestamp constants
516 * @crtc: drm_crtc whose timestamp constants should be updated.
517 * @mode: display mode containing the scanout timings
27641c3f 518 *
21b21560
VS
519 * Calculate and store various constants which are later
520 * needed by vblank and swap-completion timestamping, e.g,
521 * by drm_calc_vbltimestamp_from_scanoutpos(). They are
f5752b38 522 * derived from CRTC's true scanout timing, so they take
21b21560 523 * things like panel scaling or other adjustments into account.
27641c3f 524 */
545cdd55
VS
525void drm_calc_timestamping_constants(struct drm_crtc *crtc,
526 const struct drm_display_mode *mode)
27641c3f 527{
3c184f69 528 int linedur_ns = 0, pixeldur_ns = 0, framedur_ns = 0;
77666b72 529 int dotclock = mode->crtc_clock;
27641c3f
MK
530
531 /* Valid dotclock? */
532 if (dotclock > 0) {
0dae35a3
VS
533 int frame_size = mode->crtc_htotal * mode->crtc_vtotal;
534
535 /*
536 * Convert scanline length in pixels and video
537 * dot clock to line duration, frame duration
538 * and pixel duration in nanoseconds:
27641c3f 539 */
0dae35a3
VS
540 pixeldur_ns = 1000000 / dotclock;
541 linedur_ns = div_u64((u64) mode->crtc_htotal * 1000000, dotclock);
542 framedur_ns = div_u64((u64) frame_size * 1000000, dotclock);
c0ae24c1
VS
543
544 /*
545 * Fields of interlaced scanout modes are only half a frame duration.
546 */
547 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
548 framedur_ns /= 2;
27641c3f
MK
549 } else
550 DRM_ERROR("crtc %d: Can't calculate constants, dotclock = 0!\n",
551 crtc->base.id);
552
553 crtc->pixeldur_ns = pixeldur_ns;
554 crtc->linedur_ns = linedur_ns;
555 crtc->framedur_ns = framedur_ns;
556
557 DRM_DEBUG("crtc %d: hwmode: htotal %d, vtotal %d, vdisplay %d\n",
545cdd55
VS
558 crtc->base.id, mode->crtc_htotal,
559 mode->crtc_vtotal, mode->crtc_vdisplay);
27641c3f 560 DRM_DEBUG("crtc %d: clock %d kHz framedur %d linedur %d, pixeldur %d\n",
3c184f69
VS
561 crtc->base.id, dotclock, framedur_ns,
562 linedur_ns, pixeldur_ns);
27641c3f
MK
563}
564EXPORT_SYMBOL(drm_calc_timestamping_constants);
565
566/**
f5752b38
DV
567 * drm_calc_vbltimestamp_from_scanoutpos - precise vblank timestamp helper
568 * @dev: DRM device
569 * @crtc: Which CRTC's vblank timestamp to retrieve
570 * @max_error: Desired maximum allowable error in timestamps (nanosecs)
571 * On return contains true maximum error of timestamp
572 * @vblank_time: Pointer to struct timeval which should receive the timestamp
573 * @flags: Flags to pass to driver:
574 * 0 = Default,
575 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
576 * @refcrtc: CRTC which defines scanout timing
577 * @mode: mode which defines the scanout timings
578 *
579 * Implements calculation of exact vblank timestamps from given drm_display_mode
580 * timings and current video scanout position of a CRTC. This can be called from
581 * within get_vblank_timestamp() implementation of a kms driver to implement the
582 * actual timestamping.
27641c3f
MK
583 *
584 * Should return timestamps conforming to the OML_sync_control OpenML
585 * extension specification. The timestamp corresponds to the end of
586 * the vblank interval, aka start of scanout of topmost-leftmost display
587 * pixel in the following video frame.
588 *
589 * Requires support for optional dev->driver->get_scanout_position()
590 * in kms driver, plus a bit of setup code to provide a drm_display_mode
591 * that corresponds to the true scanout timing.
592 *
593 * The current implementation only handles standard video modes. It
594 * returns as no operation if a doublescan or interlaced video mode is
595 * active. Higher level code is expected to handle this.
596 *
f5752b38
DV
597 * Returns:
598 * Negative value on error, failure or if not supported in current
27641c3f
MK
599 * video mode:
600 *
f5752b38 601 * -EINVAL - Invalid CRTC.
27641c3f
MK
602 * -EAGAIN - Temporary unavailable, e.g., called before initial modeset.
603 * -ENOTSUPP - Function not supported in current display mode.
604 * -EIO - Failed, e.g., due to failed scanout position query.
605 *
606 * Returns or'ed positive status flags on success:
607 *
608 * DRM_VBLANKTIME_SCANOUTPOS_METHOD - Signal this method used for timestamping.
609 * DRM_VBLANKTIME_INVBL - Timestamp taken while scanout was in vblank interval.
610 *
611 */
612int drm_calc_vbltimestamp_from_scanoutpos(struct drm_device *dev, int crtc,
613 int *max_error,
614 struct timeval *vblank_time,
615 unsigned flags,
7da903ef
VS
616 const struct drm_crtc *refcrtc,
617 const struct drm_display_mode *mode)
27641c3f 618{
e62f2f5a
ID
619 ktime_t stime, etime, mono_time_offset;
620 struct timeval tv_etime;
8072bfa6 621 int vbl_status;
27641c3f 622 int vpos, hpos, i;
3c184f69 623 int framedur_ns, linedur_ns, pixeldur_ns, delta_ns, duration_ns;
27641c3f
MK
624 bool invbl;
625
626 if (crtc < 0 || crtc >= dev->num_crtcs) {
627 DRM_ERROR("Invalid crtc %d\n", crtc);
628 return -EINVAL;
629 }
630
631 /* Scanout position query not supported? Should not happen. */
632 if (!dev->driver->get_scanout_position) {
633 DRM_ERROR("Called from driver w/o get_scanout_position()!?\n");
634 return -EIO;
635 }
636
27641c3f
MK
637 /* Durations of frames, lines, pixels in nanoseconds. */
638 framedur_ns = refcrtc->framedur_ns;
639 linedur_ns = refcrtc->linedur_ns;
640 pixeldur_ns = refcrtc->pixeldur_ns;
641
642 /* If mode timing undefined, just return as no-op:
643 * Happens during initial modesetting of a crtc.
644 */
8072bfa6 645 if (framedur_ns == 0) {
27641c3f
MK
646 DRM_DEBUG("crtc %d: Noop due to uninitialized mode.\n", crtc);
647 return -EAGAIN;
648 }
649
27641c3f
MK
650 /* Get current scanout position with system timestamp.
651 * Repeat query up to DRM_TIMESTAMP_MAXRETRIES times
652 * if single query takes longer than max_error nanoseconds.
653 *
654 * This guarantees a tight bound on maximum error if
655 * code gets preempted or delayed for some reason.
656 */
657 for (i = 0; i < DRM_TIMESTAMP_MAXRETRIES; i++) {
8f6fce03
MK
658 /*
659 * Get vertical and horizontal scanout position vpos, hpos,
660 * and bounding timestamps stime, etime, pre/post query.
661 */
abca9e45 662 vbl_status = dev->driver->get_scanout_position(dev, crtc, flags, &vpos,
8f6fce03 663 &hpos, &stime, &etime);
27641c3f 664
8f6fce03
MK
665 /*
666 * Get correction for CLOCK_MONOTONIC -> CLOCK_REALTIME if
667 * CLOCK_REALTIME is requested.
668 */
c61eef72
ID
669 if (!drm_timestamp_monotonic)
670 mono_time_offset = ktime_get_monotonic_offset();
27641c3f 671
27641c3f
MK
672 /* Return as no-op if scanout query unsupported or failed. */
673 if (!(vbl_status & DRM_SCANOUTPOS_VALID)) {
674 DRM_DEBUG("crtc %d : scanoutpos query failed [%d].\n",
675 crtc, vbl_status);
676 return -EIO;
677 }
678
8f6fce03 679 /* Compute uncertainty in timestamp of scanout position query. */
e62f2f5a 680 duration_ns = ktime_to_ns(etime) - ktime_to_ns(stime);
27641c3f
MK
681
682 /* Accept result with < max_error nsecs timing uncertainty. */
3c184f69 683 if (duration_ns <= *max_error)
27641c3f
MK
684 break;
685 }
686
687 /* Noisy system timing? */
688 if (i == DRM_TIMESTAMP_MAXRETRIES) {
689 DRM_DEBUG("crtc %d: Noisy timestamp %d us > %d us [%d reps].\n",
3c184f69 690 crtc, duration_ns/1000, *max_error/1000, i);
27641c3f
MK
691 }
692
693 /* Return upper bound of timestamp precision error. */
3c184f69 694 *max_error = duration_ns;
27641c3f
MK
695
696 /* Check if in vblank area:
697 * vpos is >=0 in video scanout area, but negative
698 * within vblank area, counting down the number of lines until
699 * start of scanout.
700 */
701 invbl = vbl_status & DRM_SCANOUTPOS_INVBL;
702
703 /* Convert scanout position into elapsed time at raw_time query
704 * since start of scanout at first display scanline. delta_ns
705 * can be negative if start of scanout hasn't happened yet.
706 */
3c184f69 707 delta_ns = vpos * linedur_ns + hpos * pixeldur_ns;
27641c3f 708
c61eef72
ID
709 if (!drm_timestamp_monotonic)
710 etime = ktime_sub(etime, mono_time_offset);
711
e62f2f5a
ID
712 /* save this only for debugging purposes */
713 tv_etime = ktime_to_timeval(etime);
27641c3f
MK
714 /* Subtract time delta from raw timestamp to get final
715 * vblank_time timestamp for end of vblank.
716 */
e91abf80
MD
717 if (delta_ns < 0)
718 etime = ktime_add_ns(etime, -delta_ns);
719 else
720 etime = ktime_sub_ns(etime, delta_ns);
e62f2f5a 721 *vblank_time = ktime_to_timeval(etime);
27641c3f 722
bbb0aef5
JP
723 DRM_DEBUG("crtc %d : v %d p(%d,%d)@ %ld.%ld -> %ld.%ld [e %d us, %d rep]\n",
724 crtc, (int)vbl_status, hpos, vpos,
e62f2f5a 725 (long)tv_etime.tv_sec, (long)tv_etime.tv_usec,
bbb0aef5 726 (long)vblank_time->tv_sec, (long)vblank_time->tv_usec,
3c184f69 727 duration_ns/1000, i);
27641c3f
MK
728
729 vbl_status = DRM_VBLANKTIME_SCANOUTPOS_METHOD;
730 if (invbl)
731 vbl_status |= DRM_VBLANKTIME_INVBL;
732
733 return vbl_status;
734}
735EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos);
736
c61eef72
ID
737static struct timeval get_drm_timestamp(void)
738{
739 ktime_t now;
740
741 now = ktime_get();
742 if (!drm_timestamp_monotonic)
743 now = ktime_sub(now, ktime_get_monotonic_offset());
744
745 return ktime_to_timeval(now);
746}
747
27641c3f
MK
748/**
749 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
f5752b38 750 * vblank interval
27641c3f 751 * @dev: DRM device
f5752b38 752 * @crtc: which CRTC's vblank timestamp to retrieve
27641c3f
MK
753 * @tvblank: Pointer to target struct timeval which should receive the timestamp
754 * @flags: Flags to pass to driver:
f5752b38
DV
755 * 0 = Default,
756 * DRM_CALLED_FROM_VBLIRQ = If function is called from vbl IRQ handler
27641c3f
MK
757 *
758 * Fetches the system timestamp corresponding to the time of the most recent
f5752b38 759 * vblank interval on specified CRTC. May call into kms-driver to
27641c3f
MK
760 * compute the timestamp with a high-precision GPU specific method.
761 *
762 * Returns zero if timestamp originates from uncorrected do_gettimeofday()
763 * call, i.e., it isn't very precisely locked to the true vblank.
764 *
f5752b38
DV
765 * Returns:
766 * Non-zero if timestamp is considered to be very precise, zero otherwise.
27641c3f
MK
767 */
768u32 drm_get_last_vbltimestamp(struct drm_device *dev, int crtc,
769 struct timeval *tvblank, unsigned flags)
770{
4a1b0714 771 int ret;
27641c3f
MK
772
773 /* Define requested maximum error on timestamps (nanoseconds). */
774 int max_error = (int) drm_timestamp_precision * 1000;
775
776 /* Query driver if possible and precision timestamping enabled. */
777 if (dev->driver->get_vblank_timestamp && (max_error > 0)) {
778 ret = dev->driver->get_vblank_timestamp(dev, crtc, &max_error,
779 tvblank, flags);
780 if (ret > 0)
781 return (u32) ret;
782 }
783
784 /* GPU high precision timestamp query unsupported or failed.
c61eef72 785 * Return current monotonic/gettimeofday timestamp as best estimate.
27641c3f 786 */
c61eef72 787 *tvblank = get_drm_timestamp();
27641c3f
MK
788
789 return 0;
790}
791EXPORT_SYMBOL(drm_get_last_vbltimestamp);
792
0a3e67a4
JB
793/**
794 * drm_vblank_count - retrieve "cooked" vblank counter value
795 * @dev: DRM device
796 * @crtc: which counter to retrieve
797 *
798 * Fetches the "cooked" vblank count value that represents the number of
799 * vblank events since the system was booted, including lost events due to
800 * modesetting activity.
f5752b38
DV
801 *
802 * Returns:
803 * The software vblank counter.
0a3e67a4
JB
804 */
805u32 drm_vblank_count(struct drm_device *dev, int crtc)
806{
8a51d5be
VS
807 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
808
809 return atomic_read(&vblank->count);
0a3e67a4
JB
810}
811EXPORT_SYMBOL(drm_vblank_count);
812
27641c3f
MK
813/**
814 * drm_vblank_count_and_time - retrieve "cooked" vblank counter value
815 * and the system timestamp corresponding to that vblank counter value.
816 *
817 * @dev: DRM device
818 * @crtc: which counter to retrieve
819 * @vblanktime: Pointer to struct timeval to receive the vblank timestamp.
820 *
821 * Fetches the "cooked" vblank count value that represents the number of
822 * vblank events since the system was booted, including lost events due to
823 * modesetting activity. Returns corresponding system timestamp of the time
f5752b38 824 * of the vblank interval that corresponds to the current vblank counter value.
27641c3f
MK
825 */
826u32 drm_vblank_count_and_time(struct drm_device *dev, int crtc,
827 struct timeval *vblanktime)
828{
8a51d5be 829 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
27641c3f
MK
830 u32 cur_vblank;
831
832 /* Read timestamp from slot of _vblank_time ringbuffer
833 * that corresponds to current vblank count. Retry if
834 * count has incremented during readout. This works like
835 * a seqlock.
836 */
837 do {
8a51d5be 838 cur_vblank = atomic_read(&vblank->count);
27641c3f
MK
839 *vblanktime = vblanktimestamp(dev, crtc, cur_vblank);
840 smp_rmb();
8a51d5be 841 } while (cur_vblank != atomic_read(&vblank->count));
27641c3f
MK
842
843 return cur_vblank;
844}
845EXPORT_SYMBOL(drm_vblank_count_and_time);
846
c6eefa17
RC
847static void send_vblank_event(struct drm_device *dev,
848 struct drm_pending_vblank_event *e,
849 unsigned long seq, struct timeval *now)
850{
851 WARN_ON_SMP(!spin_is_locked(&dev->event_lock));
852 e->event.sequence = seq;
853 e->event.tv_sec = now->tv_sec;
854 e->event.tv_usec = now->tv_usec;
855
856 list_add_tail(&e->base.link,
857 &e->base.file_priv->event_list);
858 wake_up_interruptible(&e->base.file_priv->event_wait);
859 trace_drm_vblank_event_delivered(e->base.pid, e->pipe,
860 e->event.sequence);
861}
862
863/**
864 * drm_send_vblank_event - helper to send vblank event after pageflip
865 * @dev: DRM device
866 * @crtc: CRTC in question
867 * @e: the event to send
868 *
869 * Updates sequence # and timestamp on event, and sends it to userspace.
870 * Caller must hold event lock.
871 */
872void drm_send_vblank_event(struct drm_device *dev, int crtc,
873 struct drm_pending_vblank_event *e)
874{
875 struct timeval now;
876 unsigned int seq;
877 if (crtc >= 0) {
878 seq = drm_vblank_count_and_time(dev, crtc, &now);
879 } else {
880 seq = 0;
c61eef72
ID
881
882 now = get_drm_timestamp();
c6eefa17 883 }
21a245d2 884 e->pipe = crtc;
c6eefa17
RC
885 send_vblank_event(dev, e, seq, &now);
886}
887EXPORT_SYMBOL(drm_send_vblank_event);
888
f2752282
VS
889/**
890 * drm_vblank_enable - enable the vblank interrupt on a CRTC
891 * @dev: DRM device
892 * @crtc: CRTC in question
893 */
894static int drm_vblank_enable(struct drm_device *dev, int crtc)
895{
8a51d5be 896 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
f2752282
VS
897 int ret = 0;
898
899 assert_spin_locked(&dev->vbl_lock);
900
901 spin_lock(&dev->vblank_time_lock);
902
8a51d5be 903 if (!vblank->enabled) {
1cfb80b1
DV
904 /*
905 * Enable vblank irqs under vblank_time_lock protection.
f2752282
VS
906 * All vblank count & timestamp updates are held off
907 * until we are done reinitializing master counter and
908 * timestamps. Filtercode in drm_handle_vblank() will
909 * prevent double-accounting of same vblank interval.
910 */
911 ret = dev->driver->enable_vblank(dev, crtc);
912 DRM_DEBUG("enabling vblank on crtc %d, ret: %d\n", crtc, ret);
913 if (ret)
8a51d5be 914 atomic_dec(&vblank->refcount);
f2752282 915 else {
8a51d5be 916 vblank->enabled = true;
f2752282
VS
917 drm_update_vblank_count(dev, crtc);
918 }
919 }
920
921 spin_unlock(&dev->vblank_time_lock);
922
923 return ret;
924}
925
0a3e67a4
JB
926/**
927 * drm_vblank_get - get a reference count on vblank events
928 * @dev: DRM device
929 * @crtc: which CRTC to own
930 *
931 * Acquire a reference count on vblank events to avoid having them disabled
932 * while in use.
933 *
89dd6a4b
DV
934 * This is the legacy version of drm_crtc_vblank_get().
935 *
f5752b38 936 * Returns:
0a3e67a4
JB
937 * Zero on success, nonzero on failure.
938 */
939int drm_vblank_get(struct drm_device *dev, int crtc)
940{
8a51d5be 941 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
97cbc883 942 unsigned long irqflags;
0a3e67a4
JB
943 int ret = 0;
944
945 spin_lock_irqsave(&dev->vbl_lock, irqflags);
946 /* Going from 0->1 means we have to enable interrupts again */
8a51d5be 947 if (atomic_add_return(1, &vblank->refcount) == 1) {
f2752282 948 ret = drm_vblank_enable(dev, crtc);
778c9026 949 } else {
8a51d5be
VS
950 if (!vblank->enabled) {
951 atomic_dec(&vblank->refcount);
778c9026 952 ret = -EINVAL;
0a3e67a4
JB
953 }
954 }
955 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
956
957 return ret;
958}
959EXPORT_SYMBOL(drm_vblank_get);
960
89dd6a4b
DV
961/**
962 * drm_crtc_vblank_get - get a reference count on vblank events
963 * @crtc: which CRTC to own
964 *
965 * Acquire a reference count on vblank events to avoid having them disabled
966 * while in use.
967 *
968 * This is the native kms version of drm_vblank_off().
969 *
970 * Returns:
971 * Zero on success, nonzero on failure.
972 */
973int drm_crtc_vblank_get(struct drm_crtc *crtc)
974{
975 return drm_vblank_get(crtc->dev, drm_crtc_index(crtc));
976}
977EXPORT_SYMBOL(drm_crtc_vblank_get);
978
0a3e67a4
JB
979/**
980 * drm_vblank_put - give up ownership of vblank events
981 * @dev: DRM device
982 * @crtc: which counter to give up
983 *
984 * Release ownership of a given vblank counter, turning off interrupts
27641c3f 985 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
89dd6a4b
DV
986 *
987 * This is the legacy version of drm_crtc_vblank_put().
0a3e67a4
JB
988 */
989void drm_vblank_put(struct drm_device *dev, int crtc)
990{
8a51d5be
VS
991 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
992
993 BUG_ON(atomic_read(&vblank->refcount) == 0);
b3f5e732 994
0a3e67a4 995 /* Last user schedules interrupt disable */
4ed0ce3d 996 if (atomic_dec_and_test(&vblank->refcount)) {
00185e66 997 if (dev->vblank_disable_immediate || drm_vblank_offdelay < 0)
4ed0ce3d
VS
998 vblank_disable_fn((unsigned long)vblank);
999 else if (drm_vblank_offdelay > 0)
1000 mod_timer(&vblank->disable_timer,
1001 jiffies + ((drm_vblank_offdelay * HZ)/1000));
1002 }
0a3e67a4
JB
1003}
1004EXPORT_SYMBOL(drm_vblank_put);
1005
89dd6a4b
DV
1006/**
1007 * drm_crtc_vblank_put - give up ownership of vblank events
1008 * @crtc: which counter to give up
1009 *
1010 * Release ownership of a given vblank counter, turning off interrupts
1011 * if possible. Disable interrupts after drm_vblank_offdelay milliseconds.
1012 *
1013 * This is the native kms version of drm_vblank_put().
1014 */
1015void drm_crtc_vblank_put(struct drm_crtc *crtc)
1016{
1017 drm_vblank_put(crtc->dev, drm_crtc_index(crtc));
1018}
1019EXPORT_SYMBOL(drm_crtc_vblank_put);
1020
c6eefa17
RC
1021/**
1022 * drm_vblank_off - disable vblank events on a CRTC
1023 * @dev: DRM device
1024 * @crtc: CRTC in question
1025 *
f5752b38
DV
1026 * Drivers can use this function to shut down the vblank interrupt handling when
1027 * disabling a crtc. This function ensures that the latest vblank frame count is
1028 * stored so that drm_vblank_on() can restore it again.
1029 *
1030 * Drivers must use this function when the hardware vblank counter can get
1031 * reset, e.g. when suspending.
89dd6a4b
DV
1032 *
1033 * This is the legacy version of drm_crtc_vblank_off().
c6eefa17 1034 */
778c9026
LP
1035void drm_vblank_off(struct drm_device *dev, int crtc)
1036{
8a51d5be 1037 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
498548ec
CJHR
1038 struct drm_pending_vblank_event *e, *t;
1039 struct timeval now;
778c9026 1040 unsigned long irqflags;
498548ec 1041 unsigned int seq;
778c9026 1042
56cc279b
VS
1043 spin_lock_irqsave(&dev->event_lock, irqflags);
1044
1045 spin_lock(&dev->vbl_lock);
27641c3f 1046 vblank_disable_and_save(dev, crtc);
8a51d5be 1047 wake_up(&vblank->queue);
498548ec 1048
56cc279b
VS
1049 /*
1050 * Prevent subsequent drm_vblank_get() from re-enabling
1051 * the vblank interrupt by bumping the refcount.
1052 */
1053 if (!vblank->inmodeset) {
1054 atomic_inc(&vblank->refcount);
1055 vblank->inmodeset = 1;
1056 }
1057 spin_unlock(&dev->vbl_lock);
1058
498548ec
CJHR
1059 /* Send any queued vblank events, lest the natives grow disquiet */
1060 seq = drm_vblank_count_and_time(dev, crtc, &now);
e7783ba3 1061
498548ec
CJHR
1062 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1063 if (e->pipe != crtc)
1064 continue;
1065 DRM_DEBUG("Sending premature vblank event on disable: \
1066 wanted %d, current %d\n",
1067 e->event.sequence, seq);
c6eefa17 1068 list_del(&e->base.link);
498548ec 1069 drm_vblank_put(dev, e->pipe);
c6eefa17 1070 send_vblank_event(dev, e, seq, &now);
498548ec 1071 }
56cc279b 1072 spin_unlock_irqrestore(&dev->event_lock, irqflags);
778c9026
LP
1073}
1074EXPORT_SYMBOL(drm_vblank_off);
1075
89dd6a4b
DV
1076/**
1077 * drm_crtc_vblank_off - disable vblank events on a CRTC
1078 * @crtc: CRTC in question
1079 *
1080 * Drivers can use this function to shut down the vblank interrupt handling when
1081 * disabling a crtc. This function ensures that the latest vblank frame count is
1082 * stored so that drm_vblank_on can restore it again.
1083 *
1084 * Drivers must use this function when the hardware vblank counter can get
1085 * reset, e.g. when suspending.
1086 *
1087 * This is the native kms version of drm_vblank_off().
1088 */
1089void drm_crtc_vblank_off(struct drm_crtc *crtc)
1090{
1091 drm_vblank_off(crtc->dev, drm_crtc_index(crtc));
1092}
1093EXPORT_SYMBOL(drm_crtc_vblank_off);
1094
f2752282
VS
1095/**
1096 * drm_vblank_on - enable vblank events on a CRTC
1097 * @dev: DRM device
1098 * @crtc: CRTC in question
f5752b38
DV
1099 *
1100 * This functions restores the vblank interrupt state captured with
1101 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1102 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1103 * in driver load code to reflect the current hardware state of the crtc.
89dd6a4b
DV
1104 *
1105 * This is the legacy version of drm_crtc_vblank_on().
f2752282
VS
1106 */
1107void drm_vblank_on(struct drm_device *dev, int crtc)
1108{
8a51d5be 1109 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
f2752282
VS
1110 unsigned long irqflags;
1111
1112 spin_lock_irqsave(&dev->vbl_lock, irqflags);
7ffd7a68 1113 /* Drop our private "prevent drm_vblank_get" refcount */
8a51d5be
VS
1114 if (vblank->inmodeset) {
1115 atomic_dec(&vblank->refcount);
1116 vblank->inmodeset = 0;
7ffd7a68 1117 }
f8ad028c
VS
1118
1119 /*
1120 * sample the current counter to avoid random jumps
1121 * when drm_vblank_enable() applies the diff
1122 *
1123 * -1 to make sure user will never see the same
1124 * vblank counter value before and after a modeset
1125 */
8a51d5be 1126 vblank->last =
f8ad028c
VS
1127 (dev->driver->get_vblank_counter(dev, crtc) - 1) &
1128 dev->max_vblank_count;
cd19e52a
VS
1129 /*
1130 * re-enable interrupts if there are users left, or the
1131 * user wishes vblank interrupts to be enabled all the time.
1132 */
1133 if (atomic_read(&vblank->refcount) != 0 ||
1134 (!dev->vblank_disable_immediate && drm_vblank_offdelay == 0))
f2752282
VS
1135 WARN_ON(drm_vblank_enable(dev, crtc));
1136 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
1137}
1138EXPORT_SYMBOL(drm_vblank_on);
1139
89dd6a4b
DV
1140/**
1141 * drm_crtc_vblank_on - enable vblank events on a CRTC
1142 * @crtc: CRTC in question
1143 *
1144 * This functions restores the vblank interrupt state captured with
1145 * drm_vblank_off() again. Note that calls to drm_vblank_on() and
1146 * drm_vblank_off() can be unbalanced and so can also be unconditionaly called
1147 * in driver load code to reflect the current hardware state of the crtc.
1148 *
1149 * This is the native kms version of drm_vblank_on().
1150 */
1151void drm_crtc_vblank_on(struct drm_crtc *crtc)
1152{
1153 drm_vblank_on(crtc->dev, drm_crtc_index(crtc));
1154}
1155EXPORT_SYMBOL(drm_crtc_vblank_on);
1156
f453ba04
DA
1157/**
1158 * drm_vblank_pre_modeset - account for vblanks across mode sets
1159 * @dev: DRM device
1160 * @crtc: CRTC in question
f453ba04
DA
1161 *
1162 * Account for vblank events across mode setting events, which will likely
1163 * reset the hardware frame counter.
f5752b38
DV
1164 *
1165 * This is done by grabbing a temporary vblank reference to ensure that the
1166 * vblank interrupt keeps running across the modeset sequence. With this the
1167 * software-side vblank frame counting will ensure that there are no jumps or
1168 * discontinuities.
1169 *
1170 * Unfortunately this approach is racy and also doesn't work when the vblank
1171 * interrupt stops running, e.g. across system suspend resume. It is therefore
1172 * highly recommended that drivers use the newer drm_vblank_off() and
1173 * drm_vblank_on() instead. drm_vblank_pre_modeset() only works correctly when
1174 * using "cooked" software vblank frame counters and not relying on any hardware
1175 * counters.
1176 *
1177 * Drivers must call drm_vblank_post_modeset() when re-enabling the same crtc
1178 * again.
f453ba04
DA
1179 */
1180void drm_vblank_pre_modeset(struct drm_device *dev, int crtc)
1181{
8a51d5be
VS
1182 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
1183
b7ea85a4 1184 /* vblank is not initialized (IRQ not installed ?), or has been freed */
e77cef9c
JG
1185 if (!dev->num_crtcs)
1186 return;
f453ba04
DA
1187 /*
1188 * To avoid all the problems that might happen if interrupts
1189 * were enabled/disabled around or between these calls, we just
1190 * have the kernel take a reference on the CRTC (just once though
1191 * to avoid corrupting the count if multiple, mismatch calls occur),
1192 * so that interrupts remain enabled in the interim.
1193 */
8a51d5be
VS
1194 if (!vblank->inmodeset) {
1195 vblank->inmodeset = 0x1;
b3f5e732 1196 if (drm_vblank_get(dev, crtc) == 0)
8a51d5be 1197 vblank->inmodeset |= 0x2;
f453ba04
DA
1198 }
1199}
1200EXPORT_SYMBOL(drm_vblank_pre_modeset);
1201
f5752b38
DV
1202/**
1203 * drm_vblank_post_modeset - undo drm_vblank_pre_modeset changes
1204 * @dev: DRM device
1205 * @crtc: CRTC in question
1206 *
1207 * This function again drops the temporary vblank reference acquired in
1208 * drm_vblank_pre_modeset.
1209 */
f453ba04
DA
1210void drm_vblank_post_modeset(struct drm_device *dev, int crtc)
1211{
8a51d5be 1212 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
f453ba04
DA
1213 unsigned long irqflags;
1214
b7ea85a4
HC
1215 /* vblank is not initialized (IRQ not installed ?), or has been freed */
1216 if (!dev->num_crtcs)
1217 return;
1218
8a51d5be 1219 if (vblank->inmodeset) {
f453ba04 1220 spin_lock_irqsave(&dev->vbl_lock, irqflags);
ba0bf120 1221 dev->vblank_disable_allowed = true;
f453ba04 1222 spin_unlock_irqrestore(&dev->vbl_lock, irqflags);
b3f5e732 1223
8a51d5be 1224 if (vblank->inmodeset & 0x2)
b3f5e732
CW
1225 drm_vblank_put(dev, crtc);
1226
8a51d5be 1227 vblank->inmodeset = 0;
f453ba04
DA
1228 }
1229}
1230EXPORT_SYMBOL(drm_vblank_post_modeset);
1231
f5752b38 1232/*
0a3e67a4
JB
1233 * drm_modeset_ctl - handle vblank event counter changes across mode switch
1234 * @DRM_IOCTL_ARGS: standard ioctl arguments
1235 *
1236 * Applications should call the %_DRM_PRE_MODESET and %_DRM_POST_MODESET
1237 * ioctls around modesetting so that any lost vblank events are accounted for.
1238 *
1239 * Generally the counter will reset across mode sets. If interrupts are
1240 * enabled around this call, we don't have to do anything since the counter
1241 * will have already been incremented.
1242 */
1243int drm_modeset_ctl(struct drm_device *dev, void *data,
1244 struct drm_file *file_priv)
1245{
1246 struct drm_modeset_ctl *modeset = data;
19227561 1247 unsigned int crtc;
0a3e67a4
JB
1248
1249 /* If drm_vblank_init() hasn't been called yet, just no-op */
1250 if (!dev->num_crtcs)
4a1b0714 1251 return 0;
0a3e67a4 1252
29935554
LP
1253 /* KMS drivers handle this internally */
1254 if (drm_core_check_feature(dev, DRIVER_MODESET))
1255 return 0;
1256
0a3e67a4 1257 crtc = modeset->crtc;
4a1b0714
LP
1258 if (crtc >= dev->num_crtcs)
1259 return -EINVAL;
0a3e67a4 1260
0a3e67a4
JB
1261 switch (modeset->cmd) {
1262 case _DRM_PRE_MODESET:
f453ba04 1263 drm_vblank_pre_modeset(dev, crtc);
0a3e67a4
JB
1264 break;
1265 case _DRM_POST_MODESET:
f453ba04 1266 drm_vblank_post_modeset(dev, crtc);
0a3e67a4
JB
1267 break;
1268 default:
4a1b0714 1269 return -EINVAL;
0a3e67a4
JB
1270 }
1271
4a1b0714 1272 return 0;
0a3e67a4
JB
1273}
1274
c9a9c5e0
KH
1275static int drm_queue_vblank_event(struct drm_device *dev, int pipe,
1276 union drm_wait_vblank *vblwait,
1277 struct drm_file *file_priv)
1278{
ffe7c73a 1279 struct drm_vblank_crtc *vblank = &dev->vblank[pipe];
c9a9c5e0
KH
1280 struct drm_pending_vblank_event *e;
1281 struct timeval now;
1282 unsigned long flags;
1283 unsigned int seq;
ea5d552c 1284 int ret;
c9a9c5e0
KH
1285
1286 e = kzalloc(sizeof *e, GFP_KERNEL);
ea5d552c
CW
1287 if (e == NULL) {
1288 ret = -ENOMEM;
1289 goto err_put;
1290 }
c9a9c5e0
KH
1291
1292 e->pipe = pipe;
b9c2c9ae 1293 e->base.pid = current->pid;
c9a9c5e0
KH
1294 e->event.base.type = DRM_EVENT_VBLANK;
1295 e->event.base.length = sizeof e->event;
1296 e->event.user_data = vblwait->request.signal;
1297 e->base.event = &e->event.base;
1298 e->base.file_priv = file_priv;
1299 e->base.destroy = (void (*) (struct drm_pending_event *)) kfree;
1300
c9a9c5e0
KH
1301 spin_lock_irqsave(&dev->event_lock, flags);
1302
ffe7c73a
VS
1303 /*
1304 * drm_vblank_off() might have been called after we called
1305 * drm_vblank_get(). drm_vblank_off() holds event_lock
1306 * around the vblank disable, so no need for further locking.
1307 * The reference from drm_vblank_get() protects against
1308 * vblank disable from another source.
1309 */
1310 if (!vblank->enabled) {
1311 ret = -EINVAL;
1312 goto err_unlock;
1313 }
1314
c9a9c5e0 1315 if (file_priv->event_space < sizeof e->event) {
ea5d552c
CW
1316 ret = -EBUSY;
1317 goto err_unlock;
c9a9c5e0
KH
1318 }
1319
1320 file_priv->event_space -= sizeof e->event;
27641c3f
MK
1321 seq = drm_vblank_count_and_time(dev, pipe, &now);
1322
c9a9c5e0
KH
1323 if ((vblwait->request.type & _DRM_VBLANK_NEXTONMISS) &&
1324 (seq - vblwait->request.sequence) <= (1 << 23)) {
1325 vblwait->request.sequence = seq + 1;
4a921645 1326 vblwait->reply.sequence = vblwait->request.sequence;
c9a9c5e0
KH
1327 }
1328
1329 DRM_DEBUG("event on vblank count %d, current %d, crtc %d\n",
1330 vblwait->request.sequence, seq, pipe);
1331
b9c2c9ae
JB
1332 trace_drm_vblank_event_queued(current->pid, pipe,
1333 vblwait->request.sequence);
1334
c9a9c5e0
KH
1335 e->event.sequence = vblwait->request.sequence;
1336 if ((seq - vblwait->request.sequence) <= (1 << 23)) {
6f331623 1337 drm_vblank_put(dev, pipe);
c6eefa17 1338 send_vblank_event(dev, e, seq, &now);
000fa7cf 1339 vblwait->reply.sequence = seq;
c9a9c5e0 1340 } else {
a6778e9e 1341 /* drm_handle_vblank_events will call drm_vblank_put */
c9a9c5e0 1342 list_add_tail(&e->base.link, &dev->vblank_event_list);
000fa7cf 1343 vblwait->reply.sequence = vblwait->request.sequence;
c9a9c5e0
KH
1344 }
1345
1346 spin_unlock_irqrestore(&dev->event_lock, flags);
1347
1348 return 0;
ea5d552c
CW
1349
1350err_unlock:
1351 spin_unlock_irqrestore(&dev->event_lock, flags);
1352 kfree(e);
1353err_put:
6f331623 1354 drm_vblank_put(dev, pipe);
ea5d552c 1355 return ret;
c9a9c5e0
KH
1356}
1357
f5752b38 1358/*
1da177e4
LT
1359 * Wait for VBLANK.
1360 *
1361 * \param inode device inode.
6c340eac 1362 * \param file_priv DRM file private.
1da177e4
LT
1363 * \param cmd command.
1364 * \param data user argument, pointing to a drm_wait_vblank structure.
1365 * \return zero on success or a negative number on failure.
1366 *
30b23634
EA
1367 * This function enables the vblank interrupt on the pipe requested, then
1368 * sleeps waiting for the requested sequence number to occur, and drops
f5752b38 1369 * the vblank interrupt refcount afterwards. (vblank IRQ disable follows that
30b23634 1370 * after a timeout with no further vblank waits scheduled).
1da177e4 1371 */
0a3e67a4
JB
1372int drm_wait_vblank(struct drm_device *dev, void *data,
1373 struct drm_file *file_priv)
1da177e4 1374{
8a51d5be 1375 struct drm_vblank_crtc *vblank;
c153f45f 1376 union drm_wait_vblank *vblwait = data;
4a1b0714 1377 int ret;
19b01b5f 1378 unsigned int flags, seq, crtc, high_crtc;
1da177e4 1379
4984979b
DV
1380 if (!dev->irq_enabled)
1381 return -EINVAL;
1da177e4 1382
30b23634
EA
1383 if (vblwait->request.type & _DRM_VBLANK_SIGNAL)
1384 return -EINVAL;
1385
c153f45f 1386 if (vblwait->request.type &
19b01b5f
IH
1387 ~(_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1388 _DRM_VBLANK_HIGH_CRTC_MASK)) {
776c9443 1389 DRM_ERROR("Unsupported type value 0x%x, supported mask 0x%x\n",
c153f45f 1390 vblwait->request.type,
19b01b5f
IH
1391 (_DRM_VBLANK_TYPES_MASK | _DRM_VBLANK_FLAGS_MASK |
1392 _DRM_VBLANK_HIGH_CRTC_MASK));
776c9443
MCA
1393 return -EINVAL;
1394 }
1395
c153f45f 1396 flags = vblwait->request.type & _DRM_VBLANK_FLAGS_MASK;
19b01b5f
IH
1397 high_crtc = (vblwait->request.type & _DRM_VBLANK_HIGH_CRTC_MASK);
1398 if (high_crtc)
1399 crtc = high_crtc >> _DRM_VBLANK_HIGH_CRTC_SHIFT;
1400 else
1401 crtc = flags & _DRM_VBLANK_SECONDARY ? 1 : 0;
0a3e67a4 1402 if (crtc >= dev->num_crtcs)
776c9443
MCA
1403 return -EINVAL;
1404
8a51d5be
VS
1405 vblank = &dev->vblank[crtc];
1406
0a3e67a4
JB
1407 ret = drm_vblank_get(dev, crtc);
1408 if (ret) {
8d3457ec 1409 DRM_DEBUG("failed to acquire vblank counter, %d\n", ret);
0a3e67a4
JB
1410 return ret;
1411 }
1412 seq = drm_vblank_count(dev, crtc);
776c9443 1413
c153f45f 1414 switch (vblwait->request.type & _DRM_VBLANK_TYPES_MASK) {
1da177e4 1415 case _DRM_VBLANK_RELATIVE:
c153f45f
EA
1416 vblwait->request.sequence += seq;
1417 vblwait->request.type &= ~_DRM_VBLANK_RELATIVE;
1da177e4
LT
1418 case _DRM_VBLANK_ABSOLUTE:
1419 break;
1420 default:
0a3e67a4
JB
1421 ret = -EINVAL;
1422 goto done;
1da177e4
LT
1423 }
1424
a6778e9e
IH
1425 if (flags & _DRM_VBLANK_EVENT) {
1426 /* must hold on to the vblank ref until the event fires
1427 * drm_vblank_put will be called asynchronously
1428 */
c9a9c5e0 1429 return drm_queue_vblank_event(dev, crtc, vblwait, file_priv);
a6778e9e 1430 }
c9a9c5e0 1431
ab285d74 1432 if ((flags & _DRM_VBLANK_NEXTONMISS) &&
c153f45f
EA
1433 (seq - vblwait->request.sequence) <= (1<<23)) {
1434 vblwait->request.sequence = seq + 1;
ab285d74
MCA
1435 }
1436
30b23634
EA
1437 DRM_DEBUG("waiting on vblank count %d, crtc %d\n",
1438 vblwait->request.sequence, crtc);
8a51d5be
VS
1439 vblank->last_wait = vblwait->request.sequence;
1440 DRM_WAIT_ON(ret, vblank->queue, 3 * HZ,
30b23634
EA
1441 (((drm_vblank_count(dev, crtc) -
1442 vblwait->request.sequence) <= (1 << 23)) ||
8a51d5be 1443 !vblank->enabled ||
30b23634 1444 !dev->irq_enabled));
1da177e4 1445
30b23634
EA
1446 if (ret != -EINTR) {
1447 struct timeval now;
1da177e4 1448
27641c3f 1449 vblwait->reply.sequence = drm_vblank_count_and_time(dev, crtc, &now);
30b23634
EA
1450 vblwait->reply.tval_sec = now.tv_sec;
1451 vblwait->reply.tval_usec = now.tv_usec;
27641c3f 1452
30b23634
EA
1453 DRM_DEBUG("returning %d to client\n",
1454 vblwait->reply.sequence);
1da177e4 1455 } else {
30b23634 1456 DRM_DEBUG("vblank wait interrupted by signal\n");
1da177e4
LT
1457 }
1458
0a3e67a4
JB
1459done:
1460 drm_vblank_put(dev, crtc);
1da177e4
LT
1461 return ret;
1462}
1463
ea7f7abc 1464static void drm_handle_vblank_events(struct drm_device *dev, int crtc)
c9a9c5e0
KH
1465{
1466 struct drm_pending_vblank_event *e, *t;
1467 struct timeval now;
c9a9c5e0
KH
1468 unsigned int seq;
1469
56cc279b 1470 assert_spin_locked(&dev->event_lock);
c9a9c5e0 1471
56cc279b 1472 seq = drm_vblank_count_and_time(dev, crtc, &now);
c9a9c5e0
KH
1473
1474 list_for_each_entry_safe(e, t, &dev->vblank_event_list, base.link) {
1475 if (e->pipe != crtc)
1476 continue;
1477 if ((seq - e->event.sequence) > (1<<23))
1478 continue;
1479
1480 DRM_DEBUG("vblank event on %d, current %d\n",
1481 e->event.sequence, seq);
1482
c6eefa17 1483 list_del(&e->base.link);
c9a9c5e0 1484 drm_vblank_put(dev, e->pipe);
c6eefa17 1485 send_vblank_event(dev, e, seq, &now);
c9a9c5e0
KH
1486 }
1487
ac2874b9 1488 trace_drm_vblank_event(crtc, seq);
c9a9c5e0
KH
1489}
1490
0a3e67a4
JB
1491/**
1492 * drm_handle_vblank - handle a vblank event
1493 * @dev: DRM device
1494 * @crtc: where this event occurred
1495 *
1496 * Drivers should call this routine in their vblank interrupt handlers to
1497 * update the vblank counter and send any signals that may be pending.
1498 */
78c6e170 1499bool drm_handle_vblank(struct drm_device *dev, int crtc)
0a3e67a4 1500{
8a51d5be 1501 struct drm_vblank_crtc *vblank = &dev->vblank[crtc];
27641c3f
MK
1502 u32 vblcount;
1503 s64 diff_ns;
1504 struct timeval tvblank;
1505 unsigned long irqflags;
1506
c9a9c5e0 1507 if (!dev->num_crtcs)
78c6e170 1508 return false;
c9a9c5e0 1509
56cc279b
VS
1510 spin_lock_irqsave(&dev->event_lock, irqflags);
1511
27641c3f
MK
1512 /* Need timestamp lock to prevent concurrent execution with
1513 * vblank enable/disable, as this would cause inconsistent
1514 * or corrupted timestamps and vblank counts.
1515 */
56cc279b 1516 spin_lock(&dev->vblank_time_lock);
27641c3f
MK
1517
1518 /* Vblank irq handling disabled. Nothing to do. */
8a51d5be 1519 if (!vblank->enabled) {
56cc279b
VS
1520 spin_unlock(&dev->vblank_time_lock);
1521 spin_unlock_irqrestore(&dev->event_lock, irqflags);
78c6e170 1522 return false;
27641c3f
MK
1523 }
1524
1525 /* Fetch corresponding timestamp for this vblank interval from
1526 * driver and store it in proper slot of timestamp ringbuffer.
1527 */
1528
1529 /* Get current timestamp and count. */
8a51d5be 1530 vblcount = atomic_read(&vblank->count);
27641c3f
MK
1531 drm_get_last_vbltimestamp(dev, crtc, &tvblank, DRM_CALLED_FROM_VBLIRQ);
1532
1533 /* Compute time difference to timestamp of last vblank */
1534 diff_ns = timeval_to_ns(&tvblank) -
1535 timeval_to_ns(&vblanktimestamp(dev, crtc, vblcount));
1536
1537 /* Update vblank timestamp and count if at least
1538 * DRM_REDUNDANT_VBLIRQ_THRESH_NS nanoseconds
1539 * difference between last stored timestamp and current
1540 * timestamp. A smaller difference means basically
1541 * identical timestamps. Happens if this vblank has
1542 * been already processed and this is a redundant call,
1543 * e.g., due to spurious vblank interrupts. We need to
1544 * ignore those for accounting.
1545 */
c4cc3839 1546 if (abs64(diff_ns) > DRM_REDUNDANT_VBLIRQ_THRESH_NS) {
27641c3f
MK
1547 /* Store new timestamp in ringbuffer. */
1548 vblanktimestamp(dev, crtc, vblcount + 1) = tvblank;
27641c3f
MK
1549
1550 /* Increment cooked vblank count. This also atomically commits
1551 * the timestamp computed above.
1552 */
4e857c58 1553 smp_mb__before_atomic();
8a51d5be 1554 atomic_inc(&vblank->count);
4e857c58 1555 smp_mb__after_atomic();
27641c3f
MK
1556 } else {
1557 DRM_DEBUG("crtc %d: Redundant vblirq ignored. diff_ns = %d\n",
1558 crtc, (int) diff_ns);
1559 }
1560
56cc279b
VS
1561 spin_unlock(&dev->vblank_time_lock);
1562
8a51d5be 1563 wake_up(&vblank->queue);
c9a9c5e0 1564 drm_handle_vblank_events(dev, crtc);
27641c3f 1565
56cc279b
VS
1566 spin_unlock_irqrestore(&dev->event_lock, irqflags);
1567
78c6e170 1568 return true;
0a3e67a4
JB
1569}
1570EXPORT_SYMBOL(drm_handle_vblank);
This page took 0.773919 seconds and 5 git commands to generate.