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