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