Linux Audio

Check our new training course

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