2 * Copyright 2018 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
27 #include "clk_mgr_internal.h"
29 #include "dce100/dce_clk_mgr.h"
30 #include "dcn20_clk_mgr.h"
31 #include "reg_helper.h"
32 #include "core_types.h"
33 #include "dm_helpers.h"
35 #include "navi10_ip_offset.h"
36 #include "dcn/dcn_2_0_0_offset.h"
37 #include "dcn/dcn_2_0_0_sh_mask.h"
38 #include "clk/clk_11_0_0_offset.h"
39 #include "clk/clk_11_0_0_sh_mask.h"
42 #define FN(reg_name, field_name) \
43 clk_mgr->clk_mgr_shift->field_name, clk_mgr->clk_mgr_mask->field_name
48 #define BASE_INNER(seg) DCN_BASE__INST0_SEG ## seg
50 #define BASE(seg) BASE_INNER(seg)
53 .reg_name = BASE(mm ## reg_name ## _BASE_IDX) + \
56 #define CLK_BASE_INNER(seg) \
57 CLK_BASE__INST0_SEG ## seg
60 static const struct clk_mgr_registers clk_mgr_regs = {
64 static const struct clk_mgr_shift clk_mgr_shift = {
65 CLK_MASK_SH_LIST_NV10(__SHIFT)
68 static const struct clk_mgr_mask clk_mgr_mask = {
69 CLK_MASK_SH_LIST_NV10(_MASK)
72 uint32_t dentist_get_did_from_divider(int divider)
76 /* we want to floor here to get higher clock than required rather than lower */
77 if (divider < DENTIST_DIVIDER_RANGE_2_START) {
78 if (divider < DENTIST_DIVIDER_RANGE_1_START)
79 divider_id = DENTIST_BASE_DID_1;
81 divider_id = DENTIST_BASE_DID_1
82 + (divider - DENTIST_DIVIDER_RANGE_1_START)
83 / DENTIST_DIVIDER_RANGE_1_STEP;
84 } else if (divider < DENTIST_DIVIDER_RANGE_3_START) {
85 divider_id = DENTIST_BASE_DID_2
86 + (divider - DENTIST_DIVIDER_RANGE_2_START)
87 / DENTIST_DIVIDER_RANGE_2_STEP;
88 } else if (divider < DENTIST_DIVIDER_RANGE_4_START) {
89 divider_id = DENTIST_BASE_DID_3
90 + (divider - DENTIST_DIVIDER_RANGE_3_START)
91 / DENTIST_DIVIDER_RANGE_3_STEP;
93 divider_id = DENTIST_BASE_DID_4
94 + (divider - DENTIST_DIVIDER_RANGE_4_START)
95 / DENTIST_DIVIDER_RANGE_4_STEP;
96 if (divider_id > DENTIST_MAX_DID)
97 divider_id = DENTIST_MAX_DID;
103 void dcn20_update_clocks_update_dpp_dto(struct clk_mgr_internal *clk_mgr,
104 struct dc_state *context, bool safe_to_lower)
108 clk_mgr->dccg->ref_dppclk = clk_mgr->base.clks.dppclk_khz;
109 for (i = 0; i < clk_mgr->base.ctx->dc->res_pool->pipe_count; i++) {
110 int dpp_inst, dppclk_khz, prev_dppclk_khz;
112 /* Loop index will match dpp->inst if resource exists,
113 * and we want to avoid dependency on dpp object
116 dppclk_khz = context->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz;
118 prev_dppclk_khz = clk_mgr->base.ctx->dc->current_state->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz;
120 if (safe_to_lower || prev_dppclk_khz < dppclk_khz) {
121 clk_mgr->dccg->funcs->update_dpp_dto(
122 clk_mgr->dccg, dpp_inst, dppclk_khz);
127 void dcn20_update_clocks_update_dentist(struct clk_mgr_internal *clk_mgr)
129 int dpp_divider = DENTIST_DIVIDER_RANGE_SCALE_FACTOR
130 * clk_mgr->base.dentist_vco_freq_khz / clk_mgr->base.clks.dppclk_khz;
131 int disp_divider = DENTIST_DIVIDER_RANGE_SCALE_FACTOR
132 * clk_mgr->base.dentist_vco_freq_khz / clk_mgr->base.clks.dispclk_khz;
134 uint32_t dppclk_wdivider = dentist_get_did_from_divider(dpp_divider);
135 uint32_t dispclk_wdivider = dentist_get_did_from_divider(disp_divider);
137 REG_UPDATE(DENTIST_DISPCLK_CNTL,
138 DENTIST_DISPCLK_WDIVIDER, dispclk_wdivider);
139 // REG_WAIT(DENTIST_DISPCLK_CNTL, DENTIST_DISPCLK_CHG_DONE, 1, 5, 100);
140 REG_UPDATE(DENTIST_DISPCLK_CNTL,
141 DENTIST_DPPCLK_WDIVIDER, dppclk_wdivider);
142 REG_WAIT(DENTIST_DISPCLK_CNTL, DENTIST_DPPCLK_CHG_DONE, 1, 5, 100);
146 void dcn2_update_clocks(struct clk_mgr *clk_mgr_base,
147 struct dc_state *context,
150 struct clk_mgr_internal *clk_mgr = TO_CLK_MGR_INTERNAL(clk_mgr_base);
151 struct dc_clocks *new_clocks = &context->bw_ctx.bw.dcn.clk;
152 struct dc *dc = clk_mgr_base->ctx->dc;
153 struct pp_smu_funcs_nv *pp_smu = NULL;
155 bool update_dppclk = false;
156 bool update_dispclk = false;
157 bool enter_display_off = false;
158 bool dpp_clock_lowered = false;
159 struct dmcu *dmcu = clk_mgr_base->ctx->dc->res_pool->dmcu;
160 bool force_reset = false;
162 if (dc->work_arounds.skip_clock_update)
165 if (clk_mgr_base->clks.dispclk_khz == 0 ||
166 dc->debug.force_clock_mode & 0x1) {
167 //this is from resume or boot up, if forced_clock cfg option used, we bypass program dispclk and DPPCLK, but need set them for S3.
170 dcn2_read_clocks_from_hw_dentist(clk_mgr_base);
172 //force_clock_mode 0x1: force reset the clock even it is the same clock as long as it is in Passive level.
174 display_count = clk_mgr_helper_get_active_display_cnt(dc, context);
175 if (dc->res_pool->pp_smu)
176 pp_smu = &dc->res_pool->pp_smu->nv_funcs;
178 if (display_count == 0)
179 enter_display_off = true;
181 if (enter_display_off == safe_to_lower) {
182 if (pp_smu && pp_smu->set_display_count)
183 pp_smu->set_display_count(&pp_smu->pp_smu, display_count);
186 if (should_set_clock(safe_to_lower, new_clocks->phyclk_khz, clk_mgr_base->clks.phyclk_khz)) {
187 clk_mgr_base->clks.phyclk_khz = new_clocks->phyclk_khz;
188 if (pp_smu && pp_smu->set_voltage_by_freq)
189 pp_smu->set_voltage_by_freq(&pp_smu->pp_smu, PP_SMU_NV_PHYCLK, clk_mgr_base->clks.phyclk_khz / 1000);
193 if (dc->debug.force_min_dcfclk_mhz > 0)
194 new_clocks->dcfclk_khz = (new_clocks->dcfclk_khz > (dc->debug.force_min_dcfclk_mhz * 1000)) ?
195 new_clocks->dcfclk_khz : (dc->debug.force_min_dcfclk_mhz * 1000);
197 if (should_set_clock(safe_to_lower, new_clocks->dcfclk_khz, clk_mgr_base->clks.dcfclk_khz)) {
198 clk_mgr_base->clks.dcfclk_khz = new_clocks->dcfclk_khz;
199 if (pp_smu && pp_smu->set_hard_min_dcfclk_by_freq)
200 pp_smu->set_hard_min_dcfclk_by_freq(&pp_smu->pp_smu, clk_mgr_base->clks.dcfclk_khz / 1000);
203 if (should_set_clock(safe_to_lower,
204 new_clocks->dcfclk_deep_sleep_khz, clk_mgr_base->clks.dcfclk_deep_sleep_khz)) {
205 clk_mgr_base->clks.dcfclk_deep_sleep_khz = new_clocks->dcfclk_deep_sleep_khz;
206 if (pp_smu && pp_smu->set_min_deep_sleep_dcfclk)
207 pp_smu->set_min_deep_sleep_dcfclk(&pp_smu->pp_smu, clk_mgr_base->clks.dcfclk_deep_sleep_khz / 1000);
210 if (should_set_clock(safe_to_lower, new_clocks->socclk_khz, clk_mgr_base->clks.socclk_khz)) {
211 clk_mgr_base->clks.socclk_khz = new_clocks->socclk_khz;
212 if (pp_smu && pp_smu->set_hard_min_socclk_by_freq)
213 pp_smu->set_hard_min_socclk_by_freq(&pp_smu->pp_smu, clk_mgr_base->clks.socclk_khz / 1000);
216 if (should_update_pstate_support(safe_to_lower, new_clocks->p_state_change_support, clk_mgr_base->clks.p_state_change_support)) {
217 clk_mgr_base->clks.prev_p_state_change_support = clk_mgr_base->clks.p_state_change_support;
218 clk_mgr_base->clks.p_state_change_support = new_clocks->p_state_change_support;
219 if (pp_smu && pp_smu->set_pstate_handshake_support)
220 pp_smu->set_pstate_handshake_support(&pp_smu->pp_smu, clk_mgr_base->clks.p_state_change_support);
223 if (should_set_clock(safe_to_lower, new_clocks->dramclk_khz, clk_mgr_base->clks.dramclk_khz)) {
224 clk_mgr_base->clks.dramclk_khz = new_clocks->dramclk_khz;
225 if (pp_smu && pp_smu->set_hard_min_uclk_by_freq)
226 pp_smu->set_hard_min_uclk_by_freq(&pp_smu->pp_smu, clk_mgr_base->clks.dramclk_khz / 1000);
229 if (should_set_clock(safe_to_lower, new_clocks->dppclk_khz, clk_mgr->base.clks.dppclk_khz)) {
230 if (clk_mgr->base.clks.dppclk_khz > new_clocks->dppclk_khz)
231 dpp_clock_lowered = true;
232 clk_mgr->base.clks.dppclk_khz = new_clocks->dppclk_khz;
234 if (pp_smu && pp_smu->set_voltage_by_freq)
235 pp_smu->set_voltage_by_freq(&pp_smu->pp_smu, PP_SMU_NV_PIXELCLK, clk_mgr_base->clks.dppclk_khz / 1000);
237 update_dppclk = true;
240 if (should_set_clock(safe_to_lower, new_clocks->dispclk_khz, clk_mgr_base->clks.dispclk_khz)) {
241 clk_mgr_base->clks.dispclk_khz = new_clocks->dispclk_khz;
242 if (pp_smu && pp_smu->set_voltage_by_freq)
243 pp_smu->set_voltage_by_freq(&pp_smu->pp_smu, PP_SMU_NV_DISPCLK, clk_mgr_base->clks.dispclk_khz / 1000);
245 update_dispclk = true;
248 if (dc->config.forced_clocks == false || (force_reset && safe_to_lower)) {
249 if (dpp_clock_lowered) {
250 // if clock is being lowered, increase DTO before lowering refclk
251 dcn20_update_clocks_update_dpp_dto(clk_mgr, context, safe_to_lower);
252 dcn20_update_clocks_update_dentist(clk_mgr);
254 // if clock is being raised, increase refclk before lowering DTO
255 if (update_dppclk || update_dispclk)
256 dcn20_update_clocks_update_dentist(clk_mgr);
257 // always update dtos unless clock is lowered and not safe to lower
258 if (new_clocks->dppclk_khz >= dc->current_state->bw_ctx.bw.dcn.clk.dppclk_khz)
259 dcn20_update_clocks_update_dpp_dto(clk_mgr, context, safe_to_lower);
263 if (update_dispclk &&
264 dmcu && dmcu->funcs->is_dmcu_initialized(dmcu)) {
265 /*update dmcu for wait_loop count*/
266 dmcu->funcs->set_psr_wait_loop(dmcu,
267 clk_mgr_base->clks.dispclk_khz / 1000 / 7);
271 void dcn2_update_clocks_fpga(struct clk_mgr *clk_mgr,
272 struct dc_state *context,
275 struct clk_mgr_internal *clk_mgr_int = TO_CLK_MGR_INTERNAL(clk_mgr);
277 struct dc_clocks *new_clocks = &context->bw_ctx.bw.dcn.clk;
278 /* Min fclk = 1.2GHz since all the extra scemi logic seems to run off of it */
279 int fclk_adj = new_clocks->fclk_khz > 1200000 ? new_clocks->fclk_khz : 1200000;
281 if (should_set_clock(safe_to_lower, new_clocks->phyclk_khz, clk_mgr->clks.phyclk_khz)) {
282 clk_mgr->clks.phyclk_khz = new_clocks->phyclk_khz;
285 if (should_set_clock(safe_to_lower, new_clocks->dcfclk_khz, clk_mgr->clks.dcfclk_khz)) {
286 clk_mgr->clks.dcfclk_khz = new_clocks->dcfclk_khz;
289 if (should_set_clock(safe_to_lower,
290 new_clocks->dcfclk_deep_sleep_khz, clk_mgr->clks.dcfclk_deep_sleep_khz)) {
291 clk_mgr->clks.dcfclk_deep_sleep_khz = new_clocks->dcfclk_deep_sleep_khz;
294 if (should_set_clock(safe_to_lower, new_clocks->socclk_khz, clk_mgr->clks.socclk_khz)) {
295 clk_mgr->clks.socclk_khz = new_clocks->socclk_khz;
298 if (should_set_clock(safe_to_lower, new_clocks->dramclk_khz, clk_mgr->clks.dramclk_khz)) {
299 clk_mgr->clks.dramclk_khz = new_clocks->dramclk_khz;
302 if (should_set_clock(safe_to_lower, new_clocks->dppclk_khz, clk_mgr->clks.dppclk_khz)) {
303 clk_mgr->clks.dppclk_khz = new_clocks->dppclk_khz;
306 if (should_set_clock(safe_to_lower, fclk_adj, clk_mgr->clks.fclk_khz)) {
307 clk_mgr->clks.fclk_khz = fclk_adj;
310 if (should_set_clock(safe_to_lower, new_clocks->dispclk_khz, clk_mgr->clks.dispclk_khz)) {
311 clk_mgr->clks.dispclk_khz = new_clocks->dispclk_khz;
314 /* Both fclk and ref_dppclk run on the same scemi clock.
315 * So take the higher value since the DPP DTO is typically programmed
316 * such that max dppclk is 1:1 with ref_dppclk.
318 if (clk_mgr->clks.fclk_khz > clk_mgr->clks.dppclk_khz)
319 clk_mgr->clks.dppclk_khz = clk_mgr->clks.fclk_khz;
320 if (clk_mgr->clks.dppclk_khz > clk_mgr->clks.fclk_khz)
321 clk_mgr->clks.fclk_khz = clk_mgr->clks.dppclk_khz;
323 // Both fclk and ref_dppclk run on the same scemi clock.
324 clk_mgr_int->dccg->ref_dppclk = clk_mgr->clks.fclk_khz;
326 dm_set_dcn_clocks(clk_mgr->ctx, &clk_mgr->clks);
329 void dcn2_init_clocks(struct clk_mgr *clk_mgr)
331 memset(&(clk_mgr->clks), 0, sizeof(struct dc_clocks));
332 // Assumption is that boot state always supports pstate
333 clk_mgr->clks.p_state_change_support = true;
334 clk_mgr->clks.prev_p_state_change_support = true;
337 void dcn2_enable_pme_wa(struct clk_mgr *clk_mgr_base)
339 struct clk_mgr_internal *clk_mgr = TO_CLK_MGR_INTERNAL(clk_mgr_base);
340 struct pp_smu_funcs_nv *pp_smu = NULL;
342 if (clk_mgr->pp_smu) {
343 pp_smu = &clk_mgr->pp_smu->nv_funcs;
345 if (pp_smu->set_pme_wa_enable)
346 pp_smu->set_pme_wa_enable(&pp_smu->pp_smu);
351 void dcn2_read_clocks_from_hw_dentist(struct clk_mgr *clk_mgr_base)
353 struct clk_mgr_internal *clk_mgr = TO_CLK_MGR_INTERNAL(clk_mgr_base);
354 uint32_t dispclk_wdivider;
355 uint32_t dppclk_wdivider;
359 REG_GET(DENTIST_DISPCLK_CNTL, DENTIST_DISPCLK_WDIVIDER, &dispclk_wdivider);
360 REG_GET(DENTIST_DISPCLK_CNTL, DENTIST_DPPCLK_WDIVIDER, &dppclk_wdivider);
362 disp_divider = dentist_get_divider_from_did(dispclk_wdivider);
363 dpp_divider = dentist_get_divider_from_did(dispclk_wdivider);
365 if (disp_divider && dpp_divider) {
366 /* Calculate the current DFS clock, in kHz.*/
367 clk_mgr_base->clks.dispclk_khz = (DENTIST_DIVIDER_RANGE_SCALE_FACTOR
368 * clk_mgr->base.dentist_vco_freq_khz) / disp_divider;
370 clk_mgr_base->clks.dppclk_khz = (DENTIST_DIVIDER_RANGE_SCALE_FACTOR
371 * clk_mgr->base.dentist_vco_freq_khz) / dpp_divider;
376 void dcn2_get_clock(struct clk_mgr *clk_mgr,
377 struct dc_state *context,
378 enum dc_clock_type clock_type,
379 struct dc_clock_config *clock_cfg)
382 if (clock_type == DC_CLOCK_TYPE_DISPCLK) {
383 clock_cfg->max_clock_khz = context->bw_ctx.bw.dcn.clk.max_supported_dispclk_khz;
384 clock_cfg->min_clock_khz = DCN_MINIMUM_DISPCLK_Khz;
385 clock_cfg->current_clock_khz = clk_mgr->clks.dispclk_khz;
386 clock_cfg->bw_requirequired_clock_khz = context->bw_ctx.bw.dcn.clk.bw_dispclk_khz;
388 if (clock_type == DC_CLOCK_TYPE_DPPCLK) {
389 clock_cfg->max_clock_khz = context->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz;
390 clock_cfg->min_clock_khz = DCN_MINIMUM_DPPCLK_Khz;
391 clock_cfg->current_clock_khz = clk_mgr->clks.dppclk_khz;
392 clock_cfg->bw_requirequired_clock_khz = context->bw_ctx.bw.dcn.clk.bw_dppclk_khz;
396 static bool dcn2_are_clock_states_equal(struct dc_clocks *a,
399 if (a->dispclk_khz != b->dispclk_khz)
401 else if (a->dppclk_khz != b->dppclk_khz)
403 else if (a->dcfclk_khz != b->dcfclk_khz)
405 else if (a->socclk_khz != b->socclk_khz)
407 else if (a->dcfclk_deep_sleep_khz != b->dcfclk_deep_sleep_khz)
409 else if (a->phyclk_khz != b->phyclk_khz)
411 else if (a->dramclk_khz != b->dramclk_khz)
413 else if (a->p_state_change_support != b->p_state_change_support)
419 static struct clk_mgr_funcs dcn2_funcs = {
420 .get_dp_ref_clk_frequency = dce12_get_dp_ref_freq_khz,
421 .update_clocks = dcn2_update_clocks,
422 .init_clocks = dcn2_init_clocks,
423 .enable_pme_wa = dcn2_enable_pme_wa,
424 .get_clock = dcn2_get_clock,
425 .are_clock_states_equal = dcn2_are_clock_states_equal,
429 void dcn20_clk_mgr_construct(
430 struct dc_context *ctx,
431 struct clk_mgr_internal *clk_mgr,
432 struct pp_smu_funcs *pp_smu,
435 clk_mgr->base.ctx = ctx;
436 clk_mgr->pp_smu = pp_smu;
437 clk_mgr->base.funcs = &dcn2_funcs;
438 clk_mgr->regs = &clk_mgr_regs;
439 clk_mgr->clk_mgr_shift = &clk_mgr_shift;
440 clk_mgr->clk_mgr_mask = &clk_mgr_mask;
442 clk_mgr->dccg = dccg;
443 clk_mgr->dfs_bypass_disp_clk = 0;
445 clk_mgr->dprefclk_ss_percentage = 0;
446 clk_mgr->dprefclk_ss_divider = 1000;
447 clk_mgr->ss_on_dprefclk = false;
449 clk_mgr->base.dprefclk_khz = 700000; // 700 MHz planned if VCO is 3.85 GHz, will be retrieved
451 if (IS_FPGA_MAXIMUS_DC(ctx->dce_environment)) {
452 dcn2_funcs.update_clocks = dcn2_update_clocks_fpga;
453 clk_mgr->base.dentist_vco_freq_khz = 3850000;
456 /* DFS Slice 2 should be used for DPREFCLK */
457 int dprefclk_did = REG_READ(CLK3_CLK2_DFS_CNTL);
458 /* Convert DPREFCLK DFS Slice DID to actual divider*/
459 int target_div = dentist_get_divider_from_did(dprefclk_did);
461 /* get FbMult value */
462 uint32_t pll_req_reg = REG_READ(CLK3_CLK_PLL_REQ);
463 struct fixed31_32 pll_req;
465 /* set up a fixed-point number
466 * this works because the int part is on the right edge of the register
467 * and the frac part is on the left edge
470 pll_req = dc_fixpt_from_int(pll_req_reg & clk_mgr->clk_mgr_mask->FbMult_int);
471 pll_req.value |= pll_req_reg & clk_mgr->clk_mgr_mask->FbMult_frac;
473 /* multiply by REFCLK period */
474 pll_req = dc_fixpt_mul_int(pll_req, 100000);
476 /* integer part is now VCO frequency in kHz */
477 clk_mgr->base.dentist_vco_freq_khz = dc_fixpt_floor(pll_req);
479 /* in case we don't get a value from the register, use default */
480 if (clk_mgr->base.dentist_vco_freq_khz == 0)
481 clk_mgr->base.dentist_vco_freq_khz = 3850000;
483 /* Calculate the DPREFCLK in kHz.*/
484 clk_mgr->base.dprefclk_khz = (DENTIST_DIVIDER_RANGE_SCALE_FACTOR
485 * clk_mgr->base.dentist_vco_freq_khz) / target_div;
487 //Integrated_info table does not exist on dGPU projects so should not be referenced
488 //anywhere in code for dGPUs.
489 //Also there is no plan for now that DFS BYPASS will be used on NV10/12/14.
490 clk_mgr->dfs_bypass_enabled = false;
492 dce_clock_read_ss_info(clk_mgr);