Skip to contentSkip to conversation

drm/mgag200: Implement VBLANK support

PATCH Superseded Thomas Zimmermann 2 participants 17 messages Jul 5, 2024 – Jul 17, 2024

Subsystems (3)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Thomas Zimmermann

author maintainer
#0 lore raw

v4 cover drm/mgag200: Implement VBLANK support

(was: Rate-limit shadow-FB-to-console-update to screen refresh)

Implement support for VBLANK events in mgag200.

Patches 1 to 5 prepare mgag200's modesetting code by renaming or adding variables for various hardware fields. This makes the code more readable and aligns it with the programming manuals for Matrox hardware.

Patch 6 implements support for VBLANK events. The patch has been reviewed before at [1]. That old patchset never found its way into the kernel, but the VBLANK support is still useful.

Patch 7 adds support for VBLANK timestamps.

v4:
- split off the patchset from an earlier series [1]

[1] https://patchwork.freedesktop.org/series/66442/

Thomas Zimmermann (7):
drm/mgag200: Use hexadecimal register indeces drm/mgag200: Align register field names with documentation drm/mgag200: Use adjusted mode values for CRTCs drm/mgag200: Add dedicated variables for blanking fields drm/mgag200: Add dedicted variable for <linecomp> field drm/mgag200: Add vblank support
drm/mgag200: Implement struct drm_crtc_funcs.get_vblank_timestamp

 drivers/gpu/drm/mgag200/mgag200_drv.c     |  47 ++++++
 drivers/gpu/drm/mgag200/mgag200_drv.h     |  14 +-
 drivers/gpu/drm/mgag200/mgag200_g200.c    |   5 +
 drivers/gpu/drm/mgag200/mgag200_g200eh.c  |   5 +
 drivers/gpu/drm/mgag200/mgag200_g200eh3.c |   5 +
 drivers/gpu/drm/mgag200/mgag200_g200er.c  |   5 +
 drivers/gpu/drm/mgag200/mgag200_g200ev.c  |   5 +
 drivers/gpu/drm/mgag200/mgag200_g200ew3.c |   5 +
 drivers/gpu/drm/mgag200/mgag200_g200se.c  |   5 +
 drivers/gpu/drm/mgag200/mgag200_g200wb.c  |   5 +
 drivers/gpu/drm/mgag200/mgag200_mode.c    | 169 ++++++++++++++++------
 drivers/gpu/drm/mgag200/mgag200_reg.h     |   7 +
 12 files changed, 232 insertions(+), 45 deletions(-)
Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH v4 1/7 drm/mgag200: Use hexadecimal register indeces

In mgag200_set_mode_regs(), use hexadecimal indices for accessing registers. Aligns the code with the register indices in the Matrox programming manuals. Also convert to lower-case hexadecimal values.

---
 drivers/gpu/drm/mgag200/mgag200_mode.c | 50 +++++++++++++-------------
 1 file changed, 25 insertions(+), 25 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +25 -25
index d4550e4b3b01..4ceaa33e8d42 100644
@@ -249,31 +249,31 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 		   ((vdisplay & 0x400) >> 3);
 	crtcext5 = 0x00;
 
-	WREG_CRT(0, htotal - 4);
-	WREG_CRT(1, hdisplay);
-	WREG_CRT(2, hdisplay);
-	WREG_CRT(3, (htotal & 0x1F) | 0x80);
-	WREG_CRT(4, hsyncstart);
-	WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
-	WREG_CRT(6, vtotal & 0xFF);
-	WREG_CRT(7, ((vtotal & 0x100) >> 8) |
-		 ((vdisplay & 0x100) >> 7) |
-		 ((vsyncstart & 0x100) >> 6) |
-		 ((vdisplay & 0x100) >> 5) |
-		 ((vdisplay & 0x100) >> 4) | /* linecomp */
-		 ((vtotal & 0x200) >> 4) |
-		 ((vdisplay & 0x200) >> 3) |
-		 ((vsyncstart & 0x200) >> 2));
-	WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
-		 ((vdisplay & 0x200) >> 3));
-	WREG_CRT(16, vsyncstart & 0xFF);
-	WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
-	WREG_CRT(18, vdisplay & 0xFF);
-	WREG_CRT(20, 0);
-	WREG_CRT(21, vdisplay & 0xFF);
-	WREG_CRT(22, (vtotal + 1) & 0xFF);
-	WREG_CRT(23, 0xc3);
-	WREG_CRT(24, vdisplay & 0xFF);
+	WREG_CRT(0x00, htotal - 4);
+	WREG_CRT(0x01, hdisplay);
+	WREG_CRT(0x02, hdisplay);
+	WREG_CRT(0x03, (htotal & 0x1f) | 0x80);
+	WREG_CRT(0x04, hsyncstart);
+	WREG_CRT(0x05, ((htotal & 0x20) << 2) | (hsyncend & 0x1f));
+	WREG_CRT(0x06, vtotal & 0xff);
+	WREG_CRT(0x07, ((vtotal & 0x100) >> 8) |
+		       ((vdisplay & 0x100) >> 7) |
+		       ((vsyncstart & 0x100) >> 6) |
+		       ((vdisplay & 0x100) >> 5) |
+		       ((vdisplay & 0x100) >> 4) | /* linecomp */
+		       ((vtotal & 0x200) >> 4) |
+		       ((vdisplay & 0x200) >> 3) |
+		       ((vsyncstart & 0x200) >> 2));
+	WREG_CRT(0x09, ((vdisplay & 0x200) >> 4) |
+		       ((vdisplay & 0x200) >> 3));
+	WREG_CRT(0x10, vsyncstart & 0xff);
+	WREG_CRT(0x11, (vsyncend & 0x0f) | 0x20);
+	WREG_CRT(0x12, vdisplay & 0xff);
+	WREG_CRT(0x14, 0);
+	WREG_CRT(0x15, vdisplay & 0xff);
+	WREG_CRT(0x16, (vtotal + 1) & 0xff);
+	WREG_CRT(0x17, 0xc3);
+	WREG_CRT(0x18, vdisplay & 0xff);
 
 	WREG_ECRT(0x01, crtcext1);
 	WREG_ECRT(0x02, crtcext2);
Signature · 1 line
2.45.2
reviewer Reviewed-by on v4 1/7
#2 lore raw

Re: [PATCH v4 1/7] drm/mgag200: Use hexadecimal register indeces

Quoted 4 lines · Thomas Zimmermann

In mgag200_set_mode_regs(), use hexadecimal indices for accessing registers. Aligns the code with the register indices in the Matrox programming manuals. Also convert to lower-case hexadecimal values.

Thanks, it looks good to me.

Quoted 66 lines
---
  drivers/gpu/drm/mgag200/mgag200_mode.c | 50 +++++++++++++-------------
  1 file changed, 25 insertions(+), 25 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +25 -25
index d4550e4b3b01..4ceaa33e8d42 100644
@@ -249,31 +249,31 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
  		   ((vdisplay & 0x400) >> 3);
  	crtcext5 = 0x00;
 
-	WREG_CRT(0, htotal - 4);
-	WREG_CRT(1, hdisplay);
-	WREG_CRT(2, hdisplay);
-	WREG_CRT(3, (htotal & 0x1F) | 0x80);
-	WREG_CRT(4, hsyncstart);
-	WREG_CRT(5, ((htotal & 0x20) << 2) | (hsyncend & 0x1F));
-	WREG_CRT(6, vtotal & 0xFF);
-	WREG_CRT(7, ((vtotal & 0x100) >> 8) |
-		 ((vdisplay & 0x100) >> 7) |
-		 ((vsyncstart & 0x100) >> 6) |
-		 ((vdisplay & 0x100) >> 5) |
-		 ((vdisplay & 0x100) >> 4) | /* linecomp */
-		 ((vtotal & 0x200) >> 4) |
-		 ((vdisplay & 0x200) >> 3) |
-		 ((vsyncstart & 0x200) >> 2));
-	WREG_CRT(9, ((vdisplay & 0x200) >> 4) |
-		 ((vdisplay & 0x200) >> 3));
-	WREG_CRT(16, vsyncstart & 0xFF);
-	WREG_CRT(17, (vsyncend & 0x0F) | 0x20);
-	WREG_CRT(18, vdisplay & 0xFF);
-	WREG_CRT(20, 0);
-	WREG_CRT(21, vdisplay & 0xFF);
-	WREG_CRT(22, (vtotal + 1) & 0xFF);
-	WREG_CRT(23, 0xc3);
-	WREG_CRT(24, vdisplay & 0xFF);
+	WREG_CRT(0x00, htotal - 4);
+	WREG_CRT(0x01, hdisplay);
+	WREG_CRT(0x02, hdisplay);
+	WREG_CRT(0x03, (htotal & 0x1f) | 0x80);
+	WREG_CRT(0x04, hsyncstart);
+	WREG_CRT(0x05, ((htotal & 0x20) << 2) | (hsyncend & 0x1f));
+	WREG_CRT(0x06, vtotal & 0xff);
+	WREG_CRT(0x07, ((vtotal & 0x100) >> 8) |
+		       ((vdisplay & 0x100) >> 7) |
+		       ((vsyncstart & 0x100) >> 6) |
+		       ((vdisplay & 0x100) >> 5) |
+		       ((vdisplay & 0x100) >> 4) | /* linecomp */
+		       ((vtotal & 0x200) >> 4) |
+		       ((vdisplay & 0x200) >> 3) |
+		       ((vsyncstart & 0x200) >> 2));
+	WREG_CRT(0x09, ((vdisplay & 0x200) >> 4) |
+		       ((vdisplay & 0x200) >> 3));
+	WREG_CRT(0x10, vsyncstart & 0xff);
+	WREG_CRT(0x11, (vsyncend & 0x0f) | 0x20);
+	WREG_CRT(0x12, vdisplay & 0xff);
+	WREG_CRT(0x14, 0);
+	WREG_CRT(0x15, vdisplay & 0xff);
+	WREG_CRT(0x16, (vtotal + 1) & 0xff);
+	WREG_CRT(0x17, 0xc3);
+	WREG_CRT(0x18, vdisplay & 0xff);
 
  	WREG_ECRT(0x01, crtcext1);
  	WREG_ECRT(0x02, crtcext2);
author maintainer
#3 lore raw

PATCH v4 2/7 drm/mgag200: Align register field names with documentation

In reply to Thomas Zimmermann

In mgag200_set_mode_regs(), align variable names with the field names given in the Matrox programming manuals. Makes the code and docs grep- able.

---
 drivers/gpu/drm/mgag200/mgag200_mode.c | 56 +++++++++++++-------------
 1 file changed, 28 insertions(+), 28 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +28 -28
index 4ceaa33e8d42..09e2a5b80403 100644
@@ -204,12 +204,12 @@ void mgag200_init_registers(struct mga_device *mdev)
 void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode)
 {
 	const struct mgag200_device_info *info = mdev->info;
-	unsigned int hdisplay, hsyncstart, hsyncend, htotal;
-	unsigned int vdisplay, vsyncstart, vsyncend, vtotal;
+	unsigned int hdispend, hsyncstr, hsyncend, htotal;
+	unsigned int vdispend, vsyncstr, vsyncend, vtotal;
 	u8 misc, crtcext1, crtcext2, crtcext5;
 
-	hdisplay = mode->hdisplay / 8 - 1;
-	hsyncstart = mode->hsync_start / 8 - 1;
+	hdispend = mode->hdisplay / 8 - 1;
+	hsyncstr = mode->hsync_start / 8 - 1;
 	hsyncend = mode->hsync_end / 8 - 1;
 	htotal = mode->htotal / 8 - 1;
 
@@ -217,8 +217,8 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
 		htotal++;
 
-	vdisplay = mode->vdisplay - 1;
-	vsyncstart = mode->vsync_start - 1;
+	vdispend = mode->vdisplay - 1;
+	vsyncstr = mode->vsync_start - 1;
 	vsyncend = mode->vsync_end - 1;
 	vtotal = mode->vtotal - 2;
 
@@ -235,45 +235,45 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 		misc &= ~MGAREG_MISC_VSYNCPOL;
 
 	crtcext1 = (((htotal - 4) & 0x100) >> 8) |
-		   ((hdisplay & 0x100) >> 7) |
-		   ((hsyncstart & 0x100) >> 6) |
-		    (htotal & 0x40);
+		    ((hdispend & 0x100) >> 7) |
+		    ((hsyncstr & 0x100) >> 6) |
+		     (htotal & 0x40);
 	if (info->has_vidrst)
 		crtcext1 |= MGAREG_CRTCEXT1_VRSTEN |
 			    MGAREG_CRTCEXT1_HRSTEN;
 
 	crtcext2 = ((vtotal & 0xc00) >> 10) |
-		   ((vdisplay & 0x400) >> 8) |
-		   ((vdisplay & 0xc00) >> 7) |
-		   ((vsyncstart & 0xc00) >> 5) |
-		   ((vdisplay & 0x400) >> 3);
+		   ((vdispend & 0x400) >> 8) |
+		   ((vdispend & 0xc00) >> 7) |
+		   ((vsyncstr & 0xc00) >> 5) |
+		   ((vdispend & 0x400) >> 3);
 	crtcext5 = 0x00;
 
 	WREG_CRT(0x00, htotal - 4);
-	WREG_CRT(0x01, hdisplay);
-	WREG_CRT(0x02, hdisplay);
+	WREG_CRT(0x01, hdispend);
+	WREG_CRT(0x02, hdispend);
 	WREG_CRT(0x03, (htotal & 0x1f) | 0x80);
-	WREG_CRT(0x04, hsyncstart);
+	WREG_CRT(0x04, hsyncstr);
 	WREG_CRT(0x05, ((htotal & 0x20) << 2) | (hsyncend & 0x1f));
 	WREG_CRT(0x06, vtotal & 0xff);
 	WREG_CRT(0x07, ((vtotal & 0x100) >> 8) |
-		       ((vdisplay & 0x100) >> 7) |
-		       ((vsyncstart & 0x100) >> 6) |
-		       ((vdisplay & 0x100) >> 5) |
-		       ((vdisplay & 0x100) >> 4) | /* linecomp */
+		       ((vdispend & 0x100) >> 7) |
+		       ((vsyncstr & 0x100) >> 6) |
+		       ((vdispend & 0x100) >> 5) |
+		       ((vdispend & 0x100) >> 4) | /* linecomp */
 		       ((vtotal & 0x200) >> 4) |
-		       ((vdisplay & 0x200) >> 3) |
-		       ((vsyncstart & 0x200) >> 2));
-	WREG_CRT(0x09, ((vdisplay & 0x200) >> 4) |
-		       ((vdisplay & 0x200) >> 3));
-	WREG_CRT(0x10, vsyncstart & 0xff);
+		       ((vdispend & 0x200) >> 3) |
+		       ((vsyncstr & 0x200) >> 2));
+	WREG_CRT(0x09, ((vdispend & 0x200) >> 4) |
+		       ((vdispend & 0x200) >> 3));
+	WREG_CRT(0x10, vsyncstr & 0xff);
 	WREG_CRT(0x11, (vsyncend & 0x0f) | 0x20);
