freesync.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. bool nominal_field_rate_in_range = true;
  510. unsigned int refresh_range = 0;
  511. unsigned int min_refresh_in_uhz = 0;
  512. unsigned int max_refresh_in_uhz = 0;
  513. if (mod_freesync == NULL)
  514. return;
  515. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  516. /* Calculate nominal field rate for stream */
  517. nominal_field_rate_in_uhz =
  518. mod_freesync_calc_nominal_field_rate(stream);
  519. min_refresh_in_uhz = in_config->min_refresh_in_uhz;
  520. max_refresh_in_uhz = in_config->max_refresh_in_uhz;
  521. // Don't allow min > max
  522. if (min_refresh_in_uhz > max_refresh_in_uhz)
  523. min_refresh_in_uhz = max_refresh_in_uhz;
  524. // Full range may be larger than current video timing, so cap at nominal
  525. if (max_refresh_in_uhz > nominal_field_rate_in_uhz)
  526. max_refresh_in_uhz = nominal_field_rate_in_uhz;
  527. /* Allow for some rounding error of actual video timing by taking ceil.
  528. * For example, 144 Hz mode timing may actually be 143.xxx Hz when
  529. * calculated from pixel rate and vertical/horizontal totals, but
  530. * this should be allowed instead of blocking FreeSync.
  531. */
  532. if ((min_refresh_in_uhz / 1000000) >
  533. ((nominal_field_rate_in_uhz + 1000000 - 1) / 1000000))
  534. nominal_field_rate_in_range = false;
  535. // Full range may be larger than current video timing, so cap at nominal
  536. if (min_refresh_in_uhz > nominal_field_rate_in_uhz)
  537. min_refresh_in_uhz = nominal_field_rate_in_uhz;
  538. if (!vrr_settings_require_update(core_freesync,
  539. in_config, min_refresh_in_uhz, max_refresh_in_uhz,
  540. in_out_vrr))
  541. return;
  542. in_out_vrr->state = in_config->state;
  543. if ((in_config->state == VRR_STATE_UNSUPPORTED) ||
  544. (!nominal_field_rate_in_range)) {
  545. in_out_vrr->state = VRR_STATE_UNSUPPORTED;
  546. in_out_vrr->supported = false;
  547. } else {
  548. in_out_vrr->min_refresh_in_uhz = min_refresh_in_uhz;
  549. in_out_vrr->max_duration_in_us =
  550. calc_duration_in_us_from_refresh_in_uhz(
  551. min_refresh_in_uhz);
  552. in_out_vrr->max_refresh_in_uhz = max_refresh_in_uhz;
  553. in_out_vrr->min_duration_in_us =
  554. calc_duration_in_us_from_refresh_in_uhz(
  555. max_refresh_in_uhz);
  556. refresh_range = in_out_vrr->max_refresh_in_uhz -
  557. in_out_vrr->min_refresh_in_uhz;
  558. in_out_vrr->supported = true;
  559. }
  560. in_out_vrr->fixed.ramping_active = in_config->ramping;
  561. in_out_vrr->btr.btr_enabled = in_config->btr;
  562. if (in_out_vrr->max_refresh_in_uhz <
  563. 2 * in_out_vrr->min_refresh_in_uhz)
  564. in_out_vrr->btr.btr_enabled = false;
  565. in_out_vrr->btr.btr_active = false;
  566. in_out_vrr->btr.inserted_duration_in_us = 0;
  567. in_out_vrr->btr.frames_to_insert = 0;
  568. in_out_vrr->btr.frame_counter = 0;
  569. in_out_vrr->btr.mid_point_in_us =
  570. in_out_vrr->min_duration_in_us +
  571. (in_out_vrr->max_duration_in_us -
  572. in_out_vrr->min_duration_in_us) / 2;
  573. if (in_out_vrr->state == VRR_STATE_UNSUPPORTED) {
  574. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  575. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  576. } else if (in_out_vrr->state == VRR_STATE_DISABLED) {
  577. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  578. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  579. } else if (in_out_vrr->state == VRR_STATE_INACTIVE) {
  580. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  581. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  582. } else if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
  583. refresh_range >= MIN_REFRESH_RANGE_IN_US) {
  584. in_out_vrr->adjust.v_total_min =
  585. calc_v_total_from_refresh(stream,
  586. in_out_vrr->max_refresh_in_uhz);
  587. in_out_vrr->adjust.v_total_max =
  588. calc_v_total_from_refresh(stream,
  589. in_out_vrr->min_refresh_in_uhz);
  590. } else if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED) {
  591. in_out_vrr->fixed.target_refresh_in_uhz =
  592. in_out_vrr->min_refresh_in_uhz;
  593. if (in_out_vrr->fixed.ramping_active) {
  594. in_out_vrr->fixed.fixed_active = true;
  595. } else {
  596. in_out_vrr->fixed.fixed_active = true;
  597. in_out_vrr->adjust.v_total_min =
  598. calc_v_total_from_refresh(stream,
  599. in_out_vrr->fixed.target_refresh_in_uhz);
  600. in_out_vrr->adjust.v_total_max =
  601. in_out_vrr->adjust.v_total_min;
  602. }
  603. } else {
  604. in_out_vrr->state = VRR_STATE_INACTIVE;
  605. in_out_vrr->adjust.v_total_min = stream->timing.v_total;
  606. in_out_vrr->adjust.v_total_max = stream->timing.v_total;
  607. }
  608. }
  609. void mod_freesync_handle_preflip(struct mod_freesync *mod_freesync,
  610. const struct dc_plane_state *plane,
  611. const struct dc_stream_state *stream,
  612. unsigned int curr_time_stamp_in_us,
  613. struct mod_vrr_params *in_out_vrr)
  614. {
  615. struct core_freesync *core_freesync = NULL;
  616. unsigned int last_render_time_in_us = 0;
  617. unsigned int average_render_time_in_us = 0;
  618. if (mod_freesync == NULL)
  619. return;
  620. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  621. if (in_out_vrr->supported &&
  622. in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE) {
  623. unsigned int i = 0;
  624. unsigned int oldest_index = plane->time.index + 1;
  625. if (oldest_index >= DC_PLANE_UPDATE_TIMES_MAX)
  626. oldest_index = 0;
  627. last_render_time_in_us = curr_time_stamp_in_us -
  628. plane->time.prev_update_time_in_us;
  629. // Sum off all entries except oldest one
  630. for (i = 0; i < DC_PLANE_UPDATE_TIMES_MAX; i++) {
  631. average_render_time_in_us +=
  632. plane->time.time_elapsed_in_us[i];
  633. }
  634. average_render_time_in_us -=
  635. plane->time.time_elapsed_in_us[oldest_index];
  636. // Add render time for current flip
  637. average_render_time_in_us += last_render_time_in_us;
  638. average_render_time_in_us /= DC_PLANE_UPDATE_TIMES_MAX;
  639. if (in_out_vrr->btr.btr_enabled) {
  640. apply_below_the_range(core_freesync,
  641. stream,
  642. last_render_time_in_us,
  643. in_out_vrr);
  644. } else {
  645. apply_fixed_refresh(core_freesync,
  646. stream,
  647. last_render_time_in_us,
  648. in_out_vrr);
  649. }
  650. }
  651. }
  652. void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
  653. const struct dc_stream_state *stream,
  654. struct mod_vrr_params *in_out_vrr)
  655. {
  656. struct core_freesync *core_freesync = NULL;
  657. if ((mod_freesync == NULL) || (stream == NULL) || (in_out_vrr == NULL))
  658. return;
  659. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  660. if (in_out_vrr->supported == false)
  661. return;
  662. /* Below the Range Logic */
  663. /* Only execute if in fullscreen mode */
  664. if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE &&
  665. in_out_vrr->btr.btr_active) {
  666. /* TODO: pass in flag for Pre-DCE12 ASIC
  667. * in order for frame variable duration to take affect,
  668. * it needs to be done one VSYNC early, which is at
  669. * frameCounter == 1.
  670. * For DCE12 and newer updates to V_TOTAL_MIN/MAX
  671. * will take affect on current frame
  672. */
  673. if (in_out_vrr->btr.frames_to_insert ==
  674. in_out_vrr->btr.frame_counter) {
  675. in_out_vrr->adjust.v_total_min =
  676. calc_v_total_from_duration(stream,
  677. in_out_vrr,
  678. in_out_vrr->btr.inserted_duration_in_us);
  679. in_out_vrr->adjust.v_total_max =
  680. in_out_vrr->adjust.v_total_min;
  681. }
  682. if (in_out_vrr->btr.frame_counter > 0)
  683. in_out_vrr->btr.frame_counter--;
  684. /* Restore FreeSync */
  685. if (in_out_vrr->btr.frame_counter == 0) {
  686. in_out_vrr->adjust.v_total_min =
  687. calc_v_total_from_refresh(stream,
  688. in_out_vrr->max_refresh_in_uhz);
  689. in_out_vrr->adjust.v_total_max =
  690. calc_v_total_from_refresh(stream,
  691. in_out_vrr->min_refresh_in_uhz);
  692. }
  693. }
  694. /* If in fullscreen freesync mode or in video, do not program
  695. * static screen ramp values
  696. */
  697. if (in_out_vrr->state == VRR_STATE_ACTIVE_VARIABLE)
  698. in_out_vrr->fixed.ramping_active = false;
  699. /* Gradual Static Screen Ramping Logic */
  700. /* Execute if ramp is active and user enabled freesync static screen*/
  701. if (in_out_vrr->state == VRR_STATE_ACTIVE_FIXED &&
  702. in_out_vrr->fixed.ramping_active) {
  703. update_v_total_for_static_ramp(
  704. core_freesync, stream, in_out_vrr);
  705. }
  706. }
  707. void mod_freesync_get_settings(struct mod_freesync *mod_freesync,
  708. const struct mod_vrr_params *vrr,
  709. unsigned int *v_total_min, unsigned int *v_total_max,
  710. unsigned int *event_triggers,
  711. unsigned int *window_min, unsigned int *window_max,
  712. unsigned int *lfc_mid_point_in_us,
  713. unsigned int *inserted_frames,
  714. unsigned int *inserted_duration_in_us)
  715. {
  716. struct core_freesync *core_freesync = NULL;
  717. if (mod_freesync == NULL)
  718. return;
  719. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  720. if (vrr->supported) {
  721. *v_total_min = vrr->adjust.v_total_min;
  722. *v_total_max = vrr->adjust.v_total_max;
  723. *event_triggers = 0;
  724. *lfc_mid_point_in_us = vrr->btr.mid_point_in_us;
  725. *inserted_frames = vrr->btr.frames_to_insert;
  726. *inserted_duration_in_us = vrr->btr.inserted_duration_in_us;
  727. }
  728. }
  729. unsigned long long mod_freesync_calc_nominal_field_rate(
  730. const struct dc_stream_state *stream)
  731. {
  732. unsigned long long nominal_field_rate_in_uhz = 0;
  733. /* Calculate nominal field rate for stream */
  734. nominal_field_rate_in_uhz = stream->timing.pix_clk_khz;
  735. nominal_field_rate_in_uhz *= 1000ULL * 1000ULL * 1000ULL;
  736. nominal_field_rate_in_uhz = div_u64(nominal_field_rate_in_uhz,
  737. stream->timing.h_total);
  738. nominal_field_rate_in_uhz = div_u64(nominal_field_rate_in_uhz,
  739. stream->timing.v_total);
  740. return nominal_field_rate_in_uhz;
  741. }
  742. bool mod_freesync_is_valid_range(struct mod_freesync *mod_freesync,
  743. const struct dc_stream_state *stream,
  744. uint32_t min_refresh_cap_in_uhz,
  745. uint32_t max_refresh_cap_in_uhz,
  746. uint32_t min_refresh_request_in_uhz,
  747. uint32_t max_refresh_request_in_uhz)
  748. {
  749. /* Calculate nominal field rate for stream */
  750. unsigned long long nominal_field_rate_in_uhz =
  751. mod_freesync_calc_nominal_field_rate(stream);
  752. // Check nominal is within range
  753. if (nominal_field_rate_in_uhz > max_refresh_cap_in_uhz ||
  754. nominal_field_rate_in_uhz < min_refresh_cap_in_uhz)
  755. return false;
  756. // If nominal is less than max, limit the max allowed refresh rate
  757. if (nominal_field_rate_in_uhz < max_refresh_cap_in_uhz)
  758. max_refresh_cap_in_uhz = nominal_field_rate_in_uhz;
  759. // Don't allow min > max
  760. if (min_refresh_request_in_uhz > max_refresh_request_in_uhz)
  761. return false;
  762. // Check min is within range
  763. if (min_refresh_request_in_uhz > max_refresh_cap_in_uhz ||
  764. min_refresh_request_in_uhz < min_refresh_cap_in_uhz)
  765. return false;
  766. // Check max is within range
  767. if (max_refresh_request_in_uhz > max_refresh_cap_in_uhz ||
  768. max_refresh_request_in_uhz < min_refresh_cap_in_uhz)
  769. return false;
  770. // For variable range, check for at least 10 Hz range
  771. if ((max_refresh_request_in_uhz != min_refresh_request_in_uhz) &&
  772. (max_refresh_request_in_uhz - min_refresh_request_in_uhz < 10000000))
  773. return false;
  774. return true;
  775. }