xref: /linux/drivers/gpu/drm/i915/display/intel_cdclk.c (revision 9e7a9295c90e28e1bb6e88b76aaa91839266352b)
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/debugfs.h>
25 #include <linux/iopoll.h>
26 #include <linux/time.h>
27 
28 #include <drm/drm_fixed.h>
29 #include <drm/drm_print.h>
30 #include <drm/intel/intel_pcode_regs.h>
31 #include <drm/intel/pci_config.h>
32 #include <drm/intel/step.h>
33 
34 #include "hsw_ips.h"
35 #include "intel_atomic.h"
36 #include "intel_audio.h"
37 #include "intel_cdclk.h"
38 #include "intel_crtc.h"
39 #include "intel_dbuf_bw.h"
40 #include "intel_de.h"
41 #include "intel_display_regs.h"
42 #include "intel_display_types.h"
43 #include "intel_display_utils.h"
44 #include "intel_display_wa.h"
45 #include "intel_dram.h"
46 #include "intel_mchbar.h"
47 #include "intel_parent.h"
48 #include "intel_plane.h"
49 #include "intel_psr.h"
50 #include "intel_vdsc.h"
51 #include "skl_watermark.h"
52 #include "skl_watermark_regs.h"
53 #include "vlv_clock.h"
54 #include "vlv_dsi.h"
55 #include "vlv_sideband.h"
56 
57 /**
58  * DOC: CDCLK / RAWCLK
59  *
60  * The display engine uses several different clocks to do its work. There
61  * are two main clocks involved that aren't directly related to the actual
62  * pixel clock or any symbol/bit clock of the actual output port. These
63  * are the core display clock (CDCLK) and RAWCLK.
64  *
65  * CDCLK clocks most of the display pipe logic, and thus its frequency
66  * must be high enough to support the rate at which pixels are flowing
67  * through the pipes. Downscaling must also be accounted as that increases
68  * the effective pixel rate.
69  *
70  * On several platforms the CDCLK frequency can be changed dynamically
71  * to minimize power consumption for a given display configuration.
72  * Typically changes to the CDCLK frequency require all the display pipes
73  * to be shut down while the frequency is being changed.
74  *
75  * On SKL+ the DMC will toggle the CDCLK off/on during DC5/6 entry/exit.
76  * DMC will not change the active CDCLK frequency however, so that part
77  * will still be performed by the driver directly.
78  *
79  * There are multiple components involved in the generation of the CDCLK
80  * frequency:
81  *
82  * - We have the CDCLK PLL, which generates an output clock based on a
83  *   reference clock and a ratio parameter.
84  * - The CD2X Divider, which divides the output of the PLL based on a
85  *   divisor selected from a set of pre-defined choices.
86  * - The CD2X Squasher, which further divides the output based on a
87  *   waveform represented as a sequence of bits where each zero
88  *   "squashes out" a clock cycle.
89  * - And, finally, a fixed divider that divides the output frequency by 2.
90  *
91  * As such, the resulting CDCLK frequency can be calculated with the
92  * following formula:
93  *
94  *     cdclk = vco / cd2x_div / (sq_len / sq_div) / 2
95  *
96  * , where vco is the frequency generated by the PLL; cd2x_div
97  * represents the CD2X Divider; sq_len and sq_div are the bit length
98  * and the number of high bits for the CD2X Squasher waveform, respectively;
99  * and 2 represents the fixed divider.
100  *
101  * Note that some older platforms do not contain the CD2X Divider
102  * and/or CD2X Squasher, in which case we can ignore their respective
103  * factors in the formula above.
104  *
105  * Several methods exist to change the CDCLK frequency, which ones are
106  * supported depends on the platform:
107  *
108  * - Full PLL disable + re-enable with new VCO frequency. Pipes must be inactive.
109  * - CD2X divider update. Single pipe can be active as the divider update
110  *   can be synchronized with the pipe's start of vblank.
111  * - Crawl the PLL smoothly to the new VCO frequency. Pipes can be active.
112  * - Squash waveform update. Pipes can be active.
113  * - Crawl and squash can also be done back to back. Pipes can be active.
114  *
115  * RAWCLK is a fixed frequency clock, often used by various auxiliary
116  * blocks such as AUX CH or backlight PWM. Hence the only thing we
117  * really need to know about RAWCLK is its frequency so that various
118  * dividers can be programmed correctly.
119  */
120 
121 struct intel_cdclk_state {
122 	struct intel_global_state base;
123 
124 	/*
125 	 * Logical configuration of cdclk (used for all scaling,
126 	 * watermark, etc. calculations and checks). This is
127 	 * computed as if all enabled crtcs were active.
128 	 */
129 	struct intel_cdclk_config logical;
130 
131 	/*
132 	 * Actual configuration of cdclk, can be different from the
133 	 * logical configuration only when all crtc's are DPMS off.
134 	 */
135 	struct intel_cdclk_config actual;
136 
137 	/* minimum acceptable cdclk to satisfy DBUF bandwidth requirements */
138 	int dbuf_bw_min_cdclk;
139 	/* minimum acceptable cdclk for each pipe */
140 	int min_cdclk[I915_MAX_PIPES];
141 	/* minimum acceptable voltage level for each pipe */
142 	u8 min_voltage_level[I915_MAX_PIPES];
143 
144 	/* pipe to which cd2x update is synchronized */
145 	enum pipe pipe;
146 
147 	/* forced minimum cdclk for glk+ audio w/a */
148 	int force_min_cdclk;
149 
150 	/* bitmask of enabled pipes */
151 	u8 enabled_pipes;
152 
153 	/* bitmask of active pipes */
154 	u8 active_pipes;
155 
156 	/* update cdclk with pipes disabled */
157 	bool disable_pipes;
158 };
159 
160 struct intel_cdclk_funcs {
161 	void (*get_cdclk)(struct intel_display *display,
162 			  struct intel_cdclk_config *cdclk_config);
163 	void (*set_cdclk)(struct intel_display *display,
164 			  const struct intel_cdclk_config *cdclk_config,
165 			  enum pipe pipe);
166 	int (*pre_notify)(struct intel_display *display);
167 	int (*post_notify)(struct intel_display *display,
168 			   const struct intel_cdclk_config *cdclk_config);
169 	int (*modeset_calc_cdclk)(struct intel_atomic_state *state);
170 	u8 (*calc_voltage_level)(int cdclk);
171 };
172 
173 void intel_cdclk_get_cdclk(struct intel_display *display,
174 			   struct intel_cdclk_config *cdclk_config)
175 {
176 	display->cdclk.funcs->get_cdclk(display, cdclk_config);
177 }
178 
179 static int intel_cdclk_pre_notify(struct intel_display *display)
180 {
181 	int ret;
182 
183 	if (!display->cdclk.funcs->pre_notify)
184 		return 0;
185 
186 	ret = display->cdclk.funcs->pre_notify(display);
187 	if (ret)
188 		drm_err(display->drm,
189 			"Failed to inform system about start of CDCLK change (%d)\n", ret);
190 
191 	return ret;
192 }
193 
194 static void intel_cdclk_post_notify(struct intel_display *display,
195 				    const struct intel_cdclk_config *cdclk_config)
196 {
197 	int ret;
198 
199 	if (!display->cdclk.funcs->post_notify)
200 		return;
201 
202 	ret = display->cdclk.funcs->post_notify(display, cdclk_config);
203 	if (ret)
204 		drm_err(display->drm,
205 			"Failed to inform system about end of CDCLK change (%d)\n", ret);
206 }
207 
208 static void intel_cdclk_set_cdclk(struct intel_display *display,
209 				  const struct intel_cdclk_config *cdclk_config,
210 				  enum pipe pipe)
211 {
212 	if (intel_cdclk_pre_notify(display))
213 		return;
214 
215 	display->cdclk.funcs->set_cdclk(display, cdclk_config, pipe);
216 
217 	intel_cdclk_post_notify(display, cdclk_config);
218 }
219 
220 static int intel_cdclk_modeset_calc_cdclk(struct intel_atomic_state *state)
221 {
222 	struct intel_display *display = to_intel_display(state);
223 
224 	return display->cdclk.funcs->modeset_calc_cdclk(state);
225 }
226 
227 static u8 intel_cdclk_calc_voltage_level(struct intel_display *display,
228 					 int cdclk)
229 {
230 	return display->cdclk.funcs->calc_voltage_level(cdclk);
231 }
232 
233 static void fixed_133mhz_get_cdclk(struct intel_display *display,
234 				   struct intel_cdclk_config *cdclk_config)
235 {
236 	cdclk_config->cdclk = 133333;
237 }
238 
239 static void fixed_200mhz_get_cdclk(struct intel_display *display,
240 				   struct intel_cdclk_config *cdclk_config)
241 {
242 	cdclk_config->cdclk = 200000;
243 }
244 
245 static void fixed_266mhz_get_cdclk(struct intel_display *display,
246 				   struct intel_cdclk_config *cdclk_config)
247 {
248 	cdclk_config->cdclk = 266667;
249 }
250 
251 static void fixed_333mhz_get_cdclk(struct intel_display *display,
252 				   struct intel_cdclk_config *cdclk_config)
253 {
254 	cdclk_config->cdclk = 333333;
255 }
256 
257 static void fixed_400mhz_get_cdclk(struct intel_display *display,
258 				   struct intel_cdclk_config *cdclk_config)
259 {
260 	cdclk_config->cdclk = 400000;
261 }
262 
263 static void fixed_450mhz_get_cdclk(struct intel_display *display,
264 				   struct intel_cdclk_config *cdclk_config)
265 {
266 	cdclk_config->cdclk = 450000;
267 }
268 
269 static void i85x_get_cdclk(struct intel_display *display,
270 			   struct intel_cdclk_config *cdclk_config)
271 {
272 	struct pci_dev *pdev = to_pci_dev(display->drm->dev);
273 	u16 hpllcc = 0;
274 
275 	/*
276 	 * 852GM/852GMV only supports 133 MHz and the HPLLCC
277 	 * encoding is different :(
278 	 * FIXME is this the right way to detect 852GM/852GMV?
279 	 */
280 	if (pdev->revision == 0x1) {
281 		cdclk_config->cdclk = 133333;
282 		return;
283 	}
284 
285 	pci_bus_read_config_word(pdev->bus,
286 				 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
287 
288 	/* Assume that the hardware is in the high speed state.  This
289 	 * should be the default.
290 	 */
291 	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
292 	case GC_CLOCK_133_200:
293 	case GC_CLOCK_133_200_2:
294 	case GC_CLOCK_100_200:
295 		cdclk_config->cdclk = 200000;
296 		break;
297 	case GC_CLOCK_166_250:
298 		cdclk_config->cdclk = 250000;
299 		break;
300 	case GC_CLOCK_100_133:
301 		cdclk_config->cdclk = 133333;
302 		break;
303 	case GC_CLOCK_133_266:
304 	case GC_CLOCK_133_266_2:
305 	case GC_CLOCK_166_266:
306 		cdclk_config->cdclk = 266667;
307 		break;
308 	}
309 }
310 
311 static void i915gm_get_cdclk(struct intel_display *display,
312 			     struct intel_cdclk_config *cdclk_config)
313 {
314 	struct pci_dev *pdev = to_pci_dev(display->drm->dev);
315 	u16 gcfgc = 0;
316 
317 	pci_read_config_word(pdev, GCFGC, &gcfgc);
318 
319 	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
320 		cdclk_config->cdclk = 133333;
321 		return;
322 	}
323 
324 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
325 	case GC_DISPLAY_CLOCK_333_320_MHZ:
326 		cdclk_config->cdclk = 333333;
327 		break;
328 	default:
329 	case GC_DISPLAY_CLOCK_190_200_MHZ:
330 		cdclk_config->cdclk = 190000;
331 		break;
332 	}
333 }
334 
335 static void i945gm_get_cdclk(struct intel_display *display,
336 			     struct intel_cdclk_config *cdclk_config)
337 {
338 	struct pci_dev *pdev = to_pci_dev(display->drm->dev);
339 	u16 gcfgc = 0;
340 
341 	pci_read_config_word(pdev, GCFGC, &gcfgc);
342 
343 	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
344 		cdclk_config->cdclk = 133333;
345 		return;
346 	}
347 
348 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
349 	case GC_DISPLAY_CLOCK_333_320_MHZ:
350 		cdclk_config->cdclk = 320000;
351 		break;
352 	default:
353 	case GC_DISPLAY_CLOCK_190_200_MHZ:
354 		cdclk_config->cdclk = 200000;
355 		break;
356 	}
357 }
358 
359 static unsigned int intel_hpll_vco(struct intel_display *display)
360 {
361 	static const unsigned int blb_vco[8] = {
362 		[0] = 3200000,
363 		[1] = 4000000,
364 		[2] = 5333333,
365 		[3] = 4800000,
366 		[4] = 6400000,
367 	};
368 	static const unsigned int pnv_vco[8] = {
369 		[0] = 3200000,
370 		[1] = 4000000,
371 		[2] = 5333333,
372 		[3] = 4800000,
373 		[4] = 2666667,
374 	};
375 	static const unsigned int cl_vco[8] = {
376 		[0] = 3200000,
377 		[1] = 4000000,
378 		[2] = 5333333,
379 		[3] = 6400000,
380 		[4] = 3333333,
381 		[5] = 3566667,
382 		[6] = 4266667,
383 	};
384 	static const unsigned int elk_vco[8] = {
385 		[0] = 3200000,
386 		[1] = 4000000,
387 		[2] = 5333333,
388 		[3] = 4800000,
389 	};
390 	static const unsigned int ctg_vco[8] = {
391 		[0] = 3200000,
392 		[1] = 4000000,
393 		[2] = 5333333,
394 		[3] = 6400000,
395 		[4] = 2666667,
396 		[5] = 4266667,
397 	};
398 	const unsigned int *vco_table;
399 	unsigned int vco;
400 	u8 tmp = 0;
401 
402 	/* FIXME other chipsets? */
403 	if (display->platform.gm45)
404 		vco_table = ctg_vco;
405 	else if (display->platform.g45)
406 		vco_table = elk_vco;
407 	else if (display->platform.i965gm)
408 		vco_table = cl_vco;
409 	else if (display->platform.pineview)
410 		vco_table = pnv_vco;
411 	else if (display->platform.g33)
412 		vco_table = blb_vco;
413 	else
414 		return 0;
415 
416 	tmp = intel_mchbar_read(display, display->platform.pineview ||
417 				display->platform.mobile ? HPLLVCO_MOBILE : HPLLVCO);
418 
419 	vco = vco_table[tmp & 0x7];
420 	if (vco == 0)
421 		drm_err(display->drm, "Bad HPLL VCO (HPLLVCO=0x%02x)\n",
422 			tmp);
423 	else
424 		drm_dbg_kms(display->drm, "HPLL VCO %u kHz\n", vco);
425 
426 	return vco;
427 }
428 
429 static void g33_get_cdclk(struct intel_display *display,
430 			  struct intel_cdclk_config *cdclk_config)
431 {
432 	struct pci_dev *pdev = to_pci_dev(display->drm->dev);
433 	static const u8 div_3200[] = { 12, 10,  8,  7, 5, 16 };
434 	static const u8 div_4000[] = { 14, 12, 10,  8, 6, 20 };
435 	static const u8 div_4800[] = { 20, 14, 12, 10, 8, 24 };
436 	static const u8 div_5333[] = { 20, 16, 12, 12, 8, 28 };
437 	const u8 *div_table;
438 	unsigned int cdclk_sel;
439 	u16 tmp = 0;
440 
441 	cdclk_config->vco = intel_hpll_vco(display);
442 
443 	pci_read_config_word(pdev, GCFGC, &tmp);
444 
445 	cdclk_sel = (tmp >> 4) & 0x7;
446 
447 	if (cdclk_sel >= ARRAY_SIZE(div_3200))
448 		goto fail;
449 
450 	switch (cdclk_config->vco) {
451 	case 3200000:
452 		div_table = div_3200;
453 		break;
454 	case 4000000:
455 		div_table = div_4000;
456 		break;
457 	case 4800000:
458 		div_table = div_4800;
459 		break;
460 	case 5333333:
461 		div_table = div_5333;
462 		break;
463 	default:
464 		goto fail;
465 	}
466 
467 	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco,
468 						div_table[cdclk_sel]);
469 	return;
470 
471 fail:
472 	drm_err(display->drm,
473 		"Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n",
474 		cdclk_config->vco, tmp);
475 	cdclk_config->cdclk = 190476;
476 }
477 
478 static void pnv_get_cdclk(struct intel_display *display,
479 			  struct intel_cdclk_config *cdclk_config)
480 {
481 	struct pci_dev *pdev = to_pci_dev(display->drm->dev);
482 	u16 gcfgc = 0;
483 
484 	pci_read_config_word(pdev, GCFGC, &gcfgc);
485 
486 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
487 	case GC_DISPLAY_CLOCK_267_MHZ_PNV:
488 		cdclk_config->cdclk = 266667;
489 		break;
490 	case GC_DISPLAY_CLOCK_333_MHZ_PNV:
491 		cdclk_config->cdclk = 333333;
492 		break;
493 	case GC_DISPLAY_CLOCK_444_MHZ_PNV:
494 		cdclk_config->cdclk = 444444;
495 		break;
496 	case GC_DISPLAY_CLOCK_200_MHZ_PNV:
497 		cdclk_config->cdclk = 200000;
498 		break;
499 	default:
500 		drm_err(display->drm,
501 			"Unknown pnv display core clock 0x%04x\n", gcfgc);
502 		fallthrough;
503 	case GC_DISPLAY_CLOCK_133_MHZ_PNV:
504 		cdclk_config->cdclk = 133333;
505 		break;
506 	case GC_DISPLAY_CLOCK_167_MHZ_PNV:
507 		cdclk_config->cdclk = 166667;
508 		break;
509 	}
510 }
511 
512 static void i965gm_get_cdclk(struct intel_display *display,
513 			     struct intel_cdclk_config *cdclk_config)
514 {
515 	struct pci_dev *pdev = to_pci_dev(display->drm->dev);
516 	static const u8 div_3200[] = { 16, 10,  8 };
517 	static const u8 div_4000[] = { 20, 12, 10 };
518 	static const u8 div_5333[] = { 24, 16, 14 };
519 	const u8 *div_table;
520 	unsigned int cdclk_sel;
521 	u16 tmp = 0;
522 
523 	cdclk_config->vco = intel_hpll_vco(display);
524 
525 	pci_read_config_word(pdev, GCFGC, &tmp);
526 
527 	cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
528 
529 	if (cdclk_sel >= ARRAY_SIZE(div_3200))
530 		goto fail;
531 
532 	switch (cdclk_config->vco) {
533 	case 3200000:
534 		div_table = div_3200;
535 		break;
536 	case 4000000:
537 		div_table = div_4000;
538 		break;
539 	case 5333333:
540 		div_table = div_5333;
541 		break;
542 	default:
543 		goto fail;
544 	}
545 
546 	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco,
547 						div_table[cdclk_sel]);
548 	return;
549 
550 fail:
551 	drm_err(display->drm,
552 		"Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n",
553 		cdclk_config->vco, tmp);
554 	cdclk_config->cdclk = 200000;
555 }
556 
557 static void gm45_get_cdclk(struct intel_display *display,
558 			   struct intel_cdclk_config *cdclk_config)
559 {
560 	struct pci_dev *pdev = to_pci_dev(display->drm->dev);
561 	unsigned int cdclk_sel;
562 	u16 tmp = 0;
563 
564 	cdclk_config->vco = intel_hpll_vco(display);
565 
566 	pci_read_config_word(pdev, GCFGC, &tmp);
567 
568 	cdclk_sel = (tmp >> 12) & 0x1;
569 
570 	switch (cdclk_config->vco) {
571 	case 2666667:
572 	case 4000000:
573 	case 5333333:
574 		cdclk_config->cdclk = cdclk_sel ? 333333 : 222222;
575 		break;
576 	case 3200000:
577 		cdclk_config->cdclk = cdclk_sel ? 320000 : 228571;
578 		break;
579 	default:
580 		drm_err(display->drm,
581 			"Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n",
582 			cdclk_config->vco, tmp);
583 		cdclk_config->cdclk = 222222;
584 		break;
585 	}
586 }
587 
588 static void hsw_get_cdclk(struct intel_display *display,
589 			  struct intel_cdclk_config *cdclk_config)
590 {
591 	u32 lcpll = intel_de_read(display, LCPLL_CTL);
592 	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
593 
594 	if (lcpll & LCPLL_CD_SOURCE_FCLK)
595 		cdclk_config->cdclk = 800000;
596 	else if (intel_de_read(display, FUSE_STRAP) & HSW_CDCLK_LIMIT)
597 		cdclk_config->cdclk = 450000;
598 	else if (freq == LCPLL_CLK_FREQ_450)
599 		cdclk_config->cdclk = 450000;
600 	else if (display->platform.haswell_ult)
601 		cdclk_config->cdclk = 337500;
602 	else
603 		cdclk_config->cdclk = 540000;
604 }
605 
606 static int vlv_calc_cdclk(struct intel_display *display, int min_cdclk)
607 {
608 	int freq_320 = (vlv_clock_get_hpll_vco(display->drm) <<  1) % 320000 != 0 ?
609 		333333 : 320000;
610 
611 	/*
612 	 * We seem to get an unstable or solid color picture at 200MHz.
613 	 * Not sure what's wrong. For now use 200MHz only when all pipes
614 	 * are off.
615 	 */
616 	if (display->platform.valleyview && min_cdclk > freq_320)
617 		return 400000;
618 	else if (min_cdclk > 266667)
619 		return freq_320;
620 	else if (min_cdclk > 0)
621 		return 266667;
622 	else
623 		return 200000;
624 }
625 
626 static u8 vlv_calc_voltage_level(struct intel_display *display, int cdclk)
627 {
628 	if (display->platform.valleyview) {
629 		if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
630 			return 2;
631 		else if (cdclk >= 266667)
632 			return 1;
633 		else
634 			return 0;
635 	} else {
636 		/*
637 		 * Specs are full of misinformation, but testing on actual
638 		 * hardware has shown that we just need to write the desired
639 		 * CCK divider into the Punit register.
640 		 */
641 		return DIV_ROUND_CLOSEST(vlv_clock_get_hpll_vco(display->drm) << 1, cdclk) - 1;
642 	}
643 }
644 
645 static void vlv_get_cdclk(struct intel_display *display,
646 			  struct intel_cdclk_config *cdclk_config)
647 {
648 	u32 val;
649 
650 	cdclk_config->vco = vlv_clock_get_hpll_vco(display->drm);
651 	cdclk_config->cdclk = vlv_clock_get_cdclk(display->drm);
652 
653 	vlv_punit_get(display);
654 	val = vlv_punit_read(display, PUNIT_REG_DSPSSPM);
655 	vlv_punit_put(display);
656 
657 	if (display->platform.valleyview)
658 		cdclk_config->voltage_level = (val & DSPFREQGUAR_MASK) >>
659 			DSPFREQGUAR_SHIFT;
660 	else
661 		cdclk_config->voltage_level = (val & DSPFREQGUAR_MASK_CHV) >>
662 			DSPFREQGUAR_SHIFT_CHV;
663 }
664 
665 static void vlv_program_pfi_credits(struct intel_display *display)
666 {
667 	unsigned int credits, default_credits;
668 
669 	if (display->platform.cherryview)
670 		default_credits = PFI_CREDIT(12);
671 	else
672 		default_credits = PFI_CREDIT(8);
673 
674 	if (display->cdclk.hw.cdclk >= vlv_clock_get_czclk(display->drm)) {
675 		/* CHV suggested value is 31 or 63 */
676 		if (display->platform.cherryview)
677 			credits = PFI_CREDIT_63;
678 		else
679 			credits = PFI_CREDIT(15);
680 	} else {
681 		credits = default_credits;
682 	}
683 
684 	/*
685 	 * WA - write default credits before re-programming
686 	 * FIXME: should we also set the resend bit here?
687 	 */
688 	intel_de_write(display, GCI_CONTROL,
689 		       VGA_FAST_MODE_DISABLE | default_credits);
690 
691 	intel_de_write(display, GCI_CONTROL,
692 		       VGA_FAST_MODE_DISABLE | credits | PFI_CREDIT_RESEND);
693 
694 	/*
695 	 * FIXME is this guaranteed to clear
696 	 * immediately or should we poll for it?
697 	 */
698 	drm_WARN_ON(display->drm,
699 		    intel_de_read(display, GCI_CONTROL) & PFI_CREDIT_RESEND);
700 }
701 
702 static void vlv_set_cdclk(struct intel_display *display,
703 			  const struct intel_cdclk_config *cdclk_config,
704 			  enum pipe pipe)
705 {
706 	int cdclk = cdclk_config->cdclk;
707 	u32 val, cmd = cdclk_config->voltage_level;
708 	struct ref_tracker *wakeref;
709 	int ret;
710 
711 	switch (cdclk) {
712 	case 400000:
713 	case 333333:
714 	case 320000:
715 	case 266667:
716 	case 200000:
717 		break;
718 	default:
719 		MISSING_CASE(cdclk);
720 		return;
721 	}
722 
723 	/* There are cases where we can end up here with power domains
724 	 * off and a CDCLK frequency other than the minimum, like when
725 	 * issuing a modeset without actually changing any display after
726 	 * a system suspend.  So grab the display core domain, which covers
727 	 * the HW blocks needed for the following programming.
728 	 */
729 	wakeref = intel_display_power_get(display, POWER_DOMAIN_DISPLAY_CORE);
730 
731 	intel_parent_vlv_iosf_get(display,
732 				  BIT(VLV_IOSF_SB_CCK) |
733 				  BIT(VLV_IOSF_SB_BUNIT) |
734 				  BIT(VLV_IOSF_SB_PUNIT));
735 
736 	val = vlv_punit_read(display, PUNIT_REG_DSPSSPM);
737 	val &= ~DSPFREQGUAR_MASK;
738 	val |= (cmd << DSPFREQGUAR_SHIFT);
739 	vlv_punit_write(display, PUNIT_REG_DSPSSPM, val);
740 
741 	ret = poll_timeout_us(val = vlv_punit_read(display, PUNIT_REG_DSPSSPM),
742 			      (val & DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
743 			      500, 50 * 1000, false);
744 	if (ret)
745 		drm_err(display->drm, "timed out waiting for CDCLK change\n");
746 
747 	if (cdclk == 400000) {
748 		u32 divider;
749 
750 		divider = DIV_ROUND_CLOSEST(vlv_clock_get_hpll_vco(display->drm) << 1,
751 					    cdclk) - 1;
752 
753 		/* adjust cdclk divider */
754 		val = vlv_cck_read(display, CCK_DISPLAY_CLOCK_CONTROL);
755 		val &= ~CCK_FREQUENCY_VALUES;
756 		val |= divider;
757 		vlv_cck_write(display, CCK_DISPLAY_CLOCK_CONTROL, val);
758 
759 		ret = poll_timeout_us(val = vlv_cck_read(display, CCK_DISPLAY_CLOCK_CONTROL),
760 				      (val & CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
761 				      500, 50 * 1000, false);
762 		if (ret)
763 			drm_err(display->drm, "timed out waiting for CDCLK change\n");
764 	}
765 
766 	/* adjust self-refresh exit latency value */
767 	val = vlv_bunit_read(display, BUNIT_REG_BISOC);
768 	val &= ~0x7f;
769 
770 	/*
771 	 * For high bandwidth configs, we set a higher latency in the bunit
772 	 * so that the core display fetch happens in time to avoid underruns.
773 	 */
774 	if (cdclk == 400000)
775 		val |= 4500 / 250; /* 4.5 usec */
776 	else
777 		val |= 3000 / 250; /* 3.0 usec */
778 	vlv_bunit_write(display, BUNIT_REG_BISOC, val);
779 
780 	intel_parent_vlv_iosf_put(display,
781 				  BIT(VLV_IOSF_SB_CCK) |
782 				  BIT(VLV_IOSF_SB_BUNIT) |
783 				  BIT(VLV_IOSF_SB_PUNIT));
784 
785 	intel_update_cdclk(display);
786 
787 	vlv_program_pfi_credits(display);
788 
789 	intel_display_power_put(display, POWER_DOMAIN_DISPLAY_CORE, wakeref);
790 }
791 
792 static void chv_set_cdclk(struct intel_display *display,
793 			  const struct intel_cdclk_config *cdclk_config,
794 			  enum pipe pipe)
795 {
796 	int cdclk = cdclk_config->cdclk;
797 	u32 val, cmd = cdclk_config->voltage_level;
798 	struct ref_tracker *wakeref;
799 	int ret;
800 
801 	switch (cdclk) {
802 	case 333333:
803 	case 320000:
804 	case 266667:
805 	case 200000:
806 		break;
807 	default:
808 		MISSING_CASE(cdclk);
809 		return;
810 	}
811 
812 	/* There are cases where we can end up here with power domains
813 	 * off and a CDCLK frequency other than the minimum, like when
814 	 * issuing a modeset without actually changing any display after
815 	 * a system suspend.  So grab the display core domain, which covers
816 	 * the HW blocks needed for the following programming.
817 	 */
818 	wakeref = intel_display_power_get(display, POWER_DOMAIN_DISPLAY_CORE);
819 
820 	vlv_punit_get(display);
821 	val = vlv_punit_read(display, PUNIT_REG_DSPSSPM);
822 	val &= ~DSPFREQGUAR_MASK_CHV;
823 	val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
824 	vlv_punit_write(display, PUNIT_REG_DSPSSPM, val);
825 
826 	ret = poll_timeout_us(val = vlv_punit_read(display, PUNIT_REG_DSPSSPM),
827 			      (val & DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
828 			      500, 50 * 1000, false);
829 	if (ret)
830 		drm_err(display->drm, "timed out waiting for CDCLK change\n");
831 
832 	vlv_punit_put(display);
833 
834 	intel_update_cdclk(display);
835 
836 	vlv_program_pfi_credits(display);
837 
838 	intel_display_power_put(display, POWER_DOMAIN_DISPLAY_CORE, wakeref);
839 }
840 
841 static int bdw_calc_cdclk(int min_cdclk)
842 {
843 	if (min_cdclk > 540000)
844 		return 675000;
845 	else if (min_cdclk > 450000)
846 		return 540000;
847 	else if (min_cdclk > 337500)
848 		return 450000;
849 	else
850 		return 337500;
851 }
852 
853 static u8 bdw_calc_voltage_level(int cdclk)
854 {
855 	switch (cdclk) {
856 	default:
857 	case 337500:
858 		return 2;
859 	case 450000:
860 		return 0;
861 	case 540000:
862 		return 1;
863 	case 675000:
864 		return 3;
865 	}
866 }
867 
868 static void bdw_get_cdclk(struct intel_display *display,
869 			  struct intel_cdclk_config *cdclk_config)
870 {
871 	u32 lcpll = intel_de_read(display, LCPLL_CTL);
872 	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
873 
874 	if (lcpll & LCPLL_CD_SOURCE_FCLK)
875 		cdclk_config->cdclk = 800000;
876 	else if (intel_de_read(display, FUSE_STRAP) & HSW_CDCLK_LIMIT)
877 		cdclk_config->cdclk = 450000;
878 	else if (freq == LCPLL_CLK_FREQ_450)
879 		cdclk_config->cdclk = 450000;
880 	else if (freq == LCPLL_CLK_FREQ_54O_BDW)
881 		cdclk_config->cdclk = 540000;
882 	else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
883 		cdclk_config->cdclk = 337500;
884 	else
885 		cdclk_config->cdclk = 675000;
886 
887 	/*
888 	 * Can't read this out :( Let's assume it's
889 	 * at least what the CDCLK frequency requires.
890 	 */
891 	cdclk_config->voltage_level =
892 		bdw_calc_voltage_level(cdclk_config->cdclk);
893 }
894 
895 static u32 bdw_cdclk_freq_sel(int cdclk)
896 {
897 	switch (cdclk) {
898 	default:
899 		MISSING_CASE(cdclk);
900 		fallthrough;
901 	case 337500:
902 		return LCPLL_CLK_FREQ_337_5_BDW;
903 	case 450000:
904 		return LCPLL_CLK_FREQ_450;
905 	case 540000:
906 		return LCPLL_CLK_FREQ_54O_BDW;
907 	case 675000:
908 		return LCPLL_CLK_FREQ_675_BDW;
909 	}
910 }
911 
912 static int bdw_cdclk_pcode_pre_notify(struct intel_display *display)
913 {
914 	return intel_parent_pcode_write(display, BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ,
915 					0x0);
916 }
917 
918 static int bdw_cdclk_pcode_post_notify(struct intel_display *display,
919 				       const struct intel_cdclk_config *cdclk_config)
920 {
921 	return intel_parent_pcode_write(display, HSW_PCODE_DE_WRITE_FREQ_REQ,
922 					cdclk_config->voltage_level);
923 }
924 
925 static void bdw_set_cdclk(struct intel_display *display,
926 			  const struct intel_cdclk_config *cdclk_config,
927 			  enum pipe pipe)
928 {
929 	int cdclk = cdclk_config->cdclk;
930 	int ret;
931 
932 	if (drm_WARN(display->drm,
933 		     (intel_de_read(display, LCPLL_CTL) &
934 		      (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
935 		       LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
936 		       LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
937 		       LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
938 		     "trying to change cdclk frequency with cdclk not enabled\n"))
939 		return;
940 
941 	intel_de_rmw(display, LCPLL_CTL,
942 		     0, LCPLL_CD_SOURCE_FCLK);
943 
944 	/*
945 	 * According to the spec, it should be enough to poll for this 1 us.
946 	 * However, extensive testing shows that this can take longer.
947 	 */
948 	ret = intel_de_wait_for_set_us(display, LCPLL_CTL,
949 				       LCPLL_CD_SOURCE_FCLK_DONE, 100);
950 	if (ret)
951 		drm_err(display->drm, "Switching to FCLK failed\n");
952 
953 	intel_de_rmw(display, LCPLL_CTL,
954 		     LCPLL_CLK_FREQ_MASK, bdw_cdclk_freq_sel(cdclk));
955 
956 	intel_de_rmw(display, LCPLL_CTL,
957 		     LCPLL_CD_SOURCE_FCLK, 0);
958 
959 	ret = intel_de_wait_for_clear_us(display, LCPLL_CTL,
960 					 LCPLL_CD_SOURCE_FCLK_DONE, 1);
961 	if (ret)
962 		drm_err(display->drm, "Switching back to LCPLL failed\n");
963 
964 	intel_de_write(display, CDCLK_FREQ,
965 		       DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
966 
967 	intel_update_cdclk(display);
968 }
969 
970 static int skl_calc_cdclk(int min_cdclk, int vco)
971 {
972 	if (vco == 8640000) {
973 		if (min_cdclk > 540000)
974 			return 617143;
975 		else if (min_cdclk > 432000)
976 			return 540000;
977 		else if (min_cdclk > 308571)
978 			return 432000;
979 		else
980 			return 308571;
981 	} else {
982 		if (min_cdclk > 540000)
983 			return 675000;
984 		else if (min_cdclk > 450000)
985 			return 540000;
986 		else if (min_cdclk > 337500)
987 			return 450000;
988 		else
989 			return 337500;
990 	}
991 }
992 
993 static u8 skl_calc_voltage_level(int cdclk)
994 {
995 	if (cdclk > 540000)
996 		return 3;
997 	else if (cdclk > 450000)
998 		return 2;
999 	else if (cdclk > 337500)
1000 		return 1;
1001 	else
1002 		return 0;
1003 }
1004 
1005 static void skl_dpll0_update(struct intel_display *display,
1006 			     struct intel_cdclk_config *cdclk_config)
1007 {
1008 	u32 val;
1009 
1010 	cdclk_config->ref = 24000;
1011 	cdclk_config->vco = 0;
1012 
1013 	val = intel_de_read(display, LCPLL1_CTL);
1014 	if ((val & LCPLL_PLL_ENABLE) == 0)
1015 		return;
1016 
1017 	if (drm_WARN_ON(display->drm, (val & LCPLL_PLL_LOCK) == 0))
1018 		return;
1019 
1020 	val = intel_de_read(display, DPLL_CTRL1);
1021 
1022 	if (drm_WARN_ON(display->drm,
1023 			(val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
1024 				DPLL_CTRL1_SSC(SKL_DPLL0) |
1025 				DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
1026 			DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
1027 		return;
1028 
1029 	switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
1030 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
1031 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
1032 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
1033 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
1034 		cdclk_config->vco = 8100000;
1035 		break;
1036 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
1037 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
1038 		cdclk_config->vco = 8640000;
1039 		break;
1040 	default:
1041 		MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
1042 		break;
1043 	}
1044 }
1045 
1046 static void skl_get_cdclk(struct intel_display *display,
1047 			  struct intel_cdclk_config *cdclk_config)
1048 {
1049 	u32 cdctl;
1050 
1051 	skl_dpll0_update(display, cdclk_config);
1052 
1053 	cdclk_config->cdclk = cdclk_config->bypass = cdclk_config->ref;
1054 
1055 	if (cdclk_config->vco == 0)
1056 		goto out;
1057 
1058 	cdctl = intel_de_read(display, CDCLK_CTL);
1059 
1060 	if (cdclk_config->vco == 8640000) {
1061 		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
1062 		case CDCLK_FREQ_450_432:
1063 			cdclk_config->cdclk = 432000;
1064 			break;
1065 		case CDCLK_FREQ_337_308:
1066 			cdclk_config->cdclk = 308571;
1067 			break;
1068 		case CDCLK_FREQ_540:
1069 			cdclk_config->cdclk = 540000;
1070 			break;
1071 		case CDCLK_FREQ_675_617:
1072 			cdclk_config->cdclk = 617143;
1073 			break;
1074 		default:
1075 			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1076 			break;
1077 		}
1078 	} else {
1079 		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
1080 		case CDCLK_FREQ_450_432:
1081 			cdclk_config->cdclk = 450000;
1082 			break;
1083 		case CDCLK_FREQ_337_308:
1084 			cdclk_config->cdclk = 337500;
1085 			break;
1086 		case CDCLK_FREQ_540:
1087 			cdclk_config->cdclk = 540000;
1088 			break;
1089 		case CDCLK_FREQ_675_617:
1090 			cdclk_config->cdclk = 675000;
1091 			break;
1092 		default:
1093 			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
1094 			break;
1095 		}
1096 	}
1097 
1098  out:
1099 	/*
1100 	 * Can't read this out :( Let's assume it's
1101 	 * at least what the CDCLK frequency requires.
1102 	 */
1103 	cdclk_config->voltage_level =
1104 		skl_calc_voltage_level(cdclk_config->cdclk);
1105 }
1106 
1107 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
1108 static int skl_cdclk_decimal(int cdclk)
1109 {
1110 	return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
1111 }
1112 
1113 static void skl_set_preferred_cdclk_vco(struct intel_display *display, int vco)
1114 {
1115 	bool changed = display->cdclk.skl_preferred_vco_freq != vco;
1116 
1117 	display->cdclk.skl_preferred_vco_freq = vco;
1118 
1119 	if (changed)
1120 		intel_update_max_cdclk(display);
1121 }
1122 
1123 static u32 skl_dpll0_link_rate(struct intel_display *display, int vco)
1124 {
1125 	drm_WARN_ON(display->drm, vco != 8100000 && vco != 8640000);
1126 
1127 	/*
1128 	 * We always enable DPLL0 with the lowest link rate possible, but still
1129 	 * taking into account the VCO required to operate the eDP panel at the
1130 	 * desired frequency. The usual DP link rates operate with a VCO of
1131 	 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
1132 	 * The modeset code is responsible for the selection of the exact link
1133 	 * rate later on, with the constraint of choosing a frequency that
1134 	 * works with vco.
1135 	 */
1136 	if (vco == 8640000)
1137 		return DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0);
1138 	else
1139 		return DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0);
1140 }
1141 
1142 static void skl_dpll0_enable(struct intel_display *display, int vco)
1143 {
1144 	intel_de_rmw(display, DPLL_CTRL1,
1145 		     DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
1146 		     DPLL_CTRL1_SSC(SKL_DPLL0) |
1147 		     DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0),
1148 		     DPLL_CTRL1_OVERRIDE(SKL_DPLL0) |
1149 		     skl_dpll0_link_rate(display, vco));
1150 	intel_de_posting_read(display, DPLL_CTRL1);
1151 
1152 	intel_de_rmw(display, LCPLL1_CTL,
1153 		     0, LCPLL_PLL_ENABLE);
1154 
1155 	if (intel_de_wait_for_set_ms(display, LCPLL1_CTL, LCPLL_PLL_LOCK, 5))
1156 		drm_err(display->drm, "DPLL0 not locked\n");
1157 
1158 	display->cdclk.hw.vco = vco;
1159 
1160 	/* We'll want to keep using the current vco from now on. */
1161 	skl_set_preferred_cdclk_vco(display, vco);
1162 }
1163 
1164 static void skl_dpll0_disable(struct intel_display *display)
1165 {
1166 	intel_de_rmw(display, LCPLL1_CTL,
1167 		     LCPLL_PLL_ENABLE, 0);
1168 
1169 	if (intel_de_wait_for_clear_ms(display, LCPLL1_CTL, LCPLL_PLL_LOCK, 1))
1170 		drm_err(display->drm, "Couldn't disable DPLL0\n");
1171 
1172 	display->cdclk.hw.vco = 0;
1173 }
1174 
1175 static u32 skl_cdclk_freq_sel(struct intel_display *display,
1176 			      int cdclk, int vco)
1177 {
1178 	switch (cdclk) {
1179 	default:
1180 		drm_WARN_ON(display->drm,
1181 			    cdclk != display->cdclk.hw.bypass);
1182 		drm_WARN_ON(display->drm, vco != 0);
1183 		fallthrough;
1184 	case 308571:
1185 	case 337500:
1186 		return CDCLK_FREQ_337_308;
1187 	case 450000:
1188 	case 432000:
1189 		return CDCLK_FREQ_450_432;
1190 	case 540000:
1191 		return CDCLK_FREQ_540;
1192 	case 617143:
1193 	case 675000:
1194 		return CDCLK_FREQ_675_617;
1195 	}
1196 }
1197 
1198 static int skl_cdclk_pcode_pre_notify(struct intel_display *display)
1199 {
1200 	return intel_parent_pcode_request(display, SKL_PCODE_CDCLK_CONTROL,
1201 					  SKL_CDCLK_PREPARE_FOR_CHANGE,
1202 					  SKL_CDCLK_READY_FOR_CHANGE,
1203 					  SKL_CDCLK_READY_FOR_CHANGE, 3);
1204 }
1205 
1206 static int skl_cdclk_pcode_post_notify(struct intel_display *display,
1207 				       const struct intel_cdclk_config *cdclk_config)
1208 {
1209 	return intel_parent_pcode_write(display, SKL_PCODE_CDCLK_CONTROL,
1210 					cdclk_config->voltage_level);
1211 }
1212 
1213 static void skl_set_cdclk(struct intel_display *display,
1214 			  const struct intel_cdclk_config *cdclk_config,
1215 			  enum pipe pipe)
1216 {
1217 	int cdclk = cdclk_config->cdclk;
1218 	int vco = cdclk_config->vco;
1219 	u32 freq_select, cdclk_ctl;
1220 
1221 	/*
1222 	 * Based on WA#1183 CDCLK rates 308 and 617MHz CDCLK rates are
1223 	 * unsupported on SKL. In theory this should never happen since only
1224 	 * the eDP1.4 2.16 and 4.32Gbps rates require it, but eDP1.4 is not
1225 	 * supported on SKL either, see the above WA. WARN whenever trying to
1226 	 * use the corresponding VCO freq as that always leads to using the
1227 	 * minimum 308MHz CDCLK.
1228 	 */
1229 	drm_WARN_ON_ONCE(display->drm,
1230 			 display->platform.skylake && vco == 8640000);
1231 
1232 	freq_select = skl_cdclk_freq_sel(display, cdclk, vco);
1233 
1234 	if (display->cdclk.hw.vco != 0 &&
1235 	    display->cdclk.hw.vco != vco)
1236 		skl_dpll0_disable(display);
1237 
1238 	cdclk_ctl = intel_de_read(display, CDCLK_CTL);
1239 
1240 	if (display->cdclk.hw.vco != vco) {
1241 		/* Wa Display #1183: skl,kbl,cfl */
1242 		cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
1243 		cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
1244 		intel_de_write(display, CDCLK_CTL, cdclk_ctl);
1245 	}
1246 
1247 	/* Wa Display #1183: skl,kbl,cfl */
1248 	cdclk_ctl |= CDCLK_DIVMUX_CD_OVERRIDE;
1249 	intel_de_write(display, CDCLK_CTL, cdclk_ctl);
1250 	intel_de_posting_read(display, CDCLK_CTL);
1251 
1252 	if (display->cdclk.hw.vco != vco)
1253 		skl_dpll0_enable(display, vco);
1254 
1255 	/* Wa Display #1183: skl,kbl,cfl */
1256 	cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
1257 	intel_de_write(display, CDCLK_CTL, cdclk_ctl);
1258 
1259 	cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
1260 	intel_de_write(display, CDCLK_CTL, cdclk_ctl);
1261 
1262 	/* Wa Display #1183: skl,kbl,cfl */
1263 	cdclk_ctl &= ~CDCLK_DIVMUX_CD_OVERRIDE;
1264 	intel_de_write(display, CDCLK_CTL, cdclk_ctl);
1265 	intel_de_posting_read(display, CDCLK_CTL);
1266 
1267 	intel_update_cdclk(display);
1268 }
1269 
1270 static void skl_sanitize_cdclk(struct intel_display *display)
1271 {
1272 	u32 cdctl, expected;
1273 
1274 	intel_update_cdclk(display);
1275 	intel_cdclk_dump_config(display, &display->cdclk.hw, "Current CDCLK");
1276 
1277 	/* Is PLL enabled and locked ? */
1278 	if (display->cdclk.hw.vco == 0 ||
1279 	    display->cdclk.hw.cdclk == display->cdclk.hw.bypass) {
1280 		drm_dbg_kms(display->drm, "Sanitizing CDCLK due to PLL not enabled/locked\n");
1281 		goto sanitize;
1282 	}
1283 
1284 	/* DPLL okay; verify the cdclock
1285 	 *
1286 	 * Noticed in some instances that the freq selection is correct but
1287 	 * decimal part is programmed wrong from BIOS where pre-os does not
1288 	 * enable display. Verify the same as well.
1289 	 */
1290 	cdctl = intel_de_read(display, CDCLK_CTL);
1291 	expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
1292 		skl_cdclk_decimal(display->cdclk.hw.cdclk);
1293 
1294 	if (cdctl != expected) {
1295 		cdctl &= ~CDCLK_FREQ_DECIMAL_MASK;
1296 		cdctl |= expected & CDCLK_FREQ_DECIMAL_MASK;
1297 
1298 		if (cdctl != expected) {
1299 			drm_dbg_kms(display->drm, "Sanitizing CDCLK due to CDCLK_CTL 0x%x, expected 0x%x)\n",
1300 				    intel_de_read(display, CDCLK_CTL), expected);
1301 			goto sanitize;
1302 		}
1303 
1304 		drm_dbg_kms(display->drm, "Sanitizing CDCLK decimal divider (CDCLK_CTL 0x%x, expected 0x%x)\n",
1305 			    intel_de_read(display, CDCLK_CTL), expected);
1306 
1307 		intel_de_write(display, CDCLK_CTL, expected);
1308 	}
1309 
1310 	/* All well; nothing to sanitize */
1311 	return;
1312 
1313 sanitize:
1314 	/* force cdclk programming */
1315 	display->cdclk.hw.cdclk = 0;
1316 	/* force full PLL disable + enable */
1317 	display->cdclk.hw.vco = ~0;
1318 }
1319 
1320 static void skl_cdclk_init_hw(struct intel_display *display)
1321 {
1322 	struct intel_cdclk_config cdclk_config;
1323 
1324 	skl_sanitize_cdclk(display);
1325 
1326 	if (display->cdclk.hw.cdclk != 0 &&
1327 	    display->cdclk.hw.vco != 0) {
1328 		/*
1329 		 * Use the current vco as our initial
1330 		 * guess as to what the preferred vco is.
1331 		 */
1332 		if (display->cdclk.skl_preferred_vco_freq == 0)
1333 			skl_set_preferred_cdclk_vco(display,
1334 						    display->cdclk.hw.vco);
1335 		return;
1336 	}
1337 
1338 	cdclk_config = display->cdclk.hw;
1339 
1340 	cdclk_config.vco = display->cdclk.skl_preferred_vco_freq;
1341 	if (cdclk_config.vco == 0)
1342 		cdclk_config.vco = 8100000;
1343 	cdclk_config.cdclk = skl_calc_cdclk(0, cdclk_config.vco);
1344 	cdclk_config.voltage_level = skl_calc_voltage_level(cdclk_config.cdclk);
1345 
1346 	intel_cdclk_set_cdclk(display, &cdclk_config, INVALID_PIPE);
1347 }
1348 
1349 static void skl_cdclk_uninit_hw(struct intel_display *display)
1350 {
1351 	struct intel_cdclk_config cdclk_config = display->cdclk.hw;
1352 
1353 	cdclk_config.cdclk = cdclk_config.bypass;
1354 	cdclk_config.vco = 0;
1355 	cdclk_config.voltage_level = skl_calc_voltage_level(cdclk_config.cdclk);
1356 
1357 	intel_cdclk_set_cdclk(display, &cdclk_config, INVALID_PIPE);
1358 }
1359 
1360 struct intel_cdclk_vals {
1361 	u32 cdclk;
1362 	u16 refclk;
1363 	u16 waveform;
1364 	u8 ratio;
1365 };
1366 
1367 static const struct intel_cdclk_vals bxt_cdclk_table[] = {
1368 	{ .refclk = 19200, .cdclk = 144000, .ratio = 60 },
1369 	{ .refclk = 19200, .cdclk = 288000, .ratio = 60 },
1370 	{ .refclk = 19200, .cdclk = 384000, .ratio = 60 },
1371 	{ .refclk = 19200, .cdclk = 576000, .ratio = 60 },
1372 	{ .refclk = 19200, .cdclk = 624000, .ratio = 65 },
1373 	{}
1374 };
1375 
1376 static const struct intel_cdclk_vals glk_cdclk_table[] = {
1377 	{ .refclk = 19200, .cdclk =  79200, .ratio = 33 },
1378 	{ .refclk = 19200, .cdclk = 158400, .ratio = 33 },
1379 	{ .refclk = 19200, .cdclk = 316800, .ratio = 33 },
1380 	{}
1381 };
1382 
1383 static const struct intel_cdclk_vals icl_cdclk_table[] = {
1384 	{ .refclk = 19200, .cdclk = 172800, .ratio = 18 },
1385 	{ .refclk = 19200, .cdclk = 192000, .ratio = 20 },
1386 	{ .refclk = 19200, .cdclk = 307200, .ratio = 32 },
1387 	{ .refclk = 19200, .cdclk = 326400, .ratio = 68 },
1388 	{ .refclk = 19200, .cdclk = 556800, .ratio = 58 },
1389 	{ .refclk = 19200, .cdclk = 652800, .ratio = 68 },
1390 
1391 	{ .refclk = 24000, .cdclk = 180000, .ratio = 15 },
1392 	{ .refclk = 24000, .cdclk = 192000, .ratio = 16 },
1393 	{ .refclk = 24000, .cdclk = 312000, .ratio = 26 },
1394 	{ .refclk = 24000, .cdclk = 324000, .ratio = 54 },
1395 	{ .refclk = 24000, .cdclk = 552000, .ratio = 46 },
1396 	{ .refclk = 24000, .cdclk = 648000, .ratio = 54 },
1397 
1398 	{ .refclk = 38400, .cdclk = 172800, .ratio =  9 },
1399 	{ .refclk = 38400, .cdclk = 192000, .ratio = 10 },
1400 	{ .refclk = 38400, .cdclk = 307200, .ratio = 16 },
1401 	{ .refclk = 38400, .cdclk = 326400, .ratio = 34 },
1402 	{ .refclk = 38400, .cdclk = 556800, .ratio = 29 },
1403 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34 },
1404 	{}
1405 };
1406 
1407 static const struct intel_cdclk_vals rkl_cdclk_table[] = {
1408 	{ .refclk = 19200, .cdclk = 172800, .ratio =  36 },
1409 	{ .refclk = 19200, .cdclk = 192000, .ratio =  40 },
1410 	{ .refclk = 19200, .cdclk = 307200, .ratio =  64 },
1411 	{ .refclk = 19200, .cdclk = 326400, .ratio = 136 },
1412 	{ .refclk = 19200, .cdclk = 556800, .ratio = 116 },
1413 	{ .refclk = 19200, .cdclk = 652800, .ratio = 136 },
1414 
1415 	{ .refclk = 24000, .cdclk = 180000, .ratio =  30 },
1416 	{ .refclk = 24000, .cdclk = 192000, .ratio =  32 },
1417 	{ .refclk = 24000, .cdclk = 312000, .ratio =  52 },
1418 	{ .refclk = 24000, .cdclk = 324000, .ratio = 108 },
1419 	{ .refclk = 24000, .cdclk = 552000, .ratio =  92 },
1420 	{ .refclk = 24000, .cdclk = 648000, .ratio = 108 },
1421 
1422 	{ .refclk = 38400, .cdclk = 172800, .ratio = 18 },
1423 	{ .refclk = 38400, .cdclk = 192000, .ratio = 20 },
1424 	{ .refclk = 38400, .cdclk = 307200, .ratio = 32 },
1425 	{ .refclk = 38400, .cdclk = 326400, .ratio = 68 },
1426 	{ .refclk = 38400, .cdclk = 556800, .ratio = 58 },
1427 	{ .refclk = 38400, .cdclk = 652800, .ratio = 68 },
1428 	{}
1429 };
1430 
1431 static const struct intel_cdclk_vals adlp_a_step_cdclk_table[] = {
1432 	{ .refclk = 19200, .cdclk = 307200, .ratio = 32 },
1433 	{ .refclk = 19200, .cdclk = 556800, .ratio = 58 },
1434 	{ .refclk = 19200, .cdclk = 652800, .ratio = 68 },
1435 
1436 	{ .refclk = 24000, .cdclk = 312000, .ratio = 26 },
1437 	{ .refclk = 24000, .cdclk = 552000, .ratio = 46 },
1438 	{ .refclk = 24400, .cdclk = 648000, .ratio = 54 },
1439 
1440 	{ .refclk = 38400, .cdclk = 307200, .ratio = 16 },
1441 	{ .refclk = 38400, .cdclk = 556800, .ratio = 29 },
1442 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34 },
1443 	{}
1444 };
1445 
1446 static const struct intel_cdclk_vals adlp_cdclk_table[] = {
1447 	{ .refclk = 19200, .cdclk = 172800, .ratio = 27 },
1448 	{ .refclk = 19200, .cdclk = 192000, .ratio = 20 },
1449 	{ .refclk = 19200, .cdclk = 307200, .ratio = 32 },
1450 	{ .refclk = 19200, .cdclk = 556800, .ratio = 58 },
1451 	{ .refclk = 19200, .cdclk = 652800, .ratio = 68 },
1452 
1453 	{ .refclk = 24000, .cdclk = 176000, .ratio = 22 },
1454 	{ .refclk = 24000, .cdclk = 192000, .ratio = 16 },
1455 	{ .refclk = 24000, .cdclk = 312000, .ratio = 26 },
1456 	{ .refclk = 24000, .cdclk = 552000, .ratio = 46 },
1457 	{ .refclk = 24000, .cdclk = 648000, .ratio = 54 },
1458 
1459 	{ .refclk = 38400, .cdclk = 179200, .ratio = 14 },
1460 	{ .refclk = 38400, .cdclk = 192000, .ratio = 10 },
1461 	{ .refclk = 38400, .cdclk = 307200, .ratio = 16 },
1462 	{ .refclk = 38400, .cdclk = 556800, .ratio = 29 },
1463 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34 },
1464 	{}
1465 };
1466 
1467 static const struct intel_cdclk_vals rplu_cdclk_table[] = {
1468 	{ .refclk = 19200, .cdclk = 172800, .ratio = 27 },
1469 	{ .refclk = 19200, .cdclk = 192000, .ratio = 20 },
1470 	{ .refclk = 19200, .cdclk = 307200, .ratio = 32 },
1471 	{ .refclk = 19200, .cdclk = 480000, .ratio = 50 },
1472 	{ .refclk = 19200, .cdclk = 556800, .ratio = 58 },
1473 	{ .refclk = 19200, .cdclk = 652800, .ratio = 68 },
1474 
1475 	{ .refclk = 24000, .cdclk = 176000, .ratio = 22 },
1476 	{ .refclk = 24000, .cdclk = 192000, .ratio = 16 },
1477 	{ .refclk = 24000, .cdclk = 312000, .ratio = 26 },
1478 	{ .refclk = 24000, .cdclk = 480000, .ratio = 40 },
1479 	{ .refclk = 24000, .cdclk = 552000, .ratio = 46 },
1480 	{ .refclk = 24000, .cdclk = 648000, .ratio = 54 },
1481 
1482 	{ .refclk = 38400, .cdclk = 179200, .ratio = 14 },
1483 	{ .refclk = 38400, .cdclk = 192000, .ratio = 10 },
1484 	{ .refclk = 38400, .cdclk = 307200, .ratio = 16 },
1485 	{ .refclk = 38400, .cdclk = 480000, .ratio = 25 },
1486 	{ .refclk = 38400, .cdclk = 556800, .ratio = 29 },
1487 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34 },
1488 	{}
1489 };
1490 
1491 static const struct intel_cdclk_vals dg2_cdclk_table[] = {
1492 	{ .refclk = 38400, .cdclk = 163200, .ratio = 34, .waveform = 0x8888 },
1493 	{ .refclk = 38400, .cdclk = 204000, .ratio = 34, .waveform = 0x9248 },
1494 	{ .refclk = 38400, .cdclk = 244800, .ratio = 34, .waveform = 0xa4a4 },
1495 	{ .refclk = 38400, .cdclk = 285600, .ratio = 34, .waveform = 0xa54a },
1496 	{ .refclk = 38400, .cdclk = 326400, .ratio = 34, .waveform = 0xaaaa },
1497 	{ .refclk = 38400, .cdclk = 367200, .ratio = 34, .waveform = 0xad5a },
1498 	{ .refclk = 38400, .cdclk = 408000, .ratio = 34, .waveform = 0xb6b6 },
1499 	{ .refclk = 38400, .cdclk = 448800, .ratio = 34, .waveform = 0xdbb6 },
1500 	{ .refclk = 38400, .cdclk = 489600, .ratio = 34, .waveform = 0xeeee },
1501 	{ .refclk = 38400, .cdclk = 530400, .ratio = 34, .waveform = 0xf7de },
1502 	{ .refclk = 38400, .cdclk = 571200, .ratio = 34, .waveform = 0xfefe },
1503 	{ .refclk = 38400, .cdclk = 612000, .ratio = 34, .waveform = 0xfffe },
1504 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34, .waveform = 0xffff },
1505 	{}
1506 };
1507 
1508 static const struct intel_cdclk_vals mtl_cdclk_table[] = {
1509 	{ .refclk = 38400, .cdclk = 172800, .ratio = 16, .waveform = 0xad5a },
1510 	{ .refclk = 38400, .cdclk = 192000, .ratio = 16, .waveform = 0xb6b6 },
1511 	{ .refclk = 38400, .cdclk = 307200, .ratio = 16, .waveform = 0x0000 },
1512 	{ .refclk = 38400, .cdclk = 480000, .ratio = 25, .waveform = 0x0000 },
1513 	{ .refclk = 38400, .cdclk = 556800, .ratio = 29, .waveform = 0x0000 },
1514 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34, .waveform = 0x0000 },
1515 	{}
1516 };
1517 
1518 static const struct intel_cdclk_vals xe2lpd_cdclk_table[] = {
1519 	{ .refclk = 38400, .cdclk = 153600, .ratio = 16, .waveform = 0xaaaa },
1520 	{ .refclk = 38400, .cdclk = 172800, .ratio = 16, .waveform = 0xad5a },
1521 	{ .refclk = 38400, .cdclk = 192000, .ratio = 16, .waveform = 0xb6b6 },
1522 	{ .refclk = 38400, .cdclk = 211200, .ratio = 16, .waveform = 0xdbb6 },
1523 	{ .refclk = 38400, .cdclk = 230400, .ratio = 16, .waveform = 0xeeee },
1524 	{ .refclk = 38400, .cdclk = 249600, .ratio = 16, .waveform = 0xf7de },
1525 	{ .refclk = 38400, .cdclk = 268800, .ratio = 16, .waveform = 0xfefe },
1526 	{ .refclk = 38400, .cdclk = 288000, .ratio = 16, .waveform = 0xfffe },
1527 	{ .refclk = 38400, .cdclk = 307200, .ratio = 16, .waveform = 0xffff },
1528 	{ .refclk = 38400, .cdclk = 330000, .ratio = 25, .waveform = 0xdbb6 },
1529 	{ .refclk = 38400, .cdclk = 360000, .ratio = 25, .waveform = 0xeeee },
1530 	{ .refclk = 38400, .cdclk = 390000, .ratio = 25, .waveform = 0xf7de },
1531 	{ .refclk = 38400, .cdclk = 420000, .ratio = 25, .waveform = 0xfefe },
1532 	{ .refclk = 38400, .cdclk = 450000, .ratio = 25, .waveform = 0xfffe },
1533 	{ .refclk = 38400, .cdclk = 480000, .ratio = 25, .waveform = 0xffff },
1534 	{ .refclk = 38400, .cdclk = 487200, .ratio = 29, .waveform = 0xfefe },
1535 	{ .refclk = 38400, .cdclk = 522000, .ratio = 29, .waveform = 0xfffe },
1536 	{ .refclk = 38400, .cdclk = 556800, .ratio = 29, .waveform = 0xffff },
1537 	{ .refclk = 38400, .cdclk = 571200, .ratio = 34, .waveform = 0xfefe },
1538 	{ .refclk = 38400, .cdclk = 612000, .ratio = 34, .waveform = 0xfffe },
1539 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34, .waveform = 0xffff },
1540 	{}
1541 };
1542 
1543 /*
1544  * Xe2_HPD always uses the minimal cdclk table from Wa_15015413771
1545  */
1546 static const struct intel_cdclk_vals xe2hpd_cdclk_table[] = {
1547 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34, .waveform = 0xffff },
1548 	{}
1549 };
1550 
1551 static const struct intel_cdclk_vals xe3lpd_cdclk_table[] = {
1552 	{ .refclk = 38400, .cdclk = 153600, .ratio = 16, .waveform = 0xaaaa },
1553 	{ .refclk = 38400, .cdclk = 172800, .ratio = 16, .waveform = 0xad5a },
1554 	{ .refclk = 38400, .cdclk = 192000, .ratio = 16, .waveform = 0xb6b6 },
1555 	{ .refclk = 38400, .cdclk = 211200, .ratio = 16, .waveform = 0xdbb6 },
1556 	{ .refclk = 38400, .cdclk = 230400, .ratio = 16, .waveform = 0xeeee },
1557 	{ .refclk = 38400, .cdclk = 249600, .ratio = 16, .waveform = 0xf7de },
1558 	{ .refclk = 38400, .cdclk = 268800, .ratio = 16, .waveform = 0xfefe },
1559 	{ .refclk = 38400, .cdclk = 288000, .ratio = 16, .waveform = 0xfffe },
1560 	{ .refclk = 38400, .cdclk = 307200, .ratio = 16, .waveform = 0xffff },
1561 	{ .refclk = 38400, .cdclk = 326400, .ratio = 17, .waveform = 0xffff },
1562 	{ .refclk = 38400, .cdclk = 345600, .ratio = 18, .waveform = 0xffff },
1563 	{ .refclk = 38400, .cdclk = 364800, .ratio = 19, .waveform = 0xffff },
1564 	{ .refclk = 38400, .cdclk = 384000, .ratio = 20, .waveform = 0xffff },
1565 	{ .refclk = 38400, .cdclk = 403200, .ratio = 21, .waveform = 0xffff },
1566 	{ .refclk = 38400, .cdclk = 422400, .ratio = 22, .waveform = 0xffff },
1567 	{ .refclk = 38400, .cdclk = 441600, .ratio = 23, .waveform = 0xffff },
1568 	{ .refclk = 38400, .cdclk = 460800, .ratio = 24, .waveform = 0xffff },
1569 	{ .refclk = 38400, .cdclk = 480000, .ratio = 25, .waveform = 0xffff },
1570 	{ .refclk = 38400, .cdclk = 499200, .ratio = 26, .waveform = 0xffff },
1571 	{ .refclk = 38400, .cdclk = 518400, .ratio = 27, .waveform = 0xffff },
1572 	{ .refclk = 38400, .cdclk = 537600, .ratio = 28, .waveform = 0xffff },
1573 	{ .refclk = 38400, .cdclk = 556800, .ratio = 29, .waveform = 0xffff },
1574 	{ .refclk = 38400, .cdclk = 576000, .ratio = 30, .waveform = 0xffff },
1575 	{ .refclk = 38400, .cdclk = 595200, .ratio = 31, .waveform = 0xffff },
1576 	{ .refclk = 38400, .cdclk = 614400, .ratio = 32, .waveform = 0xffff },
1577 	{ .refclk = 38400, .cdclk = 633600, .ratio = 33, .waveform = 0xffff },
1578 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34, .waveform = 0xffff },
1579 	{ .refclk = 38400, .cdclk = 672000, .ratio = 35, .waveform = 0xffff },
1580 	{ .refclk = 38400, .cdclk = 691200, .ratio = 36, .waveform = 0xffff },
1581 	{}
1582 };
1583 
1584 static const struct intel_cdclk_vals xe3p_lpd_cdclk_table[] = {
1585 	{ .refclk = 38400, .cdclk = 151200, .ratio = 21, .waveform = 0xa4a4 },
1586 	{ .refclk = 38400, .cdclk = 176400, .ratio = 21, .waveform = 0xaa54 },
1587 	{ .refclk = 38400, .cdclk = 201600, .ratio = 21, .waveform = 0xaaaa },
1588 	{ .refclk = 38400, .cdclk = 226800, .ratio = 21, .waveform = 0xad5a },
1589 	{ .refclk = 38400, .cdclk = 252000, .ratio = 21, .waveform = 0xb6b6 },
1590 	{ .refclk = 38400, .cdclk = 277200, .ratio = 21, .waveform = 0xdbb6 },
1591 	{ .refclk = 38400, .cdclk = 302400, .ratio = 21, .waveform = 0xeeee },
1592 	{ .refclk = 38400, .cdclk = 327600, .ratio = 21, .waveform = 0xf7de },
1593 	{ .refclk = 38400, .cdclk = 352800, .ratio = 21, .waveform = 0xfefe },
1594 	{ .refclk = 38400, .cdclk = 378000, .ratio = 21, .waveform = 0xfffe },
1595 	{ .refclk = 38400, .cdclk = 403200, .ratio = 21, .waveform = 0xffff },
1596 	{ .refclk = 38400, .cdclk = 422400, .ratio = 22, .waveform = 0xffff },
1597 	{ .refclk = 38400, .cdclk = 441600, .ratio = 23, .waveform = 0xffff },
1598 	{ .refclk = 38400, .cdclk = 460800, .ratio = 24, .waveform = 0xffff },
1599 	{ .refclk = 38400, .cdclk = 480000, .ratio = 25, .waveform = 0xffff },
1600 	{ .refclk = 38400, .cdclk = 499200, .ratio = 26, .waveform = 0xffff },
1601 	{ .refclk = 38400, .cdclk = 518400, .ratio = 27, .waveform = 0xffff },
1602 	{ .refclk = 38400, .cdclk = 537600, .ratio = 28, .waveform = 0xffff },
1603 	{ .refclk = 38400, .cdclk = 556800, .ratio = 29, .waveform = 0xffff },
1604 	{ .refclk = 38400, .cdclk = 576000, .ratio = 30, .waveform = 0xffff },
1605 	{ .refclk = 38400, .cdclk = 595200, .ratio = 31, .waveform = 0xffff },
1606 	{ .refclk = 38400, .cdclk = 614400, .ratio = 32, .waveform = 0xffff },
1607 	{ .refclk = 38400, .cdclk = 633600, .ratio = 33, .waveform = 0xffff },
1608 	{ .refclk = 38400, .cdclk = 652800, .ratio = 34, .waveform = 0xffff },
1609 	{ .refclk = 38400, .cdclk = 672000, .ratio = 35, .waveform = 0xffff },
1610 	{ .refclk = 38400, .cdclk = 691200, .ratio = 36, .waveform = 0xffff },
1611 	{ .refclk = 38400, .cdclk = 710400, .ratio = 37, .waveform = 0xffff },
1612 	{ .refclk = 38400, .cdclk = 729600, .ratio = 38, .waveform = 0xffff },
1613 	{ .refclk = 38400, .cdclk = 748800, .ratio = 39, .waveform = 0xffff },
1614 	{ .refclk = 38400, .cdclk = 768000, .ratio = 40, .waveform = 0xffff },
1615 	{ .refclk = 38400, .cdclk = 787200, .ratio = 41, .waveform = 0xffff },
1616 	{}
1617 };
1618 
1619 static const int cdclk_squash_len = 16;
1620 
1621 static int cdclk_squash_divider(u16 waveform)
1622 {
1623 	return hweight16(waveform ?: 0xffff);
1624 }
1625 
1626 static int cdclk_divider(int cdclk, int vco, u16 waveform)
1627 {
1628 	/* 2 * cd2x divider */
1629 	return DIV_ROUND_CLOSEST(vco * cdclk_squash_divider(waveform),
1630 				 cdclk * cdclk_squash_len);
1631 }
1632 
1633 static int bxt_calc_cdclk(struct intel_display *display, int min_cdclk)
1634 {
1635 	const struct intel_cdclk_vals *table = display->cdclk.table;
1636 	int i;
1637 
1638 	for (i = 0; table[i].refclk; i++)
1639 		if (table[i].refclk == display->cdclk.hw.ref &&
1640 		    table[i].cdclk >= min_cdclk)
1641 			return table[i].cdclk;
1642 
1643 	drm_WARN(display->drm, 1,
1644 		 "Cannot satisfy minimum cdclk %d with refclk %u\n",
1645 		 min_cdclk, display->cdclk.hw.ref);
1646 	return display->cdclk.max_cdclk_freq;
1647 }
1648 
1649 static int bxt_calc_cdclk_pll_vco(struct intel_display *display, int cdclk)
1650 {
1651 	const struct intel_cdclk_vals *table = display->cdclk.table;
1652 	int i;
1653 
1654 	if (cdclk == display->cdclk.hw.bypass)
1655 		return 0;
1656 
1657 	for (i = 0; table[i].refclk; i++)
1658 		if (table[i].refclk == display->cdclk.hw.ref &&
1659 		    table[i].cdclk == cdclk)
1660 			return display->cdclk.hw.ref * table[i].ratio;
1661 
1662 	drm_WARN(display->drm, 1, "cdclk %d not valid for refclk %u\n",
1663 		 cdclk, display->cdclk.hw.ref);
1664 	return 0;
1665 }
1666 
1667 static u8 bxt_calc_voltage_level(int cdclk)
1668 {
1669 	return DIV_ROUND_UP(cdclk, 25000);
1670 }
1671 
1672 static u8 calc_voltage_level(int cdclk, int num_voltage_levels,
1673 			     const int voltage_level_max_cdclk[])
1674 {
1675 	int voltage_level;
1676 
1677 	for (voltage_level = 0; voltage_level < num_voltage_levels; voltage_level++) {
1678 		if (cdclk <= voltage_level_max_cdclk[voltage_level])
1679 			return voltage_level;
1680 	}
1681 
1682 	MISSING_CASE(cdclk);
1683 	return num_voltage_levels - 1;
1684 }
1685 
1686 static u8 icl_calc_voltage_level(int cdclk)
1687 {
1688 	static const int icl_voltage_level_max_cdclk[] = {
1689 		[0] = 312000,
1690 		[1] = 556800,
1691 		[2] = 652800,
1692 	};
1693 
1694 	return calc_voltage_level(cdclk,
1695 				  ARRAY_SIZE(icl_voltage_level_max_cdclk),
1696 				  icl_voltage_level_max_cdclk);
1697 }
1698 
1699 static u8 ehl_calc_voltage_level(int cdclk)
1700 {
1701 	static const int ehl_voltage_level_max_cdclk[] = {
1702 		[0] = 180000,
1703 		[1] = 312000,
1704 		[2] = 326400,
1705 		/*
1706 		 * Bspec lists the limit as 556.8 MHz, but some JSL
1707 		 * development boards (at least) boot with 652.8 MHz
1708 		 */
1709 		[3] = 652800,
1710 	};
1711 
1712 	return calc_voltage_level(cdclk,
1713 				  ARRAY_SIZE(ehl_voltage_level_max_cdclk),
1714 				  ehl_voltage_level_max_cdclk);
1715 }
1716 
1717 static u8 tgl_calc_voltage_level(int cdclk)
1718 {
1719 	static const int tgl_voltage_level_max_cdclk[] = {
1720 		[0] = 312000,
1721 		[1] = 326400,
1722 		[2] = 556800,
1723 		[3] = 652800,
1724 	};
1725 
1726 	return calc_voltage_level(cdclk,
1727 				  ARRAY_SIZE(tgl_voltage_level_max_cdclk),
1728 				  tgl_voltage_level_max_cdclk);
1729 }
1730 
1731 static u8 rplu_calc_voltage_level(int cdclk)
1732 {
1733 	static const int rplu_voltage_level_max_cdclk[] = {
1734 		[0] = 312000,
1735 		[1] = 480000,
1736 		[2] = 556800,
1737 		[3] = 652800,
1738 	};
1739 
1740 	return calc_voltage_level(cdclk,
1741 				  ARRAY_SIZE(rplu_voltage_level_max_cdclk),
1742 				  rplu_voltage_level_max_cdclk);
1743 }
1744 
1745 static u8 xe3lpd_calc_voltage_level(int cdclk)
1746 {
1747 	/*
1748 	 * Starting with xe3lpd power controller does not need the voltage
1749 	 * index when doing the modeset update. This function is best left
1750 	 * defined but returning 0 to the mask.
1751 	 */
1752 	return 0;
1753 }
1754 
1755 static void icl_readout_refclk(struct intel_display *display,
1756 			       struct intel_cdclk_config *cdclk_config)
1757 {
1758 	u32 dssm = intel_de_read(display, SKL_DSSM) & ICL_DSSM_CDCLK_PLL_REFCLK_MASK;
1759 
1760 	switch (dssm) {
1761 	default:
1762 		MISSING_CASE(dssm);
1763 		fallthrough;
1764 	case ICL_DSSM_CDCLK_PLL_REFCLK_24MHz:
1765 		cdclk_config->ref = 24000;
1766 		break;
1767 	case ICL_DSSM_CDCLK_PLL_REFCLK_19_2MHz:
1768 		cdclk_config->ref = 19200;
1769 		break;
1770 	case ICL_DSSM_CDCLK_PLL_REFCLK_38_4MHz:
1771 		cdclk_config->ref = 38400;
1772 		break;
1773 	}
1774 }
1775 
1776 static void bxt_de_pll_readout(struct intel_display *display,
1777 			       struct intel_cdclk_config *cdclk_config)
1778 {
1779 	u32 val, ratio;
1780 
1781 	if (display->platform.dg2)
1782 		cdclk_config->ref = 38400;
1783 	else if (DISPLAY_VER(display) >= 11)
1784 		icl_readout_refclk(display, cdclk_config);
1785 	else
1786 		cdclk_config->ref = 19200;
1787 
1788 	val = intel_de_read(display, BXT_DE_PLL_ENABLE);
1789 	if ((val & BXT_DE_PLL_PLL_ENABLE) == 0 ||
1790 	    (val & BXT_DE_PLL_LOCK) == 0) {
1791 		/*
1792 		 * CDCLK PLL is disabled, the VCO/ratio doesn't matter, but
1793 		 * setting it to zero is a way to signal that.
1794 		 */
1795 		cdclk_config->vco = 0;
1796 		return;
1797 	}
1798 
1799 	/*
1800 	 * DISPLAY_VER >= 11 have the ratio directly in the PLL enable register,
1801 	 * gen9lp had it in a separate PLL control register.
1802 	 */
1803 	if (DISPLAY_VER(display) >= 11)
1804 		ratio = val & ICL_CDCLK_PLL_RATIO_MASK;
1805 	else
1806 		ratio = intel_de_read(display, BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
1807 
1808 	cdclk_config->vco = ratio * cdclk_config->ref;
1809 }
1810 
1811 static void bxt_get_cdclk(struct intel_display *display,
1812 			  struct intel_cdclk_config *cdclk_config)
1813 {
1814 	u32 squash_ctl = 0;
1815 	u32 divider;
1816 	int div;
1817 
1818 	bxt_de_pll_readout(display, cdclk_config);
1819 
1820 	if (DISPLAY_VER(display) >= 12)
1821 		cdclk_config->bypass = cdclk_config->ref / 2;
1822 	else if (DISPLAY_VER(display) >= 11)
1823 		cdclk_config->bypass = 50000;
1824 	else
1825 		cdclk_config->bypass = cdclk_config->ref;
1826 
1827 	if (cdclk_config->vco == 0) {
1828 		cdclk_config->cdclk = cdclk_config->bypass;
1829 		goto out;
1830 	}
1831 
1832 	divider = intel_de_read(display, CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
1833 
1834 	switch (divider) {
1835 	case BXT_CDCLK_CD2X_DIV_SEL_1:
1836 		div = 2;
1837 		break;
1838 	case BXT_CDCLK_CD2X_DIV_SEL_1_5:
1839 		div = 3;
1840 		break;
1841 	case BXT_CDCLK_CD2X_DIV_SEL_2:
1842 		div = 4;
1843 		break;
1844 	case BXT_CDCLK_CD2X_DIV_SEL_4:
1845 		div = 8;
1846 		break;
1847 	default:
1848 		MISSING_CASE(divider);
1849 		return;
1850 	}
1851 
1852 	if (HAS_CDCLK_SQUASH(display))
1853 		squash_ctl = intel_de_read(display, CDCLK_SQUASH_CTL);
1854 
1855 	if (squash_ctl & CDCLK_SQUASH_ENABLE) {
1856 		u16 waveform;
1857 		int size;
1858 
1859 		size = REG_FIELD_GET(CDCLK_SQUASH_WINDOW_SIZE_MASK, squash_ctl) + 1;
1860 		waveform = REG_FIELD_GET(CDCLK_SQUASH_WAVEFORM_MASK, squash_ctl) >> (16 - size);
1861 
1862 		cdclk_config->cdclk = DIV_ROUND_CLOSEST(hweight16(waveform) *
1863 							cdclk_config->vco, size * div);
1864 	} else {
1865 		cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco, div);
1866 	}
1867 
1868  out:
1869 	if (DISPLAY_VER(display) >= 20)
1870 		cdclk_config->joined_mbus = intel_de_read(display, MBUS_CTL) & MBUS_JOIN;
1871 	/*
1872 	 * Can't read this out :( Let's assume it's
1873 	 * at least what the CDCLK frequency requires.
1874 	 */
1875 	cdclk_config->voltage_level =
1876 		intel_cdclk_calc_voltage_level(display, cdclk_config->cdclk);
1877 }
1878 
1879 static void bxt_de_pll_disable(struct intel_display *display)
1880 {
1881 	intel_de_write(display, BXT_DE_PLL_ENABLE, 0);
1882 
1883 	/* Timeout 200us */
1884 	if (intel_de_wait_for_clear_ms(display,
1885 				       BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1886 		drm_err(display->drm, "timeout waiting for DE PLL unlock\n");
1887 
1888 	display->cdclk.hw.vco = 0;
1889 }
1890 
1891 static void bxt_de_pll_enable(struct intel_display *display, int vco)
1892 {
1893 	int ratio = DIV_ROUND_CLOSEST(vco, display->cdclk.hw.ref);
1894 
1895 	intel_de_rmw(display, BXT_DE_PLL_CTL,
1896 		     BXT_DE_PLL_RATIO_MASK, BXT_DE_PLL_RATIO(ratio));
1897 
1898 	intel_de_write(display, BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
1899 
1900 	/* Timeout 200us */
1901 	if (intel_de_wait_for_set_ms(display,
1902 				     BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1903 		drm_err(display->drm, "timeout waiting for DE PLL lock\n");
1904 
1905 	display->cdclk.hw.vco = vco;
1906 }
1907 
1908 static void icl_cdclk_pll_disable(struct intel_display *display)
1909 {
1910 	/*
1911 	 * Wa_13012396614:
1912 	 * Fixes: A sporadic race condition between MDCLK selection and PLL
1913 	 *        enabling.
1914 	 * Workaround:
1915 	 *   Change programming of MDCLK source selection in CDCLK_CTL:
1916 	 *    - When disabling the CDCLK PLL, first set MDCLK source to be CD2XCLK.
1917 	 *    - When enabling the CDCLK PLL, update MDCLK source selection only
1918 	 *      after the PLL is enabled (which is already done as part of the
1919 	 *      normal flow of _bxt_set_cdclk()).
1920 	 */
1921 	if (intel_display_wa(display, INTEL_DISPLAY_WA_13012396614))
1922 		intel_de_rmw(display, CDCLK_CTL, MDCLK_SOURCE_SEL_MASK, MDCLK_SOURCE_SEL_CD2XCLK);
1923 
1924 	intel_de_rmw(display, BXT_DE_PLL_ENABLE,
1925 		     BXT_DE_PLL_PLL_ENABLE, 0);
1926 
1927 	/* Timeout 200us */
1928 	if (intel_de_wait_for_clear_ms(display, BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1929 		drm_err(display->drm, "timeout waiting for CDCLK PLL unlock\n");
1930 
1931 	display->cdclk.hw.vco = 0;
1932 }
1933 
1934 static void icl_cdclk_pll_enable(struct intel_display *display, int vco)
1935 {
1936 	int ratio = DIV_ROUND_CLOSEST(vco, display->cdclk.hw.ref);
1937 	u32 val;
1938 
1939 	val = ICL_CDCLK_PLL_RATIO(ratio);
1940 	intel_de_write(display, BXT_DE_PLL_ENABLE, val);
1941 
1942 	val |= BXT_DE_PLL_PLL_ENABLE;
1943 	intel_de_write(display, BXT_DE_PLL_ENABLE, val);
1944 
1945 	/* Timeout 200us */
1946 	if (intel_de_wait_for_set_ms(display, BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1947 		drm_err(display->drm, "timeout waiting for CDCLK PLL lock\n");
1948 
1949 	display->cdclk.hw.vco = vco;
1950 }
1951 
1952 static void adlp_cdclk_pll_crawl(struct intel_display *display, int vco)
1953 {
1954 	int ratio = DIV_ROUND_CLOSEST(vco, display->cdclk.hw.ref);
1955 	u32 val;
1956 
1957 	/* Write PLL ratio without disabling */
1958 	val = ICL_CDCLK_PLL_RATIO(ratio) | BXT_DE_PLL_PLL_ENABLE;
1959 	intel_de_write(display, BXT_DE_PLL_ENABLE, val);
1960 
1961 	/* Submit freq change request */
1962 	val |= BXT_DE_PLL_FREQ_REQ;
1963 	intel_de_write(display, BXT_DE_PLL_ENABLE, val);
1964 
1965 	/* Timeout 200us */
1966 	if (intel_de_wait_for_set_ms(display, BXT_DE_PLL_ENABLE,
1967 				     BXT_DE_PLL_LOCK | BXT_DE_PLL_FREQ_REQ_ACK, 1))
1968 		drm_err(display->drm, "timeout waiting for FREQ change request ack\n");
1969 
1970 	val &= ~BXT_DE_PLL_FREQ_REQ;
1971 	intel_de_write(display, BXT_DE_PLL_ENABLE, val);
1972 
1973 	display->cdclk.hw.vco = vco;
1974 }
1975 
1976 static u32 bxt_cdclk_cd2x_pipe_mask(struct intel_display *display)
1977 {
1978 	if (DISPLAY_VER(display) >= 11)
1979 		return ICL_CDCLK_CD2X_PIPE_MASK;
1980 	else
1981 		return BXT_CDCLK_CD2X_PIPE_MASK;
1982 }
1983 
1984 static u32 bxt_cdclk_cd2x_pipe(struct intel_display *display, enum pipe pipe)
1985 {
1986 	if (DISPLAY_VER(display) >= 11) {
1987 		if (pipe == INVALID_PIPE)
1988 			return ICL_CDCLK_CD2X_PIPE_NONE;
1989 		else
1990 			return ICL_CDCLK_CD2X_PIPE(pipe);
1991 	} else {
1992 		if (pipe == INVALID_PIPE)
1993 			return BXT_CDCLK_CD2X_PIPE_NONE;
1994 		else
1995 			return BXT_CDCLK_CD2X_PIPE(pipe);
1996 	}
1997 }
1998 
1999 static u32 bxt_cdclk_cd2x_div_sel(struct intel_display *display,
2000 				  int cdclk, int vco, u16 waveform)
2001 {
2002 	u32 ret;
2003 
2004 	/* cdclk = vco / 2 / div{1,1.5,2,4} */
2005 	switch (cdclk_divider(cdclk, vco, waveform)) {
2006 	default:
2007 		drm_WARN_ON(display->drm,
2008 			    cdclk != display->cdclk.hw.bypass);
2009 		drm_WARN_ON(display->drm, vco != 0);
2010 		fallthrough;
2011 	case 2:
2012 		ret = BXT_CDCLK_CD2X_DIV_SEL_1;
2013 		break;
2014 	case 3:
2015 		ret = BXT_CDCLK_CD2X_DIV_SEL_1_5;
2016 		break;
2017 	case 4:
2018 		ret = BXT_CDCLK_CD2X_DIV_SEL_2;
2019 		break;
2020 	case 8:
2021 		ret = BXT_CDCLK_CD2X_DIV_SEL_4;
2022 		break;
2023 	}
2024 
2025 	/*
2026 	 * On Xe3_LPD onward, the expectation is to always have
2027 	 * BXT_CDCLK_CD2X_DIV_SEL_1 as the default.
2028 	 */
2029 	if (DISPLAY_VER(display) >= 30)
2030 		drm_WARN_ON(display->drm, ret != BXT_CDCLK_CD2X_DIV_SEL_1);
2031 
2032 	return ret;
2033 }
2034 
2035 static u16 cdclk_squash_waveform(struct intel_display *display,
2036 				 int cdclk)
2037 {
2038 	const struct intel_cdclk_vals *table = display->cdclk.table;
2039 	int i;
2040 
2041 	if (cdclk == display->cdclk.hw.bypass)
2042 		return 0;
2043 
2044 	for (i = 0; table[i].refclk; i++)
2045 		if (table[i].refclk == display->cdclk.hw.ref &&
2046 		    table[i].cdclk == cdclk)
2047 			return table[i].waveform;
2048 
2049 	drm_WARN(display->drm, 1, "cdclk %d not valid for refclk %u\n",
2050 		 cdclk, display->cdclk.hw.ref);
2051 
2052 	return 0xffff;
2053 }
2054 
2055 static void icl_cdclk_pll_update(struct intel_display *display, int vco)
2056 {
2057 	if (display->cdclk.hw.vco != 0 &&
2058 	    display->cdclk.hw.vco != vco)
2059 		icl_cdclk_pll_disable(display);
2060 
2061 	if (display->cdclk.hw.vco != vco)
2062 		icl_cdclk_pll_enable(display, vco);
2063 }
2064 
2065 static void bxt_cdclk_pll_update(struct intel_display *display, int vco)
2066 {
2067 	if (display->cdclk.hw.vco != 0 &&
2068 	    display->cdclk.hw.vco != vco)
2069 		bxt_de_pll_disable(display);
2070 
2071 	if (display->cdclk.hw.vco != vco)
2072 		bxt_de_pll_enable(display, vco);
2073 }
2074 
2075 static void dg2_cdclk_squash_program(struct intel_display *display,
2076 				     u16 waveform)
2077 {
2078 	u32 squash_ctl = 0;
2079 
2080 	if (waveform)
2081 		squash_ctl = CDCLK_SQUASH_ENABLE |
2082 			     CDCLK_SQUASH_WINDOW_SIZE(0xf) | waveform;
2083 
2084 	intel_de_write(display, CDCLK_SQUASH_CTL, squash_ctl);
2085 }
2086 
2087 static bool cdclk_pll_is_unknown(unsigned int vco)
2088 {
2089 	/*
2090 	 * Ensure driver does not take the crawl path for the
2091 	 * case when the vco is set to ~0 in the
2092 	 * sanitize path.
2093 	 */
2094 	return vco == ~0;
2095 }
2096 
2097 static bool mdclk_source_is_cdclk_pll(struct intel_display *display)
2098 {
2099 	return DISPLAY_VER(display) >= 20;
2100 }
2101 
2102 static u32 xe2lpd_mdclk_source_sel(struct intel_display *display)
2103 {
2104 	if (mdclk_source_is_cdclk_pll(display))
2105 		return MDCLK_SOURCE_SEL_CDCLK_PLL;
2106 
2107 	return MDCLK_SOURCE_SEL_CD2XCLK;
2108 }
2109 
2110 int intel_mdclk_cdclk_ratio(struct intel_display *display,
2111 			    const struct intel_cdclk_config *cdclk_config)
2112 {
2113 	if (mdclk_source_is_cdclk_pll(display))
2114 		return DIV_ROUND_UP(cdclk_config->vco, cdclk_config->cdclk);
2115 
2116 	/* Otherwise, source for MDCLK is CD2XCLK. */
2117 	return 2;
2118 }
2119 
2120 static void xe2lpd_mdclk_cdclk_ratio_program(struct intel_display *display,
2121 					     const struct intel_cdclk_config *cdclk_config)
2122 {
2123 	intel_dbuf_mdclk_cdclk_ratio_update(display,
2124 					    intel_mdclk_cdclk_ratio(display, cdclk_config),
2125 					    cdclk_config->joined_mbus);
2126 }
2127 
2128 static bool cdclk_compute_crawl_and_squash_midpoint(struct intel_display *display,
2129 						    const struct intel_cdclk_config *old_cdclk_config,
2130 						    const struct intel_cdclk_config *new_cdclk_config,
2131 						    struct intel_cdclk_config *mid_cdclk_config)
2132 {
2133 	u16 old_waveform, new_waveform, mid_waveform;
2134 	int old_div, new_div, mid_div;
2135 
2136 	/* Return if PLL is in an unknown state, force a complete disable and re-enable. */
2137 	if (cdclk_pll_is_unknown(old_cdclk_config->vco))
2138 		return false;
2139 
2140 	/* Return if both Squash and Crawl are not present */
2141 	if (!HAS_CDCLK_CRAWL(display) || !HAS_CDCLK_SQUASH(display))
2142 		return false;
2143 
2144 	old_waveform = cdclk_squash_waveform(display, old_cdclk_config->cdclk);
2145 	new_waveform = cdclk_squash_waveform(display, new_cdclk_config->cdclk);
2146 
2147 	/* Return if Squash only or Crawl only is the desired action */
2148 	if (old_cdclk_config->vco == 0 || new_cdclk_config->vco == 0 ||
2149 	    old_cdclk_config->vco == new_cdclk_config->vco ||
2150 	    old_waveform == new_waveform)
2151 		return false;
2152 
2153 	old_div = cdclk_divider(old_cdclk_config->cdclk,
2154 				old_cdclk_config->vco, old_waveform);
2155 	new_div = cdclk_divider(new_cdclk_config->cdclk,
2156 				new_cdclk_config->vco, new_waveform);
2157 
2158 	/*
2159 	 * Should not happen currently. We might need more midpoint
2160 	 * transitions if we need to also change the cd2x divider.
2161 	 */
2162 	if (drm_WARN_ON(display->drm, old_div != new_div))
2163 		return false;
2164 
2165 	*mid_cdclk_config = *new_cdclk_config;
2166 
2167 	/*
2168 	 * Populate the mid_cdclk_config accordingly.
2169 	 * - If moving to a higher cdclk, the desired action is squashing.
2170 	 * The mid cdclk config should have the new (squash) waveform.
2171 	 * - If moving to a lower cdclk, the desired action is crawling.
2172 	 * The mid cdclk config should have the new vco.
2173 	 */
2174 
2175 	if (cdclk_squash_divider(new_waveform) > cdclk_squash_divider(old_waveform)) {
2176 		mid_cdclk_config->vco = old_cdclk_config->vco;
2177 		mid_div = old_div;
2178 		mid_waveform = new_waveform;
2179 	} else {
2180 		mid_cdclk_config->vco = new_cdclk_config->vco;
2181 		mid_div = new_div;
2182 		mid_waveform = old_waveform;
2183 	}
2184 
2185 	mid_cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_squash_divider(mid_waveform) *
2186 						    mid_cdclk_config->vco,
2187 						    cdclk_squash_len * mid_div);
2188 
2189 	/* make sure the mid clock came out sane */
2190 
2191 	drm_WARN_ON(display->drm, mid_cdclk_config->cdclk <
2192 		    min(old_cdclk_config->cdclk, new_cdclk_config->cdclk));
2193 	drm_WARN_ON(display->drm, mid_cdclk_config->cdclk >
2194 		    display->cdclk.max_cdclk_freq);
2195 	drm_WARN_ON(display->drm, cdclk_squash_waveform(display, mid_cdclk_config->cdclk) !=
2196 		    mid_waveform);
2197 
2198 	return true;
2199 }
2200 
2201 static bool pll_enable_wa_needed(struct intel_display *display)
2202 {
2203 	return (DISPLAY_VERx100(display) == 2000 ||
2204 		DISPLAY_VERx100(display) == 1400 ||
2205 		display->platform.dg2) &&
2206 		display->cdclk.hw.vco > 0;
2207 }
2208 
2209 static bool has_cd2x_pipe_select(struct intel_display *display)
2210 {
2211 	return IS_DISPLAY_VER(display, 10, 20) || display->platform.broxton;
2212 }
2213 
2214 static u32 bxt_cdclk_ctl(struct intel_display *display,
2215 			 const struct intel_cdclk_config *cdclk_config,
2216 			 enum pipe pipe)
2217 {
2218 	int cdclk = cdclk_config->cdclk;
2219 	int vco = cdclk_config->vco;
2220 	u16 waveform;
2221 	u32 val;
2222 
2223 	waveform = cdclk_squash_waveform(display, cdclk);
2224 
2225 	val = bxt_cdclk_cd2x_div_sel(display, cdclk, vco, waveform);
2226 
2227 	if (has_cd2x_pipe_select(display))
2228 		val |= bxt_cdclk_cd2x_pipe(display, pipe);
2229 
2230 	/*
2231 	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
2232 	 * enable otherwise.
2233 	 */
2234 	if ((display->platform.geminilake || display->platform.broxton) &&
2235 	    cdclk >= 500000)
2236 		val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
2237 
2238 	if (DISPLAY_VER(display) >= 20) {
2239 		/*
2240 		 * Wa_13012396614 requires selecting CD2XCLK as MDCLK source
2241 		 * prior to disabling the PLL, which is already handled by
2242 		 * icl_cdclk_pll_disable().  Here we are just making sure
2243 		 * we keep the expected value.
2244 		 */
2245 		if (intel_display_wa(display, INTEL_DISPLAY_WA_13012396614) &&
2246 		    vco == 0)
2247 			val |= MDCLK_SOURCE_SEL_CD2XCLK;
2248 		else
2249 			val |= xe2lpd_mdclk_source_sel(display);
2250 	} else {
2251 		val |= skl_cdclk_decimal(cdclk);
2252 	}
2253 
2254 	return val;
2255 }
2256 
2257 static int bxt_cdclk_pcode_pre_notify(struct intel_display *display)
2258 {
2259 	/*
2260 	 * BSpec requires us to wait up to 150usec, but that leads to
2261 	 * timeouts; the 2ms used here is based on experiment.
2262 	 */
2263 	return intel_parent_pcode_write_timeout(display, HSW_PCODE_DE_WRITE_FREQ_REQ,
2264 						0x80000000, 2);
2265 }
2266 
2267 static int bxt_cdclk_pcode_post_notify(struct intel_display *display,
2268 				       const struct intel_cdclk_config *cdclk_config)
2269 {
2270 	/*
2271 	 * The timeout isn't specified, the 2ms used here is based on
2272 	 * experiment.
2273 	 * FIXME: Waiting for the request completion could be delayed
2274 	 * until the next PCODE request based on BSpec.
2275 	 */
2276 	return intel_parent_pcode_write_timeout(display, HSW_PCODE_DE_WRITE_FREQ_REQ,
2277 						cdclk_config->voltage_level, 2);
2278 }
2279 
2280 static void _bxt_set_cdclk(struct intel_display *display,
2281 			   const struct intel_cdclk_config *cdclk_config,
2282 			   enum pipe pipe)
2283 {
2284 	int cdclk = cdclk_config->cdclk;
2285 	int vco = cdclk_config->vco;
2286 
2287 	if (HAS_CDCLK_CRAWL(display) && display->cdclk.hw.vco > 0 && vco > 0 &&
2288 	    !cdclk_pll_is_unknown(display->cdclk.hw.vco)) {
2289 		if (display->cdclk.hw.vco != vco)
2290 			adlp_cdclk_pll_crawl(display, vco);
2291 	} else if (DISPLAY_VER(display) >= 11) {
2292 		/* wa_15010685871: dg2, mtl */
2293 		if (pll_enable_wa_needed(display))
2294 			dg2_cdclk_squash_program(display, 0);
2295 
2296 		icl_cdclk_pll_update(display, vco);
2297 	} else {
2298 		bxt_cdclk_pll_update(display, vco);
2299 	}
2300 
2301 	if (HAS_CDCLK_SQUASH(display)) {
2302 		u16 waveform = cdclk_squash_waveform(display, cdclk);
2303 
2304 		dg2_cdclk_squash_program(display, waveform);
2305 	}
2306 
2307 	intel_de_write(display, CDCLK_CTL, bxt_cdclk_ctl(display, cdclk_config, pipe));
2308 
2309 	if (pipe != INVALID_PIPE)
2310 		intel_crtc_wait_for_next_vblank(intel_crtc_for_pipe(display, pipe));
2311 }
2312 
2313 static void bxt_set_cdclk(struct intel_display *display,
2314 			  const struct intel_cdclk_config *cdclk_config,
2315 			  enum pipe pipe)
2316 {
2317 	struct intel_cdclk_config mid_cdclk_config;
2318 	int cdclk = cdclk_config->cdclk;
2319 
2320 	if (DISPLAY_VER(display) >= 20 && cdclk < display->cdclk.hw.cdclk)
2321 		xe2lpd_mdclk_cdclk_ratio_program(display, cdclk_config);
2322 
2323 	if (cdclk_compute_crawl_and_squash_midpoint(display, &display->cdclk.hw,
2324 						    cdclk_config, &mid_cdclk_config)) {
2325 		_bxt_set_cdclk(display, &mid_cdclk_config, pipe);
2326 		_bxt_set_cdclk(display, cdclk_config, pipe);
2327 	} else {
2328 		_bxt_set_cdclk(display, cdclk_config, pipe);
2329 	}
2330 
2331 	if (DISPLAY_VER(display) >= 20 && cdclk > display->cdclk.hw.cdclk)
2332 		xe2lpd_mdclk_cdclk_ratio_program(display, cdclk_config);
2333 
2334 	intel_update_cdclk(display);
2335 
2336 	/*
2337 	 * Can't read out the voltage level :(
2338 	 * Let's just assume everything is as expected.
2339 	 */
2340 	display->cdclk.hw.voltage_level = cdclk_config->voltage_level;
2341 }
2342 
2343 static void bxt_sanitize_cdclk(struct intel_display *display)
2344 {
2345 	u32 cdctl, expected;
2346 	int cdclk, vco;
2347 
2348 	intel_update_cdclk(display);
2349 	intel_cdclk_dump_config(display, &display->cdclk.hw, "Current CDCLK");
2350 
2351 	if (display->cdclk.hw.vco == 0 ||
2352 	    display->cdclk.hw.cdclk == display->cdclk.hw.bypass) {
2353 		drm_dbg_kms(display->drm, "Sanitizing CDCLK due to PLL not enabled/locked\n");
2354 		goto sanitize;
2355 	}
2356 
2357 	/* Make sure this is a legal cdclk value for the platform */
2358 	cdclk = bxt_calc_cdclk(display, display->cdclk.hw.cdclk);
2359 	if (cdclk != display->cdclk.hw.cdclk) {
2360 		drm_dbg_kms(display->drm, "Sanitizing CDCLK due to bad CDCLK frequency\n");
2361 		goto sanitize;
2362 	}
2363 
2364 	/* Make sure the VCO is correct for the cdclk */
2365 	vco = bxt_calc_cdclk_pll_vco(display, cdclk);
2366 	if (vco != display->cdclk.hw.vco) {
2367 		drm_dbg_kms(display->drm, "Sanitizing CDCLK due to bad VCO frequency\n");
2368 		goto sanitize;
2369 	}
2370 
2371 	/*
2372 	 * Some BIOS versions leave an incorrect decimal frequency value and
2373 	 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
2374 	 * so sanitize this register.
2375 	 */
2376 	cdctl = intel_de_read(display, CDCLK_CTL);
2377 	expected = bxt_cdclk_ctl(display, &display->cdclk.hw, INVALID_PIPE);
2378 
2379 	/*
2380 	 * Let's ignore the pipe field, since BIOS could have configured the
2381 	 * dividers both syncing to an active pipe, or asynchronously
2382 	 * (PIPE_NONE).
2383 	 */
2384 	if (has_cd2x_pipe_select(display)) {
2385 		cdctl &= ~bxt_cdclk_cd2x_pipe_mask(display);
2386 		cdctl |= bxt_cdclk_cd2x_pipe(display, INVALID_PIPE);
2387 	}
2388 
2389 	if (cdctl != expected) {
2390 		if (DISPLAY_VER(display) < 20) {
2391 			cdctl &= ~CDCLK_FREQ_DECIMAL_MASK;
2392 			cdctl |= expected & CDCLK_FREQ_DECIMAL_MASK;
2393 		}
2394 
2395 		if (cdctl != expected) {
2396 			drm_dbg_kms(display->drm, "Sanitizing CDCLK due to CDCLK_CTL 0x%x, expected 0x%x\n",
2397 				    intel_de_read(display, CDCLK_CTL), expected);
2398 			goto sanitize;
2399 		}
2400 
2401 		drm_dbg_kms(display->drm, "Sanitizing CDCLK decimal divider (CDCLK_CTL 0x%x, expected 0x%x)\n",
2402 			    intel_de_read(display, CDCLK_CTL), expected);
2403 
2404 		intel_de_write(display, CDCLK_CTL, expected);
2405 	}
2406 
2407 	/* All well; nothing to sanitize */
2408 	return;
2409 
2410 sanitize:
2411 	/* force cdclk programming */
2412 	display->cdclk.hw.cdclk = 0;
2413 
2414 	/* force full PLL disable + enable */
2415 	display->cdclk.hw.vco = ~0;
2416 }
2417 
2418 static void bxt_cdclk_init_hw(struct intel_display *display)
2419 {
2420 	struct intel_cdclk_config cdclk_config;
2421 
2422 	bxt_sanitize_cdclk(display);
2423 
2424 	if (display->cdclk.hw.cdclk != 0 &&
2425 	    display->cdclk.hw.vco != 0)
2426 		return;
2427 
2428 	cdclk_config = display->cdclk.hw;
2429 
2430 	/*
2431 	 * FIXME:
2432 	 * - The initial CDCLK needs to be read from VBT.
2433 	 *   Need to make this change after VBT has changes for BXT.
2434 	 */
2435 	cdclk_config.cdclk = bxt_calc_cdclk(display, 0);
2436 	cdclk_config.vco = bxt_calc_cdclk_pll_vco(display, cdclk_config.cdclk);
2437 	cdclk_config.voltage_level =
2438 		intel_cdclk_calc_voltage_level(display, cdclk_config.cdclk);
2439 
2440 	intel_cdclk_set_cdclk(display, &cdclk_config, INVALID_PIPE);
2441 }
2442 
2443 static void bxt_cdclk_uninit_hw(struct intel_display *display)
2444 {
2445 	struct intel_cdclk_config cdclk_config = display->cdclk.hw;
2446 
2447 	cdclk_config.cdclk = cdclk_config.bypass;
2448 	cdclk_config.vco = 0;
2449 	cdclk_config.voltage_level =
2450 		intel_cdclk_calc_voltage_level(display, cdclk_config.cdclk);
2451 
2452 	intel_cdclk_set_cdclk(display, &cdclk_config, INVALID_PIPE);
2453 }
2454 
2455 /**
2456  * intel_cdclk_init_hw - Initialize CDCLK hardware
2457  * @display: display instance
2458  *
2459  * Initialize CDCLK. This consists mainly of initializing display->cdclk.hw and
2460  * sanitizing the state of the hardware if needed. This is generally done only
2461  * during the display core initialization sequence, after which the DMC will
2462  * take care of turning CDCLK off/on as needed.
2463  */
2464 void intel_cdclk_init_hw(struct intel_display *display)
2465 {
2466 	if (DISPLAY_VER(display) >= 10 || display->platform.broxton)
2467 		bxt_cdclk_init_hw(display);
2468 	else if (DISPLAY_VER(display) == 9)
2469 		skl_cdclk_init_hw(display);
2470 }
2471 
2472 /**
2473  * intel_cdclk_uninit_hw - Uninitialize CDCLK hardware
2474  * @display: display instance
2475  *
2476  * Uninitialize CDCLK. This is done only during the display core
2477  * uninitialization sequence.
2478  */
2479 void intel_cdclk_uninit_hw(struct intel_display *display)
2480 {
2481 	if (DISPLAY_VER(display) >= 10 || display->platform.broxton)
2482 		bxt_cdclk_uninit_hw(display);
2483 	else if (DISPLAY_VER(display) == 9)
2484 		skl_cdclk_uninit_hw(display);
2485 }
2486 
2487 static bool intel_cdclk_can_crawl_and_squash(struct intel_display *display,
2488 					     const struct intel_cdclk_config *a,
2489 					     const struct intel_cdclk_config *b)
2490 {
2491 	u16 old_waveform;
2492 	u16 new_waveform;
2493 
2494 	drm_WARN_ON(display->drm, cdclk_pll_is_unknown(a->vco));
2495 
2496 	if (a->vco == 0 || b->vco == 0)
2497 		return false;
2498 
2499 	if (!HAS_CDCLK_CRAWL(display) || !HAS_CDCLK_SQUASH(display))
2500 		return false;
2501 
2502 	old_waveform = cdclk_squash_waveform(display, a->cdclk);
2503 	new_waveform = cdclk_squash_waveform(display, b->cdclk);
2504 
2505 	return a->vco != b->vco &&
2506 	       old_waveform != new_waveform;
2507 }
2508 
2509 static bool intel_cdclk_can_crawl(struct intel_display *display,
2510 				  const struct intel_cdclk_config *a,
2511 				  const struct intel_cdclk_config *b)
2512 {
2513 	int a_div, b_div;
2514 
2515 	if (!HAS_CDCLK_CRAWL(display))
2516 		return false;
2517 
2518 	/*
2519 	 * The vco and cd2x divider will change independently
2520 	 * from each, so we disallow cd2x change when crawling.
2521 	 */
2522 	a_div = DIV_ROUND_CLOSEST(a->vco, a->cdclk);
2523 	b_div = DIV_ROUND_CLOSEST(b->vco, b->cdclk);
2524 
2525 	return a->vco != 0 && b->vco != 0 &&
2526 		a->vco != b->vco &&
2527 		a_div == b_div &&
2528 		a->ref == b->ref;
2529 }
2530 
2531 static bool intel_cdclk_can_squash(struct intel_display *display,
2532 				   const struct intel_cdclk_config *a,
2533 				   const struct intel_cdclk_config *b)
2534 {
2535 	/*
2536 	 * FIXME should store a bit more state in intel_cdclk_config
2537 	 * to differentiate squasher vs. cd2x divider properly. For
2538 	 * the moment all platforms with squasher use a fixed cd2x
2539 	 * divider.
2540 	 */
2541 	if (!HAS_CDCLK_SQUASH(display))
2542 		return false;
2543 
2544 	return a->cdclk != b->cdclk &&
2545 		a->vco != 0 &&
2546 		a->vco == b->vco &&
2547 		a->ref == b->ref;
2548 }
2549 
2550 /**
2551  * intel_cdclk_clock_changed - Check whether the clock changed
2552  * @a: first CDCLK configuration
2553  * @b: second CDCLK configuration
2554  *
2555  * Returns:
2556  * True if CDCLK changed in a way that requires re-programming and
2557  * False otherwise.
2558  */
2559 bool intel_cdclk_clock_changed(const struct intel_cdclk_config *a,
2560 			       const struct intel_cdclk_config *b)
2561 {
2562 	return a->cdclk != b->cdclk ||
2563 		a->vco != b->vco ||
2564 		a->ref != b->ref;
2565 }
2566 
2567 /**
2568  * intel_cdclk_can_cd2x_update - Determine if changing between the two CDCLK
2569  *                               configurations requires only a cd2x divider update
2570  * @display: display instance
2571  * @a: first CDCLK configuration
2572  * @b: second CDCLK configuration
2573  *
2574  * Returns:
2575  * True if changing between the two CDCLK configurations
2576  * can be done with just a cd2x divider update, false if not.
2577  */
2578 static bool intel_cdclk_can_cd2x_update(struct intel_display *display,
2579 					const struct intel_cdclk_config *a,
2580 					const struct intel_cdclk_config *b)
2581 {
2582 	if (!has_cd2x_pipe_select(display))
2583 		return false;
2584 
2585 	/*
2586 	 * FIXME should store a bit more state in intel_cdclk_config
2587 	 * to differentiate squasher vs. cd2x divider properly. For
2588 	 * the moment all platforms with squasher use a fixed cd2x
2589 	 * divider.
2590 	 */
2591 	if (HAS_CDCLK_SQUASH(display))
2592 		return false;
2593 
2594 	return a->cdclk != b->cdclk &&
2595 		a->vco != 0 &&
2596 		a->vco == b->vco &&
2597 		a->ref == b->ref;
2598 }
2599 
2600 /**
2601  * intel_cdclk_changed - Determine if two CDCLK configurations are different
2602  * @a: first CDCLK configuration
2603  * @b: second CDCLK configuration
2604  *
2605  * Returns:
2606  * True if the CDCLK configurations don't match, false if they do.
2607  */
2608 static bool intel_cdclk_changed(const struct intel_cdclk_config *a,
2609 				const struct intel_cdclk_config *b)
2610 {
2611 	return intel_cdclk_clock_changed(a, b) ||
2612 		a->voltage_level != b->voltage_level;
2613 }
2614 
2615 void intel_cdclk_dump_config(struct intel_display *display,
2616 			     const struct intel_cdclk_config *cdclk_config,
2617 			     const char *context)
2618 {
2619 	drm_dbg_kms(display->drm, "%s %d kHz, VCO %d kHz, ref %d kHz, bypass %d kHz, voltage level %d\n",
2620 		    context, cdclk_config->cdclk, cdclk_config->vco,
2621 		    cdclk_config->ref, cdclk_config->bypass,
2622 		    cdclk_config->voltage_level);
2623 }
2624 
2625 static int dg2_cdclk_pcode_notify(struct intel_display *display,
2626 				  u8 voltage_level,
2627 				  u8 active_pipe_count,
2628 				  u16 cdclk,
2629 				  bool cdclk_update_valid,
2630 				  bool pipe_count_update_valid)
2631 {
2632 	u32 update_mask = 0;
2633 
2634 	update_mask = DISPLAY_TO_PCODE_UPDATE_MASK(cdclk, active_pipe_count, voltage_level);
2635 
2636 	if (cdclk_update_valid)
2637 		update_mask |= DISPLAY_TO_PCODE_CDCLK_VALID;
2638 
2639 	if (pipe_count_update_valid)
2640 		update_mask |= DISPLAY_TO_PCODE_PIPE_COUNT_VALID;
2641 
2642 	return intel_parent_pcode_request(display, SKL_PCODE_CDCLK_CONTROL,
2643 					  update_mask,
2644 					  SKL_CDCLK_READY_FOR_CHANGE,
2645 					  SKL_CDCLK_READY_FOR_CHANGE, 3);
2646 }
2647 
2648 static void intel_set_cdclk(struct intel_display *display,
2649 			    const struct intel_cdclk_config *cdclk_config,
2650 			    enum pipe pipe, const char *context)
2651 {
2652 	struct intel_encoder *encoder;
2653 
2654 	if (!intel_cdclk_changed(&display->cdclk.hw, cdclk_config))
2655 		return;
2656 
2657 	if (drm_WARN_ON_ONCE(display->drm, !display->cdclk.funcs->set_cdclk))
2658 		return;
2659 
2660 	intel_cdclk_dump_config(display, cdclk_config, context);
2661 
2662 	for_each_intel_encoder_with_psr(display->drm, encoder) {
2663 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2664 
2665 		intel_psr_pause(intel_dp);
2666 	}
2667 
2668 	intel_audio_cdclk_change_pre(display);
2669 
2670 	/*
2671 	 * Lock aux/gmbus while we change cdclk in case those
2672 	 * functions use cdclk. Not all platforms/ports do,
2673 	 * but we'll lock them all for simplicity.
2674 	 */
2675 	mutex_lock(&display->gmbus.mutex);
2676 	for_each_intel_dp(display->drm, encoder) {
2677 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2678 
2679 		mutex_lock_nest_lock(&intel_dp->aux.hw_mutex,
2680 				     &display->gmbus.mutex);
2681 	}
2682 
2683 	intel_cdclk_set_cdclk(display, cdclk_config, pipe);
2684 
2685 	for_each_intel_dp(display->drm, encoder) {
2686 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2687 
2688 		mutex_unlock(&intel_dp->aux.hw_mutex);
2689 	}
2690 	mutex_unlock(&display->gmbus.mutex);
2691 
2692 	for_each_intel_encoder_with_psr(display->drm, encoder) {
2693 		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);
2694 
2695 		intel_psr_resume(intel_dp);
2696 	}
2697 
2698 	intel_audio_cdclk_change_post(display);
2699 
2700 	if (drm_WARN(display->drm,
2701 		     intel_cdclk_changed(&display->cdclk.hw, cdclk_config),
2702 		     "cdclk state doesn't match!\n")) {
2703 		intel_cdclk_dump_config(display, &display->cdclk.hw, "[hw state]");
2704 		intel_cdclk_dump_config(display, cdclk_config, "[sw state]");
2705 	}
2706 }
2707 
2708 static int dg2_power_well_count(struct intel_display *display,
2709 				const struct intel_cdclk_state *cdclk_state)
2710 {
2711 	return display->platform.dg2 ? hweight8(cdclk_state->active_pipes) : 0;
2712 }
2713 
2714 static void dg2_cdclk_pcode_pre_notify(struct intel_atomic_state *state)
2715 {
2716 	struct intel_display *display = to_intel_display(state);
2717 	const struct intel_cdclk_state *old_cdclk_state =
2718 		intel_atomic_get_old_cdclk_state(state);
2719 	const struct intel_cdclk_state *new_cdclk_state =
2720 		intel_atomic_get_new_cdclk_state(state);
2721 	u8 voltage_level, num_active_pipes = 0;
2722 	bool change_cdclk, update_pipe_count;
2723 	unsigned int cdclk_mhz = 0;
2724 	int ret;
2725 
2726 	if (!intel_cdclk_changed(&old_cdclk_state->actual,
2727 				 &new_cdclk_state->actual) &&
2728 	    dg2_power_well_count(display, old_cdclk_state) ==
2729 	    dg2_power_well_count(display, new_cdclk_state))
2730 		return;
2731 
2732 	/* According to "Sequence Before Frequency Change", voltage level set to 0x3 */
2733 	voltage_level = DISPLAY_TO_PCODE_VOLTAGE_MAX;
2734 
2735 	change_cdclk = new_cdclk_state->actual.cdclk != old_cdclk_state->actual.cdclk;
2736 	update_pipe_count = dg2_power_well_count(display, new_cdclk_state) >
2737 		dg2_power_well_count(display, old_cdclk_state);
2738 
2739 	/*
2740 	 * According to "Sequence Before Frequency Change",
2741 	 * if CDCLK is increasing, set bits 25:16 to upcoming CDCLK,
2742 	 * if CDCLK is decreasing or not changing, set bits 25:16 to current CDCLK,
2743 	 * which basically means we choose the maximum of old and new CDCLK, if we know both
2744 	 */
2745 	if (change_cdclk) {
2746 		unsigned int cdclk = max(new_cdclk_state->actual.cdclk,
2747 					 old_cdclk_state->actual.cdclk);
2748 
2749 		cdclk_mhz = DIV_ROUND_UP(cdclk, 1000);
2750 	}
2751 
2752 	/*
2753 	 * According to "Sequence For Pipe Count Change",
2754 	 * if pipe count is increasing, set bits 25:16 to upcoming pipe count
2755 	 * (power well is enabled)
2756 	 * no action if it is decreasing, before the change
2757 	 */
2758 	if (update_pipe_count)
2759 		num_active_pipes = dg2_power_well_count(display, new_cdclk_state);
2760 
2761 	ret = dg2_cdclk_pcode_notify(display, voltage_level, num_active_pipes, cdclk_mhz,
2762 				     change_cdclk, update_pipe_count);
2763 	if (ret)
2764 		drm_err(display->drm,
2765 			"Failed to inform PCODE about start of CDCLK change (%d)\n", ret);
2766 }
2767 
2768 static void dg2_cdclk_pcode_post_notify(struct intel_atomic_state *state)
2769 {
2770 	struct intel_display *display = to_intel_display(state);
2771 	const struct intel_cdclk_state *new_cdclk_state =
2772 		intel_atomic_get_new_cdclk_state(state);
2773 	const struct intel_cdclk_state *old_cdclk_state =
2774 		intel_atomic_get_old_cdclk_state(state);
2775 	u8 voltage_level, num_active_pipes = 0;
2776 	bool update_cdclk, update_pipe_count;
2777 	unsigned int cdclk_mhz = 0;
2778 	int ret;
2779 
2780 	/* According to "Sequence After Frequency Change", set voltage to used level */
2781 	voltage_level = new_cdclk_state->actual.voltage_level;
2782 
2783 	update_cdclk = new_cdclk_state->actual.cdclk != old_cdclk_state->actual.cdclk;
2784 	update_pipe_count = dg2_power_well_count(display, new_cdclk_state) <
2785 		dg2_power_well_count(display, old_cdclk_state);
2786 
2787 	/*
2788 	 * According to "Sequence After Frequency Change",
2789 	 * set bits 25:16 to current CDCLK
2790 	 */
2791 	if (update_cdclk) {
2792 		unsigned int cdclk = new_cdclk_state->actual.cdclk;
2793 
2794 		cdclk_mhz = DIV_ROUND_UP(cdclk, 1000);
2795 	}
2796 
2797 	/*
2798 	 * According to "Sequence For Pipe Count Change",
2799 	 * if pipe count is decreasing, set bits 25:16 to current pipe count,
2800 	 * after the change(power well is disabled)
2801 	 * no action if it is increasing, after the change
2802 	 */
2803 	if (update_pipe_count)
2804 		num_active_pipes = dg2_power_well_count(display, new_cdclk_state);
2805 
2806 	ret = dg2_cdclk_pcode_notify(display, voltage_level, num_active_pipes, cdclk_mhz,
2807 				     update_cdclk, update_pipe_count);
2808 	if (ret)
2809 		drm_err(display->drm,
2810 			"Failed to inform PCODE about end of CDCLK change (%d)\n", ret);
2811 }
2812 
2813 bool intel_cdclk_is_decreasing_later(struct intel_atomic_state *state)
2814 {
2815 	const struct intel_cdclk_state *old_cdclk_state =
2816 		intel_atomic_get_old_cdclk_state(state);
2817 	const struct intel_cdclk_state *new_cdclk_state =
2818 		intel_atomic_get_new_cdclk_state(state);
2819 
2820 	return new_cdclk_state && !new_cdclk_state->disable_pipes &&
2821 		new_cdclk_state->actual.cdclk < old_cdclk_state->actual.cdclk;
2822 }
2823 
2824 /**
2825  * intel_set_cdclk_pre_plane_update - Push the CDCLK state to the hardware
2826  * @state: intel atomic state
2827  *
2828  * Program the hardware before updating the HW plane state based on the
2829  * new CDCLK state, if necessary.
2830  */
2831 void
2832 intel_set_cdclk_pre_plane_update(struct intel_atomic_state *state)
2833 {
2834 	struct intel_display *display = to_intel_display(state);
2835 	const struct intel_cdclk_state *old_cdclk_state =
2836 		intel_atomic_get_old_cdclk_state(state);
2837 	const struct intel_cdclk_state *new_cdclk_state =
2838 		intel_atomic_get_new_cdclk_state(state);
2839 	struct intel_cdclk_config cdclk_config;
2840 	enum pipe pipe;
2841 
2842 	if (!new_cdclk_state)
2843 		return;
2844 
2845 	if (!intel_cdclk_changed(&old_cdclk_state->actual,
2846 				 &new_cdclk_state->actual) &&
2847 	    dg2_power_well_count(display, old_cdclk_state) ==
2848 	    dg2_power_well_count(display, new_cdclk_state))
2849 		return;
2850 
2851 	if (new_cdclk_state->disable_pipes) {
2852 		cdclk_config = new_cdclk_state->actual;
2853 		pipe = INVALID_PIPE;
2854 	} else {
2855 		if (new_cdclk_state->actual.cdclk >= old_cdclk_state->actual.cdclk) {
2856 			cdclk_config = new_cdclk_state->actual;
2857 			pipe = new_cdclk_state->pipe;
2858 		} else {
2859 			cdclk_config = old_cdclk_state->actual;
2860 			pipe = INVALID_PIPE;
2861 		}
2862 
2863 		cdclk_config.voltage_level = max(new_cdclk_state->actual.voltage_level,
2864 						 old_cdclk_state->actual.voltage_level);
2865 	}
2866 
2867 	/*
2868 	 * mbus joining will be changed later by
2869 	 * intel_dbuf_mbus_{pre,post}_ddb_update()
2870 	 */
2871 	cdclk_config.joined_mbus = old_cdclk_state->actual.joined_mbus;
2872 
2873 	drm_WARN_ON(display->drm, !new_cdclk_state->base.changed);
2874 
2875 	if (display->platform.dg2)
2876 		dg2_cdclk_pcode_pre_notify(state);
2877 
2878 	intel_set_cdclk(display, &cdclk_config, pipe,
2879 			"Pre changing CDCLK to");
2880 }
2881 
2882 /**
2883  * intel_set_cdclk_post_plane_update - Push the CDCLK state to the hardware
2884  * @state: intel atomic state
2885  *
2886  * Program the hardware after updating the HW plane state based on the
2887  * new CDCLK state, if necessary.
2888  */
2889 void
2890 intel_set_cdclk_post_plane_update(struct intel_atomic_state *state)
2891 {
2892 	struct intel_display *display = to_intel_display(state);
2893 	const struct intel_cdclk_state *old_cdclk_state =
2894 		intel_atomic_get_old_cdclk_state(state);
2895 	const struct intel_cdclk_state *new_cdclk_state =
2896 		intel_atomic_get_new_cdclk_state(state);
2897 	enum pipe pipe;
2898 
2899 	if (!new_cdclk_state)
2900 		return;
2901 
2902 	if (!intel_cdclk_changed(&old_cdclk_state->actual,
2903 				 &new_cdclk_state->actual) &&
2904 	    dg2_power_well_count(display, old_cdclk_state) ==
2905 	    dg2_power_well_count(display, new_cdclk_state))
2906 		return;
2907 
2908 	if (!new_cdclk_state->disable_pipes &&
2909 	    new_cdclk_state->actual.cdclk < old_cdclk_state->actual.cdclk)
2910 		pipe = new_cdclk_state->pipe;
2911 	else
2912 		pipe = INVALID_PIPE;
2913 
2914 	drm_WARN_ON(display->drm, !new_cdclk_state->base.changed);
2915 
2916 	intel_set_cdclk(display, &new_cdclk_state->actual, pipe,
2917 			"Post changing CDCLK to");
2918 
2919 	if (display->platform.dg2)
2920 		dg2_cdclk_pcode_post_notify(state);
2921 }
2922 
2923 /* pixels per CDCLK */
2924 int intel_cdclk_ppc(struct intel_display *display, bool double_wide)
2925 {
2926 	return HAS_2PPC(display) || double_wide ? 2 : 1;
2927 }
2928 
2929 /* max pixel rate as % of CDCLK (not accounting for PPC) */
2930 static int intel_cdclk_guardband(struct intel_display *display)
2931 {
2932 	if (DISPLAY_VER(display) >= 9 ||
2933 	    display->platform.broadwell || display->platform.haswell)
2934 		return 100;
2935 	else if (display->platform.cherryview)
2936 		return 95;
2937 	else
2938 		return 90;
2939 }
2940 
2941 static int _intel_pixel_rate_to_cdclk(const struct intel_crtc_state *crtc_state, int pixel_rate)
2942 {
2943 	struct intel_display *display = to_intel_display(crtc_state);
2944 	int ppc = intel_cdclk_ppc(display, crtc_state->double_wide);
2945 	int guardband = intel_cdclk_guardband(display);
2946 
2947 	return DIV_ROUND_UP(pixel_rate * 100, guardband * ppc);
2948 }
2949 
2950 static int intel_pixel_rate_to_cdclk(const struct intel_crtc_state *crtc_state)
2951 {
2952 	return _intel_pixel_rate_to_cdclk(crtc_state, crtc_state->pixel_rate_cdclk);
2953 }
2954 
2955 static int intel_planes_min_cdclk(const struct intel_crtc_state *crtc_state)
2956 {
2957 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2958 	struct intel_display *display = to_intel_display(crtc);
2959 	struct intel_plane *plane;
2960 	int min_cdclk = 0;
2961 
2962 	for_each_intel_plane_on_crtc(display->drm, crtc, plane)
2963 		min_cdclk = max(min_cdclk, crtc_state->plane_min_cdclk[plane->id]);
2964 
2965 	return min_cdclk;
2966 }
2967 
2968 int intel_crtc_min_cdclk(const struct intel_crtc_state *crtc_state)
2969 {
2970 	int min_cdclk;
2971 
2972 	if (!crtc_state->hw.enable)
2973 		return 0;
2974 
2975 	min_cdclk = intel_pixel_rate_to_cdclk(crtc_state);
2976 	min_cdclk = max(min_cdclk, intel_crtc_bw_min_cdclk(crtc_state));
2977 	min_cdclk = max(min_cdclk, intel_fbc_min_cdclk(crtc_state));
2978 	min_cdclk = max(min_cdclk, hsw_ips_min_cdclk(crtc_state));
2979 	min_cdclk = max(min_cdclk, intel_audio_min_cdclk(crtc_state));
2980 	min_cdclk = max(min_cdclk, vlv_dsi_min_cdclk(crtc_state));
2981 	min_cdclk = max(min_cdclk, intel_planes_min_cdclk(crtc_state));
2982 	min_cdclk = max(min_cdclk, intel_vdsc_min_cdclk(crtc_state));
2983 
2984 	return min_cdclk;
2985 }
2986 
2987 static int intel_cdclk_update_crtc_min_cdclk(struct intel_atomic_state *state,
2988 					     struct intel_crtc *crtc,
2989 					     int old_min_cdclk, int new_min_cdclk,
2990 					     bool *need_cdclk_calc)
2991 {
2992 	struct intel_display *display = to_intel_display(state);
2993 	struct intel_cdclk_state *cdclk_state;
2994 	bool allow_cdclk_decrease = intel_any_crtc_needs_modeset(state);
2995 	int ret;
2996 
2997 	if (new_min_cdclk == old_min_cdclk)
2998 		return 0;
2999 
3000 	if (!allow_cdclk_decrease && new_min_cdclk < old_min_cdclk)
3001 		return 0;
3002 
3003 	cdclk_state = intel_atomic_get_cdclk_state(state);
3004 	if (IS_ERR(cdclk_state))
3005 		return PTR_ERR(cdclk_state);
3006 
3007 	old_min_cdclk = cdclk_state->min_cdclk[crtc->pipe];
3008 
3009 	if (new_min_cdclk == old_min_cdclk)
3010 		return 0;
3011 
3012 	if (!allow_cdclk_decrease && new_min_cdclk < old_min_cdclk)
3013 		return 0;
3014 
3015 	cdclk_state->min_cdclk[crtc->pipe] = new_min_cdclk;
3016 
3017 	ret = intel_atomic_lock_global_state(&cdclk_state->base);
3018 	if (ret)
3019 		return ret;
3020 
3021 	*need_cdclk_calc = true;
3022 
3023 	drm_dbg_kms(display->drm,
3024 		    "[CRTC:%d:%s] min cdclk: %d kHz -> %d kHz\n",
3025 		    crtc->base.base.id, crtc->base.name,
3026 		    old_min_cdclk, new_min_cdclk);
3027 
3028 	return 0;
3029 }
3030 
3031 static int intel_cdclk_update_crtc_min_voltage_level(struct intel_atomic_state *state,
3032 						     struct intel_crtc *crtc,
3033 						     u8 old_min_voltage_level,
3034 						     u8 new_min_voltage_level,
3035 						     bool *need_cdclk_calc)
3036 {
3037 	struct intel_display *display = to_intel_display(state);
3038 	struct intel_cdclk_state *cdclk_state;
3039 	bool allow_voltage_level_decrease = intel_any_crtc_needs_modeset(state);
3040 	int ret;
3041 
3042 	if (new_min_voltage_level == old_min_voltage_level)
3043 		return 0;
3044 
3045 	if (!allow_voltage_level_decrease &&
3046 	    new_min_voltage_level < old_min_voltage_level)
3047 		return 0;
3048 
3049 	cdclk_state = intel_atomic_get_cdclk_state(state);
3050 	if (IS_ERR(cdclk_state))
3051 		return PTR_ERR(cdclk_state);
3052 
3053 	old_min_voltage_level = cdclk_state->min_voltage_level[crtc->pipe];
3054 
3055 	if (new_min_voltage_level == old_min_voltage_level)
3056 		return 0;
3057 
3058 	if (!allow_voltage_level_decrease &&
3059 	    new_min_voltage_level < old_min_voltage_level)
3060 		return 0;
3061 
3062 	cdclk_state->min_voltage_level[crtc->pipe] = new_min_voltage_level;
3063 
3064 	ret = intel_atomic_lock_global_state(&cdclk_state->base);
3065 	if (ret)
3066 		return ret;
3067 
3068 	*need_cdclk_calc = true;
3069 
3070 	drm_dbg_kms(display->drm,
3071 		    "[CRTC:%d:%s] min voltage level: %d -> %d\n",
3072 		    crtc->base.base.id, crtc->base.name,
3073 		    old_min_voltage_level, new_min_voltage_level);
3074 
3075 	return 0;
3076 }
3077 
3078 int intel_cdclk_update_dbuf_bw_min_cdclk(struct intel_atomic_state *state,
3079 					 int old_min_cdclk, int new_min_cdclk,
3080 					 bool *need_cdclk_calc)
3081 {
3082 	struct intel_display *display = to_intel_display(state);
3083 	struct intel_cdclk_state *cdclk_state;
3084 	bool allow_cdclk_decrease = intel_any_crtc_needs_modeset(state);
3085 	int ret;
3086 
3087 	if (new_min_cdclk == old_min_cdclk)
3088 		return 0;
3089 
3090 	if (!allow_cdclk_decrease && new_min_cdclk < old_min_cdclk)
3091 		return 0;
3092 
3093 	cdclk_state = intel_atomic_get_cdclk_state(state);
3094 	if (IS_ERR(cdclk_state))
3095 		return PTR_ERR(cdclk_state);
3096 
3097 	old_min_cdclk = cdclk_state->dbuf_bw_min_cdclk;
3098 
3099 	if (new_min_cdclk == old_min_cdclk)
3100 		return 0;
3101 
3102 	if (!allow_cdclk_decrease && new_min_cdclk < old_min_cdclk)
3103 		return 0;
3104 
3105 	cdclk_state->dbuf_bw_min_cdclk = new_min_cdclk;
3106 
3107 	ret = intel_atomic_lock_global_state(&cdclk_state->base);
3108 	if (ret)
3109 		return ret;
3110 
3111 	*need_cdclk_calc = true;
3112 
3113 	drm_dbg_kms(display->drm,
3114 		    "dbuf bandwidth min cdclk: %d kHz -> %d kHz\n",
3115 		    old_min_cdclk, new_min_cdclk);
3116 
3117 	return 0;
3118 }
3119 
3120 static bool glk_cdclk_audio_wa_needed(struct intel_display *display,
3121 				      const struct intel_cdclk_state *cdclk_state)
3122 {
3123 	return display->platform.geminilake &&
3124 		cdclk_state->enabled_pipes &&
3125 		!is_power_of_2(cdclk_state->enabled_pipes);
3126 }
3127 
3128 static int intel_compute_min_cdclk(struct intel_atomic_state *state)
3129 {
3130 	struct intel_display *display = to_intel_display(state);
3131 	struct intel_cdclk_state *cdclk_state =
3132 		intel_atomic_get_new_cdclk_state(state);
3133 	enum pipe pipe;
3134 	int min_cdclk;
3135 
3136 	min_cdclk = cdclk_state->force_min_cdclk;
3137 	min_cdclk = max(min_cdclk, cdclk_state->dbuf_bw_min_cdclk);
3138 	for_each_pipe(display, pipe)
3139 		min_cdclk = max(min_cdclk, cdclk_state->min_cdclk[pipe]);
3140 
3141 	/*
3142 	 * Avoid glk_force_audio_cdclk() causing excessive screen
3143 	 * blinking when multiple pipes are active by making sure
3144 	 * CDCLK frequency is always high enough for audio. With a
3145 	 * single active pipe we can always change CDCLK frequency
3146 	 * by changing the cd2x divider (see glk_cdclk_table[]) and
3147 	 * thus a full modeset won't be needed then.
3148 	 */
3149 	if (glk_cdclk_audio_wa_needed(display, cdclk_state))
3150 		min_cdclk = max(min_cdclk, 2 * 96000);
3151 
3152 	if (min_cdclk > display->cdclk.max_cdclk_freq) {
3153 		drm_dbg_kms(display->drm,
3154 			    "required cdclk (%d kHz) exceeds max (%d kHz)\n",
3155 			    min_cdclk, display->cdclk.max_cdclk_freq);
3156 		return -EINVAL;
3157 	}
3158 
3159 	return min_cdclk;
3160 }
3161 
3162 /*
3163  * Account for port clock min voltage level requirements.
3164  * This only really does something on DISPLA_VER >= 11 but can be
3165  * called on earlier platforms as well.
3166  *
3167  * Note that this functions assumes that 0 is
3168  * the lowest voltage value, and higher values
3169  * correspond to increasingly higher voltages.
3170  *
3171  * Should that relationship no longer hold on
3172  * future platforms this code will need to be
3173  * adjusted.
3174  */
3175 static int bxt_compute_min_voltage_level(struct intel_atomic_state *state)
3176 {
3177 	struct intel_display *display = to_intel_display(state);
3178 	struct intel_cdclk_state *cdclk_state =
3179 		intel_atomic_get_new_cdclk_state(state);
3180 	struct intel_crtc *crtc;
3181 	struct intel_crtc_state *crtc_state;
3182 	u8 min_voltage_level;
3183 	enum pipe pipe;
3184 
3185 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state) {
3186 		int ret;
3187 
3188 		if (crtc_state->hw.enable)
3189 			min_voltage_level = crtc_state->min_voltage_level;
3190 		else
3191 			min_voltage_level = 0;
3192 
3193 		if (cdclk_state->min_voltage_level[crtc->pipe] == min_voltage_level)
3194 			continue;
3195 
3196 		cdclk_state->min_voltage_level[crtc->pipe] = min_voltage_level;
3197 
3198 		ret = intel_atomic_lock_global_state(&cdclk_state->base);
3199 		if (ret)
3200 			return ret;
3201 	}
3202 
3203 	min_voltage_level = 0;
3204 	for_each_pipe(display, pipe)
3205 		min_voltage_level = max(min_voltage_level,
3206 					cdclk_state->min_voltage_level[pipe]);
3207 
3208 	return min_voltage_level;
3209 }
3210 
3211 static int vlv_modeset_calc_cdclk(struct intel_atomic_state *state)
3212 {
3213 	struct intel_display *display = to_intel_display(state);
3214 	struct intel_cdclk_state *cdclk_state =
3215 		intel_atomic_get_new_cdclk_state(state);
3216 	int min_cdclk, cdclk;
3217 
3218 	min_cdclk = intel_compute_min_cdclk(state);
3219 	if (min_cdclk < 0)
3220 		return min_cdclk;
3221 
3222 	cdclk = vlv_calc_cdclk(display, min_cdclk);
3223 
3224 	cdclk_state->logical.cdclk = cdclk;
3225 	cdclk_state->logical.voltage_level =
3226 		vlv_calc_voltage_level(display, cdclk);
3227 
3228 	if (!cdclk_state->active_pipes) {
3229 		cdclk = vlv_calc_cdclk(display, cdclk_state->force_min_cdclk);
3230 
3231 		cdclk_state->actual.cdclk = cdclk;
3232 		cdclk_state->actual.voltage_level =
3233 			vlv_calc_voltage_level(display, cdclk);
3234 	} else {
3235 		cdclk_state->actual = cdclk_state->logical;
3236 	}
3237 
3238 	return 0;
3239 }
3240 
3241 static int bdw_modeset_calc_cdclk(struct intel_atomic_state *state)
3242 {
3243 	struct intel_cdclk_state *cdclk_state =
3244 		intel_atomic_get_new_cdclk_state(state);
3245 	int min_cdclk, cdclk;
3246 
3247 	min_cdclk = intel_compute_min_cdclk(state);
3248 	if (min_cdclk < 0)
3249 		return min_cdclk;
3250 
3251 	cdclk = bdw_calc_cdclk(min_cdclk);
3252 
3253 	cdclk_state->logical.cdclk = cdclk;
3254 	cdclk_state->logical.voltage_level =
3255 		bdw_calc_voltage_level(cdclk);
3256 
3257 	if (!cdclk_state->active_pipes) {
3258 		cdclk = bdw_calc_cdclk(cdclk_state->force_min_cdclk);
3259 
3260 		cdclk_state->actual.cdclk = cdclk;
3261 		cdclk_state->actual.voltage_level =
3262 			bdw_calc_voltage_level(cdclk);
3263 	} else {
3264 		cdclk_state->actual = cdclk_state->logical;
3265 	}
3266 
3267 	return 0;
3268 }
3269 
3270 static int skl_dpll0_vco(struct intel_atomic_state *state)
3271 {
3272 	struct intel_display *display = to_intel_display(state);
3273 	struct intel_cdclk_state *cdclk_state =
3274 		intel_atomic_get_new_cdclk_state(state);
3275 	struct intel_crtc *crtc;
3276 	struct intel_crtc_state *crtc_state;
3277 	int vco;
3278 
3279 	vco = cdclk_state->logical.vco;
3280 	if (!vco)
3281 		vco = display->cdclk.skl_preferred_vco_freq;
3282 
3283 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state) {
3284 		if (!crtc_state->hw.enable)
3285 			continue;
3286 
3287 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
3288 			continue;
3289 
3290 		/*
3291 		 * DPLL0 VCO may need to be adjusted to get the correct
3292 		 * clock for eDP. This will affect cdclk as well.
3293 		 */
3294 		switch (crtc_state->port_clock / 2) {
3295 		case 108000:
3296 		case 216000:
3297 			vco = 8640000;
3298 			break;
3299 		default:
3300 			vco = 8100000;
3301 			break;
3302 		}
3303 	}
3304 
3305 	return vco;
3306 }
3307 
3308 static int skl_modeset_calc_cdclk(struct intel_atomic_state *state)
3309 {
3310 	struct intel_cdclk_state *cdclk_state =
3311 		intel_atomic_get_new_cdclk_state(state);
3312 	int min_cdclk, cdclk, vco;
3313 
3314 	min_cdclk = intel_compute_min_cdclk(state);
3315 	if (min_cdclk < 0)
3316 		return min_cdclk;
3317 
3318 	vco = skl_dpll0_vco(state);
3319 
3320 	cdclk = skl_calc_cdclk(min_cdclk, vco);
3321 
3322 	cdclk_state->logical.vco = vco;
3323 	cdclk_state->logical.cdclk = cdclk;
3324 	cdclk_state->logical.voltage_level =
3325 		skl_calc_voltage_level(cdclk);
3326 
3327 	if (!cdclk_state->active_pipes) {
3328 		cdclk = skl_calc_cdclk(cdclk_state->force_min_cdclk, vco);
3329 
3330 		cdclk_state->actual.vco = vco;
3331 		cdclk_state->actual.cdclk = cdclk;
3332 		cdclk_state->actual.voltage_level =
3333 			skl_calc_voltage_level(cdclk);
3334 	} else {
3335 		cdclk_state->actual = cdclk_state->logical;
3336 	}
3337 
3338 	return 0;
3339 }
3340 
3341 static int bxt_modeset_calc_cdclk(struct intel_atomic_state *state)
3342 {
3343 	struct intel_display *display = to_intel_display(state);
3344 	struct intel_cdclk_state *cdclk_state =
3345 		intel_atomic_get_new_cdclk_state(state);
3346 	int min_cdclk, min_voltage_level, cdclk, vco;
3347 
3348 	min_cdclk = intel_compute_min_cdclk(state);
3349 	if (min_cdclk < 0)
3350 		return min_cdclk;
3351 
3352 	min_voltage_level = bxt_compute_min_voltage_level(state);
3353 	if (min_voltage_level < 0)
3354 		return min_voltage_level;
3355 
3356 	cdclk = bxt_calc_cdclk(display, min_cdclk);
3357 	vco = bxt_calc_cdclk_pll_vco(display, cdclk);
3358 
3359 	cdclk_state->logical.vco = vco;
3360 	cdclk_state->logical.cdclk = cdclk;
3361 	cdclk_state->logical.voltage_level =
3362 		max_t(int, min_voltage_level,
3363 		      intel_cdclk_calc_voltage_level(display, cdclk));
3364 
3365 	if (!cdclk_state->active_pipes) {
3366 		cdclk = bxt_calc_cdclk(display, cdclk_state->force_min_cdclk);
3367 		vco = bxt_calc_cdclk_pll_vco(display, cdclk);
3368 
3369 		cdclk_state->actual.vco = vco;
3370 		cdclk_state->actual.cdclk = cdclk;
3371 		cdclk_state->actual.voltage_level =
3372 			intel_cdclk_calc_voltage_level(display, cdclk);
3373 	} else {
3374 		cdclk_state->actual = cdclk_state->logical;
3375 	}
3376 
3377 	return 0;
3378 }
3379 
3380 static int fixed_modeset_calc_cdclk(struct intel_atomic_state *state)
3381 {
3382 	int min_cdclk;
3383 
3384 	/*
3385 	 * We can't change the cdclk frequency, but we still want to
3386 	 * check that the required minimum frequency doesn't exceed
3387 	 * the actual cdclk frequency.
3388 	 */
3389 	min_cdclk = intel_compute_min_cdclk(state);
3390 	if (min_cdclk < 0)
3391 		return min_cdclk;
3392 
3393 	return 0;
3394 }
3395 
3396 static struct intel_global_state *intel_cdclk_duplicate_state(struct intel_global_obj *obj)
3397 {
3398 	struct intel_cdclk_state *cdclk_state;
3399 
3400 	cdclk_state = kmemdup(obj->state, sizeof(*cdclk_state), GFP_KERNEL);
3401 	if (!cdclk_state)
3402 		return NULL;
3403 
3404 	cdclk_state->pipe = INVALID_PIPE;
3405 	cdclk_state->disable_pipes = false;
3406 
3407 	return &cdclk_state->base;
3408 }
3409 
3410 static void intel_cdclk_destroy_state(struct intel_global_obj *obj,
3411 				      struct intel_global_state *state)
3412 {
3413 	kfree(state);
3414 }
3415 
3416 static const struct intel_global_state_funcs intel_cdclk_funcs = {
3417 	.atomic_duplicate_state = intel_cdclk_duplicate_state,
3418 	.atomic_destroy_state = intel_cdclk_destroy_state,
3419 };
3420 
3421 struct intel_cdclk_state *
3422 intel_atomic_get_cdclk_state(struct intel_atomic_state *state)
3423 {
3424 	struct intel_display *display = to_intel_display(state);
3425 	struct intel_global_state *cdclk_state;
3426 
3427 	cdclk_state = intel_atomic_get_global_obj_state(state, &display->cdclk.obj);
3428 	if (IS_ERR(cdclk_state))
3429 		return ERR_CAST(cdclk_state);
3430 
3431 	return to_intel_cdclk_state(cdclk_state);
3432 }
3433 
3434 static int intel_cdclk_modeset_checks(struct intel_atomic_state *state,
3435 				      bool *need_cdclk_calc)
3436 {
3437 	struct intel_display *display = to_intel_display(state);
3438 	const struct intel_cdclk_state *old_cdclk_state;
3439 	struct intel_cdclk_state *new_cdclk_state;
3440 	int ret;
3441 
3442 	if (!intel_any_crtc_enable_changed(state) &&
3443 	    !intel_any_crtc_active_changed(state))
3444 		return 0;
3445 
3446 	new_cdclk_state = intel_atomic_get_cdclk_state(state);
3447 	if (IS_ERR(new_cdclk_state))
3448 		return PTR_ERR(new_cdclk_state);
3449 
3450 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
3451 
3452 	new_cdclk_state->enabled_pipes =
3453 		intel_calc_enabled_pipes(state, old_cdclk_state->enabled_pipes);
3454 
3455 	new_cdclk_state->active_pipes =
3456 		intel_calc_active_pipes(state, old_cdclk_state->active_pipes);
3457 
3458 	ret = intel_atomic_lock_global_state(&new_cdclk_state->base);
3459 	if (ret)
3460 		return ret;
3461 
3462 	if (!old_cdclk_state->active_pipes != !new_cdclk_state->active_pipes)
3463 		*need_cdclk_calc = true;
3464 
3465 	if (glk_cdclk_audio_wa_needed(display, old_cdclk_state) !=
3466 	    glk_cdclk_audio_wa_needed(display, new_cdclk_state))
3467 		*need_cdclk_calc = true;
3468 
3469 	if (dg2_power_well_count(display, old_cdclk_state) !=
3470 	    dg2_power_well_count(display, new_cdclk_state))
3471 		*need_cdclk_calc = true;
3472 
3473 	return 0;
3474 }
3475 
3476 static int intel_crtcs_calc_min_cdclk(struct intel_atomic_state *state,
3477 				      bool *need_cdclk_calc)
3478 {
3479 	const struct intel_crtc_state *old_crtc_state;
3480 	const struct intel_crtc_state *new_crtc_state;
3481 	struct intel_crtc *crtc;
3482 	int ret;
3483 
3484 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state) {
3485 		ret = intel_cdclk_update_crtc_min_cdclk(state, crtc,
3486 							old_crtc_state->min_cdclk,
3487 							new_crtc_state->min_cdclk,
3488 							need_cdclk_calc);
3489 		if (ret)
3490 			return ret;
3491 
3492 		ret = intel_cdclk_update_crtc_min_voltage_level(state, crtc,
3493 								old_crtc_state->min_voltage_level,
3494 								new_crtc_state->min_voltage_level,
3495 								need_cdclk_calc);
3496 		if (ret)
3497 			return ret;
3498 	}
3499 
3500 	return 0;
3501 }
3502 
3503 int intel_cdclk_state_set_joined_mbus(struct intel_atomic_state *state, bool joined_mbus)
3504 {
3505 	struct intel_cdclk_state *cdclk_state;
3506 
3507 	cdclk_state = intel_atomic_get_cdclk_state(state);
3508 	if (IS_ERR(cdclk_state))
3509 		return PTR_ERR(cdclk_state);
3510 
3511 	cdclk_state->actual.joined_mbus = joined_mbus;
3512 	cdclk_state->logical.joined_mbus = joined_mbus;
3513 
3514 	return intel_atomic_lock_global_state(&cdclk_state->base);
3515 }
3516 
3517 int intel_cdclk_init(struct intel_display *display)
3518 {
3519 	struct intel_cdclk_state *cdclk_state;
3520 
3521 	cdclk_state = kzalloc_obj(*cdclk_state);
3522 	if (!cdclk_state)
3523 		return -ENOMEM;
3524 
3525 	intel_atomic_global_obj_init(display, &display->cdclk.obj,
3526 				     &cdclk_state->base, &intel_cdclk_funcs);
3527 
3528 	return 0;
3529 }
3530 
3531 static bool intel_cdclk_need_serialize(struct intel_display *display,
3532 				       const struct intel_cdclk_state *old_cdclk_state,
3533 				       const struct intel_cdclk_state *new_cdclk_state)
3534 {
3535 	/*
3536 	 * We need to poke hw for DG2, because we notify PCode if
3537 	 * pipe power well count changes.
3538 	 */
3539 	return intel_cdclk_changed(&old_cdclk_state->actual,
3540 				   &new_cdclk_state->actual) ||
3541 		dg2_power_well_count(display, old_cdclk_state) !=
3542 		dg2_power_well_count(display, new_cdclk_state);
3543 }
3544 
3545 static int intel_modeset_calc_cdclk(struct intel_atomic_state *state)
3546 {
3547 	struct intel_display *display = to_intel_display(state);
3548 	const struct intel_cdclk_state *old_cdclk_state;
3549 	struct intel_cdclk_state *new_cdclk_state;
3550 	enum pipe pipe = INVALID_PIPE;
3551 	int ret;
3552 
3553 	new_cdclk_state = intel_atomic_get_cdclk_state(state);
3554 	if (IS_ERR(new_cdclk_state))
3555 		return PTR_ERR(new_cdclk_state);
3556 
3557 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
3558 
3559 	ret = intel_cdclk_modeset_calc_cdclk(state);
3560 	if (ret)
3561 		return ret;
3562 
3563 	if (intel_cdclk_need_serialize(display, old_cdclk_state, new_cdclk_state)) {
3564 		/*
3565 		 * Also serialize commits across all crtcs
3566 		 * if the actual hw needs to be poked.
3567 		 */
3568 		ret = intel_atomic_serialize_global_state(&new_cdclk_state->base);
3569 		if (ret)
3570 			return ret;
3571 	} else if (intel_cdclk_changed(&old_cdclk_state->logical,
3572 				       &new_cdclk_state->logical)) {
3573 		ret = intel_atomic_lock_global_state(&new_cdclk_state->base);
3574 		if (ret)
3575 			return ret;
3576 	} else {
3577 		return 0;
3578 	}
3579 
3580 	if (is_power_of_2(new_cdclk_state->active_pipes) &&
3581 	    intel_cdclk_can_cd2x_update(display,
3582 					&old_cdclk_state->actual,
3583 					&new_cdclk_state->actual)) {
3584 		struct intel_crtc *crtc;
3585 		struct intel_crtc_state *crtc_state;
3586 
3587 		pipe = ilog2(new_cdclk_state->active_pipes);
3588 		crtc = intel_crtc_for_pipe(display, pipe);
3589 
3590 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
3591 		if (IS_ERR(crtc_state))
3592 			return PTR_ERR(crtc_state);
3593 
3594 		if (intel_crtc_needs_modeset(crtc_state))
3595 			pipe = INVALID_PIPE;
3596 	}
3597 
3598 	if (intel_cdclk_can_crawl_and_squash(display,
3599 					     &old_cdclk_state->actual,
3600 					     &new_cdclk_state->actual)) {
3601 		drm_dbg_kms(display->drm,
3602 			    "Can change cdclk via crawling and squashing\n");
3603 	} else if (intel_cdclk_can_squash(display,
3604 					&old_cdclk_state->actual,
3605 					&new_cdclk_state->actual)) {
3606 		drm_dbg_kms(display->drm,
3607 			    "Can change cdclk via squashing\n");
3608 	} else if (intel_cdclk_can_crawl(display,
3609 					 &old_cdclk_state->actual,
3610 					 &new_cdclk_state->actual)) {
3611 		drm_dbg_kms(display->drm,
3612 			    "Can change cdclk via crawling\n");
3613 	} else if (pipe != INVALID_PIPE) {
3614 		new_cdclk_state->pipe = pipe;
3615 
3616 		drm_dbg_kms(display->drm,
3617 			    "Can change cdclk cd2x divider with pipe %c active\n",
3618 			    pipe_name(pipe));
3619 	} else if (intel_cdclk_clock_changed(&old_cdclk_state->actual,
3620 					     &new_cdclk_state->actual)) {
3621 		/* All pipes must be switched off while we change the cdclk. */
3622 		ret = intel_modeset_all_pipes_late(state, "CDCLK change");
3623 		if (ret)
3624 			return ret;
3625 
3626 		new_cdclk_state->disable_pipes = true;
3627 
3628 		drm_dbg_kms(display->drm,
3629 			    "Modeset required for cdclk change\n");
3630 	}
3631 
3632 	if (intel_mdclk_cdclk_ratio(display, &old_cdclk_state->actual) !=
3633 	    intel_mdclk_cdclk_ratio(display, &new_cdclk_state->actual)) {
3634 		int ratio = intel_mdclk_cdclk_ratio(display, &new_cdclk_state->actual);
3635 
3636 		ret = intel_dbuf_state_set_mdclk_cdclk_ratio(state, ratio);
3637 		if (ret)
3638 			return ret;
3639 	}
3640 
3641 	drm_dbg_kms(display->drm,
3642 		    "New cdclk calculated to be logical %u kHz, actual %u kHz\n",
3643 		    new_cdclk_state->logical.cdclk,
3644 		    new_cdclk_state->actual.cdclk);
3645 	drm_dbg_kms(display->drm,
3646 		    "New voltage level calculated to be logical %u, actual %u\n",
3647 		    new_cdclk_state->logical.voltage_level,
3648 		    new_cdclk_state->actual.voltage_level);
3649 
3650 	return 0;
3651 }
3652 
3653 int intel_cdclk_atomic_check(struct intel_atomic_state *state)
3654 {
3655 	const struct intel_cdclk_state *old_cdclk_state;
3656 	struct intel_cdclk_state *new_cdclk_state;
3657 	bool need_cdclk_calc = false;
3658 	int ret;
3659 
3660 	ret = intel_cdclk_modeset_checks(state, &need_cdclk_calc);
3661 	if (ret)
3662 		return ret;
3663 
3664 	ret = intel_crtcs_calc_min_cdclk(state, &need_cdclk_calc);
3665 	if (ret)
3666 		return ret;
3667 
3668 	ret = intel_dbuf_bw_calc_min_cdclk(state, &need_cdclk_calc);
3669 	if (ret)
3670 		return ret;
3671 
3672 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
3673 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
3674 
3675 	if (new_cdclk_state &&
3676 	    old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk) {
3677 		ret = intel_atomic_lock_global_state(&new_cdclk_state->base);
3678 		if (ret)
3679 			return ret;
3680 
3681 		need_cdclk_calc = true;
3682 	}
3683 
3684 	if (need_cdclk_calc) {
3685 		ret = intel_modeset_calc_cdclk(state);
3686 		if (ret)
3687 			return ret;
3688 	}
3689 
3690 	return 0;
3691 }
3692 
3693 void intel_cdclk_update_hw_state(struct intel_display *display)
3694 {
3695 	const struct intel_dbuf_bw_state *dbuf_bw_state =
3696 		to_intel_dbuf_bw_state(display->dbuf_bw.obj.state);
3697 	struct intel_cdclk_state *cdclk_state =
3698 		to_intel_cdclk_state(display->cdclk.obj.state);
3699 	struct intel_crtc *crtc;
3700 
3701 	cdclk_state->enabled_pipes = 0;
3702 	cdclk_state->active_pipes = 0;
3703 
3704 	for_each_intel_crtc(display, crtc) {
3705 		const struct intel_crtc_state *crtc_state =
3706 			to_intel_crtc_state(crtc->base.state);
3707 		enum pipe pipe = crtc->pipe;
3708 
3709 		if (crtc_state->hw.enable)
3710 			cdclk_state->enabled_pipes |= BIT(pipe);
3711 		if (crtc_state->hw.active)
3712 			cdclk_state->active_pipes |= BIT(pipe);
3713 
3714 		cdclk_state->min_cdclk[pipe] = crtc_state->min_cdclk;
3715 		cdclk_state->min_voltage_level[pipe] = crtc_state->min_voltage_level;
3716 	}
3717 
3718 	cdclk_state->dbuf_bw_min_cdclk = intel_dbuf_bw_min_cdclk(display, dbuf_bw_state);
3719 }
3720 
3721 void intel_cdclk_crtc_disable_noatomic(struct intel_crtc *crtc)
3722 {
3723 	struct intel_display *display = to_intel_display(crtc);
3724 
3725 	intel_cdclk_update_hw_state(display);
3726 }
3727 
3728 static int intel_compute_max_dotclk(struct intel_display *display)
3729 {
3730 	int ppc = intel_cdclk_ppc(display, HAS_DOUBLE_WIDE(display));
3731 	int guardband = intel_cdclk_guardband(display);
3732 	int max_cdclk_freq = display->cdclk.max_cdclk_freq;
3733 
3734 	return ppc * max_cdclk_freq * guardband / 100;
3735 }
3736 
3737 /**
3738  * intel_update_max_cdclk - Determine the maximum support CDCLK frequency
3739  * @display: display instance
3740  *
3741  * Determine the maximum CDCLK frequency the platform supports, and also
3742  * derive the maximum dot clock frequency the maximum CDCLK frequency
3743  * allows.
3744  */
3745 void intel_update_max_cdclk(struct intel_display *display)
3746 {
3747 	if (DISPLAY_VER(display) >= 35) {
3748 		display->cdclk.max_cdclk_freq = 787200;
3749 	} else if (DISPLAY_VERx100(display) >= 3002) {
3750 		display->cdclk.max_cdclk_freq = 480000;
3751 	} else if (DISPLAY_VER(display) >= 30) {
3752 		display->cdclk.max_cdclk_freq = 691200;
3753 	} else if (display->platform.jasperlake || display->platform.elkhartlake) {
3754 		if (display->cdclk.hw.ref == 24000)
3755 			display->cdclk.max_cdclk_freq = 552000;
3756 		else
3757 			display->cdclk.max_cdclk_freq = 556800;
3758 	} else if (DISPLAY_VER(display) >= 11) {
3759 		if (display->cdclk.hw.ref == 24000)
3760 			display->cdclk.max_cdclk_freq = 648000;
3761 		else
3762 			display->cdclk.max_cdclk_freq = 652800;
3763 	} else if (display->platform.geminilake) {
3764 		display->cdclk.max_cdclk_freq = 316800;
3765 	} else if (display->platform.broxton) {
3766 		display->cdclk.max_cdclk_freq = 624000;
3767 	} else if (DISPLAY_VER(display) == 9) {
3768 		u32 limit = intel_de_read(display, SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
3769 		int max_cdclk, vco;
3770 
3771 		vco = display->cdclk.skl_preferred_vco_freq;
3772 		drm_WARN_ON(display->drm, vco != 8100000 && vco != 8640000);
3773 
3774 		/*
3775 		 * Use the lower (vco 8640) cdclk values as a
3776 		 * first guess. skl_calc_cdclk() will correct it
3777 		 * if the preferred vco is 8100 instead.
3778 		 */
3779 		if (limit == SKL_DFSM_CDCLK_LIMIT_675)
3780 			max_cdclk = 617143;
3781 		else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
3782 			max_cdclk = 540000;
3783 		else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
3784 			max_cdclk = 432000;
3785 		else
3786 			max_cdclk = 308571;
3787 
3788 		display->cdclk.max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
3789 	} else if (display->platform.broadwell)  {
3790 		/*
3791 		 * FIXME with extra cooling we can allow
3792 		 * 540 MHz for ULX and 675 Mhz for ULT.
3793 		 * How can we know if extra cooling is
3794 		 * available? PCI ID, VTB, something else?
3795 		 */
3796 		if (intel_de_read(display, FUSE_STRAP) & HSW_CDCLK_LIMIT)
3797 			display->cdclk.max_cdclk_freq = 450000;
3798 		else if (display->platform.broadwell_ulx)
3799 			display->cdclk.max_cdclk_freq = 450000;
3800 		else if (display->platform.broadwell_ult)
3801 			display->cdclk.max_cdclk_freq = 540000;
3802 		else
3803 			display->cdclk.max_cdclk_freq = 675000;
3804 	} else if (display->platform.cherryview) {
3805 		display->cdclk.max_cdclk_freq = 320000;
3806 	} else if (display->platform.valleyview) {
3807 		display->cdclk.max_cdclk_freq = 400000;
3808 	} else {
3809 		/* otherwise assume cdclk is fixed */
3810 		display->cdclk.max_cdclk_freq = display->cdclk.hw.cdclk;
3811 	}
3812 
3813 	display->cdclk.max_dotclk_freq = intel_compute_max_dotclk(display);
3814 
3815 	drm_dbg(display->drm, "Max CD clock rate: %d kHz\n",
3816 		display->cdclk.max_cdclk_freq);
3817 
3818 	drm_dbg(display->drm, "Max dotclock rate: %d kHz\n",
3819 		display->cdclk.max_dotclk_freq);
3820 }
3821 
3822 /**
3823  * intel_update_cdclk - Determine the current CDCLK frequency
3824  * @display: display instance
3825  *
3826  * Determine the current CDCLK frequency.
3827  */
3828 void intel_update_cdclk(struct intel_display *display)
3829 {
3830 	intel_cdclk_get_cdclk(display, &display->cdclk.hw);
3831 
3832 	/*
3833 	 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
3834 	 * Programmng [sic] note: bit[9:2] should be programmed to the number
3835 	 * of cdclk that generates 4MHz reference clock freq which is used to
3836 	 * generate GMBus clock. This will vary with the cdclk freq.
3837 	 */
3838 	if (display->platform.valleyview || display->platform.cherryview)
3839 		intel_de_write(display, GMBUSFREQ_VLV,
3840 			       DIV_ROUND_UP(display->cdclk.hw.cdclk, 1000));
3841 }
3842 
3843 static int dg1_rawclk(struct intel_display *display)
3844 {
3845 	/*
3846 	 * DG1 always uses a 38.4 MHz rawclk.  The bspec tells us
3847 	 * "Program Numerator=2, Denominator=4, Divider=37 decimal."
3848 	 */
3849 	intel_de_write(display, PCH_RAWCLK_FREQ,
3850 		       CNP_RAWCLK_DEN(4) | CNP_RAWCLK_DIV(37) | ICP_RAWCLK_NUM(2));
3851 
3852 	return 38400;
3853 }
3854 
3855 static int cnp_rawclk(struct intel_display *display)
3856 {
3857 	int divider, fraction;
3858 	u32 rawclk;
3859 
3860 	if (intel_de_read(display, SFUSE_STRAP) & SFUSE_STRAP_RAW_FREQUENCY) {
3861 		/* 24 MHz */
3862 		divider = 24000;
3863 		fraction = 0;
3864 	} else {
3865 		/* 19.2 MHz */
3866 		divider = 19000;
3867 		fraction = 200;
3868 	}
3869 
3870 	rawclk = CNP_RAWCLK_DIV(divider / 1000);
3871 	if (fraction) {
3872 		int numerator = 1;
3873 
3874 		rawclk |= CNP_RAWCLK_DEN(DIV_ROUND_CLOSEST(numerator * 1000,
3875 							   fraction) - 1);
3876 		if (INTEL_PCH_TYPE(display) >= PCH_ICP)
3877 			rawclk |= ICP_RAWCLK_NUM(numerator);
3878 	}
3879 
3880 	intel_de_write(display, PCH_RAWCLK_FREQ, rawclk);
3881 	return divider + fraction;
3882 }
3883 
3884 static int pch_rawclk(struct intel_display *display)
3885 {
3886 	return (intel_de_read(display, PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
3887 }
3888 
3889 static int i9xx_hrawclk(struct intel_display *display)
3890 {
3891 	/* hrawclock is 1/4 the FSB frequency */
3892 	return DIV_ROUND_CLOSEST(intel_fsb_freq(display), 4);
3893 }
3894 
3895 /**
3896  * intel_read_rawclk - Determine the current RAWCLK frequency
3897  * @display: display instance
3898  *
3899  * Determine the current RAWCLK frequency. RAWCLK is a fixed
3900  * frequency clock so this needs to done only once.
3901  */
3902 u32 intel_read_rawclk(struct intel_display *display)
3903 {
3904 	u32 freq;
3905 
3906 	if (INTEL_PCH_TYPE(display) >= PCH_MTL)
3907 		/*
3908 		 * MTL always uses a 38.4 MHz rawclk.  The bspec tells us
3909 		 * "RAWCLK_FREQ defaults to the values for 38.4 and does
3910 		 * not need to be programmed."
3911 		 */
3912 		freq = 38400;
3913 	else if (INTEL_PCH_TYPE(display) >= PCH_DG1)
3914 		freq = dg1_rawclk(display);
3915 	else if (INTEL_PCH_TYPE(display) >= PCH_CNP)
3916 		freq = cnp_rawclk(display);
3917 	else if (HAS_PCH_SPLIT(display))
3918 		freq = pch_rawclk(display);
3919 	else if (display->platform.valleyview || display->platform.cherryview)
3920 		freq = vlv_clock_get_hrawclk(display->drm);
3921 	else if (DISPLAY_VER(display) >= 3)
3922 		freq = i9xx_hrawclk(display);
3923 	else
3924 		/* no rawclk on other platforms, or no need to know it */
3925 		return 0;
3926 
3927 	return freq;
3928 }
3929 
3930 static int i915_cdclk_info_show(struct seq_file *m, void *unused)
3931 {
3932 	struct intel_display *display = m->private;
3933 
3934 	seq_printf(m, "Current CD clock frequency: %d kHz\n", display->cdclk.hw.cdclk);
3935 	seq_printf(m, "Max CD clock frequency: %d kHz\n", display->cdclk.max_cdclk_freq);
3936 	seq_printf(m, "Max pixel clock frequency: %d kHz\n", display->cdclk.max_dotclk_freq);
3937 
3938 	return 0;
3939 }
3940 
3941 DEFINE_SHOW_ATTRIBUTE(i915_cdclk_info);
3942 
3943 void intel_cdclk_debugfs_register(struct intel_display *display)
3944 {
3945 	debugfs_create_file("i915_cdclk_info", 0444, display->drm->debugfs_root,
3946 			    display, &i915_cdclk_info_fops);
3947 }
3948 
3949 static const struct intel_cdclk_funcs xe3lpd_cdclk_funcs = {
3950 	.get_cdclk = bxt_get_cdclk,
3951 	.set_cdclk = bxt_set_cdclk,
3952 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3953 	.calc_voltage_level = xe3lpd_calc_voltage_level,
3954 };
3955 
3956 static const struct intel_cdclk_funcs mtl_cdclk_funcs = {
3957 	.get_cdclk = bxt_get_cdclk,
3958 	.set_cdclk = bxt_set_cdclk,
3959 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3960 	.calc_voltage_level = rplu_calc_voltage_level,
3961 };
3962 
3963 static const struct intel_cdclk_funcs dg2_cdclk_funcs = {
3964 	.get_cdclk = bxt_get_cdclk,
3965 	.set_cdclk = bxt_set_cdclk,
3966 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3967 	.calc_voltage_level = tgl_calc_voltage_level,
3968 };
3969 
3970 static const struct intel_cdclk_funcs rplu_cdclk_funcs = {
3971 	.get_cdclk = bxt_get_cdclk,
3972 	.set_cdclk = bxt_set_cdclk,
3973 	.pre_notify = skl_cdclk_pcode_pre_notify,
3974 	.post_notify = skl_cdclk_pcode_post_notify,
3975 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3976 	.calc_voltage_level = rplu_calc_voltage_level,
3977 };
3978 
3979 static const struct intel_cdclk_funcs tgl_cdclk_funcs = {
3980 	.get_cdclk = bxt_get_cdclk,
3981 	.set_cdclk = bxt_set_cdclk,
3982 	.pre_notify = skl_cdclk_pcode_pre_notify,
3983 	.post_notify = skl_cdclk_pcode_post_notify,
3984 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3985 	.calc_voltage_level = tgl_calc_voltage_level,
3986 };
3987 
3988 static const struct intel_cdclk_funcs ehl_cdclk_funcs = {
3989 	.get_cdclk = bxt_get_cdclk,
3990 	.set_cdclk = bxt_set_cdclk,
3991 	.pre_notify = skl_cdclk_pcode_pre_notify,
3992 	.post_notify = skl_cdclk_pcode_post_notify,
3993 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
3994 	.calc_voltage_level = ehl_calc_voltage_level,
3995 };
3996 
3997 static const struct intel_cdclk_funcs icl_cdclk_funcs = {
3998 	.get_cdclk = bxt_get_cdclk,
3999 	.set_cdclk = bxt_set_cdclk,
4000 	.pre_notify = skl_cdclk_pcode_pre_notify,
4001 	.post_notify = skl_cdclk_pcode_post_notify,
4002 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
4003 	.calc_voltage_level = icl_calc_voltage_level,
4004 };
4005 
4006 static const struct intel_cdclk_funcs bxt_cdclk_funcs = {
4007 	.get_cdclk = bxt_get_cdclk,
4008 	.set_cdclk = bxt_set_cdclk,
4009 	.pre_notify = bxt_cdclk_pcode_pre_notify,
4010 	.post_notify = bxt_cdclk_pcode_post_notify,
4011 	.modeset_calc_cdclk = bxt_modeset_calc_cdclk,
4012 	.calc_voltage_level = bxt_calc_voltage_level,
4013 };
4014 
4015 static const struct intel_cdclk_funcs skl_cdclk_funcs = {
4016 	.get_cdclk = skl_get_cdclk,
4017 	.set_cdclk = skl_set_cdclk,
4018 	.pre_notify = skl_cdclk_pcode_pre_notify,
4019 	.post_notify = skl_cdclk_pcode_post_notify,
4020 	.modeset_calc_cdclk = skl_modeset_calc_cdclk,
4021 };
4022 
4023 static const struct intel_cdclk_funcs bdw_cdclk_funcs = {
4024 	.get_cdclk = bdw_get_cdclk,
4025 	.set_cdclk = bdw_set_cdclk,
4026 	.pre_notify = bdw_cdclk_pcode_pre_notify,
4027 	.post_notify = bdw_cdclk_pcode_post_notify,
4028 	.modeset_calc_cdclk = bdw_modeset_calc_cdclk,
4029 };
4030 
4031 static const struct intel_cdclk_funcs chv_cdclk_funcs = {
4032 	.get_cdclk = vlv_get_cdclk,
4033 	.set_cdclk = chv_set_cdclk,
4034 	.modeset_calc_cdclk = vlv_modeset_calc_cdclk,
4035 };
4036 
4037 static const struct intel_cdclk_funcs vlv_cdclk_funcs = {
4038 	.get_cdclk = vlv_get_cdclk,
4039 	.set_cdclk = vlv_set_cdclk,
4040 	.modeset_calc_cdclk = vlv_modeset_calc_cdclk,
4041 };
4042 
4043 static const struct intel_cdclk_funcs hsw_cdclk_funcs = {
4044 	.get_cdclk = hsw_get_cdclk,
4045 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4046 };
4047 
4048 /* SNB, IVB, 965G, 945G */
4049 static const struct intel_cdclk_funcs fixed_400mhz_cdclk_funcs = {
4050 	.get_cdclk = fixed_400mhz_get_cdclk,
4051 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4052 };
4053 
4054 static const struct intel_cdclk_funcs ilk_cdclk_funcs = {
4055 	.get_cdclk = fixed_450mhz_get_cdclk,
4056 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4057 };
4058 
4059 static const struct intel_cdclk_funcs gm45_cdclk_funcs = {
4060 	.get_cdclk = gm45_get_cdclk,
4061 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4062 };
4063 
4064 /* G45 uses G33 */
4065 
4066 static const struct intel_cdclk_funcs i965gm_cdclk_funcs = {
4067 	.get_cdclk = i965gm_get_cdclk,
4068 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4069 };
4070 
4071 /* i965G uses fixed 400 */
4072 
4073 static const struct intel_cdclk_funcs pnv_cdclk_funcs = {
4074 	.get_cdclk = pnv_get_cdclk,
4075 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4076 };
4077 
4078 static const struct intel_cdclk_funcs g33_cdclk_funcs = {
4079 	.get_cdclk = g33_get_cdclk,
4080 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4081 };
4082 
4083 static const struct intel_cdclk_funcs i945gm_cdclk_funcs = {
4084 	.get_cdclk = i945gm_get_cdclk,
4085 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4086 };
4087 
4088 /* i945G uses fixed 400 */
4089 
4090 static const struct intel_cdclk_funcs i915gm_cdclk_funcs = {
4091 	.get_cdclk = i915gm_get_cdclk,
4092 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4093 };
4094 
4095 static const struct intel_cdclk_funcs i915g_cdclk_funcs = {
4096 	.get_cdclk = fixed_333mhz_get_cdclk,
4097 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4098 };
4099 
4100 static const struct intel_cdclk_funcs i865g_cdclk_funcs = {
4101 	.get_cdclk = fixed_266mhz_get_cdclk,
4102 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4103 };
4104 
4105 static const struct intel_cdclk_funcs i85x_cdclk_funcs = {
4106 	.get_cdclk = i85x_get_cdclk,
4107 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4108 };
4109 
4110 static const struct intel_cdclk_funcs i845g_cdclk_funcs = {
4111 	.get_cdclk = fixed_200mhz_get_cdclk,
4112 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4113 };
4114 
4115 static const struct intel_cdclk_funcs i830_cdclk_funcs = {
4116 	.get_cdclk = fixed_133mhz_get_cdclk,
4117 	.modeset_calc_cdclk = fixed_modeset_calc_cdclk,
4118 };
4119 
4120 /**
4121  * intel_init_cdclk_hooks - Initialize CDCLK related modesetting hooks
4122  * @display: display instance
4123  */
4124 void intel_init_cdclk_hooks(struct intel_display *display)
4125 {
4126 	if (DISPLAY_VER(display) >= 35) {
4127 		display->cdclk.funcs = &xe3lpd_cdclk_funcs;
4128 		display->cdclk.table = xe3p_lpd_cdclk_table;
4129 	} else if (DISPLAY_VER(display) >= 30) {
4130 		display->cdclk.funcs = &xe3lpd_cdclk_funcs;
4131 		display->cdclk.table = xe3lpd_cdclk_table;
4132 	} else if (DISPLAY_VER(display) >= 20) {
4133 		display->cdclk.funcs = &mtl_cdclk_funcs;
4134 		display->cdclk.table = xe2lpd_cdclk_table;
4135 	} else if (DISPLAY_VERx100(display) >= 1401) {
4136 		display->cdclk.funcs = &mtl_cdclk_funcs;
4137 		display->cdclk.table = xe2hpd_cdclk_table;
4138 	} else if (DISPLAY_VER(display) >= 14) {
4139 		display->cdclk.funcs = &mtl_cdclk_funcs;
4140 		display->cdclk.table = mtl_cdclk_table;
4141 	} else if (display->platform.dg2) {
4142 		display->cdclk.funcs = &dg2_cdclk_funcs;
4143 		display->cdclk.table = dg2_cdclk_table;
4144 	} else if (display->platform.alderlake_p) {
4145 		/* Wa_22011320316:adl-p[a0] */
4146 		if (intel_display_wa(display, INTEL_DISPLAY_WA_22011320316)) {
4147 			display->cdclk.table = adlp_a_step_cdclk_table;
4148 			display->cdclk.funcs = &tgl_cdclk_funcs;
4149 		} else if (display->platform.alderlake_p_raptorlake_u) {
4150 			display->cdclk.table = rplu_cdclk_table;
4151 			display->cdclk.funcs = &rplu_cdclk_funcs;
4152 		} else {
4153 			display->cdclk.table = adlp_cdclk_table;
4154 			display->cdclk.funcs = &tgl_cdclk_funcs;
4155 		}
4156 	} else if (display->platform.rocketlake) {
4157 		display->cdclk.funcs = &tgl_cdclk_funcs;
4158 		display->cdclk.table = rkl_cdclk_table;
4159 	} else if (DISPLAY_VER(display) >= 12) {
4160 		display->cdclk.funcs = &tgl_cdclk_funcs;
4161 		display->cdclk.table = icl_cdclk_table;
4162 	} else if (display->platform.jasperlake || display->platform.elkhartlake) {
4163 		display->cdclk.funcs = &ehl_cdclk_funcs;
4164 		display->cdclk.table = icl_cdclk_table;
4165 	} else if (DISPLAY_VER(display) >= 11) {
4166 		display->cdclk.funcs = &icl_cdclk_funcs;
4167 		display->cdclk.table = icl_cdclk_table;
4168 	} else if (display->platform.geminilake || display->platform.broxton) {
4169 		display->cdclk.funcs = &bxt_cdclk_funcs;
4170 		if (display->platform.geminilake)
4171 			display->cdclk.table = glk_cdclk_table;
4172 		else
4173 			display->cdclk.table = bxt_cdclk_table;
4174 	} else if (DISPLAY_VER(display) == 9) {
4175 		display->cdclk.funcs = &skl_cdclk_funcs;
4176 	} else if (display->platform.broadwell) {
4177 		display->cdclk.funcs = &bdw_cdclk_funcs;
4178 	} else if (display->platform.haswell) {
4179 		display->cdclk.funcs = &hsw_cdclk_funcs;
4180 	} else if (display->platform.cherryview) {
4181 		display->cdclk.funcs = &chv_cdclk_funcs;
4182 	} else if (display->platform.valleyview) {
4183 		display->cdclk.funcs = &vlv_cdclk_funcs;
4184 	} else if (display->platform.sandybridge || display->platform.ivybridge) {
4185 		display->cdclk.funcs = &fixed_400mhz_cdclk_funcs;
4186 	} else if (display->platform.ironlake) {
4187 		display->cdclk.funcs = &ilk_cdclk_funcs;
4188 	} else if (display->platform.gm45) {
4189 		display->cdclk.funcs = &gm45_cdclk_funcs;
4190 	} else if (display->platform.g45) {
4191 		display->cdclk.funcs = &g33_cdclk_funcs;
4192 	} else if (display->platform.i965gm) {
4193 		display->cdclk.funcs = &i965gm_cdclk_funcs;
4194 	} else if (display->platform.i965g) {
4195 		display->cdclk.funcs = &fixed_400mhz_cdclk_funcs;
4196 	} else if (display->platform.pineview) {
4197 		display->cdclk.funcs = &pnv_cdclk_funcs;
4198 	} else if (display->platform.g33) {
4199 		display->cdclk.funcs = &g33_cdclk_funcs;
4200 	} else if (display->platform.i945gm) {
4201 		display->cdclk.funcs = &i945gm_cdclk_funcs;
4202 	} else if (display->platform.i945g) {
4203 		display->cdclk.funcs = &fixed_400mhz_cdclk_funcs;
4204 	} else if (display->platform.i915gm) {
4205 		display->cdclk.funcs = &i915gm_cdclk_funcs;
4206 	} else if (display->platform.i915g) {
4207 		display->cdclk.funcs = &i915g_cdclk_funcs;
4208 	} else if (display->platform.i865g) {
4209 		display->cdclk.funcs = &i865g_cdclk_funcs;
4210 	} else if (display->platform.i85x) {
4211 		display->cdclk.funcs = &i85x_cdclk_funcs;
4212 	} else if (display->platform.i845g) {
4213 		display->cdclk.funcs = &i845g_cdclk_funcs;
4214 	} else if (display->platform.i830) {
4215 		display->cdclk.funcs = &i830_cdclk_funcs;
4216 	}
4217 
4218 	if (drm_WARN(display->drm, !display->cdclk.funcs,
4219 		     "Unknown platform. Assuming i830\n"))
4220 		display->cdclk.funcs = &i830_cdclk_funcs;
4221 }
4222 
4223 int intel_cdclk_logical(const struct intel_cdclk_state *cdclk_state)
4224 {
4225 	return cdclk_state->logical.cdclk;
4226 }
4227 
4228 int intel_cdclk_actual(const struct intel_cdclk_state *cdclk_state)
4229 {
4230 	return cdclk_state->actual.cdclk;
4231 }
4232 
4233 int intel_cdclk_actual_voltage_level(const struct intel_cdclk_state *cdclk_state)
4234 {
4235 	return cdclk_state->actual.voltage_level;
4236 }
4237 
4238 int intel_cdclk_min_cdclk(const struct intel_cdclk_state *cdclk_state, enum pipe pipe)
4239 {
4240 	return cdclk_state->min_cdclk[pipe];
4241 }
4242 
4243 bool intel_cdclk_pmdemand_needs_update(struct intel_atomic_state *state)
4244 {
4245 	const struct intel_cdclk_state *new_cdclk_state, *old_cdclk_state;
4246 
4247 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
4248 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
4249 
4250 	if (new_cdclk_state &&
4251 	    (new_cdclk_state->actual.cdclk != old_cdclk_state->actual.cdclk ||
4252 	     new_cdclk_state->actual.voltage_level != old_cdclk_state->actual.voltage_level))
4253 		return true;
4254 
4255 	return false;
4256 }
4257 
4258 void intel_cdclk_force_min_cdclk(struct intel_cdclk_state *cdclk_state, int force_min_cdclk)
4259 {
4260 	cdclk_state->force_min_cdclk = force_min_cdclk;
4261 }
4262 
4263 void intel_cdclk_read_hw(struct intel_display *display)
4264 {
4265 	struct intel_cdclk_state *cdclk_state;
4266 
4267 	cdclk_state = to_intel_cdclk_state(display->cdclk.obj.state);
4268 
4269 	intel_update_cdclk(display);
4270 	intel_cdclk_dump_config(display, &display->cdclk.hw, "Current CDCLK");
4271 	cdclk_state->actual = display->cdclk.hw;
4272 	cdclk_state->logical = display->cdclk.hw;
4273 }
4274 
4275 static int calc_cdclk(const struct intel_crtc_state *crtc_state, int min_cdclk)
4276 {
4277 	struct intel_display *display = to_intel_display(crtc_state);
4278 
4279 	if (DISPLAY_VER(display) >= 10 || display->platform.broxton) {
4280 		return bxt_calc_cdclk(display, min_cdclk);
4281 	} else if (DISPLAY_VER(display) == 9) {
4282 		int vco;
4283 
4284 		vco = display->cdclk.skl_preferred_vco_freq;
4285 		if (vco == 0)
4286 			vco = 8100000;
4287 
4288 		return skl_calc_cdclk(min_cdclk, vco);
4289 	} else if (display->platform.broadwell) {
4290 		return bdw_calc_cdclk(min_cdclk);
4291 	} else if (display->platform.cherryview || display->platform.valleyview) {
4292 		return vlv_calc_cdclk(display, min_cdclk);
4293 	} else {
4294 		return display->cdclk.max_cdclk_freq;
4295 	}
4296 }
4297 
4298 static unsigned int _intel_cdclk_prefill_adj(const struct intel_crtc_state *crtc_state,
4299 					     int clock, int min_cdclk)
4300 {
4301 	struct intel_display *display = to_intel_display(crtc_state);
4302 	int ppc = intel_cdclk_ppc(display, crtc_state->double_wide);
4303 	int cdclk = calc_cdclk(crtc_state, min_cdclk);
4304 
4305 	return min(0x10000, DIV_ROUND_UP_ULL((u64)clock << 16, ppc * cdclk));
4306 }
4307 
4308 unsigned int intel_cdclk_prefill_adjustment(const struct intel_crtc_state *crtc_state)
4309 {
4310 	/* FIXME use the actual min_cdclk for the pipe here */
4311 	return intel_cdclk_prefill_adjustment_worst(crtc_state);
4312 }
4313 
4314 unsigned int intel_cdclk_prefill_adjustment_worst(const struct intel_crtc_state *crtc_state)
4315 {
4316 	int clock = crtc_state->hw.pipe_mode.crtc_clock;
4317 	int min_cdclk;
4318 
4319 	/*
4320 	 * FIXME could perhaps consider a few more of the factors
4321 	 * that go the per-crtc min_cdclk. Namely anything that
4322 	 * only changes during full modesets.
4323 	 *
4324 	 * FIXME this assumes 1:1 scaling, but the other _worst() stuff
4325 	 * assumes max downscaling, so the final result will be
4326 	 * unrealistically bad. Figure out where the actual maximum value
4327 	 * lies and use that to compute a more realistic worst case
4328 	 * estimate...
4329 	 */
4330 	min_cdclk = _intel_pixel_rate_to_cdclk(crtc_state, clock);
4331 
4332 	return _intel_cdclk_prefill_adj(crtc_state, clock, min_cdclk);
4333 }
4334 
4335 int intel_cdclk_min_cdclk_for_prefill(const struct intel_crtc_state *crtc_state,
4336 				      unsigned int prefill_lines_unadjusted,
4337 				      unsigned int prefill_lines_available)
4338 {
4339 	struct intel_display *display = to_intel_display(crtc_state);
4340 	const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4341 	int ppc = intel_cdclk_ppc(display, crtc_state->double_wide);
4342 
4343 	return DIV_ROUND_UP_ULL(mul_u32_u32(pipe_mode->crtc_clock, prefill_lines_unadjusted),
4344 				ppc * prefill_lines_available);
4345 }
4346