intel_cdclk.c 73.7 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 "intel_cdclk.h"
25
#include "intel_drv.h"
26
#include "intel_sideband.h"
27 28 29 30 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

/**
 * 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.
 */

56 57
static void fixed_133mhz_get_cdclk(struct drm_i915_private *dev_priv,
				   struct intel_cdclk_state *cdclk_state)
58
{
59
	cdclk_state->cdclk = 133333;
60 61
}

62 63
static void fixed_200mhz_get_cdclk(struct drm_i915_private *dev_priv,
				   struct intel_cdclk_state *cdclk_state)
64
{
65
	cdclk_state->cdclk = 200000;
66 67
}

68 69
static void fixed_266mhz_get_cdclk(struct drm_i915_private *dev_priv,
				   struct intel_cdclk_state *cdclk_state)
70
{
71
	cdclk_state->cdclk = 266667;
72 73
}

74 75
static void fixed_333mhz_get_cdclk(struct drm_i915_private *dev_priv,
				   struct intel_cdclk_state *cdclk_state)
76
{
77
	cdclk_state->cdclk = 333333;
78 79
}

80 81
static void fixed_400mhz_get_cdclk(struct drm_i915_private *dev_priv,
				   struct intel_cdclk_state *cdclk_state)
82
{
83
	cdclk_state->cdclk = 400000;
84 85
}

86 87
static void fixed_450mhz_get_cdclk(struct drm_i915_private *dev_priv,
				   struct intel_cdclk_state *cdclk_state)
88
{
89
	cdclk_state->cdclk = 450000;
90 91
}

92 93
static void i85x_get_cdclk(struct drm_i915_private *dev_priv,
			   struct intel_cdclk_state *cdclk_state)
94 95 96 97 98 99 100 101 102
{
	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?
	 */
103 104 105 106
	if (pdev->revision == 0x1) {
		cdclk_state->cdclk = 133333;
		return;
	}
107 108 109 110 111 112 113 114 115 116 117

	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:
118 119
		cdclk_state->cdclk = 200000;
		break;
120
	case GC_CLOCK_166_250:
121 122
		cdclk_state->cdclk = 250000;
		break;
123
	case GC_CLOCK_100_133:
124 125
		cdclk_state->cdclk = 133333;
		break;
126 127 128
	case GC_CLOCK_133_266:
	case GC_CLOCK_133_266_2:
	case GC_CLOCK_166_266:
129 130
		cdclk_state->cdclk = 266667;
		break;
131 132 133
	}
}

134 135
static void i915gm_get_cdclk(struct drm_i915_private *dev_priv,
			     struct intel_cdclk_state *cdclk_state)
136 137 138 139 140 141
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
	u16 gcfgc = 0;

	pci_read_config_word(pdev, GCFGC, &gcfgc);

142 143 144 145
	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
		cdclk_state->cdclk = 133333;
		return;
	}
146 147 148

	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
	case GC_DISPLAY_CLOCK_333_320_MHZ:
149 150
		cdclk_state->cdclk = 333333;
		break;
151 152
	default:
	case GC_DISPLAY_CLOCK_190_200_MHZ:
153 154
		cdclk_state->cdclk = 190000;
		break;
155 156 157
	}
}

158 159
static void i945gm_get_cdclk(struct drm_i915_private *dev_priv,
			     struct intel_cdclk_state *cdclk_state)
160 161 162 163 164 165
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
	u16 gcfgc = 0;

	pci_read_config_word(pdev, GCFGC, &gcfgc);

166 167 168 169
	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
		cdclk_state->cdclk = 133333;
		return;
	}
170 171 172

	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
	case GC_DISPLAY_CLOCK_333_320_MHZ:
173 174
		cdclk_state->cdclk = 320000;
		break;
175 176
	default:
	case GC_DISPLAY_CLOCK_190_200_MHZ:
177 178
		cdclk_state->cdclk = 200000;
		break;
179 180 181 182 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
	}
}

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;
223
	u8 tmp = 0;
224 225 226 227

	/* FIXME other chipsets? */
	if (IS_GM45(dev_priv))
		vco_table = ctg_vco;
228
	else if (IS_G45(dev_priv))
229 230 231 232 233 234 235 236 237 238
		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;

239 240
	tmp = I915_READ(IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv) ?
			HPLLVCO_MOBILE : HPLLVCO);
241 242 243 244 245 246 247 248 249 250

	vco = vco_table[tmp & 0x7];
	if (vco == 0)
		DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
	else
		DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);

	return vco;
}

251 252
static void g33_get_cdclk(struct drm_i915_private *dev_priv,
			  struct intel_cdclk_state *cdclk_state)
253 254
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
255 256 257 258 259
	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;
260
	unsigned int cdclk_sel;
261
	u16 tmp = 0;
262

263 264
	cdclk_state->vco = intel_hpll_vco(dev_priv);

265 266 267 268 269 270 271
	pci_read_config_word(pdev, GCFGC, &tmp);

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

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

272
	switch (cdclk_state->vco) {
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	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;
	}

289 290 291
	cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco,
					       div_table[cdclk_sel]);
	return;
292 293 294

fail:
	DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n",
295 296
		  cdclk_state->vco, tmp);
	cdclk_state->cdclk = 190476;
297 298
}

299 300
static void pnv_get_cdclk(struct drm_i915_private *dev_priv,
			  struct intel_cdclk_state *cdclk_state)
301 302 303 304 305 306 307 308
{
	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:
309 310
		cdclk_state->cdclk = 266667;
		break;
311
	case GC_DISPLAY_CLOCK_333_MHZ_PNV:
312 313
		cdclk_state->cdclk = 333333;
		break;
314
	case GC_DISPLAY_CLOCK_444_MHZ_PNV:
315 316
		cdclk_state->cdclk = 444444;
		break;
317
	case GC_DISPLAY_CLOCK_200_MHZ_PNV:
318 319
		cdclk_state->cdclk = 200000;
		break;
320 321
	default:
		DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
322
		/* fall through */
323
	case GC_DISPLAY_CLOCK_133_MHZ_PNV:
324 325
		cdclk_state->cdclk = 133333;
		break;
326
	case GC_DISPLAY_CLOCK_167_MHZ_PNV:
327 328
		cdclk_state->cdclk = 166667;
		break;
329 330 331
	}
}

332 333
static void i965gm_get_cdclk(struct drm_i915_private *dev_priv,
			     struct intel_cdclk_state *cdclk_state)
334 335
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
336 337 338 339
	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;
340
	unsigned int cdclk_sel;
341
	u16 tmp = 0;
342

343 344
	cdclk_state->vco = intel_hpll_vco(dev_priv);

345 346 347 348 349 350 351
	pci_read_config_word(pdev, GCFGC, &tmp);

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

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

352
	switch (cdclk_state->vco) {
353 354 355 356 357 358 359 360 361 362 363 364 365
	case 3200000:
		div_table = div_3200;
		break;
	case 4000000:
		div_table = div_4000;
		break;
	case 5333333:
		div_table = div_5333;
		break;
	default:
		goto fail;
	}

366 367 368
	cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco,
					       div_table[cdclk_sel]);
	return;
369 370 371

fail:
	DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n",
372 373
		  cdclk_state->vco, tmp);
	cdclk_state->cdclk = 200000;
374 375
}

376 377
static void gm45_get_cdclk(struct drm_i915_private *dev_priv,
			   struct intel_cdclk_state *cdclk_state)
378 379
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
380
	unsigned int cdclk_sel;
381
	u16 tmp = 0;
382

383 384
	cdclk_state->vco = intel_hpll_vco(dev_priv);

385 386 387 388
	pci_read_config_word(pdev, GCFGC, &tmp);

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

389
	switch (cdclk_state->vco) {
390 391 392
	case 2666667:
	case 4000000:
	case 5333333:
393 394
		cdclk_state->cdclk = cdclk_sel ? 333333 : 222222;
		break;
395
	case 3200000:
396 397
		cdclk_state->cdclk = cdclk_sel ? 320000 : 228571;
		break;
398 399
	default:
		DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n",
400 401 402
			  cdclk_state->vco, tmp);
		cdclk_state->cdclk = 222222;
		break;
403 404 405
	}
}

406 407
static void hsw_get_cdclk(struct drm_i915_private *dev_priv,
			  struct intel_cdclk_state *cdclk_state)
408
{
409 410
	u32 lcpll = I915_READ(LCPLL_CTL);
	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
411 412

	if (lcpll & LCPLL_CD_SOURCE_FCLK)
413
		cdclk_state->cdclk = 800000;
414
	else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
415
		cdclk_state->cdclk = 450000;
416
	else if (freq == LCPLL_CLK_FREQ_450)
417
		cdclk_state->cdclk = 450000;
418
	else if (IS_HSW_ULT(dev_priv))
419
		cdclk_state->cdclk = 337500;
420
	else
421
		cdclk_state->cdclk = 540000;
422 423
}

424
static int vlv_calc_cdclk(struct drm_i915_private *dev_priv, int min_cdclk)
425 426 427 428 429 430 431 432 433
{
	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.
	 */
434
	if (IS_VALLEYVIEW(dev_priv) && min_cdclk > freq_320)
435
		return 400000;
436
	else if (min_cdclk > 266667)
437
		return freq_320;
438
	else if (min_cdclk > 0)
439 440 441 442 443
		return 266667;
	else
		return 200000;
}

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
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;
	}
}

463 464
static void vlv_get_cdclk(struct drm_i915_private *dev_priv,
			  struct intel_cdclk_state *cdclk_state)
465
{
466 467
	u32 val;

468 469 470
	vlv_iosf_sb_get(dev_priv,
			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));

