1 /*
2  * Copyright © 2006-2017 Intel Corporation
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <linux/time.h>
25 
26 #include "hsw_ips.h"
27 #include "i915_reg.h"
28 #include "intel_atomic.h"
29 #include "intel_atomic_plane.h"
30 #include "intel_audio.h"
31 #include "intel_bw.h"
32 #include "intel_cdclk.h"
33 #include "intel_crtc.h"
34 #include "intel_de.h"
35 #include "intel_display_types.h"
36 #include "intel_mchbar_regs.h"
37 #include "intel_pci_config.h"
38 #include "intel_pcode.h"
39 #include "intel_psr.h"
40 #include "vlv_sideband.h"
41 
42 /**
43  * DOC: CDCLK / RAWCLK
44  *
45  * The display engine uses several different clocks to do its work. There
46  * are two main clocks involved that aren't directly related to the actual
47  * pixel clock or any symbol/bit clock of the actual output port. These
48  * are the core display clock (CDCLK) and RAWCLK.
49  *
50  * CDCLK clocks most of the display pipe logic, and thus its frequency
51  * must be high enough to support the rate at which pixels are flowing
52  * through the pipes. Downscaling must also be accounted as that increases
53  * the effective pixel rate.
54  *
55  * On several platforms the CDCLK frequency can be changed dynamically
56  * to minimize power consumption for a given display configuration.
57  * Typically changes to the CDCLK frequency require all the display pipes
58  * to be shut down while the frequency is being changed.
59  *
60  * On SKL+ the DMC will toggle the CDCLK off/on during DC5/6 entry/exit.
61  * DMC will not change the active CDCLK frequency however, so that part
62  * will still be performed by the driver directly.
63  *
64  * RAWCLK is a fixed frequency clock, often used by various auxiliary
65  * blocks such as AUX CH or backlight PWM. Hence the only thing we
66  * really need to know about RAWCLK is its frequency so that various
67  * dividers can be programmed correctly.
68  */
69 
70 struct intel_cdclk_funcs {
71 	void (*get_cdclk)(struct drm_i915_private *i915,
72 			  struct intel_cdclk_config *cdclk_config);
73 	void (*set_cdclk)(struct drm_i915_private *i915,
74 			  const struct intel_cdclk_config *cdclk_config,
75 			  enum pipe pipe);
76 	int (*modeset_calc_cdclk)(struct intel_cdclk_state *state);
77 	u8 (*calc_voltage_level)(int cdclk);
78 };
79 
80 void intel_cdclk_get_cdclk(struct drm_i915_private *dev_priv,
81 			   struct intel_cdclk_config *cdclk_config)
82 {
83 	dev_priv->display.funcs.cdclk->get_cdclk(dev_priv, cdclk_config);
84 }
85 
86 static void intel_cdclk_set_cdclk(struct drm_i915_private *dev_priv,
87 				  const struct intel_cdclk_config *cdclk_config,
88 				  enum pipe pipe)
89 {
90 	dev_priv->display.funcs.cdclk->set_cdclk(dev_priv, cdclk_config, pipe);
91 }
92 
93 static int intel_cdclk_modeset_calc_cdclk(struct drm_i915_private *dev_priv,
94 					  struct intel_cdclk_state *cdclk_config)
95 {
96 	return dev_priv->display.funcs.cdclk->modeset_calc_cdclk(cdclk_config);
97 }
98 
99 static u8 intel_cdclk_calc_voltage_level(struct drm_i915_private *dev_priv,
100 					 int cdclk)
101 {
102 	return dev_priv->display.funcs.cdclk->calc_voltage_level(cdclk);
103 }
104 
105 static void fixed_133mhz_get_cdclk(struct drm_i915_private *dev_priv,
106 				   struct intel_cdclk_config *cdclk_config)
107 {
108 	cdclk_config->cdclk = 133333;
109 }
110 
111 static void fixed_200mhz_get_cdclk(struct drm_i915_private *dev_priv,
112 				   struct intel_cdclk_config *cdclk_config)
113 {
114 	cdclk_config->cdclk = 200000;
115 }
116 
117 static void fixed_266mhz_get_cdclk(struct drm_i915_private *dev_priv,
118 				   struct intel_cdclk_config *cdclk_config)
119 {
120 	cdclk_config->cdclk = 266667;
121 }
122 
123 static void fixed_333mhz_get_cdclk(struct drm_i915_private *dev_priv,
124 				   struct intel_cdclk_config *cdclk_config)
125 {
126 	cdclk_config->cdclk = 333333;
127 }
128 
129 static void fixed_400mhz_get_cdclk(struct drm_i915_private *dev_priv,
130 				   struct intel_cdclk_config *cdclk_config)
131 {
132 	cdclk_config->cdclk = 400000;
133 }
134 
135 static void fixed_450mhz_get_cdclk(struct drm_i915_private *dev_priv,
136 				   struct intel_cdclk_config *cdclk_config)
137 {
138 	cdclk_config->cdclk = 450000;
139 }
140 
141 static void i85x_get_cdclk(struct drm_i915_private *dev_priv,
142 			   struct intel_cdclk_config *cdclk_config)
143 {
144 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
145 	u16 hpllcc = 0;
146 
147 	/*
148 	 * 852GM/852GMV only supports 133 MHz and the HPLLCC
149 	 * encoding is different :(
150 	 * FIXME is this the right way to detect 852GM/852GMV?
151 	 */
152 	if (pdev->revision == 0x1) {
153 		cdclk_config->cdclk = 133333;
154 		return;
155 	}
156 
157 	pci_bus_read_config_word(pdev->bus,
158 				 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
159 
160 	/* Assume that the hardware is in the high speed state.  This
161 	 * should be the default.
162 	 */
163 	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
164 	case GC_CLOCK_133_200:
165 	case GC_CLOCK_133_200_2:
166 	case GC_CLOCK_100_200:
167 		cdclk_config->cdclk = 200000;
168 		break;
169 	case GC_CLOCK_166_250:
170 		cdclk_config->cdclk = 250000;
171 		break;
172 	case GC_CLOCK_100_133:
173 		cdclk_config->cdclk = 133333;
174 		break;
175 	case GC_CLOCK_133_266:
176 	case GC_CLOCK_133_266_2:
177 	case GC_CLOCK_166_266:
178 		cdclk_config->cdclk = 266667;
179 		break;
180 	}
181 }
182 
183 static void i915gm_get_cdclk(struct drm_i915_private *dev_priv,
184 			     struct intel_cdclk_config *cdclk_config)
185 {
186 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
187 	u16 gcfgc = 0;
188 
189 	pci_read_config_word(pdev, GCFGC, &gcfgc);
190 
191 	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
192 		cdclk_config->cdclk = 133333;
193 		return;
194 	}
195 
196 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
197 	case GC_DISPLAY_CLOCK_333_320_MHZ:
198 		cdclk_config->cdclk = 333333;
199 		break;
200 	default:
201 	case GC_DISPLAY_CLOCK_190_200_MHZ:
202 		cdclk_config->cdclk = 190000;
203 		break;
204 	}
205 }
206 
207 static void i945gm_get_cdclk(struct drm_i915_private *dev_priv,
208 			     struct intel_cdclk_config *cdclk_config)
209 {
210 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
211 	u16 gcfgc = 0;
212 
213 	pci_read_config_word(pdev, GCFGC, &gcfgc);
214 
215 	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
216 		cdclk_config->cdclk = 133333;
217 		return;
218 	}
219 
220 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
221 	case GC_DISPLAY_CLOCK_333_320_MHZ:
222 		cdclk_config->cdclk = 320000;
223 		break;
224 	default:
225 	case GC_DISPLAY_CLOCK_190_200_MHZ:
226 		cdclk_config->cdclk = 200000;
227 		break;
228 	}
229 }
230 
231 static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
232 {
233 	static const unsigned int blb_vco[8] = {
234 		[0] = 3200000,
235 		[1] = 4000000,
236 		[2] = 5333333,
237 		[3] = 4800000,
238 		[4] = 6400000,
239 	};
240 	static const unsigned int pnv_vco[8] = {
241 		[0] = 3200000,
242 		[1] = 4000000,
243 		[2] = 5333333,
244 		[3] = 4800000,
245 		[4] = 2666667,
246 	};
247 	static const unsigned int cl_vco[8] = {
248 		[0] = 3200000,
249 		[1] = 4000000,
250 		[2] = 5333333,
251 		[3] = 6400000,
252 		[4] = 3333333,
253 		[5] = 3566667,
254 		[6] = 4266667,
255 	};
256 	static const unsigned int elk_vco[8] = {
257 		[0] = 3200000,
258 		[1] = 4000000,
259 		[2] = 5333333,
260 		[3] = 4800000,
261 	};
262 	static const unsigned int ctg_vco[8] = {
263 		[0] = 3200000,
264 		[1] = 4000000,
265 		[2] = 5333333,
266 		[3] = 6400000,
267 		[4] = 2666667,
268 		[5] = 4266667,
269 	};
270 	const unsigned int *vco_table;
271 	unsigned int vco;
272 	u8 tmp = 0;
273 
274 	/* FIXME other chipsets? */
275 	if (IS_GM45(dev_priv))
276 		vco_table = ctg_vco;
277 	else if (IS_G45(dev_priv))
278 		vco_table = elk_vco;
279 	else if (IS_I965GM(dev_priv))
280 		vco_table = cl_vco;
281 	else if (IS_PINEVIEW(dev_priv))
282 		vco_table = pnv_vco;
283 	else if (IS_G33(dev_priv))
284 		vco_table = blb_vco;
285 	else
286 		return 0;
287 
288 	tmp = intel_de_read(dev_priv,
289 			    IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
290 
291 	vco = vco_table[tmp & 0x7];
292 	if (vco == 0)
293 		drm_err(&dev_priv->drm, "Bad HPLL VCO (HPLLVCO=0x%02x)\n",
294 			tmp);
295 	else
296 		drm_dbg_kms(&dev_priv->drm, "HPLL VCO %u kHz\n", vco);
297 
298 	return vco;
299 }
300 
301 static void g33_get_cdclk(struct drm_i915_private *dev_priv,
302 			  struct intel_cdclk_config *cdclk_config)
303 {
304 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
305 	static const u8 div_3200[] = { 12, 10,  8,  7, 5, 16 };
306 	static const u8 div_4000[] = { 14, 12, 10,  8, 6, 20 };
307 	static const u8 div_4800[] = { 20, 14, 12, 10, 8, 24 };
308 	static const u8 div_5333[] = { 20, 16, 12, 12, 8, 28 };
309 	const u8 *div_table;
310 	unsigned int cdclk_sel;
311 	u16 tmp = 0;
312 
313 	cdclk_config->vco = intel_hpll_vco(dev_priv);
314 
315 	pci_read_config_word(pdev, GCFGC, &tmp);
316 
317 	cdclk_sel = (tmp >> 4) & 0x7;
318 
319 	if (cdclk_sel >= ARRAY_SIZE(div_3200))
320 		goto fail;
321 
322 	switch (cdclk_config->vco) {
323 	case 3200000:
324 		div_table = div_3200;
325 		break;
326 	case 4000000:
327 		div_table = div_4000;
328 		break;
329 	case 4800000:
330 		div_table = div_4800;
331 		break;
332 	case 5333333:
333 		div_table = div_5333;
334 		break;
335 	default:
336 		goto fail;
337 	}
338 
339 	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco,
340 						div_table[cdclk_sel]);
341 	return;
342 
343 fail:
344 	drm_err(&dev_priv->drm,
345 		"Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n",
346 		cdclk_config->vco, tmp);
347 	cdclk_config->cdclk = 190476;
348 }
349 
350 static void pnv_get_cdclk(struct drm_i915_private *dev_priv,
351 			  struct intel_cdclk_config *cdclk_config)
352 {
353 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
354 	u16 gcfgc = 0;
355 
356 	pci_read_config_word(pdev, GCFGC, &gcfgc);
357 
358 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
359 	case GC_DISPLAY_CLOCK_267_MHZ_PNV:
360 		cdclk_config->cdclk = 266667;
361 		break;
362 	case GC_DISPLAY_CLOCK_333_MHZ_PNV:
363 		cdclk_config->cdclk = 333333;
364 		break;
365 	case GC_DISPLAY_CLOCK_444_MHZ_PNV:
366 		cdclk_config->cdclk = 444444;
367 		break;
368 	case GC_DISPLAY_CLOCK_200_MHZ_PNV:
369 		cdclk_config->cdclk = 200000;
370 		break;
371 	default:
372 		drm_err(&dev_priv->drm,
373 			"Unknown pnv display core clock 0x%04x\n", gcfgc);
374 		fallthrough;
375 	case GC_DISPLAY_CLOCK_133_MHZ_PNV:
376 		cdclk_config->cdclk = 133333;
377 		break;
378 	case GC_DISPLAY_CLOCK_167_MHZ_PNV:
379 		cdclk_config->cdclk = 166667;
380 		break;
381 	}
382 }
383 
384 static void i965gm_get_cdclk(struct drm_i915_private *dev_priv,
385 			     struct intel_cdclk_config *cdclk_config)
386 {
387 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
388 	static const u8 div_3200[] = { 16, 10,  8 };
389 	static const u8 div_4000[] = { 20, 12, 10 };
390 	static const u8 div_5333[] = { 24, 16, 14 };
391 	const u8 *div_table;
392 	unsigned int cdclk_sel;
393 	u16 tmp = 0;
394 
395 	cdclk_config->vco = intel_hpll_vco(dev_priv);
396 
397 	pci_read_config_word(pdev, GCFGC, &tmp);
398 
399 	cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
400 
401 	if (cdclk_sel >= ARRAY_SIZE(div_3200))
402 		goto fail;
403 
404 	switch (cdclk_config->vco) {
405 	case 3200000:
406 		div_table = div_3200;
407 		break;
408 	case 4000000:
409 		div_table = div_4000;
410 		break;
411 	case 5333333:
412 		div_table = div_5333;
413 		break;
414 	default:
415 		goto fail;
416 	}
417 
418 	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco,
419 						div_table[cdclk_sel]);
420 	return;
421 
422 fail:
423 	drm_err(&dev_priv->drm,
424 		"Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n",
425 		cdclk_config->vco, tmp);
426 	cdclk_config->cdclk = 200000;
427 }
428 
429 static void gm45_get_cdclk(struct drm_i915_private *dev_priv,
430 			   struct intel_cdclk_config *cdclk_config)
431 {
432 	struct pci_dev *pdev = to_pci_dev(dev_priv->drm.dev);
433 	unsigned int cdclk_sel;
434 	u16 tmp = 0;
435 
436 	cdclk_config->vco = intel_hpll_vco(dev_priv);
437 
438 	pci_read_config_word(pdev, GCFGC, &tmp);
439 
440 	cdclk_sel = (tmp >> 12) & 0x1;
441 
442 	switch (cdclk_config->vco) {
443 	case 2666667:
444 	case 4000000:
445 	case 5333333:
446 		cdclk_config->cdclk = cdclk_sel ? 333333 : 222222;
447 		break;
448 	case 3200000:
449 		cdclk_config->cdclk = cdclk_sel ? 320000 : 228571;
450 		break;
451 	default:
452 		drm_err(&dev_priv->drm,
453 			"Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n",
454 			cdclk_config->vco, tmp);
455 		cdclk_config->cdclk = 222222;
456 		break;
457 	}
458 }
459 
460 static void hsw_get_cdclk(struct drm_i915_private *dev_priv,
461 			  struct intel_cdclk_config *cdclk_config)
462 {
463 	u32 lcpll = intel_de_read(dev_priv, LCPLL_CTL);
464 	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
465 
466 	if (lcpll & LCPLL_CD_SOURCE_FCLK)
467 		cdclk_config->cdclk = 800000;
468 	else if (intel_de_read(dev_priv, FUSE_STRAP) & HSW_CDCLK_LIMIT)
469 		cdclk_config->cdclk = 450000;
470 	else if (freq == LCPLL_CLK_FREQ_450)
471 		cdclk_config->cdclk = 450000;
472 	else if (IS_HSW_ULT(dev_priv))
473 		cdclk_config->cdclk = 337500;
474 	else
475 		cdclk_config->cdclk = 540000;
476 }
477 
478 static int vlv_calc_cdclk(struct drm_i915_private *dev_priv, int min_cdclk)
479 {
480 	int freq_320 = (dev_priv->hpll_freq <<  1) % 320000 != 0 ?
481 		333333 : 320000;
482 
483 	/*
484 	 * We seem to get an unstable or solid color picture at 200MHz.
485 	 * Not sure what's wrong. For now use 200MHz only when all pipes
486 	 * are off.
487 	 */
488 	if (IS_VALLEYVIEW(dev_priv) && min_cdclk > freq_320)
489 		return 400000;
490 	else if (min_cdclk > 266667)
491 		return freq_320;
492 	else if (min_cdclk > 0)
493 		return 266667;
494 	else
495 		return 200000;
496 }
497 
498 static u8 vlv_calc_voltage_level(struct drm_i915_private *dev_priv, int cdclk)
499 {
500 	if (IS_VALLEYVIEW(dev_priv)) {
501 		if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
502 			return 2;
503 		else if (cdclk >= 266667)
504 			return 1;
505 		else
506 			return 0;
507 	} else {
508 		/*
509 		 * Specs are full of misinformation, but testing on actual
510 		 * hardware has shown that we just need to write the desired
511 		 * CCK divider into the Punit register.
512 		 */
513 		return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
514 	}
515 }
516 
517 static void vlv_get_cdclk(struct drm_i915_private *dev_priv,
518 			  struct intel_cdclk_config *cdclk_config)
519 {
520 	u32 val;
521 
522 	vlv_iosf_sb_get(dev_priv,
523 			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));
524 
525 	cdclk_config->vco = vlv_get_hpll_vco(dev_priv);
526 	cdclk_config->cdclk = vlv_get_cck_clock(dev_priv, "cdclk",
527 						CCK_DISPLAY_CLOCK_CONTROL,
528 						cdclk_config->vco);
529 
530 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
531 
532 	vlv_iosf_sb_put(dev_priv,
533 			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));
534 
535 	if (IS_VALLEYVIEW(dev_priv))
536 		cdclk_config->voltage_level = (val & DSPFREQGUAR_MASK) >>
537 			DSPFREQGUAR_SHIFT;
538 	else
539 		cdclk_config->voltage_level = (val & DSPFREQGUAR_MASK_CHV) >>
540 			DSPFREQGUAR_SHIFT_CHV;
541 }
542 
543 static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
544 {
545 	unsigned int credits, default_credits;
546 
547 	if (IS_CHERRYVIEW(dev_priv))
548 		default_credits = PFI_CREDIT(12);
549 	else
550 		default_credits = PFI_CREDIT(8);
551 
552 	if (dev_priv->display.cdclk.hw.cdclk >= dev_priv->czclk_freq) {
553 		/* CHV suggested value is 31 or 63 */
554 		if (IS_CHERRYVIEW(dev_priv))
555 			credits = PFI_CREDIT_63;
556 		else
557 			credits = PFI_CREDIT(15);
558 	} else {
559 		credits = default_credits;
560 	}
561 
562 	/*
563 	 * WA - write default credits before re-programming
564 	 * FIXME: should we also set the resend bit here?
565 	 */
566 	intel_de_write(dev_priv, GCI_CONTROL,
567 		       VGA_FAST_MODE_DISABLE | default_credits);
568 
569 	intel_de_write(dev_priv, GCI_CONTROL,
570 		       VGA_FAST_MODE_DISABLE | credits | PFI_CREDIT_RESEND);
571 
572 	/*
573 	 * FIXME is this guaranteed to clear
574 	 * immediately or should we poll for it?
575 	 */
576 	drm_WARN_ON(&dev_priv->drm,
577 		    intel_de_read(dev_priv, GCI_CONTROL) & PFI_CREDIT_RESEND);
578 }
579 
580 static void vlv_set_cdclk(struct drm_i915_private *dev_priv,
581 			  const struct intel_cdclk_config *cdclk_config,
582 			  enum pipe pipe)
583 {
584 	int cdclk = cdclk_config->cdclk;
585 	u32 val, cmd = cdclk_config->voltage_level;
586 	intel_wakeref_t wakeref;
587 
588 	switch (cdclk) {
589 	case 400000:
590 	case 333333:
591 	case 320000:
592 	case 266667:
593 	case 200000:
594 		break;
595 	default:
596 		MISSING_CASE(cdclk);
597 		return;
598 	}
599 
600 	/* There are cases where we can end up here with power domains
601 	 * off and a CDCLK frequency other than the minimum, like when
602 	 * issuing a modeset without actually changing any display after
603 	 * a system suspend.  So grab the display core domain, which covers
604 	 * the HW blocks needed for the following programming.
605 	 */
606 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
607 
608 	vlv_iosf_sb_get(dev_priv,
609 			BIT(VLV_IOSF_SB_CCK) |
610 			BIT(VLV_IOSF_SB_BUNIT) |
611 			BIT(VLV_IOSF_SB_PUNIT));
612 
613 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
614 	val &= ~DSPFREQGUAR_MASK;
615 	val |= (cmd << DSPFREQGUAR_SHIFT);
616 	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
617 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM) &
618 		      DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
619 		     50)) {
620 		drm_err(&dev_priv->drm,
621 			"timed out waiting for CDclk change\n");
622 	}
623 
624 	if (cdclk == 400000) {
625 		u32 divider;
626 
627 		divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1,
628 					    cdclk) - 1;
629 
630 		/* adjust cdclk divider */
631 		val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
632 		val &= ~CCK_FREQUENCY_VALUES;
633 		val |= divider;
634 		vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
635 
636 		if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
637 			      CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
638 			     50))
639 			drm_err(&dev_priv->drm,
640 				"timed out waiting for CDclk change\n");
641 	}
642 
643 	/* adjust self-refresh exit latency value */
644 	val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
645 	val &= ~0x7f;
646 
647 	/*
648 	 * For high bandwidth configs, we set a higher latency in the bunit
649 	 * so that the core display fetch happens in time to avoid underruns.
650 	 */
651 	if (cdclk == 400000)
652 		val |= 4500 / 250; /* 4.5 usec */
653 	else
654 		val |= 3000 / 250; /* 3.0 usec */
655 	vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
656 
657 	vlv_iosf_sb_put(dev_priv,
658 			BIT(VLV_IOSF_SB_CCK) |
659 			BIT(VLV_IOSF_SB_BUNIT) |
660 			BIT(VLV_IOSF_SB_PUNIT));
661 
662 	intel_update_cdclk(dev_priv);
663 
664 	vlv_program_pfi_credits(dev_priv);
665 
666 	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
667 }
668 
669 static void chv_set_cdclk(struct drm_i915_private *dev_priv,
670 			  const struct intel_cdclk_config *cdclk_config,
671 			  enum pipe pipe)
672 {
673 	int cdclk = cdclk_config->cdclk;
674 	u32 val, cmd = cdclk_config->voltage_level;
675 	intel_wakeref_t wakeref;
676 
677 	switch (cdclk) {
678 	case 333333:
679 	case 320000:
680 	case 266667:
681 	case 200000:
682 		break;
683 	default:
684 		MISSING_CASE(cdclk);
685 		return;
686 	}
687 
688 	/* There are cases where we can end up here with power domains
689 	 * off and a CDCLK frequency other than the minimum, like when
690 	 * issuing a modeset without actually changing any display after
691 	 * a system suspend.  So grab the display core domain, which covers
692 	 * the HW blocks needed for the following programming.
693 	 */
694 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
695 
696 	vlv_punit_get(dev_priv);
697 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
698 	val &= ~DSPFREQGUAR_MASK_CHV;
699 	val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
700 	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
701 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM) &
702 		      DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
703 		     50)) {
704 		drm_err(&dev_priv->drm,
705 			"timed out waiting for CDclk change\n");
706 	}
707 
708 	vlv_punit_put(dev_priv);
709 
710 	intel_update_cdclk(dev_priv);
711 
712 	vlv_program_pfi_credits(dev_priv);
713 
714 	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
715 }
716 
717 static int bdw_calc_cdclk(int min_cdclk)
718 {
719 	if (min_cdclk > 540000)
720 		return 675000;
721 	else if (min_cdclk > 450000)
722 		return 540000;
723 	else if (min_cdclk > 337500)
724 		return 450000;
725 	else
726 		return 337500;
727 }
728 
729 static u8 bdw_calc_voltage_level(int cdclk)
730 {
731 	switch (cdclk) {
732 	default:
733 	case 337500:
734 		return 2;
735 	case 450000:
736 		return 0;
737 	case 540000:
738 		return 1;
739 	case 675000:
740 		return 3;
741 	}
742 }
743 
744 static void bdw_get_cdclk(struct drm_i915_private *dev_priv,
745 			  struct intel_cdclk_config *cdclk_config)
746 {
747 	u32 lcpll = intel_de_read(dev_priv, LCPLL_CTL);
748 	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
749 
750 	if (lcpll & LCPLL_CD_SOURCE_FCLK)
751 		cdclk_config->cdclk = 800000;
752 	else if (intel_de_read(dev_priv, FUSE_STRAP) & HSW_CDCLK_LIMIT)
753 		cdclk_config->cdclk = 450000;
754 	else if (freq == LCPLL_CLK_FREQ_450)
755 		cdclk_config->cdclk = 450000;
756 	else if (freq == LCPLL_CLK_FREQ_54O_BDW)
757 		cdclk_config->cdclk = 540000;
758 	else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
759 		cdclk_config->cdclk = 337500;
760 	else
761 		cdclk_config->cdclk = 675000;
762 
763 	/*
764 	 * Can't read this out :( Let's assume it's
765 	 * at least what the CDCLK frequency requires.
766 	 */
767 	cdclk_config->voltage_level =
768 		bdw_calc_voltage_level(cdclk_config->cdclk);
769 }
770 
771 static u32 bdw_cdclk_freq_sel(int cdclk)
772 {
773 	switch (cdclk) {
774 	default:
775 		MISSING_CASE(cdclk);
776 		fallthrough;
777 	case 337500:
778 		return LCPLL_CLK_FREQ_337_5_BDW;
779 	case 450000:
780 		return LCPLL_CLK_FREQ_450;
781 	case 540000:
782 		return LCPLL_CLK_FREQ_54O_BDW;
783 	case 675000:
784 		return LCPLL_CLK_FREQ_675_BDW;
785 	}
786 }
787 
788 static void bdw_set_cdclk(struct drm_i915_private *dev_priv,
789 			  const struct intel_cdclk_config *cdclk_config,
790 			  enum pipe pipe)
791 {
792 	int cdclk = cdclk_config->cdclk;
793 	int ret;
794 
795 	if (drm_WARN(&dev_priv->drm,
796 		     (intel_de_read(dev_priv, LCPLL_CTL) &
797 		      (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
798 		       LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
799 		       LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
800 		       LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
801 		     "trying to change cdclk frequency with cdclk not enabled\n"))
802 		return;
803 
804 	ret = snb_pcode_write(&dev_priv->uncore, BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
805 	if (ret) {
806 		drm_err(&dev_priv->drm,
807 			"failed to inform pcode about cdclk change\n");
808 		return;
809 	}
810 
811 	intel_de_rmw(dev_priv, LCPLL_CTL,
812 		     0, LCPLL_CD_SOURCE_FCLK);
813 
814 	/*
815 	 * According to the spec, it should be enough to poll for this 1 us.
816 	 * However, extensive testing shows that this can take longer.
817 	 */
818 	if (wait_for_us(intel_de_read(dev_priv, LCPLL_CTL) &
819 			LCPLL_CD_SOURCE_FCLK_DONE, 100))
820 		drm_err(&dev_priv->drm, "Switching to FCLK failed\n");
821 
822 	intel_de_rmw(dev_priv, LCPLL_CTL,
823 		     LCPLL_CLK_FREQ_MASK, bdw_cdclk_freq_sel(cdclk));
824 
825 	intel_de_rmw(dev_priv, LCPLL_CTL,
826 		     LCPLL_CD_SOURCE_FCLK, 0);
827 
828 	if (wait_for_us((intel_de_read(dev_priv, LCPLL_CTL) &
829 			 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
830 		drm_err(&dev_priv->drm, "Switching back to LCPLL failed\n");
831 
832 	snb_pcode_write(&dev_priv->uncore, HSW_PCODE_DE_WRITE_FREQ_REQ,
833 			cdclk_config->voltage_level);
834 
835 	intel_de_write(dev_priv, CDCLK_FREQ,
836 		       DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
837 
838 	intel_update_cdclk(dev_priv);
839 }
840 
841 static int skl_calc_cdclk(int min_cdclk, int vco)
842 {
843 	if (vco == 8640000) {
844 		if (min_cdclk > 540000)
845 			return 617143;
846 		else if (min_cdclk > 432000)
847 			return 540000;
848 		else if (min_cdclk > 308571)
849 			return 432000;
850 		else
851 			return 308571;
852 	} else {
853 		if (min_cdclk > 540000)
854 			return 675000;
855 		else if (min_cdclk > 450000)
856 			return 540000;
857 		else if (min_cdclk > 337500)
858 			return 450000;
859 		else
860 			return 337500;
861 	}
862 }
863 
864 static u8 skl_calc_voltage_level(int cdclk)
865 {
866 	if (cdclk > 540000)
867 		return 3;
868 	else if (cdclk > 450000)
869 		return 2;
870 	else if (cdclk > 337500)
871 		return 1;
872 	else
873 		return 0;
874 }
875 
876 static void skl_dpll0_update(struct drm_i915_private *dev_priv,
877 			     struct intel_cdclk_config *cdclk_config)
878 {
879 	u32 val;
880 
881 	cdclk_config->ref = 24000;
882 	cdclk_config->vco = 0;
883 
884 	val = intel_de_read(dev_priv, LCPLL1_CTL);
885 	if ((val & LCPLL_PLL_ENABLE) == 0)
886 		return;
887 
888 	if (drm_WARN_ON(&dev_priv->drm, (val & LCPLL_PLL_LOCK) == 0))
889 		return;
890 
891 	val = intel_de_read(dev_priv, DPLL_CTRL1);
892 
893 	if (drm_WARN_ON(&dev_priv->drm,
894 			(val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
895 				DPLL_CTRL1_SSC(SKL_DPLL0) |
896 				DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
897 			DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
898 		return;
899 
900 	switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
901 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
902 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
903 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
904 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
905 		cdclk_config->vco = 8100000;
906 		break;
907 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
908 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
909 		cdclk_config->vco = 8640000;
910 		break;
911 	default:
912 		MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
913 		break;
914 	}
915 }
916 
917 static void skl_get_cdclk(struct drm_i915_private *dev_priv,
918 			  struct intel_cdclk_config *cdclk_config)
919 {
920 	u32 cdctl;
921 
922 	skl_dpll0_update(dev_priv, cdclk_config);
923 
924 	cdclk_config->cdclk = cdclk_config->bypass = cdclk_config->ref;
925 
926 	if (cdclk_config->vco == 0)
927 		goto out;
928 
929 	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
930 
931 	if (cdclk_config->vco == 8640000) {
932 		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
933 		case CDCLK_FREQ_450_432:
934 			cdclk_config->cdclk = 432000;
935 			break;
936 		case CDCLK_FREQ_337_308:
937 			cdclk_config->cdclk = 308571;
938 			break;
939 		case CDCLK_FREQ_540:
940 			cdclk_config->cdclk = 540000;
941 			break;
942 		case CDCLK_FREQ_675_617:
943 			cdclk_config->cdclk = 617143;
944 			break;
945 		default:
946 			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
947 			break;
948 		}
949 	} else {
950 		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
951 		case CDCLK_FREQ_450_432:
952 			cdclk_config->cdclk = 450000;
953 			break;
954 		case CDCLK_FREQ_337_308:
955 			cdclk_config->cdclk = 337500;
956 			break;
957 		case CDCLK_FREQ_540:
958 			cdclk_config->cdclk = 540000;
959 			break;
960 		case CDCLK_FREQ_675_617:
961 			cdclk_config->cdclk = 675000;
962 			break;
963 		default:
964 			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
965 			break;
966 		}
967 	}
968 
969  out:
970 	/*
971 	 * Can't read this out :( Let's assume it's
972 	 * at least what the CDCLK frequency requires.
973 	 */
974 	cdclk_config->voltage_level =
975 		skl_calc_voltage_level(cdclk_config->cdclk);
976 }
977 
978 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
979 static int skl_cdclk_decimal(int cdclk)
980 {
981 	return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
982 }
983 
984 static void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv,
985 					int vco)
986 {
987 	bool changed = dev_priv->skl_preferred_vco_freq != vco;
988 
989 	dev_priv->skl_preferred_vco_freq = vco;
990 
991 	if (changed)
992 		intel_update_max_cdclk(dev_priv);
993 }
994 
995 static u32 skl_dpll0_link_rate(struct drm_i915_private *dev_priv, int vco)
996 {
997 	drm_WARN_ON(&dev_priv->drm, vco != 8100000 && vco != 8640000);
998 
999 	/*
1000 	 * We always enable DPLL0 with the lowest link rate possible, but still
1001 	 * taking into account the VCO required to operate the eDP panel at the
1002 	 * desired frequency. The usual DP link rates operate with a VCO of
1003 	 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
1004 	 * The modeset code is responsible for the selection of the exact link
1005 	 * rate later on, with the constraint of choosing a frequency that
1006 	 * works with vco.
1007 	 */
1008 	if (vco == 8640000)
1009 		return DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0);
1010 	else
1011 		return DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0);
1012 }
1013 
1014 static void skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
1015 {
1016 	intel_de_rmw(dev_priv, DPLL_CTRL1,
1017 		     DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
1018 		     DPLL_CTRL1_SSC(SKL_DPLL0) |
1019 		     DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0),
1020 		     DPLL_CTRL1_OVERRIDE(SKL_DPLL0) |
1021 		     skl_dpll0_link_rate(dev_priv, vco));
1022 	intel_de_posting_read(dev_priv, DPLL_CTRL1);
1023 
1024 	intel_de_rmw(dev_priv, LCPLL1_CTL,
1025 		     0, LCPLL_PLL_ENABLE);
1026 
1027 	if (intel_de_wait_for_set(dev_priv, LCPLL1_CTL, LCPLL_PLL_LOCK, 5))
1028 		drm_err(&dev_priv->drm, "DPLL0 not locked\n");
1029 
1030 	dev_priv->display.cdclk.hw.vco = vco;
1031 
1032 	/* We'll want to keep using the current vco from now on. */
1033 	skl_set_preferred_cdclk_vco(dev_priv, vco);
1034 }
1035 
1036 static void skl_dpll0_disable(struct drm_i915_private *dev_priv)
1037 {
1038 	intel_de_rmw(dev_priv, LCPLL1_CTL,
1039 		     LCPLL_PLL_ENABLE, 0);
1040 
1041 	if (intel_de_wait_for_clear(dev_priv, LCPLL1_CTL, LCPLL_PLL_LOCK, 1))
1042 		drm_err(&dev_priv->drm, "Couldn't disable DPLL0\n");
1043 
1044 	dev_priv->display.cdclk.hw.vco = 0;
1045 }
1046 
1047 static u32 skl_cdclk_freq_sel(struct drm_i915_private *dev_priv,
1048 			      int cdclk, int vco)
1049 {
1050 	switch (cdclk) {
1051 	default:
1052 		drm_WARN_ON(&dev_priv->drm,
1053 			    cdclk != dev_priv->display.cdclk.hw.bypass);
1054 		drm_WARN_ON(&dev_priv->drm, vco != 0);
1055 		fallthrough;
1056 	case 308571:
1057 	case 337500:
1058 		return CDCLK_FREQ_337_308;
1059 	case 450000:
1060 	case 432000:
1061 		return CDCLK_FREQ_450_432;
1062 	case 540000:
1063 		return CDCLK_FREQ_540;
1064 	case 617143:
1065 	case 675000:
1066 		return CDCLK_FREQ_675_617;
1067 	}
1068 }
1069 
1070 static void skl_set_cdclk(struct drm_i915_private *dev_priv,
1071 			  const struct intel_cdclk_config *cdclk_config,
1072 			  enum pipe pipe)
1073 {
1074 	int cdclk = cdclk_config->cdclk;
1075 	int vco = cdclk_config->vco;
1076 	u32 freq_select, cdclk_ctl;
1077 	int ret;
1078 
1079 	/*
1080 	 * Based on WA#1183 CDCLK rates 308 and 617MHz CDCLK rates are
1081 	 * unsupported on SKL. In theory this should never happen since only
1082 	 * the eDP1.4 2.16 and 4.32Gbps rates require it, but eDP1.4 is not
1083 	 * supported on SKL either, see the above WA. WARN whenever trying to
1084 	 * use the corresponding VCO freq as that always leads to using the
1085 	 * minimum 308MHz CDCLK.
1086 	 */
1087 	drm_WARN_ON_ONCE(&dev_priv->drm,
1088 			 IS_SKYLAKE(dev_priv) && vco == 8640000);
1089 
1090 	ret = skl_pcode_request(&dev_priv->uncore, SKL_PCODE_CDCLK_CONTROL,
1091 				SKL_CDCLK_PREPARE_FOR_CHANGE,
1092 				SKL_CDCLK_READY_FOR_CHANGE,
1093 				SKL_CDCLK_READY_FOR_CHANGE, 3);
1094 	if (ret) {
1095 		drm_err(&dev_priv->drm,
1096 			"Failed to inform PCU about cdclk change (%d)\n", ret);
1097 		return;
1098 	}
1099 
1100 	freq_select = skl_cdclk_freq_sel(dev_priv, cdclk, vco);
1101 
1102 	if (dev_priv->display.cdclk.hw.vco != 0 &&
1103 	    dev_priv->display.cdclk.hw.vco != vco)
1104 		skl_dpll0_disable(dev_priv);
1105 
1106 	cdclk_ctl = intel_de_read(dev_priv, CDCLK_CTL);
1107 
1108 	if (dev_priv->display.cdclk.hw.vco != vco) {
1109 		/* Wa Display #1183: skl,kbl,cfl */
1110 		cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
1111 		cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
1112 		intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1113 	}
1114 
1115 	/* Wa Display #1183: skl,kbl,cfl */
1116 	cdclk_ctl |= CDCLK_DIVMUX_CD_OVERRIDE;
1117 	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1118 	intel_de_posting_read(dev_priv, CDCLK_CTL);
1119 
1120 	if (dev_priv->display.cdclk.hw.vco != vco)
1121 		skl_dpll0_enable(dev_priv, vco);
1122 
1123 	/* Wa Display #1183: skl,kbl,cfl */
1124 	cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
1125 	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1126 
1127 	cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
1128 	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1129 
1130 	/* Wa Display #1183: skl,kbl,cfl */
1131 	cdclk_ctl &= ~CDCLK_DIVMUX_CD_OVERRIDE;
1132 	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1133 	intel_de_posting_read(dev_priv, CDCLK_CTL);
1134 
1135 	/* inform PCU of the change */
1136 	snb_pcode_write(&dev_priv->uncore, SKL_PCODE_CDCLK_CONTROL,
1137 			cdclk_config->voltage_level);
1138 
1139 	intel_update_cdclk(dev_priv);
1140 }
1141 
1142 static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
1143 {
1144 	u32 cdctl, expected;
1145 
1146 	/*
1147 	 * check if the pre-os initialized the display
1148 	 * There is SWF18 scratchpad register defined which is set by the
1149 	 * pre-os which can be used by the OS drivers to check the status
1150 	 */
1151 	if ((intel_de_read(dev_priv, SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
1152 		goto sanitize;
1153 
1154 	intel_update_cdclk(dev_priv);
1155 	intel_cdclk_dump_config(dev_priv, &dev_priv->display.cdclk.hw, "Current CDCLK");
1156 
1157 	/* Is PLL enabled and locked ? */
1158 	if (dev_priv->display.cdclk.hw.vco == 0 ||
1159 	    dev_priv->display.cdclk.hw.cdclk == dev_priv->display.cdclk.hw.bypass)
1160 		goto sanitize;
1161 
1162 	/* DPLL okay; verify the cdclock
1163 	 *
1164 	 * Noticed in some instances that the freq selection is correct but
1165 	 * decimal part is programmed wrong from BIOS where pre-os does not
1166 	 * enable display. Verify the same as well.
1167 	 */
1168 	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
1169 	expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
1170 		skl_cdclk_decimal(dev_priv->display.cdclk.hw.cdclk);
1171 	if (cdctl == expected)
1172 		/* All well; nothing to sanitize */
1173 		return;
1174 
1175 sanitize:
1176 	drm_dbg_kms(&dev_priv->drm, "Sanitizing cdclk programmed by pre-os\n");
1177 
1178 	/* force cdclk programming */
1179 	dev_priv->display.cdclk.hw.cdclk = 0;
1180 	/* force full PLL disable + enable */
1181 	dev_priv->display.cdclk.hw.vco = -1;
1182 }
1183 
1184 static void skl_cdclk_init_hw(struct drm_i915_private *dev_priv)
1185 {
1186 	struct intel_cdclk_config cdclk_config;
1187 
1188 	skl_sanitize_cdclk(dev_priv);
1189 
1190 	if (dev_priv->display.cdclk.hw.cdclk != 0 &&
1191 	    dev_priv->display.cdclk.hw.vco != 0) {
1192 		/*
1193 		 * Use the current vco as our initial
1194 		 * guess as to what the preferred vco is.
1195 		 */
1196 		if (dev_priv->skl_preferred_vco_freq == 0)
1197 			skl_set_preferred_cdclk_vco(dev_priv,
1198 						    dev_priv->display.cdclk.hw.vco);
1199 		return;
1200 	}
1201 
1202 	cdclk_config = dev_priv->display.cdclk.hw;
1203 
1204 	cdclk_config.vco = dev_priv->skl_preferred_vco_freq;
1205 	if (cdclk_config.vco == 0)
1206 		cdclk_config.vco = 8100000;
1207 	cdclk_config.cdclk = skl_calc_cdclk(0, cdclk_config.vco);
1208 	cdclk_config.voltage_level = skl_calc_voltage_level(cdclk_config.cdclk);
1209 
1210 	skl_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1211 }
1212 
1213 static void skl_cdclk_uninit_hw(struct drm_i915_private *dev_priv)
1214 {
1215 	struct intel_cdclk_config cdclk_config = dev_priv->display.cdclk.hw;
1216 
1217 	cdclk_config.cdclk = cdclk_config.bypass;
1218 	cdclk_config.vco = 0;
1219 	cdclk_config.voltage_level = skl_calc_voltage_level(cdclk_config.cdclk);
1220 
1221 	skl_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1222 }
1223 
1224 struct intel_cdclk_vals {
1225 	u32 cdclk;
1226 	u16 refclk;
1227 	u16 waveform;
1228 	u8 divider;	/* CD2X divider * 2 */
1229 	u8 ratio;
1230 };
1231 
1232 static const struct intel_cdclk_vals bxt_cdclk_table[] = {
1233 	{ .refclk = 19200, .cdclk = 144000, .divider = 8, .ratio = 60 },
1234 	{ .refclk = 19200, .cdclk = 288000, .divider = 4, .ratio = 60 },
1235 	{ .refclk = 19200, .cdclk = 384000, .divider = 3, .ratio = 60 },
1236 	{ .refclk = 19200, .cdclk = 576000, .divider = 2, .ratio = 60 },
1237 	{ .refclk = 19200, .cdclk = 624000, .divider = 2, .ratio = 65 },
1238 	{}
1239 };
1240 
1241 static const struct intel_cdclk_vals glk_cdclk_table[] = {
1242 	{ .refclk = 19200, .cdclk =  79200, .divider = 8, .ratio = 33 },
1243 	{ .refclk = 19200, .cdclk = 158400, .divider = 4, .ratio = 33 },
1244 	{ .refclk = 19200, .cdclk = 316800, .divider = 2, .ratio = 33 },
1245 	{}
1246 };
1247 
1248 static const struct intel_cdclk_vals icl_cdclk_table[] = {
1249 	{ .refclk = 19200, .cdclk = 172800, .divider = 2, .ratio = 18 },
1250 	{ .refclk = 19200, .cdclk = 192000, .divider = 2, .ratio = 20 },
1251 	{ .refclk = 19200, .cdclk = 307200, .divider = 2, .ratio = 32 },
1252 	{ .refclk = 19200, .cdclk = 326400, .divider = 4, .ratio = 68 },
1253 	{ .refclk = 19200, .cdclk = 556800, .divider = 2, .ratio = 58 },
1254 	{ .refclk = 19200, .cdclk = 652800, .divider = 2, .ratio = 68 },
1255 
1256 	{ .refclk = 24000, .cdclk = 180000, .divider = 2, .ratio = 15 },
1257 	{ .refclk = 24000, .cdclk = 192000, .divider = 2, .ratio = 16 },
1258 	{ .refclk = 24000, .cdclk = 312000, .divider = 2, .ratio = 26 },
1259 	{ .refclk = 24000, .cdclk = 324000, .divider = 4, .ratio = 54 },
1260 	{ .refclk = 24000, .cdclk = 552000, .divider = 2, .ratio = 46 },
1261 	{ .refclk = 24000, .cdclk = 648000, .divider = 2, .ratio = 54 },
1262 
1263 	{ .refclk = 38400, .cdclk = 172800, .divider = 2, .ratio =  9 },
1264 	{ .refclk = 38400, .cdclk = 192000, .divider = 2, .ratio = 10 },
1265 	{ .refclk = 38400, .cdclk = 307200, .divider = 2, .ratio = 16 },
1266 	{ .refclk = 38400, .cdclk = 326400, .divider = 4, .ratio = 34 },
1267 	{ .refclk = 38400, .cdclk = 556800, .divider = 2, .ratio = 29 },
1268 	{ .refclk = 38400, .cdclk = 652800, .divider = 2, .ratio = 34 },
1269 	{}
1270 };
1271 
1272 static const struct intel_cdclk_vals rkl_cdclk_table[] = {
1273 	{ .refclk = 19200, .cdclk = 172800, .divider = 4, .ratio =  36 },
1274 	{ .refclk = 19200, .cdclk = 192000, .divider = 4, .ratio =  40 },
1275 	{ .refclk = 19200, .cdclk = 307200, .divider = 4, .ratio =  64 },
1276 	{ .refclk = 19200, .cdclk = 326400, .divider = 8, .ratio = 136 },
1277 	{ .refclk = 19200, .cdclk = 556800, .divider = 4, .ratio = 116 },
1278 	{ .refclk = 19200, .cdclk = 652800, .divider = 4, .ratio = 136 },
1279 
1280 	{ .refclk = 24000, .cdclk = 180000, .divider = 4, .ratio =  30 },
1281 	{ .refclk = 24000, .cdclk = 192000, .divider = 4, .ratio =  32 },
1282 	{ .refclk = 24000, .cdclk = 312000, .divider = 4, .ratio =  52 },
1283 	{ .refclk = 24000, .cdclk = 324000, .divider = 8, .ratio = 108 },
1284 	{ .refclk = 24000, .cdclk = 552000, .divider = 4, .ratio =  92 },
1285 	{ .refclk = 24000, .cdclk = 648000, .divider = 4, .ratio = 108 },
1286 
1287 	{ .refclk = 38400, .cdclk = 172800, .divider = 4, .ratio = 18 },
1288 	{ .refclk = 38400, .cdclk = 192000, .divider = 4, .ratio = 20 },
1289 	{ .refclk = 38400, .cdclk = 307200, .divider = 4, .ratio = 32 },
1290 	{ .refclk = 38400, .cdclk = 326400, .divider = 8, .ratio = 68 },
1291 	{ .refclk = 38400, .cdclk = 556800, .divider = 4, .ratio = 58 },
1292 	{ .refclk = 38400, .cdclk = 652800, .divider = 4, .ratio = 68 },
1293 	{}
1294 };
1295 
1296 static const struct intel_cdclk_vals adlp_a_step_cdclk_table[] = {
1297 	{ .refclk = 19200, .cdclk = 307200, .divider = 2, .ratio = 32 },
1298 	{ .refclk = 19200, .cdclk = 556800, .divider = 2, .ratio = 58 },
1299 	{ .refclk = 19200, .cdclk = 652800, .divider = 2, .ratio = 68 },
1300 
1301 	{ .refclk = 24000, .cdclk = 312000, .divider = 2, .ratio = 26 },
1302 	{ .refclk = 24000, .cdclk = 552000, .divider = 2, .ratio = 46 },
1303 	{ .refclk = 24400, .cdclk = 648000, .divider = 2, .ratio = 54 },
1304 
1305 	{ .refclk = 38400, .cdclk = 307200, .divider = 2, .ratio = 16 },
1306 	{ .refclk = 38400, .cdclk = 556800, .divider = 2, .ratio = 29 },
1307 	{ .refclk = 38400, .cdclk = 652800, .divider = 2, .ratio = 34 },
1308 	{}
1309 };
1310 
1311 static const struct intel_cdclk_vals adlp_cdclk_table[] = {
1312 	{ .refclk = 19200, .cdclk = 172800, .divider = 3, .ratio = 27 },
1313 	{ .refclk = 19200, .cdclk = 192000, .divider = 2, .ratio = 20 },
1314 	{ .refclk = 19200, .cdclk = 307200, .divider = 2, .ratio = 32 },
1315 	{ .refclk = 19200, .cdclk = 556800, .divider = 2, .ratio = 58 },
1316 	{ .refclk = 19200, .cdclk = 652800, .divider = 2, .ratio = 68 },
1317 
1318 	{ .refclk = 24000, .cdclk = 176000, .divider = 3, .ratio = 22 },
1319 	{ .refclk = 24000, .cdclk = 192000, .divider = 2, .ratio = 16 },
1320 	{ .refclk = 24000, .cdclk = 312000, .divider = 2, .ratio = 26 },
1321 	{ .refclk = 24000, .cdclk = 552000, .divider = 2, .ratio = 46 },
1322 	{ .refclk = 24000, .cdclk = 648000, .divider = 2, .ratio = 54 },
1323 
1324 	{ .refclk = 38400, .cdclk = 179200, .divider = 3, .ratio = 14 },
1325 	{ .refclk = 38400, .cdclk = 192000, .divider = 2, .ratio = 10 },
1326 	{ .refclk = 38400, .cdclk = 307200, .divider = 2, .ratio = 16 },
1327 	{ .refclk = 38400, .cdclk = 556800, .divider = 2, .ratio = 29 },
1328 	{ .refclk = 38400, .cdclk = 652800, .divider = 2, .ratio = 34 },
1329 	{}
1330 };
1331 
1332 static const struct intel_cdclk_vals rplu_cdclk_table[] = {
1333 	{ .refclk = 19200, .cdclk = 172800, .divider = 3, .ratio = 27 },
1334 	{ .refclk = 19200, .cdclk = 192000, .divider = 2, .ratio = 20 },
1335 	{ .refclk = 19200, .cdclk = 307200, .divider = 2, .ratio = 32 },
1336 	{ .refclk = 19200, .cdclk = 480000, .divider = 2, .ratio = 50 },
1337 	{ .refclk = 19200, .cdclk = 556800, .divider = 2, .ratio = 58 },
1338 	{ .refclk = 19200, .cdclk = 652800, .divider = 2, .ratio = 68 },
1339 
1340 	{ .refclk = 24000, .cdclk = 176000, .divider = 3, .ratio = 22 },
1341 	{ .refclk = 24000, .cdclk = 192000, .divider = 2, .ratio = 16 },
1342 	{ .refclk = 24000, .cdclk = 312000, .divider = 2, .ratio = 26 },
1343 	{ .refclk = 24000, .cdclk = 480000, .divider = 2, .ratio = 40 },
1344 	{ .refclk = 24000, .cdclk = 552000, .divider = 2, .ratio = 46 },
1345 	{ .refclk = 24000, .cdclk = 648000, .divider = 2, .ratio = 54 },
1346 
1347 	{ .refclk = 38400, .cdclk = 179200, .divider = 3, .ratio = 14 },
1348 	{ .refclk = 38400, .cdclk = 192000, .divider = 2, .ratio = 10 },
1349 	{ .refclk = 38400, .cdclk = 307200, .divider = 2, .ratio = 16 },
1350 	{ .refclk = 38400, .cdclk = 480000, .divider = 2, .ratio = 25 },
1351 	{ .refclk = 38400, .cdclk = 556800, .divider = 2, .ratio = 29 },
1352 	{ .refclk = 38400, .cdclk = 652800, .divider = 2, .ratio = 34 },
1353 	{}
1354 };
1355 
1356 static const struct intel_cdclk_vals dg2_cdclk_table[] = {
1357 	{ .refclk = 38400, .cdclk = 163200, .divider = 2, .ratio = 34, .waveform = 0x8888 },
1358 	{ .refclk = 38400, .cdclk = 204000, .divider = 2, .ratio = 34, .waveform = 0x9248 },
1359 	{ .refclk = 38400, .cdclk = 244800, .divider = 2, .ratio = 34, .waveform = 0xa4a4 },
1360 	{ .refclk = 38400, .cdclk = 285600, .divider = 2, .ratio = 34, .waveform = 0xa54a },
1361 	{ .refclk = 38400, .cdclk = 326400, .divider = 2, .ratio = 34, .waveform = 0xaaaa },
1362 	{ .refclk = 38400, .cdclk = 367200, .divider = 2, .ratio = 34, .waveform = 0xad5a },
1363 	{ .refclk = 38400, .cdclk = 408000, .divider = 2, .ratio = 34, .waveform = 0xb6b6 },
1364 	{ .refclk = 38400, .cdclk = 448800, .divider = 2, .ratio = 34, .waveform = 0xdbb6 },
1365 	{ .refclk = 38400, .cdclk = 489600, .divider = 2, .ratio = 34, .waveform = 0xeeee },
1366 	{ .refclk = 38400, .cdclk = 530400, .divider = 2, .ratio = 34, .waveform = 0xf7de },
1367 	{ .refclk = 38400, .cdclk = 571200, .divider = 2, .ratio = 34, .waveform = 0xfefe },
1368 	{ .refclk = 38400, .cdclk = 612000, .divider = 2, .ratio = 34, .waveform = 0xfffe },
1369 	{ .refclk = 38400, .cdclk = 652800, .divider = 2, .ratio = 34, .waveform = 0xffff },
1370 	{}
1371 };
1372 
1373 static const struct intel_cdclk_vals mtl_cdclk_table[] = {
1374 	{ .refclk = 38400, .cdclk = 172800, .divider = 2, .ratio = 16, .waveform = 0xad5a },
1375 	{ .refclk = 38400, .cdclk = 192000, .divider = 2, .ratio = 16, .waveform = 0xb6b6 },
1376 	{ .refclk = 38400, .cdclk = 307200, .divider = 2, .ratio = 16, .waveform = 0x0000 },
1377 	{ .refclk = 38400, .cdclk = 480000, .divider = 2, .ratio = 25, .waveform = 0x0000 },
1378 	{ .refclk = 38400, .cdclk = 556800, .divider = 2, .ratio = 29, .waveform = 0x0000 },
1379 	{ .refclk = 38400, .cdclk = 652800, .divider = 2, .ratio = 34, .waveform = 0x0000 },
1380 	{}
1381 };
1382 
1383 static int bxt_calc_cdclk(struct drm_i915_private *dev_priv, int min_cdclk)
1384 {
1385 	const struct intel_cdclk_vals *table = dev_priv->display.cdclk.table;
1386 	int i;
1387 
1388 	for (i = 0; table[i].refclk; i++)
1389 		if (table[i].refclk == dev_priv->display.cdclk.hw.ref &&
1390 		    table[i].cdclk >= min_cdclk)
1391 			return table[i].cdclk;
1392 
1393 	drm_WARN(&dev_priv->drm, 1,
1394 		 "Cannot satisfy minimum cdclk %d with refclk %u\n",
1395 		 min_cdclk, dev_priv->display.cdclk.hw.ref);
1396 	return 0;
1397 }
1398 
1399 static int bxt_calc_cdclk_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
1400 {
1401 	const struct intel_cdclk_vals *table = dev_priv->display.cdclk.table;
1402 	int i;
1403 
1404 	if (cdclk == dev_priv->display.cdclk.hw.bypass)
1405 		return 0;
1406 
1407 	for (i = 0; table[i].refclk; i++)
1408 		if (table[i].refclk == dev_priv->display.cdclk.hw.ref &&
1409 		    table[i].cdclk == cdclk)
1410 			return dev_priv->display.cdclk.hw.ref * table[i].ratio;
1411 
1412 	drm_WARN(&dev_priv->drm, 1, "cdclk %d not valid for refclk %u\n",
1413 		 cdclk, dev_priv->display.cdclk.hw.ref);
1414 	return 0;
1415 }
1416 
1417 static u8 bxt_calc_voltage_level(int cdclk)
1418 {
1419 	return DIV_ROUND_UP(cdclk, 25000);
1420 }
1421 
1422 static u8 icl_calc_voltage_level(int cdclk)
1423 {
1424 	if (cdclk > 556800)
1425 		return 2;
1426 	else if (cdclk > 312000)
1427 		return 1;
1428 	else
1429 		return 0;
1430 }
1431 
1432 static u8 ehl_calc_voltage_level(int cdclk)
1433 {
1434 	if (cdclk > 326400)
1435 		return 3;
1436 	else if (cdclk > 312000)
1437 		return 2;
1438 	else if (cdclk > 180000)
1439 		return 1;
1440 	else
1441 		return 0;
1442 }
1443 
1444 static u8 tgl_calc_voltage_level(int cdclk)
1445 {
1446 	if (cdclk > 556800)
1447 		return 3;
1448 	else if (cdclk > 326400)
1449 		return 2;
1450 	else if (cdclk > 312000)
1451 		return 1;
1452 	else
1453 		return 0;
1454 }
1455 
1456 static u8 rplu_calc_voltage_level(int cdclk)
1457 {
1458 	if (cdclk > 556800)
1459 		return 3;
1460 	else if (cdclk > 480000)
1461 		return 2;
1462 	else if (cdclk > 312000)
1463 		return 1;
1464 	else
1465 		return 0;
1466 }
1467 
1468 static void icl_readout_refclk(struct drm_i915_private *dev_priv,
1469 			       struct intel_cdclk_config *cdclk_config)
1470 {
1471 	u32 dssm = intel_de_read(dev_priv, SKL_DSSM) & ICL_DSSM_CDCLK_PLL_REFCLK_MASK;
1472 
1473 	switch (dssm) {
1474 	default:
1475 		MISSING_CASE(dssm);
1476 		fallthrough;
1477 	case ICL_DSSM_CDCLK_PLL_REFCLK_24MHz:
1478 		cdclk_config->ref = 24000;
1479 		break;
1480 	case ICL_DSSM_CDCLK_PLL_REFCLK_19_2MHz:
1481 		cdclk_config->ref = 19200;
1482 		break;
1483 	case ICL_DSSM_CDCLK_PLL_REFCLK_38_4MHz:
1484 		cdclk_config->ref = 38400;
1485 		break;
1486 	}
1487 }
1488 
1489 static void bxt_de_pll_readout(struct drm_i915_private *dev_priv,
1490 			       struct intel_cdclk_config *cdclk_config)
1491 {
1492 	u32 val, ratio;
1493 
1494 	if (IS_DG2(dev_priv))
1495 		cdclk_config->ref = 38400;
1496 	else if (DISPLAY_VER(dev_priv) >= 11)
1497 		icl_readout_refclk(dev_priv, cdclk_config);
1498 	else
1499 		cdclk_config->ref = 19200;
1500 
1501 	val = intel_de_read(dev_priv, BXT_DE_PLL_ENABLE);
1502 	if ((val & BXT_DE_PLL_PLL_ENABLE) == 0 ||
1503 	    (val & BXT_DE_PLL_LOCK) == 0) {
1504 		/*
1505 		 * CDCLK PLL is disabled, the VCO/ratio doesn't matter, but
1506 		 * setting it to zero is a way to signal that.
1507 		 */
1508 		cdclk_config->vco = 0;
1509 		return;
1510 	}
1511 
1512 	/*
1513 	 * DISPLAY_VER >= 11 have the ratio directly in the PLL enable register,
1514 	 * gen9lp had it in a separate PLL control register.
1515 	 */
1516 	if (DISPLAY_VER(dev_priv) >= 11)
1517 		ratio = val & ICL_CDCLK_PLL_RATIO_MASK;
1518 	else
1519 		ratio = intel_de_read(dev_priv, BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
1520 
1521 	cdclk_config->vco = ratio * cdclk_config->ref;
1522 }
1523 
1524 static void bxt_get_cdclk(struct drm_i915_private *dev_priv,
1525 			  struct intel_cdclk_config *cdclk_config)
1526 {
1527 	u32 squash_ctl = 0;
1528 	u32 divider;
1529 	int div;
1530 
1531 	bxt_de_pll_readout(dev_priv, cdclk_config);
1532 
1533 	if (DISPLAY_VER(dev_priv) >= 12)
1534 		cdclk_config->bypass = cdclk_config->ref / 2;
1535 	else if (DISPLAY_VER(dev_priv) >= 11)
1536 		cdclk_config->bypass = 50000;
1537 	else
1538 		cdclk_config->bypass = cdclk_config->ref;
1539 
1540 	if (cdclk_config->vco == 0) {
1541 		cdclk_config->cdclk = cdclk_config->bypass;
1542 		goto out;
1543 	}
1544 
1545 	divider = intel_de_read(dev_priv, CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
1546 
1547 	switch (divider) {
1548 	case BXT_CDCLK_CD2X_DIV_SEL_1:
1549 		div = 2;
1550 		break;
1551 	case BXT_CDCLK_CD2X_DIV_SEL_1_5:
1552 		div = 3;
1553 		break;
1554 	case BXT_CDCLK_CD2X_DIV_SEL_2:
1555 		div = 4;
1556 		break;
1557 	case BXT_CDCLK_CD2X_DIV_SEL_4:
1558 		div = 8;
1559 		break;
1560 	default:
1561 		MISSING_CASE(divider);
1562 		return;
1563 	}
1564 
1565 	if (HAS_CDCLK_SQUASH(dev_priv))
1566 		squash_ctl = intel_de_read(dev_priv, CDCLK_SQUASH_CTL);
1567 
1568 	if (squash_ctl & CDCLK_SQUASH_ENABLE) {
1569 		u16 waveform;
1570 		int size;
1571 
1572 		size = REG_FIELD_GET(CDCLK_SQUASH_WINDOW_SIZE_MASK, squash_ctl) + 1;
1573 		waveform = REG_FIELD_GET(CDCLK_SQUASH_WAVEFORM_MASK, squash_ctl) >> (16 - size);
1574 
1575 		cdclk_config->cdclk = DIV_ROUND_CLOSEST(hweight16(waveform) *
1576 							cdclk_config->vco, size * div);
1577 	} else {
1578 		cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco, div);
1579 	}
1580 
1581  out:
1582 	/*
1583 	 * Can't read this out :( Let's assume it's
1584 	 * at least what the CDCLK frequency requires.
1585 	 */
1586 	cdclk_config->voltage_level =
1587 		intel_cdclk_calc_voltage_level(dev_priv, cdclk_config->cdclk);
1588 }
1589 
1590 static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
1591 {
1592 	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, 0);
1593 
1594 	/* Timeout 200us */
1595 	if (intel_de_wait_for_clear(dev_priv,
1596 				    BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1597 		drm_err(&dev_priv->drm, "timeout waiting for DE PLL unlock\n");
1598 
1599 	dev_priv->display.cdclk.hw.vco = 0;
1600 }
1601 
1602 static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
1603 {
1604 	int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->display.cdclk.hw.ref);
1605 
1606 	intel_de_rmw(dev_priv, BXT_DE_PLL_CTL,
1607 		     BXT_DE_PLL_RATIO_MASK, BXT_DE_PLL_RATIO(ratio));
1608 
1609 	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
1610 
1611 	/* Timeout 200us */
1612 	if (intel_de_wait_for_set(dev_priv,
1613 				  BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1614 		drm_err(&dev_priv->drm, "timeout waiting for DE PLL lock\n");
1615 
1616 	dev_priv->display.cdclk.hw.vco = vco;
1617 }
1618 
1619 static void icl_cdclk_pll_disable(struct drm_i915_private *dev_priv)
1620 {
1621 	intel_de_rmw(dev_priv, BXT_DE_PLL_ENABLE,
1622 		     BXT_DE_PLL_PLL_ENABLE, 0);
1623 
1624 	/* Timeout 200us */
1625 	if (intel_de_wait_for_clear(dev_priv, BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1626 		drm_err(&dev_priv->drm, "timeout waiting for CDCLK PLL unlock\n");
1627 
1628 	dev_priv->display.cdclk.hw.vco = 0;
1629 }
1630 
1631 static void icl_cdclk_pll_enable(struct drm_i915_private *dev_priv, int vco)
1632 {
1633 	int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->display.cdclk.hw.ref);
1634 	u32 val;
1635 
1636 	val = ICL_CDCLK_PLL_RATIO(ratio);
1637 	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1638 
1639 	val |= BXT_DE_PLL_PLL_ENABLE;
1640 	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1641 
1642 	/* Timeout 200us */
1643 	if (intel_de_wait_for_set(dev_priv, BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1644 		drm_err(&dev_priv->drm, "timeout waiting for CDCLK PLL lock\n");
1645 
1646 	dev_priv->display.cdclk.hw.vco = vco;
1647 }
1648 
1649 static void adlp_cdclk_pll_crawl(struct drm_i915_private *dev_priv, int vco)
1650 {
1651 	int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->display.cdclk.hw.ref);
1652 	u32 val;
1653 
1654 	/* Write PLL ratio without disabling */
1655 	val = ICL_CDCLK_PLL_RATIO(ratio) | BXT_DE_PLL_PLL_ENABLE;
1656 	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1657 
1658 	/* Submit freq change request */
1659 	val |= BXT_DE_PLL_FREQ_REQ;
1660 	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1661 
1662 	/* Timeout 200us */
1663 	if (intel_de_wait_for_set(dev_priv, BXT_DE_PLL_ENABLE,
1664 				  BXT_DE_PLL_LOCK | BXT_DE_PLL_FREQ_REQ_ACK, 1))
1665 		drm_err(&dev_priv->drm, "timeout waiting for FREQ change request ack\n");
1666 
1667 	val &= ~BXT_DE_PLL_FREQ_REQ;
1668 	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1669 
1670 	dev_priv->display.cdclk.hw.vco = vco;
1671 }
1672 
1673 static u32 bxt_cdclk_cd2x_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
1674 {
1675 	if (DISPLAY_VER(dev_priv) >= 12) {
1676 		if (pipe == INVALID_PIPE)
1677 			return TGL_CDCLK_CD2X_PIPE_NONE;
1678 		else
1679 			return TGL_CDCLK_CD2X_PIPE(pipe);
1680 	} else if (DISPLAY_VER(dev_priv) >= 11) {
1681 		if (pipe == INVALID_PIPE)
1682 			return ICL_CDCLK_CD2X_PIPE_NONE;
1683 		else
1684 			return ICL_CDCLK_CD2X_PIPE(pipe);
1685 	} else {
1686 		if (pipe == INVALID_PIPE)
1687 			return BXT_CDCLK_CD2X_PIPE_NONE;
1688 		else
1689 			return BXT_CDCLK_CD2X_PIPE(pipe);
1690 	}
1691 }
1692 
1693 static u32 bxt_cdclk_cd2x_div_sel(struct drm_i915_private *dev_priv,
1694 				  int cdclk, int vco)
1695 {
1696 	/* cdclk = vco / 2 / div{1,1.5,2,4} */
1697 	switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
1698 	default:
1699 		drm_WARN_ON(&dev_priv->drm,
1700 			    cdclk != dev_priv->display.cdclk.hw.bypass);
1701 		drm_WARN_ON(&dev_priv->drm, vco != 0);
1702 		fallthrough;
1703 	case 2:
1704 		return BXT_CDCLK_CD2X_DIV_SEL_1;
1705 	case 3:
1706 		return BXT_CDCLK_CD2X_DIV_SEL_1_5;
1707 	case 4:
1708 		return BXT_CDCLK_CD2X_DIV_SEL_2;
1709 	case 8:
1710 		return BXT_CDCLK_CD2X_DIV_SEL_4;
1711 	}
1712 }
1713 
1714 static u32 cdclk_squash_waveform(struct drm_i915_private *dev_priv,
1715 				 int cdclk)
1716 {
1717 	const struct intel_cdclk_vals *table = dev_priv->display.cdclk.table;
1718 	int i;
1719 
1720 	if (cdclk == dev_priv->display.cdclk.hw.bypass)
1721 		return 0;
1722 
1723 	for (i = 0; table[i].refclk; i++)
1724 		if (table[i].refclk == dev_priv->display.cdclk.hw.ref &&
1725 		    table[i].cdclk == cdclk)
1726 			return table[i].waveform;
1727 
1728 	drm_WARN(&dev_priv->drm, 1, "cdclk %d not valid for refclk %u\n",
1729 		 cdclk, dev_priv->display.cdclk.hw.ref);
1730 
1731 	return 0xffff;
1732 }
1733 
1734 static void icl_cdclk_pll_update(struct drm_i915_private *i915, int vco)
1735 {
1736 	if (i915->display.cdclk.hw.vco != 0 &&
1737 	    i915->display.cdclk.hw.vco != vco)
1738 		icl_cdclk_pll_disable(i915);
1739 
1740 	if (i915->display.cdclk.hw.vco != vco)
1741 		icl_cdclk_pll_enable(i915, vco);
1742 }
1743 
1744 static void bxt_cdclk_pll_update(struct drm_i915_private *i915, int vco)
1745 {
1746 	if (i915->display.cdclk.hw.vco != 0 &&
1747 	    i915->display.cdclk.hw.vco != vco)
1748 		bxt_de_pll_disable(i915);
1749 
1750 	if (i915->display.cdclk.hw.vco != vco)
1751 		bxt_de_pll_enable(i915, vco);
1752 }
1753 
1754 static void dg2_cdclk_squash_program(struct drm_i915_private *i915,
1755 				     u16 waveform)
1756 {
1757 	u32 squash_ctl = 0;
1758 
1759 	if (waveform)
1760 		squash_ctl = CDCLK_SQUASH_ENABLE |
1761 			     CDCLK_SQUASH_WINDOW_SIZE(0xf) | waveform;
1762 
1763 	intel_de_write(i915, CDCLK_SQUASH_CTL, squash_ctl);
1764 }
1765 
1766 static bool cdclk_pll_is_unknown(unsigned int vco)
1767 {
1768 	/*
1769 	 * Ensure driver does not take the crawl path for the
1770 	 * case when the vco is set to ~0 in the
1771 	 * sanitize path.
1772 	 */
1773 	return vco == ~0;
1774 }
1775 
1776 static int cdclk_squash_divider(u16 waveform)
1777 {
1778 	return hweight16(waveform ?: 0xffff);
1779 }
1780 
1781 static bool cdclk_compute_crawl_and_squash_midpoint(struct drm_i915_private *i915,
1782 						    const struct intel_cdclk_config *old_cdclk_config,
1783 						    const struct intel_cdclk_config *new_cdclk_config,
1784 						    struct intel_cdclk_config *mid_cdclk_config)
1785 {
1786 	u16 old_waveform, new_waveform, mid_waveform;
1787 	int size = 16;
1788 	int div = 2;
1789 
1790 	/* Return if PLL is in an unknown state, force a complete disable and re-enable. */
1791 	if (cdclk_pll_is_unknown(old_cdclk_config->vco))
1792 		return false;
1793 
1794 	/* Return if both Squash and Crawl are not present */
1795 	if (!HAS_CDCLK_CRAWL(i915) || !HAS_CDCLK_SQUASH(i915))
1796 		return false;
1797 
1798 	old_waveform = cdclk_squash_waveform(i915, old_cdclk_config->cdclk);
1799 	new_waveform = cdclk_squash_waveform(i915, new_cdclk_config->cdclk);
1800 
1801 	/* Return if Squash only or Crawl only is the desired action */
1802 	if (old_cdclk_config->vco == 0 || new_cdclk_config->vco == 0 ||
1803 	    old_cdclk_config->vco == new_cdclk_config->vco ||
1804 	    old_waveform == new_waveform)
1805 		return false;
1806 
1807 	*mid_cdclk_config = *new_cdclk_config;
1808 
1809 	/*
1810 	 * Populate the mid_cdclk_config accordingly.
1811 	 * - If moving to a higher cdclk, the desired action is squashing.
1812 	 * The mid cdclk config should have the new (squash) waveform.
1813 	 * - If moving to a lower cdclk, the desired action is crawling.
1814 	 * The mid cdclk config should have the new vco.
1815 	 */
1816 
1817 	if (cdclk_squash_divider(new_waveform) > cdclk_squash_divider(old_waveform)) {
1818 		mid_cdclk_config->vco = old_cdclk_config->vco;
1819 		mid_waveform = new_waveform;
1820 	} else {
1821 		mid_cdclk_config->vco = new_cdclk_config->vco;
1822 		mid_waveform = old_waveform;
1823 	}
1824 
1825 	mid_cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_squash_divider(mid_waveform) *
1826 						    mid_cdclk_config->vco, size * div);
1827 
1828 	/* make sure the mid clock came out sane */
1829 
1830 	drm_WARN_ON(&i915->drm, mid_cdclk_config->cdclk <
1831 		    min(old_cdclk_config->cdclk, new_cdclk_config->cdclk));
1832 	drm_WARN_ON(&i915->drm, mid_cdclk_config->cdclk >
1833 		    i915->display.cdclk.max_cdclk_freq);
1834 	drm_WARN_ON(&i915->drm, cdclk_squash_waveform(i915, mid_cdclk_config->cdclk) !=
1835 		    mid_waveform);
1836 
1837 	return true;
1838 }
1839 
1840 static bool pll_enable_wa_needed(struct drm_i915_private *dev_priv)
1841 {
1842 	return ((IS_DG2(dev_priv) || IS_METEORLAKE(dev_priv)) &&
1843 		dev_priv->display.cdclk.hw.vco > 0 &&
1844 		HAS_CDCLK_SQUASH(dev_priv));
1845 }
1846 
1847 static void _bxt_set_cdclk(struct drm_i915_private *dev_priv,
1848 			   const struct intel_cdclk_config *cdclk_config,
1849 			   enum pipe pipe)
1850 {
1851 	int cdclk = cdclk_config->cdclk;
1852 	int vco = cdclk_config->vco;
1853 	u32 val;
1854 	u16 waveform;
1855 	int clock;
1856 
1857 	if (HAS_CDCLK_CRAWL(dev_priv) && dev_priv->display.cdclk.hw.vco > 0 && vco > 0 &&
1858 	    !cdclk_pll_is_unknown(dev_priv->display.cdclk.hw.vco)) {
1859 		if (dev_priv->display.cdclk.hw.vco != vco)
1860 			adlp_cdclk_pll_crawl(dev_priv, vco);
1861 	} else if (DISPLAY_VER(dev_priv) >= 11) {
1862 		/* wa_15010685871: dg2, mtl */
1863 		if (pll_enable_wa_needed(dev_priv))
1864 			dg2_cdclk_squash_program(dev_priv, 0);
1865 
1866 		icl_cdclk_pll_update(dev_priv, vco);
1867 	} else
1868 		bxt_cdclk_pll_update(dev_priv, vco);
1869 
1870 	waveform = cdclk_squash_waveform(dev_priv, cdclk);
1871 
1872 	if (waveform)
1873 		clock = vco / 2;
1874 	else
1875 		clock = cdclk;
1876 
1877 	if (HAS_CDCLK_SQUASH(dev_priv))
1878 		dg2_cdclk_squash_program(dev_priv, waveform);
1879 
1880 	val = bxt_cdclk_cd2x_div_sel(dev_priv, clock, vco) |
1881 		bxt_cdclk_cd2x_pipe(dev_priv, pipe) |
1882 		skl_cdclk_decimal(cdclk);
1883 
1884 	/*
1885 	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
1886 	 * enable otherwise.
1887 	 */
1888 	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
1889 	    cdclk >= 500000)
1890 		val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
1891 	intel_de_write(dev_priv, CDCLK_CTL, val);
1892 
1893 	if (pipe != INVALID_PIPE)
1894 		intel_crtc_wait_for_next_vblank(intel_crtc_for_pipe(dev_priv, pipe));
1895 }
1896 
1897 static void bxt_set_cdclk(struct drm_i915_private *dev_priv,
1898 			  const struct intel_cdclk_config *cdclk_config,
1899 			  enum pipe pipe)
1900 {
1901 	struct intel_cdclk_config mid_cdclk_config;
1902 	int cdclk = cdclk_config->cdclk;
1903 	int ret = 0;
1904 
1905 	/*
1906 	 * Inform power controller of upcoming frequency change.
1907 	 * Display versions 14 and beyond do not follow the PUnit
1908 	 * mailbox communication, skip
1909 	 * this step.
1910 	 */
1911 	if (DISPLAY_VER(dev_priv) >= 14)
1912 		/* NOOP */;
1913 	else if (DISPLAY_VER(dev_priv) >= 11)
1914 		ret = skl_pcode_request(&dev_priv->uncore, SKL_PCODE_CDCLK_CONTROL,
1915 					SKL_CDCLK_PREPARE_FOR_CHANGE,
1916 					SKL_CDCLK_READY_FOR_CHANGE,
1917 					SKL_CDCLK_READY_FOR_CHANGE, 3);
1918 	else
1919 		/*
1920 		 * BSpec requires us to wait up to 150usec, but that leads to
1921 		 * timeouts; the 2ms used here is based on experiment.
1922 		 */
1923 		ret = snb_pcode_write_timeout(&dev_priv->uncore,
1924 					      HSW_PCODE_DE_WRITE_FREQ_REQ,
1925 					      0x80000000, 150, 2);
1926 
1927 	if (ret) {
1928 		drm_err(&dev_priv->drm,
1929 			"Failed to inform PCU about cdclk change (err %d, freq %d)\n",
1930 			ret, cdclk);
1931 		return;
1932 	}
1933 
1934 	if (cdclk_compute_crawl_and_squash_midpoint(dev_priv, &dev_priv->display.cdclk.hw,
1935 						    cdclk_config, &mid_cdclk_config)) {
1936 		_bxt_set_cdclk(dev_priv, &mid_cdclk_config, pipe);
1937 		_bxt_set_cdclk(dev_priv, cdclk_config, pipe);
1938 	} else {
1939 		_bxt_set_cdclk(dev_priv, cdclk_config, pipe);
1940 	}
1941 
1942 	if (DISPLAY_VER(dev_priv) >= 14)
1943 		/*
1944 		 * NOOP - No Pcode communication needed for
1945 		 * Display versions 14 and beyond
1946 		 */;
1947 	else if (DISPLAY_VER(dev_priv) >= 11)
1948 		ret = snb_pcode_write(&dev_priv->uncore, SKL_PCODE_CDCLK_CONTROL,
1949 				      cdclk_config->voltage_level);
1950 	else
1951 		/*
1952 		 * The timeout isn't specified, the 2ms used here is based on
1953 		 * experiment.
1954 		 * FIXME: Waiting for the request completion could be delayed
1955 		 * until the next PCODE request based on BSpec.
1956 		 */
1957 		ret = snb_pcode_write_timeout(&dev_priv->uncore,
1958 					      HSW_PCODE_DE_WRITE_FREQ_REQ,
1959 					      cdclk_config->voltage_level,
1960 					      150, 2);
1961 
1962 	if (ret) {
1963 		drm_err(&dev_priv->drm,
1964 			"PCode CDCLK freq set failed, (err %d, freq %d)\n",
1965 			ret, cdclk);
1966 		return;
1967 	}
1968 
1969 	intel_update_cdclk(dev_priv);
1970 
1971 	if (DISPLAY_VER(dev_priv) >= 11)
1972 		/*
1973 		 * Can't read out the voltage level :(
1974 		 * Let's just assume everything is as expected.
1975 		 */
1976 		dev_priv->display.cdclk.hw.voltage_level = cdclk_config->voltage_level;
1977 }
1978 
1979 static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
1980 {
1981 	u32 cdctl, expected;
1982 	int cdclk, clock, vco;
1983 
1984 	intel_update_cdclk(dev_priv);
1985 	intel_cdclk_dump_config(dev_priv, &dev_priv->display.cdclk.hw, "Current CDCLK");
1986 
1987 	if (dev_priv->display.cdclk.hw.vco == 0 ||
1988 	    dev_priv->display.cdclk.hw.cdclk == dev_priv->display.cdclk.hw.bypass)
1989 		goto sanitize;
1990 
1991 	/* DPLL okay; verify the cdclock
1992 	 *
1993 	 * Some BIOS versions leave an incorrect decimal frequency value and
1994 	 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
1995 	 * so sanitize this register.
1996 	 */
1997 	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
1998 	/*
1999 	 * Let's ignore the pipe field, since BIOS could have configured the
2000 	 * dividers both synching to an active pipe, or asynchronously
2001 	 * (PIPE_NONE).
2002 	 */
2003 	cdctl &= ~bxt_cdclk_cd2x_pipe(dev_priv, INVALID_PIPE);
2004 
2005 	/* Make sure this is a legal cdclk value for the platform */
2006 	cdclk = bxt_calc_cdclk(dev_priv, dev_priv->display.cdclk.hw.cdclk);
2007 	if (cdclk != dev_priv->display.cdclk.hw.cdclk)
2008 		goto sanitize;
2009 
2010 	/* Make sure the VCO is correct for the cdclk */
2011 	vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
2012 	if (vco != dev_priv->display.cdclk.hw.vco)
2013 		goto sanitize;
2014 
2015 	expected = skl_cdclk_decimal(cdclk);
2016 
2017 	/* Figure out what CD2X divider we should be using for this cdclk */
2018 	if (HAS_CDCLK_SQUASH(dev_priv))
2019 		clock = dev_priv->display.cdclk.hw.vco / 2;
2020 	else
2021 		clock = dev_priv->display.cdclk.hw.cdclk;
2022 
2023 	expected |= bxt_cdclk_cd2x_div_sel(dev_priv, clock,
2024 					   dev_priv->display.cdclk.hw.vco);
2025 
2026 	/*
2027 	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
2028 	 * enable otherwise.
2029 	 */
2030 	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
2031 	    dev_priv->display.cdclk.hw.cdclk >= 500000)
2032 		expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
2033 
2034 	if (cdctl == expected)
2035 		/* All well; nothing to sanitize */
2036 		return;
2037 
2038 sanitize:
2039 	drm_dbg_kms(&dev_priv->drm, "Sanitizing cdclk programmed by pre-os\n");
2040 
2041 	/* force cdclk programming */
2042 	dev_priv->display.cdclk.hw.cdclk = 0;
2043 
2044 	/* force full PLL disable + enable */
2045 	dev_priv->display.cdclk.hw.vco = -1;
2046 }
2047 
2048 static void bxt_cdclk_init_hw(struct drm_i915_private *dev_priv)
2049 {
2050 	struct intel_cdclk_config cdclk_config;
2051 
2052 	bxt_sanitize_cdclk(dev_priv);
2053 
2054 	if (dev_priv->display.cdclk.hw.cdclk != 0 &&
2055 	    dev_priv->display.cdclk.hw.vco != 0)
2056 		return;
2057 
2058 	cdclk_config = dev_priv->display.cdclk.hw;
2059 
2060 	/*
2061 	 * FIXME:
2062 	 * - The initial CDCLK needs to be read from VBT.
2063 	 *   Need to make this change after VBT has changes for BXT.
2064 	 */
2065 	cdclk_config.cdclk = bxt_calc_cdclk(dev_priv, 0);
2066 	cdclk_config.vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk_config.cdclk);
2067 	cdclk_config.voltage_level =
2068 		intel_cdclk_calc_voltage_level(dev_priv, cdclk_config.cdclk);
2069 
2070 	bxt_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
2071 }
2072 
2073 static void bxt_cdclk_uninit_hw(struct drm_i915_private *dev_priv)
2074 {
2075 	struct intel_cdclk_config cdclk_config = dev_priv->display.cdclk.hw;
2076 
2077 	cdclk_config.cdclk = cdclk_config.bypass;
2078 	cdclk_config.vco = 0;
2079 	cdclk_config.voltage_level =
2080 		intel_cdclk_calc_voltage_level(dev_priv, cdclk_config.cdclk);
2081 
2082 	bxt_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
2083 }
2084 
2085 /**
2086  * intel_cdclk_init_hw - Initialize CDCLK hardware
2087  * @i915: i915 device
2088  *
2089  * Initialize CDCLK. This consists mainly of initializing dev_priv->display.cdclk.hw and
2090  * sanitizing the state of the hardware if needed. This is generally done only
2091  * during the display core initialization sequence, after which the DMC will
2092  * take care of turning CDCLK off/on as needed.
2093  */
2094 void intel_cdclk_init_hw(struct drm_i915_private *i915)
2095 {
2096 	if (DISPLAY_VER(i915) >= 10 || IS_BROXTON(i915))
2097 		bxt_cdclk_init_hw(i915);
2098 	else if (DISPLAY_VER(i915) == 9)
2099 		skl_cdclk_init_hw(i915);
2100 }
2101 
2102 /**
2103  * intel_cdclk_uninit_hw - Uninitialize CDCLK hardware
2104  * @i915: i915 device
2105  *
2106  * Uninitialize CDCLK. This is done only during the display core
2107  * uninitialization sequence.
2108  */
2109 void intel_cdclk_uninit_hw(struct drm_i915_private *i915)
2110 {
2111 	if (DISPLAY_VER(i915) >= 10 || IS_BROXTON(i915))
2112 		bxt_cdclk_uninit_hw(i915);
2113 	else if (DISPLAY_VER(i915) == 9)
2114 		skl_cdclk_uninit_hw(i915);
2115 }
2116 
2117 static bool intel_cdclk_can_crawl_and_squash(struct drm_i915_private *i915,
2118 					     const struct intel_cdclk_config *a,
2119 					     const struct intel_cdclk_config *b)
2120 {
2121 	u16 old_waveform;
2122 	u16 new_waveform;
2123 
2124 	drm_WARN_ON(&i915->drm, cdclk_pll_is_unknown(a->vco));
2125 
2126 	if (a->vco == 0 || b->vco == 0)
2127 		return false;
2128 
2129 	if (!HAS_CDCLK_CRAWL(i915) || !HAS_CDCLK_SQUASH(i915))
2130 		return false;
2131 
2132 	old_waveform = cdclk_squash_waveform(i915, a->cdclk);
2133 	new_waveform = cdclk_squash_waveform(i915, b->cdclk);
2134 
2135 	return a->vco != b->vco &&
2136 	       old_waveform != new_waveform;
2137 }
2138 
2139 static bool intel_cdclk_can_crawl(struct drm_i915_private *dev_priv,
2140 				  const struct intel_cdclk_config *a,
2141 				  const struct intel_cdclk_config *b)
2142 {
2143 	int a_div, b_div;
2144 
2145 	if (!HAS_CDCLK_CRAWL(dev_priv))
2146 		return false;
2147 
2148 	/*
2149 	 * The vco and cd2x divider will change independently
2150 	 * from each, so we disallow cd2x change when crawling.
2151 	 */
2152 	a_div = DIV_ROUND_CLOSEST(a->vco, a->cdclk);
2153 	b_div = DIV_ROUND_CLOSEST(b->vco, b->cdclk);
2154 
2155 	return a->vco != 0 && b->vco != 0 &&
2156 		a->vco != b->vco &&
2157 		a_div == b_div &&
2158 		a->ref == b->ref;
2159 }
2160 
2161 static bool intel_cdclk_can_squash(struct drm_i915_private *dev_priv,
2162 				   const struct intel_cdclk_config *a,
2163 				   const struct intel_cdclk_config *b)
2164 {
2165 	/*
2166 	 * FIXME should store a bit more state in intel_cdclk_config
2167 	 * to differentiate squasher vs. cd2x divider properly. For
2168 	 * the moment all platforms with squasher use a fixed cd2x
2169 	 * divider.
2170 	 */
2171 	if (!HAS_CDCLK_SQUASH(dev_priv))
2172 		return false;
2173 
2174 	return a->cdclk != b->cdclk &&
2175 		a->vco != 0 &&
2176 		a->vco == b->vco &&
2177 		a->ref == b->ref;
2178 }
2179 
2180 /**
2181  * intel_cdclk_needs_modeset - Determine if changong between the CDCLK
2182  *                             configurations requires a modeset on all pipes
2183  * @a: first CDCLK configuration
2184  * @b: second CDCLK configuration
2185  *
2186  * Returns:
2187  * True if changing between the two CDCLK configurations
2188  * requires all pipes to be off, false if not.
2189  */
2190 bool intel_cdclk_needs_modeset(const struct intel_cdclk_config *a,
2191 			       const struct intel_cdclk_config *b)
2192 {
2193 	return a->cdclk != b->cdclk ||
2194 		a->vco != b->vco ||
2195 		a->ref != b->ref;
2196 }
2197 
2198 /**
2199  * intel_cdclk_can_cd2x_update - Determine if changing between the two CDCLK
2200  *                               configurations requires only a cd2x divider update
2201  * @dev_priv: i915 device
2202  * @a: first CDCLK configuration
2203  * @b: second CDCLK configuration
2204  *
2205  * Returns:
2206  * True if changing between the two CDCLK configurations
2207  * can be done with just a cd2x divider update, false if not.
2208  */
2209 static bool intel_cdclk_can_cd2x_update(struct drm_i915_private *dev_priv,
2210 					const struct intel_cdclk_config *a,
2211 					const struct intel_cdclk_config *b)
2212 {
2213 	/* Older hw doesn't have the capability */
2214 	if (DISPLAY_VER(dev_priv) < 10 && !IS_BROXTON(dev_priv))
2215 		return false;
2216 
2217 	/*
2218 	 * FIXME should store a bit more state in intel_cdclk_config
2219 	 * to differentiate squasher vs. cd2x divider properly. For
2220 	 * the moment all platforms with squasher use a fixed cd2x
2221 	 * divider.
2222 	 */
2223 	if (HAS_CDCLK_SQUASH(dev_priv))
2224 		return false;
2225 
2226 	return a->cdclk != b->cdclk &&
2227 		a->vco != 0 &&
2228 		a->vco == b->vco &&
2229 		a->ref == b->ref;
2230 }
2231 
2232 /**
2233  * intel_cdclk_changed - Determine if two CDCLK configurations are different
2234  * @a: first CDCLK configuration
2235  * @b: second CDCLK configuration
2236  *
2237  * Returns:
2238  * True if the CDCLK configurations don't match, false if they do.
2239  */
2240 static bool intel_cdclk_changed(const struct intel_cdclk_config *a,
2241 				const struct intel_cdclk_config *b)
2242 {
2243 	return intel_cdclk_needs_modeset(a, b) ||
2244 		a->voltage_level != b->voltage_level;
2245 }
2246 
2247 void intel_cdclk_dump_config(struct drm_i915_private *i915,
2248 			     const struct intel_cdclk_config *cdclk_config,
2249 			     const char *context)
2250 {
2251 	drm_dbg_kms(&i915->drm, "%s %d kHz, VCO %d kHz, ref %d kHz, bypass %d kHz, voltage level %d\n",
2252 		    context, cdclk_config->cdclk, cdclk_config->vco,
2253 		    cdclk_config->ref, cdclk_config->bypass,
2254 		    cdclk_config->voltage_level);
2255 }
2256 
2257 /**
2258  * intel_set_cdclk - Push the CDCLK configuration to the hardware
2259  * @dev_priv: i915 device
2260  * @cdclk_config: new CDCLK configuration
2261  * @pipe: pipe with which to synchronize the update
2262  *
2263  * Program the hardware based on the passed in CDCLK state,
2264  * if necessary.
2265  */
2266 static void intel_set_cdclk(struct drm_i915_private *dev_priv,
2267 			    const struct intel_cdclk_config *cdclk_config,
2268 			    enum pipe pipe)
2269 {
2270 	struct intel_encoder *encoder;
2271 
2272 	if (!intel_cdclk_changed(&dev_priv->display.cdclk.hw, cdclk_config))
2273 		return;
2274 
2275 	if (drm_WARN_ON_ONCE(&dev_priv->drm, !dev_priv->display.funcs.cdclk->set_cdclk))
2276 		return;
2277 
2278 	intel_cdclk_dump_config(dev_priv, cdclk_config, "Changing CDCLK to");
2279 
2280 	for_each_intel_encoder_with_psr(&dev_priv->drm, encoder) {
2281 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2282 
2283 		intel_psr_pause(intel_dp);
2284 	}
2285 
2286 	intel_audio_cdclk_change_pre(dev_priv);
2287 
2288 	/*
2289 	 * Lock aux/gmbus while we change cdclk in case those
2290 	 * functions use cdclk. Not all platforms/ports do,
2291 	 * but we'll lock them all for simplicity.
2292 	 */
2293 	mutex_lock(&dev_priv->display.gmbus.mutex);
2294 	for_each_intel_dp(&dev_priv->drm, encoder) {
2295 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2296 
2297 		mutex_lock_nest_lock(&intel_dp->aux.hw_mutex,
2298 				     &dev_priv->display.gmbus.mutex);
2299 	}
2300 
2301 	intel_cdclk_set_cdclk(dev_priv, cdclk_config, pipe);
2302 
2303 	for_each_intel_dp(&dev_priv->drm, encoder) {
2304 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2305 
2306 		mutex_unlock(&intel_dp->aux.hw_mutex);
2307 	}
2308 	mutex_unlock(&dev_priv->display.gmbus.mutex);
2309 
2310 	for_each_intel_encoder_with_psr(&dev_priv->drm, encoder) {
2311 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2312 
2313 		intel_psr_resume(intel_dp);
2314 	}
2315 
2316 	intel_audio_cdclk_change_post(dev_priv);
2317 
2318 	if (drm_WARN(&dev_priv->drm,
2319 		     intel_cdclk_changed(&dev_priv->display.cdclk.hw, cdclk_config),
2320 		     "cdclk state doesn't match!\n")) {
2321 		intel_cdclk_dump_config(dev_priv, &dev_priv->display.cdclk.hw, "[hw state]");
2322 		intel_cdclk_dump_config(dev_priv, cdclk_config, "[sw state]");
2323 	}
2324 }
2325 
2326 /**
2327  * intel_set_cdclk_pre_plane_update - Push the CDCLK state to the hardware
2328  * @state: intel atomic state
2329  *
2330  * Program the hardware before updating the HW plane state based on the
2331  * new CDCLK state, if necessary.
2332  */
2333 void
2334 intel_set_cdclk_pre_plane_update(struct intel_atomic_state *state)
2335 {
2336 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2337 	const struct intel_cdclk_state *old_cdclk_state =
2338 		intel_atomic_get_old_cdclk_state(state);
2339 	const struct intel_cdclk_state *new_cdclk_state =
2340 		intel_atomic_get_new_cdclk_state(state);
2341 	enum pipe pipe = new_cdclk_state->pipe;
2342 
2343 	if (!intel_cdclk_changed(&old_cdclk_state->actual,
2344 				 &new_cdclk_state->actual))
2345 		return;
2346 
2347 	if (pipe == INVALID_PIPE ||
2348 	    old_cdclk_state->actual.cdclk <= new_cdclk_state->actual.cdclk) {
2349 		drm_WARN_ON(&dev_priv->drm, !new_cdclk_state->base.changed);
2350 
2351 		intel_set_cdclk(dev_priv, &new_cdclk_state->actual, pipe);
2352 	}
2353 }
2354 
2355 /**
2356  * intel_set_cdclk_post_plane_update - Push the CDCLK state to the hardware
2357  * @state: intel atomic state
2358  *
2359  * Program the hardware after updating the HW plane state based on the
2360  * new CDCLK state, if necessary.
2361  */
2362 void
2363 intel_set_cdclk_post_plane_update(struct intel_atomic_state *state)
2364 {
2365 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2366 	const struct intel_cdclk_state *old_cdclk_state =
2367 		intel_atomic_get_old_cdclk_state(state);
2368 	const struct intel_cdclk_state *new_cdclk_state =
2369 		intel_atomic_get_new_cdclk_state(state);
2370 	enum pipe pipe = new_cdclk_state->pipe;
2371 
2372 	if (!intel_cdclk_changed(&old_cdclk_state->actual,
2373 				 &new_cdclk_state->actual))
2374 		return;
2375 
2376 	if (pipe != INVALID_PIPE &&
2377 	    old_cdclk_state->actual.cdclk > new_cdclk_state->actual.cdclk) {
2378 		drm_WARN_ON(&dev_priv->drm, !new_cdclk_state->base.changed);
2379 
2380 		intel_set_cdclk(dev_priv, &new_cdclk_state->actual, pipe);
2381 	}
2382 }
2383 
2384 static int intel_pixel_rate_to_cdclk(const struct intel_crtc_state *crtc_state)
2385 {
2386 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2387 	int pixel_rate = crtc_state->pixel_rate;
2388 
2389 	if (DISPLAY_VER(dev_priv) >= 10)
2390 		return DIV_ROUND_UP(pixel_rate, 2);
2391 	else if (DISPLAY_VER(dev_priv) == 9 ||
2392 		 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
2393 		return pixel_rate;
2394 	else if (IS_CHERRYVIEW(dev_priv))
2395 		return DIV_ROUND_UP(pixel_rate * 100, 95);
2396 	else if (crtc_state->double_wide)
2397 		return DIV_ROUND_UP(pixel_rate * 100, 90 * 2);
2398 	else
2399 		return DIV_ROUND_UP(pixel_rate * 100, 90);
2400 }
2401 
2402 static int intel_planes_min_cdclk(const struct intel_crtc_state *crtc_state)
2403 {
2404 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2405 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2406 	struct intel_plane *plane;
2407 	int min_cdclk = 0;
2408 
2409 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
2410 		min_cdclk = max(crtc_state->min_cdclk[plane->id], min_cdclk);
2411 
2412 	return min_cdclk;
2413 }
2414 
2415 int intel_crtc_compute_min_cdclk(const struct intel_crtc_state *crtc_state)
2416 {
2417 	struct drm_i915_private *dev_priv =
2418 		to_i915(crtc_state->uapi.crtc->dev);
2419 	int min_cdclk;
2420 
2421 	if (!crtc_state->hw.enable)
2422 		return 0;
2423 
2424 	min_cdclk = intel_pixel_rate_to_cdclk(crtc_state);
2425 
2426 	/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
2427 	if (IS_BROADWELL(dev_priv) && hsw_crtc_state_ips_capable(crtc_state))
2428 		min_cdclk = DIV_ROUND_UP(min_cdclk * 100, 95);
2429 
2430 	/* BSpec says "Do not use DisplayPort with CDCLK less than 432 MHz,
2431 	 * audio enabled, port width x4, and link rate HBR2 (5.4 GHz), or else
2432 	 * there may be audio corruption or screen corruption." This cdclk
2433 	 * restriction for GLK is 316.8 MHz.
2434 	 */
2435 	if (intel_crtc_has_dp_encoder(crtc_state) &&
2436 	    crtc_state->has_audio &&
2437 	    crtc_state->port_clock >= 540000 &&
2438 	    crtc_state->lane_count == 4) {
2439 		if (DISPLAY_VER(dev_priv) == 10) {
2440 			/* Display WA #1145: glk */
2441 			min_cdclk = max(316800, min_cdclk);
2442 		} else if (DISPLAY_VER(dev_priv) == 9 || IS_BROADWELL(dev_priv)) {
2443 			/* Display WA #1144: skl,bxt */
2444 			min_cdclk = max(432000, min_cdclk);
2445 		}
2446 	}
2447 
2448 	/*
2449 	 * According to BSpec, "The CD clock frequency must be at least twice
2450 	 * the frequency of the Azalia BCLK." and BCLK is 96 MHz by default.
2451 	 */
2452 	if (crtc_state->has_audio && DISPLAY_VER(dev_priv) >= 9)
2453 		min_cdclk = max(2 * 96000, min_cdclk);
2454 
2455 	/*
2456 	 * "For DP audio configuration, cdclk frequency shall be set to
2457 	 *  meet the following requirements:
2458 	 *  DP Link Frequency(MHz) | Cdclk frequency(MHz)
2459 	 *  270                    | 320 or higher
2460 	 *  162                    | 200 or higher"
2461 	 */
2462 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
2463 	    intel_crtc_has_dp_encoder(crtc_state) && crtc_state->has_audio)
2464 		min_cdclk = max(crtc_state->port_clock, min_cdclk);
2465 
2466 	/*
2467 	 * On Valleyview some DSI panels lose (v|h)sync when the clock is lower
2468 	 * than 320000KHz.
2469 	 */
2470 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) &&
2471 	    IS_VALLEYVIEW(dev_priv))
2472 		min_cdclk = max(320000, min_cdclk);
2473 
2474 	/*
2475 	 * On Geminilake once the CDCLK gets as low as 79200
2476 	 * picture gets unstable, despite that values are
2477 	 * correct for DSI PLL and DE PLL.
2478 	 */
2479 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) &&
2480 	    IS_GEMINILAKE(dev_priv))
2481 		min_cdclk = max(158400, min_cdclk);
2482 
2483 	/* Account for additional needs from the planes */
2484 	min_cdclk = max(intel_planes_min_cdclk(crtc_state), min_cdclk);
2485 
2486 	/*
2487 	 * When we decide to use only one VDSC engine, since
2488 	 * each VDSC operates with 1 ppc throughput, pixel clock
2489 	 * cannot be higher than the VDSC clock (cdclk)
2490 	 */
2491 	if (crtc_state->dsc.compression_enable && !crtc_state->dsc.dsc_split)
2492 		min_cdclk = max(min_cdclk, (int)crtc_state->pixel_rate);
2493 
2494 	/*
2495 	 * HACK. Currently for TGL/DG2 platforms we calculate
2496 	 * min_cdclk initially based on pixel_rate divided
2497 	 * by 2, accounting for also plane requirements,
2498 	 * however in some cases the lowest possible CDCLK
2499 	 * doesn't work and causing the underruns.
2500 	 * Explicitly stating here that this seems to be currently
2501 	 * rather a Hack, than final solution.
2502 	 */
2503 	if (IS_TIGERLAKE(dev_priv) || IS_DG2(dev_priv)) {
2504 		/*
2505 		 * Clamp to max_cdclk_freq in case pixel rate is higher,
2506 		 * in order not to break an 8K, but still leave W/A at place.
2507 		 */
2508 		min_cdclk = max_t(int, min_cdclk,
2509 				  min_t(int, crtc_state->pixel_rate,
2510 					dev_priv->display.cdclk.max_cdclk_freq));
2511 	}
2512 
2513 	return min_cdclk;
2514 }
2515 
2516 static int intel_compute_min_cdclk(struct intel_cdclk_state *cdclk_state)
2517 {
2518 	struct intel_atomic_state *state = cdclk_state->base.state;
2519 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2520 	const struct intel_bw_state *bw_state;
2521 	struct intel_crtc *crtc;
2522 	struct intel_crtc_state *crtc_state;
2523 	int min_cdclk, i;
2524 	enum pipe pipe;
2525 
2526 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2527 		int ret;
2528 
2529 		min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
2530 		if (min_cdclk < 0)
2531 			return min_cdclk;
2532 
2533 		if (cdclk_state->min_cdclk[crtc->pipe] == min_cdclk)
2534 			continue;
2535 
2536 		cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
2537 
2538 		ret = intel_atomic_lock_global_state(&cdclk_state->base);
2539 		if (ret)
2540 			return ret;
2541 	}
2542 
2543 	bw_state = intel_atomic_get_new_bw_state(state);
2544 	if (bw_state) {
2545 		min_cdclk = intel_bw_min_cdclk(dev_priv, bw_state);
2546 
2547 		if (cdclk_state->bw_min_cdclk != min_cdclk) {
2548 			int ret;
2549 
2550 			cdclk_state->bw_min_cdclk = min_cdclk;
2551 
2552 			ret = intel_atomic_lock_global_state(&cdclk_state->base);
2553 			if (ret)
2554 				return ret;
2555 		}
2556 	}
2557 
2558 	min_cdclk = max(cdclk_state->force_min_cdclk,
2559 			cdclk_state->bw_min_cdclk);
2560 	for_each_pipe(dev_priv, pipe)
2561 		min_cdclk = max(cdclk_state->min_cdclk[pipe], min_cdclk);
2562 
2563 	if (min_cdclk > dev_priv->display.cdclk.max_cdclk_freq) {
2564 		drm_dbg_kms(&dev_priv->drm,
2565 			    "required cdclk (%d kHz) exceeds max (%d kHz)\n",
2566 			    min_cdclk, dev_priv->display.cdclk.max_cdclk_freq);
2567 		return -EINVAL;
2568 	}
2569 
2570 	return min_cdclk;
2571 }
2572 
2573 /*
2574  * Account for port clock min voltage level requirements.
2575  * This only really does something on DISPLA_VER >= 11 but can be
2576  * called on earlier platforms as well.
2577  *
2578  * Note that this functions assumes that 0 is
2579  * the lowest voltage value, and higher values
2580  * correspond to increasingly higher voltages.
2581  *
2582  * Should that relationship no longer hold on
2583  * future platforms this code will need to be
2584  * adjusted.
2585  */
2586 static int bxt_compute_min_voltage_level(struct intel_cdclk_state *cdclk_state)
2587 {
2588 	struct intel_atomic_state *state = cdclk_state->base.state;
2589 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2590 	struct intel_crtc *crtc;
2591 	struct intel_crtc_state *crtc_state;
2592 	u8 min_voltage_level;
2593 	int i;
2594 	enum pipe pipe;
2595 
2596 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2597 		int ret;
2598 
2599 		if (crtc_state->hw.enable)
2600 			min_voltage_level = crtc_state->min_voltage_level;
2601 		else
2602 			min_voltage_level = 0;
2603 
2604 		if (cdclk_state->min_voltage_level[crtc->pipe] == min_voltage_level)
2605 			continue;
2606 
2607 		cdclk_state->min_voltage_level[crtc->pipe] = min_voltage_level;
2608 
2609 		ret = intel_atomic_lock_global_state(&cdclk_state->base);
2610 		if (ret)
2611 			return ret;
2612 	}
2613 
2614 	min_voltage_level = 0;
2615 	for_each_pipe(dev_priv, pipe)
2616 		min_voltage_level = max(cdclk_state->min_voltage_level[pipe],
2617 					min_voltage_level);
2618 
2619 	return min_voltage_level;
2620 }
2621 
2622 static int vlv_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2623 {
2624 	struct intel_atomic_state *state = cdclk_state->base.state;
2625 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2626 	int min_cdclk, cdclk;
2627 
2628 	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2629 	if (min_cdclk < 0)
2630 		return min_cdclk;
2631 
2632 	cdclk = vlv_calc_cdclk(dev_priv, min_cdclk);
2633 
2634 	cdclk_state->logical.cdclk = cdclk;
2635 	cdclk_state->logical.voltage_level =
2636 		vlv_calc_voltage_level(dev_priv, cdclk);
2637 
2638 	if (!cdclk_state->active_pipes) {
2639 		cdclk = vlv_calc_cdclk(dev_priv, cdclk_state->force_min_cdclk);
2640 
2641 		cdclk_state->actual.cdclk = cdclk;
2642 		cdclk_state->actual.voltage_level =
2643 			vlv_calc_voltage_level(dev_priv, cdclk);
2644 	} else {
2645 		cdclk_state->actual = cdclk_state->logical;
2646 	}
2647 
2648 	return 0;
2649 }
2650 
2651 static int bdw_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2652 {
2653 	int min_cdclk, cdclk;
2654 
2655 	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2656 	if (min_cdclk < 0)
2657 		return min_cdclk;
2658 
2659 	cdclk = bdw_calc_cdclk(min_cdclk);
2660 
2661 	cdclk_state->logical.cdclk = cdclk;
2662 	cdclk_state->logical.voltage_level =
2663 		bdw_calc_voltage_level(cdclk);
2664 
2665 	if (!cdclk_state->active_pipes) {
2666 		cdclk = bdw_calc_cdclk(cdclk_state->force_min_cdclk);
2667 
2668 		cdclk_state->actual.cdclk = cdclk;
2669 		cdclk_state->actual.voltage_level =
2670 			bdw_calc_voltage_level(cdclk);
2671 	} else {
2672 		cdclk_state->actual = cdclk_state->logical;
2673 	}
2674 
2675 	return 0;
2676 }
2677 
2678 static int skl_dpll0_vco(struct intel_cdclk_state *cdclk_state)
2679 {
2680 	struct intel_atomic_state *state = cdclk_state->base.state;
2681 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2682 	struct intel_crtc *crtc;
2683 	struct intel_crtc_state *crtc_state;
2684 	int vco, i;
2685 
2686 	vco = cdclk_state->logical.vco;
2687 	if (!vco)
2688 		vco = dev_priv->skl_preferred_vco_freq;
2689 
2690 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2691 		if (!crtc_state->hw.enable)
2692 			continue;
2693 
2694 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
2695 			continue;
2696 
2697 		/*
2698 		 * DPLL0 VCO may need to be adjusted to get the correct
2699 		 * clock for eDP. This will affect cdclk as well.
2700 		 */
2701 		switch (crtc_state->port_clock / 2) {
2702 		case 108000:
2703 		case 216000:
2704 			vco = 8640000;
2705 			break;
2706 		default:
2707 			vco = 8100000;
2708 			break;
2709 		}
2710 	}
2711 
2712 	return vco;
2713 }
2714 
2715 static int skl_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2716 {
2717 	int min_cdclk, cdclk, vco;
2718 
2719 	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2720 	if (min_cdclk < 0)
2721 		return min_cdclk;
2722 
2723 	vco = skl_dpll0_vco(cdclk_state);
2724 
2725 	cdclk = skl_calc_cdclk(min_cdclk, vco);
2726 
2727 	cdclk_state->logical.vco = vco;
2728 	cdclk_state->logical.cdclk = cdclk;
2729 	cdclk_state->logical.voltage_level =
2730 		skl_calc_voltage_level(cdclk);
2731 
2732 	if (!cdclk_state->active_pipes) {
2733 		cdclk = skl_calc_cdclk(cdclk_state->force_min_cdclk, vco);
2734 
2735 		cdclk_state->actual.vco = vco;
2736 		cdclk_state->actual.cdclk = cdclk;
2737 		cdclk_state->actual.voltage_level =
2738 			skl_calc_voltage_level(cdclk);
2739 	} else {
2740 		cdclk_state->actual = cdclk_state->logical;
2741 	}
2742 
2743 	return 0;
2744 }
2745 
2746 static int bxt_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2747 {
2748 	struct intel_atomic_state *state = cdclk_state->base.state;
2749 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2750 	int min_cdclk, min_voltage_level, cdclk, vco;
2751 
2752 	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2753 	if (min_cdclk < 0)
2754 		return min_cdclk;
2755 
2756 	min_voltage_level = bxt_compute_min_voltage_level(cdclk_state);
2757 	if (min_voltage_level < 0)
2758 		return min_voltage_level;
2759 
2760 	cdclk = bxt_calc_cdclk(dev_priv, min_cdclk);
2761 	vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
2762 
2763 	cdclk_state->logical.vco = vco;
2764 	cdclk_state->logical.cdclk = cdclk;
2765 	cdclk_state->logical.voltage_level =
2766 		max_t(int, min_voltage_level,
2767 		      intel_cdclk_calc_voltage_level(dev_priv, cdclk));
2768 
2769 	if (!cdclk_state->active_pipes) {
2770 		cdclk = bxt_calc_cdclk(dev_priv, cdclk_state->force_min_cdclk);
2771 		vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
2772 
2773 		cdclk_state->actual.vco = vco;
2774 		cdclk_state->actual.cdclk = cdclk;
2775 		cdclk_state->actual.voltage_level =
2776 			intel_cdclk_calc_voltage_level(dev_priv, cdclk);
2777 	} else {
2778 		cdclk_state->actual = cdclk_state->logical;
2779 	}
2780 
2781 	return 0;
2782 }
2783 
2784 static int fixed_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2785 {
2786 	int min_cdclk;
2787 
2788 	/*
2789 	 * We can't change the cdclk frequency, but we still want to
2790 	 * check that the required minimum frequency doesn't exceed
2791 	 * the actual cdclk frequency.
2792 	 */
2793 	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2794 	if (min_cdclk < 0)
2795 		return min_cdclk;
2796 
2797 	return 0;
2798 }
2799 
2800 static struct intel_global_state *intel_cdclk_duplicate_state(struct intel_global_obj *obj)
2801 {
2802 	struct intel_cdclk_state *cdclk_state;
2803 
2804 	cdclk_state = kmemdup(obj->state, sizeof(*cdclk_state), GFP_KERNEL);
2805 	if (!cdclk_state)
2806 		return NULL;
2807 
2808 	cdclk_state->pipe = INVALID_PIPE;
2809 
2810 	return &cdclk_state->base;
2811 }
2812 
2813 static void intel_cdclk_destroy_state(struct intel_global_obj *obj,
2814 				      struct intel_global_state *state)
2815 {
2816 	kfree(state);
2817 }
2818 
2819 static const struct intel_global_state_funcs intel_cdclk_funcs = {
2820 	.atomic_duplicate_state = intel_cdclk_duplicate_state,
2821 	.atomic_destroy_state = intel_cdclk_destroy_state,
2822 };
2823 
2824 struct intel_cdclk_state *
2825 intel_atomic_get_cdclk_state(struct intel_atomic_state *state)
2826 {
2827 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2828 	struct intel_global_state *cdclk_state;
2829 
2830 	cdclk_state = intel_atomic_get_global_obj_state(state, &dev_priv->display.cdclk.obj);
2831 	if (IS_ERR(cdclk_state))
2832 		return ERR_CAST(cdclk_state);
2833 
2834 	return to_intel_cdclk_state(cdclk_state);
2835 }
2836 
2837 int intel_cdclk_atomic_check(struct intel_atomic_state *state,
2838 			     bool *need_cdclk_calc)
2839 {
2840 	const struct intel_cdclk_state *old_cdclk_state;
2841 	const struct intel_cdclk_state *new_cdclk_state;
2842 	struct intel_plane_state *plane_state;
2843 	struct intel_plane *plane;
2844 	int ret;
2845 	int i;
2846 
2847 	/*
2848 	 * active_planes bitmask has been updated, and potentially affected
2849 	 * planes are part of the state. We can now compute the minimum cdclk
2850 	 * for each plane.
2851 	 */
2852 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
2853 		ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
2854 		if (ret)
2855 			return ret;
2856 	}
2857 
2858 	ret = intel_bw_calc_min_cdclk(state, need_cdclk_calc);
2859 	if (ret)
2860 		return ret;
2861 
2862 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
2863 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
2864 
2865 	if (new_cdclk_state &&
2866 	    old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
2867 		*need_cdclk_calc = true;
2868 
2869 	return 0;
2870 }
2871 
2872 int intel_cdclk_init(struct drm_i915_private *dev_priv)
2873 {
2874 	struct intel_cdclk_state *cdclk_state;
2875 
2876 	cdclk_state = kzalloc(sizeof(*cdclk_state), GFP_KERNEL);
2877 	if (!cdclk_state)
2878 		return -ENOMEM;
2879 
2880 	intel_atomic_global_obj_init(dev_priv, &dev_priv->display.cdclk.obj,
2881 				     &cdclk_state->base, &intel_cdclk_funcs);
2882 
2883 	return 0;
2884 }
2885 
2886 int intel_modeset_calc_cdclk(struct intel_atomic_state *state)
2887 {
2888 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2889 	const struct intel_cdclk_state *old_cdclk_state;
2890 	struct intel_cdclk_state *new_cdclk_state;
2891 	enum pipe pipe = INVALID_PIPE;
2892 	int ret;
2893 
2894 	new_cdclk_state = intel_atomic_get_cdclk_state(state);
2895 	if (IS_ERR(new_cdclk_state))
2896 		return PTR_ERR(new_cdclk_state);
2897 
2898 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
2899 
2900 	new_cdclk_state->active_pipes =
2901 		intel_calc_active_pipes(state, old_cdclk_state->active_pipes);
2902 
2903 	ret = intel_cdclk_modeset_calc_cdclk(dev_priv, new_cdclk_state);
2904 	if (ret)
2905 		return ret;
2906 
2907 	if (intel_cdclk_changed(&old_cdclk_state->actual,
2908 				&new_cdclk_state->actual)) {
2909 		/*
2910 		 * Also serialize commits across all crtcs
2911 		 * if the actual hw needs to be poked.
2912 		 */
2913 		ret = intel_atomic_serialize_global_state(&new_cdclk_state->base);
2914 		if (ret)
2915 			return ret;
2916 	} else if (old_cdclk_state->active_pipes != new_cdclk_state->active_pipes ||
2917 		   old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk ||
2918 		   intel_cdclk_changed(&old_cdclk_state->logical,
2919 				       &new_cdclk_state->logical)) {
2920 		ret = intel_atomic_lock_global_state(&new_cdclk_state->base);
2921 		if (ret)
2922 			return ret;
2923 	} else {
2924 		return 0;
2925 	}
2926 
2927 	if (is_power_of_2(new_cdclk_state->active_pipes) &&
2928 	    intel_cdclk_can_cd2x_update(dev_priv,
2929 					&old_cdclk_state->actual,
2930 					&new_cdclk_state->actual)) {
2931 		struct intel_crtc *crtc;
2932 		struct intel_crtc_state *crtc_state;
2933 
2934 		pipe = ilog2(new_cdclk_state->active_pipes);
2935 		crtc = intel_crtc_for_pipe(dev_priv, pipe);
2936 
2937 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
2938 		if (IS_ERR(crtc_state))
2939 			return PTR_ERR(crtc_state);
2940 
2941 		if (intel_crtc_needs_modeset(crtc_state))
2942 			pipe = INVALID_PIPE;
2943 	}
2944 
2945 	if (intel_cdclk_can_crawl_and_squash(dev_priv,
2946 					     &old_cdclk_state->actual,
2947 					     &new_cdclk_state->actual)) {
2948 		drm_dbg_kms(&dev_priv->drm,
2949 			    "Can change cdclk via crawling and squashing\n");
2950 	} else if (intel_cdclk_can_squash(dev_priv,
2951 					&old_cdclk_state->actual,
2952 					&new_cdclk_state->actual)) {
2953 		drm_dbg_kms(&dev_priv->drm,
2954 			    "Can change cdclk via squashing\n");
2955 	} else if (intel_cdclk_can_crawl(dev_priv,
2956 					 &old_cdclk_state->actual,
2957 					 &new_cdclk_state->actual)) {
2958 		drm_dbg_kms(&dev_priv->drm,
2959 			    "Can change cdclk via crawling\n");
2960 	} else if (pipe != INVALID_PIPE) {
2961 		new_cdclk_state->pipe = pipe;
2962 
2963 		drm_dbg_kms(&dev_priv->drm,
2964 			    "Can change cdclk cd2x divider with pipe %c active\n",
2965 			    pipe_name(pipe));
2966 	} else if (intel_cdclk_needs_modeset(&old_cdclk_state->actual,
2967 					     &new_cdclk_state->actual)) {
2968 		/* All pipes must be switched off while we change the cdclk. */
2969 		ret = intel_modeset_all_pipes(state, "CDCLK change");
2970 		if (ret)
2971 			return ret;
2972 
2973 		drm_dbg_kms(&dev_priv->drm,
2974 			    "Modeset required for cdclk change\n");
2975 	}
2976 
2977 	drm_dbg_kms(&dev_priv->drm,
2978 		    "New cdclk calculated to be logical %u kHz, actual %u kHz\n",
2979 		    new_cdclk_state->logical.cdclk,
2980 		    new_cdclk_state->actual.cdclk);
2981 	drm_dbg_kms(&dev_priv->drm,
2982 		    "New voltage level calculated to be logical %u, actual %u\n",
2983 		    new_cdclk_state->logical.voltage_level,
2984 		    new_cdclk_state->actual.voltage_level);
2985 
2986 	return 0;
2987 }
2988 
2989 static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
2990 {
2991 	int max_cdclk_freq = dev_priv->display.cdclk.max_cdclk_freq;
2992 
2993 	if (DISPLAY_VER(dev_priv) >= 10)
2994 		return 2 * max_cdclk_freq;
2995 	else if (DISPLAY_VER(dev_priv) == 9 ||
2996 		 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
2997 		return max_cdclk_freq;
2998 	else if (IS_CHERRYVIEW(dev_priv))
2999 		return max_cdclk_freq*95/100;
3000 	else if (DISPLAY_VER(dev_priv) < 4)
3001 		return 2*max_cdclk_freq*90/100;
3002 	else
3003 		return max_cdclk_freq*90/100;
3004 }
3005 
3006 /**
3007  * intel_update_max_cdclk - Determine the maximum support CDCLK frequency
3008  * @dev_priv: i915 device
3009  *
3010  * Determine the maximum CDCLK frequency the platform supports, and also
3011  * derive the maximum dot clock frequency the maximum CDCLK frequency
3012  * allows.
3013  */
3014 void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
3015 {
3016 	if (IS_JSL_EHL(dev_priv)) {
3017 		if (dev_priv->display.cdclk.hw.ref == 24000)
3018 			dev_priv->display.cdclk.max_cdclk_freq = 552000;
3019 		else
3020 			dev_priv->display.cdclk.max_cdclk_freq = 556800;
3021 	} else if (DISPLAY_VER(dev_priv) >= 11) {
3022 		if (dev_priv->display.cdclk.hw.ref == 24000)
3023 			dev_priv->display.cdclk.max_cdclk_freq = 648000;
3024 		else
3025 			dev_priv->display.cdclk.max_cdclk_freq = 652800;
3026 	} else if (IS_GEMINILAKE(dev_priv)) {
3027 		dev_priv->display.cdclk.max_cdclk_freq = 316800;
3028 	} else if (IS_BROXTON(dev_priv)) {
3029 		dev_priv->display.cdclk.max_cdclk_freq = 624000;
3030 	} else if (DISPLAY_VER(dev_priv) == 9) {
3031 		u32 limit = intel_de_read(dev_priv, SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
3032 		int max_cdclk, vco;
3033 
3034 		vco = dev_priv->skl_preferred_vco_freq;
3035 		drm_WARN_ON(&dev_priv->drm, vco != 8100000 && vco != 8640000);
3036 
3037 		/*
3038 		 * Use the lower (vco 8640) cdclk values as a
3039 		 * first guess. skl_calc_cdclk() will correct it
3040 		 * if the preferred vco is 8100 instead.
3041 		 */
3042 		if (limit == SKL_DFSM_CDCLK_LIMIT_675)
3043 			max_cdclk = 617143;
3044 		else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
3045 			max_cdclk = 540000;
3046 		else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
3047 			max_cdclk = 432000;
3048 		else
3049 			max_cdclk = 308571;
3050 
3051 		dev_priv->display.cdclk.max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
3052 	} else if (IS_BROADWELL(dev_priv))  {
3053 		/*
3054 		 * FIXME with extra cooling we can allow
3055 		 * 540 MHz for ULX and 675 Mhz for ULT.
3056 		 * How can we know if extra cooling is
3057 		 * available? PCI ID, VTB, something else?
3058 		 */
3059 		if (intel_de_read(dev_priv, FUSE_STRAP) & HSW_CDCLK_LIMIT)
3060 			dev_priv->display.cdclk.max_cdclk_freq = 450000;
3061 		else if (IS_BDW_ULX(dev_priv))
3062 			dev_priv->display.cdclk.max_cdclk_freq = 450000;
3063 		else if (IS_BDW_ULT(dev_priv))
3064 			dev_priv->display.cdclk.max_cdclk_freq = 540000;
3065 		else
3066 			dev_priv->display.cdclk.max_cdclk_freq = 675000;
3067 	} else if (IS_CHERRYVIEW(dev_priv)) {
3068 		dev_priv->display.cdclk.max_cdclk_freq = 320000;
3069 	} else if (IS_VALLEYVIEW(dev_priv)) {
3070 		dev_priv->display.cdclk.max_cdclk_freq = 400000;
3071 	} else {
3072 		/* otherwise assume cdclk is fixed */
3073 		dev_priv->display.cdclk.max_cdclk_freq = dev_priv->display.cdclk.hw.cdclk;
3074 	}
3075 
3076 	dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
3077 
3078 	drm_dbg(&dev_priv->drm, "Max CD clock rate: %d kHz\n",
3079 		dev_priv->display.cdclk.max_cdclk_freq);
3080 
3081 	drm_dbg(&dev_priv->drm, "Max dotclock rate: %d kHz\n",
3082 		dev_priv->max_dotclk_freq);
3083 }
3084 
3085 /**
3086  * intel_update_cdclk - Determine the current CDCLK frequency
3087  * @dev_priv: i915 device
3088  *
3089  * Determine the current CDCLK frequency.
3090  */
3091 void intel_update_cdclk(struct drm_i915_private *dev_priv)
3092 {
3093 	intel_cdclk_get_cdclk(dev_priv, &dev_priv->display.cdclk.hw);
3094 
3095 	/*
3096 	 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
3097 	 * Programmng [sic] note: bit[9:2] should be programmed to the number
3098 	 * of cdclk that generates 4MHz reference clock freq which is used to
3099 	 * generate GMBus clock. This will vary with the cdclk freq.
3100 	 */
3101 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
3102 		intel_de_write(dev_priv, GMBUSFREQ_VLV,
3103 			       DIV_ROUND_UP(dev_priv->display.cdclk.hw.cdclk, 1000));
3104 }
3105 
3106 static int dg1_rawclk(struct drm_i915_private *dev_priv)
3107 {
3108 	/*
3109 	 * DG1 always uses a 38.4 MHz rawclk.  The bspec tells us
3110 	 * "Program Numerator=2, Denominator=4, Divider=37 decimal."
3111 	 */
3112 	intel_de_write(dev_priv, PCH_RAWCLK_FREQ,
3113 		       CNP_RAWCLK_DEN(4) | CNP_RAWCLK_DIV(37) | ICP_RAWCLK_NUM(2));
3114 
3115 	return 38400;
3116 }
3117 
3118 static int cnp_rawclk(struct drm_i915_private *dev_priv)
3119 {
3120 	u32 rawclk;
3121 	int divider, fraction;
3122 
3123 	if (intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_RAW_FREQUENCY) {
3124 		/* 24 MHz */
3125 		divider = 24000;
3126 		fraction = 0;
3127 	} else {
3128 		/* 19.2 MHz */
3129 		divider = 19000;
3130 		fraction = 200;
3131 	}
3132 
3133 	rawclk = CNP_RAWCLK_DIV(divider / 1000);
3134 	if (fraction) {
3135 		int numerator = 1;
3136 
3137 		rawclk |= CNP_RAWCLK_DEN(DIV_ROUND_CLOSEST(numerator * 1000,
3138 							   fraction) - 1);
3139 		if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
3140 			rawclk |= ICP_RAWCLK_NUM(numerator);
3141 	}
3142 
3143 	intel_de_write(dev_priv, PCH_RAWCLK_FREQ, rawclk);
3144 	return divider + fraction;
3145 }
3146 
3147 static int pch_rawclk(struct drm_i915_private *dev_priv)
3148 {
3149 	return (intel_de_read(dev_priv, PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
3150 }
3151 
3152 static int vlv_hrawclk(struct drm_i915_private *dev_priv)
3153 {
3154 	/* RAWCLK_FREQ_VLV register updated from power well code */
3155 	return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
3156 				      CCK_DISPLAY_REF_CLOCK_CONTROL);
3157 }
3158 
3159 static int i9xx_hrawclk(struct drm_i915_private *dev_priv)
3160 {
3161 	u32 clkcfg;
3162 
3163 	/*
3164 	 * hrawclock is 1/4 the FSB frequency
3165 	 *
3166 	 * Note that this only reads the state of the FSB
3167 	 * straps, not the actual FSB frequency. Some BIOSen
3168 	 * let you configure each independently. Ideally we'd
3169 	 * read out the actual FSB frequency but sadly we
3170 	 * don't know which registers have that information,
3171 	 * and all the relevant docs have gone to bit heaven :(
3172 	 */
3173 	clkcfg = intel_de_read(dev_priv, CLKCFG) & CLKCFG_FSB_MASK;
3174 
3175 	if (IS_MOBILE(dev_priv)) {
3176 		switch (clkcfg) {
3177 		case CLKCFG_FSB_400:
3178 			return 100000;
3179 		case CLKCFG_FSB_533:
3180 			return 133333;
3181 		case CLKCFG_FSB_667:
3182 			return 166667;
3183 		case CLKCFG_FSB_800:
3184 			return 200000;
3185 		case CLKCFG_FSB_1067:
3186 			return 266667;
3187 		case CLKCFG_FSB_1333:
3188 			return 333333;
3189 		default:
3190 			MISSING_CASE(clkcfg);
3191 			return 133333;
3192 		}
3193 	} else {
3194 		switch (clkcfg) {
3195 		case CLKCFG_FSB_400_ALT:
3196 			return 100000;
3197 		case CLKCFG_FSB_533:
3198 			return 133333;
3199 		case CLKCFG_FSB_667:
3200 			return 166667;
3201 		case CLKCFG_FSB_800:
3202 			return 200000;
3203 		case CLKCFG_FSB_1067_ALT:
3204 			return 266667;
3205 		case CLKCFG_FSB_1333_ALT:
3206 			return 333333;
3207 		case CLKCFG_FSB_1600_ALT:
3208 			return 400000;
3209 		default:
3210 			return 133333;
3211 		}
3212 	}
3213 }
3214 
3215 /**
3216  * intel_read_rawclk - Determine the current RAWCLK frequency
3217  * @dev_priv: i915 device
3218  *
3219  * Determine the current RAWCLK frequency. RAWCLK is a fixed
3220  * frequency clock so this needs to done only once.
3221  */
3222 u32 intel_read_rawclk(struct drm_i915_private *dev_priv)
3223 {
3224 	u32 freq;
3225 
3226 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_DG1)
3227 		freq = dg1_rawclk(dev_priv);
3228 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_MTP)
3229 		/*
3230 		 * MTL always uses a 38.4 MHz rawclk.  The bspec tells us
3231 		 * "RAWCLK_FREQ defaults to the values for 38.4 and does
3232 		 * not need to be programmed."
3233 		 */
3234 		freq = 38400;
3235 	else if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
3236 		freq = cnp_rawclk(dev_priv);
3237 	else if (HAS_PCH_SPLIT(dev_priv))
3238 		freq = pch_rawclk(dev_priv);
3239 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
3240 		freq = vlv_hrawclk(dev_priv);
3241 	else if (DISPLAY_VER(dev_priv) >= 3)
3242 		freq = i9xx_hrawclk(dev_priv);
3243 	else
3244 		/* no rawclk on other platforms, or no need to know it */
3245 		return 0;
3246 
3247 	return freq;
3248 }
3249 
3250 static const struct intel_cdclk_funcs mtl_cdclk_funcs = {
3251 	.get_cdclk = bxt_get_cdclk,
3252 	.set_cdclk = bxt_set_cdclk,
3253 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3254 	.calc_voltage_level = tgl_calc_voltage_level,
3255 };
3256 
3257 static const struct intel_cdclk_funcs rplu_cdclk_funcs = {
3258 	.get_cdclk = bxt_get_cdclk,
3259 	.set_cdclk = bxt_set_cdclk,
3260 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3261 	.calc_voltage_level = rplu_calc_voltage_level,
3262 };
3263 
3264 static const struct intel_cdclk_funcs tgl_cdclk_funcs = {
3265 	.get_cdclk = bxt_get_cdclk,
3266 	.set_cdclk = bxt_set_cdclk,
3267 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3268 	.calc_voltage_level = tgl_calc_voltage_level,
3269 };
3270 
3271 static const struct intel_cdclk_funcs ehl_cdclk_funcs = {
3272 	.get_cdclk = bxt_get_cdclk,
3273 	.set_cdclk = bxt_set_cdclk,
3274 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3275 	.calc_voltage_level = ehl_calc_voltage_level,
3276 };
3277 
3278 static const struct intel_cdclk_funcs icl_cdclk_funcs = {
3279 	.get_cdclk = bxt_get_cdclk,
3280 	.set_cdclk = bxt_set_cdclk,
3281 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3282 	.calc_voltage_level = icl_calc_voltage_level,
3283 };
3284 
3285 static const struct intel_cdclk_funcs bxt_cdclk_funcs = {
3286 	.get_cdclk = bxt_get_cdclk,
3287 	.set_cdclk = bxt_set_cdclk,
3288 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3289 	.calc_voltage_level = bxt_calc_voltage_level,
3290 };
3291 
3292 static const struct intel_cdclk_funcs skl_cdclk_funcs = {
3293 	.get_cdclk = skl_get_cdclk,
3294 	.set_cdclk = skl_set_cdclk,
3295 	.modeset_calc_cdclk = skl_modeset_calc_cdclk,
3296 };
3297 
3298 static const struct intel_cdclk_funcs bdw_cdclk_funcs = {
3299 	.get_cdclk = bdw_get_cdclk,
3300 	.set_cdclk = bdw_set_cdclk,
3301 	.modeset_calc_cdclk = bdw_modeset_calc_cdclk,
3302 };
3303 
3304 static const struct intel_cdclk_funcs chv_cdclk_funcs = {
3305 	.get_cdclk = vlv_get_cdclk,
3306 	.set_cdclk = chv_set_cdclk,
3307 	.modeset_calc_cdclk = vlv_modeset_calc_cdclk,
3308 };
3309 
3310 static const struct intel_cdclk_funcs vlv_cdclk_funcs = {
3311 	.get_cdclk = vlv_get_cdclk,
3312 	.set_cdclk = vlv_set_cdclk,
3313 	.modeset_calc_cdclk = vlv_modeset_calc_cdclk,
3314 };
3315 
3316 static const struct intel_cdclk_funcs hsw_cdclk_funcs = {
3317 	.get_cdclk = hsw_get_cdclk,
3318 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3319 };
3320 
3321 /* SNB, IVB, 965G, 945G */
3322 static const struct intel_cdclk_funcs fixed_400mhz_cdclk_funcs = {
3323 	.get_cdclk = fixed_400mhz_get_cdclk,
3324 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3325 };
3326 
3327 static const struct intel_cdclk_funcs ilk_cdclk_funcs = {
3328 	.get_cdclk = fixed_450mhz_get_cdclk,
3329 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3330 };
3331 
3332 static const struct intel_cdclk_funcs gm45_cdclk_funcs = {
3333 	.get_cdclk = gm45_get_cdclk,
3334 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3335 };
3336 
3337 /* G45 uses G33 */
3338 
3339 static const struct intel_cdclk_funcs i965gm_cdclk_funcs = {
3340 	.get_cdclk = i965gm_get_cdclk,
3341 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3342 };
3343 
3344 /* i965G uses fixed 400 */
3345 
3346 static const struct intel_cdclk_funcs pnv_cdclk_funcs = {
3347 	.get_cdclk = pnv_get_cdclk,
3348 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3349 };
3350 
3351 static const struct intel_cdclk_funcs g33_cdclk_funcs = {
3352 	.get_cdclk = g33_get_cdclk,
3353 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3354 };
3355 
3356 static const struct intel_cdclk_funcs i945gm_cdclk_funcs = {
3357 	.get_cdclk = i945gm_get_cdclk,
3358 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3359 };
3360 
3361 /* i945G uses fixed 400 */
3362 
3363 static const struct intel_cdclk_funcs i915gm_cdclk_funcs = {
3364 	.get_cdclk = i915gm_get_cdclk,
3365 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3366 };
3367 
3368 static const struct intel_cdclk_funcs i915g_cdclk_funcs = {
3369 	.get_cdclk = fixed_333mhz_get_cdclk,
3370 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3371 };
3372 
3373 static const struct intel_cdclk_funcs i865g_cdclk_funcs = {
3374 	.get_cdclk = fixed_266mhz_get_cdclk,
3375 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3376 };
3377 
3378 static const struct intel_cdclk_funcs i85x_cdclk_funcs = {
3379 	.get_cdclk = i85x_get_cdclk,
3380 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3381 };
3382 
3383 static const struct intel_cdclk_funcs i845g_cdclk_funcs = {
3384 	.get_cdclk = fixed_200mhz_get_cdclk,
3385 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3386 };
3387 
3388 static const struct intel_cdclk_funcs i830_cdclk_funcs = {
3389 	.get_cdclk = fixed_133mhz_get_cdclk,
3390 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
3391 };
3392 
3393 /**
3394  * intel_init_cdclk_hooks - Initialize CDCLK related modesetting hooks
3395  * @dev_priv: i915 device
3396  */
3397 void intel_init_cdclk_hooks(struct drm_i915_private *dev_priv)
3398 {
3399 	if (IS_METEORLAKE(dev_priv)) {
3400 		dev_priv->display.funcs.cdclk = &mtl_cdclk_funcs;
3401 		dev_priv->display.cdclk.table = mtl_cdclk_table;
3402 	} else if (IS_DG2(dev_priv)) {
3403 		dev_priv->display.funcs.cdclk = &tgl_cdclk_funcs;
3404 		dev_priv->display.cdclk.table = dg2_cdclk_table;
3405 	} else if (IS_ALDERLAKE_P(dev_priv)) {
3406 		/* Wa_22011320316:adl-p[a0] */
3407 		if (IS_ADLP_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B0)) {
3408 			dev_priv->display.cdclk.table = adlp_a_step_cdclk_table;
3409 			dev_priv->display.funcs.cdclk = &tgl_cdclk_funcs;
3410 		} else if (IS_ADLP_RPLU(dev_priv)) {
3411 			dev_priv->display.cdclk.table = rplu_cdclk_table;
3412 			dev_priv->display.funcs.cdclk = &rplu_cdclk_funcs;
3413 		} else {
3414 			dev_priv->display.cdclk.table = adlp_cdclk_table;
3415 			dev_priv->display.funcs.cdclk = &tgl_cdclk_funcs;
3416 		}
3417 	} else if (IS_ROCKETLAKE(dev_priv)) {
3418 		dev_priv->display.funcs.cdclk = &tgl_cdclk_funcs;
3419 		dev_priv->display.cdclk.table = rkl_cdclk_table;
3420 	} else if (DISPLAY_VER(dev_priv) >= 12) {
3421 		dev_priv->display.funcs.cdclk = &tgl_cdclk_funcs;
3422 		dev_priv->display.cdclk.table = icl_cdclk_table;
3423 	} else if (IS_JSL_EHL(dev_priv)) {
3424 		dev_priv->display.funcs.cdclk = &ehl_cdclk_funcs;
3425 		dev_priv->display.cdclk.table = icl_cdclk_table;
3426 	} else if (DISPLAY_VER(dev_priv) >= 11) {
3427 		dev_priv->display.funcs.cdclk = &icl_cdclk_funcs;
3428 		dev_priv->display.cdclk.table = icl_cdclk_table;
3429 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
3430 		dev_priv->display.funcs.cdclk = &bxt_cdclk_funcs;
3431 		if (IS_GEMINILAKE(dev_priv))
3432 			dev_priv->display.cdclk.table = glk_cdclk_table;
3433 		else
3434 			dev_priv->display.cdclk.table = bxt_cdclk_table;
3435 	} else if (DISPLAY_VER(dev_priv) == 9) {
3436 		dev_priv->display.funcs.cdclk = &skl_cdclk_funcs;
3437 	} else if (IS_BROADWELL(dev_priv)) {
3438 		dev_priv->display.funcs.cdclk = &bdw_cdclk_funcs;
3439 	} else if (IS_HASWELL(dev_priv)) {
3440 		dev_priv->display.funcs.cdclk = &hsw_cdclk_funcs;
3441 	} else if (IS_CHERRYVIEW(dev_priv)) {
3442 		dev_priv->display.funcs.cdclk = &chv_cdclk_funcs;
3443 	} else if (IS_VALLEYVIEW(dev_priv)) {
3444 		dev_priv->display.funcs.cdclk = &vlv_cdclk_funcs;
3445 	} else if (IS_SANDYBRIDGE(dev_priv) || IS_IVYBRIDGE(dev_priv)) {
3446 		dev_priv->display.funcs.cdclk = &fixed_400mhz_cdclk_funcs;
3447 	} else if (IS_IRONLAKE(dev_priv)) {
3448 		dev_priv->display.funcs.cdclk = &ilk_cdclk_funcs;
3449 	} else if (IS_GM45(dev_priv)) {
3450 		dev_priv->display.funcs.cdclk = &gm45_cdclk_funcs;
3451 	} else if (IS_G45(dev_priv)) {
3452 		dev_priv->display.funcs.cdclk = &g33_cdclk_funcs;
3453 	} else if (IS_I965GM(dev_priv)) {
3454 		dev_priv->display.funcs.cdclk = &i965gm_cdclk_funcs;
3455 	} else if (IS_I965G(dev_priv)) {
3456 		dev_priv->display.funcs.cdclk = &fixed_400mhz_cdclk_funcs;
3457 	} else if (IS_PINEVIEW(dev_priv)) {
3458 		dev_priv->display.funcs.cdclk = &pnv_cdclk_funcs;
3459 	} else if (IS_G33(dev_priv)) {
3460 		dev_priv->display.funcs.cdclk = &g33_cdclk_funcs;
3461 	} else if (IS_I945GM(dev_priv)) {
3462 		dev_priv->display.funcs.cdclk = &i945gm_cdclk_funcs;
3463 	} else if (IS_I945G(dev_priv)) {
3464 		dev_priv->display.funcs.cdclk = &fixed_400mhz_cdclk_funcs;
3465 	} else if (IS_I915GM(dev_priv)) {
3466 		dev_priv->display.funcs.cdclk = &i915gm_cdclk_funcs;
3467 	} else if (IS_I915G(dev_priv)) {
3468 		dev_priv->display.funcs.cdclk = &i915g_cdclk_funcs;
3469 	} else if (IS_I865G(dev_priv)) {
3470 		dev_priv->display.funcs.cdclk = &i865g_cdclk_funcs;
3471 	} else if (IS_I85X(dev_priv)) {
3472 		dev_priv->display.funcs.cdclk = &i85x_cdclk_funcs;
3473 	} else if (IS_I845G(dev_priv)) {
3474 		dev_priv->display.funcs.cdclk = &i845g_cdclk_funcs;
3475 	} else if (IS_I830(dev_priv)) {
3476 		dev_priv->display.funcs.cdclk = &i830_cdclk_funcs;
3477 	}
3478 
3479 	if (drm_WARN(&dev_priv->drm, !dev_priv->display.funcs.cdclk,
3480 		     "Unknown platform. Assuming i830\n"))
3481 		dev_priv->display.funcs.cdclk = &i830_cdclk_funcs;
3482 }
3483