Skip to contentSkip to conversation

drm/i915: intel_backlight scale() math WA

PATCH In review U. Artie Eoff 1 participant 3 messages Sep 30, 2014

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

PATCH 1/2 drm/i915: intel_backlight scale() math WA

Improper truncated integer division in the scale() function causes actual_brightness != brightness. This (partial) work-around should be sufficient for a majority of use-cases, but it is by no means a complete solution.

TODO: Determine how best to scale "user" values to "hw" values, and vice-versa, when the ranges are of different sizes. That would be a buggy scenario even with this work-around.

The issue was introduced in the following (v3.17-rc1) commit:

6dda730 drm/i915: respect the VBT minimum backlight brightness

OTC-Jira: VIZ-4395

v2: (thanks to Chris Wilson) clarify commit message, use rounded division macro

v3: -DIV_ROUND_CLOSEST() fails to build with CONFIG_X86_32=y. (Jani)
-Use DIV_ROUND_CLOSEST_ULL() instead. (Damien)
-v1 and v2 originally authored by Joe Konno.

v4: Add reference to issue report in commit message.

---
 drivers/gpu/drm/i915/intel_panel.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +5 -3
index f17ada3..f7da913 100644
@@ -398,6 +398,9 @@ intel_panel_detect(struct drm_device *dev)
 	}
 }
 
+#define DIV_ROUND_CLOSEST_ULL(ll, d)	\
+({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
+
 /**
  * scale - scale values from one range to another
  *
@@ -419,9 +422,8 @@ static uint32_t scale(uint32_t source_val,
 	source_val = clamp(source_val, source_min, source_max);
 
 	/* avoid overflows */
-	target_val = (uint64_t)(source_val - source_min) *
-		(target_max - target_min);
-	do_div(target_val, source_max - source_min);
+	target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
+			(target_max - target_min), source_max - source_min);
 	target_val += target_min;
 
 	return target_val;
Signature · 1 line
1.9.3
#1 lore raw

PATCH v2 2/2 drm/i915: Move DIV_ROUND_CLOSEST_ULL macro to header

Move the duplicated DIV_ROUND_CLOSEST_ULL macro into the intel_drv.h header file so that it can be shared between intel_display.c and intel_panel.c.

OTC-Jira: VIZ-4395

v2: Add reference to issue report in commit message.

---
 drivers/gpu/drm/i915/intel_display.c | 3 ---
 drivers/gpu/drm/i915/intel_drv.h     | 3 +++
 drivers/gpu/drm/i915/intel_panel.c   | 3 ---
 3 files changed, 3 insertions(+), 6 deletions(-)
drivers/gpu/drm/i915/intel_display.c +0 -3
index 5b05ddb..db800f2 100644
@@ -73,9 +73,6 @@ static const uint32_t intel_cursor_formats[] = {
 	DRM_FORMAT_ARGB8888,
 };
 
-#define DIV_ROUND_CLOSEST_ULL(ll, d)	\
-({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
-
 static void intel_increase_pllclock(struct drm_device *dev,
 				    enum pipe pipe);
 static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
drivers/gpu/drm/i915/intel_drv.h +3 -0
index 1b72c15..8401ae3 100644
@@ -36,6 +36,9 @@
 #include <drm/drm_dp_mst_helper.h>
 #include <drm/drm_rect.h>
 
+#define DIV_ROUND_CLOSEST_ULL(ll, d)	\
+({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
+
 /**
  * _wait_for - magic (register) wait macro
  *
drivers/gpu/drm/i915/intel_panel.c +0 -3
index f7da913..fade0dd 100644
@@ -398,9 +398,6 @@ intel_panel_detect(struct drm_device *dev)
 	}
 }
 
-#define DIV_ROUND_CLOSEST_ULL(ll, d)	\
-({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
-
 /**
  * scale - scale values from one range to another
  *
Signature · 1 line
1.9.3
#2 lore raw
In reply to U. Artie Eoff

Ah snap... I forgot the v4 in the subject line :-/

Nonetheless, this is it.

Quoted 55 lines · U. Artie Eoff

Improper truncated integer division in the scale() function causes actual_brightness != brightness. This (partial) work-around should be sufficient for a majority of use-cases, but it is by no means a complete solution.

TODO: Determine how best to scale "user" values to "hw" values, and vice-versa, when the ranges are of different sizes. That would be a buggy scenario even with this work-around.

The issue was introduced in the following (v3.17-rc1) commit:

6dda730 drm/i915: respect the VBT minimum backlight brightness

OTC-Jira: VIZ-4395

v2: (thanks to Chris Wilson) clarify commit message, use rounded division macro

v3: -DIV_ROUND_CLOSEST() fails to build with CONFIG_X86_32=y. (Jani)
-Use DIV_ROUND_CLOSEST_ULL() instead. (Damien)
-v1 and v2 originally authored by Joe Konno.

v4: Add reference to issue report in commit message.

---
 drivers/gpu/drm/i915/intel_panel.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/gpu/drm/i915/intel_panel.c +5 -3
index f17ada3..f7da913 100644
@@ -398,6 +398,9 @@ intel_panel_detect(struct drm_device *dev)
 	}
 }
 
+#define DIV_ROUND_CLOSEST_ULL(ll, d)	\
+({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
+
 /**
  * scale - scale values from one range to another
  *
@@ -419,9 +422,8 @@ static uint32_t scale(uint32_t source_val,
 	source_val = clamp(source_val, source_min, source_max);
 
 	/* avoid overflows */
-	target_val = (uint64_t)(source_val - source_min) *
-		(target_max - target_min);
-	do_div(target_val, source_max - source_min);
+	target_val = DIV_ROUND_CLOSEST_ULL((uint64_t)(source_val - source_min) *
+			(target_max - target_min), source_max - source_min);
 	target_val += target_min;
 
 	return target_val;