-	WREG_CRT(0x12, vdisplay & 0xff);
+	WREG_CRT(0x12, vdispend & 0xff);
 	WREG_CRT(0x14, 0);
-	WREG_CRT(0x15, vdisplay & 0xff);
+	WREG_CRT(0x15, vdispend & 0xff);
 	WREG_CRT(0x16, (vtotal + 1) & 0xff);
 	WREG_CRT(0x17, 0xc3);
-	WREG_CRT(0x18, vdisplay & 0xff);
+	WREG_CRT(0x18, vdispend & 0xff);
 
 	WREG_ECRT(0x01, crtcext1);
 	WREG_ECRT(0x02, crtcext2);
Signature · 1 line
2.45.2
reviewer Reviewed-by on v4 2/7
#4 lore raw

Re: [PATCH v4 2/7] drm/mgag200: Align register field names with documentation

Quoted 3 lines · Thomas Zimmermann

In mgag200_set_mode_regs(), align variable names with the field names given in the Matrox programming manuals. Makes the code and docs grep- able.

I confirm it's better when the code matches the documentation.

Quoted 106 lines
---
  drivers/gpu/drm/mgag200/mgag200_mode.c | 56 +++++++++++++-------------
  1 file changed, 28 insertions(+), 28 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +28 -28
index 4ceaa33e8d42..09e2a5b80403 100644
@@ -204,12 +204,12 @@ void mgag200_init_registers(struct mga_device *mdev)
  void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode)
  {
  	const struct mgag200_device_info *info = mdev->info;
-	unsigned int hdisplay, hsyncstart, hsyncend, htotal;
-	unsigned int vdisplay, vsyncstart, vsyncend, vtotal;
+	unsigned int hdispend, hsyncstr, hsyncend, htotal;
+	unsigned int vdispend, vsyncstr, vsyncend, vtotal;
  	u8 misc, crtcext1, crtcext2, crtcext5;
 
-	hdisplay = mode->hdisplay / 8 - 1;
-	hsyncstart = mode->hsync_start / 8 - 1;
+	hdispend = mode->hdisplay / 8 - 1;
+	hsyncstr = mode->hsync_start / 8 - 1;
  	hsyncend = mode->hsync_end / 8 - 1;
  	htotal = mode->htotal / 8 - 1;
 
@@ -217,8 +217,8 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
  	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
  		htotal++;
 
-	vdisplay = mode->vdisplay - 1;
-	vsyncstart = mode->vsync_start - 1;
+	vdispend = mode->vdisplay - 1;
+	vsyncstr = mode->vsync_start - 1;
  	vsyncend = mode->vsync_end - 1;
  	vtotal = mode->vtotal - 2;
 
@@ -235,45 +235,45 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
  		misc &= ~MGAREG_MISC_VSYNCPOL;
 
  	crtcext1 = (((htotal - 4) & 0x100) >> 8) |
-		   ((hdisplay & 0x100) >> 7) |
-		   ((hsyncstart & 0x100) >> 6) |
-		    (htotal & 0x40);
+		    ((hdispend & 0x100) >> 7) |
+		    ((hsyncstr & 0x100) >> 6) |
+		     (htotal & 0x40);
  	if (info->has_vidrst)
  		crtcext1 |= MGAREG_CRTCEXT1_VRSTEN |
  			    MGAREG_CRTCEXT1_HRSTEN;
 
  	crtcext2 = ((vtotal & 0xc00) >> 10) |
-		   ((vdisplay & 0x400) >> 8) |
-		   ((vdisplay & 0xc00) >> 7) |
-		   ((vsyncstart & 0xc00) >> 5) |
-		   ((vdisplay & 0x400) >> 3);
+		   ((vdispend & 0x400) >> 8) |
+		   ((vdispend & 0xc00) >> 7) |
+		   ((vsyncstr & 0xc00) >> 5) |
+		   ((vdispend & 0x400) >> 3);
  	crtcext5 = 0x00;
 
  	WREG_CRT(0x00, htotal - 4);
-	WREG_CRT(0x01, hdisplay);
-	WREG_CRT(0x02, hdisplay);
+	WREG_CRT(0x01, hdispend);
+	WREG_CRT(0x02, hdispend);
  	WREG_CRT(0x03, (htotal & 0x1f) | 0x80);
-	WREG_CRT(0x04, hsyncstart);
+	WREG_CRT(0x04, hsyncstr);
  	WREG_CRT(0x05, ((htotal & 0x20) << 2) | (hsyncend & 0x1f));
  	WREG_CRT(0x06, vtotal & 0xff);
  	WREG_CRT(0x07, ((vtotal & 0x100) >> 8) |
-		       ((vdisplay & 0x100) >> 7) |
-		       ((vsyncstart & 0x100) >> 6) |
-		       ((vdisplay & 0x100) >> 5) |
-		       ((vdisplay & 0x100) >> 4) | /* linecomp */
+		       ((vdispend & 0x100) >> 7) |
+		       ((vsyncstr & 0x100) >> 6) |
+		       ((vdispend & 0x100) >> 5) |
+		       ((vdispend & 0x100) >> 4) | /* linecomp */
  		       ((vtotal & 0x200) >> 4) |
-		       ((vdisplay & 0x200) >> 3) |
-		       ((vsyncstart & 0x200) >> 2));
-	WREG_CRT(0x09, ((vdisplay & 0x200) >> 4) |
-		       ((vdisplay & 0x200) >> 3));
-	WREG_CRT(0x10, vsyncstart & 0xff);
+		       ((vdispend & 0x200) >> 3) |
+		       ((vsyncstr & 0x200) >> 2));
+	WREG_CRT(0x09, ((vdispend & 0x200) >> 4) |
+		       ((vdispend & 0x200) >> 3));
+	WREG_CRT(0x10, vsyncstr & 0xff);
  	WREG_CRT(0x11, (vsyncend & 0x0f) | 0x20);
-	WREG_CRT(0x12, vdisplay & 0xff);
+	WREG_CRT(0x12, vdispend & 0xff);
  	WREG_CRT(0x14, 0);
-	WREG_CRT(0x15, vdisplay & 0xff);
+	WREG_CRT(0x15, vdispend & 0xff);
  	WREG_CRT(0x16, (vtotal + 1) & 0xff);
  	WREG_CRT(0x17, 0xc3);
-	WREG_CRT(0x18, vdisplay & 0xff);
+	WREG_CRT(0x18, vdispend & 0xff);
 
  	WREG_ECRT(0x01, crtcext1);
  	WREG_ECRT(0x02, crtcext2);
author maintainer
#5 lore raw

PATCH v4 3/7 drm/mgag200: Use adjusted mode values for CRTCs

In reply to Thomas Zimmermann

Use the values with the crtc_ prefix from struct drm_display_mode to program hardware. The DRM core adjusted these values to the requirements of CRTC hardware.

---
 drivers/gpu/drm/mgag200/mgag200_mode.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +8 -8
index 09e2a5b80403..ccad5bd5960d 100644
@@ -208,19 +208,19 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 	unsigned int vdispend, vsyncstr, vsyncend, vtotal;
 	u8 misc, crtcext1, crtcext2, crtcext5;
 
-	hdispend = mode->hdisplay / 8 - 1;
-	hsyncstr = mode->hsync_start / 8 - 1;
-	hsyncend = mode->hsync_end / 8 - 1;
-	htotal = mode->htotal / 8 - 1;
+	hdispend = mode->crtc_hdisplay / 8 - 1;
+	hsyncstr = mode->crtc_hsync_start / 8 - 1;
+	hsyncend = mode->crtc_hsync_end / 8 - 1;
+	htotal = mode->crtc_htotal / 8 - 1;
 
 	/* Work around hardware quirk */
 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
 		htotal++;
 
-	vdispend = mode->vdisplay - 1;
-	vsyncstr = mode->vsync_start - 1;
-	vsyncend = mode->vsync_end - 1;
-	vtotal = mode->vtotal - 2;
+	vdispend = mode->crtc_vdisplay - 1;
+	vsyncstr = mode->crtc_vsync_start - 1;
+	vsyncend = mode->crtc_vsync_end - 1;
+	vtotal = mode->crtc_vtotal - 2;
 
 	misc = RREG8(MGA_MISC_IN);
 
Signature · 1 line
2.45.2
reviewer Reviewed-by on v4 3/7
#6 lore raw

Re: [PATCH v4 3/7] drm/mgag200: Use adjusted mode values for CRTCs

Quoted 3 lines · Thomas Zimmermann

Use the values with the crtc_ prefix from struct drm_display_mode to program hardware. The DRM core adjusted these values to the requirements of CRTC hardware.

Thanks, it looks good to me.

Quoted 38 lines
---
  drivers/gpu/drm/mgag200/mgag200_mode.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +8 -8
index 09e2a5b80403..ccad5bd5960d 100644
@@ -208,19 +208,19 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
  	unsigned int vdispend, vsyncstr, vsyncend, vtotal;
  	u8 misc, crtcext1, crtcext2, crtcext5;
 
-	hdispend = mode->hdisplay / 8 - 1;
-	hsyncstr = mode->hsync_start / 8 - 1;
-	hsyncend = mode->hsync_end / 8 - 1;
-	htotal = mode->htotal / 8 - 1;
+	hdispend = mode->crtc_hdisplay / 8 - 1;
+	hsyncstr = mode->crtc_hsync_start / 8 - 1;
+	hsyncend = mode->crtc_hsync_end / 8 - 1;
+	htotal = mode->crtc_htotal / 8 - 1;
 
  	/* Work around hardware quirk */
  	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
  		htotal++;
 
-	vdispend = mode->vdisplay - 1;
-	vsyncstr = mode->vsync_start - 1;
-	vsyncend = mode->vsync_end - 1;
-	vtotal = mode->vtotal - 2;
+	vdispend = mode->crtc_vdisplay - 1;
+	vsyncstr = mode->crtc_vsync_start - 1;
+	vsyncend = mode->crtc_vsync_end - 1;
+	vtotal = mode->crtc_vtotal - 2;
 
  	misc = RREG8(MGA_MISC_IN);
 
author maintainer
#7 lore raw

PATCH v4 4/7 drm/mgag200: Add dedicated variables for blanking fields

In reply to Thomas Zimmermann

Represent fields for horizontal and vertical blanking with <hblkstr>, <hblkend>, <vblkstr> and <vblkend>. Aligns the code with the Matrox programming manuals.

---
 drivers/gpu/drm/mgag200/mgag200_mode.c | 29 ++++++++++++++------------
 1 file changed, 16 insertions(+), 13 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +16 -13
