freesync.c 42 KB

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