Skip to contentSkip to conversation

drm/i915: restore backlight precision when converting from opregion

PATCH Superseded Aaron Lu 4 participants 9 messages Apr 28, 2014 – May 15, 2014

Subsystems (2)
lore
Share & tools
lore

Aaron Lu

author
#0 lore raw

PATCH 1/1 drm/i915: restore backlight precision when converting from opregion

When we set backlight on behalf of ACPI opregion, we will convert the backlight value in the 0-255 range defined in opregion to the actual hardware level. Commit 22505b82a2 (drm/i915: avoid brightness overflow when doing scale) is meant to fix the overflow problem when doing the conversion, but it also caused a problem that the converted hardware level doesn't quite represent the intended value: say user wants maximum backlight level(255 in opregion's range), then we will calculate the actual hardware level to be: level = freq / max * level, where freq is the hardware's max backlight level(937 on an user's box), and max and level are all 255. The converted value should be 937 but the above calculation will yield 765.

To fix this issue, just use 64 bits to do the calculation to keep the precision and avoid overflow at the same time.

---
 drivers/gpu/drm/i915/intel_panel.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +1 -4
index a953b081ee38..bdd2f24b7a6b 100644
@@ -502,10 +502,7 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 
 	/* scale to hardware max, but be careful to not overflow */
 	freq = panel->backlight.max;
-	if (freq < max)
-		level = level * freq / max;
-	else
-		level = freq / max * level;
+	level = (u64)level * freq / max;
 
 	panel->backlight.level = level;
 	if (panel->backlight.device)
Signature · 1 line
1.9.0
#1 lore raw
Quoted 27 lines · Aaron Lu

When we set backlight on behalf of ACPI opregion, we will convert the backlight value in the 0-255 range defined in opregion to the actual hardware level. Commit 22505b82a2 (drm/i915: avoid brightness overflow when doing scale) is meant to fix the overflow problem when doing the conversion, but it also caused a problem that the converted hardware level doesn't quite represent the intended value: say user wants maximum backlight level(255 in opregion's range), then we will calculate the actual hardware level to be: level = freq / max * level, where freq is the hardware's max backlight level(937 on an user's box), and max and level are all 255. The converted value should be 937 but the above calculation will yield 765.

To fix this issue, just use 64 bits to do the calculation to keep the precision and avoid overflow at the same time.

---
 drivers/gpu/drm/i915/intel_panel.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +0 -0
index a953b081ee38..bdd2f24b7a6b 100644

drivers/gpu/drm/i915/intel_panel.c void intel_panel_set_backlight(struct intel_connector *connector, u32 level, · line 505

Quoted 8 lines
drivers/gpu/drm/i915/intel_panel.c +1 -4
@@ -502,10 +502,7 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 
 	/* scale to hardware max, but be careful to not overflow */
 	freq = panel->backlight.max;
-	if (freq < max)
-		level = level * freq / max;
-	else
-		level = freq / max * level;
+	level = (u64)level * freq / max;

64bit divisions won't compile on 32bit. You need one of the DO_DIV macros, or whatever they're called again. I pain, I know ;-)
-Daniel

Quoted 6 lines
drivers/gpu/drm/i915/intel_panel.c +0 -0
 	panel->backlight.level = level;
 	if (panel->backlight.device)
Signature · 1 line
1.9.0
Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
author
#2 lore raw

PATCH v2 1/1 drm/i915: restore backlight precision when converting from opregion

Quoted 2 lines · Daniel Vetter

64bit divisions won't compile on 32bit. You need one of the DO_DIV macros, or whatever they're called again. I pain, I know ;-)

Thanks for the correction, here is an updated patch :-)

From: Aaron Lu <[email protected]>
Date: Mon, 28 Apr 2014 11:02:52 +0800
Subject: [PATCH v2] drm/i915: restore backlight precision when converting from
ACPI