index ccad5bd5960d..1cd28e7bea32 100644
@@ -204,23 +204,26 @@ void mgag200_init_registers(struct mga_device *mdev)
 void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode)
 {
 	const struct mgag200_device_info *info = mdev->info;
-	unsigned int hdispend, hsyncstr, hsyncend, htotal;
-	unsigned int vdispend, vsyncstr, vsyncend, vtotal;
+	unsigned int hdispend, hsyncstr, hsyncend, htotal, hblkstr, hblkend;
+	unsigned int vdispend, vsyncstr, vsyncend, vtotal, vblkstr, vblkend;
 	u8 misc, crtcext1, crtcext2, crtcext5;
 
 	hdispend = mode->crtc_hdisplay / 8 - 1;
 	hsyncstr = mode->crtc_hsync_start / 8 - 1;
 	hsyncend = mode->crtc_hsync_end / 8 - 1;
 	htotal = mode->crtc_htotal / 8 - 1;
-
 	/* Work around hardware quirk */
 	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
 		htotal++;
+	hblkstr = mode->crtc_hblank_start / 8 - 1;
+	hblkend = htotal;
 
 	vdispend = mode->crtc_vdisplay - 1;
 	vsyncstr = mode->crtc_vsync_start - 1;
 	vsyncend = mode->crtc_vsync_end - 1;
 	vtotal = mode->crtc_vtotal - 2;
+	vblkstr = mode->crtc_vblank_start;
+	vblkend = vtotal + 1;
 
 	misc = RREG8(MGA_MISC_IN);
 
@@ -235,43 +238,43 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 		misc &= ~MGAREG_MISC_VSYNCPOL;
 
 	crtcext1 = (((htotal - 4) & 0x100) >> 8) |
-		    ((hdispend & 0x100) >> 7) |
+		    ((hblkstr & 0x100) >> 7) |
 		    ((hsyncstr & 0x100) >> 6) |
-		     (htotal & 0x40);
+		     (hblkend & 0x40);
 	if (info->has_vidrst)
 		crtcext1 |= MGAREG_CRTCEXT1_VRSTEN |
 			    MGAREG_CRTCEXT1_HRSTEN;
 
 	crtcext2 = ((vtotal & 0xc00) >> 10) |
 		   ((vdispend & 0x400) >> 8) |
-		   ((vdispend & 0xc00) >> 7) |
+		   ((vblkstr & 0xc00) >> 7) |
 		   ((vsyncstr & 0xc00) >> 5) |
 		   ((vdispend & 0x400) >> 3);
 	crtcext5 = 0x00;
 
 	WREG_CRT(0x00, htotal - 4);
 	WREG_CRT(0x01, hdispend);
-	WREG_CRT(0x02, hdispend);
-	WREG_CRT(0x03, (htotal & 0x1f) | 0x80);
+	WREG_CRT(0x02, hblkstr);
+	WREG_CRT(0x03, (hblkend & 0x1f) | 0x80);
 	WREG_CRT(0x04, hsyncstr);
-	WREG_CRT(0x05, ((htotal & 0x20) << 2) | (hsyncend & 0x1f));
+	WREG_CRT(0x05, ((hblkend & 0x20) << 2) | (hsyncend & 0x1f));
 	WREG_CRT(0x06, vtotal & 0xff);
 	WREG_CRT(0x07, ((vtotal & 0x100) >> 8) |
 		       ((vdispend & 0x100) >> 7) |
 		       ((vsyncstr & 0x100) >> 6) |
-		       ((vdispend & 0x100) >> 5) |
+		       ((vblkstr & 0x100) >> 5) |
 		       ((vdispend & 0x100) >> 4) | /* linecomp */
 		       ((vtotal & 0x200) >> 4) |
 		       ((vdispend & 0x200) >> 3) |
 		       ((vsyncstr & 0x200) >> 2));
-	WREG_CRT(0x09, ((vdispend & 0x200) >> 4) |
+	WREG_CRT(0x09, ((vblkstr & 0x200) >> 4) |
 		       ((vdispend & 0x200) >> 3));
 	WREG_CRT(0x10, vsyncstr & 0xff);
 	WREG_CRT(0x11, (vsyncend & 0x0f) | 0x20);
 	WREG_CRT(0x12, vdispend & 0xff);
 	WREG_CRT(0x14, 0);
-	WREG_CRT(0x15, vdispend & 0xff);
-	WREG_CRT(0x16, (vtotal + 1) & 0xff);
+	WREG_CRT(0x15, vblkstr & 0xff);
+	WREG_CRT(0x16, vblkend & 0xff);
 	WREG_CRT(0x17, 0xc3);
 	WREG_CRT(0x18, vdispend & 0xff);
 
Signature · 1 line
2.45.2
reviewer Reviewed-by on v4 4/7
#8 lore raw

Re: [PATCH v4 4/7] drm/mgag200: Add dedicated variables for blanking fields

Quoted 3 lines · Thomas Zimmermann

Represent fields for horizontal and vertical blanking with <hblkstr>, <hblkend>, <vblkstr> and <vblkend>. Aligns the code with the Matrox programming manuals.

Thanks, it looks good to me.

Quoted 94 lines
---
  drivers/gpu/drm/mgag200/mgag200_mode.c | 29 ++++++++++++++------------
  1 file changed, 16 insertions(+), 13 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +16 -13
index ccad5bd5960d..1cd28e7bea32 100644
@@ -204,23 +204,26 @@ void mgag200_init_registers(struct mga_device *mdev)
  void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode)
  {
  	const struct mgag200_device_info *info = mdev->info;
-	unsigned int hdispend, hsyncstr, hsyncend, htotal;
-	unsigned int vdispend, vsyncstr, vsyncend, vtotal;
+	unsigned int hdispend, hsyncstr, hsyncend, htotal, hblkstr, hblkend;
+	unsigned int vdispend, vsyncstr, vsyncend, vtotal, vblkstr, vblkend;
  	u8 misc, crtcext1, crtcext2, crtcext5;
 
  	hdispend = mode->crtc_hdisplay / 8 - 1;
  	hsyncstr = mode->crtc_hsync_start / 8 - 1;
  	hsyncend = mode->crtc_hsync_end / 8 - 1;
  	htotal = mode->crtc_htotal / 8 - 1;
-
  	/* Work around hardware quirk */
  	if ((htotal & 0x07) == 0x06 || (htotal & 0x07) == 0x04)
  		htotal++;
+	hblkstr = mode->crtc_hblank_start / 8 - 1;
+	hblkend = htotal;
 
  	vdispend = mode->crtc_vdisplay - 1;
  	vsyncstr = mode->crtc_vsync_start - 1;
  	vsyncend = mode->crtc_vsync_end - 1;
  	vtotal = mode->crtc_vtotal - 2;
+	vblkstr = mode->crtc_vblank_start;
+	vblkend = vtotal + 1;
 
  	misc = RREG8(MGA_MISC_IN);
 
@@ -235,43 +238,43 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
  		misc &= ~MGAREG_MISC_VSYNCPOL;
 
  	crtcext1 = (((htotal - 4) & 0x100) >> 8) |
-		    ((hdispend & 0x100) >> 7) |
+		    ((hblkstr & 0x100) >> 7) |
  		    ((hsyncstr & 0x100) >> 6) |
-		     (htotal & 0x40);
+		     (hblkend & 0x40);
  	if (info->has_vidrst)
  		crtcext1 |= MGAREG_CRTCEXT1_VRSTEN |
  			    MGAREG_CRTCEXT1_HRSTEN;
 
  	crtcext2 = ((vtotal & 0xc00) >> 10) |
  		   ((vdispend & 0x400) >> 8) |
-		   ((vdispend & 0xc00) >> 7) |
+		   ((vblkstr & 0xc00) >> 7) |
  		   ((vsyncstr & 0xc00) >> 5) |
  		   ((vdispend & 0x400) >> 3);
  	crtcext5 = 0x00;
 
  	WREG_CRT(0x00, htotal - 4);
  	WREG_CRT(0x01, hdispend);
-	WREG_CRT(0x02, hdispend);
-	WREG_CRT(0x03, (htotal & 0x1f) | 0x80);
+	WREG_CRT(0x02, hblkstr);
+	WREG_CRT(0x03, (hblkend & 0x1f) | 0x80);
  	WREG_CRT(0x04, hsyncstr);
-	WREG_CRT(0x05, ((htotal & 0x20) << 2) | (hsyncend & 0x1f));
+	WREG_CRT(0x05, ((hblkend & 0x20) << 2) | (hsyncend & 0x1f));
  	WREG_CRT(0x06, vtotal & 0xff);
  	WREG_CRT(0x07, ((vtotal & 0x100) >> 8) |
  		       ((vdispend & 0x100) >> 7) |
  		       ((vsyncstr & 0x100) >> 6) |
-		       ((vdispend & 0x100) >> 5) |
+		       ((vblkstr & 0x100) >> 5) |
  		       ((vdispend & 0x100) >> 4) | /* linecomp */
  		       ((vtotal & 0x200) >> 4) |
  		       ((vdispend & 0x200) >> 3) |
  		       ((vsyncstr & 0x200) >> 2));
-	WREG_CRT(0x09, ((vdispend & 0x200) >> 4) |
+	WREG_CRT(0x09, ((vblkstr & 0x200) >> 4) |
  		       ((vdispend & 0x200) >> 3));
  	WREG_CRT(0x10, vsyncstr & 0xff);
  	WREG_CRT(0x11, (vsyncend & 0x0f) | 0x20);
  	WREG_CRT(0x12, vdispend & 0xff);
  	WREG_CRT(0x14, 0);
-	WREG_CRT(0x15, vdispend & 0xff);
-	WREG_CRT(0x16, (vtotal + 1) & 0xff);
+	WREG_CRT(0x15, vblkstr & 0xff);
+	WREG_CRT(0x16, vblkend & 0xff);
  	WREG_CRT(0x17, 0xc3);
  	WREG_CRT(0x18, vdispend & 0xff);
 
author maintainer
#9 lore raw

PATCH v4 5/7 drm/mgag200: Add dedicted variable for <linecomp> field

In reply to Thomas Zimmermann

Replace the line-compare value of <vdispend> with a dedicated variable <linecomp>. Improves readability and prepares for vblank support.

---
 drivers/gpu/drm/mgag200/mgag200_mode.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/gpu/drm/mgag200/mgag200_mode.c +7 -4
index 1cd28e7bea32..da2cbe81d4e6 100644
@@ -206,6 +206,7 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 	const struct mgag200_device_info *info = mdev->info;
 	unsigned int hdispend, hsyncstr, hsyncend, htotal, hblkstr, hblkend;
 	unsigned int vdispend, vsyncstr, vsyncend, vtotal, vblkstr, vblkend;
+	unsigned int linecomp;
 	u8 misc, crtcext1, crtcext2, crtcext5;
 
 	hdispend = mode->crtc_hdisplay / 8 - 1;
@@ -225,6 +226,8 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 	vblkstr = mode->crtc_vblank_start;
 	vblkend = vtotal + 1;
 
+	linecomp = vdispend;
+
 	misc = RREG8(MGA_MISC_IN);
 
 	if (mode->flags & DRM_MODE_FLAG_NHSYNC)
@@ -249,7 +252,7 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 		   ((vdispend & 0x400) >> 8) |
 		   ((vblkstr & 0xc00) >> 7) |
 		   ((vsyncstr & 0xc00) >> 5) |
-		   ((vdispend & 0x400) >> 3);
+		   ((linecomp & 0x400) >> 3);
 	crtcext5 = 0x00;
 
 	WREG_CRT(0x00, htotal - 4);
@@ -263,12 +266,12 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 		       ((vdispend & 0x100) >> 7) |
 		       ((vsyncstr & 0x100) >> 6) |
 		       ((vblkstr & 0x100) >> 5) |
-		       ((vdispend & 0x100) >> 4) | /* linecomp */
+		       ((linecomp & 0x100) >> 4) |
 		       ((vtotal & 0x200) >> 4) |
 		       ((vdispend & 0x200) >> 3) |
 		       ((vsyncstr & 0x200) >> 2));
 	WREG_CRT(0x09, ((vblkstr & 0x200) >> 4) |
-		       ((vdispend & 0x200) >> 3));
+		       ((linecomp & 0x200) >> 3));
 	WREG_CRT(0x10, vsyncstr & 0xff);
 	WREG_CRT(0x11, (vsyncend & 0x0f) | 0x20);
 	WREG_CRT(0x12, vdispend & 0xff);
@@ -276,7 +279,7 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
 	WREG_CRT(0x15, vblkstr & 0xff);
 	WREG_CRT(0x16, vblkend & 0xff);
 	WREG_CRT(0x17, 0xc3);
-	WREG_CRT(0x18, vdispend & 0xff);
+	WREG_CRT(0x18, linecomp & 0xff);
 
 	WREG_ECRT(0x01, crtcext1);
 	WREG_ECRT(0x02, crtcext2);
Signature · 1 line
2.45.2
author maintainer
#10 lore raw

PATCH v4 6/7 drm/mgag200: Add vblank support

In reply to Thomas Zimmermann

There's no VBLANK interrupt on Matrox chipsets. The workaround that is being used here and in other free Matrox drivers is to program <linecomp> to the value of <vblkstr> and enable the VLINE interrupt. This triggers an interrupt at the time when VBLANK begins.

VLINE uses separate registers for enabling and clearing pending interrupts. No extra synchronization between irq handler and the rest of the driver is required.

v4:
- recreate patch on latest upstream
- use devm_request_irq() for managed cleanup
- fail if vblanking cannot be initialized
- rename register constants (Sam, Emil)
- clear interrupt before registering handler (Ville)
- move <linecomp> programming into separate commit
- set <linecomp> to <vblkstr>
- fix typo in commit message

v3:
- set <linecomp> to <vdisplay> + 1 to trigger at VBLANK
- expand comment on linecomp

v2:
- only signal vblank on CRTC 0
- use constants for registers and fields
- set VLINECLR before enabling interrupt
- test against STATUS and IEN in irq handler
- coding-style fixes

---
 drivers/gpu/drm/mgag200/mgag200_drv.c     | 47 ++++++++++++++++++++
 drivers/gpu/drm/mgag200/mgag200_drv.h     |  6 ++-
 drivers/gpu/drm/mgag200/mgag200_g200.c    |  5 +++
 drivers/gpu/drm/mgag200/mgag200_g200eh.c  |  5 +++
 drivers/gpu/drm/mgag200/mgag200_g200eh3.c |  5 +++
 drivers/gpu/drm/mgag200/mgag200_g200er.c  |  5 +++
 drivers/gpu/drm/mgag200/mgag200_g200ev.c  |  5 +++
 drivers/gpu/drm/mgag200/mgag200_g200ew3.c |  5 +++
 drivers/gpu/drm/mgag200/mgag200_g200se.c  |  5 +++
 drivers/gpu/drm/mgag200/mgag200_g200wb.c  |  5 +++
 drivers/gpu/drm/mgag200/mgag200_mode.c    | 54 ++++++++++++++++++++++-
 drivers/gpu/drm/mgag200/mgag200_reg.h     |  7 +++
 12 files changed, 152 insertions(+), 2 deletions(-)
drivers/gpu/drm/mgag200/mgag200_drv.c +47 -0
index 62080cf0f2da..62479de9e659 100644
@@ -18,6 +18,7 @@
 #include <drm/drm_managed.h>
 #include <drm/drm_module.h>
 #include <drm/drm_pciids.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -84,6 +85,35 @@ resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size)
 	return offset - 65536;
 }
 
