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