linux/drivers/gpu/drm
Mario Kleiner 5b5561b366 drm/radeon: Fixup hw vblank counter/ts for new drm_update_vblank_count() (v2)
commit 4dfd6486 "drm: Use vblank timestamps to guesstimate how many
vblanks were missed" introduced in Linux 4.4-rc1 makes the drm core
more fragile to drivers which don't update hw vblank counters and
vblank timestamps in sync with firing of the vblank irq and
essentially at leading edge of vblank.

This exposed a problem with radeon-kms/amdgpu-kms which do not
satisfy above requirements:

The vblank irq fires a few scanlines before start of vblank, but
programmed pageflips complete at start of vblank and
vblank timestamps update at start of vblank, whereas the
hw vblank counter increments only later, at start of vsync.

This leads to problems like off by one errors for vblank counter
updates, vblank counters apparently going backwards or vblank
timestamps apparently having time going backwards. The net result
is stuttering of graphics in games, or little hangs, as well as
total failure of timing sensitive applications.

See bug #93147 for an example of the regression on Linux 4.4-rc:

https://bugs.freedesktop.org/show_bug.cgi?id=93147

This patch tries to align all above events better from the
viewpoint of the drm core / of external callers to fix the problem:

1. The apparent start of vblank is shifted a few scanlines earlier,
so the vblank irq now always happens after start of this extended
vblank interval and thereby drm_update_vblank_count() always samples
the updated vblank count and timestamp of the new vblank interval.

To achieve this, the reporting of scanout positions by
radeon_get_crtc_scanoutpos() now operates as if the vblank starts
radeon_crtc->lb_vblank_lead_lines before the real start of the hw
vblank interval. This means that the vblank timestamps which are based
on these scanout positions will now update at this earlier start of
vblank.

2. The driver->get_vblank_counter() function will bump the returned
vblank count as read from the hw by +1 if the query happens after
the shifted earlier start of the vblank, but before the real hw increment
at start of vsync, so the counter appears to increment at start of vblank
in sync with the timestamp update.

3. Calls from vblank irq-context and regular non-irq calls are now
treated identical, always simulating the shifted vblank start, to
avoid inconsistent results for queries happening from vblank irq vs.
happening from drm_vblank_enable() or vblank_disable_fn().

4. The radeon_flip_work_func will delay mmio programming a pageflip until
the start of the real vblank iff it happens to execute inside the shifted
earlier start of the vblank, so pageflips now also appear to execute at
start of the shifted vblank, in sync with vblank counter and timestamp
updates. This to avoid some races between updates of vblank count and
timestamps that are used for swap scheduling and pageflip execution which
could cause pageflips to execute before the scheduled target vblank.

The lb_vblank_lead_lines "fudge" value is calculated as the size of
the display controllers line buffer in scanlines for the given video
mode: Vblank irq's are triggered by the line buffer logic when the line
buffer refill for a video frame ends, ie. when the line buffer source read
position enters the hw vblank. This means that a vblank irq could fire at
most as many scanlines before the current reported scanout position of the
crtc timing generator as the number of scanlines the line buffer can
maximally hold for a given video mode.

This patch has been successfully tested on a RV730 card with DCE-3 display
engine and on a evergreen card with DCE-4 display engine, in single-display
and dual-display configuration, with different video modes.

A similar patch is needed for amdgpu-kms to fix the same problem.

Limitations:

- Line buffer sizes in pixels are hard-coded on < DCE-4 to a value
  i just guessed to be high enough to work ok, lacking info on the true
  sizes atm.

Fixes: fdo#93147

Signed-off-by: Mario Kleiner <mario.kleiner.de@gmail.com>
Cc: Alex Deucher <alexander.deucher@amd.com>
Cc: Michel Dänzer <michel.daenzer@amd.com>
Cc: Harry Wentland <Harry.Wentland@amd.com>
Cc: Ville Syrjälä <ville.syrjala@linux.intel.com>

(v1) Tested-by: Dave Witbrodt <dawitbro@sbcglobal.net>

(v2) Refine radeon_flip_work_func() for better efficiency:

     In radeon_flip_work_func, replace the busy waiting udelay(5)
     with event lock held by a more performance and energy efficient
     usleep_range() until at least predicted true start of hw vblank,
     with some slack for scheduler happiness. Release the event lock
     during waits to not delay other outputs in doing their stuff, as
     the waiting can last up to 200 usecs in some cases.

     Retested on DCE-3 and DCE-4 to verify it still works nicely.

