Skip to content

drm/vc4: BCM2712: interlaced vblank timestamps alternate by 2 lines in some modesets (FRCNT parity is not the field) #7757

Closed

Description

@ARFARAR

Describe the bug

For interlaced modes vc4_crtc_get_scanout_position() adds htotal / 2 to hpos when vc4_hvs_get_fifo_frame_count() is odd, "to correct for field offset in interlaced mode" (rpi-7.4.y d9a4bcd, vc4_crtc.c:155-161). On BCM2712 that count is SCALER6_DISPX_STATUS.FRCNT (vc4_hvs.c:828-829), and FRCNT is not reset by SCALER6_DISPX_CTRL0_RESET (vc6_hvs_init_channel, __vc6_hvs_stop_channel): it keeps counting across modesets. Its parity therefore has no fixed relation to the field, and per modeset the correction either cancels the one-line difference between the two fields or doubles it. The vblank timestamps of an interlaced mode then alternate by two line periods in some modesets and are steady in others.

Steps to reproduce the behaviour

Pi 5, HDMI-A-1, bare KMS, a sink that takes interlaced CEA modes (I added VICs 5, 20, 6, 21 to the monitor's EDID through edid_override).

  1. modetest -M vc4 -s <conn>@<crtc>:1920x1080i (and the other three modes), held.
  2. 620 consecutive DRM_IOCTL_WAIT_VBLANK (relative 1) replies; take the median of |dt(i) − dt(i+1)| over adjacent intervals.
  3. Repeat with a fresh modeset per mode. Three runs of the four modes (µs):
run 1080i60 1080i50 480i 576i
1 1 0 1 1
2 59 71 127 128
3 60 71 127 128

59 / 71 / 127 / 128 µs = 2 · htotal / clock of the four modes. The PV interrupt rate and the DRM vblank counter matched the field rate in every run; only the timestamps are affected.

Expected: a steady field period in every modeset.

Device (s)

Raspberry Pi 5

System

Raspberry Pi 5 Model B Rev 1.1 (BCM2712 D0), 16 GB, two HDMI monitors.

  • OS: Debian GNU/Linux 13 (trixie), image "Raspberry Pi reference 2026-04-21" (pi-gen 4ad56cc850fa60adcc7f07dc15879bc95cc1d281, stage4)
  • Firmware: vcgencmd version → 2026/10/30 20:55:54, version 42d1b6a9 (release) (embedded)
  • Kernel: Linux 7.4.0-rc2-v8-16k-rust+ #2 SMP PREEMPT aarch64 — built by me from rpi-7.4.y d9a4bcd (bcm2712 defconfig + CONFIG_RUST, 16K pages); drivers/gpu/drm/vc4 is unmodified in that build

Logs

run 1: V5:A=1.0µs V20:A=0.0µs V6:A=1.0µs V21:A=1.0µs
run 2: V5:A=59.0µs(n=621,pairs=619) V20:A=71.0µs(n=621,pairs=619) V6:A=127.0µs(n=621,pairs=619) V21:A=128.0µs(n=621,pairs=619)
run 3: V5:A=60.0µs(n=621,pairs=619) V20:A=71.0µs(n=621,pairs=619) V6:A=127.0µs(n=621,pairs=619) V21:A=128.0µs(n=621,pairs=619)

Additional context

What works here: take the field from SCALER6_DISPX_STATUS_OFIELD of the status word the function has already read, instead of FRCNT parity, on vc4->gen >= VC4_GEN_6_C:

		if (vc4->gen >= VC4_GEN_6_C)
			odd = !(val & SCALER6_DISPX_STATUS_OFIELD);
		else
			odd = vc4_hvs_get_fifo_frame_count(hvs, channel) % 2;

		if (odd)
			*hpos += mode->crtc_htotal / 2;

With that the same test reads 0–1 µs in 12 of 12 modesets on the same kernel. The polarity (half a line when OFIELD reads clear) is empirical: at the vblank interrupt the HVS seems to still report the field that has just ended.

Questions for someone with the HVS documentation: is FRCNT meant to survive CTRL0_RESET, and is this the right reading of OFIELD? Does the same apply to SCALER_DISPSTATX on the older HVS? (Not measured; the change above leaves it alone.)

I intend to send the patch to dri-devel for mainline as well (the code is the same there since 7687a12). The measurements were taken on my own Pi 5.

Activity

  1. popcornmix commented on Nov 16, 2026

    @popcornmix
    Collaborator
  2. 6by9 commented on Nov 16, 2026

    @6by9
    Contributor

    The docs describe FRCNT as

    A free-running 6-bit frame counter that increments each time an attempt is made to composite the first line of display X.

    That is consistent across all generations. There are no statements over when it gets reset.

    Seeing as we're wanting to know the field, using the FIELD register would make sense.
    It exists on GEN4 and GEN5 in SCALER_DISPSTAT[012] as bit 27.
    GEN6 you've already found as SCALER6_DISPX_STATUS_OFIELD.
    In all cases, 1=odd field, and 0=even field.

    Just a thought, on GEN4 or 5, if the output is to the VEC, then NTSC is odd-field first. Should that be handled in this fixup? AIUI HDMI is always even field first, so it doesn't matter there. GEN6 HVS only drives HDMI, so it's not an issue there.

    The existing vc4_hvs_get_fifo_frame_count function is also used by the downstream patches for handling stale dlist entries, so it'd be appreciated to avoid modifying that function.

  3. ARFARAR commented on Nov 16, 2026

    @ARFARAR
    Author

    Thanks. The change does not touch vc4_hvs_get_fifo_frame_count(); vc4_crtc_get_scanout_position() just stops calling it and takes the field from the status word it already reads.

    Extended as you suggest:

    • GEN4/5 use bit 27 of SCALER_DISPSTATX, GEN6 uses OFIELD.
    • Polarity: on GEN6 the half line has to be added when OFIELD reads 0 (measured). The diff assumes the same on GEN4/5.
    • VEC NTSC: yes, it needs handling. With odd_field_first vc4_crtc_config_pv() delays the even field instead of the odd one, so the correction is inverted when PV_VCONTROL_ODD_FIRST is set. That is one extra PV read per query; a flag stored at modeset would do as well.

    The only hardware I have for testing is a Raspberry Pi 5 16GB, Rev 1.1 (e04171). With this diff on rpi-7.4.y d9a4bcd the GEN6 path gives 1 us in 12 of 12 interlaced modesets (1080i60, 1080i50, 480i, 576i, three runs). The GEN4/5 and VEC paths are compiled only.

    Can someone with a Pi 4 and composite out check the GEN4/5 + VEC part?

    --- a/drivers/gpu/drm/vc4/vc4_crtc.c
    +++ b/drivers/gpu/drm/vc4/vc4_crtc.c
    @@ -153,10 +153,27 @@
     	*hpos = 0;
     
     	if (mode->flags & DRM_MODE_FLAG_INTERLACE) {
    +		bool odd_first, field;
    +
     		*vpos /= 2;
     
    -		/* Use hpos to correct for field offset in interlaced mode. */
    -		if (vc4_hvs_get_fifo_frame_count(hvs, channel) % 2)
    +		/*
    +		 * Use hpos to correct for field offset in interlaced mode.
    +		 *
    +		 * The frame count is free-running, so its parity does not
    +		 * follow the field. Take the field from the status word read
    +		 * above: at the vblank interrupt the HVS still reports the
    +		 * field that has just ended, and the half line belongs to
    +		 * the field the PV delays (the even one if odd is first).
    +		 */
    +		if (vc4->gen >= VC4_GEN_6_C)
    +			field = val & SCALER6_DISPX_STATUS_OFIELD;
    +		else
    +			field = val & SCALER_DISPSTATX_FIELD;
    +
    +		odd_first = CRTC_READ(PV_V_CONTROL) & PV_VCONTROL_ODD_FIRST;
    +
    +		if (field == odd_first)
     			*hpos += mode->crtc_htotal / 2;
     	}
     
    --- a/drivers/gpu/drm/vc4/vc4_regs.h
    +++ b/drivers/gpu/drm/vc4/vc4_regs.h
    @@ -421,6 +421,7 @@
     # define SCALER_DISPSTATX_MODE_EOF		3
     # define SCALER_DISPSTATX_FULL			BIT(29)
     # define SCALER_DISPSTATX_EMPTY			BIT(28)
    +# define SCALER_DISPSTATX_FIELD			BIT(27)
     # define SCALER_DISPSTATX_LINE_MASK		VC4_MASK(11, 0)
     # define SCALER_DISPSTATX_LINE_SHIFT		0
     
  4. popcornmix commented on Nov 16, 2026

    @popcornmix
    Collaborator

    @ARFARAR Thanks for the detailed report. I can reproduce this on Pi 5, Pi 4 and Pi 3B+. The frame counter is free running on all generations, and the earlier HVS has the same field bit (SCALER_DISPSTATX bit 27). Your inverted polarity is correct on all three, including Pi 4 composite NTSC (odd field first). #7763 applies your fix to all generations.

    Can you wait about 40 minutes for CI to complete, then run sudo rpi-update pulls/7763 and test if that fix works.
    Read the rpi-update caveats first.

  5. popcornmix commented on Nov 16, 2026

    @popcornmix
    Collaborator

    We overlapped - but claude came up with a very similar fix to your proposal.
    (I did test it on pi3, 4 and 5, hdmi and composite).

  6. ARFARAR commented on Nov 16, 2026

    @ARFARAR
    Author

    Tested the code of #7763 (a0b561d) on a Pi 5 16GB, Rev 1.1 (e04171). The board runs a self-built rpi-7.4.y d9a4bcd kernel, so instead of rpi-update I applied the PR's diff to that tree and built vc4 as a module.

    The vblank timestamp intervals are steady, 1 us, in 12 of 12 interlaced modesets (1080i60, 1080i50, 480i, 576i, three runs). The stock module was off by two lines in 8 of 12.

    The ODD_FIRST inversion in my diff above was a guess from the code; your composite NTSC result shows it is not needed.

    No surprise that Claude landed on a similar fix. This issue came out of my research project: with Claude's assistance I have rewritten the Pi 5 graphics and video drivers in Rust, as out-of-tree modules that replace vc4, v3d, bcm2712-iommu and rpi_hevc_dec (with thin C shims where the kernel has no Rust API yet).

  7. ARFARAR commented on Nov 16, 2026

    @ARFARAR
    Author

    Also tested the CI build itself: rpi-update pulls/7763 (a0b561d, 6.18.62-v8-16k+) on the same Pi 5, stock vc4 from that build. 0-1 us in 12 of 12 interlaced modesets (1080i60, 1080i50, 480i, 576i, three runs). The fix works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions