freesync.c 34 KB

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