Skip to contentSkip to conversation

drm/ast: astdp: fix loop timeout check

PATCH In review Dan Carpenter 2 participants 2 messages Aug 12, 2024

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 drm/ast: astdp: fix loop timeout check

This code has an issue because it loops until "i" is set to UINT_MAX but the test for failure assumes that "i" is set to zero. The result is that it will only print an error message if we succeed on the very last try. Reformat the loop to count forwards instead of backwards.

Fixes 2281475168d2 ("drm/ast: astdp: Perform link training during atomic_enable")

---
v3: V2 had the same bug but just without the always true if (i) statement.
Remove the final sleep.
v2: In V1, I introduced a bug where it would msleep(100) after failure
and that is a pointless thing to do. Also change the loop to a for loop.

---
 drivers/gpu/drm/ast/ast_dp.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/drm/ast/ast_dp.c +8 -7
index 5d07678b502c..ca022c287785 100644
@@ -146,18 +146,19 @@ void ast_dp_power_on_off(struct drm_device *dev, bool on)
 void ast_dp_link_training(struct ast_device *ast)
 {
 	struct drm_device *dev = &ast->base;
-	unsigned int i = 10;
+	int i;
 
-	while (i--) {
-		u8 vgacrdc = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdc);
+	for (i = 0; i < 10; i++) {
+		u8 vgacrdc;
 
-		if (vgacrdc & AST_IO_VGACRDC_LINK_SUCCESS)
-			break;
 		if (i)
 			msleep(100);
+
+		vgacrdc = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdc);
+		if (vgacrdc & AST_IO_VGACRDC_LINK_SUCCESS)
+			return;
 	}
-	if (!i)
-		drm_err(dev, "Link training failed\n");
+	drm_err(dev, "Link training failed\n");
 }
 
 void ast_dp_set_on_off(struct drm_device *dev, bool on)
Signature · 1 line
2.43.0
maintainer Reviewed-by on v3 1/1
#1 lore raw

Hi

Am 12.08.24 um 10:29 schrieb Dan Carpenter:

Quoted 7 lines

This code has an issue because it loops until "i" is set to UINT_MAX but the test for failure assumes that "i" is set to zero. The result is that it will only print an error message if we succeed on the very last try. Reformat the loop to count forwards instead of backwards.

Fixes 2281475168d2 ("drm/ast: astdp: Perform link training during atomic_enable")

Thanks a lot for the fix. I'll merge it during the week if no other comments come in.

Best regards
Thomas

Quoted 40 lines

---
v3: V2 had the same bug but just without the always true if (i) statement.
Remove the final sleep.
v2: In V1, I introduced a bug where it would msleep(100) after failure
and that is a pointless thing to do. Also change the loop to a for loop.

---
  drivers/gpu/drm/ast/ast_dp.c | 15 ++++++++-------
  1 file changed, 8 insertions(+), 7 deletions(-)
drivers/gpu/drm/ast/ast_dp.c +8 -7
index 5d07678b502c..ca022c287785 100644
@@ -146,18 +146,19 @@ void ast_dp_power_on_off(struct drm_device *dev, bool on)
  void ast_dp_link_training(struct ast_device *ast)
  {
  	struct drm_device *dev = &ast->base;
-	unsigned int i = 10;
+	int i;
 
-	while (i--) {
-		u8 vgacrdc = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdc);
+	for (i = 0; i < 10; i++) {
+		u8 vgacrdc;
 
-		if (vgacrdc & AST_IO_VGACRDC_LINK_SUCCESS)
-			break;
  		if (i)
  			msleep(100);
+
+		vgacrdc = ast_get_index_reg(ast, AST_IO_VGACRI, 0xdc);
+		if (vgacrdc & AST_IO_VGACRDC_LINK_SUCCESS)
+			return;
  	}
-	if (!i)
-		drm_err(dev, "Link training failed\n");
+	drm_err(dev, "Link training failed\n");
  }
 
  void ast_dp_set_on_off(struct drm_device *dev, bool on)
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)