1 /*
2  * Copyright 2018 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 
26 #include "dccg.h"
27 #include "clk_mgr_internal.h"
28 
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"
34 
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"
40 
41 
42 #undef FN
43 #define FN(reg_name, field_name) \
44 	clk_mgr->clk_mgr_shift->field_name, clk_mgr->clk_mgr_mask->field_name
45 
46 #define REG(reg) \
47 	(clk_mgr->regs->reg)
48 
49 #define BASE_INNER(seg) DCN_BASE__INST0_SEG ## seg
50 
51 #define BASE(seg) BASE_INNER(seg)
52 
53 #define SR(reg_name)\
54 		.reg_name = BASE(mm ## reg_name ## _BASE_IDX) +  \
55 					mm ## reg_name
56 
57 #define CLK_BASE_INNER(seg) \
58 	CLK_BASE__INST0_SEG ## seg
59 
60 
61 static const struct clk_mgr_registers clk_mgr_regs = {
62 	CLK_REG_LIST_NV10()
63 };
64 
65 static const struct clk_mgr_shift clk_mgr_shift = {
66 	CLK_MASK_SH_LIST_NV10(__SHIFT)
67 };
68 
69 static const struct clk_mgr_mask clk_mgr_mask = {
70 	CLK_MASK_SH_LIST_NV10(_MASK)
71 };
72 
73 uint32_t dentist_get_did_from_divider(int divider)
74 {
75 	uint32_t divider_id;
76 
77 	/* we want to floor here to get higher clock than required rather than lower */
78 	if (divider < DENTIST_DIVIDER_RANGE_2_START) {
79 		if (divider < DENTIST_DIVIDER_RANGE_1_START)
80 			divider_id = DENTIST_BASE_DID_1;
81 		else
82 			divider_id = DENTIST_BASE_DID_1
83 				+ (divider - DENTIST_DIVIDER_RANGE_1_START)
84 					/ DENTIST_DIVIDER_RANGE_1_STEP;
85 	} else if (divider < DENTIST_DIVIDER_RANGE_3_START) {
86 		divider_id = DENTIST_BASE_DID_2
87 				+ (divider - DENTIST_DIVIDER_RANGE_2_START)
88 					/ DENTIST_DIVIDER_RANGE_2_STEP;
89 	} else if (divider < DENTIST_DIVIDER_RANGE_4_START) {
90 		divider_id = DENTIST_BASE_DID_3
91 				+ (divider - DENTIST_DIVIDER_RANGE_3_START)
92 					/ DENTIST_DIVIDER_RANGE_3_STEP;
93 	} else {
94 		divider_id = DENTIST_BASE_DID_4
95 				+ (divider - DENTIST_DIVIDER_RANGE_4_START)
96 					/ DENTIST_DIVIDER_RANGE_4_STEP;
97 		if (divider_id > DENTIST_MAX_DID)
98 			divider_id = DENTIST_MAX_DID;
99 	}
100 
101 	return divider_id;
102 }
103 
104 void dcn20_update_clocks_update_dpp_dto(struct clk_mgr_internal *clk_mgr,
105 		struct dc_state *context, bool safe_to_lower)
106 {
107 	int i;
108 
109 	clk_mgr->dccg->ref_dppclk = clk_mgr->base.clks.dppclk_khz;
110 	for (i = 0; i < clk_mgr->base.ctx->dc->res_pool->pipe_count; i++) {
111 		int dpp_inst, dppclk_khz, prev_dppclk_khz;
112 
113 		/* Loop index will match dpp->inst if resource exists,
114 		 * and we want to avoid dependency on dpp object
115 		 */
116 		dpp_inst = i;
117 		dppclk_khz = context->res_ctx.pipe_ctx[i].plane_res.bw.dppclk_khz;
118 
119 		prev_dppclk_khz = clk_mgr->dccg->pipe_dppclk_khz[i];
120 
121 		if (safe_to_lower || prev_dppclk_khz < dppclk_khz)
122 			clk_mgr->dccg->funcs->update_dpp_dto(
123 							clk_mgr->dccg, dpp_inst, dppclk_khz);
124 	}
125 }
126 
127 void dcn20_update_clocks_update_dentist(struct clk_mgr_internal *clk_mgr, struct dc_state *context)
128 {
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;
133 
134 	uint32_t dppclk_wdivider = dentist_get_did_from_divider(dpp_divider);
135 	uint32_t dispclk_wdivider = dentist_get_did_from_divider(disp_divider);
136 	uint32_t current_dispclk_wdivider;
137 	uint32_t i;
138 
139 	REG_GET(DENTIST_DISPCLK_CNTL,
140 			DENTIST_DISPCLK_WDIVIDER, &current_dispclk_wdivider);
141 
142 	/* When changing divider to or from 127, some extra programming is required to prevent corruption */
143 	if (current_dispclk_wdivider == 127 && dispclk_wdivider != 127) {
144 		for (i = 0; i < clk_mgr->base.ctx->dc->res_pool->pipe_count; i++) {
145 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
146 			uint32_t fifo_level;
147 			struct dccg *dccg = clk_mgr->base.ctx->dc->res_pool->dccg;
148 			struct stream_encoder *stream_enc = pipe_ctx->stream_res.stream_enc;
149 			int32_t N;
150 			int32_t j;
151 
152 			if (!pipe_ctx->stream)
153 				continue;
154 			/* Virtual encoders don't have this function */
155 			if (!stream_enc->funcs->get_fifo_cal_average_level)
156 				continue;
157 			fifo_level = stream_enc->funcs->get_fifo_cal_average_level(
158 					stream_enc);
159 			N = fifo_level / 4;
160 			dccg->funcs->set_fifo_errdet_ovr_en(
161 					dccg,
162 					true);
163 			for (j = 0; j < N - 4; j++)
164 				dccg->funcs->otg_drop_pixel(
165 						dccg,
166 						pipe_ctx->stream_res.tg->inst);
167 			dccg->funcs->set_fifo_errdet_ovr_en(
168 					dccg,
169 					false);
170 		}
171 	} else if (dispclk_wdivider == 127 && current_dispclk_wdivider != 127) {
172 		REG_UPDATE(DENTIST_DISPCLK_CNTL,
173 				DENTIST_DISPCLK_WDIVIDER, 126);
174 		REG_WAIT(DENTIST_DISPCLK_CNTL, DENTIST_DISPCLK_CHG_DONE, 1, 50, 100);
175 		for (i = 0; i < clk_mgr->base.ctx->dc->res_pool->pipe_count; i++) {
176 			struct pipe_ctx *pipe_ctx = &context->res_ctx.pipe_ctx[i];
177 			struct dccg *dccg = clk_mgr->base.ctx->dc->res_pool->dccg;
178 			struct stream_encoder *stream_enc = pipe_ctx->stream_res.stream_enc;
179 			uint32_t fifo_level;
180 			int32_t N;
181 			int32_t j;
182 
183 			if (!pipe_ctx->stream)
184 				continue;
185 			/* Virtual encoders don't have this function */
186 			if (!stream_enc->funcs->get_fifo_cal_average_level)
187 				continue;
188 			fifo_level = stream_enc->funcs->get_fifo_cal_average_level(
189 					stream_enc);
190 			N = fifo_level / 4;
191 			dccg->funcs->set_fifo_errdet_ovr_en(dccg, true);
192 			for (j = 0; j < 12 - N; j++)
193 				dccg->funcs->otg_add_pixel(dccg,
194 						pipe_ctx->stream_res.tg->inst);
195 			dccg->funcs->set_fifo_errdet_ovr_en(dccg, false);
196 		}
197 	}
198 
199 	REG_UPDATE(DENTIST_DISPCLK_CNTL,
200 			DENTIST_DISPCLK_WDIVIDER, dispclk_wdivider);
201 	REG_WAIT(DENTIST_DISPCLK_CNTL, DENTIST_DISPCLK_CHG_DONE, 1, 50, 1000);
202 	REG_UPDATE(DENTIST_DISPCLK_CNTL,
203 			DENTIST_DPPCLK_WDIVIDER, dppclk_wdivider);
204 	REG_WAIT(DENTIST_DISPCLK_CNTL, DENTIST_DPPCLK_CHG_DONE, 1, 5, 100);
205 }
206 
207 
208 void dcn2_update_clocks(struct clk_mgr *clk_mgr_base,
209 			struct dc_state *context,
210 			bool safe_to_lower)
211 {
212 	struct clk_mgr_internal *clk_mgr = TO_CLK_MGR_INTERNAL(clk_mgr_base);
213 	struct dc_clocks *new_clocks = &context->bw_ctx.bw.dcn.clk;
214 	struct dc *dc = clk_mgr_base->ctx->dc;
215 	struct pp_smu_funcs_nv *pp_smu = NULL;
216 	int display_count;
217 	bool update_dppclk = false;
218 	bool update_dispclk = false;
219 	bool enter_display_off = false;
220 	bool dpp_clock_lowered = false;
221 	struct dmcu *dmcu = clk_mgr_base->ctx->dc->res_pool->dmcu;
222 	bool force_reset = false;
223 	bool p_state_change_support;
224 	int total_plane_count;
225 
226 	if (dc->work_arounds.skip_clock_update)
227 		return;
228 
229 	if (clk_mgr_base->clks.dispclk_khz == 0 ||
230 		dc->debug.force_clock_mode & 0x1) {
231 		//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.
232 		force_reset = true;
233 
234 		dcn2_read_clocks_from_hw_dentist(clk_mgr_base);
235 
236 		//force_clock_mode 0x1:  force reset the clock even it is the same clock as long as it is in Passive level.
237 	}
238 	display_count = clk_mgr_helper_get_active_display_cnt(dc, context);
239 	if (dc->res_pool->pp_smu)
240 		pp_smu = &dc->res_pool->pp_smu->nv_funcs;
241 
242 	if (display_count == 0)
243 		enter_display_off = true;
244 
245 	if (enter_display_off == safe_to_lower) {
246 		if (pp_smu && pp_smu->set_display_count)
247 			pp_smu->set_display_count(&pp_smu->pp_smu, display_count);
248 	}
249 
250 	if (dc->debug.force_min_dcfclk_mhz > 0)
251 		new_clocks->dcfclk_khz = (new_clocks->dcfclk_khz > (dc->debug.force_min_dcfclk_mhz * 1000)) ?
252 				new_clocks->dcfclk_khz : (dc->debug.force_min_dcfclk_mhz * 1000);
253 
254 	if (should_set_clock(safe_to_lower, new_clocks->dcfclk_khz, clk_mgr_base->clks.dcfclk_khz)) {
255 		clk_mgr_base->clks.dcfclk_khz = new_clocks->dcfclk_khz;
256 		if (pp_smu && pp_smu->set_hard_min_dcfclk_by_freq)
257 			pp_smu->set_hard_min_dcfclk_by_freq(&pp_smu->pp_smu, khz_to_mhz_ceil(clk_mgr_base->clks.dcfclk_khz));
258 	}
259 
260 	if (should_set_clock(safe_to_lower,
261 			new_clocks->dcfclk_deep_sleep_khz, clk_mgr_base->clks.dcfclk_deep_sleep_khz)) {
262 		clk_mgr_base->clks.dcfclk_deep_sleep_khz = new_clocks->dcfclk_deep_sleep_khz;
263 		if (pp_smu && pp_smu->set_min_deep_sleep_dcfclk)
264 			pp_smu->set_min_deep_sleep_dcfclk(&pp_smu->pp_smu, khz_to_mhz_ceil(clk_mgr_base->clks.dcfclk_deep_sleep_khz));
265 	}
266 
267 	if (should_set_clock(safe_to_lower, new_clocks->socclk_khz, clk_mgr_base->clks.socclk_khz)) {
268 		clk_mgr_base->clks.socclk_khz = new_clocks->socclk_khz;
269 		if (pp_smu && pp_smu->set_hard_min_socclk_by_freq)
270 			pp_smu->set_hard_min_socclk_by_freq(&pp_smu->pp_smu, khz_to_mhz_ceil(clk_mgr_base->clks.socclk_khz));
271 	}
272 
273 	total_plane_count = clk_mgr_helper_get_active_plane_cnt(dc, context);
274 	p_state_change_support = new_clocks->p_state_change_support || (total_plane_count == 0);
275 	if (should_update_pstate_support(safe_to_lower, p_state_change_support, clk_mgr_base->clks.p_state_change_support)) {
276 		clk_mgr_base->clks.prev_p_state_change_support = clk_mgr_base->clks.p_state_change_support;
277 		clk_mgr_base->clks.p_state_change_support = p_state_change_support;
278 		if (pp_smu && pp_smu->set_pstate_handshake_support)
279 			pp_smu->set_pstate_handshake_support(&pp_smu->pp_smu, clk_mgr_base->clks.p_state_change_support);
280 	}
281 
282 	if (should_set_clock(safe_to_lower, new_clocks->dramclk_khz, clk_mgr_base->clks.dramclk_khz)) {
283 		clk_mgr_base->clks.dramclk_khz = new_clocks->dramclk_khz;
284 		if (pp_smu && pp_smu->set_hard_min_uclk_by_freq)
285 			pp_smu->set_hard_min_uclk_by_freq(&pp_smu->pp_smu, khz_to_mhz_ceil(clk_mgr_base->clks.dramclk_khz));
286 	}
287 
288 	if (should_set_clock(safe_to_lower, new_clocks->dppclk_khz, clk_mgr->base.clks.dppclk_khz)) {
289 		if (clk_mgr->base.clks.dppclk_khz > new_clocks->dppclk_khz)
290 			dpp_clock_lowered = true;
291 		clk_mgr->base.clks.dppclk_khz = new_clocks->dppclk_khz;
292 
293 		update_dppclk = true;
294 	}
295 
296 	if (should_set_clock(safe_to_lower, new_clocks->dispclk_khz, clk_mgr_base->clks.dispclk_khz)) {
297 		clk_mgr_base->clks.dispclk_khz = new_clocks->dispclk_khz;
298 
299 		update_dispclk = true;
300 	}
301 
302 	if (update_dppclk || update_dispclk) {
303 		new_clocks->disp_dpp_voltage_level_khz = new_clocks->dppclk_khz;
304 
305 		if (update_dispclk)
306 			new_clocks->disp_dpp_voltage_level_khz = new_clocks->dispclk_khz > new_clocks->dppclk_khz ? new_clocks->dispclk_khz : new_clocks->dppclk_khz;
307 
308 		clk_mgr_base->clks.disp_dpp_voltage_level_khz = new_clocks->disp_dpp_voltage_level_khz;
309 		if (pp_smu && pp_smu->set_voltage_by_freq)
310 			pp_smu->set_voltage_by_freq(&pp_smu->pp_smu, PP_SMU_NV_DISPCLK, khz_to_mhz_ceil(clk_mgr_base->clks.disp_dpp_voltage_level_khz));
311 	}
312 
313 	if (dc->config.forced_clocks == false || (force_reset && safe_to_lower)) {
314 		if (dpp_clock_lowered) {
315 			// if clock is being lowered, increase DTO before lowering refclk
316 			dcn20_update_clocks_update_dpp_dto(clk_mgr, context, safe_to_lower);
317 			dcn20_update_clocks_update_dentist(clk_mgr, context);
318 		} else {
319 			// if clock is being raised, increase refclk before lowering DTO
320 			if (update_dppclk || update_dispclk)
321 				dcn20_update_clocks_update_dentist(clk_mgr, context);
322 			// always update dtos unless clock is lowered and not safe to lower
323 			dcn20_update_clocks_update_dpp_dto(clk_mgr, context, safe_to_lower);
324 		}
325 	}
326 
327 	if (update_dispclk &&
328 			dmcu && dmcu->funcs->is_dmcu_initialized(dmcu)) {
329 		/*update dmcu for wait_loop count*/
330 		dmcu->funcs->set_psr_wait_loop(dmcu,
331 			clk_mgr_base->clks.dispclk_khz / 1000 / 7);
332 	}
333 }
334 
335 void dcn2_update_clocks_fpga(struct clk_mgr *clk_mgr,
336 		struct dc_state *context,
337 		bool safe_to_lower)
338 {
339 	struct clk_mgr_internal *clk_mgr_int = TO_CLK_MGR_INTERNAL(clk_mgr);
340 
341 	struct dc_clocks *new_clocks = &context->bw_ctx.bw.dcn.clk;
342 	/* Min fclk = 1.2GHz since all the extra scemi logic seems to run off of it */
343 	int fclk_adj = new_clocks->fclk_khz > 1200000 ? new_clocks->fclk_khz : 1200000;
344 
345 	if (should_set_clock(safe_to_lower, new_clocks->phyclk_khz, clk_mgr->clks.phyclk_khz)) {
346 		clk_mgr->clks.phyclk_khz = new_clocks->phyclk_khz;
347 	}
348 
349 	if (should_set_clock(safe_to_lower, new_clocks->dcfclk_khz, clk_mgr->clks.dcfclk_khz)) {
350 		clk_mgr->clks.dcfclk_khz = new_clocks->dcfclk_khz;
351 	}
352 
353 	if (should_set_clock(safe_to_lower,
354 			new_clocks->dcfclk_deep_sleep_khz, clk_mgr->clks.dcfclk_deep_sleep_khz)) {
355 		clk_mgr->clks.dcfclk_deep_sleep_khz = new_clocks->dcfclk_deep_sleep_khz;
356 	}
357 
358 	if (should_set_clock(safe_to_lower, new_clocks->socclk_khz, clk_mgr->clks.socclk_khz)) {
359 		clk_mgr->clks.socclk_khz = new_clocks->socclk_khz;
360 	}
361 
362 	if (should_set_clock(safe_to_lower, new_clocks->dramclk_khz, clk_mgr->clks.dramclk_khz)) {
363 		clk_mgr->clks.dramclk_khz = new_clocks->dramclk_khz;
364 	}
365 
366 	if (should_set_clock(safe_to_lower, new_clocks->dppclk_khz, clk_mgr->clks.dppclk_khz)) {
367 		clk_mgr->clks.dppclk_khz = new_clocks->dppclk_khz;
368 	}
369 
370 	if (should_set_clock(safe_to_lower, fclk_adj, clk_mgr->clks.fclk_khz)) {
371 		clk_mgr->clks.fclk_khz = fclk_adj;
372 	}
373 
374 	if (should_set_clock(safe_to_lower, new_clocks->dispclk_khz, clk_mgr->clks.dispclk_khz)) {
375 		clk_mgr->clks.dispclk_khz = new_clocks->dispclk_khz;
376 	}
377 
378 	/* Both fclk and ref_dppclk run on the same scemi clock.
379 	 * So take the higher value since the DPP DTO is typically programmed
380 	 * such that max dppclk is 1:1 with ref_dppclk.
381 	 */
382 	if (clk_mgr->clks.fclk_khz > clk_mgr->clks.dppclk_khz)
383 		clk_mgr->clks.dppclk_khz = clk_mgr->clks.fclk_khz;
384 	if (clk_mgr->clks.dppclk_khz > clk_mgr->clks.fclk_khz)
385 		clk_mgr->clks.fclk_khz = clk_mgr->clks.dppclk_khz;
386 
387 	// Both fclk and ref_dppclk run on the same scemi clock.
388 	clk_mgr_int->dccg->ref_dppclk = clk_mgr->clks.fclk_khz;
389 
390 	/* TODO: set dtbclk in correct place */
391 	clk_mgr->clks.dtbclk_en = false;
392 	dm_set_dcn_clocks(clk_mgr->ctx, &clk_mgr->clks);
393 }
394 
395 void dcn2_init_clocks(struct clk_mgr *clk_mgr)
396 {
397 	memset(&(clk_mgr->clks), 0, sizeof(struct dc_clocks));
398 	// Assumption is that boot state always supports pstate
399 	clk_mgr->clks.p_state_change_support = true;
400 	clk_mgr->clks.prev_p_state_change_support = true;
401 }
402 
403 static void dcn2_enable_pme_wa(struct clk_mgr *clk_mgr_base)
404 {
405 	struct clk_mgr_internal *clk_mgr = TO_CLK_MGR_INTERNAL(clk_mgr_base);
406 	struct pp_smu_funcs_nv *pp_smu = NULL;
407 
408 	if (clk_mgr->pp_smu) {
409 		pp_smu = &clk_mgr->pp_smu->nv_funcs;
410 
411 		if (pp_smu->set_pme_wa_enable)
412 			pp_smu->set_pme_wa_enable(&pp_smu->pp_smu);
413 	}
414 }
415 
416 
417 void dcn2_read_clocks_from_hw_dentist(struct clk_mgr *clk_mgr_base)
418 {
419 	struct clk_mgr_internal *clk_mgr = TO_CLK_MGR_INTERNAL(clk_mgr_base);
420 	uint32_t dispclk_wdivider;
421 	uint32_t dppclk_wdivider;
422 	int disp_divider;
423 	int dpp_divider;
424 
425 	REG_GET(DENTIST_DISPCLK_CNTL, DENTIST_DISPCLK_WDIVIDER, &dispclk_wdivider);
426 	REG_GET(DENTIST_DISPCLK_CNTL, DENTIST_DPPCLK_WDIVIDER, &dppclk_wdivider);
427 
428 	disp_divider = dentist_get_divider_from_did(dispclk_wdivider);
429 	dpp_divider = dentist_get_divider_from_did(dppclk_wdivider);
430 
431 	if (disp_divider && dpp_divider) {
432 		/* Calculate the current DFS clock, in kHz.*/
433 		clk_mgr_base->clks.dispclk_khz = (DENTIST_DIVIDER_RANGE_SCALE_FACTOR
434 			* clk_mgr->base.dentist_vco_freq_khz) / disp_divider;
435 
436 		clk_mgr_base->clks.dppclk_khz = (DENTIST_DIVIDER_RANGE_SCALE_FACTOR
437 				* clk_mgr->base.dentist_vco_freq_khz) / dpp_divider;
438 	}
439 
440 }
441 
442 void dcn2_get_clock(struct clk_mgr *clk_mgr,
443 		struct dc_state *context,
444 			enum dc_clock_type clock_type,
445 			struct dc_clock_config *clock_cfg)
446 {
447 
448 	if (clock_type == DC_CLOCK_TYPE_DISPCLK) {
449 		clock_cfg->max_clock_khz = context->bw_ctx.bw.dcn.clk.max_supported_dispclk_khz;
450 		clock_cfg->min_clock_khz = DCN_MINIMUM_DISPCLK_Khz;
451 		clock_cfg->current_clock_khz = clk_mgr->clks.dispclk_khz;
452 		clock_cfg->bw_requirequired_clock_khz = context->bw_ctx.bw.dcn.clk.bw_dispclk_khz;
453 	}
454 	if (clock_type == DC_CLOCK_TYPE_DPPCLK) {
455 		clock_cfg->max_clock_khz = context->bw_ctx.bw.dcn.clk.max_supported_dppclk_khz;
456 		clock_cfg->min_clock_khz = DCN_MINIMUM_DPPCLK_Khz;
457 		clock_cfg->current_clock_khz = clk_mgr->clks.dppclk_khz;
458 		clock_cfg->bw_requirequired_clock_khz = context->bw_ctx.bw.dcn.clk.bw_dppclk_khz;
459 	}
460 }
461 
462 static bool dcn2_are_clock_states_equal(struct dc_clocks *a,
463 		struct dc_clocks *b)
464 {
465 	if (a->dispclk_khz != b->dispclk_khz)
466 		return false;
467 	else if (a->dppclk_khz != b->dppclk_khz)
468 		return false;
469 	else if (a->disp_dpp_voltage_level_khz != b->disp_dpp_voltage_level_khz)
470 		return false;
471 	else if (a->dcfclk_khz != b->dcfclk_khz)
472 		return false;
473 	else if (a->socclk_khz != b->socclk_khz)
474 		return false;
475 	else if (a->dcfclk_deep_sleep_khz != b->dcfclk_deep_sleep_khz)
476 		return false;
477 	else if (a->dramclk_khz != b->dramclk_khz)
478 		return false;
479 	else if (a->p_state_change_support != b->p_state_change_support)
480 		return false;
481 
482 	return true;
483 }
484 
485 /* Notify clk_mgr of a change in link rate, update phyclk frequency if necessary */
486 static void dcn2_notify_link_rate_change(struct clk_mgr *clk_mgr_base, struct dc_link *link)
487 {
488 	struct clk_mgr_internal *clk_mgr = TO_CLK_MGR_INTERNAL(clk_mgr_base);
489 	unsigned int i, max_phyclk_req = 0;
490 	struct pp_smu_funcs_nv *pp_smu = NULL;
491 
492 	if (!clk_mgr->pp_smu || !clk_mgr->pp_smu->nv_funcs.set_voltage_by_freq)
493 		return;
494 
495 	pp_smu = &clk_mgr->pp_smu->nv_funcs;
496 
497 	clk_mgr->cur_phyclk_req_table[link->link_index] = link->cur_link_settings.link_rate * LINK_RATE_REF_FREQ_IN_KHZ;
498 
499 	for (i = 0; i < MAX_PIPES * 2; i++) {
500 		if (clk_mgr->cur_phyclk_req_table[i] > max_phyclk_req)
501 			max_phyclk_req = clk_mgr->cur_phyclk_req_table[i];
502 	}
503 
504 	if (max_phyclk_req != clk_mgr_base->clks.phyclk_khz) {
505 		clk_mgr_base->clks.phyclk_khz = max_phyclk_req;
506 		pp_smu->set_voltage_by_freq(&pp_smu->pp_smu, PP_SMU_NV_PHYCLK, khz_to_mhz_ceil(clk_mgr_base->clks.phyclk_khz));
507 	}
508 }
509 
510 static struct clk_mgr_funcs dcn2_funcs = {
511 	.get_dp_ref_clk_frequency = dce12_get_dp_ref_freq_khz,
512 	.update_clocks = dcn2_update_clocks,
513 	.init_clocks = dcn2_init_clocks,
514 	.enable_pme_wa = dcn2_enable_pme_wa,
515 	.get_clock = dcn2_get_clock,
516 	.are_clock_states_equal = dcn2_are_clock_states_equal,
517 	.notify_link_rate_change = dcn2_notify_link_rate_change,
518 };
519 
520 
521 void dcn20_clk_mgr_construct(
522 		struct dc_context *ctx,
523 		struct clk_mgr_internal *clk_mgr,
524 		struct pp_smu_funcs *pp_smu,
525 		struct dccg *dccg)
526 {
527 	clk_mgr->base.ctx = ctx;
528 	clk_mgr->pp_smu = pp_smu;
529 	clk_mgr->base.funcs = &dcn2_funcs;
530 	clk_mgr->regs = &clk_mgr_regs;
531 	clk_mgr->clk_mgr_shift = &clk_mgr_shift;
532 	clk_mgr->clk_mgr_mask = &clk_mgr_mask;
533 
534 	clk_mgr->dccg = dccg;
535 	clk_mgr->dfs_bypass_disp_clk = 0;
536 
537 	clk_mgr->dprefclk_ss_percentage = 0;
538 	clk_mgr->dprefclk_ss_divider = 1000;
539 	clk_mgr->ss_on_dprefclk = false;
540 
541 	clk_mgr->base.dprefclk_khz = 700000; // 700 MHz planned if VCO is 3.85 GHz, will be retrieved
542 
543 	if (IS_FPGA_MAXIMUS_DC(ctx->dce_environment)) {
544 		dcn2_funcs.update_clocks = dcn2_update_clocks_fpga;
545 		clk_mgr->base.dentist_vco_freq_khz = 3850000;
546 
547 	} else {
548 		/* DFS Slice 2 should be used for DPREFCLK */
549 		int dprefclk_did = REG_READ(CLK3_CLK2_DFS_CNTL);
550 		/* Convert DPREFCLK DFS Slice DID to actual divider*/
551 		int target_div = dentist_get_divider_from_did(dprefclk_did);
552 
553 		/* get FbMult value */
554 		uint32_t pll_req_reg = REG_READ(CLK3_CLK_PLL_REQ);
555 		struct fixed31_32 pll_req;
556 
557 		/* set up a fixed-point number
558 		 * this works because the int part is on the right edge of the register
559 		 * and the frac part is on the left edge
560 		 */
561 
562 		pll_req = dc_fixpt_from_int(pll_req_reg & clk_mgr->clk_mgr_mask->FbMult_int);
563 		pll_req.value |= pll_req_reg & clk_mgr->clk_mgr_mask->FbMult_frac;
564 
565 		/* multiply by REFCLK period */
566 		pll_req = dc_fixpt_mul_int(pll_req, 100000);
567 
568 		/* integer part is now VCO frequency in kHz */
569 		clk_mgr->base.dentist_vco_freq_khz = dc_fixpt_floor(pll_req);
570 
571 		/* in case we don't get a value from the register, use default */
572 		if (clk_mgr->base.dentist_vco_freq_khz == 0)
573 			clk_mgr->base.dentist_vco_freq_khz = 3850000;
574 
575 		/* Calculate the DPREFCLK in kHz.*/
576 		clk_mgr->base.dprefclk_khz = (DENTIST_DIVIDER_RANGE_SCALE_FACTOR
577 			* clk_mgr->base.dentist_vco_freq_khz) / target_div;
578 	}
579 	//Integrated_info table does not exist on dGPU projects so should not be referenced
580 	//anywhere in code for dGPUs.
581 	//Also there is no plan for now that DFS BYPASS will be used on NV10/12/14.
582 	clk_mgr->dfs_bypass_enabled = false;
583 
584 	dce_clock_read_ss_info(clk_mgr);
585 }
586 
587