intel_cdclk.c 76.1 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_atomic.h"
25
#include "intel_cdclk.h"
26
#include "intel_display_types.h"
27
#include "intel_sideband.h"
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 56

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

57
static void fixed_133mhz_get_cdclk(struct drm_i915_private *dev_priv,
58
				   struct intel_cdclk_config *cdclk_config)
59
{
60
	cdclk_config->cdclk = 133333;
61 62
}

63
static void fixed_200mhz_get_cdclk(struct drm_i915_private *dev_priv,
64
				   struct intel_cdclk_config *cdclk_config)
65
{
66
	cdclk_config->cdclk = 200000;
67 68
}

69
static void fixed_266mhz_get_cdclk(struct drm_i915_private *dev_priv,
70
				   struct intel_cdclk_config *cdclk_config)
71
{
72
	cdclk_config->cdclk = 266667;
73 74
}

75
static void fixed_333mhz_get_cdclk(struct drm_i915_private *dev_priv,
76
				   struct intel_cdclk_config *cdclk_config)
77
{
78
	cdclk_config->cdclk = 333333;
79 80
}

81
static void fixed_400mhz_get_cdclk(struct drm_i915_private *dev_priv,
82
				   struct intel_cdclk_config *cdclk_config)
83
{
84
	cdclk_config->cdclk = 400000;
85 86
}

87
static void fixed_450mhz_get_cdclk(struct drm_i915_private *dev_priv,
88
				   struct intel_cdclk_config *cdclk_config)
89
{
90
	cdclk_config->cdclk = 450000;
91 92
}

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

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

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

	pci_read_config_word(pdev, GCFGC, &gcfgc);

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

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

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

	pci_read_config_word(pdev, GCFGC, &gcfgc);

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

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

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

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

240 241
	tmp = intel_de_read(dev_priv,
			    IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv) ? HPLLVCO_MOBILE : HPLLVCO);
242 243 244

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

	return vco;
}

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

265
	cdclk_config->vco = intel_hpll_vco(dev_priv);
266

267 268 269 270 271 272 273
	pci_read_config_word(pdev, GCFGC, &tmp);

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

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

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

291 292
	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco,
						div_table[cdclk_sel]);
293
	return;
294 295

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

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

336
static void i965gm_get_cdclk(struct drm_i915_private *dev_priv,
337
			     struct intel_cdclk_config *cdclk_config)
338 339
{
	struct pci_dev *pdev = dev_priv->drm.pdev;
340 341 342 343
	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;
344
	unsigned int cdclk_sel;
345
	u16 tmp = 0;
346

347
	cdclk_config->vco = intel_hpll_vco(dev_priv);
348

349 350 351 352 353 354 355
	pci_read_config_word(pdev, GCFGC, &tmp);

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

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

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

370 371
	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco,
						div_table[cdclk_sel]);
372
	return;
373 374

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

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

388
	cdclk_config->vco = intel_hpll_vco(dev_priv);
389

390 391 392 393
	pci_read_config_word(pdev, GCFGC, &tmp);

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

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

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

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

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

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
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;
	}
}

469
static void vlv_get_cdclk(struct drm_i915_private *dev_priv,
470
			  struct intel_cdclk_config *cdclk_config)
471
{
472 473
	u32 val;

474 475 476
	vlv_iosf_sb_get(dev_priv,
			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));

477 478 479 480
	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);
481

482
	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
483 484 485

	vlv_iosf_sb_put(dev_priv,
			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));
486 487

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

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

504
	if (dev_priv->cdclk.hw.cdclk >= dev_priv->czclk_freq) {
505 506 507 508 509 510 511 512 513 514 515 516 517
		/* 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?
	 */
518 519
	intel_de_write(dev_priv, GCI_CONTROL,
		       VGA_FAST_MODE_DISABLE | default_credits);
520

521 522
	intel_de_write(dev_priv, GCI_CONTROL,
		       VGA_FAST_MODE_DISABLE | credits | PFI_CREDIT_RESEND);
523 524 525 526 527

	/*
	 * FIXME is this guaranteed to clear
	 * immediately or should we poll for it?
	 */
528
	WARN_ON(intel_de_read(dev_priv, GCI_CONTROL) & PFI_CREDIT_RESEND);
529 530
}

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

539 540 541 542 543 544 545 546 547 548 549 550
	switch (cdclk) {
	case 400000:
	case 333333:
	case 320000:
	case 266667:
	case 200000:
		break;
	default:
		MISSING_CASE(cdclk);
		return;
	}

551 552 553
	/* 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
554
	 * a system suspend.  So grab the display core domain, which covers
555 556
	 * the HW blocks needed for the following programming.
	 */
557
	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
558

559 560 561 562 563
	vlv_iosf_sb_get(dev_priv,
			BIT(VLV_IOSF_SB_CCK) |
			BIT(VLV_IOSF_SB_BUNIT) |
			BIT(VLV_IOSF_SB_PUNIT));

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

	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))
590 591
			drm_err(&dev_priv->drm,
				"timed out waiting for CDclk change\n");
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
	}

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

608
	vlv_iosf_sb_put(dev_priv,
609 610 611
			BIT(VLV_IOSF_SB_CCK) |
			BIT(VLV_IOSF_SB_BUNIT) |
			BIT(VLV_IOSF_SB_PUNIT));
612 613

	intel_update_cdclk(dev_priv);
614 615

	vlv_program_pfi_credits(dev_priv);
616

617
	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
618 619
}

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

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

639 640 641
	/* 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
642
	 * a system suspend.  So grab the display core domain, which covers
643 644
	 * the HW blocks needed for the following programming.
	 */
645
	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
646

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

	vlv_punit_put(dev_priv);
660 661

	intel_update_cdclk(dev_priv);
662 663

	vlv_program_pfi_credits(dev_priv);
664

665
	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
666 667
}

668
static int bdw_calc_cdclk(int min_cdclk)
669
{
670
	if (min_cdclk > 540000)
671
		return 675000;
672
	else if (min_cdclk > 450000)
673
		return 540000;
674
	else if (min_cdclk > 337500)
675 676 677 678 679
		return 450000;
	else
		return 337500;
}

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
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;
	}
}

695
static void bdw_get_cdclk(struct drm_i915_private *dev_priv,
696
			  struct intel_cdclk_config *cdclk_config)
