freesync.c 42 KB

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