freesync.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210
  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_stream *core_stream = NULL;
  163. struct core_dc *core_dc = NULL;
  164. struct core_freesync *core_freesync = NULL;
  165. int persistent_freesync_enable = 0;
  166. struct persistent_data_flag flag;
  167. unsigned int nom_refresh_rate_micro_hz;
  168. unsigned long long temp;
  169. if (mod_freesync == NULL)
  170. return false;
  171. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  172. core_stream = DC_STREAM_TO_CORE(stream);
  173. core_dc = DC_TO_CORE(core_freesync->dc);
  174. flag.save_per_edid = true;
  175. flag.save_per_link = false;
  176. if (core_freesync->num_entities < MOD_FREESYNC_MAX_CONCURRENT_STREAMS) {
  177. dc_stream_retain(stream);
  178. core_freesync->map[core_freesync->num_entities].stream = stream;
  179. core_freesync->map[core_freesync->num_entities].caps = caps;
  180. core_freesync->map[core_freesync->num_entities].state.
  181. fullscreen = false;
  182. core_freesync->map[core_freesync->num_entities].state.
  183. static_screen = false;
  184. core_freesync->map[core_freesync->num_entities].state.
  185. video = false;
  186. core_freesync->map[core_freesync->num_entities].state.time.
  187. update_duration_in_ns = 0;
  188. core_freesync->map[core_freesync->num_entities].state.
  189. static_ramp.ramp_is_active = false;
  190. /* get persistent data from registry */
  191. if (dm_read_persistent_data(core_dc->ctx, stream->sink,
  192. FREESYNC_REGISTRY_NAME,
  193. "userenable", &persistent_freesync_enable,
  194. sizeof(int), &flag)) {
  195. core_freesync->map[core_freesync->num_entities].user_enable.
  196. enable_for_gaming =
  197. (persistent_freesync_enable & 1) ? true : false;
  198. core_freesync->map[core_freesync->num_entities].user_enable.
  199. enable_for_static =
  200. (persistent_freesync_enable & 2) ? true : false;
  201. core_freesync->map[core_freesync->num_entities].user_enable.
  202. enable_for_video =
  203. (persistent_freesync_enable & 4) ? true : false;
  204. } else {
  205. core_freesync->map[core_freesync->num_entities].user_enable.
  206. enable_for_gaming = false;
  207. core_freesync->map[core_freesync->num_entities].user_enable.
  208. enable_for_static = false;
  209. core_freesync->map[core_freesync->num_entities].user_enable.
  210. enable_for_video = false;
  211. }
  212. temp = core_stream->public.timing.pix_clk_khz;
  213. temp *= 1000ULL * 1000ULL * 1000ULL;
  214. temp = div_u64(temp, core_stream->public.timing.h_total);
  215. temp = div_u64(temp, core_stream->public.timing.v_total);
  216. nom_refresh_rate_micro_hz = (unsigned int) temp;
  217. if (caps->supported &&
  218. nom_refresh_rate_micro_hz >= caps->min_refresh_in_micro_hz &&
  219. nom_refresh_rate_micro_hz <= caps->max_refresh_in_micro_hz)
  220. core_stream->public.ignore_msa_timing_param = 1;
  221. core_freesync->num_entities++;
  222. return true;
  223. }
  224. return false;
  225. }
  226. bool mod_freesync_remove_stream(struct mod_freesync *mod_freesync,
  227. const struct dc_stream *stream)
  228. {
  229. int i = 0;
  230. struct core_freesync *core_freesync = NULL;
  231. unsigned int index = 0;
  232. if (mod_freesync == NULL)
  233. return false;
  234. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  235. index = map_index_from_stream(core_freesync, stream);
  236. dc_stream_release(core_freesync->map[index].stream);
  237. core_freesync->map[index].stream = NULL;
  238. /* To remove this entity, shift everything after down */
  239. for (i = index; i < core_freesync->num_entities - 1; i++)
  240. core_freesync->map[i] = core_freesync->map[i + 1];
  241. core_freesync->num_entities--;
  242. return true;
  243. }
  244. static void update_stream_freesync_context(struct core_freesync *core_freesync,
  245. const struct dc_stream *stream)
  246. {
  247. unsigned int index;
  248. struct freesync_context *ctx;
  249. struct core_stream *core_stream;
  250. core_stream = DC_STREAM_TO_CORE(stream);
  251. ctx = &core_stream->public.freesync_ctx;
  252. index = map_index_from_stream(core_freesync, stream);
  253. ctx->supported = core_freesync->map[index].caps->supported;
  254. ctx->enabled = (core_freesync->map[index].user_enable.enable_for_gaming ||
  255. core_freesync->map[index].user_enable.enable_for_video ||
  256. core_freesync->map[index].user_enable.enable_for_static);
  257. ctx->active = (core_freesync->map[index].state.fullscreen ||
  258. core_freesync->map[index].state.video ||
  259. core_freesync->map[index].state.static_ramp.ramp_is_active);
  260. ctx->min_refresh_in_micro_hz =
  261. core_freesync->map[index].caps->min_refresh_in_micro_hz;
  262. ctx->nominal_refresh_in_micro_hz = core_freesync->
  263. map[index].state.nominal_refresh_rate_in_micro_hz;
  264. }
  265. static void update_stream(struct core_freesync *core_freesync,
  266. const struct dc_stream *stream)
  267. {
  268. struct core_stream *core_stream = DC_STREAM_TO_CORE(stream);
  269. unsigned int index = map_index_from_stream(core_freesync, stream);
  270. if (core_freesync->map[index].caps->supported) {
  271. core_stream->public.ignore_msa_timing_param = 1;
  272. update_stream_freesync_context(core_freesync, stream);
  273. }
  274. }
  275. static void calc_vmin_vmax(struct core_freesync *core_freesync,
  276. const struct dc_stream *stream, int *vmin, int *vmax)
  277. {
  278. unsigned int min_frame_duration_in_ns = 0, max_frame_duration_in_ns = 0;
  279. unsigned int index = map_index_from_stream(core_freesync, stream);
  280. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  281. (1000000000ULL * 1000000),
  282. core_freesync->map[index].state.
  283. nominal_refresh_rate_in_micro_hz)));
  284. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  285. (1000000000ULL * 1000000),
  286. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  287. *vmax = div64_u64(div64_u64(((unsigned long long)(
  288. max_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  289. stream->timing.h_total), 1000000);
  290. *vmin = div64_u64(div64_u64(((unsigned long long)(
  291. min_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  292. stream->timing.h_total), 1000000);
  293. }
  294. static void calc_v_total_from_duration(const struct dc_stream *stream,
  295. unsigned int duration_in_ns, int *v_total_nominal)
  296. {
  297. *v_total_nominal = div64_u64(div64_u64(((unsigned long long)(
  298. duration_in_ns) * stream->timing.pix_clk_khz),
  299. stream->timing.h_total), 1000000);
  300. }
  301. static void calc_v_total_for_static_ramp(struct core_freesync *core_freesync,
  302. const struct dc_stream *stream,
  303. unsigned int index, int *v_total)
  304. {
  305. unsigned int frame_duration = 0;
  306. struct gradual_static_ramp *static_ramp_variables =
  307. &core_freesync->map[index].state.static_ramp;
  308. /* Calc ratio between new and current frame duration with 3 digit */
  309. unsigned int frame_duration_ratio = div64_u64(1000000,
  310. (1000 + div64_u64(((unsigned long long)(
  311. STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
  312. static_ramp_variables->ramp_current_frame_duration_in_ns),
  313. 1000000000)));
  314. /* Calculate delta between new and current frame duration in ns */
  315. unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
  316. static_ramp_variables->ramp_current_frame_duration_in_ns) *
  317. (1000 - frame_duration_ratio)), 1000);
  318. /* Adjust frame duration delta based on ratio between current and
  319. * standard frame duration (frame duration at 60 Hz refresh rate).
  320. */
  321. unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
  322. frame_duration_delta) * static_ramp_variables->
  323. ramp_current_frame_duration_in_ns), 16666666);
  324. /* Going to a higher refresh rate (lower frame duration) */
  325. if (static_ramp_variables->ramp_direction_is_up) {
  326. /* reduce frame duration */
  327. static_ramp_variables->ramp_current_frame_duration_in_ns -=
  328. ramp_rate_interpolated;
  329. /* min frame duration */
  330. frame_duration = ((unsigned int) (div64_u64(
  331. (1000000000ULL * 1000000),
  332. core_freesync->map[index].state.
  333. nominal_refresh_rate_in_micro_hz)));
  334. /* adjust for frame duration below min */
  335. if (static_ramp_variables->ramp_current_frame_duration_in_ns <=
  336. frame_duration) {
  337. static_ramp_variables->ramp_is_active = false;
  338. static_ramp_variables->
  339. ramp_current_frame_duration_in_ns =
  340. frame_duration;
  341. }
  342. /* Going to a lower refresh rate (larger frame duration) */
  343. } else {
  344. /* increase frame duration */
  345. static_ramp_variables->ramp_current_frame_duration_in_ns +=
  346. ramp_rate_interpolated;
  347. /* max frame duration */
  348. frame_duration = ((unsigned int) (div64_u64(
  349. (1000000000ULL * 1000000),
  350. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  351. /* adjust for frame duration above max */
  352. if (static_ramp_variables->ramp_current_frame_duration_in_ns >=
  353. frame_duration) {
  354. static_ramp_variables->ramp_is_active = false;
  355. static_ramp_variables->
  356. ramp_current_frame_duration_in_ns =
  357. frame_duration;
  358. }
  359. }
  360. calc_v_total_from_duration(stream, static_ramp_variables->
  361. ramp_current_frame_duration_in_ns, v_total);
  362. }
  363. static void reset_freesync_state_variables(struct freesync_state* state)
  364. {
  365. state->static_ramp.ramp_is_active = false;
  366. if (state->nominal_refresh_rate_in_micro_hz)
  367. state->static_ramp.ramp_current_frame_duration_in_ns =
  368. ((unsigned int) (div64_u64(
  369. (1000000000ULL * 1000000),
  370. state->nominal_refresh_rate_in_micro_hz)));
  371. state->btr.btr_active = false;
  372. state->btr.frame_counter = 0;
  373. state->btr.frames_to_insert = 0;
  374. state->btr.inserted_frame_duration_in_us = 0;
  375. state->btr.program_btr = false;
  376. state->fixed_refresh.fixed_refresh_active = false;
  377. state->fixed_refresh.program_fixed_refresh = false;
  378. }
  379. /*
  380. * Sets freesync mode on a stream depending on current freesync state.
  381. */
  382. static bool set_freesync_on_streams(struct core_freesync *core_freesync,
  383. const struct dc_stream **streams, int num_streams)
  384. {
  385. int v_total_nominal = 0, v_total_min = 0, v_total_max = 0;
  386. unsigned int stream_idx, map_index = 0;
  387. struct freesync_state *state;
  388. if (num_streams == 0 || streams == NULL || num_streams > 1)
  389. return false;
  390. for (stream_idx = 0; stream_idx < num_streams; stream_idx++) {
  391. map_index = map_index_from_stream(core_freesync,
  392. streams[stream_idx]);
  393. state = &core_freesync->map[map_index].state;
  394. if (core_freesync->map[map_index].caps->supported) {
  395. /* Fullscreen has the topmost priority. If the
  396. * fullscreen bit is set, we are in a fullscreen
  397. * application where it should not matter if it is
  398. * static screen. We should not check the static_screen
  399. * or video bit.
  400. *
  401. * Special cases of fullscreen include btr and fixed
  402. * refresh. We program btr on every flip and involves
  403. * programming full range right before the last inserted frame.
  404. * However, we do not want to program the full freesync range
  405. * when fixed refresh is active, because we only program
  406. * that logic once and this will override it.
  407. */
  408. if (core_freesync->map[map_index].user_enable.
  409. enable_for_gaming == true &&
  410. state->fullscreen == true &&
  411. state->fixed_refresh.fixed_refresh_active == false) {
  412. /* Enable freesync */
  413. calc_vmin_vmax(core_freesync,
  414. streams[stream_idx],
  415. &v_total_min, &v_total_max);
  416. /* Update the freesync context for the stream */
  417. update_stream_freesync_context(core_freesync,
  418. streams[stream_idx]);
  419. core_freesync->dc->stream_funcs.
  420. adjust_vmin_vmax(core_freesync->dc, streams,
  421. num_streams, v_total_min,
  422. v_total_max);
  423. return true;
  424. } else if (core_freesync->map[map_index].user_enable.
  425. enable_for_video && state->video == true) {
  426. /* Enable 48Hz feature */
  427. calc_v_total_from_duration(streams[stream_idx],
  428. state->time.update_duration_in_ns,
  429. &v_total_nominal);
  430. /* Program only if v_total_nominal is in range*/
  431. if (v_total_nominal >=
  432. streams[stream_idx]->timing.v_total) {
  433. /* Update the freesync context for
  434. * the stream
  435. */
  436. update_stream_freesync_context(
  437. core_freesync,
  438. streams[stream_idx]);
  439. core_freesync->dc->stream_funcs.
  440. adjust_vmin_vmax(
  441. core_freesync->dc, streams,
  442. num_streams, v_total_nominal,
  443. v_total_nominal);
  444. }
  445. return true;
  446. } else {
  447. /* Disable freesync */
  448. v_total_nominal = streams[stream_idx]->
  449. timing.v_total;
  450. /* Update the freesync context for
  451. * the stream
  452. */
  453. update_stream_freesync_context(
  454. core_freesync,
  455. streams[stream_idx]);
  456. core_freesync->dc->stream_funcs.
  457. adjust_vmin_vmax(
  458. core_freesync->dc, streams,
  459. num_streams, v_total_nominal,
  460. v_total_nominal);
  461. /* Reset the cached variables */
  462. reset_freesync_state_variables(state);
  463. return true;
  464. }
  465. } else {
  466. /* Disable freesync */
  467. v_total_nominal = streams[stream_idx]->
  468. timing.v_total;
  469. /*
  470. * we have to reset drr always even sink does
  471. * not support freesync because a former stream has
  472. * be programmed
  473. */
  474. core_freesync->dc->stream_funcs.
  475. adjust_vmin_vmax(
  476. core_freesync->dc, streams,
  477. num_streams, v_total_nominal,
  478. v_total_nominal);
  479. /* Reset the cached variables */
  480. reset_freesync_state_variables(state);
  481. }
  482. }
  483. return false;
  484. }
  485. static void set_static_ramp_variables(struct core_freesync *core_freesync,
  486. unsigned int index, bool enable_static_screen)
  487. {
  488. unsigned int frame_duration = 0;
  489. struct gradual_static_ramp *static_ramp_variables =
  490. &core_freesync->map[index].state.static_ramp;
  491. /* If ramp is not active, set initial frame duration depending on
  492. * whether we are enabling/disabling static screen mode. If the ramp is
  493. * already active, ramp should continue in the opposite direction
  494. * starting with the current frame duration
  495. */
  496. if (!static_ramp_variables->ramp_is_active) {
  497. static_ramp_variables->ramp_is_active = true;
  498. if (enable_static_screen == true) {
  499. /* Going to lower refresh rate, so start from max
  500. * refresh rate (min frame duration)
  501. */
  502. frame_duration = ((unsigned int) (div64_u64(
  503. (1000000000ULL * 1000000),
  504. core_freesync->map[index].state.
  505. nominal_refresh_rate_in_micro_hz)));
  506. } else {
  507. /* Going to higher refresh rate, so start from min
  508. * refresh rate (max frame duration)
  509. */
  510. frame_duration = ((unsigned int) (div64_u64(
  511. (1000000000ULL * 1000000),
  512. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  513. }
  514. static_ramp_variables->
  515. ramp_current_frame_duration_in_ns = frame_duration;
  516. }
  517. /* If we are ENABLING static screen, refresh rate should go DOWN.
  518. * If we are DISABLING static screen, refresh rate should go UP.
  519. */
  520. static_ramp_variables->ramp_direction_is_up = !enable_static_screen;
  521. }
  522. void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
  523. const struct dc_stream **streams, int num_streams)
  524. {
  525. unsigned int index, v_total = 0;
  526. struct freesync_state *state;
  527. struct core_freesync *core_freesync = NULL;
  528. if (mod_freesync == NULL)
  529. return;
  530. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  531. if (core_freesync->num_entities == 0)
  532. return;
  533. index = map_index_from_stream(core_freesync,
  534. streams[0]);
  535. if (core_freesync->map[index].caps->supported == false)
  536. return;
  537. state = &core_freesync->map[index].state;
  538. /* Below the Range Logic */
  539. /* Only execute if in fullscreen mode */
  540. if (state->fullscreen == true &&
  541. core_freesync->map[index].user_enable.enable_for_gaming) {
  542. if (state->btr.btr_active)
  543. if (state->btr.frame_counter > 0)
  544. state->btr.frame_counter--;
  545. if (state->btr.frame_counter == 1) {
  546. /* Restore FreeSync */
  547. set_freesync_on_streams(core_freesync, streams,
  548. num_streams);
  549. }
  550. }
  551. /* If in fullscreen freesync mode or in video, do not program
  552. * static screen ramp values
  553. */
  554. if (state->fullscreen == true || state->video == true) {
  555. state->static_ramp.ramp_is_active = false;
  556. return;
  557. }
  558. /* Gradual Static Screen Ramping Logic */
  559. /* Execute if ramp is active and user enabled freesync static screen*/
  560. if (state->static_ramp.ramp_is_active &&
  561. core_freesync->map[index].user_enable.enable_for_static) {
  562. calc_v_total_for_static_ramp(core_freesync, streams[0],
  563. index, &v_total);
  564. /* Update the freesync context for the stream */
  565. update_stream_freesync_context(core_freesync, streams[0]);
  566. /* Program static screen ramp values */
  567. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  568. core_freesync->dc, streams,
  569. num_streams, v_total,
  570. v_total);
  571. }
  572. }
  573. void mod_freesync_update_state(struct mod_freesync *mod_freesync,
  574. const struct dc_stream **streams, int num_streams,
  575. struct mod_freesync_params *freesync_params)
  576. {
  577. bool freesync_program_required = false;
  578. unsigned int stream_index;
  579. struct freesync_state *state;
  580. struct core_freesync *core_freesync = NULL;
  581. if (mod_freesync == NULL)
  582. return;
  583. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  584. if (core_freesync->num_entities == 0)
  585. return;
  586. for(stream_index = 0; stream_index < num_streams; stream_index++) {
  587. unsigned int map_index = map_index_from_stream(core_freesync,
  588. streams[stream_index]);
  589. state = &core_freesync->map[map_index].state;
  590. switch (freesync_params->state){
  591. case FREESYNC_STATE_FULLSCREEN:
  592. state->fullscreen = freesync_params->enable;
  593. freesync_program_required = true;
  594. state->windowed_fullscreen =
  595. freesync_params->windowed_fullscreen;
  596. break;
  597. case FREESYNC_STATE_STATIC_SCREEN:
  598. /* Static screen ramp is only enabled for embedded
  599. * panels. Also change core variables only if there
  600. * is a change.
  601. */
  602. if (dc_is_embedded_signal(
  603. streams[stream_index]->sink->sink_signal) &&
  604. state->static_screen !=
  605. freesync_params->enable) {
  606. /* Change the state flag */
  607. state->static_screen = freesync_params->enable;
  608. /* Change static screen ramp variables */
  609. set_static_ramp_variables(core_freesync,
  610. map_index,
  611. freesync_params->enable);
  612. }
  613. /* We program the ramp starting next VUpdate */
  614. break;
  615. case FREESYNC_STATE_VIDEO:
  616. /* Change core variables only if there is a change*/
  617. if(freesync_params->update_duration_in_ns !=
  618. state->time.update_duration_in_ns) {
  619. state->video = freesync_params->enable;
  620. state->time.update_duration_in_ns =
  621. freesync_params->update_duration_in_ns;
  622. freesync_program_required = true;
  623. }
  624. break;
  625. case FREESYNC_STATE_NONE:
  626. /* handle here to avoid warning */
  627. break;
  628. }
  629. }
  630. if (freesync_program_required)
  631. /* Program freesync according to current state*/
  632. set_freesync_on_streams(core_freesync, streams, num_streams);
  633. }
  634. bool mod_freesync_get_state(struct mod_freesync *mod_freesync,
  635. const struct dc_stream *stream,
  636. struct mod_freesync_params *freesync_params)
  637. {
  638. unsigned int index = 0;
  639. struct core_freesync *core_freesync = NULL;
  640. if (mod_freesync == NULL)
  641. return false;
  642. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  643. index = map_index_from_stream(core_freesync, stream);
  644. if (core_freesync->map[index].state.fullscreen) {
  645. freesync_params->state = FREESYNC_STATE_FULLSCREEN;
  646. freesync_params->enable = true;
  647. } else if (core_freesync->map[index].state.static_screen) {
  648. freesync_params->state = FREESYNC_STATE_STATIC_SCREEN;
  649. freesync_params->enable = true;
  650. } else if (core_freesync->map[index].state.video) {
  651. freesync_params->state = FREESYNC_STATE_VIDEO;
  652. freesync_params->enable = true;
  653. } else {
  654. freesync_params->state = FREESYNC_STATE_NONE;
  655. freesync_params->enable = false;
  656. }
  657. freesync_params->update_duration_in_ns =
  658. core_freesync->map[index].state.time.update_duration_in_ns;
  659. freesync_params->windowed_fullscreen =
  660. core_freesync->map[index].state.windowed_fullscreen;
  661. return true;
  662. }
  663. bool mod_freesync_set_user_enable(struct mod_freesync *mod_freesync,
  664. const struct dc_stream **streams, int num_streams,
  665. struct mod_freesync_user_enable *user_enable)
  666. {
  667. unsigned int stream_index, map_index;
  668. int persistent_data = 0;
  669. struct persistent_data_flag flag;
  670. struct core_dc *core_dc = NULL;
  671. struct core_freesync *core_freesync = NULL;
  672. if (mod_freesync == NULL)
  673. return false;
  674. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  675. core_dc = DC_TO_CORE(core_freesync->dc);
  676. flag.save_per_edid = true;
  677. flag.save_per_link = false;
  678. for(stream_index = 0; stream_index < num_streams;
  679. stream_index++){
  680. map_index = map_index_from_stream(core_freesync,
  681. streams[stream_index]);
  682. core_freesync->map[map_index].user_enable = *user_enable;
  683. /* Write persistent data in registry*/
  684. if (core_freesync->map[map_index].user_enable.
  685. enable_for_gaming)
  686. persistent_data = persistent_data | 1;
  687. if (core_freesync->map[map_index].user_enable.
  688. enable_for_static)
  689. persistent_data = persistent_data | 2;
  690. if (core_freesync->map[map_index].user_enable.
  691. enable_for_video)
  692. persistent_data = persistent_data | 4;
  693. dm_write_persistent_data(core_dc->ctx,
  694. streams[stream_index]->sink,
  695. FREESYNC_REGISTRY_NAME,
  696. "userenable",
  697. &persistent_data,
  698. sizeof(int),
  699. &flag);
  700. }
  701. set_freesync_on_streams(core_freesync, streams, num_streams);
  702. return true;
  703. }
  704. bool mod_freesync_get_user_enable(struct mod_freesync *mod_freesync,
  705. const struct dc_stream *stream,
  706. struct mod_freesync_user_enable *user_enable)
  707. {
  708. unsigned int index = 0;
  709. struct core_freesync *core_freesync = NULL;
  710. if (mod_freesync == NULL)
  711. return false;
  712. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  713. index = map_index_from_stream(core_freesync, stream);
  714. *user_enable = core_freesync->map[index].user_enable;
  715. return true;
  716. }
  717. void mod_freesync_notify_mode_change(struct mod_freesync *mod_freesync,
  718. const struct dc_stream **streams, int num_streams)
  719. {
  720. unsigned int stream_index, map_index;
  721. unsigned min_frame_duration_in_ns, max_frame_duration_in_ns;
  722. struct freesync_state *state;
  723. struct core_freesync *core_freesync = NULL;
  724. if (mod_freesync == NULL)
  725. return;
  726. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  727. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  728. map_index = map_index_from_stream(core_freesync,
  729. streams[stream_index]);
  730. state = &core_freesync->map[map_index].state;
  731. if (core_freesync->map[map_index].caps->supported) {
  732. /* Update the field rate for new timing */
  733. state->nominal_refresh_rate_in_micro_hz = 1000000 *
  734. div64_u64(div64_u64((streams[stream_index]->
  735. timing.pix_clk_khz * 1000),
  736. streams[stream_index]->timing.v_total),
  737. streams[stream_index]->timing.h_total);
  738. /* Update the stream */
  739. update_stream(core_freesync, streams[stream_index]);
  740. /* Determine whether BTR can be supported */
  741. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  742. (1000000000ULL * 1000000),
  743. state->nominal_refresh_rate_in_micro_hz)));
  744. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  745. (1000000000ULL * 1000000),
  746. core_freesync->map[map_index].caps->min_refresh_in_micro_hz)));
  747. if (max_frame_duration_in_ns >=
  748. 2 * min_frame_duration_in_ns)
  749. core_freesync->map[map_index].caps->btr_supported = true;
  750. else
  751. core_freesync->map[map_index].caps->btr_supported = false;
  752. /* Cache the time variables */
  753. state->time.max_render_time_in_us =
  754. max_frame_duration_in_ns / 1000;
  755. state->time.min_render_time_in_us =
  756. min_frame_duration_in_ns / 1000;
  757. state->btr.mid_point_in_us =
  758. (max_frame_duration_in_ns +
  759. min_frame_duration_in_ns) / 2000;
  760. }
  761. }
  762. /* Program freesync according to current state*/
  763. set_freesync_on_streams(core_freesync, streams, num_streams);
  764. }
  765. /* Add the timestamps to the cache and determine whether BTR programming
  766. * is required, depending on the times calculated
  767. */
  768. static void update_timestamps(struct core_freesync *core_freesync,
  769. const struct dc_stream *stream, unsigned int map_index,
  770. unsigned int last_render_time_in_us)
  771. {
  772. struct freesync_state *state = &core_freesync->map[map_index].state;
  773. state->time.render_times[state->time.render_times_index] =
  774. last_render_time_in_us;
  775. state->time.render_times_index++;
  776. if (state->time.render_times_index >= RENDER_TIMES_MAX_COUNT)
  777. state->time.render_times_index = 0;
  778. if (last_render_time_in_us + BTR_EXIT_MARGIN <
  779. state->time.max_render_time_in_us) {
  780. /* Exit Below the Range */
  781. if (state->btr.btr_active) {
  782. state->btr.program_btr = true;
  783. state->btr.btr_active = false;
  784. state->btr.frame_counter = 0;
  785. /* Exit Fixed Refresh mode */
  786. } else if (state->fixed_refresh.fixed_refresh_active) {
  787. state->fixed_refresh.program_fixed_refresh = true;
  788. state->fixed_refresh.fixed_refresh_active = false;
  789. }
  790. } else if (last_render_time_in_us > state->time.max_render_time_in_us) {
  791. /* Enter Below the Range */
  792. if (!state->btr.btr_active &&
  793. core_freesync->map[map_index].caps->btr_supported) {
  794. state->btr.program_btr = true;
  795. state->btr.btr_active = true;
  796. /* Enter Fixed Refresh mode */
  797. } else if (!state->fixed_refresh.fixed_refresh_active &&
  798. !core_freesync->map[map_index].caps->btr_supported) {
  799. state->fixed_refresh.program_fixed_refresh = true;
  800. state->fixed_refresh.fixed_refresh_active = true;
  801. }
  802. }
  803. /* When Below the Range is active, must react on every frame */
  804. if (state->btr.btr_active)
  805. state->btr.program_btr = true;
  806. }
  807. static void apply_below_the_range(struct core_freesync *core_freesync,
  808. const struct dc_stream *stream, unsigned int map_index,
  809. unsigned int last_render_time_in_us)
  810. {
  811. unsigned int inserted_frame_duration_in_us = 0;
  812. unsigned int mid_point_frames_ceil = 0;
  813. unsigned int mid_point_frames_floor = 0;
  814. unsigned int frame_time_in_us = 0;
  815. unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
  816. unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
  817. unsigned int frames_to_insert = 0;
  818. unsigned int inserted_frame_v_total = 0;
  819. unsigned int vmin = 0, vmax = 0;
  820. unsigned int min_frame_duration_in_ns = 0;
  821. struct freesync_state *state = &core_freesync->map[map_index].state;
  822. if (!state->btr.program_btr)
  823. return;
  824. state->btr.program_btr = false;
  825. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  826. (1000000000ULL * 1000000),
  827. state->nominal_refresh_rate_in_micro_hz)));
  828. /* Program BTR */
  829. /* BTR set to "not active" so disengage */
  830. if (!state->btr.btr_active)
  831. /* Restore FreeSync */
  832. set_freesync_on_streams(core_freesync, &stream, 1);
  833. /* BTR set to "active" so engage */
  834. else {
  835. /* Calculate number of midPoint frames that could fit within
  836. * the render time interval- take ceil of this value
  837. */
  838. mid_point_frames_ceil = (last_render_time_in_us +
  839. state->btr.mid_point_in_us- 1) /
  840. state->btr.mid_point_in_us;
  841. if (mid_point_frames_ceil > 0) {
  842. frame_time_in_us = last_render_time_in_us /
  843. mid_point_frames_ceil;
  844. delta_from_mid_point_in_us_1 = (state->btr.mid_point_in_us >
  845. frame_time_in_us) ?
  846. (state->btr.mid_point_in_us - frame_time_in_us):
  847. (frame_time_in_us - state->btr.mid_point_in_us);
  848. }
  849. /* Calculate number of midPoint frames that could fit within
  850. * the render time interval- take floor of this value
  851. */
  852. mid_point_frames_floor = last_render_time_in_us /
  853. state->btr.mid_point_in_us;
  854. if (mid_point_frames_floor > 0) {
  855. frame_time_in_us = last_render_time_in_us /
  856. mid_point_frames_floor;
  857. delta_from_mid_point_in_us_2 = (state->btr.mid_point_in_us >
  858. frame_time_in_us) ?
  859. (state->btr.mid_point_in_us - frame_time_in_us):
  860. (frame_time_in_us - state->btr.mid_point_in_us);
  861. }
  862. /* Choose number of frames to insert based on how close it
  863. * can get to the mid point of the variable range.
  864. */
  865. if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2)
  866. frames_to_insert = mid_point_frames_ceil;
  867. else
  868. frames_to_insert = mid_point_frames_floor;
  869. /* Either we've calculated the number of frames to insert,
  870. * or we need to insert min duration frames
  871. */
  872. if (frames_to_insert > 0)
  873. inserted_frame_duration_in_us = last_render_time_in_us /
  874. frames_to_insert;
  875. if (inserted_frame_duration_in_us <
  876. state->time.min_render_time_in_us)
  877. inserted_frame_duration_in_us =
  878. state->time.min_render_time_in_us;
  879. /* We need the v_total_min from capability */
  880. calc_vmin_vmax(core_freesync, stream, &vmin, &vmax);
  881. inserted_frame_v_total = vmin;
  882. if (min_frame_duration_in_ns / 1000)
  883. inserted_frame_v_total = inserted_frame_duration_in_us *
  884. vmin / (min_frame_duration_in_ns / 1000);
  885. /* Set length of inserted frames as v_total_max*/
  886. vmax = inserted_frame_v_total;
  887. /* Program V_TOTAL */
  888. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  889. core_freesync->dc, &stream,
  890. 1, vmin,
  891. vmax);
  892. /* Cache the calculated variables */
  893. state->btr.inserted_frame_duration_in_us =
  894. inserted_frame_duration_in_us;
  895. state->btr.frames_to_insert = frames_to_insert;
  896. state->btr.frame_counter = frames_to_insert;
  897. }
  898. }
  899. static void apply_fixed_refresh(struct core_freesync *core_freesync,
  900. const struct dc_stream *stream, unsigned int map_index)
  901. {
  902. unsigned int vmin = 0, vmax = 0;
  903. struct freesync_state *state = &core_freesync->map[map_index].state;
  904. if (!state->fixed_refresh.program_fixed_refresh)
  905. return;
  906. state->fixed_refresh.program_fixed_refresh = false;
  907. /* Program Fixed Refresh */
  908. /* Fixed Refresh set to "not active" so disengage */
  909. if (!state->fixed_refresh.fixed_refresh_active) {
  910. set_freesync_on_streams(core_freesync, &stream, 1);
  911. /* Fixed Refresh set to "active" so engage (fix to max) */
  912. } else {
  913. calc_vmin_vmax(core_freesync, stream, &vmin, &vmax);
  914. vmax = vmin;
  915. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  916. core_freesync->dc, &stream,
  917. 1, vmin,
  918. vmax);
  919. }
  920. }
  921. void mod_freesync_pre_update_plane_addresses(struct mod_freesync *mod_freesync,
  922. const struct dc_stream **streams, int num_streams,
  923. unsigned int curr_time_stamp_in_us)
  924. {
  925. unsigned int stream_index, map_index, last_render_time_in_us = 0;
  926. struct core_freesync *core_freesync = NULL;
  927. if (mod_freesync == NULL)
  928. return;
  929. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  930. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  931. map_index = map_index_from_stream(core_freesync,
  932. streams[stream_index]);
  933. if (core_freesync->map[map_index].caps->supported) {
  934. last_render_time_in_us = curr_time_stamp_in_us -
  935. core_freesync->map[map_index].state.time.
  936. prev_time_stamp_in_us;
  937. /* Add the timestamps to the cache and determine
  938. * whether BTR program is required
  939. */
  940. update_timestamps(core_freesync, streams[stream_index],
  941. map_index, last_render_time_in_us);
  942. if (core_freesync->map[map_index].state.fullscreen &&
  943. core_freesync->map[map_index].user_enable.
  944. enable_for_gaming) {
  945. if (core_freesync->map[map_index].caps->btr_supported) {
  946. apply_below_the_range(core_freesync,
  947. streams[stream_index], map_index,
  948. last_render_time_in_us);
  949. } else {
  950. apply_fixed_refresh(core_freesync,
  951. streams[stream_index], map_index);
  952. }
  953. }
  954. core_freesync->map[map_index].state.time.
  955. prev_time_stamp_in_us = curr_time_stamp_in_us;
  956. }
  957. }
  958. }