697
{
698
	u32 lcpll = intel_de_read(dev_priv, LCPLL_CTL);
699
	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
700 701

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

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

722
static void bdw_set_cdclk(struct drm_i915_private *dev_priv,
723
			  const struct intel_cdclk_config *cdclk_config,
724
			  enum pipe pipe)
725
{
726
	int cdclk = cdclk_config->cdclk;
727
	u32 val;
728 729
	int ret;

730
	if (WARN((intel_de_read(dev_priv, LCPLL_CTL) &
731 732 733 734 735 736 737 738 739 740
		  (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) {
741 742
		drm_err(&dev_priv->drm,
			"failed to inform pcode about cdclk change\n");
743 744 745
		return;
	}

746
	val = intel_de_read(dev_priv, LCPLL_CTL);
747
	val |= LCPLL_CD_SOURCE_FCLK;
748
	intel_de_write(dev_priv, LCPLL_CTL, val);
749

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

758
	val = intel_de_read(dev_priv, LCPLL_CTL);
759 760 761
	val &= ~LCPLL_CLK_FREQ_MASK;

	switch (cdclk) {
762 763 764 765 766 767
	default:
		MISSING_CASE(cdclk);
		/* fall through */
	case 337500:
		val |= LCPLL_CLK_FREQ_337_5_BDW;
		break;
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;
	}

779
	intel_de_write(dev_priv, LCPLL_CTL, val);
780

781
	val = intel_de_read(dev_priv, LCPLL_CTL);
782
	val &= ~LCPLL_CD_SOURCE_FCLK;
783
	intel_de_write(dev_priv, LCPLL_CTL, val);
784

785 786
	if (wait_for_us((intel_de_read(dev_priv, LCPLL_CTL) &
			 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
787
		drm_err(&dev_priv->drm, "Switching back to LCPLL failed\n");
788

789
	sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
790
				cdclk_config->voltage_level);
791

792 793
	intel_de_write(dev_priv, CDCLK_FREQ,
		       DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
794 795 796 797

	intel_update_cdclk(dev_priv);
}

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

821 822
static u8 skl_calc_voltage_level(int cdclk)
{
823
	if (cdclk > 540000)
824
		return 3;
825 826 827 828 829 830
	else if (cdclk > 450000)
		return 2;
	else if (cdclk > 337500)
		return 1;
	else
		return 0;
831 832
}

833
static void skl_dpll0_update(struct drm_i915_private *dev_priv,
834
			     struct intel_cdclk_config *cdclk_config)
835 836 837
{
	u32 val;

838 839
	cdclk_config->ref = 24000;
	cdclk_config->vco = 0;
840

841
	val = intel_de_read(dev_priv, LCPLL1_CTL);
842 843 844 845 846 847
	if ((val & LCPLL_PLL_ENABLE) == 0)
		return;

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

848
	val = intel_de_read(dev_priv, DPLL_CTRL1);
849 850 851 852 853 854 855 856 857 858 859 860

	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):
861
		cdclk_config->vco = 8100000;
862 863 864
		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):
865
		cdclk_config->vco = 8640000;
866 867 868 869 870 871 872
		break;
	default:
		MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
		break;
	}
}

873
static void skl_get_cdclk(struct drm_i915_private *dev_priv,
874
			  struct intel_cdclk_config *cdclk_config)
875 876 877
{
	u32 cdctl;

878
	skl_dpll0_update(dev_priv, cdclk_config);
879

880
	cdclk_config->cdclk = cdclk_config->bypass = cdclk_config->ref;
881

882
	if (cdclk_config->vco == 0)
883
		goto out;
884

885
	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
886

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

 out:
	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
930 931
	cdclk_config->voltage_level =
		skl_calc_voltage_level(cdclk_config->cdclk);
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
}

/* 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.
	 */
966
	val = intel_de_read(dev_priv, DPLL_CTRL1);
967 968 969 970 971 972 973 974 975 976 977

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

978 979
	intel_de_write(dev_priv, DPLL_CTRL1, val);
	intel_de_posting_read(dev_priv, DPLL_CTRL1);
980

981 982
	intel_de_write(dev_priv, LCPLL1_CTL,
		       intel_de_read(dev_priv, LCPLL1_CTL) | LCPLL_PLL_ENABLE);
983

984
	if (intel_de_wait_for_set(dev_priv, LCPLL1_CTL, LCPLL_PLL_LOCK, 5))
985
		drm_err(&dev_priv->drm, "DPLL0 not locked\n");
986

987
	dev_priv->cdclk.hw.vco = vco;
988 989 990 991 992 993 994

	/* 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)
{
995 996
	intel_de_write(dev_priv, LCPLL1_CTL,
		       intel_de_read(dev_priv, LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
997
	if (intel_de_wait_for_clear(dev_priv, LCPLL1_CTL, LCPLL_PLL_LOCK, 1))
998
		drm_err(&dev_priv->drm, "Couldn't disable DPLL0\n");
999

1000
	dev_priv->cdclk.hw.vco = 0;
1001 1002 1003
}

static void skl_set_cdclk(struct drm_i915_private *dev_priv,
1004
			  const struct intel_cdclk_config *cdclk_config,
1005
			  enum pipe pipe)
1006
{
1007 1008
	int cdclk = cdclk_config->cdclk;
	int vco = cdclk_config->vco;
1009
	u32 freq_select, cdclk_ctl;
1010 1011
	int ret;

1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	/*
	 * 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);

1022 1023 1024 1025 1026
	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) {
1027 1028
		drm_err(&dev_priv->drm,
			"Failed to inform PCU about cdclk change (%d)\n", ret);
1029 1030 1031
		return;
	}

1032
	/* Choose frequency for this cdclk */
1033
	switch (cdclk) {
1034
	default:
1035
		WARN_ON(cdclk != dev_priv->cdclk.hw.bypass);
1036 1037 1038 1039 1040 1041
		WARN_ON(vco != 0);
		/* fall through */
	case 308571:
	case 337500:
		freq_select = CDCLK_FREQ_337_308;
		break;
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	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;
	}

1055 1056
	if (dev_priv->cdclk.hw.vco != 0 &&
	    dev_priv->cdclk.hw.vco != vco)
1057 1058
		skl_dpll0_disable(dev_priv);

1059
	cdclk_ctl = intel_de_read(dev_priv, CDCLK_CTL);
1060 1061 1062 1063 1064

	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);
1065
		intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1066 1067 1068 1069
	}

	/* Wa Display #1183: skl,kbl,cfl */
	cdclk_ctl |= CDCLK_DIVMUX_CD_OVERRIDE;
1070 1071
	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
	intel_de_posting_read(dev_priv, CDCLK_CTL);
1072

1073
	if (dev_priv->cdclk.hw.vco != vco)
1074 1075
		skl_dpll0_enable(dev_priv, vco);

1076 1077
	/* Wa Display #1183: skl,kbl,cfl */
	cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
1078
	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1079 1080

	cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
1081
	intel_de_write(dev_priv, CDCLK_CTL, cdclk_ctl);
1082 1083 1084

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

	/* inform PCU of the change */
1089
	sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
1090
				cdclk_config->voltage_level);
1091 1092 1093 1094 1095 1096

	intel_update_cdclk(dev_priv);
}