+static irqreturn_t mgag200_irq_handler(int irq, void *arg)
+{
+	struct drm_device *dev = arg;
+	struct mga_device *mdev = to_mga_device(dev);
+	struct drm_crtc *crtc;
+	u32 status, ien, iclear;
+
+	status = RREG32(MGAREG_STATUS);
+
+	if (status & MGAREG_STATUS_VLINEPEN) {
+		ien = RREG32(MGAREG_IEN);
+		if (!(ien & MGAREG_IEN_VLINEIEN))
+			goto out;
+
+		crtc = drm_crtc_from_index(dev, 0);
+		if (WARN_ON_ONCE(!crtc))
+			goto out;
+		drm_crtc_handle_vblank(crtc);
+
+		iclear = RREG32(MGAREG_ICLEAR);
+		iclear |= MGAREG_ICLEAR_VLINEICLR;
+		WREG32(MGAREG_ICLEAR, iclear);
+		return IRQ_HANDLED;
+	}
+
+out:
+	return IRQ_NONE;
+}
+
 /*
  * DRM driver
  */
@@ -167,7 +197,9 @@ int mgag200_device_init(struct mga_device *mdev,
 			const struct mgag200_device_funcs *funcs)
 {
 	struct drm_device *dev = &mdev->base;
+	struct pci_dev *pdev = to_pci_dev(dev->dev);
 	u8 crtcext3, misc;
+	u32 ien, iclear;
 	int ret;
 
 	mdev->info = info;
@@ -192,6 +224,21 @@ int mgag200_device_init(struct mga_device *mdev,
 
 	mutex_unlock(&mdev->rmmio_lock);
 
+	ien = RREG32(MGAREG_IEN);
+	ien &= ~(MGAREG_IEN_VLINEIEN);
+	WREG32(MGAREG_IEN, ien);
+
+	iclear = RREG32(MGAREG_ICLEAR);
+	iclear |= MGAREG_ICLEAR_VLINEICLR;
+	WREG32(MGAREG_ICLEAR, iclear);
+
+	ret = devm_request_irq(&pdev->dev, pdev->irq, mgag200_irq_handler, IRQF_SHARED,
+			       dev->driver->name, dev);
+	if (ret) {
+		drm_err(dev, "Failed to acquire interrupt, error %d\n", ret);
+		return ret;
+	}
+
 	return 0;
 }
 
drivers/gpu/drm/mgag200/mgag200_drv.h +5 -1
index 7f7dfbd0f013..f7b22b195016 100644
@@ -421,6 +421,8 @@ void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic
 void mgag200_crtc_reset(struct drm_crtc *crtc);
 struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc);
 void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *crtc_state);
+int mgag200_crtc_enable_vblank(struct drm_crtc *crtc);
+void mgag200_crtc_disable_vblank(struct drm_crtc *crtc);
 
 #define MGAG200_CRTC_FUNCS \
 	.reset = mgag200_crtc_reset, \
@@ -428,7 +430,9 @@ void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_st
 	.set_config = drm_atomic_helper_set_config, \
 	.page_flip = drm_atomic_helper_page_flip, \
 	.atomic_duplicate_state = mgag200_crtc_atomic_duplicate_state, \
-	.atomic_destroy_state = mgag200_crtc_atomic_destroy_state
+	.atomic_destroy_state = mgag200_crtc_atomic_destroy_state, \
+	.enable_vblank = mgag200_crtc_enable_vblank, \
+	.disable_vblank = mgag200_crtc_disable_vblank
 
 void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode);
 void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format);
drivers/gpu/drm/mgag200/mgag200_g200.c +5 -0
index f874e2949840..77ce8d36cef0 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -403,5 +404,9 @@ struct mga_device *mgag200_g200_device_create(struct pci_dev *pdev, const struct
 	drm_mode_config_reset(dev);
 	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return mdev;
 }
drivers/gpu/drm/mgag200/mgag200_g200eh.c +5 -0
index 52bf49ead5c5..72bd8e3421c2 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -279,5 +280,9 @@ struct mga_device *mgag200_g200eh_device_create(struct pci_dev *pdev, const stru
 	drm_mode_config_reset(dev);
 	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return mdev;
 }
drivers/gpu/drm/mgag200/mgag200_g200eh3.c +5 -0
index e7f89b2a59fd..1bbb0745b84a 100644
@@ -7,6 +7,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -184,5 +185,9 @@ struct mga_device *mgag200_g200eh3_device_create(struct pci_dev *pdev,
 	drm_mode_config_reset(dev);
 	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return mdev;
 }
drivers/gpu/drm/mgag200/mgag200_g200er.c +5 -0
index 4e8a1756138d..3350baf08a45 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -318,5 +319,9 @@ struct mga_device *mgag200_g200er_device_create(struct pci_dev *pdev, const stru
 	drm_mode_config_reset(dev);
 	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return mdev;
 }
drivers/gpu/drm/mgag200/mgag200_g200ev.c +5 -0
index d884f3cb0ec7..88d8bcd6fe51 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -323,5 +324,9 @@ struct mga_device *mgag200_g200ev_device_create(struct pci_dev *pdev, const stru
 	drm_mode_config_reset(dev);
 	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return mdev;
 }
drivers/gpu/drm/mgag200/mgag200_g200ew3.c +5 -0
index 839401e8b465..9d08180f7612 100644
@@ -7,6 +7,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -204,5 +205,9 @@ struct mga_device *mgag200_g200ew3_device_create(struct pci_dev *pdev,
 	drm_mode_config_reset(dev);
 	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return mdev;
 }
drivers/gpu/drm/mgag200/mgag200_g200se.c +5 -0
index a824bb8ad579..fcc8075627ef 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -523,5 +524,9 @@ struct mga_device *mgag200_g200se_device_create(struct pci_dev *pdev, const stru
 	drm_mode_config_reset(dev);
 	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return mdev;
 }
drivers/gpu/drm/mgag200/mgag200_g200wb.c +5 -0
index 835df0f4fc13..4f8ef3465b9f 100644
@@ -8,6 +8,7 @@
 #include <drm/drm_drv.h>
 #include <drm/drm_gem_atomic_helper.h>
 #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
 #include "mgag200_drv.h"
 
@@ -328,5 +329,9 @@ struct mga_device *mgag200_g200wb_device_create(struct pci_dev *pdev, const stru
 	drm_mode_config_reset(dev);
 	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
 	return mdev;
 }
drivers/gpu/drm/mgag200/mgag200_mode.c +53 -1

96 lines not on this page · raw message

drivers/gpu/drm/mgag200/mgag200_reg.h +7 -0
index aa73463674e4..d4fef8f25871 100644
@@ -102,10 +102,17 @@
 #define MGAREG_EXEC		0x0100
 
 #define	MGAREG_FIFOSTATUS	0x1e10
+
 #define	MGAREG_STATUS		0x1e14
+#define MGAREG_STATUS_VLINEPEN	BIT(5)
+
 #define MGAREG_CACHEFLUSH       0x1fff
+
 #define	MGAREG_ICLEAR		0x1e18
+#define MGAREG_ICLEAR_VLINEICLR	BIT(5)
+
 #define	MGAREG_IEN		0x1e1c
+#define MGAREG_IEN_VLINEIEN	BIT(5)
 
 #define	MGAREG_VCOUNT		0x1e20
 
Signature · 1 line
2.45.2
#11 lore raw

Re: [PATCH v4 6/7] drm/mgag200: Add vblank support

Quoted 8 lines · Thomas Zimmermann

There's no VBLANK interrupt on Matrox chipsets. The workaround that is being used here and in other free Matrox drivers is to program <linecomp> to the value of <vblkstr> and enable the VLINE interrupt. This triggers an interrupt at the time when VBLANK begins.

VLINE uses separate registers for enabling and clearing pending interrupts. No extra synchronization between irq handler and the rest of the driver is required.

Thanks for this patch, I have a few comments below:

Quoted 60 lines

v4:
- recreate patch on latest upstream
- use devm_request_irq() for managed cleanup
- fail if vblanking cannot be initialized
- rename register constants (Sam, Emil)
- clear interrupt before registering handler (Ville)
- move <linecomp> programming into separate commit
- set <linecomp> to <vblkstr>
- fix typo in commit message

v3:
- set <linecomp> to <vdisplay> + 1 to trigger at VBLANK
- expand comment on linecomp

v2:
- only signal vblank on CRTC 0
- use constants for registers and fields
- set VLINECLR before enabling interrupt
- test against STATUS and IEN in irq handler
- coding-style fixes

---
  drivers/gpu/drm/mgag200/mgag200_drv.c     | 47 ++++++++++++++++++++
  drivers/gpu/drm/mgag200/mgag200_drv.h     |  6 ++-
  drivers/gpu/drm/mgag200/mgag200_g200.c    |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200eh.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200eh3.c |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200er.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200ev.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200ew3.c |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200se.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200wb.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_mode.c    | 54 ++++++++++++++++++++++-
  drivers/gpu/drm/mgag200/mgag200_reg.h     |  7 +++
  12 files changed, 152 insertions(+), 2 deletions(-)
drivers/gpu/drm/mgag200/mgag200_drv.c +5 -0
index 62080cf0f2da..62479de9e659 100644
@@ -18,6 +18,7 @@
  #include <drm/drm_managed.h>
  #include <drm/drm_module.h>
  #include <drm/drm_pciids.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -84,6 +85,35 @@ resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size)
  	return offset - 65536;
  }
 
+static irqreturn_t mgag200_irq_handler(int irq, void *arg)
+{
+	struct drm_device *dev = arg;
+	struct mga_device *mdev = to_mga_device(dev);

drivers/gpu/drm/mgag200/mgag200_drv.c resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size) · line 101

Quoted 10 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +10 -0
+	struct drm_crtc *crtc;
+	u32 status, ien, iclear;
+
+	status = RREG32(MGAREG_STATUS);
+
+	if (status & MGAREG_STATUS_VLINEPEN) {
+		ien = RREG32(MGAREG_IEN);
+		if (!(ien & MGAREG_IEN_VLINEIEN))
+			goto out;
+

This is to catch a race condition, receiving the interrupt, just after disabling it ? I think you still want to clear it, to avoid having it fire as soon as you re-enable it ?

drivers/gpu/drm/mgag200/mgag200_drv.c resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size) · line 107

Quoted 6 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +6 -0
+		crtc = drm_crtc_from_index(dev, 0);
+		if (WARN_ON_ONCE(!crtc))
+			goto out;
+		drm_crtc_handle_vblank(crtc);
+
+		iclear = RREG32(MGAREG_ICLEAR);

