intel_cdclk.c 79.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright © 2006-2017 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

24
#include <linux/time.h>
25

26
#include "intel_atomic.h"
27
#include "intel_bw.h"
28
#include "intel_cdclk.h"
29
#include "intel_display_types.h"
30
#include "intel_sideband.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

/**
 * DOC: CDCLK / RAWCLK
 *
 * The display engine uses several different clocks to do its work. There
 * are two main clocks involved that aren't directly related to the actual
 * pixel clock or any symbol/bit clock of the actual output port. These
 * are the core display clock (CDCLK) and RAWCLK.
 *
 * CDCLK clocks most of the display pipe logic, and thus its frequency
 * must be high enough to support the rate at which pixels are flowing
 * through the pipes. Downscaling must also be accounted as that increases
 * the effective pixel rate.
 *
 * On several platforms the CDCLK frequency can be changed dynamically
 * to minimize power consumption for a given display configuration.
 * Typically changes to the CDCLK frequency require all the display pipes
 * to be shut down while the frequency is being changed.
 *
 * On SKL+ the DMC will toggle the CDCLK off/on during DC5/6 entry/exit.
 * DMC will not change the active CDCLK frequency however, so that part
 * will still be performed by the driver directly.
 *
 * RAWCLK is a fixed frequency clock, often used by various auxiliary
 * blocks such as AUX CH or backlight PWM. Hence the only thing we
 * really need to know about RAWCLK is its frequency so that various
 * dividers can be programmed correctly.
 */

60
static void fixed_133mhz_get_cdclk(struct drm_i915_private *dev_priv,
61
				   struct intel_cdclk_config *cdclk_config)
62
{
63
	cdclk_config->cdclk = 133333;
64 65
}

66
static void fixed_200mhz_get_cdclk(struct drm_i915_private *dev_priv,
67
				   struct intel_cdclk_config *cdclk_config)
68
{
69
	cdclk_config->cdclk = 200000;
70 71
}

72
static void fixed_266mhz_get_cdclk(struct drm_i915_private *dev_priv,
73
				   struct intel_cdclk_config *cdclk_config)
74
{
75
	cdclk_config->cdclk = 266667;
76 77
}

78
static void fixed_333mhz_get_cdclk(struct drm_i915_private *dev_priv,
79
				   struct intel_cdclk_config *cdclk_config)
80
{
81
	cdclk_config->cdclk = 333333;
82 83
}

84
static void fixed_400mhz_get_cdclk(struct drm_i915_private *dev_priv,
85
				   struct intel_cdclk_config *cdclk_config)
86
{
87
	cdclk_config->cdclk = 400000;
88 89
}

90
static void fixed_450mhz_get_cdclk(struct drm_i915_private *dev_priv,
91
				   struct intel_cdclk_config *cdclk_config)
92
{
93
	cdclk_config->cdclk = 450000;
94 95
}

96
static void i85x_get_cdclk(struct drm_i915_private *dev_priv,
97
			   struct intel_cdclk_config *cdclk_config)
98 99 100 101 102 103 104 105 106
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
	u16 hpllcc = 0;

	/*
	 * 852GM/852GMV only supports 133 MHz and the HPLLCC
	 * encoding is different :(
	 * FIXME is this the right way to detect 852GM/852GMV?
	 */
107
	if (pdev->revision == 0x1) {
108
		cdclk_config->cdclk = 133333;
109 110
		return;
	}
111 112 113 114 115 116 117 118 119 120 121

	pci_bus_read_config_word(pdev->bus,
				 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);

	/* Assume that the hardware is in the high speed state.  This
	 * should be the default.
	 */
	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
	case GC_CLOCK_133_200:
	case GC_CLOCK_133_200_2:
	case GC_CLOCK_100_200:
122
		cdclk_config->cdclk = 200000;
123
		break;
124
	case GC_CLOCK_166_250:
125
		cdclk_config->cdclk = 250000;
126
		break;
127
	case GC_CLOCK_100_133:
128
		cdclk_config->cdclk = 133333;
129
		break;
130 131 132
	case GC_CLOCK_133_266:
	case GC_CLOCK_133_266_2:
	case GC_CLOCK_166_266:
133
		cdclk_config->cdclk = 266667;
134
		break;
135 136 137
	}
}

138
static void i915gm_get_cdclk(struct drm_i915_private *dev_priv,
139
			     struct intel_cdclk_config *cdclk_config)
140 141 142 143 144 145
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
	u16 gcfgc = 0;

	pci_read_config_word(pdev, GCFGC, &gcfgc);

146
	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
147
		cdclk_config->cdclk = 133333;
148 149
		return;
	}
150 151 152

	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
	case GC_DISPLAY_CLOCK_333_320_MHZ:
153
		cdclk_config->cdclk = 333333;
154
		break;
155 156
	default:
	case GC_DISPLAY_CLOCK_190_200_MHZ:
157
		cdclk_config->cdclk = 190000;
158
		break;
159 160 161
	}
}

162
static void i945gm_get_cdclk(struct drm_i915_private *dev_priv,
163
			     struct intel_cdclk_config *cdclk_config)
164 165 166 167 168 169
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
	u16 gcfgc = 0;

	pci_read_config_word(pdev, GCFGC, &gcfgc);

170
	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
171
		cdclk_config->cdclk = 133333;
172 173
		return;
	}
174 175 176

	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
	case GC_DISPLAY_CLOCK_333_320_MHZ:
177
		cdclk_config->cdclk = 320000;
178
		break;
179 180
	default:
	case GC_DISPLAY_CLOCK_190_200_MHZ:
181
		cdclk_config->cdclk = 200000;
182
		break;
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	}
}

static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
{
	static const unsigned int blb_vco[8] = {
		[0] = 3200000,
		[1] = 4000000,
		[2] = 5333333,
		[3] = 4800000,
		[4] = 6400000,
	};
	static const unsigned int pnv_vco[8] = {
		[0] = 3200000,
		[1] = 4000000,
		[2] = 5333333,
		[3] = 4800000,
		[4] = 2666667,
	};
	static const unsigned int cl_vco[8] = {
		[0] = 3200000,
		[1] = 4000000,
		[2] = 5333333,
		[3] = 6400000,
		[4] = 3333333,
		[5] = 3566667,
		[6] = 4266667,
	};
	static const unsigned int elk_vco[8] = {
		[0] = 3200000,
		[1] = 4000000,
		[2] = 5333333,
		[3] = 4800000,
	};
	static const unsigned int ctg_vco[8] = {
		[0] = 3200000,
		[1] = 4000000,
		[2] = 5333333,
		[3] = 6400000,
		[4] = 2666667,
		[5] = 4266667,
	};
	const unsigned int *vco_table;
	unsigned int vco;
227
	u8 tmp = 0;
228 229 230 231

	/* FIXME other chipsets? */
	if (IS_GM45(dev_priv))
		vco_table = ctg_vco;
232
	else if (IS_G45(dev_priv))
233 234 235 236 237 238 239 240 241 242
		vco_table = elk_vco;
	else if (IS_I965GM(dev_priv))
		vco_table = cl_vco;
	else if (IS_PINEVIEW(dev_priv))
		vco_table = pnv_vco;
	else if (IS_G33(dev_priv))
		vco_table = blb_vco;
	else
		return 0;

243 244
	tmp = intel_de_read(dev_priv,
			    IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
245 246 247

	vco = vco_table[tmp & 0x7];
	if (vco == 0)
248 249
		drm_err(&dev_priv->drm, "Bad HPLL VCO (HPLLVCO=0x%02x)\n",
			tmp);
250
	else
251
		drm_dbg_kms(&dev_priv->drm, "HPLL VCO %u kHz\n", vco);
252 253 254 255

	return vco;
}

256
static void g33_get_cdclk(struct drm_i915_private *dev_priv,
257
			  struct intel_cdclk_config *cdclk_config)
258 259
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
260 261 262 263 264
	static const u8 div_3200[] = { 12, 10,  8,  7, 5, 16 };
	static const u8 div_4000[] = { 14, 12, 10,  8, 6, 20 };
	static const u8 div_4800[] = { 20, 14, 12, 10, 8, 24 };
	static const u8 div_5333[] = { 20, 16, 12, 12, 8, 28 };
	const u8 *div_table;
265
	unsigned int cdclk_sel;
266
	u16 tmp = 0;
267

268
	cdclk_config->vco = intel_hpll_vco(dev_priv);
269

270 271 272 273 274 275 276
	pci_read_config_word(pdev, GCFGC, &tmp);

	cdclk_sel = (tmp >> 4) & 0x7;

	if (cdclk_sel >= ARRAY_SIZE(div_3200))
		goto fail;

277
	switch (cdclk_config->vco) {
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
	case 3200000:
		div_table = div_3200;
		break;
	case 4000000:
		div_table = div_4000;
		break;
	case 4800000:
		div_table = div_4800;
		break;
	case 5333333:
		div_table = div_5333;
		break;
	default:
		goto fail;
	}

294 295
	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco,
						div_table[cdclk_sel]);
296
	return;
297 298

fail:
299 300
	drm_err(&dev_priv->drm,
		"Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n",
301 302
		cdclk_config->vco, tmp);
	cdclk_config->cdclk = 190476;
303 304
}

305
static void pnv_get_cdclk(struct drm_i915_private *dev_priv,
306
			  struct intel_cdclk_config *cdclk_config)
307 308 309 310 311 312 313 314
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
	u16 gcfgc = 0;

	pci_read_config_word(pdev, GCFGC, &gcfgc);

	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
	case GC_DISPLAY_CLOCK_267_MHZ_PNV:
315
		cdclk_config->cdclk = 266667;
316
		break;
317
	case GC_DISPLAY_CLOCK_333_MHZ_PNV:
318
		cdclk_config->cdclk = 333333;
319
		break;
320
	case GC_DISPLAY_CLOCK_444_MHZ_PNV:
321
		cdclk_config->cdclk = 444444;
322
		break;
323
	case GC_DISPLAY_CLOCK_200_MHZ_PNV:
324
		cdclk_config->cdclk = 200000;
325
		break;
326
	default:
327 328
		drm_err(&dev_priv->drm,
			"Unknown pnv display core clock 0x%04x\n", gcfgc);
329
		/* fall through */
330
	case GC_DISPLAY_CLOCK_133_MHZ_PNV:
331
		cdclk_config->cdclk = 133333;
332
		break;
333
	case GC_DISPLAY_CLOCK_167_MHZ_PNV:
334
		cdclk_config->cdclk = 166667;
335
		break;
336 337 338
	}
}

339
static void i965gm_get_cdclk(struct drm_i915_private *dev_priv,
340
			     struct intel_cdclk_config *cdclk_config)
341 342
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
343 344 345 346
	static const u8 div_3200[] = { 16, 10,  8 };
	static const u8 div_4000[] = { 20, 12, 10 };
	static const u8 div_5333[] = { 24, 16, 14 };
	const u8 *div_table;
347
	unsigned int cdclk_sel;
348
	u16 tmp = 0;
349

350
	cdclk_config->vco = intel_hpll_vco(dev_priv);
351

352 353 354 355 356 357 358
	pci_read_config_word(pdev, GCFGC, &tmp);

	cdclk_sel = ((tmp >> 8) & 0x1f) - 1;

	if (cdclk_sel >= ARRAY_SIZE(div_3200))
		goto fail;

359
	switch (cdclk_config->vco) {
360 361 362 363 364 365 366 367 368 369 370 371 372
	case 3200000:
		div_table = div_3200;
		break;
	case 4000000:
		div_table = div_4000;
		break;
	case 5333333:
		div_table = div_5333;
		break;
	default:
		goto fail;
	}

373 374
	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco,
						div_table[cdclk_sel]);
375
	return;
376 377

fail:
378 379
	drm_err(&dev_priv->drm,
		"Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n",
380 381
		cdclk_config->vco, tmp);
	cdclk_config->cdclk = 200000;
382 383
}

384
static void gm45_get_cdclk(struct drm_i915_private *dev_priv,
385
			   struct intel_cdclk_config *cdclk_config)
386 387
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
388
	unsigned int cdclk_sel;
389
	u16 tmp = 0;
390

391
	cdclk_config->vco = intel_hpll_vco(dev_priv);
392

393 394 395 396
	pci_read_config_word(pdev, GCFGC, &tmp);

	cdclk_sel = (tmp >> 12) & 0x1;

397
	switch (cdclk_config->vco) {
398 399 400
	case 2666667:
	case 4000000:
	case 5333333:
401
		cdclk_config->cdclk = cdclk_sel ? 333333 : 222222;
402
		break;
403
	case 3200000:
404
		cdclk_config->cdclk = cdclk_sel ? 320000 : 228571;
405
		break;
406
	default:
407 408
		drm_err(&dev_priv->drm,
			"Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n",
409 410
			cdclk_config->vco, tmp);
		cdclk_config->cdclk = 222222;
411
		break;
412 413 414
	}
}

415
static void hsw_get_cdclk(struct drm_i915_private *dev_priv,
416
			  struct intel_cdclk_config *cdclk_config)
417
{
418
	u32 lcpll = intel_de_read(dev_priv, LCPLL_CTL);
419
	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
420 421

	if (lcpll & LCPLL_CD_SOURCE_FCLK)
422
		cdclk_config->cdclk = 800000;
423
	else if (intel_de_read(dev_priv, FUSE_STRAP) & HSW_CDCLK_LIMIT)
424
		cdclk_config->cdclk = 450000;
425
	else if (freq == LCPLL_CLK_FREQ_450)
426
		cdclk_config->cdclk = 450000;
427
	else if (IS_HSW_ULT(dev_priv))
428
		cdclk_config->cdclk = 337500;
429
	else
430
		cdclk_config->cdclk = 540000;
431 432
}