static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
{
1097
	u32 cdctl, expected;
1098 1099 1100 1101 1102 1103

	/*
	 * 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
	 */
1104
	if ((intel_de_read(dev_priv, SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
1105 1106 1107
		goto sanitize;

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

1110
	/* Is PLL enabled and locked ? */
1111
	if (dev_priv->cdclk.hw.vco == 0 ||
1112
	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
1113 1114 1115 1116 1117 1118 1119 1120
		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.
	 */
1121
	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
1122
	expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
1123
		skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);
1124 1125 1126 1127 1128
	if (cdctl == expected)
		/* All well; nothing to sanitize */
		return;

sanitize:
1129
	drm_dbg_kms(&dev_priv->drm, "Sanitizing cdclk programmed by pre-os\n");
1130 1131

	/* force cdclk programming */
1132
	dev_priv->cdclk.hw.cdclk = 0;
1133
	/* force full PLL disable + enable */
1134
	dev_priv->cdclk.hw.vco = -1;
1135 1136
}

1137
static void skl_init_cdclk(struct drm_i915_private *dev_priv)
1138
{
1139
	struct intel_cdclk_config cdclk_config;
1140 1141 1142

	skl_sanitize_cdclk(dev_priv);

1143 1144
	if (dev_priv->cdclk.hw.cdclk != 0 &&
	    dev_priv->cdclk.hw.vco != 0) {
1145 1146 1147 1148 1149 1150
		/*
		 * 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,
1151
						    dev_priv->cdclk.hw.vco);
1152 1153 1154
		return;
	}

1155
	cdclk_config = dev_priv->cdclk.hw;
1156

1157 1158 1159 1160 1161
	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);
1162

1163
	skl_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1164 1165
}

1166
static void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
1167
{
1168
	struct intel_cdclk_config cdclk_config = dev_priv->cdclk.hw;
1169

1170 1171 1172
	cdclk_config.cdclk = cdclk_config.bypass;
	cdclk_config.vco = 0;
	cdclk_config.voltage_level = skl_calc_voltage_level(cdclk_config.cdclk);
1173

1174
	skl_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1175 1176
}

1177 1178 1179 1180 1181 1182 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
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;

	WARN(1, "Cannot satisfy minimum cdclk %d with refclk %u\n",
	     min_cdclk, dev_priv->cdclk.hw.ref);
	return 0;
1241 1242
}

1243
static int bxt_calc_cdclk_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
1244
{
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
	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;

	WARN(1, "cdclk %d not valid for refclk %u\n",
	     cdclk, dev_priv->cdclk.hw.ref);
	return 0;
1259 1260
}

1261 1262 1263 1264 1265
static u8 bxt_calc_voltage_level(int cdclk)
{
	return DIV_ROUND_UP(cdclk, 25000);
}

1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287
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)
{
1288 1289 1290
	if (cdclk > 326400)
		return 3;
	else if (cdclk > 312000)
1291 1292 1293 1294 1295 1296 1297 1298
		return 2;
	else if (cdclk > 180000)
		return 1;
	else
		return 0;
}

static void cnl_readout_refclk(struct drm_i915_private *dev_priv,
1299
			       struct intel_cdclk_config *cdclk_config)
1300
{
1301
	if (intel_de_read(dev_priv, SKL_DSSM) & CNL_DSSM_CDCLK_PLL_REFCLK_24MHz)
1302
		cdclk_config->ref = 24000;
1303
	else
1304
		cdclk_config->ref = 19200;
1305
}
1306

1307
static void icl_readout_refclk(struct drm_i915_private *dev_priv,
1308
			       struct intel_cdclk_config *cdclk_config)
1309
{
1310
	u32 dssm = intel_de_read(dev_priv, SKL_DSSM) & ICL_DSSM_CDCLK_PLL_REFCLK_MASK;
1311 1312 1313 1314 1315 1316

	switch (dssm) {
	default:
		MISSING_CASE(dssm);
		/* fall through */
	case ICL_DSSM_CDCLK_PLL_REFCLK_24MHz:
1317
		cdclk_config->ref = 24000;
1318 1319
		break;
	case ICL_DSSM_CDCLK_PLL_REFCLK_19_2MHz:
1320
		cdclk_config->ref = 19200;
1321 1322
		break;
	case ICL_DSSM_CDCLK_PLL_REFCLK_38_4MHz:
1323
		cdclk_config->ref = 38400;
1324 1325 1326 1327 1328
		break;
	}
}

static void bxt_de_pll_readout(struct drm_i915_private *dev_priv,
1329
			       struct intel_cdclk_config *cdclk_config)
1330 1331 1332 1333
{
	u32 val, ratio;

	if (INTEL_GEN(dev_priv) >= 11)
1334
		icl_readout_refclk(dev_priv, cdclk_config);
1335
	else if (IS_CANNONLAKE(dev_priv))
1336
		cnl_readout_refclk(dev_priv, cdclk_config);
1337
	else
1338
		cdclk_config->ref = 19200;
1339

1340
	val = intel_de_read(dev_priv, BXT_DE_PLL_ENABLE);
1341 1342 1343 1344 1345 1346
	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.
		 */
1347
		cdclk_config->vco = 0;
1348
		return;
1349
	}
1350

1351 1352 1353 1354 1355 1356 1357
	/*
	 * 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
1358
		ratio = intel_de_read(dev_priv, BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
1359

1360
	cdclk_config->vco = ratio * cdclk_config->ref;
1361 1362
}

1363
static void bxt_get_cdclk(struct drm_i915_private *dev_priv,
1364
			  struct intel_cdclk_config *cdclk_config)
1365 1366
{
	u32 divider;
1367
	int div;
1368

1369
	bxt_de_pll_readout(dev_priv, cdclk_config);
1370

1371
	if (INTEL_GEN(dev_priv) >= 12)
1372
		cdclk_config->bypass = cdclk_config->ref / 2;
1373
	else if (INTEL_GEN(dev_priv) >= 11)
1374
		cdclk_config->bypass = 50000;
1375
	else
1376
		cdclk_config->bypass = cdclk_config->ref;
1377

1378 1379
	if (cdclk_config->vco == 0) {
		cdclk_config->cdclk = cdclk_config->bypass;
1380
		goto out;
1381
	}
1382

1383
	divider = intel_de_read(dev_priv, CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
1384 1385 1386 1387 1388 1389

	switch (divider) {
	case BXT_CDCLK_CD2X_DIV_SEL_1:
		div = 2;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_1_5:
1390 1391
		WARN(IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10,
		     "Unsupported divider\n");
1392 1393 1394 1395 1396 1397
		div = 3;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_2:
		div = 4;
		break;
	case BXT_CDCLK_CD2X_DIV_SEL_4:
1398
		WARN(INTEL_GEN(dev_priv) >= 10, "Unsupported divider\n");
1399 1400 1401 1402
		div = 8;
		break;
	default:
		MISSING_CASE(divider);
1403
		return;
1404 1405
	}

1406
	cdclk_config->cdclk = DIV_ROUND_CLOSEST(cdclk_config->vco, div);
1407 1408 1409 1410 1411 1412

 out:
	/*
	 * Can't read this out :( Let's assume it's
	 * at least what the CDCLK frequency requires.
	 */
1413 1414
	cdclk_config->voltage_level =
		dev_priv->display.calc_voltage_level(cdclk_config->cdclk);
1415 1416 1417 1418
}

static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
{
1419
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, 0);
1420 1421

	/* Timeout 200us */
1422 1423
	if (intel_de_wait_for_clear(dev_priv,
				    BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1424
		drm_err(&dev_priv->drm, "timeout waiting for DE PLL unlock\n");
1425

1426
	dev_priv->cdclk.hw.vco = 0;
1427 1428 1429 1430
}

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

1434
	val = intel_de_read(dev_priv, BXT_DE_PLL_CTL);
1435 1436
	val &= ~BXT_DE_PLL_RATIO_MASK;
	val |= BXT_DE_PLL_RATIO(ratio);
1437
	intel_de_write(dev_priv, BXT_DE_PLL_CTL, val);
1438

1439
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
1440 1441

	/* Timeout 200us */
1442 1443
	if (intel_de_wait_for_set(dev_priv,
				  BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
1444
		drm_err(&dev_priv->drm, "timeout waiting for DE PLL lock\n");
1445

1446
	dev_priv->cdclk.hw.vco = vco;
1447 1448
}

1449 1450 1451 1452
static void cnl_cdclk_pll_disable(struct drm_i915_private *dev_priv)
{
	u32 val;

1453
	val = intel_de_read(dev_priv, BXT_DE_PLL_ENABLE);
1454
	val &= ~BXT_DE_PLL_PLL_ENABLE;
1455
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1456 1457

	/* Timeout 200us */
1458
	if (wait_for((intel_de_read(dev_priv, BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
1459 1460
		drm_err(&dev_priv->drm,
			"timeout waiting for CDCLK PLL unlock\n");
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470

	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);
1471
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1472 1473

	val |= BXT_DE_PLL_PLL_ENABLE;
1474
	intel_de_write(dev_priv, BXT_DE_PLL_ENABLE, val);
1475 1476

	/* Timeout 200us */
1477
	if (wait_for((intel_de_read(dev_priv, BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
1478 1479
		drm_err(&dev_priv->drm,
			"timeout waiting for CDCLK PLL lock\n");
1480 1481 1482 1483

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

1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503
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);
	}
}

1504
static void bxt_set_cdclk(struct drm_i915_private *dev_priv,
1505
			  const struct intel_cdclk_config *cdclk_config,
1506
			  enum pipe pipe)
1507
{
1508 1509
	int cdclk = cdclk_config->cdclk;
	int vco = cdclk_config->vco;
1510
	u32 val, divider;
1511
	int ret;
1512

1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
	/* 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) {
1529 1530 1531
		drm_err(&dev_priv->drm,
			"Failed to inform PCU about cdclk change (err %d, freq %d)\n",
			ret, cdclk);
1532 1533 1534
		return;
	}

1535 1536
	/* cdclk = vco / 2 / div{1,1.5,2,4} */
	switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
1537
	default:
1538
		WARN_ON(cdclk != dev_priv->cdclk.hw.bypass);
1539 1540 1541 1542
		WARN_ON(vco != 0);
		/* fall through */
	case 2:
		divider = BXT_CDCLK_CD2X_DIV_SEL_1;
1543 1544
		break;
	case 3:
1545 1546
		WARN(IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10,
		     "Unsupported divider\n");
1547 1548
		divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
		break;
1549 1550
	case 4:
		divider = BXT_CDCLK_CD2X_DIV_SEL_2;
1551
		break;
1552
	case 8:
1553
		WARN(INTEL_GEN(dev_priv) >= 10, "Unsupported divider\n");
1554
		divider = BXT_CDCLK_CD2X_DIV_SEL_4;
1555 1556 1557
		break;
	}

1558 1559 1560 1561
	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);
1562

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

1566 1567 1568 1569 1570 1571 1572 1573
	} 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);
	}
1574

1575 1576
	val = divider | skl_cdclk_decimal(cdclk) |
		bxt_cdclk_cd2x_pipe(dev_priv, pipe);
1577

1578 1579 1580 1581
	/*
	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
	 * enable otherwise.
	 */
1582
	if (IS_GEN9_LP(dev_priv) && cdclk >= 500000)
1583
		val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
1584
	intel_de_write(dev_priv, CDCLK_CTL, val);
1585

1586 1587 1588
	if (pipe != INVALID_PIPE)
		intel_wait_for_vblank(dev_priv, pipe);

1589 1590
	if (INTEL_GEN(dev_priv) >= 10) {
		ret = sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
1591
					      cdclk_config->voltage_level);
1592 1593 1594 1595 1596 1597 1598 1599 1600
	} 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,
1601
						      cdclk_config->voltage_level,
1602 1603 1604
						      150, 2);
	}

1605
	if (ret) {
1606 1607 1608
		drm_err(&dev_priv->drm,
			"PCode CDCLK freq set failed, (err %d, freq %d)\n",
			ret, cdclk);
1609 1610 1611 1612
		return;
	}

	intel_update_cdclk(dev_priv);
1613 1614 1615 1616 1617 1618

	if (INTEL_GEN(dev_priv) >= 10)
		/*
		 * Can't read out the voltage level :(
		 * Let's just assume everything is as expected.
		 */
1619
		dev_priv->cdclk.hw.voltage_level = cdclk_config->voltage_level;
1620 1621 1622 1623 1624
}

static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
{
	u32 cdctl, expected;
1625
	int cdclk, vco;
1626 1627

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

1630
	if (dev_priv->cdclk.hw.vco == 0 ||
1631
	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
1632 1633 1634 1635 1636 1637 1638 1639
		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.
	 */
1640
	cdctl = intel_de_read(dev_priv, CDCLK_CTL);
1641 1642 1643 1644 1645
	/*
	 * Let's ignore the pipe field, since BIOS could have configured the
	 * dividers both synching to an active pipe, or asynchronously
	 * (PIPE_NONE).
	 */
1646
	cdctl &= ~bxt_cdclk_cd2x_pipe(dev_priv, INVALID_PIPE);
1647

1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678
	/* 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;
	}

1679 1680 1681 1682
	/*
	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
	 * enable otherwise.
	 */
M
Matt Roper 已提交
1683
	if (IS_GEN9_LP(dev_priv) && dev_priv->cdclk.hw.cdclk >= 500000)
1684 1685 1686 1687 1688 1689 1690
		expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;

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

sanitize:
1691
	drm_dbg_kms(&dev_priv->drm, "Sanitizing cdclk programmed by pre-os\n");
1692 1693

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

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

1700
static void bxt_init_cdclk(struct drm_i915_private *dev_priv)
1701
{
1702
	struct intel_cdclk_config cdclk_config;
1703 1704 1705

	bxt_sanitize_cdclk(dev_priv);

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

1710
	cdclk_config = dev_priv->cdclk.hw;
1711

1712 1713 1714 1715 1716
	/*
	 * FIXME:
	 * - The initial CDCLK needs to be read from VBT.
	 *   Need to make this change after VBT has changes for BXT.
	 */
1717 1718 1719 1720
	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);
1721

1722
	bxt_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1723 1724
}

1725
static void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
1726
{
1727
	struct intel_cdclk_config cdclk_config = dev_priv->cdclk.hw;
1728

1729 1730 1731 1732
	cdclk_config.cdclk = cdclk_config.bypass;
	cdclk_config.vco = 0;
	cdclk_config.voltage_level =
		dev_priv->display.calc_voltage_level(cdclk_config.cdclk);
1733

1734
	bxt_set_cdclk(dev_priv, &cdclk_config, INVALID_PIPE);
1735 1736
}

1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
/**
 * 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)
{
1748 1749
	if (IS_GEN9_LP(i915) || INTEL_GEN(i915) >= 10)
		bxt_init_cdclk(i915);
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
	else if (IS_GEN9_BC(i915))
		skl_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)
{
1763 1764
	if (INTEL_GEN(i915) >= 10 || IS_GEN9_LP(i915))
		bxt_uninit_cdclk(i915);
1765 1766 1767 1768
	else if (IS_GEN9_BC(i915))
		skl_uninit_cdclk(i915);
}

1769
/**
1770 1771 1772 1773
 * 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
1774 1775
 *
 * Returns:
1776 1777
 * True if changing between the two CDCLK configurations
 * requires all pipes to be off, false if not.
1778
 */
1779 1780
bool intel_cdclk_needs_modeset(const struct intel_cdclk_config *a,
			       const struct intel_cdclk_config *b)
1781
{
1782 1783 1784 1785 1786
	return a->cdclk != b->cdclk ||
		a->vco != b->vco ||
		a->ref != b->ref;
}

1787
/**
1788 1789 1790 1791 1792
 * 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
1793 1794
 *
 * Returns:
1795 1796
 * True if changing between the two CDCLK configurations
 * can be done with just a cd2x divider update, false if not.
1797
 */
1798
static bool intel_cdclk_can_cd2x_update(struct drm_i915_private *dev_priv,
1799 1800
					const struct intel_cdclk_config *a,
					const struct intel_cdclk_config *b)
1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
{
	/* 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;
}

1811
/**
1812 1813 1814
 * intel_cdclk_changed - Determine if two CDCLK configurations are different
 * @a: first CDCLK configuration
 * @b: second CDCLK configuration
1815 1816
 *
 * Returns:
1817
 * True if the CDCLK configurations don't match, false if they do.
1818
 */
1819 1820
static bool intel_cdclk_changed(const struct intel_cdclk_config *a,
				const struct intel_cdclk_config *b)
1821 1822 1823
{
	return intel_cdclk_needs_modeset(a, b) ||
		a->voltage_level != b->voltage_level;
1824 1825
}

1826 1827 1828 1829 1830 1831 1832 1833
/**
 * intel_cdclk_clear_state - clear the cdclk state
 * @state: atomic state
 *
 * Clear the cdclk state for ww_mutex backoff.
 */
void intel_cdclk_clear_state(struct intel_atomic_state *state)
{
1834 1835
	memset(&state->cdclk_state, 0, sizeof(state->cdclk_state));
	state->cdclk_state.pipe = INVALID_PIPE;
1836 1837
}

1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
/**
 * 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);

1854
	/* FIXME maybe swap() these too */
1855 1856 1857 1858 1859 1860
	memcpy(dev_priv->cdclk_state.min_cdclk,
	       state->cdclk_state.min_cdclk,
	       sizeof(state->cdclk_state.min_cdclk));
	memcpy(dev_priv->cdclk_state.min_voltage_level,
	       state->cdclk_state.min_voltage_level,
	       sizeof(state->cdclk_state.min_voltage_level));
1861

1862 1863
	dev_priv->cdclk_state.force_min_cdclk =
		state->cdclk_state.force_min_cdclk;
1864

1865 1866
	swap(state->cdclk_state.logical, dev_priv->cdclk_state.logical);
	swap(state->cdclk_state.actual, dev_priv->cdclk_state.actual);
1867 1868
}

1869 1870
void intel_dump_cdclk_config(const struct intel_cdclk_config *cdclk_config,
			     const char *context)
1871
{
1872
	DRM_DEBUG_DRIVER("%s %d kHz, VCO %d kHz, ref %d kHz, bypass %d kHz, voltage level %d\n",
1873 1874 1875
			 context, cdclk_config->cdclk, cdclk_config->vco,
			 cdclk_config->ref, cdclk_config->bypass,
			 cdclk_config->voltage_level);
1876 1877
}

1878
/**
1879
 * intel_set_cdclk - Push the CDCLK configuration to the hardware
1880
 * @dev_priv: i915 device
1881
 * @cdclk_config: new CDCLK configuration
1882
 * @pipe: pipe with which to synchronize the update
1883 1884 1885 1886
 *
 * Program the hardware based on the passed in CDCLK state,
 * if necessary.
 */
1887
static void intel_set_cdclk(struct drm_i915_private *dev_priv,
1888
			    const struct intel_cdclk_config *cdclk_config,
1889
			    enum pipe pipe)
1890
{
1891
	if (!intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_config))
1892 1893 1894 1895 1896
		return;

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

1897
	intel_dump_cdclk_config(cdclk_config, "Changing CDCLK to");
1898

1899
	dev_priv->display.set_cdclk(dev_priv, cdclk_config, pipe);
1900

1901
	if (WARN(intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_config),
1902
		 "cdclk state doesn't match!\n")) {
1903 1904
		intel_dump_cdclk_config(&dev_priv->cdclk.hw, "[hw state]");
		intel_dump_cdclk_config(cdclk_config, "[sw state]");
1905
	}
1906 1907
}

