2 * Copyright 2016 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
26 #include <linux/slab.h>
28 #include "dm_services.h"
30 #include "mod_freesync.h"
31 #include "core_types.h"
33 #define MOD_FREESYNC_MAX_CONCURRENT_STREAMS 32
35 #define MIN_REFRESH_RANGE 10
36 /* Refresh rate ramp at a fixed rate of 65 Hz/second */
37 #define STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME ((1000 / 60) * 65)
38 /* Number of elements in the render times cache array */
39 #define RENDER_TIMES_MAX_COUNT 10
40 /* Threshold to exit/exit BTR (to avoid frequent enter-exits at the lower limit) */
41 #define BTR_MAX_MARGIN 2500
42 /* Threshold to change BTR multiplier (to avoid frequent changes) */
43 #define BTR_DRIFT_MARGIN 2000
44 /* Threshold to exit fixed refresh rate */
45 #define FIXED_REFRESH_EXIT_MARGIN_IN_HZ 1
46 /* Number of consecutive frames to check before entering/exiting fixed refresh */
47 #define FIXED_REFRESH_ENTER_FRAME_COUNT 5
48 #define FIXED_REFRESH_EXIT_FRAME_COUNT 10
50 struct core_freesync {
51 struct mod_freesync public;
55 #define MOD_FREESYNC_TO_CORE(mod_freesync)\
56 container_of(mod_freesync, struct core_freesync, public)
58 struct mod_freesync *mod_freesync_create(struct dc *dc)
60 struct core_freesync *core_freesync =
61 kzalloc(sizeof(struct core_freesync), GFP_KERNEL);
63 if (core_freesync == NULL)
64 goto fail_alloc_context;
69 core_freesync->dc = dc;
70 return &core_freesync->public;
79 void mod_freesync_destroy(struct mod_freesync *mod_freesync)
81 struct core_freesync *core_freesync = NULL;
82 if (mod_freesync == NULL)
84 core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
88 #if 0 /* Unused currently */
89 static unsigned int calc_refresh_in_uhz_from_duration(
90 unsigned int duration_in_ns)
92 unsigned int refresh_in_uhz =
93 ((unsigned int)(div64_u64((1000000000ULL * 1000000),
95 return refresh_in_uhz;
99 static unsigned int calc_duration_in_us_from_refresh_in_uhz(
100 unsigned int refresh_in_uhz)
102 unsigned int duration_in_us =
103 ((unsigned int)(div64_u64((1000000000ULL * 1000),
105 return duration_in_us;
108 static unsigned int calc_duration_in_us_from_v_total(
109 const struct dc_stream_state *stream,
110 const struct mod_vrr_params *in_vrr,
111 unsigned int v_total)
113 unsigned int duration_in_us =
114 (unsigned int)(div64_u64(((unsigned long long)(v_total)
115 * 10000) * stream->timing.h_total,
116 stream->timing.pix_clk_100hz));
118 return duration_in_us;
121 unsigned int mod_freesync_calc_v_total_from_refresh(
122 const struct dc_stream_state *stream,
123 unsigned int refresh_in_uhz)
125 unsigned int v_total;
126 unsigned int frame_duration_in_ns;
128 frame_duration_in_ns =
129 ((unsigned int)(div64_u64((1000000000ULL * 1000000),
132 v_total = div64_u64(div64_u64(((unsigned long long)(
133 frame_duration_in_ns) * (stream->timing.pix_clk_100hz / 10)),
134 stream->timing.h_total), 1000000);
136 /* v_total cannot be less than nominal */
137 if (v_total < stream->timing.v_total) {
138 ASSERT(v_total < stream->timing.v_total);
139 v_total = stream->timing.v_total;
145 static unsigned int calc_v_total_from_duration(
146 const struct dc_stream_state *stream,
147 const struct mod_vrr_params *vrr,
148 unsigned int duration_in_us)
150 unsigned int v_total = 0;
152 if (duration_in_us < vrr->min_duration_in_us)
153 duration_in_us = vrr->min_duration_in_us;
155 if (duration_in_us > vrr->max_duration_in_us)
156 duration_in_us = vrr->max_duration_in_us;
158 if (dc_is_hdmi_signal(stream->signal)) {
159 uint32_t h_total_up_scaled;
161 h_total_up_scaled = stream->timing.h_total * 10000;
162 v_total = div_u64((unsigned long long)duration_in_us
163 * stream->timing.pix_clk_100hz + (h_total_up_scaled - 1),
166 v_total = div64_u64(div64_u64(((unsigned long long)(
167 duration_in_us) * (stream->timing.pix_clk_100hz / 10)),
168 stream->timing.h_total), 1000);
171 /* v_total cannot be less than nominal */
172 if (v_total < stream->timing.v_total) {
173 ASSERT(v_total < stream->timing.v_total);
174 v_total = stream->timing.v_total;
180 static void update_v_total_for_static_ramp(
181 struct core_freesync *core_freesync,
182 const struct dc_stream_state *stream,
183 struct mod_vrr_params *in_out_vrr)
185 unsigned int v_total = 0;
186 unsigned int current_duration_in_us =
187 calc_duration_in_us_from_v_total(
189 in_out_vrr->adjust.v_total_max);
190 unsigned int target_duration_in_us =
191 calc_duration_in_us_from_refresh_in_uhz(
192 in_out_vrr->fixed.target_refresh_in_uhz);
193 bool ramp_direction_is_up = (current_duration_in_us >
194 target_duration_in_us) ? true : false;
196 /* Calculate ratio between new and current frame duration with 3 digit */
197 unsigned int frame_duration_ratio = div64_u64(1000000,
198 (1000 + div64_u64(((unsigned long long)(
199 STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
200 current_duration_in_us),
203 /* Calculate delta between new and current frame duration in us */
204 unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
205 current_duration_in_us) *
206 (1000 - frame_duration_ratio)), 1000);
208 /* Adjust frame duration delta based on ratio between current and
209 * standard frame duration (frame duration at 60 Hz refresh rate).
211 unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
212 frame_duration_delta) * current_duration_in_us), 16666);
214 /* Going to a higher refresh rate (lower frame duration) */
215 if (ramp_direction_is_up) {
216 /* Reduce frame duration */
217 current_duration_in_us -= ramp_rate_interpolated;
219 /* Adjust for frame duration below min */
220 if (current_duration_in_us <= target_duration_in_us) {
221 in_out_vrr->fixed.ramping_active = false;
222 in_out_vrr->fixed.ramping_done = true;
223 current_duration_in_us =
224 calc_duration_in_us_from_refresh_in_uhz(
225 in_out_vrr->fixed.target_refresh_in_uhz);
227 /* Going to a lower refresh rate (larger frame duration) */
229 /* Increase frame duration */
230 current_duration_in_us += ramp_rate_interpolated;
232 /* Adjust for frame duration above max */
233 if (current_duration_in_us >= target_duration_in_us) {
234 in_out_vrr->fixed.ramping_active = false;
235 in_out_vrr->fixed.ramping_done = true;
236 current_duration_in_us =
237 calc_duration_in_us_from_refresh_in_uhz(
238 in_out_vrr->fixed.target_refresh_in_uhz);
242 v_total = div64_u64(div64_u64(((unsigned long long)(
243 current_duration_in_us) * (stream->timing.pix_clk_100hz / 10)),
244 stream->timing.h_total), 1000);
246 /* v_total cannot be less than nominal */
247 if (v_total < stream->timing.v_total)
248 v_total = stream->timing.v_total;
250 in_out_vrr->adjust.v_total_min = v_total;
251 in_out_vrr->adjust.v_total_max = v_total;
254 static void apply_below_the_range(struct core_freesync *core_freesync,
255 const struct dc_stream_state *stream,
256 unsigned int last_render_time_in_us,
257 struct mod_vrr_params *in_out_vrr)
259 unsigned int inserted_frame_duration_in_us = 0;
260 unsigned int mid_point_frames_ceil = 0;
261 unsigned int mid_point_frames_floor = 0;
262 unsigned int frame_time_in_us = 0;
263 unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
264 unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
265 unsigned int frames_to_insert = 0;
266 unsigned int delta_from_mid_point_delta_in_us;
267 unsigned int max_render_time_in_us =
268 in_out_vrr->max_duration_in_us - in_out_vrr->btr.margin_in_us;
271 if ((last_render_time_in_us + in_out_vrr->btr.margin_in_us / 2) < max_render_time_in_us) {
272 /* Exit Below the Range */
273 if (in_out_vrr->btr.btr_active) {
274 in_out_vrr->btr.frame_counter = 0;
275 in_out_vrr->btr.btr_active = false;
277 } else if (last_render_time_in_us > (max_render_time_in_us + in_out_vrr->btr.margin_in_us / 2)) {
278 /* Enter Below the Range */
279 if (!in_out_vrr->btr.btr_active) {
280 in_out_vrr->btr.btr_active = true;
284 /* BTR set to "not active" so disengage */
285 if (!in_out_vrr->btr.btr_active) {
286 in_out_vrr->btr.inserted_duration_in_us = 0;
287 in_out_vrr->btr.frames_to_insert = 0;
288 in_out_vrr->btr.frame_counter = 0;
290 /* Restore FreeSync */
291 in_out_vrr->adjust.v_total_min =
292 mod_freesync_calc_v_total_from_refresh(stream,
293 in_out_vrr->max_refresh_in_uhz);
294 in_out_vrr->adjust.v_total_max =
295 mod_freesync_calc_v_total_from_refresh(stream,
296 in_out_vrr->min_refresh_in_uhz);
297 /* BTR set to "active" so engage */
300 /* Calculate number of midPoint frames that could fit within
301 * the render time interval - take ceil of this value
303 mid_point_frames_ceil = (last_render_time_in_us +
304 in_out_vrr->btr.mid_point_in_us - 1) /
305 in_out_vrr->btr.mid_point_in_us;
307 if (mid_point_frames_ceil > 0) {
308 frame_time_in_us = last_render_time_in_us /
309 mid_point_frames_ceil;
310 delta_from_mid_point_in_us_1 =
311 (in_out_vrr->btr.mid_point_in_us >
313 (in_out_vrr->btr.mid_point_in_us - frame_time_in_us) :
314 (frame_time_in_us - in_out_vrr->btr.mid_point_in_us);
317 /* Calculate number of midPoint frames that could fit within
318 * the render time interval - take floor of this value
320 mid_point_frames_floor = last_render_time_in_us /
321 in_out_vrr->btr.mid_point_in_us;
323 if (mid_point_frames_floor > 0) {
325 frame_time_in_us = last_render_time_in_us /
326 mid_point_frames_floor;
327 delta_from_mid_point_in_us_2 =
328 (in_out_vrr->btr.mid_point_in_us >
330 (in_out_vrr->btr.mid_point_in_us - frame_time_in_us) :
331 (frame_time_in_us - in_out_vrr->btr.mid_point_in_us);
334 /* Choose number of frames to insert based on how close it
335 * can get to the mid point of the variable range.
336 * - Delta for CEIL: delta_from_mid_point_in_us_1
337 * - Delta for FLOOR: delta_from_mid_point_in_us_2
339 if ((last_render_time_in_us / mid_point_frames_ceil) < in_out_vrr->min_duration_in_us) {
340 /* Check for out of range.
341 * If using CEIL produces a value that is out of range,
342 * then we are forced to use FLOOR.
344 frames_to_insert = mid_point_frames_floor;
345 } else if (mid_point_frames_floor < 2) {
346 /* Check if FLOOR would result in non-LFC. In this case
349 frames_to_insert = mid_point_frames_ceil;
350 } else if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2) {
351 /* If choosing CEIL results in a frame duration that is
352 * closer to the mid point of the range.
355 frames_to_insert = mid_point_frames_ceil;
357 /* If choosing FLOOR results in a frame duration that is
358 * closer to the mid point of the range.
361 frames_to_insert = mid_point_frames_floor;
364 /* Prefer current frame multiplier when BTR is enabled unless it drifts
365 * too far from the midpoint
367 if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2) {
368 delta_from_mid_point_delta_in_us = delta_from_mid_point_in_us_2 -
369 delta_from_mid_point_in_us_1;
371 delta_from_mid_point_delta_in_us = delta_from_mid_point_in_us_1 -
372 delta_from_mid_point_in_us_2;
374 if (in_out_vrr->btr.frames_to_insert != 0 &&
375 delta_from_mid_point_delta_in_us < BTR_DRIFT_MARGIN) {
376 if (((last_render_time_in_us / in_out_vrr->btr.frames_to_insert) <
377 max_render_time_in_us) &&
378 ((last_render_time_in_us / in_out_vrr->btr.frames_to_insert) >
379 in_out_vrr->min_duration_in_us))
380 frames_to_insert = in_out_vrr->btr.frames_to_insert;
383 /* Either we've calculated the number of frames to insert,
384 * or we need to insert min duration frames
386 if (last_render_time_in_us / frames_to_insert <
387 in_out_vrr->min_duration_in_us){
388 frames_to_insert -= (frames_to_insert > 1) ?
392 if (frames_to_insert > 0)
393 inserted_frame_duration_in_us = last_render_time_in_us /
396 if (inserted_frame_duration_in_us < in_out_vrr->min_duration_in_us)
397 inserted_frame_duration_in_us = in_out_vrr->min_duration_in_us;
399 /* Cache the calculated variables */
400 in_out_vrr->btr.inserted_duration_in_us =
401 inserted_frame_duration_in_us;
402 in_out_vrr->btr.frames_to_insert = frames_to_insert;
403 in_out_vrr->btr.frame_counter = frames_to_insert;
407 static void apply_fixed_refresh(struct core_freesync *core_freesync,
408 const struct dc_stream_state *stream,
409 unsigned int last_render_time_in_us,
410 struct mod_vrr_params *in_out_vrr)
413 unsigned int max_render_time_in_us = in_out_vrr->max_duration_in_us;
415 /* Compute the exit refresh rate and exit frame duration */
416 unsigned int exit_refresh_rate_in_milli_hz = ((1000000000/max_render_time_in_us)
417 + (1000*FIXED_REFRESH_EXIT_MARGIN_IN_HZ));
418 unsigned int exit_frame_duration_in_us = 1000000000/exit_refresh_rate_in_milli_hz;
420 if (last_render_time_in_us < exit_frame_duration_in_us) {
421 /* Exit Fixed Refresh mode */
422 if (in_out_vrr->fixed.fixed_active) {
423 in_out_vrr->fixed.frame_counter++;
425 if (in_out_vrr->fixed.frame_counter >
426 FIXED_REFRESH_EXIT_FRAME_COUNT) {
427 in_out_vrr->fixed.frame_counter = 0;
428 in_out_vrr->fixed.fixed_active = false;
429 in_out_vrr->fixed.target_refresh_in_uhz = 0;
433 in_out_vrr->fixed.frame_counter = 0;
434 } else if (last_render_time_in_us > max_render_time_in_us) {
435 /* Enter Fixed Refresh mode */
436 if (!in_out_vrr->fixed.fixed_active) {
437 in_out_vrr->fixed.frame_counter++;
439 if (in_out_vrr->fixed.frame_counter >
440 FIXED_REFRESH_ENTER_FRAME_COUNT) {
441 in_out_vrr->fixed.frame_counter = 0;
442 in_out_vrr->fixed.fixed_active = true;
443 in_out_vrr->fixed.target_refresh_in_uhz =
444 in_out_vrr->max_refresh_in_uhz;
448 in_out_vrr->fixed.frame_counter = 0;
452 if (in_out_vrr->fixed.fixed_active) {
453 in_out_vrr->adjust.v_total_min =
454 mod_freesync_calc_v_total_from_refresh(
455 stream, in_out_vrr->max_refresh_in_uhz);
456 in_out_vrr->adjust.v_total_max =
457 in_out_vrr->adjust.v_total_min;
459 in_out_vrr->adjust.v_total_min =
460 mod_freesync_calc_v_total_from_refresh(stream,
461 in_out_vrr->max_refresh_in_uhz);
462 in_out_vrr->adjust.v_total_max =
463 mod_freesync_calc_v_total_from_refresh(stream,
464 in_out_vrr->min_refresh_in_uhz);
469 static bool vrr_settings_require_update(struct core_freesync *core_freesync,
470 struct mod_freesync_config *in_config,
471 unsigned int min_refresh_in_uhz,
472 unsigned int max_refresh_in_uhz,
473 struct mod_vrr_params *in_vrr)
475 if (in_vrr->state != in_config->state) {
477 } else if (in_vrr->state == VRR_STATE_ACTIVE_FIXED &&
478 in_vrr->fixed.target_refresh_in_uhz !=
479 in_config->fixed_refresh_in_uhz) {
481 } else if (in_vrr->min_refresh_in_uhz != min_refresh_in_uhz) {
483 } else if (in_vrr->max_refresh_in_uhz != max_refresh_in_uhz) {
490 bool mod_freesync_get_vmin_vmax(struct mod_freesync *mod_freesync,
491 const struct dc_stream_state *stream,
495 *vmin = stream->adjust.v_total_min;
496 *vmax = stream->adjust.v_total_max;
501 bool mod_freesync_get_v_position(struct mod_freesync *mod_freesync,
502 struct dc_stream_state *stream,
503 unsigned int *nom_v_pos,
506 struct core_freesync *core_freesync = NULL;
507 struct crtc_position position;
509 if (mod_freesync == NULL)
512 core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
514 if (dc_stream_get_crtc_position(core_freesync->dc, &stream, 1,
515 &position.vertical_count,
516 &position.nominal_vcount)) {
518 *nom_v_pos = position.nominal_vcount;
519 *v_pos = position.vertical_count;
527 static void build_vrr_infopacket_data_v1(const struct mod_vrr_params *vrr,
528 struct dc_info_packet *infopacket,
529 bool freesync_on_desktop)
531 /* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
532 infopacket->sb[1] = 0x1A;
534 /* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
535 infopacket->sb[2] = 0x00;
537 /* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
538 infopacket->sb[3] = 0x00;
544 /* PB6 = [Bits 7:3 = Reserved] */
546 /* PB6 = [Bit 0 = FreeSync Supported] */
547 if (vrr->state != VRR_STATE_UNSUPPORTED)
548 infopacket->sb[6] |= 0x01;
550 /* PB6 = [Bit 1 = FreeSync Enabled] */
551 if (vrr->state != VRR_STATE_DISABLED &&
552 vrr->state != VRR_STATE_UNSUPPORTED)
553 infopacket->sb[6] |= 0x02;
555 if (freesync_on_desktop) {
556 /* PB6 = [Bit 2 = FreeSync Active] */
557 if (vrr->state != VRR_STATE_DISABLED &&
558 vrr->state != VRR_STATE_UNSUPPORTED)
559 infopacket->sb[6] |= 0x04;
561 if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
562 vrr->state == VRR_STATE_ACTIVE_FIXED)
563 infopacket->sb[6] |= 0x04;
566 // For v1 & 2 infoframes program nominal if non-fs mode, otherwise full range
567 /* PB7 = FreeSync Minimum refresh rate (Hz) */
568 if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
569 vrr->state == VRR_STATE_ACTIVE_FIXED) {
570 infopacket->sb[7] = (unsigned char)((vrr->min_refresh_in_uhz + 500000) / 1000000);
572 infopacket->sb[7] = (unsigned char)((vrr->max_refresh_in_uhz + 500000) / 1000000);
575 /* PB8 = FreeSync Maximum refresh rate (Hz)
576 * Note: We should never go above the field rate of the mode timing set.
578 infopacket->sb[8] = (unsigned char)((vrr->max_refresh_in_uhz + 500000) / 1000000);
581 infopacket->sb[9] = 0;
582 infopacket->sb[10] = 0;
585 static void build_vrr_infopacket_data_v3(const struct mod_vrr_params *vrr,
586 struct dc_info_packet *infopacket)
588 unsigned int min_refresh;
589 unsigned int max_refresh;
590 unsigned int fixed_refresh;
591 unsigned int min_programmed;
592 unsigned int max_programmed;
594 /* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
595 infopacket->sb[1] = 0x1A;
597 /* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
598 infopacket->sb[2] = 0x00;
600 /* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
601 infopacket->sb[3] = 0x00;
607 /* PB6 = [Bits 7:3 = Reserved] */
609 /* PB6 = [Bit 0 = FreeSync Supported] */
610 if (vrr->state != VRR_STATE_UNSUPPORTED)
611 infopacket->sb[6] |= 0x01;
613 /* PB6 = [Bit 1 = FreeSync Enabled] */
614 if (vrr->state != VRR_STATE_DISABLED &&
615 vrr->state != VRR_STATE_UNSUPPORTED)
616 infopacket->sb[6] |= 0x02;
618 /* PB6 = [Bit 2 = FreeSync Active] */
619 if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
620 vrr->state == VRR_STATE_ACTIVE_FIXED)
621 infopacket->sb[6] |= 0x04;
623 min_refresh = (vrr->min_refresh_in_uhz + 500000) / 1000000;
624 max_refresh = (vrr->max_refresh_in_uhz + 500000) / 1000000;
625 fixed_refresh = (vrr->fixed_refresh_in_uhz + 500000) / 1000000;
627 min_programmed = (vrr->state == VRR_STATE_ACTIVE_FIXED) ? fixed_refresh :
628 (vrr->state == VRR_STATE_ACTIVE_VARIABLE) ? min_refresh :
629 (vrr->state == VRR_STATE_INACTIVE) ? min_refresh :
630 max_refresh; // Non-fs case, program nominal range
632 max_programmed = (vrr->state == VRR_STATE_ACTIVE_FIXED) ? fixed_refresh :
633 (vrr->state == VRR_STATE_ACTIVE_VARIABLE) ? max_refresh :
634 max_refresh;// Non-fs case, program nominal range
636 /* PB7 = FreeSync Minimum refresh rate (Hz) */
637 infopacket->sb[7] = min_programmed & 0xFF;
639 /* PB8 = FreeSync Maximum refresh rate (Hz) */
640 infopacket->sb[8] = max_programmed & 0xFF;
642 /* PB11 : MSB FreeSync Minimum refresh rate [Hz] - bits 9:8 */
643 infopacket->sb[11] = (min_programmed >> 8) & 0x03;
645 /* PB12 : MSB FreeSync Maximum refresh rate [Hz] - bits 9:8 */
646 infopacket->sb[12] = (max_programmed >> 8) & 0x03;
648 /* PB16 : Reserved bits 7:1, FixedRate bit 0 */
649 infopacket->sb[16] = (vrr->state == VRR_STATE_ACTIVE_FIXED) ? 1 : 0;
652 infopacket->sb[9] = 0;
653 infopacket->sb[10] = 0;
656 static void build_vrr_infopacket_fs2_data(enum color_transfer_func app_tf,
657 struct dc_info_packet *infopacket)
659 if (app_tf != TRANSFER_FUNC_UNKNOWN) {
660 infopacket->valid = true;
662 infopacket->sb[6] |= 0x08; // PB6 = [Bit 3 = Native Color Active]
664 if (app_tf == TRANSFER_FUNC_GAMMA_22) {
665 infopacket->sb[9] |= 0x04; // PB6 = [Bit 2 = Gamma 2.2 EOTF Active]
670 static void build_vrr_infopacket_header_v1(enum signal_type signal,
671 struct dc_info_packet *infopacket,
672 unsigned int *payload_size)
674 if (dc_is_hdmi_signal(signal)) {
678 /* HB0 = Packet Type = 0x83 (Source Product
679 * Descriptor InfoFrame)
681 infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
683 /* HB1 = Version = 0x01 */
684 infopacket->hb1 = 0x01;
686 /* HB2 = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x08] */
687 infopacket->hb2 = 0x08;
689 *payload_size = 0x08;
691 } else if (dc_is_dp_signal(signal)) {
695 /* HB0 = Secondary-data Packet ID = 0 - Only non-zero
696 * when used to associate audio related info packets
698 infopacket->hb0 = 0x00;
700 /* HB1 = Packet Type = 0x83 (Source Product
701 * Descriptor InfoFrame)
703 infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
705 /* HB2 = [Bits 7:0 = Least significant eight bits -
706 * For INFOFRAME, the value must be 1Bh]
708 infopacket->hb2 = 0x1B;
710 /* HB3 = [Bits 7:2 = INFOFRAME SDP Version Number = 0x1]
711 * [Bits 1:0 = Most significant two bits = 0x00]
713 infopacket->hb3 = 0x04;
715 *payload_size = 0x1B;
719 static void build_vrr_infopacket_header_v2(enum signal_type signal,
720 struct dc_info_packet *infopacket,
721 unsigned int *payload_size)
723 if (dc_is_hdmi_signal(signal)) {
727 /* HB0 = Packet Type = 0x83 (Source Product
728 * Descriptor InfoFrame)
730 infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
732 /* HB1 = Version = 0x02 */
733 infopacket->hb1 = 0x02;
735 /* HB2 = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x09] */
736 infopacket->hb2 = 0x09;
738 *payload_size = 0x0A;
740 } else if (dc_is_dp_signal(signal)) {
744 /* HB0 = Secondary-data Packet ID = 0 - Only non-zero
745 * when used to associate audio related info packets
747 infopacket->hb0 = 0x00;
749 /* HB1 = Packet Type = 0x83 (Source Product
750 * Descriptor InfoFrame)
752 infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
754 /* HB2 = [Bits 7:0 = Least significant eight bits -
755 * For INFOFRAME, the value must be 1Bh]
757 infopacket->hb2 = 0x1B;
759 /* HB3 = [Bits 7:2 = INFOFRAME SDP Version Number = 0x2]
760 * [Bits 1:0 = Most significant two bits = 0x00]
762 infopacket->hb3 = 0x08;
764 *payload_size = 0x1B;
768 static void build_vrr_infopacket_header_v3(enum signal_type signal,
769 struct dc_info_packet *infopacket,
770 unsigned int *payload_size)
772 unsigned char version;
775 if (dc_is_hdmi_signal(signal)) {
779 /* HB0 = Packet Type = 0x83 (Source Product
780 * Descriptor InfoFrame)
782 infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
784 /* HB1 = Version = 0x03 */
785 infopacket->hb1 = version;
787 /* HB2 = [Bits 7:5 = 0] [Bits 4:0 = Length] */
788 *payload_size = 0x10;
789 infopacket->hb2 = *payload_size - 1; //-1 for checksum
791 } else if (dc_is_dp_signal(signal)) {
795 /* HB0 = Secondary-data Packet ID = 0 - Only non-zero
796 * when used to associate audio related info packets
798 infopacket->hb0 = 0x00;
800 /* HB1 = Packet Type = 0x83 (Source Product
801 * Descriptor InfoFrame)
803 infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
805 /* HB2 = [Bits 7:0 = Least significant eight bits -
806 * For INFOFRAME, the value must be 1Bh]
808 infopacket->hb2 = 0x1B;
810 /* HB3 = [Bits 7:2 = INFOFRAME SDP Version Number = 0x2]
811 * [Bits 1:0 = Most significant two bits = 0x00]
814 infopacket->hb3 = (version & 0x3F) << 2;
816 *payload_size = 0x1B;
820 static void build_vrr_infopacket_checksum(unsigned int *payload_size,
821 struct dc_info_packet *infopacket)
823 /* Calculate checksum */
824 unsigned int idx = 0;
825 unsigned char checksum = 0;
827 checksum += infopacket->hb0;
828 checksum += infopacket->hb1;
829 checksum += infopacket->hb2;
830 checksum += infopacket->hb3;
832 for (idx = 1; idx <= *payload_size; idx++)
833 checksum += infopacket->sb[idx];
835 /* PB0 = Checksum (one byte complement) */
836 infopacket->sb[0] = (unsigned char)(0x100 - checksum);
838 infopacket->valid = true;
841 static void build_vrr_infopacket_v1(enum signal_type signal,
842 const struct mod_vrr_params *vrr,
843 struct dc_info_packet *infopacket,
844 bool freesync_on_desktop)
846 /* SPD info packet for FreeSync */
847 unsigned int payload_size = 0;
849 build_vrr_infopacket_header_v1(signal, infopacket, &payload_size);
850 build_vrr_infopacket_data_v1(vrr, infopacket, freesync_on_desktop);
851 build_vrr_infopacket_checksum(&payload_size, infopacket);
853 infopacket->valid = true;
856 static void build_vrr_infopacket_v2(enum signal_type signal,
857 const struct mod_vrr_params *vrr,
858 enum color_transfer_func app_tf,
859 struct dc_info_packet *infopacket,
860 bool freesync_on_desktop)
862 unsigned int payload_size = 0;
864 build_vrr_infopacket_header_v2(signal, infopacket, &payload_size);
865 build_vrr_infopacket_data_v1(vrr, infopacket, freesync_on_desktop);
867 build_vrr_infopacket_fs2_data(app_tf, infopacket);
869 build_vrr_infopacket_checksum(&payload_size, infopacket);
871 infopacket->valid = true;
874 static void build_vrr_infopacket_fast_transport_data(
876 unsigned int ftOutputRate,
877 struct dc_info_packet *infopacket)
879 /* PB9 : bit7 - fast transport Active*/
880 unsigned char activeBit = (ftActive) ? 1 << 7 : 0;
882 infopacket->sb[1] &= ~activeBit; //clear bit
883 infopacket->sb[1] |= activeBit; //set bit
885 /* PB13 : Target Output Pixel Rate [kHz] - bits 7:0 */
886 infopacket->sb[13] = ftOutputRate & 0xFF;
888 /* PB14 : Target Output Pixel Rate [kHz] - bits 15:8 */
889 infopacket->sb[14] = (ftOutputRate >> 8) & 0xFF;
891 /* PB15 : Target Output Pixel Rate [kHz] - bits 23:16 */
892 infopacket->sb[15] = (ftOutputRate >> 16) & 0xFF;
897 static void build_vrr_infopacket_v3(enum signal_type signal,
898 const struct mod_vrr_params *vrr,
900 bool ftActive, unsigned int ftOutputRate,
902 enum color_transfer_func app_tf,
903 struct dc_info_packet *infopacket)
905 unsigned int payload_size = 0;
907 build_vrr_infopacket_header_v3(signal, infopacket, &payload_size);
908 build_vrr_infopacket_data_v3(vrr, infopacket);
910 build_vrr_infopacket_fs2_data(app_tf, infopacket);
913 build_vrr_infopacket_fast_transport_data(
919 build_vrr_infopacket_checksum(&payload_size, infopacket);
921 infopacket->valid = true;
924 static void build_vrr_infopacket_sdp_v1_3(enum vrr_packet_type packet_type,
925 struct dc_info_packet *infopacket)
927 uint8_t idx = 0, size = 0;
929 size = ((packet_type == PACKET_TYPE_FS_V1) ? 0x08 :
930 (packet_type == PACKET_TYPE_FS_V3) ? 0x10 :
933 for (idx = infopacket->hb2; idx > 1; idx--) // Data Byte Count: 0x1B
934 infopacket->sb[idx] = infopacket->sb[idx-1];
936 infopacket->sb[1] = size; // Length
937 infopacket->sb[0] = (infopacket->hb3 >> 2) & 0x3F;//Version
938 infopacket->hb3 = (0x13 << 2); // Header,SDP 1.3
939 infopacket->hb2 = 0x1D;
942 void mod_freesync_build_vrr_infopacket(struct mod_freesync *mod_freesync,
943 const struct dc_stream_state *stream,
944 const struct mod_vrr_params *vrr,
945 enum vrr_packet_type packet_type,
946 enum color_transfer_func app_tf,
947 struct dc_info_packet *infopacket,
950 /* SPD info packet for FreeSync
951 * VTEM info packet for HdmiVRR
952 * Check if Freesync is supported. Return if false. If true,
953 * set the corresponding bit in the info packet
955 if (!vrr->send_info_frame)
958 switch (packet_type) {
959 case PACKET_TYPE_FS_V3:
961 // always populate with pixel rate.
962 build_vrr_infopacket_v3(
964 stream->timing.flags.FAST_TRANSPORT,
965 (stream->timing.flags.FAST_TRANSPORT) ?
966 stream->timing.fast_transport_output_rate_100hz :
967 stream->timing.pix_clk_100hz,
970 build_vrr_infopacket_v3(stream->signal, vrr, app_tf, infopacket);
973 case PACKET_TYPE_FS_V2:
974 build_vrr_infopacket_v2(stream->signal, vrr, app_tf, infopacket, stream->freesync_on_desktop);
976 case PACKET_TYPE_VRR:
977 case PACKET_TYPE_FS_V1:
979 build_vrr_infopacket_v1(stream->signal, vrr, infopacket, stream->freesync_on_desktop);
982 if (true == pack_sdp_v1_3 &&
983 true == dc_is_dp_signal(stream->signal) &&
984 packet_type != PACKET_TYPE_VRR &&
985 packet_type != PACKET_TYPE_VTEM)
986 build_vrr_infopacket_sdp_v1_3(packet_type, infopacket);
989 void mod_freesync_build_vrr_params(struct mod_freesync *mod_freesync,
990 const struct dc_stream_state *stream,
991 struct mod_freesync_config *in_config,
992 struct mod_vrr_params *in_out_vrr)
994 struct core_freesync *core_freesync = NULL;
995 unsigned long long nominal_field_rate_in_uhz = 0;
996 unsigned long long rounded_nominal_in_uhz = 0;
997 unsigned int refresh_range = 0;
998 unsigned long long min_refresh_in_uhz = 0;
999 unsigned long long max_refresh_in_uhz = 0;
1001 if (mod_freesync == NULL)
1004 core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
1006 /* Calculate nominal field rate for stream */
1007 nominal_field_rate_in_uhz =
1008 mod_freesync_calc_nominal_field_rate(stream);
1010 min_refresh_in_uhz = in_config->min_refresh_in_uhz;
1011 max_refresh_in_uhz = in_config->max_refresh_in_uhz;
1013 /* Full range may be larger than current video timing, so cap at nominal */
1014 if (max_refresh_in_uhz > nominal_field_rate_in_uhz)
1015 max_refresh_in_uhz = nominal_field_rate_in_uhz;
1017 /* Full range may be larger than current video timing, so cap at nominal */
1018 if (min_refresh_in_uhz > max_refresh_in_uhz)
1019 min_refresh_in_uhz = max_refresh_in_uhz;
1021 /* If a monitor reports exactly max refresh of 2x of min, enforce it on nominal */
1022 rounded_nominal_in_uhz =
1023 div_u64(nominal_field_rate_in_uhz + 50000, 100000) * 100000;
1024 if (in_config->max_refresh_in_uhz == (2 * in_config->min_refresh_in_uhz) &&
1025 in_config->max_refresh_in_uhz == rounded_nominal_in_uhz)
1026 min_refresh_in_uhz = div_u64(nominal_field_rate_in_uhz, 2);
1028 if (!vrr_settings_require_update(core_freesync,
1029 in_config, (unsigned int)min_refresh_in_uhz, (unsigned int)max_refresh_in_uhz,
1033 in_out_vrr->state = in_config->state;
1034 in_out_vrr->send_info_frame = in_config->vsif_supported;
1036 if (in_config->state == VRR_STATE_UNSUPPORTED) {
1037 in_out_vrr->state = VRR_STATE_UNSUPPORTED;
1038 in_out_vrr->supported = false;
1039 in_out_vrr->adjust.v_total_min = stream->timing.v_total;
1040 in_out_vrr->adjust.v_total_max = stream->timing.v_total;
1045 in_out_vrr->min_refresh_in_uhz = (unsigned int)min_refresh_in_uhz;
1046 in_out_vrr->max_duration_in_us =
1047 calc_duration_in_us_from_refresh_in_uhz(
1048 (unsigned int)min_refresh_in_uhz);
1050 in_out_vrr->max_refresh_in_uhz = (unsigned int)max_refresh_in_uhz;
1051 in_out_vrr->min_duration_in_us =
1052 calc_duration_in_us_from_refresh_in_uhz(
1053 (unsigned int)max_refresh_in_uhz);
1055 if (in_config->state == VRR_STATE_ACTIVE_FIXED)
1056 in_out_vrr->fixed_refresh_in_uhz = in_config->fixed_refresh_in_uhz;
1058 in_out_vrr->fixed_refresh_in_uhz = 0;
1060 refresh_range = div_u64(in_out_vrr->max_refresh_in_uhz + 500000, 1000000) -
1061 + div_u64(in_out_vrr->min_refresh_in_uhz + 500000, 1000000);
1063 in_out_vrr->supported = true;
1066 in_out_vrr->fixed.ramping_active = in_config->ramping;
1068 in_out_vrr->btr.btr_enabled = in_config->btr;
1070 if (in_out_vrr->max_refresh_in_uhz < (2 * in_out_vrr->min_refresh_in_uhz))
1071 in_out_vrr->btr.btr_enabled = false;
1073 in_out_vrr->btr.margin_in_us = in_out_vrr->max_duration_in_us -
1074 2 * in_out_vrr->min_duration_in_us;
1075 if (in_out_vrr->btr.margin_in_us > BTR_MAX_MARGIN)
1076 in_out_vrr->btr.margin_in_us = BTR_MAX_MARGIN;
1079 in_out_vrr->btr.btr_active = false;
1080 in_out_vrr->btr.inserted_duration_in_us = 0;
1081 in_out_vrr->btr.frames_to_insert = 0;
1082 in_out_vrr->btr.frame_counter = 0;
1083 in_out_vrr->fixed.fixed_active = false;
1084 in_out_vrr->fixed.target_refresh_in_uhz = 0;
1086 in_out_vrr->btr.mid_point_in_us =
1087 (in_out_vrr->min_duration_in_us +
1088 in_out_vrr->max_duration_in_us) / 2;
1090 if (in_out_vrr->state == VRR_STATE_UNSUPPORTED) {
1091 in_out_vrr->adjust.v_total_min = stream->timing.v_total;
1092 in_out_vrr->adjust.v_total_max = stream->timing.v_total;
1093 } else if (in_out_vrr->state == VRR_STATE_DISABLED) {
1094 in_out_vrr->adjust.v_total_min = stream->timing.v_total;
1095 in_out_vrr->adjust.v_total_max = stream->timing.v_total;
1096 } else if (in_out_vrr->state == VRR_STATE_INACTIVE) {
1097 in_out_vrr->adjust.v_total_min = stream->timing.v_total;
1098 in_out_vrr->adjust.v_total_max = stream->timing.v_total;
1099 } else if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
1100 refresh_range >= MIN_REFRESH_RANGE) {
1102 in_out_vrr->adjust.v_total_min =
1103 mod_freesync_calc_v_total_from_refresh(stream,
1104 in_out_vrr->max_refresh_in_uhz);
1105 in_out_vrr->adjust.v_total_max =
1106 mod_freesync_calc_v_total_from_refresh(stream,
1107 in_out_vrr->min_refresh_in_uhz);
1108 } else if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED) {
1109 in_out_vrr->fixed.target_refresh_in_uhz =
1110 in_out_vrr->fixed_refresh_in_uhz;
1111 if (in_out_vrr->fixed.ramping_active &&
1112 in_out_vrr->fixed.fixed_active) {
1113 /* Do not update vtotals if ramping is already active
1114 * in order to continue ramp from current refresh.
1116 in_out_vrr->fixed.fixed_active = true;
1118 in_out_vrr->fixed.fixed_active = true;
1119 in_out_vrr->adjust.v_total_min =
1120 mod_freesync_calc_v_total_from_refresh(stream,
1121 in_out_vrr->fixed.target_refresh_in_uhz);
1122 in_out_vrr->adjust.v_total_max =
1123 in_out_vrr->adjust.v_total_min;
1126 in_out_vrr->state = VRR_STATE_INACTIVE;
1127 in_out_vrr->adjust.v_total_min = stream->timing.v_total;
1128 in_out_vrr->adjust.v_total_max = stream->timing.v_total;
1132 void mod_freesync_handle_preflip(struct mod_freesync *mod_freesync,
1133 const struct dc_plane_state *plane,
1134 const struct dc_stream_state *stream,
1135 unsigned int curr_time_stamp_in_us,
1136 struct mod_vrr_params *in_out_vrr)
1138 struct core_freesync *core_freesync = NULL;
1139 unsigned int last_render_time_in_us = 0;
1140 unsigned int average_render_time_in_us = 0;
1142 if (mod_freesync == NULL)
1145 core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
1147 if (in_out_vrr->supported &&
1148 in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE) {
1150 unsigned int oldest_index = plane->time.index + 1;
1152 if (oldest_index >= DC_PLANE_UPDATE_TIMES_MAX)
1155 last_render_time_in_us = curr_time_stamp_in_us -
1156 plane->time.prev_update_time_in_us;
1158 /* Sum off all entries except oldest one */
1159 for (i = 0; i < DC_PLANE_UPDATE_TIMES_MAX; i++) {
1160 average_render_time_in_us +=
1161 plane->time.time_elapsed_in_us[i];
1163 average_render_time_in_us -=
1164 plane->time.time_elapsed_in_us[oldest_index];
1166 /* Add render time for current flip */
1167 average_render_time_in_us += last_render_time_in_us;
1168 average_render_time_in_us /= DC_PLANE_UPDATE_TIMES_MAX;
1170 if (in_out_vrr->btr.btr_enabled) {
1171 apply_below_the_range(core_freesync,
1173 last_render_time_in_us,
1176 apply_fixed_refresh(core_freesync,
1178 last_render_time_in_us,
1185 void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
1186 const struct dc_stream_state *stream,
1187 struct mod_vrr_params *in_out_vrr)
1189 struct core_freesync *core_freesync = NULL;
1191 if ((mod_freesync == NULL) || (stream == NULL) || (in_out_vrr == NULL))
1194 core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
1196 if (in_out_vrr->supported == false)
1199 /* Below the Range Logic */
1201 /* Only execute if in fullscreen mode */
1202 if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
1203 in_out_vrr->btr.btr_active) {
1204 /* TODO: pass in flag for Pre-DCE12 ASIC
1205 * in order for frame variable duration to take affect,
1206 * it needs to be done one VSYNC early, which is at
1207 * frameCounter == 1.
1208 * For DCE12 and newer updates to V_TOTAL_MIN/MAX
1209 * will take affect on current frame
1211 if (in_out_vrr->btr.frames_to_insert ==
1212 in_out_vrr->btr.frame_counter) {
1213 in_out_vrr->adjust.v_total_min =
1214 calc_v_total_from_duration(stream,
1216 in_out_vrr->btr.inserted_duration_in_us);
1217 in_out_vrr->adjust.v_total_max =
1218 in_out_vrr->adjust.v_total_min;
1221 if (in_out_vrr->btr.frame_counter > 0)
1222 in_out_vrr->btr.frame_counter--;
1224 /* Restore FreeSync */
1225 if (in_out_vrr->btr.frame_counter == 0) {
1226 in_out_vrr->adjust.v_total_min =
1227 mod_freesync_calc_v_total_from_refresh(stream,
1228 in_out_vrr->max_refresh_in_uhz);
1229 in_out_vrr->adjust.v_total_max =
1230 mod_freesync_calc_v_total_from_refresh(stream,
1231 in_out_vrr->min_refresh_in_uhz);
1235 /* If in fullscreen freesync mode or in video, do not program
1236 * static screen ramp values
1238 if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE)
1239 in_out_vrr->fixed.ramping_active = false;
1241 /* Gradual Static Screen Ramping Logic
1242 * Execute if ramp is active and user enabled freesync static screen
1244 if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED &&
1245 in_out_vrr->fixed.ramping_active) {
1246 update_v_total_for_static_ramp(
1247 core_freesync, stream, in_out_vrr);
1251 void mod_freesync_get_settings(struct mod_freesync *mod_freesync,
1252 const struct mod_vrr_params *vrr,
1253 unsigned int *v_total_min, unsigned int *v_total_max,
1254 unsigned int *event_triggers,
1255 unsigned int *window_min, unsigned int *window_max,
1256 unsigned int *lfc_mid_point_in_us,
1257 unsigned int *inserted_frames,
1258 unsigned int *inserted_duration_in_us)
1260 if (mod_freesync == NULL)
1263 if (vrr->supported) {
1264 *v_total_min = vrr->adjust.v_total_min;
1265 *v_total_max = vrr->adjust.v_total_max;
1266 *event_triggers = 0;
1267 *lfc_mid_point_in_us = vrr->btr.mid_point_in_us;
1268 *inserted_frames = vrr->btr.frames_to_insert;
1269 *inserted_duration_in_us = vrr->btr.inserted_duration_in_us;
1273 unsigned long long mod_freesync_calc_nominal_field_rate(
1274 const struct dc_stream_state *stream)
1276 unsigned long long nominal_field_rate_in_uhz = 0;
1277 unsigned int total = stream->timing.h_total * stream->timing.v_total;
1279 /* Calculate nominal field rate for stream, rounded up to nearest integer */
1280 nominal_field_rate_in_uhz = stream->timing.pix_clk_100hz;
1281 nominal_field_rate_in_uhz *= 100000000ULL;
1283 nominal_field_rate_in_uhz = div_u64(nominal_field_rate_in_uhz, total);
1285 return nominal_field_rate_in_uhz;
1288 unsigned long long mod_freesync_calc_field_rate_from_timing(
1289 unsigned int vtotal, unsigned int htotal, unsigned int pix_clk)
1291 unsigned long long field_rate_in_uhz = 0;
1292 unsigned int total = htotal * vtotal;
1294 /* Calculate nominal field rate for stream, rounded up to nearest integer */
1295 field_rate_in_uhz = pix_clk;
1296 field_rate_in_uhz *= 1000000ULL;
1298 field_rate_in_uhz = div_u64(field_rate_in_uhz, total);
1300 return field_rate_in_uhz;
1303 bool mod_freesync_is_valid_range(uint32_t min_refresh_cap_in_uhz,
1304 uint32_t max_refresh_cap_in_uhz,
1305 uint32_t nominal_field_rate_in_uhz)
1308 /* Typically nominal refresh calculated can have some fractional part.
1309 * Allow for some rounding error of actual video timing by taking floor
1310 * of caps and request. Round the nominal refresh rate.
1312 * Dividing will convert everything to units in Hz although input
1313 * variable name is in uHz!
1315 * Also note, this takes care of rounding error on the nominal refresh
1316 * so by rounding error we only expect it to be off by a small amount,
1317 * such as < 0.1 Hz. i.e. 143.9xxx or 144.1xxx.
1319 * Example 1. Caps Min = 40 Hz, Max = 144 Hz
1320 * Request Min = 40 Hz, Max = 144 Hz
1321 * Nominal = 143.5x Hz rounded to 144 Hz
1322 * This function should allow this as valid request
1324 * Example 2. Caps Min = 40 Hz, Max = 144 Hz
1325 * Request Min = 40 Hz, Max = 144 Hz
1326 * Nominal = 144.4x Hz rounded to 144 Hz
1327 * This function should allow this as valid request
1329 * Example 3. Caps Min = 40 Hz, Max = 144 Hz
1330 * Request Min = 40 Hz, Max = 144 Hz
1331 * Nominal = 120.xx Hz rounded to 120 Hz
1332 * This function should return NOT valid since the requested
1333 * max is greater than current timing's nominal
1335 * Example 4. Caps Min = 40 Hz, Max = 120 Hz
1336 * Request Min = 40 Hz, Max = 120 Hz
1337 * Nominal = 144.xx Hz rounded to 144 Hz
1338 * This function should return NOT valid since the nominal
1339 * is greater than the capability's max refresh
1341 nominal_field_rate_in_uhz =
1342 div_u64(nominal_field_rate_in_uhz + 500000, 1000000);
1343 min_refresh_cap_in_uhz /= 1000000;
1344 max_refresh_cap_in_uhz /= 1000000;
1346 /* Check nominal is within range */
1347 if (nominal_field_rate_in_uhz > max_refresh_cap_in_uhz ||
1348 nominal_field_rate_in_uhz < min_refresh_cap_in_uhz)
1351 /* If nominal is less than max, limit the max allowed refresh rate */
1352 if (nominal_field_rate_in_uhz < max_refresh_cap_in_uhz)
1353 max_refresh_cap_in_uhz = nominal_field_rate_in_uhz;
1355 /* Check min is within range */
1356 if (min_refresh_cap_in_uhz > max_refresh_cap_in_uhz)
1359 /* For variable range, check for at least 10 Hz range */
1360 if (nominal_field_rate_in_uhz - min_refresh_cap_in_uhz < 10)