freesync.c 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  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. /* Temp, keep btr disabled */
  364. core_freesync->map[index].caps->btr_supported = false;
  365. /* Cache the time variables */
  366. state->time.max_render_time_in_us =
  367. max_frame_duration_in_ns / 1000;
  368. state->time.min_render_time_in_us =
  369. min_frame_duration_in_ns / 1000;
  370. state->btr.mid_point_in_us =
  371. (max_frame_duration_in_ns +
  372. min_frame_duration_in_ns) / 2000;
  373. }
  374. static void calc_v_total_from_duration(const struct dc_stream *stream,
  375. unsigned int duration_in_ns, int *v_total_nominal)
  376. {
  377. *v_total_nominal = div64_u64(div64_u64(((unsigned long long)(
  378. duration_in_ns) * stream->timing.pix_clk_khz),
  379. stream->timing.h_total), 1000000);
  380. }
  381. static void calc_v_total_for_static_ramp(struct core_freesync *core_freesync,
  382. const struct dc_stream *stream,
  383. unsigned int index, int *v_total)
  384. {
  385. unsigned int frame_duration = 0;
  386. struct gradual_static_ramp *static_ramp_variables =
  387. &core_freesync->map[index].state.static_ramp;
  388. /* Calc ratio between new and current frame duration with 3 digit */
  389. unsigned int frame_duration_ratio = div64_u64(1000000,
  390. (1000 + div64_u64(((unsigned long long)(
  391. STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
  392. static_ramp_variables->ramp_current_frame_duration_in_ns),
  393. 1000000000)));
  394. /* Calculate delta between new and current frame duration in ns */
  395. unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
  396. static_ramp_variables->ramp_current_frame_duration_in_ns) *
  397. (1000 - frame_duration_ratio)), 1000);
  398. /* Adjust frame duration delta based on ratio between current and
  399. * standard frame duration (frame duration at 60 Hz refresh rate).
  400. */
  401. unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
  402. frame_duration_delta) * static_ramp_variables->
  403. ramp_current_frame_duration_in_ns), 16666666);
  404. /* Going to a higher refresh rate (lower frame duration) */
  405. if (static_ramp_variables->ramp_direction_is_up) {
  406. /* reduce frame duration */
  407. static_ramp_variables->ramp_current_frame_duration_in_ns -=
  408. ramp_rate_interpolated;
  409. /* min frame duration */
  410. frame_duration = ((unsigned int) (div64_u64(
  411. (1000000000ULL * 1000000),
  412. core_freesync->map[index].state.
  413. nominal_refresh_rate_in_micro_hz)));
  414. /* adjust for frame duration below min */
  415. if (static_ramp_variables->ramp_current_frame_duration_in_ns <=
  416. frame_duration) {
  417. static_ramp_variables->ramp_is_active = false;
  418. static_ramp_variables->
  419. ramp_current_frame_duration_in_ns =
  420. frame_duration;
  421. }
  422. /* Going to a lower refresh rate (larger frame duration) */
  423. } else {
  424. /* increase frame duration */
  425. static_ramp_variables->ramp_current_frame_duration_in_ns +=
  426. ramp_rate_interpolated;
  427. /* max frame duration */
  428. frame_duration = ((unsigned int) (div64_u64(
  429. (1000000000ULL * 1000000),
  430. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  431. /* adjust for frame duration above max */
  432. if (static_ramp_variables->ramp_current_frame_duration_in_ns >=
  433. frame_duration) {
  434. static_ramp_variables->ramp_is_active = false;
  435. static_ramp_variables->
  436. ramp_current_frame_duration_in_ns =
  437. frame_duration;
  438. }
  439. }
  440. calc_v_total_from_duration(stream, static_ramp_variables->
  441. ramp_current_frame_duration_in_ns, v_total);
  442. }
  443. static void reset_freesync_state_variables(struct freesync_state* state)
  444. {
  445. state->static_ramp.ramp_is_active = false;
  446. if (state->nominal_refresh_rate_in_micro_hz)
  447. state->static_ramp.ramp_current_frame_duration_in_ns =
  448. ((unsigned int) (div64_u64(
  449. (1000000000ULL * 1000000),
  450. state->nominal_refresh_rate_in_micro_hz)));
  451. state->btr.btr_active = false;
  452. state->btr.frame_counter = 0;
  453. state->btr.frames_to_insert = 0;
  454. state->btr.inserted_frame_duration_in_us = 0;
  455. state->btr.program_btr = false;
  456. state->fixed_refresh.fixed_active = false;
  457. state->fixed_refresh.program_fixed = false;
  458. }
  459. /*
  460. * Sets freesync mode on a stream depending on current freesync state.
  461. */
  462. static bool set_freesync_on_streams(struct core_freesync *core_freesync,
  463. const struct dc_stream **streams, int num_streams)
  464. {
  465. int v_total_nominal = 0, v_total_min = 0, v_total_max = 0;
  466. unsigned int stream_idx, map_index = 0;
  467. struct freesync_state *state;
  468. if (num_streams == 0 || streams == NULL || num_streams > 1)
  469. return false;
  470. for (stream_idx = 0; stream_idx < num_streams; stream_idx++) {
  471. map_index = map_index_from_stream(core_freesync,
  472. streams[stream_idx]);
  473. state = &core_freesync->map[map_index].state;
  474. if (core_freesync->map[map_index].caps->supported) {
  475. /* Fullscreen has the topmost priority. If the
  476. * fullscreen bit is set, we are in a fullscreen
  477. * application where it should not matter if it is
  478. * static screen. We should not check the static_screen
  479. * or video bit.
  480. *
  481. * Special cases of fullscreen include btr and fixed
  482. * refresh. We program btr on every flip and involves
  483. * programming full range right before the last inserted frame.
  484. * However, we do not want to program the full freesync range
  485. * when fixed refresh is active, because we only program
  486. * that logic once and this will override it.
  487. */
  488. if (core_freesync->map[map_index].user_enable.
  489. enable_for_gaming == true &&
  490. state->fullscreen == true &&
  491. state->fixed_refresh.fixed_active == false) {
  492. /* Enable freesync */
  493. v_total_min = state->freesync_range.vmin;
  494. v_total_max = state->freesync_range.vmax;
  495. /* Update the freesync context for the stream */
  496. update_stream_freesync_context(core_freesync,
  497. streams[stream_idx]);
  498. core_freesync->dc->stream_funcs.
  499. adjust_vmin_vmax(core_freesync->dc, streams,
  500. num_streams, v_total_min,
  501. v_total_max);
  502. return true;
  503. } else if (core_freesync->map[map_index].user_enable.
  504. enable_for_video && state->video == true) {
  505. /* Enable 48Hz feature */
  506. calc_v_total_from_duration(streams[stream_idx],
  507. state->time.update_duration_in_ns,
  508. &v_total_nominal);
  509. /* Program only if v_total_nominal is in range*/
  510. if (v_total_nominal >=
  511. streams[stream_idx]->timing.v_total) {
  512. /* Update the freesync context for
  513. * the stream
  514. */
  515. update_stream_freesync_context(
  516. core_freesync,
  517. streams[stream_idx]);
  518. core_freesync->dc->stream_funcs.
  519. adjust_vmin_vmax(
  520. core_freesync->dc, streams,
  521. num_streams, v_total_nominal,
  522. v_total_nominal);
  523. }
  524. return true;
  525. } else {
  526. /* Disable freesync */
  527. v_total_nominal = streams[stream_idx]->
  528. timing.v_total;
  529. /* Update the freesync context for
  530. * the stream
  531. */
  532. update_stream_freesync_context(
  533. core_freesync,
  534. streams[stream_idx]);
  535. core_freesync->dc->stream_funcs.
  536. adjust_vmin_vmax(
  537. core_freesync->dc, streams,
  538. num_streams, v_total_nominal,
  539. v_total_nominal);
  540. /* Reset the cached variables */
  541. reset_freesync_state_variables(state);
  542. return true;
  543. }
  544. } else {
  545. /* Disable freesync */
  546. v_total_nominal = streams[stream_idx]->
  547. timing.v_total;
  548. /*
  549. * we have to reset drr always even sink does
  550. * not support freesync because a former stream has
  551. * be programmed
  552. */
  553. core_freesync->dc->stream_funcs.
  554. adjust_vmin_vmax(
  555. core_freesync->dc, streams,
  556. num_streams, v_total_nominal,
  557. v_total_nominal);
  558. /* Reset the cached variables */
  559. reset_freesync_state_variables(state);
  560. }
  561. }
  562. return false;
  563. }
  564. static void set_static_ramp_variables(struct core_freesync *core_freesync,
  565. unsigned int index, bool enable_static_screen)
  566. {
  567. unsigned int frame_duration = 0;
  568. unsigned int nominal_refresh_rate = core_freesync->map[index].state.
  569. nominal_refresh_rate_in_micro_hz;
  570. unsigned int min_refresh_rate= core_freesync->map[index].caps->
  571. min_refresh_in_micro_hz;
  572. struct gradual_static_ramp *static_ramp_variables =
  573. &core_freesync->map[index].state.static_ramp;
  574. /* If we are ENABLING static screen, refresh rate should go DOWN.
  575. * If we are DISABLING static screen, refresh rate should go UP.
  576. */
  577. if (enable_static_screen)
  578. static_ramp_variables->ramp_direction_is_up = false;
  579. else
  580. static_ramp_variables->ramp_direction_is_up = true;
  581. /* If ramp is not active, set initial frame duration depending on
  582. * whether we are enabling/disabling static screen mode. If the ramp is
  583. * already active, ramp should continue in the opposite direction
  584. * starting with the current frame duration
  585. */
  586. if (!static_ramp_variables->ramp_is_active) {
  587. if (enable_static_screen == true) {
  588. /* Going to lower refresh rate, so start from max
  589. * refresh rate (min frame duration)
  590. */
  591. frame_duration = ((unsigned int) (div64_u64(
  592. (1000000000ULL * 1000000),
  593. nominal_refresh_rate)));
  594. } else {
  595. /* Going to higher refresh rate, so start from min
  596. * refresh rate (max frame duration)
  597. */
  598. frame_duration = ((unsigned int) (div64_u64(
  599. (1000000000ULL * 1000000),
  600. min_refresh_rate)));
  601. }
  602. static_ramp_variables->
  603. ramp_current_frame_duration_in_ns = frame_duration;
  604. static_ramp_variables->ramp_is_active = true;
  605. }
  606. }
  607. void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
  608. const struct dc_stream **streams, int num_streams)
  609. {
  610. unsigned int index, v_total, inserted_frame_v_total = 0;
  611. unsigned int min_frame_duration_in_ns, vmax, vmin = 0;
  612. struct freesync_state *state;
  613. struct core_freesync *core_freesync = NULL;
  614. struct dc_static_screen_events triggers = {0};
  615. if (mod_freesync == NULL)
  616. return;
  617. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  618. if (core_freesync->num_entities == 0)
  619. return;
  620. index = map_index_from_stream(core_freesync,
  621. streams[0]);
  622. if (core_freesync->map[index].caps->supported == false)
  623. return;
  624. state = &core_freesync->map[index].state;
  625. /* Below the Range Logic */
  626. /* Only execute if in fullscreen mode */
  627. if (state->fullscreen == true &&
  628. core_freesync->map[index].user_enable.enable_for_gaming &&
  629. core_freesync->map[index].caps->btr_supported &&
  630. state->btr.btr_active) {
  631. /* TODO: pass in flag for Pre-DCE12 ASIC
  632. * in order for frame variable duration to take affect,
  633. * it needs to be done one VSYNC early, which is at
  634. * frameCounter == 1.
  635. * For DCE12 and newer updates to V_TOTAL_MIN/MAX
  636. * will take affect on current frame
  637. */
  638. if (state->btr.frames_to_insert == state->btr.frame_counter) {
  639. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  640. (1000000000ULL * 1000000),
  641. state->nominal_refresh_rate_in_micro_hz)));
  642. vmin = state->freesync_range.vmin;
  643. inserted_frame_v_total = vmin;
  644. if (min_frame_duration_in_ns / 1000)
  645. inserted_frame_v_total =
  646. state->btr.inserted_frame_duration_in_us *
  647. vmin / (min_frame_duration_in_ns / 1000);
  648. /* Set length of inserted frames as v_total_max*/
  649. vmax = inserted_frame_v_total;
  650. vmin = inserted_frame_v_total;
  651. /* Program V_TOTAL */
  652. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  653. core_freesync->dc, streams,
  654. num_streams, vmin, vmax);
  655. }
  656. if (state->btr.frame_counter > 0)
  657. state->btr.frame_counter--;
  658. /* Restore FreeSync */
  659. if (state->btr.frame_counter == 0)
  660. set_freesync_on_streams(core_freesync, streams, num_streams);
  661. }
  662. /* If in fullscreen freesync mode or in video, do not program
  663. * static screen ramp values
  664. */
  665. if (state->fullscreen == true || state->video == true) {
  666. state->static_ramp.ramp_is_active = false;
  667. return;
  668. }
  669. /* Gradual Static Screen Ramping Logic */
  670. /* Execute if ramp is active and user enabled freesync static screen*/
  671. if (state->static_ramp.ramp_is_active &&
  672. core_freesync->map[index].user_enable.enable_for_static) {
  673. calc_v_total_for_static_ramp(core_freesync, streams[0],
  674. index, &v_total);
  675. /* Update the freesync context for the stream */
  676. update_stream_freesync_context(core_freesync, streams[0]);
  677. /* Program static screen ramp values */
  678. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  679. core_freesync->dc, streams,
  680. num_streams, v_total,
  681. v_total);
  682. triggers.overlay_update = true;
  683. triggers.surface_update = true;
  684. core_freesync->dc->stream_funcs.set_static_screen_events(
  685. core_freesync->dc, streams, num_streams,
  686. &triggers);
  687. }
  688. }
  689. void mod_freesync_update_state(struct mod_freesync *mod_freesync,
  690. const struct dc_stream **streams, int num_streams,
  691. struct mod_freesync_params *freesync_params)
  692. {
  693. bool freesync_program_required = false;
  694. unsigned int stream_index;
  695. struct freesync_state *state;
  696. struct core_freesync *core_freesync = NULL;
  697. struct dc_static_screen_events triggers = {0};
  698. if (mod_freesync == NULL)
  699. return;
  700. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  701. if (core_freesync->num_entities == 0)
  702. return;
  703. for(stream_index = 0; stream_index < num_streams; stream_index++) {
  704. unsigned int map_index = map_index_from_stream(core_freesync,
  705. streams[stream_index]);
  706. state = &core_freesync->map[map_index].state;
  707. switch (freesync_params->state){
  708. case FREESYNC_STATE_FULLSCREEN:
  709. state->fullscreen = freesync_params->enable;
  710. freesync_program_required = true;
  711. state->windowed_fullscreen =
  712. freesync_params->windowed_fullscreen;
  713. break;
  714. case FREESYNC_STATE_STATIC_SCREEN:
  715. /* Static screen ramp is only enabled for embedded
  716. * panels. Also change core variables only if there
  717. * is a change.
  718. */
  719. if (dc_is_embedded_signal(
  720. streams[stream_index]->sink->sink_signal) &&
  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_override_min_max(struct mod_freesync *mod_freesync,
  841. const struct dc_stream *streams,
  842. unsigned int min_refresh,
  843. unsigned int max_refresh,
  844. struct mod_freesync_caps *caps)
  845. {
  846. unsigned int index = 0;
  847. struct core_freesync *core_freesync;
  848. struct freesync_state *state;
  849. if (mod_freesync == NULL)
  850. return false;
  851. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  852. index = map_index_from_stream(core_freesync, streams);
  853. state = &core_freesync->map[index].state;
  854. if (max_refresh == 0)
  855. max_refresh = state->nominal_refresh_rate_in_micro_hz;
  856. if (min_refresh == 0) {
  857. /* Restore defaults */
  858. calc_freesync_range(core_freesync, streams, state,
  859. core_freesync->map[index].caps->
  860. min_refresh_in_micro_hz,
  861. state->nominal_refresh_rate_in_micro_hz);
  862. } else {
  863. calc_freesync_range(core_freesync, streams,
  864. state,
  865. min_refresh,
  866. max_refresh);
  867. /* Program vtotal min/max */
  868. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  869. core_freesync->dc, &streams, 1,
  870. state->freesync_range.vmin,
  871. state->freesync_range.vmax);
  872. }
  873. if (min_refresh != 0 &&
  874. dc_is_embedded_signal(streams->sink->sink_signal) &&
  875. (max_refresh - min_refresh >= 10000000)) {
  876. caps->supported = true;
  877. caps->min_refresh_in_micro_hz = min_refresh;
  878. caps->max_refresh_in_micro_hz = max_refresh;
  879. }
  880. /* Update the stream */
  881. update_stream(core_freesync, streams);
  882. return true;
  883. }
  884. bool mod_freesync_get_min_max(struct mod_freesync *mod_freesync,
  885. const struct dc_stream *stream,
  886. unsigned int *min_refresh,
  887. unsigned int *max_refresh)
  888. {
  889. unsigned int index = 0;
  890. struct core_freesync *core_freesync = NULL;
  891. if (mod_freesync == NULL)
  892. return false;
  893. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  894. index = map_index_from_stream(core_freesync, stream);
  895. *min_refresh =
  896. core_freesync->map[index].state.freesync_range.min_refresh;
  897. *max_refresh =
  898. core_freesync->map[index].state.freesync_range.max_refresh;
  899. return true;
  900. }
  901. bool mod_freesync_get_vmin_vmax(struct mod_freesync *mod_freesync,
  902. const struct dc_stream *stream,
  903. unsigned int *vmin,
  904. unsigned int *vmax)
  905. {
  906. unsigned int index = 0;
  907. struct core_freesync *core_freesync = NULL;
  908. if (mod_freesync == NULL)
  909. return false;
  910. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  911. index = map_index_from_stream(core_freesync, stream);
  912. *vmin =
  913. core_freesync->map[index].state.freesync_range.vmin;
  914. *vmax =
  915. core_freesync->map[index].state.freesync_range.vmax;
  916. return true;
  917. }
  918. bool mod_freesync_get_v_position(struct mod_freesync *mod_freesync,
  919. const struct dc_stream *stream,
  920. unsigned int *nom_v_pos,
  921. unsigned int *v_pos)
  922. {
  923. unsigned int index = 0;
  924. struct core_freesync *core_freesync = NULL;
  925. struct crtc_position position;
  926. if (mod_freesync == NULL)
  927. return false;
  928. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  929. index = map_index_from_stream(core_freesync, stream);
  930. if (core_freesync->dc->stream_funcs.get_crtc_position(
  931. core_freesync->dc, &stream, 1,
  932. &position.vertical_count, &position.nominal_vcount)) {
  933. *nom_v_pos = position.nominal_vcount;
  934. *v_pos = position.vertical_count;
  935. return true;
  936. }
  937. return false;
  938. }
  939. void mod_freesync_notify_mode_change(struct mod_freesync *mod_freesync,
  940. const struct dc_stream **streams, int num_streams)
  941. {
  942. unsigned int stream_index, map_index;
  943. struct freesync_state *state;
  944. struct core_freesync *core_freesync = NULL;
  945. struct dc_static_screen_events triggers = {0};
  946. unsigned long long temp = 0;
  947. if (mod_freesync == NULL)
  948. return;
  949. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  950. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  951. map_index = map_index_from_stream(core_freesync,
  952. streams[stream_index]);
  953. state = &core_freesync->map[map_index].state;
  954. /* Update the field rate for new timing */
  955. temp = streams[stream_index]->timing.pix_clk_khz;
  956. temp *= 1000ULL * 1000ULL * 1000ULL;
  957. temp = div_u64(temp,
  958. streams[stream_index]->timing.h_total);
  959. temp = div_u64(temp,
  960. streams[stream_index]->timing.v_total);
  961. state->nominal_refresh_rate_in_micro_hz =
  962. (unsigned int) temp;
  963. if (core_freesync->map[map_index].caps->supported) {
  964. /* Update the stream */
  965. update_stream(core_freesync, streams[stream_index]);
  966. /* Calculate vmin/vmax and refresh rate for
  967. * current mode
  968. */
  969. calc_freesync_range(core_freesync, *streams, state,
  970. core_freesync->map[map_index].caps->
  971. min_refresh_in_micro_hz,
  972. state->nominal_refresh_rate_in_micro_hz);
  973. /* Update mask */
  974. triggers.overlay_update = true;
  975. triggers.surface_update = true;
  976. core_freesync->dc->stream_funcs.set_static_screen_events(
  977. core_freesync->dc, streams, num_streams,
  978. &triggers);
  979. }
  980. }
  981. /* Program freesync according to current state*/
  982. set_freesync_on_streams(core_freesync, streams, num_streams);
  983. }
  984. /* Add the timestamps to the cache and determine whether BTR programming
  985. * is required, depending on the times calculated
  986. */
  987. static void update_timestamps(struct core_freesync *core_freesync,
  988. const struct dc_stream *stream, unsigned int map_index,
  989. unsigned int last_render_time_in_us)
  990. {
  991. struct freesync_state *state = &core_freesync->map[map_index].state;
  992. state->time.render_times[state->time.render_times_index] =
  993. last_render_time_in_us;
  994. state->time.render_times_index++;
  995. if (state->time.render_times_index >= RENDER_TIMES_MAX_COUNT)
  996. state->time.render_times_index = 0;
  997. if (last_render_time_in_us + BTR_EXIT_MARGIN <
  998. state->time.max_render_time_in_us) {
  999. /* Exit Below the Range */
  1000. if (state->btr.btr_active) {
  1001. state->btr.program_btr = true;
  1002. state->btr.btr_active = false;
  1003. state->btr.frame_counter = 0;
  1004. /* Exit Fixed Refresh mode */
  1005. } else if (state->fixed_refresh.fixed_active) {
  1006. state->fixed_refresh.frame_counter++;
  1007. if (state->fixed_refresh.frame_counter >
  1008. FIXED_REFRESH_EXIT_FRAME_COUNT) {
  1009. state->fixed_refresh.frame_counter = 0;
  1010. state->fixed_refresh.program_fixed = true;
  1011. state->fixed_refresh.fixed_active = false;
  1012. }
  1013. }
  1014. } else if (last_render_time_in_us > state->time.max_render_time_in_us) {
  1015. /* Enter Below the Range */
  1016. if (!state->btr.btr_active &&
  1017. core_freesync->map[map_index].caps->btr_supported) {
  1018. state->btr.program_btr = true;
  1019. state->btr.btr_active = true;
  1020. /* Enter Fixed Refresh mode */
  1021. } else if (!state->fixed_refresh.fixed_active &&
  1022. !core_freesync->map[map_index].caps->btr_supported) {
  1023. state->fixed_refresh.frame_counter++;
  1024. if (state->fixed_refresh.frame_counter >
  1025. FIXED_REFRESH_ENTER_FRAME_COUNT) {
  1026. state->fixed_refresh.frame_counter = 0;
  1027. state->fixed_refresh.program_fixed = true;
  1028. state->fixed_refresh.fixed_active = true;
  1029. }
  1030. }
  1031. }
  1032. /* When Below the Range is active, must react on every frame */
  1033. if (state->btr.btr_active)
  1034. state->btr.program_btr = true;
  1035. }
  1036. static void apply_below_the_range(struct core_freesync *core_freesync,
  1037. const struct dc_stream *stream, unsigned int map_index,
  1038. unsigned int last_render_time_in_us)
  1039. {
  1040. unsigned int inserted_frame_duration_in_us = 0;
  1041. unsigned int mid_point_frames_ceil = 0;
  1042. unsigned int mid_point_frames_floor = 0;
  1043. unsigned int frame_time_in_us = 0;
  1044. unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
  1045. unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
  1046. unsigned int frames_to_insert = 0;
  1047. unsigned int min_frame_duration_in_ns = 0;
  1048. struct freesync_state *state = &core_freesync->map[map_index].state;
  1049. if (!state->btr.program_btr)
  1050. return;
  1051. state->btr.program_btr = false;
  1052. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  1053. (1000000000ULL * 1000000),
  1054. state->nominal_refresh_rate_in_micro_hz)));
  1055. /* Program BTR */
  1056. /* BTR set to "not active" so disengage */
  1057. if (!state->btr.btr_active)
  1058. /* Restore FreeSync */
  1059. set_freesync_on_streams(core_freesync, &stream, 1);
  1060. /* BTR set to "active" so engage */
  1061. else {
  1062. /* Calculate number of midPoint frames that could fit within
  1063. * the render time interval- take ceil of this value
  1064. */
  1065. mid_point_frames_ceil = (last_render_time_in_us +
  1066. state->btr.mid_point_in_us- 1) /
  1067. state->btr.mid_point_in_us;
  1068. if (mid_point_frames_ceil > 0) {
  1069. frame_time_in_us = last_render_time_in_us /
  1070. mid_point_frames_ceil;
  1071. delta_from_mid_point_in_us_1 =
  1072. (state->btr.mid_point_in_us >
  1073. frame_time_in_us) ?
  1074. (state->btr.mid_point_in_us - frame_time_in_us):
  1075. (frame_time_in_us - state->btr.mid_point_in_us);
  1076. }
  1077. /* Calculate number of midPoint frames that could fit within
  1078. * the render time interval- take floor of this value
  1079. */
  1080. mid_point_frames_floor = last_render_time_in_us /
  1081. state->btr.mid_point_in_us;
  1082. if (mid_point_frames_floor > 0) {
  1083. frame_time_in_us = last_render_time_in_us /
  1084. mid_point_frames_floor;
  1085. delta_from_mid_point_in_us_2 =
  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. /* Choose number of frames to insert based on how close it
  1092. * can get to the mid point of the variable range.
  1093. */
  1094. if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2)
  1095. frames_to_insert = mid_point_frames_ceil;
  1096. else
  1097. frames_to_insert = mid_point_frames_floor;
  1098. /* Either we've calculated the number of frames to insert,
  1099. * or we need to insert min duration frames
  1100. */
  1101. if (frames_to_insert > 0)
  1102. inserted_frame_duration_in_us = last_render_time_in_us /
  1103. frames_to_insert;
  1104. if (inserted_frame_duration_in_us <
  1105. state->time.min_render_time_in_us)
  1106. inserted_frame_duration_in_us =
  1107. state->time.min_render_time_in_us;
  1108. /* Cache the calculated variables */
  1109. state->btr.inserted_frame_duration_in_us =
  1110. inserted_frame_duration_in_us;
  1111. state->btr.frames_to_insert = frames_to_insert;
  1112. state->btr.frame_counter = frames_to_insert;
  1113. }
  1114. }
  1115. static void apply_fixed_refresh(struct core_freesync *core_freesync,
  1116. const struct dc_stream *stream, unsigned int map_index)
  1117. {
  1118. unsigned int vmin = 0, vmax = 0;
  1119. struct freesync_state *state = &core_freesync->map[map_index].state;
  1120. if (!state->fixed_refresh.program_fixed)
  1121. return;
  1122. state->fixed_refresh.program_fixed = false;
  1123. /* Program Fixed Refresh */
  1124. /* Fixed Refresh set to "not active" so disengage */
  1125. if (!state->fixed_refresh.fixed_active) {
  1126. set_freesync_on_streams(core_freesync, &stream, 1);
  1127. /* Fixed Refresh set to "active" so engage (fix to max) */
  1128. } else {
  1129. vmin = state->freesync_range.vmin;
  1130. vmax = vmin;
  1131. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  1132. core_freesync->dc, &stream,
  1133. 1, vmin,
  1134. vmax);
  1135. }
  1136. }
  1137. void mod_freesync_pre_update_plane_addresses(struct mod_freesync *mod_freesync,
  1138. const struct dc_stream **streams, int num_streams,
  1139. unsigned int curr_time_stamp_in_us)
  1140. {
  1141. unsigned int stream_index, map_index, last_render_time_in_us = 0;
  1142. struct core_freesync *core_freesync = NULL;
  1143. if (mod_freesync == NULL)
  1144. return;
  1145. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  1146. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  1147. map_index = map_index_from_stream(core_freesync,
  1148. streams[stream_index]);
  1149. if (core_freesync->map[map_index].caps->supported) {
  1150. last_render_time_in_us = curr_time_stamp_in_us -
  1151. core_freesync->map[map_index].state.time.
  1152. prev_time_stamp_in_us;
  1153. /* Add the timestamps to the cache and determine
  1154. * whether BTR program is required
  1155. */
  1156. update_timestamps(core_freesync, streams[stream_index],
  1157. map_index, last_render_time_in_us);
  1158. if (core_freesync->map[map_index].state.fullscreen &&
  1159. core_freesync->map[map_index].user_enable.
  1160. enable_for_gaming) {
  1161. if (core_freesync->map[map_index].caps->btr_supported) {
  1162. apply_below_the_range(core_freesync,
  1163. streams[stream_index], map_index,
  1164. last_render_time_in_us);
  1165. } else {
  1166. apply_fixed_refresh(core_freesync,
  1167. streams[stream_index], map_index);
  1168. }
  1169. }
  1170. core_freesync->map[map_index].state.time.
  1171. prev_time_stamp_in_us = curr_time_stamp_in_us;
  1172. }
  1173. }
  1174. }