freesync.c 45 KB

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