iclear is a Write-Only register, according to the documentation. So reading it will always return 0.

I would suggest to just write:

WREG32(MGAREG_ICLEAR, MGAREG_ICLEAR_VLINEICLR);

Quoted 23 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +11 -0
+		iclear |= MGAREG_ICLEAR_VLINEICLR;
+		WREG32(MGAREG_ICLEAR, iclear);
+		return IRQ_HANDLED;
+	}
+
+out:
+	return IRQ_NONE;
+}
+
  /*
   * DRM driver
   */
@@ -167,7 +197,9 @@ int mgag200_device_init(struct mga_device *mdev,
  			const struct mgag200_device_funcs *funcs)
  {
  	struct drm_device *dev = &mdev->base;
+	struct pci_dev *pdev = to_pci_dev(dev->dev);
  	u8 crtcext3, misc;
+	u32 ien, iclear;
  	int ret;
 
  	mdev->info = info;

drivers/gpu/drm/mgag200/mgag200_drv.c int mgag200_device_init(struct mga_device *mdev, · line 229

Quoted 6 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +3 -0
@@ -192,6 +224,21 @@ int mgag200_device_init(struct mga_device *mdev,
 
  	mutex_unlock(&mdev->rmmio_lock);
 
+	ien = RREG32(MGAREG_IEN);
+	ien &= ~(MGAREG_IEN_VLINEIEN);
+	WREG32(MGAREG_IEN, ien);

I would suggest to disable all interrupt instead,
WREG32(MGAREG_IEN, 0);

drivers/gpu/drm/mgag200/mgag200_drv.c int mgag200_device_init(struct mga_device *mdev, · line 231

Quoted 2 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +2 -0
+
+	iclear = RREG32(MGAREG_ICLEAR);

same here, don't read the iclear register.

Quoted 289 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +10 -0
+	iclear |= MGAREG_ICLEAR_VLINEICLR;
+	WREG32(MGAREG_ICLEAR, iclear);
+
+	ret = devm_request_irq(&pdev->dev, pdev->irq, mgag200_irq_handler, IRQF_SHARED,
+			       dev->driver->name, dev);
+	if (ret) {
+		drm_err(dev, "Failed to acquire interrupt, error %d\n", ret);
+		return ret;
+	}
+
  	return 0;
  }
 
drivers/gpu/drm/mgag200/mgag200_drv.h +5 -1
index 7f7dfbd0f013..f7b22b195016 100644
@@ -421,6 +421,8 @@ void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic
  void mgag200_crtc_reset(struct drm_crtc *crtc);
  struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc);
  void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *crtc_state);
+int mgag200_crtc_enable_vblank(struct drm_crtc *crtc);
+void mgag200_crtc_disable_vblank(struct drm_crtc *crtc);
 
  #define MGAG200_CRTC_FUNCS \
  	.reset = mgag200_crtc_reset, \
@@ -428,7 +430,9 @@ void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_st
  	.set_config = drm_atomic_helper_set_config, \
  	.page_flip = drm_atomic_helper_page_flip, \
  	.atomic_duplicate_state = mgag200_crtc_atomic_duplicate_state, \
-	.atomic_destroy_state = mgag200_crtc_atomic_destroy_state
+	.atomic_destroy_state = mgag200_crtc_atomic_destroy_state, \
+	.enable_vblank = mgag200_crtc_enable_vblank, \
+	.disable_vblank = mgag200_crtc_disable_vblank
 
  void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode);
  void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format);
drivers/gpu/drm/mgag200/mgag200_g200.c +5 -0
index f874e2949840..77ce8d36cef0 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -403,5 +404,9 @@ struct mga_device *mgag200_g200_device_create(struct pci_dev *pdev, const struct
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200eh.c +5 -0
index 52bf49ead5c5..72bd8e3421c2 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -279,5 +280,9 @@ struct mga_device *mgag200_g200eh_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200eh3.c +5 -0
index e7f89b2a59fd..1bbb0745b84a 100644
@@ -7,6 +7,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -184,5 +185,9 @@ struct mga_device *mgag200_g200eh3_device_create(struct pci_dev *pdev,
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200er.c +5 -0
index 4e8a1756138d..3350baf08a45 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -318,5 +319,9 @@ struct mga_device *mgag200_g200er_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200ev.c +5 -0
index d884f3cb0ec7..88d8bcd6fe51 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -323,5 +324,9 @@ struct mga_device *mgag200_g200ev_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200ew3.c +5 -0
index 839401e8b465..9d08180f7612 100644
@@ -7,6 +7,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -204,5 +205,9 @@ struct mga_device *mgag200_g200ew3_device_create(struct pci_dev *pdev,
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200se.c +5 -0
index a824bb8ad579..fcc8075627ef 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -523,5 +524,9 @@ struct mga_device *mgag200_g200se_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200wb.c +5 -0
index 835df0f4fc13..4f8ef3465b9f 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -328,5 +329,9 @@ struct mga_device *mgag200_g200wb_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_mode.c +27 -1
index da2cbe81d4e6..ec6fb1277d6e 100644
@@ -22,6 +22,7 @@
  #include <drm/drm_gem_framebuffer_helper.h>
  #include <drm/drm_panic.h>
  #include <drm/drm_print.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_ddc.h"
  #include "mgag200_drv.h"
@@ -226,7 +227,14 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
  	vblkstr = mode->crtc_vblank_start;
  	vblkend = vtotal + 1;
 
-	linecomp = vdispend;
+	/*
+	 * There's no VBLANK interrupt on Matrox chipsets, so we use
+	 * the VLINE interrupt instead. It triggers when the current
+	 * <linecomp> has been reached. For VBLANK, this is the first
+	 * non-visible line at the bottom of the screen. Therefore,
+	 * keep <linecomp> in sync with <vblkstr>.
+	 */
+	linecomp = vblkstr;
 
  	misc = RREG8(MGA_MISC_IN);
 
@@ -637,6 +645,8 @@ void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_s
  	struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state);
  	struct drm_device *dev = crtc->dev;
  	struct mga_device *mdev = to_mga_device(dev);
+	struct drm_pending_vblank_event *event;
+	unsigned long flags;
 
  	if (crtc_state->enable && crtc_state->color_mgmt_changed) {
  		const struct drm_format_info *format = mgag200_crtc_state->format;
@@ -646,6 +656,18 @@ void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_s
  		else
  			mgag200_crtc_set_gamma_linear(mdev, format);
  	}
+
+	event = crtc->state->event;
+	if (event) {
+		crtc->state->event = NULL;
+
+		spin_lock_irqsave(&dev->event_lock, flags);
+		if (drm_crtc_vblank_get(crtc) != 0)
+			drm_crtc_send_vblank_event(crtc, event);
+		else
+			drm_crtc_arm_vblank_event(crtc, event);
+		spin_unlock_irqrestore(&dev->event_lock, flags);
+	}
  }
 
  void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *old_state)
@@ -676,6 +698,8 @@ void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_
 
  	if (funcs->enable_vidrst)
  		funcs->enable_vidrst(mdev);
+
+	drm_crtc_vblank_on(crtc);
  }
 
  void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *old_state)
@@ -683,6 +707,8 @@ void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic
  	struct mga_device *mdev = to_mga_device(crtc->dev);
  	const struct mgag200_device_funcs *funcs = mdev->funcs;
 
+	drm_crtc_vblank_off(crtc);
+
  	if (funcs->disable_vidrst)
  		funcs->disable_vidrst(mdev);
 
@@ -735,6 +761,32 @@ void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_st
  	kfree(mgag200_crtc_state);

drivers/gpu/drm/mgag200/mgag200_mode.c void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_st · line 771

Quoted 10 lines
drivers/gpu/drm/mgag200/mgag200_mode.c +8 -0
  }
 
+int mgag200_crtc_enable_vblank(struct drm_crtc *crtc)
+{
+	struct mga_device *mdev = to_mga_device(crtc->dev);
+	u32 iclear, ien;
+
+	iclear = RREG32(MGAREG_ICLEAR);
+	iclear |= MGAREG_ICLEAR_VLINEICLR;
+	WREG32(MGAREG_ICLEAR, iclear);

same here, don't read the iclear register.

Quoted 33 lines
drivers/gpu/drm/mgag200/mgag200_mode.c +18 -0
+
+	ien = RREG32(MGAREG_IEN);
+	ien |= MGAREG_IEN_VLINEIEN;
+	WREG32(MGAREG_IEN, ien);
+
+	return 0;
+}
+
+void mgag200_crtc_disable_vblank(struct drm_crtc *crtc)
+{
+	struct mga_device *mdev = to_mga_device(crtc->dev);
+	u32 ien;
+
+	ien = RREG32(MGAREG_IEN);
+	ien &= ~(MGAREG_IEN_VLINEIEN);
+	WREG32(MGAREG_IEN, ien);
+}
+
  /*
   * Mode config
   */
drivers/gpu/drm/mgag200/mgag200_reg.h +3 -0
index aa73463674e4..d4fef8f25871 100644
@@ -102,10 +102,17 @@
  #define MGAREG_EXEC		0x0100
 
  #define	MGAREG_FIFOSTATUS	0x1e10
+
  #define	MGAREG_STATUS		0x1e14
+#define MGAREG_STATUS_VLINEPEN	BIT(5)
+

drivers/gpu/drm/mgag200/mgag200_reg.h line 118

Quoted 10 lines
drivers/gpu/drm/mgag200/mgag200_reg.h +4 -0
  #define MGAREG_CACHEFLUSH       0x1fff
+
  #define	MGAREG_ICLEAR		0x1e18
+#define MGAREG_ICLEAR_VLINEICLR	BIT(5)
+
  #define	MGAREG_IEN		0x1e1c
+#define MGAREG_IEN_VLINEIEN	BIT(5)
 
  #define	MGAREG_VCOUNT		0x1e20
 

Do you know what happens if the IRQ doesn't work (ie not receiving any IRQ)?
When testing the DMA, I remember that on a few machines, I never get the Softrap IRQ. I don't know if it was a DMA engine problem, or if it's the mgag200 IRQ that was not connected properly.

I will try to find this machine, and test this series on it.

Best regards,

Signature · 1 line
Jocelyn
author maintainer
#12 lore raw

Re: [PATCH v4 6/7] drm/mgag200: Add vblank support

Hi Jocelyn

Am 08.07.24 um 14:46 schrieb Jocelyn Falempe:

Quoted 8 lines · Thomas Zimmermann
Quoted 5 lines

There's no VBLANK interrupt on Matrox chipsets. The workaround that is being used here and in other free Matrox drivers is to program <linecomp> to the value of <vblkstr> and enable the VLINE interrupt. This triggers an interrupt at the time when VBLANK begins.

Quoted 5 lines
Quoted 3 lines

VLINE uses separate registers for enabling and clearing pending interrupts. No extra synchronization between irq handler and the rest of the driver is required.

Thanks for this patch, I have a few comments below:

They make sense. I'll implement them.

[...]

Quoted 6 lines
Quoted 3 lines

  #define    MGAREG_IEN        0x1e1c
+#define MGAREG_IEN_VLINEIEN    BIT(5)
    #define    MGAREG_VCOUNT        0x1e20

Do you know what happens if the IRQ doesn't work (ie not receiving any IRQ)?

We'd miss the vblank timeout and the display would be slow or static.

Quoted 1 line

When testing the DMA, I remember that on a few machines, I never get the Softrap IRQ. I don't know if it was a DMA engine problem, or if it's the mgag200 IRQ that was not connected properly.

What to do depends. Not having an IRQ assigned is a problem in the BIOS configuration. All other DRM drivers expect the IRQ to be present and mgag200 would not be any different here. Users should assign an IRQ in BIOS. If there's really a hardware problem on some Matrox chips, we'd have to revert the patch. There's little use in handling separate vblank and non-vblank code paths. Most other patches in this patchset would still remain useful, I think.

OTOH other Matrox drivers have implemented vblank support since forever. I'd expect (well, hope at least :) that this simply isn't a problem.

Quoted 3 lines

I will try to find this machine, and test this series on it.

Thanks a lot.

Best regards
Thomas

Quoted 2 lines

Best regards,

Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author maintainer
#13 lore raw

Re: [PATCH v4 6/7] drm/mgag200: Add vblank support

In reply to Jocelyn Falempe

Hi

Am 08.07.24 um 14:46 schrieb Jocelyn Falempe:

Quoted 73 lines · Thomas Zimmermann
Quoted 8 lines

There's no VBLANK interrupt on Matrox chipsets. The workaround that is being used here and in other free Matrox drivers is to program <linecomp> to the value of <vblkstr> and enable the VLINE interrupt. This triggers an interrupt at the time when VBLANK begins.

VLINE uses separate registers for enabling and clearing pending interrupts. No extra synchronization between irq handler and the rest of the driver is required.

Thanks for this patch, I have a few comments below:

Quoted 60 lines