433
static int vlv_calc_cdclk(struct drm_i915_private *dev_priv, int min_cdclk)
434 435 436 437 438 439 440 441 442
{
	int freq_320 = (dev_priv->hpll_freq <<  1) % 320000 != 0 ?
		333333 : 320000;

	/*
	 * We seem to get an unstable or solid color picture at 200MHz.
	 * Not sure what's wrong. For now use 200MHz only when all pipes
	 * are off.
	 */
443
	if (IS_VALLEYVIEW(dev_priv) && min_cdclk > freq_320)
444
		return 400000;
445
	else if (min_cdclk > 266667)
446
		return freq_320;
447
	else if (min_cdclk > 0)
448 449 450 451 452
		return 266667;
	else
		return 200000;
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
static u8 vlv_calc_voltage_level(struct drm_i915_private *dev_priv, int cdclk)
{
	if (IS_VALLEYVIEW(dev_priv)) {
		if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
			return 2;
		else if (cdclk >= 266667)
			return 1;
		else
			return 0;
	} else {
		/*
		 * Specs are full of misinformation, but testing on actual
		 * hardware has shown that we just need to write the desired
		 * CCK divider into the Punit register.
		 */
		return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
	}
}

472
static void vlv_get_cdclk(struct drm_i915_private *dev_priv,
473
			  struct intel_cdclk_config *cdclk_config)
474
{
475 476
	u32 val;

477 478 479
	vlv_iosf_sb_get(dev_priv,
			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));

480 481 482 483
	cdclk_config->vco = vlv_get_hpll_vco(dev_priv);
	cdclk_config->cdclk = vlv_get_cck_clock(dev_priv, "cdclk",
						CCK_DISPLAY_CLOCK_CONTROL,
						cdclk_config->vco);
484

485
	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
486 487 488

	vlv_iosf_sb_put(dev_priv,
			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));
489 490

	if (IS_VALLEYVIEW(dev_priv))
491
		cdclk_config->voltage_level = (val & DSPFREQGUAR_MASK) >>
492 493
			DSPFREQGUAR_SHIFT;
	else
494
		cdclk_config->voltage_level = (val & DSPFREQGUAR_MASK_CHV) >>
495
			DSPFREQGUAR_SHIFT_CHV;
496 497 498 499 500 501 502 503 504 505 506
}

static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
{
	unsigned int credits, default_credits;

	if (IS_CHERRYVIEW(dev_priv))
		default_credits = PFI_CREDIT(12);
	else
		default_credits = PFI_CREDIT(8);

507
	if (dev_priv->cdclk.hw.cdclk >= dev_priv->czclk_freq) {
508 509 510 511 512 513 514 515 516 517 518 519 520
		/* CHV suggested value is 31 or 63 */
		if (IS_CHERRYVIEW(dev_priv))
			credits = PFI_CREDIT_63;
		else
			credits = PFI_CREDIT(15);
	} else {
		credits = default_credits;
	}

	/*
	 * WA - write default credits before re-programming
	 * FIXME: should we also set the resend bit here?
	 */
521 522
	intel_de_write(dev_priv, GCI_CONTROL,
		       VGA_FAST_MODE_DISABLE | default_credits);
523

524 525
	intel_de_write(dev_priv, GCI_CONTROL,
		       VGA_FAST_MODE_DISABLE | credits | PFI_CREDIT_RESEND);
526 527 528 529 530

	/*
	 * FIXME is this guaranteed to clear
	 * immediately or should we poll for it?
	 */
531 532
	drm_WARN_ON(&dev_priv->drm,
		    intel_de_read(dev_priv, GCI_CONTROL) & PFI_CREDIT_RESEND);
533 534
}

535
static void vlv_set_cdclk(struct drm_i915_private *dev_priv,
536
			  const struct intel_cdclk_config *cdclk_config,
537
			  enum pipe pipe)
538
{
539 540
	int cdclk = cdclk_config->cdclk;
	u32 val, cmd = cdclk_config->voltage_level;
541
	intel_wakeref_t wakeref;
542

543 544 545 546 547 548 549 550 551 552 553 554
	switch (cdclk) {
	case 400000:
	case 333333:
	case 320000:
	case 266667:
	case 200000:
		break;
	default:
		MISSING_CASE(cdclk);
		return;
	}

555 556 557
	/* There are cases where we can end up here with power domains
	 * off and a CDCLK frequency other than the minimum, like when
	 * issuing a modeset without actually changing any display after
558
	 * a system suspend.  So grab the display core domain, which covers
559 560
	 * the HW blocks needed for the following programming.
	 */
561
	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
562

563 564 565 566 567
	vlv_iosf_sb_get(dev_priv,
			BIT(VLV_IOSF_SB_CCK) |
			BIT(VLV_IOSF_SB_BUNIT) |
			BIT(VLV_IOSF_SB_PUNIT));

568
	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
569 570
	val &= ~DSPFREQGUAR_MASK;
	val |= (cmd << DSPFREQGUAR_SHIFT);
571 572
	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM) &
573 574
		      DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
		     50)) {
575 576
		drm_err(&dev_priv->drm,
			"timed out waiting for CDclk change\n");
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
	}

	if (cdclk == 400000) {
		u32 divider;

		divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1,
					    cdclk) - 1;

		/* adjust cdclk divider */
		val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
		val &= ~CCK_FREQUENCY_VALUES;
		val |= divider;
		vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);

		if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
			      CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
			     50))
594 595
			drm_err(&dev_priv->drm,
				"timed out waiting for CDclk change\n");
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
	}

	/* adjust self-refresh exit latency value */
	val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
	val &= ~0x7f;

	/*
	 * For high bandwidth configs, we set a higher latency in the bunit
	 * so that the core display fetch happens in time to avoid underruns.
	 */
	if (cdclk == 400000)
		val |= 4500 / 250; /* 4.5 usec */
	else
		val |= 3000 / 250; /* 3.0 usec */
	vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);

612
	vlv_iosf_sb_put(dev_priv,
613 614 615
			BIT(VLV_IOSF_SB_CCK) |
			BIT(VLV_IOSF_SB_BUNIT) |
			BIT(VLV_IOSF_SB_PUNIT));
616 617

	intel_update_cdclk(dev_priv);
618 619

	vlv_program_pfi_credits(dev_priv);
620

621
	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
622 623
}

624
static void chv_set_cdclk(struct drm_i915_private *dev_priv,
625
			  const struct intel_cdclk_config *cdclk_config,
626
			  enum pipe pipe)
627
{
628 629
	int cdclk = cdclk_config->cdclk;
	u32 val, cmd = cdclk_config->voltage_level;
630
	intel_wakeref_t wakeref;
631 632 633 634 635 636 637 638 639 640 641 642

	switch (cdclk) {
	case 333333:
	case 320000:
	case 266667:
	case 200000:
		break;
	default:
		MISSING_CASE(cdclk);
		return;
	}

643 644 645
	/* There are cases where we can end up here with power domains
	 * off and a CDCLK frequency other than the minimum, like when
	 * issuing a modeset without actually changing any display after
646
	 * a system suspend.  So grab the display core domain, which covers
647 648
	 * the HW blocks needed for the following programming.
	 */
649
	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
650

651
	vlv_punit_get(dev_priv);
652
	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
653 654
	val &= ~DSPFREQGUAR_MASK_CHV;
	val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
655 656
	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM) &
657 658
		      DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
		     50)) {
659 660
		drm_err(&dev_priv->drm,
			"timed out waiting for CDclk change\n");
661
	}
662 663

	vlv_punit_put(dev_priv);
664 665

	intel_update_cdclk(dev_priv);
666 667

	vlv_program_pfi_credits(dev_priv);
668

669
	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
670 671
}

672
static int bdw_calc_cdclk(int min_cdclk)
673
{
674
	if (min_cdclk > 540000)
675
		return 675000;
676
	else if (min_cdclk > 450000)
677
		return 540000;
678
	else if (min_cdclk > 337500)
679 680 681 682 683
		return 450000;
	else
		return 337500;
}

684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
static u8 bdw_calc_voltage_level(int cdclk)
{
	switch (cdclk) {
	default:
	case 337500:
		return 2;
	case 450000:
		return 0;
	case 540000:
		return 1;
	case 675000:
		return 3;
	}
}

699
static void bdw_get_cdclk(struct drm_i915_private *dev_priv,
700
			  struct intel_cdclk_config *cdclk_config)
701
{
702
	u32 lcpll = intel_de_read(dev_priv, LCPLL_CTL);
703
	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
704 705

	if (lcpll & LCPLL_CD_SOURCE_FCLK)
706
		cdclk_config->cdclk = 800000;
707
	else if (intel_de_read(dev_priv, FUSE_STRAP) & HSW_CDCLK_LIMIT)
708
		cdclk_config->cdclk = 450000;
709
	else if (freq == LCPLL_CLK_FREQ_450)
710
		cdclk_config->cdclk = 450000;
711
	else if (freq == LCPLL_CLK_FREQ_54O_BDW)
712
		cdclk_config->cdclk = 540000;
713
	else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
714
		cdclk_config->cdclk = 337500;
715
	else
716
		cdclk_config->cdclk = 675000;
717 718 719 720 721

	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
722 723
	cdclk_config->voltage_level =
		bdw_calc_voltage_level(cdclk_config->cdclk);
724 725
}

726
static void bdw_set_cdclk(struct drm_i915_private *dev_priv,
727
			  const struct intel_cdclk_config *cdclk_config,
728
			  enum pipe pipe)
