freesync.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. struct gradual_static_ramp {
  42. bool ramp_is_active;
  43. bool ramp_direction_is_up;
  44. unsigned int ramp_current_frame_duration_in_ns;
  45. };
  46. struct time_cache {
  47. /* video (48Hz feature) related */
  48. unsigned int update_duration_in_ns;
  49. /* BTR/fixed refresh related */
  50. unsigned int prev_time_stamp_in_us;
  51. unsigned int min_render_time_in_us;
  52. unsigned int max_render_time_in_us;
  53. unsigned int render_times_index;
  54. unsigned int render_times[RENDER_TIMES_MAX_COUNT];
  55. };
  56. struct below_the_range {
  57. bool btr_active;
  58. bool program_btr;
  59. unsigned int mid_point_in_us;
  60. unsigned int inserted_frame_duration_in_us;
  61. unsigned int frames_to_insert;
  62. unsigned int frame_counter;
  63. };
  64. struct fixed_refresh {
  65. bool fixed_active;
  66. bool program_fixed;
  67. unsigned int frame_counter;
  68. };
  69. struct freesync_range {
  70. unsigned int min_refresh;
  71. unsigned int max_frame_duration;
  72. unsigned int vmax;
  73. unsigned int max_refresh;
  74. unsigned int min_frame_duration;
  75. unsigned int vmin;
  76. };
  77. struct freesync_state {
  78. bool fullscreen;
  79. bool static_screen;
  80. bool video;
  81. unsigned int nominal_refresh_rate_in_micro_hz;
  82. bool windowed_fullscreen;
  83. struct time_cache time;
  84. struct gradual_static_ramp static_ramp;
  85. struct below_the_range btr;
  86. struct fixed_refresh fixed_refresh;
  87. struct freesync_range freesync_range;
  88. };
  89. struct freesync_entity {
  90. const struct dc_stream *stream;
  91. struct mod_freesync_caps *caps;
  92. struct freesync_state state;
  93. struct mod_freesync_user_enable user_enable;
  94. };
  95. struct freesync_registry_options {
  96. unsigned int min_refresh_from_edid;
  97. };
  98. struct core_freesync {
  99. struct mod_freesync public;
  100. struct dc *dc;
  101. struct freesync_entity *map;
  102. int num_entities;
  103. struct freesync_registry_options opts;
  104. };
  105. #define MOD_FREESYNC_TO_CORE(mod_freesync)\
  106. container_of(mod_freesync, struct core_freesync, public)
  107. static bool check_dc_support(const struct dc *dc)
  108. {
  109. if (dc->stream_funcs.adjust_vmin_vmax == NULL)
  110. return false;
  111. return true;
  112. }
  113. struct mod_freesync *mod_freesync_create(struct dc *dc)
  114. {
  115. struct core_freesync *core_freesync =
  116. dm_alloc(sizeof(struct core_freesync));
  117. struct core_dc *core_dc = DC_TO_CORE(dc);
  118. struct persistent_data_flag flag;
  119. int i, data = 0;
  120. if (core_freesync == NULL)
  121. goto fail_alloc_context;
  122. core_freesync->map = dm_alloc(sizeof(struct freesync_entity) *
  123. MOD_FREESYNC_MAX_CONCURRENT_STREAMS);
  124. if (core_freesync->map == NULL)
  125. goto fail_alloc_map;
  126. for (i = 0; i < MOD_FREESYNC_MAX_CONCURRENT_STREAMS; i++)
  127. core_freesync->map[i].stream = NULL;
  128. core_freesync->num_entities = 0;
  129. if (dc == NULL)
  130. goto fail_construct;
  131. core_freesync->dc = dc;
  132. if (!check_dc_support(dc))
  133. goto fail_construct;
  134. /* Create initial module folder in registry for freesync enable data */
  135. flag.save_per_edid = true;
  136. flag.save_per_link = false;
  137. dm_write_persistent_data(core_dc->ctx, NULL, FREESYNC_REGISTRY_NAME,
  138. NULL, NULL, 0, &flag);
  139. flag.save_per_edid = false;
  140. flag.save_per_link = false;
  141. if (dm_read_persistent_data(core_dc->ctx, NULL, NULL,
  142. "DalDrrSupport", &data, sizeof(data), &flag)) {
  143. core_freesync->opts.min_refresh_from_edid = data;
  144. }
  145. return &core_freesync->public;
  146. fail_construct:
  147. dm_free(core_freesync->map);
  148. fail_alloc_map:
  149. dm_free(core_freesync);
  150. fail_alloc_context:
  151. return NULL;
  152. }
  153. void mod_freesync_destroy(struct mod_freesync *mod_freesync)
  154. {
  155. if (mod_freesync != NULL) {
  156. int i;
  157. struct core_freesync *core_freesync =
  158. MOD_FREESYNC_TO_CORE(mod_freesync);
  159. for (i = 0; i < core_freesync->num_entities; i++)
  160. if (core_freesync->map[i].stream)
  161. dc_stream_release(core_freesync->map[i].stream);
  162. dm_free(core_freesync->map);
  163. dm_free(core_freesync);
  164. }
  165. }
  166. /* Given a specific dc_stream* this function finds its equivalent
  167. * on the core_freesync->map and returns the corresponding index
  168. */
  169. static unsigned int map_index_from_stream(struct core_freesync *core_freesync,
  170. const struct dc_stream *stream)
  171. {
  172. unsigned int index = 0;
  173. for (index = 0; index < core_freesync->num_entities; index++) {
  174. if (core_freesync->map[index].stream == stream) {
  175. return index;
  176. }
  177. }
  178. /* Could not find stream requested */
  179. ASSERT(false);
  180. return index;
  181. }
  182. bool mod_freesync_add_stream(struct mod_freesync *mod_freesync,
  183. const struct dc_stream *stream, struct mod_freesync_caps *caps)
  184. {
  185. struct core_stream *core_stream = NULL;
  186. struct core_dc *core_dc = NULL;
  187. struct core_freesync *core_freesync = NULL;
  188. int persistent_freesync_enable = 0;
  189. struct persistent_data_flag flag;
  190. unsigned int nom_refresh_rate_micro_hz;
  191. unsigned long long temp;
  192. if (mod_freesync == NULL)
  193. return false;
  194. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  195. core_stream = DC_STREAM_TO_CORE(stream);
  196. core_dc = DC_TO_CORE(core_freesync->dc);
  197. flag.save_per_edid = true;
  198. flag.save_per_link = false;
  199. if (core_freesync->num_entities < MOD_FREESYNC_MAX_CONCURRENT_STREAMS) {
  200. dc_stream_retain(stream);
  201. temp = stream->timing.pix_clk_khz;
  202. temp *= 1000ULL * 1000ULL * 1000ULL;
  203. temp = div_u64(temp, stream->timing.h_total);
  204. temp = div_u64(temp, stream->timing.v_total);
  205. nom_refresh_rate_micro_hz = (unsigned int) temp;
  206. if (core_freesync->opts.min_refresh_from_edid != 0 &&
  207. dc_is_embedded_signal(stream->sink->sink_signal)
  208. && (nom_refresh_rate_micro_hz -
  209. core_freesync->opts.min_refresh_from_edid *
  210. 1000000) >= 10000000) {
  211. caps->supported = true;
  212. caps->min_refresh_in_micro_hz =
  213. core_freesync->opts.min_refresh_from_edid *
  214. 1000000;
  215. caps->max_refresh_in_micro_hz =
  216. nom_refresh_rate_micro_hz;
  217. }
  218. core_freesync->map[core_freesync->num_entities].stream = stream;
  219. core_freesync->map[core_freesync->num_entities].caps = caps;
  220. core_freesync->map[core_freesync->num_entities].state.
  221. fullscreen = false;
  222. core_freesync->map[core_freesync->num_entities].state.
  223. static_screen = false;
  224. core_freesync->map[core_freesync->num_entities].state.
  225. video = false;
  226. core_freesync->map[core_freesync->num_entities].state.time.
  227. update_duration_in_ns = 0;
  228. core_freesync->map[core_freesync->num_entities].state.
  229. static_ramp.ramp_is_active = false;
  230. /* get persistent data from registry */
  231. if (dm_read_persistent_data(core_dc->ctx, stream->sink,
  232. FREESYNC_REGISTRY_NAME,
  233. "userenable", &persistent_freesync_enable,
  234. sizeof(int), &flag)) {
  235. core_freesync->map[core_freesync->num_entities].user_enable.
  236. enable_for_gaming =
  237. (persistent_freesync_enable & 1) ? true : false;
  238. core_freesync->map[core_freesync->num_entities].user_enable.
  239. enable_for_static =
  240. (persistent_freesync_enable & 2) ? true : false;
  241. core_freesync->map[core_freesync->num_entities].user_enable.
  242. enable_for_video =
  243. (persistent_freesync_enable & 4) ? true : false;
  244. } else {
  245. core_freesync->map[core_freesync->num_entities].user_enable.
  246. enable_for_gaming = false;
  247. core_freesync->map[core_freesync->num_entities].user_enable.
  248. enable_for_static = false;
  249. core_freesync->map[core_freesync->num_entities].user_enable.
  250. enable_for_video = false;
  251. }
  252. if (caps->supported &&
  253. nom_refresh_rate_micro_hz >= caps->min_refresh_in_micro_hz &&
  254. nom_refresh_rate_micro_hz <= caps->max_refresh_in_micro_hz)
  255. core_stream->public.ignore_msa_timing_param = 1;
  256. core_freesync->num_entities++;
  257. return true;
  258. }
  259. return false;
  260. }
  261. bool mod_freesync_remove_stream(struct mod_freesync *mod_freesync,
  262. const struct dc_stream *stream)
  263. {
  264. int i = 0;
  265. struct core_freesync *core_freesync = NULL;
  266. unsigned int index = 0;
  267. if (mod_freesync == NULL)
  268. return false;
  269. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  270. index = map_index_from_stream(core_freesync, stream);
  271. dc_stream_release(core_freesync->map[index].stream);
  272. core_freesync->map[index].stream = NULL;
  273. /* To remove this entity, shift everything after down */
  274. for (i = index; i < core_freesync->num_entities - 1; i++)
  275. core_freesync->map[i] = core_freesync->map[i + 1];
  276. core_freesync->num_entities--;
  277. return true;
  278. }
  279. static void update_stream_freesync_context(struct core_freesync *core_freesync,
  280. const struct dc_stream *stream)
  281. {
  282. unsigned int index;
  283. struct freesync_context *ctx;
  284. struct core_stream *core_stream;
  285. core_stream = DC_STREAM_TO_CORE(stream);
  286. ctx = &core_stream->public.freesync_ctx;
  287. index = map_index_from_stream(core_freesync, stream);
  288. ctx->supported = core_freesync->map[index].caps->supported;
  289. ctx->enabled = (core_freesync->map[index].user_enable.enable_for_gaming ||
  290. core_freesync->map[index].user_enable.enable_for_video ||
  291. core_freesync->map[index].user_enable.enable_for_static);
  292. ctx->active = (core_freesync->map[index].state.fullscreen ||
  293. core_freesync->map[index].state.video ||
  294. core_freesync->map[index].state.static_ramp.ramp_is_active);
  295. ctx->min_refresh_in_micro_hz =
  296. core_freesync->map[index].caps->min_refresh_in_micro_hz;
  297. ctx->nominal_refresh_in_micro_hz = core_freesync->
  298. map[index].state.nominal_refresh_rate_in_micro_hz;
  299. }
  300. static void update_stream(struct core_freesync *core_freesync,
  301. const struct dc_stream *stream)
  302. {
  303. struct core_stream *core_stream = DC_STREAM_TO_CORE(stream);
  304. unsigned int index = map_index_from_stream(core_freesync, stream);
  305. if (core_freesync->map[index].caps->supported) {
  306. core_stream->public.ignore_msa_timing_param = 1;
  307. update_stream_freesync_context(core_freesync, stream);
  308. }
  309. }
  310. static void calc_freesync_range(struct core_freesync *core_freesync,
  311. const struct dc_stream *stream,
  312. struct freesync_state *state,
  313. unsigned int min_refresh_in_uhz,
  314. unsigned int max_refresh_in_uhz)
  315. {
  316. unsigned int min_frame_duration_in_ns = 0, max_frame_duration_in_ns = 0;
  317. unsigned int index = map_index_from_stream(core_freesync, stream);
  318. uint32_t vtotal = stream->timing.v_total;
  319. if ((min_refresh_in_uhz == 0) || (max_refresh_in_uhz == 0)) {
  320. state->freesync_range.min_refresh =
  321. state->nominal_refresh_rate_in_micro_hz;
  322. state->freesync_range.max_refresh =
  323. state->nominal_refresh_rate_in_micro_hz;
  324. state->freesync_range.max_frame_duration = 0;
  325. state->freesync_range.min_frame_duration = 0;
  326. state->freesync_range.vmax = vtotal;
  327. state->freesync_range.vmin = vtotal;
  328. return;
  329. }
  330. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  331. (1000000000ULL * 1000000),
  332. max_refresh_in_uhz)));
  333. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  334. (1000000000ULL * 1000000),
  335. min_refresh_in_uhz)));
  336. state->freesync_range.min_refresh = min_refresh_in_uhz;
  337. state->freesync_range.max_refresh = max_refresh_in_uhz;
  338. state->freesync_range.max_frame_duration = max_frame_duration_in_ns;
  339. state->freesync_range.min_frame_duration = min_frame_duration_in_ns;
  340. state->freesync_range.vmax = div64_u64(div64_u64(((unsigned long long)(
  341. max_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  342. stream->timing.h_total), 1000000);
  343. state->freesync_range.vmin = div64_u64(div64_u64(((unsigned long long)(
  344. min_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  345. stream->timing.h_total), 1000000);
  346. /* vmin/vmax cannot be less than vtotal */
  347. if (state->freesync_range.vmin < vtotal) {
  348. /* Error of 1 is permissible */
  349. ASSERT((state->freesync_range.vmin + 1) >= vtotal);
  350. state->freesync_range.vmin = vtotal;
  351. }
  352. if (state->freesync_range.vmax < vtotal) {
  353. /* Error of 1 is permissible */
  354. ASSERT((state->freesync_range.vmax + 1) >= vtotal);
  355. state->freesync_range.vmax = vtotal;
  356. }
  357. /* Determine whether BTR can be supported */
  358. if (max_frame_duration_in_ns >=
  359. 2 * min_frame_duration_in_ns)
  360. core_freesync->map[index].caps->btr_supported = true;
  361. else
  362. core_freesync->map[index].caps->btr_supported = false;
  363. /* Cache the time variables */
  364. state->time.max_render_time_in_us =
  365. max_frame_duration_in_ns / 1000;
  366. state->time.min_render_time_in_us =
  367. min_frame_duration_in_ns / 1000;
  368. state->btr.mid_point_in_us =
  369. (max_frame_duration_in_ns +
  370. min_frame_duration_in_ns) / 2000;
  371. }
  372. static void calc_v_total_from_duration(const struct dc_stream *stream,
  373. unsigned int duration_in_ns, int *v_total_nominal)
  374. {
  375. *v_total_nominal = div64_u64(div64_u64(((unsigned long long)(
  376. duration_in_ns) * stream->timing.pix_clk_khz),
  377. stream->timing.h_total), 1000000);
  378. }
  379. static void calc_v_total_for_static_ramp(struct core_freesync *core_freesync,
  380. const struct dc_stream *stream,
  381. unsigned int index, int *v_total)
  382. {
  383. unsigned int frame_duration = 0;
  384. struct gradual_static_ramp *static_ramp_variables =
  385. &core_freesync->map[index].state.static_ramp;
  386. /* Calc ratio between new and current frame duration with 3 digit */
  387. unsigned int frame_duration_ratio = div64_u64(1000000,
  388. (1000 + div64_u64(((unsigned long long)(
  389. STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
  390. static_ramp_variables->ramp_current_frame_duration_in_ns),
  391. 1000000000)));
  392. /* Calculate delta between new and current frame duration in ns */
  393. unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
  394. static_ramp_variables->ramp_current_frame_duration_in_ns) *
  395. (1000 - frame_duration_ratio)), 1000);
  396. /* Adjust frame duration delta based on ratio between current and
  397. * standard frame duration (frame duration at 60 Hz refresh rate).
  398. */
  399. unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
  400. frame_duration_delta) * static_ramp_variables->
  401. ramp_current_frame_duration_in_ns), 16666666);
  402. /* Going to a higher refresh rate (lower frame duration) */
  403. if (static_ramp_variables->ramp_direction_is_up) {
  404. /* reduce frame duration */
  405. static_ramp_variables->ramp_current_frame_duration_in_ns -=
  406. ramp_rate_interpolated;
  407. /* min frame duration */
  408. frame_duration = ((unsigned int) (div64_u64(
  409. (1000000000ULL * 1000000),
  410. core_freesync->map[index].state.
  411. nominal_refresh_rate_in_micro_hz)));
  412. /* adjust for frame duration below min */
  413. if (static_ramp_variables->ramp_current_frame_duration_in_ns <=
  414. frame_duration) {
  415. static_ramp_variables->ramp_is_active = false;
  416. static_ramp_variables->
  417. ramp_current_frame_duration_in_ns =
  418. frame_duration;
  419. }
  420. /* Going to a lower refresh rate (larger frame duration) */
  421. } else {
  422. /* increase frame duration */
  423. static_ramp_variables->ramp_current_frame_duration_in_ns +=
  424. ramp_rate_interpolated;
  425. /* max frame duration */
  426. frame_duration = ((unsigned int) (div64_u64(
  427. (1000000000ULL * 1000000),
  428. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  429. /* adjust for frame duration above max */
  430. if (static_ramp_variables->ramp_current_frame_duration_in_ns >=
  431. frame_duration) {
  432. static_ramp_variables->ramp_is_active = false;
  433. static_ramp_variables->
  434. ramp_current_frame_duration_in_ns =
  435. frame_duration;
  436. }
  437. }
  438. calc_v_total_from_duration(stream, static_ramp_variables->
  439. ramp_current_frame_duration_in_ns, v_total);
  440. }
  441. static void reset_freesync_state_variables(struct freesync_state* state)
  442. {
  443. state->static_ramp.ramp_is_active = false;
  444. if (state->nominal_refresh_rate_in_micro_hz)
  445. state->static_ramp.ramp_current_frame_duration_in_ns =
  446. ((unsigned int) (div64_u64(
  447. (1000000000ULL * 1000000),
  448. state->nominal_refresh_rate_in_micro_hz)));
  449. state->btr.btr_active = false;
  450. state->btr.frame_counter = 0;
  451. state->btr.frames_to_insert = 0;
  452. state->btr.inserted_frame_duration_in_us = 0;
  453. state->btr.program_btr = false;
  454. state->fixed_refresh.fixed_active = false;
  455. state->fixed_refresh.program_fixed = false;
  456. }
  457. /*
  458. * Sets freesync mode on a stream depending on current freesync state.
  459. */
  460. static bool set_freesync_on_streams(struct core_freesync *core_freesync,
  461. const struct dc_stream **streams, int num_streams)
  462. {
  463. int v_total_nominal = 0, v_total_min = 0, v_total_max = 0;
  464. unsigned int stream_idx, map_index = 0;
  465. struct freesync_state *state;
  466. if (num_streams == 0 || streams == NULL || num_streams > 1)
  467. return false;
  468. for (stream_idx = 0; stream_idx < num_streams; stream_idx++) {
  469. map_index = map_index_from_stream(core_freesync,
  470. streams[stream_idx]);
  471. state = &core_freesync->map[map_index].state;
  472. if (core_freesync->map[map_index].caps->supported) {
  473. /* Fullscreen has the topmost priority. If the
  474. * fullscreen bit is set, we are in a fullscreen
  475. * application where it should not matter if it is
  476. * static screen. We should not check the static_screen
  477. * or video bit.
  478. *
  479. * Special cases of fullscreen include btr and fixed
  480. * refresh. We program btr on every flip and involves
  481. * programming full range right before the last inserted frame.
  482. * However, we do not want to program the full freesync range
  483. * when fixed refresh is active, because we only program
  484. * that logic once and this will override it.
  485. */
  486. if (core_freesync->map[map_index].user_enable.
  487. enable_for_gaming == true &&
  488. state->fullscreen == true &&
  489. state->fixed_refresh.fixed_active == false) {
  490. /* Enable freesync */
  491. v_total_min = state->freesync_range.vmin;
  492. v_total_max = state->freesync_range.vmax;
  493. /* Update the freesync context for the stream */
  494. update_stream_freesync_context(core_freesync,
  495. streams[stream_idx]);
  496. core_freesync->dc->stream_funcs.
  497. adjust_vmin_vmax(core_freesync->dc, streams,
  498. num_streams, v_total_min,
  499. v_total_max);
  500. return true;
  501. } else if (core_freesync->map[map_index].user_enable.
  502. enable_for_video && state->video == true) {
  503. /* Enable 48Hz feature */
  504. calc_v_total_from_duration(streams[stream_idx],
  505. state->time.update_duration_in_ns,
  506. &v_total_nominal);
  507. /* Program only if v_total_nominal is in range*/
  508. if (v_total_nominal >=
  509. streams[stream_idx]->timing.v_total) {
  510. /* Update the freesync context for
  511. * the stream
  512. */
  513. update_stream_freesync_context(
  514. core_freesync,
  515. streams[stream_idx]);
  516. core_freesync->dc->stream_funcs.
  517. adjust_vmin_vmax(
  518. core_freesync->dc, streams,
  519. num_streams, v_total_nominal,
  520. v_total_nominal);
  521. }
  522. return true;
  523. } else {
  524. /* Disable freesync */
  525. v_total_nominal = streams[stream_idx]->
  526. timing.v_total;
  527. /* Update the freesync context for
  528. * the stream
  529. */
  530. update_stream_freesync_context(
  531. core_freesync,
  532. streams[stream_idx]);
  533. core_freesync->dc->stream_funcs.
  534. adjust_vmin_vmax(
  535. core_freesync->dc, streams,
  536. num_streams, v_total_nominal,
  537. v_total_nominal);
  538. /* Reset the cached variables */
  539. reset_freesync_state_variables(state);
  540. return true;
  541. }
  542. } else {
  543. /* Disable freesync */
  544. v_total_nominal = streams[stream_idx]->
  545. timing.v_total;
  546. /*
  547. * we have to reset drr always even sink does
  548. * not support freesync because a former stream has
  549. * be programmed
  550. */
  551. core_freesync->dc->stream_funcs.
  552. adjust_vmin_vmax(
  553. core_freesync->dc, streams,
  554. num_streams, v_total_nominal,
  555. v_total_nominal);
  556. /* Reset the cached variables */
  557. reset_freesync_state_variables(state);
  558. }
  559. }
  560. return false;
  561. }
  562. static void set_static_ramp_variables(struct core_freesync *core_freesync,
  563. unsigned int index, bool enable_static_screen)
  564. {
  565. unsigned int frame_duration = 0;
  566. unsigned int nominal_refresh_rate = core_freesync->map[index].state.
  567. nominal_refresh_rate_in_micro_hz;
  568. unsigned int min_refresh_rate= core_freesync->map[index].caps->
  569. min_refresh_in_micro_hz;
  570. struct gradual_static_ramp *static_ramp_variables =
  571. &core_freesync->map[index].state.static_ramp;
  572. /* If we are ENABLING static screen, refresh rate should go DOWN.
  573. * If we are DISABLING static screen, refresh rate should go UP.
  574. */
  575. if (enable_static_screen)
  576. static_ramp_variables->ramp_direction_is_up = false;
  577. else
  578. static_ramp_variables->ramp_direction_is_up = true;
  579. /* If ramp is not active, set initial frame duration depending on
  580. * whether we are enabling/disabling static screen mode. If the ramp is
  581. * already active, ramp should continue in the opposite direction
  582. * starting with the current frame duration
  583. */
  584. if (!static_ramp_variables->ramp_is_active) {
  585. if (enable_static_screen == true) {
  586. /* Going to lower refresh rate, so start from max
  587. * refresh rate (min frame duration)
  588. */
  589. frame_duration = ((unsigned int) (div64_u64(
  590. (1000000000ULL * 1000000),
  591. nominal_refresh_rate)));
  592. } else {
  593. /* Going to higher refresh rate, so start from min
  594. * refresh rate (max frame duration)
  595. */
  596. frame_duration = ((unsigned int) (div64_u64(
  597. (1000000000ULL * 1000000),
  598. min_refresh_rate)));
  599. }
  600. static_ramp_variables->
  601. ramp_current_frame_duration_in_ns = frame_duration;
  602. static_ramp_variables->ramp_is_active = true;
  603. }
  604. }
  605. void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
  606. const struct dc_stream **streams, int num_streams)
  607. {
  608. unsigned int index, v_total, inserted_frame_v_total = 0;
  609. unsigned int min_frame_duration_in_ns, vmax, vmin = 0;
  610. struct freesync_state *state;
  611. struct core_freesync *core_freesync = NULL;
  612. struct dc_static_screen_events triggers = {0};
  613. if (mod_freesync == NULL)
  614. return;
  615. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  616. if (core_freesync->num_entities == 0)
  617. return;
  618. index = map_index_from_stream(core_freesync,
  619. streams[0]);
  620. if (core_freesync->map[index].caps->supported == false)
  621. return;
  622. state = &core_freesync->map[index].state;
  623. /* Below the Range Logic */
  624. /* Only execute if in fullscreen mode */
  625. if (state->fullscreen == true &&
  626. core_freesync->map[index].user_enable.enable_for_gaming &&
  627. core_freesync->map[index].caps->btr_supported &&
  628. state->btr.btr_active) {
  629. /* TODO: pass in flag for Pre-DCE12 ASIC
  630. * in order for frame variable duration to take affect,
  631. * it needs to be done one VSYNC early, which is at
  632. * frameCounter == 1.
  633. * For DCE12 and newer updates to V_TOTAL_MIN/MAX
  634. * will take affect on current frame
  635. */
  636. if (state->btr.frames_to_insert == state->btr.frame_counter) {
  637. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  638. (1000000000ULL * 1000000),
  639. state->nominal_refresh_rate_in_micro_hz)));
  640. vmin = state->freesync_range.vmin;
  641. inserted_frame_v_total = vmin;
  642. if (min_frame_duration_in_ns / 1000)
  643. inserted_frame_v_total =
  644. state->btr.inserted_frame_duration_in_us *
  645. vmin / (min_frame_duration_in_ns / 1000);
  646. /* Set length of inserted frames as v_total_max*/
  647. vmax = inserted_frame_v_total;
  648. vmin = inserted_frame_v_total;
  649. /* Program V_TOTAL */
  650. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  651. core_freesync->dc, streams,
  652. num_streams, vmin, vmax);
  653. }
  654. if (state->btr.frame_counter > 0)
  655. state->btr.frame_counter--;
  656. /* Restore FreeSync */
  657. if (state->btr.frame_counter == 0)
  658. set_freesync_on_streams(core_freesync, streams, num_streams);
  659. }
  660. /* If in fullscreen freesync mode or in video, do not program
  661. * static screen ramp values
  662. */
  663. if (state->fullscreen == true || state->video == true) {
  664. state->static_ramp.ramp_is_active = false;
  665. return;
  666. }
  667. /* Gradual Static Screen Ramping Logic */
  668. /* Execute if ramp is active and user enabled freesync static screen*/
  669. if (state->static_ramp.ramp_is_active &&
  670. core_freesync->map[index].user_enable.enable_for_static) {
  671. calc_v_total_for_static_ramp(core_freesync, streams[0],
  672. index, &v_total);
  673. /* Update the freesync context for the stream */
  674. update_stream_freesync_context(core_freesync, streams[0]);
  675. /* Program static screen ramp values */
  676. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  677. core_freesync->dc, streams,
  678. num_streams, v_total,
  679. v_total);
  680. triggers.overlay_update = true;
  681. triggers.surface_update = true;
  682. core_freesync->dc->stream_funcs.set_static_screen_events(
  683. core_freesync->dc, streams, num_streams,
  684. &triggers);
  685. }
  686. }
  687. void mod_freesync_update_state(struct mod_freesync *mod_freesync,
  688. const struct dc_stream **streams, int num_streams,
  689. struct mod_freesync_params *freesync_params)
  690. {
  691. bool freesync_program_required = false;
  692. unsigned int stream_index;
  693. struct freesync_state *state;
  694. struct core_freesync *core_freesync = NULL;
  695. struct dc_static_screen_events triggers = {0};
  696. if (mod_freesync == NULL)
  697. return;
  698. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  699. if (core_freesync->num_entities == 0)
  700. return;
  701. for(stream_index = 0; stream_index < num_streams; stream_index++) {
  702. unsigned int map_index = map_index_from_stream(core_freesync,
  703. streams[stream_index]);
  704. state = &core_freesync->map[map_index].state;
  705. switch (freesync_params->state){
  706. case FREESYNC_STATE_FULLSCREEN:
  707. state->fullscreen = freesync_params->enable;
  708. freesync_program_required = true;
  709. state->windowed_fullscreen =
  710. freesync_params->windowed_fullscreen;
  711. break;
  712. case FREESYNC_STATE_STATIC_SCREEN:
  713. /* Static screen ramp is only enabled for embedded
  714. * panels. Also change core variables only if there
  715. * is a change.
  716. */
  717. if ((dc_is_embedded_signal(
  718. streams[stream_index]->sink->sink_signal) ||
  719. core_freesync->map[map_index].caps->
  720. no_static_for_external_dp == false) &&
  721. state->static_screen !=
  722. freesync_params->enable) {
  723. /* Change the state flag */
  724. state->static_screen = freesync_params->enable;
  725. /* Change static screen ramp variables */
  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. }