1908
/**
1909 1910
 * intel_set_cdclk_pre_plane_update - Push the CDCLK state to the hardware
 * @state: intel atomic state
1911
 *
1912 1913
 * Program the hardware before updating the HW plane state based on the
 * new CDCLK state, if necessary.
1914 1915
 */
void
1916
intel_set_cdclk_pre_plane_update(struct intel_atomic_state *state)
1917
{
1918 1919
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
	/* called after intel_cdclk_swap_state()! */
1920 1921 1922
	const struct intel_cdclk_state *old_cdclk_state = &state->cdclk_state;
	const struct intel_cdclk_state *new_cdclk_state = &dev_priv->cdclk_state;
	enum pipe pipe = old_cdclk_state->pipe; /* not swapped */
1923

1924 1925 1926
	if (pipe == INVALID_PIPE ||
	    old_cdclk_state->actual.cdclk <= new_cdclk_state->actual.cdclk)
		intel_set_cdclk(dev_priv, &new_cdclk_state->actual, pipe);
1927 1928 1929
}

/**
1930 1931
 * intel_set_cdclk_post_plane_update - Push the CDCLK state to the hardware
 * @state: intel atomic state
1932
 *
1933 1934
 * Program the hardware before updating the HW plane state based on the
 * new CDCLK state, if necessary.
1935 1936
 */