471 472 473 474
	cdclk_state->vco = vlv_get_hpll_vco(dev_priv);
	cdclk_state->cdclk = vlv_get_cck_clock(dev_priv, "cdclk",
					       CCK_DISPLAY_CLOCK_CONTROL,
					       cdclk_state->vco);
475

476
	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
477 478 479

	vlv_iosf_sb_put(dev_priv,
			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));
480 481 482 483 484 485 486

	if (IS_VALLEYVIEW(dev_priv))
		cdclk_state->voltage_level = (val & DSPFREQGUAR_MASK) >>
			DSPFREQGUAR_SHIFT;
	else
		cdclk_state->voltage_level = (val & DSPFREQGUAR_MASK_CHV) >>
			DSPFREQGUAR_SHIFT_CHV;
487 488 489 490 491 492 493 494 495 496 497
}

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);

498
	if (dev_priv->cdclk.hw.cdclk >= dev_priv->czclk_freq) {
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
		/* 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?
	 */
	I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
		   default_credits);

	I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
		   credits | PFI_CREDIT_RESEND);

	/*
	 * FIXME is this guaranteed to clear
	 * immediately or should we poll for it?
	 */
	WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
}

525
static void vlv_set_cdclk(struct drm_i915_private *dev_priv,
526 527
			  const struct intel_cdclk_state *cdclk_state,
			  enum pipe pipe)
528
{
529
	int cdclk = cdclk_state->cdclk;
530
	u32 val, cmd = cdclk_state->voltage_level;
531
	intel_wakeref_t wakeref;
532

533 534 535 536 537 538 539 540 541 542 543 544
	switch (cdclk) {
	case 400000:
	case 333333:
	case 320000:
	case 266667:
	case 200000:
		break;
	default:
		MISSING_CASE(cdclk);
		return;
	}

545 546 547
	/* 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
548
	 * a system suspend.  So grab the display core domain, which covers
549 550
	 * the HW blocks needed for the following programming.
	 */
551
	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
552

553 554 555 556 557
	vlv_iosf_sb_get(dev_priv,
			BIT(VLV_IOSF_SB_CCK) |
			BIT(VLV_IOSF_SB_BUNIT) |
			BIT(VLV_IOSF_SB_PUNIT));

558
	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
559 560
	val &= ~DSPFREQGUAR_MASK;
	val |= (cmd << DSPFREQGUAR_SHIFT);
561 562
	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM) &
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
		      DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
		     50)) {
		DRM_ERROR("timed out waiting for CDclk change\n");
	}

	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))
			DRM_ERROR("timed out waiting for CDclk change\n");
	}

	/* 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);

600
	vlv_iosf_sb_put(dev_priv,
601 602 603
			BIT(VLV_IOSF_SB_CCK) |
			BIT(VLV_IOSF_SB_BUNIT) |
			BIT(VLV_IOSF_SB_PUNIT));
604 605

	intel_update_cdclk(dev_priv);
606 607

	vlv_program_pfi_credits(dev_priv);
608

609
	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
610 611
}

612
static void chv_set_cdclk(struct drm_i915_private *dev_priv,
613 614
			  const struct intel_cdclk_state *cdclk_state,
			  enum pipe pipe)
615
{
616
	int cdclk = cdclk_state->cdclk;
617
	u32 val, cmd = cdclk_state->voltage_level;
618
	intel_wakeref_t wakeref;
619 620 621 622 623 624 625 626 627 628 629 630

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

631 632 633
	/* 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
634
	 * a system suspend.  So grab the display core domain, which covers
635 636
	 * the HW blocks needed for the following programming.
	 */
637
	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
638

639
	vlv_punit_get(dev_priv);
640
	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
641 642
	val &= ~DSPFREQGUAR_MASK_CHV;
	val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
643 644
	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM) &
645 646 647 648
		      DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
		     50)) {
		DRM_ERROR("timed out waiting for CDclk change\n");
	}
649 650

	vlv_punit_put(dev_priv);
651 652

	intel_update_cdclk(dev_priv);
653 654

	vlv_program_pfi_credits(dev_priv);
655

656
	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
657 658
}

659
static int bdw_calc_cdclk(int min_cdclk)
660
{
661
	if (min_cdclk > 540000)
662
		return 675000;
663
	else if (min_cdclk > 450000)
664
		return 540000;
665
	else if (min_cdclk > 337500)
666 667 668 669 670
		return 450000;
	else
		return 337500;
}

671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
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;
	}
}

686 687
static void bdw_get_cdclk(struct drm_i915_private *dev_priv,
			  struct intel_cdclk_state *cdclk_state)
688
{
689 690
	u32 lcpll = I915_READ(LCPLL_CTL);
	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
691 692

	if (lcpll & LCPLL_CD_SOURCE_FCLK)
693
		cdclk_state->cdclk = 800000;
694
	else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
695
		cdclk_state->cdclk = 450000;
696
	else if (freq == LCPLL_CLK_FREQ_450)
697
		cdclk_state->cdclk = 450000;
698
	else if (freq == LCPLL_CLK_FREQ_54O_BDW)
699
		cdclk_state->cdclk = 540000;
700
	else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
701
		cdclk_state->cdclk = 337500;
702
	else
703
		cdclk_state->cdclk = 675000;
704 705 706 707 708 709 710

	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
	cdclk_state->voltage_level =
		bdw_calc_voltage_level(cdclk_state->cdclk);
711 712
}

713
static void bdw_set_cdclk(struct drm_i915_private *dev_priv,
714 715
			  const struct intel_cdclk_state *cdclk_state,
			  enum pipe pipe)
716
{
717
	int cdclk = cdclk_state->cdclk;
718
	u32 val;
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
	int ret;

	if (WARN((I915_READ(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"))
		return;

	ret = sandybridge_pcode_write(dev_priv,
				      BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
	if (ret) {
		DRM_ERROR("failed to inform pcode about cdclk change\n");
		return;
	}

	val = I915_READ(LCPLL_CTL);
	val |= LCPLL_CD_SOURCE_FCLK;
	I915_WRITE(LCPLL_CTL, val);

740 741 742 743
	/*
	 * According to the spec, it should be enough to poll for this 1 us.
	 * However, extensive testing shows that this can take longer.
	 */
744
	if (wait_for_us(I915_READ(LCPLL_CTL) &
745
			LCPLL_CD_SOURCE_FCLK_DONE, 100))
746 747 748 749 750 751
		DRM_ERROR("Switching to FCLK failed\n");

	val = I915_READ(LCPLL_CTL);
	val &= ~LCPLL_CLK_FREQ_MASK;

	switch (cdclk) {
752 753 754 755 756 757
	default:
		MISSING_CASE(cdclk);
		/* fall through */
	case 337500:
		val |= LCPLL_CLK_FREQ_337_5_BDW;
		break;
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778
	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;
	}

	I915_WRITE(LCPLL_CTL, val);

	val = I915_READ(LCPLL_CTL);
	val &= ~LCPLL_CD_SOURCE_FCLK;
	I915_WRITE(LCPLL_CTL, val);

	if (wait_for_us((I915_READ(LCPLL_CTL) &
			LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
		DRM_ERROR("Switching back to LCPLL failed\n");

779 780
	sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
				cdclk_state->voltage_level);
781 782 783 784 785 786

	I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);

	intel_update_cdclk(dev_priv);
}

787
static int skl_calc_cdclk(int min_cdclk, int vco)
788 789
{
	if (vco == 8640000) {
790
		if (min_cdclk > 540000)
791
			return 617143;
792
		else if (min_cdclk > 432000)
793
			return 540000;
794
		else if (min_cdclk > 308571)
795 796 797 798
			return 432000;
		else
			return 308571;
	} else {
799
		if (min_cdclk > 540000)
800
			return 675000;
801
		else if (min_cdclk > 450000)
802
			return 540000;
803
		else if (min_cdclk > 337500)
804 805 806 807 808 809
			return 450000;
		else
			return 337500;
	}
}

810 811
static u8 skl_calc_voltage_level(int cdclk)
{
812
	if (cdclk > 540000)
813
		return 3;
814 815 816 817 818 819
	else if (cdclk > 450000)
		return 2;
	else if (cdclk > 337500)
		return 1;
	else
		return 0;
820 821
}

822 823
static void skl_dpll0_update(struct drm_i915_private *dev_priv,
			     struct intel_cdclk_state *cdclk_state)
824 825 826
{
	u32 val;

827 828
	cdclk_state->ref = 24000;
	cdclk_state->vco = 0;
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

	val = I915_READ(LCPLL1_CTL);
	if ((val & LCPLL_PLL_ENABLE) == 0)
		return;

	if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
		return;

	val = I915_READ(DPLL_CTRL1);

	if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
			    DPLL_CTRL1_SSC(SKL_DPLL0) |
			    DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
		    DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
		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):
850
		cdclk_state->vco = 8100000;
851 852 853
		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):
854
		cdclk_state->vco = 8640000;
855 856 857 858 859 860 861
		break;
	default:
		MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
		break;
	}
}

862 863
static void skl_get_cdclk(struct drm_i915_private *dev_priv,
			  struct intel_cdclk_state *cdclk_state)
864 865 866
{
	u32 cdctl;

867
	skl_dpll0_update(dev_priv, cdclk_state);
868

869
	cdclk_state->cdclk = cdclk_state->bypass = cdclk_state->ref;
870 871

	if (cdclk_state->vco == 0)
872
		goto out;
873 874 875

	cdctl = I915_READ(CDCLK_CTL);

876
	if (cdclk_state->vco == 8640000) {
877 878
		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
		case CDCLK_FREQ_450_432:
879 880
			cdclk_state->cdclk = 432000;
			break;
881
		case CDCLK_FREQ_337_308:
882 883
			cdclk_state->cdclk = 308571;
			break;
884
		case CDCLK_FREQ_540:
885 886
			cdclk_state->cdclk = 540000;
			break;
887
		case CDCLK_FREQ_675_617:
888 889
			cdclk_state->cdclk = 617143;
			break;
890 891
		default:
			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
892
			break;
893 894 895 896
		}
	} else {
		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
		case CDCLK_FREQ_450_432:
897 898
			cdclk_state->cdclk = 450000;
			break;
899
		case CDCLK_FREQ_337_308:
900 901
			cdclk_state->cdclk = 337500;
			break;
902
		case CDCLK_FREQ_540:
903 904
			cdclk_state->cdclk = 540000;
			break;
905
		case CDCLK_FREQ_675_617:
906 907
			cdclk_state->cdclk = 675000;
			break;
908 909
		default:
			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
910
			break;
911 912
		}
	}
913 914 915 916 917 918 919 920

 out:
	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
	cdclk_state->voltage_level =
		skl_calc_voltage_level(cdclk_state->cdclk);
921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
}

/* 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;

	WARN_ON(vco != 8100000 && vco != 8640000);

	/*
	 * 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.
	 */
	val = I915_READ(DPLL_CTRL1);

	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);

	I915_WRITE(DPLL_CTRL1, val);
	POSTING_READ(DPLL_CTRL1);

	I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);

