freesync.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241
  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. #include "core_dc.h"
  30. #define MOD_FREESYNC_MAX_CONCURRENT_STREAMS 32
  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 20
  35. /* Threshold to exit BTR (to avoid frequent enter-exits at the lower limit) */
  36. #define BTR_EXIT_MARGIN 2000
  37. #define FREESYNC_REGISTRY_NAME "freesync_v1"
  38. struct gradual_static_ramp {
  39. bool ramp_is_active;
  40. bool ramp_direction_is_up;
  41. unsigned int ramp_current_frame_duration_in_ns;
  42. };
  43. struct time_cache {
  44. /* video (48Hz feature) related */
  45. unsigned int update_duration_in_ns;
  46. /* BTR/fixed refresh related */
  47. unsigned int prev_time_stamp_in_us;
  48. unsigned int min_render_time_in_us;
  49. unsigned int max_render_time_in_us;
  50. unsigned int render_times_index;
  51. unsigned int render_times[RENDER_TIMES_MAX_COUNT];
  52. };
  53. struct below_the_range {
  54. bool btr_active;
  55. bool program_btr;
  56. unsigned int mid_point_in_us;
  57. unsigned int inserted_frame_duration_in_us;
  58. unsigned int frames_to_insert;
  59. unsigned int frame_counter;
  60. };
  61. struct fixed_refresh {
  62. bool fixed_refresh_active;
  63. bool program_fixed_refresh;
  64. };
  65. struct freesync_state {
  66. bool fullscreen;
  67. bool static_screen;
  68. bool video;
  69. unsigned int nominal_refresh_rate_in_micro_hz;
  70. bool windowed_fullscreen;
  71. struct time_cache time;
  72. struct gradual_static_ramp static_ramp;
  73. struct below_the_range btr;
  74. struct fixed_refresh fixed_refresh;
  75. };
  76. struct freesync_entity {
  77. const struct dc_stream *stream;
  78. struct mod_freesync_caps *caps;
  79. struct freesync_state state;
  80. struct mod_freesync_user_enable user_enable;
  81. };
  82. struct core_freesync {
  83. struct mod_freesync public;
  84. struct dc *dc;
  85. struct freesync_entity *map;
  86. int num_entities;
  87. };
  88. #define MOD_FREESYNC_TO_CORE(mod_freesync)\
  89. container_of(mod_freesync, struct core_freesync, public)
  90. static bool check_dc_support(const struct dc *dc)
  91. {
  92. if (dc->stream_funcs.adjust_vmin_vmax == NULL)
  93. return false;
  94. return true;
  95. }
  96. struct mod_freesync *mod_freesync_create(struct dc *dc)
  97. {
  98. struct core_freesync *core_freesync =
  99. dm_alloc(sizeof(struct core_freesync));
  100. struct core_dc *core_dc = DC_TO_CORE(dc);
  101. struct persistent_data_flag flag;
  102. int i = 0;
  103. if (core_freesync == NULL)
  104. goto fail_alloc_context;
  105. core_freesync->map = dm_alloc(sizeof(struct freesync_entity) *
  106. MOD_FREESYNC_MAX_CONCURRENT_STREAMS);
  107. if (core_freesync->map == NULL)
  108. goto fail_alloc_map;
  109. for (i = 0; i < MOD_FREESYNC_MAX_CONCURRENT_STREAMS; i++)
  110. core_freesync->map[i].stream = NULL;
  111. core_freesync->num_entities = 0;
  112. if (dc == NULL)
  113. goto fail_construct;
  114. core_freesync->dc = dc;
  115. if (!check_dc_support(dc))
  116. goto fail_construct;
  117. /* Create initial module folder in registry for freesync enable data */
  118. flag.save_per_edid = true;
  119. flag.save_per_link = false;
  120. dm_write_persistent_data(core_dc->ctx, NULL, FREESYNC_REGISTRY_NAME, NULL, NULL,
  121. 0, &flag);
  122. return &core_freesync->public;
  123. fail_construct:
  124. dm_free(core_freesync->map);
  125. fail_alloc_map:
  126. dm_free(core_freesync);
  127. fail_alloc_context:
  128. return NULL;
  129. }
  130. void mod_freesync_destroy(struct mod_freesync *mod_freesync)
  131. {
  132. if (mod_freesync != NULL) {
  133. int i;
  134. struct core_freesync *core_freesync =
  135. MOD_FREESYNC_TO_CORE(mod_freesync);
  136. for (i = 0; i < core_freesync->num_entities; i++)
  137. if (core_freesync->map[i].stream)
  138. dc_stream_release(core_freesync->map[i].stream);
  139. dm_free(core_freesync->map);
  140. dm_free(core_freesync);
  141. }
  142. }
  143. /* Given a specific dc_stream* this function finds its equivalent
  144. * on the core_freesync->map and returns the corresponding index
  145. */
  146. static unsigned int map_index_from_stream(struct core_freesync *core_freesync,
  147. const struct dc_stream *stream)
  148. {
  149. unsigned int index = 0;
  150. for (index = 0; index < core_freesync->num_entities; index++) {
  151. if (core_freesync->map[index].stream == stream) {
  152. return index;
  153. }
  154. }
  155. /* Could not find stream requested */
  156. ASSERT(false);
  157. return index;
  158. }
  159. bool mod_freesync_add_stream(struct mod_freesync *mod_freesync,
  160. const struct dc_stream *stream, struct mod_freesync_caps *caps)
  161. {
  162. struct core_stream *core_stream = NULL;
  163. struct core_dc *core_dc = NULL;
  164. struct core_freesync *core_freesync = NULL;
  165. int persistent_freesync_enable = 0;
  166. struct persistent_data_flag flag;
  167. unsigned int nom_refresh_rate_micro_hz;
  168. unsigned long long temp;
  169. if (mod_freesync == NULL)
  170. return false;
  171. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  172. core_stream = DC_STREAM_TO_CORE(stream);
  173. core_dc = DC_TO_CORE(core_freesync->dc);
  174. flag.save_per_edid = true;
  175. flag.save_per_link = false;
  176. if (core_freesync->num_entities < MOD_FREESYNC_MAX_CONCURRENT_STREAMS) {
  177. dc_stream_retain(stream);
  178. core_freesync->map[core_freesync->num_entities].stream = stream;
  179. core_freesync->map[core_freesync->num_entities].caps = caps;
  180. core_freesync->map[core_freesync->num_entities].state.
  181. fullscreen = false;
  182. core_freesync->map[core_freesync->num_entities].state.
  183. static_screen = false;
  184. core_freesync->map[core_freesync->num_entities].state.
  185. video = false;
  186. core_freesync->map[core_freesync->num_entities].state.time.
  187. update_duration_in_ns = 0;
  188. core_freesync->map[core_freesync->num_entities].state.
  189. static_ramp.ramp_is_active = false;
  190. /* get persistent data from registry */
  191. if (dm_read_persistent_data(core_dc->ctx, stream->sink,
  192. FREESYNC_REGISTRY_NAME,
  193. "userenable", &persistent_freesync_enable,
  194. sizeof(int), &flag)) {
  195. core_freesync->map[core_freesync->num_entities].user_enable.
  196. enable_for_gaming =
  197. (persistent_freesync_enable & 1) ? true : false;
  198. core_freesync->map[core_freesync->num_entities].user_enable.
  199. enable_for_static =
  200. (persistent_freesync_enable & 2) ? true : false;
  201. core_freesync->map[core_freesync->num_entities].user_enable.
  202. enable_for_video =
  203. (persistent_freesync_enable & 4) ? true : false;
  204. } else {
  205. core_freesync->map[core_freesync->num_entities].user_enable.
  206. enable_for_gaming = false;
  207. core_freesync->map[core_freesync->num_entities].user_enable.
  208. enable_for_static = false;
  209. core_freesync->map[core_freesync->num_entities].user_enable.
  210. enable_for_video = false;
  211. }
  212. temp = stream->timing.pix_clk_khz;
  213. temp *= 1000ULL * 1000ULL * 1000ULL;
  214. temp = div_u64(temp, stream->timing.h_total);
  215. temp = div_u64(temp, stream->timing.v_total);
  216. nom_refresh_rate_micro_hz = (unsigned int) temp;
  217. if (caps->supported &&
  218. nom_refresh_rate_micro_hz >= caps->min_refresh_in_micro_hz &&
  219. nom_refresh_rate_micro_hz <= caps->max_refresh_in_micro_hz)
  220. core_stream->public.ignore_msa_timing_param = 1;
  221. core_freesync->num_entities++;
  222. return true;
  223. }
  224. return false;
  225. }
  226. bool mod_freesync_remove_stream(struct mod_freesync *mod_freesync,
  227. const struct dc_stream *stream)
  228. {
  229. int i = 0;
  230. struct core_freesync *core_freesync = NULL;
  231. unsigned int index = 0;
  232. if (mod_freesync == NULL)
  233. return false;
  234. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  235. index = map_index_from_stream(core_freesync, stream);
  236. dc_stream_release(core_freesync->map[index].stream);
  237. core_freesync->map[index].stream = NULL;
  238. /* To remove this entity, shift everything after down */
  239. for (i = index; i < core_freesync->num_entities - 1; i++)
  240. core_freesync->map[i] = core_freesync->map[i + 1];
  241. core_freesync->num_entities--;
  242. return true;
  243. }
  244. static void update_stream_freesync_context(struct core_freesync *core_freesync,
  245. const struct dc_stream *stream)
  246. {
  247. unsigned int index;
  248. struct freesync_context *ctx;
  249. struct core_stream *core_stream;
  250. core_stream = DC_STREAM_TO_CORE(stream);
  251. ctx = &core_stream->public.freesync_ctx;
  252. index = map_index_from_stream(core_freesync, stream);
  253. ctx->supported = core_freesync->map[index].caps->supported;
  254. ctx->enabled = (core_freesync->map[index].user_enable.enable_for_gaming ||
  255. core_freesync->map[index].user_enable.enable_for_video ||
  256. core_freesync->map[index].user_enable.enable_for_static);
  257. ctx->active = (core_freesync->map[index].state.fullscreen ||
  258. core_freesync->map[index].state.video ||
  259. core_freesync->map[index].state.static_ramp.ramp_is_active);
  260. ctx->min_refresh_in_micro_hz =
  261. core_freesync->map[index].caps->min_refresh_in_micro_hz;
  262. ctx->nominal_refresh_in_micro_hz = core_freesync->
  263. map[index].state.nominal_refresh_rate_in_micro_hz;
  264. }
  265. static void update_stream(struct core_freesync *core_freesync,
  266. const struct dc_stream *stream)
  267. {
  268. struct core_stream *core_stream = DC_STREAM_TO_CORE(stream);
  269. unsigned int index = map_index_from_stream(core_freesync, stream);
  270. if (core_freesync->map[index].caps->supported) {
  271. core_stream->public.ignore_msa_timing_param = 1;
  272. update_stream_freesync_context(core_freesync, stream);
  273. }
  274. }
  275. static void calc_vmin_vmax(struct core_freesync *core_freesync,
  276. const struct dc_stream *stream, int *vmin, int *vmax)
  277. {
  278. unsigned int min_frame_duration_in_ns = 0, max_frame_duration_in_ns = 0;
  279. unsigned int index = map_index_from_stream(core_freesync, stream);
  280. uint32_t vtotal = stream->timing.v_total;
  281. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  282. (1000000000ULL * 1000000),
  283. core_freesync->map[index].state.
  284. nominal_refresh_rate_in_micro_hz)));
  285. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  286. (1000000000ULL * 1000000),
  287. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  288. *vmax = div64_u64(div64_u64(((unsigned long long)(
  289. max_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  290. stream->timing.h_total), 1000000);
  291. *vmin = div64_u64(div64_u64(((unsigned long long)(
  292. min_frame_duration_in_ns) * stream->timing.pix_clk_khz),
  293. stream->timing.h_total), 1000000);
  294. /* In case of 4k free sync monitor, vmin or vmax cannot be less than vtotal */
  295. if (*vmin < vtotal) {
  296. ASSERT(false);
  297. *vmin = vtotal;
  298. }
  299. if (*vmax < vtotal) {
  300. ASSERT(false);
  301. *vmax = vtotal;
  302. }
  303. }
  304. static void calc_v_total_from_duration(const struct dc_stream *stream,
  305. unsigned int duration_in_ns, int *v_total_nominal)
  306. {
  307. *v_total_nominal = div64_u64(div64_u64(((unsigned long long)(
  308. duration_in_ns) * stream->timing.pix_clk_khz),
  309. stream->timing.h_total), 1000000);
  310. }
  311. static void calc_v_total_for_static_ramp(struct core_freesync *core_freesync,
  312. const struct dc_stream *stream,
  313. unsigned int index, int *v_total)
  314. {
  315. unsigned int frame_duration = 0;
  316. struct gradual_static_ramp *static_ramp_variables =
  317. &core_freesync->map[index].state.static_ramp;
  318. /* Calc ratio between new and current frame duration with 3 digit */
  319. unsigned int frame_duration_ratio = div64_u64(1000000,
  320. (1000 + div64_u64(((unsigned long long)(
  321. STATIC_SCREEN_RAMP_DELTA_REFRESH_RATE_PER_FRAME) *
  322. static_ramp_variables->ramp_current_frame_duration_in_ns),
  323. 1000000000)));
  324. /* Calculate delta between new and current frame duration in ns */
  325. unsigned int frame_duration_delta = div64_u64(((unsigned long long)(
  326. static_ramp_variables->ramp_current_frame_duration_in_ns) *
  327. (1000 - frame_duration_ratio)), 1000);
  328. /* Adjust frame duration delta based on ratio between current and
  329. * standard frame duration (frame duration at 60 Hz refresh rate).
  330. */
  331. unsigned int ramp_rate_interpolated = div64_u64(((unsigned long long)(
  332. frame_duration_delta) * static_ramp_variables->
  333. ramp_current_frame_duration_in_ns), 16666666);
  334. /* Going to a higher refresh rate (lower frame duration) */
  335. if (static_ramp_variables->ramp_direction_is_up) {
  336. /* reduce frame duration */
  337. static_ramp_variables->ramp_current_frame_duration_in_ns -=
  338. ramp_rate_interpolated;
  339. /* min frame duration */
  340. frame_duration = ((unsigned int) (div64_u64(
  341. (1000000000ULL * 1000000),
  342. core_freesync->map[index].state.
  343. nominal_refresh_rate_in_micro_hz)));
  344. /* adjust for frame duration below min */
  345. if (static_ramp_variables->ramp_current_frame_duration_in_ns <=
  346. frame_duration) {
  347. static_ramp_variables->ramp_is_active = false;
  348. static_ramp_variables->
  349. ramp_current_frame_duration_in_ns =
  350. frame_duration;
  351. }
  352. /* Going to a lower refresh rate (larger frame duration) */
  353. } else {
  354. /* increase frame duration */
  355. static_ramp_variables->ramp_current_frame_duration_in_ns +=
  356. ramp_rate_interpolated;
  357. /* max frame duration */
  358. frame_duration = ((unsigned int) (div64_u64(
  359. (1000000000ULL * 1000000),
  360. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  361. /* adjust for frame duration above max */
  362. if (static_ramp_variables->ramp_current_frame_duration_in_ns >=
  363. frame_duration) {
  364. static_ramp_variables->ramp_is_active = false;
  365. static_ramp_variables->
  366. ramp_current_frame_duration_in_ns =
  367. frame_duration;
  368. }
  369. }
  370. calc_v_total_from_duration(stream, static_ramp_variables->
  371. ramp_current_frame_duration_in_ns, v_total);
  372. }
  373. static void reset_freesync_state_variables(struct freesync_state* state)
  374. {
  375. state->static_ramp.ramp_is_active = false;
  376. if (state->nominal_refresh_rate_in_micro_hz)
  377. state->static_ramp.ramp_current_frame_duration_in_ns =
  378. ((unsigned int) (div64_u64(
  379. (1000000000ULL * 1000000),
  380. state->nominal_refresh_rate_in_micro_hz)));
  381. state->btr.btr_active = false;
  382. state->btr.frame_counter = 0;
  383. state->btr.frames_to_insert = 0;
  384. state->btr.inserted_frame_duration_in_us = 0;
  385. state->btr.program_btr = false;
  386. state->fixed_refresh.fixed_refresh_active = false;
  387. state->fixed_refresh.program_fixed_refresh = false;
  388. }
  389. /*
  390. * Sets freesync mode on a stream depending on current freesync state.
  391. */
  392. static bool set_freesync_on_streams(struct core_freesync *core_freesync,
  393. const struct dc_stream **streams, int num_streams)
  394. {
  395. int v_total_nominal = 0, v_total_min = 0, v_total_max = 0;
  396. unsigned int stream_idx, map_index = 0;
  397. struct freesync_state *state;
  398. if (num_streams == 0 || streams == NULL || num_streams > 1)
  399. return false;
  400. for (stream_idx = 0; stream_idx < num_streams; stream_idx++) {
  401. map_index = map_index_from_stream(core_freesync,
  402. streams[stream_idx]);
  403. state = &core_freesync->map[map_index].state;
  404. if (core_freesync->map[map_index].caps->supported) {
  405. /* Fullscreen has the topmost priority. If the
  406. * fullscreen bit is set, we are in a fullscreen
  407. * application where it should not matter if it is
  408. * static screen. We should not check the static_screen
  409. * or video bit.
  410. *
  411. * Special cases of fullscreen include btr and fixed
  412. * refresh. We program btr on every flip and involves
  413. * programming full range right before the last inserted frame.
  414. * However, we do not want to program the full freesync range
  415. * when fixed refresh is active, because we only program
  416. * that logic once and this will override it.
  417. */
  418. if (core_freesync->map[map_index].user_enable.
  419. enable_for_gaming == true &&
  420. state->fullscreen == true &&
  421. state->fixed_refresh.fixed_refresh_active == false) {
  422. /* Enable freesync */
  423. calc_vmin_vmax(core_freesync,
  424. streams[stream_idx],
  425. &v_total_min, &v_total_max);
  426. /* Update the freesync context for the stream */
  427. update_stream_freesync_context(core_freesync,
  428. streams[stream_idx]);
  429. core_freesync->dc->stream_funcs.
  430. adjust_vmin_vmax(core_freesync->dc, streams,
  431. num_streams, v_total_min,
  432. v_total_max);
  433. return true;
  434. } else if (core_freesync->map[map_index].user_enable.
  435. enable_for_video && state->video == true) {
  436. /* Enable 48Hz feature */
  437. calc_v_total_from_duration(streams[stream_idx],
  438. state->time.update_duration_in_ns,
  439. &v_total_nominal);
  440. /* Program only if v_total_nominal is in range*/
  441. if (v_total_nominal >=
  442. streams[stream_idx]->timing.v_total) {
  443. /* Update the freesync context for
  444. * the stream
  445. */
  446. update_stream_freesync_context(
  447. core_freesync,
  448. streams[stream_idx]);
  449. core_freesync->dc->stream_funcs.
  450. adjust_vmin_vmax(
  451. core_freesync->dc, streams,
  452. num_streams, v_total_nominal,
  453. v_total_nominal);
  454. }
  455. return true;
  456. } else {
  457. /* Disable freesync */
  458. v_total_nominal = streams[stream_idx]->
  459. timing.v_total;
  460. /* Update the freesync context for
  461. * the stream
  462. */
  463. update_stream_freesync_context(
  464. core_freesync,
  465. streams[stream_idx]);
  466. core_freesync->dc->stream_funcs.
  467. adjust_vmin_vmax(
  468. core_freesync->dc, streams,
  469. num_streams, v_total_nominal,
  470. v_total_nominal);
  471. /* Reset the cached variables */
  472. reset_freesync_state_variables(state);
  473. return true;
  474. }
  475. } else {
  476. /* Disable freesync */
  477. v_total_nominal = streams[stream_idx]->
  478. timing.v_total;
  479. /*
  480. * we have to reset drr always even sink does
  481. * not support freesync because a former stream has
  482. * be programmed
  483. */
  484. core_freesync->dc->stream_funcs.
  485. adjust_vmin_vmax(
  486. core_freesync->dc, streams,
  487. num_streams, v_total_nominal,
  488. v_total_nominal);
  489. /* Reset the cached variables */
  490. reset_freesync_state_variables(state);
  491. }
  492. }
  493. return false;
  494. }
  495. static void set_static_ramp_variables(struct core_freesync *core_freesync,
  496. unsigned int index, bool enable_static_screen)
  497. {
  498. unsigned int frame_duration = 0;
  499. struct gradual_static_ramp *static_ramp_variables =
  500. &core_freesync->map[index].state.static_ramp;
  501. /* If ramp is not active, set initial frame duration depending on
  502. * whether we are enabling/disabling static screen mode. If the ramp is
  503. * already active, ramp should continue in the opposite direction
  504. * starting with the current frame duration
  505. */
  506. if (!static_ramp_variables->ramp_is_active) {
  507. static_ramp_variables->ramp_is_active = true;
  508. if (enable_static_screen == true) {
  509. /* Going to lower refresh rate, so start from max
  510. * refresh rate (min frame duration)
  511. */
  512. frame_duration = ((unsigned int) (div64_u64(
  513. (1000000000ULL * 1000000),
  514. core_freesync->map[index].state.
  515. nominal_refresh_rate_in_micro_hz)));
  516. } else {
  517. /* Going to higher refresh rate, so start from min
  518. * refresh rate (max frame duration)
  519. */
  520. frame_duration = ((unsigned int) (div64_u64(
  521. (1000000000ULL * 1000000),
  522. core_freesync->map[index].caps->min_refresh_in_micro_hz)));
  523. }
  524. static_ramp_variables->
  525. ramp_current_frame_duration_in_ns = frame_duration;
  526. }
  527. /* If we are ENABLING static screen, refresh rate should go DOWN.
  528. * If we are DISABLING static screen, refresh rate should go UP.
  529. */
  530. static_ramp_variables->ramp_direction_is_up = !enable_static_screen;
  531. }
  532. void mod_freesync_handle_v_update(struct mod_freesync *mod_freesync,
  533. const struct dc_stream **streams, int num_streams)
  534. {
  535. unsigned int index, v_total, inserted_frame_v_total = 0;
  536. unsigned int min_frame_duration_in_ns, vmax, vmin = 0;
  537. struct freesync_state *state;
  538. struct core_freesync *core_freesync = NULL;
  539. struct dc_static_screen_events triggers = {0};
  540. if (mod_freesync == NULL)
  541. return;
  542. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  543. if (core_freesync->num_entities == 0)
  544. return;
  545. index = map_index_from_stream(core_freesync,
  546. streams[0]);
  547. if (core_freesync->map[index].caps->supported == false)
  548. return;
  549. state = &core_freesync->map[index].state;
  550. /* Below the Range Logic */
  551. /* Only execute if in fullscreen mode */
  552. if (state->fullscreen == true &&
  553. core_freesync->map[index].user_enable.enable_for_gaming &&
  554. core_freesync->map[index].caps->btr_supported &&
  555. state->btr.btr_active) {
  556. /* TODO: pass in flag for Pre-DCE12 ASIC
  557. * in order for frame variable duration to take affect,
  558. * it needs to be done one VSYNC early, which is at
  559. * frameCounter == 1.
  560. * For DCE12 and newer updates to V_TOTAL_MIN/MAX
  561. * will take affect on current frame
  562. */
  563. if (state->btr.frames_to_insert == state->btr.frame_counter) {
  564. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  565. (1000000000ULL * 1000000),
  566. state->nominal_refresh_rate_in_micro_hz)));
  567. calc_vmin_vmax(core_freesync, *streams, &vmin, &vmax);
  568. inserted_frame_v_total = vmin;
  569. if (min_frame_duration_in_ns / 1000)
  570. inserted_frame_v_total =
  571. state->btr.inserted_frame_duration_in_us *
  572. vmin / (min_frame_duration_in_ns / 1000);
  573. /* Set length of inserted frames as v_total_max*/
  574. vmax = inserted_frame_v_total;
  575. vmin = inserted_frame_v_total;
  576. /* Program V_TOTAL */
  577. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  578. core_freesync->dc, streams,
  579. num_streams, vmin, vmax);
  580. }
  581. if (state->btr.frame_counter > 0)
  582. state->btr.frame_counter--;
  583. /* Restore FreeSync */
  584. if (state->btr.frame_counter == 0)
  585. set_freesync_on_streams(core_freesync, streams, num_streams);
  586. }
  587. /* If in fullscreen freesync mode or in video, do not program
  588. * static screen ramp values
  589. */
  590. if (state->fullscreen == true || state->video == true) {
  591. state->static_ramp.ramp_is_active = false;
  592. return;
  593. }
  594. /* Gradual Static Screen Ramping Logic */
  595. /* Execute if ramp is active and user enabled freesync static screen*/
  596. if (state->static_ramp.ramp_is_active &&
  597. core_freesync->map[index].user_enable.enable_for_static) {
  598. calc_v_total_for_static_ramp(core_freesync, streams[0],
  599. index, &v_total);
  600. /* Update the freesync context for the stream */
  601. update_stream_freesync_context(core_freesync, streams[0]);
  602. /* Program static screen ramp values */
  603. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  604. core_freesync->dc, streams,
  605. num_streams, v_total,
  606. v_total);
  607. triggers.overlay_update = true;
  608. triggers.surface_update = true;
  609. core_freesync->dc->stream_funcs.set_static_screen_events(
  610. core_freesync->dc, streams, num_streams,
  611. &triggers);
  612. }
  613. }
  614. void mod_freesync_update_state(struct mod_freesync *mod_freesync,
  615. const struct dc_stream **streams, int num_streams,
  616. struct mod_freesync_params *freesync_params)
  617. {
  618. bool freesync_program_required = false;
  619. unsigned int stream_index;
  620. struct freesync_state *state;
  621. struct core_freesync *core_freesync = NULL;
  622. if (mod_freesync == NULL)
  623. return;
  624. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  625. if (core_freesync->num_entities == 0)
  626. return;
  627. for(stream_index = 0; stream_index < num_streams; stream_index++) {
  628. unsigned int map_index = map_index_from_stream(core_freesync,
  629. streams[stream_index]);
  630. state = &core_freesync->map[map_index].state;
  631. switch (freesync_params->state){
  632. case FREESYNC_STATE_FULLSCREEN:
  633. state->fullscreen = freesync_params->enable;
  634. freesync_program_required = true;
  635. state->windowed_fullscreen =
  636. freesync_params->windowed_fullscreen;
  637. break;
  638. case FREESYNC_STATE_STATIC_SCREEN:
  639. /* Static screen ramp is only enabled for embedded
  640. * panels. Also change core variables only if there
  641. * is a change.
  642. */
  643. if (dc_is_embedded_signal(
  644. streams[stream_index]->sink->sink_signal) &&
  645. state->static_screen !=
  646. freesync_params->enable) {
  647. /* Change the state flag */
  648. state->static_screen = freesync_params->enable;
  649. /* Change static screen ramp variables */
  650. set_static_ramp_variables(core_freesync,
  651. map_index,
  652. freesync_params->enable);
  653. }
  654. /* We program the ramp starting next VUpdate */
  655. break;
  656. case FREESYNC_STATE_VIDEO:
  657. /* Change core variables only if there is a change*/
  658. if(freesync_params->update_duration_in_ns !=
  659. state->time.update_duration_in_ns) {
  660. state->video = freesync_params->enable;
  661. state->time.update_duration_in_ns =
  662. freesync_params->update_duration_in_ns;
  663. freesync_program_required = true;
  664. }
  665. break;
  666. case FREESYNC_STATE_NONE:
  667. /* handle here to avoid warning */
  668. break;
  669. }
  670. }
  671. if (freesync_program_required)
  672. /* Program freesync according to current state*/
  673. set_freesync_on_streams(core_freesync, streams, num_streams);
  674. }
  675. bool mod_freesync_get_state(struct mod_freesync *mod_freesync,
  676. const struct dc_stream *stream,
  677. struct mod_freesync_params *freesync_params)
  678. {
  679. unsigned int index = 0;
  680. struct core_freesync *core_freesync = NULL;
  681. if (mod_freesync == NULL)
  682. return false;
  683. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  684. index = map_index_from_stream(core_freesync, stream);
  685. if (core_freesync->map[index].state.fullscreen) {
  686. freesync_params->state = FREESYNC_STATE_FULLSCREEN;
  687. freesync_params->enable = true;
  688. } else if (core_freesync->map[index].state.static_screen) {
  689. freesync_params->state = FREESYNC_STATE_STATIC_SCREEN;
  690. freesync_params->enable = true;
  691. } else if (core_freesync->map[index].state.video) {
  692. freesync_params->state = FREESYNC_STATE_VIDEO;
  693. freesync_params->enable = true;
  694. } else {
  695. freesync_params->state = FREESYNC_STATE_NONE;
  696. freesync_params->enable = false;
  697. }
  698. freesync_params->update_duration_in_ns =
  699. core_freesync->map[index].state.time.update_duration_in_ns;
  700. freesync_params->windowed_fullscreen =
  701. core_freesync->map[index].state.windowed_fullscreen;
  702. return true;
  703. }
  704. bool mod_freesync_set_user_enable(struct mod_freesync *mod_freesync,
  705. const struct dc_stream **streams, int num_streams,
  706. struct mod_freesync_user_enable *user_enable)
  707. {
  708. unsigned int stream_index, map_index;
  709. int persistent_data = 0;
  710. struct persistent_data_flag flag;
  711. struct core_dc *core_dc = NULL;
  712. struct core_freesync *core_freesync = NULL;
  713. if (mod_freesync == NULL)
  714. return false;
  715. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  716. core_dc = DC_TO_CORE(core_freesync->dc);
  717. flag.save_per_edid = true;
  718. flag.save_per_link = false;
  719. for(stream_index = 0; stream_index < num_streams;
  720. stream_index++){
  721. map_index = map_index_from_stream(core_freesync,
  722. streams[stream_index]);
  723. core_freesync->map[map_index].user_enable = *user_enable;
  724. /* Write persistent data in registry*/
  725. if (core_freesync->map[map_index].user_enable.
  726. enable_for_gaming)
  727. persistent_data = persistent_data | 1;
  728. if (core_freesync->map[map_index].user_enable.
  729. enable_for_static)
  730. persistent_data = persistent_data | 2;
  731. if (core_freesync->map[map_index].user_enable.
  732. enable_for_video)
  733. persistent_data = persistent_data | 4;
  734. dm_write_persistent_data(core_dc->ctx,
  735. streams[stream_index]->sink,
  736. FREESYNC_REGISTRY_NAME,
  737. "userenable",
  738. &persistent_data,
  739. sizeof(int),
  740. &flag);
  741. }
  742. set_freesync_on_streams(core_freesync, streams, num_streams);
  743. return true;
  744. }
  745. bool mod_freesync_get_user_enable(struct mod_freesync *mod_freesync,
  746. const struct dc_stream *stream,
  747. struct mod_freesync_user_enable *user_enable)
  748. {
  749. unsigned int index = 0;
  750. struct core_freesync *core_freesync = NULL;
  751. if (mod_freesync == NULL)
  752. return false;
  753. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  754. index = map_index_from_stream(core_freesync, stream);
  755. *user_enable = core_freesync->map[index].user_enable;
  756. return true;
  757. }
  758. void mod_freesync_notify_mode_change(struct mod_freesync *mod_freesync,
  759. const struct dc_stream **streams, int num_streams)
  760. {
  761. unsigned int stream_index, map_index;
  762. unsigned min_frame_duration_in_ns, max_frame_duration_in_ns;
  763. struct freesync_state *state;
  764. struct core_freesync *core_freesync = NULL;
  765. if (mod_freesync == NULL)
  766. return;
  767. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  768. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  769. map_index = map_index_from_stream(core_freesync,
  770. streams[stream_index]);
  771. state = &core_freesync->map[map_index].state;
  772. if (core_freesync->map[map_index].caps->supported) {
  773. /* Update the field rate for new timing */
  774. unsigned long long temp;
  775. temp = streams[stream_index]->timing.pix_clk_khz;
  776. temp *= 1000ULL * 1000ULL * 1000ULL;
  777. temp = div_u64(temp, streams[stream_index]->timing.h_total);
  778. temp = div_u64(temp, streams[stream_index]->timing.v_total);
  779. state->nominal_refresh_rate_in_micro_hz = (unsigned int) temp;
  780. /* Update the stream */
  781. update_stream(core_freesync, streams[stream_index]);
  782. /* Determine whether BTR can be supported */
  783. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  784. (1000000000ULL * 1000000),
  785. state->nominal_refresh_rate_in_micro_hz)));
  786. max_frame_duration_in_ns = ((unsigned int) (div64_u64(
  787. (1000000000ULL * 1000000),
  788. core_freesync->map[map_index].caps->min_refresh_in_micro_hz)));
  789. if (max_frame_duration_in_ns >=
  790. 2 * min_frame_duration_in_ns)
  791. core_freesync->map[map_index].caps->btr_supported = true;
  792. else
  793. core_freesync->map[map_index].caps->btr_supported = false;
  794. /* Cache the time variables */
  795. state->time.max_render_time_in_us =
  796. max_frame_duration_in_ns / 1000;
  797. state->time.min_render_time_in_us =
  798. min_frame_duration_in_ns / 1000;
  799. state->btr.mid_point_in_us =
  800. (max_frame_duration_in_ns +
  801. min_frame_duration_in_ns) / 2000;
  802. }
  803. }
  804. /* Program freesync according to current state*/
  805. set_freesync_on_streams(core_freesync, streams, num_streams);
  806. }
  807. /* Add the timestamps to the cache and determine whether BTR programming
  808. * is required, depending on the times calculated
  809. */
  810. static void update_timestamps(struct core_freesync *core_freesync,
  811. const struct dc_stream *stream, unsigned int map_index,
  812. unsigned int last_render_time_in_us)
  813. {
  814. struct freesync_state *state = &core_freesync->map[map_index].state;
  815. state->time.render_times[state->time.render_times_index] =
  816. last_render_time_in_us;
  817. state->time.render_times_index++;
  818. if (state->time.render_times_index >= RENDER_TIMES_MAX_COUNT)
  819. state->time.render_times_index = 0;
  820. if (last_render_time_in_us + BTR_EXIT_MARGIN <
  821. state->time.max_render_time_in_us) {
  822. /* Exit Below the Range */
  823. if (state->btr.btr_active) {
  824. state->btr.program_btr = true;
  825. state->btr.btr_active = false;
  826. state->btr.frame_counter = 0;
  827. /* Exit Fixed Refresh mode */
  828. } else if (state->fixed_refresh.fixed_refresh_active) {
  829. state->fixed_refresh.program_fixed_refresh = true;
  830. state->fixed_refresh.fixed_refresh_active = false;
  831. }
  832. } else if (last_render_time_in_us > state->time.max_render_time_in_us) {
  833. /* Enter Below the Range */
  834. if (!state->btr.btr_active &&
  835. core_freesync->map[map_index].caps->btr_supported) {
  836. state->btr.program_btr = true;
  837. state->btr.btr_active = true;
  838. /* Enter Fixed Refresh mode */
  839. } else if (!state->fixed_refresh.fixed_refresh_active &&
  840. !core_freesync->map[map_index].caps->btr_supported) {
  841. state->fixed_refresh.program_fixed_refresh = true;
  842. state->fixed_refresh.fixed_refresh_active = true;
  843. }
  844. }
  845. /* When Below the Range is active, must react on every frame */
  846. if (state->btr.btr_active)
  847. state->btr.program_btr = true;
  848. }
  849. static void apply_below_the_range(struct core_freesync *core_freesync,
  850. const struct dc_stream *stream, unsigned int map_index,
  851. unsigned int last_render_time_in_us)
  852. {
  853. unsigned int inserted_frame_duration_in_us = 0;
  854. unsigned int mid_point_frames_ceil = 0;
  855. unsigned int mid_point_frames_floor = 0;
  856. unsigned int frame_time_in_us = 0;
  857. unsigned int delta_from_mid_point_in_us_1 = 0xFFFFFFFF;
  858. unsigned int delta_from_mid_point_in_us_2 = 0xFFFFFFFF;
  859. unsigned int frames_to_insert = 0;
  860. unsigned int min_frame_duration_in_ns = 0;
  861. struct freesync_state *state = &core_freesync->map[map_index].state;
  862. if (!state->btr.program_btr)
  863. return;
  864. state->btr.program_btr = false;
  865. min_frame_duration_in_ns = ((unsigned int) (div64_u64(
  866. (1000000000ULL * 1000000),
  867. state->nominal_refresh_rate_in_micro_hz)));
  868. /* Program BTR */
  869. /* BTR set to "not active" so disengage */
  870. if (!state->btr.btr_active)
  871. /* Restore FreeSync */
  872. set_freesync_on_streams(core_freesync, &stream, 1);
  873. /* BTR set to "active" so engage */
  874. else {
  875. /* Calculate number of midPoint frames that could fit within
  876. * the render time interval- take ceil of this value
  877. */
  878. mid_point_frames_ceil = (last_render_time_in_us +
  879. state->btr.mid_point_in_us- 1) /
  880. state->btr.mid_point_in_us;
  881. if (mid_point_frames_ceil > 0) {
  882. frame_time_in_us = last_render_time_in_us /
  883. mid_point_frames_ceil;
  884. delta_from_mid_point_in_us_1 = (state->btr.mid_point_in_us >
  885. frame_time_in_us) ?
  886. (state->btr.mid_point_in_us - frame_time_in_us):
  887. (frame_time_in_us - state->btr.mid_point_in_us);
  888. }
  889. /* Calculate number of midPoint frames that could fit within
  890. * the render time interval- take floor of this value
  891. */
  892. mid_point_frames_floor = last_render_time_in_us /
  893. state->btr.mid_point_in_us;
  894. if (mid_point_frames_floor > 0) {
  895. frame_time_in_us = last_render_time_in_us /
  896. mid_point_frames_floor;
  897. delta_from_mid_point_in_us_2 = (state->btr.mid_point_in_us >
  898. frame_time_in_us) ?
  899. (state->btr.mid_point_in_us - frame_time_in_us):
  900. (frame_time_in_us - state->btr.mid_point_in_us);
  901. }
  902. /* Choose number of frames to insert based on how close it
  903. * can get to the mid point of the variable range.
  904. */
  905. if (delta_from_mid_point_in_us_1 < delta_from_mid_point_in_us_2)
  906. frames_to_insert = mid_point_frames_ceil;
  907. else
  908. frames_to_insert = mid_point_frames_floor;
  909. /* Either we've calculated the number of frames to insert,
  910. * or we need to insert min duration frames
  911. */
  912. if (frames_to_insert > 0)
  913. inserted_frame_duration_in_us = last_render_time_in_us /
  914. frames_to_insert;
  915. if (inserted_frame_duration_in_us <
  916. state->time.min_render_time_in_us)
  917. inserted_frame_duration_in_us =
  918. state->time.min_render_time_in_us;
  919. /* Cache the calculated variables */
  920. state->btr.inserted_frame_duration_in_us =
  921. inserted_frame_duration_in_us;
  922. state->btr.frames_to_insert = frames_to_insert;
  923. state->btr.frame_counter = frames_to_insert;
  924. }
  925. }
  926. static void apply_fixed_refresh(struct core_freesync *core_freesync,
  927. const struct dc_stream *stream, unsigned int map_index)
  928. {
  929. unsigned int vmin = 0, vmax = 0;
  930. struct freesync_state *state = &core_freesync->map[map_index].state;
  931. if (!state->fixed_refresh.program_fixed_refresh)
  932. return;
  933. state->fixed_refresh.program_fixed_refresh = false;
  934. /* Program Fixed Refresh */
  935. /* Fixed Refresh set to "not active" so disengage */
  936. if (!state->fixed_refresh.fixed_refresh_active) {
  937. set_freesync_on_streams(core_freesync, &stream, 1);
  938. /* Fixed Refresh set to "active" so engage (fix to max) */
  939. } else {
  940. calc_vmin_vmax(core_freesync, stream, &vmin, &vmax);
  941. vmax = vmin;
  942. core_freesync->dc->stream_funcs.adjust_vmin_vmax(
  943. core_freesync->dc, &stream,
  944. 1, vmin,
  945. vmax);
  946. }
  947. }
  948. void mod_freesync_pre_update_plane_addresses(struct mod_freesync *mod_freesync,
  949. const struct dc_stream **streams, int num_streams,
  950. unsigned int curr_time_stamp_in_us)
  951. {
  952. unsigned int stream_index, map_index, last_render_time_in_us = 0;
  953. struct core_freesync *core_freesync = NULL;
  954. if (mod_freesync == NULL)
  955. return;
  956. core_freesync = MOD_FREESYNC_TO_CORE(mod_freesync);
  957. for (stream_index = 0; stream_index < num_streams; stream_index++) {
  958. map_index = map_index_from_stream(core_freesync,
  959. streams[stream_index]);
  960. if (core_freesync->map[map_index].caps->supported) {
  961. last_render_time_in_us = curr_time_stamp_in_us -
  962. core_freesync->map[map_index].state.time.
  963. prev_time_stamp_in_us;
  964. /* Add the timestamps to the cache and determine
  965. * whether BTR program is required
  966. */
  967. update_timestamps(core_freesync, streams[stream_index],
  968. map_index, last_render_time_in_us);
  969. if (core_freesync->map[map_index].state.fullscreen &&
  970. core_freesync->map[map_index].user_enable.
  971. enable_for_gaming) {
  972. if (core_freesync->map[map_index].caps->btr_supported) {
  973. apply_below_the_range(core_freesync,
  974. streams[stream_index], map_index,
  975. last_render_time_in_us);
  976. } else {
  977. apply_fixed_refresh(core_freesync,
  978. streams[stream_index], map_index);
  979. }
  980. }
  981. core_freesync->map[map_index].state.time.
  982. prev_time_stamp_in_us = curr_time_stamp_in_us;
  983. }
  984. }
  985. }