freesync.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * Copyright 2016 Advanced Micro Devices, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: AMD
  23. *
  24. */
  25. #include "dm_services.h"
  26. #include "dc.h"
  27. #include "mod_freesync.h"
  28. #include "core_types.h"
  29. #define MOD_FREESYNC_MAX_CONCURRENT_STREAMS 32
  30. #define MIN_REFRESH_RANGE_IN_US 10000000
  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 10
  35. /* Threshold to exit BTR (to avoid frequent enter-exits at the lower limit) */
  36. #define BTR_EXIT_MARGIN 2000
  37. /* Number of consecutive frames to check before entering/exiting fixed refresh*/
  38. #define FIXED_REFRESH_ENTER_FRAME_COUNT 5
  39. #define FIXED_REFRESH_EXIT_FRAME_COUNT 5
  40. struct core_freesync {
  41. struct mod_freesync public;
  42. struct dc *dc;
  43. };
  44. #define MOD_FREESYNC_TO_CORE(mod_freesync)\
  45. container_of(mod_freesync, struct core_freesync, public)
  46. struct mod_freesync *mod_freesync_create(struct dc *dc)
  47. {
  48. struct core_freesync *core_freesync =
  49. kzalloc(sizeof(struct core_freesync), GFP_KERNEL);
  50. if (core_freesync == NULL)
  51. goto fail_alloc_context;
  52. if (dc == NULL)
  53. goto fail_construct;
  54. core_freesync->dc = dc;
  55. return &core_freesync->public;
  56. fail_construct:
  57. kfree(core_freesync);
  58. fail_alloc_context:
  59. return NULL;
  60. }
  61. void mod_freesync_destroy(struct mod_freesync *mod_freesync)
  62. {
  63. struct core_freesync *core_freesync = NULL;
  64. if (mod_freesync == NULL)
  65. return;
  66. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  67. kfree(core_freesync);
  68. }
  69. #if 0 /* unused currently */
  70. static unsigned int calc_refresh_in_uhz_from_duration(
  71. unsigned int duration_in_ns)
  72. {
  73. unsigned int refresh_in_uhz =
  74. ((unsigned int)(div64_u64((1000000000ULL * 1000000),
  75. duration_in_ns)));
  76. return refresh_in_uhz;
  77. }
  78. #endif
  79. static unsigned int calc_duration_in_us_from_refresh_in_uhz(
  80. unsigned int refresh_in_uhz)
  81. {
  82. unsigned int duration_in_us =
  83. ((unsigned int)(div64_u64((1000000000ULL * 1000),
  84. refresh_in_uhz)));
  85. return duration_in_us;
  86. }
  87. static unsigned int calc_duration_in_us_from_v_total(
  88. const struct dc_stream_state *stream,
  89. const struct mod_vrr_params *in_vrr,
  90. unsigned int v_total)
  91. {
  92. unsigned int duration_in_us =
  93. (unsigned int)(div64_u64(((unsigned long long)(v_total)
  94. * 1000) * stream->timing.h_total,
  95. stream->timing.pix_clk_khz));
  96. return duration_in_us;
  97. }
  98. static unsigned int calc_v_total_from_refresh(
  99. const struct dc_stream_state *stream,
  100. unsigned int refresh_in_uhz)
  101. {
  102. unsigned int v_total = stream->timing.v_total;
  103. unsigned int frame_duration_in_ns;
  104. frame_duration_in_ns =
  105. ((unsigned int)(div64_u64((1000000000ULL * 1000000),
  106. refresh_in_uhz)));
  107. v_total = div64_u64(div64_u64(((unsigned long long)(
  108. frame_duration_in_ns) * stream->timing.pix_clk_khz),
  109. stream->timing.h_total), 1000000);
  110. /* v_total cannot be less than nominal */
  111. if (v_total < stream->timing.v_total) {
  112. ASSERT(v_total < stream->timing.v_total);
  113. v_total = stream->timing.v_total;
  114. }
  115. return v_total;
  116. }
  117. static unsigned int calc_v_total_from_duration(
  118. const struct dc_stream_state *stream,
  119. const struct mod_vrr_params *vrr,
  120. unsigned int duration_in_us)
  121. {
  122. unsigned int v_total = 0;
  123. if (duration_in_us < vrr->min_duration_in_us)
  124. duration_in_us = vrr->min_duration_in_us;
  125. if (duration_in_us > vrr->max_duration_in_us)
  126. duration_in_us = vrr->max_duration_in_us;
  127. v_total = div64_u64(div64_u64(((unsigned long long)(
  128. duration_in_us) * stream->timing.pix_clk_khz),
  129. stream->timing.h_total), 1000);
  130. /* v_total cannot be less than nominal */
  131. if (v_total < stream->timing.v_total) {
  132. ASSERT(v_total < stream->timing.v_total);
  133. v_total = stream->timing.v_total;
  134. }
  135. return v_total;
  136. }
  137. static void update_v_total_for_static_ramp(
  138. struct core_freesync *core_freesync,
  139. const struct dc_stream_state *stream,
  140. struct mod_vrr_params *in_out_vrr)
  141. {
  142. unsigned int v_total = 0;
  143. unsigned int current_duration_in_us =
  144. calc_duration_in_us_from_v_total(
  145. stream, in_out_vrr,
  146. in_out_vrr->adjust.v_total_max);
  147. unsigned int target_duration_in_us =
  148. calc_duration_in_us_from_refresh_in_uhz(
  149. in_out_vrr->fixed.target_refresh_in_uhz);
  150. bool ramp_direction_is_up = (current_duration_in_us >
  151. target_duration_in_us) ? true : false;
  152. /* Calc ratio between new and current frame duration with 3 digit */
  153. unsigned int frame_duration_ratio = div64_u64(1000000,
  154. (1000 + div64_u64(((unsigned long long)(
  155. STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
  156. current_duration_in_us),
  157. 1000000)));
  158. /* Calculate delta between new and current frame duration in us */
  159. unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
  160. current_duration_in_us) *
  161. (1000 - frame_duration_ratio)), 1000);
  162. /* Adjust frame duration delta based on ratio between current and
  163. * standard frame duration (frame duration at 60 Hz refresh rate).
  164. */
  165. unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
  166. frame_duration_delta) * current_duration_in_us), 16666);
  167. /* Going to a higher refresh rate (lower frame duration) */
  168. if (ramp_direction_is_up) {
  169. /* reduce frame duration */
  170. current_duration_in_us -= ramp_rate_interpolated;
  171. /* adjust for frame duration below min */
  172. if (current_duration_in_us <= target_duration_in_us) {
  173. in_out_vrr->fixed.ramping_active = false;
  174. in_out_vrr->fixed.ramping_done = true;
  175. current_duration_in_us =
  176. calc_duration_in_us_from_refresh_in_uhz(
  177. in_out_vrr->fixed.target_refresh_in_uhz);
  178. }
  179. /* Going to a lower refresh rate (larger frame duration) */
  180. } else {
  181. /* increase frame duration */
  182. current_duration_in_us += ramp_rate_interpolated;
  183. /* adjust for frame duration above max */
  184. if (current_duration_in_us >= target_duration_in_us) {
  185. in_out_vrr->fixed.ramping_active = false;
  186. in_out_vrr->fixed.ramping_done = true;
  187. current_duration_in_us =
  188. calc_duration_in_us_from_refresh_in_uhz(
  189. in_out_vrr->fixed.target_refresh_in_uhz);
  190. }
  191. }
  192. v_total = div64_u64(div64_u64(((unsigned long long)(
  193. current_duration_in_us) * stream->timing.pix_clk_khz),
  194. stream->timing.h_total), 1000);
  195. in_out_vrr->adjust.v_total_min = v_total;
  196. in_out_vrr->adjust.v_total_max = v_total;
  197. }
  198. static void apply_below_the_range(struct core_freesync *core_freesync,
  199. const struct dc_stream_state *stream,
  200. unsigned int last_render_time_in_us,
  201. struct mod_vrr_params *in_out_vrr)
  202. {
  203. unsigned int inserted_frame_duration_in_us = 0;
  204. unsigned int mid_point_frames_ceil = 0;
  205. unsigned int mid_point_frames_floor = 0;
  206. unsigned int frame_time_in_us = 0;
  207. unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
  208. unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
  209. unsigned int frames_to_insert = 0;
  210. unsigned int min_frame_duration_in_ns = 0;
  211. unsigned int max_render_time_in_us = in_out_vrr->max_duration_in_us;
  212. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  213. (1000000000ULL * 1000000),
  214. in_out_vrr->max_refresh_in_uhz)));
  215. /* Program BTR */
  216. if (last_render_time_in_us + BTR_EXIT_MARGIN < max_render_time_in_us) {
  217. /* Exit Below the Range */
  218. if (in_out_vrr->btr.btr_active) {
  219. in_out_vrr->btr.frame_counter = 0;
  220. in_out_vrr->btr.btr_active = false;
  221. /* Exit Fixed Refresh mode */
  222. } else if (in_out_vrr->fixed.fixed_active) {
  223. in_out_vrr->fixed.frame_counter++;
  224. if (in_out_vrr->fixed.frame_counter >
  225. FIXED_REFRESH_EXIT_FRAME_COUNT) {
  226. in_out_vrr->fixed.frame_counter = 0;
  227. in_out_vrr->fixed.fixed_active = false;
  228. }
  229. }
  230. } else if (last_render_time_in_us > max_render_time_in_us) {
  231. /* Enter Below the Range */
  232. if (!in_out_vrr->btr.btr_active &&
  233. in_out_vrr->btr.btr_enabled) {
  234. in_out_vrr->btr.btr_active = true;
  235. /* Enter Fixed Refresh mode */
  236. } else if (!in_out_vrr->fixed.fixed_active &&
  237. !in_out_vrr->btr.btr_enabled) {
  238. in_out_vrr->fixed.frame_counter++;
  239. if (in_out_vrr->fixed.frame_counter >
  240. FIXED_REFRESH_ENTER_FRAME_COUNT) {
  241. in_out_vrr->fixed.frame_counter = 0;
  242. in_out_vrr->fixed.fixed_active = true;
  243. }
  244. }
  245. }
  246. /* BTR set to "not active" so disengage */
  247. if (!in_out_vrr->btr.btr_active) {
  248. in_out_vrr->btr.btr_active = false;
  249. in_out_vrr->btr.inserted_duration_in_us = 0;
  250. in_out_vrr->btr.frames_to_insert = 0;
  251. in_out_vrr->btr.frame_counter = 0;
  252. /* Restore FreeSync */
  253. in_out_vrr->adjust.v_total_min =
  254. calc_v_total_from_refresh(stream,
  255. in_out_vrr->max_refresh_in_uhz);
  256. in_out_vrr->adjust.v_total_max =
  257. calc_v_total_from_refresh(stream,
  258. in_out_vrr->min_refresh_in_uhz);
  259. /* BTR set to "active" so engage */
  260. } else {
  261. /* Calculate number of midPoint frames that could fit within
  262. * the render time interval- take ceil of this value
  263. */
  264. mid_point_frames_ceil = (last_render_time_in_us +
  265. in_out_vrr->btr.mid_point_in_us - 1) /
  266. in_out_vrr->btr.mid_point_in_us;
  267. if (mid_point_frames_ceil > 0) {
  268. frame_time_in_us = last_render_time_in_us /
  269. mid_point_frames_ceil;
  270. delta_from_mid_point_in_us_1 =
  271. (in_out_vrr->btr.mid_point_in_us >
  272. frame_time_in_us) ?
  273. (in_out_vrr->btr.mid_point_in_us - frame_time_in_us) :
  274. (frame_time_in_us - in_out_vrr->btr.mid_point_in_us);
  275. }
  276. /* Calculate number of midPoint frames that could fit within
  277. * the render time interval- take floor of this value
  278. */
  279. mid_point_frames_floor = last_render_time_in_us /
  280. in_out_vrr->btr.mid_point_in_us;
  281. if (mid_point_frames_floor > 0) {
  282. frame_time_in_us = last_render_time_in_us /
  283. mid_point_frames_floor;
  284. delta_from_mid_point_in_us_2 =
  285. (in_out_vrr->btr.mid_point_in_us >
  286. frame_time_in_us) ?
  287. (in_out_vrr->btr.mid_point_in_us - frame_time_in_us) :
  288. (frame_time_in_us - in_out_vrr->btr.mid_point_in_us);
  289. }
  290. /* Choose number of frames to insert based on how close it
  291. * can get to the mid point of the variable range.
  292. */
  293. if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2)
  294. frames_to_insert = mid_point_frames_ceil;
  295. else
  296. frames_to_insert = mid_point_frames_floor;
  297. /* Either we've calculated the number of frames to insert,
  298. * or we need to insert min duration frames
  299. */
  300. if (frames_to_insert > 0)
  301. inserted_frame_duration_in_us = last_render_time_in_us /
  302. frames_to_insert;
  303. if (inserted_frame_duration_in_us <
  304. (1000000 / in_out_vrr->max_refresh_in_uhz))
  305. inserted_frame_duration_in_us =
  306. (1000000 / in_out_vrr->max_refresh_in_uhz);
  307. /* Cache the calculated variables */
  308. in_out_vrr->btr.inserted_duration_in_us =
  309. inserted_frame_duration_in_us;
  310. in_out_vrr->btr.frames_to_insert = frames_to_insert;
  311. in_out_vrr->btr.frame_counter = frames_to_insert;
  312. }
  313. }
  314. static void apply_fixed_refresh(struct core_freesync *core_freesync,
  315. const struct dc_stream_state *stream,
  316. unsigned int last_render_time_in_us,
  317. struct mod_vrr_params *in_out_vrr)
  318. {
  319. bool update = false;
  320. unsigned int max_render_time_in_us = in_out_vrr->max_duration_in_us;
  321. if (last_render_time_in_us + BTR_EXIT_MARGIN < max_render_time_in_us) {
  322. /* Exit Fixed Refresh mode */
  323. if (in_out_vrr->fixed.fixed_active) {
  324. in_out_vrr->fixed.frame_counter++;
  325. if (in_out_vrr->fixed.frame_counter >
  326. FIXED_REFRESH_EXIT_FRAME_COUNT) {
  327. in_out_vrr->fixed.frame_counter = 0;
  328. in_out_vrr->fixed.fixed_active = false;
  329. in_out_vrr->fixed.target_refresh_in_uhz = 0;
  330. update = true;
  331. }
  332. }
  333. } else if (last_render_time_in_us > max_render_time_in_us) {
  334. /* Enter Fixed Refresh mode */
  335. if (!in_out_vrr->fixed.fixed_active) {
  336. in_out_vrr->fixed.frame_counter++;
  337. if (in_out_vrr->fixed.frame_counter >
  338. FIXED_REFRESH_ENTER_FRAME_COUNT) {
  339. in_out_vrr->fixed.frame_counter = 0;
  340. in_out_vrr->fixed.fixed_active = true;
  341. in_out_vrr->fixed.target_refresh_in_uhz =
  342. in_out_vrr->max_refresh_in_uhz;
  343. update = true;
  344. }
  345. }
  346. }
  347. if (update) {
  348. if (in_out_vrr->fixed.fixed_active) {
  349. in_out_vrr->adjust.v_total_min =
  350. calc_v_total_from_refresh(
  351. stream, in_out_vrr->max_refresh_in_uhz);
  352. in_out_vrr->adjust.v_total_max =
  353. in_out_vrr->adjust.v_total_min;
  354. } else {
  355. in_out_vrr->adjust.v_total_min =
  356. calc_v_total_from_refresh(stream,
  357. in_out_vrr->max_refresh_in_uhz);
  358. in_out_vrr->adjust.v_total_max =
  359. calc_v_total_from_refresh(stream,
  360. in_out_vrr->min_refresh_in_uhz);
  361. }
  362. }
  363. }
  364. static bool vrr_settings_require_update(struct core_freesync *core_freesync,
  365. struct mod_freesync_config *in_config,
  366. unsigned int min_refresh_in_uhz,
  367. unsigned int max_refresh_in_uhz,
  368. struct mod_vrr_params *in_vrr)
  369. {
  370. if (in_vrr->state != in_config->state) {
  371. return true;
  372. } else if (in_vrr->state == VRR_STATE_ACTIVE_FIXED &&
  373. in_vrr->fixed.target_refresh_in_uhz !=
  374. in_config->min_refresh_in_uhz) {
  375. return true;
  376. } else if (in_vrr->min_refresh_in_uhz != min_refresh_in_uhz) {
  377. return true;
  378. } else if (in_vrr->max_refresh_in_uhz != max_refresh_in_uhz) {
  379. return true;
  380. }
  381. return false;
  382. }
  383. bool mod_freesync_get_vmin_vmax(struct mod_freesync *mod_freesync,
  384. const struct dc_stream_state *stream,
  385. unsigned int *vmin,
  386. unsigned int *vmax)
  387. {
  388. *vmin = stream->adjust.v_total_min;
  389. *vmax = stream->adjust.v_total_max;
  390. return true;
  391. }
  392. bool mod_freesync_get_v_position(struct mod_freesync *mod_freesync,
  393. struct dc_stream_state *stream,
  394. unsigned int *nom_v_pos,
  395. unsigned int *v_pos)
  396. {
  397. struct core_freesync *core_freesync = NULL;
  398. struct crtc_position position;
  399. if (mod_freesync == NULL)
  400. return false;
  401. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  402. if (dc_stream_get_crtc_position(core_freesync->dc, &stream, 1,
  403. &position.vertical_count,
  404. &position.nominal_vcount)) {
  405. *nom_v_pos = position.nominal_vcount;
  406. *v_pos = position.vertical_count;
  407. return true;
  408. }
  409. return false;
  410. }
  411. void mod_freesync_build_vrr_infopacket(struct mod_freesync *mod_freesync,
  412. const struct dc_stream_state *stream,
  413. const struct mod_vrr_params *vrr,
  414. struct dc_info_packet *infopacket)
  415. {
  416. /* SPD info packet for FreeSync */
  417. unsigned char checksum = 0;
  418. unsigned int idx, payload_size = 0;
  419. /* Check if Freesync is supported. Return if false. If true,
  420. * set the corresponding bit in the info packet
  421. */
  422. if (!vrr->supported)
  423. return;
  424. if (dc_is_hdmi_signal(stream->signal)) {
  425. /* HEADER */
  426. /* HB0 = Packet Type = 0x83 (Source Product
  427. * Descriptor InfoFrame)
  428. */
  429. infopacket->hb0 = DC_HDMI_INFOFRAME_TYPE_SPD;
  430. /* HB1 = Version = 0x01 */
  431. infopacket->hb1 = 0x01;
  432. /* HB2 = [Bits 7:5 = 0] [Bits 4:0 = Length = 0x08] */
  433. infopacket->hb2 = 0x08;
  434. payload_size = 0x08;
  435. } else if (dc_is_dp_signal(stream->signal)) {
  436. /* HEADER */
  437. /* HB0 = Secondary-data Packet ID = 0 - Only non-zero
  438. * when used to associate audio related info packets
  439. */
  440. infopacket->hb0 = 0x00;
  441. /* HB1 = Packet Type = 0x83 (Source Product
  442. * Descriptor InfoFrame)
  443. */
  444. infopacket->hb1 = DC_HDMI_INFOFRAME_TYPE_SPD;
  445. /* HB2 = [Bits 7:0 = Least significant eight bits -
  446. * For INFOFRAME, the value must be 1Bh]
  447. */
  448. infopacket->hb2 = 0x1B;
  449. /* HB3 = [Bits 7:2 = INFOFRAME SDP Version Number = 0x1]
  450. * [Bits 1:0 = Most significant two bits = 0x00]
  451. */
  452. infopacket->hb3 = 0x04;
  453. payload_size = 0x1B;
  454. }
  455. /* PB1 = 0x1A (24bit AMD IEEE OUI (0x00001A) - Byte 0) */
  456. infopacket->sb[1] = 0x1A;
  457. /* PB2 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 1) */
  458. infopacket->sb[2] = 0x00;
  459. /* PB3 = 0x00 (24bit AMD IEEE OUI (0x00001A) - Byte 2) */
  460. infopacket->sb[3] = 0x00;
  461. /* PB4 = Reserved */
  462. /* PB5 = Reserved */
  463. /* PB6 = [Bits 7:3 = Reserved] */
  464. /* PB6 = [Bit 0 = FreeSync Supported] */
  465. if (vrr->state != VRR_STATE_UNSUPPORTED)
  466. infopacket->sb[6] |= 0x01;
  467. /* PB6 = [Bit 1 = FreeSync Enabled] */
  468. if (vrr->state != VRR_STATE_DISABLED &&
  469. vrr->state != VRR_STATE_UNSUPPORTED)
  470. infopacket->sb[6] |= 0x02;
  471. /* PB6 = [Bit 2 = FreeSync Active] */
  472. if (vrr->state == VRR_STATE_ACTIVE_VARIABLE ||
  473. vrr->state == VRR_STATE_ACTIVE_FIXED)
  474. infopacket->sb[6] |= 0x04;
  475. /* PB7 = FreeSync Minimum refresh rate (Hz) */
  476. infopacket->sb[7] = (unsigned char)(vrr->min_refresh_in_uhz / 1000000);
  477. /* PB8 = FreeSync Maximum refresh rate (Hz)
  478. * Note: We should never go above the field rate of the mode timing set.
  479. */
  480. infopacket->sb[8] = (unsigned char)(vrr->max_refresh_in_uhz / 1000000);
  481. /* PB9 - PB27 = Reserved */
  482. /* Calculate checksum */
  483. checksum += infopacket->hb0;
  484. checksum += infopacket->hb1;
  485. checksum += infopacket->hb2;
  486. checksum += infopacket->hb3;
  487. for (idx = 1; idx <= payload_size; idx++)
  488. checksum += infopacket->sb[idx];
  489. /* PB0 = Checksum (one byte complement) */
  490. infopacket->sb[0] = (unsigned char)(0x100 - checksum);
  491. infopacket->valid = true;
  492. }
  493. void mod_freesync_build_vrr_params(struct mod_freesync *mod_freesync,
  494. const struct dc_stream_state *stream,
  495. struct mod_freesync_config *in_config,
  496. struct mod_vrr_params *in_out_vrr)
  497. {
  498. struct core_freesync *core_freesync = NULL;
  499. unsigned long long nominal_field_rate_in_uhz = 0;
  500. unsigned int refresh_range = 0;
  501. unsigned int min_refresh_in_uhz = 0;
  502. unsigned int max_refresh_in_uhz = 0;
  503. if (mod_freesync == NULL)
  504. return;
  505. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  506. /* Calculate nominal field rate for stream */
  507. nominal_field_rate_in_uhz =
  508. mod_freesync_calc_nominal_field_rate(stream);
  509. min_refresh_in_uhz = in_config->min_refresh_in_uhz;
  510. max_refresh_in_uhz = in_config->max_refresh_in_uhz;
  511. // Don't allow min > max
  512. if (min_refresh_in_uhz > max_refresh_in_uhz)
  513. min_refresh_in_uhz = max_refresh_in_uhz;
  514. // Full range may be larger than current video timing, so cap at nominal
  515. if (max_refresh_in_uhz > nominal_field_rate_in_uhz)
  516. max_refresh_in_uhz = nominal_field_rate_in_uhz;
  517. // Full range may be larger than current video timing, so cap at nominal
  518. if (min_refresh_in_uhz > nominal_field_rate_in_uhz)
  519. min_refresh_in_uhz = nominal_field_rate_in_uhz;
  520. if (!vrr_settings_require_update(core_freesync,
  521. in_config, min_refresh_in_uhz, max_refresh_in_uhz,
  522. in_out_vrr))
  523. return;
  524. in_out_vrr->state = in_config->state;
  525. if (in_config->state == VRR_STATE_UNSUPPORTED) {
  526. in_out_vrr->state = VRR_STATE_UNSUPPORTED;
  527. in_out_vrr->supported = false;
  528. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  529. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  530. return;
  531. } else {
  532. in_out_vrr->min_refresh_in_uhz = min_refresh_in_uhz;
  533. in_out_vrr->max_duration_in_us =
  534. calc_duration_in_us_from_refresh_in_uhz(
  535. min_refresh_in_uhz);
  536. in_out_vrr->max_refresh_in_uhz = max_refresh_in_uhz;
  537. in_out_vrr->min_duration_in_us =
  538. calc_duration_in_us_from_refresh_in_uhz(
  539. max_refresh_in_uhz);
  540. refresh_range = in_out_vrr->max_refresh_in_uhz -
  541. in_out_vrr->min_refresh_in_uhz;
  542. in_out_vrr->supported = true;
  543. }
  544. in_out_vrr->fixed.ramping_active = in_config->ramping;
  545. in_out_vrr->btr.btr_enabled = in_config->btr;
  546. if (in_out_vrr->max_refresh_in_uhz <
  547. 2 * in_out_vrr->min_refresh_in_uhz)
  548. in_out_vrr->btr.btr_enabled = false;
  549. in_out_vrr->btr.btr_active = false;
  550. in_out_vrr->btr.inserted_duration_in_us = 0;
  551. in_out_vrr->btr.frames_to_insert = 0;
  552. in_out_vrr->btr.frame_counter = 0;
  553. in_out_vrr->btr.mid_point_in_us =
  554. in_out_vrr->min_duration_in_us +
  555. (in_out_vrr->max_duration_in_us -
  556. in_out_vrr->min_duration_in_us) / 2;
  557. if (in_out_vrr->state == VRR_STATE_UNSUPPORTED) {
  558. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  559. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  560. } else if (in_out_vrr->state == VRR_STATE_DISABLED) {
  561. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  562. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  563. } else if (in_out_vrr->state == VRR_STATE_INACTIVE) {
  564. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  565. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  566. } else if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
  567. refresh_range >= MIN_REFRESH_RANGE_IN_US) {
  568. in_out_vrr->adjust.v_total_min =
  569. calc_v_total_from_refresh(stream,
  570. in_out_vrr->max_refresh_in_uhz);
  571. in_out_vrr->adjust.v_total_max =
  572. calc_v_total_from_refresh(stream,
  573. in_out_vrr->min_refresh_in_uhz);
  574. } else if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED) {
  575. in_out_vrr->fixed.target_refresh_in_uhz =
  576. in_out_vrr->min_refresh_in_uhz;
  577. if (in_out_vrr->fixed.ramping_active &&
  578. in_out_vrr->fixed.fixed_active) {
  579. /* Do not update vtotals if ramping is already active
  580. * in order to continue ramp from current refresh.
  581. */
  582. in_out_vrr->fixed.fixed_active = true;
  583. } else {
  584. in_out_vrr->fixed.fixed_active = true;
  585. in_out_vrr->adjust.v_total_min =
  586. calc_v_total_from_refresh(stream,
  587. in_out_vrr->fixed.target_refresh_in_uhz);
  588. in_out_vrr->adjust.v_total_max =
  589. in_out_vrr->adjust.v_total_min;
  590. }
  591. } else {
  592. in_out_vrr->state = VRR_STATE_INACTIVE;
  593. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  594. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  595. }
  596. }
  597. void mod_freesync_handle_preflip(struct mod_freesync *mod_freesync,
  598. const struct dc_plane_state *plane,
  599. const struct dc_stream_state *stream,
  600. unsigned int curr_time_stamp_in_us,
  601. struct mod_vrr_params *in_out_vrr)
  602. {
  603. struct core_freesync *core_freesync = NULL;
  604. unsigned int last_render_time_in_us = 0;
  605. unsigned int average_render_time_in_us = 0;
  606. if (mod_freesync == NULL)
  607. return;
  608. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  609. if (in_out_vrr->supported &&
  610. in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE) {
  611. unsigned int i = 0;
  612. unsigned int oldest_index = plane->time.index + 1;
  613. if (oldest_index >= DC_PLANE_UPDATE_TIMES_MAX)
  614. oldest_index = 0;
  615. last_render_time_in_us = curr_time_stamp_in_us -
  616. plane->time.prev_update_time_in_us;
  617. // Sum off all entries except oldest one
  618. for (i = 0; i < DC_PLANE_UPDATE_TIMES_MAX; i++) {
  619. average_render_time_in_us +=
  620. plane->time.time_elapsed_in_us[i];
  621. }
  622. average_render_time_in_us -=
  623. plane->time.time_elapsed_in_us[oldest_index];
  624. // Add render time for current flip
  625. average_render_time_in_us += last_render_time_in_us;
  626. average_render_time_in_us /= DC_PLANE_UPDATE_TIMES_MAX;
  627. if (in_out_vrr->btr.btr_enabled) {
  628. apply_below_the_range(core_freesync,
  629. stream,
  630. last_render_time_in_us,
  631. in_out_vrr);
  632. } else {
  633. apply_fixed_refresh(core_freesync,
  634. stream,
  635. last_render_time_in_us,
  636. in_out_vrr);
  637. }
  638. }
  639. }
  640. void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
  641. const struct dc_stream_state *stream,
  642. struct mod_vrr_params *in_out_vrr)
  643. {
  644. struct core_freesync *core_freesync = NULL;
  645. if ((mod_freesync == NULL) || (stream == NULL) || (in_out_vrr == NULL))
  646. return;
  647. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  648. if (in_out_vrr->supported == false)
  649. return;
  650. /* Below the Range Logic */
  651. /* Only execute if in fullscreen mode */
  652. if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
  653. in_out_vrr->btr.btr_active) {
  654. /* TODO: pass in flag for Pre-DCE12 ASIC
  655. * in order for frame variable duration to take affect,
  656. * it needs to be done one VSYNC early, which is at
  657. * frameCounter == 1.
  658. * For DCE12 and newer updates to V_TOTAL_MIN/MAX
  659. * will take affect on current frame
  660. */
  661. if (in_out_vrr->btr.frames_to_insert ==
  662. in_out_vrr->btr.frame_counter) {
  663. in_out_vrr->adjust.v_total_min =
  664. calc_v_total_from_duration(stream,
  665. in_out_vrr,
  666. in_out_vrr->btr.inserted_duration_in_us);
  667. in_out_vrr->adjust.v_total_max =
  668. in_out_vrr->adjust.v_total_min;
  669. }
  670. if (in_out_vrr->btr.frame_counter > 0)
  671. in_out_vrr->btr.frame_counter--;
  672. /* Restore FreeSync */
  673. if (in_out_vrr->btr.frame_counter == 0) {
  674. in_out_vrr->adjust.v_total_min =
  675. calc_v_total_from_refresh(stream,
  676. in_out_vrr->max_refresh_in_uhz);
  677. in_out_vrr->adjust.v_total_max =
  678. calc_v_total_from_refresh(stream,
  679. in_out_vrr->min_refresh_in_uhz);
  680. }
  681. }
  682. /* If in fullscreen freesync mode or in video, do not program
  683. * static screen ramp values
  684. */
  685. if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE)
  686. in_out_vrr->fixed.ramping_active = false;
  687. /* Gradual Static Screen Ramping Logic */
  688. /* Execute if ramp is active and user enabled freesync static screen*/
  689. if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED &&
  690. in_out_vrr->fixed.ramping_active) {
  691. update_v_total_for_static_ramp(
  692. core_freesync, stream, in_out_vrr);
  693. }
  694. }
  695. void mod_freesync_get_settings(struct mod_freesync *mod_freesync,
  696. const struct mod_vrr_params *vrr,
  697. unsigned int *v_total_min, unsigned int *v_total_max,
  698. unsigned int *event_triggers,
  699. unsigned int *window_min, unsigned int *window_max,
  700. unsigned int *lfc_mid_point_in_us,
  701. unsigned int *inserted_frames,
  702. unsigned int *inserted_duration_in_us)
  703. {
  704. struct core_freesync *core_freesync = NULL;
  705. if (mod_freesync == NULL)
  706. return;
  707. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  708. if (vrr->supported) {
  709. *v_total_min = vrr->adjust.v_total_min;
  710. *v_total_max = vrr->adjust.v_total_max;
  711. *event_triggers = 0;
  712. *lfc_mid_point_in_us = vrr->btr.mid_point_in_us;
  713. *inserted_frames = vrr->btr.frames_to_insert;
  714. *inserted_duration_in_us = vrr->btr.inserted_duration_in_us;
  715. }
  716. }
  717. unsigned long long mod_freesync_calc_nominal_field_rate(
  718. const struct dc_stream_state *stream)
  719. {
  720. unsigned long long nominal_field_rate_in_uhz = 0;
  721. /* Calculate nominal field rate for stream */
  722. nominal_field_rate_in_uhz = stream->timing.pix_clk_khz;
  723. nominal_field_rate_in_uhz *= 1000ULL * 1000ULL * 1000ULL;
  724. nominal_field_rate_in_uhz = div_u64(nominal_field_rate_in_uhz,
  725. stream->timing.h_total);
  726. nominal_field_rate_in_uhz = div_u64(nominal_field_rate_in_uhz,
  727. stream->timing.v_total);
  728. return nominal_field_rate_in_uhz;
  729. }
  730. bool mod_freesync_is_valid_range(struct mod_freesync *mod_freesync,
  731. const struct dc_stream_state *stream,
  732. uint32_t min_refresh_cap_in_uhz,
  733. uint32_t max_refresh_cap_in_uhz,
  734. uint32_t min_refresh_request_in_uhz,
  735. uint32_t max_refresh_request_in_uhz)
  736. {
  737. /* Calculate nominal field rate for stream */
  738. unsigned long long nominal_field_rate_in_uhz =
  739. mod_freesync_calc_nominal_field_rate(stream);
  740. /* Allow for some rounding error of actual video timing by taking ceil.
  741. * For example, 144 Hz mode timing may actually be 143.xxx Hz when
  742. * calculated from pixel rate and vertical/horizontal totals, but
  743. * this should be allowed instead of blocking FreeSync.
  744. */
  745. nominal_field_rate_in_uhz = div_u64(nominal_field_rate_in_uhz, 1000000);
  746. min_refresh_cap_in_uhz /= 1000000;
  747. max_refresh_cap_in_uhz /= 1000000;
  748. min_refresh_request_in_uhz /= 1000000;
  749. max_refresh_request_in_uhz /= 1000000;
  750. // Check nominal is within range
  751. if (nominal_field_rate_in_uhz > max_refresh_cap_in_uhz ||
  752. nominal_field_rate_in_uhz < min_refresh_cap_in_uhz)
  753. return false;
  754. // If nominal is less than max, limit the max allowed refresh rate
  755. if (nominal_field_rate_in_uhz < max_refresh_cap_in_uhz)
  756. max_refresh_cap_in_uhz = nominal_field_rate_in_uhz;
  757. // Don't allow min > max
  758. if (min_refresh_request_in_uhz > max_refresh_request_in_uhz)
  759. return false;
  760. // Check min is within range
  761. if (min_refresh_request_in_uhz > max_refresh_cap_in_uhz ||
  762. min_refresh_request_in_uhz < min_refresh_cap_in_uhz)
  763. return false;
  764. // Check max is within range
  765. if (max_refresh_request_in_uhz > max_refresh_cap_in_uhz ||
  766. max_refresh_request_in_uhz < min_refresh_cap_in_uhz)
  767. return false;
  768. // For variable range, check for at least 10 Hz range
  769. if ((max_refresh_request_in_uhz != min_refresh_request_in_uhz) &&
  770. (max_refresh_request_in_uhz - min_refresh_request_in_uhz < 10))
  771. return false;
  772. return true;
  773. }