972
	if (intel_wait_for_register(&dev_priv->uncore,
973 974 975 976
				    LCPLL1_CTL, LCPLL_PLL_LOCK, LCPLL_PLL_LOCK,
				    5))
		DRM_ERROR("DPLL0 not locked\n");

977
	dev_priv->cdclk.hw.vco = vco;
978 979 980 981 982 983 984 985

	/* 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)
{
	I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
986 987 988
	if (intel_wait_for_register(&dev_priv->uncore,
				    LCPLL1_CTL, LCPLL_PLL_LOCK, 0,
				    1))
989 990
		DRM_ERROR("Couldn't disable DPLL0\n");

991
	dev_priv->cdclk.hw.vco = 0;
992 993 994
}

static void skl_set_cdclk(struct drm_i915_private *dev_priv,
995 996
			  const struct intel_cdclk_state *cdclk_state,
			  enum pipe pipe)
997
{
998 999
	int cdclk = cdclk_state->cdclk;
	int vco = cdclk_state->vco;
1000
	u32 freq_select, cdclk_ctl;
1001 1002
	int ret;

1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	/*
	 * 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.
	 */
	WARN_ON_ONCE(IS_SKYLAKE(dev_priv) && vco == 8640000);

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
	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) {
		DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
			  ret);
		return;
	}

1023
	/* Choose frequency for this cdclk */
1024
	switch (cdclk) {
1025
	default:
1026
		WARN_ON(cdclk != dev_priv->cdclk.hw.bypass);
1027 1028 1029 1030 1031 1032
		WARN_ON(vco != 0);
		/* fall through */
	case 308571:
	case 337500:
		freq_select = CDCLK_FREQ_337_308;
		break;
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
	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;
	}

1046 1047
	if (dev_priv->cdclk.hw.vco != 0 &&
	    dev_priv->cdclk.hw.vco != vco)
1048 1049
		skl_dpll0_disable(dev_priv);

1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
	cdclk_ctl = I915_READ(CDCLK_CTL);

	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);
		I915_WRITE(CDCLK_CTL, cdclk_ctl);
	}

	/* Wa Display #1183: skl,kbl,cfl */
	cdclk_ctl |= CDCLK_DIVMUX_CD_OVERRIDE;
	I915_WRITE(CDCLK_CTL, cdclk_ctl);
	POSTING_READ(CDCLK_CTL);

1064
	if (dev_priv->cdclk.hw.vco != vco)
1065 1066
		skl_dpll0_enable(dev_priv, vco);

1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
	/* Wa Display #1183: skl,kbl,cfl */
	cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
	I915_WRITE(CDCLK_CTL, cdclk_ctl);

	cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
	I915_WRITE(CDCLK_CTL, cdclk_ctl);

	/* Wa Display #1183: skl,kbl,cfl */
	cdclk_ctl &= ~CDCLK_DIVMUX_CD_OVERRIDE;
	I915_WRITE(CDCLK_CTL, cdclk_ctl);
1077 1078 1079
	POSTING_READ(CDCLK_CTL);

	/* inform PCU of the change */
1080 1081
	sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
				cdclk_state->voltage_level);
1082 1083 1084 1085 1086 1087

	intel_update_cdclk(dev_priv);
}

static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
{
1088
	u32 cdctl, expected;
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

	/*
	 * 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
	 */
	if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
		goto sanitize;

	intel_update_cdclk(dev_priv);
1099 1100
	intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");

1101
	/* Is PLL enabled and locked ? */
1102
	if (dev_priv->cdclk.hw.vco == 0 ||
1103
	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113
		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.
	 */
	cdctl = I915_READ(CDCLK_CTL);
	expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
1114
		skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);
1115 1116 1117 1118 1119 1120 1121 1122
	if (cdctl == expected)
		/* All well; nothing to sanitize */
		return;

sanitize:
	DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");

	/* force cdclk programming */
1123
	dev_priv->cdclk.hw.cdclk = 0;
1124
	/* force full PLL disable + enable */
1125
	dev_priv->cdclk.hw.vco = -1;
1126 1127
}

1128
static void skl_init_cdclk(struct drm_i915_private *dev_priv)
1129
{
1130
	struct intel_cdclk_state cdclk_state;
1131 1132 1133

	skl_sanitize_cdclk(dev_priv);

1134 1135
	if (dev_priv->cdclk.hw.cdclk != 0 &&
	    dev_priv->cdclk.hw.vco != 0) {
1136 1137 1138 1139 1140 1141
		/*
		 * 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,
1142
						    dev_priv->cdclk.hw.vco);
1143 1144 1145
		return;
	}

1146 1147 1148 1149 1150 1151
	cdclk_state = dev_priv->cdclk.hw;

	cdclk_state.vco = dev_priv->skl_preferred_vco_freq;
	if (cdclk_state.vco == 0)
		cdclk_state.vco = 8100000;
	cdclk_state.cdclk = skl_calc_cdclk(0, cdclk_state.vco);
1152
	cdclk_state.voltage_level = skl_calc_voltage_level(cdclk_state.cdclk);
1153

1154
	skl_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
1155 1156
}

1157
static void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
1158
{
1159 1160
	struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;

1161
	cdclk_state.cdclk = cdclk_state.bypass;
1162
	cdclk_state.vco = 0;
1163
	cdclk_state.voltage_level = skl_calc_voltage_level(cdclk_state.cdclk);
1164

1165
	skl_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
1166 1167
}

1168
static int bxt_calc_cdclk(int min_cdclk)
1169
{
1170
	if (min_cdclk > 576000)
1171
		return 624000;
1172
	else if (min_cdclk > 384000)
1173
		return 576000;
1174
	else if (min_cdclk > 288000)
1175
		return 384000;
1176
	else if (min_cdclk > 144000)
1177 1178 1179 1180 1181
		return 288000;
	else
		return 144000;
}

1182
static int glk_calc_cdclk(int min_cdclk)
1183
{
1184
	if (min_cdclk > 158400)
1185
		return 316800;
1186
	else if (min_cdclk > 79200)
1187 1188 1189 1190 1191
		return 158400;
	else
		return 79200;
}

1192 1193 1194 1195 1196
static u8 bxt_calc_voltage_level(int cdclk)
{
	return DIV_ROUND_UP(cdclk, 25000);
}

1197 1198 1199 1200
static int bxt_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
{
	int ratio;

1201
	if (cdclk == dev_priv->cdclk.hw.bypass)
1202 1203 1204 1205 1206
		return 0;

	switch (cdclk) {
	default:
		MISSING_CASE(cdclk);
1207
		/* fall through */
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
	case 144000:
	case 288000:
	case 384000:
	case 576000:
		ratio = 60;
		break;
	case 624000:
		ratio = 65;
		break;
	}

1219
	return dev_priv->cdclk.hw.ref * ratio;
1220 1221 1222 1223 1224 1225
}

static int glk_de_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
{
	int ratio;

1226
	if (cdclk == dev_priv->cdclk.hw.bypass)
1227 1228 1229 1230 1231
		return 0;

	switch (cdclk) {
	default:
		MISSING_CASE(cdclk);
1232
		/* fall through */
1233 1234 1235 1236 1237 1238 1239
	case  79200:
	case 158400:
	case 316800:
		ratio = 33;
		break;
	}

1240
	return dev_priv->cdclk.hw.ref * ratio;
1241 1242
}

1243 1244
static void bxt_de_pll_update(struct drm_i915_private *dev_priv,
			      struct intel_cdclk_state *cdclk_state)
1245 1246 1247
{
	u32 val;

1248 1249
	cdclk_state->ref = 19200;
	cdclk_state->vco = 0;
1250 1251 1252 1253 1254 1255 1256 1257 1258

	val = I915_READ(BXT_DE_PLL_ENABLE);
	if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
		return;

	if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
		return;

	val = I915_READ(BXT_DE_PLL_CTL);
1259
	cdclk_state->vco = (val & BXT_DE_PLL_RATIO_MASK) * cdclk_state->ref;
1260 1261
}

1262 1263
static void bxt_get_cdclk(struct drm_i915_private *dev_priv,
			  struct intel_cdclk_state *cdclk_state)
