freesync.c 42 KB

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