(v2) Signed-off-by: Mario Kleiner <mario.kleiner.de@gmail.com>
Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
2015-12-04 13:11:41 -05:00
..
amd drm/amdgpu: add spin lock to protect freed list in vm (v2) 2015-12-04 13:08:13 -05:00
armada drm/armada: Convert the probe function to the generic drm_of_component_probe() 2015-10-20 12:01:34 +02:00
ast drm/ast: Initialized data needed to map fbdev memory 2015-11-11 12:27:31 +10:00
atmel-hlcdc drm: Stop using drm_vblank_count() as the hw frame counter 2015-10-07 16:13:52 +02:00
bochs
bridge drm/panel: Changes for v4.4-rc1 2015-11-05 11:07:13 +10:00
cirrus
exynos drm/exynos/gem: remove DMA-mapping hacks used for constructing page array 2015-11-03 11:46:39 +09:00
fsl-dcu drm: Stop using drm_vblank_count() as the hw frame counter 2015-10-07 16:13:52 +02:00
gma500 drm/irq: Use unsigned int pipe in public API 2015-10-06 12:57:47 +02:00
i2c Merge branch 'drm-tda998x-devel' of git://ftp.arm.linux.org.uk/~rmk/linux-arm into drm-next 2015-10-15 14:17:13 +10:00
i810
i915 Revert "drm/i915: skip modeset if compatible for everyone." 2015-11-19 10:38:09 +02:00
imx imx-drm fixes and color format updates 2015-11-05 11:06:56 +10:00
mga drm/irq: Use unsigned int pipe in public API 2015-10-06 12:57:47 +02:00
mgag200 drm/mgag200: fix kernel hang in cursor code. 2015-11-19 13:20:01 +10:00
msm drm/msm: Remove local fbdev emulation Kconfig option 2015-10-22 15:46:36 -04:00
nouveau Merge branch 'linux-4.4' of git://anongit.freedesktop.org/git/nouveau/linux-2.6 into drm-fixes 2015-11-28 06:50:34 +10:00
omapdrm drm: Check crtc viewport correctly with rotated primary plane on atomic drivers 2015-10-19 11:00:47 +02:00
panel spi: Drop owner assignment from spi_drivers 2015-10-28 10:30:17 +09:00
qxl Merge tag 'topic/drm-misc-2015-10-19' of git://anongit.freedesktop.org/drm-intel into drm-next 2015-10-20 09:01:49 +10:00
r128 drm/irq: Use unsigned int pipe in public API 2015-10-06 12:57:47 +02:00
radeon drm/radeon: Fixup hw vblank counter/ts for new drm_update_vblank_count() (v2) 2015-12-04 13:11:41 -05:00
rcar-du drm: rcar-du: Add support for the R8A7794 DU 2015-10-21 18:52:38 +03:00
rockchip Revert "drm/rockchip: Convert the probe function to the generic drm_of_component_probe()" 2015-11-13 09:59:46 +10:00
savage
shmobile drm: Stop using drm_vblank_count() as the hw frame counter 2015-10-07 16:13:52 +02:00
sis include/uapi/drm/sis_drm.h: move sis_file_private to drivers/gpu/drm/sis/sis_drv.h 2015-10-16 11:27:46 +10:00
sti drm/sti: load HQVDP firmware the first time HQVDP's plane is used 2015-11-03 13:04:56 +01:00
tdfx
tegra Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux 2015-11-10 09:33:06 -08:00
tilcdc drm: Stop using drm_vblank_count() as the hw frame counter 2015-10-07 16:13:52 +02:00
ttm drm/ttm: Fix memory space allocation v2 2015-09-15 00:57:01 -07:00
udl
vc4 drm/vc4: Make sure that planes aren't scaled. 2015-11-17 12:27:03 -08:00
vgem drm/vgem: Drop vgem_drm_gem_mmap 2015-10-19 11:00:44 +02:00
via via_drm.h: move struct via_file_private definition to drivers/gpu/drm/via/via_drv.h 2015-10-16 11:27:49 +10:00
virtio Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux 2015-11-10 09:33:06 -08:00
vmwgfx Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux 2015-11-10 09:33:06 -08:00
ati_pcigart.c
drm_agpsupport.c drm: Remove __OS_HAS_AGP 2015-09-30 10:44:48 +02:00
drm_atomic_helper.c drm/atomic-helper: Check encoder/crtc constraints 2015-11-19 17:11:13 +02:00
drm_atomic.c drm/atomic: add a drm_atomic_clean_old_fb helper. 2015-11-17 13:02:14 +02:00
drm_auth.c
drm_bridge.c
drm_bufs.c drm: Remove __OS_HAS_AGP 2015-09-30 10:44:48 +02:00
drm_cache.c
drm_context.c
drm_crtc_helper.c
drm_crtc_internal.h
drm_crtc.c Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux 2015-11-10 09:33:06 -08:00
drm_debugfs.c
drm_dma.c
drm_dp_helper.c Linux 4.3-rc2 2015-09-24 17:18:41 +02:00
drm_dp_mst_topology.c drm: fix mutex leak in drm_dp_get_mst_branch_device 2015-10-22 08:29:08 +10:00
drm_drv.c drm: misc cleanup 2015-10-16 15:50:57 +02:00
drm_edid_load.c drm: Constify generic_edid_names[] 2015-09-08 13:45:52 +02:00
drm_edid.c drm/edid: Round to closest when computing the CEA/HDMI alternate clock 2015-10-20 12:01:29 +02:00
drm_encoder_slave.c
drm_fb_cma_helper.c
drm_fb_helper.c drm/core: Fix old_fb handling in pan_display_atomic. 2015-11-17 13:02:14 +02:00
drm_flip_work.c
drm_fops.c
drm_gem_cma_helper.c Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux 2015-11-10 09:33:06 -08:00
drm_gem.c Merge branch 'drm-next' of git://people.freedesktop.org/~airlied/linux 2015-11-10 09:33:06 -08:00
drm_global.c
drm_hashtab.c
drm_info.c
drm_internal.h drm: move drm_class into drm_sysfs.c 2015-09-09 15:05:17 +02:00
drm_ioc32.c drm: Remove __OS_HAS_AGP 2015-09-30 10:44:48 +02:00
drm_ioctl.c drm: Enforce unlocked ioctl operation for kms driver ioctls 2015-10-16 15:50:54 +02:00
drm_irq.c drm: Don't oops in drm_calc_timestamping_constants() if drm_vblank_init() wasn't called 2015-11-13 09:55:00 +10:00
drm_legacy.h
drm_lock.c signals: kill block_all_signals() and unblock_all_signals() 2015-11-06 17:50:42 -08:00
drm_memory.c drm: Remove __OS_HAS_AGP 2015-09-30 10:44:48 +02:00
drm_mipi_dsi.c
drm_mm.c drm/mm: Do DRM_MM_CREATE_TOP adj_start calculation after color_adjust 2015-09-08 13:45:47 +02:00
drm_modes.c
drm_modeset_lock.c drm/atomic: Make sure lock is held in trylock contexts. 2015-09-08 13:45:53 +02:00
drm_of.c drm: Introduce generic probe function for component based masters. 2015-10-20 12:01:29 +02:00
drm_panel.c
drm_pci.c drm/doc: Update docs about device instance setup 2015-09-30 10:06:06 +02:00
drm_plane_helper.c drm/atomic: Make prepare_fb/cleanup_fb only take state, v3. 2015-09-08 13:45:54 +02:00
drm_platform.c drm/doc: Update docs about device instance setup 2015-09-30 10:06:06 +02:00
drm_prime.c
drm_probe_helper.c drm: Add a non-locking version of drm_kms_helper_poll_enable(), v2 2015-09-30 16:04:08 +03:00
drm_rect.c drm: Use DRM_ROTATE_MASK and DRM_REFLECT_MASK 2015-10-05 15:32:51 +02:00
drm_scatter.c
drm_sysfs.c Merge commit '06d1ee32a4d25356a710b49d5e95dbdd68bdf505' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux into drm-next 2015-10-16 10:25:28 +10:00
drm_trace_points.c
drm_trace.h
drm_vm.c drm: Remove __OS_HAS_AGP 2015-09-30 10:44:48 +02:00
drm_vma_manager.c drm/gem: Use kref_get_unless_zero for the weak mmap references 2015-10-19 11:00:44 +02:00
Kconfig drm/vc4: Add KMS support for Raspberry Pi. 2015-10-21 10:33:12 +01:00
Makefile Merge tag 'drm-vc4-next-2015-10-21' of http://github.com/anholt/linux into drm-next 2015-10-22 10:31:17 +10:00