freesync.c 43 KB

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