Skip to content

Commit a463b26

Browse files
Bayan Zabihiyanalexdeucher
authored andcommitted
drm/amd/display: Fix frames_to_insert math
[Why] The math on deciding on how many "frames to insert" sometimes sent us over the max refresh rate. Also integer overflow can occur if we have high refresh rates. [How] Instead of clipping the frame duration such that it doesn’t go below the min, just remove a frame from the number of frames to insert. + Use unsigned long long for intermediate calculations to prevent integer overflow. Signed-off-by: Bayan Zabihiyan <bayan.zabihiyan@amd.com> Reviewed-by: Aric Cyr <Aric.Cyr@amd.com> Acked-by: Leo Li <sunpeng.li@amd.com> Signed-off-by: Alex Deucher <alexander.deucher@amd.com>
1 parent 1cbcfc9 commit a463b26

File tree

1 file changed

+17
-10
lines changed
  • drivers/gpu/drm/amd/display/modules/freesync

1 file changed

+17
-10
lines changed

drivers/gpu/drm/amd/display/modules/freesync/freesync.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,12 @@ static void apply_below_the_range(struct core_freesync *core_freesync,
435435
/* Either we've calculated the number of frames to insert,
436436
* or we need to insert min duration frames
437437
*/
438+
if (last_render_time_in_us / frames_to_insert <
439+
in_out_vrr->min_duration_in_us){
440+
frames_to_insert -= (frames_to_insert > 1) ?
441+
1 : 0;
442+
}
443+
438444
if (frames_to_insert > 0)
439445
inserted_frame_duration_in_us = last_render_time_in_us /
440446
frames_to_insert;
@@ -887,8 +893,8 @@ void mod_freesync_build_vrr_params(struct mod_freesync *mod_freesync,
887893
struct core_freesync *core_freesync = NULL;
888894
unsigned long long nominal_field_rate_in_uhz = 0;
889895
unsigned int refresh_range = 0;
890-
unsigned int min_refresh_in_uhz = 0;
891-
unsigned int max_refresh_in_uhz = 0;
896+
unsigned long long min_refresh_in_uhz = 0;
897+
unsigned long long max_refresh_in_uhz = 0;
892898

893899
if (mod_freesync == NULL)
894900
return;
@@ -915,7 +921,7 @@ void mod_freesync_build_vrr_params(struct mod_freesync *mod_freesync,
915921
min_refresh_in_uhz = nominal_field_rate_in_uhz;
916922

917923
if (!vrr_settings_require_update(core_freesync,
918-
in_config, min_refresh_in_uhz, max_refresh_in_uhz,
924+
in_config, (unsigned int)min_refresh_in_uhz, (unsigned int)max_refresh_in_uhz,
919925
in_out_vrr))
920926
return;
921927

@@ -931,15 +937,15 @@ void mod_freesync_build_vrr_params(struct mod_freesync *mod_freesync,
931937
return;
932938

933939
} else {
934-
in_out_vrr->min_refresh_in_uhz = min_refresh_in_uhz;
940+
in_out_vrr->min_refresh_in_uhz = (unsigned int)min_refresh_in_uhz;
935941
in_out_vrr->max_duration_in_us =
936942
calc_duration_in_us_from_refresh_in_uhz(
937-
min_refresh_in_uhz);
943+
(unsigned int)min_refresh_in_uhz);
938944

939-
in_out_vrr->max_refresh_in_uhz = max_refresh_in_uhz;
945+
in_out_vrr->max_refresh_in_uhz = (unsigned int)max_refresh_in_uhz;
940946
in_out_vrr->min_duration_in_us =
941947
calc_duration_in_us_from_refresh_in_uhz(
942-
max_refresh_in_uhz);
948+
(unsigned int)max_refresh_in_uhz);
943949

944950
refresh_range = in_out_vrr->max_refresh_in_uhz -
945951
in_out_vrr->min_refresh_in_uhz;
@@ -950,17 +956,18 @@ void mod_freesync_build_vrr_params(struct mod_freesync *mod_freesync,
950956
in_out_vrr->fixed.ramping_active = in_config->ramping;
951957

952958
in_out_vrr->btr.btr_enabled = in_config->btr;
959+
953960
if (in_out_vrr->max_refresh_in_uhz <
954961
2 * in_out_vrr->min_refresh_in_uhz)
955962
in_out_vrr->btr.btr_enabled = false;
963+
956964
in_out_vrr->btr.btr_active = false;
957965
in_out_vrr->btr.inserted_duration_in_us = 0;
958966
in_out_vrr->btr.frames_to_insert = 0;
959967
in_out_vrr->btr.frame_counter = 0;
960968
in_out_vrr->btr.mid_point_in_us =
961-
in_out_vrr->min_duration_in_us +
962-
(in_out_vrr->max_duration_in_us -
963-
in_out_vrr->min_duration_in_us) / 2;
969+
(in_out_vrr->min_duration_in_us +
970+
in_out_vrr->max_duration_in_us) / 2;
964971

965972
if (in_out_vrr->state == VRR_STATE_UNSUPPORTED) {
966973
in_out_vrr->adjust.v_total_min = stream->timing.v_total;

0 commit comments

Comments
 (0)