freesync.c 40 KB

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