1264 1265
{
	u32 divider;
1266
	int div;
1267

1268
	bxt_de_pll_update(dev_priv, cdclk_state);
1269

1270
	cdclk_state->cdclk = cdclk_state->bypass = cdclk_state->ref;
1271 1272

	if (cdclk_state->vco == 0)
1273
		goto out;
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292

	divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;

	switch (divider) {
	case BXT_CDCLK_CD2X_DIV_SEL_1:
		div = 2;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_1_5:
		WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
		div = 3;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_2:
		div = 4;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_4:
		div = 8;
		break;
	default:
		MISSING_CASE(divider);
1293
		return;
1294 1295
	}

1296
	cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco, div);
1297 1298 1299 1300 1301 1302 1303 1304

 out:
	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
	cdclk_state->voltage_level =
		bxt_calc_voltage_level(cdclk_state->cdclk);
1305 1306 1307 1308 1309 1310 1311
}

static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
{
	I915_WRITE(BXT_DE_PLL_ENABLE, 0);

	/* Timeout 200us */
1312
	if (intel_wait_for_register(&dev_priv->uncore,
1313 1314 1315 1316
				    BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 0,
				    1))
		DRM_ERROR("timeout waiting for DE PLL unlock\n");

1317
	dev_priv->cdclk.hw.vco = 0;
1318 1319 1320 1321
}

static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
{
1322
	int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk.hw.ref);
1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	u32 val;

	val = I915_READ(BXT_DE_PLL_CTL);
	val &= ~BXT_DE_PLL_RATIO_MASK;
	val |= BXT_DE_PLL_RATIO(ratio);
	I915_WRITE(BXT_DE_PLL_CTL, val);

	I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);

	/* Timeout 200us */
1333
	if (intel_wait_for_register(&dev_priv->uncore,
1334 1335 1336 1337 1338 1339
				    BXT_DE_PLL_ENABLE,
				    BXT_DE_PLL_LOCK,
				    BXT_DE_PLL_LOCK,
				    1))
		DRM_ERROR("timeout waiting for DE PLL lock\n");

1340
	dev_priv->cdclk.hw.vco = vco;
1341 1342
}

1343
static void bxt_set_cdclk(struct drm_i915_private *dev_priv,
1344 1345
			  const struct intel_cdclk_state *cdclk_state,
			  enum pipe pipe)
1346
{
1347 1348
	int cdclk = cdclk_state->cdclk;
	int vco = cdclk_state->vco;
1349
	u32 val, divider;
1350
	int ret;
1351 1352 1353

	/* cdclk = vco / 2 / div{1,1.5,2,4} */
	switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
1354
	default:
1355
		WARN_ON(cdclk != dev_priv->cdclk.hw.bypass);
1356 1357 1358 1359
		WARN_ON(vco != 0);
		/* fall through */
	case 2:
		divider = BXT_CDCLK_CD2X_DIV_SEL_1;
1360 1361 1362 1363 1364
		break;
	case 3:
		WARN(IS_GEMINILAKE(dev_priv), "Unsupported divider\n");
		divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
		break;
1365 1366
	case 4:
		divider = BXT_CDCLK_CD2X_DIV_SEL_2;
1367
		break;
1368 1369
	case 8:
		divider = BXT_CDCLK_CD2X_DIV_SEL_4;
1370 1371 1372
		break;
	}

1373 1374 1375 1376 1377 1378 1379
	/*
	 * Inform power controller of upcoming frequency change. 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,
1380
					      0x80000000, 150, 2);
1381 1382 1383 1384 1385 1386
	if (ret) {
		DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
			  ret, cdclk);
		return;
	}

1387 1388
	if (dev_priv->cdclk.hw.vco != 0 &&
	    dev_priv->cdclk.hw.vco != vco)
1389 1390
		bxt_de_pll_disable(dev_priv);

1391
	if (dev_priv->cdclk.hw.vco != vco)
1392 1393 1394
		bxt_de_pll_enable(dev_priv, vco);

	val = divider | skl_cdclk_decimal(cdclk);
1395 1396 1397 1398
	if (pipe == INVALID_PIPE)
		val |= BXT_CDCLK_CD2X_PIPE_NONE;
	else
		val |= BXT_CDCLK_CD2X_PIPE(pipe);
1399 1400 1401 1402 1403 1404 1405 1406
	/*
	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
	 * enable otherwise.
	 */
	if (cdclk >= 500000)
		val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
	I915_WRITE(CDCLK_CTL, val);

1407 1408 1409
	if (pipe != INVALID_PIPE)
		intel_wait_for_vblank(dev_priv, pipe);

1410 1411 1412 1413 1414 1415 1416 1417
	/*
	 * 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,
1418
					      cdclk_state->voltage_level, 150, 2);
1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432
	if (ret) {
		DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
			  ret, cdclk);
		return;
	}

	intel_update_cdclk(dev_priv);
}

static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
{
	u32 cdctl, expected;

	intel_update_cdclk(dev_priv);
1433
	intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
1434

1435
	if (dev_priv->cdclk.hw.vco == 0 ||
1436
	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
		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.
	 */
	cdctl = I915_READ(CDCLK_CTL);
	/*
	 * Let's ignore the pipe field, since BIOS could have configured the
	 * dividers both synching to an active pipe, or asynchronously
	 * (PIPE_NONE).
	 */
	cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;

	expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
1454
		skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);
1455 1456 1457 1458
	/*
	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
	 * enable otherwise.
	 */
1459
	if (dev_priv->cdclk.hw.cdclk >= 500000)
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469
		expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;

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

sanitize:
	DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");

	/* force cdclk programming */
1470
	dev_priv->cdclk.hw.cdclk = 0;
1471 1472

	/* force full PLL disable + enable */
1473
	dev_priv->cdclk.hw.vco = -1;
1474 1475
}

1476
static void bxt_init_cdclk(struct drm_i915_private *dev_priv)
1477
{
1478
	struct intel_cdclk_state cdclk_state;
1479 1480 1481

	bxt_sanitize_cdclk(dev_priv);

1482 1483
	if (dev_priv->cdclk.hw.cdclk != 0 &&
	    dev_priv->cdclk.hw.vco != 0)
1484 1485
		return;

1486 1487
	cdclk_state = dev_priv->cdclk.hw;

1488 1489 1490 1491 1492
	/*
	 * FIXME:
	 * - The initial CDCLK needs to be read from VBT.
	 *   Need to make this change after VBT has changes for BXT.
	 */
1493
	if (IS_GEMINILAKE(dev_priv)) {
1494 1495
		cdclk_state.cdclk = glk_calc_cdclk(0);
		cdclk_state.vco = glk_de_pll_vco(dev_priv, cdclk_state.cdclk);
1496
	} else {
1497 1498
		cdclk_state.cdclk = bxt_calc_cdclk(0);
		cdclk_state.vco = bxt_de_pll_vco(dev_priv, cdclk_state.cdclk);
1499
	}
1500
	cdclk_state.voltage_level = bxt_calc_voltage_level(cdclk_state.cdclk);
1501

1502
	bxt_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
1503 1504
}

1505
static void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
1506
{
1507 1508
	struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;

1509
	cdclk_state.cdclk = cdclk_state.bypass;
1510
	cdclk_state.vco = 0;
1511
	cdclk_state.voltage_level = bxt_calc_voltage_level(cdclk_state.cdclk);
1512

1513
	bxt_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
1514 1515
}

1516
static int cnl_calc_cdclk(int min_cdclk)
1517
{
1518
	if (min_cdclk > 336000)
1519
		return 528000;
1520
	else if (min_cdclk > 168000)
1521 1522 1523 1524 1525
		return 336000;
	else
		return 168000;
}

1526 1527
static u8 cnl_calc_voltage_level(int cdclk)
{
1528
	if (cdclk > 336000)
1529
		return 2;
1530 1531 1532 1533
	else if (cdclk > 168000)
		return 1;
	else
		return 0;
1534 1535
}

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565
static void cnl_cdclk_pll_update(struct drm_i915_private *dev_priv,
				 struct intel_cdclk_state *cdclk_state)
{
	u32 val;

	if (I915_READ(SKL_DSSM) & CNL_DSSM_CDCLK_PLL_REFCLK_24MHz)
		cdclk_state->ref = 24000;
	else
		cdclk_state->ref = 19200;

	cdclk_state->vco = 0;

	val = I915_READ(BXT_DE_PLL_ENABLE);
	if ((val & BXT_DE_PLL_PLL_ENABLE) == 0)
		return;

	if (WARN_ON((val & BXT_DE_PLL_LOCK) == 0))
		return;

	cdclk_state->vco = (val & CNL_CDCLK_PLL_RATIO_MASK) * cdclk_state->ref;
}

static void cnl_get_cdclk(struct drm_i915_private *dev_priv,
			 struct intel_cdclk_state *cdclk_state)
{
	u32 divider;
	int div;

	cnl_cdclk_pll_update(dev_priv, cdclk_state);

1566
	cdclk_state->cdclk = cdclk_state->bypass = cdclk_state->ref;
1567 1568

	if (cdclk_state->vco == 0)
1569
		goto out;
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585

	divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;

	switch (divider) {
	case BXT_CDCLK_CD2X_DIV_SEL_1:
		div = 2;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_2:
		div = 4;
		break;
	default:
		MISSING_CASE(divider);
		return;
	}

	cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco, div);
1586 1587 1588 1589 1590 1591 1592 1593

 out:
	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
	cdclk_state->voltage_level =
		cnl_calc_voltage_level(cdclk_state->cdclk);