v4:
- recreate patch on latest upstream
- use devm_request_irq() for managed cleanup
- fail if vblanking cannot be initialized
- rename register constants (Sam, Emil)
- clear interrupt before registering handler (Ville)
- move <linecomp> programming into separate commit
- set <linecomp> to <vblkstr>
- fix typo in commit message

v3:
- set <linecomp> to <vdisplay> + 1 to trigger at VBLANK
- expand comment on linecomp

v2:
- only signal vblank on CRTC 0
- use constants for registers and fields
- set VLINECLR before enabling interrupt
- test against STATUS and IEN in irq handler
- coding-style fixes

---
  drivers/gpu/drm/mgag200/mgag200_drv.c     | 47 ++++++++++++++++++++ drivers/gpu/drm/mgag200/mgag200_drv.h     |  6 ++- drivers/gpu/drm/mgag200/mgag200_g200.c    |  5 +++ drivers/gpu/drm/mgag200/mgag200_g200eh.c  |  5 +++ drivers/gpu/drm/mgag200/mgag200_g200eh3.c |  5 +++ drivers/gpu/drm/mgag200/mgag200_g200er.c  |  5 +++ drivers/gpu/drm/mgag200/mgag200_g200ev.c  |  5 +++ drivers/gpu/drm/mgag200/mgag200_g200ew3.c |  5 +++ drivers/gpu/drm/mgag200/mgag200_g200se.c  |  5 +++ drivers/gpu/drm/mgag200/mgag200_g200wb.c  |  5 +++ drivers/gpu/drm/mgag200/mgag200_mode.c    | 54 ++++++++++++++++++++++- drivers/gpu/drm/mgag200/mgag200_reg.h     |  7 +++

  12 files changed, 152 insertions(+), 2 deletions(-)
drivers/gpu/drm/mgag200/mgag200_drv.c +1 -0
index 62080cf0f2da..62479de9e659 100644
@@ -18,6 +18,7 @@
   #include <drm/drm_managed.h>
   #include <drm/drm_module.h>
   #include <drm/drm_pciids.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"
   @@ -84,6 +85,35 @@ resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size)
       return offset - 65536;

  }
  +static irqreturn_t mgag200_irq_handler(int irq, void *arg)

drivers/gpu/drm/mgag200/mgag200_drv.c +6 -0
+{
+    struct drm_device *dev = arg;
+    struct mga_device *mdev = to_mga_device(dev);
+    struct drm_crtc *crtc;
+    u32 status, ien, iclear;
+
Quoted 8 lines

drivers/gpu/drm/mgag200/mgag200_drv.c resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size) · line 101

Quoted 7 lines

+    status = RREG32(MGAREG_STATUS);

drivers/gpu/drm/mgag200/mgag200_drv.c +6 -0
+
+    if (status & MGAREG_STATUS_VLINEPEN) {
+        ien = RREG32(MGAREG_IEN);
+        if (!(ien & MGAREG_IEN_VLINEIEN))
+            goto out;
+

This is to catch a race condition, receiving the interrupt, just after disabling it ? I think you still want to clear it, to avoid having it fire as soon as you re-enable it ?

Shared interrupts (with another device) can bring us here. I was thinking that we might have stale state on the Matrox device (i.e., VLINEIEN is off, but the VLINEPEN status bit is stil set). So it checks whether the VLINE interrupt is really on.

Quoted 372 lines
Quoted 7 lines

+        crtc = drm_crtc_from_index(dev, 0);
+        if (WARN_ON_ONCE(!crtc))
+            goto out;
+        drm_crtc_handle_vblank(crtc);
+
+        iclear = RREG32(MGAREG_ICLEAR);

iclear is a Write-Only register, according to the documentation. So reading it will always return 0.

I would suggest to just write:

WREG32(MGAREG_ICLEAR, MGAREG_ICLEAR_VLINEICLR);

Quoted 22 lines

+        iclear |= MGAREG_ICLEAR_VLINEICLR;
+        WREG32(MGAREG_ICLEAR, iclear);
+        return IRQ_HANDLED;
+    }

drivers/gpu/drm/mgag200/mgag200_drv.c +5 -0
+
+out:
+    return IRQ_NONE;
+}
+

  /*
   * DRM driver
   */

drivers/gpu/drm/mgag200/mgag200_drv.c +2 -0
@@ -167,7 +197,9 @@ int mgag200_device_init(struct mga_device *mdev,
               const struct mgag200_device_funcs *funcs)
   {
       struct drm_device *dev = &mdev->base;
+    struct pci_dev *pdev = to_pci_dev(dev->dev);
       u8 crtcext3, misc;
+    u32 ien, iclear;
       int ret;
         mdev->info = info;

drivers/gpu/drm/mgag200/mgag200_drv.c int mgag200_device_init(struct mga_device *mdev, · line 227

Quoted 4 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +2 -0
@@ -192,6 +224,21 @@ int mgag200_device_init(struct mga_device *mdev,
         mutex_unlock(&mdev->rmmio_lock);
   +    ien = RREG32(MGAREG_IEN);
+    ien &= ~(MGAREG_IEN_VLINEIEN);
+    WREG32(MGAREG_IEN, ien);

I would suggest to disable all interrupt instead,
WREG32(MGAREG_IEN, 0);

drivers/gpu/drm/mgag200/mgag200_drv.c int mgag200_device_init(struct mga_device *mdev, · line 229

Quoted 2 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +2 -0
+
+    iclear = RREG32(MGAREG_ICLEAR);

same here, don't read the iclear register.

Quoted 261 lines
drivers/gpu/drm/mgag200/mgag200_drv.c +10 -0
+    iclear |= MGAREG_ICLEAR_VLINEICLR;
+    WREG32(MGAREG_ICLEAR, iclear);
+
+    ret = devm_request_irq(&pdev->dev, pdev->irq, 
 mgag200_irq_handler, IRQF_SHARED,
+                   dev->driver->name, dev);
+    if (ret) {
+        drm_err(dev, "Failed to acquire interrupt, error %d\n", ret);
+        return ret;
+    }
+
       return 0;
   }

  diff --git a/drivers/gpu/drm/mgag200/mgag200_drv.h b/drivers/gpu/drm/mgag200/mgag200_drv.h index 7f7dfbd0f013..f7b22b195016 100644

drivers/gpu/drm/mgag200/mgag200_drv.h +0 -0
@@ -421,6 +421,8 @@ void mgag200_crtc_helper_atomic_disable(struct 

drm_crtc *crtc, struct drm_atomic
  void mgag200_crtc_reset(struct drm_crtc *crtc);
  struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc); void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *crtc_state);

drivers/gpu/drm/mgag200/mgag200_drv.h +2 -0
+int mgag200_crtc_enable_vblank(struct drm_crtc *crtc);
+void mgag200_crtc_disable_vblank(struct drm_crtc *crtc);

    #define MGAG200_CRTC_FUNCS \
      .reset = mgag200_crtc_reset, \

drivers/gpu/drm/mgag200/mgag200_drv.h +0 -0
@@ -428,7 +430,9 @@ void mgag200_crtc_atomic_destroy_state(struct 

drm_crtc *crtc, struct drm_crtc_st
      .set_config = drm_atomic_helper_set_config, \
      .page_flip = drm_atomic_helper_page_flip, \
      .atomic_duplicate_state = mgag200_crtc_atomic_duplicate_state, \
-    .atomic_destroy_state = mgag200_crtc_atomic_destroy_state
+    .atomic_destroy_state = mgag200_crtc_atomic_destroy_state, \
+    .enable_vblank = mgag200_crtc_enable_vblank, \
+    .disable_vblank = mgag200_crtc_disable_vblank
    void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode); void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format);

drivers/gpu/drm/mgag200/mgag200_g200.c +1 -0
index f874e2949840..77ce8d36cef0 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/drm_gem_atomic_helper.h>
   #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"

  @@ -403,5 +404,9 @@ struct mga_device *mgag200_g200_device_create(struct pci_dev *pdev, const struct drm_mode_config_reset(dev);
      drm_kms_helper_poll_init(dev);
  +    ret = drm_vblank_init(dev, 1);
+    if (ret)
+        return ERR_PTR(ret);
+
      return mdev;
  }

drivers/gpu/drm/mgag200/mgag200_g200eh.c +1 -0
index 52bf49ead5c5..72bd8e3421c2 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/drm_gem_atomic_helper.h>
   #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"

  @@ -279,5 +280,9 @@ struct mga_device *mgag200_g200eh_device_create(struct pci_dev *pdev, const stru drm_mode_config_reset(dev);
      drm_kms_helper_poll_init(dev);
  +    ret = drm_vblank_init(dev, 1);
+    if (ret)
+        return ERR_PTR(ret);
+
      return mdev;
  }

drivers/gpu/drm/mgag200/mgag200_g200eh3.c +1 -0
index e7f89b2a59fd..1bbb0745b84a 100644
@@ -7,6 +7,7 @@
   #include <drm/drm_drv.h>
   #include <drm/drm_gem_atomic_helper.h>
   #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"
   @@ -184,5 +185,9 @@ struct mga_device *mgag200_g200eh3_device_create(struct pci_dev *pdev,
       drm_mode_config_reset(dev);

      drm_kms_helper_poll_init(dev);
  +    ret = drm_vblank_init(dev, 1);
+    if (ret)
+        return ERR_PTR(ret);
+
      return mdev;
  }

drivers/gpu/drm/mgag200/mgag200_g200er.c +1 -0
index 4e8a1756138d..3350baf08a45 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/drm_gem_atomic_helper.h>
   #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"

  @@ -318,5 +319,9 @@ struct mga_device *mgag200_g200er_device_create(struct pci_dev *pdev, const stru drm_mode_config_reset(dev);
      drm_kms_helper_poll_init(dev);
  +    ret = drm_vblank_init(dev, 1);
+    if (ret)
+        return ERR_PTR(ret);
+
      return mdev;
  }

drivers/gpu/drm/mgag200/mgag200_g200ev.c +1 -0
index d884f3cb0ec7..88d8bcd6fe51 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/drm_gem_atomic_helper.h>
   #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"

  @@ -323,5 +324,9 @@ struct mga_device *mgag200_g200ev_device_create(struct pci_dev *pdev, const stru drm_mode_config_reset(dev);
      drm_kms_helper_poll_init(dev);
  +    ret = drm_vblank_init(dev, 1);
+    if (ret)
+        return ERR_PTR(ret);
+
      return mdev;
  }

drivers/gpu/drm/mgag200/mgag200_g200ew3.c +1 -0
index 839401e8b465..9d08180f7612 100644
@@ -7,6 +7,7 @@
   #include <drm/drm_drv.h>
   #include <drm/drm_gem_atomic_helper.h>
   #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"
   @@ -204,5 +205,9 @@ struct mga_device *mgag200_g200ew3_device_create(struct pci_dev *pdev,
       drm_mode_config_reset(dev);

      drm_kms_helper_poll_init(dev);
  +    ret = drm_vblank_init(dev, 1);
+    if (ret)
+        return ERR_PTR(ret);
+
      return mdev;
  }

drivers/gpu/drm/mgag200/mgag200_g200se.c +1 -0
index a824bb8ad579..fcc8075627ef 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/drm_gem_atomic_helper.h>
   #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"

  @@ -523,5 +524,9 @@ struct mga_device *mgag200_g200se_device_create(struct pci_dev *pdev, const stru drm_mode_config_reset(dev);
      drm_kms_helper_poll_init(dev);
  +    ret = drm_vblank_init(dev, 1);
+    if (ret)
+        return ERR_PTR(ret);
+
      return mdev;
  }

drivers/gpu/drm/mgag200/mgag200_g200wb.c +1 -0
index 835df0f4fc13..4f8ef3465b9f 100644
@@ -8,6 +8,7 @@
   #include <drm/drm_drv.h>
   #include <drm/drm_gem_atomic_helper.h>
   #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_drv.h"

  @@ -328,5 +329,9 @@ struct mga_device *mgag200_g200wb_device_create(struct pci_dev *pdev, const stru drm_mode_config_reset(dev);
      drm_kms_helper_poll_init(dev);
  +    ret = drm_vblank_init(dev, 1);
+    if (ret)
+        return ERR_PTR(ret);
+
      return mdev;
  }

drivers/gpu/drm/mgag200/mgag200_mode.c +9 -0
index da2cbe81d4e6..ec6fb1277d6e 100644
@@ -22,6 +22,7 @@
   #include <drm/drm_gem_framebuffer_helper.h>
   #include <drm/drm_panic.h>
   #include <drm/drm_print.h>
+#include <drm/drm_vblank.h>
     #include "mgag200_ddc.h"
   #include "mgag200_drv.h"
@@ -226,7 +227,14 @@ void mgag200_set_mode_regs(struct mga_device 
 *mdev, const struct drm_display_mod
       vblkstr = mode->crtc_vblank_start;
       vblkend = vtotal + 1;
   -    linecomp = vdispend;
+    /*
+     * There's no VBLANK interrupt on Matrox chipsets, so we use
+     * the VLINE interrupt instead. It triggers when the current
+     * <linecomp> has been reached. For VBLANK, this is the first
+     * non-visible line at the bottom of the screen. Therefore,
+     * keep <linecomp> in sync with <vblkstr>.
+     */
+    linecomp = vblkstr;
         misc = RREG8(MGA_MISC_IN);

  @@ -637,6 +645,8 @@ void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_s struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state); struct drm_device *dev = crtc->dev;
      struct mga_device *mdev = to_mga_device(dev);
+    struct drm_pending_vblank_event *event;
+    unsigned long flags;
        if (crtc_state->enable && crtc_state->color_mgmt_changed) {
          const struct drm_format_info *format = mgag200_crtc_state->format;

drivers/gpu/drm/mgag200/mgag200_mode.c +0 -0
@@ -646,6 +656,18 @@ void mgag200_crtc_helper_atomic_flush(struct 

drm_crtc *crtc, struct drm_atomic_s
          else mgag200_crtc_set_gamma_linear(mdev, format); }

drivers/gpu/drm/mgag200/mgag200_mode.c +12 -0
+
+    event = crtc->state->event;
+    if (event) {
+        crtc->state->event = NULL;
+
+        spin_lock_irqsave(&dev->event_lock, flags);
+        if (drm_crtc_vblank_get(crtc) != 0)
+            drm_crtc_send_vblank_event(crtc, event);
+        else
+            drm_crtc_arm_vblank_event(crtc, event);
+        spin_unlock_irqrestore(&dev->event_lock, flags);
+    }

  }
    void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *old_state)