void
1937
intel_set_cdclk_post_plane_update(struct intel_atomic_state *state)
1938
{
1939 1940
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
	/* called after intel_cdclk_swap_state()! */
1941 1942 1943
	const struct intel_cdclk_state *old_cdclk_state = &state->cdclk_state;
	const struct intel_cdclk_state *new_cdclk_state = &dev_priv->cdclk_state;
	enum pipe pipe = old_cdclk_state->pipe; /* not swapped */
1944

1945 1946 1947
	if (pipe != INVALID_PIPE &&
	    old_cdclk_state->actual.cdclk > new_cdclk_state->actual.cdclk)
		intel_set_cdclk(dev_priv, &new_cdclk_state->actual, pipe);
1948 1949
}

1950
static int intel_pixel_rate_to_cdclk(const struct intel_crtc_state *crtc_state)
1951
{
1952
	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1953 1954
	int pixel_rate = crtc_state->pixel_rate;

1955
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
1956
		return DIV_ROUND_UP(pixel_rate, 2);
1957
	else if (IS_GEN(dev_priv, 9) ||
1958 1959 1960 1961
		 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);
1962 1963
	else if (crtc_state->double_wide)
		return DIV_ROUND_UP(pixel_rate * 100, 90 * 2);
1964 1965 1966 1967
	else
		return DIV_ROUND_UP(pixel_rate * 100, 90);
}

