freesync.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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, inserted_frame_v_total = 0;
  526. unsigned int min_frame_duration_in_ns, vmax, vmin = 0;
  527. struct freesync_state *state;
  528. struct core_freesync *core_freesync = NULL;
  529. if (mod_freesync == NULL)
  530. return;
  531. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  532. if (core_freesync->num_entities == 0)
  533. return;
  534. index = map_index_from_stream(core_freesync,
  535. streams[0]);
  536. if (core_freesync->map[index].caps->supported == false)
  537. return;
  538. state = &core_freesync->map[index].state;
  539. /* Below the Range Logic */
  540. /* Only execute if in fullscreen mode */
  541. if (state->fullscreen == true &&
  542. core_freesync->map[index].user_enable.enable_for_gaming &&
  543. core_freesync->map[index].caps->btr_supported &&
  544. state->btr.btr_active) {
  545. /* TODO: pass in flag for Pre-DCE12 ASIC
  546. * in order for frame variable duration to take affect,
  547. * it needs to be done one VSYNC early, which is at
  548. * frameCounter == 1.
  549. * For DCE12 and newer updates to V_TOTAL_MIN/MAX
  550. * will take affect on current frame
  551. */
  552. if (state->btr.frames_to_insert == state->btr.frame_counter) {
  553. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  554. (1000000000ULL * 1000000),
  555. state->nominal_refresh_rate_in_micro_hz)));
  556. calc_vmin_vmax(core_freesync, *streams, &vmin, &vmax);
  557. inserted_frame_v_total = vmin;
  558. if (min_frame_duration_in_ns / 1000)
  559. inserted_frame_v_total =
  560. state->btr.inserted_frame_duration_in_us *
  561. vmin / (min_frame_duration_in_ns / 1000);
  562. /* Set length of inserted frames as v_total_max*/
  563. vmax = inserted_frame_v_total;
  564. vmin = inserted_frame_v_total;
  565. /* Program V_TOTAL */
  566. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  567. core_freesync->dc, streams,
  568. num_streams, vmin, vmax);
  569. }
  570. if (state->btr.frame_counter > 0)
  571. state->btr.frame_counter--;
  572. /* Restore FreeSync */
  573. if (state->btr.frame_counter == 0)
  574. set_freesync_on_streams(core_freesync, streams, num_streams);
  575. }
  576. /* If in fullscreen freesync mode or in video, do not program
  577. * static screen ramp values
  578. */
  579. if (state->fullscreen == true || state->video == true) {
  580. state->static_ramp.ramp_is_active = false;
  581. return;
  582. }
  583. /* Gradual Static Screen Ramping Logic */
  584. /* Execute if ramp is active and user enabled freesync static screen*/
  585. if (state->static_ramp.ramp_is_active &&
  586. core_freesync->map[index].user_enable.enable_for_static) {
  587. calc_v_total_for_static_ramp(core_freesync, streams[0],
  588. index, &v_total);
  589. /* Update the freesync context for the stream */
  590. update_stream_freesync_context(core_freesync, streams[0]);
  591. /* Program static screen ramp values */
  592. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  593. core_freesync->dc, streams,
  594. num_streams, v_total,
  595. v_total);
  596. }
  597. }
  598. void mod_freesync_update_state(struct mod_freesync *mod_freesync,
  599. const struct dc_stream **streams, int num_streams,
  600. struct mod_freesync_params *freesync_params)
  601. {
  602. bool freesync_program_required = false;
  603. unsigned int stream_index;
  604. struct freesync_state *state;
  605. struct core_freesync *core_freesync = NULL;
  606. if (mod_freesync == NULL)
  607. return;
  608. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  609. if (core_freesync->num_entities == 0)
  610. return;
  611. for(stream_index = 0; stream_index < num_streams; stream_index++) {
  612. unsigned int map_index = map_index_from_stream(core_freesync,
  613. streams[stream_index]);
  614. state = &core_freesync->map[map_index].state;
  615. switch (freesync_params->state){
  616. case FREESYNC_STATE_FULLSCREEN:
  617. state->fullscreen = freesync_params->enable;
  618. freesync_program_required = true;
  619. state->windowed_fullscreen =
  620. freesync_params->windowed_fullscreen;
  621. break;
  622. case FREESYNC_STATE_STATIC_SCREEN:
  623. /* Static screen ramp is only enabled for embedded
  624. * panels. Also change core variables only if there
  625. * is a change.
  626. */
  627. if (dc_is_embedded_signal(
  628. streams[stream_index]->sink->sink_signal) &&
  629. state->static_screen !=
  630. freesync_params->enable) {
  631. /* Change the state flag */
  632. state->static_screen = freesync_params->enable;
  633. /* Change static screen ramp variables */
  634. set_static_ramp_variables(core_freesync,
  635. map_index,
  636. freesync_params->enable);
  637. }
  638. /* We program the ramp starting next VUpdate */
  639. break;
  640. case FREESYNC_STATE_VIDEO:
  641. /* Change core variables only if there is a change*/
  642. if(freesync_params->update_duration_in_ns !=
  643. state->time.update_duration_in_ns) {
  644. state->video = freesync_params->enable;
  645. state->time.update_duration_in_ns =
  646. freesync_params->update_duration_in_ns;
  647. freesync_program_required = true;
  648. }
  649. break;
  650. case FREESYNC_STATE_NONE:
  651. /* handle here to avoid warning */
  652. break;
  653. }
  654. }
  655. if (freesync_program_required)
  656. /* Program freesync according to current state*/
  657. set_freesync_on_streams(core_freesync, streams, num_streams);
  658. }
  659. bool mod_freesync_get_state(struct mod_freesync *mod_freesync,
  660. const struct dc_stream *stream,
  661. struct mod_freesync_params *freesync_params)
  662. {
  663. unsigned int index = 0;
  664. struct core_freesync *core_freesync = NULL;
  665. if (mod_freesync == NULL)
  666. return false;
  667. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  668. index = map_index_from_stream(core_freesync, stream);
  669. if (core_freesync->map[index].state.fullscreen) {
  670. freesync_params->state = FREESYNC_STATE_FULLSCREEN;
  671. freesync_params->enable = true;
  672. } else if (core_freesync->map[index].state.static_screen) {
  673. freesync_params->state = FREESYNC_STATE_STATIC_SCREEN;
  674. freesync_params->enable = true;
  675. } else if (core_freesync->map[index].state.video) {
  676. freesync_params->state = FREESYNC_STATE_VIDEO;
  677. freesync_params->enable = true;
  678. } else {
  679. freesync_params->state = FREESYNC_STATE_NONE;
  680. freesync_params->enable = false;
  681. }
  682. freesync_params->update_duration_in_ns =
  683. core_freesync->map[index].state.time.update_duration_in_ns;
  684. freesync_params->windowed_fullscreen =
  685. core_freesync->map[index].state.windowed_fullscreen;
  686. return true;
  687. }
  688. bool mod_freesync_set_user_enable(struct mod_freesync *mod_freesync,
  689. const struct dc_stream **streams, int num_streams,
  690. struct mod_freesync_user_enable *user_enable)
  691. {
  692. unsigned int stream_index, map_index;
  693. int persistent_data = 0;
  694. struct persistent_data_flag flag;
  695. struct core_dc *core_dc = NULL;
  696. struct core_freesync *core_freesync = NULL;
  697. if (mod_freesync == NULL)
  698. return false;
  699. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  700. core_dc = DC_TO_CORE(core_freesync->dc);
  701. flag.save_per_edid = true;
  702. flag.save_per_link = false;
  703. for(stream_index = 0; stream_index < num_streams;
  704. stream_index++){
  705. map_index = map_index_from_stream(core_freesync,
  706. streams[stream_index]);
  707. core_freesync->map[map_index].user_enable = *user_enable;
  708. /* Write persistent data in registry*/
  709. if (core_freesync->map[map_index].user_enable.
  710. enable_for_gaming)
  711. persistent_data = persistent_data | 1;
  712. if (core_freesync->map[map_index].user_enable.
  713. enable_for_static)
  714. persistent_data = persistent_data | 2;
  715. if (core_freesync->map[map_index].user_enable.
  716. enable_for_video)
  717. persistent_data = persistent_data | 4;
  718. dm_write_persistent_data(core_dc->ctx,
  719. streams[stream_index]->sink,
  720. FREESYNC_REGISTRY_NAME,
  721. "userenable",
  722. &persistent_data,
  723. sizeof(int),
  724. &flag);
  725. }
  726. set_freesync_on_streams(core_freesync, streams, num_streams);
  727. return true;
  728. }
  729. bool mod_freesync_get_user_enable(struct mod_freesync *mod_freesync,
  730. const struct dc_stream *stream,
  731. struct mod_freesync_user_enable *user_enable)
  732. {
  733. unsigned int index = 0;
  734. struct core_freesync *core_freesync = NULL;
  735. if (mod_freesync == NULL)
  736. return false;
  737. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  738. index = map_index_from_stream(core_freesync, stream);
  739. *user_enable = core_freesync->map[index].user_enable;
  740. return true;
  741. }
  742. void mod_freesync_notify_mode_change(struct mod_freesync *mod_freesync,
  743. const struct dc_stream **streams, int num_streams)
  744. {
  745. unsigned int stream_index, map_index;
  746. unsigned min_frame_duration_in_ns, max_frame_duration_in_ns;
  747. struct freesync_state *state;
  748. struct core_freesync *core_freesync = NULL;
  749. if (mod_freesync == NULL)
  750. return;
  751. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  752. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  753. map_index = map_index_from_stream(core_freesync,
  754. streams[stream_index]);
  755. state = &core_freesync->map[map_index].state;
  756. if (core_freesync->map[map_index].caps->supported) {
  757. /* Update the field rate for new timing */
  758. unsigned long long temp;
  759. temp = streams[stream_index]->timing.pix_clk_khz;
  760. temp *= 1000ULL * 1000ULL * 1000ULL;
  761. temp = div_u64(temp, streams[stream_index]->timing.h_total);
  762. temp = div_u64(temp, streams[stream_index]->timing.v_total);
  763. state->nominal_refresh_rate_in_micro_hz = (unsigned int) temp;
  764. /* Update the stream */
  765. update_stream(core_freesync, streams[stream_index]);
  766. /* Determine whether BTR can be supported */
  767. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  768. (1000000000ULL * 1000000),
  769. state->nominal_refresh_rate_in_micro_hz)));
  770. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  771. (1000000000ULL * 1000000),
  772. core_freesync->map[map_index].caps->min_refresh_in_micro_hz)));
  773. if (max_frame_duration_in_ns >=
  774. 2 * min_frame_duration_in_ns)
  775. core_freesync->map[map_index].caps->btr_supported = true;
  776. else
  777. core_freesync->map[map_index].caps->btr_supported = false;
  778. /* Cache the time variables */
  779. state->time.max_render_time_in_us =
  780. max_frame_duration_in_ns / 1000;
  781. state->time.min_render_time_in_us =
  782. min_frame_duration_in_ns / 1000;
  783. state->btr.mid_point_in_us =
  784. (max_frame_duration_in_ns +
  785. min_frame_duration_in_ns) / 2000;
  786. }
  787. }
  788. /* Program freesync according to current state*/
  789. set_freesync_on_streams(core_freesync, streams, num_streams);
  790. }
  791. /* Add the timestamps to the cache and determine whether BTR programming
  792. * is required, depending on the times calculated
  793. */
  794. static void update_timestamps(struct core_freesync *core_freesync,
  795. const struct dc_stream *stream, unsigned int map_index,
  796. unsigned int last_render_time_in_us)
  797. {
  798. struct freesync_state *state = &core_freesync->map[map_index].state;
  799. state->time.render_times[state->time.render_times_index] =
  800. last_render_time_in_us;
  801. state->time.render_times_index++;
  802. if (state->time.render_times_index >= RENDER_TIMES_MAX_COUNT)
  803. state->time.render_times_index = 0;
  804. if (last_render_time_in_us + BTR_EXIT_MARGIN <
  805. state->time.max_render_time_in_us) {
  806. /* Exit Below the Range */
  807. if (state->btr.btr_active) {
  808. state->btr.program_btr = true;
  809. state->btr.btr_active = false;
  810. state->btr.frame_counter = 0;
  811. /* Exit Fixed Refresh mode */
  812. } else if (state->fixed_refresh.fixed_refresh_active) {
  813. state->fixed_refresh.program_fixed_refresh = true;
  814. state->fixed_refresh.fixed_refresh_active = false;
  815. }
  816. } else if (last_render_time_in_us > state->time.max_render_time_in_us) {
  817. /* Enter Below the Range */
  818. if (!state->btr.btr_active &&
  819. core_freesync->map[map_index].caps->btr_supported) {
  820. state->btr.program_btr = true;
  821. state->btr.btr_active = true;
  822. /* Enter Fixed Refresh mode */
  823. } else if (!state->fixed_refresh.fixed_refresh_active &&
  824. !core_freesync->map[map_index].caps->btr_supported) {
  825. state->fixed_refresh.program_fixed_refresh = true;
  826. state->fixed_refresh.fixed_refresh_active = true;
  827. }
  828. }
  829. /* When Below the Range is active, must react on every frame */
  830. if (state->btr.btr_active)
  831. state->btr.program_btr = true;
  832. }
  833. static void apply_below_the_range(struct core_freesync *core_freesync,
  834. const struct dc_stream *stream, unsigned int map_index,
  835. unsigned int last_render_time_in_us)
  836. {
  837. unsigned int inserted_frame_duration_in_us = 0;
  838. unsigned int mid_point_frames_ceil = 0;
  839. unsigned int mid_point_frames_floor = 0;
  840. unsigned int frame_time_in_us = 0;
  841. unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
  842. unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
  843. unsigned int frames_to_insert = 0;
  844. unsigned int min_frame_duration_in_ns = 0;
  845. struct freesync_state *state = &core_freesync->map[map_index].state;
  846. if (!state->btr.program_btr)
  847. return;
  848. state->btr.program_btr = false;
  849. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  850. (1000000000ULL * 1000000),
  851. state->nominal_refresh_rate_in_micro_hz)));
  852. /* Program BTR */
  853. /* BTR set to "not active" so disengage */
  854. if (!state->btr.btr_active)
  855. /* Restore FreeSync */
  856. set_freesync_on_streams(core_freesync, &stream, 1);
  857. /* BTR set to "active" so engage */
  858. else {
  859. /* Calculate number of midPoint frames that could fit within
  860. * the render time interval- take ceil of this value
  861. */
  862. mid_point_frames_ceil = (last_render_time_in_us +
  863. state->btr.mid_point_in_us- 1) /
  864. state->btr.mid_point_in_us;
  865. if (mid_point_frames_ceil > 0) {
  866. frame_time_in_us = last_render_time_in_us /
  867. mid_point_frames_ceil;
  868. delta_from_mid_point_in_us_1 = (state->btr.mid_point_in_us >
  869. frame_time_in_us) ?
  870. (state->btr.mid_point_in_us - frame_time_in_us):
  871. (frame_time_in_us - state->btr.mid_point_in_us);
  872. }
  873. /* Calculate number of midPoint frames that could fit within
  874. * the render time interval- take floor of this value
  875. */
  876. mid_point_frames_floor = last_render_time_in_us /
  877. state->btr.mid_point_in_us;
  878. if (mid_point_frames_floor > 0) {
  879. frame_time_in_us = last_render_time_in_us /
  880. mid_point_frames_floor;
  881. delta_from_mid_point_in_us_2 = (state->btr.mid_point_in_us >
  882. frame_time_in_us) ?
  883. (state->btr.mid_point_in_us - frame_time_in_us):
  884. (frame_time_in_us - state->btr.mid_point_in_us);
  885. }
  886. /* Choose number of frames to insert based on how close it
  887. * can get to the mid point of the variable range.
  888. */
  889. if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2)
  890. frames_to_insert = mid_point_frames_ceil;
  891. else
  892. frames_to_insert = mid_point_frames_floor;
  893. /* Either we've calculated the number of frames to insert,
  894. * or we need to insert min duration frames
  895. */
  896. if (frames_to_insert > 0)
  897. inserted_frame_duration_in_us = last_render_time_in_us /
  898. frames_to_insert;
  899. if (inserted_frame_duration_in_us <
  900. state->time.min_render_time_in_us)
  901. inserted_frame_duration_in_us =
  902. state->time.min_render_time_in_us;
  903. /* Cache the calculated variables */
  904. state->btr.inserted_frame_duration_in_us =
  905. inserted_frame_duration_in_us;
  906. state->btr.frames_to_insert = frames_to_insert;
  907. state->btr.frame_counter = frames_to_insert;
  908. }
  909. }
  910. static void apply_fixed_refresh(struct core_freesync *core_freesync,
  911. const struct dc_stream *stream, unsigned int map_index)
  912. {
  913. unsigned int vmin = 0, vmax = 0;
  914. struct freesync_state *state = &core_freesync->map[map_index].state;
  915. if (!state->fixed_refresh.program_fixed_refresh)
  916. return;
  917. state->fixed_refresh.program_fixed_refresh = false;
  918. /* Program Fixed Refresh */
  919. /* Fixed Refresh set to "not active" so disengage */
  920. if (!state->fixed_refresh.fixed_refresh_active) {
  921. set_freesync_on_streams(core_freesync, &stream, 1);
  922. /* Fixed Refresh set to "active" so engage (fix to max) */
  923. } else {
  924. calc_vmin_vmax(core_freesync, stream, &vmin, &vmax);
  925. vmax = vmin;
  926. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  927. core_freesync->dc, &stream,
  928. 1, vmin,
  929. vmax);
  930. }
  931. }
  932. void mod_freesync_pre_update_plane_addresses(struct mod_freesync *mod_freesync,
  933. const struct dc_stream **streams, int num_streams,
  934. unsigned int curr_time_stamp_in_us)
  935. {
  936. unsigned int stream_index, map_index, last_render_time_in_us = 0;
  937. struct core_freesync *core_freesync = NULL;
  938. if (mod_freesync == NULL)
  939. return;
  940. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  941. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  942. map_index = map_index_from_stream(core_freesync,
  943. streams[stream_index]);
  944. if (core_freesync->map[map_index].caps->supported) {
  945. last_render_time_in_us = curr_time_stamp_in_us -
  946. core_freesync->map[map_index].state.time.
  947. prev_time_stamp_in_us;
  948. /* Add the timestamps to the cache and determine
  949. * whether BTR program is required
  950. */
  951. update_timestamps(core_freesync, streams[stream_index],
  952. map_index, last_render_time_in_us);
  953. if (core_freesync->map[map_index].state.fullscreen &&
  954. core_freesync->map[map_index].user_enable.
  955. enable_for_gaming) {
  956. if (core_freesync->map[map_index].caps->btr_supported) {
  957. apply_below_the_range(core_freesync,
  958. streams[stream_index], map_index,
  959. last_render_time_in_us);
  960. } else {
  961. apply_fixed_refresh(core_freesync,
  962. streams[stream_index], map_index);
  963. }
  964. }
  965. core_freesync->map[map_index].state.time.
  966. prev_time_stamp_in_us = curr_time_stamp_in_us;
  967. }
  968. }
  969. }