dce_clocks.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. /*
  2. * Copyright 2012-16 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 "dce_clocks.h"
  26. #include "dm_services.h"
  27. #include "reg_helper.h"
  28. #include "fixed32_32.h"
  29. #include "bios_parser_interface.h"
  30. #include "dc.h"
  31. #include "dmcu.h"
  32. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  33. #include "dcn_calcs.h"
  34. #endif
  35. #include "core_types.h"
  36. #include "dc_types.h"
  37. #define TO_DCE_CLOCKS(clocks)\
  38. container_of(clocks, struct dce_disp_clk, base)
  39. #define REG(reg) \
  40. (clk_dce->regs->reg)
  41. #undef FN
  42. #define FN(reg_name, field_name) \
  43. clk_dce->clk_shift->field_name, clk_dce->clk_mask->field_name
  44. #define CTX \
  45. clk_dce->base.ctx
  46. #define DC_LOGGER \
  47. clk->ctx->logger
  48. /* Max clock values for each state indexed by "enum clocks_state": */
  49. static const struct state_dependent_clocks dce80_max_clks_by_state[] = {
  50. /* ClocksStateInvalid - should not be used */
  51. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  52. /* ClocksStateUltraLow - not expected to be used for DCE 8.0 */
  53. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  54. /* ClocksStateLow */
  55. { .display_clk_khz = 352000, .pixel_clk_khz = 330000},
  56. /* ClocksStateNominal */
  57. { .display_clk_khz = 600000, .pixel_clk_khz = 400000 },
  58. /* ClocksStatePerformance */
  59. { .display_clk_khz = 600000, .pixel_clk_khz = 400000 } };
  60. static const struct state_dependent_clocks dce110_max_clks_by_state[] = {
  61. /*ClocksStateInvalid - should not be used*/
  62. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  63. /*ClocksStateUltraLow - currently by HW design team not supposed to be used*/
  64. { .display_clk_khz = 352000, .pixel_clk_khz = 330000 },
  65. /*ClocksStateLow*/
  66. { .display_clk_khz = 352000, .pixel_clk_khz = 330000 },
  67. /*ClocksStateNominal*/
  68. { .display_clk_khz = 467000, .pixel_clk_khz = 400000 },
  69. /*ClocksStatePerformance*/
  70. { .display_clk_khz = 643000, .pixel_clk_khz = 400000 } };
  71. static const struct state_dependent_clocks dce112_max_clks_by_state[] = {
  72. /*ClocksStateInvalid - should not be used*/
  73. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  74. /*ClocksStateUltraLow - currently by HW design team not supposed to be used*/
  75. { .display_clk_khz = 389189, .pixel_clk_khz = 346672 },
  76. /*ClocksStateLow*/
  77. { .display_clk_khz = 459000, .pixel_clk_khz = 400000 },
  78. /*ClocksStateNominal*/
  79. { .display_clk_khz = 667000, .pixel_clk_khz = 600000 },
  80. /*ClocksStatePerformance*/
  81. { .display_clk_khz = 1132000, .pixel_clk_khz = 600000 } };
  82. static const struct state_dependent_clocks dce120_max_clks_by_state[] = {
  83. /*ClocksStateInvalid - should not be used*/
  84. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  85. /*ClocksStateUltraLow - currently by HW design team not supposed to be used*/
  86. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  87. /*ClocksStateLow*/
  88. { .display_clk_khz = 460000, .pixel_clk_khz = 400000 },
  89. /*ClocksStateNominal*/
  90. { .display_clk_khz = 670000, .pixel_clk_khz = 600000 },
  91. /*ClocksStatePerformance*/
  92. { .display_clk_khz = 1133000, .pixel_clk_khz = 600000 } };
  93. /* Starting point for each divider range.*/
  94. enum dce_divider_range_start {
  95. DIVIDER_RANGE_01_START = 200, /* 2.00*/
  96. DIVIDER_RANGE_02_START = 1600, /* 16.00*/
  97. DIVIDER_RANGE_03_START = 3200, /* 32.00*/
  98. DIVIDER_RANGE_SCALE_FACTOR = 100 /* Results are scaled up by 100.*/
  99. };
  100. /* Ranges for divider identifiers (Divider ID or DID)
  101. mmDENTIST_DISPCLK_CNTL.DENTIST_DISPCLK_WDIVIDER*/
  102. enum dce_divider_id_register_setting {
  103. DIVIDER_RANGE_01_BASE_DIVIDER_ID = 0X08,
  104. DIVIDER_RANGE_02_BASE_DIVIDER_ID = 0X40,
  105. DIVIDER_RANGE_03_BASE_DIVIDER_ID = 0X60,
  106. DIVIDER_RANGE_MAX_DIVIDER_ID = 0X80
  107. };
  108. /* Step size between each divider within a range.
  109. Incrementing the DENTIST_DISPCLK_WDIVIDER by one
  110. will increment the divider by this much.*/
  111. enum dce_divider_range_step_size {
  112. DIVIDER_RANGE_01_STEP_SIZE = 25, /* 0.25*/
  113. DIVIDER_RANGE_02_STEP_SIZE = 50, /* 0.50*/
  114. DIVIDER_RANGE_03_STEP_SIZE = 100 /* 1.00 */
  115. };
  116. static bool dce_divider_range_construct(
  117. struct dce_divider_range *div_range,
  118. int range_start,
  119. int range_step,
  120. int did_min,
  121. int did_max)
  122. {
  123. div_range->div_range_start = range_start;
  124. div_range->div_range_step = range_step;
  125. div_range->did_min = did_min;
  126. div_range->did_max = did_max;
  127. if (div_range->div_range_step == 0) {
  128. div_range->div_range_step = 1;
  129. /*div_range_step cannot be zero*/
  130. BREAK_TO_DEBUGGER();
  131. }
  132. /* Calculate this based on the other inputs.*/
  133. /* See DividerRange.h for explanation of */
  134. /* the relationship between divider id (DID) and a divider.*/
  135. /* Number of Divider IDs = (Maximum Divider ID - Minimum Divider ID)*/
  136. /* Maximum divider identified in this range =
  137. * (Number of Divider IDs)*Step size between dividers
  138. * + The start of this range.*/
  139. div_range->div_range_end = (did_max - did_min) * range_step
  140. + range_start;
  141. return true;
  142. }
  143. static int dce_divider_range_calc_divider(
  144. struct dce_divider_range *div_range,
  145. int did)
  146. {
  147. /* Is this DID within our range?*/
  148. if ((did < div_range->did_min) || (did >= div_range->did_max))
  149. return INVALID_DIVIDER;
  150. return ((did - div_range->did_min) * div_range->div_range_step)
  151. + div_range->div_range_start;
  152. }
  153. static int dce_divider_range_get_divider(
  154. struct dce_divider_range *div_range,
  155. int ranges_num,
  156. int did)
  157. {
  158. int div = INVALID_DIVIDER;
  159. int i;
  160. for (i = 0; i < ranges_num; i++) {
  161. /* Calculate divider with given divider ID*/
  162. div = dce_divider_range_calc_divider(&div_range[i], did);
  163. /* Found a valid return divider*/
  164. if (div != INVALID_DIVIDER)
  165. break;
  166. }
  167. return div;
  168. }
  169. static int dce_clocks_get_dp_ref_freq(struct display_clock *clk)
  170. {
  171. struct dce_disp_clk *clk_dce = TO_DCE_CLOCKS(clk);
  172. int dprefclk_wdivider;
  173. int dprefclk_src_sel;
  174. int dp_ref_clk_khz = 600000;
  175. int target_div = INVALID_DIVIDER;
  176. /* ASSERT DP Reference Clock source is from DFS*/
  177. REG_GET(DPREFCLK_CNTL, DPREFCLK_SRC_SEL, &dprefclk_src_sel);
  178. ASSERT(dprefclk_src_sel == 0);
  179. /* Read the mmDENTIST_DISPCLK_CNTL to get the currently
  180. * programmed DID DENTIST_DPREFCLK_WDIVIDER*/
  181. REG_GET(DENTIST_DISPCLK_CNTL, DENTIST_DPREFCLK_WDIVIDER, &dprefclk_wdivider);
  182. /* Convert DENTIST_DPREFCLK_WDIVIDERto actual divider*/
  183. target_div = dce_divider_range_get_divider(
  184. clk_dce->divider_ranges,
  185. DIVIDER_RANGE_MAX,
  186. dprefclk_wdivider);
  187. if (target_div != INVALID_DIVIDER) {
  188. /* Calculate the current DFS clock, in kHz.*/
  189. dp_ref_clk_khz = (DIVIDER_RANGE_SCALE_FACTOR
  190. * clk_dce->dentist_vco_freq_khz) / target_div;
  191. }
  192. /* SW will adjust DP REF Clock average value for all purposes
  193. * (DP DTO / DP Audio DTO and DP GTC)
  194. if clock is spread for all cases:
  195. -if SS enabled on DP Ref clock and HW de-spreading enabled with SW
  196. calculations for DS_INCR/DS_MODULO (this is planned to be default case)
  197. -if SS enabled on DP Ref clock and HW de-spreading enabled with HW
  198. calculations (not planned to be used, but average clock should still
  199. be valid)
  200. -if SS enabled on DP Ref clock and HW de-spreading disabled
  201. (should not be case with CIK) then SW should program all rates
  202. generated according to average value (case as with previous ASICs)
  203. */
  204. if (clk_dce->ss_on_dprefclk && clk_dce->dprefclk_ss_divider != 0) {
  205. struct fixed32_32 ss_percentage = dal_fixed32_32_div_int(
  206. dal_fixed32_32_from_fraction(
  207. clk_dce->dprefclk_ss_percentage,
  208. clk_dce->dprefclk_ss_divider), 200);
  209. struct fixed32_32 adj_dp_ref_clk_khz;
  210. ss_percentage = dal_fixed32_32_sub(dal_fixed32_32_one,
  211. ss_percentage);
  212. adj_dp_ref_clk_khz =
  213. dal_fixed32_32_mul_int(
  214. ss_percentage,
  215. dp_ref_clk_khz);
  216. dp_ref_clk_khz = dal_fixed32_32_floor(adj_dp_ref_clk_khz);
  217. }
  218. return dp_ref_clk_khz;
  219. }
  220. /* TODO: This is DCN DPREFCLK: it could be program by DENTIST by VBIOS
  221. * or CLK0_CLK11 by SMU. For DCE120, it is wlays 600Mhz. Will re-visit
  222. * clock implementation
  223. */
  224. static int dce_clocks_get_dp_ref_freq_wrkaround(struct display_clock *clk)
  225. {
  226. struct dce_disp_clk *clk_dce = TO_DCE_CLOCKS(clk);
  227. int dp_ref_clk_khz = 600000;
  228. if (clk_dce->ss_on_dprefclk && clk_dce->dprefclk_ss_divider != 0) {
  229. struct fixed32_32 ss_percentage = dal_fixed32_32_div_int(
  230. dal_fixed32_32_from_fraction(
  231. clk_dce->dprefclk_ss_percentage,
  232. clk_dce->dprefclk_ss_divider), 200);
  233. struct fixed32_32 adj_dp_ref_clk_khz;
  234. ss_percentage = dal_fixed32_32_sub(dal_fixed32_32_one,
  235. ss_percentage);
  236. adj_dp_ref_clk_khz =
  237. dal_fixed32_32_mul_int(
  238. ss_percentage,
  239. dp_ref_clk_khz);
  240. dp_ref_clk_khz = dal_fixed32_32_floor(adj_dp_ref_clk_khz);
  241. }
  242. return dp_ref_clk_khz;
  243. }
  244. static enum dm_pp_clocks_state dce_get_required_clocks_state(
  245. struct display_clock *clk,
  246. struct state_dependent_clocks *req_clocks)
  247. {
  248. struct dce_disp_clk *clk_dce = TO_DCE_CLOCKS(clk);
  249. int i;
  250. enum dm_pp_clocks_state low_req_clk;
  251. /* Iterate from highest supported to lowest valid state, and update
  252. * lowest RequiredState with the lowest state that satisfies
  253. * all required clocks
  254. */
  255. for (i = clk->max_clks_state; i >= DM_PP_CLOCKS_STATE_ULTRA_LOW; i--)
  256. if (req_clocks->display_clk_khz >
  257. clk_dce->max_clks_by_state[i].display_clk_khz
  258. || req_clocks->pixel_clk_khz >
  259. clk_dce->max_clks_by_state[i].pixel_clk_khz)
  260. break;
  261. low_req_clk = i + 1;
  262. if (low_req_clk > clk->max_clks_state) {
  263. DC_LOG_WARNING("%s: clocks unsupported disp_clk %d pix_clk %d",
  264. __func__,
  265. req_clocks->display_clk_khz,
  266. req_clocks->pixel_clk_khz);
  267. low_req_clk = DM_PP_CLOCKS_STATE_INVALID;
  268. }
  269. return low_req_clk;
  270. }
  271. static bool dce_clock_set_min_clocks_state(
  272. struct display_clock *clk,
  273. enum dm_pp_clocks_state clocks_state)
  274. {
  275. struct dm_pp_power_level_change_request level_change_req = {
  276. clocks_state };
  277. if (clocks_state > clk->max_clks_state) {
  278. /*Requested state exceeds max supported state.*/
  279. DC_LOG_WARNING("Requested state exceeds max supported state");
  280. return false;
  281. } else if (clocks_state == clk->cur_min_clks_state) {
  282. /*if we're trying to set the same state, we can just return
  283. * since nothing needs to be done*/
  284. return true;
  285. }
  286. /* get max clock state from PPLIB */
  287. if (dm_pp_apply_power_level_change_request(clk->ctx, &level_change_req))
  288. clk->cur_min_clks_state = clocks_state;
  289. return true;
  290. }
  291. static int dce_set_clock(
  292. struct display_clock *clk,
  293. int requested_clk_khz)
  294. {
  295. struct dce_disp_clk *clk_dce = TO_DCE_CLOCKS(clk);
  296. struct bp_pixel_clock_parameters pxl_clk_params = { 0 };
  297. struct dc_bios *bp = clk->ctx->dc_bios;
  298. int actual_clock = requested_clk_khz;
  299. /* Make sure requested clock isn't lower than minimum threshold*/
  300. if (requested_clk_khz > 0)
  301. requested_clk_khz = max(requested_clk_khz,
  302. clk_dce->dentist_vco_freq_khz / 64);
  303. /* Prepare to program display clock*/
  304. pxl_clk_params.target_pixel_clock = requested_clk_khz;
  305. pxl_clk_params.pll_id = CLOCK_SOURCE_ID_DFS;
  306. bp->funcs->program_display_engine_pll(bp, &pxl_clk_params);
  307. if (clk_dce->dfs_bypass_enabled) {
  308. /* Cache the fixed display clock*/
  309. clk_dce->dfs_bypass_disp_clk =
  310. pxl_clk_params.dfs_bypass_display_clock;
  311. actual_clock = pxl_clk_params.dfs_bypass_display_clock;
  312. }
  313. /* from power down, we need mark the clock state as ClocksStateNominal
  314. * from HWReset, so when resume we will call pplib voltage regulator.*/
  315. if (requested_clk_khz == 0)
  316. clk->cur_min_clks_state = DM_PP_CLOCKS_STATE_NOMINAL;
  317. return actual_clock;
  318. }
  319. static int dce_psr_set_clock(
  320. struct display_clock *clk,
  321. int requested_clk_khz)
  322. {
  323. struct dce_disp_clk *clk_dce = TO_DCE_CLOCKS(clk);
  324. struct dc_context *ctx = clk_dce->base.ctx;
  325. struct dc *core_dc = ctx->dc;
  326. struct dmcu *dmcu = core_dc->res_pool->dmcu;
  327. int actual_clk_khz = requested_clk_khz;
  328. actual_clk_khz = dce_set_clock(clk, requested_clk_khz);
  329. dmcu->funcs->set_psr_wait_loop(dmcu, actual_clk_khz / 1000 / 7);
  330. return actual_clk_khz;
  331. }
  332. static int dce112_set_clock(
  333. struct display_clock *clk,
  334. int requested_clk_khz)
  335. {
  336. struct dce_disp_clk *clk_dce = TO_DCE_CLOCKS(clk);
  337. struct bp_set_dce_clock_parameters dce_clk_params;
  338. struct dc_bios *bp = clk->ctx->dc_bios;
  339. struct dc *core_dc = clk->ctx->dc;
  340. struct dmcu *dmcu = core_dc->res_pool->dmcu;
  341. int actual_clock = requested_clk_khz;
  342. /* Prepare to program display clock*/
  343. memset(&dce_clk_params, 0, sizeof(dce_clk_params));
  344. /* Make sure requested clock isn't lower than minimum threshold*/
  345. if (requested_clk_khz > 0)
  346. requested_clk_khz = max(requested_clk_khz,
  347. clk_dce->dentist_vco_freq_khz / 62);
  348. dce_clk_params.target_clock_frequency = requested_clk_khz;
  349. dce_clk_params.pll_id = CLOCK_SOURCE_ID_DFS;
  350. dce_clk_params.clock_type = DCECLOCK_TYPE_DISPLAY_CLOCK;
  351. bp->funcs->set_dce_clock(bp, &dce_clk_params);
  352. actual_clock = dce_clk_params.target_clock_frequency;
  353. /* from power down, we need mark the clock state as ClocksStateNominal
  354. * from HWReset, so when resume we will call pplib voltage regulator.*/
  355. if (requested_clk_khz == 0)
  356. clk->cur_min_clks_state = DM_PP_CLOCKS_STATE_NOMINAL;
  357. /*Program DP ref Clock*/
  358. /*VBIOS will determine DPREFCLK frequency, so we don't set it*/
  359. dce_clk_params.target_clock_frequency = 0;
  360. dce_clk_params.clock_type = DCECLOCK_TYPE_DPREFCLK;
  361. dce_clk_params.flags.USE_GENLOCK_AS_SOURCE_FOR_DPREFCLK =
  362. (dce_clk_params.pll_id ==
  363. CLOCK_SOURCE_COMBO_DISPLAY_PLL0);
  364. bp->funcs->set_dce_clock(bp, &dce_clk_params);
  365. if (!IS_FPGA_MAXIMUS_DC(core_dc->ctx->dce_environment)) {
  366. if (clk_dce->dfs_bypass_disp_clk != actual_clock)
  367. dmcu->funcs->set_psr_wait_loop(dmcu,
  368. actual_clock / 1000 / 7);
  369. }
  370. clk_dce->dfs_bypass_disp_clk = actual_clock;
  371. return actual_clock;
  372. }
  373. static void dce_clock_read_integrated_info(struct dce_disp_clk *clk_dce)
  374. {
  375. struct dc_debug *debug = &clk_dce->base.ctx->dc->debug;
  376. struct dc_bios *bp = clk_dce->base.ctx->dc_bios;
  377. struct integrated_info info = { { { 0 } } };
  378. struct dc_firmware_info fw_info = { { 0 } };
  379. int i;
  380. if (bp->integrated_info)
  381. info = *bp->integrated_info;
  382. clk_dce->dentist_vco_freq_khz = info.dentist_vco_freq;
  383. if (clk_dce->dentist_vco_freq_khz == 0) {
  384. bp->funcs->get_firmware_info(bp, &fw_info);
  385. clk_dce->dentist_vco_freq_khz =
  386. fw_info.smu_gpu_pll_output_freq;
  387. if (clk_dce->dentist_vco_freq_khz == 0)
  388. clk_dce->dentist_vco_freq_khz = 3600000;
  389. }
  390. /*update the maximum display clock for each power state*/
  391. for (i = 0; i < NUMBER_OF_DISP_CLK_VOLTAGE; ++i) {
  392. enum dm_pp_clocks_state clk_state = DM_PP_CLOCKS_STATE_INVALID;
  393. switch (i) {
  394. case 0:
  395. clk_state = DM_PP_CLOCKS_STATE_ULTRA_LOW;
  396. break;
  397. case 1:
  398. clk_state = DM_PP_CLOCKS_STATE_LOW;
  399. break;
  400. case 2:
  401. clk_state = DM_PP_CLOCKS_STATE_NOMINAL;
  402. break;
  403. case 3:
  404. clk_state = DM_PP_CLOCKS_STATE_PERFORMANCE;
  405. break;
  406. default:
  407. clk_state = DM_PP_CLOCKS_STATE_INVALID;
  408. break;
  409. }
  410. /*Do not allow bad VBIOS/SBIOS to override with invalid values,
  411. * check for > 100MHz*/
  412. if (info.disp_clk_voltage[i].max_supported_clk >= 100000)
  413. clk_dce->max_clks_by_state[clk_state].display_clk_khz =
  414. info.disp_clk_voltage[i].max_supported_clk;
  415. }
  416. if (!debug->disable_dfs_bypass && bp->integrated_info)
  417. if (bp->integrated_info->gpu_cap_info & DFS_BYPASS_ENABLE)
  418. clk_dce->dfs_bypass_enabled = true;
  419. clk_dce->use_max_disp_clk = debug->max_disp_clk;
  420. }
  421. static void dce_clock_read_ss_info(struct dce_disp_clk *clk_dce)
  422. {
  423. struct dc_bios *bp = clk_dce->base.ctx->dc_bios;
  424. int ss_info_num = bp->funcs->get_ss_entry_number(
  425. bp, AS_SIGNAL_TYPE_GPU_PLL);
  426. if (ss_info_num) {
  427. struct spread_spectrum_info info = { { 0 } };
  428. enum bp_result result = bp->funcs->get_spread_spectrum_info(
  429. bp, AS_SIGNAL_TYPE_GPU_PLL, 0, &info);
  430. /* Based on VBIOS, VBIOS will keep entry for GPU PLL SS
  431. * even if SS not enabled and in that case
  432. * SSInfo.spreadSpectrumPercentage !=0 would be sign
  433. * that SS is enabled
  434. */
  435. if (result == BP_RESULT_OK &&
  436. info.spread_spectrum_percentage != 0) {
  437. clk_dce->ss_on_dprefclk = true;
  438. clk_dce->dprefclk_ss_divider = info.spread_percentage_divider;
  439. if (info.type.CENTER_MODE == 0) {
  440. /* TODO: Currently for DP Reference clock we
  441. * need only SS percentage for
  442. * downspread */
  443. clk_dce->dprefclk_ss_percentage =
  444. info.spread_spectrum_percentage;
  445. }
  446. return;
  447. }
  448. result = bp->funcs->get_spread_spectrum_info(
  449. bp, AS_SIGNAL_TYPE_DISPLAY_PORT, 0, &info);
  450. /* Based on VBIOS, VBIOS will keep entry for DPREFCLK SS
  451. * even if SS not enabled and in that case
  452. * SSInfo.spreadSpectrumPercentage !=0 would be sign
  453. * that SS is enabled
  454. */
  455. if (result == BP_RESULT_OK &&
  456. info.spread_spectrum_percentage != 0) {
  457. clk_dce->ss_on_dprefclk = true;
  458. clk_dce->dprefclk_ss_divider = info.spread_percentage_divider;
  459. if (info.type.CENTER_MODE == 0) {
  460. /* Currently for DP Reference clock we
  461. * need only SS percentage for
  462. * downspread */
  463. clk_dce->dprefclk_ss_percentage =
  464. info.spread_spectrum_percentage;
  465. }
  466. }
  467. }
  468. }
  469. static bool dce_apply_clock_voltage_request(
  470. struct display_clock *clk,
  471. enum dm_pp_clock_type clocks_type,
  472. int clocks_in_khz,
  473. bool pre_mode_set,
  474. bool update_dp_phyclk)
  475. {
  476. bool send_request = false;
  477. struct dm_pp_clock_for_voltage_req clock_voltage_req = {0};
  478. switch (clocks_type) {
  479. case DM_PP_CLOCK_TYPE_DISPLAY_CLK:
  480. case DM_PP_CLOCK_TYPE_PIXELCLK:
  481. case DM_PP_CLOCK_TYPE_DISPLAYPHYCLK:
  482. break;
  483. default:
  484. BREAK_TO_DEBUGGER();
  485. return false;
  486. }
  487. clock_voltage_req.clk_type = clocks_type;
  488. clock_voltage_req.clocks_in_khz = clocks_in_khz;
  489. /* to pplib */
  490. if (pre_mode_set) {
  491. switch (clocks_type) {
  492. case DM_PP_CLOCK_TYPE_DISPLAY_CLK:
  493. if (clocks_in_khz > clk->cur_clocks_value.dispclk_in_khz) {
  494. clk->cur_clocks_value.dispclk_notify_pplib_done = true;
  495. send_request = true;
  496. } else
  497. clk->cur_clocks_value.dispclk_notify_pplib_done = false;
  498. /* no matter incrase or decrase clock, update current clock value */
  499. clk->cur_clocks_value.dispclk_in_khz = clocks_in_khz;
  500. break;
  501. case DM_PP_CLOCK_TYPE_PIXELCLK:
  502. if (clocks_in_khz > clk->cur_clocks_value.max_pixelclk_in_khz) {
  503. clk->cur_clocks_value.pixelclk_notify_pplib_done = true;
  504. send_request = true;
  505. } else
  506. clk->cur_clocks_value.pixelclk_notify_pplib_done = false;
  507. /* no matter incrase or decrase clock, update current clock value */
  508. clk->cur_clocks_value.max_pixelclk_in_khz = clocks_in_khz;
  509. break;
  510. case DM_PP_CLOCK_TYPE_DISPLAYPHYCLK:
  511. if (clocks_in_khz > clk->cur_clocks_value.max_non_dp_phyclk_in_khz) {
  512. clk->cur_clocks_value.phyclk_notigy_pplib_done = true;
  513. send_request = true;
  514. } else
  515. clk->cur_clocks_value.phyclk_notigy_pplib_done = false;
  516. /* no matter incrase or decrase clock, update current clock value */
  517. clk->cur_clocks_value.max_non_dp_phyclk_in_khz = clocks_in_khz;
  518. break;
  519. default:
  520. ASSERT(0);
  521. break;
  522. }
  523. } else {
  524. switch (clocks_type) {
  525. case DM_PP_CLOCK_TYPE_DISPLAY_CLK:
  526. if (!clk->cur_clocks_value.dispclk_notify_pplib_done)
  527. send_request = true;
  528. break;
  529. case DM_PP_CLOCK_TYPE_PIXELCLK:
  530. if (!clk->cur_clocks_value.pixelclk_notify_pplib_done)
  531. send_request = true;
  532. break;
  533. case DM_PP_CLOCK_TYPE_DISPLAYPHYCLK:
  534. if (!clk->cur_clocks_value.phyclk_notigy_pplib_done)
  535. send_request = true;
  536. break;
  537. default:
  538. ASSERT(0);
  539. break;
  540. }
  541. }
  542. if (send_request) {
  543. #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
  544. if (clk->ctx->dce_version >= DCN_VERSION_1_0) {
  545. struct dc *core_dc = clk->ctx->dc;
  546. /*use dcfclk request voltage*/
  547. clock_voltage_req.clk_type = DM_PP_CLOCK_TYPE_DCFCLK;
  548. clock_voltage_req.clocks_in_khz =
  549. dcn_find_dcfclk_suits_all(core_dc, &clk->cur_clocks_value);
  550. }
  551. #endif
  552. dm_pp_apply_clock_for_voltage_request(
  553. clk->ctx, &clock_voltage_req);
  554. }
  555. if (update_dp_phyclk && (clocks_in_khz >
  556. clk->cur_clocks_value.max_dp_phyclk_in_khz))
  557. clk->cur_clocks_value.max_dp_phyclk_in_khz = clocks_in_khz;
  558. return true;
  559. }
  560. static const struct display_clock_funcs dce120_funcs = {
  561. .get_dp_ref_clk_frequency = dce_clocks_get_dp_ref_freq_wrkaround,
  562. .apply_clock_voltage_request = dce_apply_clock_voltage_request,
  563. .set_clock = dce112_set_clock
  564. };
  565. static const struct display_clock_funcs dce112_funcs = {
  566. .get_dp_ref_clk_frequency = dce_clocks_get_dp_ref_freq,
  567. .get_required_clocks_state = dce_get_required_clocks_state,
  568. .set_min_clocks_state = dce_clock_set_min_clocks_state,
  569. .set_clock = dce112_set_clock
  570. };
  571. static const struct display_clock_funcs dce110_funcs = {
  572. .get_dp_ref_clk_frequency = dce_clocks_get_dp_ref_freq,
  573. .get_required_clocks_state = dce_get_required_clocks_state,
  574. .set_min_clocks_state = dce_clock_set_min_clocks_state,
  575. .set_clock = dce_psr_set_clock
  576. };
  577. static const struct display_clock_funcs dce_funcs = {
  578. .get_dp_ref_clk_frequency = dce_clocks_get_dp_ref_freq,
  579. .get_required_clocks_state = dce_get_required_clocks_state,
  580. .set_min_clocks_state = dce_clock_set_min_clocks_state,
  581. .set_clock = dce_set_clock
  582. };
  583. static void dce_disp_clk_construct(
  584. struct dce_disp_clk *clk_dce,
  585. struct dc_context *ctx,
  586. const struct dce_disp_clk_registers *regs,
  587. const struct dce_disp_clk_shift *clk_shift,
  588. const struct dce_disp_clk_mask *clk_mask)
  589. {
  590. struct display_clock *base = &clk_dce->base;
  591. base->ctx = ctx;
  592. base->funcs = &dce_funcs;
  593. clk_dce->regs = regs;
  594. clk_dce->clk_shift = clk_shift;
  595. clk_dce->clk_mask = clk_mask;
  596. clk_dce->dfs_bypass_disp_clk = 0;
  597. clk_dce->dprefclk_ss_percentage = 0;
  598. clk_dce->dprefclk_ss_divider = 1000;
  599. clk_dce->ss_on_dprefclk = false;
  600. base->max_clks_state = DM_PP_CLOCKS_STATE_NOMINAL;
  601. base->cur_min_clks_state = DM_PP_CLOCKS_STATE_INVALID;
  602. dce_clock_read_integrated_info(clk_dce);
  603. dce_clock_read_ss_info(clk_dce);
  604. dce_divider_range_construct(
  605. &clk_dce->divider_ranges[DIVIDER_RANGE_01],
  606. DIVIDER_RANGE_01_START,
  607. DIVIDER_RANGE_01_STEP_SIZE,
  608. DIVIDER_RANGE_01_BASE_DIVIDER_ID,
  609. DIVIDER_RANGE_02_BASE_DIVIDER_ID);
  610. dce_divider_range_construct(
  611. &clk_dce->divider_ranges[DIVIDER_RANGE_02],
  612. DIVIDER_RANGE_02_START,
  613. DIVIDER_RANGE_02_STEP_SIZE,
  614. DIVIDER_RANGE_02_BASE_DIVIDER_ID,
  615. DIVIDER_RANGE_03_BASE_DIVIDER_ID);
  616. dce_divider_range_construct(
  617. &clk_dce->divider_ranges[DIVIDER_RANGE_03],
  618. DIVIDER_RANGE_03_START,
  619. DIVIDER_RANGE_03_STEP_SIZE,
  620. DIVIDER_RANGE_03_BASE_DIVIDER_ID,
  621. DIVIDER_RANGE_MAX_DIVIDER_ID);
  622. }
  623. struct display_clock *dce_disp_clk_create(
  624. struct dc_context *ctx,
  625. const struct dce_disp_clk_registers *regs,
  626. const struct dce_disp_clk_shift *clk_shift,
  627. const struct dce_disp_clk_mask *clk_mask)
  628. {
  629. struct dce_disp_clk *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  630. if (clk_dce == NULL) {
  631. BREAK_TO_DEBUGGER();
  632. return NULL;
  633. }
  634. memcpy(clk_dce->max_clks_by_state,
  635. dce80_max_clks_by_state,
  636. sizeof(dce80_max_clks_by_state));
  637. dce_disp_clk_construct(
  638. clk_dce, ctx, regs, clk_shift, clk_mask);
  639. return &clk_dce->base;
  640. }
  641. struct display_clock *dce110_disp_clk_create(
  642. struct dc_context *ctx,
  643. const struct dce_disp_clk_registers *regs,
  644. const struct dce_disp_clk_shift *clk_shift,
  645. const struct dce_disp_clk_mask *clk_mask)
  646. {
  647. struct dce_disp_clk *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  648. if (clk_dce == NULL) {
  649. BREAK_TO_DEBUGGER();
  650. return NULL;
  651. }
  652. memcpy(clk_dce->max_clks_by_state,
  653. dce110_max_clks_by_state,
  654. sizeof(dce110_max_clks_by_state));
  655. dce_disp_clk_construct(
  656. clk_dce, ctx, regs, clk_shift, clk_mask);
  657. clk_dce->base.funcs = &dce110_funcs;
  658. return &clk_dce->base;
  659. }
  660. struct display_clock *dce112_disp_clk_create(
  661. struct dc_context *ctx,
  662. const struct dce_disp_clk_registers *regs,
  663. const struct dce_disp_clk_shift *clk_shift,
  664. const struct dce_disp_clk_mask *clk_mask)
  665. {
  666. struct dce_disp_clk *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  667. if (clk_dce == NULL) {
  668. BREAK_TO_DEBUGGER();
  669. return NULL;
  670. }
  671. memcpy(clk_dce->max_clks_by_state,
  672. dce112_max_clks_by_state,
  673. sizeof(dce112_max_clks_by_state));
  674. dce_disp_clk_construct(
  675. clk_dce, ctx, regs, clk_shift, clk_mask);
  676. clk_dce->base.funcs = &dce112_funcs;
  677. return &clk_dce->base;
  678. }
  679. struct display_clock *dce120_disp_clk_create(struct dc_context *ctx)
  680. {
  681. struct dce_disp_clk *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  682. struct dm_pp_clock_levels_with_voltage clk_level_info = {0};
  683. if (clk_dce == NULL) {
  684. BREAK_TO_DEBUGGER();
  685. return NULL;
  686. }
  687. memcpy(clk_dce->max_clks_by_state,
  688. dce120_max_clks_by_state,
  689. sizeof(dce120_max_clks_by_state));
  690. dce_disp_clk_construct(
  691. clk_dce, ctx, NULL, NULL, NULL);
  692. clk_dce->base.funcs = &dce120_funcs;
  693. /* new in dce120 */
  694. if (!ctx->dc->debug.disable_pplib_clock_request &&
  695. dm_pp_get_clock_levels_by_type_with_voltage(
  696. ctx, DM_PP_CLOCK_TYPE_DISPLAY_CLK, &clk_level_info)
  697. && clk_level_info.num_levels)
  698. clk_dce->max_displ_clk_in_khz =
  699. clk_level_info.data[clk_level_info.num_levels - 1].clocks_in_khz;
  700. else
  701. clk_dce->max_displ_clk_in_khz = 1133000;
  702. return &clk_dce->base;
  703. }
  704. void dce_disp_clk_destroy(struct display_clock **disp_clk)
  705. {
  706. struct dce_disp_clk *clk_dce = TO_DCE_CLOCKS(*disp_clk);
  707. kfree(clk_dce);
  708. *disp_clk = NULL;
  709. }