14562236bSHarry Wentland /*
2da55037aSAustin Zheng  * Copyright 2016-2023 Advanced Micro Devices, Inc.
34562236bSHarry Wentland  *
44562236bSHarry Wentland  * Permission is hereby granted, free of charge, to any person obtaining a
54562236bSHarry Wentland  * copy of this software and associated documentation files (the "Software"),
64562236bSHarry Wentland  * to deal in the Software without restriction, including without limitation
74562236bSHarry Wentland  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84562236bSHarry Wentland  * and/or sell copies of the Software, and to permit persons to whom the
94562236bSHarry Wentland  * Software is furnished to do so, subject to the following conditions:
104562236bSHarry Wentland  *
114562236bSHarry Wentland  * The above copyright notice and this permission notice shall be included in
124562236bSHarry Wentland  * all copies or substantial portions of the Software.
134562236bSHarry Wentland  *
144562236bSHarry Wentland  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
154562236bSHarry Wentland  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
164562236bSHarry Wentland  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
174562236bSHarry Wentland  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
184562236bSHarry Wentland  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
194562236bSHarry Wentland  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
204562236bSHarry Wentland  * OTHER DEALINGS IN THE SOFTWARE.
214562236bSHarry Wentland  *
224562236bSHarry Wentland  * Authors: AMD
234562236bSHarry Wentland  *
244562236bSHarry Wentland  */
254562236bSHarry Wentland 
264562236bSHarry Wentland #include "dm_services.h"
274562236bSHarry Wentland #include "dc.h"
284562236bSHarry Wentland #include "mod_freesync.h"
294562236bSHarry Wentland #include "core_types.h"
304562236bSHarry Wentland 
314562236bSHarry Wentland #define MOD_FREESYNC_MAX_CONCURRENT_STREAMS  32
324562236bSHarry Wentland 
33ad339f69SJaehyun Chung #define MIN_REFRESH_RANGE 10
344562236bSHarry Wentland /* Refresh rate ramp at a fixed rate of 65 Hz/second */
354562236bSHarry Wentland #define STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME ((1000 / 60) * 65)
364562236bSHarry Wentland /* Number of elements in the render times cache array */
37a3e1737eSAnthony Koo #define RENDER_TIMES_MAX_COUNT 10
38ded6119eSAmanda Liu /* Threshold to exit/exit BTR (to avoid frequent enter-exits at the lower limit) */
39ded6119eSAmanda Liu #define BTR_MAX_MARGIN 2500
409070d18fSAric Cyr /* Threshold to change BTR multiplier (to avoid frequent changes) */
419070d18fSAric Cyr #define BTR_DRIFT_MARGIN 2000
42de801062SHarmanprit Tatla /* Threshold to exit fixed refresh rate */
435dff371aSAric Cyr #define FIXED_REFRESH_EXIT_MARGIN_IN_HZ 1
44d09fec0fSAnthony Koo /* Number of consecutive frames to check before entering/exiting fixed refresh */
45d09fec0fSAnthony Koo #define FIXED_REFRESH_ENTER_FRAME_COUNT 5
465dff371aSAric Cyr #define FIXED_REFRESH_EXIT_FRAME_COUNT 10
473fe5739dSAngus Wang /* Flip interval workaround constants */
483fe5739dSAngus Wang #define VSYNCS_BETWEEN_FLIP_THRESHOLD 2
493fe5739dSAngus Wang #define FREESYNC_CONSEC_FLIP_AFTER_VSYNC 5
503fe5739dSAngus Wang #define FREESYNC_VSYNC_TO_FLIP_DELTA_IN_US 500
51aacbed5bSpo-tchen #define MICRO_HZ_TO_HZ(x) (x / 1000000)
524562236bSHarry Wentland 
534562236bSHarry Wentland struct core_freesync {
544562236bSHarry Wentland 	struct mod_freesync public;
554562236bSHarry Wentland 	struct dc *dc;
564562236bSHarry Wentland };
574562236bSHarry Wentland 
584562236bSHarry Wentland #define MOD_FREESYNC_TO_CORE(mod_freesync)\
594562236bSHarry Wentland 		container_of(mod_freesync, struct core_freesync, public)
604562236bSHarry Wentland 
mod_freesync_create(struct dc * dc)614562236bSHarry Wentland struct mod_freesync *mod_freesync_create(struct dc *dc)
624562236bSHarry Wentland {
634562236bSHarry Wentland 	struct core_freesync *core_freesync =
642004f45eSHarry Wentland 			kzalloc(sizeof(struct core_freesync), GFP_KERNEL);
654562236bSHarry Wentland 
664562236bSHarry Wentland 	if (core_freesync == NULL)
674562236bSHarry Wentland 		goto fail_alloc_context;
684562236bSHarry Wentland 
694562236bSHarry Wentland 	if (dc == NULL)
704562236bSHarry Wentland 		goto fail_construct;
714562236bSHarry Wentland 
724562236bSHarry Wentland 	core_freesync->dc = dc;
734562236bSHarry Wentland 	return &core_freesync->public;
744562236bSHarry Wentland 
754562236bSHarry Wentland fail_construct:
762004f45eSHarry Wentland 	kfree(core_freesync);
774562236bSHarry Wentland 
784562236bSHarry Wentland fail_alloc_context:
794562236bSHarry Wentland 	return NULL;
804562236bSHarry Wentland }
814562236bSHarry Wentland 
mod_freesync_destroy(struct mod_freesync * mod_freesync)824562236bSHarry Wentland void mod_freesync_destroy(struct mod_freesync *mod_freesync)
834562236bSHarry Wentland {
8498e6436dSAnthony Koo 	struct core_freesync *core_freesync = NULL;
850c3c952dSMarcelo Mendes Spessoto Junior 
8698e6436dSAnthony Koo 	if (mod_freesync == NULL)
8798e6436dSAnthony Koo 		return;
8898e6436dSAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
892004f45eSHarry Wentland 	kfree(core_freesync);
904562236bSHarry Wentland }
914562236bSHarry Wentland 
92fe984cb3SFelipe #if 0 /* Unused currently */
9398e6436dSAnthony Koo static unsigned int calc_refresh_in_uhz_from_duration(
9498e6436dSAnthony Koo 		unsigned int duration_in_ns)
954562236bSHarry Wentland {
9698e6436dSAnthony Koo 	unsigned int refresh_in_uhz =
9798e6436dSAnthony Koo 			((unsigned int)(div64_u64((1000000000ULL * 1000000),
9898e6436dSAnthony Koo 					duration_in_ns)));
9998e6436dSAnthony Koo 	return refresh_in_uhz;
10098e6436dSAnthony Koo }
10198e6436dSAnthony Koo #endif
1024562236bSHarry Wentland 
calc_duration_in_us_from_refresh_in_uhz(unsigned int refresh_in_uhz)10398e6436dSAnthony Koo static unsigned int calc_duration_in_us_from_refresh_in_uhz(
10498e6436dSAnthony Koo 		unsigned int refresh_in_uhz)
1054562236bSHarry Wentland {
10698e6436dSAnthony Koo 	unsigned int duration_in_us =
10798e6436dSAnthony Koo 			((unsigned int)(div64_u64((1000000000ULL * 1000),
10898e6436dSAnthony Koo 					refresh_in_uhz)));
10998e6436dSAnthony Koo 	return duration_in_us;
1104562236bSHarry Wentland }
1114562236bSHarry Wentland 
calc_duration_in_us_from_v_total(const struct dc_stream_state * stream,const struct mod_vrr_params * in_vrr,unsigned int v_total)11298e6436dSAnthony Koo static unsigned int calc_duration_in_us_from_v_total(
11398e6436dSAnthony Koo 		const struct dc_stream_state *stream,
11498e6436dSAnthony Koo 		const struct mod_vrr_params *in_vrr,
11598e6436dSAnthony Koo 		unsigned int v_total)
1164562236bSHarry Wentland {
11798e6436dSAnthony Koo 	unsigned int duration_in_us =
11898e6436dSAnthony Koo 			(unsigned int)(div64_u64(((unsigned long long)(v_total)
119380604e2SKen Chalmers 				* 10000) * stream->timing.h_total,
120380604e2SKen Chalmers 					stream->timing.pix_clk_100hz));
1217a1c37e0SAnthony Koo 
12298e6436dSAnthony Koo 	return duration_in_us;
1234562236bSHarry Wentland }
1244562236bSHarry Wentland 
calc_max_hardware_v_total(const struct dc_stream_state * stream)125*a29997b7SDillon Varone static unsigned int calc_max_hardware_v_total(const struct dc_stream_state *stream)
126*a29997b7SDillon Varone {
127*a29997b7SDillon Varone 	unsigned int max_hw_v_total = stream->ctx->dc->caps.max_v_total;
128*a29997b7SDillon Varone 
129*a29997b7SDillon Varone 	if (stream->ctx->dc->caps.vtotal_limited_by_fp2) {
130*a29997b7SDillon Varone 		max_hw_v_total -= stream->timing.v_front_porch + 1;
131*a29997b7SDillon Varone 	}
132*a29997b7SDillon Varone 
133*a29997b7SDillon Varone 	return max_hw_v_total;
134*a29997b7SDillon Varone }
135*a29997b7SDillon Varone 
mod_freesync_calc_v_total_from_refresh(const struct dc_stream_state * stream,unsigned int refresh_in_uhz)13649c70eceSAlvin Lee unsigned int mod_freesync_calc_v_total_from_refresh(
13798e6436dSAnthony Koo 		const struct dc_stream_state *stream,
13898e6436dSAnthony Koo 		unsigned int refresh_in_uhz)
139a3e1737eSAnthony Koo {
140a501e22cSColin Ian King 	unsigned int v_total;
14198e6436dSAnthony Koo 	unsigned int frame_duration_in_ns;
142a3e1737eSAnthony Koo 
143d1fd30e5SCharlene Liu 	if (refresh_in_uhz == 0)
144d1fd30e5SCharlene Liu 		return stream->timing.v_total;
145d1fd30e5SCharlene Liu 
14698e6436dSAnthony Koo 	frame_duration_in_ns =
14798e6436dSAnthony Koo 			((unsigned int)(div64_u64((1000000000ULL * 1000000),
14898e6436dSAnthony Koo 					refresh_in_uhz)));
149a3e1737eSAnthony Koo 
150aacbed5bSpo-tchen 	if (MICRO_HZ_TO_HZ(refresh_in_uhz) <= stream->timing.min_refresh_in_uhz) {
151aacbed5bSpo-tchen 		/* When the target refresh rate is the minimum panel refresh rate,
152aacbed5bSpo-tchen 		 * round down the vtotal value to avoid stretching vblank over
153aacbed5bSpo-tchen 		 * panel's vtotal boundary.
154aacbed5bSpo-tchen 		 */
155aacbed5bSpo-tchen 		v_total = div64_u64(div64_u64(((unsigned long long)(
156aacbed5bSpo-tchen 				frame_duration_in_ns) * (stream->timing.pix_clk_100hz / 10)),
157aacbed5bSpo-tchen 				stream->timing.h_total), 1000000);
158aacbed5bSpo-tchen 	} else {
15998e6436dSAnthony Koo 		v_total = div64_u64(div64_u64(((unsigned long long)(
160380604e2SKen Chalmers 				frame_duration_in_ns) * (stream->timing.pix_clk_100hz / 10)),
161c03fca61SRobin Chen 				stream->timing.h_total) + 500000, 1000000);
162aacbed5bSpo-tchen 	}
1636c626ffbSYongqiang Sun 
16498e6436dSAnthony Koo 	/* v_total cannot be less than nominal */
16598e6436dSAnthony Koo 	if (v_total < stream->timing.v_total) {
16698e6436dSAnthony Koo 		ASSERT(v_total < stream->timing.v_total);
16798e6436dSAnthony Koo 		v_total = stream->timing.v_total;
1686c626ffbSYongqiang Sun 	}
1696c626ffbSYongqiang Sun 
17098e6436dSAnthony Koo 	return v_total;
1716c626ffbSYongqiang Sun }
17272ada5f7SEric Cook 
calc_v_total_from_duration(const struct dc_stream_state * stream,const struct mod_vrr_params * vrr,unsigned int duration_in_us)17398e6436dSAnthony Koo static unsigned int calc_v_total_from_duration(
17498e6436dSAnthony Koo 		const struct dc_stream_state *stream,
17598e6436dSAnthony Koo 		const struct mod_vrr_params *vrr,
17698e6436dSAnthony Koo 		unsigned int duration_in_us)
1774562236bSHarry Wentland {
17898e6436dSAnthony Koo 	unsigned int v_total = 0;
17998e6436dSAnthony Koo 
18098e6436dSAnthony Koo 	if (duration_in_us < vrr->min_duration_in_us)
18198e6436dSAnthony Koo 		duration_in_us = vrr->min_duration_in_us;
18298e6436dSAnthony Koo 
18398e6436dSAnthony Koo 	if (duration_in_us > vrr->max_duration_in_us)
18498e6436dSAnthony Koo 		duration_in_us = vrr->max_duration_in_us;
18598e6436dSAnthony Koo 
186f1b8479dSRodrigo Siqueira 	if (dc_is_hdmi_signal(stream->signal)) { // change for HDMI to comply with spec
18733df94e1SGuo, Bing 		uint32_t h_total_up_scaled;
18833df94e1SGuo, Bing 
18933df94e1SGuo, Bing 		h_total_up_scaled = stream->timing.h_total * 10000;
19033df94e1SGuo, Bing 		v_total = div_u64((unsigned long long)duration_in_us
19133df94e1SGuo, Bing 					* stream->timing.pix_clk_100hz + (h_total_up_scaled - 1),
192f1b8479dSRodrigo Siqueira 					h_total_up_scaled); //ceiling for MMax and MMin for MVRR
19333df94e1SGuo, Bing 	} else {
19498e6436dSAnthony Koo 		v_total = div64_u64(div64_u64(((unsigned long long)(
195380604e2SKen Chalmers 					duration_in_us) * (stream->timing.pix_clk_100hz / 10)),
19698e6436dSAnthony Koo 					stream->timing.h_total), 1000);
19733df94e1SGuo, Bing 	}
19898e6436dSAnthony Koo 
19998e6436dSAnthony Koo 	/* v_total cannot be less than nominal */
20098e6436dSAnthony Koo 	if (v_total < stream->timing.v_total) {
20198e6436dSAnthony Koo 		ASSERT(v_total < stream->timing.v_total);
20298e6436dSAnthony Koo 		v_total = stream->timing.v_total;
2034562236bSHarry Wentland 	}
2044562236bSHarry Wentland 
20598e6436dSAnthony Koo 	return v_total;
20698e6436dSAnthony Koo }
2074562236bSHarry Wentland 
update_v_total_for_static_ramp(struct core_freesync * core_freesync,const struct dc_stream_state * stream,struct mod_vrr_params * in_out_vrr)20898e6436dSAnthony Koo static void update_v_total_for_static_ramp(
20998e6436dSAnthony Koo 		struct core_freesync *core_freesync,
21098e6436dSAnthony Koo 		const struct dc_stream_state *stream,
21198e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
21298e6436dSAnthony Koo {
21398e6436dSAnthony Koo 	unsigned int v_total = 0;
21498e6436dSAnthony Koo 	unsigned int current_duration_in_us =
21598e6436dSAnthony Koo 			calc_duration_in_us_from_v_total(
21698e6436dSAnthony Koo 				stream, in_out_vrr,
21798e6436dSAnthony Koo 				in_out_vrr->adjust.v_total_max);
21898e6436dSAnthony Koo 	unsigned int target_duration_in_us =
21998e6436dSAnthony Koo 			calc_duration_in_us_from_refresh_in_uhz(
22098e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz);
22198e6436dSAnthony Koo 	bool ramp_direction_is_up = (current_duration_in_us >
22298e6436dSAnthony Koo 				target_duration_in_us) ? true : false;
2234562236bSHarry Wentland 
224fe984cb3SFelipe 	/* Calculate ratio between new and current frame duration with 3 digit */
2254562236bSHarry Wentland 	unsigned int frame_duration_ratio = div64_u64(1000000,
2264562236bSHarry Wentland 		(1000 +  div64_u64(((unsigned long long)(
2274562236bSHarry Wentland 		STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
22898e6436dSAnthony Koo 		current_duration_in_us),
22998e6436dSAnthony Koo 		1000000)));
2304562236bSHarry Wentland 
23198e6436dSAnthony Koo 	/* Calculate delta between new and current frame duration in us */
2324562236bSHarry Wentland 	unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
23398e6436dSAnthony Koo 		current_duration_in_us) *
2344562236bSHarry Wentland 		(1000 - frame_duration_ratio)), 1000);
2354562236bSHarry Wentland 
2364562236bSHarry Wentland 	/* Adjust frame duration delta based on ratio between current and
2374562236bSHarry Wentland 	 * standard frame duration (frame duration at 60 Hz refresh rate).
2384562236bSHarry Wentland 	 */
2394562236bSHarry Wentland 	unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
24098e6436dSAnthony Koo 		frame_duration_delta) * current_duration_in_us), 16666);
2414562236bSHarry Wentland 
2424562236bSHarry Wentland 	/* Going to a higher refresh rate (lower frame duration) */
24398e6436dSAnthony Koo 	if (ramp_direction_is_up) {
244fe984cb3SFelipe 		/* Reduce frame duration */
24598e6436dSAnthony Koo 		current_duration_in_us -= ramp_rate_interpolated;
2464562236bSHarry Wentland 
247fe984cb3SFelipe 		/* Adjust for frame duration below min */
24898e6436dSAnthony Koo 		if (current_duration_in_us <= target_duration_in_us) {
24998e6436dSAnthony Koo 			in_out_vrr->fixed.ramping_active = false;
25098e6436dSAnthony Koo 			in_out_vrr->fixed.ramping_done = true;
25198e6436dSAnthony Koo 			current_duration_in_us =
25298e6436dSAnthony Koo 				calc_duration_in_us_from_refresh_in_uhz(
25398e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz);
2544562236bSHarry Wentland 		}
2554562236bSHarry Wentland 	/* Going to a lower refresh rate (larger frame duration) */
2564562236bSHarry Wentland 	} else {
257fe984cb3SFelipe 		/* Increase frame duration */
25898e6436dSAnthony Koo 		current_duration_in_us += ramp_rate_interpolated;
2594562236bSHarry Wentland 
260fe984cb3SFelipe 		/* Adjust for frame duration above max */
26198e6436dSAnthony Koo 		if (current_duration_in_us >= target_duration_in_us) {
26298e6436dSAnthony Koo 			in_out_vrr->fixed.ramping_active = false;
26398e6436dSAnthony Koo 			in_out_vrr->fixed.ramping_done = true;
26498e6436dSAnthony Koo 			current_duration_in_us =
26598e6436dSAnthony Koo 				calc_duration_in_us_from_refresh_in_uhz(
26698e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz);
2674562236bSHarry Wentland 		}
2684562236bSHarry Wentland 	}
2694562236bSHarry Wentland 
270953c2901SAnthony Koo 	v_total = div64_u64(div64_u64(((unsigned long long)(
271380604e2SKen Chalmers 			current_duration_in_us) * (stream->timing.pix_clk_100hz / 10)),
272953c2901SAnthony Koo 				stream->timing.h_total), 1000);
2734562236bSHarry Wentland 
2748396745dSJaehyun Chung 	/* v_total cannot be less than nominal */
2758396745dSJaehyun Chung 	if (v_total < stream->timing.v_total)
2768396745dSJaehyun Chung 		v_total = stream->timing.v_total;
2778396745dSJaehyun Chung 
27898e6436dSAnthony Koo 	in_out_vrr->adjust.v_total_min = v_total;
27998e6436dSAnthony Koo 	in_out_vrr->adjust.v_total_max = v_total;
2804562236bSHarry Wentland }
2814562236bSHarry Wentland 
apply_below_the_range(struct core_freesync * core_freesync,const struct dc_stream_state * stream,unsigned int last_render_time_in_us,struct mod_vrr_params * in_out_vrr)2824562236bSHarry Wentland static void apply_below_the_range(struct core_freesync *core_freesync,
28398e6436dSAnthony Koo 		const struct dc_stream_state *stream,
28498e6436dSAnthony Koo 		unsigned int last_render_time_in_us,
28598e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
2864562236bSHarry Wentland {
2874562236bSHarry Wentland 	unsigned int inserted_frame_duration_in_us = 0;
2884562236bSHarry Wentland 	unsigned int mid_point_frames_ceil = 0;
2894562236bSHarry Wentland 	unsigned int mid_point_frames_floor = 0;
2904562236bSHarry Wentland 	unsigned int frame_time_in_us = 0;
2914562236bSHarry Wentland 	unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
2924562236bSHarry Wentland 	unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
2934562236bSHarry Wentland 	unsigned int frames_to_insert = 0;
2949070d18fSAric Cyr 	unsigned int delta_from_mid_point_delta_in_us;
295ded6119eSAmanda Liu 	unsigned int max_render_time_in_us =
296ded6119eSAmanda Liu 			in_out_vrr->max_duration_in_us - in_out_vrr->btr.margin_in_us;
2974562236bSHarry Wentland 
2984562236bSHarry Wentland 	/* Program BTR */
299ded6119eSAmanda Liu 	if ((last_render_time_in_us + in_out_vrr->btr.margin_in_us / 2) < max_render_time_in_us) {
30098e6436dSAnthony Koo 		/* Exit Below the Range */
30198e6436dSAnthony Koo 		if (in_out_vrr->btr.btr_active) {
30298e6436dSAnthony Koo 			in_out_vrr->btr.frame_counter = 0;
30398e6436dSAnthony Koo 			in_out_vrr->btr.btr_active = false;
30498e6436dSAnthony Koo 		}
305ded6119eSAmanda Liu 	} else if (last_render_time_in_us > (max_render_time_in_us + in_out_vrr->btr.margin_in_us / 2)) {
30698e6436dSAnthony Koo 		/* Enter Below the Range */
3070c3c952dSMarcelo Mendes Spessoto Junior 		if (!in_out_vrr->btr.btr_active)
30898e6436dSAnthony Koo 			in_out_vrr->btr.btr_active = true;
30998e6436dSAnthony Koo 	}
3104562236bSHarry Wentland 
3114562236bSHarry Wentland 	/* BTR set to "not active" so disengage */
31298e6436dSAnthony Koo 	if (!in_out_vrr->btr.btr_active) {
31398e6436dSAnthony Koo 		in_out_vrr->btr.inserted_duration_in_us = 0;
31498e6436dSAnthony Koo 		in_out_vrr->btr.frames_to_insert = 0;
31598e6436dSAnthony Koo 		in_out_vrr->btr.frame_counter = 0;
3164562236bSHarry Wentland 
3174562236bSHarry Wentland 		/* Restore FreeSync */
31898e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min =
31949c70eceSAlvin Lee 			mod_freesync_calc_v_total_from_refresh(stream,
32098e6436dSAnthony Koo 				in_out_vrr->max_refresh_in_uhz);
32198e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max =
32249c70eceSAlvin Lee 			mod_freesync_calc_v_total_from_refresh(stream,
32398e6436dSAnthony Koo 				in_out_vrr->min_refresh_in_uhz);
3244562236bSHarry Wentland 	/* BTR set to "active" so engage */
32598e6436dSAnthony Koo 	} else {
3264562236bSHarry Wentland 
3274562236bSHarry Wentland 		/* Calculate number of midPoint frames that could fit within
3284562236bSHarry Wentland 		 * the render time interval - take ceil of this value
3294562236bSHarry Wentland 		 */
3304562236bSHarry Wentland 		mid_point_frames_ceil = (last_render_time_in_us +
33198e6436dSAnthony Koo 				in_out_vrr->btr.mid_point_in_us - 1) /
33298e6436dSAnthony Koo 					in_out_vrr->btr.mid_point_in_us;
3334562236bSHarry Wentland 
3344562236bSHarry Wentland 		if (mid_point_frames_ceil > 0) {
3354562236bSHarry Wentland 			frame_time_in_us = last_render_time_in_us /
3364562236bSHarry Wentland 				mid_point_frames_ceil;
337d09fec0fSAnthony Koo 			delta_from_mid_point_in_us_1 =
33898e6436dSAnthony Koo 				(in_out_vrr->btr.mid_point_in_us >
3394562236bSHarry Wentland 				frame_time_in_us) ?
34098e6436dSAnthony Koo 				(in_out_vrr->btr.mid_point_in_us - frame_time_in_us) :
34198e6436dSAnthony Koo 				(frame_time_in_us - in_out_vrr->btr.mid_point_in_us);
3424562236bSHarry Wentland 		}
3434562236bSHarry Wentland 
3444562236bSHarry Wentland 		/* Calculate number of midPoint frames that could fit within
3454562236bSHarry Wentland 		 * the render time interval - take floor of this value
3464562236bSHarry Wentland 		 */
3474562236bSHarry Wentland 		mid_point_frames_floor = last_render_time_in_us /
34898e6436dSAnthony Koo 				in_out_vrr->btr.mid_point_in_us;
3494562236bSHarry Wentland 
3504562236bSHarry Wentland 		if (mid_point_frames_floor > 0) {
3514562236bSHarry Wentland 
3524562236bSHarry Wentland 			frame_time_in_us = last_render_time_in_us /
3534562236bSHarry Wentland 				mid_point_frames_floor;
354d09fec0fSAnthony Koo 			delta_from_mid_point_in_us_2 =
35598e6436dSAnthony Koo 				(in_out_vrr->btr.mid_point_in_us >
3564562236bSHarry Wentland 				frame_time_in_us) ?
35798e6436dSAnthony Koo 				(in_out_vrr->btr.mid_point_in_us - frame_time_in_us) :
35898e6436dSAnthony Koo 				(frame_time_in_us - in_out_vrr->btr.mid_point_in_us);
3594562236bSHarry Wentland 		}
3604562236bSHarry Wentland 
3614562236bSHarry Wentland 		/* Choose number of frames to insert based on how close it
3624562236bSHarry Wentland 		 * can get to the mid point of the variable range.
363e4ed4dbbSAnthony Koo 		 *  - Delta for CEIL: delta_from_mid_point_in_us_1
364e4ed4dbbSAnthony Koo 		 *  - Delta for FLOOR: delta_from_mid_point_in_us_2
3654562236bSHarry Wentland 		 */
366084f658eSHamza Mahfooz 		if (mid_point_frames_ceil &&
367084f658eSHamza Mahfooz 		    (last_render_time_in_us / mid_point_frames_ceil) <
368084f658eSHamza Mahfooz 		    in_out_vrr->min_duration_in_us) {
369e4ed4dbbSAnthony Koo 			/* Check for out of range.
370e4ed4dbbSAnthony Koo 			 * If using CEIL produces a value that is out of range,
371e4ed4dbbSAnthony Koo 			 * then we are forced to use FLOOR.
372e4ed4dbbSAnthony Koo 			 */
3734562236bSHarry Wentland 			frames_to_insert = mid_point_frames_floor;
374e4ed4dbbSAnthony Koo 		} else if (mid_point_frames_floor < 2) {
375e4ed4dbbSAnthony Koo 			/* Check if FLOOR would result in non-LFC. In this case
376e4ed4dbbSAnthony Koo 			 * choose to use CEIL
377e4ed4dbbSAnthony Koo 			 */
378e4ed4dbbSAnthony Koo 			frames_to_insert = mid_point_frames_ceil;
379e4ed4dbbSAnthony Koo 		} else if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2) {
380e4ed4dbbSAnthony Koo 			/* If choosing CEIL results in a frame duration that is
381e4ed4dbbSAnthony Koo 			 * closer to the mid point of the range.
382e4ed4dbbSAnthony Koo 			 * Choose CEIL
383e4ed4dbbSAnthony Koo 			 */
384e4ed4dbbSAnthony Koo 			frames_to_insert = mid_point_frames_ceil;
385e4ed4dbbSAnthony Koo 		} else {
386e4ed4dbbSAnthony Koo 			/* If choosing FLOOR results in a frame duration that is
387e4ed4dbbSAnthony Koo 			 * closer to the mid point of the range.
388e4ed4dbbSAnthony Koo 			 * Choose FLOOR
389e4ed4dbbSAnthony Koo 			 */
390e4ed4dbbSAnthony Koo 			frames_to_insert = mid_point_frames_floor;
3919070d18fSAric Cyr 		}
3929070d18fSAric Cyr 
3939070d18fSAric Cyr 		/* Prefer current frame multiplier when BTR is enabled unless it drifts
3949070d18fSAric Cyr 		 * too far from the midpoint
3959070d18fSAric Cyr 		 */
396e4ed4dbbSAnthony Koo 		if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2) {
397e4ed4dbbSAnthony Koo 			delta_from_mid_point_delta_in_us = delta_from_mid_point_in_us_2 -
398e4ed4dbbSAnthony Koo 					delta_from_mid_point_in_us_1;
399e4ed4dbbSAnthony Koo 		} else {
400e4ed4dbbSAnthony Koo 			delta_from_mid_point_delta_in_us = delta_from_mid_point_in_us_1 -
401e4ed4dbbSAnthony Koo 					delta_from_mid_point_in_us_2;
402e4ed4dbbSAnthony Koo 		}
4039070d18fSAric Cyr 		if (in_out_vrr->btr.frames_to_insert != 0 &&
4049070d18fSAric Cyr 				delta_from_mid_point_delta_in_us < BTR_DRIFT_MARGIN) {
4059070d18fSAric Cyr 			if (((last_render_time_in_us / in_out_vrr->btr.frames_to_insert) <
406ded6119eSAmanda Liu 					max_render_time_in_us) &&
4079070d18fSAric Cyr 				((last_render_time_in_us / in_out_vrr->btr.frames_to_insert) >
4089070d18fSAric Cyr 					in_out_vrr->min_duration_in_us))
4099070d18fSAric Cyr 				frames_to_insert = in_out_vrr->btr.frames_to_insert;
4109070d18fSAric Cyr 		}
4114562236bSHarry Wentland 
4124562236bSHarry Wentland 		/* Either we've calculated the number of frames to insert,
4134562236bSHarry Wentland 		 * or we need to insert min duration frames
4144562236bSHarry Wentland 		 */
415084f658eSHamza Mahfooz 		if (frames_to_insert &&
416084f658eSHamza Mahfooz 		    (last_render_time_in_us / frames_to_insert) <
417a463b263SBayan Zabihiyan 		    in_out_vrr->min_duration_in_us){
418a463b263SBayan Zabihiyan 			frames_to_insert -= (frames_to_insert > 1) ?
419a463b263SBayan Zabihiyan 					1 : 0;
420a463b263SBayan Zabihiyan 		}
421a463b263SBayan Zabihiyan 
4224562236bSHarry Wentland 		if (frames_to_insert > 0)
4234562236bSHarry Wentland 			inserted_frame_duration_in_us = last_render_time_in_us /
4244562236bSHarry Wentland 							frames_to_insert;
4254562236bSHarry Wentland 
426dc4a9049SMario Kleiner 		if (inserted_frame_duration_in_us < in_out_vrr->min_duration_in_us)
427dc4a9049SMario Kleiner 			inserted_frame_duration_in_us = in_out_vrr->min_duration_in_us;
4284562236bSHarry Wentland 
4294562236bSHarry Wentland 		/* Cache the calculated variables */
43098e6436dSAnthony Koo 		in_out_vrr->btr.inserted_duration_in_us =
4314562236bSHarry Wentland 			inserted_frame_duration_in_us;
43298e6436dSAnthony Koo 		in_out_vrr->btr.frames_to_insert = frames_to_insert;
43398e6436dSAnthony Koo 		in_out_vrr->btr.frame_counter = frames_to_insert;
4344562236bSHarry Wentland 	}
4354562236bSHarry Wentland }
4364562236bSHarry Wentland 
apply_fixed_refresh(struct core_freesync * core_freesync,const struct dc_stream_state * stream,unsigned int last_render_time_in_us,struct mod_vrr_params * in_out_vrr)4374562236bSHarry Wentland static void apply_fixed_refresh(struct core_freesync *core_freesync,
43898e6436dSAnthony Koo 		const struct dc_stream_state *stream,
43998e6436dSAnthony Koo 		unsigned int last_render_time_in_us,
44098e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
4414562236bSHarry Wentland {
44298e6436dSAnthony Koo 	bool update = false;
44398e6436dSAnthony Koo 	unsigned int max_render_time_in_us = in_out_vrr->max_duration_in_us;
4444562236bSHarry Wentland 
4455ea39850SHaiyi Zhou 	/* Compute the exit refresh rate and exit frame duration */
446de801062SHarmanprit Tatla 	unsigned int exit_refresh_rate_in_milli_hz = ((1000000000/max_render_time_in_us)
447de801062SHarmanprit Tatla 			+ (1000*FIXED_REFRESH_EXIT_MARGIN_IN_HZ));
448de801062SHarmanprit Tatla 	unsigned int exit_frame_duration_in_us = 1000000000/exit_refresh_rate_in_milli_hz;
449de801062SHarmanprit Tatla 
450de801062SHarmanprit Tatla 	if (last_render_time_in_us < exit_frame_duration_in_us) {
45198e6436dSAnthony Koo 		/* Exit Fixed Refresh mode */
45298e6436dSAnthony Koo 		if (in_out_vrr->fixed.fixed_active) {
45398e6436dSAnthony Koo 			in_out_vrr->fixed.frame_counter++;
45498e6436dSAnthony Koo 
45598e6436dSAnthony Koo 			if (in_out_vrr->fixed.frame_counter >
45698e6436dSAnthony Koo 					FIXED_REFRESH_EXIT_FRAME_COUNT) {
45798e6436dSAnthony Koo 				in_out_vrr->fixed.frame_counter = 0;
45898e6436dSAnthony Koo 				in_out_vrr->fixed.fixed_active = false;
45998e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz = 0;
46098e6436dSAnthony Koo 				update = true;
46198e6436dSAnthony Koo 			}
4625dff371aSAric Cyr 		} else
4635dff371aSAric Cyr 			in_out_vrr->fixed.frame_counter = 0;
46498e6436dSAnthony Koo 	} else if (last_render_time_in_us > max_render_time_in_us) {
46598e6436dSAnthony Koo 		/* Enter Fixed Refresh mode */
46698e6436dSAnthony Koo 		if (!in_out_vrr->fixed.fixed_active) {
46798e6436dSAnthony Koo 			in_out_vrr->fixed.frame_counter++;
46898e6436dSAnthony Koo 
46998e6436dSAnthony Koo 			if (in_out_vrr->fixed.frame_counter >
47098e6436dSAnthony Koo 					FIXED_REFRESH_ENTER_FRAME_COUNT) {
47198e6436dSAnthony Koo 				in_out_vrr->fixed.frame_counter = 0;
47298e6436dSAnthony Koo 				in_out_vrr->fixed.fixed_active = true;
47398e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz =
47498e6436dSAnthony Koo 						in_out_vrr->max_refresh_in_uhz;
47598e6436dSAnthony Koo 				update = true;
47698e6436dSAnthony Koo 			}
4775dff371aSAric Cyr 		} else
4785dff371aSAric Cyr 			in_out_vrr->fixed.frame_counter = 0;
47998e6436dSAnthony Koo 	}
48098e6436dSAnthony Koo 
48198e6436dSAnthony Koo 	if (update) {
48298e6436dSAnthony Koo 		if (in_out_vrr->fixed.fixed_active) {
48398e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
48449c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(
48598e6436dSAnthony Koo 				stream, in_out_vrr->max_refresh_in_uhz);
48698e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
48798e6436dSAnthony Koo 					in_out_vrr->adjust.v_total_min;
48898e6436dSAnthony Koo 		} else {
48998e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
49049c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
491ff6014d6SAnthony Koo 					in_out_vrr->max_refresh_in_uhz);
49298e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
49349c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
494ff6014d6SAnthony Koo 					in_out_vrr->min_refresh_in_uhz);
49598e6436dSAnthony Koo 		}
49698e6436dSAnthony Koo 	}
49798e6436dSAnthony Koo }
49898e6436dSAnthony Koo 
determine_flip_interval_workaround_req(struct mod_vrr_params * in_vrr,unsigned int curr_time_stamp_in_us)4993fe5739dSAngus Wang static void determine_flip_interval_workaround_req(struct mod_vrr_params *in_vrr,
5003fe5739dSAngus Wang 		unsigned int curr_time_stamp_in_us)
5013fe5739dSAngus Wang {
5023fe5739dSAngus Wang 	in_vrr->flip_interval.vsync_to_flip_in_us = curr_time_stamp_in_us -
5033fe5739dSAngus Wang 			in_vrr->flip_interval.v_update_timestamp_in_us;
5043fe5739dSAngus Wang 
5053fe5739dSAngus Wang 	/* Determine conditions for stopping workaround */
5063fe5739dSAngus Wang 	if (in_vrr->flip_interval.flip_interval_workaround_active &&
5073fe5739dSAngus Wang 			in_vrr->flip_interval.vsyncs_between_flip < VSYNCS_BETWEEN_FLIP_THRESHOLD &&
5083fe5739dSAngus Wang 			in_vrr->flip_interval.vsync_to_flip_in_us > FREESYNC_VSYNC_TO_FLIP_DELTA_IN_US) {
5093fe5739dSAngus Wang 		in_vrr->flip_interval.flip_interval_detect_counter = 0;
5103fe5739dSAngus Wang 		in_vrr->flip_interval.program_flip_interval_workaround = true;
5113fe5739dSAngus Wang 		in_vrr->flip_interval.flip_interval_workaround_active = false;
5123fe5739dSAngus Wang 	} else {
5133fe5739dSAngus Wang 		/* Determine conditions for starting workaround */
5143fe5739dSAngus Wang 		if (in_vrr->flip_interval.vsyncs_between_flip >= VSYNCS_BETWEEN_FLIP_THRESHOLD &&
5153fe5739dSAngus Wang 				in_vrr->flip_interval.vsync_to_flip_in_us < FREESYNC_VSYNC_TO_FLIP_DELTA_IN_US) {
5163fe5739dSAngus Wang 			/* Increase flip interval counter we have 2 vsyncs between flips and
5173fe5739dSAngus Wang 			 * vsync to flip interval is less than 500us
5183fe5739dSAngus Wang 			 */
5193fe5739dSAngus Wang 			in_vrr->flip_interval.flip_interval_detect_counter++;
5203fe5739dSAngus Wang 			if (in_vrr->flip_interval.flip_interval_detect_counter > FREESYNC_CONSEC_FLIP_AFTER_VSYNC) {
5213fe5739dSAngus Wang 				/* Start workaround if we detect 5 consecutive instances of the above case */
5223fe5739dSAngus Wang 				in_vrr->flip_interval.program_flip_interval_workaround = true;
5233fe5739dSAngus Wang 				in_vrr->flip_interval.flip_interval_workaround_active = true;
5243fe5739dSAngus Wang 			}
5253fe5739dSAngus Wang 		} else {
5263fe5739dSAngus Wang 			/* Reset the flip interval counter if we condition is no longer met */
5273fe5739dSAngus Wang 			in_vrr->flip_interval.flip_interval_detect_counter = 0;
5283fe5739dSAngus Wang 		}
5293fe5739dSAngus Wang 	}
5303fe5739dSAngus Wang 
5313fe5739dSAngus Wang 	in_vrr->flip_interval.vsyncs_between_flip = 0;
5323fe5739dSAngus Wang }
5333fe5739dSAngus Wang 
vrr_settings_require_update(struct core_freesync * core_freesync,struct mod_freesync_config * in_config,unsigned int min_refresh_in_uhz,unsigned int max_refresh_in_uhz,struct mod_vrr_params * in_vrr)53498e6436dSAnthony Koo static bool vrr_settings_require_update(struct core_freesync *core_freesync,
53598e6436dSAnthony Koo 		struct mod_freesync_config *in_config,
53698e6436dSAnthony Koo 		unsigned int min_refresh_in_uhz,
53798e6436dSAnthony Koo 		unsigned int max_refresh_in_uhz,
53898e6436dSAnthony Koo 		struct mod_vrr_params *in_vrr)
53998e6436dSAnthony Koo {
54098e6436dSAnthony Koo 	if (in_vrr->state != in_config->state) {
54198e6436dSAnthony Koo 		return true;
54298e6436dSAnthony Koo 	} else if (in_vrr->state == VRR_STATE_ACTIVE_FIXED &&
54398e6436dSAnthony Koo 			in_vrr->fixed.target_refresh_in_uhz !=
544d2bacc38SHaiyi Zhou 					in_config->fixed_refresh_in_uhz) {
54598e6436dSAnthony Koo 		return true;
54698e6436dSAnthony Koo 	} else if (in_vrr->min_refresh_in_uhz != min_refresh_in_uhz) {
54798e6436dSAnthony Koo 		return true;
54898e6436dSAnthony Koo 	} else if (in_vrr->max_refresh_in_uhz != max_refresh_in_uhz) {
54998e6436dSAnthony Koo 		return true;
55098e6436dSAnthony Koo 	}
55198e6436dSAnthony Koo 
55298e6436dSAnthony Koo 	return false;
55398e6436dSAnthony Koo }
55498e6436dSAnthony Koo 
build_vrr_infopacket_data_v1(const struct mod_vrr_params * vrr,struct dc_info_packet * infopacket,bool freesync_on_desktop)555d2bacc38SHaiyi Zhou static void build_vrr_infopacket_data_v1(const struct mod_vrr_params *vrr,
5564932d176Shvanzyll 		struct dc_info_packet *infopacket,
5574932d176Shvanzyll 		bool freesync_on_desktop)
558ca35899cSBayan Zabihiyan {
559a9f54ce3SAhmad Othman 	/* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
560a9f54ce3SAhmad Othman 	infopacket->sb[1] = 0x1A;
561ca35899cSBayan Zabihiyan 
562a9f54ce3SAhmad Othman 	/* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
563a9f54ce3SAhmad Othman 	infopacket->sb[2] = 0x00;
564e03868ecSReza Amini 
565a9f54ce3SAhmad Othman 	/* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
566a9f54ce3SAhmad Othman 	infopacket->sb[3] = 0x00;
567a9f54ce3SAhmad Othman 
568a9f54ce3SAhmad Othman 	/* PB4 = Reserved */
569a9f54ce3SAhmad Othman 
570a9f54ce3SAhmad Othman 	/* PB5 = Reserved */
571a9f54ce3SAhmad Othman 
572a9f54ce3SAhmad Othman 	/* PB6 = [Bits 7:3 = Reserved] */
573a9f54ce3SAhmad Othman 
574a9f54ce3SAhmad Othman 	/* PB6 = [Bit 0 = FreeSync Supported] */
575a9f54ce3SAhmad Othman 	if (vrr->state != VRR_STATE_UNSUPPORTED)
576a9f54ce3SAhmad Othman 		infopacket->sb[6] |= 0x01;
577a9f54ce3SAhmad Othman 
578a9f54ce3SAhmad Othman 	/* PB6 = [Bit 1 = FreeSync Enabled] */
579a9f54ce3SAhmad Othman 	if (vrr->state != VRR_STATE_DISABLED &&
580a9f54ce3SAhmad Othman 			vrr->state != VRR_STATE_UNSUPPORTED)
581a9f54ce3SAhmad Othman 		infopacket->sb[6] |= 0x02;
582a9f54ce3SAhmad Othman 
5834932d176Shvanzyll 	if (freesync_on_desktop) {
584a9f54ce3SAhmad Othman 		/* PB6 = [Bit 2 = FreeSync Active] */
5850774e08aSHarry VanZyllDeJong 		if (vrr->state != VRR_STATE_DISABLED &&
5860774e08aSHarry VanZyllDeJong 			vrr->state != VRR_STATE_UNSUPPORTED)
587a9f54ce3SAhmad Othman 			infopacket->sb[6] |= 0x04;
5884932d176Shvanzyll 	} else {
5894932d176Shvanzyll 		if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
5904932d176Shvanzyll 			vrr->state == VRR_STATE_ACTIVE_FIXED)
5914932d176Shvanzyll 			infopacket->sb[6] |= 0x04;
5924932d176Shvanzyll 	}
593a9f54ce3SAhmad Othman 
594d2bacc38SHaiyi Zhou 	// For v1 & 2 infoframes program nominal if non-fs mode, otherwise full range
595a9f54ce3SAhmad Othman 	/* PB7 = FreeSync Minimum refresh rate (Hz) */
596d2bacc38SHaiyi Zhou 	if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
597d2bacc38SHaiyi Zhou 			vrr->state == VRR_STATE_ACTIVE_FIXED) {
5983fc6376eSAric Cyr 		infopacket->sb[7] = (unsigned char)((vrr->min_refresh_in_uhz + 500000) / 1000000);
599d2bacc38SHaiyi Zhou 	} else {
600d2bacc38SHaiyi Zhou 		infopacket->sb[7] = (unsigned char)((vrr->max_refresh_in_uhz + 500000) / 1000000);
601d2bacc38SHaiyi Zhou 	}
602a9f54ce3SAhmad Othman 
603a9f54ce3SAhmad Othman 	/* PB8 = FreeSync Maximum refresh rate (Hz)
604a9f54ce3SAhmad Othman 	 * Note: We should never go above the field rate of the mode timing set.
605a9f54ce3SAhmad Othman 	 */
6063fc6376eSAric Cyr 	infopacket->sb[8] = (unsigned char)((vrr->max_refresh_in_uhz + 500000) / 1000000);
607d2bacc38SHaiyi Zhou }
608d2bacc38SHaiyi Zhou 
build_vrr_infopacket_data_v3(const struct mod_vrr_params * vrr,struct dc_info_packet * infopacket,bool freesync_on_desktop)609d2bacc38SHaiyi Zhou static void build_vrr_infopacket_data_v3(const struct mod_vrr_params *vrr,
610689008e1SDillon Varone 		struct dc_info_packet *infopacket,
611689008e1SDillon Varone 		bool freesync_on_desktop)
612d2bacc38SHaiyi Zhou {
6139bc41626SReza Amini 	unsigned int min_refresh;
6149bc41626SReza Amini 	unsigned int max_refresh;
6159bc41626SReza Amini 	unsigned int fixed_refresh;
6169bc41626SReza Amini 	unsigned int min_programmed;
6179bc41626SReza Amini 
618d2bacc38SHaiyi Zhou 	/* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
619d2bacc38SHaiyi Zhou 	infopacket->sb[1] = 0x1A;
620d2bacc38SHaiyi Zhou 
621d2bacc38SHaiyi Zhou 	/* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
622d2bacc38SHaiyi Zhou 	infopacket->sb[2] = 0x00;
623d2bacc38SHaiyi Zhou 
624d2bacc38SHaiyi Zhou 	/* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
625d2bacc38SHaiyi Zhou 	infopacket->sb[3] = 0x00;
626d2bacc38SHaiyi Zhou 
627d2bacc38SHaiyi Zhou 	/* PB4 = Reserved */
628d2bacc38SHaiyi Zhou 
629d2bacc38SHaiyi Zhou 	/* PB5 = Reserved */
630d2bacc38SHaiyi Zhou 
631d2bacc38SHaiyi Zhou 	/* PB6 = [Bits 7:3 = Reserved] */
632d2bacc38SHaiyi Zhou 
633d2bacc38SHaiyi Zhou 	/* PB6 = [Bit 0 = FreeSync Supported] */
634d2bacc38SHaiyi Zhou 	if (vrr->state != VRR_STATE_UNSUPPORTED)
635d2bacc38SHaiyi Zhou 		infopacket->sb[6] |= 0x01;
636d2bacc38SHaiyi Zhou 
637d2bacc38SHaiyi Zhou 	/* PB6 = [Bit 1 = FreeSync Enabled] */
638d2bacc38SHaiyi Zhou 	if (vrr->state != VRR_STATE_DISABLED &&
639d2bacc38SHaiyi Zhou 			vrr->state != VRR_STATE_UNSUPPORTED)
640d2bacc38SHaiyi Zhou 		infopacket->sb[6] |= 0x02;
641d2bacc38SHaiyi Zhou 
642d2bacc38SHaiyi Zhou 	/* PB6 = [Bit 2 = FreeSync Active] */
643689008e1SDillon Varone 	if (freesync_on_desktop) {
644689008e1SDillon Varone 		if (vrr->state != VRR_STATE_DISABLED &&
645689008e1SDillon Varone 			vrr->state != VRR_STATE_UNSUPPORTED)
646689008e1SDillon Varone 			infopacket->sb[6] |= 0x04;
647689008e1SDillon Varone 	} else {
648d2bacc38SHaiyi Zhou 		if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
649d2bacc38SHaiyi Zhou 			vrr->state == VRR_STATE_ACTIVE_FIXED)
650d2bacc38SHaiyi Zhou 			infopacket->sb[6] |= 0x04;
651689008e1SDillon Varone 	}
652d2bacc38SHaiyi Zhou 
6539bc41626SReza Amini 	min_refresh = (vrr->min_refresh_in_uhz + 500000) / 1000000;
6549bc41626SReza Amini 	max_refresh = (vrr->max_refresh_in_uhz + 500000) / 1000000;
6559bc41626SReza Amini 	fixed_refresh = (vrr->fixed_refresh_in_uhz + 500000) / 1000000;
6569bc41626SReza Amini 
6579bc41626SReza Amini 	min_programmed = (vrr->state == VRR_STATE_ACTIVE_FIXED) ? fixed_refresh :
6589bc41626SReza Amini 			(vrr->state == VRR_STATE_ACTIVE_VARIABLE) ? min_refresh :
659983bcb4cSAMD\ramini 			(vrr->state == VRR_STATE_INACTIVE) ? min_refresh :
6609bc41626SReza Amini 			max_refresh; // Non-fs case, program nominal range
6619bc41626SReza Amini 
662d2bacc38SHaiyi Zhou 	/* PB7 = FreeSync Minimum refresh rate (Hz) */
6639bc41626SReza Amini 	infopacket->sb[7] = min_programmed & 0xFF;
6649bc41626SReza Amini 
665d2bacc38SHaiyi Zhou 	/* PB8 = FreeSync Maximum refresh rate (Hz) */
66643693e85SMuhammad Ansari 	infopacket->sb[8] = max_refresh & 0xFF;
6679bc41626SReza Amini 
668983bcb4cSAMD\ramini 	/* PB11 : MSB FreeSync Minimum refresh rate [Hz] - bits 9:8 */
669983bcb4cSAMD\ramini 	infopacket->sb[11] = (min_programmed >> 8) & 0x03;
6709bc41626SReza Amini 
671983bcb4cSAMD\ramini 	/* PB12 : MSB FreeSync Maximum refresh rate [Hz] - bits 9:8 */
67243693e85SMuhammad Ansari 	infopacket->sb[12] = (max_refresh >> 8) & 0x03;
673983bcb4cSAMD\ramini 
674983bcb4cSAMD\ramini 	/* PB16 : Reserved bits 7:1, FixedRate bit 0 */
675983bcb4cSAMD\ramini 	infopacket->sb[16] = (vrr->state == VRR_STATE_ACTIVE_FIXED) ? 1 : 0;
676a9f54ce3SAhmad Othman }
677a9f54ce3SAhmad Othman 
build_vrr_infopacket_fs2_data(enum color_transfer_func app_tf,struct dc_info_packet * infopacket)678a9f54ce3SAhmad Othman static void build_vrr_infopacket_fs2_data(enum color_transfer_func app_tf,
679a9f54ce3SAhmad Othman 		struct dc_info_packet *infopacket)
680a9f54ce3SAhmad Othman {
681a9f54ce3SAhmad Othman 	if (app_tf != TRANSFER_FUNC_UNKNOWN) {
682a9f54ce3SAhmad Othman 		infopacket->valid = true;
683a9f54ce3SAhmad Othman 
684c21a764aSKrunoslav Kovac 		if (app_tf == TRANSFER_FUNC_PQ2084)
685c21a764aSKrunoslav Kovac 			infopacket->sb[9] |= 0x20; // PB9 = [Bit 5 = PQ EOTF Active]
686c21a764aSKrunoslav Kovac 		else {
687a9f54ce3SAhmad Othman 			infopacket->sb[6] |= 0x08;  // PB6 = [Bit 3 = Native Color Active]
68852b5432cSMike Hsieh 			if (app_tf == TRANSFER_FUNC_GAMMA_22)
689c21a764aSKrunoslav Kovac 				infopacket->sb[9] |= 0x04;  // PB9 = [Bit 2 = Gamma 2.2 EOTF Active]
690a9f54ce3SAhmad Othman 		}
691a9f54ce3SAhmad Othman 	}
692ca35899cSBayan Zabihiyan }
693ca35899cSBayan Zabihiyan 
build_vrr_infopacket_header_v1(enum signal_type signal,struct dc_info_packet * infopacket,unsigned int * payload_size)694c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_header_v1(enum signal_type signal,
695c2791297SSivapiriyanKumarasamy 		struct dc_info_packet *infopacket,
696c2791297SSivapiriyanKumarasamy 		unsigned int *payload_size)
69798e6436dSAnthony Koo {
698c2791297SSivapiriyanKumarasamy 	if (dc_is_hdmi_signal(signal)) {
6994562236bSHarry Wentland 
70098e6436dSAnthony Koo 		/* HEADER */
7014562236bSHarry Wentland 
70298e6436dSAnthony Koo 		/* HB0  = Packet Type = 0x83 (Source Product
70398e6436dSAnthony Koo 		 *	  Descriptor InfoFrame)
70498e6436dSAnthony Koo 		 */
70598e6436dSAnthony Koo 		infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
7064562236bSHarry Wentland 
70798e6436dSAnthony Koo 		/* HB1  = Version = 0x01 */
70898e6436dSAnthony Koo 		infopacket->hb1 = 0x01;
7094562236bSHarry Wentland 
71098e6436dSAnthony Koo 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x08] */
71198e6436dSAnthony Koo 		infopacket->hb2 = 0x08;
71298e6436dSAnthony Koo 
713c2791297SSivapiriyanKumarasamy 		*payload_size = 0x08;
71498e6436dSAnthony Koo 
715c2791297SSivapiriyanKumarasamy 	} else if (dc_is_dp_signal(signal)) {
71698e6436dSAnthony Koo 
71798e6436dSAnthony Koo 		/* HEADER */
71898e6436dSAnthony Koo 
71998e6436dSAnthony Koo 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
72098e6436dSAnthony Koo 		 *	  when used to associate audio related info packets
72198e6436dSAnthony Koo 		 */
72298e6436dSAnthony Koo 		infopacket->hb0 = 0x00;
72398e6436dSAnthony Koo 
72498e6436dSAnthony Koo 		/* HB1  = Packet Type = 0x83 (Source Product
72598e6436dSAnthony Koo 		 *	  Descriptor InfoFrame)
72698e6436dSAnthony Koo 		 */
72798e6436dSAnthony Koo 		infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
72898e6436dSAnthony Koo 
72998e6436dSAnthony Koo 		/* HB2  = [Bits 7:0 = Least significant eight bits -
73098e6436dSAnthony Koo 		 *	  For INFOFRAME, the value must be 1Bh]
73198e6436dSAnthony Koo 		 */
73298e6436dSAnthony Koo 		infopacket->hb2 = 0x1B;
73398e6436dSAnthony Koo 
73498e6436dSAnthony Koo 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x1]
73598e6436dSAnthony Koo 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
73698e6436dSAnthony Koo 		 */
73798e6436dSAnthony Koo 		infopacket->hb3 = 0x04;
73898e6436dSAnthony Koo 
739c2791297SSivapiriyanKumarasamy 		*payload_size = 0x1B;
740c2791297SSivapiriyanKumarasamy 	}
7414562236bSHarry Wentland }
7424562236bSHarry Wentland 
build_vrr_infopacket_header_v2(enum signal_type signal,struct dc_info_packet * infopacket,unsigned int * payload_size)743c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_header_v2(enum signal_type signal,
744c2791297SSivapiriyanKumarasamy 		struct dc_info_packet *infopacket,
745c2791297SSivapiriyanKumarasamy 		unsigned int *payload_size)
746c2791297SSivapiriyanKumarasamy {
747c2791297SSivapiriyanKumarasamy 	if (dc_is_hdmi_signal(signal)) {
748c2791297SSivapiriyanKumarasamy 
749c2791297SSivapiriyanKumarasamy 		/* HEADER */
750c2791297SSivapiriyanKumarasamy 
751c2791297SSivapiriyanKumarasamy 		/* HB0  = Packet Type = 0x83 (Source Product
752c2791297SSivapiriyanKumarasamy 		 *	  Descriptor InfoFrame)
753c2791297SSivapiriyanKumarasamy 		 */
754c2791297SSivapiriyanKumarasamy 		infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
755c2791297SSivapiriyanKumarasamy 
756c2791297SSivapiriyanKumarasamy 		/* HB1  = Version = 0x02 */
757c2791297SSivapiriyanKumarasamy 		infopacket->hb1 = 0x02;
758c2791297SSivapiriyanKumarasamy 
759c2791297SSivapiriyanKumarasamy 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x09] */
760c2791297SSivapiriyanKumarasamy 		infopacket->hb2 = 0x09;
761c2791297SSivapiriyanKumarasamy 
76205911836SLeo Ma 		*payload_size = 0x09;
763c2791297SSivapiriyanKumarasamy 	} else if (dc_is_dp_signal(signal)) {
764c2791297SSivapiriyanKumarasamy 
765c2791297SSivapiriyanKumarasamy 		/* HEADER */
766c2791297SSivapiriyanKumarasamy 
767c2791297SSivapiriyanKumarasamy 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
768c2791297SSivapiriyanKumarasamy 		 *	  when used to associate audio related info packets
769c2791297SSivapiriyanKumarasamy 		 */
770c2791297SSivapiriyanKumarasamy 		infopacket->hb0 = 0x00;
771c2791297SSivapiriyanKumarasamy 
772c2791297SSivapiriyanKumarasamy 		/* HB1  = Packet Type = 0x83 (Source Product
773c2791297SSivapiriyanKumarasamy 		 *	  Descriptor InfoFrame)
774c2791297SSivapiriyanKumarasamy 		 */
775c2791297SSivapiriyanKumarasamy 		infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
776c2791297SSivapiriyanKumarasamy 
777c2791297SSivapiriyanKumarasamy 		/* HB2  = [Bits 7:0 = Least significant eight bits -
778c2791297SSivapiriyanKumarasamy 		 *	  For INFOFRAME, the value must be 1Bh]
779c2791297SSivapiriyanKumarasamy 		 */
780c2791297SSivapiriyanKumarasamy 		infopacket->hb2 = 0x1B;
781c2791297SSivapiriyanKumarasamy 
782c2791297SSivapiriyanKumarasamy 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x2]
783c2791297SSivapiriyanKumarasamy 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
784c2791297SSivapiriyanKumarasamy 		 */
785c2791297SSivapiriyanKumarasamy 		infopacket->hb3 = 0x08;
786c2791297SSivapiriyanKumarasamy 
787c2791297SSivapiriyanKumarasamy 		*payload_size = 0x1B;
788c2791297SSivapiriyanKumarasamy 	}
789c2791297SSivapiriyanKumarasamy }
790c2791297SSivapiriyanKumarasamy 
build_vrr_infopacket_header_v3(enum signal_type signal,struct dc_info_packet * infopacket,unsigned int * payload_size)7919bc41626SReza Amini static void build_vrr_infopacket_header_v3(enum signal_type signal,
7929bc41626SReza Amini 		struct dc_info_packet *infopacket,
7939bc41626SReza Amini 		unsigned int *payload_size)
7949bc41626SReza Amini {
7959bc41626SReza Amini 	unsigned char version;
7969bc41626SReza Amini 
7979bc41626SReza Amini 	version = 3;
7989bc41626SReza Amini 	if (dc_is_hdmi_signal(signal)) {
7999bc41626SReza Amini 
8009bc41626SReza Amini 		/* HEADER */
8019bc41626SReza Amini 
8029bc41626SReza Amini 		/* HB0  = Packet Type = 0x83 (Source Product
8039bc41626SReza Amini 		 *	  Descriptor InfoFrame)
8049bc41626SReza Amini 		 */
8059bc41626SReza Amini 		infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
8069bc41626SReza Amini 
8079bc41626SReza Amini 		/* HB1  = Version = 0x03 */
8089bc41626SReza Amini 		infopacket->hb1 = version;
8099bc41626SReza Amini 
8109bc41626SReza Amini 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length] */
81105911836SLeo Ma 		infopacket->hb2 = 0x10;
8129bc41626SReza Amini 
81305911836SLeo Ma 		*payload_size = 0x10;
8149bc41626SReza Amini 	} else if (dc_is_dp_signal(signal)) {
8159bc41626SReza Amini 
8169bc41626SReza Amini 		/* HEADER */
8179bc41626SReza Amini 
8189bc41626SReza Amini 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
8199bc41626SReza Amini 		 *	  when used to associate audio related info packets
8209bc41626SReza Amini 		 */
8219bc41626SReza Amini 		infopacket->hb0 = 0x00;
8229bc41626SReza Amini 
8239bc41626SReza Amini 		/* HB1  = Packet Type = 0x83 (Source Product
8249bc41626SReza Amini 		 *	  Descriptor InfoFrame)
8259bc41626SReza Amini 		 */
8269bc41626SReza Amini 		infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
8279bc41626SReza Amini 
8289bc41626SReza Amini 		/* HB2  = [Bits 7:0 = Least significant eight bits -
8299bc41626SReza Amini 		 *	  For INFOFRAME, the value must be 1Bh]
8309bc41626SReza Amini 		 */
8319bc41626SReza Amini 		infopacket->hb2 = 0x1B;
8329bc41626SReza Amini 
8339bc41626SReza Amini 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x2]
8349bc41626SReza Amini 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
8359bc41626SReza Amini 		 */
8369bc41626SReza Amini 
8379bc41626SReza Amini 		infopacket->hb3 = (version & 0x3F) << 2;
8389bc41626SReza Amini 
8399bc41626SReza Amini 		*payload_size = 0x1B;
8409bc41626SReza Amini 	}
8419bc41626SReza Amini }
8429bc41626SReza Amini 
build_vrr_infopacket_checksum(unsigned int * payload_size,struct dc_info_packet * infopacket)843c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_checksum(unsigned int *payload_size,
844c2791297SSivapiriyanKumarasamy 		struct dc_info_packet *infopacket)
845c2791297SSivapiriyanKumarasamy {
84698e6436dSAnthony Koo 	/* Calculate checksum */
847c2791297SSivapiriyanKumarasamy 	unsigned int idx = 0;
848c2791297SSivapiriyanKumarasamy 	unsigned char checksum = 0;
849c2791297SSivapiriyanKumarasamy 
85098e6436dSAnthony Koo 	checksum += infopacket->hb0;
85198e6436dSAnthony Koo 	checksum += infopacket->hb1;
85298e6436dSAnthony Koo 	checksum += infopacket->hb2;
85398e6436dSAnthony Koo 	checksum += infopacket->hb3;
85498e6436dSAnthony Koo 
855c2791297SSivapiriyanKumarasamy 	for (idx = 1; idx <= *payload_size; idx++)
85698e6436dSAnthony Koo 		checksum += infopacket->sb[idx];
85798e6436dSAnthony Koo 
85898e6436dSAnthony Koo 	/* PB0 = Checksum (one byte complement) */
85998e6436dSAnthony Koo 	infopacket->sb[0] = (unsigned char)(0x100 - checksum);
86098e6436dSAnthony Koo 
86198e6436dSAnthony Koo 	infopacket->valid = true;
86298e6436dSAnthony Koo }
86398e6436dSAnthony Koo 
build_vrr_infopacket_v1(enum signal_type signal,const struct mod_vrr_params * vrr,struct dc_info_packet * infopacket,bool freesync_on_desktop)864c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_v1(enum signal_type signal,
865c2791297SSivapiriyanKumarasamy 		const struct mod_vrr_params *vrr,
8664932d176Shvanzyll 		struct dc_info_packet *infopacket,
8674932d176Shvanzyll 		bool freesync_on_desktop)
868c2791297SSivapiriyanKumarasamy {
869c2791297SSivapiriyanKumarasamy 	/* SPD info packet for FreeSync */
870c2791297SSivapiriyanKumarasamy 	unsigned int payload_size = 0;
871c2791297SSivapiriyanKumarasamy 
872c2791297SSivapiriyanKumarasamy 	build_vrr_infopacket_header_v1(signal, infopacket, &payload_size);
8734932d176Shvanzyll 	build_vrr_infopacket_data_v1(vrr, infopacket, freesync_on_desktop);
874c2791297SSivapiriyanKumarasamy 	build_vrr_infopacket_checksum(&payload_size, infopacket);
875c2791297SSivapiriyanKumarasamy 
876c2791297SSivapiriyanKumarasamy 	infopacket->valid = true;
877c2791297SSivapiriyanKumarasamy }
878c2791297SSivapiriyanKumarasamy 
build_vrr_infopacket_v2(enum signal_type signal,const struct mod_vrr_params * vrr,enum color_transfer_func app_tf,struct dc_info_packet * infopacket,bool freesync_on_desktop)879c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_v2(enum signal_type signal,
880c2791297SSivapiriyanKumarasamy 		const struct mod_vrr_params *vrr,
881672e78caSNathan Chancellor 		enum color_transfer_func app_tf,
8824932d176Shvanzyll 		struct dc_info_packet *infopacket,
8834932d176Shvanzyll 		bool freesync_on_desktop)
884c2791297SSivapiriyanKumarasamy {
885c2791297SSivapiriyanKumarasamy 	unsigned int payload_size = 0;
886c2791297SSivapiriyanKumarasamy 
887c2791297SSivapiriyanKumarasamy 	build_vrr_infopacket_header_v2(signal, infopacket, &payload_size);
8884932d176Shvanzyll 	build_vrr_infopacket_data_v1(vrr, infopacket, freesync_on_desktop);
889d2bacc38SHaiyi Zhou 
890d2bacc38SHaiyi Zhou 	build_vrr_infopacket_fs2_data(app_tf, infopacket);
891d2bacc38SHaiyi Zhou 
892d2bacc38SHaiyi Zhou 	build_vrr_infopacket_checksum(&payload_size, infopacket);
893d2bacc38SHaiyi Zhou 
894d2bacc38SHaiyi Zhou 	infopacket->valid = true;
895d2bacc38SHaiyi Zhou }
896d2bacc38SHaiyi Zhou 
build_vrr_infopacket_v3(enum signal_type signal,const struct mod_vrr_params * vrr,enum color_transfer_func app_tf,struct dc_info_packet * infopacket,bool freesync_on_desktop)897d2bacc38SHaiyi Zhou static void build_vrr_infopacket_v3(enum signal_type signal,
898d2bacc38SHaiyi Zhou 		const struct mod_vrr_params *vrr,
899d2bacc38SHaiyi Zhou 		enum color_transfer_func app_tf,
900689008e1SDillon Varone 		struct dc_info_packet *infopacket,
901689008e1SDillon Varone 		bool freesync_on_desktop)
902d2bacc38SHaiyi Zhou {
903d2bacc38SHaiyi Zhou 	unsigned int payload_size = 0;
904d2bacc38SHaiyi Zhou 
9059bc41626SReza Amini 	build_vrr_infopacket_header_v3(signal, infopacket, &payload_size);
906689008e1SDillon Varone 	build_vrr_infopacket_data_v3(vrr, infopacket, freesync_on_desktop);
907c2791297SSivapiriyanKumarasamy 
908672e78caSNathan Chancellor 	build_vrr_infopacket_fs2_data(app_tf, infopacket);
909c2791297SSivapiriyanKumarasamy 
910c2791297SSivapiriyanKumarasamy 	build_vrr_infopacket_checksum(&payload_size, infopacket);
911c2791297SSivapiriyanKumarasamy 
912c2791297SSivapiriyanKumarasamy 	infopacket->valid = true;
913c2791297SSivapiriyanKumarasamy }
914c2791297SSivapiriyanKumarasamy 
build_vrr_infopacket_sdp_v1_3(enum vrr_packet_type packet_type,struct dc_info_packet * infopacket)9154cda3243SMax.Tseng static void build_vrr_infopacket_sdp_v1_3(enum vrr_packet_type packet_type,
9164cda3243SMax.Tseng 										struct dc_info_packet *infopacket)
9174cda3243SMax.Tseng {
9184cda3243SMax.Tseng 	uint8_t idx = 0, size = 0;
9194cda3243SMax.Tseng 
9204cda3243SMax.Tseng 	size = ((packet_type == PACKET_TYPE_FS_V1) ? 0x08 :
9214cda3243SMax.Tseng 			(packet_type == PACKET_TYPE_FS_V3) ? 0x10 :
9224cda3243SMax.Tseng 												0x09);
9234cda3243SMax.Tseng 
9244cda3243SMax.Tseng 	for (idx = infopacket->hb2; idx > 1; idx--) // Data Byte Count: 0x1B
9254cda3243SMax.Tseng 		infopacket->sb[idx] = infopacket->sb[idx-1];
9264cda3243SMax.Tseng 
9274cda3243SMax.Tseng 	infopacket->sb[1] = size;                         // Length
9284cda3243SMax.Tseng 	infopacket->sb[0] = (infopacket->hb3 >> 2) & 0x3F;//Version
9294cda3243SMax.Tseng 	infopacket->hb3   = (0x13 << 2);                  // Header,SDP 1.3
9304cda3243SMax.Tseng 	infopacket->hb2   = 0x1D;
9314cda3243SMax.Tseng }
9324cda3243SMax.Tseng 
mod_freesync_build_vrr_infopacket(struct mod_freesync * mod_freesync,const struct dc_stream_state * stream,const struct mod_vrr_params * vrr,enum vrr_packet_type packet_type,enum color_transfer_func app_tf,struct dc_info_packet * infopacket,bool pack_sdp_v1_3)933c2791297SSivapiriyanKumarasamy void mod_freesync_build_vrr_infopacket(struct mod_freesync *mod_freesync,
934c2791297SSivapiriyanKumarasamy 		const struct dc_stream_state *stream,
935c2791297SSivapiriyanKumarasamy 		const struct mod_vrr_params *vrr,
936c2791297SSivapiriyanKumarasamy 		enum vrr_packet_type packet_type,
937672e78caSNathan Chancellor 		enum color_transfer_func app_tf,
9384cda3243SMax.Tseng 		struct dc_info_packet *infopacket,
9394cda3243SMax.Tseng 		bool pack_sdp_v1_3)
940c2791297SSivapiriyanKumarasamy {
941ca35899cSBayan Zabihiyan 	/* SPD info packet for FreeSync
942ca35899cSBayan Zabihiyan 	 * VTEM info packet for HdmiVRR
943ca35899cSBayan Zabihiyan 	 * Check if Freesync is supported. Return if false. If true,
944c2791297SSivapiriyanKumarasamy 	 * set the corresponding bit in the info packet
945c2791297SSivapiriyanKumarasamy 	 */
946486b7680SJaehyun Chung 	if (!vrr->send_info_frame)
947c2791297SSivapiriyanKumarasamy 		return;
948c2791297SSivapiriyanKumarasamy 
949c2791297SSivapiriyanKumarasamy 	switch (packet_type) {
950d2bacc38SHaiyi Zhou 	case PACKET_TYPE_FS_V3:
951627441f5SAric Cyr 		build_vrr_infopacket_v3(stream->signal, vrr, app_tf, infopacket, stream->freesync_on_desktop);
952d2bacc38SHaiyi Zhou 		break;
953d2bacc38SHaiyi Zhou 	case PACKET_TYPE_FS_V2:
954627441f5SAric Cyr 		build_vrr_infopacket_v2(stream->signal, vrr, app_tf, infopacket, stream->freesync_on_desktop);
955c2791297SSivapiriyanKumarasamy 		break;
956ecd0136bSHarmanprit Tatla 	case PACKET_TYPE_VRR:
957d2bacc38SHaiyi Zhou 	case PACKET_TYPE_FS_V1:
958c2791297SSivapiriyanKumarasamy 	default:
959627441f5SAric Cyr 		build_vrr_infopacket_v1(stream->signal, vrr, infopacket, stream->freesync_on_desktop);
960c2791297SSivapiriyanKumarasamy 	}
9614cda3243SMax.Tseng 
9624cda3243SMax.Tseng 	if (true == pack_sdp_v1_3 &&
9634cda3243SMax.Tseng 		true == dc_is_dp_signal(stream->signal) &&
9644cda3243SMax.Tseng 		packet_type != PACKET_TYPE_VRR &&
9654cda3243SMax.Tseng 		packet_type != PACKET_TYPE_VTEM)
9664cda3243SMax.Tseng 		build_vrr_infopacket_sdp_v1_3(packet_type, infopacket);
967c2791297SSivapiriyanKumarasamy }
968c2791297SSivapiriyanKumarasamy 
mod_freesync_build_vrr_params(struct mod_freesync * mod_freesync,const struct dc_stream_state * stream,struct mod_freesync_config * in_config,struct mod_vrr_params * in_out_vrr)96998e6436dSAnthony Koo void mod_freesync_build_vrr_params(struct mod_freesync *mod_freesync,
97098e6436dSAnthony Koo 		const struct dc_stream_state *stream,
97198e6436dSAnthony Koo 		struct mod_freesync_config *in_config,
97298e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
9734562236bSHarry Wentland {
9747a1c37e0SAnthony Koo 	struct core_freesync *core_freesync = NULL;
97598e6436dSAnthony Koo 	unsigned long long nominal_field_rate_in_uhz = 0;
9765a6b5458SAric Cyr 	unsigned long long rounded_nominal_in_uhz = 0;
97798e6436dSAnthony Koo 	unsigned int refresh_range = 0;
978a463b263SBayan Zabihiyan 	unsigned long long min_refresh_in_uhz = 0;
979a463b263SBayan Zabihiyan 	unsigned long long max_refresh_in_uhz = 0;
980da55037aSAustin Zheng 	unsigned long long min_hardware_refresh_in_uhz = 0;
9817a1c37e0SAnthony Koo 
9827a1c37e0SAnthony Koo 	if (mod_freesync == NULL)
9837a1c37e0SAnthony Koo 		return;
9847a1c37e0SAnthony Koo 
9857a1c37e0SAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
9864562236bSHarry Wentland 
98798e6436dSAnthony Koo 	/* Calculate nominal field rate for stream */
988ff6014d6SAnthony Koo 	nominal_field_rate_in_uhz =
989ff6014d6SAnthony Koo 			mod_freesync_calc_nominal_field_rate(stream);
9904562236bSHarry Wentland 
991da55037aSAustin Zheng 	if (stream->ctx->dc->caps.max_v_total != 0 && stream->timing.h_total != 0) {
992da55037aSAustin Zheng 		min_hardware_refresh_in_uhz = div64_u64((stream->timing.pix_clk_100hz * 100000000ULL),
993*a29997b7SDillon Varone 			(stream->timing.h_total * (long long)calc_max_hardware_v_total(stream)));
994da55037aSAustin Zheng 	}
995da55037aSAustin Zheng 	/* Limit minimum refresh rate to what can be supported by hardware */
996da55037aSAustin Zheng 	min_refresh_in_uhz = min_hardware_refresh_in_uhz > in_config->min_refresh_in_uhz ?
997da55037aSAustin Zheng 		min_hardware_refresh_in_uhz : in_config->min_refresh_in_uhz;
99898e6436dSAnthony Koo 	max_refresh_in_uhz = in_config->max_refresh_in_uhz;
9994562236bSHarry Wentland 
1000fe984cb3SFelipe 	/* Full range may be larger than current video timing, so cap at nominal */
100198e6436dSAnthony Koo 	if (max_refresh_in_uhz > nominal_field_rate_in_uhz)
100298e6436dSAnthony Koo 		max_refresh_in_uhz = nominal_field_rate_in_uhz;
100398e6436dSAnthony Koo 
1004fe984cb3SFelipe 	/* Full range may be larger than current video timing, so cap at nominal */
10055a6b5458SAric Cyr 	if (min_refresh_in_uhz > max_refresh_in_uhz)
10065a6b5458SAric Cyr 		min_refresh_in_uhz = max_refresh_in_uhz;
10075a6b5458SAric Cyr 
1008fe984cb3SFelipe 	/* If a monitor reports exactly max refresh of 2x of min, enforce it on nominal */
10095a6b5458SAric Cyr 	rounded_nominal_in_uhz =
10105a6b5458SAric Cyr 			div_u64(nominal_field_rate_in_uhz + 50000, 100000) * 100000;
10115a6b5458SAric Cyr 	if (in_config->max_refresh_in_uhz == (2 * in_config->min_refresh_in_uhz) &&
10125a6b5458SAric Cyr 		in_config->max_refresh_in_uhz == rounded_nominal_in_uhz)
10135a6b5458SAric Cyr 		min_refresh_in_uhz = div_u64(nominal_field_rate_in_uhz, 2);
101498e6436dSAnthony Koo 
101598e6436dSAnthony Koo 	if (!vrr_settings_require_update(core_freesync,
1016a463b263SBayan Zabihiyan 			in_config, (unsigned int)min_refresh_in_uhz, (unsigned int)max_refresh_in_uhz,
101798e6436dSAnthony Koo 			in_out_vrr))
101898e6436dSAnthony Koo 		return;
101998e6436dSAnthony Koo 
102098e6436dSAnthony Koo 	in_out_vrr->state = in_config->state;
1021ca35899cSBayan Zabihiyan 	in_out_vrr->send_info_frame = in_config->vsif_supported;
102298e6436dSAnthony Koo 
1023050790ccSAnthony Koo 	if (in_config->state == VRR_STATE_UNSUPPORTED) {
102498e6436dSAnthony Koo 		in_out_vrr->state = VRR_STATE_UNSUPPORTED;
102598e6436dSAnthony Koo 		in_out_vrr->supported = false;
1026050790ccSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
1027050790ccSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
1028050790ccSAnthony Koo 
1029050790ccSAnthony Koo 		return;
1030050790ccSAnthony Koo 
103198e6436dSAnthony Koo 	} else {
1032a463b263SBayan Zabihiyan 		in_out_vrr->min_refresh_in_uhz = (unsigned int)min_refresh_in_uhz;
103398e6436dSAnthony Koo 		in_out_vrr->max_duration_in_us =
103498e6436dSAnthony Koo 				calc_duration_in_us_from_refresh_in_uhz(
1035a463b263SBayan Zabihiyan 						(unsigned int)min_refresh_in_uhz);
103698e6436dSAnthony Koo 
1037a463b263SBayan Zabihiyan 		in_out_vrr->max_refresh_in_uhz = (unsigned int)max_refresh_in_uhz;
103898e6436dSAnthony Koo 		in_out_vrr->min_duration_in_us =
103998e6436dSAnthony Koo 				calc_duration_in_us_from_refresh_in_uhz(
1040a463b263SBayan Zabihiyan 						(unsigned int)max_refresh_in_uhz);
104198e6436dSAnthony Koo 
1042d2bacc38SHaiyi Zhou 		if (in_config->state == VRR_STATE_ACTIVE_FIXED)
1043d2bacc38SHaiyi Zhou 			in_out_vrr->fixed_refresh_in_uhz = in_config->fixed_refresh_in_uhz;
1044d2bacc38SHaiyi Zhou 		else
1045d2bacc38SHaiyi Zhou 			in_out_vrr->fixed_refresh_in_uhz = 0;
1046d2bacc38SHaiyi Zhou 
1047ad339f69SJaehyun Chung 		refresh_range = div_u64(in_out_vrr->max_refresh_in_uhz + 500000, 1000000) -
1048bcebe44fSRodrigo Siqueira 				div_u64(in_out_vrr->min_refresh_in_uhz + 500000, 1000000);
104998e6436dSAnthony Koo 
105098e6436dSAnthony Koo 		in_out_vrr->supported = true;
105198e6436dSAnthony Koo 	}
105298e6436dSAnthony Koo 
105398e6436dSAnthony Koo 	in_out_vrr->fixed.ramping_active = in_config->ramping;
105498e6436dSAnthony Koo 
105598e6436dSAnthony Koo 	in_out_vrr->btr.btr_enabled = in_config->btr;
1056a463b263SBayan Zabihiyan 
10575a6b5458SAric Cyr 	if (in_out_vrr->max_refresh_in_uhz < (2 * in_out_vrr->min_refresh_in_uhz))
105898e6436dSAnthony Koo 		in_out_vrr->btr.btr_enabled = false;
10595a6b5458SAric Cyr 	else {
10605a6b5458SAric Cyr 		in_out_vrr->btr.margin_in_us = in_out_vrr->max_duration_in_us -
10615a6b5458SAric Cyr 				2 * in_out_vrr->min_duration_in_us;
10625a6b5458SAric Cyr 		if (in_out_vrr->btr.margin_in_us > BTR_MAX_MARGIN)
10635a6b5458SAric Cyr 			in_out_vrr->btr.margin_in_us = BTR_MAX_MARGIN;
10645a6b5458SAric Cyr 	}
1065a463b263SBayan Zabihiyan 
106698e6436dSAnthony Koo 	in_out_vrr->btr.btr_active = false;
106798e6436dSAnthony Koo 	in_out_vrr->btr.inserted_duration_in_us = 0;
106898e6436dSAnthony Koo 	in_out_vrr->btr.frames_to_insert = 0;
106998e6436dSAnthony Koo 	in_out_vrr->btr.frame_counter = 0;
10701075735eSAlvin Lee 	in_out_vrr->fixed.fixed_active = false;
10711075735eSAlvin Lee 	in_out_vrr->fixed.target_refresh_in_uhz = 0;
1072ded6119eSAmanda Liu 
107398e6436dSAnthony Koo 	in_out_vrr->btr.mid_point_in_us =
1074a463b263SBayan Zabihiyan 				(in_out_vrr->min_duration_in_us +
1075a463b263SBayan Zabihiyan 				 in_out_vrr->max_duration_in_us) / 2;
107698e6436dSAnthony Koo 
107798e6436dSAnthony Koo 	if (in_out_vrr->state == VRR_STATE_UNSUPPORTED) {
107898e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
107998e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
108098e6436dSAnthony Koo 	} else if (in_out_vrr->state == VRR_STATE_DISABLED) {
108198e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
108298e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
108398e6436dSAnthony Koo 	} else if (in_out_vrr->state == VRR_STATE_INACTIVE) {
108498e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
108598e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
108698e6436dSAnthony Koo 	} else if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
1087ad339f69SJaehyun Chung 			refresh_range >= MIN_REFRESH_RANGE) {
10886f8f7644SAmanda Liu 
108998e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min =
109049c70eceSAlvin Lee 			mod_freesync_calc_v_total_from_refresh(stream,
109198e6436dSAnthony Koo 				in_out_vrr->max_refresh_in_uhz);
109298e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max =
109349c70eceSAlvin Lee 			mod_freesync_calc_v_total_from_refresh(stream,
109498e6436dSAnthony Koo 				in_out_vrr->min_refresh_in_uhz);
109598e6436dSAnthony Koo 	} else if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED) {
109698e6436dSAnthony Koo 		in_out_vrr->fixed.target_refresh_in_uhz =
1097d2bacc38SHaiyi Zhou 				in_out_vrr->fixed_refresh_in_uhz;
1098953c2901SAnthony Koo 		if (in_out_vrr->fixed.ramping_active &&
1099953c2901SAnthony Koo 				in_out_vrr->fixed.fixed_active) {
1100953c2901SAnthony Koo 			/* Do not update vtotals if ramping is already active
1101953c2901SAnthony Koo 			 * in order to continue ramp from current refresh.
1102953c2901SAnthony Koo 			 */
110398e6436dSAnthony Koo 			in_out_vrr->fixed.fixed_active = true;
110498e6436dSAnthony Koo 		} else {
110598e6436dSAnthony Koo 			in_out_vrr->fixed.fixed_active = true;
110698e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
110749c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
110898e6436dSAnthony Koo 					in_out_vrr->fixed.target_refresh_in_uhz);
110998e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
111098e6436dSAnthony Koo 				in_out_vrr->adjust.v_total_min;
111198e6436dSAnthony Koo 		}
111298e6436dSAnthony Koo 	} else {
111398e6436dSAnthony Koo 		in_out_vrr->state = VRR_STATE_INACTIVE;
111498e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
111598e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
111698e6436dSAnthony Koo 	}
1117eed4eddaSRobin Chen 
1118eed4eddaSRobin Chen 	in_out_vrr->adjust.allow_otg_v_count_halt = (in_config->state == VRR_STATE_ACTIVE_FIXED) ? true : false;
111998e6436dSAnthony Koo }
112098e6436dSAnthony Koo 
mod_freesync_handle_preflip(struct mod_freesync * mod_freesync,const struct dc_plane_state * plane,const struct dc_stream_state * stream,unsigned int curr_time_stamp_in_us,struct mod_vrr_params * in_out_vrr)112198e6436dSAnthony Koo void mod_freesync_handle_preflip(struct mod_freesync *mod_freesync,
112298e6436dSAnthony Koo 		const struct dc_plane_state *plane,
112398e6436dSAnthony Koo 		const struct dc_stream_state *stream,
112498e6436dSAnthony Koo 		unsigned int curr_time_stamp_in_us,
112598e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
112698e6436dSAnthony Koo {
112798e6436dSAnthony Koo 	struct core_freesync *core_freesync = NULL;
112898e6436dSAnthony Koo 	unsigned int last_render_time_in_us = 0;
112998e6436dSAnthony Koo 
113098e6436dSAnthony Koo 	if (mod_freesync == NULL)
113198e6436dSAnthony Koo 		return;
113298e6436dSAnthony Koo 
113398e6436dSAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
113498e6436dSAnthony Koo 
113598e6436dSAnthony Koo 	if (in_out_vrr->supported &&
113698e6436dSAnthony Koo 			in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE) {
11374562236bSHarry Wentland 
11384562236bSHarry Wentland 		last_render_time_in_us = curr_time_stamp_in_us -
113998e6436dSAnthony Koo 				plane->time.prev_update_time_in_us;
11404562236bSHarry Wentland 
114198e6436dSAnthony Koo 		if (in_out_vrr->btr.btr_enabled) {
11424562236bSHarry Wentland 			apply_below_the_range(core_freesync,
114398e6436dSAnthony Koo 					stream,
114498e6436dSAnthony Koo 					last_render_time_in_us,
114598e6436dSAnthony Koo 					in_out_vrr);
11464562236bSHarry Wentland 		} else {
11474562236bSHarry Wentland 			apply_fixed_refresh(core_freesync,
114898e6436dSAnthony Koo 				stream,
114998e6436dSAnthony Koo 				last_render_time_in_us,
115098e6436dSAnthony Koo 				in_out_vrr);
115198e6436dSAnthony Koo 		}
115298e6436dSAnthony Koo 
11533fe5739dSAngus Wang 		determine_flip_interval_workaround_req(in_out_vrr,
11543fe5739dSAngus Wang 				curr_time_stamp_in_us);
11553fe5739dSAngus Wang 
11564562236bSHarry Wentland 	}
11574562236bSHarry Wentland }
11584562236bSHarry Wentland 
mod_freesync_handle_v_update(struct mod_freesync * mod_freesync,const struct dc_stream_state * stream,struct mod_vrr_params * in_out_vrr)115998e6436dSAnthony Koo void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
116098e6436dSAnthony Koo 		const struct dc_stream_state *stream,
116198e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
116298e6436dSAnthony Koo {
116398e6436dSAnthony Koo 	struct core_freesync *core_freesync = NULL;
11643fe5739dSAngus Wang 	unsigned int cur_timestamp_in_us;
116519a2e1e3SAngus Wang 	unsigned long long cur_tick;
116698e6436dSAnthony Koo 
116798e6436dSAnthony Koo 	if ((mod_freesync == NULL) || (stream == NULL) || (in_out_vrr == NULL))
116898e6436dSAnthony Koo 		return;
116998e6436dSAnthony Koo 
117098e6436dSAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
117198e6436dSAnthony Koo 
117298e6436dSAnthony Koo 	if (in_out_vrr->supported == false)
117398e6436dSAnthony Koo 		return;
117498e6436dSAnthony Koo 
117519a2e1e3SAngus Wang 	cur_tick = dm_get_timestamp(core_freesync->dc->ctx);
117619a2e1e3SAngus Wang 	cur_timestamp_in_us = (unsigned int)
117719a2e1e3SAngus Wang 			div_u64(dm_get_elapse_time_in_ns(core_freesync->dc->ctx, cur_tick, 0), 1000);
11783fe5739dSAngus Wang 
11793fe5739dSAngus Wang 	in_out_vrr->flip_interval.vsyncs_between_flip++;
11803fe5739dSAngus Wang 	in_out_vrr->flip_interval.v_update_timestamp_in_us = cur_timestamp_in_us;
11813fe5739dSAngus Wang 
11823fe5739dSAngus Wang 	if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
11833fe5739dSAngus Wang 			(in_out_vrr->flip_interval.flip_interval_workaround_active ||
11843fe5739dSAngus Wang 			(!in_out_vrr->flip_interval.flip_interval_workaround_active &&
11853fe5739dSAngus Wang 			in_out_vrr->flip_interval.program_flip_interval_workaround))) {
11863fe5739dSAngus Wang 		// set freesync vmin vmax to nominal for workaround
11873fe5739dSAngus Wang 		in_out_vrr->adjust.v_total_min =
11883fe5739dSAngus Wang 			mod_freesync_calc_v_total_from_refresh(
11893fe5739dSAngus Wang 			stream, in_out_vrr->max_refresh_in_uhz);
11903fe5739dSAngus Wang 		in_out_vrr->adjust.v_total_max =
11913fe5739dSAngus Wang 				in_out_vrr->adjust.v_total_min;
11923fe5739dSAngus Wang 		in_out_vrr->flip_interval.program_flip_interval_workaround = false;
11933fe5739dSAngus Wang 		in_out_vrr->flip_interval.do_flip_interval_workaround_cleanup = true;
11943fe5739dSAngus Wang 		return;
11953fe5739dSAngus Wang 	}
11963fe5739dSAngus Wang 
11973fe5739dSAngus Wang 	if (in_out_vrr->state != VRR_STATE_ACTIVE_VARIABLE &&
11983fe5739dSAngus Wang 			in_out_vrr->flip_interval.do_flip_interval_workaround_cleanup) {
11993fe5739dSAngus Wang 		in_out_vrr->flip_interval.do_flip_interval_workaround_cleanup = false;
12003fe5739dSAngus Wang 		in_out_vrr->flip_interval.flip_interval_detect_counter = 0;
12013fe5739dSAngus Wang 		in_out_vrr->flip_interval.vsyncs_between_flip = 0;
12023fe5739dSAngus Wang 		in_out_vrr->flip_interval.vsync_to_flip_in_us = 0;
12033fe5739dSAngus Wang 	}
12043fe5739dSAngus Wang 
120598e6436dSAnthony Koo 	/* Below the Range Logic */
120698e6436dSAnthony Koo 
120798e6436dSAnthony Koo 	/* Only execute if in fullscreen mode */
120898e6436dSAnthony Koo 	if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
120998e6436dSAnthony Koo 					in_out_vrr->btr.btr_active) {
121098e6436dSAnthony Koo 		/* TODO: pass in flag for Pre-DCE12 ASIC
121198e6436dSAnthony Koo 		 * in order for frame variable duration to take affect,
121298e6436dSAnthony Koo 		 * it needs to be done one VSYNC early, which is at
121398e6436dSAnthony Koo 		 * frameCounter == 1.
121498e6436dSAnthony Koo 		 * For DCE12 and newer updates to V_TOTAL_MIN/MAX
121598e6436dSAnthony Koo 		 * will take affect on current frame
121698e6436dSAnthony Koo 		 */
121798e6436dSAnthony Koo 		if (in_out_vrr->btr.frames_to_insert ==
121898e6436dSAnthony Koo 				in_out_vrr->btr.frame_counter) {
121998e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
122098e6436dSAnthony Koo 				calc_v_total_from_duration(stream,
122198e6436dSAnthony Koo 				in_out_vrr,
122298e6436dSAnthony Koo 				in_out_vrr->btr.inserted_duration_in_us);
122398e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
122498e6436dSAnthony Koo 				in_out_vrr->adjust.v_total_min;
12254562236bSHarry Wentland 		}
12264562236bSHarry Wentland 
122798e6436dSAnthony Koo 		if (in_out_vrr->btr.frame_counter > 0)
122898e6436dSAnthony Koo 			in_out_vrr->btr.frame_counter--;
122998e6436dSAnthony Koo 
123098e6436dSAnthony Koo 		/* Restore FreeSync */
123198e6436dSAnthony Koo 		if (in_out_vrr->btr.frame_counter == 0) {
123298e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
123349c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
123498e6436dSAnthony Koo 				in_out_vrr->max_refresh_in_uhz);
123598e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
123649c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
123798e6436dSAnthony Koo 				in_out_vrr->min_refresh_in_uhz);
123898e6436dSAnthony Koo 		}
123998e6436dSAnthony Koo 	}
124098e6436dSAnthony Koo 
124198e6436dSAnthony Koo 	/* If in fullscreen freesync mode or in video, do not program
124298e6436dSAnthony Koo 	 * static screen ramp values
124398e6436dSAnthony Koo 	 */
124498e6436dSAnthony Koo 	if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE)
124598e6436dSAnthony Koo 		in_out_vrr->fixed.ramping_active = false;
124698e6436dSAnthony Koo 
1247fe984cb3SFelipe 	/* Gradual Static Screen Ramping Logic
1248fe984cb3SFelipe 	 * Execute if ramp is active and user enabled freesync static screen
1249fe984cb3SFelipe 	 */
125098e6436dSAnthony Koo 	if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED &&
125198e6436dSAnthony Koo 				in_out_vrr->fixed.ramping_active) {
125298e6436dSAnthony Koo 		update_v_total_for_static_ramp(
125398e6436dSAnthony Koo 				core_freesync, stream, in_out_vrr);
12544562236bSHarry Wentland 	}
12554562236bSHarry Wentland }
1256a3e1737eSAnthony Koo 
mod_freesync_calc_nominal_field_rate(const struct dc_stream_state * stream)1257ff6014d6SAnthony Koo unsigned long long mod_freesync_calc_nominal_field_rate(
1258ff6014d6SAnthony Koo 			const struct dc_stream_state *stream)
1259ff6014d6SAnthony Koo {
1260ff6014d6SAnthony Koo 	unsigned long long nominal_field_rate_in_uhz = 0;
1261c5980231SAric Cyr 	unsigned int total = stream->timing.h_total * stream->timing.v_total;
1262ff6014d6SAnthony Koo 
1263c5980231SAric Cyr 	/* Calculate nominal field rate for stream, rounded up to nearest integer */
12645a6b5458SAric Cyr 	nominal_field_rate_in_uhz = stream->timing.pix_clk_100hz;
12655a6b5458SAric Cyr 	nominal_field_rate_in_uhz *= 100000000ULL;
1266c5980231SAric Cyr 
1267c5980231SAric Cyr 	nominal_field_rate_in_uhz =	div_u64(nominal_field_rate_in_uhz, total);
1268ff6014d6SAnthony Koo 
1269ff6014d6SAnthony Koo 	return nominal_field_rate_in_uhz;
1270ff6014d6SAnthony Koo }
1271ff6014d6SAnthony Koo 
mod_freesync_get_freesync_enabled(struct mod_vrr_params * pVrr)1272ebfb1526SHarry VanZyllDeJong bool mod_freesync_get_freesync_enabled(struct mod_vrr_params *pVrr)
1273c2fbe663SFelipe Clark {
1274c2fbe663SFelipe Clark 	return (pVrr->state != VRR_STATE_UNSUPPORTED) && (pVrr->state != VRR_STATE_DISABLED);
1275c2fbe663SFelipe Clark }
1276