729
{
730
	int cdclk = cdclk_config->cdclk;
731
	u32 val;
732 733
	int ret;

734 735 736 737 738 739 740
	if (drm_WARN(&dev_priv->drm,
		     (intel_de_read(dev_priv, LCPLL_CTL) &
		      (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
		       LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
		       LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
		       LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
		     "trying to change cdclk frequency with cdclk not enabled\n"))
741 742 743 744 745
		return;

	ret = sandybridge_pcode_write(dev_priv,
				      BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
	if (ret) {
746 747
		drm_err(&dev_priv->drm,
			"failed to inform pcode about cdclk change\n");
748 749 750
		return;
	}

751
	val = intel_de_read(dev_priv, LCPLL_CTL);
752
	val |= LCPLL_CD_SOURCE_FCLK;
753
	intel_de_write(dev_priv, LCPLL_CTL, val);
754

755 756 757 758
	/*
	 * According to the spec, it should be enough to poll for this 1 us.
	 * However, extensive testing shows that this can take longer.
	 */
759
	if (wait_for_us(intel_de_read(dev_priv, LCPLL_CTL) &
760
			LCPLL_CD_SOURCE_FCLK_DONE, 100))
761
		drm_err(&dev_priv->drm, "Switching to FCLK failed\n");
762

763
	val = intel_de_read(dev_priv, LCPLL_CTL);
764 765 766
	val &= ~LCPLL_CLK_FREQ_MASK;

	switch (cdclk) {
767 768 769 770 771 772
	default:
		MISSING_CASE(cdclk);
		/* fall through */
	case 337500:
		val |= LCPLL_CLK_FREQ_337_5_BDW;
		break;
773 774 775 776 777 778 779 780 781 782 783
	case 450000:
		val |= LCPLL_CLK_FREQ_450;
		break;
	case 540000:
		val |= LCPLL_CLK_FREQ_54O_BDW;
		break;
	case 675000:
		val |= LCPLL_CLK_FREQ_675_BDW;
		break;
	}

784
	intel_de_write(dev_priv, LCPLL_CTL, val);
785

786
	val = intel_de_read(dev_priv, LCPLL_CTL);
787
	val &= ~LCPLL_CD_SOURCE_FCLK;
788
	intel_de_write(dev_priv, LCPLL_CTL, val);
789

790 791
	if (wait_for_us((intel_de_read(dev_priv, LCPLL_CTL) &
			 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
792
		drm_err(&dev_priv->drm, "Switching back to LCPLL failed\n");
793

794
	sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
795
				cdclk_config->voltage_level);
796

797 798
	intel_de_write(dev_priv, CDCLK_FREQ,
		       DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
799 800 801 802

	intel_update_cdclk(dev_priv);
}

803
static int skl_calc_cdclk(int min_cdclk, int vco)
804 805
{
	if (vco == 8640000) {
806
		if (min_cdclk > 540000)
807
			return 617143;
808
		else if (min_cdclk > 432000)
809
			return 540000;
810
		else if (min_cdclk > 308571)
811 812 813 814
			return 432000;
		else
			return 308571;
	} else {
815
		if (min_cdclk > 540000)
816
			return 675000;
817
		else if (min_cdclk > 450000)
818
			return 540000;
819
		else if (min_cdclk > 337500)
820 821 822 823 824 825
			return 450000;
		else
			return 337500;
	}
}

826 827
static u8 skl_calc_voltage_level(int cdclk)
{
828
	if (cdclk > 540000)
829
		return 3;
830 831 832 833 834 835
	else if (cdclk > 450000)
		return 2;
	else if (cdclk > 337500)
		return 1;
	else
		return 0;
836 837
}

838
static void skl_dpll0_update(struct drm_i915_private *dev_priv,
839
			     struct intel_cdclk_config *cdclk_config)
840 841 842
{
	u32 val;

843 844
	cdclk_config->ref = 24000;
	cdclk_config->vco = 0;
845

846
	val = intel_de_read(dev_priv, LCPLL1_CTL);
847 848 849
	if ((val & LCPLL_PLL_ENABLE) == 0)
		return;

850
	if (drm_WARN_ON(&dev_priv->drm, (val & LCPLL_PLL_LOCK) == 0))
851 852
		return;

853
	val = intel_de_read(dev_priv, DPLL_CTRL1);
854

855 856 857 858 859
	if (drm_WARN_ON(&dev_priv->drm,
			(val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
				DPLL_CTRL1_SSC(SKL_DPLL0) |
				DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
			DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
860 861 862 863 864 865 866
		return;

	switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
867
		cdclk_config->vco = 8100000;
868 869 870
		break;
	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
871
		cdclk_config->vco = 8640000;
872 873 874 875 876 877 878
		break;
	default:
		MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
		break;
	}
}

879
static void skl_get_cdclk(struct drm_i915_private *dev_priv,
880
			  struct intel_cdclk_config *cdclk_config)
881 882 883
{
	u32 cdctl;

884
	skl_dpll0_update(dev_priv, cdclk_config);
885

886
	cdclk_config->cdclk = cdclk_config->bypass = cdclk_config->ref;
887

888
	if (cdclk_config->vco == 0)
889
		goto out;
890

891
	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
892

893
	if (cdclk_config->vco == 8640000) {
894 895
		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
		case CDCLK_FREQ_450_432:
896
			cdclk_config->cdclk = 432000;
897
			break;
898
		case CDCLK_FREQ_337_308:
899
			cdclk_config->cdclk = 308571;
900
			break;
901
		case CDCLK_FREQ_540:
902
			cdclk_config->cdclk = 540000;
903
			break;
904
		case CDCLK_FREQ_675_617:
905
			cdclk_config->cdclk = 617143;
906
			break;
907 908
		default:
			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
909
			break;
910 911 912 913
		}
	} else {
		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
		case CDCLK_FREQ_450_432:
914
			cdclk_config->cdclk = 450000;
915
			break;
916
		case CDCLK_FREQ_337_308:
917
			cdclk_config->cdclk = 337500;
918
			break;
919
		case CDCLK_FREQ_540:
920
			cdclk_config->cdclk = 540000;
921
			break;
922
		case CDCLK_FREQ_675_617:
923
			cdclk_config->cdclk = 675000;
924
			break;
925 926
		default:
			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
927
			break;
928 929
		}
	}
930 931 932 933 934 935

 out:
	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
936 937
	cdclk_config->voltage_level =
		skl_calc_voltage_level(cdclk_config->cdclk);
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
}

/* convert from kHz to .1 fixpoint MHz with -1MHz offset */
static int skl_cdclk_decimal(int cdclk)
{
	return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
}

static void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv,
					int vco)
{
	bool changed = dev_priv->skl_preferred_vco_freq != vco;

	dev_priv->skl_preferred_vco_freq = vco;

	if (changed)
		intel_update_max_cdclk(dev_priv);
}

static void skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
{
	u32 val;

961
	drm_WARN_ON(&dev_priv->drm, vco != 8100000 && vco != 8640000);
962 963 964 965 966 967 968 969 970 971

	/*
	 * We always enable DPLL0 with the lowest link rate possible, but still
	 * taking into account the VCO required to operate the eDP panel at the
	 * desired frequency. The usual DP link rates operate with a VCO of
	 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
	 * The modeset code is responsible for the selection of the exact link
	 * rate later on, with the constraint of choosing a frequency that
	 * works with vco.
	 */
972
	val = intel_de_read(dev_priv, DPLL_CTRL1);
973 974 975 976 977 978 979 980 981 982 983

	val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
		 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
	val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
	if (vco == 8640000)
		val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
					    SKL_DPLL0);
	else
		val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
					    SKL_DPLL0);

984 985
	intel_de_write(dev_priv, DPLL_CTRL1, val);
	intel_de_posting_read(dev_priv, DPLL_CTRL1);
986

987 988
	intel_de_write(dev_priv, LCPLL1_CTL,
		       intel_de_read(dev_priv, LCPLL1_CTL) | LCPLL_PLL_ENABLE);
989

990
	if (intel_de_wait_for_set(dev_priv, LCPLL1_CTL, LCPLL_PLL_LOCK, 5))
991
		drm_err(&dev_priv->drm, "DPLL0 not locked\n");
992

993
	dev_priv->cdclk.hw.vco = vco;
994 995 996 997 998 999 1000

	/* We'll want to keep using the current vco from now on. */
	skl_set_preferred_cdclk_vco(dev_priv, vco);
}

static void skl_dpll0_disable(struct drm_i915_private *dev_priv)
{
1001 1002
	intel_de_write(dev_priv, LCPLL1_CTL,
		       intel_de_read(dev_priv, LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
1003
	if (intel_de_wait_for_clear(dev_priv, LCPLL1_CTL, LCPLL_PLL_LOCK, 1))
1004
		drm_err(&dev_priv->drm, "Couldn't disable DPLL0\n");
1005

1006
	dev_priv->cdclk.hw.vco = 0;
1007 1008 1009
}

static void skl_set_cdclk(struct drm_i915_private *dev_priv,
1010
			  const struct intel_cdclk_config *cdclk_config,
1011
			  enum pipe pipe)
1012
{
1013 1014
	int cdclk = cdclk_config->cdclk;
	int vco = cdclk_config->vco;
1015
	u32 freq_select, cdclk_ctl;
1016 1017
	int ret;

1018 1019 1020 1021 1022 1023 1024 1025
	/*
	 * Based on WA#1183 CDCLK rates 308 and 617MHz CDCLK rates are
	 * unsupported on SKL. In theory this should never happen since only
	 * the eDP1.4 2.16 and 4.32Gbps rates require it, but eDP1.4 is not
	 * supported on SKL either, see the above WA. WARN whenever trying to
	 * use the corresponding VCO freq as that always leads to using the
	 * minimum 308MHz CDCLK.
	 */
1026 1027
	drm_WARN_ON_ONCE(&dev_priv->drm,
			 IS_SKYLAKE(dev_priv) && vco == 8640000);
1028

1029 1030 1031 1032 1033
	ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
				SKL_CDCLK_PREPARE_FOR_CHANGE,
				SKL_CDCLK_READY_FOR_CHANGE,
				SKL_CDCLK_READY_FOR_CHANGE, 3);
	if (ret) {
1034 1035
		drm_err(&dev_priv->drm,
			"Failed to inform PCU about cdclk change (%d)\n", ret);
1036 1037 1038
		return;
	}

1039
	/* Choose frequency for this cdclk */
1040
	switch (cdclk) {
1041
	default:
1042 1043 1044
		drm_WARN_ON(&dev_priv->drm,
			    cdclk != dev_priv->cdclk.hw.bypass);
		drm_WARN_ON(&dev_priv->drm, vco != 0);
1045 1046 1047 1048 1049
		/* fall through */
	case 308571:
	case 337500:
		freq_select = CDCLK_FREQ_337_308;
		break;
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	case 450000:
	case 432000:
		freq_select = CDCLK_FREQ_450_432;
		break;
	case 540000:
		freq_select = CDCLK_FREQ_540;
		break;
	case 617143:
	case 675000:
		freq_select = CDCLK_FREQ_675_617;
		break;
	}

1063 1064
	if (dev_priv->cdclk.hw.vco != 0 &&
	    dev_priv->cdclk.hw.vco != vco)
1065 1066
		skl_dpll0_disable(dev_priv);

1067
	cdclk_ctl = intel_de_read(dev_priv, CDCLK_CTL);
1068 1069 1070 1071 1072

	if (dev_priv->cdclk.hw.vco != vco) {
		/* Wa Display #1183: skl,kbl,cfl */
		cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
		cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
1073
		intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1074 1075 1076 1077
	}

	/* Wa Display #1183: skl,kbl,cfl */
	cdclk_ctl |= CDCLK_DIVMUX_CD_OVERRIDE;
1078 1079
	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
	intel_de_posting_read(dev_priv, CDCLK_CTL);
1080

1081
	if (dev_priv->cdclk.hw.vco != vco)
1082 1083
		skl_dpll0_enable(dev_priv, vco);

1084 1085
	/* Wa Display #1183: skl,kbl,cfl */
	cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
1086
	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1087 1088

	cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
1089
	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1090 1091 1092

	/* Wa Display #1183: skl,kbl,cfl */
	cdclk_ctl &= ~CDCLK_DIVMUX_CD_OVERRIDE;
1093 1094
	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
	intel_de_posting_read(dev_priv, CDCLK_CTL);
1095 1096

	/* inform PCU of the change */
1097
	sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
1098
				cdclk_config->voltage_level);
1099 1100 1101 1102 1103 1104

	intel_update_cdclk(dev_priv);
}

static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
{
1105
	u32 cdctl, expected;
1106 1107 1108 1109 1110 1111

	/*
	 * check if the pre-os initialized the display
	 * There is SWF18 scratchpad register defined which is set by the
	 * pre-os which can be used by the OS drivers to check the status
	 */
1112
	if ((intel_de_read(dev_priv, SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
1113 1114 1115
		goto sanitize;

	intel_update_cdclk(dev_priv);
1116
	intel_dump_cdclk_config(&dev_priv->cdclk.hw, "Current CDCLK");
1117

1118
	/* Is PLL enabled and locked ? */
1119
	if (dev_priv->cdclk.hw.vco == 0 ||
1120
	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
1121 1122 1123 1124 1125 1126 1127 1128
		goto sanitize;

	/* DPLL okay; verify the cdclock
	 *
	 * Noticed in some instances that the freq selection is correct but
	 * decimal part is programmed wrong from BIOS where pre-os does not
	 * enable display. Verify the same as well.
	 */
1129
	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
1130
	expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
1131
		skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);
1132 1133 1134 1135 1136
	if (cdctl == expected)
		/* All well; nothing to sanitize */
		return;

sanitize:
1137
	drm_dbg_kms(&dev_priv->drm, "Sanitizing cdclk programmed by pre-os\n");
1138 1139

	/* force cdclk programming */
1140
	dev_priv->cdclk.hw.cdclk = 0;
1141
	/* force full PLL disable + enable */
1142
	dev_priv->cdclk.hw.vco = -1;
1143 1144
}

1145
static void skl_cdclk_init_hw(struct drm_i915_private *dev_priv)
1146
{
1147
	struct intel_cdclk_config cdclk_config;
1148 1149 1150

	skl_sanitize_cdclk(dev_priv);

1151 1152
	if (dev_priv->cdclk.hw.cdclk != 0 &&
	    dev_priv->cdclk.hw.vco != 0) {
1153 1154 1155 1156 1157 1158
		/*
		 * Use the current vco as our initial
		 * guess as to what the preferred vco is.
		 */
		if (dev_priv->skl_preferred_vco_freq == 0)
			skl_set_preferred_cdclk_vco(dev_priv,
1159
						    dev_priv->cdclk.hw.vco);
1160 1161 1162
		return;
	}

1163
	cdclk_config = dev_priv->cdclk.hw;
1164

1165 1166 1167 1168 1169
	cdclk_config.vco = dev_priv->skl_preferred_vco_freq;
	if (cdclk_config.vco == 0)
		cdclk_config.vco = 8100000;
	cdclk_config.cdclk = skl_calc_cdclk(0, cdclk_config.vco);
	cdclk_config.voltage_level = skl_calc_voltage_level(cdclk_config.cdclk);
1170

1171
	skl_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1172 1173
}

1174
static void skl_cdclk_uninit_hw(struct drm_i915_private *dev_priv)
1175
{
1176
	struct intel_cdclk_config cdclk_config = dev_priv->cdclk.hw;
1177

1178 1179 1180
	cdclk_config.cdclk = cdclk_config.bypass;
	cdclk_config.vco = 0;
	cdclk_config.voltage_level = skl_calc_voltage_level(cdclk_config.cdclk);
1181

1182
	skl_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1183 1184
}

1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
static const struct intel_cdclk_vals bxt_cdclk_table[] = {
	{ .refclk = 19200, .cdclk = 144000, .divider = 8, .ratio = 60 },
	{ .refclk = 19200, .cdclk = 288000, .divider = 4, .ratio = 60 },
	{ .refclk = 19200, .cdclk = 384000, .divider = 3, .ratio = 60 },
	{ .refclk = 19200, .cdclk = 576000, .divider = 2, .ratio = 60 },
	{ .refclk = 19200, .cdclk = 624000, .divider = 2, .ratio = 65 },
	{}
};

static const struct intel_cdclk_vals glk_cdclk_table[] = {
	{ .refclk = 19200, .cdclk =  79200, .divider = 8, .ratio = 33 },
	{ .refclk = 19200, .cdclk = 158400, .divider = 4, .ratio = 33 },
	{ .refclk = 19200, .cdclk = 316800, .divider = 2, .ratio = 33 },
	{}
};

static const struct intel_cdclk_vals cnl_cdclk_table[] = {
	{ .refclk = 19200, .cdclk = 168000, .divider = 4, .ratio = 35 },
	{ .refclk = 19200, .cdclk = 336000, .divider = 2, .ratio = 35 },
	{ .refclk = 19200, .cdclk = 528000, .divider = 2, .ratio = 55 },

	{ .refclk = 24000, .cdclk = 168000, .divider = 4, .ratio = 28 },
	{ .refclk = 24000, .cdclk = 336000, .divider = 2, .ratio = 28 },
	{ .refclk = 24000, .cdclk = 528000, .divider = 2, .ratio = 44 },
	{}
};

static const struct intel_cdclk_vals icl_cdclk_table[] = {
	{ .refclk = 19200, .cdclk = 172800, .divider = 2, .ratio = 18 },
	{ .refclk = 19200, .cdclk = 192000, .divider = 2, .ratio = 20 },
	{ .refclk = 19200, .cdclk = 307200, .divider = 2, .ratio = 32 },
	{ .refclk = 19200, .cdclk = 326400, .divider = 4, .ratio = 68 },
	{ .refclk = 19200, .cdclk = 556800, .divider = 2, .ratio = 58 },
	{ .refclk = 19200, .cdclk = 652800, .divider = 2, .ratio = 68 },

	{ .refclk = 24000, .cdclk = 180000, .divider = 2, .ratio = 15 },
	{ .refclk = 24000, .cdclk = 192000, .divider = 2, .ratio = 16 },
	{ .refclk = 24000, .cdclk = 312000, .divider = 2, .ratio = 26 },
	{ .refclk = 24000, .cdclk = 324000, .divider = 4, .ratio = 54 },
	{ .refclk = 24000, .cdclk = 552000, .divider = 2, .ratio = 46 },
	{ .refclk = 24000, .cdclk = 648000, .divider = 2, .ratio = 54 },

	{ .refclk = 38400, .cdclk = 172800, .divider = 2, .ratio =  9 },
	{ .refclk = 38400, .cdclk = 192000, .divider = 2, .ratio = 10 },
	{ .refclk = 38400, .cdclk = 307200, .divider = 2, .ratio = 16 },
	{ .refclk = 38400, .cdclk = 326400, .divider = 4, .ratio = 34 },
	{ .refclk = 38400, .cdclk = 556800, .divider = 2, .ratio = 29 },
	{ .refclk = 38400, .cdclk = 652800, .divider = 2, .ratio = 34 },
	{}
};

static int bxt_calc_cdclk(struct drm_i915_private *dev_priv, int min_cdclk)
{
	const struct intel_cdclk_vals *table = dev_priv->cdclk.table;
	int i;

	for (i = 0; table[i].refclk; i++)
		if (table[i].refclk == dev_priv->cdclk.hw.ref &&
		    table[i].cdclk >= min_cdclk)
			return table[i].cdclk;

1246 1247 1248
	drm_WARN(&dev_priv->drm, 1,
		 "Cannot satisfy minimum cdclk %d with refclk %u\n",
		 min_cdclk, dev_priv->cdclk.hw.ref);
1249
	return 0;
1250 1251
}

1252
static int bxt_calc_cdclk_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
1253
{
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
	const struct intel_cdclk_vals *table = dev_priv->cdclk.table;
	int i;

	if (cdclk == dev_priv->cdclk.hw.bypass)
		return 0;

	for (i = 0; table[i].refclk; i++)
		if (table[i].refclk == dev_priv->cdclk.hw.ref &&
		    table[i].cdclk == cdclk)
			return dev_priv->cdclk.hw.ref * table[i].ratio;

1265 1266
	drm_WARN(&dev_priv->drm, 1, "cdclk %d not valid for refclk %u\n",
		 cdclk, dev_priv->cdclk.hw.ref);
1267
	return 0;
1268 1269
}

1270 1271 1272 1273 1274
static u8 bxt_calc_voltage_level(int cdclk)
{
	return DIV_ROUND_UP(cdclk, 25000);
}

1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
static u8 cnl_calc_voltage_level(int cdclk)
{
	if (cdclk > 336000)
		return 2;
	else if (cdclk > 168000)
		return 1;
	else
		return 0;
}

static u8 icl_calc_voltage_level(int cdclk)
{
	if (cdclk > 556800)
		return 2;
	else if (cdclk > 312000)
		return 1;
	else
		return 0;
}

static u8 ehl_calc_voltage_level(int cdclk)
{
1297 1298 1299
	if (cdclk > 326400)
		return 3;
	else if (cdclk > 312000)
1300 1301 1302 1303 1304 1305 1306
		return 2;
	else if (cdclk > 180000)
		return 1;
	else
		return 0;
}

1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318
static u8 tgl_calc_voltage_level(int cdclk)
{
	if (cdclk > 556800)
		return 3;
	else if (cdclk > 326400)
		return 2;
	else if (cdclk > 312000)
		return 1;
	else
		return 0;
}

1319
static void cnl_readout_refclk(struct drm_i915_private *dev_priv,
1320
			       struct intel_cdclk_config *cdclk_config)
1321
{
1322
	if (intel_de_read(dev_priv, SKL_DSSM) & CNL_DSSM_CDCLK_PLL_REFCLK_24MHz)
1323
		cdclk_config->ref = 24000;
1324
	else
1325
		cdclk_config->ref = 19200;
1326
}
1327

1328
static void icl_readout_refclk(struct drm_i915_private *dev_priv,
1329
			       struct intel_cdclk_config *cdclk_config)
1330
{
1331
	u32 dssm = intel_de_read(dev_priv, SKL_DSSM) & ICL_DSSM_CDCLK_PLL_REFCLK_MASK;
1332 1333 1334 1335 1336 1337

	switch (dssm) {
	default:
		MISSING_CASE(dssm);
		/* fall through */
	case ICL_DSSM_CDCLK_PLL_REFCLK_24MHz:
1338
		cdclk_config->ref = 24000;
1339 1340
		break;
	case ICL_DSSM_CDCLK_PLL_REFCLK_19_2MHz:
1341
		cdclk_config->ref = 19200;
1342 1343
		break;
	case ICL_DSSM_CDCLK_PLL_REFCLK_38_4MHz:
1344
		cdclk_config->ref = 38400;
1345 1346 1347 1348 1349
		break;
	}
}

static void bxt_de_pll_readout(struct drm_i915_private *dev_priv,
1350
			       struct intel_cdclk_config *cdclk_config)
1351 1352 1353 1354
{
	u32 val, ratio;

	if (INTEL_GEN(dev_priv) >= 11)
1355
		icl_readout_refclk(dev_priv, cdclk_config);
1356
	else if (IS_CANNONLAKE(dev_priv))
1357
		cnl_readout_refclk(dev_priv, cdclk_config);
1358
	else
1359
		cdclk_config->ref = 19200;
1360

1361
	val = intel_de_read(dev_priv, BXT_DE_PLL_ENABLE);
1362 1363 1364 1365 1366 1367
	if ((val & BXT_DE_PLL_PLL_ENABLE) == 0 ||
	    (val & BXT_DE_PLL_LOCK) == 0) {
		/*
		 * CDCLK PLL is disabled, the VCO/ratio doesn't matter, but
		 * setting it to zero is a way to signal that.
		 */
1368
		cdclk_config->vco = 0;
1369
		return;
1370
	}
1371

1372 1373 1374 1375 1376 1377 1378
	/*
	 * CNL+ have the ratio directly in the PLL enable register, gen9lp had
	 * it in a separate PLL control register.
	 */
	if (INTEL_GEN(dev_priv) >= 10)
		ratio = val & CNL_CDCLK_PLL_RATIO_MASK;
	else
1379
		ratio = intel_de_read(dev_priv, BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
1380

1381
	cdclk_config->vco = ratio * cdclk_config->ref;
1382 1383
}

1384
static void bxt_get_cdclk(struct drm_i915_private *dev_priv,
1385
			  struct intel_cdclk_config *cdclk_config)
1386 1387
{
	u32 divider;
1388
	int div;
1389

1390
	bxt_de_pll_readout(dev_priv, cdclk_config);
1391

1392
	if (INTEL_GEN(dev_priv) >= 12)
1393
		cdclk_config->bypass = cdclk_config->ref / 2;
1394
	else if (INTEL_GEN(dev_priv) >= 11)
1395
		cdclk_config->bypass = 50000;
1396
	else
1397
		cdclk_config->bypass = cdclk_config->ref;
1398

1399 1400
	if (cdclk_config->vco == 0) {
		cdclk_config->cdclk = cdclk_config->bypass;
1401
		goto out;
1402
	}
1403

1404
	divider = intel_de_read(dev_priv, CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
1405 1406 1407 1408 1409 1410

	switch (divider) {
	case BXT_CDCLK_CD2X_DIV_SEL_1:
		div = 2;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_1_5:
1411 1412 1413
		drm_WARN(&dev_priv->drm,
			 IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10,
			 "Unsupported divider\n");
1414 1415 1416 1417 1418 1419
		div = 3;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_2:
		div = 4;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_4:
1420 1421
		drm_WARN(&dev_priv->drm, INTEL_GEN(dev_priv) >= 10,
			 "Unsupported divider\n");
1422 1423 1424 1425
		div = 8;
		break;
	default:
		MISSING_CASE(divider);
1426
		return;
1427 1428
	}

1429
	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco, div);
1430 1431 1432 1433 1434 1435

 out:
	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
1436 1437
	cdclk_config->voltage_level =
		dev_priv->display.calc_voltage_level(cdclk_config->cdclk);
1438 1439 1440 1441
}

static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
{
1442
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, 0);
1443 1444

	/* Timeout 200us */
1445 1446
	if (intel_de_wait_for_clear(dev_priv,
				    BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1447
		drm_err(&dev_priv->drm, "timeout waiting for DE PLL unlock\n");
1448

1449
	dev_priv->cdclk.hw.vco = 0;
1450 1451 1452 1453
}

static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
{
1454
	int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk.hw.ref);
1455 1456
	u32 val;

1457
	val = intel_de_read(dev_priv, BXT_DE_PLL_CTL);
1458 1459
	val &= ~BXT_DE_PLL_RATIO_MASK;
	val |= BXT_DE_PLL_RATIO(ratio);
1460
	intel_de_write(dev_priv, BXT_DE_PLL_CTL, val);
1461

1462
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
1463 1464

	/* Timeout 200us */
1465 1466
	if (intel_de_wait_for_set(dev_priv,
				  BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1467
		drm_err(&dev_priv->drm, "timeout waiting for DE PLL lock\n");
1468

1469
	dev_priv->cdclk.hw.vco = vco;
1470 1471
}

1472 1473 1474 1475
static void cnl_cdclk_pll_disable(struct drm_i915_private *dev_priv)
{
	u32 val;

1476
	val = intel_de_read(dev_priv, BXT_DE_PLL_ENABLE);
1477
	val &= ~BXT_DE_PLL_PLL_ENABLE;
1478
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1479 1480

	/* Timeout 200us */
1481
	if (wait_for((intel_de_read(dev_priv, BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
1482 1483
		drm_err(&dev_priv->drm,
			"timeout waiting for CDCLK PLL unlock\n");
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493

	dev_priv->cdclk.hw.vco = 0;
}

static void cnl_cdclk_pll_enable(struct drm_i915_private *dev_priv, int vco)
{
	int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk.hw.ref);
	u32 val;

	val = CNL_CDCLK_PLL_RATIO(ratio);
1494
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1495 1496

	val |= BXT_DE_PLL_PLL_ENABLE;
1497
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1498 1499

	/* Timeout 200us */
1500
	if (wait_for((intel_de_read(dev_priv, BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
1501 1502
		drm_err(&dev_priv->drm,
			"timeout waiting for CDCLK PLL lock\n");
1503 1504 1505 1506

	dev_priv->cdclk.hw.vco = vco;
}

1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526
static u32 bxt_cdclk_cd2x_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
{
	if (INTEL_GEN(dev_priv) >= 12) {
		if (pipe == INVALID_PIPE)
			return TGL_CDCLK_CD2X_PIPE_NONE;
		else
			return TGL_CDCLK_CD2X_PIPE(pipe);
	} else if (INTEL_GEN(dev_priv) >= 11) {
		if (pipe == INVALID_PIPE)
			return ICL_CDCLK_CD2X_PIPE_NONE;
		else
			return ICL_CDCLK_CD2X_PIPE(pipe);
	} else {
		if (pipe == INVALID_PIPE)
			return BXT_CDCLK_CD2X_PIPE_NONE;
		else
			return BXT_CDCLK_CD2X_PIPE(pipe);
	}
}

1527
static void bxt_set_cdclk(struct drm_i915_private *dev_priv,
1528
			  const struct intel_cdclk_config *cdclk_config,
1529
			  enum pipe pipe)
1530
{
1531 1532
	int cdclk = cdclk_config->cdclk;
	int vco = cdclk_config->vco;
1533
	u32 val, divider;
1534
	int ret;
1535

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551
	/* Inform power controller of upcoming frequency change. */
	if (INTEL_GEN(dev_priv) >= 10)
		ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
					SKL_CDCLK_PREPARE_FOR_CHANGE,
					SKL_CDCLK_READY_FOR_CHANGE,
					SKL_CDCLK_READY_FOR_CHANGE, 3);
	else
		/*
		 * BSpec requires us to wait up to 150usec, but that leads to
		 * timeouts; the 2ms used here is based on experiment.
		 */
		ret = sandybridge_pcode_write_timeout(dev_priv,
						      HSW_PCODE_DE_WRITE_FREQ_REQ,
						      0x80000000, 150, 2);

	if (ret) {
1552 1553 1554
		drm_err(&dev_priv->drm,
			"Failed to inform PCU about cdclk change (err %d, freq %d)\n",
			ret, cdclk);
1555 1556 1557
		return;
	}

1558 1559
	/* cdclk = vco / 2 / div{1,1.5,2,4} */
	switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
1560
	default:
1561 1562 1563
		drm_WARN_ON(&dev_priv->drm,
			    cdclk != dev_priv->cdclk.hw.bypass);
		drm_WARN_ON(&dev_priv->drm, vco != 0);
1564 1565 1566
		/* fall through */
	case 2:
		divider = BXT_CDCLK_CD2X_DIV_SEL_1;
1567 1568
		break;
	case 3:
1569 1570 1571
		drm_WARN(&dev_priv->drm,
			 IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10,
			 "Unsupported divider\n");
1572 1573
		divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
		break;
1574 1575
	case 4:
		divider = BXT_CDCLK_CD2X_DIV_SEL_2;
1576
		break;
1577
	case 8:
1578 1579
		drm_WARN(&dev_priv->drm, INTEL_GEN(dev_priv) >= 10,
			 "Unsupported divider\n");
1580
		divider = BXT_CDCLK_CD2X_DIV_SEL_4;
1581 1582 1583
		break;
	}

1584 1585 1586 1587
	if (INTEL_GEN(dev_priv) >= 10) {
		if (dev_priv->cdclk.hw.vco != 0 &&
		    dev_priv->cdclk.hw.vco != vco)
			cnl_cdclk_pll_disable(dev_priv);
1588

1589 1590
		if (dev_priv->cdclk.hw.vco != vco)
			cnl_cdclk_pll_enable(dev_priv, vco);
1591

1592 1593 1594 1595 1596 1597 1598 1599
	} else {
		if (dev_priv->cdclk.hw.vco != 0 &&
		    dev_priv->cdclk.hw.vco != vco)
			bxt_de_pll_disable(dev_priv);

		if (dev_priv->cdclk.hw.vco != vco)
			bxt_de_pll_enable(dev_priv, vco);
	}
1600

1601 1602
	val = divider | skl_cdclk_decimal(cdclk) |
		bxt_cdclk_cd2x_pipe(dev_priv, pipe);
1603

1604 1605 1606 1607
	/*
	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
	 * enable otherwise.
	 */
1608
	if (IS_GEN9_LP(dev_priv) && cdclk >= 500000)
1609
		val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
1610
	intel_de_write(dev_priv, CDCLK_CTL, val);
1611

1612 1613 1614
	if (pipe != INVALID_PIPE)
		intel_wait_for_vblank(dev_priv, pipe);

1615 1616
	if (INTEL_GEN(dev_priv) >= 10) {
		ret = sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
1617
					      cdclk_config->voltage_level);
1618 1619 1620 1621 1622 1623 1624 1625 1626
	} else {
		/*
		 * The timeout isn't specified, the 2ms used here is based on
		 * experiment.
		 * FIXME: Waiting for the request completion could be delayed
		 * until the next PCODE request based on BSpec.
		 */
		ret = sandybridge_pcode_write_timeout(dev_priv,
						      HSW_PCODE_DE_WRITE_FREQ_REQ,
1627
						      cdclk_config->voltage_level,
1628 1629 1630
						      150, 2);
	}

1631
	if (ret) {
1632 1633 1634
		drm_err(&dev_priv->drm,
			"PCode CDCLK freq set failed, (err %d, freq %d)\n",
			ret, cdclk);
1635 1636 1637 1638
		return;
	}

	intel_update_cdclk(dev_priv);
1639 1640 1641 1642 1643 1644

	if (INTEL_GEN(dev_priv) >= 10)
		/*
		 * Can't read out the voltage level :(
		 * Let's just assume everything is as expected.
		 */
1645
		dev_priv->cdclk.hw.voltage_level = cdclk_config->voltage_level;
1646 1647 1648 1649 1650
}

static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
{
	u32 cdctl, expected;
1651
	int cdclk, vco;
1652 1653

	intel_update_cdclk(dev_priv);
1654
	intel_dump_cdclk_config(&dev_priv->cdclk.hw, "Current CDCLK");
1655

1656
	if (dev_priv->cdclk.hw.vco == 0 ||
1657
	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
1658 1659 1660 1661 1662 1663 1664 1665
		goto sanitize;

	/* DPLL okay; verify the cdclock
	 *
	 * Some BIOS versions leave an incorrect decimal frequency value and
	 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
	 * so sanitize this register.
	 */
1666
	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
1667 1668 1669 1670 1671
	/*
	 * Let's ignore the pipe field, since BIOS could have configured the
	 * dividers both synching to an active pipe, or asynchronously
	 * (PIPE_NONE).
	 */
1672
	cdctl &= ~bxt_cdclk_cd2x_pipe(dev_priv, INVALID_PIPE);
1673

1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704
	/* Make sure this is a legal cdclk value for the platform */
	cdclk = bxt_calc_cdclk(dev_priv, dev_priv->cdclk.hw.cdclk);
	if (cdclk != dev_priv->cdclk.hw.cdclk)
		goto sanitize;

	/* Make sure the VCO is correct for the cdclk */
	vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
	if (vco != dev_priv->cdclk.hw.vco)
		goto sanitize;

	expected = skl_cdclk_decimal(cdclk);

	/* Figure out what CD2X divider we should be using for this cdclk */
	switch (DIV_ROUND_CLOSEST(dev_priv->cdclk.hw.vco,
				  dev_priv->cdclk.hw.cdclk)) {
	case 2:
		expected |= BXT_CDCLK_CD2X_DIV_SEL_1;
		break;
	case 3:
		expected |= BXT_CDCLK_CD2X_DIV_SEL_1_5;
		break;
	case 4:
		expected |= BXT_CDCLK_CD2X_DIV_SEL_2;
		break;
	case 8:
		expected |= BXT_CDCLK_CD2X_DIV_SEL_4;
		break;
	default:
		goto sanitize;
	}

1705 1706 1707 1708
	/*
	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
	 * enable otherwise.
	 */
M
Matt Roper 已提交
1709
	if (IS_GEN9_LP(dev_priv) && dev_priv->cdclk.hw.cdclk >= 500000)
1710 1711 1712 1713 1714 1715 1716
		expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;

	if (cdctl == expected)
		/* All well; nothing to sanitize */
		return;

sanitize:
1717
	drm_dbg_kms(&dev_priv->drm, "Sanitizing cdclk programmed by pre-os\n");
1718 1719

	/* force cdclk programming */
1720
	dev_priv->cdclk.hw.cdclk = 0;
1721 1722

	/* force full PLL disable + enable */
1723
	dev_priv->cdclk.hw.vco = -1;
1724 1725
}

1726
static void bxt_cdclk_init_hw(struct drm_i915_private *dev_priv)
1727
{
1728
	struct intel_cdclk_config cdclk_config;
1729 1730 1731

	bxt_sanitize_cdclk(dev_priv);

1732 1733
	if (dev_priv->cdclk.hw.cdclk != 0 &&
	    dev_priv->cdclk.hw.vco != 0)
1734 1735
		return;

1736
	cdclk_config = dev_priv->cdclk.hw;
1737

1738 1739 1740 1741 1742
	/*
	 * FIXME:
	 * - The initial CDCLK needs to be read from VBT.
	 *   Need to make this change after VBT has changes for BXT.
	 */
1743 1744 1745 1746
	cdclk_config.cdclk = bxt_calc_cdclk(dev_priv, 0);
	cdclk_config.vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk_config.cdclk);
	cdclk_config.voltage_level =
		dev_priv->display.calc_voltage_level(cdclk_config.cdclk);
1747

1748
	bxt_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1749 1750
}

1751
static void bxt_cdclk_uninit_hw(struct drm_i915_private *dev_priv)
1752
{
1753
	struct intel_cdclk_config cdclk_config = dev_priv->cdclk.hw;
1754

1755 1756 1757 1758
	cdclk_config.cdclk = cdclk_config.bypass;
	cdclk_config.vco = 0;
	cdclk_config.voltage_level =
		dev_priv->display.calc_voltage_level(cdclk_config.cdclk);
1759

1760
	bxt_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1761 1762
}

1763
/**
1764
 * intel_cdclk_init_hw - Initialize CDCLK hardware
1765 1766 1767 1768 1769 1770 1771
 * @i915: i915 device
 *
 * Initialize CDCLK. This consists mainly of initializing dev_priv->cdclk.hw and
 * sanitizing the state of the hardware if needed. This is generally done only
 * during the display core initialization sequence, after which the DMC will
 * take care of turning CDCLK off/on as needed.
 */
1772
void intel_cdclk_init_hw(struct drm_i915_private *i915)
1773
{
1774
	if (IS_GEN9_LP(i915) || INTEL_GEN(i915) >= 10)
1775
		bxt_cdclk_init_hw(i915);
1776
	else if (IS_GEN9_BC(i915))
1777
		skl_cdclk_init_hw(i915);
1778 1779 1780
}

/**
1781
 * intel_cdclk_uninit_hw - Uninitialize CDCLK hardware
1782 1783 1784 1785 1786
 * @i915: i915 device
 *
 * Uninitialize CDCLK. This is done only during the display core
 * uninitialization sequence.
 */
1787
void intel_cdclk_uninit_hw(struct drm_i915_private *i915)
1788
{
1789
	if (INTEL_GEN(i915) >= 10 || IS_GEN9_LP(i915))
1790
		bxt_cdclk_uninit_hw(i915);
1791
	else if (IS_GEN9_BC(i915))
1792
		skl_cdclk_uninit_hw(i915);
1793 1794
}

1795
/**
1796 1797 1798 1799
 * intel_cdclk_needs_modeset - Determine if changong between the CDCLK
 *                             configurations requires a modeset on all pipes
 * @a: first CDCLK configuration
 * @b: second CDCLK configuration
1800 1801
 *
 * Returns:
1802 1803
 * True if changing between the two CDCLK configurations
 * requires all pipes to be off, false if not.
1804
 */
1805 1806
bool intel_cdclk_needs_modeset(const struct intel_cdclk_config *a,
			       const struct intel_cdclk_config *b)
1807
{
1808 1809 1810 1811 1812
	return a->cdclk != b->cdclk ||
		a->vco != b->vco ||
		a->ref != b->ref;
}

1813
/**
1814 1815 1816 1817 1818
 * intel_cdclk_can_cd2x_update - Determine if changing between the two CDCLK
 *                               configurations requires only a cd2x divider update
 * @dev_priv: i915 device
 * @a: first CDCLK configuration
 * @b: second CDCLK configuration
1819 1820
 *
 * Returns:
1821 1822
 * True if changing between the two CDCLK configurations
 * can be done with just a cd2x divider update, false if not.
1823
 */
1824
static bool intel_cdclk_can_cd2x_update(struct drm_i915_private *dev_priv,
1825 1826
					const struct intel_cdclk_config *a,
					const struct intel_cdclk_config *b)
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836
{
	/* Older hw doesn't have the capability */
	if (INTEL_GEN(dev_priv) < 10 && !IS_GEN9_LP(dev_priv))
		return false;

	return a->cdclk != b->cdclk &&
		a->vco == b->vco &&
		a->ref == b->ref;
}

1837
/**
1838 1839 1840
 * intel_cdclk_changed - Determine if two CDCLK configurations are different
 * @a: first CDCLK configuration
 * @b: second CDCLK configuration
1841 1842
 *
 * Returns:
1843
 * True if the CDCLK configurations don't match, false if they do.
1844
 */
1845 1846
static bool intel_cdclk_changed(const struct intel_cdclk_config *a,
				const struct intel_cdclk_config *b)
1847 1848 1849
{
	return intel_cdclk_needs_modeset(a, b) ||
		a->voltage_level != b->voltage_level;
1850 1851
}

1852 1853
void intel_dump_cdclk_config(const struct intel_cdclk_config *cdclk_config,
			     const char *context)
1854
{
1855
	DRM_DEBUG_DRIVER("%s %d kHz, VCO %d kHz, ref %d kHz, bypass %d kHz, voltage level %d\n",
1856 1857 1858
			 context, cdclk_config->cdclk, cdclk_config->vco,
			 cdclk_config->ref, cdclk_config->bypass,
			 cdclk_config->voltage_level);
1859 1860
}

1861
/**
1862
 * intel_set_cdclk - Push the CDCLK configuration to the hardware
1863
 * @dev_priv: i915 device
1864
 * @cdclk_config: new CDCLK configuration
1865
 * @pipe: pipe with which to synchronize the update
1866 1867 1868 1869
 *
 * Program the hardware based on the passed in CDCLK state,
 * if necessary.
 */
1870
static void intel_set_cdclk(struct drm_i915_private *dev_priv,
1871
			    const struct intel_cdclk_config *cdclk_config,
1872
			    enum pipe pipe)
1873
{
1874 1875
	struct intel_encoder *encoder;

1876
	if (!intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_config))
1877 1878
		return;

1879
	if (drm_WARN_ON_ONCE(&dev_priv->drm, !dev_priv->display.set_cdclk))
1880 1881
		return;

1882
	intel_dump_cdclk_config(cdclk_config, "Changing CDCLK to");
1883

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
	/*
	 * Lock aux/gmbus while we change cdclk in case those
	 * functions use cdclk. Not all platforms/ports do,
	 * but we'll lock them all for simplicity.
	 */
	mutex_lock(&dev_priv->gmbus_mutex);
	for_each_intel_dp(&dev_priv->drm, encoder) {
		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);

		mutex_lock_nest_lock(&intel_dp->aux.hw_mutex,
				     &dev_priv->gmbus_mutex);
	}

1897
	dev_priv->display.set_cdclk(dev_priv, cdclk_config, pipe);
1898

1899 1900 1901 1902 1903 1904 1905
	for_each_intel_dp(&dev_priv->drm, encoder) {
		struct intel_dp *intel_dp = enc_to_intel_dp(encoder);

		mutex_unlock(&intel_dp->aux.hw_mutex);
	}
	mutex_unlock(&dev_priv->gmbus_mutex);

1906 1907 1908
	if (drm_WARN(&dev_priv->drm,
		     intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_config),
		     "cdclk state doesn't match!\n")) {
1909 1910
		intel_dump_cdclk_config(&dev_priv->cdclk.hw, "[hw state]");
		intel_dump_cdclk_config(cdclk_config, "[sw state]");
1911
	}
1912 1913
}

1914
/**
1915 1916
 * intel_set_cdclk_pre_plane_update - Push the CDCLK state to the hardware
 * @state: intel atomic state
1917
 *
1918 1919
 * Program the hardware before updating the HW plane state based on the
 * new CDCLK state, if necessary.
1920 1921
 */
void
1922
intel_set_cdclk_pre_plane_update(struct intel_atomic_state *state)
1923
{
1924
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1925 1926 1927 1928
	const struct intel_cdclk_state *old_cdclk_state =
		intel_atomic_get_old_cdclk_state(state);
	const struct intel_cdclk_state *new_cdclk_state =
		intel_atomic_get_new_cdclk_state(state);
1929
	enum pipe pipe = new_cdclk_state->pipe;
1930

1931 1932 1933 1934
	if (!intel_cdclk_changed(&old_cdclk_state->actual,
				 &new_cdclk_state->actual))
		return;

1935
	if (pipe == INVALID_PIPE ||
1936
	    old_cdclk_state->actual.cdclk <= new_cdclk_state->actual.cdclk) {
1937
		drm_WARN_ON(&dev_priv->drm, !new_cdclk_state->base.changed);
1938

1939
		intel_set_cdclk(dev_priv, &new_cdclk_state->actual, pipe);
1940
	}
1941 1942 1943
}

/**
1944 1945
 * intel_set_cdclk_post_plane_update - Push the CDCLK state to the hardware
 * @state: intel atomic state
1946
 *
1947
 * Program the hardware after updating the HW plane state based on the
1948
 * new CDCLK state, if necessary.
1949 1950
 */
void
1951
intel_set_cdclk_post_plane_update(struct intel_atomic_state *state)
1952
{
1953
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1954 1955 1956 1957
	const struct intel_cdclk_state *old_cdclk_state =
		intel_atomic_get_old_cdclk_state(state);
	const struct intel_cdclk_state *new_cdclk_state =
		intel_atomic_get_new_cdclk_state(state);
1958
	enum pipe pipe = new_cdclk_state->pipe;
1959

1960 1961 1962 1963
	if (!intel_cdclk_changed(&old_cdclk_state->actual,
				 &new_cdclk_state->actual))
		return;

1964
	if (pipe != INVALID_PIPE &&
1965
	    old_cdclk_state->actual.cdclk > new_cdclk_state->actual.cdclk) {
1966
		drm_WARN_ON(&dev_priv->drm, !new_cdclk_state->base.changed);
1967

1968
		intel_set_cdclk(dev_priv, &new_cdclk_state->actual, pipe);
1969
	}
1970 1971
}

1972
static int intel_pixel_rate_to_cdclk(const struct intel_crtc_state *crtc_state)
1973
{
1974
	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1975 1976
	int pixel_rate = crtc_state->pixel_rate;

1977
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
1978
		return DIV_ROUND_UP(pixel_rate, 2);
1979
	else if (IS_GEN(dev_priv, 9) ||
1980 1981 1982 1983
		 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
		return pixel_rate;
	else if (IS_CHERRYVIEW(dev_priv))
		return DIV_ROUND_UP(pixel_rate * 100, 95);
1984 1985
	else if (crtc_state->double_wide)
		return DIV_ROUND_UP(pixel_rate * 100, 90 * 2);
1986 1987 1988 1989
	else
		return DIV_ROUND_UP(pixel_rate * 100, 90);
}

1990 1991
static int intel_planes_min_cdclk(const struct intel_crtc_state *crtc_state)
{
1992
	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1993 1994 1995 1996 1997 1998 1999 2000 2001 2002
	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
	struct intel_plane *plane;
	int min_cdclk = 0;

	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
		min_cdclk = max(crtc_state->min_cdclk[plane->id], min_cdclk);

	return min_cdclk;
}

2003
int intel_crtc_compute_min_cdclk(const struct intel_crtc_state *crtc_state)
2004 2005
{
	struct drm_i915_private *dev_priv =
2006
		to_i915(crtc_state->uapi.crtc->dev);
2007 2008
	int min_cdclk;

2009
	if (!crtc_state->hw.enable)
2010 2011
		return 0;

2012
	min_cdclk = intel_pixel_rate_to_cdclk(crtc_state);
2013 2014

	/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
2015
	if (IS_BROADWELL(dev_priv) && hsw_crtc_state_ips_capable(crtc_state))
2016
		min_cdclk = DIV_ROUND_UP(min_cdclk * 100, 95);
2017

2018 2019 2020
	/* BSpec says "Do not use DisplayPort with CDCLK less than 432 MHz,
	 * audio enabled, port width x4, and link rate HBR2 (5.4 GHz), or else
	 * there may be audio corruption or screen corruption." This cdclk
2021
	 * restriction for GLK is 316.8 MHz.
2022 2023 2024 2025
	 */
	if (intel_crtc_has_dp_encoder(crtc_state) &&
	    crtc_state->has_audio &&
	    crtc_state->port_clock >= 540000 &&
2026
	    crtc_state->lane_count == 4) {
2027 2028 2029
		if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv)) {
			/* Display WA #1145: glk,cnl */
			min_cdclk = max(316800, min_cdclk);
2030
		} else if (IS_GEN(dev_priv, 9) || IS_BROADWELL(dev_priv)) {
2031 2032 2033
			/* Display WA #1144: skl,bxt */
			min_cdclk = max(432000, min_cdclk);
		}
2034
	}
2035

2036 2037
	/*
	 * According to BSpec, "The CD clock frequency must be at least twice
2038 2039
	 * the frequency of the Azalia BCLK." and BCLK is 96 MHz by default.
	 */
2040
	if (crtc_state->has_audio && INTEL_GEN(dev_priv) >= 9)
2041
		min_cdclk = max(2 * 96000, min_cdclk);
2042

2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
	/*
	 * "For DP audio configuration, cdclk frequency shall be set to
	 *  meet the following requirements:
	 *  DP Link Frequency(MHz) | Cdclk frequency(MHz)
	 *  270                    | 320 or higher
	 *  162                    | 200 or higher"
	 */
	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
	    intel_crtc_has_dp_encoder(crtc_state) && crtc_state->has_audio)
		min_cdclk = max(crtc_state->port_clock, min_cdclk);

2054 2055 2056 2057 2058 2059 2060 2061
	/*
	 * On Valleyview some DSI panels lose (v|h)sync when the clock is lower
	 * than 320000KHz.
	 */
	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) &&
	    IS_VALLEYVIEW(dev_priv))
		min_cdclk = max(320000, min_cdclk);

2062 2063 2064 2065 2066 2067 2068 2069 2070
	/*
	 * On Geminilake once the CDCLK gets as low as 79200
	 * picture gets unstable, despite that values are
	 * correct for DSI PLL and DE PLL.
	 */
	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) &&
	    IS_GEMINILAKE(dev_priv))
		min_cdclk = max(158400, min_cdclk);

2071 2072 2073
	/* Account for additional needs from the planes */
	min_cdclk = max(intel_planes_min_cdclk(crtc_state), min_cdclk);

2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085
	/*
	 * HACK. Currently for TGL platforms we calculate
	 * min_cdclk initially based on pixel_rate divided
	 * by 2, accounting for also plane requirements,
	 * however in some cases the lowest possible CDCLK
	 * doesn't work and causing the underruns.
	 * Explicitly stating here that this seems to be currently
	 * rather a Hack, than final solution.
	 */
	if (IS_TIGERLAKE(dev_priv))
		min_cdclk = max(min_cdclk, (int)crtc_state->pixel_rate);

2086
	if (min_cdclk > dev_priv->max_cdclk_freq) {
2087 2088 2089
		drm_dbg_kms(&dev_priv->drm,
			    "required cdclk (%d kHz) exceeds max (%d kHz)\n",
			    min_cdclk, dev_priv->max_cdclk_freq);
2090 2091 2092
		return -EINVAL;
	}

2093
	return min_cdclk;
2094 2095
}

2096
static int intel_compute_min_cdclk(struct intel_cdclk_state *cdclk_state)
2097
{
2098
	struct intel_atomic_state *state = cdclk_state->base.state;
2099 2100
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
	struct intel_bw_state *bw_state = NULL;
2101
	struct intel_crtc *crtc;
2102
	struct intel_crtc_state *crtc_state;
2103
	int min_cdclk, i;
2104
	enum pipe pipe;
2105

2106
	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2107 2108
		int ret;

2109 2110 2111 2112
		min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
		if (min_cdclk < 0)
			return min_cdclk;

2113 2114 2115 2116
		bw_state = intel_atomic_get_bw_state(state);
		if (IS_ERR(bw_state))
			return PTR_ERR(bw_state);

2117
		if (cdclk_state->min_cdclk[i] == min_cdclk)
2118 2119
			continue;

2120
		cdclk_state->min_cdclk[i] = min_cdclk;
2121

2122
		ret = intel_atomic_lock_global_state(&cdclk_state->base);
2123 2124
		if (ret)
			return ret;
2125
	}
2126

2127
	min_cdclk = cdclk_state->force_min_cdclk;
2128 2129
	for_each_pipe(dev_priv, pipe) {
		min_cdclk = max(cdclk_state->min_cdclk[pipe], min_cdclk);
2130

2131 2132
		if (!bw_state)
			continue;
2133 2134 2135

		min_cdclk = max(bw_state->min_cdclk, min_cdclk);
	}
2136

2137
	return min_cdclk;
2138 2139
}

2140
/*
2141 2142 2143 2144
 * Account for port clock min voltage level requirements.
 * This only really does something on CNL+ but can be
 * called on earlier platforms as well.
 *
2145 2146 2147 2148 2149 2150 2151 2152
 * Note that this functions assumes that 0 is
 * the lowest voltage value, and higher values
 * correspond to increasingly higher voltages.
 *
 * Should that relationship no longer hold on
 * future platforms this code will need to be
 * adjusted.
 */
2153
static int bxt_compute_min_voltage_level(struct intel_cdclk_state *cdclk_state)
2154
{
2155
	struct intel_atomic_state *state = cdclk_state->base.state;
2156 2157 2158 2159 2160 2161 2162 2163
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
	struct intel_crtc *crtc;
	struct intel_crtc_state *crtc_state;
	u8 min_voltage_level;
	int i;
	enum pipe pipe;

	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2164 2165
		int ret;

2166
		if (crtc_state->hw.enable)
2167
			min_voltage_level = crtc_state->min_voltage_level;
2168
		else
2169 2170
			min_voltage_level = 0;

2171
		if (cdclk_state->min_voltage_level[i] == min_voltage_level)
2172 2173
			continue;

2174
		cdclk_state->min_voltage_level[i] = min_voltage_level;
2175

2176
		ret = intel_atomic_lock_global_state(&cdclk_state->base);
2177 2178
		if (ret)
			return ret;
2179 2180 2181 2182
	}

	min_voltage_level = 0;
	for_each_pipe(dev_priv, pipe)
2183
		min_voltage_level = max(cdclk_state->min_voltage_level[pipe],
2184 2185 2186 2187 2188
					min_voltage_level);

	return min_voltage_level;
}

2189
static int vlv_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2190
{
2191
	struct intel_atomic_state *state = cdclk_state->base.state;
2192
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2193
	int min_cdclk, cdclk;
2194

2195
	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2196 2197
	if (min_cdclk < 0)
		return min_cdclk;
2198

2199
	cdclk = vlv_calc_cdclk(dev_priv, min_cdclk);
2200

2201 2202
	cdclk_state->logical.cdclk = cdclk;
	cdclk_state->logical.voltage_level =
2203
		vlv_calc_voltage_level(dev_priv, cdclk);
2204

2205
	if (!cdclk_state->active_pipes) {
2206
		cdclk = vlv_calc_cdclk(dev_priv, cdclk_state->force_min_cdclk);
2207

2208 2209
		cdclk_state->actual.cdclk = cdclk;
		cdclk_state->actual.voltage_level =
2210
			vlv_calc_voltage_level(dev_priv, cdclk);
2211
	} else {
2212
		cdclk_state->actual = cdclk_state->logical;
2213
	}
2214 2215 2216 2217

	return 0;
}

2218
static int bdw_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2219
{
2220 2221
	int min_cdclk, cdclk;

2222
	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2223 2224
	if (min_cdclk < 0)
		return min_cdclk;
2225 2226 2227 2228 2229

	/*
	 * FIXME should also account for plane ratio
	 * once 64bpp pixel formats are supported.
	 */
2230
	cdclk = bdw_calc_cdclk(min_cdclk);
2231

2232 2233
	cdclk_state->logical.cdclk = cdclk;
	cdclk_state->logical.voltage_level =
2234
		bdw_calc_voltage_level(cdclk);
2235

2236
	if (!cdclk_state->active_pipes) {
2237
		cdclk = bdw_calc_cdclk(cdclk_state->force_min_cdclk);
2238

2239 2240
		cdclk_state->actual.cdclk = cdclk;
		cdclk_state->actual.voltage_level =
2241
			bdw_calc_voltage_level(cdclk);
2242
	} else {
2243
		cdclk_state->actual = cdclk_state->logical;
2244
	}
2245 2246 2247 2248

	return 0;
}

2249
static int skl_dpll0_vco(struct intel_cdclk_state *cdclk_state)
2250
{
2251
	struct intel_atomic_state *state = cdclk_state->base.state;
2252
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2253 2254 2255 2256
	struct intel_crtc *crtc;
	struct intel_crtc_state *crtc_state;
	int vco, i;

2257
	vco = cdclk_state->logical.vco;
2258 2259 2260
	if (!vco)
		vco = dev_priv->skl_preferred_vco_freq;

2261
	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2262
		if (!crtc_state->hw.enable)
2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285
			continue;

		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
			continue;

		/*
		 * DPLL0 VCO may need to be adjusted to get the correct
		 * clock for eDP. This will affect cdclk as well.
		 */
		switch (crtc_state->port_clock / 2) {
		case 108000:
		case 216000:
			vco = 8640000;
			break;
		default:
			vco = 8100000;
			break;
		}
	}

	return vco;
}

2286
static int skl_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2287
{
2288 2289
	int min_cdclk, cdclk, vco;

2290
	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2291 2292
	if (min_cdclk < 0)
		return min_cdclk;
2293

2294
	vco = skl_dpll0_vco(cdclk_state);
2295 2296 2297 2298 2299

	/*
	 * FIXME should also account for plane ratio
	 * once 64bpp pixel formats are supported.
	 */
2300
	cdclk = skl_calc_cdclk(min_cdclk, vco);
2301

2302 2303 2304
	cdclk_state->logical.vco = vco;
	cdclk_state->logical.cdclk = cdclk;
	cdclk_state->logical.voltage_level =
2305
		skl_calc_voltage_level(cdclk);
2306

2307
	if (!cdclk_state->active_pipes) {
2308
		cdclk = skl_calc_cdclk(cdclk_state->force_min_cdclk, vco);
2309

2310 2311 2312
		cdclk_state->actual.vco = vco;
		cdclk_state->actual.cdclk = cdclk;
		cdclk_state->actual.voltage_level =
2313
			skl_calc_voltage_level(cdclk);
2314
	} else {
2315
		cdclk_state->actual = cdclk_state->logical;
2316
	}
2317 2318 2319 2320

	return 0;
}

2321
static int bxt_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2322
{
2323
	struct intel_atomic_state *state = cdclk_state->base.state;
2324
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2325
	int min_cdclk, min_voltage_level, cdclk, vco;
2326

2327
	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2328 2329
	if (min_cdclk < 0)
		return min_cdclk;
2330

2331
	min_voltage_level = bxt_compute_min_voltage_level(cdclk_state);
2332 2333 2334
	if (min_voltage_level < 0)
		return min_voltage_level;

2335 2336
	cdclk = bxt_calc_cdclk(dev_priv, min_cdclk);
	vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
2337

2338 2339 2340
	cdclk_state->logical.vco = vco;
	cdclk_state->logical.cdclk = cdclk;
	cdclk_state->logical.voltage_level =
2341 2342
		max_t(int, min_voltage_level,
		      dev_priv->display.calc_voltage_level(cdclk));
2343

2344
	if (!cdclk_state->active_pipes) {
2345
		cdclk = bxt_calc_cdclk(dev_priv, cdclk_state->force_min_cdclk);
2346
		vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
2347

2348 2349 2350
		cdclk_state->actual.vco = vco;
		cdclk_state->actual.cdclk = cdclk;
		cdclk_state->actual.voltage_level =
2351
			dev_priv->display.calc_voltage_level(cdclk);
2352
	} else {
2353
		cdclk_state->actual = cdclk_state->logical;
2354 2355 2356 2357 2358
	}

	return 0;
}

2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375
static int intel_modeset_all_pipes(struct intel_atomic_state *state)
{
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
	struct intel_crtc *crtc;

	/*
	 * Add all pipes to the state, and force
	 * a modeset on all the active ones.
	 */
	for_each_intel_crtc(&dev_priv->drm, crtc) {
		struct intel_crtc_state *crtc_state;
		int ret;

		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
		if (IS_ERR(crtc_state))
			return PTR_ERR(crtc_state);

2376
		if (!crtc_state->hw.active ||
2377
		    drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
2378 2379
			continue;

2380
		crtc_state->uapi.mode_changed = true;
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397

		ret = drm_atomic_add_affected_connectors(&state->base,
							 &crtc->base);
		if (ret)
			return ret;

		ret = drm_atomic_add_affected_planes(&state->base,
						     &crtc->base);
		if (ret)
			return ret;

		crtc_state->update_planes |= crtc_state->active_planes;
	}

	return 0;
}

2398
static int fixed_modeset_calc_cdclk(struct intel_cdclk_state *cdclk_state)
2399 2400 2401 2402 2403 2404 2405 2406
{
	int min_cdclk;

	/*
	 * We can't change the cdclk frequency, but we still want to
	 * check that the required minimum frequency doesn't exceed
	 * the actual cdclk frequency.
	 */
2407
	min_cdclk = intel_compute_min_cdclk(cdclk_state);
2408 2409 2410 2411 2412 2413
	if (min_cdclk < 0)
		return min_cdclk;

	return 0;
}

2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465
static struct intel_global_state *intel_cdclk_duplicate_state(struct intel_global_obj *obj)
{
	struct intel_cdclk_state *cdclk_state;

	cdclk_state = kmemdup(obj->state, sizeof(*cdclk_state), GFP_KERNEL);
	if (!cdclk_state)
		return NULL;

	cdclk_state->force_min_cdclk_changed = false;
	cdclk_state->pipe = INVALID_PIPE;

	return &cdclk_state->base;
}

static void intel_cdclk_destroy_state(struct intel_global_obj *obj,
				      struct intel_global_state *state)
{
	kfree(state);
}

static const struct intel_global_state_funcs intel_cdclk_funcs = {
	.atomic_duplicate_state = intel_cdclk_duplicate_state,
	.atomic_destroy_state = intel_cdclk_destroy_state,
};

struct intel_cdclk_state *
intel_atomic_get_cdclk_state(struct intel_atomic_state *state)
{
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
	struct intel_global_state *cdclk_state;

	cdclk_state = intel_atomic_get_global_obj_state(state, &dev_priv->cdclk.obj);
	if (IS_ERR(cdclk_state))
		return ERR_CAST(cdclk_state);

	return to_intel_cdclk_state(cdclk_state);
}

int intel_cdclk_init(struct drm_i915_private *dev_priv)
{
	struct intel_cdclk_state *cdclk_state;

	cdclk_state = kzalloc(sizeof(*cdclk_state), GFP_KERNEL);
	if (!cdclk_state)
		return -ENOMEM;

	intel_atomic_global_obj_init(dev_priv, &dev_priv->cdclk.obj,
				     &cdclk_state->base, &intel_cdclk_funcs);

	return 0;
}

2466 2467 2468
int intel_modeset_calc_cdclk(struct intel_atomic_state *state)
{
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2469 2470
	const struct intel_cdclk_state *old_cdclk_state;
	struct intel_cdclk_state *new_cdclk_state;
2471 2472 2473
	enum pipe pipe;
	int ret;

2474 2475 2476
	new_cdclk_state = intel_atomic_get_cdclk_state(state);
	if (IS_ERR(new_cdclk_state))
		return PTR_ERR(new_cdclk_state);
2477

2478
	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
2479

2480 2481 2482
	new_cdclk_state->active_pipes =
		intel_calc_active_pipes(state, old_cdclk_state->active_pipes);

2483
	ret = dev_priv->display.modeset_calc_cdclk(new_cdclk_state);
2484 2485 2486
	if (ret)
		return ret;

2487 2488
	if (intel_cdclk_changed(&old_cdclk_state->actual,
				&new_cdclk_state->actual)) {
2489 2490 2491 2492
		/*
		 * Also serialize commits across all crtcs
		 * if the actual hw needs to be poked.
		 */
2493
		ret = intel_atomic_serialize_global_state(&new_cdclk_state->base);
2494 2495
		if (ret)
			return ret;
2496 2497
	} else if (old_cdclk_state->active_pipes != new_cdclk_state->active_pipes ||
		   intel_cdclk_changed(&old_cdclk_state->logical,
2498
				       &new_cdclk_state->logical)) {
2499
		ret = intel_atomic_lock_global_state(&new_cdclk_state->base);
2500
		if (ret)
2501
			return ret;
2502 2503
	} else {
		return 0;
2504 2505
	}

2506
	if (is_power_of_2(new_cdclk_state->active_pipes) &&
2507
	    intel_cdclk_can_cd2x_update(dev_priv,
2508 2509
					&old_cdclk_state->actual,
					&new_cdclk_state->actual)) {
2510 2511 2512
		struct intel_crtc *crtc;
		struct intel_crtc_state *crtc_state;

2513
		pipe = ilog2(new_cdclk_state->active_pipes);
2514
		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
2515 2516 2517 2518 2519

		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
		if (IS_ERR(crtc_state))
			return PTR_ERR(crtc_state);

2520
		if (drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
2521 2522 2523 2524 2525
			pipe = INVALID_PIPE;
	} else {
		pipe = INVALID_PIPE;
	}

2526
	if (pipe != INVALID_PIPE) {
2527
		new_cdclk_state->pipe = pipe;
2528

2529 2530 2531
		drm_dbg_kms(&dev_priv->drm,
			    "Can change cdclk with pipe %c active\n",
			    pipe_name(pipe));
2532 2533
	} else if (intel_cdclk_needs_modeset(&old_cdclk_state->actual,
					     &new_cdclk_state->actual)) {
2534
		/* All pipes must be switched off while we change the cdclk. */
2535 2536 2537 2538
		ret = intel_modeset_all_pipes(state);
		if (ret)
			return ret;

2539
		new_cdclk_state->pipe = INVALID_PIPE;
2540

2541 2542
		drm_dbg_kms(&dev_priv->drm,
			    "Modeset required for cdclk change\n");
2543 2544
	}

2545 2546
	drm_dbg_kms(&dev_priv->drm,
		    "New cdclk calculated to be logical %u kHz, actual %u kHz\n",
2547 2548
		    new_cdclk_state->logical.cdclk,
		    new_cdclk_state->actual.cdclk);
2549 2550
	drm_dbg_kms(&dev_priv->drm,
		    "New voltage level calculated to be logical %u, actual %u\n",
2551 2552
		    new_cdclk_state->logical.voltage_level,
		    new_cdclk_state->actual.voltage_level);
2553 2554 2555 2556

	return 0;
}

2557 2558 2559 2560
static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
{
	int max_cdclk_freq = dev_priv->max_cdclk_freq;

2561
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
2562
		return 2 * max_cdclk_freq;
2563
	else if (IS_GEN(dev_priv, 9) ||
2564
		 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
2565 2566 2567
		return max_cdclk_freq;
	else if (IS_CHERRYVIEW(dev_priv))
		return max_cdclk_freq*95/100;
2568
	else if (INTEL_GEN(dev_priv) < 4)
2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583
		return 2*max_cdclk_freq*90/100;
	else
		return max_cdclk_freq*90/100;
}

/**
 * intel_update_max_cdclk - Determine the maximum support CDCLK frequency
 * @dev_priv: i915 device
 *
 * Determine the maximum CDCLK frequency the platform supports, and also
 * derive the maximum dot clock frequency the maximum CDCLK frequency
 * allows.
 */
void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
{
2584 2585 2586 2587 2588 2589
	if (IS_ELKHARTLAKE(dev_priv)) {
		if (dev_priv->cdclk.hw.ref == 24000)
			dev_priv->max_cdclk_freq = 552000;
		else
			dev_priv->max_cdclk_freq = 556800;
	} else if (INTEL_GEN(dev_priv) >= 11) {
2590 2591 2592 2593 2594
		if (dev_priv->cdclk.hw.ref == 24000)
			dev_priv->max_cdclk_freq = 648000;
		else
			dev_priv->max_cdclk_freq = 652800;
	} else if (IS_CANNONLAKE(dev_priv)) {
2595 2596
		dev_priv->max_cdclk_freq = 528000;
	} else if (IS_GEN9_BC(dev_priv)) {
2597
		u32 limit = intel_de_read(dev_priv, SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
2598 2599 2600
		int max_cdclk, vco;

		vco = dev_priv->skl_preferred_vco_freq;
2601
		drm_WARN_ON(&dev_priv->drm, vco != 8100000 && vco != 8640000);
2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628

		/*
		 * Use the lower (vco 8640) cdclk values as a
		 * first guess. skl_calc_cdclk() will correct it
		 * if the preferred vco is 8100 instead.
		 */
		if (limit == SKL_DFSM_CDCLK_LIMIT_675)
			max_cdclk = 617143;
		else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
			max_cdclk = 540000;
		else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
			max_cdclk = 432000;
		else
			max_cdclk = 308571;

		dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
	} else if (IS_GEMINILAKE(dev_priv)) {
		dev_priv->max_cdclk_freq = 316800;
	} else if (IS_BROXTON(dev_priv)) {
		dev_priv->max_cdclk_freq = 624000;
	} else if (IS_BROADWELL(dev_priv))  {
		/*
		 * FIXME with extra cooling we can allow
		 * 540 MHz for ULX and 675 Mhz for ULT.
		 * How can we know if extra cooling is
		 * available? PCI ID, VTB, something else?
		 */
2629
		if (intel_de_read(dev_priv, FUSE_STRAP) & HSW_CDCLK_LIMIT)
2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
			dev_priv->max_cdclk_freq = 450000;
		else if (IS_BDW_ULX(dev_priv))
			dev_priv->max_cdclk_freq = 450000;
		else if (IS_BDW_ULT(dev_priv))
			dev_priv->max_cdclk_freq = 540000;
		else
			dev_priv->max_cdclk_freq = 675000;
	} else if (IS_CHERRYVIEW(dev_priv)) {
		dev_priv->max_cdclk_freq = 320000;
	} else if (IS_VALLEYVIEW(dev_priv)) {
		dev_priv->max_cdclk_freq = 400000;
	} else {
		/* otherwise assume cdclk is fixed */
2643
		dev_priv->max_cdclk_freq = dev_priv->cdclk.hw.cdclk;
2644 2645 2646 2647
	}

	dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);

2648 2649
	drm_dbg(&dev_priv->drm, "Max CD clock rate: %d kHz\n",
		dev_priv->max_cdclk_freq);
2650

2651 2652
	drm_dbg(&dev_priv->drm, "Max dotclock rate: %d kHz\n",
		dev_priv->max_dotclk_freq);
2653 2654 2655 2656 2657 2658 2659 2660 2661 2662
}

/**
 * intel_update_cdclk - Determine the current CDCLK frequency
 * @dev_priv: i915 device
 *
 * Determine the current CDCLK frequency.
 */
void intel_update_cdclk(struct drm_i915_private *dev_priv)
{
2663
	dev_priv->display.get_cdclk(dev_priv, &dev_priv->cdclk.hw);
2664 2665 2666 2667 2668 2669 2670 2671

	/*
	 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
	 * Programmng [sic] note: bit[9:2] should be programmed to the number
	 * of cdclk that generates 4MHz reference clock freq which is used to
	 * generate GMBus clock. This will vary with the cdclk freq.
	 */
	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2672 2673
		intel_de_write(dev_priv, GMBUSFREQ_VLV,
		               DIV_ROUND_UP(dev_priv->cdclk.hw.cdclk, 1000));
2674 2675
}

2676 2677 2678 2679 2680
static int cnp_rawclk(struct drm_i915_private *dev_priv)
{
	u32 rawclk;
	int divider, fraction;

2681
	if (intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_RAW_FREQUENCY) {
2682 2683 2684 2685 2686 2687 2688 2689 2690
		/* 24 MHz */
		divider = 24000;
		fraction = 0;
	} else {
		/* 19.2 MHz */
		divider = 19000;
		fraction = 200;
	}

2691
	rawclk = CNP_RAWCLK_DIV(divider / 1000);
2692 2693
	if (fraction) {
		int numerator = 1;
2694

2695 2696
		rawclk |= CNP_RAWCLK_DEN(DIV_ROUND_CLOSEST(numerator * 1000,
							   fraction) - 1);
2697
		if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2698
			rawclk |= ICP_RAWCLK_NUM(numerator);
2699 2700
	}

2701
	intel_de_write(dev_priv, PCH_RAWCLK_FREQ, rawclk);
2702
	return divider + fraction;
2703 2704
}

2705 2706
static int pch_rawclk(struct drm_i915_private *dev_priv)
{
2707
	return (intel_de_read(dev_priv, PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
2708 2709 2710 2711 2712 2713 2714 2715 2716
}

static int vlv_hrawclk(struct drm_i915_private *dev_priv)
{
	/* RAWCLK_FREQ_VLV register updated from power well code */
	return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
				      CCK_DISPLAY_REF_CLOCK_CONTROL);
}

2717
static int i9xx_hrawclk(struct drm_i915_private *dev_priv)
2718
{
2719
	u32 clkcfg;
2720

2721 2722 2723 2724 2725 2726 2727 2728 2729 2730
	/*
	 * hrawclock is 1/4 the FSB frequency
	 *
	 * Note that this only reads the state of the FSB
	 * straps, not the actual FSB frequency. Some BIOSen
	 * let you configure each independently. Ideally we'd
	 * read out the actual FSB frequency but sadly we
	 * don't know which registers have that information,
	 * and all the relevant docs have gone to bit heaven :(
	 */
2731 2732
	clkcfg = intel_de_read(dev_priv, CLKCFG) & CLKCFG_FSB_MASK;

2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769
	if (IS_MOBILE(dev_priv)) {
		switch (clkcfg) {
		case CLKCFG_FSB_400:
			return 100000;
		case CLKCFG_FSB_533:
			return 133333;
		case CLKCFG_FSB_667:
			return 166667;
		case CLKCFG_FSB_800:
			return 200000;
		case CLKCFG_FSB_1067:
			return 266667;
		case CLKCFG_FSB_1333:
			return 333333;
		default:
			MISSING_CASE(clkcfg);
			return 133333;
		}
	} else {
		switch (clkcfg) {
		case CLKCFG_FSB_400_ALT:
			return 100000;
		case CLKCFG_FSB_533:
			return 133333;
		case CLKCFG_FSB_667:
			return 166667;
		case CLKCFG_FSB_800:
			return 200000;
		case CLKCFG_FSB_1067_ALT:
			return 266667;
		case CLKCFG_FSB_1333_ALT:
			return 333333;
		case CLKCFG_FSB_1600_ALT:
			return 400000;
		default:
			return 133333;
		}
2770 2771 2772 2773
	}
}

/**
2774
 * intel_read_rawclk - Determine the current RAWCLK frequency
2775 2776 2777 2778 2779
 * @dev_priv: i915 device
 *
 * Determine the current RAWCLK frequency. RAWCLK is a fixed
 * frequency clock so this needs to done only once.
 */
2780
u32 intel_read_rawclk(struct drm_i915_private *dev_priv)
2781
{
2782 2783
	u32 freq;

2784
	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
2785
		freq = cnp_rawclk(dev_priv);
2786
	else if (HAS_PCH_SPLIT(dev_priv))
2787
		freq = pch_rawclk(dev_priv);
2788
	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
2789
		freq = vlv_hrawclk(dev_priv);
2790 2791
	else if (INTEL_GEN(dev_priv) >= 3)
		freq = i9xx_hrawclk(dev_priv);
2792 2793
	else
		/* no rawclk on other platforms, or no need to know it */
2794
		return 0;
2795

2796
	return freq;
2797 2798 2799 2800 2801 2802 2803 2804
}

/**
 * intel_init_cdclk_hooks - Initialize CDCLK related modesetting hooks
 * @dev_priv: i915 device
 */
void intel_init_cdclk_hooks(struct drm_i915_private *dev_priv)
{
2805 2806
	if (INTEL_GEN(dev_priv) >= 12) {
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2807
		dev_priv->display.bw_calc_min_cdclk = skl_bw_calc_min_cdclk;
2808 2809 2810 2811
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
		dev_priv->display.calc_voltage_level = tgl_calc_voltage_level;
		dev_priv->cdclk.table = icl_cdclk_table;
	} else if (IS_ELKHARTLAKE(dev_priv)) {
2812
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2813
		dev_priv->display.bw_calc_min_cdclk = skl_bw_calc_min_cdclk;
2814
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2815 2816 2817
		dev_priv->display.calc_voltage_level = ehl_calc_voltage_level;
		dev_priv->cdclk.table = icl_cdclk_table;
	} else if (INTEL_GEN(dev_priv) >= 11) {
2818
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2819
		dev_priv->display.bw_calc_min_cdclk = skl_bw_calc_min_cdclk;
2820
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2821
		dev_priv->display.calc_voltage_level = icl_calc_voltage_level;
2822
		dev_priv->cdclk.table = icl_cdclk_table;
2823
	} else if (IS_CANNONLAKE(dev_priv)) {
2824
		dev_priv->display.bw_calc_min_cdclk = skl_bw_calc_min_cdclk;
2825
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2826
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2827
		dev_priv->display.calc_voltage_level = cnl_calc_voltage_level;
2828
		dev_priv->cdclk.table = cnl_cdclk_table;
2829
	} else if (IS_GEN9_LP(dev_priv)) {
2830
		dev_priv->display.bw_calc_min_cdclk = skl_bw_calc_min_cdclk;
2831
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2832
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2833
		dev_priv->display.calc_voltage_level = bxt_calc_voltage_level;
2834 2835 2836 2837
		if (IS_GEMINILAKE(dev_priv))
			dev_priv->cdclk.table = glk_cdclk_table;
		else
			dev_priv->cdclk.table = bxt_cdclk_table;
2838
	} else if (IS_GEN9_BC(dev_priv)) {
2839
		dev_priv->display.bw_calc_min_cdclk = skl_bw_calc_min_cdclk;
2840
		dev_priv->display.set_cdclk = skl_set_cdclk;
2841
		dev_priv->display.modeset_calc_cdclk = skl_modeset_calc_cdclk;
2842
	} else if (IS_BROADWELL(dev_priv)) {
2843
		dev_priv->display.bw_calc_min_cdclk = intel_bw_calc_min_cdclk;
2844
		dev_priv->display.set_cdclk = bdw_set_cdclk;
2845
		dev_priv->display.modeset_calc_cdclk = bdw_modeset_calc_cdclk;
2846
	} else if (IS_CHERRYVIEW(dev_priv)) {
2847
		dev_priv->display.bw_calc_min_cdclk = intel_bw_calc_min_cdclk;
2848
		dev_priv->display.set_cdclk = chv_set_cdclk;
2849
		dev_priv->display.modeset_calc_cdclk = vlv_modeset_calc_cdclk;
2850
	} else if (IS_VALLEYVIEW(dev_priv)) {
2851
		dev_priv->display.bw_calc_min_cdclk = intel_bw_calc_min_cdclk;
2852
		dev_priv->display.set_cdclk = vlv_set_cdclk;
2853
		dev_priv->display.modeset_calc_cdclk = vlv_modeset_calc_cdclk;
2854
	} else {
2855
		dev_priv->display.bw_calc_min_cdclk = intel_bw_calc_min_cdclk;
2856
		dev_priv->display.modeset_calc_cdclk = fixed_modeset_calc_cdclk;
2857 2858
	}

2859
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEN9_LP(dev_priv))
2860
		dev_priv->display.get_cdclk = bxt_get_cdclk;
2861 2862
	else if (IS_GEN9_BC(dev_priv))
		dev_priv->display.get_cdclk = skl_get_cdclk;
2863 2864 2865 2866 2867 2868
	else if (IS_BROADWELL(dev_priv))
		dev_priv->display.get_cdclk = bdw_get_cdclk;
	else if (IS_HASWELL(dev_priv))
		dev_priv->display.get_cdclk = hsw_get_cdclk;
	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
		dev_priv->display.get_cdclk = vlv_get_cdclk;
2869
	else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
2870
		dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
2871
	else if (IS_GEN(dev_priv, 5))
2872 2873 2874
		dev_priv->display.get_cdclk = fixed_450mhz_get_cdclk;
	else if (IS_GM45(dev_priv))
		dev_priv->display.get_cdclk = gm45_get_cdclk;
2875
	else if (IS_G45(dev_priv))
2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899
		dev_priv->display.get_cdclk = g33_get_cdclk;
	else if (IS_I965GM(dev_priv))
		dev_priv->display.get_cdclk = i965gm_get_cdclk;
	else if (IS_I965G(dev_priv))
		dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
	else if (IS_PINEVIEW(dev_priv))
		dev_priv->display.get_cdclk = pnv_get_cdclk;
	else if (IS_G33(dev_priv))
		dev_priv->display.get_cdclk = g33_get_cdclk;
	else if (IS_I945GM(dev_priv))
		dev_priv->display.get_cdclk = i945gm_get_cdclk;
	else if (IS_I945G(dev_priv))
		dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
	else if (IS_I915GM(dev_priv))
		dev_priv->display.get_cdclk = i915gm_get_cdclk;
	else if (IS_I915G(dev_priv))
		dev_priv->display.get_cdclk = fixed_333mhz_get_cdclk;
	else if (IS_I865G(dev_priv))
		dev_priv->display.get_cdclk = fixed_266mhz_get_cdclk;
	else if (IS_I85X(dev_priv))
		dev_priv->display.get_cdclk = i85x_get_cdclk;
	else if (IS_I845G(dev_priv))
		dev_priv->display.get_cdclk = fixed_200mhz_get_cdclk;
	else { /* 830 */
2900 2901
		drm_WARN(&dev_priv->drm, !IS_I830(dev_priv),
			 "Unknown platform. Assuming 133 MHz CDCLK\n");
2902 2903 2904
		dev_priv->display.get_cdclk = fixed_133mhz_get_cdclk;
	}
}