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
514562236bSHarry Wentland 
524562236bSHarry Wentland struct core_freesync {
534562236bSHarry Wentland 	struct mod_freesync public;
544562236bSHarry Wentland 	struct dc *dc;
554562236bSHarry Wentland };
564562236bSHarry Wentland 
574562236bSHarry Wentland #define MOD_FREESYNC_TO_CORE(mod_freesync)\
584562236bSHarry Wentland 		container_of(mod_freesync, struct core_freesync, public)
594562236bSHarry Wentland 
mod_freesync_create(struct dc * dc)604562236bSHarry Wentland struct mod_freesync *mod_freesync_create(struct dc *dc)
614562236bSHarry Wentland {
624562236bSHarry Wentland 	struct core_freesync *core_freesync =
632004f45eSHarry Wentland 			kzalloc(sizeof(struct core_freesync), GFP_KERNEL);
644562236bSHarry Wentland 
654562236bSHarry Wentland 	if (core_freesync == NULL)
664562236bSHarry Wentland 		goto fail_alloc_context;
674562236bSHarry Wentland 
684562236bSHarry Wentland 	if (dc == NULL)
694562236bSHarry Wentland 		goto fail_construct;
704562236bSHarry Wentland 
714562236bSHarry Wentland 	core_freesync->dc = dc;
724562236bSHarry Wentland 	return &core_freesync->public;
734562236bSHarry Wentland 
744562236bSHarry Wentland fail_construct:
752004f45eSHarry Wentland 	kfree(core_freesync);
764562236bSHarry Wentland 
774562236bSHarry Wentland fail_alloc_context:
784562236bSHarry Wentland 	return NULL;
794562236bSHarry Wentland }
804562236bSHarry Wentland 
mod_freesync_destroy(struct mod_freesync * mod_freesync)814562236bSHarry Wentland void mod_freesync_destroy(struct mod_freesync *mod_freesync)
824562236bSHarry Wentland {
8398e6436dSAnthony Koo 	struct core_freesync *core_freesync = NULL;
8498e6436dSAnthony Koo 	if (mod_freesync == NULL)
8598e6436dSAnthony Koo 		return;
8698e6436dSAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
872004f45eSHarry Wentland 	kfree(core_freesync);
884562236bSHarry Wentland }
894562236bSHarry Wentland 
90fe984cb3SFelipe #if 0 /* Unused currently */
9198e6436dSAnthony Koo static unsigned int calc_refresh_in_uhz_from_duration(
9298e6436dSAnthony Koo 		unsigned int duration_in_ns)
934562236bSHarry Wentland {
9498e6436dSAnthony Koo 	unsigned int refresh_in_uhz =
9598e6436dSAnthony Koo 			((unsigned int)(div64_u64((1000000000ULL * 1000000),
9698e6436dSAnthony Koo 					duration_in_ns)));
9798e6436dSAnthony Koo 	return refresh_in_uhz;
9898e6436dSAnthony Koo }
9998e6436dSAnthony Koo #endif
1004562236bSHarry Wentland 
calc_duration_in_us_from_refresh_in_uhz(unsigned int refresh_in_uhz)10198e6436dSAnthony Koo static unsigned int calc_duration_in_us_from_refresh_in_uhz(
10298e6436dSAnthony Koo 		unsigned int refresh_in_uhz)
1034562236bSHarry Wentland {
10498e6436dSAnthony Koo 	unsigned int duration_in_us =
10598e6436dSAnthony Koo 			((unsigned int)(div64_u64((1000000000ULL * 1000),
10698e6436dSAnthony Koo 					refresh_in_uhz)));
10798e6436dSAnthony Koo 	return duration_in_us;
1084562236bSHarry Wentland }
1094562236bSHarry 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)11098e6436dSAnthony Koo static unsigned int calc_duration_in_us_from_v_total(
11198e6436dSAnthony Koo 		const struct dc_stream_state *stream,
11298e6436dSAnthony Koo 		const struct mod_vrr_params *in_vrr,
11398e6436dSAnthony Koo 		unsigned int v_total)
1144562236bSHarry Wentland {
11598e6436dSAnthony Koo 	unsigned int duration_in_us =
11698e6436dSAnthony Koo 			(unsigned int)(div64_u64(((unsigned long long)(v_total)
117380604e2SKen Chalmers 				* 10000) * stream->timing.h_total,
118380604e2SKen Chalmers 					stream->timing.pix_clk_100hz));
1197a1c37e0SAnthony Koo 
12098e6436dSAnthony Koo 	return duration_in_us;
1214562236bSHarry Wentland }
1224562236bSHarry Wentland 
mod_freesync_calc_v_total_from_refresh(const struct dc_stream_state * stream,unsigned int refresh_in_uhz)12349c70eceSAlvin Lee unsigned int mod_freesync_calc_v_total_from_refresh(
12498e6436dSAnthony Koo 		const struct dc_stream_state *stream,
12598e6436dSAnthony Koo 		unsigned int refresh_in_uhz)
126a3e1737eSAnthony Koo {
127a501e22cSColin Ian King 	unsigned int v_total;
12898e6436dSAnthony Koo 	unsigned int frame_duration_in_ns;
129a3e1737eSAnthony Koo 
13098e6436dSAnthony Koo 	frame_duration_in_ns =
13198e6436dSAnthony Koo 			((unsigned int)(div64_u64((1000000000ULL * 1000000),
13298e6436dSAnthony Koo 					refresh_in_uhz)));
133a3e1737eSAnthony Koo 
13498e6436dSAnthony Koo 	v_total = div64_u64(div64_u64(((unsigned long long)(
135380604e2SKen Chalmers 			frame_duration_in_ns) * (stream->timing.pix_clk_100hz / 10)),
1364562236bSHarry Wentland 			stream->timing.h_total), 1000000);
1376c626ffbSYongqiang Sun 
13898e6436dSAnthony Koo 	/* v_total cannot be less than nominal */
13998e6436dSAnthony Koo 	if (v_total < stream->timing.v_total) {
14098e6436dSAnthony Koo 		ASSERT(v_total < stream->timing.v_total);
14198e6436dSAnthony Koo 		v_total = stream->timing.v_total;
1426c626ffbSYongqiang Sun 	}
1436c626ffbSYongqiang Sun 
14498e6436dSAnthony Koo 	return v_total;
1456c626ffbSYongqiang Sun }
14672ada5f7SEric Cook 
calc_v_total_from_duration(const struct dc_stream_state * stream,const struct mod_vrr_params * vrr,unsigned int duration_in_us)14798e6436dSAnthony Koo static unsigned int calc_v_total_from_duration(
14898e6436dSAnthony Koo 		const struct dc_stream_state *stream,
14998e6436dSAnthony Koo 		const struct mod_vrr_params *vrr,
15098e6436dSAnthony Koo 		unsigned int duration_in_us)
1514562236bSHarry Wentland {
15298e6436dSAnthony Koo 	unsigned int v_total = 0;
15398e6436dSAnthony Koo 
15498e6436dSAnthony Koo 	if (duration_in_us < vrr->min_duration_in_us)
15598e6436dSAnthony Koo 		duration_in_us = vrr->min_duration_in_us;
15698e6436dSAnthony Koo 
15798e6436dSAnthony Koo 	if (duration_in_us > vrr->max_duration_in_us)
15898e6436dSAnthony Koo 		duration_in_us = vrr->max_duration_in_us;
15998e6436dSAnthony Koo 
16033df94e1SGuo, Bing 	if (dc_is_hdmi_signal(stream->signal)) {
16133df94e1SGuo, Bing 		uint32_t h_total_up_scaled;
16233df94e1SGuo, Bing 
16333df94e1SGuo, Bing 		h_total_up_scaled = stream->timing.h_total * 10000;
16433df94e1SGuo, Bing 		v_total = div_u64((unsigned long long)duration_in_us
16533df94e1SGuo, Bing 					* stream->timing.pix_clk_100hz + (h_total_up_scaled - 1),
16633df94e1SGuo, Bing 					h_total_up_scaled);
16733df94e1SGuo, Bing 	} else {
16898e6436dSAnthony Koo 		v_total = div64_u64(div64_u64(((unsigned long long)(
169380604e2SKen Chalmers 					duration_in_us) * (stream->timing.pix_clk_100hz / 10)),
17098e6436dSAnthony Koo 					stream->timing.h_total), 1000);
17133df94e1SGuo, Bing 	}
17298e6436dSAnthony Koo 
17398e6436dSAnthony Koo 	/* v_total cannot be less than nominal */
17498e6436dSAnthony Koo 	if (v_total < stream->timing.v_total) {
17598e6436dSAnthony Koo 		ASSERT(v_total < stream->timing.v_total);
17698e6436dSAnthony Koo 		v_total = stream->timing.v_total;
1774562236bSHarry Wentland 	}
1784562236bSHarry Wentland 
17998e6436dSAnthony Koo 	return v_total;
18098e6436dSAnthony Koo }
1814562236bSHarry 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)18298e6436dSAnthony Koo static void update_v_total_for_static_ramp(
18398e6436dSAnthony Koo 		struct core_freesync *core_freesync,
18498e6436dSAnthony Koo 		const struct dc_stream_state *stream,
18598e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
18698e6436dSAnthony Koo {
18798e6436dSAnthony Koo 	unsigned int v_total = 0;
18898e6436dSAnthony Koo 	unsigned int current_duration_in_us =
18998e6436dSAnthony Koo 			calc_duration_in_us_from_v_total(
19098e6436dSAnthony Koo 				stream, in_out_vrr,
19198e6436dSAnthony Koo 				in_out_vrr->adjust.v_total_max);
19298e6436dSAnthony Koo 	unsigned int target_duration_in_us =
19398e6436dSAnthony Koo 			calc_duration_in_us_from_refresh_in_uhz(
19498e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz);
19598e6436dSAnthony Koo 	bool ramp_direction_is_up = (current_duration_in_us >
19698e6436dSAnthony Koo 				target_duration_in_us) ? true : false;
1974562236bSHarry Wentland 
198fe984cb3SFelipe 	/* Calculate ratio between new and current frame duration with 3 digit */
1994562236bSHarry Wentland 	unsigned int frame_duration_ratio = div64_u64(1000000,
2004562236bSHarry Wentland 		(1000 +  div64_u64(((unsigned long long)(
2014562236bSHarry Wentland 		STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
20298e6436dSAnthony Koo 		current_duration_in_us),
20398e6436dSAnthony Koo 		1000000)));
2044562236bSHarry Wentland 
20598e6436dSAnthony Koo 	/* Calculate delta between new and current frame duration in us */
2064562236bSHarry Wentland 	unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
20798e6436dSAnthony Koo 		current_duration_in_us) *
2084562236bSHarry Wentland 		(1000 - frame_duration_ratio)), 1000);
2094562236bSHarry Wentland 
2104562236bSHarry Wentland 	/* Adjust frame duration delta based on ratio between current and
2114562236bSHarry Wentland 	 * standard frame duration (frame duration at 60 Hz refresh rate).
2124562236bSHarry Wentland 	 */
2134562236bSHarry Wentland 	unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
21498e6436dSAnthony Koo 		frame_duration_delta) * current_duration_in_us), 16666);
2154562236bSHarry Wentland 
2164562236bSHarry Wentland 	/* Going to a higher refresh rate (lower frame duration) */
21798e6436dSAnthony Koo 	if (ramp_direction_is_up) {
218fe984cb3SFelipe 		/* Reduce frame duration */
21998e6436dSAnthony Koo 		current_duration_in_us -= ramp_rate_interpolated;
2204562236bSHarry Wentland 
221fe984cb3SFelipe 		/* Adjust for frame duration below min */
22298e6436dSAnthony Koo 		if (current_duration_in_us <= target_duration_in_us) {
22398e6436dSAnthony Koo 			in_out_vrr->fixed.ramping_active = false;
22498e6436dSAnthony Koo 			in_out_vrr->fixed.ramping_done = true;
22598e6436dSAnthony Koo 			current_duration_in_us =
22698e6436dSAnthony Koo 				calc_duration_in_us_from_refresh_in_uhz(
22798e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz);
2284562236bSHarry Wentland 		}
2294562236bSHarry Wentland 	/* Going to a lower refresh rate (larger frame duration) */
2304562236bSHarry Wentland 	} else {
231fe984cb3SFelipe 		/* Increase frame duration */
23298e6436dSAnthony Koo 		current_duration_in_us += ramp_rate_interpolated;
2334562236bSHarry Wentland 
234fe984cb3SFelipe 		/* Adjust for frame duration above max */
23598e6436dSAnthony Koo 		if (current_duration_in_us >= target_duration_in_us) {
23698e6436dSAnthony Koo 			in_out_vrr->fixed.ramping_active = false;
23798e6436dSAnthony Koo 			in_out_vrr->fixed.ramping_done = true;
23898e6436dSAnthony Koo 			current_duration_in_us =
23998e6436dSAnthony Koo 				calc_duration_in_us_from_refresh_in_uhz(
24098e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz);
2414562236bSHarry Wentland 		}
2424562236bSHarry Wentland 	}
2434562236bSHarry Wentland 
244953c2901SAnthony Koo 	v_total = div64_u64(div64_u64(((unsigned long long)(
245380604e2SKen Chalmers 			current_duration_in_us) * (stream->timing.pix_clk_100hz / 10)),
246953c2901SAnthony Koo 				stream->timing.h_total), 1000);
2474562236bSHarry Wentland 
2488396745dSJaehyun Chung 	/* v_total cannot be less than nominal */
2498396745dSJaehyun Chung 	if (v_total < stream->timing.v_total)
2508396745dSJaehyun Chung 		v_total = stream->timing.v_total;
2518396745dSJaehyun Chung 
25298e6436dSAnthony Koo 	in_out_vrr->adjust.v_total_min = v_total;
25398e6436dSAnthony Koo 	in_out_vrr->adjust.v_total_max = v_total;
2544562236bSHarry Wentland }
2554562236bSHarry 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)2564562236bSHarry Wentland static void apply_below_the_range(struct core_freesync *core_freesync,
25798e6436dSAnthony Koo 		const struct dc_stream_state *stream,
25898e6436dSAnthony Koo 		unsigned int last_render_time_in_us,
25998e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
2604562236bSHarry Wentland {
2614562236bSHarry Wentland 	unsigned int inserted_frame_duration_in_us = 0;
2624562236bSHarry Wentland 	unsigned int mid_point_frames_ceil = 0;
2634562236bSHarry Wentland 	unsigned int mid_point_frames_floor = 0;
2644562236bSHarry Wentland 	unsigned int frame_time_in_us = 0;
2654562236bSHarry Wentland 	unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
2664562236bSHarry Wentland 	unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
2674562236bSHarry Wentland 	unsigned int frames_to_insert = 0;
2689070d18fSAric Cyr 	unsigned int delta_from_mid_point_delta_in_us;
269ded6119eSAmanda Liu 	unsigned int max_render_time_in_us =
270ded6119eSAmanda Liu 			in_out_vrr->max_duration_in_us - in_out_vrr->btr.margin_in_us;
2714562236bSHarry Wentland 
2724562236bSHarry Wentland 	/* Program BTR */
273ded6119eSAmanda Liu 	if ((last_render_time_in_us + in_out_vrr->btr.margin_in_us / 2) < max_render_time_in_us) {
27498e6436dSAnthony Koo 		/* Exit Below the Range */
27598e6436dSAnthony Koo 		if (in_out_vrr->btr.btr_active) {
27698e6436dSAnthony Koo 			in_out_vrr->btr.frame_counter = 0;
27798e6436dSAnthony Koo 			in_out_vrr->btr.btr_active = false;
27898e6436dSAnthony Koo 		}
279ded6119eSAmanda Liu 	} else if (last_render_time_in_us > (max_render_time_in_us + in_out_vrr->btr.margin_in_us / 2)) {
28098e6436dSAnthony Koo 		/* Enter Below the Range */
281ded6119eSAmanda Liu 		if (!in_out_vrr->btr.btr_active) {
28298e6436dSAnthony Koo 			in_out_vrr->btr.btr_active = true;
28398e6436dSAnthony Koo 		}
284ded6119eSAmanda Liu 	}
2854562236bSHarry Wentland 
2864562236bSHarry Wentland 	/* BTR set to "not active" so disengage */
28798e6436dSAnthony Koo 	if (!in_out_vrr->btr.btr_active) {
28898e6436dSAnthony Koo 		in_out_vrr->btr.inserted_duration_in_us = 0;
28998e6436dSAnthony Koo 		in_out_vrr->btr.frames_to_insert = 0;
29098e6436dSAnthony Koo 		in_out_vrr->btr.frame_counter = 0;
2914562236bSHarry Wentland 
2924562236bSHarry Wentland 		/* Restore FreeSync */
29398e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min =
29449c70eceSAlvin Lee 			mod_freesync_calc_v_total_from_refresh(stream,
29598e6436dSAnthony Koo 				in_out_vrr->max_refresh_in_uhz);
29698e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max =
29749c70eceSAlvin Lee 			mod_freesync_calc_v_total_from_refresh(stream,
29898e6436dSAnthony Koo 				in_out_vrr->min_refresh_in_uhz);
2994562236bSHarry Wentland 	/* BTR set to "active" so engage */
30098e6436dSAnthony Koo 	} else {
3014562236bSHarry Wentland 
3024562236bSHarry Wentland 		/* Calculate number of midPoint frames that could fit within
3034562236bSHarry Wentland 		 * the render time interval - take ceil of this value
3044562236bSHarry Wentland 		 */
3054562236bSHarry Wentland 		mid_point_frames_ceil = (last_render_time_in_us +
30698e6436dSAnthony Koo 				in_out_vrr->btr.mid_point_in_us - 1) /
30798e6436dSAnthony Koo 					in_out_vrr->btr.mid_point_in_us;
3084562236bSHarry Wentland 
3094562236bSHarry Wentland 		if (mid_point_frames_ceil > 0) {
3104562236bSHarry Wentland 			frame_time_in_us = last_render_time_in_us /
3114562236bSHarry Wentland 				mid_point_frames_ceil;
312d09fec0fSAnthony Koo 			delta_from_mid_point_in_us_1 =
31398e6436dSAnthony Koo 				(in_out_vrr->btr.mid_point_in_us >
3144562236bSHarry Wentland 				frame_time_in_us) ?
31598e6436dSAnthony Koo 				(in_out_vrr->btr.mid_point_in_us - frame_time_in_us) :
31698e6436dSAnthony Koo 				(frame_time_in_us - in_out_vrr->btr.mid_point_in_us);
3174562236bSHarry Wentland 		}
3184562236bSHarry Wentland 
3194562236bSHarry Wentland 		/* Calculate number of midPoint frames that could fit within
3204562236bSHarry Wentland 		 * the render time interval - take floor of this value
3214562236bSHarry Wentland 		 */
3224562236bSHarry Wentland 		mid_point_frames_floor = last_render_time_in_us /
32398e6436dSAnthony Koo 				in_out_vrr->btr.mid_point_in_us;
3244562236bSHarry Wentland 
3254562236bSHarry Wentland 		if (mid_point_frames_floor > 0) {
3264562236bSHarry Wentland 
3274562236bSHarry Wentland 			frame_time_in_us = last_render_time_in_us /
3284562236bSHarry Wentland 				mid_point_frames_floor;
329d09fec0fSAnthony Koo 			delta_from_mid_point_in_us_2 =
33098e6436dSAnthony Koo 				(in_out_vrr->btr.mid_point_in_us >
3314562236bSHarry Wentland 				frame_time_in_us) ?
33298e6436dSAnthony Koo 				(in_out_vrr->btr.mid_point_in_us - frame_time_in_us) :
33398e6436dSAnthony Koo 				(frame_time_in_us - in_out_vrr->btr.mid_point_in_us);
3344562236bSHarry Wentland 		}
3354562236bSHarry Wentland 
3364562236bSHarry Wentland 		/* Choose number of frames to insert based on how close it
3374562236bSHarry Wentland 		 * can get to the mid point of the variable range.
338e4ed4dbbSAnthony Koo 		 *  - Delta for CEIL: delta_from_mid_point_in_us_1
339e4ed4dbbSAnthony Koo 		 *  - Delta for FLOOR: delta_from_mid_point_in_us_2
3404562236bSHarry Wentland 		 */
341*07e388aaSHamza Mahfooz 		if (mid_point_frames_ceil &&
342*07e388aaSHamza Mahfooz 		    (last_render_time_in_us / mid_point_frames_ceil) <
343*07e388aaSHamza Mahfooz 		    in_out_vrr->min_duration_in_us) {
344e4ed4dbbSAnthony Koo 			/* Check for out of range.
345e4ed4dbbSAnthony Koo 			 * If using CEIL produces a value that is out of range,
346e4ed4dbbSAnthony Koo 			 * then we are forced to use FLOOR.
347e4ed4dbbSAnthony Koo 			 */
3484562236bSHarry Wentland 			frames_to_insert = mid_point_frames_floor;
349e4ed4dbbSAnthony Koo 		} else if (mid_point_frames_floor < 2) {
350e4ed4dbbSAnthony Koo 			/* Check if FLOOR would result in non-LFC. In this case
351e4ed4dbbSAnthony Koo 			 * choose to use CEIL
352e4ed4dbbSAnthony Koo 			 */
353e4ed4dbbSAnthony Koo 			frames_to_insert = mid_point_frames_ceil;
354e4ed4dbbSAnthony Koo 		} else if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2) {
355e4ed4dbbSAnthony Koo 			/* If choosing CEIL results in a frame duration that is
356e4ed4dbbSAnthony Koo 			 * closer to the mid point of the range.
357e4ed4dbbSAnthony Koo 			 * Choose CEIL
358e4ed4dbbSAnthony Koo 			 */
359e4ed4dbbSAnthony Koo 			frames_to_insert = mid_point_frames_ceil;
360e4ed4dbbSAnthony Koo 		} else {
361e4ed4dbbSAnthony Koo 			/* If choosing FLOOR results in a frame duration that is
362e4ed4dbbSAnthony Koo 			 * closer to the mid point of the range.
363e4ed4dbbSAnthony Koo 			 * Choose FLOOR
364e4ed4dbbSAnthony Koo 			 */
365e4ed4dbbSAnthony Koo 			frames_to_insert = mid_point_frames_floor;
3669070d18fSAric Cyr 		}
3679070d18fSAric Cyr 
3689070d18fSAric Cyr 		/* Prefer current frame multiplier when BTR is enabled unless it drifts
3699070d18fSAric Cyr 		 * too far from the midpoint
3709070d18fSAric Cyr 		 */
371e4ed4dbbSAnthony Koo 		if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2) {
372e4ed4dbbSAnthony Koo 			delta_from_mid_point_delta_in_us = delta_from_mid_point_in_us_2 -
373e4ed4dbbSAnthony Koo 					delta_from_mid_point_in_us_1;
374e4ed4dbbSAnthony Koo 		} else {
375e4ed4dbbSAnthony Koo 			delta_from_mid_point_delta_in_us = delta_from_mid_point_in_us_1 -
376e4ed4dbbSAnthony Koo 					delta_from_mid_point_in_us_2;
377e4ed4dbbSAnthony Koo 		}
3789070d18fSAric Cyr 		if (in_out_vrr->btr.frames_to_insert != 0 &&
3799070d18fSAric Cyr 				delta_from_mid_point_delta_in_us < BTR_DRIFT_MARGIN) {
3809070d18fSAric Cyr 			if (((last_render_time_in_us / in_out_vrr->btr.frames_to_insert) <
381ded6119eSAmanda Liu 					max_render_time_in_us) &&
3829070d18fSAric Cyr 				((last_render_time_in_us / in_out_vrr->btr.frames_to_insert) >
3839070d18fSAric Cyr 					in_out_vrr->min_duration_in_us))
3849070d18fSAric Cyr 				frames_to_insert = in_out_vrr->btr.frames_to_insert;
3859070d18fSAric Cyr 		}
3864562236bSHarry Wentland 
3874562236bSHarry Wentland 		/* Either we've calculated the number of frames to insert,
3884562236bSHarry Wentland 		 * or we need to insert min duration frames
3894562236bSHarry Wentland 		 */
390*07e388aaSHamza Mahfooz 		if (frames_to_insert &&
391*07e388aaSHamza Mahfooz 		    (last_render_time_in_us / frames_to_insert) <
392a463b263SBayan Zabihiyan 		    in_out_vrr->min_duration_in_us){
393a463b263SBayan Zabihiyan 			frames_to_insert -= (frames_to_insert > 1) ?
394a463b263SBayan Zabihiyan 					1 : 0;
395a463b263SBayan Zabihiyan 		}
396a463b263SBayan Zabihiyan 
3974562236bSHarry Wentland 		if (frames_to_insert > 0)
3984562236bSHarry Wentland 			inserted_frame_duration_in_us = last_render_time_in_us /
3994562236bSHarry Wentland 							frames_to_insert;
4004562236bSHarry Wentland 
401dc4a9049SMario Kleiner 		if (inserted_frame_duration_in_us < in_out_vrr->min_duration_in_us)
402dc4a9049SMario Kleiner 			inserted_frame_duration_in_us = in_out_vrr->min_duration_in_us;
4034562236bSHarry Wentland 
4044562236bSHarry Wentland 		/* Cache the calculated variables */
40598e6436dSAnthony Koo 		in_out_vrr->btr.inserted_duration_in_us =
4064562236bSHarry Wentland 			inserted_frame_duration_in_us;
40798e6436dSAnthony Koo 		in_out_vrr->btr.frames_to_insert = frames_to_insert;
40898e6436dSAnthony Koo 		in_out_vrr->btr.frame_counter = frames_to_insert;
4094562236bSHarry Wentland 	}
4104562236bSHarry Wentland }
4114562236bSHarry 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)4124562236bSHarry Wentland static void apply_fixed_refresh(struct core_freesync *core_freesync,
41398e6436dSAnthony Koo 		const struct dc_stream_state *stream,
41498e6436dSAnthony Koo 		unsigned int last_render_time_in_us,
41598e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
4164562236bSHarry Wentland {
41798e6436dSAnthony Koo 	bool update = false;
41898e6436dSAnthony Koo 	unsigned int max_render_time_in_us = in_out_vrr->max_duration_in_us;
4194562236bSHarry Wentland 
4205ea39850SHaiyi Zhou 	/* Compute the exit refresh rate and exit frame duration */
421de801062SHarmanprit Tatla 	unsigned int exit_refresh_rate_in_milli_hz = ((1000000000/max_render_time_in_us)
422de801062SHarmanprit Tatla 			+ (1000*FIXED_REFRESH_EXIT_MARGIN_IN_HZ));
423de801062SHarmanprit Tatla 	unsigned int exit_frame_duration_in_us = 1000000000/exit_refresh_rate_in_milli_hz;
424de801062SHarmanprit Tatla 
425de801062SHarmanprit Tatla 	if (last_render_time_in_us < exit_frame_duration_in_us) {
42698e6436dSAnthony Koo 		/* Exit Fixed Refresh mode */
42798e6436dSAnthony Koo 		if (in_out_vrr->fixed.fixed_active) {
42898e6436dSAnthony Koo 			in_out_vrr->fixed.frame_counter++;
42998e6436dSAnthony Koo 
43098e6436dSAnthony Koo 			if (in_out_vrr->fixed.frame_counter >
43198e6436dSAnthony Koo 					FIXED_REFRESH_EXIT_FRAME_COUNT) {
43298e6436dSAnthony Koo 				in_out_vrr->fixed.frame_counter = 0;
43398e6436dSAnthony Koo 				in_out_vrr->fixed.fixed_active = false;
43498e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz = 0;
43598e6436dSAnthony Koo 				update = true;
43698e6436dSAnthony Koo 			}
4375dff371aSAric Cyr 		} else
4385dff371aSAric Cyr 			in_out_vrr->fixed.frame_counter = 0;
43998e6436dSAnthony Koo 	} else if (last_render_time_in_us > max_render_time_in_us) {
44098e6436dSAnthony Koo 		/* Enter Fixed Refresh mode */
44198e6436dSAnthony Koo 		if (!in_out_vrr->fixed.fixed_active) {
44298e6436dSAnthony Koo 			in_out_vrr->fixed.frame_counter++;
44398e6436dSAnthony Koo 
44498e6436dSAnthony Koo 			if (in_out_vrr->fixed.frame_counter >
44598e6436dSAnthony Koo 					FIXED_REFRESH_ENTER_FRAME_COUNT) {
44698e6436dSAnthony Koo 				in_out_vrr->fixed.frame_counter = 0;
44798e6436dSAnthony Koo 				in_out_vrr->fixed.fixed_active = true;
44898e6436dSAnthony Koo 				in_out_vrr->fixed.target_refresh_in_uhz =
44998e6436dSAnthony Koo 						in_out_vrr->max_refresh_in_uhz;
45098e6436dSAnthony Koo 				update = true;
45198e6436dSAnthony Koo 			}
4525dff371aSAric Cyr 		} else
4535dff371aSAric Cyr 			in_out_vrr->fixed.frame_counter = 0;
45498e6436dSAnthony Koo 	}
45598e6436dSAnthony Koo 
45698e6436dSAnthony Koo 	if (update) {
45798e6436dSAnthony Koo 		if (in_out_vrr->fixed.fixed_active) {
45898e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
45949c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(
46098e6436dSAnthony Koo 				stream, in_out_vrr->max_refresh_in_uhz);
46198e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
46298e6436dSAnthony Koo 					in_out_vrr->adjust.v_total_min;
46398e6436dSAnthony Koo 		} else {
46498e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
46549c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
466ff6014d6SAnthony Koo 					in_out_vrr->max_refresh_in_uhz);
46798e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
46849c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
469ff6014d6SAnthony Koo 					in_out_vrr->min_refresh_in_uhz);
47098e6436dSAnthony Koo 		}
47198e6436dSAnthony Koo 	}
47298e6436dSAnthony Koo }
47398e6436dSAnthony Koo 
determine_flip_interval_workaround_req(struct mod_vrr_params * in_vrr,unsigned int curr_time_stamp_in_us)4743fe5739dSAngus Wang static void determine_flip_interval_workaround_req(struct mod_vrr_params *in_vrr,
4753fe5739dSAngus Wang 		unsigned int curr_time_stamp_in_us)
4763fe5739dSAngus Wang {
4773fe5739dSAngus Wang 	in_vrr->flip_interval.vsync_to_flip_in_us = curr_time_stamp_in_us -
4783fe5739dSAngus Wang 			in_vrr->flip_interval.v_update_timestamp_in_us;
4793fe5739dSAngus Wang 
4803fe5739dSAngus Wang 	/* Determine conditions for stopping workaround */
4813fe5739dSAngus Wang 	if (in_vrr->flip_interval.flip_interval_workaround_active &&
4823fe5739dSAngus Wang 			in_vrr->flip_interval.vsyncs_between_flip < VSYNCS_BETWEEN_FLIP_THRESHOLD &&
4833fe5739dSAngus Wang 			in_vrr->flip_interval.vsync_to_flip_in_us > FREESYNC_VSYNC_TO_FLIP_DELTA_IN_US) {
4843fe5739dSAngus Wang 		in_vrr->flip_interval.flip_interval_detect_counter = 0;
4853fe5739dSAngus Wang 		in_vrr->flip_interval.program_flip_interval_workaround = true;
4863fe5739dSAngus Wang 		in_vrr->flip_interval.flip_interval_workaround_active = false;
4873fe5739dSAngus Wang 	} else {
4883fe5739dSAngus Wang 		/* Determine conditions for starting workaround */
4893fe5739dSAngus Wang 		if (in_vrr->flip_interval.vsyncs_between_flip >= VSYNCS_BETWEEN_FLIP_THRESHOLD &&
4903fe5739dSAngus Wang 				in_vrr->flip_interval.vsync_to_flip_in_us < FREESYNC_VSYNC_TO_FLIP_DELTA_IN_US) {
4913fe5739dSAngus Wang 			/* Increase flip interval counter we have 2 vsyncs between flips and
4923fe5739dSAngus Wang 			 * vsync to flip interval is less than 500us
4933fe5739dSAngus Wang 			 */
4943fe5739dSAngus Wang 			in_vrr->flip_interval.flip_interval_detect_counter++;
4953fe5739dSAngus Wang 			if (in_vrr->flip_interval.flip_interval_detect_counter > FREESYNC_CONSEC_FLIP_AFTER_VSYNC) {
4963fe5739dSAngus Wang 				/* Start workaround if we detect 5 consecutive instances of the above case */
4973fe5739dSAngus Wang 				in_vrr->flip_interval.program_flip_interval_workaround = true;
4983fe5739dSAngus Wang 				in_vrr->flip_interval.flip_interval_workaround_active = true;
4993fe5739dSAngus Wang 			}
5003fe5739dSAngus Wang 		} else {
5013fe5739dSAngus Wang 			/* Reset the flip interval counter if we condition is no longer met */
5023fe5739dSAngus Wang 			in_vrr->flip_interval.flip_interval_detect_counter = 0;
5033fe5739dSAngus Wang 		}
5043fe5739dSAngus Wang 	}
5053fe5739dSAngus Wang 
5063fe5739dSAngus Wang 	in_vrr->flip_interval.vsyncs_between_flip = 0;
5073fe5739dSAngus Wang }
5083fe5739dSAngus 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)50998e6436dSAnthony Koo static bool vrr_settings_require_update(struct core_freesync *core_freesync,
51098e6436dSAnthony Koo 		struct mod_freesync_config *in_config,
51198e6436dSAnthony Koo 		unsigned int min_refresh_in_uhz,
51298e6436dSAnthony Koo 		unsigned int max_refresh_in_uhz,
51398e6436dSAnthony Koo 		struct mod_vrr_params *in_vrr)
51498e6436dSAnthony Koo {
51598e6436dSAnthony Koo 	if (in_vrr->state != in_config->state) {
51698e6436dSAnthony Koo 		return true;
51798e6436dSAnthony Koo 	} else if (in_vrr->state == VRR_STATE_ACTIVE_FIXED &&
51898e6436dSAnthony Koo 			in_vrr->fixed.target_refresh_in_uhz !=
519d2bacc38SHaiyi Zhou 					in_config->fixed_refresh_in_uhz) {
52098e6436dSAnthony Koo 		return true;
52198e6436dSAnthony Koo 	} else if (in_vrr->min_refresh_in_uhz != min_refresh_in_uhz) {
52298e6436dSAnthony Koo 		return true;
52398e6436dSAnthony Koo 	} else if (in_vrr->max_refresh_in_uhz != max_refresh_in_uhz) {
52498e6436dSAnthony Koo 		return true;
52598e6436dSAnthony Koo 	}
52698e6436dSAnthony Koo 
52798e6436dSAnthony Koo 	return false;
52898e6436dSAnthony Koo }
52998e6436dSAnthony Koo 
mod_freesync_get_vmin_vmax(struct mod_freesync * mod_freesync,const struct dc_stream_state * stream,unsigned int * vmin,unsigned int * vmax)53098e6436dSAnthony Koo bool mod_freesync_get_vmin_vmax(struct mod_freesync *mod_freesync,
53198e6436dSAnthony Koo 		const struct dc_stream_state *stream,
53298e6436dSAnthony Koo 		unsigned int *vmin,
53398e6436dSAnthony Koo 		unsigned int *vmax)
53498e6436dSAnthony Koo {
53598e6436dSAnthony Koo 	*vmin = stream->adjust.v_total_min;
53698e6436dSAnthony Koo 	*vmax = stream->adjust.v_total_max;
53798e6436dSAnthony Koo 
53898e6436dSAnthony Koo 	return true;
53998e6436dSAnthony Koo }
54098e6436dSAnthony Koo 
mod_freesync_get_v_position(struct mod_freesync * mod_freesync,struct dc_stream_state * stream,unsigned int * nom_v_pos,unsigned int * v_pos)54198e6436dSAnthony Koo bool mod_freesync_get_v_position(struct mod_freesync *mod_freesync,
54298e6436dSAnthony Koo 		struct dc_stream_state *stream,
54398e6436dSAnthony Koo 		unsigned int *nom_v_pos,
54498e6436dSAnthony Koo 		unsigned int *v_pos)
54598e6436dSAnthony Koo {
54698e6436dSAnthony Koo 	struct core_freesync *core_freesync = NULL;
54798e6436dSAnthony Koo 	struct crtc_position position;
54898e6436dSAnthony Koo 
54998e6436dSAnthony Koo 	if (mod_freesync == NULL)
55098e6436dSAnthony Koo 		return false;
55198e6436dSAnthony Koo 
55298e6436dSAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
55398e6436dSAnthony Koo 
55498e6436dSAnthony Koo 	if (dc_stream_get_crtc_position(core_freesync->dc, &stream, 1,
55598e6436dSAnthony Koo 					&position.vertical_count,
55698e6436dSAnthony Koo 					&position.nominal_vcount)) {
55798e6436dSAnthony Koo 
55898e6436dSAnthony Koo 		*nom_v_pos = position.nominal_vcount;
55998e6436dSAnthony Koo 		*v_pos = position.vertical_count;
56098e6436dSAnthony Koo 
56198e6436dSAnthony Koo 		return true;
56298e6436dSAnthony Koo 	}
56398e6436dSAnthony Koo 
56498e6436dSAnthony Koo 	return false;
56598e6436dSAnthony Koo }
56698e6436dSAnthony Koo 
build_vrr_infopacket_data_v1(const struct mod_vrr_params * vrr,struct dc_info_packet * infopacket,bool freesync_on_desktop)567d2bacc38SHaiyi Zhou static void build_vrr_infopacket_data_v1(const struct mod_vrr_params *vrr,
5684932d176Shvanzyll 		struct dc_info_packet *infopacket,
5694932d176Shvanzyll 		bool freesync_on_desktop)
570ca35899cSBayan Zabihiyan {
571a9f54ce3SAhmad Othman 	/* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
572a9f54ce3SAhmad Othman 	infopacket->sb[1] = 0x1A;
573ca35899cSBayan Zabihiyan 
574a9f54ce3SAhmad Othman 	/* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
575a9f54ce3SAhmad Othman 	infopacket->sb[2] = 0x00;
576e03868ecSReza Amini 
577a9f54ce3SAhmad Othman 	/* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
578a9f54ce3SAhmad Othman 	infopacket->sb[3] = 0x00;
579a9f54ce3SAhmad Othman 
580a9f54ce3SAhmad Othman 	/* PB4 = Reserved */
581a9f54ce3SAhmad Othman 
582a9f54ce3SAhmad Othman 	/* PB5 = Reserved */
583a9f54ce3SAhmad Othman 
584a9f54ce3SAhmad Othman 	/* PB6 = [Bits 7:3 = Reserved] */
585a9f54ce3SAhmad Othman 
586a9f54ce3SAhmad Othman 	/* PB6 = [Bit 0 = FreeSync Supported] */
587a9f54ce3SAhmad Othman 	if (vrr->state != VRR_STATE_UNSUPPORTED)
588a9f54ce3SAhmad Othman 		infopacket->sb[6] |= 0x01;
589a9f54ce3SAhmad Othman 
590a9f54ce3SAhmad Othman 	/* PB6 = [Bit 1 = FreeSync Enabled] */
591a9f54ce3SAhmad Othman 	if (vrr->state != VRR_STATE_DISABLED &&
592a9f54ce3SAhmad Othman 			vrr->state != VRR_STATE_UNSUPPORTED)
593a9f54ce3SAhmad Othman 		infopacket->sb[6] |= 0x02;
594a9f54ce3SAhmad Othman 
5954932d176Shvanzyll 	if (freesync_on_desktop) {
596a9f54ce3SAhmad Othman 		/* PB6 = [Bit 2 = FreeSync Active] */
5970774e08aSHarry VanZyllDeJong 		if (vrr->state != VRR_STATE_DISABLED &&
5980774e08aSHarry VanZyllDeJong 			vrr->state != VRR_STATE_UNSUPPORTED)
599a9f54ce3SAhmad Othman 			infopacket->sb[6] |= 0x04;
6004932d176Shvanzyll 	} else {
6014932d176Shvanzyll 		if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
6024932d176Shvanzyll 			vrr->state == VRR_STATE_ACTIVE_FIXED)
6034932d176Shvanzyll 			infopacket->sb[6] |= 0x04;
6044932d176Shvanzyll 	}
605a9f54ce3SAhmad Othman 
606d2bacc38SHaiyi Zhou 	// For v1 & 2 infoframes program nominal if non-fs mode, otherwise full range
607a9f54ce3SAhmad Othman 	/* PB7 = FreeSync Minimum refresh rate (Hz) */
608d2bacc38SHaiyi Zhou 	if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
609d2bacc38SHaiyi Zhou 			vrr->state == VRR_STATE_ACTIVE_FIXED) {
6103fc6376eSAric Cyr 		infopacket->sb[7] = (unsigned char)((vrr->min_refresh_in_uhz + 500000) / 1000000);
611d2bacc38SHaiyi Zhou 	} else {
612d2bacc38SHaiyi Zhou 		infopacket->sb[7] = (unsigned char)((vrr->max_refresh_in_uhz + 500000) / 1000000);
613d2bacc38SHaiyi Zhou 	}
614a9f54ce3SAhmad Othman 
615a9f54ce3SAhmad Othman 	/* PB8 = FreeSync Maximum refresh rate (Hz)
616a9f54ce3SAhmad Othman 	 * Note: We should never go above the field rate of the mode timing set.
617a9f54ce3SAhmad Othman 	 */
6183fc6376eSAric Cyr 	infopacket->sb[8] = (unsigned char)((vrr->max_refresh_in_uhz + 500000) / 1000000);
619d2bacc38SHaiyi Zhou }
620d2bacc38SHaiyi Zhou 
build_vrr_infopacket_data_v3(const struct mod_vrr_params * vrr,struct dc_info_packet * infopacket,bool freesync_on_desktop)621d2bacc38SHaiyi Zhou static void build_vrr_infopacket_data_v3(const struct mod_vrr_params *vrr,
622689008e1SDillon Varone 		struct dc_info_packet *infopacket,
623689008e1SDillon Varone 		bool freesync_on_desktop)
624d2bacc38SHaiyi Zhou {
6259bc41626SReza Amini 	unsigned int min_refresh;
6269bc41626SReza Amini 	unsigned int max_refresh;
6279bc41626SReza Amini 	unsigned int fixed_refresh;
6289bc41626SReza Amini 	unsigned int min_programmed;
6299bc41626SReza Amini 	unsigned int max_programmed;
6309bc41626SReza Amini 
631d2bacc38SHaiyi Zhou 	/* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
632d2bacc38SHaiyi Zhou 	infopacket->sb[1] = 0x1A;
633d2bacc38SHaiyi Zhou 
634d2bacc38SHaiyi Zhou 	/* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
635d2bacc38SHaiyi Zhou 	infopacket->sb[2] = 0x00;
636d2bacc38SHaiyi Zhou 
637d2bacc38SHaiyi Zhou 	/* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
638d2bacc38SHaiyi Zhou 	infopacket->sb[3] = 0x00;
639d2bacc38SHaiyi Zhou 
640d2bacc38SHaiyi Zhou 	/* PB4 = Reserved */
641d2bacc38SHaiyi Zhou 
642d2bacc38SHaiyi Zhou 	/* PB5 = Reserved */
643d2bacc38SHaiyi Zhou 
644d2bacc38SHaiyi Zhou 	/* PB6 = [Bits 7:3 = Reserved] */
645d2bacc38SHaiyi Zhou 
646d2bacc38SHaiyi Zhou 	/* PB6 = [Bit 0 = FreeSync Supported] */
647d2bacc38SHaiyi Zhou 	if (vrr->state != VRR_STATE_UNSUPPORTED)
648d2bacc38SHaiyi Zhou 		infopacket->sb[6] |= 0x01;
649d2bacc38SHaiyi Zhou 
650d2bacc38SHaiyi Zhou 	/* PB6 = [Bit 1 = FreeSync Enabled] */
651d2bacc38SHaiyi Zhou 	if (vrr->state != VRR_STATE_DISABLED &&
652d2bacc38SHaiyi Zhou 			vrr->state != VRR_STATE_UNSUPPORTED)
653d2bacc38SHaiyi Zhou 		infopacket->sb[6] |= 0x02;
654d2bacc38SHaiyi Zhou 
655d2bacc38SHaiyi Zhou 	/* PB6 = [Bit 2 = FreeSync Active] */
656689008e1SDillon Varone 	if (freesync_on_desktop) {
657689008e1SDillon Varone 		if (vrr->state != VRR_STATE_DISABLED &&
658689008e1SDillon Varone 			vrr->state != VRR_STATE_UNSUPPORTED)
659689008e1SDillon Varone 			infopacket->sb[6] |= 0x04;
660689008e1SDillon Varone 	} else {
661d2bacc38SHaiyi Zhou 		if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
662d2bacc38SHaiyi Zhou 			vrr->state == VRR_STATE_ACTIVE_FIXED)
663d2bacc38SHaiyi Zhou 			infopacket->sb[6] |= 0x04;
664689008e1SDillon Varone 	}
665d2bacc38SHaiyi Zhou 
6669bc41626SReza Amini 	min_refresh = (vrr->min_refresh_in_uhz + 500000) / 1000000;
6679bc41626SReza Amini 	max_refresh = (vrr->max_refresh_in_uhz + 500000) / 1000000;
6689bc41626SReza Amini 	fixed_refresh = (vrr->fixed_refresh_in_uhz + 500000) / 1000000;
6699bc41626SReza Amini 
6709bc41626SReza Amini 	min_programmed = (vrr->state == VRR_STATE_ACTIVE_FIXED) ? fixed_refresh :
6719bc41626SReza Amini 			(vrr->state == VRR_STATE_ACTIVE_VARIABLE) ? min_refresh :
672983bcb4cSAMD\ramini 			(vrr->state == VRR_STATE_INACTIVE) ? min_refresh :
6739bc41626SReza Amini 			max_refresh; // Non-fs case, program nominal range
6749bc41626SReza Amini 
6759bc41626SReza Amini 	max_programmed = (vrr->state == VRR_STATE_ACTIVE_FIXED) ? fixed_refresh :
6769bc41626SReza Amini 			(vrr->state == VRR_STATE_ACTIVE_VARIABLE) ? max_refresh :
6779bc41626SReza Amini 			max_refresh;// Non-fs case, program nominal range
6789bc41626SReza Amini 
679d2bacc38SHaiyi Zhou 	/* PB7 = FreeSync Minimum refresh rate (Hz) */
6809bc41626SReza Amini 	infopacket->sb[7] = min_programmed & 0xFF;
6819bc41626SReza Amini 
682d2bacc38SHaiyi Zhou 	/* PB8 = FreeSync Maximum refresh rate (Hz) */
6839bc41626SReza Amini 	infopacket->sb[8] = max_programmed & 0xFF;
6849bc41626SReza Amini 
685983bcb4cSAMD\ramini 	/* PB11 : MSB FreeSync Minimum refresh rate [Hz] - bits 9:8 */
686983bcb4cSAMD\ramini 	infopacket->sb[11] = (min_programmed >> 8) & 0x03;
6879bc41626SReza Amini 
688983bcb4cSAMD\ramini 	/* PB12 : MSB FreeSync Maximum refresh rate [Hz] - bits 9:8 */
689983bcb4cSAMD\ramini 	infopacket->sb[12] = (max_programmed >> 8) & 0x03;
690983bcb4cSAMD\ramini 
691983bcb4cSAMD\ramini 	/* PB16 : Reserved bits 7:1, FixedRate bit 0 */
692983bcb4cSAMD\ramini 	infopacket->sb[16] = (vrr->state == VRR_STATE_ACTIVE_FIXED) ? 1 : 0;
693a9f54ce3SAhmad Othman }
694a9f54ce3SAhmad Othman 
build_vrr_infopacket_fs2_data(enum color_transfer_func app_tf,struct dc_info_packet * infopacket)695a9f54ce3SAhmad Othman static void build_vrr_infopacket_fs2_data(enum color_transfer_func app_tf,
696a9f54ce3SAhmad Othman 		struct dc_info_packet *infopacket)
697a9f54ce3SAhmad Othman {
698a9f54ce3SAhmad Othman 	if (app_tf != TRANSFER_FUNC_UNKNOWN) {
699a9f54ce3SAhmad Othman 		infopacket->valid = true;
700a9f54ce3SAhmad Othman 
70152b5432cSMike Hsieh 		if (app_tf != TRANSFER_FUNC_PQ2084) {
702a9f54ce3SAhmad Othman 			infopacket->sb[6] |= 0x08;  // PB6 = [Bit 3 = Native Color Active]
70352b5432cSMike Hsieh 			if (app_tf == TRANSFER_FUNC_GAMMA_22)
704a9f54ce3SAhmad Othman 				infopacket->sb[9] |= 0x04;  // PB6 = [Bit 2 = Gamma 2.2 EOTF Active]
705a9f54ce3SAhmad Othman 		}
706a9f54ce3SAhmad Othman 	}
707ca35899cSBayan Zabihiyan }
708ca35899cSBayan Zabihiyan 
build_vrr_infopacket_header_v1(enum signal_type signal,struct dc_info_packet * infopacket,unsigned int * payload_size)709c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_header_v1(enum signal_type signal,
710c2791297SSivapiriyanKumarasamy 		struct dc_info_packet *infopacket,
711c2791297SSivapiriyanKumarasamy 		unsigned int *payload_size)
71298e6436dSAnthony Koo {
713c2791297SSivapiriyanKumarasamy 	if (dc_is_hdmi_signal(signal)) {
7144562236bSHarry Wentland 
71598e6436dSAnthony Koo 		/* HEADER */
7164562236bSHarry Wentland 
71798e6436dSAnthony Koo 		/* HB0  = Packet Type = 0x83 (Source Product
71898e6436dSAnthony Koo 		 *	  Descriptor InfoFrame)
71998e6436dSAnthony Koo 		 */
72098e6436dSAnthony Koo 		infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
7214562236bSHarry Wentland 
72298e6436dSAnthony Koo 		/* HB1  = Version = 0x01 */
72398e6436dSAnthony Koo 		infopacket->hb1 = 0x01;
7244562236bSHarry Wentland 
72598e6436dSAnthony Koo 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x08] */
72698e6436dSAnthony Koo 		infopacket->hb2 = 0x08;
72798e6436dSAnthony Koo 
728c2791297SSivapiriyanKumarasamy 		*payload_size = 0x08;
72998e6436dSAnthony Koo 
730c2791297SSivapiriyanKumarasamy 	} else if (dc_is_dp_signal(signal)) {
73198e6436dSAnthony Koo 
73298e6436dSAnthony Koo 		/* HEADER */
73398e6436dSAnthony Koo 
73498e6436dSAnthony Koo 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
73598e6436dSAnthony Koo 		 *	  when used to associate audio related info packets
73698e6436dSAnthony Koo 		 */
73798e6436dSAnthony Koo 		infopacket->hb0 = 0x00;
73898e6436dSAnthony Koo 
73998e6436dSAnthony Koo 		/* HB1  = Packet Type = 0x83 (Source Product
74098e6436dSAnthony Koo 		 *	  Descriptor InfoFrame)
74198e6436dSAnthony Koo 		 */
74298e6436dSAnthony Koo 		infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
74398e6436dSAnthony Koo 
74498e6436dSAnthony Koo 		/* HB2  = [Bits 7:0 = Least significant eight bits -
74598e6436dSAnthony Koo 		 *	  For INFOFRAME, the value must be 1Bh]
74698e6436dSAnthony Koo 		 */
74798e6436dSAnthony Koo 		infopacket->hb2 = 0x1B;
74898e6436dSAnthony Koo 
74998e6436dSAnthony Koo 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x1]
75098e6436dSAnthony Koo 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
75198e6436dSAnthony Koo 		 */
75298e6436dSAnthony Koo 		infopacket->hb3 = 0x04;
75398e6436dSAnthony Koo 
754c2791297SSivapiriyanKumarasamy 		*payload_size = 0x1B;
755c2791297SSivapiriyanKumarasamy 	}
7564562236bSHarry Wentland }
7574562236bSHarry Wentland 
build_vrr_infopacket_header_v2(enum signal_type signal,struct dc_info_packet * infopacket,unsigned int * payload_size)758c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_header_v2(enum signal_type signal,
759c2791297SSivapiriyanKumarasamy 		struct dc_info_packet *infopacket,
760c2791297SSivapiriyanKumarasamy 		unsigned int *payload_size)
761c2791297SSivapiriyanKumarasamy {
762c2791297SSivapiriyanKumarasamy 	if (dc_is_hdmi_signal(signal)) {
763c2791297SSivapiriyanKumarasamy 
764c2791297SSivapiriyanKumarasamy 		/* HEADER */
765c2791297SSivapiriyanKumarasamy 
766c2791297SSivapiriyanKumarasamy 		/* HB0  = Packet Type = 0x83 (Source Product
767c2791297SSivapiriyanKumarasamy 		 *	  Descriptor InfoFrame)
768c2791297SSivapiriyanKumarasamy 		 */
769c2791297SSivapiriyanKumarasamy 		infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
770c2791297SSivapiriyanKumarasamy 
771c2791297SSivapiriyanKumarasamy 		/* HB1  = Version = 0x02 */
772c2791297SSivapiriyanKumarasamy 		infopacket->hb1 = 0x02;
773c2791297SSivapiriyanKumarasamy 
774c2791297SSivapiriyanKumarasamy 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x09] */
775c2791297SSivapiriyanKumarasamy 		infopacket->hb2 = 0x09;
776c2791297SSivapiriyanKumarasamy 
77705911836SLeo Ma 		*payload_size = 0x09;
778c2791297SSivapiriyanKumarasamy 	} else if (dc_is_dp_signal(signal)) {
779c2791297SSivapiriyanKumarasamy 
780c2791297SSivapiriyanKumarasamy 		/* HEADER */
781c2791297SSivapiriyanKumarasamy 
782c2791297SSivapiriyanKumarasamy 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
783c2791297SSivapiriyanKumarasamy 		 *	  when used to associate audio related info packets
784c2791297SSivapiriyanKumarasamy 		 */
785c2791297SSivapiriyanKumarasamy 		infopacket->hb0 = 0x00;
786c2791297SSivapiriyanKumarasamy 
787c2791297SSivapiriyanKumarasamy 		/* HB1  = Packet Type = 0x83 (Source Product
788c2791297SSivapiriyanKumarasamy 		 *	  Descriptor InfoFrame)
789c2791297SSivapiriyanKumarasamy 		 */
790c2791297SSivapiriyanKumarasamy 		infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
791c2791297SSivapiriyanKumarasamy 
792c2791297SSivapiriyanKumarasamy 		/* HB2  = [Bits 7:0 = Least significant eight bits -
793c2791297SSivapiriyanKumarasamy 		 *	  For INFOFRAME, the value must be 1Bh]
794c2791297SSivapiriyanKumarasamy 		 */
795c2791297SSivapiriyanKumarasamy 		infopacket->hb2 = 0x1B;
796c2791297SSivapiriyanKumarasamy 
797c2791297SSivapiriyanKumarasamy 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x2]
798c2791297SSivapiriyanKumarasamy 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
799c2791297SSivapiriyanKumarasamy 		 */
800c2791297SSivapiriyanKumarasamy 		infopacket->hb3 = 0x08;
801c2791297SSivapiriyanKumarasamy 
802c2791297SSivapiriyanKumarasamy 		*payload_size = 0x1B;
803c2791297SSivapiriyanKumarasamy 	}
804c2791297SSivapiriyanKumarasamy }
805c2791297SSivapiriyanKumarasamy 
build_vrr_infopacket_header_v3(enum signal_type signal,struct dc_info_packet * infopacket,unsigned int * payload_size)8069bc41626SReza Amini static void build_vrr_infopacket_header_v3(enum signal_type signal,
8079bc41626SReza Amini 		struct dc_info_packet *infopacket,
8089bc41626SReza Amini 		unsigned int *payload_size)
8099bc41626SReza Amini {
8109bc41626SReza Amini 	unsigned char version;
8119bc41626SReza Amini 
8129bc41626SReza Amini 	version = 3;
8139bc41626SReza Amini 	if (dc_is_hdmi_signal(signal)) {
8149bc41626SReza Amini 
8159bc41626SReza Amini 		/* HEADER */
8169bc41626SReza Amini 
8179bc41626SReza Amini 		/* HB0  = Packet Type = 0x83 (Source Product
8189bc41626SReza Amini 		 *	  Descriptor InfoFrame)
8199bc41626SReza Amini 		 */
8209bc41626SReza Amini 		infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
8219bc41626SReza Amini 
8229bc41626SReza Amini 		/* HB1  = Version = 0x03 */
8239bc41626SReza Amini 		infopacket->hb1 = version;
8249bc41626SReza Amini 
8259bc41626SReza Amini 		/* HB2  = [Bits 7:5 = 0] [Bits 4:0 = Length] */
82605911836SLeo Ma 		infopacket->hb2 = 0x10;
8279bc41626SReza Amini 
82805911836SLeo Ma 		*payload_size = 0x10;
8299bc41626SReza Amini 	} else if (dc_is_dp_signal(signal)) {
8309bc41626SReza Amini 
8319bc41626SReza Amini 		/* HEADER */
8329bc41626SReza Amini 
8339bc41626SReza Amini 		/* HB0  = Secondary-data Packet ID = 0 - Only non-zero
8349bc41626SReza Amini 		 *	  when used to associate audio related info packets
8359bc41626SReza Amini 		 */
8369bc41626SReza Amini 		infopacket->hb0 = 0x00;
8379bc41626SReza Amini 
8389bc41626SReza Amini 		/* HB1  = Packet Type = 0x83 (Source Product
8399bc41626SReza Amini 		 *	  Descriptor InfoFrame)
8409bc41626SReza Amini 		 */
8419bc41626SReza Amini 		infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
8429bc41626SReza Amini 
8439bc41626SReza Amini 		/* HB2  = [Bits 7:0 = Least significant eight bits -
8449bc41626SReza Amini 		 *	  For INFOFRAME, the value must be 1Bh]
8459bc41626SReza Amini 		 */
8469bc41626SReza Amini 		infopacket->hb2 = 0x1B;
8479bc41626SReza Amini 
8489bc41626SReza Amini 		/* HB3  = [Bits 7:2 = INFOFRAME SDP Version Number = 0x2]
8499bc41626SReza Amini 		 *	  [Bits 1:0 = Most significant two bits = 0x00]
8509bc41626SReza Amini 		 */
8519bc41626SReza Amini 
8529bc41626SReza Amini 		infopacket->hb3 = (version & 0x3F) << 2;
8539bc41626SReza Amini 
8549bc41626SReza Amini 		*payload_size = 0x1B;
8559bc41626SReza Amini 	}
8569bc41626SReza Amini }
8579bc41626SReza Amini 
build_vrr_infopacket_checksum(unsigned int * payload_size,struct dc_info_packet * infopacket)858c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_checksum(unsigned int *payload_size,
859c2791297SSivapiriyanKumarasamy 		struct dc_info_packet *infopacket)
860c2791297SSivapiriyanKumarasamy {
86198e6436dSAnthony Koo 	/* Calculate checksum */
862c2791297SSivapiriyanKumarasamy 	unsigned int idx = 0;
863c2791297SSivapiriyanKumarasamy 	unsigned char checksum = 0;
864c2791297SSivapiriyanKumarasamy 
86598e6436dSAnthony Koo 	checksum += infopacket->hb0;
86698e6436dSAnthony Koo 	checksum += infopacket->hb1;
86798e6436dSAnthony Koo 	checksum += infopacket->hb2;
86898e6436dSAnthony Koo 	checksum += infopacket->hb3;
86998e6436dSAnthony Koo 
870c2791297SSivapiriyanKumarasamy 	for (idx = 1; idx <= *payload_size; idx++)
87198e6436dSAnthony Koo 		checksum += infopacket->sb[idx];
87298e6436dSAnthony Koo 
87398e6436dSAnthony Koo 	/* PB0 = Checksum (one byte complement) */
87498e6436dSAnthony Koo 	infopacket->sb[0] = (unsigned char)(0x100 - checksum);
87598e6436dSAnthony Koo 
87698e6436dSAnthony Koo 	infopacket->valid = true;
87798e6436dSAnthony Koo }
87898e6436dSAnthony Koo 
build_vrr_infopacket_v1(enum signal_type signal,const struct mod_vrr_params * vrr,struct dc_info_packet * infopacket,bool freesync_on_desktop)879c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_v1(enum signal_type signal,
880c2791297SSivapiriyanKumarasamy 		const struct mod_vrr_params *vrr,
8814932d176Shvanzyll 		struct dc_info_packet *infopacket,
8824932d176Shvanzyll 		bool freesync_on_desktop)
883c2791297SSivapiriyanKumarasamy {
884c2791297SSivapiriyanKumarasamy 	/* SPD info packet for FreeSync */
885c2791297SSivapiriyanKumarasamy 	unsigned int payload_size = 0;
886c2791297SSivapiriyanKumarasamy 
887c2791297SSivapiriyanKumarasamy 	build_vrr_infopacket_header_v1(signal, infopacket, &payload_size);
8884932d176Shvanzyll 	build_vrr_infopacket_data_v1(vrr, infopacket, freesync_on_desktop);
889c2791297SSivapiriyanKumarasamy 	build_vrr_infopacket_checksum(&payload_size, infopacket);
890c2791297SSivapiriyanKumarasamy 
891c2791297SSivapiriyanKumarasamy 	infopacket->valid = true;
892c2791297SSivapiriyanKumarasamy }
893c2791297SSivapiriyanKumarasamy 
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)894c2791297SSivapiriyanKumarasamy static void build_vrr_infopacket_v2(enum signal_type signal,
895c2791297SSivapiriyanKumarasamy 		const struct mod_vrr_params *vrr,
896672e78caSNathan Chancellor 		enum color_transfer_func app_tf,
8974932d176Shvanzyll 		struct dc_info_packet *infopacket,
8984932d176Shvanzyll 		bool freesync_on_desktop)
899c2791297SSivapiriyanKumarasamy {
900c2791297SSivapiriyanKumarasamy 	unsigned int payload_size = 0;
901c2791297SSivapiriyanKumarasamy 
902c2791297SSivapiriyanKumarasamy 	build_vrr_infopacket_header_v2(signal, infopacket, &payload_size);
9034932d176Shvanzyll 	build_vrr_infopacket_data_v1(vrr, infopacket, freesync_on_desktop);
904d2bacc38SHaiyi Zhou 
905d2bacc38SHaiyi Zhou 	build_vrr_infopacket_fs2_data(app_tf, infopacket);
906d2bacc38SHaiyi Zhou 
907d2bacc38SHaiyi Zhou 	build_vrr_infopacket_checksum(&payload_size, infopacket);
908d2bacc38SHaiyi Zhou 
909d2bacc38SHaiyi Zhou 	infopacket->valid = true;
910d2bacc38SHaiyi Zhou }
911d2bacc38SHaiyi 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)912d2bacc38SHaiyi Zhou static void build_vrr_infopacket_v3(enum signal_type signal,
913d2bacc38SHaiyi Zhou 		const struct mod_vrr_params *vrr,
914d2bacc38SHaiyi Zhou 		enum color_transfer_func app_tf,
915689008e1SDillon Varone 		struct dc_info_packet *infopacket,
916689008e1SDillon Varone 		bool freesync_on_desktop)
917d2bacc38SHaiyi Zhou {
918d2bacc38SHaiyi Zhou 	unsigned int payload_size = 0;
919d2bacc38SHaiyi Zhou 
9209bc41626SReza Amini 	build_vrr_infopacket_header_v3(signal, infopacket, &payload_size);
921689008e1SDillon Varone 	build_vrr_infopacket_data_v3(vrr, infopacket, freesync_on_desktop);
922c2791297SSivapiriyanKumarasamy 
923672e78caSNathan Chancellor 	build_vrr_infopacket_fs2_data(app_tf, infopacket);
924c2791297SSivapiriyanKumarasamy 
925c2791297SSivapiriyanKumarasamy 	build_vrr_infopacket_checksum(&payload_size, infopacket);
926c2791297SSivapiriyanKumarasamy 
927c2791297SSivapiriyanKumarasamy 	infopacket->valid = true;
928c2791297SSivapiriyanKumarasamy }
929c2791297SSivapiriyanKumarasamy 
build_vrr_infopacket_sdp_v1_3(enum vrr_packet_type packet_type,struct dc_info_packet * infopacket)9304cda3243SMax.Tseng static void build_vrr_infopacket_sdp_v1_3(enum vrr_packet_type packet_type,
9314cda3243SMax.Tseng 										struct dc_info_packet *infopacket)
9324cda3243SMax.Tseng {
9334cda3243SMax.Tseng 	uint8_t idx = 0, size = 0;
9344cda3243SMax.Tseng 
9354cda3243SMax.Tseng 	size = ((packet_type == PACKET_TYPE_FS_V1) ? 0x08 :
9364cda3243SMax.Tseng 			(packet_type == PACKET_TYPE_FS_V3) ? 0x10 :
9374cda3243SMax.Tseng 												0x09);
9384cda3243SMax.Tseng 
9394cda3243SMax.Tseng 	for (idx = infopacket->hb2; idx > 1; idx--) // Data Byte Count: 0x1B
9404cda3243SMax.Tseng 		infopacket->sb[idx] = infopacket->sb[idx-1];
9414cda3243SMax.Tseng 
9424cda3243SMax.Tseng 	infopacket->sb[1] = size;                         // Length
9434cda3243SMax.Tseng 	infopacket->sb[0] = (infopacket->hb3 >> 2) & 0x3F;//Version
9444cda3243SMax.Tseng 	infopacket->hb3   = (0x13 << 2);                  // Header,SDP 1.3
9454cda3243SMax.Tseng 	infopacket->hb2   = 0x1D;
9464cda3243SMax.Tseng }
9474cda3243SMax.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)948c2791297SSivapiriyanKumarasamy void mod_freesync_build_vrr_infopacket(struct mod_freesync *mod_freesync,
949c2791297SSivapiriyanKumarasamy 		const struct dc_stream_state *stream,
950c2791297SSivapiriyanKumarasamy 		const struct mod_vrr_params *vrr,
951c2791297SSivapiriyanKumarasamy 		enum vrr_packet_type packet_type,
952672e78caSNathan Chancellor 		enum color_transfer_func app_tf,
9534cda3243SMax.Tseng 		struct dc_info_packet *infopacket,
9544cda3243SMax.Tseng 		bool pack_sdp_v1_3)
955c2791297SSivapiriyanKumarasamy {
956ca35899cSBayan Zabihiyan 	/* SPD info packet for FreeSync
957ca35899cSBayan Zabihiyan 	 * VTEM info packet for HdmiVRR
958ca35899cSBayan Zabihiyan 	 * Check if Freesync is supported. Return if false. If true,
959c2791297SSivapiriyanKumarasamy 	 * set the corresponding bit in the info packet
960c2791297SSivapiriyanKumarasamy 	 */
961486b7680SJaehyun Chung 	if (!vrr->send_info_frame)
962c2791297SSivapiriyanKumarasamy 		return;
963c2791297SSivapiriyanKumarasamy 
964c2791297SSivapiriyanKumarasamy 	switch (packet_type) {
965d2bacc38SHaiyi Zhou 	case PACKET_TYPE_FS_V3:
966627441f5SAric Cyr 		build_vrr_infopacket_v3(stream->signal, vrr, app_tf, infopacket, stream->freesync_on_desktop);
967d2bacc38SHaiyi Zhou 		break;
968d2bacc38SHaiyi Zhou 	case PACKET_TYPE_FS_V2:
969627441f5SAric Cyr 		build_vrr_infopacket_v2(stream->signal, vrr, app_tf, infopacket, stream->freesync_on_desktop);
970c2791297SSivapiriyanKumarasamy 		break;
971ecd0136bSHarmanprit Tatla 	case PACKET_TYPE_VRR:
972d2bacc38SHaiyi Zhou 	case PACKET_TYPE_FS_V1:
973c2791297SSivapiriyanKumarasamy 	default:
974627441f5SAric Cyr 		build_vrr_infopacket_v1(stream->signal, vrr, infopacket, stream->freesync_on_desktop);
975c2791297SSivapiriyanKumarasamy 	}
9764cda3243SMax.Tseng 
9774cda3243SMax.Tseng 	if (true == pack_sdp_v1_3 &&
9784cda3243SMax.Tseng 		true == dc_is_dp_signal(stream->signal) &&
9794cda3243SMax.Tseng 		packet_type != PACKET_TYPE_VRR &&
9804cda3243SMax.Tseng 		packet_type != PACKET_TYPE_VTEM)
9814cda3243SMax.Tseng 		build_vrr_infopacket_sdp_v1_3(packet_type, infopacket);
982c2791297SSivapiriyanKumarasamy }
983c2791297SSivapiriyanKumarasamy 
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)98498e6436dSAnthony Koo void mod_freesync_build_vrr_params(struct mod_freesync *mod_freesync,
98598e6436dSAnthony Koo 		const struct dc_stream_state *stream,
98698e6436dSAnthony Koo 		struct mod_freesync_config *in_config,
98798e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
9884562236bSHarry Wentland {
9897a1c37e0SAnthony Koo 	struct core_freesync *core_freesync = NULL;
99098e6436dSAnthony Koo 	unsigned long long nominal_field_rate_in_uhz = 0;
9915a6b5458SAric Cyr 	unsigned long long rounded_nominal_in_uhz = 0;
99298e6436dSAnthony Koo 	unsigned int refresh_range = 0;
993a463b263SBayan Zabihiyan 	unsigned long long min_refresh_in_uhz = 0;
994a463b263SBayan Zabihiyan 	unsigned long long max_refresh_in_uhz = 0;
995da55037aSAustin Zheng 	unsigned long long min_hardware_refresh_in_uhz = 0;
9967a1c37e0SAnthony Koo 
9977a1c37e0SAnthony Koo 	if (mod_freesync == NULL)
9987a1c37e0SAnthony Koo 		return;
9997a1c37e0SAnthony Koo 
10007a1c37e0SAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
10014562236bSHarry Wentland 
100298e6436dSAnthony Koo 	/* Calculate nominal field rate for stream */
1003ff6014d6SAnthony Koo 	nominal_field_rate_in_uhz =
1004ff6014d6SAnthony Koo 			mod_freesync_calc_nominal_field_rate(stream);
10054562236bSHarry Wentland 
1006da55037aSAustin Zheng 	if (stream->ctx->dc->caps.max_v_total != 0 && stream->timing.h_total != 0) {
1007da55037aSAustin Zheng 		min_hardware_refresh_in_uhz = div64_u64((stream->timing.pix_clk_100hz * 100000000ULL),
1008da55037aSAustin Zheng 			(stream->timing.h_total * stream->ctx->dc->caps.max_v_total));
1009da55037aSAustin Zheng 	}
1010da55037aSAustin Zheng 	/* Limit minimum refresh rate to what can be supported by hardware */
1011da55037aSAustin Zheng 	min_refresh_in_uhz = min_hardware_refresh_in_uhz > in_config->min_refresh_in_uhz ?
1012da55037aSAustin Zheng 		min_hardware_refresh_in_uhz : in_config->min_refresh_in_uhz;
101398e6436dSAnthony Koo 	max_refresh_in_uhz = in_config->max_refresh_in_uhz;
10144562236bSHarry Wentland 
1015fe984cb3SFelipe 	/* Full range may be larger than current video timing, so cap at nominal */
101698e6436dSAnthony Koo 	if (max_refresh_in_uhz > nominal_field_rate_in_uhz)
101798e6436dSAnthony Koo 		max_refresh_in_uhz = nominal_field_rate_in_uhz;
101898e6436dSAnthony Koo 
1019fe984cb3SFelipe 	/* Full range may be larger than current video timing, so cap at nominal */
10205a6b5458SAric Cyr 	if (min_refresh_in_uhz > max_refresh_in_uhz)
10215a6b5458SAric Cyr 		min_refresh_in_uhz = max_refresh_in_uhz;
10225a6b5458SAric Cyr 
1023fe984cb3SFelipe 	/* If a monitor reports exactly max refresh of 2x of min, enforce it on nominal */
10245a6b5458SAric Cyr 	rounded_nominal_in_uhz =
10255a6b5458SAric Cyr 			div_u64(nominal_field_rate_in_uhz + 50000, 100000) * 100000;
10265a6b5458SAric Cyr 	if (in_config->max_refresh_in_uhz == (2 * in_config->min_refresh_in_uhz) &&
10275a6b5458SAric Cyr 		in_config->max_refresh_in_uhz == rounded_nominal_in_uhz)
10285a6b5458SAric Cyr 		min_refresh_in_uhz = div_u64(nominal_field_rate_in_uhz, 2);
102998e6436dSAnthony Koo 
103098e6436dSAnthony Koo 	if (!vrr_settings_require_update(core_freesync,
1031a463b263SBayan Zabihiyan 			in_config, (unsigned int)min_refresh_in_uhz, (unsigned int)max_refresh_in_uhz,
103298e6436dSAnthony Koo 			in_out_vrr))
103398e6436dSAnthony Koo 		return;
103498e6436dSAnthony Koo 
103598e6436dSAnthony Koo 	in_out_vrr->state = in_config->state;
1036ca35899cSBayan Zabihiyan 	in_out_vrr->send_info_frame = in_config->vsif_supported;
103798e6436dSAnthony Koo 
1038050790ccSAnthony Koo 	if (in_config->state == VRR_STATE_UNSUPPORTED) {
103998e6436dSAnthony Koo 		in_out_vrr->state = VRR_STATE_UNSUPPORTED;
104098e6436dSAnthony Koo 		in_out_vrr->supported = false;
1041050790ccSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
1042050790ccSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
1043050790ccSAnthony Koo 
1044050790ccSAnthony Koo 		return;
1045050790ccSAnthony Koo 
104698e6436dSAnthony Koo 	} else {
1047a463b263SBayan Zabihiyan 		in_out_vrr->min_refresh_in_uhz = (unsigned int)min_refresh_in_uhz;
104898e6436dSAnthony Koo 		in_out_vrr->max_duration_in_us =
104998e6436dSAnthony Koo 				calc_duration_in_us_from_refresh_in_uhz(
1050a463b263SBayan Zabihiyan 						(unsigned int)min_refresh_in_uhz);
105198e6436dSAnthony Koo 
1052a463b263SBayan Zabihiyan 		in_out_vrr->max_refresh_in_uhz = (unsigned int)max_refresh_in_uhz;
105398e6436dSAnthony Koo 		in_out_vrr->min_duration_in_us =
105498e6436dSAnthony Koo 				calc_duration_in_us_from_refresh_in_uhz(
1055a463b263SBayan Zabihiyan 						(unsigned int)max_refresh_in_uhz);
105698e6436dSAnthony Koo 
1057d2bacc38SHaiyi Zhou 		if (in_config->state == VRR_STATE_ACTIVE_FIXED)
1058d2bacc38SHaiyi Zhou 			in_out_vrr->fixed_refresh_in_uhz = in_config->fixed_refresh_in_uhz;
1059d2bacc38SHaiyi Zhou 		else
1060d2bacc38SHaiyi Zhou 			in_out_vrr->fixed_refresh_in_uhz = 0;
1061d2bacc38SHaiyi Zhou 
1062ad339f69SJaehyun Chung 		refresh_range = div_u64(in_out_vrr->max_refresh_in_uhz + 500000, 1000000) -
1063ad339f69SJaehyun Chung +				div_u64(in_out_vrr->min_refresh_in_uhz + 500000, 1000000);
106498e6436dSAnthony Koo 
106598e6436dSAnthony Koo 		in_out_vrr->supported = true;
106698e6436dSAnthony Koo 	}
106798e6436dSAnthony Koo 
106898e6436dSAnthony Koo 	in_out_vrr->fixed.ramping_active = in_config->ramping;
106998e6436dSAnthony Koo 
107098e6436dSAnthony Koo 	in_out_vrr->btr.btr_enabled = in_config->btr;
1071a463b263SBayan Zabihiyan 
10725a6b5458SAric Cyr 	if (in_out_vrr->max_refresh_in_uhz < (2 * in_out_vrr->min_refresh_in_uhz))
107398e6436dSAnthony Koo 		in_out_vrr->btr.btr_enabled = false;
10745a6b5458SAric Cyr 	else {
10755a6b5458SAric Cyr 		in_out_vrr->btr.margin_in_us = in_out_vrr->max_duration_in_us -
10765a6b5458SAric Cyr 				2 * in_out_vrr->min_duration_in_us;
10775a6b5458SAric Cyr 		if (in_out_vrr->btr.margin_in_us > BTR_MAX_MARGIN)
10785a6b5458SAric Cyr 			in_out_vrr->btr.margin_in_us = BTR_MAX_MARGIN;
10795a6b5458SAric Cyr 	}
1080a463b263SBayan Zabihiyan 
108198e6436dSAnthony Koo 	in_out_vrr->btr.btr_active = false;
108298e6436dSAnthony Koo 	in_out_vrr->btr.inserted_duration_in_us = 0;
108398e6436dSAnthony Koo 	in_out_vrr->btr.frames_to_insert = 0;
108498e6436dSAnthony Koo 	in_out_vrr->btr.frame_counter = 0;
10851075735eSAlvin Lee 	in_out_vrr->fixed.fixed_active = false;
10861075735eSAlvin Lee 	in_out_vrr->fixed.target_refresh_in_uhz = 0;
1087ded6119eSAmanda Liu 
108898e6436dSAnthony Koo 	in_out_vrr->btr.mid_point_in_us =
1089a463b263SBayan Zabihiyan 				(in_out_vrr->min_duration_in_us +
1090a463b263SBayan Zabihiyan 				 in_out_vrr->max_duration_in_us) / 2;
109198e6436dSAnthony Koo 
109298e6436dSAnthony Koo 	if (in_out_vrr->state == VRR_STATE_UNSUPPORTED) {
109398e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
109498e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
109598e6436dSAnthony Koo 	} else if (in_out_vrr->state == VRR_STATE_DISABLED) {
109698e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
109798e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
109898e6436dSAnthony Koo 	} else if (in_out_vrr->state == VRR_STATE_INACTIVE) {
109998e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
110098e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
110198e6436dSAnthony Koo 	} else if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
1102ad339f69SJaehyun Chung 			refresh_range >= MIN_REFRESH_RANGE) {
11036f8f7644SAmanda Liu 
110498e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min =
110549c70eceSAlvin Lee 			mod_freesync_calc_v_total_from_refresh(stream,
110698e6436dSAnthony Koo 				in_out_vrr->max_refresh_in_uhz);
110798e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max =
110849c70eceSAlvin Lee 			mod_freesync_calc_v_total_from_refresh(stream,
110998e6436dSAnthony Koo 				in_out_vrr->min_refresh_in_uhz);
111098e6436dSAnthony Koo 	} else if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED) {
111198e6436dSAnthony Koo 		in_out_vrr->fixed.target_refresh_in_uhz =
1112d2bacc38SHaiyi Zhou 				in_out_vrr->fixed_refresh_in_uhz;
1113953c2901SAnthony Koo 		if (in_out_vrr->fixed.ramping_active &&
1114953c2901SAnthony Koo 				in_out_vrr->fixed.fixed_active) {
1115953c2901SAnthony Koo 			/* Do not update vtotals if ramping is already active
1116953c2901SAnthony Koo 			 * in order to continue ramp from current refresh.
1117953c2901SAnthony Koo 			 */
111898e6436dSAnthony Koo 			in_out_vrr->fixed.fixed_active = true;
111998e6436dSAnthony Koo 		} else {
112098e6436dSAnthony Koo 			in_out_vrr->fixed.fixed_active = true;
112198e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
112249c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
112398e6436dSAnthony Koo 					in_out_vrr->fixed.target_refresh_in_uhz);
112498e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
112598e6436dSAnthony Koo 				in_out_vrr->adjust.v_total_min;
112698e6436dSAnthony Koo 		}
112798e6436dSAnthony Koo 	} else {
112898e6436dSAnthony Koo 		in_out_vrr->state = VRR_STATE_INACTIVE;
112998e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_min = stream->timing.v_total;
113098e6436dSAnthony Koo 		in_out_vrr->adjust.v_total_max = stream->timing.v_total;
113198e6436dSAnthony Koo 	}
113298e6436dSAnthony Koo }
113398e6436dSAnthony 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)113498e6436dSAnthony Koo void mod_freesync_handle_preflip(struct mod_freesync *mod_freesync,
113598e6436dSAnthony Koo 		const struct dc_plane_state *plane,
113698e6436dSAnthony Koo 		const struct dc_stream_state *stream,
113798e6436dSAnthony Koo 		unsigned int curr_time_stamp_in_us,
113898e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
113998e6436dSAnthony Koo {
114098e6436dSAnthony Koo 	struct core_freesync *core_freesync = NULL;
114198e6436dSAnthony Koo 	unsigned int last_render_time_in_us = 0;
114298e6436dSAnthony Koo 
114398e6436dSAnthony Koo 	if (mod_freesync == NULL)
114498e6436dSAnthony Koo 		return;
114598e6436dSAnthony Koo 
114698e6436dSAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
114798e6436dSAnthony Koo 
114898e6436dSAnthony Koo 	if (in_out_vrr->supported &&
114998e6436dSAnthony Koo 			in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE) {
11504562236bSHarry Wentland 
11514562236bSHarry Wentland 		last_render_time_in_us = curr_time_stamp_in_us -
115298e6436dSAnthony Koo 				plane->time.prev_update_time_in_us;
11534562236bSHarry Wentland 
115498e6436dSAnthony Koo 		if (in_out_vrr->btr.btr_enabled) {
11554562236bSHarry Wentland 			apply_below_the_range(core_freesync,
115698e6436dSAnthony Koo 					stream,
115798e6436dSAnthony Koo 					last_render_time_in_us,
115898e6436dSAnthony Koo 					in_out_vrr);
11594562236bSHarry Wentland 		} else {
11604562236bSHarry Wentland 			apply_fixed_refresh(core_freesync,
116198e6436dSAnthony Koo 				stream,
116298e6436dSAnthony Koo 				last_render_time_in_us,
116398e6436dSAnthony Koo 				in_out_vrr);
116498e6436dSAnthony Koo 		}
116598e6436dSAnthony Koo 
11663fe5739dSAngus Wang 		determine_flip_interval_workaround_req(in_out_vrr,
11673fe5739dSAngus Wang 				curr_time_stamp_in_us);
11683fe5739dSAngus Wang 
11694562236bSHarry Wentland 	}
11704562236bSHarry Wentland }
11714562236bSHarry Wentland 
mod_freesync_handle_v_update(struct mod_freesync * mod_freesync,const struct dc_stream_state * stream,struct mod_vrr_params * in_out_vrr)117298e6436dSAnthony Koo void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
117398e6436dSAnthony Koo 		const struct dc_stream_state *stream,
117498e6436dSAnthony Koo 		struct mod_vrr_params *in_out_vrr)
117598e6436dSAnthony Koo {
117698e6436dSAnthony Koo 	struct core_freesync *core_freesync = NULL;
11773fe5739dSAngus Wang 	unsigned int cur_timestamp_in_us;
117819a2e1e3SAngus Wang 	unsigned long long cur_tick;
117998e6436dSAnthony Koo 
118098e6436dSAnthony Koo 	if ((mod_freesync == NULL) || (stream == NULL) || (in_out_vrr == NULL))
118198e6436dSAnthony Koo 		return;
118298e6436dSAnthony Koo 
118398e6436dSAnthony Koo 	core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
118498e6436dSAnthony Koo 
118598e6436dSAnthony Koo 	if (in_out_vrr->supported == false)
118698e6436dSAnthony Koo 		return;
118798e6436dSAnthony Koo 
118819a2e1e3SAngus Wang 	cur_tick = dm_get_timestamp(core_freesync->dc->ctx);
118919a2e1e3SAngus Wang 	cur_timestamp_in_us = (unsigned int)
119019a2e1e3SAngus Wang 			div_u64(dm_get_elapse_time_in_ns(core_freesync->dc->ctx, cur_tick, 0), 1000);
11913fe5739dSAngus Wang 
11923fe5739dSAngus Wang 	in_out_vrr->flip_interval.vsyncs_between_flip++;
11933fe5739dSAngus Wang 	in_out_vrr->flip_interval.v_update_timestamp_in_us = cur_timestamp_in_us;
11943fe5739dSAngus Wang 
11953fe5739dSAngus Wang 	if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
11963fe5739dSAngus Wang 			(in_out_vrr->flip_interval.flip_interval_workaround_active ||
11973fe5739dSAngus Wang 			(!in_out_vrr->flip_interval.flip_interval_workaround_active &&
11983fe5739dSAngus Wang 			in_out_vrr->flip_interval.program_flip_interval_workaround))) {
11993fe5739dSAngus Wang 		// set freesync vmin vmax to nominal for workaround
12003fe5739dSAngus Wang 		in_out_vrr->adjust.v_total_min =
12013fe5739dSAngus Wang 			mod_freesync_calc_v_total_from_refresh(
12023fe5739dSAngus Wang 			stream, in_out_vrr->max_refresh_in_uhz);
12033fe5739dSAngus Wang 		in_out_vrr->adjust.v_total_max =
12043fe5739dSAngus Wang 				in_out_vrr->adjust.v_total_min;
12053fe5739dSAngus Wang 		in_out_vrr->flip_interval.program_flip_interval_workaround = false;
12063fe5739dSAngus Wang 		in_out_vrr->flip_interval.do_flip_interval_workaround_cleanup = true;
12073fe5739dSAngus Wang 		return;
12083fe5739dSAngus Wang 	}
12093fe5739dSAngus Wang 
12103fe5739dSAngus Wang 	if (in_out_vrr->state != VRR_STATE_ACTIVE_VARIABLE &&
12113fe5739dSAngus Wang 			in_out_vrr->flip_interval.do_flip_interval_workaround_cleanup) {
12123fe5739dSAngus Wang 		in_out_vrr->flip_interval.do_flip_interval_workaround_cleanup = false;
12133fe5739dSAngus Wang 		in_out_vrr->flip_interval.flip_interval_detect_counter = 0;
12143fe5739dSAngus Wang 		in_out_vrr->flip_interval.vsyncs_between_flip = 0;
12153fe5739dSAngus Wang 		in_out_vrr->flip_interval.vsync_to_flip_in_us = 0;
12163fe5739dSAngus Wang 	}
12173fe5739dSAngus Wang 
121898e6436dSAnthony Koo 	/* Below the Range Logic */
121998e6436dSAnthony Koo 
122098e6436dSAnthony Koo 	/* Only execute if in fullscreen mode */
122198e6436dSAnthony Koo 	if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
122298e6436dSAnthony Koo 					in_out_vrr->btr.btr_active) {
122398e6436dSAnthony Koo 		/* TODO: pass in flag for Pre-DCE12 ASIC
122498e6436dSAnthony Koo 		 * in order for frame variable duration to take affect,
122598e6436dSAnthony Koo 		 * it needs to be done one VSYNC early, which is at
122698e6436dSAnthony Koo 		 * frameCounter == 1.
122798e6436dSAnthony Koo 		 * For DCE12 and newer updates to V_TOTAL_MIN/MAX
122898e6436dSAnthony Koo 		 * will take affect on current frame
122998e6436dSAnthony Koo 		 */
123098e6436dSAnthony Koo 		if (in_out_vrr->btr.frames_to_insert ==
123198e6436dSAnthony Koo 				in_out_vrr->btr.frame_counter) {
123298e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
123398e6436dSAnthony Koo 				calc_v_total_from_duration(stream,
123498e6436dSAnthony Koo 				in_out_vrr,
123598e6436dSAnthony Koo 				in_out_vrr->btr.inserted_duration_in_us);
123698e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
123798e6436dSAnthony Koo 				in_out_vrr->adjust.v_total_min;
12384562236bSHarry Wentland 		}
12394562236bSHarry Wentland 
124098e6436dSAnthony Koo 		if (in_out_vrr->btr.frame_counter > 0)
124198e6436dSAnthony Koo 			in_out_vrr->btr.frame_counter--;
124298e6436dSAnthony Koo 
124398e6436dSAnthony Koo 		/* Restore FreeSync */
124498e6436dSAnthony Koo 		if (in_out_vrr->btr.frame_counter == 0) {
124598e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_min =
124649c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
124798e6436dSAnthony Koo 				in_out_vrr->max_refresh_in_uhz);
124898e6436dSAnthony Koo 			in_out_vrr->adjust.v_total_max =
124949c70eceSAlvin Lee 				mod_freesync_calc_v_total_from_refresh(stream,
125098e6436dSAnthony Koo 				in_out_vrr->min_refresh_in_uhz);
125198e6436dSAnthony Koo 		}
125298e6436dSAnthony Koo 	}
125398e6436dSAnthony Koo 
125498e6436dSAnthony Koo 	/* If in fullscreen freesync mode or in video, do not program
125598e6436dSAnthony Koo 	 * static screen ramp values
125698e6436dSAnthony Koo 	 */
125798e6436dSAnthony Koo 	if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE)
125898e6436dSAnthony Koo 		in_out_vrr->fixed.ramping_active = false;
125998e6436dSAnthony Koo 
1260fe984cb3SFelipe 	/* Gradual Static Screen Ramping Logic
1261fe984cb3SFelipe 	 * Execute if ramp is active and user enabled freesync static screen
1262fe984cb3SFelipe 	 */
126398e6436dSAnthony Koo 	if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED &&
126498e6436dSAnthony Koo 				in_out_vrr->fixed.ramping_active) {
126598e6436dSAnthony Koo 		update_v_total_for_static_ramp(
126698e6436dSAnthony Koo 				core_freesync, stream, in_out_vrr);
12674562236bSHarry Wentland 	}
12684562236bSHarry Wentland }
1269a3e1737eSAnthony Koo 
mod_freesync_get_settings(struct mod_freesync * mod_freesync,const struct mod_vrr_params * vrr,unsigned int * v_total_min,unsigned int * v_total_max,unsigned int * event_triggers,unsigned int * window_min,unsigned int * window_max,unsigned int * lfc_mid_point_in_us,unsigned int * inserted_frames,unsigned int * inserted_duration_in_us)1270a3e1737eSAnthony Koo void mod_freesync_get_settings(struct mod_freesync *mod_freesync,
127198e6436dSAnthony Koo 		const struct mod_vrr_params *vrr,
1272a3e1737eSAnthony Koo 		unsigned int *v_total_min, unsigned int *v_total_max,
1273a3e1737eSAnthony Koo 		unsigned int *event_triggers,
1274a3e1737eSAnthony Koo 		unsigned int *window_min, unsigned int *window_max,
1275a3e1737eSAnthony Koo 		unsigned int *lfc_mid_point_in_us,
1276a3e1737eSAnthony Koo 		unsigned int *inserted_frames,
1277a3e1737eSAnthony Koo 		unsigned int *inserted_duration_in_us)
1278a3e1737eSAnthony Koo {
1279a3e1737eSAnthony Koo 	if (mod_freesync == NULL)
1280a3e1737eSAnthony Koo 		return;
1281a3e1737eSAnthony Koo 
128298e6436dSAnthony Koo 	if (vrr->supported) {
128398e6436dSAnthony Koo 		*v_total_min = vrr->adjust.v_total_min;
128498e6436dSAnthony Koo 		*v_total_max = vrr->adjust.v_total_max;
1285a3e1737eSAnthony Koo 		*event_triggers = 0;
128698e6436dSAnthony Koo 		*lfc_mid_point_in_us = vrr->btr.mid_point_in_us;
128798e6436dSAnthony Koo 		*inserted_frames = vrr->btr.frames_to_insert;
128898e6436dSAnthony Koo 		*inserted_duration_in_us = vrr->btr.inserted_duration_in_us;
1289a3e1737eSAnthony Koo 	}
1290a3e1737eSAnthony Koo }
1291a3e1737eSAnthony Koo 
mod_freesync_calc_nominal_field_rate(const struct dc_stream_state * stream)1292ff6014d6SAnthony Koo unsigned long long mod_freesync_calc_nominal_field_rate(
1293ff6014d6SAnthony Koo 			const struct dc_stream_state *stream)
1294ff6014d6SAnthony Koo {
1295ff6014d6SAnthony Koo 	unsigned long long nominal_field_rate_in_uhz = 0;
1296c5980231SAric Cyr 	unsigned int total = stream->timing.h_total * stream->timing.v_total;
1297ff6014d6SAnthony Koo 
1298c5980231SAric Cyr 	/* Calculate nominal field rate for stream, rounded up to nearest integer */
12995a6b5458SAric Cyr 	nominal_field_rate_in_uhz = stream->timing.pix_clk_100hz;
13005a6b5458SAric Cyr 	nominal_field_rate_in_uhz *= 100000000ULL;
1301c5980231SAric Cyr 
1302c5980231SAric Cyr 	nominal_field_rate_in_uhz =	div_u64(nominal_field_rate_in_uhz, total);
1303ff6014d6SAnthony Koo 
1304ff6014d6SAnthony Koo 	return nominal_field_rate_in_uhz;
1305ff6014d6SAnthony Koo }
1306ff6014d6SAnthony Koo 
mod_freesync_calc_field_rate_from_timing(unsigned int vtotal,unsigned int htotal,unsigned int pix_clk)130749c70eceSAlvin Lee unsigned long long mod_freesync_calc_field_rate_from_timing(
130849c70eceSAlvin Lee 		unsigned int vtotal, unsigned int htotal, unsigned int pix_clk)
130949c70eceSAlvin Lee {
131049c70eceSAlvin Lee 	unsigned long long field_rate_in_uhz = 0;
131149c70eceSAlvin Lee 	unsigned int total = htotal * vtotal;
131249c70eceSAlvin Lee 
131349c70eceSAlvin Lee 	/* Calculate nominal field rate for stream, rounded up to nearest integer */
131449c70eceSAlvin Lee 	field_rate_in_uhz = pix_clk;
131549c70eceSAlvin Lee 	field_rate_in_uhz *= 1000000ULL;
131649c70eceSAlvin Lee 
131749c70eceSAlvin Lee 	field_rate_in_uhz =	div_u64(field_rate_in_uhz, total);
131849c70eceSAlvin Lee 
131949c70eceSAlvin Lee 	return field_rate_in_uhz;
132049c70eceSAlvin Lee }
132149c70eceSAlvin Lee 
mod_freesync_get_freesync_enabled(struct mod_vrr_params * pVrr)1322ebfb1526SHarry VanZyllDeJong bool mod_freesync_get_freesync_enabled(struct mod_vrr_params *pVrr)
1323c2fbe663SFelipe Clark {
1324c2fbe663SFelipe Clark 	return (pVrr->state != VRR_STATE_UNSUPPORTED) && (pVrr->state != VRR_STATE_DISABLED);
1325c2fbe663SFelipe Clark }
1326c2fbe663SFelipe Clark 
mod_freesync_is_valid_range(uint32_t min_refresh_cap_in_uhz,uint32_t max_refresh_cap_in_uhz,uint32_t nominal_field_rate_in_uhz)1327ad339f69SJaehyun Chung bool mod_freesync_is_valid_range(uint32_t min_refresh_cap_in_uhz,
1328e80e9446SAnthony Koo 		uint32_t max_refresh_cap_in_uhz,
1329ad339f69SJaehyun Chung 		uint32_t nominal_field_rate_in_uhz)
1330e80e9446SAnthony Koo {
1331e80e9446SAnthony Koo 
1332bf2af91cSAnthony Koo 	/* Typically nominal refresh calculated can have some fractional part.
1333bf2af91cSAnthony Koo 	 * Allow for some rounding error of actual video timing by taking floor
1334bf2af91cSAnthony Koo 	 * of caps and request. Round the nominal refresh rate.
1335bf2af91cSAnthony Koo 	 *
1336bf2af91cSAnthony Koo 	 * Dividing will convert everything to units in Hz although input
1337bf2af91cSAnthony Koo 	 * variable name is in uHz!
1338bf2af91cSAnthony Koo 	 *
1339bf2af91cSAnthony Koo 	 * Also note, this takes care of rounding error on the nominal refresh
1340bf2af91cSAnthony Koo 	 * so by rounding error we only expect it to be off by a small amount,
1341bf2af91cSAnthony Koo 	 * such as < 0.1 Hz. i.e. 143.9xxx or 144.1xxx.
1342bf2af91cSAnthony Koo 	 *
1343bf2af91cSAnthony Koo 	 * Example 1. Caps    Min = 40 Hz, Max = 144 Hz
1344bf2af91cSAnthony Koo 	 *            Request Min = 40 Hz, Max = 144 Hz
1345bf2af91cSAnthony Koo 	 *                    Nominal = 143.5x Hz rounded to 144 Hz
1346bf2af91cSAnthony Koo 	 *            This function should allow this as valid request
1347bf2af91cSAnthony Koo 	 *
1348bf2af91cSAnthony Koo 	 * Example 2. Caps    Min = 40 Hz, Max = 144 Hz
1349bf2af91cSAnthony Koo 	 *            Request Min = 40 Hz, Max = 144 Hz
1350bf2af91cSAnthony Koo 	 *                    Nominal = 144.4x Hz rounded to 144 Hz
1351bf2af91cSAnthony Koo 	 *            This function should allow this as valid request
1352bf2af91cSAnthony Koo 	 *
1353bf2af91cSAnthony Koo 	 * Example 3. Caps    Min = 40 Hz, Max = 144 Hz
1354bf2af91cSAnthony Koo 	 *            Request Min = 40 Hz, Max = 144 Hz
1355bf2af91cSAnthony Koo 	 *                    Nominal = 120.xx Hz rounded to 120 Hz
1356bf2af91cSAnthony Koo 	 *            This function should return NOT valid since the requested
1357bf2af91cSAnthony Koo 	 *            max is greater than current timing's nominal
1358bf2af91cSAnthony Koo 	 *
1359bf2af91cSAnthony Koo 	 * Example 4. Caps    Min = 40 Hz, Max = 120 Hz
1360bf2af91cSAnthony Koo 	 *            Request Min = 40 Hz, Max = 120 Hz
1361bf2af91cSAnthony Koo 	 *                    Nominal = 144.xx Hz rounded to 144 Hz
1362bf2af91cSAnthony Koo 	 *            This function should return NOT valid since the nominal
1363bf2af91cSAnthony Koo 	 *            is greater than the capability's max refresh
1364be922ff7SAnthony Koo 	 */
1365bf2af91cSAnthony Koo 	nominal_field_rate_in_uhz =
1366bf2af91cSAnthony Koo 			div_u64(nominal_field_rate_in_uhz + 500000, 1000000);
1367be922ff7SAnthony Koo 	min_refresh_cap_in_uhz /= 1000000;
1368be922ff7SAnthony Koo 	max_refresh_cap_in_uhz /= 1000000;
1369be922ff7SAnthony Koo 
1370fe984cb3SFelipe 	/* Check nominal is within range */
1371e80e9446SAnthony Koo 	if (nominal_field_rate_in_uhz > max_refresh_cap_in_uhz ||
1372e80e9446SAnthony Koo 		nominal_field_rate_in_uhz < min_refresh_cap_in_uhz)
1373e80e9446SAnthony Koo 		return false;
1374e80e9446SAnthony Koo 
1375fe984cb3SFelipe 	/* If nominal is less than max, limit the max allowed refresh rate */
1376e80e9446SAnthony Koo 	if (nominal_field_rate_in_uhz < max_refresh_cap_in_uhz)
1377e80e9446SAnthony Koo 		max_refresh_cap_in_uhz = nominal_field_rate_in_uhz;
1378e80e9446SAnthony Koo 
1379fe984cb3SFelipe 	/* Check min is within range */
1380ad339f69SJaehyun Chung 	if (min_refresh_cap_in_uhz > max_refresh_cap_in_uhz)
1381e80e9446SAnthony Koo 		return false;
1382e80e9446SAnthony Koo 
1383fe984cb3SFelipe 	/* For variable range, check for at least 10 Hz range */
1384ad339f69SJaehyun Chung 	if (nominal_field_rate_in_uhz - min_refresh_cap_in_uhz < 10)
1385e80e9446SAnthony Koo 		return false;
1386e80e9446SAnthony Koo 
1387e80e9446SAnthony Koo 	return true;
1388e80e9446SAnthony Koo }
1389