When we set backlight on behalf of ACPI opregion, we will convert the backlight value in the 0-255 range defined in opregion to the actual hardware level. Commit 22505b82a2 (drm/i915: avoid brightness overflow when doing scale) is meant to fix the overflow problem when doing the conversion, but it also caused a problem that the converted hardware level doesn't quite represent the intended value: say user wants maximum backlight level(255 in opregion's range), then we will calculate the actual hardware level to be: level = freq / max * level, where freq is the hardware's max backlight level(937 on an user's box), and max and level are all 255. The converted value should be 937 but the above calculation will yield 765.

To fix this issue, just use 64 bits to do the calculation to keep the precision and avoid overflow at the same time.

---
v2: use do_div as reminded by Daniel.

 drivers/gpu/drm/i915/intel_panel.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +4 -4
index a953b081ee38..8725917a3d0d 100644
@@ -492,6 +492,7 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 	enum pipe pipe = intel_get_pipe_from_connector(connector);
 	u32 freq;
 	unsigned long flags;
+	u64 n;
 
 	if (!panel->backlight.present || pipe == INVALID_PIPE)
 		return;
@@ -502,10 +503,9 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 
 	/* scale to hardware max, but be careful to not overflow */
 	freq = panel->backlight.max;
-	if (freq < max)
-		level = level * freq / max;
-	else
-		level = freq / max * level;
+	n = level * freq;
+	do_div(n, max);
+	level = n;
 
 	panel->backlight.level = level;
 	if (panel->backlight.device)
Signature · 1 line
1.9.0
#3 lore raw
Quoted 48 lines · Aaron Lu
Quoted 2 lines · Daniel Vetter

64bit divisions won't compile on 32bit. You need one of the DO_DIV macros, or whatever they're called again. I pain, I know ;-)

Thanks for the correction, here is an updated patch :-)

From: Aaron Lu <[email protected]>
Date: Mon, 28 Apr 2014 11:02:52 +0800
Subject: [PATCH v2] drm/i915: restore backlight precision when converting from
ACPI

When we set backlight on behalf of ACPI opregion, we will convert the backlight value in the 0-255 range defined in opregion to the actual hardware level. Commit 22505b82a2 (drm/i915: avoid brightness overflow when doing scale) is meant to fix the overflow problem when doing the conversion, but it also caused a problem that the converted hardware level doesn't quite represent the intended value: say user wants maximum backlight level(255 in opregion's range), then we will calculate the actual hardware level to be: level = freq / max * level, where freq is the hardware's max backlight level(937 on an user's box), and max and level are all 255. The converted value should be 937 but the above calculation will yield 765.

To fix this issue, just use 64 bits to do the calculation to keep the precision and avoid overflow at the same time.

---
v2: use do_div as reminded by Daniel.

 drivers/gpu/drm/i915/intel_panel.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +1 -0
index a953b081ee38..8725917a3d0d 100644
@@ -492,6 +492,7 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 	enum pipe pipe = intel_get_pipe_from_connector(connector);
 	u32 freq;
 	unsigned long flags;
+	u64 n;
 
 	if (!panel->backlight.present || pipe == INVALID_PIPE)
 		return;

drivers/gpu/drm/i915/intel_panel.c void intel_panel_set_backlight(struct intel_connector *connector, u32 level, · line 506

Quoted 8 lines
drivers/gpu/drm/i915/intel_panel.c +1 -4
@@ -502,10 +503,9 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 
 	/* scale to hardware max, but be careful to not overflow */
 	freq = panel->backlight.max;
-	if (freq < max)
-		level = level * freq / max;
-	else
-		level = freq / max * level;
+	n = level * freq;

32b * 32b = 32b

n = (u64)level * freq; to avoid overflow as you claim.

Also this still has the same rounding error as before. -Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
author
#4 lore raw
Quoted 60 lines · Chris Wilson
Quoted 48 lines · Aaron Lu
Quoted 2 lines · Daniel Vetter

64bit divisions won't compile on 32bit. You need one of the DO_DIV macros, or whatever they're called again. I pain, I know ;-)