1968 1969
static int intel_planes_min_cdclk(const struct intel_crtc_state *crtc_state)
{
1970
	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980
	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;
}

1981
int intel_crtc_compute_min_cdclk(const struct intel_crtc_state *crtc_state)
1982 1983
{
	struct drm_i915_private *dev_priv =
1984
		to_i915(crtc_state->uapi.crtc->dev);
1985 1986
	int min_cdclk;

1987
	if (!crtc_state->hw.enable)
1988 1989
		return 0;

1990
	min_cdclk = intel_pixel_rate_to_cdclk(crtc_state);
1991 1992

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

1996 1997 1998
	/* 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
1999
	 * restriction for GLK is 316.8 MHz.
2000 2001 2002 2003
	 */
	if (intel_crtc_has_dp_encoder(crtc_state) &&
	    crtc_state->has_audio &&
	    crtc_state->port_clock >= 540000 &&
2004
	    crtc_state->lane_count == 4) {
2005 2006 2007
		if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv)) {
			/* Display WA #1145: glk,cnl */
			min_cdclk = max(316800, min_cdclk);
2008
		} else if (IS_GEN(dev_priv, 9) || IS_BROADWELL(dev_priv)) {
2009 2010 2011
			/* Display WA #1144: skl,bxt */
			min_cdclk = max(432000, min_cdclk);
		}
2012
	}
2013

2014 2015
	/*
	 * According to BSpec, "The CD clock frequency must be at least twice
2016 2017
	 * the frequency of the Azalia BCLK." and BCLK is 96 MHz by default.
	 */
2018
	if (crtc_state->has_audio && INTEL_GEN(dev_priv) >= 9)
2019
		min_cdclk = max(2 * 96000, min_cdclk);
2020

2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
	/*
	 * "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);

2032 2033 2034 2035 2036 2037 2038 2039
	/*
	 * 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);

2040 2041 2042 2043 2044 2045 2046 2047 2048
	/*
	 * 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);

2049 2050 2051
	/* Account for additional needs from the planes */
	min_cdclk = max(intel_planes_min_cdclk(crtc_state), min_cdclk);

2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063
	/*
	 * 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);

2064
	if (min_cdclk > dev_priv->max_cdclk_freq) {
2065 2066 2067
		drm_dbg_kms(&dev_priv->drm,
			    "required cdclk (%d kHz) exceeds max (%d kHz)\n",
			    min_cdclk, dev_priv->max_cdclk_freq);
2068 2069 2070
		return -EINVAL;
	}

2071
	return min_cdclk;
2072 2073
}

2074
static int intel_compute_min_cdclk(struct intel_atomic_state *state)
2075
{
2076
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2077
	struct intel_cdclk_state *cdclk_state = &state->cdclk_state;
2078
	struct intel_crtc *crtc;
2079
	struct intel_crtc_state *crtc_state;
2080
	int min_cdclk, i;
2081 2082
	enum pipe pipe;

2083
	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2084 2085
		int ret;

2086 2087 2088 2089
		min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
		if (min_cdclk < 0)
			return min_cdclk;

2090
		if (cdclk_state->min_cdclk[i] == min_cdclk)
2091 2092
			continue;

2093
		cdclk_state->min_cdclk[i] = min_cdclk;
2094 2095 2096 2097

		ret = intel_atomic_lock_global_state(state);
		if (ret)
			return ret;
2098
	}
2099

2100
	min_cdclk = cdclk_state->force_min_cdclk;
2101
	for_each_pipe(dev_priv, pipe)
2102
		min_cdclk = max(cdclk_state->min_cdclk[pipe], min_cdclk);
2103

2104
	return min_cdclk;
2105 2106
}

2107
/*
2108 2109 2110 2111
 * Account for port clock min voltage level requirements.
 * This only really does something on CNL+ but can be
 * called on earlier platforms as well.
 *
2112 2113 2114 2115 2116 2117 2118 2119
 * 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.
 */
2120
static int bxt_compute_min_voltage_level(struct intel_atomic_state *state)
2121 2122
{
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2123
	struct intel_cdclk_state *cdclk_state = &state->cdclk_state;
2124 2125 2126 2127 2128 2129 2130
	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) {
2131 2132
		int ret;

2133
		if (crtc_state->hw.enable)
2134
			min_voltage_level = crtc_state->min_voltage_level;
2135
		else
2136 2137
			min_voltage_level = 0;

2138
		if (cdclk_state->min_voltage_level[i] == min_voltage_level)
2139 2140
			continue;

2141
		cdclk_state->min_voltage_level[i] = min_voltage_level;
2142 2143 2144 2145

		ret = intel_atomic_lock_global_state(state);
		if (ret)
			return ret;
2146 2147 2148 2149
	}

	min_voltage_level = 0;
	for_each_pipe(dev_priv, pipe)
2150
		min_voltage_level = max(cdclk_state->min_voltage_level[pipe],
2151 2152 2153 2154 2155
					min_voltage_level);

	return min_voltage_level;
}

2156
static int vlv_modeset_calc_cdclk(struct intel_atomic_state *state)
2157
{
2158
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2159
	struct intel_cdclk_state *cdclk_state = &state->cdclk_state;
2160
	int min_cdclk, cdclk;
2161

2162 2163 2164
	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;
2165

2166
	cdclk = vlv_calc_cdclk(dev_priv, min_cdclk);
2167

2168 2169
	cdclk_state->logical.cdclk = cdclk;
	cdclk_state->logical.voltage_level =
2170
		vlv_calc_voltage_level(dev_priv, cdclk);
2171

2172
	if (!state->active_pipes) {
2173
		cdclk = vlv_calc_cdclk(dev_priv, cdclk_state->force_min_cdclk);
2174

2175 2176
		cdclk_state->actual.cdclk = cdclk;
		cdclk_state->actual.voltage_level =
2177
			vlv_calc_voltage_level(dev_priv, cdclk);
2178
	} else {
2179
		cdclk_state->actual = cdclk_state->logical;
2180
	}
2181 2182 2183 2184

	return 0;
}

