freesync.c 42 KB

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