1594 1595
}

1596 1597 1598 1599 1600 1601 1602 1603 1604 1605
static void cnl_cdclk_pll_disable(struct drm_i915_private *dev_priv)
{
	u32 val;

	val = I915_READ(BXT_DE_PLL_ENABLE);
	val &= ~BXT_DE_PLL_PLL_ENABLE;
	I915_WRITE(BXT_DE_PLL_ENABLE, val);

	/* Timeout 200us */
	if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
M
Masanari Iida 已提交
1606
		DRM_ERROR("timeout waiting for CDCLK PLL unlock\n");
1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623

	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);
	I915_WRITE(BXT_DE_PLL_ENABLE, val);

	val |= BXT_DE_PLL_PLL_ENABLE;
	I915_WRITE(BXT_DE_PLL_ENABLE, val);

	/* Timeout 200us */
	if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
M
Masanari Iida 已提交
1624
		DRM_ERROR("timeout waiting for CDCLK PLL lock\n");
1625 1626 1627 1628 1629

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

static void cnl_set_cdclk(struct drm_i915_private *dev_priv,
1630 1631
			  const struct intel_cdclk_state *cdclk_state,
			  enum pipe pipe)
1632 1633 1634
{
	int cdclk = cdclk_state->cdclk;
	int vco = cdclk_state->vco;
1635
	u32 val, divider;
1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650
	int ret;

	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) {
		DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
			  ret);
		return;
	}

	/* cdclk = vco / 2 / div{1,2} */
	switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
	default:
1651
		WARN_ON(cdclk != dev_priv->cdclk.hw.bypass);
1652
		WARN_ON(vco != 0);
1653 1654
		/* fall through */
	case 2:
1655 1656
		divider = BXT_CDCLK_CD2X_DIV_SEL_1;
		break;
1657 1658 1659
	case 4:
		divider = BXT_CDCLK_CD2X_DIV_SEL_2;
		break;
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
	}

	if (dev_priv->cdclk.hw.vco != 0 &&
	    dev_priv->cdclk.hw.vco != vco)
		cnl_cdclk_pll_disable(dev_priv);

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

	val = divider | skl_cdclk_decimal(cdclk);
1670 1671 1672 1673
	if (pipe == INVALID_PIPE)
		val |= BXT_CDCLK_CD2X_PIPE_NONE;
	else
		val |= BXT_CDCLK_CD2X_PIPE(pipe);
1674 1675
	I915_WRITE(CDCLK_CTL, val);

1676 1677 1678
	if (pipe != INVALID_PIPE)
		intel_wait_for_vblank(dev_priv, pipe);

1679
	/* inform PCU of the change */
1680 1681
	sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
				cdclk_state->voltage_level);
1682 1683

	intel_update_cdclk(dev_priv);
1684 1685 1686 1687 1688 1689

	/*
	 * Can't read out the voltage level :(
	 * Let's just assume everything is as expected.
	 */
	dev_priv->cdclk.hw.voltage_level = cdclk_state->voltage_level;
1690 1691
}

1692 1693 1694 1695
static int cnl_cdclk_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
{
	int ratio;

1696
	if (cdclk == dev_priv->cdclk.hw.bypass)
1697 1698 1699 1700 1701
		return 0;

	switch (cdclk) {
	default:
		MISSING_CASE(cdclk);
1702
		/* fall through */
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
	case 168000:
	case 336000:
		ratio = dev_priv->cdclk.hw.ref == 19200 ? 35 : 28;
		break;
	case 528000:
		ratio = dev_priv->cdclk.hw.ref == 19200 ? 55 : 44;
		break;
	}

	return dev_priv->cdclk.hw.ref * ratio;
}

static void cnl_sanitize_cdclk(struct drm_i915_private *dev_priv)
{
	u32 cdctl, expected;

	intel_update_cdclk(dev_priv);
1720
	intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
1721 1722

	if (dev_priv->cdclk.hw.vco == 0 ||
1723
	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
		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.
	 */
	cdctl = I915_READ(CDCLK_CTL);
	/*
	 * Let's ignore the pipe field, since BIOS could have configured the
	 * dividers both synching to an active pipe, or asynchronously
	 * (PIPE_NONE).
	 */
	cdctl &= ~BXT_CDCLK_CD2X_PIPE_NONE;

	expected = (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) |
		   skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);

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

sanitize:
	DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");

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

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

1757 1758
static int icl_calc_cdclk(int min_cdclk, unsigned int ref)
{
1759 1760 1761 1762
	static const int ranges_24[] = { 180000, 192000, 312000, 552000, 648000 };
	static const int ranges_19_38[] = { 172800, 192000, 307200, 556800, 652800 };
	const int *ranges;
	int len, i;
1763 1764 1765 1766

	switch (ref) {
	default:
		MISSING_CASE(ref);
1767
		/* fall through */
1768 1769
	case 24000:
		ranges = ranges_24;
1770
		len = ARRAY_SIZE(ranges_24);
1771 1772 1773 1774
		break;
	case 19200:
	case 38400:
		ranges = ranges_19_38;
1775
		len = ARRAY_SIZE(ranges_19_38);
1776 1777 1778
		break;
	}

1779 1780 1781 1782 1783 1784 1785
	for (i = 0; i < len; i++) {
		if (min_cdclk <= ranges[i])
			return ranges[i];
	}

	WARN_ON(min_cdclk > ranges[len - 1]);
	return ranges[len - 1];
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
}

static int icl_calc_cdclk_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
{
	int ratio;

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

	switch (cdclk) {
	default:
		MISSING_CASE(cdclk);
1798
		/* fall through */
1799
	case 172800:
1800 1801 1802 1803 1804 1805
	case 307200:
	case 556800:
	case 652800:
		WARN_ON(dev_priv->cdclk.hw.ref != 19200 &&
			dev_priv->cdclk.hw.ref != 38400);
		break;
1806
	case 180000:
1807 1808 1809 1810
	case 312000:
	case 552000:
	case 648000:
		WARN_ON(dev_priv->cdclk.hw.ref != 24000);
1811 1812 1813 1814 1815 1816
		break;
	case 192000:
		WARN_ON(dev_priv->cdclk.hw.ref != 19200 &&
			dev_priv->cdclk.hw.ref != 38400 &&
			dev_priv->cdclk.hw.ref != 24000);
		break;
1817 1818 1819 1820 1821 1822 1823 1824
	}

	ratio = cdclk / (dev_priv->cdclk.hw.ref / 2);

	return dev_priv->cdclk.hw.ref * ratio;
}

static void icl_set_cdclk(struct drm_i915_private *dev_priv,
1825 1826
			  const struct intel_cdclk_state *cdclk_state,
			  enum pipe pipe)
1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848
{
	unsigned int cdclk = cdclk_state->cdclk;
	unsigned int vco = cdclk_state->vco;
	int ret;

	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) {
		DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
			  ret);
		return;
	}

	if (dev_priv->cdclk.hw.vco != 0 &&
	    dev_priv->cdclk.hw.vco != vco)
		cnl_cdclk_pll_disable(dev_priv);

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

1849 1850 1851 1852 1853
	/*
	 * On ICL CD2X_DIV can only be 1, so we'll never end up changing the
	 * divider here synchronized to a pipe while CDCLK is on, nor will we
	 * need the corresponding vblank wait.
	 */
1854 1855 1856
	I915_WRITE(CDCLK_CTL, ICL_CDCLK_CD2X_PIPE_NONE |
			      skl_cdclk_decimal(cdclk));

1857 1858
	sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
				cdclk_state->voltage_level);
1859 1860

	intel_update_cdclk(dev_priv);
1861 1862 1863 1864 1865 1866 1867 1868

	/*
	 * Can't read out the voltage level :(
	 * Let's just assume everything is as expected.
	 */
	dev_priv->cdclk.hw.voltage_level = cdclk_state->voltage_level;
}

1869
static u8 icl_calc_voltage_level(struct drm_i915_private *dev_priv, int cdclk)
1870
{
1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885
	if (IS_ELKHARTLAKE(dev_priv)) {
		if (cdclk > 312000)
			return 2;
		else if (cdclk > 180000)
			return 1;
		else
			return 0;
	} else {
		if (cdclk > 556800)
			return 2;
		else if (cdclk > 312000)
			return 1;
		else
			return 0;
	}
1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898
}

static void icl_get_cdclk(struct drm_i915_private *dev_priv,
			  struct intel_cdclk_state *cdclk_state)
{
	u32 val;

	cdclk_state->bypass = 50000;

	val = I915_READ(SKL_DSSM);
	switch (val & ICL_DSSM_CDCLK_PLL_REFCLK_MASK) {
	default:
		MISSING_CASE(val);
1899
		/* fall through */
1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919
	case ICL_DSSM_CDCLK_PLL_REFCLK_24MHz:
		cdclk_state->ref = 24000;
		break;
	case ICL_DSSM_CDCLK_PLL_REFCLK_19_2MHz:
		cdclk_state->ref = 19200;
		break;
	case ICL_DSSM_CDCLK_PLL_REFCLK_38_4MHz:
		cdclk_state->ref = 38400;
		break;
	}

	val = I915_READ(BXT_DE_PLL_ENABLE);
	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.
		 */
		cdclk_state->vco = 0;
		cdclk_state->cdclk = cdclk_state->bypass;
1920
		goto out;
1921 1922 1923 1924 1925 1926 1927 1928
	}

	cdclk_state->vco = (val & BXT_DE_PLL_RATIO_MASK) * cdclk_state->ref;

	val = I915_READ(CDCLK_CTL);
	WARN_ON((val & BXT_CDCLK_CD2X_DIV_SEL_MASK) != 0);

	cdclk_state->cdclk = cdclk_state->vco / 2;
