Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drm: Add get_scanout_position() to struct drm_crtc_helper_funcs

The new callback get_scanout_position() reads the current location
of the scanout process. The operation is currently located in struct
drm_driver, but really belongs to the CRTC. Drivers will be converted
in separate patches.

To help with the conversion, the timestamp calculation has been
moved from drm_calc_vbltimestamp_from_scanoutpos() to
drm_crtc_vblank_helper_get_vblank_timestamp_internal(). The helper
function supports the new and old interface of get_scanout_position().
drm_calc_vbltimestamp_from_scanoutpos() remains as a wrapper around
the new function.

Callback functions return the scanout position from the CRTC. The
legacy version of the interface receives the device and pipe index,
the modern version receives a pointer to the CRTC. We keep the
legacy version until all drivers have been converted.

v4:
* 80-character line fixes
v3:
* refactor drm_calc_vbltimestamp_from_scanoutpos() to minimize
code duplication
* define types for get_scanout_position() callbacks
v2:
* fix logical op in drm_calc_vbltimestamp_from_scanoutpos()

Signed-off-by: Thomas Zimmermann <tzimmermann@suse.de>
Tested-by: Yannick Fertré <yannick.fertre@st.com>
Reviewed-by: Ville Syrjälä <ville.syrjala@linux.intel.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20200123135943.24140-3-tzimmermann@suse.de

