freesync.c 28 KB

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