freesync.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*
  2. * Copyright 2016 Advanced Micro Devices, Inc.
  3. *
  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:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  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.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #include "dm_services.h"
  26. #include "dc.h"
  27. #include "mod_freesync.h"
  28. #include "core_types.h"
  29. #include "core_dc.h"
  30. #define MOD_FREESYNC_MAX_CONCURRENT_STREAMS 32
  31. /* Refresh rate ramp at a fixed rate of 65 Hz/second */
  32. #define STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME ((1000 / 60) * 65)
  33. /* Number of elements in the render times cache array */
  34. #define RENDER_TIMES_MAX_COUNT 20
  35. /* Threshold to exit BTR (to avoid frequent enter-exits at the lower limit) */
  36. #define BTR_EXIT_MARGIN 2000
  37. #define FREESYNC_REGISTRY_NAME "freesync_v1"
  38. struct gradual_static_ramp {
  39. bool ramp_is_active;
  40. bool ramp_direction_is_up;
  41. unsigned int ramp_current_frame_duration_in_ns;
  42. };
  43. struct time_cache {
  44. /* video (48Hz feature) related */
  45. unsigned int update_duration_in_ns;
  46. /* BTR/fixed refresh related */
  47. unsigned int prev_time_stamp_in_us;
  48. unsigned int min_render_time_in_us;
  49. unsigned int max_render_time_in_us;
  50. unsigned int render_times_index;
  51. unsigned int render_times[RENDER_TIMES_MAX_COUNT];
  52. };
  53. struct below_the_range {
  54. bool btr_active;
  55. bool program_btr;
  56. unsigned int mid_point_in_us;
  57. unsigned int inserted_frame_duration_in_us;
  58. unsigned int frames_to_insert;
  59. unsigned int frame_counter;
  60. };
  61. struct fixed_refresh {
  62. bool fixed_refresh_active;
  63. bool program_fixed_refresh;
  64. };
  65. struct freesync_range {
  66. unsigned int min_refresh;
  67. unsigned int max_frame_duration;
  68. unsigned int vmax;
  69. unsigned int max_refresh;
  70. unsigned int min_frame_duration;
  71. unsigned int vmin;
  72. };
  73. struct freesync_state {
  74. bool fullscreen;
  75. bool static_screen;
  76. bool video;
  77. unsigned int nominal_refresh_rate_in_micro_hz;
  78. bool windowed_fullscreen;
  79. struct time_cache time;
  80. struct gradual_static_ramp static_ramp;
  81. struct below_the_range btr;
  82. struct fixed_refresh fixed_refresh;
  83. struct freesync_range freesync_range;
  84. };
  85. struct freesync_entity {
  86. const struct dc_stream *stream;
  87. struct mod_freesync_caps *caps;
  88. struct freesync_state state;
  89. struct mod_freesync_user_enable user_enable;
  90. };
  91. struct freesync_registry_options {
  92. unsigned int min_refresh_from_edid;
  93. };
  94. struct core_freesync {
  95. struct mod_freesync public;
  96. struct dc *dc;
  97. struct freesync_entity *map;
  98. int num_entities;
  99. struct freesync_registry_options opts;
  100. };
  101. #define MOD_FREESYNC_TO_CORE(mod_freesync)\
  102. container_of(mod_freesync, struct core_freesync, public)
  103. static bool check_dc_support(const struct dc *dc)
  104. {
  105. if (dc->stream_funcs.adjust_vmin_vmax == NULL)
  106. return false;
  107. return true;
  108. }
  109. struct mod_freesync *mod_freesync_create(struct dc *dc)
  110. {
  111. struct core_freesync *core_freesync =
  112. dm_alloc(sizeof(struct core_freesync));
  113. struct core_dc *core_dc = DC_TO_CORE(dc);
  114. struct persistent_data_flag flag;
  115. int i, data = 0;
  116. if (core_freesync == NULL)
  117. goto fail_alloc_context;
  118. core_freesync->map = dm_alloc(sizeof(struct freesync_entity) *
  119. MOD_FREESYNC_MAX_CONCURRENT_STREAMS);
  120. if (core_freesync->map == NULL)
  121. goto fail_alloc_map;
  122. for (i = 0; i < MOD_FREESYNC_MAX_CONCURRENT_STREAMS; i++)
  123. core_freesync->map[i].stream = NULL;
  124. core_freesync->num_entities = 0;
  125. if (dc == NULL)
  126. goto fail_construct;
  127. core_freesync->dc = dc;
  128. if (!check_dc_support(dc))
  129. goto fail_construct;
  130. /* Create initial module folder in registry for freesync enable data */
  131. flag.save_per_edid = true;
  132. flag.save_per_link = false;
  133. dm_write_persistent_data(core_dc->ctx, NULL, FREESYNC_REGISTRY_NAME, NULL, NULL,
  134. 0, &flag);
  135. flag.save_per_edid = false;
  136. flag.save_per_link = false;
  137. if (dm_read_persistent_data(core_dc->ctx, NULL, NULL,
  138. "DalDrrSupport", &data, sizeof(data), &flag)) {
  139. core_freesync->opts.min_refresh_from_edid = data;
  140. }
  141. return &core_freesync->public;
  142. fail_construct:
  143. dm_free(core_freesync->map);
  144. fail_alloc_map:
  145. dm_free(core_freesync);
  146. fail_alloc_context:
  147. return NULL;
  148. }
  149. void mod_freesync_destroy(struct mod_freesync *mod_freesync)
  150. {
  151. if (mod_freesync != NULL) {
  152. int i;
  153. struct core_freesync *core_freesync =
  154. MOD_FREESYNC_TO_CORE(mod_freesync);
  155. for (i = 0; i < core_freesync->num_entities; i++)
  156. if (core_freesync->map[i].stream)
  157. dc_stream_release(core_freesync->map[i].stream);
  158. dm_free(core_freesync->map);
  159. dm_free(core_freesync);
  160. }
  161. }
  162. /* Given a specific dc_stream* this function finds its equivalent
  163. * on the core_freesync->map and returns the corresponding index
  164. */
  165. static unsigned int map_index_from_stream(struct core_freesync *core_freesync,
  166. const struct dc_stream *stream)
  167. {
  168. unsigned int index = 0;
  169. for (index = 0; index < core_freesync->num_entities; index++) {
  170. if (core_freesync->map[index].stream == stream) {
  171. return index;
  172. }
  173. }
  174. /* Could not find stream requested */
  175. ASSERT(false);
  176. return index;
  177. }
  178. bool mod_freesync_add_stream(struct mod_freesync *mod_freesync,
  179. const struct dc_stream *stream, struct mod_freesync_caps *caps)
  180. {
  181. struct core_stream *core_stream = NULL;
  182. struct core_dc *core_dc = NULL;
  183. struct core_freesync *core_freesync = NULL;
  184. int persistent_freesync_enable = 0;
  185. struct persistent_data_flag flag;
  186. unsigned int nom_refresh_rate_micro_hz;
  187. unsigned long long temp;
  188. if (mod_freesync == NULL)
  189. return false;
  190. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  191. core_stream = DC_STREAM_TO_CORE(stream);
  192. core_dc = DC_TO_CORE(core_freesync->dc);
  193. flag.save_per_edid = true;
  194. flag.save_per_link = false;
  195. if (core_freesync->num_entities < MOD_FREESYNC_MAX_CONCURRENT_STREAMS) {
  196. dc_stream_retain(stream);
  197. temp = stream->timing.pix_clk_khz;
  198. temp *= 1000ULL * 1000ULL * 1000ULL;
  199. temp = div_u64(temp, stream->timing.h_total);
  200. temp = div_u64(temp, stream->timing.v_total);
  201. nom_refresh_rate_micro_hz = (unsigned int) temp;
  202. if (core_freesync->opts.min_refresh_from_edid != 0 &&
  203. dc_is_embedded_signal(
  204. stream->sink->sink_signal)) {
  205. caps->supported = true;
  206. caps->min_refresh_in_micro_hz =
  207. core_freesync->opts.min_refresh_from_edid *
  208. 1000000;
  209. caps->max_refresh_in_micro_hz =
  210. nom_refresh_rate_micro_hz;
  211. }
  212. core_freesync->map[core_freesync->num_entities].stream = stream;
  213. core_freesync->map[core_freesync->num_entities].caps = caps;
  214. core_freesync->map[core_freesync->num_entities].state.
  215. fullscreen = false;
  216. core_freesync->map[core_freesync->num_entities].state.
  217. static_screen = false;
  218. core_freesync->map[core_freesync->num_entities].state.
  219. video = false;
  220. core_freesync->map[core_freesync->num_entities].state.time.
  221. update_duration_in_ns = 0;
  222. core_freesync->map[core_freesync->num_entities].state.
  223. static_ramp.ramp_is_active = false;
  224. /* get persistent data from registry */
  225. if (dm_read_persistent_data(core_dc->ctx, stream->sink,
  226. FREESYNC_REGISTRY_NAME,
  227. "userenable", &persistent_freesync_enable,
  228. sizeof(int), &flag)) {
  229. core_freesync->map[core_freesync->num_entities].user_enable.
  230. enable_for_gaming =
  231. (persistent_freesync_enable & 1) ? true : false;
  232. core_freesync->map[core_freesync->num_entities].user_enable.
  233. enable_for_static =
  234. (persistent_freesync_enable & 2) ? true : false;
  235. core_freesync->map[core_freesync->num_entities].user_enable.
  236. enable_for_video =
  237. (persistent_freesync_enable & 4) ? true : false;
  238. } else {
  239. core_freesync->map[core_freesync->num_entities].user_enable.
  240. enable_for_gaming = false;
  241. core_freesync->map[core_freesync->num_entities].user_enable.
  242. enable_for_static = false;
  243. core_freesync->map[core_freesync->num_entities].user_enable.
  244. enable_for_video = false;
  245. }
  246. if (caps->supported &&
  247. nom_refresh_rate_micro_hz >= caps->min_refresh_in_micro_hz &&
  248. nom_refresh_rate_micro_hz <= caps->max_refresh_in_micro_hz)
  249. core_stream->public.ignore_msa_timing_param = 1;
  250. core_freesync->num_entities++;
  251. return true;
  252. }
  253. return false;
  254. }
  255. bool mod_freesync_remove_stream(struct mod_freesync *mod_freesync,
  256. const struct dc_stream *stream)
  257. {
  258. int i = 0;
  259. struct core_freesync *core_freesync = NULL;
  260. unsigned int index = 0;
  261. if (mod_freesync == NULL)
  262. return false;
  263. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  264. index = map_index_from_stream(core_freesync, stream);
  265. dc_stream_release(core_freesync->map[index].stream);
  266. core_freesync->map[index].stream = NULL;
  267. /* To remove this entity, shift everything after down */
  268. for (i = index; i < core_freesync->num_entities - 1; i++)
  269. core_freesync->map[i] = core_freesync->map[i + 1];
  270. core_freesync->num_entities--;
  271. return true;
  272. }
  273. static void update_stream_freesync_context(struct core_freesync *core_freesync,
  274. const struct dc_stream *stream)
  275. {
  276. unsigned int index;
  277. struct freesync_context *ctx;
  278. struct core_stream *core_stream;
  279. core_stream = DC_STREAM_TO_CORE(stream);
  280. ctx = &core_stream->public.freesync_ctx;
  281. index = map_index_from_stream(core_freesync, stream);
  282. ctx->supported = core_freesync->map[index].caps->supported;
  283. ctx->enabled = (core_freesync->map[index].user_enable.enable_for_gaming ||
  284. core_freesync->map[index].user_enable.enable_for_video ||
  285. core_freesync->map[index].user_enable.enable_for_static);
  286. ctx->active = (core_freesync->map[index].state.fullscreen ||
  287. core_freesync->map[index].state.video ||
  288. core_freesync->map[index].state.static_ramp.ramp_is_active);
  289. ctx->min_refresh_in_micro_hz =
  290. core_freesync->map[index].caps->min_refresh_in_micro_hz;
  291. ctx->nominal_refresh_in_micro_hz = core_freesync->
  292. map[index].state.nominal_refresh_rate_in_micro_hz;
  293. }
  294. static void update_stream(struct core_freesync *core_freesync,
  295. const struct dc_stream *stream)
  296. {
  297. struct core_stream *core_stream = DC_STREAM_TO_CORE(stream);
  298. unsigned int index = map_index_from_stream(core_freesync, stream);
  299. if (core_freesync->map[index].caps->supported) {
  300. core_stream->public.ignore_msa_timing_param = 1;
  301. update_stream_freesync_context(core_freesync, stream);
  302. }
  303. }
  304. static void calc_freesync_range(struct core_freesync *core_freesync,
  305. const struct dc_stream *stream,
  306. struct freesync_state *state,
  307. unsigned int min_refresh_in_uhz,
  308. unsigned int max_refresh_in_uhz)
  309. {
  310. unsigned int min_frame_duration_in_ns = 0, max_frame_duration_in_ns = 0;
  311. unsigned int index = map_index_from_stream(core_freesync, stream);
  312. uint32_t vtotal = stream->timing.v_total;
  313. if ((min_refresh_in_uhz == 0) || (max_refresh_in_uhz == 0)) {
  314. state->freesync_range.min_refresh =
  315. state->nominal_refresh_rate_in_micro_hz;
  316. state->freesync_range.max_refresh =
  317. state->nominal_refresh_rate_in_micro_hz;
  318. state->freesync_range.max_frame_duration = 0;
  319. state->freesync_range.min_frame_duration = 0;
  320. state->freesync_range.vmax = vtotal;
  321. state->freesync_range.vmin = vtotal;
  322. return;
  323. }
  324. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  325. (1000000000ULL * 1000000),
  326. max_refresh_in_uhz)));
  327. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  328. (1000000000ULL * 1000000),
  329. min_refresh_in_uhz)));
  330. state->freesync_range.min_refresh = min_refresh_in_uhz;
  331. state->freesync_range.max_refresh = max_refresh_in_uhz;
  332. state->freesync_range.max_frame_duration = max_frame_duration_in_ns;
  333. state->freesync_range.min_frame_duration = min_frame_duration_in_ns;
  334. state->freesync_range.vmax = div64_u64(div64_u64(((unsigned long long)(
  335. max_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  336. stream->timing.h_total), 1000000);
  337. state->freesync_range.vmin = div64_u64(div64_u64(((unsigned long long)(
  338. min_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  339. stream->timing.h_total), 1000000);
  340. /* In case of 4k free sync monitor, vmin or vmax cannot be less than vtotal */
  341. if (state->freesync_range.vmin < vtotal) {
  342. ASSERT(false);
  343. state->freesync_range.vmin = vtotal;
  344. }
  345. if (state->freesync_range.vmax < vtotal) {
  346. ASSERT(false);
  347. state->freesync_range.vmax = vtotal;
  348. }
  349. /* Determine whether BTR can be supported */
  350. if (max_frame_duration_in_ns >=
  351. 2 * min_frame_duration_in_ns)
  352. core_freesync->map[index].caps->btr_supported = true;
  353. else
  354. core_freesync->map[index].caps->btr_supported = false;
  355. /* Cache the time variables */
  356. state->time.max_render_time_in_us =
  357. max_frame_duration_in_ns / 1000;
  358. state->time.min_render_time_in_us =
  359. min_frame_duration_in_ns / 1000;
  360. state->btr.mid_point_in_us =
  361. (max_frame_duration_in_ns +
  362. min_frame_duration_in_ns) / 2000;
  363. }
  364. static void calc_v_total_from_duration(const struct dc_stream *stream,
  365. unsigned int duration_in_ns, int *v_total_nominal)
  366. {
  367. *v_total_nominal = div64_u64(div64_u64(((unsigned long long)(
  368. duration_in_ns) * stream->timing.pix_clk_khz),
  369. stream->timing.h_total), 1000000);
  370. }
  371. static void calc_v_total_for_static_ramp(struct core_freesync *core_freesync,
  372. const struct dc_stream *stream,
  373. unsigned int index, int *v_total)
  374. {
  375. unsigned int frame_duration = 0;
  376. struct gradual_static_ramp *static_ramp_variables =
  377. &core_freesync->map[index].state.static_ramp;
  378. /* Calc ratio between new and current frame duration with 3 digit */
  379. unsigned int frame_duration_ratio = div64_u64(1000000,
  380. (1000 + div64_u64(((unsigned long long)(
  381. STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
  382. static_ramp_variables->ramp_current_frame_duration_in_ns),
  383. 1000000000)));
  384. /* Calculate delta between new and current frame duration in ns */
  385. unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
  386. static_ramp_variables->ramp_current_frame_duration_in_ns) *
  387. (1000 - frame_duration_ratio)), 1000);
  388. /* Adjust frame duration delta based on ratio between current and
  389. * standard frame duration (frame duration at 60 Hz refresh rate).
  390. */
  391. unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
  392. frame_duration_delta) * static_ramp_variables->
  393. ramp_current_frame_duration_in_ns), 16666666);
  394. /* Going to a higher refresh rate (lower frame duration) */
  395. if (static_ramp_variables->ramp_direction_is_up) {
  396. /* reduce frame duration */
  397. static_ramp_variables->ramp_current_frame_duration_in_ns -=
  398. ramp_rate_interpolated;
  399. /* min frame duration */
  400. frame_duration = ((unsigned int) (div64_u64(
  401. (1000000000ULL * 1000000),
  402. core_freesync->map[index].state.
  403. nominal_refresh_rate_in_micro_hz)));
  404. /* adjust for frame duration below min */
  405. if (static_ramp_variables->ramp_current_frame_duration_in_ns <=
  406. frame_duration) {
  407. static_ramp_variables->ramp_is_active = false;
  408. static_ramp_variables->
  409. ramp_current_frame_duration_in_ns =
  410. frame_duration;
  411. }
  412. /* Going to a lower refresh rate (larger frame duration) */
  413. } else {
  414. /* increase frame duration */
  415. static_ramp_variables->ramp_current_frame_duration_in_ns +=
  416. ramp_rate_interpolated;
  417. /* max frame duration */
  418. frame_duration = ((unsigned int) (div64_u64(
  419. (1000000000ULL * 1000000),
  420. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  421. /* adjust for frame duration above max */
  422. if (static_ramp_variables->ramp_current_frame_duration_in_ns >=
  423. frame_duration) {
  424. static_ramp_variables->ramp_is_active = false;
  425. static_ramp_variables->
  426. ramp_current_frame_duration_in_ns =
  427. frame_duration;
  428. }
  429. }
  430. calc_v_total_from_duration(stream, static_ramp_variables->
  431. ramp_current_frame_duration_in_ns, v_total);
  432. }
  433. static void reset_freesync_state_variables(struct freesync_state* state)
  434. {
  435. state->static_ramp.ramp_is_active = false;
  436. if (state->nominal_refresh_rate_in_micro_hz)
  437. state->static_ramp.ramp_current_frame_duration_in_ns =
  438. ((unsigned int) (div64_u64(
  439. (1000000000ULL * 1000000),
  440. state->nominal_refresh_rate_in_micro_hz)));
  441. state->btr.btr_active = false;
  442. state->btr.frame_counter = 0;
  443. state->btr.frames_to_insert = 0;
  444. state->btr.inserted_frame_duration_in_us = 0;
  445. state->btr.program_btr = false;
  446. state->fixed_refresh.fixed_refresh_active = false;
  447. state->fixed_refresh.program_fixed_refresh = false;
  448. }
  449. /*
  450. * Sets freesync mode on a stream depending on current freesync state.
  451. */
  452. static bool set_freesync_on_streams(struct core_freesync *core_freesync,
  453. const struct dc_stream **streams, int num_streams)
  454. {
  455. int v_total_nominal = 0, v_total_min = 0, v_total_max = 0;
  456. unsigned int stream_idx, map_index = 0;
  457. struct freesync_state *state;
  458. if (num_streams == 0 || streams == NULL || num_streams > 1)
  459. return false;
  460. for (stream_idx = 0; stream_idx < num_streams; stream_idx++) {
  461. map_index = map_index_from_stream(core_freesync,
  462. streams[stream_idx]);
  463. state = &core_freesync->map[map_index].state;
  464. if (core_freesync->map[map_index].caps->supported) {
  465. /* Fullscreen has the topmost priority. If the
  466. * fullscreen bit is set, we are in a fullscreen
  467. * application where it should not matter if it is
  468. * static screen. We should not check the static_screen
  469. * or video bit.
  470. *
  471. * Special cases of fullscreen include btr and fixed
  472. * refresh. We program btr on every flip and involves
  473. * programming full range right before the last inserted frame.
  474. * However, we do not want to program the full freesync range
  475. * when fixed refresh is active, because we only program
  476. * that logic once and this will override it.
  477. */
  478. if (core_freesync->map[map_index].user_enable.
  479. enable_for_gaming == true &&
  480. state->fullscreen == true &&
  481. state->fixed_refresh.fixed_refresh_active == false) {
  482. /* Enable freesync */
  483. v_total_min = state->freesync_range.vmin;
  484. v_total_max = state->freesync_range.vmax;
  485. /* Update the freesync context for the stream */
  486. update_stream_freesync_context(core_freesync,
  487. streams[stream_idx]);
  488. core_freesync->dc->stream_funcs.
  489. adjust_vmin_vmax(core_freesync->dc, streams,
  490. num_streams, v_total_min,
  491. v_total_max);
  492. return true;
  493. } else if (core_freesync->map[map_index].user_enable.
  494. enable_for_video && state->video == true) {
  495. /* Enable 48Hz feature */
  496. calc_v_total_from_duration(streams[stream_idx],
  497. state->time.update_duration_in_ns,
  498. &v_total_nominal);
  499. /* Program only if v_total_nominal is in range*/
  500. if (v_total_nominal >=
  501. streams[stream_idx]->timing.v_total) {
  502. /* Update the freesync context for
  503. * the stream
  504. */
  505. update_stream_freesync_context(
  506. core_freesync,
  507. streams[stream_idx]);
  508. core_freesync->dc->stream_funcs.
  509. adjust_vmin_vmax(
  510. core_freesync->dc, streams,
  511. num_streams, v_total_nominal,
  512. v_total_nominal);
  513. }
  514. return true;
  515. } else {
  516. /* Disable freesync */
  517. v_total_nominal = streams[stream_idx]->
  518. timing.v_total;
  519. /* Update the freesync context for
  520. * the stream
  521. */
  522. update_stream_freesync_context(
  523. core_freesync,
  524. streams[stream_idx]);
  525. core_freesync->dc->stream_funcs.
  526. adjust_vmin_vmax(
  527. core_freesync->dc, streams,
  528. num_streams, v_total_nominal,
  529. v_total_nominal);
  530. /* Reset the cached variables */
  531. reset_freesync_state_variables(state);
  532. return true;
  533. }
  534. } else {
  535. /* Disable freesync */
  536. v_total_nominal = streams[stream_idx]->
  537. timing.v_total;
  538. /*
  539. * we have to reset drr always even sink does
  540. * not support freesync because a former stream has
  541. * be programmed
  542. */
  543. core_freesync->dc->stream_funcs.
  544. adjust_vmin_vmax(
  545. core_freesync->dc, streams,
  546. num_streams, v_total_nominal,
  547. v_total_nominal);
  548. /* Reset the cached variables */
  549. reset_freesync_state_variables(state);
  550. }
  551. }
  552. return false;
  553. }
  554. static void set_static_ramp_variables(struct core_freesync *core_freesync,
  555. unsigned int index, bool enable_static_screen)
  556. {
  557. unsigned int frame_duration = 0;
  558. struct gradual_static_ramp *static_ramp_variables =
  559. &core_freesync->map[index].state.static_ramp;
  560. /* If ramp is not active, set initial frame duration depending on
  561. * whether we are enabling/disabling static screen mode. If the ramp is
  562. * already active, ramp should continue in the opposite direction
  563. * starting with the current frame duration
  564. */
  565. if (!static_ramp_variables->ramp_is_active) {
  566. static_ramp_variables->ramp_is_active = true;
  567. if (enable_static_screen == true) {
  568. /* Going to lower refresh rate, so start from max
  569. * refresh rate (min frame duration)
  570. */
  571. frame_duration = ((unsigned int) (div64_u64(
  572. (1000000000ULL * 1000000),
  573. core_freesync->map[index].state.
  574. nominal_refresh_rate_in_micro_hz)));
  575. } else {
  576. /* Going to higher refresh rate, so start from min
  577. * refresh rate (max frame duration)
  578. */
  579. frame_duration = ((unsigned int) (div64_u64(
  580. (1000000000ULL * 1000000),
  581. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  582. }
  583. static_ramp_variables->
  584. ramp_current_frame_duration_in_ns = frame_duration;
  585. }
  586. /* If we are ENABLING static screen, refresh rate should go DOWN.
  587. * If we are DISABLING static screen, refresh rate should go UP.
  588. */
  589. static_ramp_variables->ramp_direction_is_up = !enable_static_screen;
  590. }
  591. void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
  592. const struct dc_stream **streams, int num_streams)
  593. {
  594. unsigned int index, v_total, inserted_frame_v_total = 0;
  595. unsigned int min_frame_duration_in_ns, vmax, vmin = 0;
  596. struct freesync_state *state;
  597. struct core_freesync *core_freesync = NULL;
  598. struct dc_static_screen_events triggers = {0};
  599. if (mod_freesync == NULL)
  600. return;
  601. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  602. if (core_freesync->num_entities == 0)
  603. return;
  604. index = map_index_from_stream(core_freesync,
  605. streams[0]);
  606. if (core_freesync->map[index].caps->supported == false)
  607. return;
  608. state = &core_freesync->map[index].state;
  609. /* Below the Range Logic */
  610. /* Only execute if in fullscreen mode */
  611. if (state->fullscreen == true &&
  612. core_freesync->map[index].user_enable.enable_for_gaming &&
  613. core_freesync->map[index].caps->btr_supported &&
  614. state->btr.btr_active) {
  615. /* TODO: pass in flag for Pre-DCE12 ASIC
  616. * in order for frame variable duration to take affect,
  617. * it needs to be done one VSYNC early, which is at
  618. * frameCounter == 1.
  619. * For DCE12 and newer updates to V_TOTAL_MIN/MAX
  620. * will take affect on current frame
  621. */
  622. if (state->btr.frames_to_insert == state->btr.frame_counter) {
  623. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  624. (1000000000ULL * 1000000),
  625. state->nominal_refresh_rate_in_micro_hz)));
  626. vmin = state->freesync_range.vmin;
  627. inserted_frame_v_total = vmin;
  628. if (min_frame_duration_in_ns / 1000)
  629. inserted_frame_v_total =
  630. state->btr.inserted_frame_duration_in_us *
  631. vmin / (min_frame_duration_in_ns / 1000);
  632. /* Set length of inserted frames as v_total_max*/
  633. vmax = inserted_frame_v_total;
  634. vmin = inserted_frame_v_total;
  635. /* Program V_TOTAL */
  636. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  637. core_freesync->dc, streams,
  638. num_streams, vmin, vmax);
  639. }
  640. if (state->btr.frame_counter > 0)
  641. state->btr.frame_counter--;
  642. /* Restore FreeSync */
  643. if (state->btr.frame_counter == 0)
  644. set_freesync_on_streams(core_freesync, streams, num_streams);
  645. }
  646. /* If in fullscreen freesync mode or in video, do not program
  647. * static screen ramp values
  648. */
  649. if (state->fullscreen == true || state->video == true) {
  650. state->static_ramp.ramp_is_active = false;
  651. return;
  652. }
  653. /* Gradual Static Screen Ramping Logic */
  654. /* Execute if ramp is active and user enabled freesync static screen*/
  655. if (state->static_ramp.ramp_is_active &&
  656. core_freesync->map[index].user_enable.enable_for_static) {
  657. calc_v_total_for_static_ramp(core_freesync, streams[0],
  658. index, &v_total);
  659. /* Update the freesync context for the stream */
  660. update_stream_freesync_context(core_freesync, streams[0]);
  661. /* Program static screen ramp values */
  662. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  663. core_freesync->dc, streams,
  664. num_streams, v_total,
  665. v_total);
  666. triggers.overlay_update = true;
  667. triggers.surface_update = true;
  668. core_freesync->dc->stream_funcs.set_static_screen_events(
  669. core_freesync->dc, streams, num_streams,
  670. &triggers);
  671. }
  672. }
  673. void mod_freesync_update_state(struct mod_freesync *mod_freesync,
  674. const struct dc_stream **streams, int num_streams,
  675. struct mod_freesync_params *freesync_params)
  676. {
  677. bool freesync_program_required = false;
  678. unsigned int stream_index;
  679. struct freesync_state *state;
  680. struct core_freesync *core_freesync = NULL;
  681. if (mod_freesync == NULL)
  682. return;
  683. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  684. if (core_freesync->num_entities == 0)
  685. return;
  686. for(stream_index = 0; stream_index < num_streams; stream_index++) {
  687. unsigned int map_index = map_index_from_stream(core_freesync,
  688. streams[stream_index]);
  689. state = &core_freesync->map[map_index].state;
  690. switch (freesync_params->state){
  691. case FREESYNC_STATE_FULLSCREEN:
  692. state->fullscreen = freesync_params->enable;
  693. freesync_program_required = true;
  694. state->windowed_fullscreen =
  695. freesync_params->windowed_fullscreen;
  696. break;
  697. case FREESYNC_STATE_STATIC_SCREEN:
  698. /* Static screen ramp is only enabled for embedded
  699. * panels. Also change core variables only if there
  700. * is a change.
  701. */
  702. if (dc_is_embedded_signal(
  703. streams[stream_index]->sink->sink_signal) &&
  704. state->static_screen !=
  705. freesync_params->enable) {
  706. /* Change the state flag */
  707. state->static_screen = freesync_params->enable;
  708. /* Change static screen ramp variables */
  709. set_static_ramp_variables(core_freesync,
  710. map_index,
  711. freesync_params->enable);
  712. }
  713. /* We program the ramp starting next VUpdate */
  714. break;
  715. case FREESYNC_STATE_VIDEO:
  716. /* Change core variables only if there is a change*/
  717. if(freesync_params->update_duration_in_ns !=
  718. state->time.update_duration_in_ns) {
  719. state->video = freesync_params->enable;
  720. state->time.update_duration_in_ns =
  721. freesync_params->update_duration_in_ns;
  722. freesync_program_required = true;
  723. }
  724. break;
  725. case FREESYNC_STATE_NONE:
  726. /* handle here to avoid warning */
  727. break;
  728. }
  729. }
  730. if (freesync_program_required)
  731. /* Program freesync according to current state*/
  732. set_freesync_on_streams(core_freesync, streams, num_streams);
  733. }
  734. bool mod_freesync_get_state(struct mod_freesync *mod_freesync,
  735. const struct dc_stream *stream,
  736. struct mod_freesync_params *freesync_params)
  737. {
  738. unsigned int index = 0;
  739. struct core_freesync *core_freesync = NULL;
  740. if (mod_freesync == NULL)
  741. return false;
  742. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  743. index = map_index_from_stream(core_freesync, stream);
  744. if (core_freesync->map[index].state.fullscreen) {
  745. freesync_params->state = FREESYNC_STATE_FULLSCREEN;
  746. freesync_params->enable = true;
  747. } else if (core_freesync->map[index].state.static_screen) {
  748. freesync_params->state = FREESYNC_STATE_STATIC_SCREEN;
  749. freesync_params->enable = true;
  750. } else if (core_freesync->map[index].state.video) {
  751. freesync_params->state = FREESYNC_STATE_VIDEO;
  752. freesync_params->enable = true;
  753. } else {
  754. freesync_params->state = FREESYNC_STATE_NONE;
  755. freesync_params->enable = false;
  756. }
  757. freesync_params->update_duration_in_ns =
  758. core_freesync->map[index].state.time.update_duration_in_ns;
  759. freesync_params->windowed_fullscreen =
  760. core_freesync->map[index].state.windowed_fullscreen;
  761. return true;
  762. }
  763. bool mod_freesync_set_user_enable(struct mod_freesync *mod_freesync,
  764. const struct dc_stream **streams, int num_streams,
  765. struct mod_freesync_user_enable *user_enable)
  766. {
  767. unsigned int stream_index, map_index;
  768. int persistent_data = 0;
  769. struct persistent_data_flag flag;
  770. struct core_dc *core_dc = NULL;
  771. struct core_freesync *core_freesync = NULL;
  772. if (mod_freesync == NULL)
  773. return false;
  774. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  775. core_dc = DC_TO_CORE(core_freesync->dc);
  776. flag.save_per_edid = true;
  777. flag.save_per_link = false;
  778. for(stream_index = 0; stream_index < num_streams;
  779. stream_index++){
  780. map_index = map_index_from_stream(core_freesync,
  781. streams[stream_index]);
  782. core_freesync->map[map_index].user_enable = *user_enable;
  783. /* Write persistent data in registry*/
  784. if (core_freesync->map[map_index].user_enable.
  785. enable_for_gaming)
  786. persistent_data = persistent_data | 1;
  787. if (core_freesync->map[map_index].user_enable.
  788. enable_for_static)
  789. persistent_data = persistent_data | 2;
  790. if (core_freesync->map[map_index].user_enable.
  791. enable_for_video)
  792. persistent_data = persistent_data | 4;
  793. dm_write_persistent_data(core_dc->ctx,
  794. streams[stream_index]->sink,
  795. FREESYNC_REGISTRY_NAME,
  796. "userenable",
  797. &persistent_data,
  798. sizeof(int),
  799. &flag);
  800. }
  801. set_freesync_on_streams(core_freesync, streams, num_streams);
  802. return true;
  803. }
  804. bool mod_freesync_get_user_enable(struct mod_freesync *mod_freesync,
  805. const struct dc_stream *stream,
  806. struct mod_freesync_user_enable *user_enable)
  807. {
  808. unsigned int index = 0;
  809. struct core_freesync *core_freesync = NULL;
  810. if (mod_freesync == NULL)
  811. return false;
  812. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  813. index = map_index_from_stream(core_freesync, stream);
  814. *user_enable = core_freesync->map[index].user_enable;
  815. return true;
  816. }
  817. bool mod_freesync_override_min_max(struct mod_freesync *mod_freesync,
  818. const struct dc_stream *streams,
  819. unsigned int min_refresh,
  820. unsigned int max_refresh)
  821. {
  822. unsigned int index = 0;
  823. struct core_freesync *core_freesync;
  824. struct freesync_state *state;
  825. if (mod_freesync == NULL)
  826. return false;
  827. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  828. index = map_index_from_stream(core_freesync, streams);
  829. state = &core_freesync->map[index].state;
  830. if (min_refresh == 0 || max_refresh == 0) {
  831. /* Restore defaults */
  832. calc_freesync_range(core_freesync, streams, state,
  833. core_freesync->map[index].caps->
  834. min_refresh_in_micro_hz,
  835. state->nominal_refresh_rate_in_micro_hz);
  836. } else {
  837. calc_freesync_range(core_freesync, streams,
  838. state,
  839. min_refresh,
  840. max_refresh);
  841. /* Program vtotal min/max */
  842. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  843. core_freesync->dc, &streams, 1,
  844. state->freesync_range.vmin,
  845. state->freesync_range.vmax);
  846. }
  847. return true;
  848. }
  849. bool mod_freesync_get_min_max(struct mod_freesync *mod_freesync,
  850. const struct dc_stream *stream,
  851. unsigned int *min_refresh,
  852. unsigned int *max_refresh)
  853. {
  854. unsigned int index = 0;
  855. struct core_freesync *core_freesync = NULL;
  856. if (mod_freesync == NULL)
  857. return false;
  858. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  859. index = map_index_from_stream(core_freesync, stream);
  860. *min_refresh =
  861. core_freesync->map[index].state.freesync_range.min_refresh;
  862. *max_refresh =
  863. core_freesync->map[index].state.freesync_range.max_refresh;
  864. return true;
  865. }
  866. bool mod_freesync_get_vmin_vmax(struct mod_freesync *mod_freesync,
  867. const struct dc_stream *stream,
  868. unsigned int *vmin,
  869. unsigned int *vmax)
  870. {
  871. unsigned int index = 0;
  872. struct core_freesync *core_freesync = NULL;
  873. if (mod_freesync == NULL)
  874. return false;
  875. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  876. index = map_index_from_stream(core_freesync, stream);
  877. *vmin =
  878. core_freesync->map[index].state.freesync_range.vmin;
  879. *vmax =
  880. core_freesync->map[index].state.freesync_range.vmax;
  881. return true;
  882. }
  883. bool mod_freesync_get_v_position(struct mod_freesync *mod_freesync,
  884. const struct dc_stream *stream,
  885. unsigned int *nom_v_pos,
  886. unsigned int *v_pos)
  887. {
  888. unsigned int index = 0;
  889. struct core_freesync *core_freesync = NULL;
  890. struct crtc_position position;
  891. if (mod_freesync == NULL)
  892. return false;
  893. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  894. index = map_index_from_stream(core_freesync, stream);
  895. if (core_freesync->dc->stream_funcs.get_crtc_position(
  896. core_freesync->dc, &stream, 1,
  897. &position.vertical_count, &position.nominal_vcount)) {
  898. *nom_v_pos = position.vertical_count;
  899. *v_pos = position.nominal_vcount;
  900. return true;
  901. }
  902. return false;
  903. }
  904. void mod_freesync_notify_mode_change(struct mod_freesync *mod_freesync,
  905. const struct dc_stream **streams, int num_streams)
  906. {
  907. unsigned int stream_index, map_index;
  908. struct freesync_state *state;
  909. struct core_freesync *core_freesync = NULL;
  910. if (mod_freesync == NULL)
  911. return;
  912. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  913. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  914. map_index = map_index_from_stream(core_freesync,
  915. streams[stream_index]);
  916. state = &core_freesync->map[map_index].state;
  917. if (core_freesync->map[map_index].caps->supported) {
  918. /* Update the field rate for new timing */
  919. unsigned long long temp;
  920. temp = streams[stream_index]->timing.pix_clk_khz;
  921. temp *= 1000ULL * 1000ULL * 1000ULL;
  922. temp = div_u64(temp,
  923. streams[stream_index]->timing.h_total);
  924. temp = div_u64(temp,
  925. streams[stream_index]->timing.v_total);
  926. state->nominal_refresh_rate_in_micro_hz =
  927. (unsigned int) temp;
  928. /* Update the stream */
  929. update_stream(core_freesync, streams[stream_index]);
  930. /* Calculate vmin/vmax and refresh rate for
  931. * current mode
  932. */
  933. calc_freesync_range(core_freesync, *streams, state,
  934. core_freesync->map[map_index].caps->
  935. min_refresh_in_micro_hz,
  936. state->nominal_refresh_rate_in_micro_hz);
  937. }
  938. }
  939. /* Program freesync according to current state*/
  940. set_freesync_on_streams(core_freesync, streams, num_streams);
  941. }
  942. /* Add the timestamps to the cache and determine whether BTR programming
  943. * is required, depending on the times calculated
  944. */
  945. static void update_timestamps(struct core_freesync *core_freesync,
  946. const struct dc_stream *stream, unsigned int map_index,
  947. unsigned int last_render_time_in_us)
  948. {
  949. struct freesync_state *state = &core_freesync->map[map_index].state;
  950. state->time.render_times[state->time.render_times_index] =
  951. last_render_time_in_us;
  952. state->time.render_times_index++;
  953. if (state->time.render_times_index >= RENDER_TIMES_MAX_COUNT)
  954. state->time.render_times_index = 0;
  955. if (last_render_time_in_us + BTR_EXIT_MARGIN <
  956. state->time.max_render_time_in_us) {
  957. /* Exit Below the Range */
  958. if (state->btr.btr_active) {
  959. state->btr.program_btr = true;
  960. state->btr.btr_active = false;
  961. state->btr.frame_counter = 0;
  962. /* Exit Fixed Refresh mode */
  963. } else if (state->fixed_refresh.fixed_refresh_active) {
  964. state->fixed_refresh.program_fixed_refresh = true;
  965. state->fixed_refresh.fixed_refresh_active = false;
  966. }
  967. } else if (last_render_time_in_us > state->time.max_render_time_in_us) {
  968. /* Enter Below the Range */
  969. if (!state->btr.btr_active &&
  970. core_freesync->map[map_index].caps->btr_supported) {
  971. state->btr.program_btr = true;
  972. state->btr.btr_active = true;
  973. /* Enter Fixed Refresh mode */
  974. } else if (!state->fixed_refresh.fixed_refresh_active &&
  975. !core_freesync->map[map_index].caps->btr_supported) {
  976. state->fixed_refresh.program_fixed_refresh = true;
  977. state->fixed_refresh.fixed_refresh_active = true;
  978. }
  979. }
  980. /* When Below the Range is active, must react on every frame */
  981. if (state->btr.btr_active)
  982. state->btr.program_btr = true;
  983. }
  984. static void apply_below_the_range(struct core_freesync *core_freesync,
  985. const struct dc_stream *stream, unsigned int map_index,
  986. unsigned int last_render_time_in_us)
  987. {
  988. unsigned int inserted_frame_duration_in_us = 0;
  989. unsigned int mid_point_frames_ceil = 0;
  990. unsigned int mid_point_frames_floor = 0;
  991. unsigned int frame_time_in_us = 0;
  992. unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
  993. unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
  994. unsigned int frames_to_insert = 0;
  995. unsigned int min_frame_duration_in_ns = 0;
  996. struct freesync_state *state = &core_freesync->map[map_index].state;
  997. if (!state->btr.program_btr)
  998. return;
  999. state->btr.program_btr = false;
  1000. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  1001. (1000000000ULL * 1000000),
  1002. state->nominal_refresh_rate_in_micro_hz)));
  1003. /* Program BTR */
  1004. /* BTR set to "not active" so disengage */
  1005. if (!state->btr.btr_active)
  1006. /* Restore FreeSync */
  1007. set_freesync_on_streams(core_freesync, &stream, 1);
  1008. /* BTR set to "active" so engage */
  1009. else {
  1010. /* Calculate number of midPoint frames that could fit within
  1011. * the render time interval- take ceil of this value
  1012. */
  1013. mid_point_frames_ceil = (last_render_time_in_us +
  1014. state->btr.mid_point_in_us- 1) /
  1015. state->btr.mid_point_in_us;
  1016. if (mid_point_frames_ceil > 0) {
  1017. frame_time_in_us = last_render_time_in_us /
  1018. mid_point_frames_ceil;
  1019. delta_from_mid_point_in_us_1 = (state->btr.mid_point_in_us >
  1020. frame_time_in_us) ?
  1021. (state->btr.mid_point_in_us - frame_time_in_us):
  1022. (frame_time_in_us - state->btr.mid_point_in_us);
  1023. }
  1024. /* Calculate number of midPoint frames that could fit within
  1025. * the render time interval- take floor of this value
  1026. */
  1027. mid_point_frames_floor = last_render_time_in_us /
  1028. state->btr.mid_point_in_us;
  1029. if (mid_point_frames_floor > 0) {
  1030. frame_time_in_us = last_render_time_in_us /
  1031. mid_point_frames_floor;
  1032. delta_from_mid_point_in_us_2 = (state->btr.mid_point_in_us >
  1033. frame_time_in_us) ?
  1034. (state->btr.mid_point_in_us - frame_time_in_us):
  1035. (frame_time_in_us - state->btr.mid_point_in_us);
  1036. }
  1037. /* Choose number of frames to insert based on how close it
  1038. * can get to the mid point of the variable range.
  1039. */
  1040. if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2)
  1041. frames_to_insert = mid_point_frames_ceil;
  1042. else
  1043. frames_to_insert = mid_point_frames_floor;
  1044. /* Either we've calculated the number of frames to insert,
  1045. * or we need to insert min duration frames
  1046. */
  1047. if (frames_to_insert > 0)
  1048. inserted_frame_duration_in_us = last_render_time_in_us /
  1049. frames_to_insert;
  1050. if (inserted_frame_duration_in_us <
  1051. state->time.min_render_time_in_us)
  1052. inserted_frame_duration_in_us =
  1053. state->time.min_render_time_in_us;
  1054. /* Cache the calculated variables */
  1055. state->btr.inserted_frame_duration_in_us =
  1056. inserted_frame_duration_in_us;
  1057. state->btr.frames_to_insert = frames_to_insert;
  1058. state->btr.frame_counter = frames_to_insert;
  1059. }
  1060. }
  1061. static void apply_fixed_refresh(struct core_freesync *core_freesync,
  1062. const struct dc_stream *stream, unsigned int map_index)
  1063. {
  1064. unsigned int vmin = 0, vmax = 0;
  1065. struct freesync_state *state = &core_freesync->map[map_index].state;
  1066. if (!state->fixed_refresh.program_fixed_refresh)
  1067. return;
  1068. state->fixed_refresh.program_fixed_refresh = false;
  1069. /* Program Fixed Refresh */
  1070. /* Fixed Refresh set to "not active" so disengage */
  1071. if (!state->fixed_refresh.fixed_refresh_active) {
  1072. set_freesync_on_streams(core_freesync, &stream, 1);
  1073. /* Fixed Refresh set to "active" so engage (fix to max) */
  1074. } else {
  1075. vmin = state->freesync_range.vmin;
  1076. vmax = vmin;
  1077. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  1078. core_freesync->dc, &stream,
  1079. 1, vmin,
  1080. vmax);
  1081. }
  1082. }
  1083. void mod_freesync_pre_update_plane_addresses(struct mod_freesync *mod_freesync,
  1084. const struct dc_stream **streams, int num_streams,
  1085. unsigned int curr_time_stamp_in_us)
  1086. {
  1087. unsigned int stream_index, map_index, last_render_time_in_us = 0;
  1088. struct core_freesync *core_freesync = NULL;
  1089. if (mod_freesync == NULL)
  1090. return;
  1091. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  1092. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  1093. map_index = map_index_from_stream(core_freesync,
  1094. streams[stream_index]);
  1095. if (core_freesync->map[map_index].caps->supported) {
  1096. last_render_time_in_us = curr_time_stamp_in_us -
  1097. core_freesync->map[map_index].state.time.
  1098. prev_time_stamp_in_us;
  1099. /* Add the timestamps to the cache and determine
  1100. * whether BTR program is required
  1101. */
  1102. update_timestamps(core_freesync, streams[stream_index],
  1103. map_index, last_render_time_in_us);
  1104. if (core_freesync->map[map_index].state.fullscreen &&
  1105. core_freesync->map[map_index].user_enable.
  1106. enable_for_gaming) {
  1107. if (core_freesync->map[map_index].caps->btr_supported) {
  1108. apply_below_the_range(core_freesync,
  1109. streams[stream_index], map_index,
  1110. last_render_time_in_us);
  1111. } else {
  1112. apply_fixed_refresh(core_freesync,
  1113. streams[stream_index], map_index);
  1114. }
  1115. }
  1116. core_freesync->map[map_index].state.time.
  1117. prev_time_stamp_in_us = curr_time_stamp_in_us;
  1118. }
  1119. }
  1120. }