1929 1930 1931 1932 1933 1934 1935

out:
	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
	cdclk_state->voltage_level =
1936
		icl_calc_voltage_level(dev_priv, cdclk_state->cdclk);
1937 1938
}

1939
static void icl_init_cdclk(struct drm_i915_private *dev_priv)
1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
{
	struct intel_cdclk_state sanitized_state;
	u32 val;

	/* This sets dev_priv->cdclk.hw. */
	intel_update_cdclk(dev_priv);
	intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");

	/* This means CDCLK disabled. */
	if (dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
		goto sanitize;

	val = I915_READ(CDCLK_CTL);

	if ((val & BXT_CDCLK_CD2X_DIV_SEL_MASK) != 0)
		goto sanitize;

	if ((val & CDCLK_FREQ_DECIMAL_MASK) !=
	    skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk))
		goto sanitize;

	return;

sanitize:
	DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");

	sanitized_state.ref = dev_priv->cdclk.hw.ref;
	sanitized_state.cdclk = icl_calc_cdclk(0, sanitized_state.ref);
	sanitized_state.vco = icl_calc_cdclk_pll_vco(dev_priv,
						     sanitized_state.cdclk);
1970
	sanitized_state.voltage_level =
1971 1972
				icl_calc_voltage_level(dev_priv,
						       sanitized_state.cdclk);
1973

1974
	icl_set_cdclk(dev_priv, &sanitized_state, INVALID_PIPE);
1975 1976
}

1977
static void icl_uninit_cdclk(struct drm_i915_private *dev_priv)
1978 1979 1980 1981 1982
{
	struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;

	cdclk_state.cdclk = cdclk_state.bypass;
	cdclk_state.vco = 0;
1983 1984
	cdclk_state.voltage_level = icl_calc_voltage_level(dev_priv,
							   cdclk_state.cdclk);
1985

1986
	icl_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
1987 1988
}

1989
static void cnl_init_cdclk(struct drm_i915_private *dev_priv)
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
{
	struct intel_cdclk_state cdclk_state;

	cnl_sanitize_cdclk(dev_priv);

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

	cdclk_state = dev_priv->cdclk.hw;

2001
	cdclk_state.cdclk = cnl_calc_cdclk(0);
2002
	cdclk_state.vco = cnl_cdclk_pll_vco(dev_priv, cdclk_state.cdclk);
2003
	cdclk_state.voltage_level = cnl_calc_voltage_level(cdclk_state.cdclk);
2004

2005
	cnl_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
2006 2007
}

2008
static void cnl_uninit_cdclk(struct drm_i915_private *dev_priv)
2009 2010 2011
{
	struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;

2012
	cdclk_state.cdclk = cdclk_state.bypass;
2013
	cdclk_state.vco = 0;
2014
	cdclk_state.voltage_level = cnl_calc_voltage_level(cdclk_state.cdclk);
2015

2016
	cnl_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
2017 2018
}

2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
/**
 * intel_cdclk_init - Initialize CDCLK
 * @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.
 */
void intel_cdclk_init(struct drm_i915_private *i915)
{
2030
	if (INTEL_GEN(i915) >= 11)
2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048
		icl_init_cdclk(i915);
	else if (IS_CANNONLAKE(i915))
		cnl_init_cdclk(i915);
	else if (IS_GEN9_BC(i915))
		skl_init_cdclk(i915);
	else if (IS_GEN9_LP(i915))
		bxt_init_cdclk(i915);
}

/**
 * intel_cdclk_uninit - Uninitialize CDCLK
 * @i915: i915 device
 *
 * Uninitialize CDCLK. This is done only during the display core
 * uninitialization sequence.
 */
void intel_cdclk_uninit(struct drm_i915_private *i915)
{
2049
	if (INTEL_GEN(i915) >= 11)
2050 2051 2052 2053 2054 2055 2056 2057 2058
		icl_uninit_cdclk(i915);
	else if (IS_CANNONLAKE(i915))
		cnl_uninit_cdclk(i915);
	else if (IS_GEN9_BC(i915))
		skl_uninit_cdclk(i915);
	else if (IS_GEN9_LP(i915))
		bxt_uninit_cdclk(i915);
}

2059
/**
2060
 * intel_cdclk_needs_modeset - Determine if two CDCLK states require a modeset on all pipes
2061 2062 2063 2064
 * @a: first CDCLK state
 * @b: second CDCLK state
 *
 * Returns:
2065
 * True if the CDCLK states require pipes to be off during reprogramming, false if not.
2066
 */
2067
bool intel_cdclk_needs_modeset(const struct intel_cdclk_state *a,
2068 2069
			       const struct intel_cdclk_state *b)
{
2070 2071 2072 2073 2074
	return a->cdclk != b->cdclk ||
		a->vco != b->vco ||
		a->ref != b->ref;
}

2075 2076
/**
 * intel_cdclk_needs_cd2x_update - Determine if two CDCLK states require a cd2x divider update
2077
 * @dev_priv: Not a CDCLK state, it's the drm_i915_private!
2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096
 * @a: first CDCLK state
 * @b: second CDCLK state
 *
 * Returns:
 * True if the CDCLK states require just a cd2x divider update, false if not.
 */
bool intel_cdclk_needs_cd2x_update(struct drm_i915_private *dev_priv,
				   const struct intel_cdclk_state *a,
				   const struct intel_cdclk_state *b)
{
	/* 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;
}

2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
/**
 * intel_cdclk_changed - Determine if two CDCLK states are different
 * @a: first CDCLK state
 * @b: second CDCLK state
 *
 * Returns:
 * True if the CDCLK states don't match, false if they do.
 */
bool intel_cdclk_changed(const struct intel_cdclk_state *a,
			 const struct intel_cdclk_state *b)
{
	return intel_cdclk_needs_modeset(a, b) ||
		a->voltage_level != b->voltage_level;
2110 2111
}

2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131
/**
 * intel_cdclk_swap_state - make atomic CDCLK configuration effective
 * @state: atomic state
 *
 * This is the CDCLK version of drm_atomic_helper_swap_state() since the
 * helper does not handle driver-specific global state.
 *
 * Similarly to the atomic helpers this function does a complete swap,
 * i.e. it also puts the old state into @state. This is used by the commit
 * code to determine how CDCLK has changed (for instance did it increase or
 * decrease).
 */
void intel_cdclk_swap_state(struct intel_atomic_state *state)
{
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);

	swap(state->cdclk.logical, dev_priv->cdclk.logical);
	swap(state->cdclk.actual, dev_priv->cdclk.actual);
}

2132 2133 2134
void intel_dump_cdclk_state(const struct intel_cdclk_state *cdclk_state,
			    const char *context)
{
2135
	DRM_DEBUG_DRIVER("%s %d kHz, VCO %d kHz, ref %d kHz, bypass %d kHz, voltage level %d\n",
2136
			 context, cdclk_state->cdclk, cdclk_state->vco,
2137 2138
			 cdclk_state->ref, cdclk_state->bypass,
			 cdclk_state->voltage_level);
2139 2140
}

2141 2142 2143 2144
/**
 * intel_set_cdclk - Push the CDCLK state to the hardware
 * @dev_priv: i915 device
 * @cdclk_state: new CDCLK state
2145
 * @pipe: pipe with which to synchronize the update
2146 2147 2148 2149
 *
 * Program the hardware based on the passed in CDCLK state,
 * if necessary.
 */
2150 2151 2152
static void intel_set_cdclk(struct drm_i915_private *dev_priv,
			    const struct intel_cdclk_state *cdclk_state,
			    enum pipe pipe)
2153
{
2154
	if (!intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_state))
2155 2156 2157 2158 2159
		return;

	if (WARN_ON_ONCE(!dev_priv->display.set_cdclk))
		return;

2160
	intel_dump_cdclk_state(cdclk_state, "Changing CDCLK to");
2161

2162
	dev_priv->display.set_cdclk(dev_priv, cdclk_state, pipe);
2163 2164 2165 2166 2167 2168

	if (WARN(intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_state),
		 "cdclk state doesn't match!\n")) {
		intel_dump_cdclk_state(&dev_priv->cdclk.hw, "[hw state]");
		intel_dump_cdclk_state(cdclk_state, "[sw state]");
	}
2169 2170
}

2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210
/**
 * intel_set_cdclk_pre_plane_update - Push the CDCLK state to the hardware
 * @dev_priv: i915 device
 * @old_state: old CDCLK state
 * @new_state: new CDCLK state
 * @pipe: pipe with which to synchronize the update
 *
 * Program the hardware before updating the HW plane state based on the passed
 * in CDCLK state, if necessary.
 */
void
intel_set_cdclk_pre_plane_update(struct drm_i915_private *dev_priv,
				 const struct intel_cdclk_state *old_state,
				 const struct intel_cdclk_state *new_state,
				 enum pipe pipe)
{
	if (pipe == INVALID_PIPE || old_state->cdclk <= new_state->cdclk)
		intel_set_cdclk(dev_priv, new_state, pipe);
}

/**
 * intel_set_cdclk_post_plane_update - Push the CDCLK state to the hardware
 * @dev_priv: i915 device
 * @old_state: old CDCLK state
 * @new_state: new CDCLK state
 * @pipe: pipe with which to synchronize the update
 *
 * Program the hardware after updating the HW plane state based on the passed
 * in CDCLK state, if necessary.
 */
