freesync.c 42 KB

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