+157 -23
+83 -18
drivers/gpu/drm/drm_vblank.c
··· 30 30 #include <drm/drm_crtc.h> 31 31 #include <drm/drm_drv.h> 32 32 #include <drm/drm_framebuffer.h> 33 + #include <drm/drm_modeset_helper_vtables.h> 33 34 #include <drm/drm_print.h> 34 35 #include <drm/drm_vblank.h> 35 36 ··· 606 605 * Implements calculation of exact vblank timestamps from given drm_display_mode 607 606 * timings and current video scanout position of a CRTC. This can be directly 608 607 * used as the &drm_driver.get_vblank_timestamp implementation of a kms driver 609 - * if &drm_driver.get_scanout_position is implemented. 608 + * if &drm_crtc_helper_funcs.get_scanout_position is implemented. 610 609 * 611 610 * The current implementation only handles standard video modes. For double scan 612 611 * and interlaced modes the driver is supposed to adjust the hardware mode ··· 628 627 ktime_t *vblank_time, 629 628 bool in_vblank_irq) 630 629 { 631 - struct timespec64 ts_etime, ts_vblank_time; 632 - ktime_t stime, etime; 633 - bool vbl_status; 634 630 struct drm_crtc *crtc; 635 - const struct drm_display_mode *mode; 636 - struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 637 - int vpos, hpos, i; 638 - int delta_ns, duration_ns; 639 631 640 632 if (!drm_core_check_feature(dev, DRIVER_MODESET)) 641 633 return false; 642 634 643 635 crtc = drm_crtc_from_index(dev, pipe); 636 + if (!crtc) 637 + return false; 644 638 645 - if (pipe >= dev->num_crtcs || !crtc) { 639 + return drm_crtc_vblank_helper_get_vblank_timestamp_internal(crtc, 640 + max_error, 641 + vblank_time, 642 + in_vblank_irq, 643 + crtc->helper_private->get_scanout_position, 644 + dev->driver->get_scanout_position); 645 + } 646 + EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos); 647 + 648 + /** 649 + * drm_crtc_vblank_helper_get_vblank_timestamp_internal - precise vblank 650 + * timestamp helper 651 + * @dev: DRM device 652 + * @pipe: index of CRTC whose vblank timestamp to retrieve 653 + * @max_error: Desired maximum allowable error in timestamps (nanosecs) 654 + * On return contains true maximum error of timestamp 655 + * @vblank_time: Pointer to time which should receive the timestamp 656 + * @in_vblank_irq: 657 + * True when called from drm_crtc_handle_vblank(). Some drivers 658 + * need to apply some workarounds for gpu-specific vblank irq quirks 659 + * if flag is set. 660 + * @get_scanout_position: 661 + * Callback function to retrieve the scanout position. See 662 + * @struct drm_crtc_helper_funcs.get_scanout_position. 663 + * @get_scanout_position_legacy: 664 + * Callback function to retrieve the scanout position. See 665 + * @struct drm_driver.get_scanout_position. 666 + * 667 + * Implements calculation of exact vblank timestamps from given drm_display_mode 668 + * timings and current video scanout position of a CRTC. 669 + * 670 + * The current implementation only handles standard video modes. For double scan 671 + * and interlaced modes the driver is supposed to adjust the hardware mode 672 + * (taken from &drm_crtc_state.adjusted mode for atomic modeset drivers) to 673 + * match the scanout position reported. 674 + * 675 + * Note that atomic drivers must call drm_calc_timestamping_constants() before 676 + * enabling a CRTC. The atomic helpers already take care of that in 677 + * drm_atomic_helper_update_legacy_modeset_state(). 678 + * 679 + * Returns: 680 + * 681 + * Returns true on success, and false on failure, i.e. when no accurate 682 + * timestamp could be acquired. 683 + */ 684 + bool 685 + drm_crtc_vblank_helper_get_vblank_timestamp_internal( 686 + struct drm_crtc *crtc, int *max_error, ktime_t *vblank_time, 687 + bool in_vblank_irq, 688 + drm_vblank_get_scanout_position_func get_scanout_position, 689 + drm_vblank_get_scanout_position_legacy_func get_scanout_position_legacy) 690 + { 691 + struct drm_device *dev = crtc->dev; 692 + unsigned int pipe = crtc->index; 693 + struct drm_vblank_crtc *vblank = &dev->vblank[pipe]; 694 + struct timespec64 ts_etime, ts_vblank_time; 695 + ktime_t stime, etime; 696 + bool vbl_status; 697 + const struct drm_display_mode *mode; 698 + int vpos, hpos, i; 699 + int delta_ns, duration_ns; 700 + 701 + if (pipe >= dev->num_crtcs) { 646 702 DRM_ERROR("Invalid crtc %u\n", pipe); 647 703 return false; 648 704 } 649 705 650 706 /* Scanout position query not supported? Should not happen. */ 651 - if (!dev->driver->get_scanout_position) { 652 - DRM_ERROR("Called from driver w/o get_scanout_position()!?\n"); 707 + if (!get_scanout_position && !get_scanout_position_legacy) { 708 + DRM_ERROR("Called from CRTC w/o get_scanout_position()!?\n"); 653 709 return false; 654 710 } 655 711 ··· 721 663 if (mode->crtc_clock == 0) { 722 664 DRM_DEBUG("crtc %u: Noop due to uninitialized mode.\n", pipe); 723 665 WARN_ON_ONCE(drm_drv_uses_atomic_modeset(dev)); 724 - 725 666 return false; 726 667 } 727 668 ··· 736 679 * Get vertical and horizontal scanout position vpos, hpos, 737 680 * and bounding timestamps stime, etime, pre/post query. 738 681 */ 739 - vbl_status = dev->driver->get_scanout_position(dev, pipe, 740 - in_vblank_irq, 741 - &vpos, &hpos, 742 - &stime, &etime, 743 - mode); 682 + if (get_scanout_position) { 683 + vbl_status = get_scanout_position(crtc, 684 + in_vblank_irq, 685 + &vpos, &hpos, 686 + &stime, &etime, 687 + mode); 688 + } else { 689 + vbl_status = get_scanout_position_legacy(dev, pipe, 690 + in_vblank_irq, 691 + &vpos, &hpos, 692 + &stime, &etime, 693 + mode); 694 + } 744 695 745 696 /* Return as no-op if scanout query unsupported or failed. */ 746 697 if (!vbl_status) { ··· 800 735 801 736 return true; 802 737 } 803 - EXPORT_SYMBOL(drm_calc_vbltimestamp_from_scanoutpos); 738 + EXPORT_SYMBOL(drm_crtc_vblank_helper_get_vblank_timestamp_internal); 804 739 805 740 /** 806 741 * drm_get_last_vbltimestamp - retrieve raw timestamp for the most recent
+2 -5
include/drm/drm_drv.h
··· 362 362 * True on success, false if a reliable scanout position counter could 363 363 * not be read out. 364 364 * 365 - * FIXME: 366 - * 367 - * Since this is a helper to implement @get_vblank_timestamp, we should 368 - * move it to &struct drm_crtc_helper_funcs, like all the other 369 - * helper-internal hooks. 365 + * This is deprecated and should not be used by new drivers. 366 + * Use &drm_crtc_helper_funcs.get_scanout_position instead. 370 367 */ 371 368 bool (*get_scanout_position) (struct drm_device *dev, unsigned int pipe, 372 369 bool in_vblank_irq, int *vpos, int *hpos,
+47
include/drm/drm_modeset_helper_vtables.h
··· 450 450 */ 451 451 void (*atomic_disable)(struct drm_crtc *crtc, 452 452 struct drm_crtc_state *old_crtc_state); 453 + 454 + /** 455 + * @get_scanout_position: 456 + * 457 + * Called by vblank timestamping code. 458 + * 459 + * Returns the current display scanout position from a CRTC and an 460 + * optional accurate ktime_get() timestamp of when the position was 461 + * measured. Note that this is a helper callback which is only used 462 + * if a driver uses drm_calc_vbltimestamp_from_scanoutpos() for the 463 + * @drm_driver.get_vblank_timestamp callback. 464 + * 465 + * Parameters: 466 + * 467 + * crtc: 468 + * The CRTC. 469 + * in_vblank_irq: 470 + * True when called from drm_crtc_handle_vblank(). Some drivers 471 + * need to apply some workarounds for gpu-specific vblank irq 472 + * quirks if the flag is set. 473 + * vpos: 474 + * Target location for current vertical scanout position. 475 + * hpos: 476 + * Target location for current horizontal scanout position. 477 + * stime: 478 + * Target location for timestamp taken immediately before 479 + * scanout position query. Can be NULL to skip timestamp. 480 + * etime: 481 + * Target location for timestamp taken immediately after 482 + * scanout position query. Can be NULL to skip timestamp. 483 + * mode: 484 + * Current display timings. 485 + * 486 + * Returns vpos as a positive number while in active scanout area. 487 + * Returns vpos as a negative number inside vblank, counting the number 488 + * of scanlines to go until end of vblank, e.g., -1 means "one scanline 489 + * until start of active scanout / end of vblank." 490 + * 491 + * Returns: 492 + * 493 + * True on success, false if a reliable scanout position counter could 494 + * not be read out. 495 + */ 496 + bool (*get_scanout_position)(struct drm_crtc *crtc, 497 + bool in_vblank_irq, int *vpos, int *hpos, 498 + ktime_t *stime, ktime_t *etime, 499 + const struct drm_display_mode *mode); 453 500 }; 454 501 455 502 /**
+25
include/drm/drm_vblank.h
··· 239 239 wait_queue_head_t *drm_crtc_vblank_waitqueue(struct drm_crtc *crtc); 240 240 void drm_crtc_set_max_vblank_count(struct drm_crtc *crtc, 241 241 u32 max_vblank_count); 242 + 243 + typedef bool (*drm_vblank_get_scanout_position_func)(struct drm_crtc *crtc, 244 + bool in_vblank_irq, 245 + int *vpos, int *hpos, 246 + ktime_t *stime, 247 + ktime_t *etime, 248 + const struct drm_display_mode *mode); 249 + 250 + typedef bool (*drm_vblank_get_scanout_position_legacy_func)(struct drm_device *dev, 251 + unsigned int pipe, 252 + bool in_vblank_irq, 253 + int *vpos, 254 + int *hpos, 255 + ktime_t *stime, 256 + ktime_t *etime, 257 + const struct drm_display_mode *mode); 258 + 259 + bool 260 + drm_crtc_vblank_helper_get_vblank_timestamp_internal(struct drm_crtc *crtc, 261 + int *max_error, 262 + ktime_t *vblank_time, 263 + bool in_vblank_irq, 264 + drm_vblank_get_scanout_position_func get_scanout_position, 265 + drm_vblank_get_scanout_position_legacy_func get_scanout_position_legacy); 266 + 242 267 #endif