Home | History | Annotate | Line # | Download | only in display
      1  1.1  riastrad /*	$NetBSD: intel_cdclk.c,v 1.2 2021/12/18 23:45:29 riastradh Exp $	*/
      2  1.1  riastrad 
      3  1.1  riastrad /*
      4  1.1  riastrad  * Copyright  2006-2017 Intel Corporation
      5  1.1  riastrad  *
      6  1.1  riastrad  * Permission is hereby granted, free of charge, to any person obtaining a
      7  1.1  riastrad  * copy of this software and associated documentation files (the "Software"),
      8  1.1  riastrad  * to deal in the Software without restriction, including without limitation
      9  1.1  riastrad  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
     10  1.1  riastrad  * and/or sell copies of the Software, and to permit persons to whom the
     11  1.1  riastrad  * Software is furnished to do so, subject to the following conditions:
     12  1.1  riastrad  *
     13  1.1  riastrad  * The above copyright notice and this permission notice (including the next
     14  1.1  riastrad  * paragraph) shall be included in all copies or substantial portions of the
     15  1.1  riastrad  * Software.
     16  1.1  riastrad  *
     17  1.1  riastrad  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     18  1.1  riastrad  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     19  1.1  riastrad  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     20  1.1  riastrad  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     21  1.1  riastrad  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     22  1.1  riastrad  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     23  1.1  riastrad  * DEALINGS IN THE SOFTWARE.
     24  1.1  riastrad  */
     25  1.1  riastrad 
     26  1.1  riastrad #include <sys/cdefs.h>
     27  1.1  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_cdclk.c,v 1.2 2021/12/18 23:45:29 riastradh Exp $");
     28  1.1  riastrad 
     29  1.1  riastrad #include "intel_atomic.h"
     30  1.1  riastrad #include "intel_cdclk.h"
     31  1.1  riastrad #include "intel_display_types.h"
     32  1.1  riastrad #include "intel_sideband.h"
     33  1.1  riastrad 
     34  1.1  riastrad /**
     35  1.1  riastrad  * DOC: CDCLK / RAWCLK
     36  1.1  riastrad  *
     37  1.1  riastrad  * The display engine uses several different clocks to do its work. There
     38  1.1  riastrad  * are two main clocks involved that aren't directly related to the actual
     39  1.1  riastrad  * pixel clock or any symbol/bit clock of the actual output port. These
     40  1.1  riastrad  * are the core display clock (CDCLK) and RAWCLK.
     41  1.1  riastrad  *
     42  1.1  riastrad  * CDCLK clocks most of the display pipe logic, and thus its frequency
     43  1.1  riastrad  * must be high enough to support the rate at which pixels are flowing
     44  1.1  riastrad  * through the pipes. Downscaling must also be accounted as that increases
     45  1.1  riastrad  * the effective pixel rate.
     46  1.1  riastrad  *
     47  1.1  riastrad  * On several platforms the CDCLK frequency can be changed dynamically
     48  1.1  riastrad  * to minimize power consumption for a given display configuration.
     49  1.1  riastrad  * Typically changes to the CDCLK frequency require all the display pipes
     50  1.1  riastrad  * to be shut down while the frequency is being changed.
     51  1.1  riastrad  *
     52  1.1  riastrad  * On SKL+ the DMC will toggle the CDCLK off/on during DC5/6 entry/exit.
     53  1.1  riastrad  * DMC will not change the active CDCLK frequency however, so that part
     54  1.1  riastrad  * will still be performed by the driver directly.
     55  1.1  riastrad  *
     56  1.1  riastrad  * RAWCLK is a fixed frequency clock, often used by various auxiliary
     57  1.1  riastrad  * blocks such as AUX CH or backlight PWM. Hence the only thing we
     58  1.1  riastrad  * really need to know about RAWCLK is its frequency so that various
     59  1.1  riastrad  * dividers can be programmed correctly.
     60  1.1  riastrad  */
     61  1.1  riastrad 
     62  1.1  riastrad static void fixed_133mhz_get_cdclk(struct drm_i915_private *dev_priv,
     63  1.1  riastrad 				   struct intel_cdclk_state *cdclk_state)
     64  1.1  riastrad {
     65  1.1  riastrad 	cdclk_state->cdclk = 133333;
     66  1.1  riastrad }
     67  1.1  riastrad 
     68  1.1  riastrad static void fixed_200mhz_get_cdclk(struct drm_i915_private *dev_priv,
     69  1.1  riastrad 				   struct intel_cdclk_state *cdclk_state)
     70  1.1  riastrad {
     71  1.1  riastrad 	cdclk_state->cdclk = 200000;
     72  1.1  riastrad }
     73  1.1  riastrad 
     74  1.1  riastrad static void fixed_266mhz_get_cdclk(struct drm_i915_private *dev_priv,
     75  1.1  riastrad 				   struct intel_cdclk_state *cdclk_state)
     76  1.1  riastrad {
     77  1.1  riastrad 	cdclk_state->cdclk = 266667;
     78  1.1  riastrad }
     79  1.1  riastrad 
     80  1.1  riastrad static void fixed_333mhz_get_cdclk(struct drm_i915_private *dev_priv,
     81  1.1  riastrad 				   struct intel_cdclk_state *cdclk_state)
     82  1.1  riastrad {
     83  1.1  riastrad 	cdclk_state->cdclk = 333333;
     84  1.1  riastrad }
     85  1.1  riastrad 
     86  1.1  riastrad static void fixed_400mhz_get_cdclk(struct drm_i915_private *dev_priv,
     87  1.1  riastrad 				   struct intel_cdclk_state *cdclk_state)
     88  1.1  riastrad {
     89  1.1  riastrad 	cdclk_state->cdclk = 400000;
     90  1.1  riastrad }
     91  1.1  riastrad 
     92  1.1  riastrad static void fixed_450mhz_get_cdclk(struct drm_i915_private *dev_priv,
     93  1.1  riastrad 				   struct intel_cdclk_state *cdclk_state)
     94  1.1  riastrad {
     95  1.1  riastrad 	cdclk_state->cdclk = 450000;
     96  1.1  riastrad }
     97  1.1  riastrad 
     98  1.1  riastrad static void i85x_get_cdclk(struct drm_i915_private *dev_priv,
     99  1.1  riastrad 			   struct intel_cdclk_state *cdclk_state)
    100  1.1  riastrad {
    101  1.1  riastrad 	struct pci_dev *pdev = dev_priv->drm.pdev;
    102  1.1  riastrad 	u16 hpllcc = 0;
    103  1.1  riastrad 
    104  1.1  riastrad 	/*
    105  1.1  riastrad 	 * 852GM/852GMV only supports 133 MHz and the HPLLCC
    106  1.1  riastrad 	 * encoding is different :(
    107  1.1  riastrad 	 * FIXME is this the right way to detect 852GM/852GMV?
    108  1.1  riastrad 	 */
    109  1.1  riastrad 	if (pdev->revision == 0x1) {
    110  1.1  riastrad 		cdclk_state->cdclk = 133333;
    111  1.1  riastrad 		return;
    112  1.1  riastrad 	}
    113  1.1  riastrad 
    114  1.1  riastrad 	pci_bus_read_config_word(pdev->bus,
    115  1.1  riastrad 				 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
    116  1.1  riastrad 
    117  1.1  riastrad 	/* Assume that the hardware is in the high speed state.  This
    118  1.1  riastrad 	 * should be the default.
    119  1.1  riastrad 	 */
    120  1.1  riastrad 	switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
    121  1.1  riastrad 	case GC_CLOCK_133_200:
    122  1.1  riastrad 	case GC_CLOCK_133_200_2:
    123  1.1  riastrad 	case GC_CLOCK_100_200:
    124  1.1  riastrad 		cdclk_state->cdclk = 200000;
    125  1.1  riastrad 		break;
    126  1.1  riastrad 	case GC_CLOCK_166_250:
    127  1.1  riastrad 		cdclk_state->cdclk = 250000;
    128  1.1  riastrad 		break;
    129  1.1  riastrad 	case GC_CLOCK_100_133:
    130  1.1  riastrad 		cdclk_state->cdclk = 133333;
    131  1.1  riastrad 		break;
    132  1.1  riastrad 	case GC_CLOCK_133_266:
    133  1.1  riastrad 	case GC_CLOCK_133_266_2:
    134  1.1  riastrad 	case GC_CLOCK_166_266:
    135  1.1  riastrad 		cdclk_state->cdclk = 266667;
    136  1.1  riastrad 		break;
    137  1.1  riastrad 	}
    138  1.1  riastrad }
    139  1.1  riastrad 
    140  1.1  riastrad static void i915gm_get_cdclk(struct drm_i915_private *dev_priv,
    141  1.1  riastrad 			     struct intel_cdclk_state *cdclk_state)
    142  1.1  riastrad {
    143  1.1  riastrad 	struct pci_dev *pdev = dev_priv->drm.pdev;
    144  1.1  riastrad 	u16 gcfgc = 0;
    145  1.1  riastrad 
    146  1.1  riastrad 	pci_read_config_word(pdev, GCFGC, &gcfgc);
    147  1.1  riastrad 
    148  1.1  riastrad 	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
    149  1.1  riastrad 		cdclk_state->cdclk = 133333;
    150  1.1  riastrad 		return;
    151  1.1  riastrad 	}
    152  1.1  riastrad 
    153  1.1  riastrad 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
    154  1.1  riastrad 	case GC_DISPLAY_CLOCK_333_320_MHZ:
    155  1.1  riastrad 		cdclk_state->cdclk = 333333;
    156  1.1  riastrad 		break;
    157  1.1  riastrad 	default:
    158  1.1  riastrad 	case GC_DISPLAY_CLOCK_190_200_MHZ:
    159  1.1  riastrad 		cdclk_state->cdclk = 190000;
    160  1.1  riastrad 		break;
    161  1.1  riastrad 	}
    162  1.1  riastrad }
    163  1.1  riastrad 
    164  1.1  riastrad static void i945gm_get_cdclk(struct drm_i915_private *dev_priv,
    165  1.1  riastrad 			     struct intel_cdclk_state *cdclk_state)
    166  1.1  riastrad {
    167  1.1  riastrad 	struct pci_dev *pdev = dev_priv->drm.pdev;
    168  1.1  riastrad 	u16 gcfgc = 0;
    169  1.1  riastrad 
    170  1.1  riastrad 	pci_read_config_word(pdev, GCFGC, &gcfgc);
    171  1.1  riastrad 
    172  1.1  riastrad 	if (gcfgc & GC_LOW_FREQUENCY_ENABLE) {
    173  1.1  riastrad 		cdclk_state->cdclk = 133333;
    174  1.1  riastrad 		return;
    175  1.1  riastrad 	}
    176  1.1  riastrad 
    177  1.1  riastrad 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
    178  1.1  riastrad 	case GC_DISPLAY_CLOCK_333_320_MHZ:
    179  1.1  riastrad 		cdclk_state->cdclk = 320000;
    180  1.1  riastrad 		break;
    181  1.1  riastrad 	default:
    182  1.1  riastrad 	case GC_DISPLAY_CLOCK_190_200_MHZ:
    183  1.1  riastrad 		cdclk_state->cdclk = 200000;
    184  1.1  riastrad 		break;
    185  1.1  riastrad 	}
    186  1.1  riastrad }
    187  1.1  riastrad 
    188  1.1  riastrad static unsigned int intel_hpll_vco(struct drm_i915_private *dev_priv)
    189  1.1  riastrad {
    190  1.1  riastrad 	static const unsigned int blb_vco[8] = {
    191  1.1  riastrad 		[0] = 3200000,
    192  1.1  riastrad 		[1] = 4000000,
    193  1.1  riastrad 		[2] = 5333333,
    194  1.1  riastrad 		[3] = 4800000,
    195  1.1  riastrad 		[4] = 6400000,
    196  1.1  riastrad 	};
    197  1.1  riastrad 	static const unsigned int pnv_vco[8] = {
    198  1.1  riastrad 		[0] = 3200000,
    199  1.1  riastrad 		[1] = 4000000,
    200  1.1  riastrad 		[2] = 5333333,
    201  1.1  riastrad 		[3] = 4800000,
    202  1.1  riastrad 		[4] = 2666667,
    203  1.1  riastrad 	};
    204  1.1  riastrad 	static const unsigned int cl_vco[8] = {
    205  1.1  riastrad 		[0] = 3200000,
    206  1.1  riastrad 		[1] = 4000000,
    207  1.1  riastrad 		[2] = 5333333,
    208  1.1  riastrad 		[3] = 6400000,
    209  1.1  riastrad 		[4] = 3333333,
    210  1.1  riastrad 		[5] = 3566667,
    211  1.1  riastrad 		[6] = 4266667,
    212  1.1  riastrad 	};
    213  1.1  riastrad 	static const unsigned int elk_vco[8] = {
    214  1.1  riastrad 		[0] = 3200000,
    215  1.1  riastrad 		[1] = 4000000,
    216  1.1  riastrad 		[2] = 5333333,
    217  1.1  riastrad 		[3] = 4800000,
    218  1.1  riastrad 	};
    219  1.1  riastrad 	static const unsigned int ctg_vco[8] = {
    220  1.1  riastrad 		[0] = 3200000,
    221  1.1  riastrad 		[1] = 4000000,
    222  1.1  riastrad 		[2] = 5333333,
    223  1.1  riastrad 		[3] = 6400000,
    224  1.1  riastrad 		[4] = 2666667,
    225  1.1  riastrad 		[5] = 4266667,
    226  1.1  riastrad 	};
    227  1.1  riastrad 	const unsigned int *vco_table;
    228  1.1  riastrad 	unsigned int vco;
    229  1.1  riastrad 	u8 tmp = 0;
    230  1.1  riastrad 
    231  1.1  riastrad 	/* FIXME other chipsets? */
    232  1.1  riastrad 	if (IS_GM45(dev_priv))
    233  1.1  riastrad 		vco_table = ctg_vco;
    234  1.1  riastrad 	else if (IS_G45(dev_priv))
    235  1.1  riastrad 		vco_table = elk_vco;
    236  1.1  riastrad 	else if (IS_I965GM(dev_priv))
    237  1.1  riastrad 		vco_table = cl_vco;
    238  1.1  riastrad 	else if (IS_PINEVIEW(dev_priv))
    239  1.1  riastrad 		vco_table = pnv_vco;
    240  1.1  riastrad 	else if (IS_G33(dev_priv))
    241  1.1  riastrad 		vco_table = blb_vco;
    242  1.1  riastrad 	else
    243  1.1  riastrad 		return 0;
    244  1.1  riastrad 
    245  1.1  riastrad 	tmp = I915_READ(IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv) ?
    246  1.1  riastrad 			HPLLVCO_MOBILE : HPLLVCO);
    247  1.1  riastrad 
    248  1.1  riastrad 	vco = vco_table[tmp & 0x7];
    249  1.1  riastrad 	if (vco == 0)
    250  1.1  riastrad 		DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
    251  1.1  riastrad 	else
    252  1.1  riastrad 		DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
    253  1.1  riastrad 
    254  1.1  riastrad 	return vco;
    255  1.1  riastrad }
    256  1.1  riastrad 
    257  1.1  riastrad static void g33_get_cdclk(struct drm_i915_private *dev_priv,
    258  1.1  riastrad 			  struct intel_cdclk_state *cdclk_state)
    259  1.1  riastrad {
    260  1.1  riastrad 	struct pci_dev *pdev = dev_priv->drm.pdev;
    261  1.1  riastrad 	static const u8 div_3200[] = { 12, 10,  8,  7, 5, 16 };
    262  1.1  riastrad 	static const u8 div_4000[] = { 14, 12, 10,  8, 6, 20 };
    263  1.1  riastrad 	static const u8 div_4800[] = { 20, 14, 12, 10, 8, 24 };
    264  1.1  riastrad 	static const u8 div_5333[] = { 20, 16, 12, 12, 8, 28 };
    265  1.1  riastrad 	const u8 *div_table;
    266  1.1  riastrad 	unsigned int cdclk_sel;
    267  1.1  riastrad 	u16 tmp = 0;
    268  1.1  riastrad 
    269  1.1  riastrad 	cdclk_state->vco = intel_hpll_vco(dev_priv);
    270  1.1  riastrad 
    271  1.1  riastrad 	pci_read_config_word(pdev, GCFGC, &tmp);
    272  1.1  riastrad 
    273  1.1  riastrad 	cdclk_sel = (tmp >> 4) & 0x7;
    274  1.1  riastrad 
    275  1.1  riastrad 	if (cdclk_sel >= ARRAY_SIZE(div_3200))
    276  1.1  riastrad 		goto fail;
    277  1.1  riastrad 
    278  1.1  riastrad 	switch (cdclk_state->vco) {
    279  1.1  riastrad 	case 3200000:
    280  1.1  riastrad 		div_table = div_3200;
    281  1.1  riastrad 		break;
    282  1.1  riastrad 	case 4000000:
    283  1.1  riastrad 		div_table = div_4000;
    284  1.1  riastrad 		break;
    285  1.1  riastrad 	case 4800000:
    286  1.1  riastrad 		div_table = div_4800;
    287  1.1  riastrad 		break;
    288  1.1  riastrad 	case 5333333:
    289  1.1  riastrad 		div_table = div_5333;
    290  1.1  riastrad 		break;
    291  1.1  riastrad 	default:
    292  1.1  riastrad 		goto fail;
    293  1.1  riastrad 	}
    294  1.1  riastrad 
    295  1.1  riastrad 	cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco,
    296  1.1  riastrad 					       div_table[cdclk_sel]);
    297  1.1  riastrad 	return;
    298  1.1  riastrad 
    299  1.1  riastrad fail:
    300  1.1  riastrad 	DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n",
    301  1.1  riastrad 		  cdclk_state->vco, tmp);
    302  1.1  riastrad 	cdclk_state->cdclk = 190476;
    303  1.1  riastrad }
    304  1.1  riastrad 
    305  1.1  riastrad static void pnv_get_cdclk(struct drm_i915_private *dev_priv,
    306  1.1  riastrad 			  struct intel_cdclk_state *cdclk_state)
    307  1.1  riastrad {
    308  1.1  riastrad 	struct pci_dev *pdev = dev_priv->drm.pdev;
    309  1.1  riastrad 	u16 gcfgc = 0;
    310  1.1  riastrad 
    311  1.1  riastrad 	pci_read_config_word(pdev, GCFGC, &gcfgc);
    312  1.1  riastrad 
    313  1.1  riastrad 	switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
    314  1.1  riastrad 	case GC_DISPLAY_CLOCK_267_MHZ_PNV:
    315  1.1  riastrad 		cdclk_state->cdclk = 266667;
    316  1.1  riastrad 		break;
    317  1.1  riastrad 	case GC_DISPLAY_CLOCK_333_MHZ_PNV:
    318  1.1  riastrad 		cdclk_state->cdclk = 333333;
    319  1.1  riastrad 		break;
    320  1.1  riastrad 	case GC_DISPLAY_CLOCK_444_MHZ_PNV:
    321  1.1  riastrad 		cdclk_state->cdclk = 444444;
    322  1.1  riastrad 		break;
    323  1.1  riastrad 	case GC_DISPLAY_CLOCK_200_MHZ_PNV:
    324  1.1  riastrad 		cdclk_state->cdclk = 200000;
    325  1.1  riastrad 		break;
    326  1.1  riastrad 	default:
    327  1.1  riastrad 		DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
    328  1.1  riastrad 		/* fall through */
    329  1.1  riastrad 	case GC_DISPLAY_CLOCK_133_MHZ_PNV:
    330  1.1  riastrad 		cdclk_state->cdclk = 133333;
    331  1.1  riastrad 		break;
    332  1.1  riastrad 	case GC_DISPLAY_CLOCK_167_MHZ_PNV:
    333  1.1  riastrad 		cdclk_state->cdclk = 166667;
    334  1.1  riastrad 		break;
    335  1.1  riastrad 	}
    336  1.1  riastrad }
    337  1.1  riastrad 
    338  1.1  riastrad static void i965gm_get_cdclk(struct drm_i915_private *dev_priv,
    339  1.1  riastrad 			     struct intel_cdclk_state *cdclk_state)
    340  1.1  riastrad {
    341  1.1  riastrad 	struct pci_dev *pdev = dev_priv->drm.pdev;
    342  1.1  riastrad 	static const u8 div_3200[] = { 16, 10,  8 };
    343  1.1  riastrad 	static const u8 div_4000[] = { 20, 12, 10 };
    344  1.1  riastrad 	static const u8 div_5333[] = { 24, 16, 14 };
    345  1.1  riastrad 	const u8 *div_table;
    346  1.1  riastrad 	unsigned int cdclk_sel;
    347  1.1  riastrad 	u16 tmp = 0;
    348  1.1  riastrad 
    349  1.1  riastrad 	cdclk_state->vco = intel_hpll_vco(dev_priv);
    350  1.1  riastrad 
    351  1.1  riastrad 	pci_read_config_word(pdev, GCFGC, &tmp);
    352  1.1  riastrad 
    353  1.1  riastrad 	cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
    354  1.1  riastrad 
    355  1.1  riastrad 	if (cdclk_sel >= ARRAY_SIZE(div_3200))
    356  1.1  riastrad 		goto fail;
    357  1.1  riastrad 
    358  1.1  riastrad 	switch (cdclk_state->vco) {
    359  1.1  riastrad 	case 3200000:
    360  1.1  riastrad 		div_table = div_3200;
    361  1.1  riastrad 		break;
    362  1.1  riastrad 	case 4000000:
    363  1.1  riastrad 		div_table = div_4000;
    364  1.1  riastrad 		break;
    365  1.1  riastrad 	case 5333333:
    366  1.1  riastrad 		div_table = div_5333;
    367  1.1  riastrad 		break;
    368  1.1  riastrad 	default:
    369  1.1  riastrad 		goto fail;
    370  1.1  riastrad 	}
    371  1.1  riastrad 
    372  1.1  riastrad 	cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco,
    373  1.1  riastrad 					       div_table[cdclk_sel]);
    374  1.1  riastrad 	return;
    375  1.1  riastrad 
    376  1.1  riastrad fail:
    377  1.1  riastrad 	DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n",
    378  1.1  riastrad 		  cdclk_state->vco, tmp);
    379  1.1  riastrad 	cdclk_state->cdclk = 200000;
    380  1.1  riastrad }
    381  1.1  riastrad 
    382  1.1  riastrad static void gm45_get_cdclk(struct drm_i915_private *dev_priv,
    383  1.1  riastrad 			   struct intel_cdclk_state *cdclk_state)
    384  1.1  riastrad {
    385  1.1  riastrad 	struct pci_dev *pdev = dev_priv->drm.pdev;
    386  1.1  riastrad 	unsigned int cdclk_sel;
    387  1.1  riastrad 	u16 tmp = 0;
    388  1.1  riastrad 
    389  1.1  riastrad 	cdclk_state->vco = intel_hpll_vco(dev_priv);
    390  1.1  riastrad 
    391  1.1  riastrad 	pci_read_config_word(pdev, GCFGC, &tmp);
    392  1.1  riastrad 
    393  1.1  riastrad 	cdclk_sel = (tmp >> 12) & 0x1;
    394  1.1  riastrad 
    395  1.1  riastrad 	switch (cdclk_state->vco) {
    396  1.1  riastrad 	case 2666667:
    397  1.1  riastrad 	case 4000000:
    398  1.1  riastrad 	case 5333333:
    399  1.1  riastrad 		cdclk_state->cdclk = cdclk_sel ? 333333 : 222222;
    400  1.1  riastrad 		break;
    401  1.1  riastrad 	case 3200000:
    402  1.1  riastrad 		cdclk_state->cdclk = cdclk_sel ? 320000 : 228571;
    403  1.1  riastrad 		break;
    404  1.1  riastrad 	default:
    405  1.1  riastrad 		DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n",
    406  1.1  riastrad 			  cdclk_state->vco, tmp);
    407  1.1  riastrad 		cdclk_state->cdclk = 222222;
    408  1.1  riastrad 		break;
    409  1.1  riastrad 	}
    410  1.1  riastrad }
    411  1.1  riastrad 
    412  1.1  riastrad static void hsw_get_cdclk(struct drm_i915_private *dev_priv,
    413  1.1  riastrad 			  struct intel_cdclk_state *cdclk_state)
    414  1.1  riastrad {
    415  1.1  riastrad 	u32 lcpll = I915_READ(LCPLL_CTL);
    416  1.1  riastrad 	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
    417  1.1  riastrad 
    418  1.1  riastrad 	if (lcpll & LCPLL_CD_SOURCE_FCLK)
    419  1.1  riastrad 		cdclk_state->cdclk = 800000;
    420  1.1  riastrad 	else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
    421  1.1  riastrad 		cdclk_state->cdclk = 450000;
    422  1.1  riastrad 	else if (freq == LCPLL_CLK_FREQ_450)
    423  1.1  riastrad 		cdclk_state->cdclk = 450000;
    424  1.1  riastrad 	else if (IS_HSW_ULT(dev_priv))
    425  1.1  riastrad 		cdclk_state->cdclk = 337500;
    426  1.1  riastrad 	else
    427  1.1  riastrad 		cdclk_state->cdclk = 540000;
    428  1.1  riastrad }
    429  1.1  riastrad 
    430  1.1  riastrad static int vlv_calc_cdclk(struct drm_i915_private *dev_priv, int min_cdclk)
    431  1.1  riastrad {
    432  1.1  riastrad 	int freq_320 = (dev_priv->hpll_freq <<  1) % 320000 != 0 ?
    433  1.1  riastrad 		333333 : 320000;
    434  1.1  riastrad 
    435  1.1  riastrad 	/*
    436  1.1  riastrad 	 * We seem to get an unstable or solid color picture at 200MHz.
    437  1.1  riastrad 	 * Not sure what's wrong. For now use 200MHz only when all pipes
    438  1.1  riastrad 	 * are off.
    439  1.1  riastrad 	 */
    440  1.1  riastrad 	if (IS_VALLEYVIEW(dev_priv) && min_cdclk > freq_320)
    441  1.1  riastrad 		return 400000;
    442  1.1  riastrad 	else if (min_cdclk > 266667)
    443  1.1  riastrad 		return freq_320;
    444  1.1  riastrad 	else if (min_cdclk > 0)
    445  1.1  riastrad 		return 266667;
    446  1.1  riastrad 	else
    447  1.1  riastrad 		return 200000;
    448  1.1  riastrad }
    449  1.1  riastrad 
    450  1.1  riastrad static u8 vlv_calc_voltage_level(struct drm_i915_private *dev_priv, int cdclk)
    451  1.1  riastrad {
    452  1.1  riastrad 	if (IS_VALLEYVIEW(dev_priv)) {
    453  1.1  riastrad 		if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
    454  1.1  riastrad 			return 2;
    455  1.1  riastrad 		else if (cdclk >= 266667)
    456  1.1  riastrad 			return 1;
    457  1.1  riastrad 		else
    458  1.1  riastrad 			return 0;
    459  1.1  riastrad 	} else {
    460  1.1  riastrad 		/*
    461  1.1  riastrad 		 * Specs are full of misinformation, but testing on actual
    462  1.1  riastrad 		 * hardware has shown that we just need to write the desired
    463  1.1  riastrad 		 * CCK divider into the Punit register.
    464  1.1  riastrad 		 */
    465  1.1  riastrad 		return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
    466  1.1  riastrad 	}
    467  1.1  riastrad }
    468  1.1  riastrad 
    469  1.1  riastrad static void vlv_get_cdclk(struct drm_i915_private *dev_priv,
    470  1.1  riastrad 			  struct intel_cdclk_state *cdclk_state)
    471  1.1  riastrad {
    472  1.1  riastrad 	u32 val;
    473  1.1  riastrad 
    474  1.1  riastrad 	vlv_iosf_sb_get(dev_priv,
    475  1.1  riastrad 			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));
    476  1.1  riastrad 
    477  1.1  riastrad 	cdclk_state->vco = vlv_get_hpll_vco(dev_priv);
    478  1.1  riastrad 	cdclk_state->cdclk = vlv_get_cck_clock(dev_priv, "cdclk",
    479  1.1  riastrad 					       CCK_DISPLAY_CLOCK_CONTROL,
    480  1.1  riastrad 					       cdclk_state->vco);
    481  1.1  riastrad 
    482  1.1  riastrad 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
    483  1.1  riastrad 
    484  1.1  riastrad 	vlv_iosf_sb_put(dev_priv,
    485  1.1  riastrad 			BIT(VLV_IOSF_SB_CCK) | BIT(VLV_IOSF_SB_PUNIT));
    486  1.1  riastrad 
    487  1.1  riastrad 	if (IS_VALLEYVIEW(dev_priv))
    488  1.1  riastrad 		cdclk_state->voltage_level = (val & DSPFREQGUAR_MASK) >>
    489  1.1  riastrad 			DSPFREQGUAR_SHIFT;
    490  1.1  riastrad 	else
    491  1.1  riastrad 		cdclk_state->voltage_level = (val & DSPFREQGUAR_MASK_CHV) >>
    492  1.1  riastrad 			DSPFREQGUAR_SHIFT_CHV;
    493  1.1  riastrad }
    494  1.1  riastrad 
    495  1.1  riastrad static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
    496  1.1  riastrad {
    497  1.1  riastrad 	unsigned int credits, default_credits;
    498  1.1  riastrad 
    499  1.1  riastrad 	if (IS_CHERRYVIEW(dev_priv))
    500  1.1  riastrad 		default_credits = PFI_CREDIT(12);
    501  1.1  riastrad 	else
    502  1.1  riastrad 		default_credits = PFI_CREDIT(8);
    503  1.1  riastrad 
    504  1.1  riastrad 	if (dev_priv->cdclk.hw.cdclk >= dev_priv->czclk_freq) {
    505  1.1  riastrad 		/* CHV suggested value is 31 or 63 */
    506  1.1  riastrad 		if (IS_CHERRYVIEW(dev_priv))
    507  1.1  riastrad 			credits = PFI_CREDIT_63;
    508  1.1  riastrad 		else
    509  1.1  riastrad 			credits = PFI_CREDIT(15);
    510  1.1  riastrad 	} else {
    511  1.1  riastrad 		credits = default_credits;
    512  1.1  riastrad 	}
    513  1.1  riastrad 
    514  1.1  riastrad 	/*
    515  1.1  riastrad 	 * WA - write default credits before re-programming
    516  1.1  riastrad 	 * FIXME: should we also set the resend bit here?
    517  1.1  riastrad 	 */
    518  1.1  riastrad 	I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
    519  1.1  riastrad 		   default_credits);
    520  1.1  riastrad 
    521  1.1  riastrad 	I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
    522  1.1  riastrad 		   credits | PFI_CREDIT_RESEND);
    523  1.1  riastrad 
    524  1.1  riastrad 	/*
    525  1.1  riastrad 	 * FIXME is this guaranteed to clear
    526  1.1  riastrad 	 * immediately or should we poll for it?
    527  1.1  riastrad 	 */
    528  1.1  riastrad 	WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
    529  1.1  riastrad }
    530  1.1  riastrad 
    531  1.1  riastrad static void vlv_set_cdclk(struct drm_i915_private *dev_priv,
    532  1.1  riastrad 			  const struct intel_cdclk_state *cdclk_state,
    533  1.1  riastrad 			  enum pipe pipe)
    534  1.1  riastrad {
    535  1.1  riastrad 	int cdclk = cdclk_state->cdclk;
    536  1.1  riastrad 	u32 val, cmd = cdclk_state->voltage_level;
    537  1.1  riastrad 	intel_wakeref_t wakeref;
    538  1.1  riastrad 
    539  1.1  riastrad 	switch (cdclk) {
    540  1.1  riastrad 	case 400000:
    541  1.1  riastrad 	case 333333:
    542  1.1  riastrad 	case 320000:
    543  1.1  riastrad 	case 266667:
    544  1.1  riastrad 	case 200000:
    545  1.1  riastrad 		break;
    546  1.1  riastrad 	default:
    547  1.1  riastrad 		MISSING_CASE(cdclk);
    548  1.1  riastrad 		return;
    549  1.1  riastrad 	}
    550  1.1  riastrad 
    551  1.1  riastrad 	/* There are cases where we can end up here with power domains
    552  1.1  riastrad 	 * off and a CDCLK frequency other than the minimum, like when
    553  1.1  riastrad 	 * issuing a modeset without actually changing any display after
    554  1.1  riastrad 	 * a system suspend.  So grab the display core domain, which covers
    555  1.1  riastrad 	 * the HW blocks needed for the following programming.
    556  1.1  riastrad 	 */
    557  1.1  riastrad 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
    558  1.1  riastrad 
    559  1.1  riastrad 	vlv_iosf_sb_get(dev_priv,
    560  1.1  riastrad 			BIT(VLV_IOSF_SB_CCK) |
    561  1.1  riastrad 			BIT(VLV_IOSF_SB_BUNIT) |
    562  1.1  riastrad 			BIT(VLV_IOSF_SB_PUNIT));
    563  1.1  riastrad 
    564  1.1  riastrad 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
    565  1.1  riastrad 	val &= ~DSPFREQGUAR_MASK;
    566  1.1  riastrad 	val |= (cmd << DSPFREQGUAR_SHIFT);
    567  1.1  riastrad 	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
    568  1.1  riastrad 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM) &
    569  1.1  riastrad 		      DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
    570  1.1  riastrad 		     50)) {
    571  1.1  riastrad 		DRM_ERROR("timed out waiting for CDclk change\n");
    572  1.1  riastrad 	}
    573  1.1  riastrad 
    574  1.1  riastrad 	if (cdclk == 400000) {
    575  1.1  riastrad 		u32 divider;
    576  1.1  riastrad 
    577  1.1  riastrad 		divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1,
    578  1.1  riastrad 					    cdclk) - 1;
    579  1.1  riastrad 
    580  1.1  riastrad 		/* adjust cdclk divider */
    581  1.1  riastrad 		val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
    582  1.1  riastrad 		val &= ~CCK_FREQUENCY_VALUES;
    583  1.1  riastrad 		val |= divider;
    584  1.1  riastrad 		vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
    585  1.1  riastrad 
    586  1.1  riastrad 		if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
    587  1.1  riastrad 			      CCK_FREQUENCY_STATUS) == (divider << CCK_FREQUENCY_STATUS_SHIFT),
    588  1.1  riastrad 			     50))
    589  1.1  riastrad 			DRM_ERROR("timed out waiting for CDclk change\n");
    590  1.1  riastrad 	}
    591  1.1  riastrad 
    592  1.1  riastrad 	/* adjust self-refresh exit latency value */
    593  1.1  riastrad 	val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
    594  1.1  riastrad 	val &= ~0x7f;
    595  1.1  riastrad 
    596  1.1  riastrad 	/*
    597  1.1  riastrad 	 * For high bandwidth configs, we set a higher latency in the bunit
    598  1.1  riastrad 	 * so that the core display fetch happens in time to avoid underruns.
    599  1.1  riastrad 	 */
    600  1.1  riastrad 	if (cdclk == 400000)
    601  1.1  riastrad 		val |= 4500 / 250; /* 4.5 usec */
    602  1.1  riastrad 	else
    603  1.1  riastrad 		val |= 3000 / 250; /* 3.0 usec */
    604  1.1  riastrad 	vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
    605  1.1  riastrad 
    606  1.1  riastrad 	vlv_iosf_sb_put(dev_priv,
    607  1.1  riastrad 			BIT(VLV_IOSF_SB_CCK) |
    608  1.1  riastrad 			BIT(VLV_IOSF_SB_BUNIT) |
    609  1.1  riastrad 			BIT(VLV_IOSF_SB_PUNIT));
    610  1.1  riastrad 
    611  1.1  riastrad 	intel_update_cdclk(dev_priv);
    612  1.1  riastrad 
    613  1.1  riastrad 	vlv_program_pfi_credits(dev_priv);
    614  1.1  riastrad 
    615  1.1  riastrad 	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
    616  1.1  riastrad }
    617  1.1  riastrad 
    618  1.1  riastrad static void chv_set_cdclk(struct drm_i915_private *dev_priv,
    619  1.1  riastrad 			  const struct intel_cdclk_state *cdclk_state,
    620  1.1  riastrad 			  enum pipe pipe)
    621  1.1  riastrad {
    622  1.1  riastrad 	int cdclk = cdclk_state->cdclk;
    623  1.1  riastrad 	u32 val, cmd = cdclk_state->voltage_level;
    624  1.1  riastrad 	intel_wakeref_t wakeref;
    625  1.1  riastrad 
    626  1.1  riastrad 	switch (cdclk) {
    627  1.1  riastrad 	case 333333:
    628  1.1  riastrad 	case 320000:
    629  1.1  riastrad 	case 266667:
    630  1.1  riastrad 	case 200000:
    631  1.1  riastrad 		break;
    632  1.1  riastrad 	default:
    633  1.1  riastrad 		MISSING_CASE(cdclk);
    634  1.1  riastrad 		return;
    635  1.1  riastrad 	}
    636  1.1  riastrad 
    637  1.1  riastrad 	/* There are cases where we can end up here with power domains
    638  1.1  riastrad 	 * off and a CDCLK frequency other than the minimum, like when
    639  1.1  riastrad 	 * issuing a modeset without actually changing any display after
    640  1.1  riastrad 	 * a system suspend.  So grab the display core domain, which covers
    641  1.1  riastrad 	 * the HW blocks needed for the following programming.
    642  1.1  riastrad 	 */
    643  1.1  riastrad 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DISPLAY_CORE);
    644  1.1  riastrad 
    645  1.1  riastrad 	vlv_punit_get(dev_priv);
    646  1.1  riastrad 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
    647  1.1  riastrad 	val &= ~DSPFREQGUAR_MASK_CHV;
    648  1.1  riastrad 	val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
    649  1.1  riastrad 	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
    650  1.1  riastrad 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM) &
    651  1.1  riastrad 		      DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
    652  1.1  riastrad 		     50)) {
    653  1.1  riastrad 		DRM_ERROR("timed out waiting for CDclk change\n");
    654  1.1  riastrad 	}
    655  1.1  riastrad 
    656  1.1  riastrad 	vlv_punit_put(dev_priv);
    657  1.1  riastrad 
    658  1.1  riastrad 	intel_update_cdclk(dev_priv);
    659  1.1  riastrad 
    660  1.1  riastrad 	vlv_program_pfi_credits(dev_priv);
    661  1.1  riastrad 
    662  1.1  riastrad 	intel_display_power_put(dev_priv, POWER_DOMAIN_DISPLAY_CORE, wakeref);
    663  1.1  riastrad }
    664  1.1  riastrad 
    665  1.1  riastrad static int bdw_calc_cdclk(int min_cdclk)
    666  1.1  riastrad {
    667  1.1  riastrad 	if (min_cdclk > 540000)
    668  1.1  riastrad 		return 675000;
    669  1.1  riastrad 	else if (min_cdclk > 450000)
    670  1.1  riastrad 		return 540000;
    671  1.1  riastrad 	else if (min_cdclk > 337500)
    672  1.1  riastrad 		return 450000;
    673  1.1  riastrad 	else
    674  1.1  riastrad 		return 337500;
    675  1.1  riastrad }
    676  1.1  riastrad 
    677  1.1  riastrad static u8 bdw_calc_voltage_level(int cdclk)
    678  1.1  riastrad {
    679  1.1  riastrad 	switch (cdclk) {
    680  1.1  riastrad 	default:
    681  1.1  riastrad 	case 337500:
    682  1.1  riastrad 		return 2;
    683  1.1  riastrad 	case 450000:
    684  1.1  riastrad 		return 0;
    685  1.1  riastrad 	case 540000:
    686  1.1  riastrad 		return 1;
    687  1.1  riastrad 	case 675000:
    688  1.1  riastrad 		return 3;
    689  1.1  riastrad 	}
    690  1.1  riastrad }
    691  1.1  riastrad 
    692  1.1  riastrad static void bdw_get_cdclk(struct drm_i915_private *dev_priv,
    693  1.1  riastrad 			  struct intel_cdclk_state *cdclk_state)
    694  1.1  riastrad {
    695  1.1  riastrad 	u32 lcpll = I915_READ(LCPLL_CTL);
    696  1.1  riastrad 	u32 freq = lcpll & LCPLL_CLK_FREQ_MASK;
    697  1.1  riastrad 
    698  1.1  riastrad 	if (lcpll & LCPLL_CD_SOURCE_FCLK)
    699  1.1  riastrad 		cdclk_state->cdclk = 800000;
    700  1.1  riastrad 	else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
    701  1.1  riastrad 		cdclk_state->cdclk = 450000;
    702  1.1  riastrad 	else if (freq == LCPLL_CLK_FREQ_450)
    703  1.1  riastrad 		cdclk_state->cdclk = 450000;
    704  1.1  riastrad 	else if (freq == LCPLL_CLK_FREQ_54O_BDW)
    705  1.1  riastrad 		cdclk_state->cdclk = 540000;
    706  1.1  riastrad 	else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
    707  1.1  riastrad 		cdclk_state->cdclk = 337500;
    708  1.1  riastrad 	else
    709  1.1  riastrad 		cdclk_state->cdclk = 675000;
    710  1.1  riastrad 
    711  1.1  riastrad 	/*
    712  1.1  riastrad 	 * Can't read this out :( Let's assume it's
    713  1.1  riastrad 	 * at least what the CDCLK frequency requires.
    714  1.1  riastrad 	 */
    715  1.1  riastrad 	cdclk_state->voltage_level =
    716  1.1  riastrad 		bdw_calc_voltage_level(cdclk_state->cdclk);
    717  1.1  riastrad }
    718  1.1  riastrad 
    719  1.1  riastrad static void bdw_set_cdclk(struct drm_i915_private *dev_priv,
    720  1.1  riastrad 			  const struct intel_cdclk_state *cdclk_state,
    721  1.1  riastrad 			  enum pipe pipe)
    722  1.1  riastrad {
    723  1.1  riastrad 	int cdclk = cdclk_state->cdclk;
    724  1.1  riastrad 	u32 val;
    725  1.1  riastrad 	int ret;
    726  1.1  riastrad 
    727  1.1  riastrad 	if (WARN((I915_READ(LCPLL_CTL) &
    728  1.1  riastrad 		  (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
    729  1.1  riastrad 		   LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
    730  1.1  riastrad 		   LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
    731  1.1  riastrad 		   LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
    732  1.1  riastrad 		 "trying to change cdclk frequency with cdclk not enabled\n"))
    733  1.1  riastrad 		return;
    734  1.1  riastrad 
    735  1.1  riastrad 	ret = sandybridge_pcode_write(dev_priv,
    736  1.1  riastrad 				      BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
    737  1.1  riastrad 	if (ret) {
    738  1.1  riastrad 		DRM_ERROR("failed to inform pcode about cdclk change\n");
    739  1.1  riastrad 		return;
    740  1.1  riastrad 	}
    741  1.1  riastrad 
    742  1.1  riastrad 	val = I915_READ(LCPLL_CTL);
    743  1.1  riastrad 	val |= LCPLL_CD_SOURCE_FCLK;
    744  1.1  riastrad 	I915_WRITE(LCPLL_CTL, val);
    745  1.1  riastrad 
    746  1.1  riastrad 	/*
    747  1.1  riastrad 	 * According to the spec, it should be enough to poll for this 1 us.
    748  1.1  riastrad 	 * However, extensive testing shows that this can take longer.
    749  1.1  riastrad 	 */
    750  1.1  riastrad 	if (wait_for_us(I915_READ(LCPLL_CTL) &
    751  1.1  riastrad 			LCPLL_CD_SOURCE_FCLK_DONE, 100))
    752  1.1  riastrad 		DRM_ERROR("Switching to FCLK failed\n");
    753  1.1  riastrad 
    754  1.1  riastrad 	val = I915_READ(LCPLL_CTL);
    755  1.1  riastrad 	val &= ~LCPLL_CLK_FREQ_MASK;
    756  1.1  riastrad 
    757  1.1  riastrad 	switch (cdclk) {
    758  1.1  riastrad 	default:
    759  1.1  riastrad 		MISSING_CASE(cdclk);
    760  1.1  riastrad 		/* fall through */
    761  1.1  riastrad 	case 337500:
    762  1.1  riastrad 		val |= LCPLL_CLK_FREQ_337_5_BDW;
    763  1.1  riastrad 		break;
    764  1.1  riastrad 	case 450000:
    765  1.1  riastrad 		val |= LCPLL_CLK_FREQ_450;
    766  1.1  riastrad 		break;
    767  1.1  riastrad 	case 540000:
    768  1.1  riastrad 		val |= LCPLL_CLK_FREQ_54O_BDW;
    769  1.1  riastrad 		break;
    770  1.1  riastrad 	case 675000:
    771  1.1  riastrad 		val |= LCPLL_CLK_FREQ_675_BDW;
    772  1.1  riastrad 		break;
    773  1.1  riastrad 	}
    774  1.1  riastrad 
    775  1.1  riastrad 	I915_WRITE(LCPLL_CTL, val);
    776  1.1  riastrad 
    777  1.1  riastrad 	val = I915_READ(LCPLL_CTL);
    778  1.1  riastrad 	val &= ~LCPLL_CD_SOURCE_FCLK;
    779  1.1  riastrad 	I915_WRITE(LCPLL_CTL, val);
    780  1.1  riastrad 
    781  1.1  riastrad 	if (wait_for_us((I915_READ(LCPLL_CTL) &
    782  1.1  riastrad 			LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
    783  1.1  riastrad 		DRM_ERROR("Switching back to LCPLL failed\n");
    784  1.1  riastrad 
    785  1.1  riastrad 	sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
    786  1.1  riastrad 				cdclk_state->voltage_level);
    787  1.1  riastrad 
    788  1.1  riastrad 	I915_WRITE(CDCLK_FREQ, DIV_ROUND_CLOSEST(cdclk, 1000) - 1);
    789  1.1  riastrad 
    790  1.1  riastrad 	intel_update_cdclk(dev_priv);
    791  1.1  riastrad }
    792  1.1  riastrad 
    793  1.1  riastrad static int skl_calc_cdclk(int min_cdclk, int vco)
    794  1.1  riastrad {
    795  1.1  riastrad 	if (vco == 8640000) {
    796  1.1  riastrad 		if (min_cdclk > 540000)
    797  1.1  riastrad 			return 617143;
    798  1.1  riastrad 		else if (min_cdclk > 432000)
    799  1.1  riastrad 			return 540000;
    800  1.1  riastrad 		else if (min_cdclk > 308571)
    801  1.1  riastrad 			return 432000;
    802  1.1  riastrad 		else
    803  1.1  riastrad 			return 308571;
    804  1.1  riastrad 	} else {
    805  1.1  riastrad 		if (min_cdclk > 540000)
    806  1.1  riastrad 			return 675000;
    807  1.1  riastrad 		else if (min_cdclk > 450000)
    808  1.1  riastrad 			return 540000;
    809  1.1  riastrad 		else if (min_cdclk > 337500)
    810  1.1  riastrad 			return 450000;
    811  1.1  riastrad 		else
    812  1.1  riastrad 			return 337500;
    813  1.1  riastrad 	}
    814  1.1  riastrad }
    815  1.1  riastrad 
    816  1.1  riastrad static u8 skl_calc_voltage_level(int cdclk)
    817  1.1  riastrad {
    818  1.1  riastrad 	if (cdclk > 540000)
    819  1.1  riastrad 		return 3;
    820  1.1  riastrad 	else if (cdclk > 450000)
    821  1.1  riastrad 		return 2;
    822  1.1  riastrad 	else if (cdclk > 337500)
    823  1.1  riastrad 		return 1;
    824  1.1  riastrad 	else
    825  1.1  riastrad 		return 0;
    826  1.1  riastrad }
    827  1.1  riastrad 
    828  1.1  riastrad static void skl_dpll0_update(struct drm_i915_private *dev_priv,
    829  1.1  riastrad 			     struct intel_cdclk_state *cdclk_state)
    830  1.1  riastrad {
    831  1.1  riastrad 	u32 val;
    832  1.1  riastrad 
    833  1.1  riastrad 	cdclk_state->ref = 24000;
    834  1.1  riastrad 	cdclk_state->vco = 0;
    835  1.1  riastrad 
    836  1.1  riastrad 	val = I915_READ(LCPLL1_CTL);
    837  1.1  riastrad 	if ((val & LCPLL_PLL_ENABLE) == 0)
    838  1.1  riastrad 		return;
    839  1.1  riastrad 
    840  1.1  riastrad 	if (WARN_ON((val & LCPLL_PLL_LOCK) == 0))
    841  1.1  riastrad 		return;
    842  1.1  riastrad 
    843  1.1  riastrad 	val = I915_READ(DPLL_CTRL1);
    844  1.1  riastrad 
    845  1.1  riastrad 	if (WARN_ON((val & (DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) |
    846  1.1  riastrad 			    DPLL_CTRL1_SSC(SKL_DPLL0) |
    847  1.1  riastrad 			    DPLL_CTRL1_OVERRIDE(SKL_DPLL0))) !=
    848  1.1  riastrad 		    DPLL_CTRL1_OVERRIDE(SKL_DPLL0)))
    849  1.1  riastrad 		return;
    850  1.1  riastrad 
    851  1.1  riastrad 	switch (val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) {
    852  1.1  riastrad 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810, SKL_DPLL0):
    853  1.1  riastrad 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1350, SKL_DPLL0):
    854  1.1  riastrad 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1620, SKL_DPLL0):
    855  1.1  riastrad 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2700, SKL_DPLL0):
    856  1.1  riastrad 		cdclk_state->vco = 8100000;
    857  1.1  riastrad 		break;
    858  1.1  riastrad 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080, SKL_DPLL0):
    859  1.1  riastrad 	case DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_2160, SKL_DPLL0):
    860  1.1  riastrad 		cdclk_state->vco = 8640000;
    861  1.1  riastrad 		break;
    862  1.1  riastrad 	default:
    863  1.1  riastrad 		MISSING_CASE(val & DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
    864  1.1  riastrad 		break;
    865  1.1  riastrad 	}
    866  1.1  riastrad }
    867  1.1  riastrad 
    868  1.1  riastrad static void skl_get_cdclk(struct drm_i915_private *dev_priv,
    869  1.1  riastrad 			  struct intel_cdclk_state *cdclk_state)
    870  1.1  riastrad {
    871  1.1  riastrad 	u32 cdctl;
    872  1.1  riastrad 
    873  1.1  riastrad 	skl_dpll0_update(dev_priv, cdclk_state);
    874  1.1  riastrad 
    875  1.1  riastrad 	cdclk_state->cdclk = cdclk_state->bypass = cdclk_state->ref;
    876  1.1  riastrad 
    877  1.1  riastrad 	if (cdclk_state->vco == 0)
    878  1.1  riastrad 		goto out;
    879  1.1  riastrad 
    880  1.1  riastrad 	cdctl = I915_READ(CDCLK_CTL);
    881  1.1  riastrad 
    882  1.1  riastrad 	if (cdclk_state->vco == 8640000) {
    883  1.1  riastrad 		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
    884  1.1  riastrad 		case CDCLK_FREQ_450_432:
    885  1.1  riastrad 			cdclk_state->cdclk = 432000;
    886  1.1  riastrad 			break;
    887  1.1  riastrad 		case CDCLK_FREQ_337_308:
    888  1.1  riastrad 			cdclk_state->cdclk = 308571;
    889  1.1  riastrad 			break;
    890  1.1  riastrad 		case CDCLK_FREQ_540:
    891  1.1  riastrad 			cdclk_state->cdclk = 540000;
    892  1.1  riastrad 			break;
    893  1.1  riastrad 		case CDCLK_FREQ_675_617:
    894  1.1  riastrad 			cdclk_state->cdclk = 617143;
    895  1.1  riastrad 			break;
    896  1.1  riastrad 		default:
    897  1.1  riastrad 			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
    898  1.1  riastrad 			break;
    899  1.1  riastrad 		}
    900  1.1  riastrad 	} else {
    901  1.1  riastrad 		switch (cdctl & CDCLK_FREQ_SEL_MASK) {
    902  1.1  riastrad 		case CDCLK_FREQ_450_432:
    903  1.1  riastrad 			cdclk_state->cdclk = 450000;
    904  1.1  riastrad 			break;
    905  1.1  riastrad 		case CDCLK_FREQ_337_308:
    906  1.1  riastrad 			cdclk_state->cdclk = 337500;
    907  1.1  riastrad 			break;
    908  1.1  riastrad 		case CDCLK_FREQ_540:
    909  1.1  riastrad 			cdclk_state->cdclk = 540000;
    910  1.1  riastrad 			break;
    911  1.1  riastrad 		case CDCLK_FREQ_675_617:
    912  1.1  riastrad 			cdclk_state->cdclk = 675000;
    913  1.1  riastrad 			break;
    914  1.1  riastrad 		default:
    915  1.1  riastrad 			MISSING_CASE(cdctl & CDCLK_FREQ_SEL_MASK);
    916  1.1  riastrad 			break;
    917  1.1  riastrad 		}
    918  1.1  riastrad 	}
    919  1.1  riastrad 
    920  1.1  riastrad  out:
    921  1.1  riastrad 	/*
    922  1.1  riastrad 	 * Can't read this out :( Let's assume it's
    923  1.1  riastrad 	 * at least what the CDCLK frequency requires.
    924  1.1  riastrad 	 */
    925  1.1  riastrad 	cdclk_state->voltage_level =
    926  1.1  riastrad 		skl_calc_voltage_level(cdclk_state->cdclk);
    927  1.1  riastrad }
    928  1.1  riastrad 
    929  1.1  riastrad /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
    930  1.1  riastrad static int skl_cdclk_decimal(int cdclk)
    931  1.1  riastrad {
    932  1.1  riastrad 	return DIV_ROUND_CLOSEST(cdclk - 1000, 500);
    933  1.1  riastrad }
    934  1.1  riastrad 
    935  1.1  riastrad static void skl_set_preferred_cdclk_vco(struct drm_i915_private *dev_priv,
    936  1.1  riastrad 					int vco)
    937  1.1  riastrad {
    938  1.1  riastrad 	bool changed = dev_priv->skl_preferred_vco_freq != vco;
    939  1.1  riastrad 
    940  1.1  riastrad 	dev_priv->skl_preferred_vco_freq = vco;
    941  1.1  riastrad 
    942  1.1  riastrad 	if (changed)
    943  1.1  riastrad 		intel_update_max_cdclk(dev_priv);
    944  1.1  riastrad }
    945  1.1  riastrad 
    946  1.1  riastrad static void skl_dpll0_enable(struct drm_i915_private *dev_priv, int vco)
    947  1.1  riastrad {
    948  1.1  riastrad 	u32 val;
    949  1.1  riastrad 
    950  1.1  riastrad 	WARN_ON(vco != 8100000 && vco != 8640000);
    951  1.1  riastrad 
    952  1.1  riastrad 	/*
    953  1.1  riastrad 	 * We always enable DPLL0 with the lowest link rate possible, but still
    954  1.1  riastrad 	 * taking into account the VCO required to operate the eDP panel at the
    955  1.1  riastrad 	 * desired frequency. The usual DP link rates operate with a VCO of
    956  1.1  riastrad 	 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
    957  1.1  riastrad 	 * The modeset code is responsible for the selection of the exact link
    958  1.1  riastrad 	 * rate later on, with the constraint of choosing a frequency that
    959  1.1  riastrad 	 * works with vco.
    960  1.1  riastrad 	 */
    961  1.1  riastrad 	val = I915_READ(DPLL_CTRL1);
    962  1.1  riastrad 
    963  1.1  riastrad 	val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
    964  1.1  riastrad 		 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
    965  1.1  riastrad 	val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
    966  1.1  riastrad 	if (vco == 8640000)
    967  1.1  riastrad 		val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
    968  1.1  riastrad 					    SKL_DPLL0);
    969  1.1  riastrad 	else
    970  1.1  riastrad 		val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
    971  1.1  riastrad 					    SKL_DPLL0);
    972  1.1  riastrad 
    973  1.1  riastrad 	I915_WRITE(DPLL_CTRL1, val);
    974  1.1  riastrad 	POSTING_READ(DPLL_CTRL1);
    975  1.1  riastrad 
    976  1.1  riastrad 	I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
    977  1.1  riastrad 
    978  1.1  riastrad 	if (intel_de_wait_for_set(dev_priv, LCPLL1_CTL, LCPLL_PLL_LOCK, 5))
    979  1.1  riastrad 		DRM_ERROR("DPLL0 not locked\n");
    980  1.1  riastrad 
    981  1.1  riastrad 	dev_priv->cdclk.hw.vco = vco;
    982  1.1  riastrad 
    983  1.1  riastrad 	/* We'll want to keep using the current vco from now on. */
    984  1.1  riastrad 	skl_set_preferred_cdclk_vco(dev_priv, vco);
    985  1.1  riastrad }
    986  1.1  riastrad 
    987  1.1  riastrad static void skl_dpll0_disable(struct drm_i915_private *dev_priv)
    988  1.1  riastrad {
    989  1.1  riastrad 	I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
    990  1.1  riastrad 	if (intel_de_wait_for_clear(dev_priv, LCPLL1_CTL, LCPLL_PLL_LOCK, 1))
    991  1.1  riastrad 		DRM_ERROR("Couldn't disable DPLL0\n");
    992  1.1  riastrad 
    993  1.1  riastrad 	dev_priv->cdclk.hw.vco = 0;
    994  1.1  riastrad }
    995  1.1  riastrad 
    996  1.1  riastrad static void skl_set_cdclk(struct drm_i915_private *dev_priv,
    997  1.1  riastrad 			  const struct intel_cdclk_state *cdclk_state,
    998  1.1  riastrad 			  enum pipe pipe)
    999  1.1  riastrad {
   1000  1.1  riastrad 	int cdclk = cdclk_state->cdclk;
   1001  1.1  riastrad 	int vco = cdclk_state->vco;
   1002  1.1  riastrad 	u32 freq_select, cdclk_ctl;
   1003  1.1  riastrad 	int ret;
   1004  1.1  riastrad 
   1005  1.1  riastrad 	/*
   1006  1.1  riastrad 	 * Based on WA#1183 CDCLK rates 308 and 617MHz CDCLK rates are
   1007  1.1  riastrad 	 * unsupported on SKL. In theory this should never happen since only
   1008  1.1  riastrad 	 * the eDP1.4 2.16 and 4.32Gbps rates require it, but eDP1.4 is not
   1009  1.1  riastrad 	 * supported on SKL either, see the above WA. WARN whenever trying to
   1010  1.1  riastrad 	 * use the corresponding VCO freq as that always leads to using the
   1011  1.1  riastrad 	 * minimum 308MHz CDCLK.
   1012  1.1  riastrad 	 */
   1013  1.1  riastrad 	WARN_ON_ONCE(IS_SKYLAKE(dev_priv) && vco == 8640000);
   1014  1.1  riastrad 
   1015  1.1  riastrad 	ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
   1016  1.1  riastrad 				SKL_CDCLK_PREPARE_FOR_CHANGE,
   1017  1.1  riastrad 				SKL_CDCLK_READY_FOR_CHANGE,
   1018  1.1  riastrad 				SKL_CDCLK_READY_FOR_CHANGE, 3);
   1019  1.1  riastrad 	if (ret) {
   1020  1.1  riastrad 		DRM_ERROR("Failed to inform PCU about cdclk change (%d)\n",
   1021  1.1  riastrad 			  ret);
   1022  1.1  riastrad 		return;
   1023  1.1  riastrad 	}
   1024  1.1  riastrad 
   1025  1.1  riastrad 	/* Choose frequency for this cdclk */
   1026  1.1  riastrad 	switch (cdclk) {
   1027  1.1  riastrad 	default:
   1028  1.1  riastrad 		WARN_ON(cdclk != dev_priv->cdclk.hw.bypass);
   1029  1.1  riastrad 		WARN_ON(vco != 0);
   1030  1.1  riastrad 		/* fall through */
   1031  1.1  riastrad 	case 308571:
   1032  1.1  riastrad 	case 337500:
   1033  1.1  riastrad 		freq_select = CDCLK_FREQ_337_308;
   1034  1.1  riastrad 		break;
   1035  1.1  riastrad 	case 450000:
   1036  1.1  riastrad 	case 432000:
   1037  1.1  riastrad 		freq_select = CDCLK_FREQ_450_432;
   1038  1.1  riastrad 		break;
   1039  1.1  riastrad 	case 540000:
   1040  1.1  riastrad 		freq_select = CDCLK_FREQ_540;
   1041  1.1  riastrad 		break;
   1042  1.1  riastrad 	case 617143:
   1043  1.1  riastrad 	case 675000:
   1044  1.1  riastrad 		freq_select = CDCLK_FREQ_675_617;
   1045  1.1  riastrad 		break;
   1046  1.1  riastrad 	}
   1047  1.1  riastrad 
   1048  1.1  riastrad 	if (dev_priv->cdclk.hw.vco != 0 &&
   1049  1.1  riastrad 	    dev_priv->cdclk.hw.vco != vco)
   1050  1.1  riastrad 		skl_dpll0_disable(dev_priv);
   1051  1.1  riastrad 
   1052  1.1  riastrad 	cdclk_ctl = I915_READ(CDCLK_CTL);
   1053  1.1  riastrad 
   1054  1.1  riastrad 	if (dev_priv->cdclk.hw.vco != vco) {
   1055  1.1  riastrad 		/* Wa Display #1183: skl,kbl,cfl */
   1056  1.1  riastrad 		cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
   1057  1.1  riastrad 		cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
   1058  1.1  riastrad 		I915_WRITE(CDCLK_CTL, cdclk_ctl);
   1059  1.1  riastrad 	}
   1060  1.1  riastrad 
   1061  1.1  riastrad 	/* Wa Display #1183: skl,kbl,cfl */
   1062  1.1  riastrad 	cdclk_ctl |= CDCLK_DIVMUX_CD_OVERRIDE;
   1063  1.1  riastrad 	I915_WRITE(CDCLK_CTL, cdclk_ctl);
   1064  1.1  riastrad 	POSTING_READ(CDCLK_CTL);
   1065  1.1  riastrad 
   1066  1.1  riastrad 	if (dev_priv->cdclk.hw.vco != vco)
   1067  1.1  riastrad 		skl_dpll0_enable(dev_priv, vco);
   1068  1.1  riastrad 
   1069  1.1  riastrad 	/* Wa Display #1183: skl,kbl,cfl */
   1070  1.1  riastrad 	cdclk_ctl &= ~(CDCLK_FREQ_SEL_MASK | CDCLK_FREQ_DECIMAL_MASK);
   1071  1.1  riastrad 	I915_WRITE(CDCLK_CTL, cdclk_ctl);
   1072  1.1  riastrad 
   1073  1.1  riastrad 	cdclk_ctl |= freq_select | skl_cdclk_decimal(cdclk);
   1074  1.1  riastrad 	I915_WRITE(CDCLK_CTL, cdclk_ctl);
   1075  1.1  riastrad 
   1076  1.1  riastrad 	/* Wa Display #1183: skl,kbl,cfl */
   1077  1.1  riastrad 	cdclk_ctl &= ~CDCLK_DIVMUX_CD_OVERRIDE;
   1078  1.1  riastrad 	I915_WRITE(CDCLK_CTL, cdclk_ctl);
   1079  1.1  riastrad 	POSTING_READ(CDCLK_CTL);
   1080  1.1  riastrad 
   1081  1.1  riastrad 	/* inform PCU of the change */
   1082  1.1  riastrad 	sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
   1083  1.1  riastrad 				cdclk_state->voltage_level);
   1084  1.1  riastrad 
   1085  1.1  riastrad 	intel_update_cdclk(dev_priv);
   1086  1.1  riastrad }
   1087  1.1  riastrad 
   1088  1.1  riastrad static void skl_sanitize_cdclk(struct drm_i915_private *dev_priv)
   1089  1.1  riastrad {
   1090  1.1  riastrad 	u32 cdctl, expected;
   1091  1.1  riastrad 
   1092  1.1  riastrad 	/*
   1093  1.1  riastrad 	 * check if the pre-os initialized the display
   1094  1.1  riastrad 	 * There is SWF18 scratchpad register defined which is set by the
   1095  1.1  riastrad 	 * pre-os which can be used by the OS drivers to check the status
   1096  1.1  riastrad 	 */
   1097  1.1  riastrad 	if ((I915_READ(SWF_ILK(0x18)) & 0x00FFFFFF) == 0)
   1098  1.1  riastrad 		goto sanitize;
   1099  1.1  riastrad 
   1100  1.1  riastrad 	intel_update_cdclk(dev_priv);
   1101  1.1  riastrad 	intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
   1102  1.1  riastrad 
   1103  1.1  riastrad 	/* Is PLL enabled and locked ? */
   1104  1.1  riastrad 	if (dev_priv->cdclk.hw.vco == 0 ||
   1105  1.1  riastrad 	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
   1106  1.1  riastrad 		goto sanitize;
   1107  1.1  riastrad 
   1108  1.1  riastrad 	/* DPLL okay; verify the cdclock
   1109  1.1  riastrad 	 *
   1110  1.1  riastrad 	 * Noticed in some instances that the freq selection is correct but
   1111  1.1  riastrad 	 * decimal part is programmed wrong from BIOS where pre-os does not
   1112  1.1  riastrad 	 * enable display. Verify the same as well.
   1113  1.1  riastrad 	 */
   1114  1.1  riastrad 	cdctl = I915_READ(CDCLK_CTL);
   1115  1.1  riastrad 	expected = (cdctl & CDCLK_FREQ_SEL_MASK) |
   1116  1.1  riastrad 		skl_cdclk_decimal(dev_priv->cdclk.hw.cdclk);
   1117  1.1  riastrad 	if (cdctl == expected)
   1118  1.1  riastrad 		/* All well; nothing to sanitize */
   1119  1.1  riastrad 		return;
   1120  1.1  riastrad 
   1121  1.1  riastrad sanitize:
   1122  1.1  riastrad 	DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
   1123  1.1  riastrad 
   1124  1.1  riastrad 	/* force cdclk programming */
   1125  1.1  riastrad 	dev_priv->cdclk.hw.cdclk = 0;
   1126  1.1  riastrad 	/* force full PLL disable + enable */
   1127  1.1  riastrad 	dev_priv->cdclk.hw.vco = -1;
   1128  1.1  riastrad }
   1129  1.1  riastrad 
   1130  1.1  riastrad static void skl_init_cdclk(struct drm_i915_private *dev_priv)
   1131  1.1  riastrad {
   1132  1.1  riastrad 	struct intel_cdclk_state cdclk_state;
   1133  1.1  riastrad 
   1134  1.1  riastrad 	skl_sanitize_cdclk(dev_priv);
   1135  1.1  riastrad 
   1136  1.1  riastrad 	if (dev_priv->cdclk.hw.cdclk != 0 &&
   1137  1.1  riastrad 	    dev_priv->cdclk.hw.vco != 0) {
   1138  1.1  riastrad 		/*
   1139  1.1  riastrad 		 * Use the current vco as our initial
   1140  1.1  riastrad 		 * guess as to what the preferred vco is.
   1141  1.1  riastrad 		 */
   1142  1.1  riastrad 		if (dev_priv->skl_preferred_vco_freq == 0)
   1143  1.1  riastrad 			skl_set_preferred_cdclk_vco(dev_priv,
   1144  1.1  riastrad 						    dev_priv->cdclk.hw.vco);
   1145  1.1  riastrad 		return;
   1146  1.1  riastrad 	}
   1147  1.1  riastrad 
   1148  1.1  riastrad 	cdclk_state = dev_priv->cdclk.hw;
   1149  1.1  riastrad 
   1150  1.1  riastrad 	cdclk_state.vco = dev_priv->skl_preferred_vco_freq;
   1151  1.1  riastrad 	if (cdclk_state.vco == 0)
   1152  1.1  riastrad 		cdclk_state.vco = 8100000;
   1153  1.1  riastrad 	cdclk_state.cdclk = skl_calc_cdclk(0, cdclk_state.vco);
   1154  1.1  riastrad 	cdclk_state.voltage_level = skl_calc_voltage_level(cdclk_state.cdclk);
   1155  1.1  riastrad 
   1156  1.1  riastrad 	skl_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
   1157  1.1  riastrad }
   1158  1.1  riastrad 
   1159  1.1  riastrad static void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
   1160  1.1  riastrad {
   1161  1.1  riastrad 	struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;
   1162  1.1  riastrad 
   1163  1.1  riastrad 	cdclk_state.cdclk = cdclk_state.bypass;
   1164  1.1  riastrad 	cdclk_state.vco = 0;
   1165  1.1  riastrad 	cdclk_state.voltage_level = skl_calc_voltage_level(cdclk_state.cdclk);
   1166  1.1  riastrad 
   1167  1.1  riastrad 	skl_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
   1168  1.1  riastrad }
   1169  1.1  riastrad 
   1170  1.1  riastrad static const struct intel_cdclk_vals bxt_cdclk_table[] = {
   1171  1.1  riastrad 	{ .refclk = 19200, .cdclk = 144000, .divider = 8, .ratio = 60 },
   1172  1.1  riastrad 	{ .refclk = 19200, .cdclk = 288000, .divider = 4, .ratio = 60 },
   1173  1.1  riastrad 	{ .refclk = 19200, .cdclk = 384000, .divider = 3, .ratio = 60 },
   1174  1.1  riastrad 	{ .refclk = 19200, .cdclk = 576000, .divider = 2, .ratio = 60 },
   1175  1.1  riastrad 	{ .refclk = 19200, .cdclk = 624000, .divider = 2, .ratio = 65 },
   1176  1.1  riastrad 	{}
   1177  1.1  riastrad };
   1178  1.1  riastrad 
   1179  1.1  riastrad static const struct intel_cdclk_vals glk_cdclk_table[] = {
   1180  1.1  riastrad 	{ .refclk = 19200, .cdclk =  79200, .divider = 8, .ratio = 33 },
   1181  1.1  riastrad 	{ .refclk = 19200, .cdclk = 158400, .divider = 4, .ratio = 33 },
   1182  1.1  riastrad 	{ .refclk = 19200, .cdclk = 316800, .divider = 2, .ratio = 33 },
   1183  1.1  riastrad 	{}
   1184  1.1  riastrad };
   1185  1.1  riastrad 
   1186  1.1  riastrad static const struct intel_cdclk_vals cnl_cdclk_table[] = {
   1187  1.1  riastrad 	{ .refclk = 19200, .cdclk = 168000, .divider = 4, .ratio = 35 },
   1188  1.1  riastrad 	{ .refclk = 19200, .cdclk = 336000, .divider = 2, .ratio = 35 },
   1189  1.1  riastrad 	{ .refclk = 19200, .cdclk = 528000, .divider = 2, .ratio = 55 },
   1190  1.1  riastrad 
   1191  1.1  riastrad 	{ .refclk = 24000, .cdclk = 168000, .divider = 4, .ratio = 28 },
   1192  1.1  riastrad 	{ .refclk = 24000, .cdclk = 336000, .divider = 2, .ratio = 28 },
   1193  1.1  riastrad 	{ .refclk = 24000, .cdclk = 528000, .divider = 2, .ratio = 44 },
   1194  1.1  riastrad 	{}
   1195  1.1  riastrad };
   1196  1.1  riastrad 
   1197  1.1  riastrad static const struct intel_cdclk_vals icl_cdclk_table[] = {
   1198  1.1  riastrad 	{ .refclk = 19200, .cdclk = 172800, .divider = 2, .ratio = 18 },
   1199  1.1  riastrad 	{ .refclk = 19200, .cdclk = 192000, .divider = 2, .ratio = 20 },
   1200  1.1  riastrad 	{ .refclk = 19200, .cdclk = 307200, .divider = 2, .ratio = 32 },
   1201  1.1  riastrad 	{ .refclk = 19200, .cdclk = 326400, .divider = 4, .ratio = 68 },
   1202  1.1  riastrad 	{ .refclk = 19200, .cdclk = 556800, .divider = 2, .ratio = 58 },
   1203  1.1  riastrad 	{ .refclk = 19200, .cdclk = 652800, .divider = 2, .ratio = 68 },
   1204  1.1  riastrad 
   1205  1.1  riastrad 	{ .refclk = 24000, .cdclk = 180000, .divider = 2, .ratio = 15 },
   1206  1.1  riastrad 	{ .refclk = 24000, .cdclk = 192000, .divider = 2, .ratio = 16 },
   1207  1.1  riastrad 	{ .refclk = 24000, .cdclk = 312000, .divider = 2, .ratio = 26 },
   1208  1.1  riastrad 	{ .refclk = 24000, .cdclk = 324000, .divider = 4, .ratio = 54 },
   1209  1.1  riastrad 	{ .refclk = 24000, .cdclk = 552000, .divider = 2, .ratio = 46 },
   1210  1.1  riastrad 	{ .refclk = 24000, .cdclk = 648000, .divider = 2, .ratio = 54 },
   1211  1.1  riastrad 
   1212  1.1  riastrad 	{ .refclk = 38400, .cdclk = 172800, .divider = 2, .ratio =  9 },
   1213  1.1  riastrad 	{ .refclk = 38400, .cdclk = 192000, .divider = 2, .ratio = 10 },
   1214  1.1  riastrad 	{ .refclk = 38400, .cdclk = 307200, .divider = 2, .ratio = 16 },
   1215  1.1  riastrad 	{ .refclk = 38400, .cdclk = 326400, .divider = 4, .ratio = 34 },
   1216  1.1  riastrad 	{ .refclk = 38400, .cdclk = 556800, .divider = 2, .ratio = 29 },
   1217  1.1  riastrad 	{ .refclk = 38400, .cdclk = 652800, .divider = 2, .ratio = 34 },
   1218  1.1  riastrad 	{}
   1219  1.1  riastrad };
   1220  1.1  riastrad 
   1221  1.1  riastrad static int bxt_calc_cdclk(struct drm_i915_private *dev_priv, int min_cdclk)
   1222  1.1  riastrad {
   1223  1.1  riastrad 	const struct intel_cdclk_vals *table = dev_priv->cdclk.table;
   1224  1.1  riastrad 	int i;
   1225  1.1  riastrad 
   1226  1.1  riastrad 	for (i = 0; table[i].refclk; i++)
   1227  1.1  riastrad 		if (table[i].refclk == dev_priv->cdclk.hw.ref &&
   1228  1.1  riastrad 		    table[i].cdclk >= min_cdclk)
   1229  1.1  riastrad 			return table[i].cdclk;
   1230  1.1  riastrad 
   1231  1.1  riastrad 	WARN(1, "Cannot satisfy minimum cdclk %d with refclk %u\n",
   1232  1.1  riastrad 	     min_cdclk, dev_priv->cdclk.hw.ref);
   1233  1.1  riastrad 	return 0;
   1234  1.1  riastrad }
   1235  1.1  riastrad 
   1236  1.1  riastrad static int bxt_calc_cdclk_pll_vco(struct drm_i915_private *dev_priv, int cdclk)
   1237  1.1  riastrad {
   1238  1.1  riastrad 	const struct intel_cdclk_vals *table = dev_priv->cdclk.table;
   1239  1.1  riastrad 	int i;
   1240  1.1  riastrad 
   1241  1.1  riastrad 	if (cdclk == dev_priv->cdclk.hw.bypass)
   1242  1.1  riastrad 		return 0;
   1243  1.1  riastrad 
   1244  1.1  riastrad 	for (i = 0; table[i].refclk; i++)
   1245  1.1  riastrad 		if (table[i].refclk == dev_priv->cdclk.hw.ref &&
   1246  1.1  riastrad 		    table[i].cdclk == cdclk)
   1247  1.1  riastrad 			return dev_priv->cdclk.hw.ref * table[i].ratio;
   1248  1.1  riastrad 
   1249  1.1  riastrad 	WARN(1, "cdclk %d not valid for refclk %u\n",
   1250  1.1  riastrad 	     cdclk, dev_priv->cdclk.hw.ref);
   1251  1.1  riastrad 	return 0;
   1252  1.1  riastrad }
   1253  1.1  riastrad 
   1254  1.1  riastrad static u8 bxt_calc_voltage_level(int cdclk)
   1255  1.1  riastrad {
   1256  1.1  riastrad 	return DIV_ROUND_UP(cdclk, 25000);
   1257  1.1  riastrad }
   1258  1.1  riastrad 
   1259  1.1  riastrad static u8 cnl_calc_voltage_level(int cdclk)
   1260  1.1  riastrad {
   1261  1.1  riastrad 	if (cdclk > 336000)
   1262  1.1  riastrad 		return 2;
   1263  1.1  riastrad 	else if (cdclk > 168000)
   1264  1.1  riastrad 		return 1;
   1265  1.1  riastrad 	else
   1266  1.1  riastrad 		return 0;
   1267  1.1  riastrad }
   1268  1.1  riastrad 
   1269  1.1  riastrad static u8 icl_calc_voltage_level(int cdclk)
   1270  1.1  riastrad {
   1271  1.1  riastrad 	if (cdclk > 556800)
   1272  1.1  riastrad 		return 2;
   1273  1.1  riastrad 	else if (cdclk > 312000)
   1274  1.1  riastrad 		return 1;
   1275  1.1  riastrad 	else
   1276  1.1  riastrad 		return 0;
   1277  1.1  riastrad }
   1278  1.1  riastrad 
   1279  1.1  riastrad static u8 ehl_calc_voltage_level(int cdclk)
   1280  1.1  riastrad {
   1281  1.1  riastrad 	if (cdclk > 326400)
   1282  1.1  riastrad 		return 3;
   1283  1.1  riastrad 	else if (cdclk > 312000)
   1284  1.1  riastrad 		return 2;
   1285  1.1  riastrad 	else if (cdclk > 180000)
   1286  1.1  riastrad 		return 1;
   1287  1.1  riastrad 	else
   1288  1.1  riastrad 		return 0;
   1289  1.1  riastrad }
   1290  1.1  riastrad 
   1291  1.1  riastrad static void cnl_readout_refclk(struct drm_i915_private *dev_priv,
   1292  1.1  riastrad 			       struct intel_cdclk_state *cdclk_state)
   1293  1.1  riastrad {
   1294  1.1  riastrad 	if (I915_READ(SKL_DSSM) & CNL_DSSM_CDCLK_PLL_REFCLK_24MHz)
   1295  1.1  riastrad 		cdclk_state->ref = 24000;
   1296  1.1  riastrad 	else
   1297  1.1  riastrad 		cdclk_state->ref = 19200;
   1298  1.1  riastrad }
   1299  1.1  riastrad 
   1300  1.1  riastrad static void icl_readout_refclk(struct drm_i915_private *dev_priv,
   1301  1.1  riastrad 			       struct intel_cdclk_state *cdclk_state)
   1302  1.1  riastrad {
   1303  1.1  riastrad 	u32 dssm = I915_READ(SKL_DSSM) & ICL_DSSM_CDCLK_PLL_REFCLK_MASK;
   1304  1.1  riastrad 
   1305  1.1  riastrad 	switch (dssm) {
   1306  1.1  riastrad 	default:
   1307  1.1  riastrad 		MISSING_CASE(dssm);
   1308  1.1  riastrad 		/* fall through */
   1309  1.1  riastrad 	case ICL_DSSM_CDCLK_PLL_REFCLK_24MHz:
   1310  1.1  riastrad 		cdclk_state->ref = 24000;
   1311  1.1  riastrad 		break;
   1312  1.1  riastrad 	case ICL_DSSM_CDCLK_PLL_REFCLK_19_2MHz:
   1313  1.1  riastrad 		cdclk_state->ref = 19200;
   1314  1.1  riastrad 		break;
   1315  1.1  riastrad 	case ICL_DSSM_CDCLK_PLL_REFCLK_38_4MHz:
   1316  1.1  riastrad 		cdclk_state->ref = 38400;
   1317  1.1  riastrad 		break;
   1318  1.1  riastrad 	}
   1319  1.1  riastrad }
   1320  1.1  riastrad 
   1321  1.1  riastrad static void bxt_de_pll_readout(struct drm_i915_private *dev_priv,
   1322  1.1  riastrad 			       struct intel_cdclk_state *cdclk_state)
   1323  1.1  riastrad {
   1324  1.1  riastrad 	u32 val, ratio;
   1325  1.1  riastrad 
   1326  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 11)
   1327  1.1  riastrad 		icl_readout_refclk(dev_priv, cdclk_state);
   1328  1.1  riastrad 	else if (IS_CANNONLAKE(dev_priv))
   1329  1.1  riastrad 		cnl_readout_refclk(dev_priv, cdclk_state);
   1330  1.1  riastrad 	else
   1331  1.1  riastrad 		cdclk_state->ref = 19200;
   1332  1.1  riastrad 
   1333  1.1  riastrad 	val = I915_READ(BXT_DE_PLL_ENABLE);
   1334  1.1  riastrad 	if ((val & BXT_DE_PLL_PLL_ENABLE) == 0 ||
   1335  1.1  riastrad 	    (val & BXT_DE_PLL_LOCK) == 0) {
   1336  1.1  riastrad 		/*
   1337  1.1  riastrad 		 * CDCLK PLL is disabled, the VCO/ratio doesn't matter, but
   1338  1.1  riastrad 		 * setting it to zero is a way to signal that.
   1339  1.1  riastrad 		 */
   1340  1.1  riastrad 		cdclk_state->vco = 0;
   1341  1.1  riastrad 		return;
   1342  1.1  riastrad 	}
   1343  1.1  riastrad 
   1344  1.1  riastrad 	/*
   1345  1.1  riastrad 	 * CNL+ have the ratio directly in the PLL enable register, gen9lp had
   1346  1.1  riastrad 	 * it in a separate PLL control register.
   1347  1.1  riastrad 	 */
   1348  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10)
   1349  1.1  riastrad 		ratio = val & CNL_CDCLK_PLL_RATIO_MASK;
   1350  1.1  riastrad 	else
   1351  1.1  riastrad 		ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
   1352  1.1  riastrad 
   1353  1.1  riastrad 	cdclk_state->vco = ratio * cdclk_state->ref;
   1354  1.1  riastrad }
   1355  1.1  riastrad 
   1356  1.1  riastrad static void bxt_get_cdclk(struct drm_i915_private *dev_priv,
   1357  1.1  riastrad 			  struct intel_cdclk_state *cdclk_state)
   1358  1.1  riastrad {
   1359  1.1  riastrad 	u32 divider;
   1360  1.1  riastrad 	int div;
   1361  1.1  riastrad 
   1362  1.1  riastrad 	bxt_de_pll_readout(dev_priv, cdclk_state);
   1363  1.1  riastrad 
   1364  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 12)
   1365  1.1  riastrad 		cdclk_state->bypass = cdclk_state->ref / 2;
   1366  1.1  riastrad 	else if (INTEL_GEN(dev_priv) >= 11)
   1367  1.1  riastrad 		cdclk_state->bypass = 50000;
   1368  1.1  riastrad 	else
   1369  1.1  riastrad 		cdclk_state->bypass = cdclk_state->ref;
   1370  1.1  riastrad 
   1371  1.1  riastrad 	if (cdclk_state->vco == 0) {
   1372  1.1  riastrad 		cdclk_state->cdclk = cdclk_state->bypass;
   1373  1.1  riastrad 		goto out;
   1374  1.1  riastrad 	}
   1375  1.1  riastrad 
   1376  1.1  riastrad 	divider = I915_READ(CDCLK_CTL) & BXT_CDCLK_CD2X_DIV_SEL_MASK;
   1377  1.1  riastrad 
   1378  1.1  riastrad 	switch (divider) {
   1379  1.1  riastrad 	case BXT_CDCLK_CD2X_DIV_SEL_1:
   1380  1.1  riastrad 		div = 2;
   1381  1.1  riastrad 		break;
   1382  1.1  riastrad 	case BXT_CDCLK_CD2X_DIV_SEL_1_5:
   1383  1.1  riastrad 		WARN(IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10,
   1384  1.1  riastrad 		     "Unsupported divider\n");
   1385  1.1  riastrad 		div = 3;
   1386  1.1  riastrad 		break;
   1387  1.1  riastrad 	case BXT_CDCLK_CD2X_DIV_SEL_2:
   1388  1.1  riastrad 		div = 4;
   1389  1.1  riastrad 		break;
   1390  1.1  riastrad 	case BXT_CDCLK_CD2X_DIV_SEL_4:
   1391  1.1  riastrad 		WARN(INTEL_GEN(dev_priv) >= 10, "Unsupported divider\n");
   1392  1.1  riastrad 		div = 8;
   1393  1.1  riastrad 		break;
   1394  1.1  riastrad 	default:
   1395  1.1  riastrad 		MISSING_CASE(divider);
   1396  1.1  riastrad 		return;
   1397  1.1  riastrad 	}
   1398  1.1  riastrad 
   1399  1.1  riastrad 	cdclk_state->cdclk = DIV_ROUND_CLOSEST(cdclk_state->vco, div);
   1400  1.1  riastrad 
   1401  1.1  riastrad  out:
   1402  1.1  riastrad 	/*
   1403  1.1  riastrad 	 * Can't read this out :( Let's assume it's
   1404  1.1  riastrad 	 * at least what the CDCLK frequency requires.
   1405  1.1  riastrad 	 */
   1406  1.1  riastrad 	cdclk_state->voltage_level =
   1407  1.1  riastrad 		dev_priv->display.calc_voltage_level(cdclk_state->cdclk);
   1408  1.1  riastrad }
   1409  1.1  riastrad 
   1410  1.1  riastrad static void bxt_de_pll_disable(struct drm_i915_private *dev_priv)
   1411  1.1  riastrad {
   1412  1.1  riastrad 	I915_WRITE(BXT_DE_PLL_ENABLE, 0);
   1413  1.1  riastrad 
   1414  1.1  riastrad 	/* Timeout 200us */
   1415  1.1  riastrad 	if (intel_de_wait_for_clear(dev_priv,
   1416  1.1  riastrad 				    BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
   1417  1.1  riastrad 		DRM_ERROR("timeout waiting for DE PLL unlock\n");
   1418  1.1  riastrad 
   1419  1.1  riastrad 	dev_priv->cdclk.hw.vco = 0;
   1420  1.1  riastrad }
   1421  1.1  riastrad 
   1422  1.1  riastrad static void bxt_de_pll_enable(struct drm_i915_private *dev_priv, int vco)
   1423  1.1  riastrad {
   1424  1.1  riastrad 	int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk.hw.ref);
   1425  1.1  riastrad 	u32 val;
   1426  1.1  riastrad 
   1427  1.1  riastrad 	val = I915_READ(BXT_DE_PLL_CTL);
   1428  1.1  riastrad 	val &= ~BXT_DE_PLL_RATIO_MASK;
   1429  1.1  riastrad 	val |= BXT_DE_PLL_RATIO(ratio);
   1430  1.1  riastrad 	I915_WRITE(BXT_DE_PLL_CTL, val);
   1431  1.1  riastrad 
   1432  1.1  riastrad 	I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
   1433  1.1  riastrad 
   1434  1.1  riastrad 	/* Timeout 200us */
   1435  1.1  riastrad 	if (intel_de_wait_for_set(dev_priv,
   1436  1.1  riastrad 				  BXT_DE_PLL_ENABLE, BXT_DE_PLL_LOCK, 1))
   1437  1.1  riastrad 		DRM_ERROR("timeout waiting for DE PLL lock\n");
   1438  1.1  riastrad 
   1439  1.1  riastrad 	dev_priv->cdclk.hw.vco = vco;
   1440  1.1  riastrad }
   1441  1.1  riastrad 
   1442  1.1  riastrad static void cnl_cdclk_pll_disable(struct drm_i915_private *dev_priv)
   1443  1.1  riastrad {
   1444  1.1  riastrad 	u32 val;
   1445  1.1  riastrad 
   1446  1.1  riastrad 	val = I915_READ(BXT_DE_PLL_ENABLE);
   1447  1.1  riastrad 	val &= ~BXT_DE_PLL_PLL_ENABLE;
   1448  1.1  riastrad 	I915_WRITE(BXT_DE_PLL_ENABLE, val);
   1449  1.1  riastrad 
   1450  1.1  riastrad 	/* Timeout 200us */
   1451  1.1  riastrad 	if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) == 0, 1))
   1452  1.1  riastrad 		DRM_ERROR("timeout waiting for CDCLK PLL unlock\n");
   1453  1.1  riastrad 
   1454  1.1  riastrad 	dev_priv->cdclk.hw.vco = 0;
   1455  1.1  riastrad }
   1456  1.1  riastrad 
   1457  1.1  riastrad static void cnl_cdclk_pll_enable(struct drm_i915_private *dev_priv, int vco)
   1458  1.1  riastrad {
   1459  1.1  riastrad 	int ratio = DIV_ROUND_CLOSEST(vco, dev_priv->cdclk.hw.ref);
   1460  1.1  riastrad 	u32 val;
   1461  1.1  riastrad 
   1462  1.1  riastrad 	val = CNL_CDCLK_PLL_RATIO(ratio);
   1463  1.1  riastrad 	I915_WRITE(BXT_DE_PLL_ENABLE, val);
   1464  1.1  riastrad 
   1465  1.1  riastrad 	val |= BXT_DE_PLL_PLL_ENABLE;
   1466  1.1  riastrad 	I915_WRITE(BXT_DE_PLL_ENABLE, val);
   1467  1.1  riastrad 
   1468  1.1  riastrad 	/* Timeout 200us */
   1469  1.1  riastrad 	if (wait_for((I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK) != 0, 1))
   1470  1.1  riastrad 		DRM_ERROR("timeout waiting for CDCLK PLL lock\n");
   1471  1.1  riastrad 
   1472  1.1  riastrad 	dev_priv->cdclk.hw.vco = vco;
   1473  1.1  riastrad }
   1474  1.1  riastrad 
   1475  1.1  riastrad static u32 bxt_cdclk_cd2x_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
   1476  1.1  riastrad {
   1477  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 12) {
   1478  1.1  riastrad 		if (pipe == INVALID_PIPE)
   1479  1.1  riastrad 			return TGL_CDCLK_CD2X_PIPE_NONE;
   1480  1.1  riastrad 		else
   1481  1.1  riastrad 			return TGL_CDCLK_CD2X_PIPE(pipe);
   1482  1.1  riastrad 	} else if (INTEL_GEN(dev_priv) >= 11) {
   1483  1.1  riastrad 		if (pipe == INVALID_PIPE)
   1484  1.1  riastrad 			return ICL_CDCLK_CD2X_PIPE_NONE;
   1485  1.1  riastrad 		else
   1486  1.1  riastrad 			return ICL_CDCLK_CD2X_PIPE(pipe);
   1487  1.1  riastrad 	} else {
   1488  1.1  riastrad 		if (pipe == INVALID_PIPE)
   1489  1.1  riastrad 			return BXT_CDCLK_CD2X_PIPE_NONE;
   1490  1.1  riastrad 		else
   1491  1.1  riastrad 			return BXT_CDCLK_CD2X_PIPE(pipe);
   1492  1.1  riastrad 	}
   1493  1.1  riastrad }
   1494  1.1  riastrad 
   1495  1.1  riastrad static void bxt_set_cdclk(struct drm_i915_private *dev_priv,
   1496  1.1  riastrad 			  const struct intel_cdclk_state *cdclk_state,
   1497  1.1  riastrad 			  enum pipe pipe)
   1498  1.1  riastrad {
   1499  1.1  riastrad 	int cdclk = cdclk_state->cdclk;
   1500  1.1  riastrad 	int vco = cdclk_state->vco;
   1501  1.1  riastrad 	u32 val, divider;
   1502  1.1  riastrad 	int ret;
   1503  1.1  riastrad 
   1504  1.1  riastrad 	/* Inform power controller of upcoming frequency change. */
   1505  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10)
   1506  1.1  riastrad 		ret = skl_pcode_request(dev_priv, SKL_PCODE_CDCLK_CONTROL,
   1507  1.1  riastrad 					SKL_CDCLK_PREPARE_FOR_CHANGE,
   1508  1.1  riastrad 					SKL_CDCLK_READY_FOR_CHANGE,
   1509  1.1  riastrad 					SKL_CDCLK_READY_FOR_CHANGE, 3);
   1510  1.1  riastrad 	else
   1511  1.1  riastrad 		/*
   1512  1.1  riastrad 		 * BSpec requires us to wait up to 150usec, but that leads to
   1513  1.1  riastrad 		 * timeouts; the 2ms used here is based on experiment.
   1514  1.1  riastrad 		 */
   1515  1.1  riastrad 		ret = sandybridge_pcode_write_timeout(dev_priv,
   1516  1.1  riastrad 						      HSW_PCODE_DE_WRITE_FREQ_REQ,
   1517  1.1  riastrad 						      0x80000000, 150, 2);
   1518  1.1  riastrad 
   1519  1.1  riastrad 	if (ret) {
   1520  1.1  riastrad 		DRM_ERROR("Failed to inform PCU about cdclk change (err %d, freq %d)\n",
   1521  1.1  riastrad 			  ret, cdclk);
   1522  1.1  riastrad 		return;
   1523  1.1  riastrad 	}
   1524  1.1  riastrad 
   1525  1.1  riastrad 	/* cdclk = vco / 2 / div{1,1.5,2,4} */
   1526  1.1  riastrad 	switch (DIV_ROUND_CLOSEST(vco, cdclk)) {
   1527  1.1  riastrad 	default:
   1528  1.1  riastrad 		WARN_ON(cdclk != dev_priv->cdclk.hw.bypass);
   1529  1.1  riastrad 		WARN_ON(vco != 0);
   1530  1.1  riastrad 		/* fall through */
   1531  1.1  riastrad 	case 2:
   1532  1.1  riastrad 		divider = BXT_CDCLK_CD2X_DIV_SEL_1;
   1533  1.1  riastrad 		break;
   1534  1.1  riastrad 	case 3:
   1535  1.1  riastrad 		WARN(IS_GEMINILAKE(dev_priv) || INTEL_GEN(dev_priv) >= 10,
   1536  1.1  riastrad 		     "Unsupported divider\n");
   1537  1.1  riastrad 		divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
   1538  1.1  riastrad 		break;
   1539  1.1  riastrad 	case 4:
   1540  1.1  riastrad 		divider = BXT_CDCLK_CD2X_DIV_SEL_2;
   1541  1.1  riastrad 		break;
   1542  1.1  riastrad 	case 8:
   1543  1.1  riastrad 		WARN(INTEL_GEN(dev_priv) >= 10, "Unsupported divider\n");
   1544  1.1  riastrad 		divider = BXT_CDCLK_CD2X_DIV_SEL_4;
   1545  1.1  riastrad 		break;
   1546  1.1  riastrad 	}
   1547  1.1  riastrad 
   1548  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10) {
   1549  1.1  riastrad 		if (dev_priv->cdclk.hw.vco != 0 &&
   1550  1.1  riastrad 		    dev_priv->cdclk.hw.vco != vco)
   1551  1.1  riastrad 			cnl_cdclk_pll_disable(dev_priv);
   1552  1.1  riastrad 
   1553  1.1  riastrad 		if (dev_priv->cdclk.hw.vco != vco)
   1554  1.1  riastrad 			cnl_cdclk_pll_enable(dev_priv, vco);
   1555  1.1  riastrad 
   1556  1.1  riastrad 	} else {
   1557  1.1  riastrad 		if (dev_priv->cdclk.hw.vco != 0 &&
   1558  1.1  riastrad 		    dev_priv->cdclk.hw.vco != vco)
   1559  1.1  riastrad 			bxt_de_pll_disable(dev_priv);
   1560  1.1  riastrad 
   1561  1.1  riastrad 		if (dev_priv->cdclk.hw.vco != vco)
   1562  1.1  riastrad 			bxt_de_pll_enable(dev_priv, vco);
   1563  1.1  riastrad 	}
   1564  1.1  riastrad 
   1565  1.1  riastrad 	val = divider | skl_cdclk_decimal(cdclk) |
   1566  1.1  riastrad 		bxt_cdclk_cd2x_pipe(dev_priv, pipe);
   1567  1.1  riastrad 
   1568  1.1  riastrad 	/*
   1569  1.1  riastrad 	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
   1570  1.1  riastrad 	 * enable otherwise.
   1571  1.1  riastrad 	 */
   1572  1.1  riastrad 	if (IS_GEN9_LP(dev_priv) && cdclk >= 500000)
   1573  1.1  riastrad 		val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
   1574  1.1  riastrad 	I915_WRITE(CDCLK_CTL, val);
   1575  1.1  riastrad 
   1576  1.1  riastrad 	if (pipe != INVALID_PIPE)
   1577  1.1  riastrad 		intel_wait_for_vblank(dev_priv, pipe);
   1578  1.1  riastrad 
   1579  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10) {
   1580  1.1  riastrad 		ret = sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL,
   1581  1.1  riastrad 					      cdclk_state->voltage_level);
   1582  1.1  riastrad 	} else {
   1583  1.1  riastrad 		/*
   1584  1.1  riastrad 		 * The timeout isn't specified, the 2ms used here is based on
   1585  1.1  riastrad 		 * experiment.
   1586  1.1  riastrad 		 * FIXME: Waiting for the request completion could be delayed
   1587  1.1  riastrad 		 * until the next PCODE request based on BSpec.
   1588  1.1  riastrad 		 */
   1589  1.1  riastrad 		ret = sandybridge_pcode_write_timeout(dev_priv,
   1590  1.1  riastrad 						      HSW_PCODE_DE_WRITE_FREQ_REQ,
   1591  1.1  riastrad 						      cdclk_state->voltage_level,
   1592  1.1  riastrad 						      150, 2);
   1593  1.1  riastrad 	}
   1594  1.1  riastrad 
   1595  1.1  riastrad 	if (ret) {
   1596  1.1  riastrad 		DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
   1597  1.1  riastrad 			  ret, cdclk);
   1598  1.1  riastrad 		return;
   1599  1.1  riastrad 	}
   1600  1.1  riastrad 
   1601  1.1  riastrad 	intel_update_cdclk(dev_priv);
   1602  1.1  riastrad 
   1603  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10)
   1604  1.1  riastrad 		/*
   1605  1.1  riastrad 		 * Can't read out the voltage level :(
   1606  1.1  riastrad 		 * Let's just assume everything is as expected.
   1607  1.1  riastrad 		 */
   1608  1.1  riastrad 		dev_priv->cdclk.hw.voltage_level = cdclk_state->voltage_level;
   1609  1.1  riastrad }
   1610  1.1  riastrad 
   1611  1.1  riastrad static void bxt_sanitize_cdclk(struct drm_i915_private *dev_priv)
   1612  1.1  riastrad {
   1613  1.1  riastrad 	u32 cdctl, expected;
   1614  1.1  riastrad 	int cdclk, vco;
   1615  1.1  riastrad 
   1616  1.1  riastrad 	intel_update_cdclk(dev_priv);
   1617  1.1  riastrad 	intel_dump_cdclk_state(&dev_priv->cdclk.hw, "Current CDCLK");
   1618  1.1  riastrad 
   1619  1.1  riastrad 	if (dev_priv->cdclk.hw.vco == 0 ||
   1620  1.1  riastrad 	    dev_priv->cdclk.hw.cdclk == dev_priv->cdclk.hw.bypass)
   1621  1.1  riastrad 		goto sanitize;
   1622  1.1  riastrad 
   1623  1.1  riastrad 	/* DPLL okay; verify the cdclock
   1624  1.1  riastrad 	 *
   1625  1.1  riastrad 	 * Some BIOS versions leave an incorrect decimal frequency value and
   1626  1.1  riastrad 	 * set reserved MBZ bits in CDCLK_CTL at least during exiting from S4,
   1627  1.1  riastrad 	 * so sanitize this register.
   1628  1.1  riastrad 	 */
   1629  1.1  riastrad 	cdctl = I915_READ(CDCLK_CTL);
   1630  1.1  riastrad 	/*
   1631  1.1  riastrad 	 * Let's ignore the pipe field, since BIOS could have configured the
   1632  1.1  riastrad 	 * dividers both synching to an active pipe, or asynchronously
   1633  1.1  riastrad 	 * (PIPE_NONE).
   1634  1.1  riastrad 	 */
   1635  1.1  riastrad 	cdctl &= ~bxt_cdclk_cd2x_pipe(dev_priv, INVALID_PIPE);
   1636  1.1  riastrad 
   1637  1.1  riastrad 	/* Make sure this is a legal cdclk value for the platform */
   1638  1.1  riastrad 	cdclk = bxt_calc_cdclk(dev_priv, dev_priv->cdclk.hw.cdclk);
   1639  1.1  riastrad 	if (cdclk != dev_priv->cdclk.hw.cdclk)
   1640  1.1  riastrad 		goto sanitize;
   1641  1.1  riastrad 
   1642  1.1  riastrad 	/* Make sure the VCO is correct for the cdclk */
   1643  1.1  riastrad 	vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
   1644  1.1  riastrad 	if (vco != dev_priv->cdclk.hw.vco)
   1645  1.1  riastrad 		goto sanitize;
   1646  1.1  riastrad 
   1647  1.1  riastrad 	expected = skl_cdclk_decimal(cdclk);
   1648  1.1  riastrad 
   1649  1.1  riastrad 	/* Figure out what CD2X divider we should be using for this cdclk */
   1650  1.1  riastrad 	switch (DIV_ROUND_CLOSEST(dev_priv->cdclk.hw.vco,
   1651  1.1  riastrad 				  dev_priv->cdclk.hw.cdclk)) {
   1652  1.1  riastrad 	case 2:
   1653  1.1  riastrad 		expected |= BXT_CDCLK_CD2X_DIV_SEL_1;
   1654  1.1  riastrad 		break;
   1655  1.1  riastrad 	case 3:
   1656  1.1  riastrad 		expected |= BXT_CDCLK_CD2X_DIV_SEL_1_5;
   1657  1.1  riastrad 		break;
   1658  1.1  riastrad 	case 4:
   1659  1.1  riastrad 		expected |= BXT_CDCLK_CD2X_DIV_SEL_2;
   1660  1.1  riastrad 		break;
   1661  1.1  riastrad 	case 8:
   1662  1.1  riastrad 		expected |= BXT_CDCLK_CD2X_DIV_SEL_4;
   1663  1.1  riastrad 		break;
   1664  1.1  riastrad 	default:
   1665  1.1  riastrad 		goto sanitize;
   1666  1.1  riastrad 	}
   1667  1.1  riastrad 
   1668  1.1  riastrad 	/*
   1669  1.1  riastrad 	 * Disable SSA Precharge when CD clock frequency < 500 MHz,
   1670  1.1  riastrad 	 * enable otherwise.
   1671  1.1  riastrad 	 */
   1672  1.1  riastrad 	if (IS_GEN9_LP(dev_priv) && dev_priv->cdclk.hw.cdclk >= 500000)
   1673  1.1  riastrad 		expected |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
   1674  1.1  riastrad 
   1675  1.1  riastrad 	if (cdctl == expected)
   1676  1.1  riastrad 		/* All well; nothing to sanitize */
   1677  1.1  riastrad 		return;
   1678  1.1  riastrad 
   1679  1.1  riastrad sanitize:
   1680  1.1  riastrad 	DRM_DEBUG_KMS("Sanitizing cdclk programmed by pre-os\n");
   1681  1.1  riastrad 
   1682  1.1  riastrad 	/* force cdclk programming */
   1683  1.1  riastrad 	dev_priv->cdclk.hw.cdclk = 0;
   1684  1.1  riastrad 
   1685  1.1  riastrad 	/* force full PLL disable + enable */
   1686  1.1  riastrad 	dev_priv->cdclk.hw.vco = -1;
   1687  1.1  riastrad }
   1688  1.1  riastrad 
   1689  1.1  riastrad static void bxt_init_cdclk(struct drm_i915_private *dev_priv)
   1690  1.1  riastrad {
   1691  1.1  riastrad 	struct intel_cdclk_state cdclk_state;
   1692  1.1  riastrad 
   1693  1.1  riastrad 	bxt_sanitize_cdclk(dev_priv);
   1694  1.1  riastrad 
   1695  1.1  riastrad 	if (dev_priv->cdclk.hw.cdclk != 0 &&
   1696  1.1  riastrad 	    dev_priv->cdclk.hw.vco != 0)
   1697  1.1  riastrad 		return;
   1698  1.1  riastrad 
   1699  1.1  riastrad 	cdclk_state = dev_priv->cdclk.hw;
   1700  1.1  riastrad 
   1701  1.1  riastrad 	/*
   1702  1.1  riastrad 	 * FIXME:
   1703  1.1  riastrad 	 * - The initial CDCLK needs to be read from VBT.
   1704  1.1  riastrad 	 *   Need to make this change after VBT has changes for BXT.
   1705  1.1  riastrad 	 */
   1706  1.1  riastrad 	cdclk_state.cdclk = bxt_calc_cdclk(dev_priv, 0);
   1707  1.1  riastrad 	cdclk_state.vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk_state.cdclk);
   1708  1.1  riastrad 	cdclk_state.voltage_level =
   1709  1.1  riastrad 		dev_priv->display.calc_voltage_level(cdclk_state.cdclk);
   1710  1.1  riastrad 
   1711  1.1  riastrad 	bxt_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
   1712  1.1  riastrad }
   1713  1.1  riastrad 
   1714  1.1  riastrad static void bxt_uninit_cdclk(struct drm_i915_private *dev_priv)
   1715  1.1  riastrad {
   1716  1.1  riastrad 	struct intel_cdclk_state cdclk_state = dev_priv->cdclk.hw;
   1717  1.1  riastrad 
   1718  1.1  riastrad 	cdclk_state.cdclk = cdclk_state.bypass;
   1719  1.1  riastrad 	cdclk_state.vco = 0;
   1720  1.1  riastrad 	cdclk_state.voltage_level =
   1721  1.1  riastrad 		dev_priv->display.calc_voltage_level(cdclk_state.cdclk);
   1722  1.1  riastrad 
   1723  1.1  riastrad 	bxt_set_cdclk(dev_priv, &cdclk_state, INVALID_PIPE);
   1724  1.1  riastrad }
   1725  1.1  riastrad 
   1726  1.1  riastrad /**
   1727  1.1  riastrad  * intel_cdclk_init - Initialize CDCLK
   1728  1.1  riastrad  * @i915: i915 device
   1729  1.1  riastrad  *
   1730  1.1  riastrad  * Initialize CDCLK. This consists mainly of initializing dev_priv->cdclk.hw and
   1731  1.1  riastrad  * sanitizing the state of the hardware if needed. This is generally done only
   1732  1.1  riastrad  * during the display core initialization sequence, after which the DMC will
   1733  1.1  riastrad  * take care of turning CDCLK off/on as needed.
   1734  1.1  riastrad  */
   1735  1.1  riastrad void intel_cdclk_init(struct drm_i915_private *i915)
   1736  1.1  riastrad {
   1737  1.1  riastrad 	if (IS_GEN9_LP(i915) || INTEL_GEN(i915) >= 10)
   1738  1.1  riastrad 		bxt_init_cdclk(i915);
   1739  1.1  riastrad 	else if (IS_GEN9_BC(i915))
   1740  1.1  riastrad 		skl_init_cdclk(i915);
   1741  1.1  riastrad }
   1742  1.1  riastrad 
   1743  1.1  riastrad /**
   1744  1.1  riastrad  * intel_cdclk_uninit - Uninitialize CDCLK
   1745  1.1  riastrad  * @i915: i915 device
   1746  1.1  riastrad  *
   1747  1.1  riastrad  * Uninitialize CDCLK. This is done only during the display core
   1748  1.1  riastrad  * uninitialization sequence.
   1749  1.1  riastrad  */
   1750  1.1  riastrad void intel_cdclk_uninit(struct drm_i915_private *i915)
   1751  1.1  riastrad {
   1752  1.1  riastrad 	if (INTEL_GEN(i915) >= 10 || IS_GEN9_LP(i915))
   1753  1.1  riastrad 		bxt_uninit_cdclk(i915);
   1754  1.1  riastrad 	else if (IS_GEN9_BC(i915))
   1755  1.1  riastrad 		skl_uninit_cdclk(i915);
   1756  1.1  riastrad }
   1757  1.1  riastrad 
   1758  1.1  riastrad /**
   1759  1.1  riastrad  * intel_cdclk_needs_modeset - Determine if two CDCLK states require a modeset on all pipes
   1760  1.1  riastrad  * @a: first CDCLK state
   1761  1.1  riastrad  * @b: second CDCLK state
   1762  1.1  riastrad  *
   1763  1.1  riastrad  * Returns:
   1764  1.1  riastrad  * True if the CDCLK states require pipes to be off during reprogramming, false if not.
   1765  1.1  riastrad  */
   1766  1.1  riastrad bool intel_cdclk_needs_modeset(const struct intel_cdclk_state *a,
   1767  1.1  riastrad 			       const struct intel_cdclk_state *b)
   1768  1.1  riastrad {
   1769  1.1  riastrad 	return a->cdclk != b->cdclk ||
   1770  1.1  riastrad 		a->vco != b->vco ||
   1771  1.1  riastrad 		a->ref != b->ref;
   1772  1.1  riastrad }
   1773  1.1  riastrad 
   1774  1.1  riastrad /**
   1775  1.1  riastrad  * intel_cdclk_needs_cd2x_update - Determine if two CDCLK states require a cd2x divider update
   1776  1.1  riastrad  * @dev_priv: Not a CDCLK state, it's the drm_i915_private!
   1777  1.1  riastrad  * @a: first CDCLK state
   1778  1.1  riastrad  * @b: second CDCLK state
   1779  1.1  riastrad  *
   1780  1.1  riastrad  * Returns:
   1781  1.1  riastrad  * True if the CDCLK states require just a cd2x divider update, false if not.
   1782  1.1  riastrad  */
   1783  1.1  riastrad static bool intel_cdclk_needs_cd2x_update(struct drm_i915_private *dev_priv,
   1784  1.1  riastrad 					  const struct intel_cdclk_state *a,
   1785  1.1  riastrad 					  const struct intel_cdclk_state *b)
   1786  1.1  riastrad {
   1787  1.1  riastrad 	/* Older hw doesn't have the capability */
   1788  1.1  riastrad 	if (INTEL_GEN(dev_priv) < 10 && !IS_GEN9_LP(dev_priv))
   1789  1.1  riastrad 		return false;
   1790  1.1  riastrad 
   1791  1.1  riastrad 	return a->cdclk != b->cdclk &&
   1792  1.1  riastrad 		a->vco == b->vco &&
   1793  1.1  riastrad 		a->ref == b->ref;
   1794  1.1  riastrad }
   1795  1.1  riastrad 
   1796  1.1  riastrad /**
   1797  1.1  riastrad  * intel_cdclk_changed - Determine if two CDCLK states are different
   1798  1.1  riastrad  * @a: first CDCLK state
   1799  1.1  riastrad  * @b: second CDCLK state
   1800  1.1  riastrad  *
   1801  1.1  riastrad  * Returns:
   1802  1.1  riastrad  * True if the CDCLK states don't match, false if they do.
   1803  1.1  riastrad  */
   1804  1.1  riastrad static bool intel_cdclk_changed(const struct intel_cdclk_state *a,
   1805  1.1  riastrad 				const struct intel_cdclk_state *b)
   1806  1.1  riastrad {
   1807  1.1  riastrad 	return intel_cdclk_needs_modeset(a, b) ||
   1808  1.1  riastrad 		a->voltage_level != b->voltage_level;
   1809  1.1  riastrad }
   1810  1.1  riastrad 
   1811  1.1  riastrad /**
   1812  1.1  riastrad  * intel_cdclk_swap_state - make atomic CDCLK configuration effective
   1813  1.1  riastrad  * @state: atomic state
   1814  1.1  riastrad  *
   1815  1.1  riastrad  * This is the CDCLK version of drm_atomic_helper_swap_state() since the
   1816  1.1  riastrad  * helper does not handle driver-specific global state.
   1817  1.1  riastrad  *
   1818  1.1  riastrad  * Similarly to the atomic helpers this function does a complete swap,
   1819  1.1  riastrad  * i.e. it also puts the old state into @state. This is used by the commit
   1820  1.1  riastrad  * code to determine how CDCLK has changed (for instance did it increase or
   1821  1.1  riastrad  * decrease).
   1822  1.1  riastrad  */
   1823  1.1  riastrad void intel_cdclk_swap_state(struct intel_atomic_state *state)
   1824  1.1  riastrad {
   1825  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   1826  1.1  riastrad 
   1827  1.1  riastrad 	swap(state->cdclk.logical, dev_priv->cdclk.logical);
   1828  1.1  riastrad 	swap(state->cdclk.actual, dev_priv->cdclk.actual);
   1829  1.1  riastrad }
   1830  1.1  riastrad 
   1831  1.1  riastrad void intel_dump_cdclk_state(const struct intel_cdclk_state *cdclk_state,
   1832  1.1  riastrad 			    const char *context)
   1833  1.1  riastrad {
   1834  1.1  riastrad 	DRM_DEBUG_DRIVER("%s %d kHz, VCO %d kHz, ref %d kHz, bypass %d kHz, voltage level %d\n",
   1835  1.1  riastrad 			 context, cdclk_state->cdclk, cdclk_state->vco,
   1836  1.1  riastrad 			 cdclk_state->ref, cdclk_state->bypass,
   1837  1.1  riastrad 			 cdclk_state->voltage_level);
   1838  1.1  riastrad }
   1839  1.1  riastrad 
   1840  1.1  riastrad /**
   1841  1.1  riastrad  * intel_set_cdclk - Push the CDCLK state to the hardware
   1842  1.1  riastrad  * @dev_priv: i915 device
   1843  1.1  riastrad  * @cdclk_state: new CDCLK state
   1844  1.1  riastrad  * @pipe: pipe with which to synchronize the update
   1845  1.1  riastrad  *
   1846  1.1  riastrad  * Program the hardware based on the passed in CDCLK state,
   1847  1.1  riastrad  * if necessary.
   1848  1.1  riastrad  */
   1849  1.1  riastrad static void intel_set_cdclk(struct drm_i915_private *dev_priv,
   1850  1.1  riastrad 			    const struct intel_cdclk_state *cdclk_state,
   1851  1.1  riastrad 			    enum pipe pipe)
   1852  1.1  riastrad {
   1853  1.1  riastrad 	if (!intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_state))
   1854  1.1  riastrad 		return;
   1855  1.1  riastrad 
   1856  1.1  riastrad 	if (WARN_ON_ONCE(!dev_priv->display.set_cdclk))
   1857  1.1  riastrad 		return;
   1858  1.1  riastrad 
   1859  1.1  riastrad 	intel_dump_cdclk_state(cdclk_state, "Changing CDCLK to");
   1860  1.1  riastrad 
   1861  1.1  riastrad 	dev_priv->display.set_cdclk(dev_priv, cdclk_state, pipe);
   1862  1.1  riastrad 
   1863  1.1  riastrad 	if (WARN(intel_cdclk_changed(&dev_priv->cdclk.hw, cdclk_state),
   1864  1.1  riastrad 		 "cdclk state doesn't match!\n")) {
   1865  1.1  riastrad 		intel_dump_cdclk_state(&dev_priv->cdclk.hw, "[hw state]");
   1866  1.1  riastrad 		intel_dump_cdclk_state(cdclk_state, "[sw state]");
   1867  1.1  riastrad 	}
   1868  1.1  riastrad }
   1869  1.1  riastrad 
   1870  1.1  riastrad /**
   1871  1.1  riastrad  * intel_set_cdclk_pre_plane_update - Push the CDCLK state to the hardware
   1872  1.1  riastrad  * @dev_priv: i915 device
   1873  1.1  riastrad  * @old_state: old CDCLK state
   1874  1.1  riastrad  * @new_state: new CDCLK state
   1875  1.1  riastrad  * @pipe: pipe with which to synchronize the update
   1876  1.1  riastrad  *
   1877  1.1  riastrad  * Program the hardware before updating the HW plane state based on the passed
   1878  1.1  riastrad  * in CDCLK state, if necessary.
   1879  1.1  riastrad  */
   1880  1.1  riastrad void
   1881  1.1  riastrad intel_set_cdclk_pre_plane_update(struct drm_i915_private *dev_priv,
   1882  1.1  riastrad 				 const struct intel_cdclk_state *old_state,
   1883  1.1  riastrad 				 const struct intel_cdclk_state *new_state,
   1884  1.1  riastrad 				 enum pipe pipe)
   1885  1.1  riastrad {
   1886  1.1  riastrad 	if (pipe == INVALID_PIPE || old_state->cdclk <= new_state->cdclk)
   1887  1.1  riastrad 		intel_set_cdclk(dev_priv, new_state, pipe);
   1888  1.1  riastrad }
   1889  1.1  riastrad 
   1890  1.1  riastrad /**
   1891  1.1  riastrad  * intel_set_cdclk_post_plane_update - Push the CDCLK state to the hardware
   1892  1.1  riastrad  * @dev_priv: i915 device
   1893  1.1  riastrad  * @old_state: old CDCLK state
   1894  1.1  riastrad  * @new_state: new CDCLK state
   1895  1.1  riastrad  * @pipe: pipe with which to synchronize the update
   1896  1.1  riastrad  *
   1897  1.1  riastrad  * Program the hardware after updating the HW plane state based on the passed
   1898  1.1  riastrad  * in CDCLK state, if necessary.
   1899  1.1  riastrad  */
   1900  1.1  riastrad void
   1901  1.1  riastrad intel_set_cdclk_post_plane_update(struct drm_i915_private *dev_priv,
   1902  1.1  riastrad 				  const struct intel_cdclk_state *old_state,
   1903  1.1  riastrad 				  const struct intel_cdclk_state *new_state,
   1904  1.1  riastrad 				  enum pipe pipe)
   1905  1.1  riastrad {
   1906  1.1  riastrad 	if (pipe != INVALID_PIPE && old_state->cdclk > new_state->cdclk)
   1907  1.1  riastrad 		intel_set_cdclk(dev_priv, new_state, pipe);
   1908  1.1  riastrad }
   1909  1.1  riastrad 
   1910  1.1  riastrad static int intel_pixel_rate_to_cdclk(const struct intel_crtc_state *crtc_state)
   1911  1.1  riastrad {
   1912  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   1913  1.1  riastrad 	int pixel_rate = crtc_state->pixel_rate;
   1914  1.1  riastrad 
   1915  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
   1916  1.1  riastrad 		return DIV_ROUND_UP(pixel_rate, 2);
   1917  1.1  riastrad 	else if (IS_GEN(dev_priv, 9) ||
   1918  1.1  riastrad 		 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
   1919  1.1  riastrad 		return pixel_rate;
   1920  1.1  riastrad 	else if (IS_CHERRYVIEW(dev_priv))
   1921  1.1  riastrad 		return DIV_ROUND_UP(pixel_rate * 100, 95);
   1922  1.1  riastrad 	else if (crtc_state->double_wide)
   1923  1.1  riastrad 		return DIV_ROUND_UP(pixel_rate * 100, 90 * 2);
   1924  1.1  riastrad 	else
   1925  1.1  riastrad 		return DIV_ROUND_UP(pixel_rate * 100, 90);
   1926  1.1  riastrad }
   1927  1.1  riastrad 
   1928  1.1  riastrad static int intel_planes_min_cdclk(const struct intel_crtc_state *crtc_state)
   1929  1.1  riastrad {
   1930  1.1  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
   1931  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   1932  1.1  riastrad 	struct intel_plane *plane;
   1933  1.1  riastrad 	int min_cdclk = 0;
   1934  1.1  riastrad 
   1935  1.1  riastrad 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
   1936  1.1  riastrad 		min_cdclk = max(crtc_state->min_cdclk[plane->id], min_cdclk);
   1937  1.1  riastrad 
   1938  1.1  riastrad 	return min_cdclk;
   1939  1.1  riastrad }
   1940  1.1  riastrad 
   1941  1.1  riastrad int intel_crtc_compute_min_cdclk(const struct intel_crtc_state *crtc_state)
   1942  1.1  riastrad {
   1943  1.1  riastrad 	struct drm_i915_private *dev_priv =
   1944  1.1  riastrad 		to_i915(crtc_state->uapi.crtc->dev);
   1945  1.1  riastrad 	int min_cdclk;
   1946  1.1  riastrad 
   1947  1.1  riastrad 	if (!crtc_state->hw.enable)
   1948  1.1  riastrad 		return 0;
   1949  1.1  riastrad 
   1950  1.1  riastrad 	min_cdclk = intel_pixel_rate_to_cdclk(crtc_state);
   1951  1.1  riastrad 
   1952  1.1  riastrad 	/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
   1953  1.1  riastrad 	if (IS_BROADWELL(dev_priv) && hsw_crtc_state_ips_capable(crtc_state))
   1954  1.1  riastrad 		min_cdclk = DIV_ROUND_UP(min_cdclk * 100, 95);
   1955  1.1  riastrad 
   1956  1.1  riastrad 	/* BSpec says "Do not use DisplayPort with CDCLK less than 432 MHz,
   1957  1.1  riastrad 	 * audio enabled, port width x4, and link rate HBR2 (5.4 GHz), or else
   1958  1.1  riastrad 	 * there may be audio corruption or screen corruption." This cdclk
   1959  1.1  riastrad 	 * restriction for GLK is 316.8 MHz.
   1960  1.1  riastrad 	 */
   1961  1.1  riastrad 	if (intel_crtc_has_dp_encoder(crtc_state) &&
   1962  1.1  riastrad 	    crtc_state->has_audio &&
   1963  1.1  riastrad 	    crtc_state->port_clock >= 540000 &&
   1964  1.1  riastrad 	    crtc_state->lane_count == 4) {
   1965  1.1  riastrad 		if (IS_CANNONLAKE(dev_priv) || IS_GEMINILAKE(dev_priv)) {
   1966  1.1  riastrad 			/* Display WA #1145: glk,cnl */
   1967  1.1  riastrad 			min_cdclk = max(316800, min_cdclk);
   1968  1.1  riastrad 		} else if (IS_GEN(dev_priv, 9) || IS_BROADWELL(dev_priv)) {
   1969  1.1  riastrad 			/* Display WA #1144: skl,bxt */
   1970  1.1  riastrad 			min_cdclk = max(432000, min_cdclk);
   1971  1.1  riastrad 		}
   1972  1.1  riastrad 	}
   1973  1.1  riastrad 
   1974  1.1  riastrad 	/*
   1975  1.1  riastrad 	 * According to BSpec, "The CD clock frequency must be at least twice
   1976  1.1  riastrad 	 * the frequency of the Azalia BCLK." and BCLK is 96 MHz by default.
   1977  1.1  riastrad 	 */
   1978  1.1  riastrad 	if (crtc_state->has_audio && INTEL_GEN(dev_priv) >= 9)
   1979  1.1  riastrad 		min_cdclk = max(2 * 96000, min_cdclk);
   1980  1.1  riastrad 
   1981  1.1  riastrad 	/*
   1982  1.1  riastrad 	 * "For DP audio configuration, cdclk frequency shall be set to
   1983  1.1  riastrad 	 *  meet the following requirements:
   1984  1.1  riastrad 	 *  DP Link Frequency(MHz) | Cdclk frequency(MHz)
   1985  1.1  riastrad 	 *  270                    | 320 or higher
   1986  1.1  riastrad 	 *  162                    | 200 or higher"
   1987  1.1  riastrad 	 */
   1988  1.1  riastrad 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
   1989  1.1  riastrad 	    intel_crtc_has_dp_encoder(crtc_state) && crtc_state->has_audio)
   1990  1.1  riastrad 		min_cdclk = max(crtc_state->port_clock, min_cdclk);
   1991  1.1  riastrad 
   1992  1.1  riastrad 	/*
   1993  1.1  riastrad 	 * On Valleyview some DSI panels lose (v|h)sync when the clock is lower
   1994  1.1  riastrad 	 * than 320000KHz.
   1995  1.1  riastrad 	 */
   1996  1.1  riastrad 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) &&
   1997  1.1  riastrad 	    IS_VALLEYVIEW(dev_priv))
   1998  1.1  riastrad 		min_cdclk = max(320000, min_cdclk);
   1999  1.1  riastrad 
   2000  1.1  riastrad 	/*
   2001  1.1  riastrad 	 * On Geminilake once the CDCLK gets as low as 79200
   2002  1.1  riastrad 	 * picture gets unstable, despite that values are
   2003  1.1  riastrad 	 * correct for DSI PLL and DE PLL.
   2004  1.1  riastrad 	 */
   2005  1.1  riastrad 	if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_DSI) &&
   2006  1.1  riastrad 	    IS_GEMINILAKE(dev_priv))
   2007  1.1  riastrad 		min_cdclk = max(158400, min_cdclk);
   2008  1.1  riastrad 
   2009  1.1  riastrad 	/* Account for additional needs from the planes */
   2010  1.1  riastrad 	min_cdclk = max(intel_planes_min_cdclk(crtc_state), min_cdclk);
   2011  1.1  riastrad 
   2012  1.1  riastrad 	/*
   2013  1.1  riastrad 	 * HACK. Currently for TGL platforms we calculate
   2014  1.1  riastrad 	 * min_cdclk initially based on pixel_rate divided
   2015  1.1  riastrad 	 * by 2, accounting for also plane requirements,
   2016  1.1  riastrad 	 * however in some cases the lowest possible CDCLK
   2017  1.1  riastrad 	 * doesn't work and causing the underruns.
   2018  1.1  riastrad 	 * Explicitly stating here that this seems to be currently
   2019  1.1  riastrad 	 * rather a Hack, than final solution.
   2020  1.1  riastrad 	 */
   2021  1.1  riastrad 	if (IS_TIGERLAKE(dev_priv))
   2022  1.1  riastrad 		min_cdclk = max(min_cdclk, (int)crtc_state->pixel_rate);
   2023  1.1  riastrad 
   2024  1.1  riastrad 	if (min_cdclk > dev_priv->max_cdclk_freq) {
   2025  1.1  riastrad 		DRM_DEBUG_KMS("required cdclk (%d kHz) exceeds max (%d kHz)\n",
   2026  1.1  riastrad 			      min_cdclk, dev_priv->max_cdclk_freq);
   2027  1.1  riastrad 		return -EINVAL;
   2028  1.1  riastrad 	}
   2029  1.1  riastrad 
   2030  1.1  riastrad 	return min_cdclk;
   2031  1.1  riastrad }
   2032  1.1  riastrad 
   2033  1.1  riastrad static int intel_compute_min_cdclk(struct intel_atomic_state *state)
   2034  1.1  riastrad {
   2035  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   2036  1.1  riastrad 	struct intel_crtc *crtc;
   2037  1.1  riastrad 	struct intel_crtc_state *crtc_state;
   2038  1.1  riastrad 	int min_cdclk, i;
   2039  1.1  riastrad 	enum pipe pipe;
   2040  1.1  riastrad 
   2041  1.1  riastrad 	memcpy(state->min_cdclk, dev_priv->min_cdclk,
   2042  1.1  riastrad 	       sizeof(state->min_cdclk));
   2043  1.1  riastrad 
   2044  1.1  riastrad 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
   2045  1.1  riastrad 		int ret;
   2046  1.1  riastrad 
   2047  1.1  riastrad 		min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
   2048  1.1  riastrad 		if (min_cdclk < 0)
   2049  1.1  riastrad 			return min_cdclk;
   2050  1.1  riastrad 
   2051  1.1  riastrad 		if (state->min_cdclk[i] == min_cdclk)
   2052  1.1  riastrad 			continue;
   2053  1.1  riastrad 
   2054  1.1  riastrad 		state->min_cdclk[i] = min_cdclk;
   2055  1.1  riastrad 
   2056  1.1  riastrad 		ret = intel_atomic_lock_global_state(state);
   2057  1.1  riastrad 		if (ret)
   2058  1.1  riastrad 			return ret;
   2059  1.1  riastrad 	}
   2060  1.1  riastrad 
   2061  1.1  riastrad 	min_cdclk = state->cdclk.force_min_cdclk;
   2062  1.1  riastrad 	for_each_pipe(dev_priv, pipe)
   2063  1.1  riastrad 		min_cdclk = max(state->min_cdclk[pipe], min_cdclk);
   2064  1.1  riastrad 
   2065  1.1  riastrad 	return min_cdclk;
   2066  1.1  riastrad }
   2067  1.1  riastrad 
   2068  1.1  riastrad /*
   2069  1.1  riastrad  * Account for port clock min voltage level requirements.
   2070  1.1  riastrad  * This only really does something on CNL+ but can be
   2071  1.1  riastrad  * called on earlier platforms as well.
   2072  1.1  riastrad  *
   2073  1.1  riastrad  * Note that this functions assumes that 0 is
   2074  1.1  riastrad  * the lowest voltage value, and higher values
   2075  1.1  riastrad  * correspond to increasingly higher voltages.
   2076  1.1  riastrad  *
   2077  1.1  riastrad  * Should that relationship no longer hold on
   2078  1.1  riastrad  * future platforms this code will need to be
   2079  1.1  riastrad  * adjusted.
   2080  1.1  riastrad  */
   2081  1.1  riastrad static int bxt_compute_min_voltage_level(struct intel_atomic_state *state)
   2082  1.1  riastrad {
   2083  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   2084  1.1  riastrad 	struct intel_crtc *crtc;
   2085  1.1  riastrad 	struct intel_crtc_state *crtc_state;
   2086  1.1  riastrad 	u8 min_voltage_level;
   2087  1.1  riastrad 	int i;
   2088  1.1  riastrad 	enum pipe pipe;
   2089  1.1  riastrad 
   2090  1.1  riastrad 	memcpy(state->min_voltage_level, dev_priv->min_voltage_level,
   2091  1.1  riastrad 	       sizeof(state->min_voltage_level));
   2092  1.1  riastrad 
   2093  1.1  riastrad 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
   2094  1.1  riastrad 		int ret;
   2095  1.1  riastrad 
   2096  1.1  riastrad 		if (crtc_state->hw.enable)
   2097  1.1  riastrad 			min_voltage_level = crtc_state->min_voltage_level;
   2098  1.1  riastrad 		else
   2099  1.1  riastrad 			min_voltage_level = 0;
   2100  1.1  riastrad 
   2101  1.1  riastrad 		if (state->min_voltage_level[i] == min_voltage_level)
   2102  1.1  riastrad 			continue;
   2103  1.1  riastrad 
   2104  1.1  riastrad 		state->min_voltage_level[i] = min_voltage_level;
   2105  1.1  riastrad 
   2106  1.1  riastrad 		ret = intel_atomic_lock_global_state(state);
   2107  1.1  riastrad 		if (ret)
   2108  1.1  riastrad 			return ret;
   2109  1.1  riastrad 	}
   2110  1.1  riastrad 
   2111  1.1  riastrad 	min_voltage_level = 0;
   2112  1.1  riastrad 	for_each_pipe(dev_priv, pipe)
   2113  1.1  riastrad 		min_voltage_level = max(state->min_voltage_level[pipe],
   2114  1.1  riastrad 					min_voltage_level);
   2115  1.1  riastrad 
   2116  1.1  riastrad 	return min_voltage_level;
   2117  1.1  riastrad }
   2118  1.1  riastrad 
   2119  1.1  riastrad static int vlv_modeset_calc_cdclk(struct intel_atomic_state *state)
   2120  1.1  riastrad {
   2121  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   2122  1.1  riastrad 	int min_cdclk, cdclk;
   2123  1.1  riastrad 
   2124  1.1  riastrad 	min_cdclk = intel_compute_min_cdclk(state);
   2125  1.1  riastrad 	if (min_cdclk < 0)
   2126  1.1  riastrad 		return min_cdclk;
   2127  1.1  riastrad 
   2128  1.1  riastrad 	cdclk = vlv_calc_cdclk(dev_priv, min_cdclk);
   2129  1.1  riastrad 
   2130  1.1  riastrad 	state->cdclk.logical.cdclk = cdclk;
   2131  1.1  riastrad 	state->cdclk.logical.voltage_level =
   2132  1.1  riastrad 		vlv_calc_voltage_level(dev_priv, cdclk);
   2133  1.1  riastrad 
   2134  1.1  riastrad 	if (!state->active_pipes) {
   2135  1.1  riastrad 		cdclk = vlv_calc_cdclk(dev_priv, state->cdclk.force_min_cdclk);
   2136  1.1  riastrad 
   2137  1.1  riastrad 		state->cdclk.actual.cdclk = cdclk;
   2138  1.1  riastrad 		state->cdclk.actual.voltage_level =
   2139  1.1  riastrad 			vlv_calc_voltage_level(dev_priv, cdclk);
   2140  1.1  riastrad 	} else {
   2141  1.1  riastrad 		state->cdclk.actual = state->cdclk.logical;
   2142  1.1  riastrad 	}
   2143  1.1  riastrad 
   2144  1.1  riastrad 	return 0;
   2145  1.1  riastrad }
   2146  1.1  riastrad 
   2147  1.1  riastrad static int bdw_modeset_calc_cdclk(struct intel_atomic_state *state)
   2148  1.1  riastrad {
   2149  1.1  riastrad 	int min_cdclk, cdclk;
   2150  1.1  riastrad 
   2151  1.1  riastrad 	min_cdclk = intel_compute_min_cdclk(state);
   2152  1.1  riastrad 	if (min_cdclk < 0)
   2153  1.1  riastrad 		return min_cdclk;
   2154  1.1  riastrad 
   2155  1.1  riastrad 	/*
   2156  1.1  riastrad 	 * FIXME should also account for plane ratio
   2157  1.1  riastrad 	 * once 64bpp pixel formats are supported.
   2158  1.1  riastrad 	 */
   2159  1.1  riastrad 	cdclk = bdw_calc_cdclk(min_cdclk);
   2160  1.1  riastrad 
   2161  1.1  riastrad 	state->cdclk.logical.cdclk = cdclk;
   2162  1.1  riastrad 	state->cdclk.logical.voltage_level =
   2163  1.1  riastrad 		bdw_calc_voltage_level(cdclk);
   2164  1.1  riastrad 
   2165  1.1  riastrad 	if (!state->active_pipes) {
   2166  1.1  riastrad 		cdclk = bdw_calc_cdclk(state->cdclk.force_min_cdclk);
   2167  1.1  riastrad 
   2168  1.1  riastrad 		state->cdclk.actual.cdclk = cdclk;
   2169  1.1  riastrad 		state->cdclk.actual.voltage_level =
   2170  1.1  riastrad 			bdw_calc_voltage_level(cdclk);
   2171  1.1  riastrad 	} else {
   2172  1.1  riastrad 		state->cdclk.actual = state->cdclk.logical;
   2173  1.1  riastrad 	}
   2174  1.1  riastrad 
   2175  1.1  riastrad 	return 0;
   2176  1.1  riastrad }
   2177  1.1  riastrad 
   2178  1.1  riastrad static int skl_dpll0_vco(struct intel_atomic_state *state)
   2179  1.1  riastrad {
   2180  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   2181  1.1  riastrad 	struct intel_crtc *crtc;
   2182  1.1  riastrad 	struct intel_crtc_state *crtc_state;
   2183  1.1  riastrad 	int vco, i;
   2184  1.1  riastrad 
   2185  1.1  riastrad 	vco = state->cdclk.logical.vco;
   2186  1.1  riastrad 	if (!vco)
   2187  1.1  riastrad 		vco = dev_priv->skl_preferred_vco_freq;
   2188  1.1  riastrad 
   2189  1.1  riastrad 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
   2190  1.1  riastrad 		if (!crtc_state->hw.enable)
   2191  1.1  riastrad 			continue;
   2192  1.1  riastrad 
   2193  1.1  riastrad 		if (!intel_crtc_has_type(crtc_state, INTEL_OUTPUT_EDP))
   2194  1.1  riastrad 			continue;
   2195  1.1  riastrad 
   2196  1.1  riastrad 		/*
   2197  1.1  riastrad 		 * DPLL0 VCO may need to be adjusted to get the correct
   2198  1.1  riastrad 		 * clock for eDP. This will affect cdclk as well.
   2199  1.1  riastrad 		 */
   2200  1.1  riastrad 		switch (crtc_state->port_clock / 2) {
   2201  1.1  riastrad 		case 108000:
   2202  1.1  riastrad 		case 216000:
   2203  1.1  riastrad 			vco = 8640000;
   2204  1.1  riastrad 			break;
   2205  1.1  riastrad 		default:
   2206  1.1  riastrad 			vco = 8100000;
   2207  1.1  riastrad 			break;
   2208  1.1  riastrad 		}
   2209  1.1  riastrad 	}
   2210  1.1  riastrad 
   2211  1.1  riastrad 	return vco;
   2212  1.1  riastrad }
   2213  1.1  riastrad 
   2214  1.1  riastrad static int skl_modeset_calc_cdclk(struct intel_atomic_state *state)
   2215  1.1  riastrad {
   2216  1.1  riastrad 	int min_cdclk, cdclk, vco;
   2217  1.1  riastrad 
   2218  1.1  riastrad 	min_cdclk = intel_compute_min_cdclk(state);
   2219  1.1  riastrad 	if (min_cdclk < 0)
   2220  1.1  riastrad 		return min_cdclk;
   2221  1.1  riastrad 
   2222  1.1  riastrad 	vco = skl_dpll0_vco(state);
   2223  1.1  riastrad 
   2224  1.1  riastrad 	/*
   2225  1.1  riastrad 	 * FIXME should also account for plane ratio
   2226  1.1  riastrad 	 * once 64bpp pixel formats are supported.
   2227  1.1  riastrad 	 */
   2228  1.1  riastrad 	cdclk = skl_calc_cdclk(min_cdclk, vco);
   2229  1.1  riastrad 
   2230  1.1  riastrad 	state->cdclk.logical.vco = vco;
   2231  1.1  riastrad 	state->cdclk.logical.cdclk = cdclk;
   2232  1.1  riastrad 	state->cdclk.logical.voltage_level =
   2233  1.1  riastrad 		skl_calc_voltage_level(cdclk);
   2234  1.1  riastrad 
   2235  1.1  riastrad 	if (!state->active_pipes) {
   2236  1.1  riastrad 		cdclk = skl_calc_cdclk(state->cdclk.force_min_cdclk, vco);
   2237  1.1  riastrad 
   2238  1.1  riastrad 		state->cdclk.actual.vco = vco;
   2239  1.1  riastrad 		state->cdclk.actual.cdclk = cdclk;
   2240  1.1  riastrad 		state->cdclk.actual.voltage_level =
   2241  1.1  riastrad 			skl_calc_voltage_level(cdclk);
   2242  1.1  riastrad 	} else {
   2243  1.1  riastrad 		state->cdclk.actual = state->cdclk.logical;
   2244  1.1  riastrad 	}
   2245  1.1  riastrad 
   2246  1.1  riastrad 	return 0;
   2247  1.1  riastrad }
   2248  1.1  riastrad 
   2249  1.1  riastrad static int bxt_modeset_calc_cdclk(struct intel_atomic_state *state)
   2250  1.1  riastrad {
   2251  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   2252  1.1  riastrad 	int min_cdclk, min_voltage_level, cdclk, vco;
   2253  1.1  riastrad 
   2254  1.1  riastrad 	min_cdclk = intel_compute_min_cdclk(state);
   2255  1.1  riastrad 	if (min_cdclk < 0)
   2256  1.1  riastrad 		return min_cdclk;
   2257  1.1  riastrad 
   2258  1.1  riastrad 	min_voltage_level = bxt_compute_min_voltage_level(state);
   2259  1.1  riastrad 	if (min_voltage_level < 0)
   2260  1.1  riastrad 		return min_voltage_level;
   2261  1.1  riastrad 
   2262  1.1  riastrad 	cdclk = bxt_calc_cdclk(dev_priv, min_cdclk);
   2263  1.1  riastrad 	vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
   2264  1.1  riastrad 
   2265  1.1  riastrad 	state->cdclk.logical.vco = vco;
   2266  1.1  riastrad 	state->cdclk.logical.cdclk = cdclk;
   2267  1.1  riastrad 	state->cdclk.logical.voltage_level =
   2268  1.1  riastrad 		max_t(int, min_voltage_level,
   2269  1.1  riastrad 		      dev_priv->display.calc_voltage_level(cdclk));
   2270  1.1  riastrad 
   2271  1.1  riastrad 	if (!state->active_pipes) {
   2272  1.1  riastrad 		cdclk = bxt_calc_cdclk(dev_priv, state->cdclk.force_min_cdclk);
   2273  1.1  riastrad 		vco = bxt_calc_cdclk_pll_vco(dev_priv, cdclk);
   2274  1.1  riastrad 
   2275  1.1  riastrad 		state->cdclk.actual.vco = vco;
   2276  1.1  riastrad 		state->cdclk.actual.cdclk = cdclk;
   2277  1.1  riastrad 		state->cdclk.actual.voltage_level =
   2278  1.1  riastrad 			dev_priv->display.calc_voltage_level(cdclk);
   2279  1.1  riastrad 	} else {
   2280  1.1  riastrad 		state->cdclk.actual = state->cdclk.logical;
   2281  1.1  riastrad 	}
   2282  1.1  riastrad 
   2283  1.1  riastrad 	return 0;
   2284  1.1  riastrad }
   2285  1.1  riastrad 
   2286  1.1  riastrad static int intel_modeset_all_pipes(struct intel_atomic_state *state)
   2287  1.1  riastrad {
   2288  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   2289  1.1  riastrad 	struct intel_crtc *crtc;
   2290  1.1  riastrad 
   2291  1.1  riastrad 	/*
   2292  1.1  riastrad 	 * Add all pipes to the state, and force
   2293  1.1  riastrad 	 * a modeset on all the active ones.
   2294  1.1  riastrad 	 */
   2295  1.1  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   2296  1.1  riastrad 		struct intel_crtc_state *crtc_state;
   2297  1.1  riastrad 		int ret;
   2298  1.1  riastrad 
   2299  1.1  riastrad 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
   2300  1.1  riastrad 		if (IS_ERR(crtc_state))
   2301  1.1  riastrad 			return PTR_ERR(crtc_state);
   2302  1.1  riastrad 
   2303  1.1  riastrad 		if (!crtc_state->hw.active ||
   2304  1.1  riastrad 		    drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
   2305  1.1  riastrad 			continue;
   2306  1.1  riastrad 
   2307  1.1  riastrad 		crtc_state->uapi.mode_changed = true;
   2308  1.1  riastrad 
   2309  1.1  riastrad 		ret = drm_atomic_add_affected_connectors(&state->base,
   2310  1.1  riastrad 							 &crtc->base);
   2311  1.1  riastrad 		if (ret)
   2312  1.1  riastrad 			return ret;
   2313  1.1  riastrad 
   2314  1.1  riastrad 		ret = drm_atomic_add_affected_planes(&state->base,
   2315  1.1  riastrad 						     &crtc->base);
   2316  1.1  riastrad 		if (ret)
   2317  1.1  riastrad 			return ret;
   2318  1.1  riastrad 
   2319  1.1  riastrad 		crtc_state->update_planes |= crtc_state->active_planes;
   2320  1.1  riastrad 	}
   2321  1.1  riastrad 
   2322  1.1  riastrad 	return 0;
   2323  1.1  riastrad }
   2324  1.1  riastrad 
   2325  1.1  riastrad static int fixed_modeset_calc_cdclk(struct intel_atomic_state *state)
   2326  1.1  riastrad {
   2327  1.1  riastrad 	int min_cdclk;
   2328  1.1  riastrad 
   2329  1.1  riastrad 	/*
   2330  1.1  riastrad 	 * We can't change the cdclk frequency, but we still want to
   2331  1.1  riastrad 	 * check that the required minimum frequency doesn't exceed
   2332  1.1  riastrad 	 * the actual cdclk frequency.
   2333  1.1  riastrad 	 */
   2334  1.1  riastrad 	min_cdclk = intel_compute_min_cdclk(state);
   2335  1.1  riastrad 	if (min_cdclk < 0)
   2336  1.1  riastrad 		return min_cdclk;
   2337  1.1  riastrad 
   2338  1.1  riastrad 	return 0;
   2339  1.1  riastrad }
   2340  1.1  riastrad 
   2341  1.1  riastrad int intel_modeset_calc_cdclk(struct intel_atomic_state *state)
   2342  1.1  riastrad {
   2343  1.1  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   2344  1.1  riastrad 	enum pipe pipe;
   2345  1.1  riastrad 	int ret;
   2346  1.1  riastrad 
   2347  1.1  riastrad 	ret = dev_priv->display.modeset_calc_cdclk(state);
   2348  1.1  riastrad 	if (ret)
   2349  1.1  riastrad 		return ret;
   2350  1.1  riastrad 
   2351  1.1  riastrad 	/*
   2352  1.1  riastrad 	 * Writes to dev_priv->cdclk.{actual,logical} must protected
   2353  1.1  riastrad 	 * by holding all the crtc mutexes even if we don't end up
   2354  1.1  riastrad 	 * touching the hardware
   2355  1.1  riastrad 	 */
   2356  1.1  riastrad 	if (intel_cdclk_changed(&dev_priv->cdclk.actual,
   2357  1.1  riastrad 				&state->cdclk.actual)) {
   2358  1.1  riastrad 		/*
   2359  1.1  riastrad 		 * Also serialize commits across all crtcs
   2360  1.1  riastrad 		 * if the actual hw needs to be poked.
   2361  1.1  riastrad 		 */
   2362  1.1  riastrad 		ret = intel_atomic_serialize_global_state(state);
   2363  1.1  riastrad 		if (ret)
   2364  1.1  riastrad 			return ret;
   2365  1.1  riastrad 	} else if (intel_cdclk_changed(&dev_priv->cdclk.logical,
   2366  1.1  riastrad 				       &state->cdclk.logical)) {
   2367  1.1  riastrad 		ret = intel_atomic_lock_global_state(state);
   2368  1.1  riastrad 		if (ret)
   2369  1.1  riastrad 			return ret;
   2370  1.1  riastrad 	} else {
   2371  1.1  riastrad 		return 0;
   2372  1.1  riastrad 	}
   2373  1.1  riastrad 
   2374  1.1  riastrad 	if (is_power_of_2(state->active_pipes) &&
   2375  1.1  riastrad 	    intel_cdclk_needs_cd2x_update(dev_priv,
   2376  1.1  riastrad 					  &dev_priv->cdclk.actual,
   2377  1.1  riastrad 					  &state->cdclk.actual)) {
   2378  1.1  riastrad 		struct intel_crtc *crtc;
   2379  1.1  riastrad 		struct intel_crtc_state *crtc_state;
   2380  1.1  riastrad 
   2381  1.1  riastrad 		pipe = ilog2(state->active_pipes);
   2382  1.1  riastrad 		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
   2383  1.1  riastrad 
   2384  1.1  riastrad 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
   2385  1.1  riastrad 		if (IS_ERR(crtc_state))
   2386  1.1  riastrad 			return PTR_ERR(crtc_state);
   2387  1.1  riastrad 
   2388  1.1  riastrad 		if (drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
   2389  1.1  riastrad 			pipe = INVALID_PIPE;
   2390  1.1  riastrad 	} else {
   2391  1.1  riastrad 		pipe = INVALID_PIPE;
   2392  1.1  riastrad 	}
   2393  1.1  riastrad 
   2394  1.1  riastrad 	if (pipe != INVALID_PIPE) {
   2395  1.1  riastrad 		state->cdclk.pipe = pipe;
   2396  1.1  riastrad 
   2397  1.1  riastrad 		DRM_DEBUG_KMS("Can change cdclk with pipe %c active\n",
   2398  1.1  riastrad 			      pipe_name(pipe));
   2399  1.1  riastrad 	} else if (intel_cdclk_needs_modeset(&dev_priv->cdclk.actual,
   2400  1.1  riastrad 					     &state->cdclk.actual)) {
   2401  1.1  riastrad 		/* All pipes must be switched off while we change the cdclk. */
   2402  1.1  riastrad 		ret = intel_modeset_all_pipes(state);
   2403  1.1  riastrad 		if (ret)
   2404  1.1  riastrad 			return ret;
   2405  1.1  riastrad 
   2406  1.1  riastrad 		state->cdclk.pipe = INVALID_PIPE;
   2407  1.1  riastrad 
   2408  1.1  riastrad 		DRM_DEBUG_KMS("Modeset required for cdclk change\n");
   2409  1.1  riastrad 	}
   2410  1.1  riastrad 
   2411  1.1  riastrad 	DRM_DEBUG_KMS("New cdclk calculated to be logical %u kHz, actual %u kHz\n",
   2412  1.1  riastrad 		      state->cdclk.logical.cdclk,
   2413  1.1  riastrad 		      state->cdclk.actual.cdclk);
   2414  1.1  riastrad 	DRM_DEBUG_KMS("New voltage level calculated to be logical %u, actual %u\n",
   2415  1.1  riastrad 		      state->cdclk.logical.voltage_level,
   2416  1.1  riastrad 		      state->cdclk.actual.voltage_level);
   2417  1.1  riastrad 
   2418  1.1  riastrad 	return 0;
   2419  1.1  riastrad }
   2420  1.1  riastrad 
   2421  1.1  riastrad static int intel_compute_max_dotclk(struct drm_i915_private *dev_priv)
   2422  1.1  riastrad {
   2423  1.1  riastrad 	int max_cdclk_freq = dev_priv->max_cdclk_freq;
   2424  1.1  riastrad 
   2425  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
   2426  1.1  riastrad 		return 2 * max_cdclk_freq;
   2427  1.1  riastrad 	else if (IS_GEN(dev_priv, 9) ||
   2428  1.1  riastrad 		 IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
   2429  1.1  riastrad 		return max_cdclk_freq;
   2430  1.1  riastrad 	else if (IS_CHERRYVIEW(dev_priv))
   2431  1.1  riastrad 		return max_cdclk_freq*95/100;
   2432  1.1  riastrad 	else if (INTEL_GEN(dev_priv) < 4)
   2433  1.1  riastrad 		return 2*max_cdclk_freq*90/100;
   2434  1.1  riastrad 	else
   2435  1.1  riastrad 		return max_cdclk_freq*90/100;
   2436  1.1  riastrad }
   2437  1.1  riastrad 
   2438  1.1  riastrad /**
   2439  1.1  riastrad  * intel_update_max_cdclk - Determine the maximum support CDCLK frequency
   2440  1.1  riastrad  * @dev_priv: i915 device
   2441  1.1  riastrad  *
   2442  1.1  riastrad  * Determine the maximum CDCLK frequency the platform supports, and also
   2443  1.1  riastrad  * derive the maximum dot clock frequency the maximum CDCLK frequency
   2444  1.1  riastrad  * allows.
   2445  1.1  riastrad  */
   2446  1.1  riastrad void intel_update_max_cdclk(struct drm_i915_private *dev_priv)
   2447  1.1  riastrad {
   2448  1.1  riastrad 	if (IS_ELKHARTLAKE(dev_priv)) {
   2449  1.1  riastrad 		if (dev_priv->cdclk.hw.ref == 24000)
   2450  1.1  riastrad 			dev_priv->max_cdclk_freq = 552000;
   2451  1.1  riastrad 		else
   2452  1.1  riastrad 			dev_priv->max_cdclk_freq = 556800;
   2453  1.1  riastrad 	} else if (INTEL_GEN(dev_priv) >= 11) {
   2454  1.1  riastrad 		if (dev_priv->cdclk.hw.ref == 24000)
   2455  1.1  riastrad 			dev_priv->max_cdclk_freq = 648000;
   2456  1.1  riastrad 		else
   2457  1.1  riastrad 			dev_priv->max_cdclk_freq = 652800;
   2458  1.1  riastrad 	} else if (IS_CANNONLAKE(dev_priv)) {
   2459  1.1  riastrad 		dev_priv->max_cdclk_freq = 528000;
   2460  1.1  riastrad 	} else if (IS_GEN9_BC(dev_priv)) {
   2461  1.1  riastrad 		u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
   2462  1.1  riastrad 		int max_cdclk, vco;
   2463  1.1  riastrad 
   2464  1.1  riastrad 		vco = dev_priv->skl_preferred_vco_freq;
   2465  1.1  riastrad 		WARN_ON(vco != 8100000 && vco != 8640000);
   2466  1.1  riastrad 
   2467  1.1  riastrad 		/*
   2468  1.1  riastrad 		 * Use the lower (vco 8640) cdclk values as a
   2469  1.1  riastrad 		 * first guess. skl_calc_cdclk() will correct it
   2470  1.1  riastrad 		 * if the preferred vco is 8100 instead.
   2471  1.1  riastrad 		 */
   2472  1.1  riastrad 		if (limit == SKL_DFSM_CDCLK_LIMIT_675)
   2473  1.1  riastrad 			max_cdclk = 617143;
   2474  1.1  riastrad 		else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
   2475  1.1  riastrad 			max_cdclk = 540000;
   2476  1.1  riastrad 		else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
   2477  1.1  riastrad 			max_cdclk = 432000;
   2478  1.1  riastrad 		else
   2479  1.1  riastrad 			max_cdclk = 308571;
   2480  1.1  riastrad 
   2481  1.1  riastrad 		dev_priv->max_cdclk_freq = skl_calc_cdclk(max_cdclk, vco);
   2482  1.1  riastrad 	} else if (IS_GEMINILAKE(dev_priv)) {
   2483  1.1  riastrad 		dev_priv->max_cdclk_freq = 316800;
   2484  1.1  riastrad 	} else if (IS_BROXTON(dev_priv)) {
   2485  1.1  riastrad 		dev_priv->max_cdclk_freq = 624000;
   2486  1.1  riastrad 	} else if (IS_BROADWELL(dev_priv))  {
   2487  1.1  riastrad 		/*
   2488  1.1  riastrad 		 * FIXME with extra cooling we can allow
   2489  1.1  riastrad 		 * 540 MHz for ULX and 675 Mhz for ULT.
   2490  1.1  riastrad 		 * How can we know if extra cooling is
   2491  1.1  riastrad 		 * available? PCI ID, VTB, something else?
   2492  1.1  riastrad 		 */
   2493  1.1  riastrad 		if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
   2494  1.1  riastrad 			dev_priv->max_cdclk_freq = 450000;
   2495  1.1  riastrad 		else if (IS_BDW_ULX(dev_priv))
   2496  1.1  riastrad 			dev_priv->max_cdclk_freq = 450000;
   2497  1.1  riastrad 		else if (IS_BDW_ULT(dev_priv))
   2498  1.1  riastrad 			dev_priv->max_cdclk_freq = 540000;
   2499  1.1  riastrad 		else
   2500  1.1  riastrad 			dev_priv->max_cdclk_freq = 675000;
   2501  1.1  riastrad 	} else if (IS_CHERRYVIEW(dev_priv)) {
   2502  1.1  riastrad 		dev_priv->max_cdclk_freq = 320000;
   2503  1.1  riastrad 	} else if (IS_VALLEYVIEW(dev_priv)) {
   2504  1.1  riastrad 		dev_priv->max_cdclk_freq = 400000;
   2505  1.1  riastrad 	} else {
   2506  1.1  riastrad 		/* otherwise assume cdclk is fixed */
   2507  1.1  riastrad 		dev_priv->max_cdclk_freq = dev_priv->cdclk.hw.cdclk;
   2508  1.1  riastrad 	}
   2509  1.1  riastrad 
   2510  1.1  riastrad 	dev_priv->max_dotclk_freq = intel_compute_max_dotclk(dev_priv);
   2511  1.1  riastrad 
   2512  1.1  riastrad 	DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
   2513  1.1  riastrad 			 dev_priv->max_cdclk_freq);
   2514  1.1  riastrad 
   2515  1.1  riastrad 	DRM_DEBUG_DRIVER("Max dotclock rate: %d kHz\n",
   2516  1.1  riastrad 			 dev_priv->max_dotclk_freq);
   2517  1.1  riastrad }
   2518  1.1  riastrad 
   2519  1.1  riastrad /**
   2520  1.1  riastrad  * intel_update_cdclk - Determine the current CDCLK frequency
   2521  1.1  riastrad  * @dev_priv: i915 device
   2522  1.1  riastrad  *
   2523  1.1  riastrad  * Determine the current CDCLK frequency.
   2524  1.1  riastrad  */
   2525  1.1  riastrad void intel_update_cdclk(struct drm_i915_private *dev_priv)
   2526  1.1  riastrad {
   2527  1.1  riastrad 	dev_priv->display.get_cdclk(dev_priv, &dev_priv->cdclk.hw);
   2528  1.1  riastrad 
   2529  1.1  riastrad 	/*
   2530  1.1  riastrad 	 * 9:0 CMBUS [sic] CDCLK frequency (cdfreq):
   2531  1.1  riastrad 	 * Programmng [sic] note: bit[9:2] should be programmed to the number
   2532  1.1  riastrad 	 * of cdclk that generates 4MHz reference clock freq which is used to
   2533  1.1  riastrad 	 * generate GMBus clock. This will vary with the cdclk freq.
   2534  1.1  riastrad 	 */
   2535  1.1  riastrad 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
   2536  1.1  riastrad 		I915_WRITE(GMBUSFREQ_VLV,
   2537  1.1  riastrad 			   DIV_ROUND_UP(dev_priv->cdclk.hw.cdclk, 1000));
   2538  1.1  riastrad }
   2539  1.1  riastrad 
   2540  1.1  riastrad static int cnp_rawclk(struct drm_i915_private *dev_priv)
   2541  1.1  riastrad {
   2542  1.1  riastrad 	u32 rawclk;
   2543  1.1  riastrad 	int divider, fraction;
   2544  1.1  riastrad 
   2545  1.1  riastrad 	if (I915_READ(SFUSE_STRAP) & SFUSE_STRAP_RAW_FREQUENCY) {
   2546  1.1  riastrad 		/* 24 MHz */
   2547  1.1  riastrad 		divider = 24000;
   2548  1.1  riastrad 		fraction = 0;
   2549  1.1  riastrad 	} else {
   2550  1.1  riastrad 		/* 19.2 MHz */
   2551  1.1  riastrad 		divider = 19000;
   2552  1.1  riastrad 		fraction = 200;
   2553  1.1  riastrad 	}
   2554  1.1  riastrad 
   2555  1.1  riastrad 	rawclk = CNP_RAWCLK_DIV(divider / 1000);
   2556  1.1  riastrad 	if (fraction) {
   2557  1.1  riastrad 		int numerator = 1;
   2558  1.1  riastrad 
   2559  1.1  riastrad 		rawclk |= CNP_RAWCLK_DEN(DIV_ROUND_CLOSEST(numerator * 1000,
   2560  1.1  riastrad 							   fraction) - 1);
   2561  1.1  riastrad 		if (INTEL_PCH_TYPE(dev_priv) >= PCH_ICP)
   2562  1.1  riastrad 			rawclk |= ICP_RAWCLK_NUM(numerator);
   2563  1.1  riastrad 	}
   2564  1.1  riastrad 
   2565  1.1  riastrad 	I915_WRITE(PCH_RAWCLK_FREQ, rawclk);
   2566  1.1  riastrad 	return divider + fraction;
   2567  1.1  riastrad }
   2568  1.1  riastrad 
   2569  1.1  riastrad static int pch_rawclk(struct drm_i915_private *dev_priv)
   2570  1.1  riastrad {
   2571  1.1  riastrad 	return (I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK) * 1000;
   2572  1.1  riastrad }
   2573  1.1  riastrad 
   2574  1.1  riastrad static int vlv_hrawclk(struct drm_i915_private *dev_priv)
   2575  1.1  riastrad {
   2576  1.1  riastrad 	/* RAWCLK_FREQ_VLV register updated from power well code */
   2577  1.1  riastrad 	return vlv_get_cck_clock_hpll(dev_priv, "hrawclk",
   2578  1.1  riastrad 				      CCK_DISPLAY_REF_CLOCK_CONTROL);
   2579  1.1  riastrad }
   2580  1.1  riastrad 
   2581  1.1  riastrad static int g4x_hrawclk(struct drm_i915_private *dev_priv)
   2582  1.1  riastrad {
   2583  1.1  riastrad 	u32 clkcfg;
   2584  1.1  riastrad 
   2585  1.1  riastrad 	/* hrawclock is 1/4 the FSB frequency */
   2586  1.1  riastrad 	clkcfg = I915_READ(CLKCFG);
   2587  1.1  riastrad 	switch (clkcfg & CLKCFG_FSB_MASK) {
   2588  1.1  riastrad 	case CLKCFG_FSB_400:
   2589  1.1  riastrad 		return 100000;
   2590  1.1  riastrad 	case CLKCFG_FSB_533:
   2591  1.1  riastrad 		return 133333;
   2592  1.1  riastrad 	case CLKCFG_FSB_667:
   2593  1.1  riastrad 		return 166667;
   2594  1.1  riastrad 	case CLKCFG_FSB_800:
   2595  1.1  riastrad 		return 200000;
   2596  1.1  riastrad 	case CLKCFG_FSB_1067:
   2597  1.1  riastrad 	case CLKCFG_FSB_1067_ALT:
   2598  1.1  riastrad 		return 266667;
   2599  1.1  riastrad 	case CLKCFG_FSB_1333:
   2600  1.1  riastrad 	case CLKCFG_FSB_1333_ALT:
   2601  1.1  riastrad 		return 333333;
   2602  1.1  riastrad 	default:
   2603  1.1  riastrad 		return 133333;
   2604  1.1  riastrad 	}
   2605  1.1  riastrad }
   2606  1.1  riastrad 
   2607  1.1  riastrad /**
   2608  1.1  riastrad  * intel_update_rawclk - Determine the current RAWCLK frequency
   2609  1.1  riastrad  * @dev_priv: i915 device
   2610  1.1  riastrad  *
   2611  1.1  riastrad  * Determine the current RAWCLK frequency. RAWCLK is a fixed
   2612  1.1  riastrad  * frequency clock so this needs to done only once.
   2613  1.1  riastrad  */
   2614  1.1  riastrad void intel_update_rawclk(struct drm_i915_private *dev_priv)
   2615  1.1  riastrad {
   2616  1.1  riastrad 	if (INTEL_PCH_TYPE(dev_priv) >= PCH_CNP)
   2617  1.1  riastrad 		dev_priv->rawclk_freq = cnp_rawclk(dev_priv);
   2618  1.1  riastrad 	else if (HAS_PCH_SPLIT(dev_priv))
   2619  1.1  riastrad 		dev_priv->rawclk_freq = pch_rawclk(dev_priv);
   2620  1.1  riastrad 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
   2621  1.1  riastrad 		dev_priv->rawclk_freq = vlv_hrawclk(dev_priv);
   2622  1.1  riastrad 	else if (IS_G4X(dev_priv) || IS_PINEVIEW(dev_priv))
   2623  1.1  riastrad 		dev_priv->rawclk_freq = g4x_hrawclk(dev_priv);
   2624  1.1  riastrad 	else
   2625  1.1  riastrad 		/* no rawclk on other platforms, or no need to know it */
   2626  1.1  riastrad 		return;
   2627  1.1  riastrad 
   2628  1.1  riastrad 	DRM_DEBUG_DRIVER("rawclk rate: %d kHz\n", dev_priv->rawclk_freq);
   2629  1.1  riastrad }
   2630  1.1  riastrad 
   2631  1.1  riastrad /**
   2632  1.1  riastrad  * intel_init_cdclk_hooks - Initialize CDCLK related modesetting hooks
   2633  1.1  riastrad  * @dev_priv: i915 device
   2634  1.1  riastrad  */
   2635  1.1  riastrad void intel_init_cdclk_hooks(struct drm_i915_private *dev_priv)
   2636  1.1  riastrad {
   2637  1.1  riastrad 	if (IS_ELKHARTLAKE(dev_priv)) {
   2638  1.1  riastrad 		dev_priv->display.set_cdclk = bxt_set_cdclk;
   2639  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
   2640  1.1  riastrad 		dev_priv->display.calc_voltage_level = ehl_calc_voltage_level;
   2641  1.1  riastrad 		dev_priv->cdclk.table = icl_cdclk_table;
   2642  1.1  riastrad 	} else if (INTEL_GEN(dev_priv) >= 11) {
   2643  1.1  riastrad 		dev_priv->display.set_cdclk = bxt_set_cdclk;
   2644  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
   2645  1.1  riastrad 		dev_priv->display.calc_voltage_level = icl_calc_voltage_level;
   2646  1.1  riastrad 		dev_priv->cdclk.table = icl_cdclk_table;
   2647  1.1  riastrad 	} else if (IS_CANNONLAKE(dev_priv)) {
   2648  1.1  riastrad 		dev_priv->display.set_cdclk = bxt_set_cdclk;
   2649  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
   2650  1.1  riastrad 		dev_priv->display.calc_voltage_level = cnl_calc_voltage_level;
   2651  1.1  riastrad 		dev_priv->cdclk.table = cnl_cdclk_table;
   2652  1.1  riastrad 	} else if (IS_GEN9_LP(dev_priv)) {
   2653  1.1  riastrad 		dev_priv->display.set_cdclk = bxt_set_cdclk;
   2654  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = bxt_modeset_calc_cdclk;
   2655  1.1  riastrad 		dev_priv->display.calc_voltage_level = bxt_calc_voltage_level;
   2656  1.1  riastrad 		if (IS_GEMINILAKE(dev_priv))
   2657  1.1  riastrad 			dev_priv->cdclk.table = glk_cdclk_table;
   2658  1.1  riastrad 		else
   2659  1.1  riastrad 			dev_priv->cdclk.table = bxt_cdclk_table;
   2660  1.1  riastrad 	} else if (IS_GEN9_BC(dev_priv)) {
   2661  1.1  riastrad 		dev_priv->display.set_cdclk = skl_set_cdclk;
   2662  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = skl_modeset_calc_cdclk;
   2663  1.1  riastrad 	} else if (IS_BROADWELL(dev_priv)) {
   2664  1.1  riastrad 		dev_priv->display.set_cdclk = bdw_set_cdclk;
   2665  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = bdw_modeset_calc_cdclk;
   2666  1.1  riastrad 	} else if (IS_CHERRYVIEW(dev_priv)) {
   2667  1.1  riastrad 		dev_priv->display.set_cdclk = chv_set_cdclk;
   2668  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = vlv_modeset_calc_cdclk;
   2669  1.1  riastrad 	} else if (IS_VALLEYVIEW(dev_priv)) {
   2670  1.1  riastrad 		dev_priv->display.set_cdclk = vlv_set_cdclk;
   2671  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = vlv_modeset_calc_cdclk;
   2672  1.1  riastrad 	} else {
   2673  1.1  riastrad 		dev_priv->display.modeset_calc_cdclk = fixed_modeset_calc_cdclk;
   2674  1.1  riastrad 	}
   2675  1.1  riastrad 
   2676  1.1  riastrad 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEN9_LP(dev_priv))
   2677  1.1  riastrad 		dev_priv->display.get_cdclk = bxt_get_cdclk;
   2678  1.1  riastrad 	else if (IS_GEN9_BC(dev_priv))
   2679  1.1  riastrad 		dev_priv->display.get_cdclk = skl_get_cdclk;
   2680  1.1  riastrad 	else if (IS_BROADWELL(dev_priv))
   2681  1.1  riastrad 		dev_priv->display.get_cdclk = bdw_get_cdclk;
   2682  1.1  riastrad 	else if (IS_HASWELL(dev_priv))
   2683  1.1  riastrad 		dev_priv->display.get_cdclk = hsw_get_cdclk;
   2684  1.1  riastrad 	else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
   2685  1.1  riastrad 		dev_priv->display.get_cdclk = vlv_get_cdclk;
   2686  1.1  riastrad 	else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv))
   2687  1.1  riastrad 		dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
   2688  1.1  riastrad 	else if (IS_GEN(dev_priv, 5))
   2689  1.1  riastrad 		dev_priv->display.get_cdclk = fixed_450mhz_get_cdclk;
   2690  1.1  riastrad 	else if (IS_GM45(dev_priv))
   2691  1.1  riastrad 		dev_priv->display.get_cdclk = gm45_get_cdclk;
   2692  1.1  riastrad 	else if (IS_G45(dev_priv))
   2693  1.1  riastrad 		dev_priv->display.get_cdclk = g33_get_cdclk;
   2694  1.1  riastrad 	else if (IS_I965GM(dev_priv))
   2695  1.1  riastrad 		dev_priv->display.get_cdclk = i965gm_get_cdclk;
   2696  1.1  riastrad 	else if (IS_I965G(dev_priv))
   2697  1.1  riastrad 		dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
   2698  1.1  riastrad 	else if (IS_PINEVIEW(dev_priv))
   2699  1.1  riastrad 		dev_priv->display.get_cdclk = pnv_get_cdclk;
   2700  1.1  riastrad 	else if (IS_G33(dev_priv))
   2701  1.1  riastrad 		dev_priv->display.get_cdclk = g33_get_cdclk;
   2702  1.1  riastrad 	else if (IS_I945GM(dev_priv))
   2703  1.1  riastrad 		dev_priv->display.get_cdclk = i945gm_get_cdclk;
   2704  1.1  riastrad 	else if (IS_I945G(dev_priv))
   2705  1.1  riastrad 		dev_priv->display.get_cdclk = fixed_400mhz_get_cdclk;
   2706  1.1  riastrad 	else if (IS_I915GM(dev_priv))
   2707  1.1  riastrad 		dev_priv->display.get_cdclk = i915gm_get_cdclk;
   2708  1.1  riastrad 	else if (IS_I915G(dev_priv))
   2709  1.1  riastrad 		dev_priv->display.get_cdclk = fixed_333mhz_get_cdclk;
   2710  1.1  riastrad 	else if (IS_I865G(dev_priv))
   2711  1.1  riastrad 		dev_priv->display.get_cdclk = fixed_266mhz_get_cdclk;
   2712  1.1  riastrad 	else if (IS_I85X(dev_priv))
   2713  1.1  riastrad 		dev_priv->display.get_cdclk = i85x_get_cdclk;
   2714  1.1  riastrad 	else if (IS_I845G(dev_priv))
   2715  1.1  riastrad 		dev_priv->display.get_cdclk = fixed_200mhz_get_cdclk;
   2716  1.1  riastrad 	else { /* 830 */
   2717  1.1  riastrad 		WARN(!IS_I830(dev_priv),
   2718  1.1  riastrad 		     "Unknown platform. Assuming 133 MHz CDCLK\n");
   2719  1.1  riastrad 		dev_priv->display.get_cdclk = fixed_133mhz_get_cdclk;
   2720  1.1  riastrad 	}
   2721  1.1  riastrad }
   2722