freesync.c 32 KB

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