void
intel_set_cdclk_post_plane_update(struct drm_i915_private *dev_priv,
				  const struct intel_cdclk_state *old_state,
				  const struct intel_cdclk_state *new_state,
				  enum pipe pipe)
{
	if (pipe != INVALID_PIPE && old_state->cdclk > new_state->cdclk)
		intel_set_cdclk(dev_priv, new_state, pipe);
}

2211 2212 2213
static int intel_pixel_rate_to_cdclk(struct drm_i915_private *dev_priv,
				     int pixel_rate)
{
2214
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
2215
		return DIV_ROUND_UP(pixel_rate, 2);
2216
	else if (IS_GEN(dev_priv, 9) ||
2217 2218 2219 2220 2221 2222 2223 2224 2225
		 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);
	else
		return DIV_ROUND_UP(pixel_rate * 100, 90);
}

int intel_crtc_compute_min_cdclk(const struct intel_crtc_state *crtc_state)
2226 2227 2228
{
	struct drm_i915_private *dev_priv =
		to_i915(crtc_state->base.crtc->dev);
2229 2230 2231 2232 2233 2234
	int min_cdclk;

	if (!crtc_state->base.enable)
		return 0;

	min_cdclk = intel_pixel_rate_to_cdclk(dev_priv, crtc_state->pixel_rate);
2235 2236

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

2240 2241 2242
	/* 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
2243
	 * restriction for GLK is 316.8 MHz.
2244 2245 2246 2247
	 */
	if (intel_crtc_has_dp_encoder(crtc_state) &&
	    crtc_state->has_audio &&
	    crtc_state->port_clock >= 540000 &&
2248
	    crtc_state->lane_count == 4) {
2249 2250 2251
		if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv)) {
			/* Display WA #1145: glk,cnl */
			min_cdclk = max(316800, min_cdclk);
2252
		} else if (IS_GEN(dev_priv, 9) || IS_BROADWELL(dev_priv)) {
2253 2254 2255
			/* Display WA #1144: skl,bxt */
			min_cdclk = max(432000, min_cdclk);
		}
2256
	}
2257

2258 2259
	/*
	 * According to BSpec, "The CD clock frequency must be at least twice
2260 2261
	 * the frequency of the Azalia BCLK." and BCLK is 96 MHz by default.
	 */
2262
	if (crtc_state->has_audio && INTEL_GEN(dev_priv) >= 9)
2263
		min_cdclk = max(2 * 96000, min_cdclk);
2264

2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275
	/*
	 * "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);

2276 2277 2278 2279 2280 2281 2282 2283
	/*
	 * 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);

2284 2285 2286 2287 2288 2289 2290 2291 2292
	/*
	 * 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);

2293 2294 2295 2296 2297 2298
	if (min_cdclk > dev_priv->max_cdclk_freq) {
		DRM_DEBUG_KMS("required cdclk (%d kHz) exceeds max (%d kHz)\n",
			      min_cdclk, dev_priv->max_cdclk_freq);
		return -EINVAL;
	}

2299
	return min_cdclk;
2300 2301
}

2302
static int intel_compute_min_cdclk(struct intel_atomic_state *state)
2303
{
2304
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2305
	struct intel_crtc *crtc;
2306
	struct intel_crtc_state *crtc_state;
2307
	int min_cdclk, i;
2308 2309
	enum pipe pipe;

2310 2311
	memcpy(state->min_cdclk, dev_priv->min_cdclk,
	       sizeof(state->min_cdclk));
2312

2313
	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2314 2315 2316 2317
		min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
		if (min_cdclk < 0)
			return min_cdclk;

2318
		state->min_cdclk[i] = min_cdclk;
2319
	}
2320

2321
	min_cdclk = state->cdclk.force_min_cdclk;
2322
	for_each_pipe(dev_priv, pipe)
2323
		min_cdclk = max(state->min_cdclk[pipe], min_cdclk);
2324

2325
	return min_cdclk;
2326 2327
}

2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364
/*
 * 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.
 */
static u8 cnl_compute_min_voltage_level(struct intel_atomic_state *state)
{
	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;

	memcpy(state->min_voltage_level, dev_priv->min_voltage_level,
	       sizeof(state->min_voltage_level));

	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
		if (crtc_state->base.enable)
			state->min_voltage_level[i] =
				crtc_state->min_voltage_level;
		else
			state->min_voltage_level[i] = 0;
	}

	min_voltage_level = 0;
	for_each_pipe(dev_priv, pipe)
		min_voltage_level = max(state->min_voltage_level[pipe],
					min_voltage_level);

	return min_voltage_level;
}

2365
static int vlv_modeset_calc_cdclk(struct intel_atomic_state *state)
2366
{
2367
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2368
	int min_cdclk, cdclk;
2369

2370 2371 2372
	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;
2373

2374
	cdclk = vlv_calc_cdclk(dev_priv, min_cdclk);
2375

2376 2377
	state->cdclk.logical.cdclk = cdclk;
	state->cdclk.logical.voltage_level =
2378
		vlv_calc_voltage_level(dev_priv, cdclk);
2379

2380 2381
	if (!state->active_crtcs) {
		cdclk = vlv_calc_cdclk(dev_priv, state->cdclk.force_min_cdclk);
2382

2383 2384
		state->cdclk.actual.cdclk = cdclk;
		state->cdclk.actual.voltage_level =
2385
			vlv_calc_voltage_level(dev_priv, cdclk);
2386
	} else {
2387
		state->cdclk.actual = state->cdclk.logical;
2388
	}
2389 2390 2391 2392

	return 0;
}

2393
static int bdw_modeset_calc_cdclk(struct intel_atomic_state *state)
2394
{
2395 2396 2397 2398 2399
	int min_cdclk, cdclk;

	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;
2400 2401 2402 2403 2404

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

2407 2408
	state->cdclk.logical.cdclk = cdclk;
	state->cdclk.logical.voltage_level =
2409
		bdw_calc_voltage_level(cdclk);
2410

2411 2412
	if (!state->active_crtcs) {
		cdclk = bdw_calc_cdclk(state->cdclk.force_min_cdclk);
2413

2414 2415
		state->cdclk.actual.cdclk = cdclk;
		state->cdclk.actual.voltage_level =
2416
			bdw_calc_voltage_level(cdclk);
2417
	} else {
2418
		state->cdclk.actual = state->cdclk.logical;
2419
	}
2420 2421 2422 2423

	return 0;
}

2424
static int skl_dpll0_vco(struct intel_atomic_state *state)
2425
{
2426
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2427 2428 2429 2430
	struct intel_crtc *crtc;
	struct intel_crtc_state *crtc_state;
	int vco, i;

2431
	vco = state->cdclk.logical.vco;
2432 2433 2434
	if (!vco)
		vco = dev_priv->skl_preferred_vco_freq;

2435
	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
		if (!crtc_state->base.enable)
			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;
}

2460
static int skl_modeset_calc_cdclk(struct intel_atomic_state *state)
2461
{
2462 2463 2464 2465 2466
	int min_cdclk, cdclk, vco;

	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;
2467

2468
	vco = skl_dpll0_vco(state);
2469 2470 2471 2472 2473

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

2476 2477 2478
	state->cdclk.logical.vco = vco;
	state->cdclk.logical.cdclk = cdclk;
	state->cdclk.logical.voltage_level =
2479
		skl_calc_voltage_level(cdclk);
2480

2481 2482
	if (!state->active_crtcs) {
		cdclk = skl_calc_cdclk(state->cdclk.force_min_cdclk, vco);
2483

2484 2485 2486
		state->cdclk.actual.vco = vco;
		state->cdclk.actual.cdclk = cdclk;
		state->cdclk.actual.voltage_level =
2487
			skl_calc_voltage_level(cdclk);
2488
	} else {
2489
		state->cdclk.actual = state->cdclk.logical;
2490
	}
2491 2492 2493 2494

	return 0;
}

2495
static int bxt_modeset_calc_cdclk(struct intel_atomic_state *state)
2496
{
2497
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2498 2499 2500 2501 2502
	int min_cdclk, cdclk, vco;

	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;
2503

2504
	if (IS_GEMINILAKE(dev_priv)) {
2505
		cdclk = glk_calc_cdclk(min_cdclk);
2506 2507
		vco = glk_de_pll_vco(dev_priv, cdclk);
	} else {
2508
		cdclk = bxt_calc_cdclk(min_cdclk);
2509 2510 2511
		vco = bxt_de_pll_vco(dev_priv, cdclk);
	}

2512 2513 2514
	state->cdclk.logical.vco = vco;
	state->cdclk.logical.cdclk = cdclk;
	state->cdclk.logical.voltage_level =
2515
		bxt_calc_voltage_level(cdclk);
2516

2517
	if (!state->active_crtcs) {
2518
		if (IS_GEMINILAKE(dev_priv)) {
2519
			cdclk = glk_calc_cdclk(state->cdclk.force_min_cdclk);
2520 2521
			vco = glk_de_pll_vco(dev_priv, cdclk);
		} else {
2522
			cdclk = bxt_calc_cdclk(state->cdclk.force_min_cdclk);
2523 2524
			vco = bxt_de_pll_vco(dev_priv, cdclk);
		}
2525

2526 2527 2528
		state->cdclk.actual.vco = vco;
		state->cdclk.actual.cdclk = cdclk;
		state->cdclk.actual.voltage_level =
2529
			bxt_calc_voltage_level(cdclk);
2530
	} else {
2531
		state->cdclk.actual = state->cdclk.logical;
2532 2533 2534 2535 2536
	}

	return 0;
}