Thanks for the correction, here is an updated patch :-)

From: Aaron Lu <[email protected]>
Date: Mon, 28 Apr 2014 11:02:52 +0800
Subject: [PATCH v2] drm/i915: restore backlight precision when converting from
ACPI

When we set backlight on behalf of ACPI opregion, we will convert the backlight value in the 0-255 range defined in opregion to the actual hardware level. Commit 22505b82a2 (drm/i915: avoid brightness overflow when doing scale) is meant to fix the overflow problem when doing the conversion, but it also caused a problem that the converted hardware level doesn't quite represent the intended value: say user wants maximum backlight level(255 in opregion's range), then we will calculate the actual hardware level to be: level = freq / max * level, where freq is the hardware's max backlight level(937 on an user's box), and max and level are all 255. The converted value should be 937 but the above calculation will yield 765.

To fix this issue, just use 64 bits to do the calculation to keep the precision and avoid overflow at the same time.

---
v2: use do_div as reminded by Daniel.

 drivers/gpu/drm/i915/intel_panel.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +1 -0
index a953b081ee38..8725917a3d0d 100644
@@ -492,6 +492,7 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 	enum pipe pipe = intel_get_pipe_from_connector(connector);
 	u32 freq;
 	unsigned long flags;
+	u64 n;
 
 	if (!panel->backlight.present || pipe == INVALID_PIPE)
 		return;

drivers/gpu/drm/i915/intel_panel.c void intel_panel_set_backlight(struct intel_connector *connector, u32 level, · line 506

Quoted 8 lines
drivers/gpu/drm/i915/intel_panel.c +1 -4
@@ -502,10 +503,9 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 
 	/* scale to hardware max, but be careful to not overflow */
 	freq = panel->backlight.max;
-	if (freq < max)
-		level = level * freq / max;
-	else
-		level = freq / max * level;
+	n = level * freq;

32b * 32b = 32b

Quoted 1 line

n = (u64)level * freq; to avoid overflow as you claim.

Ah...yes, my fault, thanks.

Quoted 2 lines

Also this still has the same rounding error as before.

I didn't get this, care to explain?

#5 lore raw
Quoted 4 lines · Aaron Lu
Quoted 1 line · Chris Wilson

Also this still has the same rounding error as before.

I didn't get this, care to explain?

The calculation you use, truncates, rather than say round to nearest, would is the same discrepancy in your changelog.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
author
#6 lore raw
Quoted 8 lines · Chris Wilson
Quoted 4 lines · Aaron Lu
Quoted 1 line · Chris Wilson

Also this still has the same rounding error as before.

I didn't get this, care to explain?

The calculation you use, truncates, rather than say round to nearest, would is the same discrepancy in your changelog.

This patch is meant to fix the problem that when user specify the max backlight level in acpi_video interface, we will get the hardware max level too in i915. With this patch, the hardware level we will get is:
level * freq / max = 255 * 937 / 255 = 937, which is correct.

For other values, the truncates is OK:
254 * 937 / 255 = 933
253 * 937 / 255 = 929
... ...

author
#7 lore raw

PATCH v3 1/1 drm/i915: restore backlight precision when converting from opregion

In reply to Chris Wilson
Quoted 3 lines · Chris Wilson

32b * 32b = 32b

n = (u64)level * freq; to avoid overflow as you claim.

Updated patch to fix this problem is here, thanks!

Quoted 1 line

From a0f41a92d949c814c203672ff7efe219a90ca6df Mon Sep 17 00:00:00 2001

From: Aaron Lu <[email protected]>
Date: Mon, 28 Apr 2014 11:02:52 +0800
Subject: [PATCH] drm/i915: restore backlight precision when converting from
ACPI

When we set backlight on behalf of ACPI opregion, we will convert the backlight value in the 0-255 range defined in opregion to the actual hardware level. Commit 22505b82a2 (drm/i915: avoid brightness overflow when doing scale) is meant to fix the overflow problem when doing the conversion, but it also caused a problem that the converted hardware level doesn't quite represent the intended value: say user wants maximum backlight level(255 in opregion's range), then we will calculate the actual hardware level to be: level = freq / max * level, where freq is the hardware's max backlight level(937 on an user's box), and max and level are all 255. The converted value should be 937 but the above calculation will yield 765.