2185
static int bdw_modeset_calc_cdclk(struct intel_atomic_state *state)
2186
{
2187
	struct intel_cdclk_state *cdclk_state = &state->cdclk_state;
2188 2189 2190 2191 2192
	int min_cdclk, cdclk;

	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;
2193 2194 2195 2196 2197

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

2200 2201
	cdclk_state->logical.cdclk = cdclk;
	cdclk_state->logical.voltage_level =
2202
		bdw_calc_voltage_level(cdclk);
2203

2204
	if (!state->active_pipes) {
2205
		cdclk = bdw_calc_cdclk(cdclk_state->force_min_cdclk);
2206

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

	return 0;
}

2217
static int skl_dpll0_vco(struct intel_atomic_state *state)
2218
{
2219
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2220
	struct intel_cdclk_state *cdclk_state = &state->cdclk_state;
2221 2222 2223 2224
	struct intel_crtc *crtc;
	struct intel_crtc_state *crtc_state;
	int vco, i;

2225
	vco = cdclk_state->logical.vco;
2226 2227 2228
	if (!vco)
		vco = dev_priv->skl_preferred_vco_freq;

2229
	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
2230
		if (!crtc_state->hw.enable)
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
			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;
}

2254
static int skl_modeset_calc_cdclk(struct intel_atomic_state *state)
2255
{
2256
	struct intel_cdclk_state *cdclk_state = &state->cdclk_state;
2257 2258 2259 2260 2261
	int min_cdclk, cdclk, vco;

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

2263
	vco = skl_dpll0_vco(state);
2264 2265 2266 2267 2268

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

2271 2272 2273
	cdclk_state->logical.vco = vco;
	cdclk_state->logical.cdclk = cdclk;
	cdclk_state->logical.voltage_level =
2274
		skl_calc_voltage_level(cdclk);
2275

2276
	if (!state->active_pipes) {
2277
		cdclk = skl_calc_cdclk(cdclk_state->force_min_cdclk, vco);
2278

2279 2280 2281
		cdclk_state->actual.vco = vco;
		cdclk_state->actual.cdclk = cdclk;
		cdclk_state->actual.voltage_level =
2282
			skl_calc_voltage_level(cdclk);
2283
	} else {
2284
		cdclk_state->actual = cdclk_state->logical;
2285
	}
2286 2287 2288 2289

	return 0;
}

2290
static int bxt_modeset_calc_cdclk(struct intel_atomic_state *state)
2291
{
2292
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2293
	struct intel_cdclk_state *cdclk_state = &state->cdclk_state;
2294
	int min_cdclk, min_voltage_level, cdclk, vco;
2295 2296 2297 2298

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

2300 2301 2302 2303
	min_voltage_level = bxt_compute_min_voltage_level(state);
	if (min_voltage_level < 0)
		return min_voltage_level;

2304 2305
	cdclk = bxt_calc_cdclk(dev_priv, min_cdclk);
	vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
2306

2307 2308 2309
	cdclk_state->logical.vco = vco;
	cdclk_state->logical.cdclk = cdclk;
	cdclk_state->logical.voltage_level =
2310 2311
		max_t(int, min_voltage_level,
		      dev_priv->display.calc_voltage_level(cdclk));
2312

2313
	if (!state->active_pipes) {
2314
		cdclk = bxt_calc_cdclk(dev_priv, cdclk_state->force_min_cdclk);
2315
		vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
2316

2317 2318 2319
		cdclk_state->actual.vco = vco;
		cdclk_state->actual.cdclk = cdclk;
		cdclk_state->actual.voltage_level =
2320
			dev_priv->display.calc_voltage_level(cdclk);
2321
	} else {
2322
		cdclk_state->actual = cdclk_state->logical;
2323 2324 2325 2326 2327
	}

	return 0;
}

2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344
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);

2345
		if (!crtc_state->hw.active ||
2346
		    drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
2347 2348
			continue;

2349
		crtc_state->uapi.mode_changed = true;
2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366

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

2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382
static int fixed_modeset_calc_cdclk(struct intel_atomic_state *state)
{
	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.
	 */
	min_cdclk = intel_compute_min_cdclk(state);
	if (min_cdclk < 0)
		return min_cdclk;

	return 0;
}

2383 2384 2385
int intel_modeset_calc_cdclk(struct intel_atomic_state *state)
{
	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2386 2387
	const struct intel_cdclk_state *old_cdclk_state = &dev_priv->cdclk_state;
	struct intel_cdclk_state *new_cdclk_state = &state->cdclk_state;
2388 2389 2390
	enum pipe pipe;
	int ret;

2391 2392 2393 2394
	memcpy(new_cdclk_state->min_cdclk, old_cdclk_state->min_cdclk,
	       sizeof(new_cdclk_state->min_cdclk));
	memcpy(new_cdclk_state->min_voltage_level, old_cdclk_state->min_voltage_level,
	       sizeof(new_cdclk_state->min_voltage_level));
2395 2396

	/* keep the current setting */
2397 2398
	if (!new_cdclk_state->force_min_cdclk_changed)
		new_cdclk_state->force_min_cdclk = old_cdclk_state->force_min_cdclk;
2399

2400 2401
	new_cdclk_state->logical = old_cdclk_state->logical;
	new_cdclk_state->actual = old_cdclk_state->actual;
2402

2403 2404 2405 2406 2407
	ret = dev_priv->display.modeset_calc_cdclk(state);
	if (ret)
		return ret;

	/*
2408 2409
	 * Writes to dev_priv->cdclk.{actual,logical} must protected
	 * by holding all the crtc mutexes even if we don't end up
2410 2411
	 * touching the hardware
	 */
2412 2413
	if (intel_cdclk_changed(&old_cdclk_state->actual,
				&new_cdclk_state->actual)) {
2414 2415 2416 2417 2418 2419 2420
		/*
		 * Also serialize commits across all crtcs
		 * if the actual hw needs to be poked.
		 */
		ret = intel_atomic_serialize_global_state(state);
		if (ret)
			return ret;
2421 2422
	} else if (intel_cdclk_changed(&old_cdclk_state->logical,
				       &new_cdclk_state->logical)) {
2423 2424
		ret = intel_atomic_lock_global_state(state);
		if (ret)
2425
			return ret;
2426 2427
	} else {
		return 0;
2428 2429
	}

2430
	if (is_power_of_2(state->active_pipes) &&
2431
	    intel_cdclk_can_cd2x_update(dev_priv,
2432 2433
					&old_cdclk_state->actual,
					&new_cdclk_state->actual)) {
2434 2435 2436 2437 2438
		struct intel_crtc *crtc;
		struct intel_crtc_state *crtc_state;

		pipe = ilog2(state->active_pipes);
		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
2439 2440 2441 2442 2443

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

2444
		if (drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
2445 2446 2447 2448 2449
			pipe = INVALID_PIPE;
	} else {
		pipe = INVALID_PIPE;
	}

2450
	if (pipe != INVALID_PIPE) {
2451
		new_cdclk_state->pipe = pipe;
2452

2453 2454 2455
		drm_dbg_kms(&dev_priv->drm,
			    "Can change cdclk with pipe %c active\n",
			    pipe_name(pipe));
2456 2457
	} else if (intel_cdclk_needs_modeset(&old_cdclk_state->actual,
					     &new_cdclk_state->actual)) {
2458
		/* All pipes must be switched off while we change the cdclk. */
2459 2460 2461 2462
		ret = intel_modeset_all_pipes(state);
		if (ret)
			return ret;

2463
		new_cdclk_state->pipe = INVALID_PIPE;
2464

2465 2466
		drm_dbg_kms(&dev_priv->drm,
			    "Modeset required for cdclk change\n");
2467 2468
	}

2469 2470
	drm_dbg_kms(&dev_priv->drm,
		    "New cdclk calculated to be logical %u kHz, actual %u kHz\n",
2471 2472
		    new_cdclk_state->logical.cdclk,
		    new_cdclk_state->actual.cdclk);
2473 2474
	drm_dbg_kms(&dev_priv->drm,
		    "New voltage level calculated to be logical %u, actual %u\n",
2475 2476
		    new_cdclk_state->logical.voltage_level,
		    new_cdclk_state->actual.voltage_level);
2477 2478 2479 2480

	return 0;
}

2481 2482 2483 2484
static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
{
	int max_cdclk_freq = dev_priv->max_cdclk_freq;

2485
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
2486
		return 2 * max_cdclk_freq;
2487
	else if (IS_GEN(dev_priv, 9) ||
2488
		 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
2489 2490 2491
		return max_cdclk_freq;
	else if (IS_CHERRYVIEW(dev_priv))
		return max_cdclk_freq*95/100;
2492
	else if (INTEL_GEN(dev_priv) < 4)
2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507
		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)
{
2508 2509 2510 2511 2512 2513
	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) {
2514 2515 2516 2517 2518
		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)) {
2519 2520
		dev_priv->max_cdclk_freq = 528000;
	} else if (IS_GEN9_BC(dev_priv)) {
2521
		u32 limit = intel_de_read(dev_priv, SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552
		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?
		 */
2553
		if (intel_de_read(dev_priv, FUSE_STRAP) & HSW_CDCLK_LIMIT)
2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566
			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 */
2567
		dev_priv->max_cdclk_freq = dev_priv->cdclk.hw.cdclk;
2568 2569 2570 2571
	}

	dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);

2572 2573
	drm_dbg(&dev_priv->drm, "Max CD clock rate: %d kHz\n",
		dev_priv->max_cdclk_freq);
2574

2575 2576
	drm_dbg(&dev_priv->drm, "Max dotclock rate: %d kHz\n",
		dev_priv->max_dotclk_freq);
2577 2578 2579 2580 2581 2582 2583 2584 2585 2586
}

/**
 * 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)
{
2587
	dev_priv->display.get_cdclk(dev_priv, &dev_priv->cdclk.hw);
2588 2589 2590 2591 2592 2593 2594 2595

	/*
	 * 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))
2596 2597
		intel_de_write(dev_priv, GMBUSFREQ_VLV,
		               DIV_ROUND_UP(dev_priv->cdclk.hw.cdclk, 1000));
2598 2599
}

2600 2601 2602 2603 2604
static int cnp_rawclk(struct drm_i915_private *dev_priv)
{
	u32 rawclk;
	int divider, fraction;

2605
	if (intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_RAW_FREQUENCY) {
2606 2607 2608 2609 2610 2611 2612 2613 2614
		/* 24 MHz */
		divider = 24000;
		fraction = 0;
	} else {
		/* 19.2 MHz */
		divider = 19000;
		fraction = 200;
	}