drivers/gpu/drm/mgag200/mgag200_mode.c +0 -0
@@ -676,6 +698,8 @@ void mgag200_crtc_helper_atomic_enable(struct 

drm_crtc *crtc, struct drm_atomic_
        if (funcs->enable_vidrst)
          funcs->enable_vidrst(mdev);
+
+    drm_crtc_vblank_on(crtc);
  }
    void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *old_state)

drivers/gpu/drm/mgag200/mgag200_mode.c +0 -0
@@ -683,6 +707,8 @@ void mgag200_crtc_helper_atomic_disable(struct 

drm_crtc *crtc, struct drm_atomic
      struct mga_device *mdev = to_mga_device(crtc->dev);
      const struct mgag200_device_funcs *funcs = mdev->funcs;
  +    drm_crtc_vblank_off(crtc);
+
      if (funcs->disable_vidrst)
          funcs->disable_vidrst(mdev);
  @@ -735,6 +761,32 @@ void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_st kfree(mgag200_crtc_state);
  }
  +int mgag200_crtc_enable_vblank(struct drm_crtc *crtc)

drivers/gpu/drm/mgag200/mgag200_mode.c void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_st · line 771

Quoted 7 lines
drivers/gpu/drm/mgag200/mgag200_mode.c +7 -0
+{
+    struct mga_device *mdev = to_mga_device(crtc->dev);
+    u32 iclear, ien;
+
+    iclear = RREG32(MGAREG_ICLEAR);
+    iclear |= MGAREG_ICLEAR_VLINEICLR;
+    WREG32(MGAREG_ICLEAR, iclear);

same here, don't read the iclear register.

Quoted 30 lines
drivers/gpu/drm/mgag200/mgag200_mode.c +18 -0
+
+    ien = RREG32(MGAREG_IEN);
+    ien |= MGAREG_IEN_VLINEIEN;
+    WREG32(MGAREG_IEN, ien);
+
+    return 0;
+}
+
+void mgag200_crtc_disable_vblank(struct drm_crtc *crtc)
+{
+    struct mga_device *mdev = to_mga_device(crtc->dev);
+    u32 ien;
+
+    ien = RREG32(MGAREG_IEN);
+    ien &= ~(MGAREG_IEN_VLINEIEN);
+    WREG32(MGAREG_IEN, ien);
+}
+

  /*
   * Mode config
   */

drivers/gpu/drm/mgag200/mgag200_reg.h +1 -0
index aa73463674e4..d4fef8f25871 100644
@@ -102,10 +102,17 @@
   #define MGAREG_EXEC        0x0100
     #define    MGAREG_FIFOSTATUS    0x1e10
+
   #define    MGAREG_STATUS        0x1e14

drivers/gpu/drm/mgag200/mgag200_reg.h line 115

Quoted 10 lines
drivers/gpu/drm/mgag200/mgag200_reg.h +6 -0
+#define MGAREG_STATUS_VLINEPEN    BIT(5)
+
   #define MGAREG_CACHEFLUSH       0x1fff
+
   #define    MGAREG_ICLEAR        0x1e18
+#define MGAREG_ICLEAR_VLINEICLR    BIT(5)
+
   #define    MGAREG_IEN        0x1e1c
+#define MGAREG_IEN_VLINEIEN    BIT(5)
     #define    MGAREG_VCOUNT        0x1e20

Do you know what happens if the IRQ doesn't work (ie not receiving any IRQ)?
When testing the DMA, I remember that on a few machines, I never get the Softrap IRQ. I don't know if it was a DMA engine problem, or if it's the mgag200 IRQ that was not connected properly.

I will try to find this machine, and test this series on it.

Best regards,

Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author maintainer Tested-by Jocelyn Falempe on v4 6/7
#14 lore raw

Re: [PATCH v4 6/7] drm/mgag200: Add vblank support

In reply to Thomas Zimmermann

Am 05.07.24 um 13:47 schrieb Thomas Zimmermann:

Quoted 30 lines

There's no VBLANK interrupt on Matrox chipsets. The workaround that is being used here and in other free Matrox drivers is to program <linecomp> to the value of <vblkstr> and enable the VLINE interrupt. This triggers an interrupt at the time when VBLANK begins.

VLINE uses separate registers for enabling and clearing pending interrupts. No extra synchronization between irq handler and the rest of the driver is required.

v4:
- recreate patch on latest upstream
- use devm_request_irq() for managed cleanup
- fail if vblanking cannot be initialized
- rename register constants (Sam, Emil)
- clear interrupt before registering handler (Ville)
- move <linecomp> programming into separate commit
- set <linecomp> to <vblkstr>
- fix typo in commit message

v3:
- set <linecomp> to <vdisplay> + 1 to trigger at VBLANK
- expand comment on linecomp

v2:
- only signal vblank on CRTC 0
- use constants for registers and fields
- set VLINECLR before enabling interrupt
- test against STATUS and IEN in irq handler
- coding-style fixes

Quoted 3 lines

via irc: https://dri.freedesktop.org/~cbrill/dri-log/index.php?channel=dri-devel&date=2024-07-17

Quoted 424 lines
---
  drivers/gpu/drm/mgag200/mgag200_drv.c     | 47 ++++++++++++++++++++
  drivers/gpu/drm/mgag200/mgag200_drv.h     |  6 ++-
  drivers/gpu/drm/mgag200/mgag200_g200.c    |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200eh.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200eh3.c |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200er.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200ev.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200ew3.c |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200se.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_g200wb.c  |  5 +++
  drivers/gpu/drm/mgag200/mgag200_mode.c    | 54 ++++++++++++++++++++++-
  drivers/gpu/drm/mgag200/mgag200_reg.h     |  7 +++
  12 files changed, 152 insertions(+), 2 deletions(-)
drivers/gpu/drm/mgag200/mgag200_drv.c +47 -0
index 62080cf0f2da..62479de9e659 100644
@@ -18,6 +18,7 @@
  #include <drm/drm_managed.h>
  #include <drm/drm_module.h>
  #include <drm/drm_pciids.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -84,6 +85,35 @@ resource_size_t mgag200_probe_vram(void __iomem *mem, resource_size_t size)
  	return offset - 65536;
  }
 
+static irqreturn_t mgag200_irq_handler(int irq, void *arg)
+{
+	struct drm_device *dev = arg;
+	struct mga_device *mdev = to_mga_device(dev);
+	struct drm_crtc *crtc;
+	u32 status, ien, iclear;
+
+	status = RREG32(MGAREG_STATUS);
+
+	if (status & MGAREG_STATUS_VLINEPEN) {
+		ien = RREG32(MGAREG_IEN);
+		if (!(ien & MGAREG_IEN_VLINEIEN))
+			goto out;
+
+		crtc = drm_crtc_from_index(dev, 0);
+		if (WARN_ON_ONCE(!crtc))
+			goto out;
+		drm_crtc_handle_vblank(crtc);
+
+		iclear = RREG32(MGAREG_ICLEAR);
+		iclear |= MGAREG_ICLEAR_VLINEICLR;
+		WREG32(MGAREG_ICLEAR, iclear);
+		return IRQ_HANDLED;
+	}
+
+out:
+	return IRQ_NONE;
+}
+
  /*
   * DRM driver
   */
@@ -167,7 +197,9 @@ int mgag200_device_init(struct mga_device *mdev,
  			const struct mgag200_device_funcs *funcs)
  {
  	struct drm_device *dev = &mdev->base;
+	struct pci_dev *pdev = to_pci_dev(dev->dev);
  	u8 crtcext3, misc;
+	u32 ien, iclear;
  	int ret;
 
  	mdev->info = info;
@@ -192,6 +224,21 @@ int mgag200_device_init(struct mga_device *mdev,
 
  	mutex_unlock(&mdev->rmmio_lock);
 
+	ien = RREG32(MGAREG_IEN);
+	ien &= ~(MGAREG_IEN_VLINEIEN);
+	WREG32(MGAREG_IEN, ien);
+
+	iclear = RREG32(MGAREG_ICLEAR);
+	iclear |= MGAREG_ICLEAR_VLINEICLR;
+	WREG32(MGAREG_ICLEAR, iclear);
+
+	ret = devm_request_irq(&pdev->dev, pdev->irq, mgag200_irq_handler, IRQF_SHARED,
+			       dev->driver->name, dev);
+	if (ret) {
+		drm_err(dev, "Failed to acquire interrupt, error %d\n", ret);
+		return ret;
+	}
+
  	return 0;
  }
 
drivers/gpu/drm/mgag200/mgag200_drv.h +5 -1
index 7f7dfbd0f013..f7b22b195016 100644
@@ -421,6 +421,8 @@ void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic
  void mgag200_crtc_reset(struct drm_crtc *crtc);
  struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc);
  void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_state *crtc_state);
+int mgag200_crtc_enable_vblank(struct drm_crtc *crtc);
+void mgag200_crtc_disable_vblank(struct drm_crtc *crtc);
 
  #define MGAG200_CRTC_FUNCS \
  	.reset = mgag200_crtc_reset, \
@@ -428,7 +430,9 @@ void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_st
  	.set_config = drm_atomic_helper_set_config, \
  	.page_flip = drm_atomic_helper_page_flip, \
  	.atomic_duplicate_state = mgag200_crtc_atomic_duplicate_state, \
-	.atomic_destroy_state = mgag200_crtc_atomic_destroy_state
+	.atomic_destroy_state = mgag200_crtc_atomic_destroy_state, \
+	.enable_vblank = mgag200_crtc_enable_vblank, \
+	.disable_vblank = mgag200_crtc_disable_vblank
 
  void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode);
  void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format);
