freesync.c 45 KB

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