freesync.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * Copyright 2016 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #include "dm_services.h"
  26. #include "dc.h"
  27. #include "mod_freesync.h"
  28. #include "core_types.h"
  29. #include "core_dc.h"
  30. #define MOD_FREESYNC_MAX_CONCURRENT_STREAMS 32
  31. /* Refresh rate ramp at a fixed rate of 65 Hz/second */
  32. #define STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME ((1000 / 60) * 65)
  33. /* Number of elements in the render times cache array */
  34. #define RENDER_TIMES_MAX_COUNT 20
  35. /* Threshold to exit BTR (to avoid frequent enter-exits at the lower limit) */
  36. #define BTR_EXIT_MARGIN 2000
  37. #define FREESYNC_REGISTRY_NAME "freesync_v1"
  38. struct gradual_static_ramp {
  39. bool ramp_is_active;
  40. bool ramp_direction_is_up;
  41. unsigned int ramp_current_frame_duration_in_ns;
  42. };
  43. struct time_cache {
  44. /* video (48Hz feature) related */
  45. unsigned int update_duration_in_ns;
  46. /* BTR/fixed refresh related */
  47. unsigned int prev_time_stamp_in_us;
  48. unsigned int min_render_time_in_us;
  49. unsigned int max_render_time_in_us;
  50. unsigned int render_times_index;
  51. unsigned int render_times[RENDER_TIMES_MAX_COUNT];
  52. };
  53. struct below_the_range {
  54. bool btr_active;
  55. bool program_btr;
  56. unsigned int mid_point_in_us;
  57. unsigned int inserted_frame_duration_in_us;
  58. unsigned int frames_to_insert;
  59. unsigned int frame_counter;
  60. };
  61. struct fixed_refresh {
  62. bool fixed_refresh_active;
  63. bool program_fixed_refresh;
  64. };
  65. struct freesync_state {
  66. bool fullscreen;
  67. bool static_screen;
  68. bool video;
  69. unsigned int nominal_refresh_rate_in_micro_hz;
  70. bool windowed_fullscreen;
  71. struct time_cache time;
  72. struct gradual_static_ramp static_ramp;
  73. struct below_the_range btr;
  74. struct fixed_refresh fixed_refresh;
  75. };
  76. struct freesync_entity {
  77. const struct dc_stream *stream;
  78. struct mod_freesync_caps *caps;
  79. struct freesync_state state;
  80. struct mod_freesync_user_enable user_enable;
  81. };
  82. struct core_freesync {
  83. struct mod_freesync public;
  84. struct dc *dc;
  85. struct freesync_entity *map;
  86. int num_entities;
  87. };
  88. #define MOD_FREESYNC_TO_CORE(mod_freesync)\
  89. container_of(mod_freesync, struct core_freesync, public)
  90. static bool check_dc_support(const struct dc *dc)
  91. {
  92. if (dc->stream_funcs.adjust_vmin_vmax == NULL)
  93. return false;
  94. return true;
  95. }
  96. struct mod_freesync *mod_freesync_create(struct dc *dc)
  97. {
  98. struct core_freesync *core_freesync =
  99. dm_alloc(sizeof(struct core_freesync));
  100. struct core_dc *core_dc = DC_TO_CORE(dc);
  101. struct persistent_data_flag flag;
  102. int i = 0;
  103. if (core_freesync == NULL)
  104. goto fail_alloc_context;
  105. core_freesync->map = dm_alloc(sizeof(struct freesync_entity) *
  106. MOD_FREESYNC_MAX_CONCURRENT_STREAMS);
  107. if (core_freesync->map == NULL)
  108. goto fail_alloc_map;
  109. for (i = 0; i < MOD_FREESYNC_MAX_CONCURRENT_STREAMS; i++)
  110. core_freesync->map[i].stream = NULL;
  111. core_freesync->num_entities = 0;
  112. if (dc == NULL)
  113. goto fail_construct;
  114. core_freesync->dc = dc;
  115. if (!check_dc_support(dc))
  116. goto fail_construct;
  117. /* Create initial module folder in registry for freesync enable data */
  118. flag.save_per_edid = true;
  119. flag.save_per_link = false;
  120. dm_write_persistent_data(core_dc->ctx, NULL, FREESYNC_REGISTRY_NAME, NULL, NULL,
  121. 0, &flag);
  122. return &core_freesync->public;
  123. fail_construct:
  124. dm_free(core_freesync->map);
  125. fail_alloc_map:
  126. dm_free(core_freesync);
  127. fail_alloc_context:
  128. return NULL;
  129. }
  130. void mod_freesync_destroy(struct mod_freesync *mod_freesync)
  131. {
  132. if (mod_freesync != NULL) {
  133. int i;
  134. struct core_freesync *core_freesync =
  135. MOD_FREESYNC_TO_CORE(mod_freesync);
  136. for (i = 0; i < core_freesync->num_entities; i++)
  137. if (core_freesync->map[i].stream)
  138. dc_stream_release(core_freesync->map[i].stream);
  139. dm_free(core_freesync->map);
  140. dm_free(core_freesync);
  141. }
  142. }
  143. /* Given a specific dc_stream* this function finds its equivalent
  144. * on the core_freesync->map and returns the corresponding index
  145. */
  146. static unsigned int map_index_from_stream(struct core_freesync *core_freesync,
  147. const struct dc_stream *stream)
  148. {
  149. unsigned int index = 0;
  150. for (index = 0; index < core_freesync->num_entities; index++) {
  151. if (core_freesync->map[index].stream == stream) {
  152. return index;
  153. }
  154. }
  155. /* Could not find stream requested */
  156. ASSERT(false);
  157. return index;
  158. }
  159. bool mod_freesync_add_stream(struct mod_freesync *mod_freesync,
  160. const struct dc_stream *stream, struct mod_freesync_caps *caps)
  161. {
  162. struct core_freesync *core_freesync =
  163. MOD_FREESYNC_TO_CORE(mod_freesync);
  164. struct core_stream *core_stream =
  165. DC_STREAM_TO_CORE(stream);
  166. struct core_dc *core_dc = DC_TO_CORE(core_freesync->dc);
  167. int persistent_freesync_enable = 0;
  168. struct persistent_data_flag flag;
  169. flag.save_per_edid = true;
  170. flag.save_per_link = false;
  171. if (core_freesync->num_entities < MOD_FREESYNC_MAX_CONCURRENT_STREAMS) {
  172. dc_stream_retain(stream);
  173. core_freesync->map[core_freesync->num_entities].stream = stream;
  174. core_freesync->map[core_freesync->num_entities].caps = caps;
  175. core_freesync->map[core_freesync->num_entities].state.
  176. fullscreen = false;
  177. core_freesync->map[core_freesync->num_entities].state.
  178. static_screen = false;
  179. core_freesync->map[core_freesync->num_entities].state.
  180. video = false;
  181. core_freesync->map[core_freesync->num_entities].state.time.
  182. update_duration_in_ns = 0;
  183. core_freesync->map[core_freesync->num_entities].state.
  184. static_ramp.ramp_is_active = false;
  185. /* get persistent data from registry */
  186. if (dm_read_persistent_data(core_dc->ctx, stream->sink,
  187. FREESYNC_REGISTRY_NAME,
  188. "userenable", &persistent_freesync_enable,
  189. sizeof(int), &flag)) {
  190. core_freesync->map[core_freesync->num_entities].user_enable.
  191. enable_for_gaming =
  192. (persistent_freesync_enable & 1) ? true : false;
  193. core_freesync->map[core_freesync->num_entities].user_enable.
  194. enable_for_static =
  195. (persistent_freesync_enable & 2) ? true : false;
  196. core_freesync->map[core_freesync->num_entities].user_enable.
  197. enable_for_video =
  198. (persistent_freesync_enable & 4) ? true : false;
  199. } else {
  200. core_freesync->map[core_freesync->num_entities].user_enable.
  201. enable_for_gaming = false;
  202. core_freesync->map[core_freesync->num_entities].user_enable.
  203. enable_for_static = false;
  204. core_freesync->map[core_freesync->num_entities].user_enable.
  205. enable_for_video = false;
  206. }
  207. if (caps->supported)
  208. core_stream->public.ignore_msa_timing_param = 1;
  209. core_freesync->num_entities++;
  210. return true;
  211. }
  212. return false;
  213. }
  214. bool mod_freesync_remove_stream(struct mod_freesync *mod_freesync,
  215. const struct dc_stream *stream)
  216. {
  217. struct core_freesync *core_freesync =
  218. MOD_FREESYNC_TO_CORE(mod_freesync);
  219. int i = 0;
  220. unsigned int index = map_index_from_stream(core_freesync, stream);
  221. dc_stream_release(core_freesync->map[index].stream);
  222. core_freesync->map[index].stream = NULL;
  223. /* To remove this entity, shift everything after down */
  224. for (i = index; i < core_freesync->num_entities - 1; i++)
  225. core_freesync->map[i] = core_freesync->map[i + 1];
  226. core_freesync->num_entities--;
  227. return true;
  228. }
  229. static void update_stream_freesync_context(struct core_freesync *core_freesync,
  230. const struct dc_stream *stream)
  231. {
  232. unsigned int index;
  233. struct freesync_context *ctx;
  234. struct core_stream *core_stream;
  235. core_stream = DC_STREAM_TO_CORE(stream);
  236. ctx = &core_stream->public.freesync_ctx;
  237. index = map_index_from_stream(core_freesync, stream);
  238. ctx->supported = core_freesync->map[index].caps->supported;
  239. ctx->enabled = (core_freesync->map[index].user_enable.enable_for_gaming ||
  240. core_freesync->map[index].user_enable.enable_for_video ||
  241. core_freesync->map[index].user_enable.enable_for_static);
  242. ctx->active = (core_freesync->map[index].state.fullscreen ||
  243. core_freesync->map[index].state.video ||
  244. core_freesync->map[index].state.static_ramp.ramp_is_active);
  245. ctx->min_refresh_in_micro_hz =
  246. core_freesync->map[index].caps->min_refresh_in_micro_hz;
  247. ctx->nominal_refresh_in_micro_hz = core_freesync->
  248. map[index].state.nominal_refresh_rate_in_micro_hz;
  249. }
  250. static void update_stream(struct core_freesync *core_freesync,
  251. const struct dc_stream *stream)
  252. {
  253. struct core_stream *core_stream = DC_STREAM_TO_CORE(stream);
  254. unsigned int index = map_index_from_stream(core_freesync, stream);
  255. if (core_freesync->map[index].caps->supported) {
  256. core_stream->public.ignore_msa_timing_param = 1;
  257. update_stream_freesync_context(core_freesync, stream);
  258. }
  259. }
  260. static void calc_vmin_vmax(struct core_freesync *core_freesync,
  261. const struct dc_stream *stream, int *vmin, int *vmax)
  262. {
  263. unsigned int min_frame_duration_in_ns = 0, max_frame_duration_in_ns = 0;
  264. unsigned int index = map_index_from_stream(core_freesync, stream);
  265. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  266. (1000000000ULL * 1000000),
  267. core_freesync->map[index].state.
  268. nominal_refresh_rate_in_micro_hz)));
  269. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  270. (1000000000ULL * 1000000),
  271. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  272. *vmax = div64_u64(div64_u64(((unsigned long long)(
  273. max_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  274. stream->timing.h_total), 1000000);
  275. *vmin = div64_u64(div64_u64(((unsigned long long)(
  276. min_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  277. stream->timing.h_total), 1000000);
  278. }
  279. static void calc_v_total_from_duration(const struct dc_stream *stream,
  280. unsigned int duration_in_ns, int *v_total_nominal)
  281. {
  282. *v_total_nominal = div64_u64(div64_u64(((unsigned long long)(
  283. duration_in_ns) * stream->timing.pix_clk_khz),
  284. stream->timing.h_total), 1000000);
  285. }
  286. static void calc_v_total_for_static_ramp(struct core_freesync *core_freesync,
  287. const struct dc_stream *stream,
  288. unsigned int index, int *v_total)
  289. {
  290. unsigned int frame_duration = 0;
  291. struct gradual_static_ramp *static_ramp_variables =
  292. &core_freesync->map[index].state.static_ramp;
  293. /* Calc ratio between new and current frame duration with 3 digit */
  294. unsigned int frame_duration_ratio = div64_u64(1000000,
  295. (1000 + div64_u64(((unsigned long long)(
  296. STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
  297. static_ramp_variables->ramp_current_frame_duration_in_ns),
  298. 1000000000)));
  299. /* Calculate delta between new and current frame duration in ns */
  300. unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
  301. static_ramp_variables->ramp_current_frame_duration_in_ns) *
  302. (1000 - frame_duration_ratio)), 1000);
  303. /* Adjust frame duration delta based on ratio between current and
  304. * standard frame duration (frame duration at 60 Hz refresh rate).
  305. */
  306. unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
  307. frame_duration_delta) * static_ramp_variables->
  308. ramp_current_frame_duration_in_ns), 16666666);
  309. /* Going to a higher refresh rate (lower frame duration) */
  310. if (static_ramp_variables->ramp_direction_is_up) {
  311. /* reduce frame duration */
  312. static_ramp_variables->ramp_current_frame_duration_in_ns -=
  313. ramp_rate_interpolated;
  314. /* min frame duration */
  315. frame_duration = ((unsigned int) (div64_u64(
  316. (1000000000ULL * 1000000),
  317. core_freesync->map[index].state.
  318. nominal_refresh_rate_in_micro_hz)));
  319. /* adjust for frame duration below min */
  320. if (static_ramp_variables->ramp_current_frame_duration_in_ns <=
  321. frame_duration) {
  322. static_ramp_variables->ramp_is_active = false;
  323. static_ramp_variables->
  324. ramp_current_frame_duration_in_ns =
  325. frame_duration;
  326. }
  327. /* Going to a lower refresh rate (larger frame duration) */
  328. } else {
  329. /* increase frame duration */
  330. static_ramp_variables->ramp_current_frame_duration_in_ns +=
  331. ramp_rate_interpolated;
  332. /* max frame duration */
  333. frame_duration = ((unsigned int) (div64_u64(
  334. (1000000000ULL * 1000000),
  335. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  336. /* adjust for frame duration above max */
  337. if (static_ramp_variables->ramp_current_frame_duration_in_ns >=
  338. frame_duration) {
  339. static_ramp_variables->ramp_is_active = false;
  340. static_ramp_variables->
  341. ramp_current_frame_duration_in_ns =
  342. frame_duration;
  343. }
  344. }
  345. calc_v_total_from_duration(stream, static_ramp_variables->
  346. ramp_current_frame_duration_in_ns, v_total);
  347. }
  348. static void reset_freesync_state_variables(struct freesync_state* state)
  349. {
  350. state->static_ramp.ramp_is_active = false;
  351. if (state->nominal_refresh_rate_in_micro_hz)
  352. state->static_ramp.ramp_current_frame_duration_in_ns =
  353. ((unsigned int) (div64_u64(
  354. (1000000000ULL * 1000000),
  355. state->nominal_refresh_rate_in_micro_hz)));
  356. state->btr.btr_active = false;
  357. state->btr.frame_counter = 0;
  358. state->btr.frames_to_insert = 0;
  359. state->btr.inserted_frame_duration_in_us = 0;
  360. state->btr.program_btr = false;
  361. state->fixed_refresh.fixed_refresh_active = false;
  362. state->fixed_refresh.program_fixed_refresh = false;
  363. }
  364. /*
  365. * Sets freesync mode on a stream depending on current freesync state.
  366. */
  367. static bool set_freesync_on_streams(struct core_freesync *core_freesync,
  368. const struct dc_stream **streams, int num_streams)
  369. {
  370. int v_total_nominal = 0, v_total_min = 0, v_total_max = 0;
  371. unsigned int stream_idx, map_index = 0;
  372. struct freesync_state *state;
  373. if (num_streams == 0 || streams == NULL || num_streams > 1)
  374. return false;
  375. for (stream_idx = 0; stream_idx < num_streams; stream_idx++) {
  376. map_index = map_index_from_stream(core_freesync,
  377. streams[stream_idx]);
  378. state = &core_freesync->map[map_index].state;
  379. if (core_freesync->map[map_index].caps->supported) {
  380. /* Fullscreen has the topmost priority. If the
  381. * fullscreen bit is set, we are in a fullscreen
  382. * application where it should not matter if it is
  383. * static screen. We should not check the static_screen
  384. * or video bit.
  385. *
  386. * Special cases of fullscreen include btr and fixed
  387. * refresh. We program btr on every flip and involves
  388. * programming full range right before the last inserted frame.
  389. * However, we do not want to program the full freesync range
  390. * when fixed refresh is active, because we only program
  391. * that logic once and this will override it.
  392. */
  393. if (core_freesync->map[map_index].user_enable.
  394. enable_for_gaming == true &&
  395. state->fullscreen == true &&
  396. state->fixed_refresh.fixed_refresh_active == false) {
  397. /* Enable freesync */
  398. calc_vmin_vmax(core_freesync,
  399. streams[stream_idx],
  400. &v_total_min, &v_total_max);
  401. /* Update the freesync context for the stream */
  402. update_stream_freesync_context(core_freesync,
  403. streams[stream_idx]);
  404. core_freesync->dc->stream_funcs.
  405. adjust_vmin_vmax(core_freesync->dc, streams,
  406. num_streams, v_total_min,
  407. v_total_max);
  408. return true;
  409. } else if (core_freesync->map[map_index].user_enable.
  410. enable_for_video && state->video == true) {
  411. /* Enable 48Hz feature */
  412. calc_v_total_from_duration(streams[stream_idx],
  413. state->time.update_duration_in_ns,
  414. &v_total_nominal);
  415. /* Program only if v_total_nominal is in range*/
  416. if (v_total_nominal >=
  417. streams[stream_idx]->timing.v_total) {
  418. /* Update the freesync context for
  419. * the stream
  420. */
  421. update_stream_freesync_context(
  422. core_freesync,
  423. streams[stream_idx]);
  424. core_freesync->dc->stream_funcs.
  425. adjust_vmin_vmax(
  426. core_freesync->dc, streams,
  427. num_streams, v_total_nominal,
  428. v_total_nominal);
  429. }
  430. return true;
  431. } else {
  432. /* Disable freesync */
  433. v_total_nominal = streams[stream_idx]->
  434. timing.v_total;
  435. /* Update the freesync context for
  436. * the stream
  437. */
  438. update_stream_freesync_context(
  439. core_freesync,
  440. streams[stream_idx]);
  441. core_freesync->dc->stream_funcs.
  442. adjust_vmin_vmax(
  443. core_freesync->dc, streams,
  444. num_streams, v_total_nominal,
  445. v_total_nominal);
  446. /* Reset the cached variables */
  447. reset_freesync_state_variables(state);
  448. return true;
  449. }
  450. } else {
  451. /* Disable freesync */
  452. v_total_nominal = streams[stream_idx]->
  453. timing.v_total;
  454. /*
  455. * we have to reset drr always even sink does
  456. * not support freesync because a former stream has
  457. * be programmed
  458. */
  459. core_freesync->dc->stream_funcs.
  460. adjust_vmin_vmax(
  461. core_freesync->dc, streams,
  462. num_streams, v_total_nominal,
  463. v_total_nominal);
  464. /* Reset the cached variables */
  465. reset_freesync_state_variables(state);
  466. }
  467. }
  468. return false;
  469. }
  470. static void set_static_ramp_variables(struct core_freesync *core_freesync,
  471. unsigned int index, bool enable_static_screen)
  472. {
  473. unsigned int frame_duration = 0;
  474. struct gradual_static_ramp *static_ramp_variables =
  475. &core_freesync->map[index].state.static_ramp;
  476. /* If ramp is not active, set initial frame duration depending on
  477. * whether we are enabling/disabling static screen mode. If the ramp is
  478. * already active, ramp should continue in the opposite direction
  479. * starting with the current frame duration
  480. */
  481. if (!static_ramp_variables->ramp_is_active) {
  482. static_ramp_variables->ramp_is_active = true;
  483. if (enable_static_screen == true) {
  484. /* Going to lower refresh rate, so start from max
  485. * refresh rate (min frame duration)
  486. */
  487. frame_duration = ((unsigned int) (div64_u64(
  488. (1000000000ULL * 1000000),
  489. core_freesync->map[index].state.
  490. nominal_refresh_rate_in_micro_hz)));
  491. } else {
  492. /* Going to higher refresh rate, so start from min
  493. * refresh rate (max frame duration)
  494. */
  495. frame_duration = ((unsigned int) (div64_u64(
  496. (1000000000ULL * 1000000),
  497. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  498. }
  499. static_ramp_variables->
  500. ramp_current_frame_duration_in_ns = frame_duration;
  501. }
  502. /* If we are ENABLING static screen, refresh rate should go DOWN.
  503. * If we are DISABLING static screen, refresh rate should go UP.
  504. */
  505. static_ramp_variables->ramp_direction_is_up = !enable_static_screen;
  506. }
  507. void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
  508. const struct dc_stream **streams, int num_streams)
  509. {
  510. struct core_freesync *core_freesync =
  511. MOD_FREESYNC_TO_CORE(mod_freesync);
  512. unsigned int index, v_total = 0;
  513. struct freesync_state *state;
  514. if (core_freesync->num_entities == 0)
  515. return;
  516. index = map_index_from_stream(core_freesync,
  517. streams[0]);
  518. if (core_freesync->map[index].caps->supported == false)
  519. return;
  520. state = &core_freesync->map[index].state;
  521. /* Below the Range Logic */
  522. /* Only execute if in fullscreen mode */
  523. if (state->fullscreen == true &&
  524. core_freesync->map[index].user_enable.enable_for_gaming) {
  525. if (state->btr.btr_active)
  526. if (state->btr.frame_counter > 0)
  527. state->btr.frame_counter--;
  528. if (state->btr.frame_counter == 1) {
  529. /* Restore FreeSync */
  530. set_freesync_on_streams(core_freesync, streams,
  531. num_streams);
  532. }
  533. }
  534. /* If in fullscreen freesync mode or in video, do not program
  535. * static screen ramp values
  536. */
  537. if (state->fullscreen == true || state->video == true) {
  538. state->static_ramp.ramp_is_active = false;
  539. return;
  540. }
  541. /* Gradual Static Screen Ramping Logic */
  542. /* Execute if ramp is active and user enabled freesync static screen*/
  543. if (state->static_ramp.ramp_is_active &&
  544. core_freesync->map[index].user_enable.enable_for_static) {
  545. calc_v_total_for_static_ramp(core_freesync, streams[0],
  546. index, &v_total);
  547. /* Update the freesync context for the stream */
  548. update_stream_freesync_context(core_freesync, streams[0]);
  549. /* Program static screen ramp values */
  550. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  551. core_freesync->dc, streams,
  552. num_streams, v_total,
  553. v_total);
  554. }
  555. }
  556. void mod_freesync_update_state(struct mod_freesync *mod_freesync,
  557. const struct dc_stream **streams, int num_streams,
  558. struct mod_freesync_params *freesync_params)
  559. {
  560. struct core_freesync *core_freesync =
  561. MOD_FREESYNC_TO_CORE(mod_freesync);
  562. bool freesync_program_required = false;
  563. unsigned int stream_index;
  564. struct freesync_state *state;
  565. if (core_freesync->num_entities == 0)
  566. return;
  567. for(stream_index = 0; stream_index < num_streams; stream_index++) {
  568. unsigned int map_index = map_index_from_stream(core_freesync,
  569. streams[stream_index]);
  570. state = &core_freesync->map[map_index].state;
  571. switch (freesync_params->state){
  572. case FREESYNC_STATE_FULLSCREEN:
  573. state->fullscreen = freesync_params->enable;
  574. freesync_program_required = true;
  575. state->windowed_fullscreen =
  576. freesync_params->windowed_fullscreen;
  577. break;
  578. case FREESYNC_STATE_STATIC_SCREEN:
  579. /* Static screen ramp is only enabled for embedded
  580. * panels. Also change core variables only if there
  581. * is a change.
  582. */
  583. if (dc_is_embedded_signal(
  584. streams[stream_index]->sink->sink_signal) &&
  585. state->static_screen !=
  586. freesync_params->enable) {
  587. /* Change the state flag */
  588. state->static_screen = freesync_params->enable;
  589. /* Change static screen ramp variables */
  590. set_static_ramp_variables(core_freesync,
  591. map_index,
  592. freesync_params->enable);
  593. }
  594. /* We program the ramp starting next VUpdate */
  595. break;
  596. case FREESYNC_STATE_VIDEO:
  597. /* Change core variables only if there is a change*/
  598. if(freesync_params->update_duration_in_ns !=
  599. state->time.update_duration_in_ns) {
  600. state->video = freesync_params->enable;
  601. state->time.update_duration_in_ns =
  602. freesync_params->update_duration_in_ns;
  603. freesync_program_required = true;
  604. }
  605. break;
  606. case FREESYNC_STATE_NONE:
  607. /* handle here to avoid warning */
  608. break;
  609. }
  610. }
  611. if (freesync_program_required)
  612. /* Program freesync according to current state*/
  613. set_freesync_on_streams(core_freesync, streams, num_streams);
  614. }
  615. bool mod_freesync_get_state(struct mod_freesync *mod_freesync,
  616. const struct dc_stream *stream,
  617. struct mod_freesync_params *freesync_params)
  618. {
  619. struct core_freesync *core_freesync =
  620. MOD_FREESYNC_TO_CORE(mod_freesync);
  621. unsigned int index = map_index_from_stream(core_freesync, stream);
  622. if (core_freesync->map[index].state.fullscreen) {
  623. freesync_params->state = FREESYNC_STATE_FULLSCREEN;
  624. freesync_params->enable = true;
  625. } else if (core_freesync->map[index].state.static_screen) {
  626. freesync_params->state = FREESYNC_STATE_STATIC_SCREEN;
  627. freesync_params->enable = true;
  628. } else if (core_freesync->map[index].state.video) {
  629. freesync_params->state = FREESYNC_STATE_VIDEO;
  630. freesync_params->enable = true;
  631. } else {
  632. freesync_params->state = FREESYNC_STATE_NONE;
  633. freesync_params->enable = false;
  634. }
  635. freesync_params->update_duration_in_ns =
  636. core_freesync->map[index].state.time.update_duration_in_ns;
  637. freesync_params->windowed_fullscreen =
  638. core_freesync->map[index].state.windowed_fullscreen;
  639. return true;
  640. }
  641. bool mod_freesync_set_user_enable(struct mod_freesync *mod_freesync,
  642. const struct dc_stream **streams, int num_streams,
  643. struct mod_freesync_user_enable *user_enable)
  644. {
  645. struct core_freesync *core_freesync =
  646. MOD_FREESYNC_TO_CORE(mod_freesync);
  647. struct core_dc *core_dc = DC_TO_CORE(core_freesync->dc);
  648. unsigned int stream_index, map_index;
  649. int persistent_data = 0;
  650. struct persistent_data_flag flag;
  651. flag.save_per_edid = true;
  652. flag.save_per_link = false;
  653. for(stream_index = 0; stream_index < num_streams;
  654. stream_index++){
  655. map_index = map_index_from_stream(core_freesync,
  656. streams[stream_index]);
  657. core_freesync->map[map_index].user_enable = *user_enable;
  658. /* Write persistent data in registry*/
  659. if (core_freesync->map[map_index].user_enable.
  660. enable_for_gaming)
  661. persistent_data = persistent_data | 1;
  662. if (core_freesync->map[map_index].user_enable.
  663. enable_for_static)
  664. persistent_data = persistent_data | 2;
  665. if (core_freesync->map[map_index].user_enable.
  666. enable_for_video)
  667. persistent_data = persistent_data | 4;
  668. dm_write_persistent_data(core_dc->ctx,
  669. streams[stream_index]->sink,
  670. FREESYNC_REGISTRY_NAME,
  671. "userenable",
  672. &persistent_data,
  673. sizeof(int),
  674. &flag);
  675. }
  676. set_freesync_on_streams(core_freesync, streams, num_streams);
  677. return true;
  678. }
  679. bool mod_freesync_get_user_enable(struct mod_freesync *mod_freesync,
  680. const struct dc_stream *stream,
  681. struct mod_freesync_user_enable *user_enable)
  682. {
  683. struct core_freesync *core_freesync =
  684. MOD_FREESYNC_TO_CORE(mod_freesync);
  685. unsigned int index = map_index_from_stream(core_freesync, stream);
  686. *user_enable = core_freesync->map[index].user_enable;
  687. return true;
  688. }
  689. void mod_freesync_notify_mode_change(struct mod_freesync *mod_freesync,
  690. const struct dc_stream **streams, int num_streams)
  691. {
  692. struct core_freesync *core_freesync =
  693. MOD_FREESYNC_TO_CORE(mod_freesync);
  694. unsigned int stream_index, map_index;
  695. unsigned min_frame_duration_in_ns, max_frame_duration_in_ns;
  696. struct freesync_state *state;
  697. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  698. map_index = map_index_from_stream(core_freesync,
  699. streams[stream_index]);
  700. state = &core_freesync->map[map_index].state;
  701. if (core_freesync->map[map_index].caps->supported) {
  702. /* Update the field rate for new timing */
  703. state->nominal_refresh_rate_in_micro_hz = 1000000 *
  704. div64_u64(div64_u64((streams[stream_index]->
  705. timing.pix_clk_khz * 1000),
  706. streams[stream_index]->timing.v_total),
  707. streams[stream_index]->timing.h_total);
  708. /* Update the stream */
  709. update_stream(core_freesync, streams[stream_index]);
  710. /* Determine whether BTR can be supported */
  711. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  712. (1000000000ULL * 1000000),
  713. state->nominal_refresh_rate_in_micro_hz)));
  714. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  715. (1000000000ULL * 1000000),
  716. core_freesync->map[map_index].caps->min_refresh_in_micro_hz)));
  717. if (max_frame_duration_in_ns >=
  718. 2 * min_frame_duration_in_ns)
  719. core_freesync->map[map_index].caps->btr_supported = true;
  720. else
  721. core_freesync->map[map_index].caps->btr_supported = false;
  722. /* Cache the time variables */
  723. state->time.max_render_time_in_us =
  724. max_frame_duration_in_ns / 1000;
  725. state->time.min_render_time_in_us =
  726. min_frame_duration_in_ns / 1000;
  727. state->btr.mid_point_in_us =
  728. (max_frame_duration_in_ns +
  729. min_frame_duration_in_ns) / 2000;
  730. }
  731. }
  732. /* Program freesync according to current state*/
  733. set_freesync_on_streams(core_freesync, streams, num_streams);
  734. }
  735. /* Add the timestamps to the cache and determine whether BTR programming
  736. * is required, depending on the times calculated
  737. */
  738. static void update_timestamps(struct core_freesync *core_freesync,
  739. const struct dc_stream *stream, unsigned int map_index,
  740. unsigned int last_render_time_in_us)
  741. {
  742. struct freesync_state *state = &core_freesync->map[map_index].state;
  743. state->time.render_times[state->time.render_times_index] =
  744. last_render_time_in_us;
  745. state->time.render_times_index++;
  746. if (state->time.render_times_index >= RENDER_TIMES_MAX_COUNT)
  747. state->time.render_times_index = 0;
  748. if (last_render_time_in_us + BTR_EXIT_MARGIN <
  749. state->time.max_render_time_in_us) {
  750. /* Exit Below the Range */
  751. if (state->btr.btr_active) {
  752. state->btr.program_btr = true;
  753. state->btr.btr_active = false;
  754. state->btr.frame_counter = 0;
  755. /* Exit Fixed Refresh mode */
  756. } else if (state->fixed_refresh.fixed_refresh_active) {
  757. state->fixed_refresh.program_fixed_refresh = true;
  758. state->fixed_refresh.fixed_refresh_active = false;
  759. }
  760. } else if (last_render_time_in_us > state->time.max_render_time_in_us) {
  761. /* Enter Below the Range */
  762. if (!state->btr.btr_active &&
  763. core_freesync->map[map_index].caps->btr_supported) {
  764. state->btr.program_btr = true;
  765. state->btr.btr_active = true;
  766. /* Enter Fixed Refresh mode */
  767. } else if (!state->fixed_refresh.fixed_refresh_active &&
  768. !core_freesync->map[map_index].caps->btr_supported) {
  769. state->fixed_refresh.program_fixed_refresh = true;
  770. state->fixed_refresh.fixed_refresh_active = true;
  771. }
  772. }
  773. /* When Below the Range is active, must react on every frame */
  774. if (state->btr.btr_active)
  775. state->btr.program_btr = true;
  776. }
  777. static void apply_below_the_range(struct core_freesync *core_freesync,
  778. const struct dc_stream *stream, unsigned int map_index,
  779. unsigned int last_render_time_in_us)
  780. {
  781. unsigned int inserted_frame_duration_in_us = 0;
  782. unsigned int mid_point_frames_ceil = 0;
  783. unsigned int mid_point_frames_floor = 0;
  784. unsigned int frame_time_in_us = 0;
  785. unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
  786. unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
  787. unsigned int frames_to_insert = 0;
  788. unsigned int inserted_frame_v_total = 0;
  789. unsigned int vmin = 0, vmax = 0;
  790. unsigned int min_frame_duration_in_ns = 0;
  791. struct freesync_state *state = &core_freesync->map[map_index].state;
  792. if (!state->btr.program_btr)
  793. return;
  794. state->btr.program_btr = false;
  795. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  796. (1000000000ULL * 1000000),
  797. state->nominal_refresh_rate_in_micro_hz)));
  798. /* Program BTR */
  799. /* BTR set to "not active" so disengage */
  800. if (!state->btr.btr_active)
  801. /* Restore FreeSync */
  802. set_freesync_on_streams(core_freesync, &stream, 1);
  803. /* BTR set to "active" so engage */
  804. else {
  805. /* Calculate number of midPoint frames that could fit within
  806. * the render time interval- take ceil of this value
  807. */
  808. mid_point_frames_ceil = (last_render_time_in_us +
  809. state->btr.mid_point_in_us- 1) /
  810. state->btr.mid_point_in_us;
  811. if (mid_point_frames_ceil > 0) {
  812. frame_time_in_us = last_render_time_in_us /
  813. mid_point_frames_ceil;
  814. delta_from_mid_point_in_us_1 = (state->btr.mid_point_in_us >
  815. frame_time_in_us) ?
  816. (state->btr.mid_point_in_us - frame_time_in_us):
  817. (frame_time_in_us - state->btr.mid_point_in_us);
  818. }
  819. /* Calculate number of midPoint frames that could fit within
  820. * the render time interval- take floor of this value
  821. */
  822. mid_point_frames_floor = last_render_time_in_us /
  823. state->btr.mid_point_in_us;
  824. if (mid_point_frames_floor > 0) {
  825. frame_time_in_us = last_render_time_in_us /
  826. mid_point_frames_floor;
  827. delta_from_mid_point_in_us_2 = (state->btr.mid_point_in_us >
  828. frame_time_in_us) ?
  829. (state->btr.mid_point_in_us - frame_time_in_us):
  830. (frame_time_in_us - state->btr.mid_point_in_us);
  831. }
  832. /* Choose number of frames to insert based on how close it
  833. * can get to the mid point of the variable range.
  834. */
  835. if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2)
  836. frames_to_insert = mid_point_frames_ceil;
  837. else
  838. frames_to_insert = mid_point_frames_floor;
  839. /* Either we've calculated the number of frames to insert,
  840. * or we need to insert min duration frames
  841. */
  842. if (frames_to_insert > 0)
  843. inserted_frame_duration_in_us = last_render_time_in_us /
  844. frames_to_insert;
  845. if (inserted_frame_duration_in_us <
  846. state->time.min_render_time_in_us)
  847. inserted_frame_duration_in_us =
  848. state->time.min_render_time_in_us;
  849. /* We need the v_total_min from capability */
  850. calc_vmin_vmax(core_freesync, stream, &vmin, &vmax);
  851. inserted_frame_v_total = vmin;
  852. if (min_frame_duration_in_ns / 1000)
  853. inserted_frame_v_total = inserted_frame_duration_in_us *
  854. vmin / (min_frame_duration_in_ns / 1000);
  855. /* Set length of inserted frames as v_total_max*/
  856. vmax = inserted_frame_v_total;
  857. /* Program V_TOTAL */
  858. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  859. core_freesync->dc, &stream,
  860. 1, vmin,
  861. vmax);
  862. /* Cache the calculated variables */
  863. state->btr.inserted_frame_duration_in_us =
  864. inserted_frame_duration_in_us;
  865. state->btr.frames_to_insert = frames_to_insert;
  866. state->btr.frame_counter = frames_to_insert;
  867. }
  868. }
  869. static void apply_fixed_refresh(struct core_freesync *core_freesync,
  870. const struct dc_stream *stream, unsigned int map_index)
  871. {
  872. unsigned int vmin = 0, vmax = 0;
  873. struct freesync_state *state = &core_freesync->map[map_index].state;
  874. if (!state->fixed_refresh.program_fixed_refresh)
  875. return;
  876. state->fixed_refresh.program_fixed_refresh = false;
  877. /* Program Fixed Refresh */
  878. /* Fixed Refresh set to "not active" so disengage */
  879. if (!state->fixed_refresh.fixed_refresh_active) {
  880. set_freesync_on_streams(core_freesync, &stream, 1);
  881. /* Fixed Refresh set to "active" so engage (fix to max) */
  882. } else {
  883. calc_vmin_vmax(core_freesync, stream, &vmin, &vmax);
  884. vmax = vmin;
  885. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  886. core_freesync->dc, &stream,
  887. 1, vmin,
  888. vmax);
  889. }
  890. }
  891. void mod_freesync_pre_update_plane_addresses(struct mod_freesync *mod_freesync,
  892. const struct dc_stream **streams, int num_streams,
  893. unsigned int curr_time_stamp_in_us)
  894. {
  895. unsigned int stream_index, map_index, last_render_time_in_us = 0;
  896. struct core_freesync *core_freesync =
  897. MOD_FREESYNC_TO_CORE(mod_freesync);
  898. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  899. map_index = map_index_from_stream(core_freesync,
  900. streams[stream_index]);
  901. if (core_freesync->map[map_index].caps->supported) {
  902. last_render_time_in_us = curr_time_stamp_in_us -
  903. core_freesync->map[map_index].state.time.
  904. prev_time_stamp_in_us;
  905. /* Add the timestamps to the cache and determine
  906. * whether BTR program is required
  907. */
  908. update_timestamps(core_freesync, streams[stream_index],
  909. map_index, last_render_time_in_us);
  910. if (core_freesync->map[map_index].state.fullscreen &&
  911. core_freesync->map[map_index].user_enable.
  912. enable_for_gaming) {
  913. if (core_freesync->map[map_index].caps->btr_supported) {
  914. apply_below_the_range(core_freesync,
  915. streams[stream_index], map_index,
  916. last_render_time_in_us);
  917. } else {
  918. apply_fixed_refresh(core_freesync,
  919. streams[stream_index], map_index);
  920. }
  921. }
  922. core_freesync->map[map_index].state.time.
  923. prev_time_stamp_in_us = curr_time_stamp_in_us;
  924. }
  925. }
  926. }