dce_clocks.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  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 "fixed31_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. #include "dal_asic_id.h"
  38. #define TO_DCE_CLOCKS(clocks)\
  39. container_of(clocks, struct dce_dccg, base)
  40. #define REG(reg) \
  41. (clk_dce->regs->reg)
  42. #undef FN
  43. #define FN(reg_name, field_name) \
  44. clk_dce->clk_shift->field_name, clk_dce->clk_mask->field_name
  45. #define CTX \
  46. clk_dce->base.ctx
  47. #define DC_LOGGER \
  48. clk->ctx->logger
  49. /* Max clock values for each state indexed by "enum clocks_state": */
  50. static const struct state_dependent_clocks dce80_max_clks_by_state[] = {
  51. /* ClocksStateInvalid - should not be used */
  52. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  53. /* ClocksStateUltraLow - not expected to be used for DCE 8.0 */
  54. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  55. /* ClocksStateLow */
  56. { .display_clk_khz = 352000, .pixel_clk_khz = 330000},
  57. /* ClocksStateNominal */
  58. { .display_clk_khz = 600000, .pixel_clk_khz = 400000 },
  59. /* ClocksStatePerformance */
  60. { .display_clk_khz = 600000, .pixel_clk_khz = 400000 } };
  61. static const struct state_dependent_clocks dce110_max_clks_by_state[] = {
  62. /*ClocksStateInvalid - should not be used*/
  63. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  64. /*ClocksStateUltraLow - currently by HW design team not supposed to be used*/
  65. { .display_clk_khz = 352000, .pixel_clk_khz = 330000 },
  66. /*ClocksStateLow*/
  67. { .display_clk_khz = 352000, .pixel_clk_khz = 330000 },
  68. /*ClocksStateNominal*/
  69. { .display_clk_khz = 467000, .pixel_clk_khz = 400000 },
  70. /*ClocksStatePerformance*/
  71. { .display_clk_khz = 643000, .pixel_clk_khz = 400000 } };
  72. static const struct state_dependent_clocks dce112_max_clks_by_state[] = {
  73. /*ClocksStateInvalid - should not be used*/
  74. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  75. /*ClocksStateUltraLow - currently by HW design team not supposed to be used*/
  76. { .display_clk_khz = 389189, .pixel_clk_khz = 346672 },
  77. /*ClocksStateLow*/
  78. { .display_clk_khz = 459000, .pixel_clk_khz = 400000 },
  79. /*ClocksStateNominal*/
  80. { .display_clk_khz = 667000, .pixel_clk_khz = 600000 },
  81. /*ClocksStatePerformance*/
  82. { .display_clk_khz = 1132000, .pixel_clk_khz = 600000 } };
  83. static const struct state_dependent_clocks dce120_max_clks_by_state[] = {
  84. /*ClocksStateInvalid - should not be used*/
  85. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  86. /*ClocksStateUltraLow - currently by HW design team not supposed to be used*/
  87. { .display_clk_khz = 0, .pixel_clk_khz = 0 },
  88. /*ClocksStateLow*/
  89. { .display_clk_khz = 460000, .pixel_clk_khz = 400000 },
  90. /*ClocksStateNominal*/
  91. { .display_clk_khz = 670000, .pixel_clk_khz = 600000 },
  92. /*ClocksStatePerformance*/
  93. { .display_clk_khz = 1133000, .pixel_clk_khz = 600000 } };
  94. /* Starting DID for each range */
  95. enum dentist_base_divider_id {
  96. DENTIST_BASE_DID_1 = 0x08,
  97. DENTIST_BASE_DID_2 = 0x40,
  98. DENTIST_BASE_DID_3 = 0x60,
  99. DENTIST_BASE_DID_4 = 0x7e,
  100. DENTIST_MAX_DID = 0x7f
  101. };
  102. /* Starting point and step size for each divider range.*/
  103. enum dentist_divider_range {
  104. DENTIST_DIVIDER_RANGE_1_START = 8, /* 2.00 */
  105. DENTIST_DIVIDER_RANGE_1_STEP = 1, /* 0.25 */
  106. DENTIST_DIVIDER_RANGE_2_START = 64, /* 16.00 */
  107. DENTIST_DIVIDER_RANGE_2_STEP = 2, /* 0.50 */
  108. DENTIST_DIVIDER_RANGE_3_START = 128, /* 32.00 */
  109. DENTIST_DIVIDER_RANGE_3_STEP = 4, /* 1.00 */
  110. DENTIST_DIVIDER_RANGE_4_START = 248, /* 62.00 */
  111. DENTIST_DIVIDER_RANGE_4_STEP = 264, /* 66.00 */
  112. DENTIST_DIVIDER_RANGE_SCALE_FACTOR = 4
  113. };
  114. static int dentist_get_divider_from_did(int did)
  115. {
  116. if (did < DENTIST_BASE_DID_1)
  117. did = DENTIST_BASE_DID_1;
  118. if (did > DENTIST_MAX_DID)
  119. did = DENTIST_MAX_DID;
  120. if (did < DENTIST_BASE_DID_2) {
  121. return DENTIST_DIVIDER_RANGE_1_START + DENTIST_DIVIDER_RANGE_1_STEP
  122. * (did - DENTIST_BASE_DID_1);
  123. } else if (did < DENTIST_BASE_DID_3) {
  124. return DENTIST_DIVIDER_RANGE_2_START + DENTIST_DIVIDER_RANGE_2_STEP
  125. * (did - DENTIST_BASE_DID_2);
  126. } else if (did < DENTIST_BASE_DID_4) {
  127. return DENTIST_DIVIDER_RANGE_3_START + DENTIST_DIVIDER_RANGE_3_STEP
  128. * (did - DENTIST_BASE_DID_3);
  129. } else {
  130. return DENTIST_DIVIDER_RANGE_4_START + DENTIST_DIVIDER_RANGE_4_STEP
  131. * (did - DENTIST_BASE_DID_4);
  132. }
  133. }
  134. /* SW will adjust DP REF Clock average value for all purposes
  135. * (DP DTO / DP Audio DTO and DP GTC)
  136. if clock is spread for all cases:
  137. -if SS enabled on DP Ref clock and HW de-spreading enabled with SW
  138. calculations for DS_INCR/DS_MODULO (this is planned to be default case)
  139. -if SS enabled on DP Ref clock and HW de-spreading enabled with HW
  140. calculations (not planned to be used, but average clock should still
  141. be valid)
  142. -if SS enabled on DP Ref clock and HW de-spreading disabled
  143. (should not be case with CIK) then SW should program all rates
  144. generated according to average value (case as with previous ASICs)
  145. */
  146. static int dccg_adjust_dp_ref_freq_for_ss(struct dce_dccg *clk_dce, int dp_ref_clk_khz)
  147. {
  148. if (clk_dce->ss_on_dprefclk && clk_dce->dprefclk_ss_divider != 0) {
  149. struct fixed31_32 ss_percentage = dc_fixpt_div_int(
  150. dc_fixpt_from_fraction(clk_dce->dprefclk_ss_percentage,
  151. clk_dce->dprefclk_ss_divider), 200);
  152. struct fixed31_32 adj_dp_ref_clk_khz;
  153. ss_percentage = dc_fixpt_sub(dc_fixpt_one, ss_percentage);
  154. adj_dp_ref_clk_khz = dc_fixpt_mul_int(ss_percentage, dp_ref_clk_khz);
  155. dp_ref_clk_khz = dc_fixpt_floor(adj_dp_ref_clk_khz);
  156. }
  157. return dp_ref_clk_khz;
  158. }
  159. static int dce_get_dp_ref_freq_khz(struct dccg *clk)
  160. {
  161. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(clk);
  162. int dprefclk_wdivider;
  163. int dprefclk_src_sel;
  164. int dp_ref_clk_khz = 600000;
  165. int target_div;
  166. /* ASSERT DP Reference Clock source is from DFS*/
  167. REG_GET(DPREFCLK_CNTL, DPREFCLK_SRC_SEL, &dprefclk_src_sel);
  168. ASSERT(dprefclk_src_sel == 0);
  169. /* Read the mmDENTIST_DISPCLK_CNTL to get the currently
  170. * programmed DID DENTIST_DPREFCLK_WDIVIDER*/
  171. REG_GET(DENTIST_DISPCLK_CNTL, DENTIST_DPREFCLK_WDIVIDER, &dprefclk_wdivider);
  172. /* Convert DENTIST_DPREFCLK_WDIVIDERto actual divider*/
  173. target_div = dentist_get_divider_from_did(dprefclk_wdivider);
  174. /* Calculate the current DFS clock, in kHz.*/
  175. dp_ref_clk_khz = (DENTIST_DIVIDER_RANGE_SCALE_FACTOR
  176. * clk_dce->dentist_vco_freq_khz) / target_div;
  177. return dccg_adjust_dp_ref_freq_for_ss(clk_dce, dp_ref_clk_khz);
  178. }
  179. static int dce12_get_dp_ref_freq_khz(struct dccg *clk)
  180. {
  181. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(clk);
  182. return dccg_adjust_dp_ref_freq_for_ss(clk_dce, clk_dce->dprefclk_khz);
  183. }
  184. static enum dm_pp_clocks_state dce_get_required_clocks_state(
  185. struct dccg *clk,
  186. struct dc_clocks *req_clocks)
  187. {
  188. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(clk);
  189. int i;
  190. enum dm_pp_clocks_state low_req_clk;
  191. /* Iterate from highest supported to lowest valid state, and update
  192. * lowest RequiredState with the lowest state that satisfies
  193. * all required clocks
  194. */
  195. for (i = clk->max_clks_state; i >= DM_PP_CLOCKS_STATE_ULTRA_LOW; i--)
  196. if (req_clocks->dispclk_khz >
  197. clk_dce->max_clks_by_state[i].display_clk_khz
  198. || req_clocks->phyclk_khz >
  199. clk_dce->max_clks_by_state[i].pixel_clk_khz)
  200. break;
  201. low_req_clk = i + 1;
  202. if (low_req_clk > clk->max_clks_state) {
  203. /* set max clock state for high phyclock, invalid on exceeding display clock */
  204. if (clk_dce->max_clks_by_state[clk->max_clks_state].display_clk_khz
  205. < req_clocks->dispclk_khz)
  206. low_req_clk = DM_PP_CLOCKS_STATE_INVALID;
  207. else
  208. low_req_clk = clk->max_clks_state;
  209. }
  210. return low_req_clk;
  211. }
  212. static int dce_set_clock(
  213. struct dccg *clk,
  214. int requested_clk_khz)
  215. {
  216. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(clk);
  217. struct bp_pixel_clock_parameters pxl_clk_params = { 0 };
  218. struct dc_bios *bp = clk->ctx->dc_bios;
  219. int actual_clock = requested_clk_khz;
  220. /* Make sure requested clock isn't lower than minimum threshold*/
  221. if (requested_clk_khz > 0)
  222. requested_clk_khz = max(requested_clk_khz,
  223. clk_dce->dentist_vco_freq_khz / 64);
  224. /* Prepare to program display clock*/
  225. pxl_clk_params.target_pixel_clock = requested_clk_khz;
  226. pxl_clk_params.pll_id = CLOCK_SOURCE_ID_DFS;
  227. if (clk_dce->dfs_bypass_active)
  228. pxl_clk_params.flags.SET_DISPCLK_DFS_BYPASS = true;
  229. bp->funcs->program_display_engine_pll(bp, &pxl_clk_params);
  230. if (clk_dce->dfs_bypass_active) {
  231. /* Cache the fixed display clock*/
  232. clk_dce->dfs_bypass_disp_clk =
  233. pxl_clk_params.dfs_bypass_display_clock;
  234. actual_clock = pxl_clk_params.dfs_bypass_display_clock;
  235. }
  236. /* from power down, we need mark the clock state as ClocksStateNominal
  237. * from HWReset, so when resume we will call pplib voltage regulator.*/
  238. if (requested_clk_khz == 0)
  239. clk->cur_min_clks_state = DM_PP_CLOCKS_STATE_NOMINAL;
  240. return actual_clock;
  241. }
  242. static int dce_psr_set_clock(
  243. struct dccg *clk,
  244. int requested_clk_khz)
  245. {
  246. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(clk);
  247. struct dc_context *ctx = clk_dce->base.ctx;
  248. struct dc *core_dc = ctx->dc;
  249. struct dmcu *dmcu = core_dc->res_pool->dmcu;
  250. int actual_clk_khz = requested_clk_khz;
  251. actual_clk_khz = dce_set_clock(clk, requested_clk_khz);
  252. dmcu->funcs->set_psr_wait_loop(dmcu, actual_clk_khz / 1000 / 7);
  253. return actual_clk_khz;
  254. }
  255. static int dce112_set_clock(
  256. struct dccg *clk,
  257. int requested_clk_khz)
  258. {
  259. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(clk);
  260. struct bp_set_dce_clock_parameters dce_clk_params;
  261. struct dc_bios *bp = clk->ctx->dc_bios;
  262. struct dc *core_dc = clk->ctx->dc;
  263. struct dmcu *dmcu = core_dc->res_pool->dmcu;
  264. int actual_clock = requested_clk_khz;
  265. /* Prepare to program display clock*/
  266. memset(&dce_clk_params, 0, sizeof(dce_clk_params));
  267. /* Make sure requested clock isn't lower than minimum threshold*/
  268. if (requested_clk_khz > 0)
  269. requested_clk_khz = max(requested_clk_khz,
  270. clk_dce->dentist_vco_freq_khz / 62);
  271. dce_clk_params.target_clock_frequency = requested_clk_khz;
  272. dce_clk_params.pll_id = CLOCK_SOURCE_ID_DFS;
  273. dce_clk_params.clock_type = DCECLOCK_TYPE_DISPLAY_CLOCK;
  274. bp->funcs->set_dce_clock(bp, &dce_clk_params);
  275. actual_clock = dce_clk_params.target_clock_frequency;
  276. /* from power down, we need mark the clock state as ClocksStateNominal
  277. * from HWReset, so when resume we will call pplib voltage regulator.*/
  278. if (requested_clk_khz == 0)
  279. clk->cur_min_clks_state = DM_PP_CLOCKS_STATE_NOMINAL;
  280. /*Program DP ref Clock*/
  281. /*VBIOS will determine DPREFCLK frequency, so we don't set it*/
  282. dce_clk_params.target_clock_frequency = 0;
  283. dce_clk_params.clock_type = DCECLOCK_TYPE_DPREFCLK;
  284. if (!ASICREV_IS_VEGA20_P(clk->ctx->asic_id.hw_internal_rev))
  285. dce_clk_params.flags.USE_GENLOCK_AS_SOURCE_FOR_DPREFCLK =
  286. (dce_clk_params.pll_id ==
  287. CLOCK_SOURCE_COMBO_DISPLAY_PLL0);
  288. else
  289. dce_clk_params.flags.USE_GENLOCK_AS_SOURCE_FOR_DPREFCLK = false;
  290. bp->funcs->set_dce_clock(bp, &dce_clk_params);
  291. if (!IS_FPGA_MAXIMUS_DC(core_dc->ctx->dce_environment)) {
  292. if (clk_dce->dfs_bypass_disp_clk != actual_clock)
  293. dmcu->funcs->set_psr_wait_loop(dmcu,
  294. actual_clock / 1000 / 7);
  295. }
  296. clk_dce->dfs_bypass_disp_clk = actual_clock;
  297. return actual_clock;
  298. }
  299. static void dce_clock_read_integrated_info(struct dce_dccg *clk_dce)
  300. {
  301. struct dc_debug_options *debug = &clk_dce->base.ctx->dc->debug;
  302. struct dc_bios *bp = clk_dce->base.ctx->dc_bios;
  303. struct integrated_info info = { { { 0 } } };
  304. struct dc_firmware_info fw_info = { { 0 } };
  305. int i;
  306. if (bp->integrated_info)
  307. info = *bp->integrated_info;
  308. clk_dce->dentist_vco_freq_khz = info.dentist_vco_freq;
  309. if (clk_dce->dentist_vco_freq_khz == 0) {
  310. bp->funcs->get_firmware_info(bp, &fw_info);
  311. clk_dce->dentist_vco_freq_khz =
  312. fw_info.smu_gpu_pll_output_freq;
  313. if (clk_dce->dentist_vco_freq_khz == 0)
  314. clk_dce->dentist_vco_freq_khz = 3600000;
  315. }
  316. /*update the maximum display clock for each power state*/
  317. for (i = 0; i < NUMBER_OF_DISP_CLK_VOLTAGE; ++i) {
  318. enum dm_pp_clocks_state clk_state = DM_PP_CLOCKS_STATE_INVALID;
  319. switch (i) {
  320. case 0:
  321. clk_state = DM_PP_CLOCKS_STATE_ULTRA_LOW;
  322. break;
  323. case 1:
  324. clk_state = DM_PP_CLOCKS_STATE_LOW;
  325. break;
  326. case 2:
  327. clk_state = DM_PP_CLOCKS_STATE_NOMINAL;
  328. break;
  329. case 3:
  330. clk_state = DM_PP_CLOCKS_STATE_PERFORMANCE;
  331. break;
  332. default:
  333. clk_state = DM_PP_CLOCKS_STATE_INVALID;
  334. break;
  335. }
  336. /*Do not allow bad VBIOS/SBIOS to override with invalid values,
  337. * check for > 100MHz*/
  338. if (info.disp_clk_voltage[i].max_supported_clk >= 100000)
  339. clk_dce->max_clks_by_state[clk_state].display_clk_khz =
  340. info.disp_clk_voltage[i].max_supported_clk;
  341. }
  342. if (!debug->disable_dfs_bypass && bp->integrated_info)
  343. if (bp->integrated_info->gpu_cap_info & DFS_BYPASS_ENABLE)
  344. clk_dce->dfs_bypass_enabled = true;
  345. }
  346. static void dce_clock_read_ss_info(struct dce_dccg *clk_dce)
  347. {
  348. struct dc_bios *bp = clk_dce->base.ctx->dc_bios;
  349. int ss_info_num = bp->funcs->get_ss_entry_number(
  350. bp, AS_SIGNAL_TYPE_GPU_PLL);
  351. if (ss_info_num) {
  352. struct spread_spectrum_info info = { { 0 } };
  353. enum bp_result result = bp->funcs->get_spread_spectrum_info(
  354. bp, AS_SIGNAL_TYPE_GPU_PLL, 0, &info);
  355. /* Based on VBIOS, VBIOS will keep entry for GPU PLL SS
  356. * even if SS not enabled and in that case
  357. * SSInfo.spreadSpectrumPercentage !=0 would be sign
  358. * that SS is enabled
  359. */
  360. if (result == BP_RESULT_OK &&
  361. info.spread_spectrum_percentage != 0) {
  362. clk_dce->ss_on_dprefclk = true;
  363. clk_dce->dprefclk_ss_divider = info.spread_percentage_divider;
  364. if (info.type.CENTER_MODE == 0) {
  365. /* TODO: Currently for DP Reference clock we
  366. * need only SS percentage for
  367. * downspread */
  368. clk_dce->dprefclk_ss_percentage =
  369. info.spread_spectrum_percentage;
  370. }
  371. return;
  372. }
  373. result = bp->funcs->get_spread_spectrum_info(
  374. bp, AS_SIGNAL_TYPE_DISPLAY_PORT, 0, &info);
  375. /* Based on VBIOS, VBIOS will keep entry for DPREFCLK SS
  376. * even if SS not enabled and in that case
  377. * SSInfo.spreadSpectrumPercentage !=0 would be sign
  378. * that SS is enabled
  379. */
  380. if (result == BP_RESULT_OK &&
  381. info.spread_spectrum_percentage != 0) {
  382. clk_dce->ss_on_dprefclk = true;
  383. clk_dce->dprefclk_ss_divider = info.spread_percentage_divider;
  384. if (info.type.CENTER_MODE == 0) {
  385. /* Currently for DP Reference clock we
  386. * need only SS percentage for
  387. * downspread */
  388. clk_dce->dprefclk_ss_percentage =
  389. info.spread_spectrum_percentage;
  390. }
  391. }
  392. }
  393. }
  394. static inline bool should_set_clock(bool safe_to_lower, int calc_clk, int cur_clk)
  395. {
  396. return ((safe_to_lower && calc_clk < cur_clk) || calc_clk > cur_clk);
  397. }
  398. static void dce12_update_clocks(struct dccg *dccg,
  399. struct dc_clocks *new_clocks,
  400. bool safe_to_lower)
  401. {
  402. struct dm_pp_clock_for_voltage_req clock_voltage_req = {0};
  403. /* TODO: Investigate why this is needed to fix display corruption. */
  404. new_clocks->dispclk_khz = new_clocks->dispclk_khz * 115 / 100;
  405. if (should_set_clock(safe_to_lower, new_clocks->dispclk_khz, dccg->clks.dispclk_khz)) {
  406. clock_voltage_req.clk_type = DM_PP_CLOCK_TYPE_DISPLAY_CLK;
  407. clock_voltage_req.clocks_in_khz = new_clocks->dispclk_khz;
  408. new_clocks->dispclk_khz = dccg->funcs->set_dispclk(dccg, new_clocks->dispclk_khz);
  409. dccg->clks.dispclk_khz = new_clocks->dispclk_khz;
  410. dm_pp_apply_clock_for_voltage_request(dccg->ctx, &clock_voltage_req);
  411. }
  412. if (should_set_clock(safe_to_lower, new_clocks->phyclk_khz, dccg->clks.phyclk_khz)) {
  413. clock_voltage_req.clk_type = DM_PP_CLOCK_TYPE_DISPLAYPHYCLK;
  414. clock_voltage_req.clocks_in_khz = new_clocks->phyclk_khz;
  415. dccg->clks.phyclk_khz = new_clocks->phyclk_khz;
  416. dm_pp_apply_clock_for_voltage_request(dccg->ctx, &clock_voltage_req);
  417. }
  418. }
  419. #ifdef CONFIG_DRM_AMD_DC_DCN1_0
  420. static int dcn1_determine_dppclk_threshold(struct dccg *dccg, struct dc_clocks *new_clocks)
  421. {
  422. bool request_dpp_div = new_clocks->dispclk_khz > new_clocks->dppclk_khz;
  423. bool dispclk_increase = new_clocks->dispclk_khz > dccg->clks.dispclk_khz;
  424. int disp_clk_threshold = new_clocks->max_supported_dppclk_khz;
  425. bool cur_dpp_div = dccg->clks.dispclk_khz > dccg->clks.dppclk_khz;
  426. /* increase clock, looking for div is 0 for current, request div is 1*/
  427. if (dispclk_increase) {
  428. /* already divided by 2, no need to reach target clk with 2 steps*/
  429. if (cur_dpp_div)
  430. return new_clocks->dispclk_khz;
  431. /* request disp clk is lower than maximum supported dpp clk,
  432. * no need to reach target clk with two steps.
  433. */
  434. if (new_clocks->dispclk_khz <= disp_clk_threshold)
  435. return new_clocks->dispclk_khz;
  436. /* target dpp clk not request divided by 2, still within threshold */
  437. if (!request_dpp_div)
  438. return new_clocks->dispclk_khz;
  439. } else {
  440. /* decrease clock, looking for current dppclk divided by 2,
  441. * request dppclk not divided by 2.
  442. */
  443. /* current dpp clk not divided by 2, no need to ramp*/
  444. if (!cur_dpp_div)
  445. return new_clocks->dispclk_khz;
  446. /* current disp clk is lower than current maximum dpp clk,
  447. * no need to ramp
  448. */
  449. if (dccg->clks.dispclk_khz <= disp_clk_threshold)
  450. return new_clocks->dispclk_khz;
  451. /* request dpp clk need to be divided by 2 */
  452. if (request_dpp_div)
  453. return new_clocks->dispclk_khz;
  454. }
  455. return disp_clk_threshold;
  456. }
  457. static void dcn1_ramp_up_dispclk_with_dpp(struct dccg *dccg, struct dc_clocks *new_clocks)
  458. {
  459. struct dc *dc = dccg->ctx->dc;
  460. int dispclk_to_dpp_threshold = dcn1_determine_dppclk_threshold(dccg, new_clocks);
  461. bool request_dpp_div = new_clocks->dispclk_khz > new_clocks->dppclk_khz;
  462. int i;
  463. /* set disp clk to dpp clk threshold */
  464. dccg->funcs->set_dispclk(dccg, dispclk_to_dpp_threshold);
  465. /* update request dpp clk division option */
  466. for (i = 0; i < dc->res_pool->pipe_count; i++) {
  467. struct pipe_ctx *pipe_ctx = &dc->current_state->res_ctx.pipe_ctx[i];
  468. if (!pipe_ctx->plane_state)
  469. continue;
  470. pipe_ctx->plane_res.dpp->funcs->dpp_dppclk_control(
  471. pipe_ctx->plane_res.dpp,
  472. request_dpp_div,
  473. true);
  474. }
  475. /* If target clk not same as dppclk threshold, set to target clock */
  476. if (dispclk_to_dpp_threshold != new_clocks->dispclk_khz)
  477. dccg->funcs->set_dispclk(dccg, new_clocks->dispclk_khz);
  478. dccg->clks.dispclk_khz = new_clocks->dispclk_khz;
  479. dccg->clks.dppclk_khz = new_clocks->dppclk_khz;
  480. dccg->clks.max_supported_dppclk_khz = new_clocks->max_supported_dppclk_khz;
  481. }
  482. static void dcn1_update_clocks(struct dccg *dccg,
  483. struct dc_clocks *new_clocks,
  484. bool safe_to_lower)
  485. {
  486. struct dc *dc = dccg->ctx->dc;
  487. struct pp_smu_display_requirement_rv *smu_req_cur =
  488. &dc->res_pool->pp_smu_req;
  489. struct pp_smu_display_requirement_rv smu_req = *smu_req_cur;
  490. struct pp_smu_funcs_rv *pp_smu = dc->res_pool->pp_smu;
  491. struct dm_pp_clock_for_voltage_req clock_voltage_req = {0};
  492. bool send_request_to_increase = false;
  493. bool send_request_to_lower = false;
  494. if (new_clocks->phyclk_khz)
  495. smu_req.display_count = 1;
  496. else
  497. smu_req.display_count = 0;
  498. if (new_clocks->dispclk_khz > dccg->clks.dispclk_khz
  499. || new_clocks->phyclk_khz > dccg->clks.phyclk_khz
  500. || new_clocks->fclk_khz > dccg->clks.fclk_khz
  501. || new_clocks->dcfclk_khz > dccg->clks.dcfclk_khz)
  502. send_request_to_increase = true;
  503. if (should_set_clock(safe_to_lower, new_clocks->phyclk_khz, dccg->clks.phyclk_khz)) {
  504. dccg->clks.phyclk_khz = new_clocks->phyclk_khz;
  505. send_request_to_lower = true;
  506. }
  507. if (should_set_clock(safe_to_lower, new_clocks->fclk_khz, dccg->clks.fclk_khz)) {
  508. dccg->clks.fclk_khz = new_clocks->fclk_khz;
  509. clock_voltage_req.clk_type = DM_PP_CLOCK_TYPE_FCLK;
  510. clock_voltage_req.clocks_in_khz = new_clocks->fclk_khz;
  511. smu_req.hard_min_fclk_khz = new_clocks->fclk_khz;
  512. dm_pp_apply_clock_for_voltage_request(dccg->ctx, &clock_voltage_req);
  513. send_request_to_lower = true;
  514. }
  515. if (should_set_clock(safe_to_lower, new_clocks->dcfclk_khz, dccg->clks.dcfclk_khz)) {
  516. dccg->clks.dcfclk_khz = new_clocks->dcfclk_khz;
  517. smu_req.hard_min_dcefclk_khz = new_clocks->dcfclk_khz;
  518. send_request_to_lower = true;
  519. }
  520. if (should_set_clock(safe_to_lower,
  521. new_clocks->dcfclk_deep_sleep_khz, dccg->clks.dcfclk_deep_sleep_khz)) {
  522. dccg->clks.dcfclk_deep_sleep_khz = new_clocks->dcfclk_deep_sleep_khz;
  523. smu_req.min_deep_sleep_dcefclk_mhz = new_clocks->dcfclk_deep_sleep_khz;
  524. send_request_to_lower = true;
  525. }
  526. /* make sure dcf clk is before dpp clk to
  527. * make sure we have enough voltage to run dpp clk
  528. */
  529. if (send_request_to_increase) {
  530. /*use dcfclk to request voltage*/
  531. clock_voltage_req.clk_type = DM_PP_CLOCK_TYPE_DCFCLK;
  532. clock_voltage_req.clocks_in_khz = dcn_find_dcfclk_suits_all(dc, new_clocks);
  533. dm_pp_apply_clock_for_voltage_request(dccg->ctx, &clock_voltage_req);
  534. if (pp_smu->set_display_requirement)
  535. pp_smu->set_display_requirement(&pp_smu->pp_smu, &smu_req);
  536. }
  537. /* dcn1 dppclk is tied to dispclk */
  538. /* program dispclk on = as a w/a for sleep resume clock ramping issues */
  539. if (should_set_clock(safe_to_lower, new_clocks->dispclk_khz, dccg->clks.dispclk_khz)
  540. || new_clocks->dispclk_khz == dccg->clks.dispclk_khz) {
  541. dcn1_ramp_up_dispclk_with_dpp(dccg, new_clocks);
  542. dccg->clks.dispclk_khz = new_clocks->dispclk_khz;
  543. send_request_to_lower = true;
  544. }
  545. if (!send_request_to_increase && send_request_to_lower) {
  546. /*use dcfclk to request voltage*/
  547. clock_voltage_req.clk_type = DM_PP_CLOCK_TYPE_DCFCLK;
  548. clock_voltage_req.clocks_in_khz = dcn_find_dcfclk_suits_all(dc, new_clocks);
  549. dm_pp_apply_clock_for_voltage_request(dccg->ctx, &clock_voltage_req);
  550. if (pp_smu->set_display_requirement)
  551. pp_smu->set_display_requirement(&pp_smu->pp_smu, &smu_req);
  552. }
  553. *smu_req_cur = smu_req;
  554. }
  555. #endif
  556. static void dce_update_clocks(struct dccg *dccg,
  557. struct dc_clocks *new_clocks,
  558. bool safe_to_lower)
  559. {
  560. struct dm_pp_power_level_change_request level_change_req;
  561. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(dccg);
  562. /* TODO: Investigate why this is needed to fix display corruption. */
  563. if (!clk_dce->dfs_bypass_active)
  564. new_clocks->dispclk_khz = new_clocks->dispclk_khz * 115 / 100;
  565. level_change_req.power_level = dce_get_required_clocks_state(dccg, new_clocks);
  566. /* get max clock state from PPLIB */
  567. if ((level_change_req.power_level < dccg->cur_min_clks_state && safe_to_lower)
  568. || level_change_req.power_level > dccg->cur_min_clks_state) {
  569. if (dm_pp_apply_power_level_change_request(dccg->ctx, &level_change_req))
  570. dccg->cur_min_clks_state = level_change_req.power_level;
  571. }
  572. if (should_set_clock(safe_to_lower, new_clocks->dispclk_khz, dccg->clks.dispclk_khz)) {
  573. new_clocks->dispclk_khz = dccg->funcs->set_dispclk(dccg, new_clocks->dispclk_khz);
  574. dccg->clks.dispclk_khz = new_clocks->dispclk_khz;
  575. }
  576. }
  577. static bool dce_update_dfs_bypass(
  578. struct dccg *dccg,
  579. struct dc *dc,
  580. struct dc_state *context,
  581. int requested_clock_khz)
  582. {
  583. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(dccg);
  584. struct resource_context *res_ctx = &context->res_ctx;
  585. enum signal_type signal_type = SIGNAL_TYPE_NONE;
  586. bool was_active = clk_dce->dfs_bypass_active;
  587. int i;
  588. /* Disable DFS bypass by default. */
  589. clk_dce->dfs_bypass_active = false;
  590. /* Check that DFS bypass is available. */
  591. if (!clk_dce->dfs_bypass_enabled)
  592. goto update;
  593. /* Check if the requested display clock is below the threshold. */
  594. if (requested_clock_khz >= 400000)
  595. goto update;
  596. /* DFS-bypass should only be enabled on single stream setups */
  597. if (context->stream_count != 1)
  598. goto update;
  599. /* Check that the stream's signal type is an embedded panel */
  600. for (i = 0; i < dc->res_pool->pipe_count; i++) {
  601. if (res_ctx->pipe_ctx[i].stream) {
  602. struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
  603. signal_type = pipe_ctx->stream->sink->link->connector_signal;
  604. break;
  605. }
  606. }
  607. if (signal_type == SIGNAL_TYPE_EDP ||
  608. signal_type == SIGNAL_TYPE_LVDS)
  609. clk_dce->dfs_bypass_active = true;
  610. update:
  611. /* Update the clock state. We don't need to respect safe_to_lower
  612. * because DFS bypass should always be greater than the current
  613. * display clock frequency.
  614. */
  615. if (was_active != clk_dce->dfs_bypass_active) {
  616. dccg->clks.dispclk_khz =
  617. dccg->funcs->set_dispclk(dccg, dccg->clks.dispclk_khz);
  618. return true;
  619. }
  620. return false;
  621. }
  622. #ifdef CONFIG_DRM_AMD_DC_DCN1_0
  623. static const struct display_clock_funcs dcn1_funcs = {
  624. .get_dp_ref_clk_frequency = dce12_get_dp_ref_freq_khz,
  625. .set_dispclk = dce112_set_clock,
  626. .update_clocks = dcn1_update_clocks
  627. };
  628. #endif
  629. static const struct display_clock_funcs dce120_funcs = {
  630. .get_dp_ref_clk_frequency = dce12_get_dp_ref_freq_khz,
  631. .set_dispclk = dce112_set_clock,
  632. .update_clocks = dce12_update_clocks
  633. };
  634. static const struct display_clock_funcs dce112_funcs = {
  635. .get_dp_ref_clk_frequency = dce_get_dp_ref_freq_khz,
  636. .set_dispclk = dce112_set_clock,
  637. .update_clocks = dce_update_clocks
  638. };
  639. static const struct display_clock_funcs dce110_funcs = {
  640. .get_dp_ref_clk_frequency = dce_get_dp_ref_freq_khz,
  641. .set_dispclk = dce_psr_set_clock,
  642. .update_clocks = dce_update_clocks,
  643. .update_dfs_bypass = dce_update_dfs_bypass
  644. };
  645. static const struct display_clock_funcs dce_funcs = {
  646. .get_dp_ref_clk_frequency = dce_get_dp_ref_freq_khz,
  647. .set_dispclk = dce_set_clock,
  648. .update_clocks = dce_update_clocks
  649. };
  650. static void dce_dccg_construct(
  651. struct dce_dccg *clk_dce,
  652. struct dc_context *ctx,
  653. const struct dccg_registers *regs,
  654. const struct dccg_shift *clk_shift,
  655. const struct dccg_mask *clk_mask)
  656. {
  657. struct dccg *base = &clk_dce->base;
  658. base->ctx = ctx;
  659. base->funcs = &dce_funcs;
  660. clk_dce->regs = regs;
  661. clk_dce->clk_shift = clk_shift;
  662. clk_dce->clk_mask = clk_mask;
  663. clk_dce->dfs_bypass_disp_clk = 0;
  664. clk_dce->dprefclk_ss_percentage = 0;
  665. clk_dce->dprefclk_ss_divider = 1000;
  666. clk_dce->ss_on_dprefclk = false;
  667. base->max_clks_state = DM_PP_CLOCKS_STATE_NOMINAL;
  668. base->cur_min_clks_state = DM_PP_CLOCKS_STATE_INVALID;
  669. dce_clock_read_integrated_info(clk_dce);
  670. dce_clock_read_ss_info(clk_dce);
  671. }
  672. struct dccg *dce_dccg_create(
  673. struct dc_context *ctx,
  674. const struct dccg_registers *regs,
  675. const struct dccg_shift *clk_shift,
  676. const struct dccg_mask *clk_mask)
  677. {
  678. struct dce_dccg *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  679. if (clk_dce == NULL) {
  680. BREAK_TO_DEBUGGER();
  681. return NULL;
  682. }
  683. memcpy(clk_dce->max_clks_by_state,
  684. dce80_max_clks_by_state,
  685. sizeof(dce80_max_clks_by_state));
  686. dce_dccg_construct(
  687. clk_dce, ctx, regs, clk_shift, clk_mask);
  688. return &clk_dce->base;
  689. }
  690. struct dccg *dce110_dccg_create(
  691. struct dc_context *ctx,
  692. const struct dccg_registers *regs,
  693. const struct dccg_shift *clk_shift,
  694. const struct dccg_mask *clk_mask)
  695. {
  696. struct dce_dccg *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  697. if (clk_dce == NULL) {
  698. BREAK_TO_DEBUGGER();
  699. return NULL;
  700. }
  701. memcpy(clk_dce->max_clks_by_state,
  702. dce110_max_clks_by_state,
  703. sizeof(dce110_max_clks_by_state));
  704. dce_dccg_construct(
  705. clk_dce, ctx, regs, clk_shift, clk_mask);
  706. clk_dce->base.funcs = &dce110_funcs;
  707. return &clk_dce->base;
  708. }
  709. struct dccg *dce112_dccg_create(
  710. struct dc_context *ctx,
  711. const struct dccg_registers *regs,
  712. const struct dccg_shift *clk_shift,
  713. const struct dccg_mask *clk_mask)
  714. {
  715. struct dce_dccg *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  716. if (clk_dce == NULL) {
  717. BREAK_TO_DEBUGGER();
  718. return NULL;
  719. }
  720. memcpy(clk_dce->max_clks_by_state,
  721. dce112_max_clks_by_state,
  722. sizeof(dce112_max_clks_by_state));
  723. dce_dccg_construct(
  724. clk_dce, ctx, regs, clk_shift, clk_mask);
  725. clk_dce->base.funcs = &dce112_funcs;
  726. return &clk_dce->base;
  727. }
  728. struct dccg *dce120_dccg_create(struct dc_context *ctx)
  729. {
  730. struct dce_dccg *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  731. if (clk_dce == NULL) {
  732. BREAK_TO_DEBUGGER();
  733. return NULL;
  734. }
  735. memcpy(clk_dce->max_clks_by_state,
  736. dce120_max_clks_by_state,
  737. sizeof(dce120_max_clks_by_state));
  738. dce_dccg_construct(
  739. clk_dce, ctx, NULL, NULL, NULL);
  740. clk_dce->dprefclk_khz = 600000;
  741. clk_dce->base.funcs = &dce120_funcs;
  742. return &clk_dce->base;
  743. }
  744. #ifdef CONFIG_DRM_AMD_DC_DCN1_0
  745. struct dccg *dcn1_dccg_create(struct dc_context *ctx)
  746. {
  747. struct dc_debug_options *debug = &ctx->dc->debug;
  748. struct dc_bios *bp = ctx->dc_bios;
  749. struct dc_firmware_info fw_info = { { 0 } };
  750. struct dce_dccg *clk_dce = kzalloc(sizeof(*clk_dce), GFP_KERNEL);
  751. if (clk_dce == NULL) {
  752. BREAK_TO_DEBUGGER();
  753. return NULL;
  754. }
  755. clk_dce->base.ctx = ctx;
  756. clk_dce->base.funcs = &dcn1_funcs;
  757. clk_dce->dfs_bypass_disp_clk = 0;
  758. clk_dce->dprefclk_ss_percentage = 0;
  759. clk_dce->dprefclk_ss_divider = 1000;
  760. clk_dce->ss_on_dprefclk = false;
  761. clk_dce->dprefclk_khz = 600000;
  762. if (bp->integrated_info)
  763. clk_dce->dentist_vco_freq_khz = bp->integrated_info->dentist_vco_freq;
  764. if (clk_dce->dentist_vco_freq_khz == 0) {
  765. bp->funcs->get_firmware_info(bp, &fw_info);
  766. clk_dce->dentist_vco_freq_khz = fw_info.smu_gpu_pll_output_freq;
  767. if (clk_dce->dentist_vco_freq_khz == 0)
  768. clk_dce->dentist_vco_freq_khz = 3600000;
  769. }
  770. if (!debug->disable_dfs_bypass && bp->integrated_info)
  771. if (bp->integrated_info->gpu_cap_info & DFS_BYPASS_ENABLE)
  772. clk_dce->dfs_bypass_enabled = true;
  773. dce_clock_read_ss_info(clk_dce);
  774. return &clk_dce->base;
  775. }
  776. #endif
  777. void dce_dccg_destroy(struct dccg **dccg)
  778. {
  779. struct dce_dccg *clk_dce = TO_DCE_CLOCKS(*dccg);
  780. kfree(clk_dce);
  781. *dccg = NULL;
  782. }