To fix this issue, just use 64 bits to do the calculation to keep the precision and avoid overflow at the same time.

---
 drivers/gpu/drm/i915/intel_panel.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +4 -4
index 0eead16aeda7..cb8cfb7e0974 100644
@@ -492,6 +492,7 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 	enum pipe pipe = intel_get_pipe_from_connector(connector);
 	u32 freq;
 	unsigned long flags;
+	u64 n;
 
 	if (!panel->backlight.present || pipe == INVALID_PIPE)
 		return;
@@ -502,10 +503,9 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 
 	/* scale to hardware max, but be careful to not overflow */
 	freq = panel->backlight.max;
-	if (freq < max)
-		level = level * freq / max;
-	else
-		level = freq / max * level;
+	n = (u64)level * freq;
+	do_div(n, max);
+	level = n;
 
 	panel->backlight.level = level;
 	if (panel->backlight.device)
Signature · 1 line
1.9.0
maintainer Applied
#8 lore raw
Quoted 6 lines · Aaron Lu
Quoted 3 lines · Chris Wilson

32b * 32b = 32b

n = (u64)level * freq; to avoid overflow as you claim.

Updated patch to fix this problem is here, thanks!

Pushed to -fixes with Chris' IRC r-b, thanks for the patch and review.

BR,
Jani.

Quoted 63 lines

From a0f41a92d949c814c203672ff7efe219a90ca6df Mon Sep 17 00:00:00 2001
From: Aaron Lu <[email protected]>
Date: Mon, 28 Apr 2014 11:02:52 +0800
Subject: [PATCH] drm/i915: restore backlight precision when converting from
ACPI

When we set backlight on behalf of ACPI opregion, we will convert the backlight value in the 0-255 range defined in opregion to the actual hardware level. Commit 22505b82a2 (drm/i915: avoid brightness overflow when doing scale) is meant to fix the overflow problem when doing the conversion, but it also caused a problem that the converted hardware level doesn't quite represent the intended value: say user wants maximum backlight level(255 in opregion's range), then we will calculate the actual hardware level to be: level = freq / max * level, where freq is the hardware's max backlight level(937 on an user's box), and max and level are all 255. The converted value should be 937 but the above calculation will yield 765.

To fix this issue, just use 64 bits to do the calculation to keep the precision and avoid overflow at the same time.

---
 drivers/gpu/drm/i915/intel_panel.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +4 -4
index 0eead16aeda7..cb8cfb7e0974 100644
@@ -492,6 +492,7 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 	enum pipe pipe = intel_get_pipe_from_connector(connector);
 	u32 freq;
 	unsigned long flags;
+	u64 n;
 
 	if (!panel->backlight.present || pipe == INVALID_PIPE)
 		return;
@@ -502,10 +503,9 @@ void intel_panel_set_backlight(struct intel_connector *connector, u32 level,
 
 	/* scale to hardware max, but be careful to not overflow */
 	freq = panel->backlight.max;
-	if (freq < max)
-		level = level * freq / max;
-	else
-		level = freq / max * level;
+	n = (u64)level * freq;
+	do_div(n, max);
+	level = n;
 
 	panel->backlight.level = level;
 	if (panel->backlight.device)
Signature · 6 lines
1.9.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
Signature · 1 line
Jani Nikula, Intel Open Source Technology Center