drivers/gpu/drm/mgag200/mgag200_g200.c +5 -0
index f874e2949840..77ce8d36cef0 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -403,5 +404,9 @@ struct mga_device *mgag200_g200_device_create(struct pci_dev *pdev, const struct
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200eh.c +5 -0
index 52bf49ead5c5..72bd8e3421c2 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -279,5 +280,9 @@ struct mga_device *mgag200_g200eh_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200eh3.c +5 -0
index e7f89b2a59fd..1bbb0745b84a 100644
@@ -7,6 +7,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -184,5 +185,9 @@ struct mga_device *mgag200_g200eh3_device_create(struct pci_dev *pdev,
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200er.c +5 -0
index 4e8a1756138d..3350baf08a45 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -318,5 +319,9 @@ struct mga_device *mgag200_g200er_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200ev.c +5 -0
index d884f3cb0ec7..88d8bcd6fe51 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -323,5 +324,9 @@ struct mga_device *mgag200_g200ev_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200ew3.c +5 -0
index 839401e8b465..9d08180f7612 100644
@@ -7,6 +7,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -204,5 +205,9 @@ struct mga_device *mgag200_g200ew3_device_create(struct pci_dev *pdev,
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200se.c +5 -0
index a824bb8ad579..fcc8075627ef 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -523,5 +524,9 @@ struct mga_device *mgag200_g200se_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_g200wb.c +5 -0
index 835df0f4fc13..4f8ef3465b9f 100644
@@ -8,6 +8,7 @@
  #include <drm/drm_drv.h>
  #include <drm/drm_gem_atomic_helper.h>
  #include <drm/drm_probe_helper.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_drv.h"
 
@@ -328,5 +329,9 @@ struct mga_device *mgag200_g200wb_device_create(struct pci_dev *pdev, const stru
  	drm_mode_config_reset(dev);
  	drm_kms_helper_poll_init(dev);
 
+	ret = drm_vblank_init(dev, 1);
+	if (ret)
+		return ERR_PTR(ret);
+
  	return mdev;
  }
drivers/gpu/drm/mgag200/mgag200_mode.c +53 -1
index da2cbe81d4e6..ec6fb1277d6e 100644
@@ -22,6 +22,7 @@
  #include <drm/drm_gem_framebuffer_helper.h>
  #include <drm/drm_panic.h>
  #include <drm/drm_print.h>
+#include <drm/drm_vblank.h>
 
  #include "mgag200_ddc.h"
  #include "mgag200_drv.h"
@@ -226,7 +227,14 @@ void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mod
  	vblkstr = mode->crtc_vblank_start;
  	vblkend = vtotal + 1;
 
-	linecomp = vdispend;
+	/*
+	 * There's no VBLANK interrupt on Matrox chipsets, so we use
+	 * the VLINE interrupt instead. It triggers when the current
+	 * <linecomp> has been reached. For VBLANK, this is the first
+	 * non-visible line at the bottom of the screen. Therefore,
+	 * keep <linecomp> in sync with <vblkstr>.
+	 */
+	linecomp = vblkstr;
 
  	misc = RREG8(MGA_MISC_IN);
 
@@ -637,6 +645,8 @@ void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_s
  	struct mgag200_crtc_state *mgag200_crtc_state = to_mgag200_crtc_state(crtc_state);
  	struct drm_device *dev = crtc->dev;
  	struct mga_device *mdev = to_mga_device(dev);
+	struct drm_pending_vblank_event *event;
+	unsigned long flags;
 
  	if (crtc_state->enable && crtc_state->color_mgmt_changed) {
  		const struct drm_format_info *format = mgag200_crtc_state->format;
@@ -646,6 +656,18 @@ void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_s
  		else
  			mgag200_crtc_set_gamma_linear(mdev, format);
  	}
+
+	event = crtc->state->event;
+	if (event) {
+		crtc->state->event = NULL;
+
+		spin_lock_irqsave(&dev->event_lock, flags);
+		if (drm_crtc_vblank_get(crtc) != 0)
+			drm_crtc_send_vblank_event(crtc, event);
+		else
+			drm_crtc_arm_vblank_event(crtc, event);
+		spin_unlock_irqrestore(&dev->event_lock, flags);
+	}
  }
 
  void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *old_state)
@@ -676,6 +698,8 @@ void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_
 
  	if (funcs->enable_vidrst)
  		funcs->enable_vidrst(mdev);
+
+	drm_crtc_vblank_on(crtc);
  }
 
  void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *old_state)
@@ -683,6 +707,8 @@ void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic
  	struct mga_device *mdev = to_mga_device(crtc->dev);
  	const struct mgag200_device_funcs *funcs = mdev->funcs;
 
+	drm_crtc_vblank_off(crtc);
+
  	if (funcs->disable_vidrst)
  		funcs->disable_vidrst(mdev);
 
@@ -735,6 +761,32 @@ void mgag200_crtc_atomic_destroy_state(struct drm_crtc *crtc, struct drm_crtc_st
  	kfree(mgag200_crtc_state);
  }
 
+int mgag200_crtc_enable_vblank(struct drm_crtc *crtc)
+{
+	struct mga_device *mdev = to_mga_device(crtc->dev);
+	u32 iclear, ien;
+
+	iclear = RREG32(MGAREG_ICLEAR);
+	iclear |= MGAREG_ICLEAR_VLINEICLR;
+	WREG32(MGAREG_ICLEAR, iclear);
+
+	ien = RREG32(MGAREG_IEN);
+	ien |= MGAREG_IEN_VLINEIEN;
+	WREG32(MGAREG_IEN, ien);
+
+	return 0;
+}
+
+void mgag200_crtc_disable_vblank(struct drm_crtc *crtc)
+{
+	struct mga_device *mdev = to_mga_device(crtc->dev);
+	u32 ien;
+
+	ien = RREG32(MGAREG_IEN);
+	ien &= ~(MGAREG_IEN_VLINEIEN);
+	WREG32(MGAREG_IEN, ien);
+}
+
  /*
   * Mode config
   */
drivers/gpu/drm/mgag200/mgag200_reg.h +7 -0
index aa73463674e4..d4fef8f25871 100644
@@ -102,10 +102,17 @@
  #define MGAREG_EXEC		0x0100
 
  #define	MGAREG_FIFOSTATUS	0x1e10
+
  #define	MGAREG_STATUS		0x1e14
+#define MGAREG_STATUS_VLINEPEN	BIT(5)
+
  #define MGAREG_CACHEFLUSH       0x1fff
+
  #define	MGAREG_ICLEAR		0x1e18
+#define MGAREG_ICLEAR_VLINEICLR	BIT(5)
+
  #define	MGAREG_IEN		0x1e1c
+#define MGAREG_IEN_VLINEIEN	BIT(5)
 
  #define	MGAREG_VCOUNT		0x1e20
 
Signature · 7 lines
--
Thomas Zimmermann
Graphics Driver Developer
SUSE Software Solutions Germany GmbH
Frankenstrasse 146, 90461 Nuernberg, Germany
GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman
HRB 36809 (AG Nuernberg)
author maintainer
#15 lore raw

PATCH v4 7/7 drm/mgag200: Implement struct drm_crtc_funcs.get_vblank_timestamp

In reply to Thomas Zimmermann

Implement struct drm_crtc_funcs.get_vblank_timestamp with the DRM helper drm_crtc_vblank_helper_get_vblank_timestamp() with its helper get_scanout_position. Read the scanout position from the MGAREG_VCOUNT register.

---
 drivers/gpu/drm/mgag200/mgag200_drv.h  | 10 ++++++++--
 drivers/gpu/drm/mgag200/mgag200_mode.c | 25 +++++++++++++++++++++++++
 2 files changed, 33 insertions(+), 2 deletions(-)
drivers/gpu/drm/mgag200/mgag200_drv.h +8 -2
index f7b22b195016..acfa05335b09 100644
@@ -410,13 +410,18 @@ int mgag200_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_st
 void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
 void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
 void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
+bool mgag200_crtc_helper_get_scanout_position(struct drm_crtc *crtc, bool in_vblank_irq,
+					      int *vpos, int *hpos,
+					      ktime_t *stime, ktime_t *etime,
+					      const struct drm_display_mode *mode);
 
 #define MGAG200_CRTC_HELPER_FUNCS \
 	.mode_valid = mgag200_crtc_helper_mode_valid, \
 	.atomic_check = mgag200_crtc_helper_atomic_check, \
 	.atomic_flush = mgag200_crtc_helper_atomic_flush, \
 	.atomic_enable = mgag200_crtc_helper_atomic_enable, \
-	.atomic_disable = mgag200_crtc_helper_atomic_disable
+	.atomic_disable = mgag200_crtc_helper_atomic_disable, \
+	.get_scanout_position = mgag200_crtc_helper_get_scanout_position
 
 void mgag200_crtc_reset(struct drm_crtc *crtc);
 struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc);
@@ -432,7 +437,8 @@ void mgag200_crtc_disable_vblank(struct drm_crtc *crtc);
 	.atomic_duplicate_state = mgag200_crtc_atomic_duplicate_state, \
 	.atomic_destroy_state = mgag200_crtc_atomic_destroy_state, \
 	.enable_vblank = mgag200_crtc_enable_vblank, \
-	.disable_vblank = mgag200_crtc_disable_vblank
+	.disable_vblank = mgag200_crtc_disable_vblank, \
+	.get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp
 
 void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode);
 void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format);
drivers/gpu/drm/mgag200/mgag200_mode.c +25 -0
index ec6fb1277d6e..fb03422c763b 100644
@@ -718,6 +718,31 @@ void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic
 		funcs->enable_vidrst(mdev);
 }
 
+bool mgag200_crtc_helper_get_scanout_position(struct drm_crtc *crtc, bool in_vblank_irq,
+					      int *vpos, int *hpos,
+					      ktime_t *stime, ktime_t *etime,
+					      const struct drm_display_mode *mode)
+{
+	struct mga_device *mdev = to_mga_device(crtc->dev);
+	u32 vcount;
+
+	if (stime)
+		*stime = ktime_get();
+
+	if (vpos) {
+		vcount = RREG32(MGAREG_VCOUNT);
+		*vpos = vcount & GENMASK(11, 0);
+	}
+
+	if (hpos)
+		*hpos = mode->htotal >> 1; // near middle of scanline on average
+
+	if (etime)
+		*etime = ktime_get();
+
+	return true;
+}
+
 void mgag200_crtc_reset(struct drm_crtc *crtc)
 {
 	struct mgag200_crtc_state *mgag200_crtc_state;
Signature · 1 line
2.45.2
reviewer Reviewed-by on v4 7/7
#16 lore raw

Re: [PATCH v4 7/7] drm/mgag200: Implement struct drm_crtc_funcs.get_vblank_timestamp

Quoted 4 lines · Thomas Zimmermann

Implement struct drm_crtc_funcs.get_vblank_timestamp with the DRM helper drm_crtc_vblank_helper_get_vblank_timestamp() with its helper get_scanout_position. Read the scanout position from the MGAREG_VCOUNT register.

Thanks, it looks good to me.

Quoted 77 lines
---
  drivers/gpu/drm/mgag200/mgag200_drv.h  | 10 ++++++++--
  drivers/gpu/drm/mgag200/mgag200_mode.c | 25 +++++++++++++++++++++++++
  2 files changed, 33 insertions(+), 2 deletions(-)
drivers/gpu/drm/mgag200/mgag200_drv.h +8 -2
index f7b22b195016..acfa05335b09 100644
@@ -410,13 +410,18 @@ int mgag200_crtc_helper_atomic_check(struct drm_crtc *crtc, struct drm_atomic_st
  void mgag200_crtc_helper_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
  void mgag200_crtc_helper_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
  void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *old_state);
+bool mgag200_crtc_helper_get_scanout_position(struct drm_crtc *crtc, bool in_vblank_irq,
+					      int *vpos, int *hpos,
+					      ktime_t *stime, ktime_t *etime,
+					      const struct drm_display_mode *mode);
 
  #define MGAG200_CRTC_HELPER_FUNCS \
  	.mode_valid = mgag200_crtc_helper_mode_valid, \
  	.atomic_check = mgag200_crtc_helper_atomic_check, \
  	.atomic_flush = mgag200_crtc_helper_atomic_flush, \
  	.atomic_enable = mgag200_crtc_helper_atomic_enable, \
-	.atomic_disable = mgag200_crtc_helper_atomic_disable
+	.atomic_disable = mgag200_crtc_helper_atomic_disable, \
+	.get_scanout_position = mgag200_crtc_helper_get_scanout_position
 
  void mgag200_crtc_reset(struct drm_crtc *crtc);
  struct drm_crtc_state *mgag200_crtc_atomic_duplicate_state(struct drm_crtc *crtc);
@@ -432,7 +437,8 @@ void mgag200_crtc_disable_vblank(struct drm_crtc *crtc);
  	.atomic_duplicate_state = mgag200_crtc_atomic_duplicate_state, \
  	.atomic_destroy_state = mgag200_crtc_atomic_destroy_state, \
  	.enable_vblank = mgag200_crtc_enable_vblank, \
-	.disable_vblank = mgag200_crtc_disable_vblank
+	.disable_vblank = mgag200_crtc_disable_vblank, \
+	.get_vblank_timestamp = drm_crtc_vblank_helper_get_vblank_timestamp
 
  void mgag200_set_mode_regs(struct mga_device *mdev, const struct drm_display_mode *mode);
  void mgag200_set_format_regs(struct mga_device *mdev, const struct drm_format_info *format);
drivers/gpu/drm/mgag200/mgag200_mode.c +25 -0
index ec6fb1277d6e..fb03422c763b 100644
@@ -718,6 +718,31 @@ void mgag200_crtc_helper_atomic_disable(struct drm_crtc *crtc, struct drm_atomic
  		funcs->enable_vidrst(mdev);
  }
 
+bool mgag200_crtc_helper_get_scanout_position(struct drm_crtc *crtc, bool in_vblank_irq,
+					      int *vpos, int *hpos,
+					      ktime_t *stime, ktime_t *etime,
+					      const struct drm_display_mode *mode)
+{
+	struct mga_device *mdev = to_mga_device(crtc->dev);
+	u32 vcount;
+
+	if (stime)
+		*stime = ktime_get();
+
+	if (vpos) {
+		vcount = RREG32(MGAREG_VCOUNT);
+		*vpos = vcount & GENMASK(11, 0);
+	}
+
+	if (hpos)
+		*hpos = mode->htotal >> 1; // near middle of scanline on average
+
+	if (etime)
+		*etime = ktime_get();
+
+	return true;
+}
+
  void mgag200_crtc_reset(struct drm_crtc *crtc)
  {
  	struct mgag200_crtc_state *mgag200_crtc_state;