2615
	rawclk = CNP_RAWCLK_DIV(divider / 1000);
2616 2617
	if (fraction) {
		int numerator = 1;
2618

2619 2620
		rawclk |= CNP_RAWCLK_DEN(DIV_ROUND_CLOSEST(numerator * 1000,
							   fraction) - 1);
2621
		if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
2622
			rawclk |= ICP_RAWCLK_NUM(numerator);
2623 2624
	}

2625
	intel_de_write(dev_priv, PCH_RAWCLK_FREQ, rawclk);
2626
	return divider + fraction;
2627 2628
}

2629 2630
static int pch_rawclk(struct drm_i915_private *dev_priv)
{
2631
	return (intel_de_read(dev_priv, PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642
}

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)
{
2643
	u32 clkcfg;
2644 2645

	/* hrawclock is 1/4 the FSB frequency */
2646
	clkcfg = intel_de_read(dev_priv, CLKCFG);
2647 2648 2649 2650 2651 2652 2653 2654 2655 2656
	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:
2657
	case CLKCFG_FSB_1067_ALT:
2658 2659
		return 266667;
	case CLKCFG_FSB_1333:
2660
	case CLKCFG_FSB_1333_ALT:
2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675
		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)
{
2676
	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
2677 2678
		dev_priv->rawclk_freq = cnp_rawclk(dev_priv);
	else if (HAS_PCH_SPLIT(dev_priv))
2679 2680 2681 2682 2683 2684 2685 2686 2687
		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;

2688 2689
	drm_dbg(&dev_priv->drm, "rawclk rate: %d kHz\n",
		dev_priv->rawclk_freq);
2690 2691 2692 2693 2694 2695 2696 2697
}

/**
 * intel_init_cdclk_hooks - Initialize CDCLK related modesetting hooks
 * @dev_priv: i915 device
 */
void intel_init_cdclk_hooks(struct drm_i915_private *dev_priv)
{
2698 2699
	if (IS_ELKHARTLAKE(dev_priv)) {
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2700
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2701 2702 2703
		dev_priv->display.calc_voltage_level = ehl_calc_voltage_level;
		dev_priv->cdclk.table = icl_cdclk_table;
	} else if (INTEL_GEN(dev_priv) >= 11) {
2704
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2705
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2706
		dev_priv->display.calc_voltage_level = icl_calc_voltage_level;
2707
		dev_priv->cdclk.table = icl_cdclk_table;
2708
	} else if (IS_CANNONLAKE(dev_priv)) {
2709
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2710
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2711
		dev_priv->display.calc_voltage_level = cnl_calc_voltage_level;
2712
		dev_priv->cdclk.table = cnl_cdclk_table;
2713
	} else if (IS_GEN9_LP(dev_priv)) {
2714
		dev_priv->display.set_cdclk = bxt_set_cdclk;
2715
		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
2716
		dev_priv->display.calc_voltage_level = bxt_calc_voltage_level;
2717 2718 2719 2720
		if (IS_GEMINILAKE(dev_priv))
			dev_priv->cdclk.table = glk_cdclk_table;
		else
			dev_priv->cdclk.table = bxt_cdclk_table;
2721
	} else if (IS_GEN9_BC(dev_priv)) {
2722
		dev_priv->display.set_cdclk = skl_set_cdclk;
2723
		dev_priv->display.modeset_calc_cdclk = skl_modeset_calc_cdclk;
2724 2725
	} else if (IS_BROADWELL(dev_priv)) {
		dev_priv->display.set_cdclk = bdw_set_cdclk;
2726
		dev_priv->display.modeset_calc_cdclk = bdw_modeset_calc_cdclk;
2727 2728
	} else if (IS_CHERRYVIEW(dev_priv)) {
		dev_priv->display.set_cdclk = chv_set_cdclk;
2729
		dev_priv->display.modeset_calc_cdclk = vlv_modeset_calc_cdclk;
2730 2731
	} else if (IS_VALLEYVIEW(dev_priv)) {
		dev_priv->display.set_cdclk = vlv_set_cdclk;
2732
		dev_priv->display.modeset_calc_cdclk = vlv_modeset_calc_cdclk;
2733 2734
	} else {
		dev_priv->display.modeset_calc_cdclk = fixed_modeset_calc_cdclk;
2735 2736
	}

2737
	if (INTEL_GEN(dev_priv) >= 10 || IS_GEN9_LP(dev_priv))
2738
		dev_priv->display.get_cdclk = bxt_get_cdclk;
2739 2740
	else if (IS_GEN9_BC(dev_priv))
		dev_priv->display.get_cdclk = skl_get_cdclk;
2741 2742 2743 2744 2745 2746
	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;
2747
	else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
2748
		dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
2749
	else if (IS_GEN(dev_priv, 5))
2750 2751 2752
		dev_priv->display.get_cdclk = fixed_450mhz_get_cdclk;
	else if (IS_GM45(dev_priv))
		dev_priv->display.get_cdclk = gm45_get_cdclk;
2753
	else if (IS_G45(dev_priv))
2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782
		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;
	}
}