freesync.c 28 KB

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