2537
static int cnl_modeset_calc_cdclk(struct intel_atomic_state *state)
2538
{
2539
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2540 2541 2542 2543 2544
	int min_cdclk, cdclk, vco;

	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;
2545

2546
	cdclk = cnl_calc_cdclk(min_cdclk);
2547 2548
	vco = cnl_cdclk_pll_vco(dev_priv, cdclk);

2549 2550 2551
	state->cdclk.logical.vco = vco;
	state->cdclk.logical.cdclk = cdclk;
	state->cdclk.logical.voltage_level =
2552
		max(cnl_calc_voltage_level(cdclk),
2553
		    cnl_compute_min_voltage_level(state));
2554

2555 2556
	if (!state->active_crtcs) {
		cdclk = cnl_calc_cdclk(state->cdclk.force_min_cdclk);
2557 2558
		vco = cnl_cdclk_pll_vco(dev_priv, cdclk);

2559 2560 2561
		state->cdclk.actual.vco = vco;
		state->cdclk.actual.cdclk = cdclk;
		state->cdclk.actual.voltage_level =
2562
			cnl_calc_voltage_level(cdclk);
2563
	} else {
2564
		state->cdclk.actual = state->cdclk.logical;
2565 2566 2567 2568 2569
	}

	return 0;
}

2570
static int icl_modeset_calc_cdclk(struct intel_atomic_state *state)
2571
{
2572 2573
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
	unsigned int ref = state->cdclk.logical.ref;
2574 2575 2576 2577 2578 2579 2580 2581 2582
	int min_cdclk, cdclk, vco;

	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;

	cdclk = icl_calc_cdclk(min_cdclk, ref);
	vco = icl_calc_cdclk_pll_vco(dev_priv, cdclk);

2583 2584 2585
	state->cdclk.logical.vco = vco;
	state->cdclk.logical.cdclk = cdclk;
	state->cdclk.logical.voltage_level =
2586
		max(icl_calc_voltage_level(dev_priv, cdclk),
2587
		    cnl_compute_min_voltage_level(state));
2588

2589 2590
	if (!state->active_crtcs) {
		cdclk = icl_calc_cdclk(state->cdclk.force_min_cdclk, ref);
2591 2592
		vco = icl_calc_cdclk_pll_vco(dev_priv, cdclk);

2593 2594 2595
		state->cdclk.actual.vco = vco;
		state->cdclk.actual.cdclk = cdclk;
		state->cdclk.actual.voltage_level =
2596
			icl_calc_voltage_level(dev_priv, cdclk);
2597
	} else {
2598
		state->cdclk.actual = state->cdclk.logical;
2599 2600 2601 2602 2603
	}

	return 0;
}

2604 2605 2606 2607
static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
{
	int max_cdclk_freq = dev_priv->max_cdclk_freq;

2608
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
2609
		return 2 * max_cdclk_freq;
2610
	else if (IS_GEN(dev_priv, 9) ||
2611
		 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
2612 2613 2614
		return max_cdclk_freq;
	else if (IS_CHERRYVIEW(dev_priv))
		return max_cdclk_freq*95/100;
2615
	else if (INTEL_GEN(dev_priv) < 4)
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630
		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)
{
2631 2632 2633 2634 2635 2636
	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) {
2637 2638 2639 2640 2641
		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)) {
2642 2643
		dev_priv->max_cdclk_freq = 528000;
	} else if (IS_GEN9_BC(dev_priv)) {
2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
		u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
		int max_cdclk, vco;

		vco = dev_priv->skl_preferred_vco_freq;
		WARN_ON(vco != 8100000 && vco != 8640000);

		/*
		 * 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?
		 */
		if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
			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 */
2690
		dev_priv->max_cdclk_freq = dev_priv->cdclk.hw.cdclk;
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709
	}

	dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);

	DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
			 dev_priv->max_cdclk_freq);

	DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
			 dev_priv->max_dotclk_freq);
}

/**
 * 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)
{
2710
	dev_priv->display.get_cdclk(dev_priv, &dev_priv->cdclk.hw);
2711 2712 2713 2714 2715 2716 2717 2718 2719

	/*
	 * 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))
		I915_WRITE(GMBUSFREQ_VLV,
2720
			   DIV_ROUND_UP(dev_priv->cdclk.hw.cdclk, 1000));
2721 2722
}

2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737
static int cnp_rawclk(struct drm_i915_private *dev_priv)
{
	u32 rawclk;
	int divider, fraction;

	if (I915_READ(SFUSE_STRAP) & SFUSE_STRAP_RAW_FREQUENCY) {
		/* 24 MHz */
		divider = 24000;
		fraction = 0;
	} else {
		/* 19.2 MHz */
		divider = 19000;
		fraction = 200;
	}

2738
	rawclk = CNP_RAWCLK_DIV(divider / 1000);
2739 2740
	if (fraction) {
		int numerator = 1;
2741

2742 2743
		rawclk |= CNP_RAWCLK_DEN(DIV_ROUND_CLOSEST(numerator * 1000,
							   fraction) - 1);
2744
		if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2745
			rawclk |= ICP_RAWCLK_NUM(numerator);
2746 2747 2748
	}

	I915_WRITE(PCH_RAWCLK_FREQ, rawclk);
2749
	return divider + fraction;
2750 2751
}

2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765
static int pch_rawclk(struct drm_i915_private *dev_priv)
{
	return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
}

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);
}

static int g4x_hrawclk(struct drm_i915_private *dev_priv)
{
2766
	u32 clkcfg;
2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779

	/* hrawclock is 1/4 the FSB frequency */
	clkcfg = I915_READ(CLKCFG);
	switch (clkcfg & CLKCFG_FSB_MASK) {
	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:
2780
	case CLKCFG_FSB_1067_ALT:
2781 2782
		return 266667;
	case CLKCFG_FSB_1333:
2783
	case CLKCFG_FSB_1333_ALT:
2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798
		return 333333;
	default:
		return 133333;
	}
}

/**
 * intel_update_rawclk - Determine the current RAWCLK frequency
 * @dev_priv: i915 device
 *
 * Determine the current RAWCLK frequency. RAWCLK is a fixed
 * frequency clock so this needs to done only once.
 */
void intel_update_rawclk(struct drm_i915_private *dev_priv)
{
2799
	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
2800 2801
		dev_priv->rawclk_freq = cnp_rawclk(dev_priv);
	else if (HAS_PCH_SPLIT(dev_priv))
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819
		dev_priv->rawclk_freq = pch_rawclk(dev_priv);
	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
		dev_priv->rawclk_freq = vlv_hrawclk(dev_priv);
	else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
		dev_priv->rawclk_freq = g4x_hrawclk(dev_priv);
	else
		/* no rawclk on other platforms, or no need to know it */
		return;

	DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
}

/**
 * intel_init_cdclk_hooks - Initialize CDCLK related modesetting hooks
 * @dev_priv: i915 device
 */
void intel_init_cdclk_hooks(struct drm_i915_private *dev_priv)
{
2820
	if (INTEL_GEN(dev_priv) >= 11) {
2821 2822 2823 2824
		dev_priv->display.set_cdclk = icl_set_cdclk;
		dev_priv->display.modeset_calc_cdclk = icl_modeset_calc_cdclk;
	} else if (IS_CANNONLAKE(dev_priv)) {
		dev_priv->display.set_cdclk = cnl_set_cdclk;
2825
		dev_priv->display.modeset_calc_cdclk = cnl_modeset_calc_cdclk;
2826
	} else if (IS_GEN9_LP(dev_priv)) {
2827
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2828
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2829
	} else if (IS_GEN9_BC(dev_priv)) {
2830
		dev_priv->display.set_cdclk = skl_set_cdclk;
2831
		dev_priv->display.modeset_calc_cdclk = skl_modeset_calc_cdclk;
2832 2833
	} else if (IS_BROADWELL(dev_priv)) {
		dev_priv->display.set_cdclk = bdw_set_cdclk;
2834
		dev_priv->display.modeset_calc_cdclk = bdw_modeset_calc_cdclk;
2835 2836
	} else if (IS_CHERRYVIEW(dev_priv)) {
		dev_priv->display.set_cdclk = chv_set_cdclk;
2837
		dev_priv->display.modeset_calc_cdclk = vlv_modeset_calc_cdclk;
2838 2839
	} else if (IS_VALLEYVIEW(dev_priv)) {
		dev_priv->display.set_cdclk = vlv_set_cdclk;
2840
		dev_priv->display.modeset_calc_cdclk = vlv_modeset_calc_cdclk;
2841 2842
	}

2843
	if (INTEL_GEN(dev_priv) >= 11)
2844 2845
		dev_priv->display.get_cdclk = icl_get_cdclk;
	else if (IS_CANNONLAKE(dev_priv))
2846
		dev_priv->display.get_cdclk = cnl_get_cdclk;
2847 2848
	else if (IS_GEN9_LP(dev_priv))
		dev_priv->display.get_cdclk = bxt_get_cdclk;
2849 2850
	else if (IS_GEN9_BC(dev_priv))
		dev_priv->display.get_cdclk = skl_get_cdclk;
2851 2852 2853 2854 2855 2856
	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;
2857
	else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
2858
		dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
2859
	else if (IS_GEN(dev_priv, 5))
2860 2861 2862
		dev_priv->display.get_cdclk = fixed_450mhz_get_cdclk;
	else if (IS_GM45(dev_priv))
		dev_priv->display.get_cdclk = gm45_get_cdclk;
2863
	else if (IS_G45(dev_priv))
2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892
		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 */
		WARN(!IS_I830(dev_priv),
		     "Unknown platform. Assuming 133 MHz CDCLK\n");
		dev_priv->display.get_cdclk = fixed_133mhz_get_cdclk;
	}
}