Home | History | Annotate | Line # | Download | only in i915
      1  1.26  riastrad /*	$NetBSD: intel_pm.c,v 1.26 2021/12/18 23:45:28 riastradh Exp $	*/
      2  1.10  riastrad 
      3   1.1  riastrad /*
      4   1.1  riastrad  * Copyright  2012 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 DEALINGS
     23   1.1  riastrad  * IN THE SOFTWARE.
     24   1.1  riastrad  *
     25   1.1  riastrad  * Authors:
     26   1.1  riastrad  *    Eugeni Dodonov <eugeni.dodonov (at) intel.com>
     27   1.1  riastrad  *
     28   1.1  riastrad  */
     29   1.1  riastrad 
     30  1.10  riastrad #include <sys/cdefs.h>
     31  1.26  riastrad __KERNEL_RCSID(0, "$NetBSD: intel_pm.c,v 1.26 2021/12/18 23:45:28 riastradh Exp $");
     32  1.26  riastrad 
     33  1.26  riastrad #include <linux/module.h>
     34  1.26  riastrad #include <linux/pm_runtime.h>
     35  1.26  riastrad 
     36  1.26  riastrad #include <drm/drm_atomic_helper.h>
     37  1.26  riastrad #include <drm/drm_fourcc.h>
     38  1.26  riastrad #include <drm/drm_plane_helper.h>
     39  1.26  riastrad 
     40  1.26  riastrad #include "display/intel_atomic.h"
     41  1.26  riastrad #include "display/intel_display_types.h"
     42  1.26  riastrad #include "display/intel_fbc.h"
     43  1.26  riastrad #include "display/intel_sprite.h"
     44  1.26  riastrad 
     45  1.26  riastrad #include "gt/intel_llc.h"
     46  1.10  riastrad 
     47   1.1  riastrad #include "i915_drv.h"
     48  1.26  riastrad #include "i915_irq.h"
     49  1.15  riastrad #include "i915_trace.h"
     50  1.26  riastrad #include "intel_pm.h"
     51  1.26  riastrad #include "intel_sideband.h"
     52   1.2  riastrad #ifndef __NetBSD__
     53   1.1  riastrad #include "../../../platform/x86/intel_ips.h"
     54   1.2  riastrad #endif
     55   1.1  riastrad 
     56  1.21  riastrad #include <linux/nbsd-namespace.h>
     57  1.21  riastrad 
     58  1.26  riastrad static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
     59  1.26  riastrad {
     60  1.26  riastrad 	if (HAS_LLC(dev_priv)) {
     61  1.26  riastrad 		/*
     62  1.26  riastrad 		 * WaCompressedResourceDisplayNewHashMode:skl,kbl
     63  1.26  riastrad 		 * Display WA #0390: skl,kbl
     64  1.26  riastrad 		 *
     65  1.26  riastrad 		 * Must match Sampler, Pixel Back End, and Media. See
     66  1.26  riastrad 		 * WaCompressedResourceSamplerPbeMediaNewHashMode.
     67  1.26  riastrad 		 */
     68  1.26  riastrad 		I915_WRITE(CHICKEN_PAR1_1,
     69  1.26  riastrad 			   I915_READ(CHICKEN_PAR1_1) |
     70  1.26  riastrad 			   SKL_DE_COMPRESSED_HASH_MODE);
     71  1.26  riastrad 	}
     72  1.26  riastrad 
     73  1.26  riastrad 	/* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
     74  1.26  riastrad 	I915_WRITE(CHICKEN_PAR1_1,
     75  1.26  riastrad 		   I915_READ(CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
     76  1.26  riastrad 
     77  1.26  riastrad 	/* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
     78  1.26  riastrad 	I915_WRITE(GEN8_CHICKEN_DCPR_1,
     79  1.26  riastrad 		   I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
     80  1.26  riastrad 
     81  1.26  riastrad 	/* WaFbcTurnOffFbcWatermark:skl,bxt,kbl,cfl */
     82  1.26  riastrad 	/* WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl */
     83  1.26  riastrad 	I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
     84  1.26  riastrad 		   DISP_FBC_WM_DIS |
     85  1.26  riastrad 		   DISP_FBC_MEMORY_WAKE);
     86  1.26  riastrad 
     87  1.26  riastrad 	/* WaFbcHighMemBwCorruptionAvoidance:skl,bxt,kbl,cfl */
     88  1.26  riastrad 	I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
     89  1.26  riastrad 		   ILK_DPFC_DISABLE_DUMMY0);
     90  1.26  riastrad 
     91  1.26  riastrad 	if (IS_SKYLAKE(dev_priv)) {
     92  1.26  riastrad 		/* WaDisableDopClockGating */
     93  1.26  riastrad 		I915_WRITE(GEN7_MISCCPCTL, I915_READ(GEN7_MISCCPCTL)
     94  1.26  riastrad 			   & ~GEN7_DOP_CLOCK_GATE_ENABLE);
     95  1.26  riastrad 	}
     96  1.26  riastrad }
     97   1.1  riastrad 
     98  1.26  riastrad static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
     99   1.1  riastrad {
    100  1.26  riastrad 	gen9_init_clock_gating(dev_priv);
    101   1.1  riastrad 
    102  1.10  riastrad 	/* WaDisableSDEUnitClockGating:bxt */
    103  1.10  riastrad 	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
    104  1.10  riastrad 		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
    105   1.1  riastrad 
    106  1.10  riastrad 	/*
    107  1.10  riastrad 	 * FIXME:
    108  1.10  riastrad 	 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
    109   1.1  riastrad 	 */
    110  1.10  riastrad 	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
    111  1.10  riastrad 		   GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
    112  1.20      maya 
    113  1.20      maya 	/*
    114  1.26  riastrad 	 * Wa: Backlight PWM may stop in the asserted state, causing backlight
    115  1.26  riastrad 	 * to stay fully on.
    116  1.26  riastrad 	 */
    117  1.26  riastrad 	I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
    118  1.26  riastrad 		   PWM1_GATING_DIS | PWM2_GATING_DIS);
    119  1.26  riastrad 
    120  1.26  riastrad 	/*
    121  1.20      maya 	 * Lower the display internal timeout.
    122  1.20      maya 	 * This is needed to avoid any hard hangs when DSI port PLL
    123  1.20      maya 	 * is off and a MMIO access is attempted by any privilege
    124  1.20      maya 	 * application, using batch buffers or any other means.
    125  1.20      maya 	 */
    126  1.20      maya 	I915_WRITE(RM_TIMEOUT, MMIO_TIMEOUT_US(950));
    127   1.1  riastrad }
    128   1.1  riastrad 
    129  1.26  riastrad static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
    130  1.26  riastrad {
    131  1.26  riastrad 	gen9_init_clock_gating(dev_priv);
    132  1.26  riastrad 
    133  1.26  riastrad 	/*
    134  1.26  riastrad 	 * WaDisablePWMClockGating:glk
    135  1.26  riastrad 	 * Backlight PWM may stop in the asserted state, causing backlight
    136  1.26  riastrad 	 * to stay fully on.
    137  1.26  riastrad 	 */
    138  1.26  riastrad 	I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
    139  1.26  riastrad 		   PWM1_GATING_DIS | PWM2_GATING_DIS);
    140  1.26  riastrad 
    141  1.26  riastrad 	/* WaDDIIOTimeout:glk */
    142  1.26  riastrad 	if (IS_GLK_REVID(dev_priv, 0, GLK_REVID_A1)) {
    143  1.26  riastrad 		u32 val = I915_READ(CHICKEN_MISC_2);
    144  1.26  riastrad 		val &= ~(GLK_CL0_PWR_DOWN |
    145  1.26  riastrad 			 GLK_CL1_PWR_DOWN |
    146  1.26  riastrad 			 GLK_CL2_PWR_DOWN);
    147  1.26  riastrad 		I915_WRITE(CHICKEN_MISC_2, val);
    148  1.26  riastrad 	}
    149  1.26  riastrad 
    150  1.26  riastrad }
    151  1.26  riastrad 
    152  1.26  riastrad static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
    153   1.1  riastrad {
    154   1.1  riastrad 	u32 tmp;
    155   1.1  riastrad 
    156   1.1  riastrad 	tmp = I915_READ(CLKCFG);
    157   1.1  riastrad 
    158   1.1  riastrad 	switch (tmp & CLKCFG_FSB_MASK) {
    159   1.1  riastrad 	case CLKCFG_FSB_533:
    160   1.1  riastrad 		dev_priv->fsb_freq = 533; /* 133*4 */
    161   1.1  riastrad 		break;
    162   1.1  riastrad 	case CLKCFG_FSB_800:
    163   1.1  riastrad 		dev_priv->fsb_freq = 800; /* 200*4 */
    164   1.1  riastrad 		break;
    165   1.1  riastrad 	case CLKCFG_FSB_667:
    166   1.1  riastrad 		dev_priv->fsb_freq =  667; /* 167*4 */
    167   1.1  riastrad 		break;
    168   1.1  riastrad 	case CLKCFG_FSB_400:
    169   1.1  riastrad 		dev_priv->fsb_freq = 400; /* 100*4 */
    170   1.1  riastrad 		break;
    171   1.1  riastrad 	}
    172   1.1  riastrad 
    173   1.1  riastrad 	switch (tmp & CLKCFG_MEM_MASK) {
    174   1.1  riastrad 	case CLKCFG_MEM_533:
    175   1.1  riastrad 		dev_priv->mem_freq = 533;
    176   1.1  riastrad 		break;
    177   1.1  riastrad 	case CLKCFG_MEM_667:
    178   1.1  riastrad 		dev_priv->mem_freq = 667;
    179   1.1  riastrad 		break;
    180   1.1  riastrad 	case CLKCFG_MEM_800:
    181   1.1  riastrad 		dev_priv->mem_freq = 800;
    182   1.1  riastrad 		break;
    183   1.1  riastrad 	}
    184   1.1  riastrad 
    185   1.1  riastrad 	/* detect pineview DDR3 setting */
    186   1.1  riastrad 	tmp = I915_READ(CSHRDDR3CTL);
    187   1.1  riastrad 	dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
    188   1.1  riastrad }
    189   1.1  riastrad 
    190  1.26  riastrad static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
    191   1.1  riastrad {
    192   1.1  riastrad 	u16 ddrpll, csipll;
    193   1.1  riastrad 
    194  1.26  riastrad 	ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
    195  1.26  riastrad 	csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
    196   1.1  riastrad 
    197   1.1  riastrad 	switch (ddrpll & 0xff) {
    198   1.1  riastrad 	case 0xc:
    199   1.1  riastrad 		dev_priv->mem_freq = 800;
    200   1.1  riastrad 		break;
    201   1.1  riastrad 	case 0x10:
    202   1.1  riastrad 		dev_priv->mem_freq = 1066;
    203   1.1  riastrad 		break;
    204   1.1  riastrad 	case 0x14:
    205   1.1  riastrad 		dev_priv->mem_freq = 1333;
    206   1.1  riastrad 		break;
    207   1.1  riastrad 	case 0x18:
    208   1.1  riastrad 		dev_priv->mem_freq = 1600;
    209   1.1  riastrad 		break;
    210   1.1  riastrad 	default:
    211  1.26  riastrad 		drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
    212  1.26  riastrad 			ddrpll & 0xff);
    213   1.1  riastrad 		dev_priv->mem_freq = 0;
    214   1.1  riastrad 		break;
    215   1.1  riastrad 	}
    216   1.1  riastrad 
    217   1.1  riastrad 	switch (csipll & 0x3ff) {
    218   1.1  riastrad 	case 0x00c:
    219   1.1  riastrad 		dev_priv->fsb_freq = 3200;
    220   1.1  riastrad 		break;
    221   1.1  riastrad 	case 0x00e:
    222   1.1  riastrad 		dev_priv->fsb_freq = 3733;
    223   1.1  riastrad 		break;
    224   1.1  riastrad 	case 0x010:
    225   1.1  riastrad 		dev_priv->fsb_freq = 4266;
    226   1.1  riastrad 		break;
    227   1.1  riastrad 	case 0x012:
    228   1.1  riastrad 		dev_priv->fsb_freq = 4800;
    229   1.1  riastrad 		break;
    230   1.1  riastrad 	case 0x014:
    231   1.1  riastrad 		dev_priv->fsb_freq = 5333;
    232   1.1  riastrad 		break;
    233   1.1  riastrad 	case 0x016:
    234   1.1  riastrad 		dev_priv->fsb_freq = 5866;
    235   1.1  riastrad 		break;
    236   1.1  riastrad 	case 0x018:
    237   1.1  riastrad 		dev_priv->fsb_freq = 6400;
    238   1.1  riastrad 		break;
    239   1.1  riastrad 	default:
    240  1.26  riastrad 		drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
    241  1.26  riastrad 			csipll & 0x3ff);
    242   1.1  riastrad 		dev_priv->fsb_freq = 0;
    243   1.1  riastrad 		break;
    244   1.1  riastrad 	}
    245   1.1  riastrad }
    246   1.1  riastrad 
    247   1.1  riastrad static const struct cxsr_latency cxsr_latency_table[] = {
    248   1.1  riastrad 	{1, 0, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
    249   1.1  riastrad 	{1, 0, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
    250   1.1  riastrad 	{1, 0, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
    251   1.1  riastrad 	{1, 1, 800, 667, 6420, 36420, 6873, 36873},    /* DDR3-667 SC */
    252   1.1  riastrad 	{1, 1, 800, 800, 5902, 35902, 6318, 36318},    /* DDR3-800 SC */
    253   1.1  riastrad 
    254   1.1  riastrad 	{1, 0, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
    255   1.1  riastrad 	{1, 0, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
    256   1.1  riastrad 	{1, 0, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
    257   1.1  riastrad 	{1, 1, 667, 667, 6438, 36438, 6911, 36911},    /* DDR3-667 SC */
    258   1.1  riastrad 	{1, 1, 667, 800, 5941, 35941, 6377, 36377},    /* DDR3-800 SC */
    259   1.1  riastrad 
    260   1.1  riastrad 	{1, 0, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
    261   1.1  riastrad 	{1, 0, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
    262   1.1  riastrad 	{1, 0, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
    263   1.1  riastrad 	{1, 1, 400, 667, 6509, 36509, 7062, 37062},    /* DDR3-667 SC */
    264   1.1  riastrad 	{1, 1, 400, 800, 5985, 35985, 6501, 36501},    /* DDR3-800 SC */
    265   1.1  riastrad 
    266   1.1  riastrad 	{0, 0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
    267   1.1  riastrad 	{0, 0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
    268   1.1  riastrad 	{0, 0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
    269   1.1  riastrad 	{0, 1, 800, 667, 6476, 36476, 6955, 36955},    /* DDR3-667 SC */
    270   1.1  riastrad 	{0, 1, 800, 800, 5958, 35958, 6400, 36400},    /* DDR3-800 SC */
    271   1.1  riastrad 
    272   1.1  riastrad 	{0, 0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
    273   1.1  riastrad 	{0, 0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
    274   1.1  riastrad 	{0, 0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
    275   1.1  riastrad 	{0, 1, 667, 667, 6494, 36494, 6993, 36993},    /* DDR3-667 SC */
    276   1.1  riastrad 	{0, 1, 667, 800, 5998, 35998, 6460, 36460},    /* DDR3-800 SC */
    277   1.1  riastrad 
    278   1.1  riastrad 	{0, 0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
    279   1.1  riastrad 	{0, 0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
    280   1.1  riastrad 	{0, 0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
    281   1.1  riastrad 	{0, 1, 400, 667, 6566, 36566, 7145, 37145},    /* DDR3-667 SC */
    282   1.1  riastrad 	{0, 1, 400, 800, 6042, 36042, 6584, 36584},    /* DDR3-800 SC */
    283   1.1  riastrad };
    284   1.1  riastrad 
    285  1.26  riastrad static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
    286  1.26  riastrad 							 bool is_ddr3,
    287   1.1  riastrad 							 int fsb,
    288   1.1  riastrad 							 int mem)
    289   1.1  riastrad {
    290   1.1  riastrad 	const struct cxsr_latency *latency;
    291   1.1  riastrad 	int i;
    292   1.1  riastrad 
    293   1.1  riastrad 	if (fsb == 0 || mem == 0)
    294   1.1  riastrad 		return NULL;
    295   1.1  riastrad 
    296   1.1  riastrad 	for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
    297   1.1  riastrad 		latency = &cxsr_latency_table[i];
    298   1.1  riastrad 		if (is_desktop == latency->is_desktop &&
    299   1.1  riastrad 		    is_ddr3 == latency->is_ddr3 &&
    300   1.1  riastrad 		    fsb == latency->fsb_freq && mem == latency->mem_freq)
    301   1.1  riastrad 			return latency;
    302   1.1  riastrad 	}
    303   1.1  riastrad 
    304   1.1  riastrad 	DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
    305   1.1  riastrad 
    306   1.1  riastrad 	return NULL;
    307   1.1  riastrad }
    308   1.1  riastrad 
    309  1.10  riastrad static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
    310  1.10  riastrad {
    311  1.10  riastrad 	u32 val;
    312  1.10  riastrad 
    313  1.26  riastrad 	vlv_punit_get(dev_priv);
    314  1.10  riastrad 
    315  1.10  riastrad 	val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
    316  1.10  riastrad 	if (enable)
    317  1.10  riastrad 		val &= ~FORCE_DDR_HIGH_FREQ;
    318  1.10  riastrad 	else
    319  1.10  riastrad 		val |= FORCE_DDR_HIGH_FREQ;
    320  1.10  riastrad 	val &= ~FORCE_DDR_LOW_FREQ;
    321  1.10  riastrad 	val |= FORCE_DDR_FREQ_REQ_ACK;
    322  1.10  riastrad 	vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
    323  1.10  riastrad 
    324  1.10  riastrad 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
    325  1.10  riastrad 		      FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
    326  1.26  riastrad 		drm_err(&dev_priv->drm,
    327  1.26  riastrad 			"timed out waiting for Punit DDR DVFS request\n");
    328  1.10  riastrad 
    329  1.26  riastrad 	vlv_punit_put(dev_priv);
    330  1.10  riastrad }
    331  1.10  riastrad 
    332  1.10  riastrad static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
    333  1.10  riastrad {
    334  1.10  riastrad 	u32 val;
    335  1.10  riastrad 
    336  1.26  riastrad 	vlv_punit_get(dev_priv);
    337  1.10  riastrad 
    338  1.26  riastrad 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
    339  1.10  riastrad 	if (enable)
    340  1.10  riastrad 		val |= DSP_MAXFIFO_PM5_ENABLE;
    341  1.10  riastrad 	else
    342  1.10  riastrad 		val &= ~DSP_MAXFIFO_PM5_ENABLE;
    343  1.26  riastrad 	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
    344  1.10  riastrad 
    345  1.26  riastrad 	vlv_punit_put(dev_priv);
    346  1.10  riastrad }
    347  1.10  riastrad 
    348  1.10  riastrad #define FW_WM(value, plane) \
    349  1.19   msaitoh 	(((u32)(value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
    350  1.10  riastrad 
    351  1.26  riastrad static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
    352   1.1  riastrad {
    353  1.26  riastrad 	bool was_enabled;
    354  1.10  riastrad 	u32 val;
    355  1.10  riastrad 
    356  1.26  riastrad 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
    357  1.26  riastrad 		was_enabled = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
    358  1.10  riastrad 		I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
    359  1.10  riastrad 		POSTING_READ(FW_BLC_SELF_VLV);
    360  1.26  riastrad 	} else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
    361  1.26  riastrad 		was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
    362  1.10  riastrad 		I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
    363  1.10  riastrad 		POSTING_READ(FW_BLC_SELF);
    364  1.26  riastrad 	} else if (IS_PINEVIEW(dev_priv)) {
    365  1.26  riastrad 		val = I915_READ(DSPFW3);
    366  1.26  riastrad 		was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
    367  1.26  riastrad 		if (enable)
    368  1.26  riastrad 			val |= PINEVIEW_SELF_REFRESH_EN;
    369  1.26  riastrad 		else
    370  1.26  riastrad 			val &= ~PINEVIEW_SELF_REFRESH_EN;
    371  1.10  riastrad 		I915_WRITE(DSPFW3, val);
    372  1.10  riastrad 		POSTING_READ(DSPFW3);
    373  1.26  riastrad 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
    374  1.26  riastrad 		was_enabled = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
    375  1.10  riastrad 		val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
    376  1.10  riastrad 			       _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
    377  1.10  riastrad 		I915_WRITE(FW_BLC_SELF, val);
    378  1.10  riastrad 		POSTING_READ(FW_BLC_SELF);
    379  1.26  riastrad 	} else if (IS_I915GM(dev_priv)) {
    380  1.26  riastrad 		/*
    381  1.26  riastrad 		 * FIXME can't find a bit like this for 915G, and
    382  1.26  riastrad 		 * and yet it does have the related watermark in
    383  1.26  riastrad 		 * FW_BLC_SELF. What's going on?
    384  1.26  riastrad 		 */
    385  1.26  riastrad 		was_enabled = I915_READ(INSTPM) & INSTPM_SELF_EN;
    386  1.10  riastrad 		val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
    387  1.10  riastrad 			       _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
    388  1.10  riastrad 		I915_WRITE(INSTPM, val);
    389  1.10  riastrad 		POSTING_READ(INSTPM);
    390  1.10  riastrad 	} else {
    391  1.26  riastrad 		return false;
    392  1.10  riastrad 	}
    393   1.1  riastrad 
    394  1.26  riastrad 	trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
    395  1.26  riastrad 
    396  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
    397  1.26  riastrad 		    enableddisabled(enable),
    398  1.26  riastrad 		    enableddisabled(was_enabled));
    399  1.26  riastrad 
    400  1.26  riastrad 	return was_enabled;
    401   1.1  riastrad }
    402   1.1  riastrad 
    403  1.26  riastrad /**
    404  1.26  riastrad  * intel_set_memory_cxsr - Configure CxSR state
    405  1.26  riastrad  * @dev_priv: i915 device
    406  1.26  riastrad  * @enable: Allow vs. disallow CxSR
    407  1.26  riastrad  *
    408  1.26  riastrad  * Allow or disallow the system to enter a special CxSR
    409  1.26  riastrad  * (C-state self refresh) state. What typically happens in CxSR mode
    410  1.26  riastrad  * is that several display FIFOs may get combined into a single larger
    411  1.26  riastrad  * FIFO for a particular plane (so called max FIFO mode) to allow the
    412  1.26  riastrad  * system to defer memory fetches longer, and the memory will enter
    413  1.26  riastrad  * self refresh.
    414  1.26  riastrad  *
    415  1.26  riastrad  * Note that enabling CxSR does not guarantee that the system enter
    416  1.26  riastrad  * this special mode, nor does it guarantee that the system stays
    417  1.26  riastrad  * in that mode once entered. So this just allows/disallows the system
    418  1.26  riastrad  * to autonomously utilize the CxSR mode. Other factors such as core
    419  1.26  riastrad  * C-states will affect when/if the system actually enters/exits the
    420  1.26  riastrad  * CxSR mode.
    421  1.26  riastrad  *
    422  1.26  riastrad  * Note that on VLV/CHV this actually only controls the max FIFO mode,
    423  1.26  riastrad  * and the system is free to enter/exit memory self refresh at any time
    424  1.26  riastrad  * even when the use of CxSR has been disallowed.
    425  1.26  riastrad  *
    426  1.26  riastrad  * While the system is actually in the CxSR/max FIFO mode, some plane
    427  1.26  riastrad  * control registers will not get latched on vblank. Thus in order to
    428  1.26  riastrad  * guarantee the system will respond to changes in the plane registers
    429  1.26  riastrad  * we must always disallow CxSR prior to making changes to those registers.
    430  1.26  riastrad  * Unfortunately the system will re-evaluate the CxSR conditions at
    431  1.26  riastrad  * frame start which happens after vblank start (which is when the plane
    432  1.26  riastrad  * registers would get latched), so we can't proceed with the plane update
    433  1.26  riastrad  * during the same frame where we disallowed CxSR.
    434  1.26  riastrad  *
    435  1.26  riastrad  * Certain platforms also have a deeper HPLL SR mode. Fortunately the
    436  1.26  riastrad  * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
    437  1.26  riastrad  * the hardware w.r.t. HPLL SR when writing to plane registers.
    438  1.26  riastrad  * Disallowing just CxSR is sufficient.
    439  1.26  riastrad  */
    440  1.26  riastrad bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
    441  1.26  riastrad {
    442  1.26  riastrad 	bool ret;
    443  1.26  riastrad 
    444  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
    445  1.26  riastrad 	ret = _intel_set_memory_cxsr(dev_priv, enable);
    446  1.26  riastrad 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
    447  1.26  riastrad 		dev_priv->wm.vlv.cxsr = enable;
    448  1.26  riastrad 	else if (IS_G4X(dev_priv))
    449  1.26  riastrad 		dev_priv->wm.g4x.cxsr = enable;
    450  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
    451  1.26  riastrad 
    452  1.26  riastrad 	return ret;
    453  1.26  riastrad }
    454  1.10  riastrad 
    455   1.1  riastrad /*
    456   1.1  riastrad  * Latency for FIFO fetches is dependent on several factors:
    457   1.1  riastrad  *   - memory configuration (speed, channels)
    458   1.1  riastrad  *   - chipset
    459   1.1  riastrad  *   - current MCH state
    460   1.1  riastrad  * It can be fairly high in some situations, so here we assume a fairly
    461   1.1  riastrad  * pessimal value.  It's a tradeoff between extra memory fetches (if we
    462   1.1  riastrad  * set this value too high, the FIFO will fetch frequently to stay full)
    463   1.1  riastrad  * and power consumption (set it too low to save power and we might see
    464   1.1  riastrad  * FIFO underruns and display "flicker").
    465   1.1  riastrad  *
    466   1.1  riastrad  * A value of 5us seems to be a good balance; safe for very low end
    467   1.1  riastrad  * platforms but not overly aggressive on lower latency configs.
    468   1.1  riastrad  */
    469  1.10  riastrad static const int pessimal_latency_ns = 5000;
    470  1.10  riastrad 
    471  1.10  riastrad #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
    472  1.10  riastrad 	((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
    473   1.1  riastrad 
    474  1.26  riastrad static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
    475   1.1  riastrad {
    476  1.26  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
    477  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
    478  1.26  riastrad 	struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
    479  1.26  riastrad 	enum pipe pipe = crtc->pipe;
    480  1.26  riastrad 	int sprite0_start, sprite1_start;
    481   1.1  riastrad 
    482  1.10  riastrad 	switch (pipe) {
    483  1.26  riastrad 		u32 dsparb, dsparb2, dsparb3;
    484  1.10  riastrad 	case PIPE_A:
    485  1.10  riastrad 		dsparb = I915_READ(DSPARB);
    486  1.10  riastrad 		dsparb2 = I915_READ(DSPARB2);
    487  1.10  riastrad 		sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
    488  1.10  riastrad 		sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
    489  1.10  riastrad 		break;
    490  1.10  riastrad 	case PIPE_B:
    491  1.10  riastrad 		dsparb = I915_READ(DSPARB);
    492  1.10  riastrad 		dsparb2 = I915_READ(DSPARB2);
    493  1.10  riastrad 		sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
    494  1.10  riastrad 		sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
    495  1.10  riastrad 		break;
    496  1.10  riastrad 	case PIPE_C:
    497  1.10  riastrad 		dsparb2 = I915_READ(DSPARB2);
    498  1.10  riastrad 		dsparb3 = I915_READ(DSPARB3);
    499  1.10  riastrad 		sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
    500  1.10  riastrad 		sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
    501  1.10  riastrad 		break;
    502  1.10  riastrad 	default:
    503  1.26  riastrad 		MISSING_CASE(pipe);
    504  1.26  riastrad 		return;
    505  1.10  riastrad 	}
    506  1.10  riastrad 
    507  1.26  riastrad 	fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
    508  1.26  riastrad 	fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
    509  1.26  riastrad 	fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
    510  1.26  riastrad 	fifo_state->plane[PLANE_CURSOR] = 63;
    511  1.10  riastrad }
    512  1.10  riastrad 
    513  1.26  riastrad static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
    514  1.26  riastrad 			      enum i9xx_plane_id i9xx_plane)
    515  1.10  riastrad {
    516  1.26  riastrad 	u32 dsparb = I915_READ(DSPARB);
    517  1.10  riastrad 	int size;
    518  1.10  riastrad 
    519  1.10  riastrad 	size = dsparb & 0x7f;
    520  1.26  riastrad 	if (i9xx_plane == PLANE_B)
    521  1.10  riastrad 		size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
    522   1.1  riastrad 
    523  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
    524  1.26  riastrad 		    dsparb, plane_name(i9xx_plane), size);
    525   1.1  riastrad 
    526   1.1  riastrad 	return size;
    527   1.1  riastrad }
    528   1.1  riastrad 
    529  1.26  riastrad static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
    530  1.26  riastrad 			      enum i9xx_plane_id i9xx_plane)
    531   1.1  riastrad {
    532  1.26  riastrad 	u32 dsparb = I915_READ(DSPARB);
    533   1.1  riastrad 	int size;
    534   1.1  riastrad 
    535   1.1  riastrad 	size = dsparb & 0x1ff;
    536  1.26  riastrad 	if (i9xx_plane == PLANE_B)
    537   1.1  riastrad 		size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
    538   1.1  riastrad 	size >>= 1; /* Convert to cachelines */
    539   1.1  riastrad 
    540  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
    541  1.26  riastrad 		    dsparb, plane_name(i9xx_plane), size);
    542   1.1  riastrad 
    543   1.1  riastrad 	return size;
    544   1.1  riastrad }
    545   1.1  riastrad 
    546  1.26  riastrad static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
    547  1.26  riastrad 			      enum i9xx_plane_id i9xx_plane)
    548   1.1  riastrad {
    549  1.26  riastrad 	u32 dsparb = I915_READ(DSPARB);
    550   1.1  riastrad 	int size;
    551   1.1  riastrad 
    552   1.1  riastrad 	size = dsparb & 0x7f;
    553   1.1  riastrad 	size >>= 2; /* Convert to cachelines */
    554   1.1  riastrad 
    555  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
    556  1.26  riastrad 		    dsparb, plane_name(i9xx_plane), size);
    557   1.1  riastrad 
    558   1.1  riastrad 	return size;
    559   1.1  riastrad }
    560   1.1  riastrad 
    561   1.1  riastrad /* Pineview has different values for various configs */
    562  1.26  riastrad static const struct intel_watermark_params pnv_display_wm = {
    563  1.10  riastrad 	.fifo_size = PINEVIEW_DISPLAY_FIFO,
    564  1.10  riastrad 	.max_wm = PINEVIEW_MAX_WM,
    565  1.10  riastrad 	.default_wm = PINEVIEW_DFT_WM,
    566  1.10  riastrad 	.guard_size = PINEVIEW_GUARD_WM,
    567  1.10  riastrad 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
    568   1.1  riastrad };
    569  1.26  riastrad 
    570  1.26  riastrad static const struct intel_watermark_params pnv_display_hplloff_wm = {
    571  1.10  riastrad 	.fifo_size = PINEVIEW_DISPLAY_FIFO,
    572  1.10  riastrad 	.max_wm = PINEVIEW_MAX_WM,
    573  1.10  riastrad 	.default_wm = PINEVIEW_DFT_HPLLOFF_WM,
    574  1.10  riastrad 	.guard_size = PINEVIEW_GUARD_WM,
    575  1.10  riastrad 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
    576   1.1  riastrad };
    577  1.26  riastrad 
    578  1.26  riastrad static const struct intel_watermark_params pnv_cursor_wm = {
    579  1.10  riastrad 	.fifo_size = PINEVIEW_CURSOR_FIFO,
    580  1.10  riastrad 	.max_wm = PINEVIEW_CURSOR_MAX_WM,
    581  1.10  riastrad 	.default_wm = PINEVIEW_CURSOR_DFT_WM,
    582  1.10  riastrad 	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
    583  1.10  riastrad 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
    584   1.1  riastrad };
    585  1.26  riastrad 
    586  1.26  riastrad static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
    587  1.10  riastrad 	.fifo_size = PINEVIEW_CURSOR_FIFO,
    588  1.10  riastrad 	.max_wm = PINEVIEW_CURSOR_MAX_WM,
    589  1.10  riastrad 	.default_wm = PINEVIEW_CURSOR_DFT_WM,
    590  1.10  riastrad 	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
    591  1.10  riastrad 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
    592   1.1  riastrad };
    593  1.26  riastrad 
    594   1.1  riastrad static const struct intel_watermark_params i965_cursor_wm_info = {
    595  1.10  riastrad 	.fifo_size = I965_CURSOR_FIFO,
    596  1.10  riastrad 	.max_wm = I965_CURSOR_MAX_WM,
    597  1.10  riastrad 	.default_wm = I965_CURSOR_DFT_WM,
    598  1.10  riastrad 	.guard_size = 2,
    599  1.10  riastrad 	.cacheline_size = I915_FIFO_LINE_SIZE,
    600   1.1  riastrad };
    601  1.26  riastrad 
    602   1.1  riastrad static const struct intel_watermark_params i945_wm_info = {
    603  1.10  riastrad 	.fifo_size = I945_FIFO_SIZE,
    604  1.10  riastrad 	.max_wm = I915_MAX_WM,
    605  1.10  riastrad 	.default_wm = 1,
    606  1.10  riastrad 	.guard_size = 2,
    607  1.10  riastrad 	.cacheline_size = I915_FIFO_LINE_SIZE,
    608   1.1  riastrad };
    609  1.26  riastrad 
    610   1.1  riastrad static const struct intel_watermark_params i915_wm_info = {
    611  1.10  riastrad 	.fifo_size = I915_FIFO_SIZE,
    612  1.10  riastrad 	.max_wm = I915_MAX_WM,
    613  1.10  riastrad 	.default_wm = 1,
    614  1.10  riastrad 	.guard_size = 2,
    615  1.10  riastrad 	.cacheline_size = I915_FIFO_LINE_SIZE,
    616  1.10  riastrad };
    617  1.26  riastrad 
    618  1.10  riastrad static const struct intel_watermark_params i830_a_wm_info = {
    619  1.10  riastrad 	.fifo_size = I855GM_FIFO_SIZE,
    620  1.10  riastrad 	.max_wm = I915_MAX_WM,
    621  1.10  riastrad 	.default_wm = 1,
    622  1.10  riastrad 	.guard_size = 2,
    623  1.10  riastrad 	.cacheline_size = I830_FIFO_LINE_SIZE,
    624   1.1  riastrad };
    625  1.26  riastrad 
    626  1.10  riastrad static const struct intel_watermark_params i830_bc_wm_info = {
    627  1.10  riastrad 	.fifo_size = I855GM_FIFO_SIZE,
    628  1.10  riastrad 	.max_wm = I915_MAX_WM/2,
    629  1.10  riastrad 	.default_wm = 1,
    630  1.10  riastrad 	.guard_size = 2,
    631  1.10  riastrad 	.cacheline_size = I830_FIFO_LINE_SIZE,
    632   1.1  riastrad };
    633  1.26  riastrad 
    634   1.3  riastrad static const struct intel_watermark_params i845_wm_info = {
    635  1.10  riastrad 	.fifo_size = I830_FIFO_SIZE,
    636  1.10  riastrad 	.max_wm = I915_MAX_WM,
    637  1.10  riastrad 	.default_wm = 1,
    638  1.10  riastrad 	.guard_size = 2,
    639  1.10  riastrad 	.cacheline_size = I830_FIFO_LINE_SIZE,
    640   1.1  riastrad };
    641   1.1  riastrad 
    642   1.1  riastrad /**
    643  1.26  riastrad  * intel_wm_method1 - Method 1 / "small buffer" watermark formula
    644  1.26  riastrad  * @pixel_rate: Pipe pixel rate in kHz
    645  1.26  riastrad  * @cpp: Plane bytes per pixel
    646  1.26  riastrad  * @latency: Memory wakeup latency in 0.1us units
    647  1.26  riastrad  *
    648  1.26  riastrad  * Compute the watermark using the method 1 or "small buffer"
    649  1.26  riastrad  * formula. The caller may additonally add extra cachelines
    650  1.26  riastrad  * to account for TLB misses and clock crossings.
    651  1.26  riastrad  *
    652  1.26  riastrad  * This method is concerned with the short term drain rate
    653  1.26  riastrad  * of the FIFO, ie. it does not account for blanking periods
    654  1.26  riastrad  * which would effectively reduce the average drain rate across
    655  1.26  riastrad  * a longer period. The name "small" refers to the fact the
    656  1.26  riastrad  * FIFO is relatively small compared to the amount of data
    657  1.26  riastrad  * fetched.
    658  1.26  riastrad  *
    659  1.26  riastrad  * The FIFO level vs. time graph might look something like:
    660  1.26  riastrad  *
    661  1.26  riastrad  *   |\   |\
    662  1.26  riastrad  *   | \  | \
    663  1.26  riastrad  * __---__---__ (- plane active, _ blanking)
    664  1.26  riastrad  * -> time
    665  1.26  riastrad  *
    666  1.26  riastrad  * or perhaps like this:
    667  1.26  riastrad  *
    668  1.26  riastrad  *   |\|\  |\|\
    669  1.26  riastrad  * __----__----__ (- plane active, _ blanking)
    670  1.26  riastrad  * -> time
    671  1.26  riastrad  *
    672  1.26  riastrad  * Returns:
    673  1.26  riastrad  * The watermark in bytes
    674  1.26  riastrad  */
    675  1.26  riastrad static unsigned int intel_wm_method1(unsigned int pixel_rate,
    676  1.26  riastrad 				     unsigned int cpp,
    677  1.26  riastrad 				     unsigned int latency)
    678  1.26  riastrad {
    679  1.26  riastrad 	u64 ret;
    680  1.26  riastrad 
    681  1.26  riastrad 	ret = mul_u32_u32(pixel_rate, cpp * latency);
    682  1.26  riastrad 	ret = DIV_ROUND_UP_ULL(ret, 10000);
    683  1.26  riastrad 
    684  1.26  riastrad 	return ret;
    685  1.26  riastrad }
    686  1.26  riastrad 
    687  1.26  riastrad /**
    688  1.26  riastrad  * intel_wm_method2 - Method 2 / "large buffer" watermark formula
    689  1.26  riastrad  * @pixel_rate: Pipe pixel rate in kHz
    690  1.26  riastrad  * @htotal: Pipe horizontal total
    691  1.26  riastrad  * @width: Plane width in pixels
    692  1.26  riastrad  * @cpp: Plane bytes per pixel
    693  1.26  riastrad  * @latency: Memory wakeup latency in 0.1us units
    694  1.26  riastrad  *
    695  1.26  riastrad  * Compute the watermark using the method 2 or "large buffer"
    696  1.26  riastrad  * formula. The caller may additonally add extra cachelines
    697  1.26  riastrad  * to account for TLB misses and clock crossings.
    698  1.26  riastrad  *
    699  1.26  riastrad  * This method is concerned with the long term drain rate
    700  1.26  riastrad  * of the FIFO, ie. it does account for blanking periods
    701  1.26  riastrad  * which effectively reduce the average drain rate across
    702  1.26  riastrad  * a longer period. The name "large" refers to the fact the
    703  1.26  riastrad  * FIFO is relatively large compared to the amount of data
    704  1.26  riastrad  * fetched.
    705  1.26  riastrad  *
    706  1.26  riastrad  * The FIFO level vs. time graph might look something like:
    707  1.26  riastrad  *
    708  1.26  riastrad  *    |\___       |\___
    709  1.26  riastrad  *    |    \___   |    \___
    710  1.26  riastrad  *    |        \  |        \
    711  1.26  riastrad  * __ --__--__--__--__--__--__ (- plane active, _ blanking)
    712  1.26  riastrad  * -> time
    713  1.26  riastrad  *
    714  1.26  riastrad  * Returns:
    715  1.26  riastrad  * The watermark in bytes
    716  1.26  riastrad  */
    717  1.26  riastrad static unsigned int intel_wm_method2(unsigned int pixel_rate,
    718  1.26  riastrad 				     unsigned int htotal,
    719  1.26  riastrad 				     unsigned int width,
    720  1.26  riastrad 				     unsigned int cpp,
    721  1.26  riastrad 				     unsigned int latency)
    722  1.26  riastrad {
    723  1.26  riastrad 	unsigned int ret;
    724  1.26  riastrad 
    725  1.26  riastrad 	/*
    726  1.26  riastrad 	 * FIXME remove once all users are computing
    727  1.26  riastrad 	 * watermarks in the correct place.
    728  1.26  riastrad 	 */
    729  1.26  riastrad 	if (WARN_ON_ONCE(htotal == 0))
    730  1.26  riastrad 		htotal = 1;
    731  1.26  riastrad 
    732  1.26  riastrad 	ret = (latency * pixel_rate) / (htotal * 10000);
    733  1.26  riastrad 	ret = (ret + 1) * width * cpp;
    734  1.26  riastrad 
    735  1.26  riastrad 	return ret;
    736  1.26  riastrad }
    737  1.26  riastrad 
    738  1.26  riastrad /**
    739   1.1  riastrad  * intel_calculate_wm - calculate watermark level
    740  1.26  riastrad  * @pixel_rate: pixel clock
    741   1.1  riastrad  * @wm: chip FIFO params
    742  1.26  riastrad  * @fifo_size: size of the FIFO buffer
    743  1.26  riastrad  * @cpp: bytes per pixel
    744   1.1  riastrad  * @latency_ns: memory latency for the platform
    745   1.1  riastrad  *
    746   1.1  riastrad  * Calculate the watermark level (the level at which the display plane will
    747   1.1  riastrad  * start fetching from memory again).  Each chip has a different display
    748   1.1  riastrad  * FIFO size and allocation, so the caller needs to figure that out and pass
    749   1.1  riastrad  * in the correct intel_watermark_params structure.
    750   1.1  riastrad  *
    751   1.1  riastrad  * As the pixel clock runs, the FIFO will be drained at a rate that depends
    752   1.1  riastrad  * on the pixel size.  When it reaches the watermark level, it'll start
    753   1.1  riastrad  * fetching FIFO line sized based chunks from memory until the FIFO fills
    754   1.1  riastrad  * past the watermark point.  If the FIFO drains completely, a FIFO underrun
    755   1.1  riastrad  * will occur, and a display engine hang could result.
    756   1.1  riastrad  */
    757  1.26  riastrad static unsigned int intel_calculate_wm(int pixel_rate,
    758  1.26  riastrad 				       const struct intel_watermark_params *wm,
    759  1.26  riastrad 				       int fifo_size, int cpp,
    760  1.26  riastrad 				       unsigned int latency_ns)
    761   1.1  riastrad {
    762  1.26  riastrad 	int entries, wm_size;
    763   1.1  riastrad 
    764   1.1  riastrad 	/*
    765   1.1  riastrad 	 * Note: we need to make sure we don't overflow for various clock &
    766   1.1  riastrad 	 * latency values.
    767   1.1  riastrad 	 * clocks go from a few thousand to several hundred thousand.
    768   1.1  riastrad 	 * latency is usually a few thousand
    769   1.1  riastrad 	 */
    770  1.26  riastrad 	entries = intel_wm_method1(pixel_rate, cpp,
    771  1.26  riastrad 				   latency_ns / 100);
    772  1.26  riastrad 	entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
    773  1.26  riastrad 		wm->guard_size;
    774  1.26  riastrad 	DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
    775   1.1  riastrad 
    776  1.26  riastrad 	wm_size = fifo_size - entries;
    777  1.26  riastrad 	DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
    778   1.1  riastrad 
    779   1.1  riastrad 	/* Don't promote wm_size to unsigned... */
    780  1.26  riastrad 	if (wm_size > wm->max_wm)
    781   1.1  riastrad 		wm_size = wm->max_wm;
    782   1.1  riastrad 	if (wm_size <= 0)
    783   1.1  riastrad 		wm_size = wm->default_wm;
    784  1.10  riastrad 
    785  1.10  riastrad 	/*
    786  1.10  riastrad 	 * Bspec seems to indicate that the value shouldn't be lower than
    787  1.10  riastrad 	 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
    788  1.10  riastrad 	 * Lets go for 8 which is the burst size since certain platforms
    789  1.10  riastrad 	 * already use a hardcoded 8 (which is what the spec says should be
    790  1.10  riastrad 	 * done).
    791  1.10  riastrad 	 */
    792  1.10  riastrad 	if (wm_size <= 8)
    793  1.10  riastrad 		wm_size = 8;
    794  1.10  riastrad 
    795   1.1  riastrad 	return wm_size;
    796   1.1  riastrad }
    797   1.1  riastrad 
    798  1.26  riastrad static bool is_disabling(int old, int new, int threshold)
    799  1.26  riastrad {
    800  1.26  riastrad 	return old >= threshold && new < threshold;
    801  1.26  riastrad }
    802  1.26  riastrad 
    803  1.26  riastrad static bool is_enabling(int old, int new, int threshold)
    804  1.26  riastrad {
    805  1.26  riastrad 	return old < threshold && new >= threshold;
    806  1.26  riastrad }
    807  1.26  riastrad 
    808  1.26  riastrad static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
    809  1.26  riastrad {
    810  1.26  riastrad 	return dev_priv->wm.max_level + 1;
    811  1.26  riastrad }
    812  1.26  riastrad 
    813  1.26  riastrad static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
    814  1.26  riastrad 				   const struct intel_plane_state *plane_state)
    815  1.26  riastrad {
    816  1.26  riastrad 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
    817  1.26  riastrad 
    818  1.26  riastrad 	/* FIXME check the 'enable' instead */
    819  1.26  riastrad 	if (!crtc_state->hw.active)
    820  1.26  riastrad 		return false;
    821  1.26  riastrad 
    822  1.26  riastrad 	/*
    823  1.26  riastrad 	 * Treat cursor with fb as always visible since cursor updates
    824  1.26  riastrad 	 * can happen faster than the vrefresh rate, and the current
    825  1.26  riastrad 	 * watermark code doesn't handle that correctly. Cursor updates
    826  1.26  riastrad 	 * which set/clear the fb or change the cursor size are going
    827  1.26  riastrad 	 * to get throttled by intel_legacy_cursor_update() to work
    828  1.26  riastrad 	 * around this problem with the watermark code.
    829  1.26  riastrad 	 */
    830  1.26  riastrad 	if (plane->id == PLANE_CURSOR)
    831  1.26  riastrad 		return plane_state->hw.fb != NULL;
    832  1.26  riastrad 	else
    833  1.26  riastrad 		return plane_state->uapi.visible;
    834  1.26  riastrad }
    835  1.26  riastrad 
    836  1.26  riastrad static bool intel_crtc_active(struct intel_crtc *crtc)
    837  1.26  riastrad {
    838  1.26  riastrad 	/* Be paranoid as we can arrive here with only partial
    839  1.26  riastrad 	 * state retrieved from the hardware during setup.
    840  1.26  riastrad 	 *
    841  1.26  riastrad 	 * We can ditch the adjusted_mode.crtc_clock check as soon
    842  1.26  riastrad 	 * as Haswell has gained clock readout/fastboot support.
    843  1.26  riastrad 	 *
    844  1.26  riastrad 	 * We can ditch the crtc->primary->state->fb check as soon as we can
    845  1.26  riastrad 	 * properly reconstruct framebuffers.
    846  1.26  riastrad 	 *
    847  1.26  riastrad 	 * FIXME: The intel_crtc->active here should be switched to
    848  1.26  riastrad 	 * crtc->state->active once we have proper CRTC states wired up
    849  1.26  riastrad 	 * for atomic.
    850  1.26  riastrad 	 */
    851  1.26  riastrad 	return crtc->active && crtc->base.primary->state->fb &&
    852  1.26  riastrad 		crtc->config->hw.adjusted_mode.crtc_clock;
    853  1.26  riastrad }
    854  1.26  riastrad 
    855  1.26  riastrad static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
    856   1.1  riastrad {
    857  1.26  riastrad 	struct intel_crtc *crtc, *enabled = NULL;
    858   1.1  riastrad 
    859  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
    860   1.1  riastrad 		if (intel_crtc_active(crtc)) {
    861   1.1  riastrad 			if (enabled)
    862   1.1  riastrad 				return NULL;
    863   1.1  riastrad 			enabled = crtc;
    864   1.1  riastrad 		}
    865   1.1  riastrad 	}
    866   1.1  riastrad 
    867   1.1  riastrad 	return enabled;
    868   1.1  riastrad }
    869   1.1  riastrad 
    870  1.26  riastrad static void pnv_update_wm(struct intel_crtc *unused_crtc)
    871   1.1  riastrad {
    872  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
    873  1.26  riastrad 	struct intel_crtc *crtc;
    874   1.1  riastrad 	const struct cxsr_latency *latency;
    875   1.1  riastrad 	u32 reg;
    876  1.26  riastrad 	unsigned int wm;
    877   1.1  riastrad 
    878  1.26  riastrad 	latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
    879  1.26  riastrad 					 dev_priv->is_ddr3,
    880  1.26  riastrad 					 dev_priv->fsb_freq,
    881  1.26  riastrad 					 dev_priv->mem_freq);
    882   1.1  riastrad 	if (!latency) {
    883  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
    884  1.26  riastrad 			    "Unknown FSB/MEM found, disable CxSR\n");
    885  1.10  riastrad 		intel_set_memory_cxsr(dev_priv, false);
    886   1.1  riastrad 		return;
    887   1.1  riastrad 	}
    888   1.1  riastrad 
    889  1.26  riastrad 	crtc = single_enabled_crtc(dev_priv);
    890   1.1  riastrad 	if (crtc) {
    891  1.26  riastrad 		const struct drm_display_mode *adjusted_mode =
    892  1.26  riastrad 			&crtc->config->hw.adjusted_mode;
    893  1.26  riastrad 		const struct drm_framebuffer *fb =
    894  1.26  riastrad 			crtc->base.primary->state->fb;
    895  1.26  riastrad 		int cpp = fb->format->cpp[0];
    896  1.10  riastrad 		int clock = adjusted_mode->crtc_clock;
    897   1.1  riastrad 
    898   1.1  riastrad 		/* Display SR */
    899  1.26  riastrad 		wm = intel_calculate_wm(clock, &pnv_display_wm,
    900  1.26  riastrad 					pnv_display_wm.fifo_size,
    901  1.26  riastrad 					cpp, latency->display_sr);
    902   1.1  riastrad 		reg = I915_READ(DSPFW1);
    903   1.1  riastrad 		reg &= ~DSPFW_SR_MASK;
    904  1.10  riastrad 		reg |= FW_WM(wm, SR);
    905   1.1  riastrad 		I915_WRITE(DSPFW1, reg);
    906  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
    907   1.1  riastrad 
    908   1.1  riastrad 		/* cursor SR */
    909  1.26  riastrad 		wm = intel_calculate_wm(clock, &pnv_cursor_wm,
    910  1.26  riastrad 					pnv_display_wm.fifo_size,
    911  1.26  riastrad 					4, latency->cursor_sr);
    912   1.1  riastrad 		reg = I915_READ(DSPFW3);
    913   1.1  riastrad 		reg &= ~DSPFW_CURSOR_SR_MASK;
    914  1.10  riastrad 		reg |= FW_WM(wm, CURSOR_SR);
    915   1.1  riastrad 		I915_WRITE(DSPFW3, reg);
    916   1.1  riastrad 
    917   1.1  riastrad 		/* Display HPLL off SR */
    918  1.26  riastrad 		wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
    919  1.26  riastrad 					pnv_display_hplloff_wm.fifo_size,
    920  1.26  riastrad 					cpp, latency->display_hpll_disable);
    921   1.1  riastrad 		reg = I915_READ(DSPFW3);
    922   1.1  riastrad 		reg &= ~DSPFW_HPLL_SR_MASK;
    923  1.10  riastrad 		reg |= FW_WM(wm, HPLL_SR);
    924   1.1  riastrad 		I915_WRITE(DSPFW3, reg);
    925   1.1  riastrad 
    926   1.1  riastrad 		/* cursor HPLL off SR */
    927  1.26  riastrad 		wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
    928  1.26  riastrad 					pnv_display_hplloff_wm.fifo_size,
    929  1.26  riastrad 					4, latency->cursor_hpll_disable);
    930   1.1  riastrad 		reg = I915_READ(DSPFW3);
    931   1.1  riastrad 		reg &= ~DSPFW_HPLL_CURSOR_MASK;
    932  1.10  riastrad 		reg |= FW_WM(wm, HPLL_CURSOR);
    933   1.1  riastrad 		I915_WRITE(DSPFW3, reg);
    934  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
    935   1.1  riastrad 
    936  1.10  riastrad 		intel_set_memory_cxsr(dev_priv, true);
    937   1.1  riastrad 	} else {
    938  1.10  riastrad 		intel_set_memory_cxsr(dev_priv, false);
    939   1.1  riastrad 	}
    940   1.1  riastrad }
    941   1.1  riastrad 
    942   1.1  riastrad /*
    943  1.26  riastrad  * Documentation says:
    944  1.26  riastrad  * "If the line size is small, the TLB fetches can get in the way of the
    945  1.26  riastrad  *  data fetches, causing some lag in the pixel data return which is not
    946  1.26  riastrad  *  accounted for in the above formulas. The following adjustment only
    947  1.26  riastrad  *  needs to be applied if eight whole lines fit in the buffer at once.
    948  1.26  riastrad  *  The WM is adjusted upwards by the difference between the FIFO size
    949  1.26  riastrad  *  and the size of 8 whole lines. This adjustment is always performed
    950  1.26  riastrad  *  in the actual pixel depth regardless of whether FBC is enabled or not."
    951   1.1  riastrad  */
    952  1.26  riastrad static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
    953  1.26  riastrad {
    954  1.26  riastrad 	int tlb_miss = fifo_size * 64 - width * cpp * 8;
    955   1.1  riastrad 
    956  1.26  riastrad 	return max(0, tlb_miss);
    957   1.1  riastrad }
    958   1.1  riastrad 
    959  1.26  riastrad static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
    960  1.26  riastrad 				const struct g4x_wm_values *wm)
    961   1.1  riastrad {
    962  1.26  riastrad 	enum pipe pipe;
    963   1.1  riastrad 
    964  1.26  riastrad 	for_each_pipe(dev_priv, pipe)
    965  1.26  riastrad 		trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
    966   1.1  riastrad 
    967  1.26  riastrad 	I915_WRITE(DSPFW1,
    968  1.26  riastrad 		   FW_WM(wm->sr.plane, SR) |
    969  1.26  riastrad 		   FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
    970  1.26  riastrad 		   FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
    971  1.26  riastrad 		   FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
    972  1.26  riastrad 	I915_WRITE(DSPFW2,
    973  1.26  riastrad 		   (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
    974  1.26  riastrad 		   FW_WM(wm->sr.fbc, FBC_SR) |
    975  1.26  riastrad 		   FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
    976  1.26  riastrad 		   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
    977  1.26  riastrad 		   FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
    978  1.26  riastrad 		   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
    979  1.26  riastrad 	I915_WRITE(DSPFW3,
    980  1.26  riastrad 		   (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
    981  1.26  riastrad 		   FW_WM(wm->sr.cursor, CURSOR_SR) |
    982  1.26  riastrad 		   FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
    983  1.26  riastrad 		   FW_WM(wm->hpll.plane, HPLL_SR));
    984   1.1  riastrad 
    985  1.26  riastrad 	POSTING_READ(DSPFW1);
    986   1.1  riastrad }
    987   1.1  riastrad 
    988  1.10  riastrad #define FW_WM_VLV(value, plane) \
    989  1.10  riastrad 	(((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
    990  1.10  riastrad 
    991  1.26  riastrad static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
    992  1.10  riastrad 				const struct vlv_wm_values *wm)
    993  1.10  riastrad {
    994  1.26  riastrad 	enum pipe pipe;
    995  1.26  riastrad 
    996  1.26  riastrad 	for_each_pipe(dev_priv, pipe) {
    997  1.26  riastrad 		trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
    998  1.26  riastrad 
    999  1.26  riastrad 		I915_WRITE(VLV_DDL(pipe),
   1000  1.26  riastrad 			   (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
   1001  1.26  riastrad 			   (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
   1002  1.26  riastrad 			   (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
   1003  1.26  riastrad 			   (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
   1004  1.26  riastrad 	}
   1005  1.10  riastrad 
   1006  1.26  riastrad 	/*
   1007  1.26  riastrad 	 * Zero the (unused) WM1 watermarks, and also clear all the
   1008  1.26  riastrad 	 * high order bits so that there are no out of bounds values
   1009  1.26  riastrad 	 * present in the registers during the reprogramming.
   1010  1.26  riastrad 	 */
   1011  1.26  riastrad 	I915_WRITE(DSPHOWM, 0);
   1012  1.26  riastrad 	I915_WRITE(DSPHOWM1, 0);
   1013  1.26  riastrad 	I915_WRITE(DSPFW4, 0);
   1014  1.26  riastrad 	I915_WRITE(DSPFW5, 0);
   1015  1.26  riastrad 	I915_WRITE(DSPFW6, 0);
   1016  1.10  riastrad 
   1017  1.10  riastrad 	I915_WRITE(DSPFW1,
   1018  1.10  riastrad 		   FW_WM(wm->sr.plane, SR) |
   1019  1.26  riastrad 		   FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
   1020  1.26  riastrad 		   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
   1021  1.26  riastrad 		   FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
   1022  1.10  riastrad 	I915_WRITE(DSPFW2,
   1023  1.26  riastrad 		   FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
   1024  1.26  riastrad 		   FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
   1025  1.26  riastrad 		   FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
   1026  1.10  riastrad 	I915_WRITE(DSPFW3,
   1027  1.10  riastrad 		   FW_WM(wm->sr.cursor, CURSOR_SR));
   1028  1.10  riastrad 
   1029  1.10  riastrad 	if (IS_CHERRYVIEW(dev_priv)) {
   1030  1.10  riastrad 		I915_WRITE(DSPFW7_CHV,
   1031  1.26  riastrad 			   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
   1032  1.26  riastrad 			   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
   1033  1.10  riastrad 		I915_WRITE(DSPFW8_CHV,
   1034  1.26  riastrad 			   FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
   1035  1.26  riastrad 			   FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
   1036  1.10  riastrad 		I915_WRITE(DSPFW9_CHV,
   1037  1.26  riastrad 			   FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
   1038  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
   1039  1.10  riastrad 		I915_WRITE(DSPHOWM,
   1040  1.10  riastrad 			   FW_WM(wm->sr.plane >> 9, SR_HI) |
   1041  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
   1042  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
   1043  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
   1044  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
   1045  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
   1046  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
   1047  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
   1048  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
   1049  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
   1050  1.10  riastrad 	} else {
   1051  1.10  riastrad 		I915_WRITE(DSPFW7,
   1052  1.26  riastrad 			   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
   1053  1.26  riastrad 			   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
   1054  1.10  riastrad 		I915_WRITE(DSPHOWM,
   1055  1.10  riastrad 			   FW_WM(wm->sr.plane >> 9, SR_HI) |
   1056  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
   1057  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
   1058  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
   1059  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
   1060  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
   1061  1.26  riastrad 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
   1062  1.10  riastrad 	}
   1063  1.10  riastrad 
   1064  1.10  riastrad 	POSTING_READ(DSPFW1);
   1065  1.10  riastrad }
   1066  1.10  riastrad 
   1067  1.10  riastrad #undef FW_WM_VLV
   1068  1.10  riastrad 
   1069  1.26  riastrad static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
   1070  1.10  riastrad {
   1071  1.26  riastrad 	/* all latencies in usec */
   1072  1.26  riastrad 	dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
   1073  1.26  riastrad 	dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
   1074  1.26  riastrad 	dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
   1075  1.26  riastrad 
   1076  1.26  riastrad 	dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
   1077  1.26  riastrad }
   1078  1.26  riastrad 
   1079  1.26  riastrad static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
   1080  1.26  riastrad {
   1081  1.26  riastrad 	/*
   1082  1.26  riastrad 	 * DSPCNTR[13] supposedly controls whether the
   1083  1.26  riastrad 	 * primary plane can use the FIFO space otherwise
   1084  1.26  riastrad 	 * reserved for the sprite plane. It's not 100% clear
   1085  1.26  riastrad 	 * what the actual FIFO size is, but it looks like we
   1086  1.26  riastrad 	 * can happily set both primary and sprite watermarks
   1087  1.26  riastrad 	 * up to 127 cachelines. So that would seem to mean
   1088  1.26  riastrad 	 * that either DSPCNTR[13] doesn't do anything, or that
   1089  1.26  riastrad 	 * the total FIFO is >= 256 cachelines in size. Either
   1090  1.26  riastrad 	 * way, we don't seem to have to worry about this
   1091  1.26  riastrad 	 * repartitioning as the maximum watermark value the
   1092  1.26  riastrad 	 * register can hold for each plane is lower than the
   1093  1.26  riastrad 	 * minimum FIFO size.
   1094  1.26  riastrad 	 */
   1095  1.26  riastrad 	switch (plane_id) {
   1096  1.26  riastrad 	case PLANE_CURSOR:
   1097  1.26  riastrad 		return 63;
   1098  1.26  riastrad 	case PLANE_PRIMARY:
   1099  1.26  riastrad 		return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
   1100  1.26  riastrad 	case PLANE_SPRITE0:
   1101  1.26  riastrad 		return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
   1102  1.26  riastrad 	default:
   1103  1.26  riastrad 		MISSING_CASE(plane_id);
   1104  1.26  riastrad 		return 0;
   1105  1.26  riastrad 	}
   1106  1.10  riastrad }
   1107  1.10  riastrad 
   1108  1.26  riastrad static int g4x_fbc_fifo_size(int level)
   1109  1.10  riastrad {
   1110  1.26  riastrad 	switch (level) {
   1111  1.26  riastrad 	case G4X_WM_LEVEL_SR:
   1112  1.26  riastrad 		return 7;
   1113  1.26  riastrad 	case G4X_WM_LEVEL_HPLL:
   1114  1.26  riastrad 		return 15;
   1115  1.26  riastrad 	default:
   1116  1.26  riastrad 		MISSING_CASE(level);
   1117  1.26  riastrad 		return 0;
   1118  1.10  riastrad 	}
   1119  1.10  riastrad }
   1120  1.10  riastrad 
   1121  1.26  riastrad static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
   1122  1.26  riastrad 			  const struct intel_plane_state *plane_state,
   1123  1.26  riastrad 			  int level)
   1124  1.10  riastrad {
   1125  1.26  riastrad 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
   1126  1.10  riastrad 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
   1127  1.26  riastrad 	const struct drm_display_mode *adjusted_mode =
   1128  1.26  riastrad 		&crtc_state->hw.adjusted_mode;
   1129  1.26  riastrad 	unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
   1130  1.26  riastrad 	unsigned int clock, htotal, cpp, width, wm;
   1131  1.10  riastrad 
   1132  1.26  riastrad 	if (latency == 0)
   1133  1.10  riastrad 		return USHRT_MAX;
   1134  1.10  riastrad 
   1135  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state))
   1136  1.10  riastrad 		return 0;
   1137  1.10  riastrad 
   1138  1.26  riastrad 	cpp = plane_state->hw.fb->format->cpp[0];
   1139  1.26  riastrad 
   1140  1.26  riastrad 	/*
   1141  1.26  riastrad 	 * Not 100% sure which way ELK should go here as the
   1142  1.26  riastrad 	 * spec only says CL/CTG should assume 32bpp and BW
   1143  1.26  riastrad 	 * doesn't need to. But as these things followed the
   1144  1.26  riastrad 	 * mobile vs. desktop lines on gen3 as well, let's
   1145  1.26  riastrad 	 * assume ELK doesn't need this.
   1146  1.26  riastrad 	 *
   1147  1.26  riastrad 	 * The spec also fails to list such a restriction for
   1148  1.26  riastrad 	 * the HPLL watermark, which seems a little strange.
   1149  1.26  riastrad 	 * Let's use 32bpp for the HPLL watermark as well.
   1150  1.26  riastrad 	 */
   1151  1.26  riastrad 	if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
   1152  1.26  riastrad 	    level != G4X_WM_LEVEL_NORMAL)
   1153  1.26  riastrad 		cpp = max(cpp, 4u);
   1154  1.26  riastrad 
   1155  1.26  riastrad 	clock = adjusted_mode->crtc_clock;
   1156  1.26  riastrad 	htotal = adjusted_mode->crtc_htotal;
   1157  1.26  riastrad 
   1158  1.26  riastrad 	width = drm_rect_width(&plane_state->uapi.dst);
   1159  1.10  riastrad 
   1160  1.26  riastrad 	if (plane->id == PLANE_CURSOR) {
   1161  1.26  riastrad 		wm = intel_wm_method2(clock, htotal, width, cpp, latency);
   1162  1.26  riastrad 	} else if (plane->id == PLANE_PRIMARY &&
   1163  1.26  riastrad 		   level == G4X_WM_LEVEL_NORMAL) {
   1164  1.26  riastrad 		wm = intel_wm_method1(clock, cpp, latency);
   1165  1.10  riastrad 	} else {
   1166  1.26  riastrad 		unsigned int small, large;
   1167  1.26  riastrad 
   1168  1.26  riastrad 		small = intel_wm_method1(clock, cpp, latency);
   1169  1.26  riastrad 		large = intel_wm_method2(clock, htotal, width, cpp, latency);
   1170  1.26  riastrad 
   1171  1.26  riastrad 		wm = min(small, large);
   1172  1.26  riastrad 	}
   1173  1.26  riastrad 
   1174  1.26  riastrad 	wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
   1175  1.26  riastrad 			      width, cpp);
   1176  1.26  riastrad 
   1177  1.26  riastrad 	wm = DIV_ROUND_UP(wm, 64) + 2;
   1178  1.26  riastrad 
   1179  1.26  riastrad 	return min_t(unsigned int, wm, USHRT_MAX);
   1180  1.26  riastrad }
   1181  1.26  riastrad 
   1182  1.26  riastrad static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
   1183  1.26  riastrad 				 int level, enum plane_id plane_id, u16 value)
   1184  1.26  riastrad {
   1185  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   1186  1.26  riastrad 	bool dirty = false;
   1187  1.26  riastrad 
   1188  1.26  riastrad 	for (; level < intel_wm_num_levels(dev_priv); level++) {
   1189  1.26  riastrad 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
   1190  1.26  riastrad 
   1191  1.26  riastrad 		dirty |= raw->plane[plane_id] != value;
   1192  1.26  riastrad 		raw->plane[plane_id] = value;
   1193  1.10  riastrad 	}
   1194  1.10  riastrad 
   1195  1.26  riastrad 	return dirty;
   1196  1.10  riastrad }
   1197  1.10  riastrad 
   1198  1.26  riastrad static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
   1199  1.26  riastrad 			       int level, u16 value)
   1200  1.10  riastrad {
   1201  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   1202  1.26  riastrad 	bool dirty = false;
   1203  1.10  riastrad 
   1204  1.26  riastrad 	/* NORMAL level doesn't have an FBC watermark */
   1205  1.26  riastrad 	level = max(level, G4X_WM_LEVEL_SR);
   1206  1.10  riastrad 
   1207  1.26  riastrad 	for (; level < intel_wm_num_levels(dev_priv); level++) {
   1208  1.26  riastrad 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
   1209  1.10  riastrad 
   1210  1.26  riastrad 		dirty |= raw->fbc != value;
   1211  1.26  riastrad 		raw->fbc = value;
   1212  1.10  riastrad 	}
   1213  1.10  riastrad 
   1214  1.26  riastrad 	return dirty;
   1215  1.26  riastrad }
   1216  1.10  riastrad 
   1217  1.26  riastrad static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
   1218  1.26  riastrad 			      const struct intel_plane_state *plane_state,
   1219  1.26  riastrad 			      u32 pri_val);
   1220  1.26  riastrad 
   1221  1.26  riastrad static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
   1222  1.26  riastrad 				     const struct intel_plane_state *plane_state)
   1223  1.26  riastrad {
   1224  1.26  riastrad 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
   1225  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   1226  1.26  riastrad 	int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
   1227  1.26  riastrad 	enum plane_id plane_id = plane->id;
   1228  1.26  riastrad 	bool dirty = false;
   1229  1.26  riastrad 	int level;
   1230  1.10  riastrad 
   1231  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state)) {
   1232  1.26  riastrad 		dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
   1233  1.26  riastrad 		if (plane_id == PLANE_PRIMARY)
   1234  1.26  riastrad 			dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
   1235  1.26  riastrad 		goto out;
   1236  1.10  riastrad 	}
   1237  1.10  riastrad 
   1238  1.26  riastrad 	for (level = 0; level < num_levels; level++) {
   1239  1.26  riastrad 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
   1240  1.26  riastrad 		int wm, max_wm;
   1241  1.10  riastrad 
   1242  1.26  riastrad 		wm = g4x_compute_wm(crtc_state, plane_state, level);
   1243  1.26  riastrad 		max_wm = g4x_plane_fifo_size(plane_id, level);
   1244  1.10  riastrad 
   1245  1.26  riastrad 		if (wm > max_wm)
   1246  1.10  riastrad 			break;
   1247  1.10  riastrad 
   1248  1.26  riastrad 		dirty |= raw->plane[plane_id] != wm;
   1249  1.26  riastrad 		raw->plane[plane_id] = wm;
   1250  1.26  riastrad 
   1251  1.26  riastrad 		if (plane_id != PLANE_PRIMARY ||
   1252  1.26  riastrad 		    level == G4X_WM_LEVEL_NORMAL)
   1253  1.10  riastrad 			continue;
   1254  1.10  riastrad 
   1255  1.26  riastrad 		wm = ilk_compute_fbc_wm(crtc_state, plane_state,
   1256  1.26  riastrad 					raw->plane[plane_id]);
   1257  1.26  riastrad 		max_wm = g4x_fbc_fifo_size(level);
   1258  1.26  riastrad 
   1259  1.26  riastrad 		/*
   1260  1.26  riastrad 		 * FBC wm is not mandatory as we
   1261  1.26  riastrad 		 * can always just disable its use.
   1262  1.26  riastrad 		 */
   1263  1.26  riastrad 		if (wm > max_wm)
   1264  1.26  riastrad 			wm = USHRT_MAX;
   1265  1.26  riastrad 
   1266  1.26  riastrad 		dirty |= raw->fbc != wm;
   1267  1.26  riastrad 		raw->fbc = wm;
   1268  1.26  riastrad 	}
   1269  1.10  riastrad 
   1270  1.26  riastrad 	/* mark watermarks as invalid */
   1271  1.26  riastrad 	dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
   1272  1.26  riastrad 
   1273  1.26  riastrad 	if (plane_id == PLANE_PRIMARY)
   1274  1.26  riastrad 		dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
   1275  1.26  riastrad 
   1276  1.26  riastrad  out:
   1277  1.26  riastrad 	if (dirty) {
   1278  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   1279  1.26  riastrad 			    "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
   1280  1.26  riastrad 			    plane->base.name,
   1281  1.26  riastrad 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
   1282  1.26  riastrad 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
   1283  1.26  riastrad 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
   1284  1.26  riastrad 
   1285  1.26  riastrad 		if (plane_id == PLANE_PRIMARY)
   1286  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   1287  1.26  riastrad 				    "FBC watermarks: SR=%d, HPLL=%d\n",
   1288  1.26  riastrad 				    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
   1289  1.26  riastrad 				    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
   1290  1.10  riastrad 	}
   1291  1.10  riastrad 
   1292  1.26  riastrad 	return dirty;
   1293  1.10  riastrad }
   1294  1.10  riastrad 
   1295  1.26  riastrad static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
   1296  1.26  riastrad 				      enum plane_id plane_id, int level)
   1297  1.10  riastrad {
   1298  1.26  riastrad 	const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
   1299  1.10  riastrad 
   1300  1.26  riastrad 	return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
   1301  1.10  riastrad }
   1302  1.10  riastrad 
   1303  1.26  riastrad static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
   1304  1.26  riastrad 				     int level)
   1305   1.1  riastrad {
   1306  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   1307  1.10  riastrad 
   1308  1.26  riastrad 	if (level > dev_priv->wm.max_level)
   1309  1.26  riastrad 		return false;
   1310  1.10  riastrad 
   1311  1.26  riastrad 	return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
   1312  1.26  riastrad 		g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
   1313  1.26  riastrad 		g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
   1314  1.26  riastrad }
   1315  1.10  riastrad 
   1316  1.26  riastrad /* mark all levels starting from 'level' as invalid */
   1317  1.26  riastrad static void g4x_invalidate_wms(struct intel_crtc *crtc,
   1318  1.26  riastrad 			       struct g4x_wm_state *wm_state, int level)
   1319  1.26  riastrad {
   1320  1.26  riastrad 	if (level <= G4X_WM_LEVEL_NORMAL) {
   1321  1.26  riastrad 		enum plane_id plane_id;
   1322  1.10  riastrad 
   1323  1.26  riastrad 		for_each_plane_id_on_crtc(crtc, plane_id)
   1324  1.26  riastrad 			wm_state->wm.plane[plane_id] = USHRT_MAX;
   1325  1.26  riastrad 	}
   1326  1.10  riastrad 
   1327  1.26  riastrad 	if (level <= G4X_WM_LEVEL_SR) {
   1328  1.10  riastrad 		wm_state->cxsr = false;
   1329  1.26  riastrad 		wm_state->sr.cursor = USHRT_MAX;
   1330  1.26  riastrad 		wm_state->sr.plane = USHRT_MAX;
   1331  1.26  riastrad 		wm_state->sr.fbc = USHRT_MAX;
   1332  1.26  riastrad 	}
   1333  1.26  riastrad 
   1334  1.26  riastrad 	if (level <= G4X_WM_LEVEL_HPLL) {
   1335  1.26  riastrad 		wm_state->hpll_en = false;
   1336  1.26  riastrad 		wm_state->hpll.cursor = USHRT_MAX;
   1337  1.26  riastrad 		wm_state->hpll.plane = USHRT_MAX;
   1338  1.26  riastrad 		wm_state->hpll.fbc = USHRT_MAX;
   1339  1.10  riastrad 	}
   1340  1.26  riastrad }
   1341  1.10  riastrad 
   1342  1.26  riastrad static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
   1343  1.26  riastrad {
   1344  1.26  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
   1345  1.26  riastrad 	struct intel_atomic_state *state =
   1346  1.26  riastrad 		to_intel_atomic_state(crtc_state->uapi.state);
   1347  1.26  riastrad 	struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
   1348  1.26  riastrad 	int num_active_planes = hweight8(crtc_state->active_planes &
   1349  1.26  riastrad 					 ~BIT(PLANE_CURSOR));
   1350  1.26  riastrad 	const struct g4x_pipe_wm *raw;
   1351  1.26  riastrad 	const struct intel_plane_state *old_plane_state;
   1352  1.26  riastrad 	const struct intel_plane_state *new_plane_state;
   1353  1.26  riastrad 	struct intel_plane *plane;
   1354  1.26  riastrad 	enum plane_id plane_id;
   1355  1.26  riastrad 	int i, level;
   1356  1.26  riastrad 	unsigned int dirty = 0;
   1357  1.10  riastrad 
   1358  1.26  riastrad 	for_each_oldnew_intel_plane_in_state(state, plane,
   1359  1.26  riastrad 					     old_plane_state,
   1360  1.26  riastrad 					     new_plane_state, i) {
   1361  1.26  riastrad 		if (new_plane_state->hw.crtc != &crtc->base &&
   1362  1.26  riastrad 		    old_plane_state->hw.crtc != &crtc->base)
   1363  1.10  riastrad 			continue;
   1364  1.10  riastrad 
   1365  1.26  riastrad 		if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
   1366  1.26  riastrad 			dirty |= BIT(plane->id);
   1367  1.26  riastrad 	}
   1368  1.10  riastrad 
   1369  1.26  riastrad 	if (!dirty)
   1370  1.26  riastrad 		return 0;
   1371  1.10  riastrad 
   1372  1.26  riastrad 	level = G4X_WM_LEVEL_NORMAL;
   1373  1.26  riastrad 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
   1374  1.26  riastrad 		goto out;
   1375  1.10  riastrad 
   1376  1.26  riastrad 	raw = &crtc_state->wm.g4x.raw[level];
   1377  1.26  riastrad 	for_each_plane_id_on_crtc(crtc, plane_id)
   1378  1.26  riastrad 		wm_state->wm.plane[plane_id] = raw->plane[plane_id];
   1379   1.1  riastrad 
   1380  1.26  riastrad 	level = G4X_WM_LEVEL_SR;
   1381   1.1  riastrad 
   1382  1.26  riastrad 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
   1383  1.26  riastrad 		goto out;
   1384   1.1  riastrad 
   1385  1.26  riastrad 	raw = &crtc_state->wm.g4x.raw[level];
   1386  1.26  riastrad 	wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
   1387  1.26  riastrad 	wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
   1388  1.26  riastrad 	wm_state->sr.fbc = raw->fbc;
   1389   1.1  riastrad 
   1390  1.26  riastrad 	wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
   1391   1.1  riastrad 
   1392  1.26  riastrad 	level = G4X_WM_LEVEL_HPLL;
   1393   1.1  riastrad 
   1394  1.26  riastrad 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
   1395  1.26  riastrad 		goto out;
   1396  1.10  riastrad 
   1397  1.26  riastrad 	raw = &crtc_state->wm.g4x.raw[level];
   1398  1.26  riastrad 	wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
   1399  1.26  riastrad 	wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
   1400  1.26  riastrad 	wm_state->hpll.fbc = raw->fbc;
   1401  1.10  riastrad 
   1402  1.26  riastrad 	wm_state->hpll_en = wm_state->cxsr;
   1403  1.10  riastrad 
   1404  1.26  riastrad 	level++;
   1405  1.10  riastrad 
   1406  1.26  riastrad  out:
   1407  1.26  riastrad 	if (level == G4X_WM_LEVEL_NORMAL)
   1408  1.26  riastrad 		return -EINVAL;
   1409  1.10  riastrad 
   1410  1.26  riastrad 	/* invalidate the higher levels */
   1411  1.26  riastrad 	g4x_invalidate_wms(crtc, wm_state, level);
   1412  1.10  riastrad 
   1413  1.26  riastrad 	/*
   1414  1.26  riastrad 	 * Determine if the FBC watermark(s) can be used. IF
   1415  1.26  riastrad 	 * this isn't the case we prefer to disable the FBC
   1416  1.26  riastrad 	 ( watermark(s) rather than disable the SR/HPLL
   1417  1.26  riastrad 	 * level(s) entirely.
   1418  1.26  riastrad 	 */
   1419  1.26  riastrad 	wm_state->fbc_en = level > G4X_WM_LEVEL_NORMAL;
   1420  1.10  riastrad 
   1421  1.26  riastrad 	if (level >= G4X_WM_LEVEL_SR &&
   1422  1.26  riastrad 	    wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
   1423  1.26  riastrad 		wm_state->fbc_en = false;
   1424  1.26  riastrad 	else if (level >= G4X_WM_LEVEL_HPLL &&
   1425  1.26  riastrad 		 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
   1426  1.26  riastrad 		wm_state->fbc_en = false;
   1427  1.10  riastrad 
   1428  1.26  riastrad 	return 0;
   1429  1.26  riastrad }
   1430  1.10  riastrad 
   1431  1.26  riastrad static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
   1432  1.26  riastrad {
   1433  1.26  riastrad 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
   1434  1.26  riastrad 	struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
   1435  1.26  riastrad 	const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
   1436  1.26  riastrad 	struct intel_atomic_state *intel_state =
   1437  1.26  riastrad 		to_intel_atomic_state(new_crtc_state->uapi.state);
   1438  1.26  riastrad 	const struct intel_crtc_state *old_crtc_state =
   1439  1.26  riastrad 		intel_atomic_get_old_crtc_state(intel_state, crtc);
   1440  1.26  riastrad 	const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
   1441  1.26  riastrad 	enum plane_id plane_id;
   1442  1.10  riastrad 
   1443  1.26  riastrad 	if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
   1444  1.26  riastrad 		*intermediate = *optimal;
   1445  1.10  riastrad 
   1446  1.26  riastrad 		intermediate->cxsr = false;
   1447  1.26  riastrad 		intermediate->hpll_en = false;
   1448  1.26  riastrad 		goto out;
   1449  1.26  riastrad 	}
   1450  1.10  riastrad 
   1451  1.26  riastrad 	intermediate->cxsr = optimal->cxsr && active->cxsr &&
   1452  1.26  riastrad 		!new_crtc_state->disable_cxsr;
   1453  1.26  riastrad 	intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
   1454  1.26  riastrad 		!new_crtc_state->disable_cxsr;
   1455  1.26  riastrad 	intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
   1456  1.26  riastrad 
   1457  1.26  riastrad 	for_each_plane_id_on_crtc(crtc, plane_id) {
   1458  1.26  riastrad 		intermediate->wm.plane[plane_id] =
   1459  1.26  riastrad 			max(optimal->wm.plane[plane_id],
   1460  1.26  riastrad 			    active->wm.plane[plane_id]);
   1461  1.26  riastrad 
   1462  1.26  riastrad 		WARN_ON(intermediate->wm.plane[plane_id] >
   1463  1.26  riastrad 			g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
   1464  1.26  riastrad 	}
   1465  1.26  riastrad 
   1466  1.26  riastrad 	intermediate->sr.plane = max(optimal->sr.plane,
   1467  1.26  riastrad 				     active->sr.plane);
   1468  1.26  riastrad 	intermediate->sr.cursor = max(optimal->sr.cursor,
   1469  1.26  riastrad 				      active->sr.cursor);
   1470  1.26  riastrad 	intermediate->sr.fbc = max(optimal->sr.fbc,
   1471  1.26  riastrad 				   active->sr.fbc);
   1472  1.26  riastrad 
   1473  1.26  riastrad 	intermediate->hpll.plane = max(optimal->hpll.plane,
   1474  1.26  riastrad 				       active->hpll.plane);
   1475  1.26  riastrad 	intermediate->hpll.cursor = max(optimal->hpll.cursor,
   1476  1.26  riastrad 					active->hpll.cursor);
   1477  1.26  riastrad 	intermediate->hpll.fbc = max(optimal->hpll.fbc,
   1478  1.26  riastrad 				     active->hpll.fbc);
   1479  1.26  riastrad 
   1480  1.26  riastrad 	WARN_ON((intermediate->sr.plane >
   1481  1.26  riastrad 		 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
   1482  1.26  riastrad 		 intermediate->sr.cursor >
   1483  1.26  riastrad 		 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
   1484  1.26  riastrad 		intermediate->cxsr);
   1485  1.26  riastrad 	WARN_ON((intermediate->sr.plane >
   1486  1.26  riastrad 		 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
   1487  1.26  riastrad 		 intermediate->sr.cursor >
   1488  1.26  riastrad 		 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
   1489  1.26  riastrad 		intermediate->hpll_en);
   1490  1.26  riastrad 
   1491  1.26  riastrad 	WARN_ON(intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
   1492  1.26  riastrad 		intermediate->fbc_en && intermediate->cxsr);
   1493  1.26  riastrad 	WARN_ON(intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
   1494  1.26  riastrad 		intermediate->fbc_en && intermediate->hpll_en);
   1495  1.10  riastrad 
   1496  1.26  riastrad out:
   1497  1.26  riastrad 	/*
   1498  1.26  riastrad 	 * If our intermediate WM are identical to the final WM, then we can
   1499  1.26  riastrad 	 * omit the post-vblank programming; only update if it's different.
   1500  1.26  riastrad 	 */
   1501  1.26  riastrad 	if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
   1502  1.26  riastrad 		new_crtc_state->wm.need_postvbl_update = true;
   1503  1.10  riastrad 
   1504  1.26  riastrad 	return 0;
   1505   1.1  riastrad }
   1506   1.1  riastrad 
   1507  1.26  riastrad static void g4x_merge_wm(struct drm_i915_private *dev_priv,
   1508  1.26  riastrad 			 struct g4x_wm_values *wm)
   1509  1.10  riastrad {
   1510  1.10  riastrad 	struct intel_crtc *crtc;
   1511  1.26  riastrad 	int num_active_pipes = 0;
   1512  1.10  riastrad 
   1513  1.10  riastrad 	wm->cxsr = true;
   1514  1.26  riastrad 	wm->hpll_en = true;
   1515  1.26  riastrad 	wm->fbc_en = true;
   1516  1.10  riastrad 
   1517  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   1518  1.26  riastrad 		const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
   1519  1.10  riastrad 
   1520  1.10  riastrad 		if (!crtc->active)
   1521  1.10  riastrad 			continue;
   1522  1.10  riastrad 
   1523  1.10  riastrad 		if (!wm_state->cxsr)
   1524  1.10  riastrad 			wm->cxsr = false;
   1525  1.26  riastrad 		if (!wm_state->hpll_en)
   1526  1.26  riastrad 			wm->hpll_en = false;
   1527  1.26  riastrad 		if (!wm_state->fbc_en)
   1528  1.26  riastrad 			wm->fbc_en = false;
   1529  1.10  riastrad 
   1530  1.26  riastrad 		num_active_pipes++;
   1531  1.10  riastrad 	}
   1532  1.10  riastrad 
   1533  1.26  riastrad 	if (num_active_pipes != 1) {
   1534  1.10  riastrad 		wm->cxsr = false;
   1535  1.26  riastrad 		wm->hpll_en = false;
   1536  1.26  riastrad 		wm->fbc_en = false;
   1537  1.26  riastrad 	}
   1538  1.10  riastrad 
   1539  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   1540  1.26  riastrad 		const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
   1541  1.22  riastrad 		enum pipe pipe = crtc->pipe;
   1542  1.10  riastrad 
   1543  1.26  riastrad 		wm->pipe[pipe] = wm_state->wm;
   1544  1.26  riastrad 		if (crtc->active && wm->cxsr)
   1545  1.26  riastrad 			wm->sr = wm_state->sr;
   1546  1.26  riastrad 		if (crtc->active && wm->hpll_en)
   1547  1.26  riastrad 			wm->hpll = wm_state->hpll;
   1548  1.10  riastrad 	}
   1549  1.10  riastrad }
   1550   1.1  riastrad 
   1551  1.26  riastrad static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
   1552   1.1  riastrad {
   1553  1.26  riastrad 	struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
   1554  1.26  riastrad 	struct g4x_wm_values new_wm = {};
   1555  1.10  riastrad 
   1556  1.26  riastrad 	g4x_merge_wm(dev_priv, &new_wm);
   1557  1.10  riastrad 
   1558  1.26  riastrad 	if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
   1559  1.10  riastrad 		return;
   1560  1.10  riastrad 
   1561  1.26  riastrad 	if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
   1562  1.26  riastrad 		_intel_set_memory_cxsr(dev_priv, false);
   1563  1.10  riastrad 
   1564  1.26  riastrad 	g4x_write_wm_values(dev_priv, &new_wm);
   1565  1.10  riastrad 
   1566  1.26  riastrad 	if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
   1567  1.26  riastrad 		_intel_set_memory_cxsr(dev_priv, true);
   1568  1.10  riastrad 
   1569  1.26  riastrad 	*old_wm = new_wm;
   1570  1.26  riastrad }
   1571   1.1  riastrad 
   1572  1.26  riastrad static void g4x_initial_watermarks(struct intel_atomic_state *state,
   1573  1.26  riastrad 				   struct intel_crtc *crtc)
   1574  1.26  riastrad {
   1575  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   1576  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   1577  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   1578   1.1  riastrad 
   1579  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   1580  1.26  riastrad 	crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
   1581  1.26  riastrad 	g4x_program_watermarks(dev_priv);
   1582  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   1583  1.26  riastrad }
   1584   1.1  riastrad 
   1585  1.26  riastrad static void g4x_optimize_watermarks(struct intel_atomic_state *state,
   1586  1.26  riastrad 				    struct intel_crtc *crtc)
   1587  1.26  riastrad {
   1588  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   1589  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   1590  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   1591   1.1  riastrad 
   1592  1.26  riastrad 	if (!crtc_state->wm.need_postvbl_update)
   1593  1.26  riastrad 		return;
   1594   1.1  riastrad 
   1595  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   1596  1.26  riastrad 	crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
   1597  1.26  riastrad 	g4x_program_watermarks(dev_priv);
   1598  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   1599   1.1  riastrad }
   1600   1.1  riastrad 
   1601  1.26  riastrad /* latency must be in 0.1us units. */
   1602  1.26  riastrad static unsigned int vlv_wm_method2(unsigned int pixel_rate,
   1603  1.26  riastrad 				   unsigned int htotal,
   1604  1.26  riastrad 				   unsigned int width,
   1605  1.26  riastrad 				   unsigned int cpp,
   1606  1.26  riastrad 				   unsigned int latency)
   1607   1.1  riastrad {
   1608  1.26  riastrad 	unsigned int ret;
   1609   1.1  riastrad 
   1610  1.26  riastrad 	ret = intel_wm_method2(pixel_rate, htotal,
   1611  1.26  riastrad 			       width, cpp, latency);
   1612  1.26  riastrad 	ret = DIV_ROUND_UP(ret, 64);
   1613   1.1  riastrad 
   1614  1.26  riastrad 	return ret;
   1615   1.1  riastrad }
   1616   1.1  riastrad 
   1617  1.26  riastrad static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
   1618   1.1  riastrad {
   1619  1.26  riastrad 	/* all latencies in usec */
   1620  1.26  riastrad 	dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
   1621  1.26  riastrad 
   1622  1.26  riastrad 	dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
   1623   1.1  riastrad 
   1624  1.26  riastrad 	if (IS_CHERRYVIEW(dev_priv)) {
   1625  1.26  riastrad 		dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
   1626  1.26  riastrad 		dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
   1627   1.1  riastrad 
   1628  1.26  riastrad 		dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
   1629  1.26  riastrad 	}
   1630  1.26  riastrad }
   1631   1.1  riastrad 
   1632  1.26  riastrad static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
   1633  1.26  riastrad 				const struct intel_plane_state *plane_state,
   1634  1.26  riastrad 				int level)
   1635  1.26  riastrad {
   1636  1.26  riastrad 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
   1637  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
   1638  1.26  riastrad 	const struct drm_display_mode *adjusted_mode =
   1639  1.26  riastrad 		&crtc_state->hw.adjusted_mode;
   1640  1.26  riastrad 	unsigned int clock, htotal, cpp, width, wm;
   1641   1.1  riastrad 
   1642  1.26  riastrad 	if (dev_priv->wm.pri_latency[level] == 0)
   1643  1.26  riastrad 		return USHRT_MAX;
   1644   1.1  riastrad 
   1645  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state))
   1646  1.26  riastrad 		return 0;
   1647   1.1  riastrad 
   1648  1.26  riastrad 	cpp = plane_state->hw.fb->format->cpp[0];
   1649  1.26  riastrad 	clock = adjusted_mode->crtc_clock;
   1650  1.26  riastrad 	htotal = adjusted_mode->crtc_htotal;
   1651  1.26  riastrad 	width = crtc_state->pipe_src_w;
   1652   1.1  riastrad 
   1653  1.26  riastrad 	if (plane->id == PLANE_CURSOR) {
   1654  1.26  riastrad 		/*
   1655  1.26  riastrad 		 * FIXME the formula gives values that are
   1656  1.26  riastrad 		 * too big for the cursor FIFO, and hence we
   1657  1.26  riastrad 		 * would never be able to use cursors. For
   1658  1.26  riastrad 		 * now just hardcode the watermark.
   1659  1.26  riastrad 		 */
   1660  1.26  riastrad 		wm = 63;
   1661   1.1  riastrad 	} else {
   1662  1.26  riastrad 		wm = vlv_wm_method2(clock, htotal, width, cpp,
   1663  1.26  riastrad 				    dev_priv->wm.pri_latency[level] * 10);
   1664   1.1  riastrad 	}
   1665   1.1  riastrad 
   1666  1.26  riastrad 	return min_t(unsigned int, wm, USHRT_MAX);
   1667  1.26  riastrad }
   1668  1.26  riastrad 
   1669  1.26  riastrad static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
   1670  1.26  riastrad {
   1671  1.26  riastrad 	return (active_planes & (BIT(PLANE_SPRITE0) |
   1672  1.26  riastrad 				 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
   1673  1.26  riastrad }
   1674   1.1  riastrad 
   1675  1.26  riastrad static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
   1676  1.26  riastrad {
   1677  1.26  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
   1678  1.26  riastrad 	const struct g4x_pipe_wm *raw =
   1679  1.26  riastrad 		&crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
   1680  1.26  riastrad 	struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
   1681  1.26  riastrad 	unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
   1682  1.26  riastrad 	int num_active_planes = hweight8(active_planes);
   1683  1.26  riastrad 	const int fifo_size = 511;
   1684  1.26  riastrad 	int fifo_extra, fifo_left = fifo_size;
   1685  1.26  riastrad 	int sprite0_fifo_extra = 0;
   1686  1.26  riastrad 	unsigned int total_rate;
   1687  1.26  riastrad 	enum plane_id plane_id;
   1688  1.26  riastrad 
   1689  1.26  riastrad 	/*
   1690  1.26  riastrad 	 * When enabling sprite0 after sprite1 has already been enabled
   1691  1.26  riastrad 	 * we tend to get an underrun unless sprite0 already has some
   1692  1.26  riastrad 	 * FIFO space allcoated. Hence we always allocate at least one
   1693  1.26  riastrad 	 * cacheline for sprite0 whenever sprite1 is enabled.
   1694  1.26  riastrad 	 *
   1695  1.26  riastrad 	 * All other plane enable sequences appear immune to this problem.
   1696  1.26  riastrad 	 */
   1697  1.26  riastrad 	if (vlv_need_sprite0_fifo_workaround(active_planes))
   1698  1.26  riastrad 		sprite0_fifo_extra = 1;
   1699  1.10  riastrad 
   1700  1.26  riastrad 	total_rate = raw->plane[PLANE_PRIMARY] +
   1701  1.26  riastrad 		raw->plane[PLANE_SPRITE0] +
   1702  1.26  riastrad 		raw->plane[PLANE_SPRITE1] +
   1703  1.26  riastrad 		sprite0_fifo_extra;
   1704   1.1  riastrad 
   1705  1.26  riastrad 	if (total_rate > fifo_size)
   1706  1.26  riastrad 		return -EINVAL;
   1707  1.10  riastrad 
   1708  1.26  riastrad 	if (total_rate == 0)
   1709  1.26  riastrad 		total_rate = 1;
   1710   1.1  riastrad 
   1711  1.26  riastrad 	for_each_plane_id_on_crtc(crtc, plane_id) {
   1712  1.26  riastrad 		unsigned int rate;
   1713   1.1  riastrad 
   1714  1.26  riastrad 		if ((active_planes & BIT(plane_id)) == 0) {
   1715  1.26  riastrad 			fifo_state->plane[plane_id] = 0;
   1716  1.26  riastrad 			continue;
   1717  1.26  riastrad 		}
   1718   1.1  riastrad 
   1719  1.26  riastrad 		rate = raw->plane[plane_id];
   1720  1.26  riastrad 		fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
   1721  1.26  riastrad 		fifo_left -= fifo_state->plane[plane_id];
   1722  1.10  riastrad 	}
   1723  1.10  riastrad 
   1724  1.26  riastrad 	fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
   1725  1.26  riastrad 	fifo_left -= sprite0_fifo_extra;
   1726   1.1  riastrad 
   1727  1.26  riastrad 	fifo_state->plane[PLANE_CURSOR] = 63;
   1728   1.1  riastrad 
   1729  1.26  riastrad 	fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
   1730   1.1  riastrad 
   1731  1.26  riastrad 	/* spread the remainder evenly */
   1732  1.26  riastrad 	for_each_plane_id_on_crtc(crtc, plane_id) {
   1733  1.26  riastrad 		int plane_extra;
   1734   1.1  riastrad 
   1735  1.26  riastrad 		if (fifo_left == 0)
   1736  1.26  riastrad 			break;
   1737   1.3  riastrad 
   1738  1.26  riastrad 		if ((active_planes & BIT(plane_id)) == 0)
   1739  1.26  riastrad 			continue;
   1740   1.3  riastrad 
   1741  1.26  riastrad 		plane_extra = min(fifo_extra, fifo_left);
   1742  1.26  riastrad 		fifo_state->plane[plane_id] += plane_extra;
   1743  1.26  riastrad 		fifo_left -= plane_extra;
   1744   1.3  riastrad 	}
   1745   1.3  riastrad 
   1746  1.26  riastrad 	WARN_ON(active_planes != 0 && fifo_left != 0);
   1747   1.1  riastrad 
   1748  1.26  riastrad 	/* give it all to the first plane if none are active */
   1749  1.26  riastrad 	if (active_planes == 0) {
   1750  1.26  riastrad 		WARN_ON(fifo_left != fifo_size);
   1751  1.26  riastrad 		fifo_state->plane[PLANE_PRIMARY] = fifo_left;
   1752   1.1  riastrad 	}
   1753   1.1  riastrad 
   1754  1.26  riastrad 	return 0;
   1755  1.26  riastrad }
   1756   1.1  riastrad 
   1757  1.26  riastrad /* mark all levels starting from 'level' as invalid */
   1758  1.26  riastrad static void vlv_invalidate_wms(struct intel_crtc *crtc,
   1759  1.26  riastrad 			       struct vlv_wm_state *wm_state, int level)
   1760  1.26  riastrad {
   1761  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   1762   1.1  riastrad 
   1763  1.26  riastrad 	for (; level < intel_wm_num_levels(dev_priv); level++) {
   1764  1.26  riastrad 		enum plane_id plane_id;
   1765   1.1  riastrad 
   1766  1.26  riastrad 		for_each_plane_id_on_crtc(crtc, plane_id)
   1767  1.26  riastrad 			wm_state->wm[level].plane[plane_id] = USHRT_MAX;
   1768   1.1  riastrad 
   1769  1.26  riastrad 		wm_state->sr[level].cursor = USHRT_MAX;
   1770  1.26  riastrad 		wm_state->sr[level].plane = USHRT_MAX;
   1771  1.26  riastrad 	}
   1772   1.1  riastrad }
   1773   1.1  riastrad 
   1774  1.26  riastrad static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
   1775   1.1  riastrad {
   1776  1.26  riastrad 	if (wm > fifo_size)
   1777  1.26  riastrad 		return USHRT_MAX;
   1778  1.26  riastrad 	else
   1779  1.26  riastrad 		return fifo_size - wm;
   1780  1.26  riastrad }
   1781   1.1  riastrad 
   1782  1.26  riastrad /*
   1783  1.26  riastrad  * Starting from 'level' set all higher
   1784  1.26  riastrad  * levels to 'value' in the "raw" watermarks.
   1785  1.26  riastrad  */
   1786  1.26  riastrad static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
   1787  1.26  riastrad 				 int level, enum plane_id plane_id, u16 value)
   1788  1.26  riastrad {
   1789  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   1790  1.26  riastrad 	int num_levels = intel_wm_num_levels(dev_priv);
   1791  1.26  riastrad 	bool dirty = false;
   1792   1.1  riastrad 
   1793  1.26  riastrad 	for (; level < num_levels; level++) {
   1794  1.26  riastrad 		struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
   1795   1.1  riastrad 
   1796  1.26  riastrad 		dirty |= raw->plane[plane_id] != value;
   1797  1.26  riastrad 		raw->plane[plane_id] = value;
   1798  1.26  riastrad 	}
   1799   1.1  riastrad 
   1800  1.26  riastrad 	return dirty;
   1801   1.1  riastrad }
   1802   1.1  riastrad 
   1803  1.26  riastrad static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
   1804  1.26  riastrad 				     const struct intel_plane_state *plane_state)
   1805   1.3  riastrad {
   1806  1.26  riastrad 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
   1807  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   1808  1.26  riastrad 	enum plane_id plane_id = plane->id;
   1809  1.26  riastrad 	int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
   1810  1.26  riastrad 	int level;
   1811  1.26  riastrad 	bool dirty = false;
   1812   1.3  riastrad 
   1813  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state)) {
   1814  1.26  riastrad 		dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
   1815  1.26  riastrad 		goto out;
   1816  1.26  riastrad 	}
   1817   1.1  riastrad 
   1818  1.26  riastrad 	for (level = 0; level < num_levels; level++) {
   1819  1.26  riastrad 		struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
   1820  1.26  riastrad 		int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
   1821  1.26  riastrad 		int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
   1822   1.1  riastrad 
   1823  1.26  riastrad 		if (wm > max_wm)
   1824  1.26  riastrad 			break;
   1825  1.10  riastrad 
   1826  1.26  riastrad 		dirty |= raw->plane[plane_id] != wm;
   1827  1.26  riastrad 		raw->plane[plane_id] = wm;
   1828  1.26  riastrad 	}
   1829   1.1  riastrad 
   1830  1.26  riastrad 	/* mark all higher levels as invalid */
   1831  1.26  riastrad 	dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
   1832   1.1  riastrad 
   1833  1.26  riastrad out:
   1834  1.26  riastrad 	if (dirty)
   1835  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   1836  1.26  riastrad 			    "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
   1837  1.26  riastrad 			    plane->base.name,
   1838  1.26  riastrad 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
   1839  1.26  riastrad 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
   1840  1.26  riastrad 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
   1841   1.1  riastrad 
   1842  1.26  riastrad 	return dirty;
   1843   1.3  riastrad }
   1844   1.3  riastrad 
   1845  1.26  riastrad static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
   1846  1.26  riastrad 				      enum plane_id plane_id, int level)
   1847   1.3  riastrad {
   1848  1.26  riastrad 	const struct g4x_pipe_wm *raw =
   1849  1.26  riastrad 		&crtc_state->wm.vlv.raw[level];
   1850  1.26  riastrad 	const struct vlv_fifo_state *fifo_state =
   1851  1.26  riastrad 		&crtc_state->wm.vlv.fifo_state;
   1852   1.1  riastrad 
   1853  1.26  riastrad 	return raw->plane[plane_id] <= fifo_state->plane[plane_id];
   1854   1.3  riastrad }
   1855   1.1  riastrad 
   1856  1.26  riastrad static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
   1857   1.3  riastrad {
   1858  1.26  riastrad 	return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
   1859  1.26  riastrad 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
   1860  1.26  riastrad 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
   1861  1.26  riastrad 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
   1862   1.3  riastrad }
   1863   1.1  riastrad 
   1864  1.26  riastrad static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
   1865   1.3  riastrad {
   1866  1.26  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
   1867  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   1868  1.26  riastrad 	struct intel_atomic_state *state =
   1869  1.26  riastrad 		to_intel_atomic_state(crtc_state->uapi.state);
   1870  1.26  riastrad 	struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
   1871  1.26  riastrad 	const struct vlv_fifo_state *fifo_state =
   1872  1.26  riastrad 		&crtc_state->wm.vlv.fifo_state;
   1873  1.26  riastrad 	int num_active_planes = hweight8(crtc_state->active_planes &
   1874  1.26  riastrad 					 ~BIT(PLANE_CURSOR));
   1875  1.26  riastrad 	bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
   1876  1.26  riastrad 	const struct intel_plane_state *old_plane_state;
   1877  1.26  riastrad 	const struct intel_plane_state *new_plane_state;
   1878  1.26  riastrad 	struct intel_plane *plane;
   1879  1.26  riastrad 	enum plane_id plane_id;
   1880  1.26  riastrad 	int level, ret, i;
   1881  1.26  riastrad 	unsigned int dirty = 0;
   1882   1.1  riastrad 
   1883  1.26  riastrad 	for_each_oldnew_intel_plane_in_state(state, plane,
   1884  1.26  riastrad 					     old_plane_state,
   1885  1.26  riastrad 					     new_plane_state, i) {
   1886  1.26  riastrad 		if (new_plane_state->hw.crtc != &crtc->base &&
   1887  1.26  riastrad 		    old_plane_state->hw.crtc != &crtc->base)
   1888  1.26  riastrad 			continue;
   1889   1.3  riastrad 
   1890  1.26  riastrad 		if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
   1891  1.26  riastrad 			dirty |= BIT(plane->id);
   1892  1.26  riastrad 	}
   1893   1.3  riastrad 
   1894  1.26  riastrad 	/*
   1895  1.26  riastrad 	 * DSPARB registers may have been reset due to the
   1896  1.26  riastrad 	 * power well being turned off. Make sure we restore
   1897  1.26  riastrad 	 * them to a consistent state even if no primary/sprite
   1898  1.26  riastrad 	 * planes are initially active.
   1899  1.26  riastrad 	 */
   1900  1.26  riastrad 	if (needs_modeset)
   1901  1.26  riastrad 		crtc_state->fifo_changed = true;
   1902   1.3  riastrad 
   1903  1.26  riastrad 	if (!dirty)
   1904   1.3  riastrad 		return 0;
   1905   1.1  riastrad 
   1906  1.26  riastrad 	/* cursor changes don't warrant a FIFO recompute */
   1907  1.26  riastrad 	if (dirty & ~BIT(PLANE_CURSOR)) {
   1908  1.26  riastrad 		const struct intel_crtc_state *old_crtc_state =
   1909  1.26  riastrad 			intel_atomic_get_old_crtc_state(state, crtc);
   1910  1.26  riastrad 		const struct vlv_fifo_state *old_fifo_state =
   1911  1.26  riastrad 			&old_crtc_state->wm.vlv.fifo_state;
   1912   1.3  riastrad 
   1913  1.26  riastrad 		ret = vlv_compute_fifo(crtc_state);
   1914  1.26  riastrad 		if (ret)
   1915  1.26  riastrad 			return ret;
   1916   1.3  riastrad 
   1917  1.26  riastrad 		if (needs_modeset ||
   1918  1.26  riastrad 		    memcmp(old_fifo_state, fifo_state,
   1919  1.26  riastrad 			   sizeof(*fifo_state)) != 0)
   1920  1.26  riastrad 			crtc_state->fifo_changed = true;
   1921  1.26  riastrad 	}
   1922   1.1  riastrad 
   1923  1.26  riastrad 	/* initially allow all levels */
   1924  1.26  riastrad 	wm_state->num_levels = intel_wm_num_levels(dev_priv);
   1925  1.26  riastrad 	/*
   1926  1.26  riastrad 	 * Note that enabling cxsr with no primary/sprite planes
   1927  1.26  riastrad 	 * enabled can wedge the pipe. Hence we only allow cxsr
   1928  1.26  riastrad 	 * with exactly one enabled primary/sprite plane.
   1929  1.26  riastrad 	 */
   1930  1.26  riastrad 	wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
   1931   1.1  riastrad 
   1932  1.26  riastrad 	for (level = 0; level < wm_state->num_levels; level++) {
   1933  1.26  riastrad 		const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
   1934  1.26  riastrad 		const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
   1935   1.1  riastrad 
   1936  1.26  riastrad 		if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
   1937  1.26  riastrad 			break;
   1938   1.1  riastrad 
   1939  1.26  riastrad 		for_each_plane_id_on_crtc(crtc, plane_id) {
   1940  1.26  riastrad 			wm_state->wm[level].plane[plane_id] =
   1941  1.26  riastrad 				vlv_invert_wm_value(raw->plane[plane_id],
   1942  1.26  riastrad 						    fifo_state->plane[plane_id]);
   1943  1.26  riastrad 		}
   1944   1.1  riastrad 
   1945  1.26  riastrad 		wm_state->sr[level].plane =
   1946  1.26  riastrad 			vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
   1947  1.26  riastrad 						 raw->plane[PLANE_SPRITE0],
   1948  1.26  riastrad 						 raw->plane[PLANE_SPRITE1]),
   1949  1.26  riastrad 					    sr_fifo_size);
   1950  1.10  riastrad 
   1951  1.26  riastrad 		wm_state->sr[level].cursor =
   1952  1.26  riastrad 			vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
   1953  1.26  riastrad 					    63);
   1954  1.26  riastrad 	}
   1955   1.1  riastrad 
   1956  1.26  riastrad 	if (level == 0)
   1957  1.26  riastrad 		return -EINVAL;
   1958   1.1  riastrad 
   1959  1.26  riastrad 	/* limit to only levels we can actually handle */
   1960  1.26  riastrad 	wm_state->num_levels = level;
   1961  1.10  riastrad 
   1962  1.26  riastrad 	/* invalidate the higher levels */
   1963  1.26  riastrad 	vlv_invalidate_wms(crtc, wm_state, level);
   1964   1.1  riastrad 
   1965  1.26  riastrad 	return 0;
   1966   1.3  riastrad }
   1967   1.1  riastrad 
   1968  1.26  riastrad #define VLV_FIFO(plane, value) \
   1969  1.26  riastrad 	(((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
   1970  1.26  riastrad 
   1971  1.26  riastrad static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
   1972  1.26  riastrad 				   struct intel_crtc *crtc)
   1973   1.3  riastrad {
   1974  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   1975  1.26  riastrad 	struct intel_uncore *uncore = &dev_priv->uncore;
   1976  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   1977  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   1978  1.26  riastrad 	const struct vlv_fifo_state *fifo_state =
   1979  1.26  riastrad 		&crtc_state->wm.vlv.fifo_state;
   1980  1.26  riastrad 	int sprite0_start, sprite1_start, fifo_size;
   1981  1.26  riastrad 
   1982  1.26  riastrad 	if (!crtc_state->fifo_changed)
   1983  1.26  riastrad 		return;
   1984   1.1  riastrad 
   1985  1.26  riastrad 	sprite0_start = fifo_state->plane[PLANE_PRIMARY];
   1986  1.26  riastrad 	sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
   1987  1.26  riastrad 	fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
   1988  1.10  riastrad 
   1989  1.26  riastrad 	WARN_ON(fifo_state->plane[PLANE_CURSOR] != 63);
   1990  1.26  riastrad 	WARN_ON(fifo_size != 511);
   1991  1.10  riastrad 
   1992  1.26  riastrad 	trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
   1993  1.10  riastrad 
   1994  1.26  riastrad 	/*
   1995  1.26  riastrad 	 * uncore.lock serves a double purpose here. It allows us to
   1996  1.26  riastrad 	 * use the less expensive I915_{READ,WRITE}_FW() functions, and
   1997  1.26  riastrad 	 * it protects the DSPARB registers from getting clobbered by
   1998  1.26  riastrad 	 * parallel updates from multiple pipes.
   1999  1.26  riastrad 	 *
   2000  1.26  riastrad 	 * intel_pipe_update_start() has already disabled interrupts
   2001  1.26  riastrad 	 * for us, so a plain spin_lock() is sufficient here.
   2002  1.26  riastrad 	 */
   2003  1.26  riastrad 	spin_lock(&uncore->lock);
   2004   1.1  riastrad 
   2005  1.26  riastrad 	switch (crtc->pipe) {
   2006  1.26  riastrad 		u32 dsparb, dsparb2, dsparb3;
   2007  1.26  riastrad 	case PIPE_A:
   2008  1.26  riastrad 		dsparb = intel_uncore_read_fw(uncore, DSPARB);
   2009  1.26  riastrad 		dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
   2010   1.1  riastrad 
   2011  1.26  riastrad 		dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
   2012  1.26  riastrad 			    VLV_FIFO(SPRITEB, 0xff));
   2013  1.26  riastrad 		dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
   2014  1.26  riastrad 			   VLV_FIFO(SPRITEB, sprite1_start));
   2015   1.1  riastrad 
   2016  1.26  riastrad 		dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
   2017  1.26  riastrad 			     VLV_FIFO(SPRITEB_HI, 0x1));
   2018  1.26  riastrad 		dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
   2019  1.26  riastrad 			   VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
   2020   1.1  riastrad 
   2021  1.26  riastrad 		intel_uncore_write_fw(uncore, DSPARB, dsparb);
   2022  1.26  riastrad 		intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
   2023  1.26  riastrad 		break;
   2024  1.26  riastrad 	case PIPE_B:
   2025  1.26  riastrad 		dsparb = intel_uncore_read_fw(uncore, DSPARB);
   2026  1.26  riastrad 		dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
   2027  1.26  riastrad 
   2028  1.26  riastrad 		dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
   2029  1.26  riastrad 			    VLV_FIFO(SPRITED, 0xff));
   2030  1.26  riastrad 		dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
   2031  1.26  riastrad 			   VLV_FIFO(SPRITED, sprite1_start));
   2032  1.26  riastrad 
   2033  1.26  riastrad 		dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
   2034  1.26  riastrad 			     VLV_FIFO(SPRITED_HI, 0xff));
   2035  1.26  riastrad 		dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
   2036  1.26  riastrad 			   VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
   2037  1.26  riastrad 
   2038  1.26  riastrad 		intel_uncore_write_fw(uncore, DSPARB, dsparb);
   2039  1.26  riastrad 		intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
   2040  1.26  riastrad 		break;
   2041  1.26  riastrad 	case PIPE_C:
   2042  1.26  riastrad 		dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
   2043  1.26  riastrad 		dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
   2044  1.26  riastrad 
   2045  1.26  riastrad 		dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
   2046  1.26  riastrad 			     VLV_FIFO(SPRITEF, 0xff));
   2047  1.26  riastrad 		dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
   2048  1.26  riastrad 			    VLV_FIFO(SPRITEF, sprite1_start));
   2049  1.26  riastrad 
   2050  1.26  riastrad 		dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
   2051  1.26  riastrad 			     VLV_FIFO(SPRITEF_HI, 0xff));
   2052  1.26  riastrad 		dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
   2053  1.26  riastrad 			   VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
   2054  1.26  riastrad 
   2055  1.26  riastrad 		intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
   2056  1.26  riastrad 		intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
   2057  1.26  riastrad 		break;
   2058  1.26  riastrad 	default:
   2059  1.26  riastrad 		break;
   2060   1.1  riastrad 	}
   2061   1.1  riastrad 
   2062  1.26  riastrad 	intel_uncore_posting_read_fw(uncore, DSPARB);
   2063  1.26  riastrad 
   2064  1.26  riastrad 	spin_unlock(&uncore->lock);
   2065   1.3  riastrad }
   2066   1.1  riastrad 
   2067  1.26  riastrad #undef VLV_FIFO
   2068  1.26  riastrad 
   2069  1.26  riastrad static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
   2070   1.3  riastrad {
   2071  1.26  riastrad 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
   2072  1.26  riastrad 	struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
   2073  1.26  riastrad 	const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
   2074  1.26  riastrad 	struct intel_atomic_state *intel_state =
   2075  1.26  riastrad 		to_intel_atomic_state(new_crtc_state->uapi.state);
   2076  1.26  riastrad 	const struct intel_crtc_state *old_crtc_state =
   2077  1.26  riastrad 		intel_atomic_get_old_crtc_state(intel_state, crtc);
   2078  1.26  riastrad 	const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
   2079  1.26  riastrad 	int level;
   2080   1.1  riastrad 
   2081  1.26  riastrad 	if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
   2082  1.26  riastrad 		*intermediate = *optimal;
   2083   1.1  riastrad 
   2084  1.26  riastrad 		intermediate->cxsr = false;
   2085  1.26  riastrad 		goto out;
   2086  1.26  riastrad 	}
   2087  1.10  riastrad 
   2088  1.26  riastrad 	intermediate->num_levels = min(optimal->num_levels, active->num_levels);
   2089  1.26  riastrad 	intermediate->cxsr = optimal->cxsr && active->cxsr &&
   2090  1.26  riastrad 		!new_crtc_state->disable_cxsr;
   2091   1.1  riastrad 
   2092  1.26  riastrad 	for (level = 0; level < intermediate->num_levels; level++) {
   2093  1.26  riastrad 		enum plane_id plane_id;
   2094   1.1  riastrad 
   2095  1.26  riastrad 		for_each_plane_id_on_crtc(crtc, plane_id) {
   2096  1.26  riastrad 			intermediate->wm[level].plane[plane_id] =
   2097  1.26  riastrad 				min(optimal->wm[level].plane[plane_id],
   2098  1.26  riastrad 				    active->wm[level].plane[plane_id]);
   2099  1.26  riastrad 		}
   2100   1.1  riastrad 
   2101  1.26  riastrad 		intermediate->sr[level].plane = min(optimal->sr[level].plane,
   2102  1.26  riastrad 						    active->sr[level].plane);
   2103  1.26  riastrad 		intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
   2104  1.26  riastrad 						     active->sr[level].cursor);
   2105  1.26  riastrad 	}
   2106   1.1  riastrad 
   2107  1.26  riastrad 	vlv_invalidate_wms(crtc, intermediate, level);
   2108   1.1  riastrad 
   2109  1.26  riastrad out:
   2110   1.3  riastrad 	/*
   2111  1.26  riastrad 	 * If our intermediate WM are identical to the final WM, then we can
   2112  1.26  riastrad 	 * omit the post-vblank programming; only update if it's different.
   2113   1.3  riastrad 	 */
   2114  1.26  riastrad 	if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
   2115  1.26  riastrad 		new_crtc_state->wm.need_postvbl_update = true;
   2116   1.3  riastrad 
   2117  1.26  riastrad 	return 0;
   2118   1.3  riastrad }
   2119   1.1  riastrad 
   2120  1.26  riastrad static void vlv_merge_wm(struct drm_i915_private *dev_priv,
   2121  1.26  riastrad 			 struct vlv_wm_values *wm)
   2122   1.3  riastrad {
   2123  1.26  riastrad 	struct intel_crtc *crtc;
   2124  1.26  riastrad 	int num_active_pipes = 0;
   2125  1.26  riastrad 
   2126  1.26  riastrad 	wm->level = dev_priv->wm.max_level;
   2127  1.26  riastrad 	wm->cxsr = true;
   2128  1.26  riastrad 
   2129  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   2130  1.26  riastrad 		const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
   2131  1.26  riastrad 
   2132  1.26  riastrad 		if (!crtc->active)
   2133  1.26  riastrad 			continue;
   2134   1.3  riastrad 
   2135  1.26  riastrad 		if (!wm_state->cxsr)
   2136  1.26  riastrad 			wm->cxsr = false;
   2137   1.3  riastrad 
   2138  1.26  riastrad 		num_active_pipes++;
   2139  1.26  riastrad 		wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
   2140  1.10  riastrad 	}
   2141  1.10  riastrad 
   2142  1.26  riastrad 	if (num_active_pipes != 1)
   2143  1.26  riastrad 		wm->cxsr = false;
   2144   1.1  riastrad 
   2145  1.26  riastrad 	if (num_active_pipes > 1)
   2146  1.26  riastrad 		wm->level = VLV_WM_LEVEL_PM2;
   2147   1.1  riastrad 
   2148  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   2149  1.26  riastrad 		const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
   2150  1.26  riastrad 		enum pipe pipe = crtc->pipe;
   2151   1.1  riastrad 
   2152  1.26  riastrad 		wm->pipe[pipe] = wm_state->wm[wm->level];
   2153  1.26  riastrad 		if (crtc->active && wm->cxsr)
   2154  1.26  riastrad 			wm->sr = wm_state->sr[wm->level];
   2155   1.1  riastrad 
   2156  1.26  riastrad 		wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
   2157  1.26  riastrad 		wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
   2158  1.26  riastrad 		wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
   2159  1.26  riastrad 		wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
   2160  1.26  riastrad 	}
   2161   1.3  riastrad }
   2162   1.3  riastrad 
   2163  1.26  riastrad static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
   2164   1.3  riastrad {
   2165  1.26  riastrad 	struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
   2166  1.26  riastrad 	struct vlv_wm_values new_wm = {};
   2167   1.3  riastrad 
   2168  1.26  riastrad 	vlv_merge_wm(dev_priv, &new_wm);
   2169  1.10  riastrad 
   2170  1.26  riastrad 	if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
   2171  1.26  riastrad 		return;
   2172  1.10  riastrad 
   2173  1.26  riastrad 	if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
   2174  1.26  riastrad 		chv_set_memory_dvfs(dev_priv, false);
   2175  1.10  riastrad 
   2176  1.26  riastrad 	if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
   2177  1.26  riastrad 		chv_set_memory_pm5(dev_priv, false);
   2178  1.10  riastrad 
   2179  1.26  riastrad 	if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
   2180  1.26  riastrad 		_intel_set_memory_cxsr(dev_priv, false);
   2181  1.10  riastrad 
   2182  1.26  riastrad 	vlv_write_wm_values(dev_priv, &new_wm);
   2183  1.10  riastrad 
   2184  1.26  riastrad 	if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
   2185  1.26  riastrad 		_intel_set_memory_cxsr(dev_priv, true);
   2186  1.10  riastrad 
   2187  1.26  riastrad 	if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
   2188  1.26  riastrad 		chv_set_memory_pm5(dev_priv, true);
   2189  1.10  riastrad 
   2190  1.26  riastrad 	if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
   2191  1.26  riastrad 		chv_set_memory_dvfs(dev_priv, true);
   2192   1.3  riastrad 
   2193  1.26  riastrad 	*old_wm = new_wm;
   2194   1.3  riastrad }
   2195   1.3  riastrad 
   2196  1.26  riastrad static void vlv_initial_watermarks(struct intel_atomic_state *state,
   2197  1.26  riastrad 				   struct intel_crtc *crtc)
   2198   1.3  riastrad {
   2199  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   2200  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   2201  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   2202  1.26  riastrad 
   2203  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   2204  1.26  riastrad 	crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
   2205  1.26  riastrad 	vlv_program_watermarks(dev_priv);
   2206  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   2207   1.3  riastrad }
   2208   1.3  riastrad 
   2209  1.26  riastrad static void vlv_optimize_watermarks(struct intel_atomic_state *state,
   2210  1.26  riastrad 				    struct intel_crtc *crtc)
   2211   1.3  riastrad {
   2212  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   2213  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   2214  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   2215   1.3  riastrad 
   2216  1.26  riastrad 	if (!crtc_state->wm.need_postvbl_update)
   2217  1.26  riastrad 		return;
   2218   1.1  riastrad 
   2219  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   2220  1.26  riastrad 	crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
   2221  1.26  riastrad 	vlv_program_watermarks(dev_priv);
   2222  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   2223   1.1  riastrad }
   2224   1.1  riastrad 
   2225  1.26  riastrad static void i965_update_wm(struct intel_crtc *unused_crtc)
   2226   1.1  riastrad {
   2227  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
   2228  1.26  riastrad 	struct intel_crtc *crtc;
   2229  1.26  riastrad 	int srwm = 1;
   2230  1.26  riastrad 	int cursor_sr = 16;
   2231  1.26  riastrad 	bool cxsr_enabled;
   2232   1.3  riastrad 
   2233  1.26  riastrad 	/* Calc sr entries for one plane configs */
   2234  1.26  riastrad 	crtc = single_enabled_crtc(dev_priv);
   2235  1.26  riastrad 	if (crtc) {
   2236  1.26  riastrad 		/* self-refresh has much higher latency */
   2237  1.26  riastrad 		static const int sr_latency_ns = 12000;
   2238  1.26  riastrad 		const struct drm_display_mode *adjusted_mode =
   2239  1.26  riastrad 			&crtc->config->hw.adjusted_mode;
   2240  1.26  riastrad 		const struct drm_framebuffer *fb =
   2241  1.26  riastrad 			crtc->base.primary->state->fb;
   2242  1.26  riastrad 		int clock = adjusted_mode->crtc_clock;
   2243  1.26  riastrad 		int htotal = adjusted_mode->crtc_htotal;
   2244  1.26  riastrad 		int hdisplay = crtc->config->pipe_src_w;
   2245  1.26  riastrad 		int cpp = fb->format->cpp[0];
   2246  1.26  riastrad 		int entries;
   2247   1.3  riastrad 
   2248  1.26  riastrad 		entries = intel_wm_method2(clock, htotal,
   2249  1.26  riastrad 					   hdisplay, cpp, sr_latency_ns / 100);
   2250  1.26  riastrad 		entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
   2251  1.26  riastrad 		srwm = I965_FIFO_SIZE - entries;
   2252  1.26  riastrad 		if (srwm < 0)
   2253  1.26  riastrad 			srwm = 1;
   2254  1.26  riastrad 		srwm &= 0x1ff;
   2255  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   2256  1.26  riastrad 			    "self-refresh entries: %d, wm: %d\n",
   2257  1.26  riastrad 			    entries, srwm);
   2258  1.26  riastrad 
   2259  1.26  riastrad 		entries = intel_wm_method2(clock, htotal,
   2260  1.26  riastrad 					   crtc->base.cursor->state->crtc_w, 4,
   2261  1.26  riastrad 					   sr_latency_ns / 100);
   2262  1.26  riastrad 		entries = DIV_ROUND_UP(entries,
   2263  1.26  riastrad 				       i965_cursor_wm_info.cacheline_size) +
   2264  1.26  riastrad 			i965_cursor_wm_info.guard_size;
   2265   1.3  riastrad 
   2266  1.26  riastrad 		cursor_sr = i965_cursor_wm_info.fifo_size - entries;
   2267  1.26  riastrad 		if (cursor_sr > i965_cursor_wm_info.max_wm)
   2268  1.26  riastrad 			cursor_sr = i965_cursor_wm_info.max_wm;
   2269  1.26  riastrad 
   2270  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   2271  1.26  riastrad 			    "self-refresh watermark: display plane %d "
   2272  1.26  riastrad 			    "cursor %d\n", srwm, cursor_sr);
   2273   1.1  riastrad 
   2274  1.26  riastrad 		cxsr_enabled = true;
   2275  1.26  riastrad 	} else {
   2276  1.26  riastrad 		cxsr_enabled = false;
   2277  1.26  riastrad 		/* Turn off self refresh if both pipes are enabled */
   2278  1.26  riastrad 		intel_set_memory_cxsr(dev_priv, false);
   2279   1.1  riastrad 	}
   2280   1.3  riastrad 
   2281  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   2282  1.26  riastrad 		    "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
   2283  1.26  riastrad 		    srwm);
   2284   1.1  riastrad 
   2285  1.26  riastrad 	/* 965 has limitations... */
   2286  1.26  riastrad 	I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
   2287  1.26  riastrad 		   FW_WM(8, CURSORB) |
   2288  1.26  riastrad 		   FW_WM(8, PLANEB) |
   2289  1.26  riastrad 		   FW_WM(8, PLANEA));
   2290  1.26  riastrad 	I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
   2291  1.26  riastrad 		   FW_WM(8, PLANEC_OLD));
   2292  1.26  riastrad 	/* update cursor SR watermark */
   2293  1.26  riastrad 	I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
   2294   1.1  riastrad 
   2295  1.26  riastrad 	if (cxsr_enabled)
   2296  1.26  riastrad 		intel_set_memory_cxsr(dev_priv, true);
   2297   1.1  riastrad }
   2298   1.1  riastrad 
   2299  1.26  riastrad #undef FW_WM
   2300   1.3  riastrad 
   2301  1.26  riastrad static void i9xx_update_wm(struct intel_crtc *unused_crtc)
   2302   1.3  riastrad {
   2303  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
   2304  1.26  riastrad 	const struct intel_watermark_params *wm_info;
   2305  1.26  riastrad 	u32 fwater_lo;
   2306  1.26  riastrad 	u32 fwater_hi;
   2307  1.26  riastrad 	int cwm, srwm = 1;
   2308  1.26  riastrad 	int fifo_size;
   2309  1.26  riastrad 	int planea_wm, planeb_wm;
   2310  1.26  riastrad 	struct intel_crtc *crtc, *enabled = NULL;
   2311   1.3  riastrad 
   2312  1.26  riastrad 	if (IS_I945GM(dev_priv))
   2313  1.26  riastrad 		wm_info = &i945_wm_info;
   2314  1.26  riastrad 	else if (!IS_GEN(dev_priv, 2))
   2315  1.26  riastrad 		wm_info = &i915_wm_info;
   2316  1.26  riastrad 	else
   2317  1.26  riastrad 		wm_info = &i830_a_wm_info;
   2318   1.3  riastrad 
   2319  1.26  riastrad 	fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
   2320  1.26  riastrad 	crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
   2321  1.26  riastrad 	if (intel_crtc_active(crtc)) {
   2322  1.26  riastrad 		const struct drm_display_mode *adjusted_mode =
   2323  1.26  riastrad 			&crtc->config->hw.adjusted_mode;
   2324  1.26  riastrad 		const struct drm_framebuffer *fb =
   2325  1.26  riastrad 			crtc->base.primary->state->fb;
   2326  1.26  riastrad 		int cpp;
   2327   1.3  riastrad 
   2328  1.26  riastrad 		if (IS_GEN(dev_priv, 2))
   2329  1.26  riastrad 			cpp = 4;
   2330  1.26  riastrad 		else
   2331  1.26  riastrad 			cpp = fb->format->cpp[0];
   2332   1.3  riastrad 
   2333  1.26  riastrad 		planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
   2334  1.26  riastrad 					       wm_info, fifo_size, cpp,
   2335  1.26  riastrad 					       pessimal_latency_ns);
   2336  1.26  riastrad 		enabled = crtc;
   2337  1.26  riastrad 	} else {
   2338  1.26  riastrad 		planea_wm = fifo_size - wm_info->guard_size;
   2339  1.26  riastrad 		if (planea_wm > (long)wm_info->max_wm)
   2340  1.26  riastrad 			planea_wm = wm_info->max_wm;
   2341  1.26  riastrad 	}
   2342   1.3  riastrad 
   2343  1.26  riastrad 	if (IS_GEN(dev_priv, 2))
   2344  1.26  riastrad 		wm_info = &i830_bc_wm_info;
   2345   1.3  riastrad 
   2346  1.26  riastrad 	fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
   2347  1.26  riastrad 	crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
   2348  1.26  riastrad 	if (intel_crtc_active(crtc)) {
   2349  1.26  riastrad 		const struct drm_display_mode *adjusted_mode =
   2350  1.26  riastrad 			&crtc->config->hw.adjusted_mode;
   2351  1.26  riastrad 		const struct drm_framebuffer *fb =
   2352  1.26  riastrad 			crtc->base.primary->state->fb;
   2353  1.26  riastrad 		int cpp;
   2354   1.1  riastrad 
   2355  1.26  riastrad 		if (IS_GEN(dev_priv, 2))
   2356  1.26  riastrad 			cpp = 4;
   2357  1.26  riastrad 		else
   2358  1.26  riastrad 			cpp = fb->format->cpp[0];
   2359   1.1  riastrad 
   2360  1.26  riastrad 		planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
   2361  1.26  riastrad 					       wm_info, fifo_size, cpp,
   2362  1.26  riastrad 					       pessimal_latency_ns);
   2363  1.26  riastrad 		if (enabled == NULL)
   2364  1.26  riastrad 			enabled = crtc;
   2365  1.26  riastrad 		else
   2366  1.26  riastrad 			enabled = NULL;
   2367  1.26  riastrad 	} else {
   2368  1.26  riastrad 		planeb_wm = fifo_size - wm_info->guard_size;
   2369  1.26  riastrad 		if (planeb_wm > (long)wm_info->max_wm)
   2370  1.26  riastrad 			planeb_wm = wm_info->max_wm;
   2371  1.26  riastrad 	}
   2372   1.3  riastrad 
   2373  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   2374  1.26  riastrad 		    "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
   2375   1.3  riastrad 
   2376  1.26  riastrad 	if (IS_I915GM(dev_priv) && enabled) {
   2377  1.26  riastrad 		struct drm_i915_gem_object *obj;
   2378   1.3  riastrad 
   2379  1.26  riastrad 		obj = intel_fb_obj(enabled->base.primary->state->fb);
   2380   1.3  riastrad 
   2381  1.26  riastrad 		/* self-refresh seems busted with untiled */
   2382  1.26  riastrad 		if (!i915_gem_object_is_tiled(obj))
   2383  1.26  riastrad 			enabled = NULL;
   2384  1.10  riastrad 	}
   2385  1.10  riastrad 
   2386  1.26  riastrad 	/*
   2387  1.26  riastrad 	 * Overlay gets an aggressive default since video jitter is bad.
   2388  1.26  riastrad 	 */
   2389  1.26  riastrad 	cwm = 2;
   2390   1.3  riastrad 
   2391  1.26  riastrad 	/* Play safe and disable self-refresh before adjusting watermarks. */
   2392  1.26  riastrad 	intel_set_memory_cxsr(dev_priv, false);
   2393   1.1  riastrad 
   2394  1.26  riastrad 	/* Calc sr entries for one plane configs */
   2395  1.26  riastrad 	if (HAS_FW_BLC(dev_priv) && enabled) {
   2396  1.26  riastrad 		/* self-refresh has much higher latency */
   2397  1.26  riastrad 		static const int sr_latency_ns = 6000;
   2398  1.26  riastrad 		const struct drm_display_mode *adjusted_mode =
   2399  1.26  riastrad 			&enabled->config->hw.adjusted_mode;
   2400  1.26  riastrad 		const struct drm_framebuffer *fb =
   2401  1.26  riastrad 			enabled->base.primary->state->fb;
   2402  1.26  riastrad 		int clock = adjusted_mode->crtc_clock;
   2403  1.26  riastrad 		int htotal = adjusted_mode->crtc_htotal;
   2404  1.26  riastrad 		int hdisplay = enabled->config->pipe_src_w;
   2405  1.26  riastrad 		int cpp;
   2406  1.26  riastrad 		int entries;
   2407   1.3  riastrad 
   2408  1.26  riastrad 		if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
   2409  1.26  riastrad 			cpp = 4;
   2410  1.26  riastrad 		else
   2411  1.26  riastrad 			cpp = fb->format->cpp[0];
   2412   1.3  riastrad 
   2413  1.26  riastrad 		entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
   2414  1.26  riastrad 					   sr_latency_ns / 100);
   2415  1.26  riastrad 		entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
   2416  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   2417  1.26  riastrad 			    "self-refresh entries: %d\n", entries);
   2418  1.26  riastrad 		srwm = wm_info->fifo_size - entries;
   2419  1.26  riastrad 		if (srwm < 0)
   2420  1.26  riastrad 			srwm = 1;
   2421   1.3  riastrad 
   2422  1.26  riastrad 		if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
   2423  1.26  riastrad 			I915_WRITE(FW_BLC_SELF,
   2424  1.26  riastrad 				   FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
   2425  1.26  riastrad 		else
   2426  1.26  riastrad 			I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
   2427  1.10  riastrad 	}
   2428  1.10  riastrad 
   2429  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   2430  1.26  riastrad 		    "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
   2431  1.26  riastrad 		     planea_wm, planeb_wm, cwm, srwm);
   2432   1.3  riastrad 
   2433  1.26  riastrad 	fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
   2434  1.26  riastrad 	fwater_hi = (cwm & 0x1f);
   2435  1.10  riastrad 
   2436  1.26  riastrad 	/* Set request length to 8 cachelines per fetch */
   2437  1.26  riastrad 	fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
   2438  1.26  riastrad 	fwater_hi = fwater_hi | (1 << 8);
   2439  1.10  riastrad 
   2440  1.26  riastrad 	I915_WRITE(FW_BLC, fwater_lo);
   2441  1.26  riastrad 	I915_WRITE(FW_BLC2, fwater_hi);
   2442   1.3  riastrad 
   2443  1.26  riastrad 	if (enabled)
   2444  1.26  riastrad 		intel_set_memory_cxsr(dev_priv, true);
   2445   1.3  riastrad }
   2446   1.3  riastrad 
   2447  1.26  riastrad static void i845_update_wm(struct intel_crtc *unused_crtc)
   2448   1.3  riastrad {
   2449  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
   2450  1.26  riastrad 	struct intel_crtc *crtc;
   2451  1.26  riastrad 	const struct drm_display_mode *adjusted_mode;
   2452  1.26  riastrad 	u32 fwater_lo;
   2453  1.26  riastrad 	int planea_wm;
   2454   1.3  riastrad 
   2455  1.26  riastrad 	crtc = single_enabled_crtc(dev_priv);
   2456  1.26  riastrad 	if (crtc == NULL)
   2457   1.3  riastrad 		return;
   2458   1.3  riastrad 
   2459  1.26  riastrad 	adjusted_mode = &crtc->config->hw.adjusted_mode;
   2460  1.26  riastrad 	planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
   2461  1.26  riastrad 				       &i845_wm_info,
   2462  1.26  riastrad 				       dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
   2463  1.26  riastrad 				       4, pessimal_latency_ns);
   2464  1.26  riastrad 	fwater_lo = I915_READ(FW_BLC) & ~0xfff;
   2465  1.26  riastrad 	fwater_lo |= (3<<8) | planea_wm;
   2466   1.3  riastrad 
   2467  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   2468  1.26  riastrad 		    "Setting FIFO watermarks - A: %d\n", planea_wm);
   2469   1.3  riastrad 
   2470  1.26  riastrad 	I915_WRITE(FW_BLC, fwater_lo);
   2471   1.3  riastrad }
   2472   1.1  riastrad 
   2473  1.26  riastrad /* latency must be in 0.1us units. */
   2474  1.26  riastrad static unsigned int ilk_wm_method1(unsigned int pixel_rate,
   2475  1.26  riastrad 				   unsigned int cpp,
   2476  1.26  riastrad 				   unsigned int latency)
   2477   1.3  riastrad {
   2478  1.26  riastrad 	unsigned int ret;
   2479   1.1  riastrad 
   2480  1.26  riastrad 	ret = intel_wm_method1(pixel_rate, cpp, latency);
   2481  1.26  riastrad 	ret = DIV_ROUND_UP(ret, 64) + 2;
   2482   1.1  riastrad 
   2483  1.26  riastrad 	return ret;
   2484   1.3  riastrad }
   2485   1.3  riastrad 
   2486  1.26  riastrad /* latency must be in 0.1us units. */
   2487  1.26  riastrad static unsigned int ilk_wm_method2(unsigned int pixel_rate,
   2488  1.26  riastrad 				   unsigned int htotal,
   2489  1.26  riastrad 				   unsigned int width,
   2490  1.26  riastrad 				   unsigned int cpp,
   2491  1.26  riastrad 				   unsigned int latency)
   2492   1.3  riastrad {
   2493  1.26  riastrad 	unsigned int ret;
   2494   1.3  riastrad 
   2495  1.26  riastrad 	ret = intel_wm_method2(pixel_rate, htotal,
   2496  1.26  riastrad 			       width, cpp, latency);
   2497  1.26  riastrad 	ret = DIV_ROUND_UP(ret, 64) + 2;
   2498   1.3  riastrad 
   2499  1.26  riastrad 	return ret;
   2500   1.3  riastrad }
   2501   1.3  riastrad 
   2502  1.26  riastrad static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
   2503   1.3  riastrad {
   2504  1.26  riastrad 	/*
   2505  1.26  riastrad 	 * Neither of these should be possible since this function shouldn't be
   2506  1.26  riastrad 	 * called if the CRTC is off or the plane is invisible.  But let's be
   2507  1.26  riastrad 	 * extra paranoid to avoid a potential divide-by-zero if we screw up
   2508  1.26  riastrad 	 * elsewhere in the driver.
   2509  1.26  riastrad 	 */
   2510  1.26  riastrad 	if (WARN_ON(!cpp))
   2511  1.26  riastrad 		return 0;
   2512  1.26  riastrad 	if (WARN_ON(!horiz_pixels))
   2513  1.26  riastrad 		return 0;
   2514   1.3  riastrad 
   2515  1.26  riastrad 	return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
   2516   1.3  riastrad }
   2517   1.3  riastrad 
   2518  1.26  riastrad struct ilk_wm_maximums {
   2519  1.26  riastrad 	u16 pri;
   2520  1.26  riastrad 	u16 spr;
   2521  1.26  riastrad 	u16 cur;
   2522  1.26  riastrad 	u16 fbc;
   2523  1.26  riastrad };
   2524   1.3  riastrad 
   2525  1.26  riastrad /*
   2526  1.26  riastrad  * For both WM_PIPE and WM_LP.
   2527  1.26  riastrad  * mem_value must be in 0.1us units.
   2528  1.26  riastrad  */
   2529  1.26  riastrad static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
   2530  1.26  riastrad 			      const struct intel_plane_state *plane_state,
   2531  1.26  riastrad 			      u32 mem_value, bool is_lp)
   2532   1.3  riastrad {
   2533  1.26  riastrad 	u32 method1, method2;
   2534  1.26  riastrad 	int cpp;
   2535   1.3  riastrad 
   2536  1.26  riastrad 	if (mem_value == 0)
   2537  1.26  riastrad 		return U32_MAX;
   2538   1.3  riastrad 
   2539  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state))
   2540  1.26  riastrad 		return 0;
   2541   1.3  riastrad 
   2542  1.26  riastrad 	cpp = plane_state->hw.fb->format->cpp[0];
   2543   1.3  riastrad 
   2544  1.26  riastrad 	method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
   2545   1.3  riastrad 
   2546  1.26  riastrad 	if (!is_lp)
   2547  1.26  riastrad 		return method1;
   2548   1.3  riastrad 
   2549  1.26  riastrad 	method2 = ilk_wm_method2(crtc_state->pixel_rate,
   2550  1.26  riastrad 				 crtc_state->hw.adjusted_mode.crtc_htotal,
   2551  1.26  riastrad 				 drm_rect_width(&plane_state->uapi.dst),
   2552  1.26  riastrad 				 cpp, mem_value);
   2553   1.3  riastrad 
   2554  1.26  riastrad 	return min(method1, method2);
   2555   1.3  riastrad }
   2556   1.3  riastrad 
   2557  1.26  riastrad /*
   2558  1.26  riastrad  * For both WM_PIPE and WM_LP.
   2559  1.26  riastrad  * mem_value must be in 0.1us units.
   2560  1.26  riastrad  */
   2561  1.26  riastrad static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
   2562  1.26  riastrad 			      const struct intel_plane_state *plane_state,
   2563  1.26  riastrad 			      u32 mem_value)
   2564   1.3  riastrad {
   2565  1.26  riastrad 	u32 method1, method2;
   2566  1.26  riastrad 	int cpp;
   2567  1.26  riastrad 
   2568  1.26  riastrad 	if (mem_value == 0)
   2569  1.26  riastrad 		return U32_MAX;
   2570   1.3  riastrad 
   2571  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state))
   2572  1.26  riastrad 		return 0;
   2573   1.1  riastrad 
   2574  1.26  riastrad 	cpp = plane_state->hw.fb->format->cpp[0];
   2575   1.3  riastrad 
   2576  1.26  riastrad 	method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
   2577  1.26  riastrad 	method2 = ilk_wm_method2(crtc_state->pixel_rate,
   2578  1.26  riastrad 				 crtc_state->hw.adjusted_mode.crtc_htotal,
   2579  1.26  riastrad 				 drm_rect_width(&plane_state->uapi.dst),
   2580  1.26  riastrad 				 cpp, mem_value);
   2581  1.26  riastrad 	return min(method1, method2);
   2582   1.3  riastrad }
   2583   1.3  riastrad 
   2584   1.3  riastrad /*
   2585  1.26  riastrad  * For both WM_PIPE and WM_LP.
   2586  1.26  riastrad  * mem_value must be in 0.1us units.
   2587   1.3  riastrad  */
   2588  1.26  riastrad static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
   2589  1.26  riastrad 			      const struct intel_plane_state *plane_state,
   2590  1.26  riastrad 			      u32 mem_value)
   2591   1.3  riastrad {
   2592  1.26  riastrad 	int cpp;
   2593   1.3  riastrad 
   2594  1.26  riastrad 	if (mem_value == 0)
   2595  1.26  riastrad 		return U32_MAX;
   2596   1.3  riastrad 
   2597  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state))
   2598  1.26  riastrad 		return 0;
   2599   1.3  riastrad 
   2600  1.26  riastrad 	cpp = plane_state->hw.fb->format->cpp[0];
   2601   1.3  riastrad 
   2602  1.26  riastrad 	return ilk_wm_method2(crtc_state->pixel_rate,
   2603  1.26  riastrad 			      crtc_state->hw.adjusted_mode.crtc_htotal,
   2604  1.26  riastrad 			      drm_rect_width(&plane_state->uapi.dst),
   2605  1.26  riastrad 			      cpp, mem_value);
   2606  1.26  riastrad }
   2607   1.3  riastrad 
   2608  1.26  riastrad /* Only for WM_LP. */
   2609  1.26  riastrad static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
   2610  1.26  riastrad 			      const struct intel_plane_state *plane_state,
   2611  1.26  riastrad 			      u32 pri_val)
   2612  1.26  riastrad {
   2613  1.26  riastrad 	int cpp;
   2614   1.1  riastrad 
   2615  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state))
   2616  1.26  riastrad 		return 0;
   2617   1.1  riastrad 
   2618  1.26  riastrad 	cpp = plane_state->hw.fb->format->cpp[0];
   2619   1.1  riastrad 
   2620  1.26  riastrad 	return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
   2621  1.26  riastrad 			  cpp);
   2622  1.26  riastrad }
   2623   1.1  riastrad 
   2624  1.26  riastrad static unsigned int
   2625  1.26  riastrad ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
   2626  1.26  riastrad {
   2627  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 8)
   2628  1.26  riastrad 		return 3072;
   2629  1.26  riastrad 	else if (INTEL_GEN(dev_priv) >= 7)
   2630  1.26  riastrad 		return 768;
   2631  1.26  riastrad 	else
   2632  1.26  riastrad 		return 512;
   2633  1.26  riastrad }
   2634   1.3  riastrad 
   2635  1.26  riastrad static unsigned int
   2636  1.26  riastrad ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
   2637  1.26  riastrad 		     int level, bool is_sprite)
   2638  1.26  riastrad {
   2639  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 8)
   2640  1.26  riastrad 		/* BDW primary/sprite plane watermarks */
   2641  1.26  riastrad 		return level == 0 ? 255 : 2047;
   2642  1.26  riastrad 	else if (INTEL_GEN(dev_priv) >= 7)
   2643  1.26  riastrad 		/* IVB/HSW primary/sprite plane watermarks */
   2644  1.26  riastrad 		return level == 0 ? 127 : 1023;
   2645  1.26  riastrad 	else if (!is_sprite)
   2646  1.26  riastrad 		/* ILK/SNB primary plane watermarks */
   2647  1.26  riastrad 		return level == 0 ? 127 : 511;
   2648  1.26  riastrad 	else
   2649  1.26  riastrad 		/* ILK/SNB sprite plane watermarks */
   2650  1.26  riastrad 		return level == 0 ? 63 : 255;
   2651   1.3  riastrad }
   2652   1.3  riastrad 
   2653  1.26  riastrad static unsigned int
   2654  1.26  riastrad ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
   2655   1.3  riastrad {
   2656  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 7)
   2657  1.26  riastrad 		return level == 0 ? 63 : 255;
   2658  1.26  riastrad 	else
   2659  1.26  riastrad 		return level == 0 ? 31 : 63;
   2660  1.26  riastrad }
   2661   1.3  riastrad 
   2662  1.26  riastrad static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
   2663  1.26  riastrad {
   2664  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 8)
   2665  1.26  riastrad 		return 31;
   2666  1.26  riastrad 	else
   2667  1.26  riastrad 		return 15;
   2668   1.3  riastrad }
   2669   1.3  riastrad 
   2670  1.26  riastrad /* Calculate the maximum primary/sprite plane watermark */
   2671  1.26  riastrad static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
   2672  1.26  riastrad 				     int level,
   2673  1.26  riastrad 				     const struct intel_wm_config *config,
   2674  1.26  riastrad 				     enum intel_ddb_partitioning ddb_partitioning,
   2675  1.26  riastrad 				     bool is_sprite)
   2676  1.26  riastrad {
   2677  1.26  riastrad 	unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
   2678  1.10  riastrad 
   2679  1.26  riastrad 	/* if sprites aren't enabled, sprites get nothing */
   2680  1.26  riastrad 	if (is_sprite && !config->sprites_enabled)
   2681  1.26  riastrad 		return 0;
   2682  1.10  riastrad 
   2683  1.26  riastrad 	/* HSW allows LP1+ watermarks even with multiple pipes */
   2684  1.26  riastrad 	if (level == 0 || config->num_pipes_active > 1) {
   2685  1.26  riastrad 		fifo_size /= INTEL_NUM_PIPES(dev_priv);
   2686  1.10  riastrad 
   2687  1.26  riastrad 		/*
   2688  1.26  riastrad 		 * For some reason the non self refresh
   2689  1.26  riastrad 		 * FIFO size is only half of the self
   2690  1.26  riastrad 		 * refresh FIFO size on ILK/SNB.
   2691  1.26  riastrad 		 */
   2692  1.26  riastrad 		if (INTEL_GEN(dev_priv) <= 6)
   2693  1.26  riastrad 			fifo_size /= 2;
   2694  1.10  riastrad 	}
   2695  1.10  riastrad 
   2696  1.26  riastrad 	if (config->sprites_enabled) {
   2697  1.26  riastrad 		/* level 0 is always calculated with 1:1 split */
   2698  1.26  riastrad 		if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
   2699  1.26  riastrad 			if (is_sprite)
   2700  1.26  riastrad 				fifo_size *= 5;
   2701  1.26  riastrad 			fifo_size /= 6;
   2702  1.26  riastrad 		} else {
   2703  1.26  riastrad 			fifo_size /= 2;
   2704  1.26  riastrad 		}
   2705  1.26  riastrad 	}
   2706  1.10  riastrad 
   2707  1.26  riastrad 	/* clamp to max that the registers can hold */
   2708  1.26  riastrad 	return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
   2709  1.26  riastrad }
   2710  1.10  riastrad 
   2711  1.26  riastrad /* Calculate the maximum cursor plane watermark */
   2712  1.26  riastrad static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
   2713  1.26  riastrad 				      int level,
   2714  1.26  riastrad 				      const struct intel_wm_config *config)
   2715  1.26  riastrad {
   2716  1.26  riastrad 	/* HSW LP1+ watermarks w/ multiple pipes */
   2717  1.26  riastrad 	if (level > 0 && config->num_pipes_active > 1)
   2718  1.26  riastrad 		return 64;
   2719  1.10  riastrad 
   2720  1.26  riastrad 	/* otherwise just report max that registers can hold */
   2721  1.26  riastrad 	return ilk_cursor_wm_reg_max(dev_priv, level);
   2722  1.10  riastrad }
   2723  1.10  riastrad 
   2724  1.26  riastrad static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
   2725  1.26  riastrad 				    int level,
   2726  1.26  riastrad 				    const struct intel_wm_config *config,
   2727  1.26  riastrad 				    enum intel_ddb_partitioning ddb_partitioning,
   2728  1.26  riastrad 				    struct ilk_wm_maximums *max)
   2729   1.3  riastrad {
   2730  1.26  riastrad 	max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
   2731  1.26  riastrad 	max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
   2732  1.26  riastrad 	max->cur = ilk_cursor_wm_max(dev_priv, level, config);
   2733  1.26  riastrad 	max->fbc = ilk_fbc_wm_reg_max(dev_priv);
   2734  1.10  riastrad }
   2735   1.3  riastrad 
   2736  1.26  riastrad static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
   2737  1.26  riastrad 					int level,
   2738  1.26  riastrad 					struct ilk_wm_maximums *max)
   2739  1.10  riastrad {
   2740  1.26  riastrad 	max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
   2741  1.26  riastrad 	max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
   2742  1.26  riastrad 	max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
   2743  1.26  riastrad 	max->fbc = ilk_fbc_wm_reg_max(dev_priv);
   2744  1.10  riastrad }
   2745   1.3  riastrad 
   2746  1.26  riastrad static bool ilk_validate_wm_level(int level,
   2747  1.26  riastrad 				  const struct ilk_wm_maximums *max,
   2748  1.26  riastrad 				  struct intel_wm_level *result)
   2749  1.10  riastrad {
   2750  1.26  riastrad 	bool ret;
   2751  1.26  riastrad 
   2752  1.26  riastrad 	/* already determined to be invalid? */
   2753  1.26  riastrad 	if (!result->enable)
   2754  1.26  riastrad 		return false;
   2755   1.1  riastrad 
   2756  1.26  riastrad 	result->enable = result->pri_val <= max->pri &&
   2757  1.26  riastrad 			 result->spr_val <= max->spr &&
   2758  1.26  riastrad 			 result->cur_val <= max->cur;
   2759   1.3  riastrad 
   2760  1.26  riastrad 	ret = result->enable;
   2761   1.3  riastrad 
   2762  1.26  riastrad 	/*
   2763  1.26  riastrad 	 * HACK until we can pre-compute everything,
   2764  1.26  riastrad 	 * and thus fail gracefully if LP0 watermarks
   2765  1.26  riastrad 	 * are exceeded...
   2766  1.26  riastrad 	 */
   2767  1.26  riastrad 	if (level == 0 && !result->enable) {
   2768  1.26  riastrad 		if (result->pri_val > max->pri)
   2769  1.26  riastrad 			DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
   2770  1.26  riastrad 				      level, result->pri_val, max->pri);
   2771  1.26  riastrad 		if (result->spr_val > max->spr)
   2772  1.26  riastrad 			DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
   2773  1.26  riastrad 				      level, result->spr_val, max->spr);
   2774  1.26  riastrad 		if (result->cur_val > max->cur)
   2775  1.26  riastrad 			DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
   2776  1.26  riastrad 				      level, result->cur_val, max->cur);
   2777   1.3  riastrad 
   2778  1.26  riastrad 		result->pri_val = min_t(u32, result->pri_val, max->pri);
   2779  1.26  riastrad 		result->spr_val = min_t(u32, result->spr_val, max->spr);
   2780  1.26  riastrad 		result->cur_val = min_t(u32, result->cur_val, max->cur);
   2781  1.26  riastrad 		result->enable = true;
   2782   1.1  riastrad 	}
   2783  1.26  riastrad 
   2784  1.26  riastrad 	return ret;
   2785  1.10  riastrad }
   2786   1.1  riastrad 
   2787  1.26  riastrad static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
   2788  1.26  riastrad 				 const struct intel_crtc *intel_crtc,
   2789  1.26  riastrad 				 int level,
   2790  1.26  riastrad 				 struct intel_crtc_state *crtc_state,
   2791  1.26  riastrad 				 const struct intel_plane_state *pristate,
   2792  1.26  riastrad 				 const struct intel_plane_state *sprstate,
   2793  1.26  riastrad 				 const struct intel_plane_state *curstate,
   2794  1.26  riastrad 				 struct intel_wm_level *result)
   2795  1.10  riastrad {
   2796  1.26  riastrad 	u16 pri_latency = dev_priv->wm.pri_latency[level];
   2797  1.26  riastrad 	u16 spr_latency = dev_priv->wm.spr_latency[level];
   2798  1.26  riastrad 	u16 cur_latency = dev_priv->wm.cur_latency[level];
   2799  1.26  riastrad 
   2800  1.26  riastrad 	/* WM1+ latency values stored in 0.5us units */
   2801  1.26  riastrad 	if (level > 0) {
   2802  1.26  riastrad 		pri_latency *= 5;
   2803  1.26  riastrad 		spr_latency *= 5;
   2804  1.26  riastrad 		cur_latency *= 5;
   2805  1.26  riastrad 	}
   2806   1.3  riastrad 
   2807  1.26  riastrad 	if (pristate) {
   2808  1.26  riastrad 		result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
   2809  1.26  riastrad 						     pri_latency, level);
   2810  1.26  riastrad 		result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
   2811  1.10  riastrad 	}
   2812   1.3  riastrad 
   2813  1.26  riastrad 	if (sprstate)
   2814  1.26  riastrad 		result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
   2815  1.26  riastrad 
   2816  1.26  riastrad 	if (curstate)
   2817  1.26  riastrad 		result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
   2818  1.26  riastrad 
   2819  1.26  riastrad 	result->enable = true;
   2820   1.3  riastrad }
   2821   1.3  riastrad 
   2822  1.26  riastrad static u32
   2823  1.26  riastrad hsw_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
   2824   1.3  riastrad {
   2825  1.26  riastrad 	const struct intel_atomic_state *intel_state =
   2826  1.26  riastrad 		to_intel_atomic_state(crtc_state->uapi.state);
   2827  1.26  riastrad 	const struct drm_display_mode *adjusted_mode =
   2828  1.26  riastrad 		&crtc_state->hw.adjusted_mode;
   2829  1.26  riastrad 	u32 linetime, ips_linetime;
   2830  1.10  riastrad 
   2831  1.26  riastrad 	if (!crtc_state->hw.active)
   2832  1.26  riastrad 		return 0;
   2833  1.26  riastrad 	if (WARN_ON(adjusted_mode->crtc_clock == 0))
   2834  1.26  riastrad 		return 0;
   2835  1.26  riastrad 	if (WARN_ON(intel_state->cdclk.logical.cdclk == 0))
   2836  1.26  riastrad 		return 0;
   2837   1.3  riastrad 
   2838  1.26  riastrad 	/* The WM are computed with base on how long it takes to fill a single
   2839  1.26  riastrad 	 * row at the given clock rate, multiplied by 8.
   2840  1.26  riastrad 	 * */
   2841  1.26  riastrad 	linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
   2842  1.26  riastrad 				     adjusted_mode->crtc_clock);
   2843  1.26  riastrad 	ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
   2844  1.26  riastrad 					 intel_state->cdclk.logical.cdclk);
   2845   1.3  riastrad 
   2846  1.26  riastrad 	return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
   2847  1.26  riastrad 	       PIPE_WM_LINETIME_TIME(linetime);
   2848   1.3  riastrad }
   2849   1.3  riastrad 
   2850  1.26  riastrad static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
   2851  1.26  riastrad 				  u16 wm[8])
   2852   1.3  riastrad {
   2853  1.26  riastrad 	struct intel_uncore *uncore = &dev_priv->uncore;
   2854  1.10  riastrad 
   2855  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 9) {
   2856  1.26  riastrad 		u32 val;
   2857  1.26  riastrad 		int ret, i;
   2858  1.26  riastrad 		int level, max_level = ilk_wm_max_level(dev_priv);
   2859  1.10  riastrad 
   2860  1.26  riastrad 		/* read the first set of memory latencies[0:3] */
   2861  1.26  riastrad 		val = 0; /* data0 to be programmed to 0 for first set */
   2862  1.26  riastrad 		ret = sandybridge_pcode_read(dev_priv,
   2863  1.26  riastrad 					     GEN9_PCODE_READ_MEM_LATENCY,
   2864  1.26  riastrad 					     &val, NULL);
   2865  1.10  riastrad 
   2866  1.26  riastrad 		if (ret) {
   2867  1.26  riastrad 			drm_err(&dev_priv->drm,
   2868  1.26  riastrad 				"SKL Mailbox read error = %d\n", ret);
   2869  1.26  riastrad 			return;
   2870  1.26  riastrad 		}
   2871  1.10  riastrad 
   2872  1.26  riastrad 		wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
   2873  1.26  riastrad 		wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
   2874  1.26  riastrad 				GEN9_MEM_LATENCY_LEVEL_MASK;
   2875  1.26  riastrad 		wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
   2876  1.26  riastrad 				GEN9_MEM_LATENCY_LEVEL_MASK;
   2877  1.26  riastrad 		wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
   2878  1.26  riastrad 				GEN9_MEM_LATENCY_LEVEL_MASK;
   2879  1.10  riastrad 
   2880  1.26  riastrad 		/* read the second set of memory latencies[4:7] */
   2881  1.26  riastrad 		val = 1; /* data0 to be programmed to 1 for second set */
   2882  1.26  riastrad 		ret = sandybridge_pcode_read(dev_priv,
   2883  1.26  riastrad 					     GEN9_PCODE_READ_MEM_LATENCY,
   2884  1.26  riastrad 					     &val, NULL);
   2885  1.26  riastrad 		if (ret) {
   2886  1.26  riastrad 			drm_err(&dev_priv->drm,
   2887  1.26  riastrad 				"SKL Mailbox read error = %d\n", ret);
   2888  1.26  riastrad 			return;
   2889  1.26  riastrad 		}
   2890  1.10  riastrad 
   2891  1.26  riastrad 		wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
   2892  1.26  riastrad 		wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
   2893  1.26  riastrad 				GEN9_MEM_LATENCY_LEVEL_MASK;
   2894  1.26  riastrad 		wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
   2895  1.26  riastrad 				GEN9_MEM_LATENCY_LEVEL_MASK;
   2896  1.26  riastrad 		wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
   2897  1.26  riastrad 				GEN9_MEM_LATENCY_LEVEL_MASK;
   2898   1.3  riastrad 
   2899   1.3  riastrad 		/*
   2900  1.26  riastrad 		 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
   2901  1.26  riastrad 		 * need to be disabled. We make sure to sanitize the values out
   2902  1.26  riastrad 		 * of the punit to satisfy this requirement.
   2903   1.3  riastrad 		 */
   2904  1.26  riastrad 		for (level = 1; level <= max_level; level++) {
   2905  1.26  riastrad 			if (wm[level] == 0) {
   2906  1.26  riastrad 				for (i = level + 1; i <= max_level; i++)
   2907  1.26  riastrad 					wm[i] = 0;
   2908  1.26  riastrad 				break;
   2909  1.26  riastrad 			}
   2910  1.26  riastrad 		}
   2911   1.3  riastrad 
   2912   1.3  riastrad 		/*
   2913  1.26  riastrad 		 * WaWmMemoryReadLatency:skl+,glk
   2914  1.26  riastrad 		 *
   2915  1.26  riastrad 		 * punit doesn't take into account the read latency so we need
   2916  1.26  riastrad 		 * to add 2us to the various latency levels we retrieve from the
   2917  1.26  riastrad 		 * punit when level 0 response data us 0us.
   2918   1.3  riastrad 		 */
   2919  1.26  riastrad 		if (wm[0] == 0) {
   2920  1.26  riastrad 			wm[0] += 2;
   2921  1.26  riastrad 			for (level = 1; level <= max_level; level++) {
   2922  1.26  riastrad 				if (wm[level] == 0)
   2923  1.26  riastrad 					break;
   2924  1.26  riastrad 				wm[level] += 2;
   2925  1.26  riastrad 			}
   2926  1.10  riastrad 		}
   2927  1.10  riastrad 
   2928  1.26  riastrad 		/*
   2929  1.26  riastrad 		 * WA Level-0 adjustment for 16GB DIMMs: SKL+
   2930  1.26  riastrad 		 * If we could not get dimm info enable this WA to prevent from
   2931  1.26  riastrad 		 * any underrun. If not able to get Dimm info assume 16GB dimm
   2932  1.26  riastrad 		 * to avoid any underrun.
   2933  1.26  riastrad 		 */
   2934  1.26  riastrad 		if (dev_priv->dram_info.is_16gb_dimm)
   2935  1.26  riastrad 			wm[0] += 1;
   2936  1.10  riastrad 
   2937  1.26  riastrad 	} else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
   2938  1.26  riastrad 		u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
   2939  1.10  riastrad 
   2940  1.26  riastrad 		wm[0] = (sskpd >> 56) & 0xFF;
   2941  1.26  riastrad 		if (wm[0] == 0)
   2942  1.26  riastrad 			wm[0] = sskpd & 0xF;
   2943  1.26  riastrad 		wm[1] = (sskpd >> 4) & 0xFF;
   2944  1.26  riastrad 		wm[2] = (sskpd >> 12) & 0xFF;
   2945  1.26  riastrad 		wm[3] = (sskpd >> 20) & 0x1FF;
   2946  1.26  riastrad 		wm[4] = (sskpd >> 32) & 0x1FF;
   2947  1.26  riastrad 	} else if (INTEL_GEN(dev_priv) >= 6) {
   2948  1.26  riastrad 		u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
   2949   1.3  riastrad 
   2950  1.26  riastrad 		wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
   2951  1.26  riastrad 		wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
   2952  1.26  riastrad 		wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
   2953  1.26  riastrad 		wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
   2954  1.26  riastrad 	} else if (INTEL_GEN(dev_priv) >= 5) {
   2955  1.26  riastrad 		u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
   2956  1.10  riastrad 
   2957  1.26  riastrad 		/* ILK primary LP0 latency is 700 ns */
   2958  1.26  riastrad 		wm[0] = 7;
   2959  1.26  riastrad 		wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
   2960  1.26  riastrad 		wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
   2961  1.10  riastrad 	} else {
   2962  1.26  riastrad 		MISSING_CASE(INTEL_DEVID(dev_priv));
   2963  1.10  riastrad 	}
   2964   1.1  riastrad }
   2965   1.1  riastrad 
   2966  1.26  riastrad static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
   2967  1.26  riastrad 				       u16 wm[5])
   2968   1.1  riastrad {
   2969  1.26  riastrad 	/* ILK sprite LP0 latency is 1300 ns */
   2970  1.26  riastrad 	if (IS_GEN(dev_priv, 5))
   2971  1.26  riastrad 		wm[0] = 13;
   2972   1.1  riastrad }
   2973   1.1  riastrad 
   2974  1.26  riastrad static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
   2975  1.26  riastrad 				       u16 wm[5])
   2976   1.1  riastrad {
   2977  1.26  riastrad 	/* ILK cursor LP0 latency is 1300 ns */
   2978  1.26  riastrad 	if (IS_GEN(dev_priv, 5))
   2979  1.26  riastrad 		wm[0] = 13;
   2980   1.1  riastrad }
   2981   1.1  riastrad 
   2982  1.26  riastrad int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
   2983   1.1  riastrad {
   2984  1.26  riastrad 	/* how many WM levels are we expecting */
   2985  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 9)
   2986  1.26  riastrad 		return 7;
   2987  1.26  riastrad 	else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   2988  1.26  riastrad 		return 4;
   2989  1.26  riastrad 	else if (INTEL_GEN(dev_priv) >= 6)
   2990  1.26  riastrad 		return 3;
   2991  1.26  riastrad 	else
   2992  1.26  riastrad 		return 2;
   2993  1.10  riastrad }
   2994   1.1  riastrad 
   2995  1.26  riastrad static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
   2996  1.26  riastrad 				   const char *name,
   2997  1.26  riastrad 				   const u16 wm[8])
   2998  1.10  riastrad {
   2999  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   3000  1.10  riastrad 
   3001  1.26  riastrad 	for (level = 0; level <= max_level; level++) {
   3002  1.26  riastrad 		unsigned int latency = wm[level];
   3003  1.10  riastrad 
   3004  1.26  riastrad 		if (latency == 0) {
   3005  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   3006  1.26  riastrad 				    "%s WM%d latency not provided\n",
   3007  1.26  riastrad 				    name, level);
   3008  1.26  riastrad 			continue;
   3009  1.10  riastrad 		}
   3010   1.1  riastrad 
   3011  1.26  riastrad 		/*
   3012  1.26  riastrad 		 * - latencies are in us on gen9.
   3013  1.26  riastrad 		 * - before then, WM1+ latency values are in 0.5us units
   3014  1.26  riastrad 		 */
   3015  1.26  riastrad 		if (INTEL_GEN(dev_priv) >= 9)
   3016  1.26  riastrad 			latency *= 10;
   3017  1.26  riastrad 		else if (level > 0)
   3018  1.26  riastrad 			latency *= 5;
   3019  1.10  riastrad 
   3020  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   3021  1.26  riastrad 			    "%s WM%d latency %u (%u.%u usec)\n", name, level,
   3022  1.26  riastrad 			    wm[level], latency / 10, latency % 10);
   3023   1.1  riastrad 	}
   3024  1.26  riastrad }
   3025  1.26  riastrad 
   3026  1.26  riastrad static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
   3027  1.26  riastrad 				    u16 wm[5], u16 min)
   3028  1.26  riastrad {
   3029  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   3030   1.1  riastrad 
   3031  1.26  riastrad 	if (wm[0] >= min)
   3032  1.10  riastrad 		return false;
   3033  1.10  riastrad 
   3034  1.26  riastrad 	wm[0] = max(wm[0], min);
   3035  1.26  riastrad 	for (level = 1; level <= max_level; level++)
   3036  1.26  riastrad 		wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
   3037   1.1  riastrad 
   3038  1.10  riastrad 	return true;
   3039   1.1  riastrad }
   3040   1.1  riastrad 
   3041  1.26  riastrad static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
   3042  1.10  riastrad {
   3043  1.26  riastrad 	bool changed;
   3044  1.10  riastrad 
   3045  1.26  riastrad 	/*
   3046  1.26  riastrad 	 * The BIOS provided WM memory latency values are often
   3047  1.26  riastrad 	 * inadequate for high resolution displays. Adjust them.
   3048  1.26  riastrad 	 */
   3049  1.26  riastrad 	changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
   3050  1.26  riastrad 		ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
   3051  1.26  riastrad 		ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
   3052   1.1  riastrad 
   3053  1.26  riastrad 	if (!changed)
   3054  1.26  riastrad 		return;
   3055  1.10  riastrad 
   3056  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   3057  1.26  riastrad 		    "WM latency values increased to avoid potential underruns\n");
   3058  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
   3059  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
   3060  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
   3061  1.10  riastrad }
   3062   1.1  riastrad 
   3063  1.26  riastrad static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
   3064   1.1  riastrad {
   3065  1.26  riastrad 	/*
   3066  1.26  riastrad 	 * On some SNB machines (Thinkpad X220 Tablet at least)
   3067  1.26  riastrad 	 * LP3 usage can cause vblank interrupts to be lost.
   3068  1.26  riastrad 	 * The DEIIR bit will go high but it looks like the CPU
   3069  1.26  riastrad 	 * never gets interrupted.
   3070  1.26  riastrad 	 *
   3071  1.26  riastrad 	 * It's not clear whether other interrupt source could
   3072  1.26  riastrad 	 * be affected or if this is somehow limited to vblank
   3073  1.26  riastrad 	 * interrupts only. To play it safe we disable LP3
   3074  1.26  riastrad 	 * watermarks entirely.
   3075  1.26  riastrad 	 */
   3076  1.26  riastrad 	if (dev_priv->wm.pri_latency[3] == 0 &&
   3077  1.26  riastrad 	    dev_priv->wm.spr_latency[3] == 0 &&
   3078  1.26  riastrad 	    dev_priv->wm.cur_latency[3] == 0)
   3079  1.26  riastrad 		return;
   3080   1.1  riastrad 
   3081  1.26  riastrad 	dev_priv->wm.pri_latency[3] = 0;
   3082  1.26  riastrad 	dev_priv->wm.spr_latency[3] = 0;
   3083  1.26  riastrad 	dev_priv->wm.cur_latency[3] = 0;
   3084   1.1  riastrad 
   3085  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   3086  1.26  riastrad 		    "LP3 watermarks disabled due to potential for lost interrupts\n");
   3087  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
   3088  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
   3089  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
   3090  1.10  riastrad }
   3091   1.1  riastrad 
   3092  1.26  riastrad static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
   3093  1.10  riastrad {
   3094  1.26  riastrad 	intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
   3095   1.1  riastrad 
   3096  1.26  riastrad 	memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
   3097  1.26  riastrad 	       sizeof(dev_priv->wm.pri_latency));
   3098  1.26  riastrad 	memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
   3099  1.26  riastrad 	       sizeof(dev_priv->wm.pri_latency));
   3100   1.1  riastrad 
   3101  1.26  riastrad 	intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
   3102  1.26  riastrad 	intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
   3103   1.1  riastrad 
   3104  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
   3105  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
   3106  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
   3107   1.1  riastrad 
   3108  1.26  riastrad 	if (IS_GEN(dev_priv, 6)) {
   3109  1.26  riastrad 		snb_wm_latency_quirk(dev_priv);
   3110  1.26  riastrad 		snb_wm_lp3_irq_quirk(dev_priv);
   3111  1.10  riastrad 	}
   3112  1.26  riastrad }
   3113   1.1  riastrad 
   3114  1.26  riastrad static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
   3115  1.26  riastrad {
   3116  1.26  riastrad 	intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
   3117  1.26  riastrad 	intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
   3118  1.10  riastrad }
   3119   1.1  riastrad 
   3120  1.26  riastrad static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
   3121  1.26  riastrad 				 struct intel_pipe_wm *pipe_wm)
   3122  1.10  riastrad {
   3123  1.26  riastrad 	/* LP0 watermark maximums depend on this pipe alone */
   3124  1.26  riastrad 	const struct intel_wm_config config = {
   3125  1.26  riastrad 		.num_pipes_active = 1,
   3126  1.26  riastrad 		.sprites_enabled = pipe_wm->sprites_enabled,
   3127  1.26  riastrad 		.sprites_scaled = pipe_wm->sprites_scaled,
   3128  1.26  riastrad 	};
   3129  1.26  riastrad 	struct ilk_wm_maximums max;
   3130   1.1  riastrad 
   3131  1.26  riastrad 	/* LP0 watermarks always use 1/2 DDB partitioning */
   3132  1.26  riastrad 	ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
   3133   1.1  riastrad 
   3134  1.26  riastrad 	/* At least LP0 must be valid */
   3135  1.26  riastrad 	if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
   3136  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
   3137  1.26  riastrad 		return false;
   3138  1.26  riastrad 	}
   3139   1.1  riastrad 
   3140  1.26  riastrad 	return true;
   3141  1.26  riastrad }
   3142   1.1  riastrad 
   3143  1.26  riastrad /* Compute new watermarks for the pipe */
   3144  1.26  riastrad static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
   3145  1.26  riastrad {
   3146  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   3147  1.26  riastrad 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->uapi.crtc);
   3148  1.26  riastrad 	struct intel_pipe_wm *pipe_wm;
   3149  1.26  riastrad 	struct intel_plane *plane;
   3150  1.26  riastrad 	const struct intel_plane_state *plane_state;
   3151  1.26  riastrad 	const struct intel_plane_state *pristate = NULL;
   3152  1.26  riastrad 	const struct intel_plane_state *sprstate = NULL;
   3153  1.26  riastrad 	const struct intel_plane_state *curstate = NULL;
   3154  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
   3155  1.26  riastrad 	struct ilk_wm_maximums max;
   3156   1.1  riastrad 
   3157  1.26  riastrad 	pipe_wm = &crtc_state->wm.ilk.optimal;
   3158   1.1  riastrad 
   3159  1.26  riastrad 	intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
   3160  1.26  riastrad 		if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
   3161  1.26  riastrad 			pristate = plane_state;
   3162  1.26  riastrad 		else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
   3163  1.26  riastrad 			sprstate = plane_state;
   3164  1.26  riastrad 		else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
   3165  1.26  riastrad 			curstate = plane_state;
   3166  1.26  riastrad 	}
   3167  1.26  riastrad 
   3168  1.26  riastrad 	pipe_wm->pipe_enabled = crtc_state->hw.active;
   3169  1.26  riastrad 	if (sprstate) {
   3170  1.26  riastrad 		pipe_wm->sprites_enabled = sprstate->uapi.visible;
   3171  1.26  riastrad 		pipe_wm->sprites_scaled = sprstate->uapi.visible &&
   3172  1.26  riastrad 			(drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
   3173  1.26  riastrad 			 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
   3174  1.10  riastrad 	}
   3175   1.1  riastrad 
   3176  1.26  riastrad 	usable_level = max_level;
   3177   1.1  riastrad 
   3178  1.26  riastrad 	/* ILK/SNB: LP2+ watermarks only w/o sprites */
   3179  1.26  riastrad 	if (INTEL_GEN(dev_priv) <= 6 && pipe_wm->sprites_enabled)
   3180  1.26  riastrad 		usable_level = 1;
   3181   1.1  riastrad 
   3182  1.26  riastrad 	/* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
   3183  1.26  riastrad 	if (pipe_wm->sprites_scaled)
   3184  1.26  riastrad 		usable_level = 0;
   3185   1.1  riastrad 
   3186  1.26  riastrad 	memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
   3187  1.26  riastrad 	ilk_compute_wm_level(dev_priv, intel_crtc, 0, crtc_state,
   3188  1.26  riastrad 			     pristate, sprstate, curstate, &pipe_wm->wm[0]);
   3189   1.1  riastrad 
   3190  1.26  riastrad 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   3191  1.26  riastrad 		pipe_wm->linetime = hsw_compute_linetime_wm(crtc_state);
   3192   1.1  riastrad 
   3193  1.26  riastrad 	if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
   3194  1.26  riastrad 		return -EINVAL;
   3195   1.1  riastrad 
   3196  1.26  riastrad 	ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
   3197   1.1  riastrad 
   3198  1.26  riastrad 	for (level = 1; level <= usable_level; level++) {
   3199  1.26  riastrad 		struct intel_wm_level *wm = &pipe_wm->wm[level];
   3200   1.1  riastrad 
   3201  1.26  riastrad 		ilk_compute_wm_level(dev_priv, intel_crtc, level, crtc_state,
   3202  1.26  riastrad 				     pristate, sprstate, curstate, wm);
   3203   1.1  riastrad 
   3204  1.26  riastrad 		/*
   3205  1.26  riastrad 		 * Disable any watermark level that exceeds the
   3206  1.26  riastrad 		 * register maximums since such watermarks are
   3207  1.26  riastrad 		 * always invalid.
   3208  1.26  riastrad 		 */
   3209  1.26  riastrad 		if (!ilk_validate_wm_level(level, &max, wm)) {
   3210  1.26  riastrad 			memset(wm, 0, sizeof(*wm));
   3211  1.26  riastrad 			break;
   3212  1.10  riastrad 		}
   3213  1.26  riastrad 	}
   3214   1.1  riastrad 
   3215  1.26  riastrad 	return 0;
   3216   1.1  riastrad }
   3217   1.1  riastrad 
   3218  1.10  riastrad /*
   3219  1.26  riastrad  * Build a set of 'intermediate' watermark values that satisfy both the old
   3220  1.26  riastrad  * state and the new state.  These can be programmed to the hardware
   3221  1.26  riastrad  * immediately.
   3222   1.1  riastrad  */
   3223  1.26  riastrad static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
   3224  1.26  riastrad {
   3225  1.26  riastrad 	struct intel_crtc *intel_crtc = to_intel_crtc(newstate->uapi.crtc);
   3226  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
   3227  1.26  riastrad 	struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
   3228  1.26  riastrad 	struct intel_atomic_state *intel_state =
   3229  1.26  riastrad 		to_intel_atomic_state(newstate->uapi.state);
   3230  1.26  riastrad 	const struct intel_crtc_state *oldstate =
   3231  1.26  riastrad 		intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
   3232  1.26  riastrad 	const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
   3233  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   3234  1.26  riastrad 
   3235  1.26  riastrad 	/*
   3236  1.26  riastrad 	 * Start with the final, target watermarks, then combine with the
   3237  1.26  riastrad 	 * currently active watermarks to get values that are safe both before
   3238  1.26  riastrad 	 * and after the vblank.
   3239  1.26  riastrad 	 */
   3240  1.26  riastrad 	*a = newstate->wm.ilk.optimal;
   3241  1.26  riastrad 	if (!newstate->hw.active || drm_atomic_crtc_needs_modeset(&newstate->uapi) ||
   3242  1.26  riastrad 	    intel_state->skip_intermediate_wm)
   3243  1.26  riastrad 		return 0;
   3244  1.10  riastrad 
   3245  1.26  riastrad 	a->pipe_enabled |= b->pipe_enabled;
   3246  1.26  riastrad 	a->sprites_enabled |= b->sprites_enabled;
   3247  1.26  riastrad 	a->sprites_scaled |= b->sprites_scaled;
   3248   1.1  riastrad 
   3249  1.26  riastrad 	for (level = 0; level <= max_level; level++) {
   3250  1.26  riastrad 		struct intel_wm_level *a_wm = &a->wm[level];
   3251  1.26  riastrad 		const struct intel_wm_level *b_wm = &b->wm[level];
   3252   1.3  riastrad 
   3253  1.26  riastrad 		a_wm->enable &= b_wm->enable;
   3254  1.26  riastrad 		a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
   3255  1.26  riastrad 		a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
   3256  1.26  riastrad 		a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
   3257  1.26  riastrad 		a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
   3258  1.10  riastrad 	}
   3259   1.3  riastrad 
   3260  1.26  riastrad 	/*
   3261  1.26  riastrad 	 * We need to make sure that these merged watermark values are
   3262  1.26  riastrad 	 * actually a valid configuration themselves.  If they're not,
   3263  1.26  riastrad 	 * there's no safe way to transition from the old state to
   3264  1.26  riastrad 	 * the new state, so we need to fail the atomic transaction.
   3265  1.26  riastrad 	 */
   3266  1.26  riastrad 	if (!ilk_validate_pipe_wm(dev_priv, a))
   3267  1.26  riastrad 		return -EINVAL;
   3268  1.10  riastrad 
   3269  1.26  riastrad 	/*
   3270  1.26  riastrad 	 * If our intermediate WM are identical to the final WM, then we can
   3271  1.26  riastrad 	 * omit the post-vblank programming; only update if it's different.
   3272  1.26  riastrad 	 */
   3273  1.26  riastrad 	if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
   3274  1.26  riastrad 		newstate->wm.need_postvbl_update = true;
   3275  1.10  riastrad 
   3276  1.26  riastrad 	return 0;
   3277  1.10  riastrad }
   3278  1.10  riastrad 
   3279  1.26  riastrad /*
   3280  1.26  riastrad  * Merge the watermarks from all active pipes for a specific level.
   3281  1.26  riastrad  */
   3282  1.26  riastrad static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
   3283  1.26  riastrad 			       int level,
   3284  1.26  riastrad 			       struct intel_wm_level *ret_wm)
   3285  1.10  riastrad {
   3286  1.26  riastrad 	const struct intel_crtc *intel_crtc;
   3287  1.26  riastrad 
   3288  1.26  riastrad 	ret_wm->enable = true;
   3289  1.10  riastrad 
   3290  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
   3291  1.26  riastrad 		const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
   3292  1.26  riastrad 		const struct intel_wm_level *wm = &active->wm[level];
   3293  1.10  riastrad 
   3294  1.26  riastrad 		if (!active->pipe_enabled)
   3295  1.10  riastrad 			continue;
   3296  1.10  riastrad 
   3297  1.26  riastrad 		/*
   3298  1.26  riastrad 		 * The watermark values may have been used in the past,
   3299  1.26  riastrad 		 * so we must maintain them in the registers for some
   3300  1.26  riastrad 		 * time even if the level is now disabled.
   3301  1.26  riastrad 		 */
   3302  1.26  riastrad 		if (!wm->enable)
   3303  1.26  riastrad 			ret_wm->enable = false;
   3304   1.3  riastrad 
   3305  1.26  riastrad 		ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
   3306  1.26  riastrad 		ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
   3307  1.26  riastrad 		ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
   3308  1.26  riastrad 		ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
   3309  1.26  riastrad 	}
   3310  1.26  riastrad }
   3311   1.3  riastrad 
   3312  1.26  riastrad /*
   3313  1.26  riastrad  * Merge all low power watermarks for all active pipes.
   3314  1.26  riastrad  */
   3315  1.26  riastrad static void ilk_wm_merge(struct drm_i915_private *dev_priv,
   3316  1.26  riastrad 			 const struct intel_wm_config *config,
   3317  1.26  riastrad 			 const struct ilk_wm_maximums *max,
   3318  1.26  riastrad 			 struct intel_pipe_wm *merged)
   3319  1.26  riastrad {
   3320  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   3321  1.26  riastrad 	int last_enabled_level = max_level;
   3322   1.3  riastrad 
   3323  1.26  riastrad 	/* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
   3324  1.26  riastrad 	if ((INTEL_GEN(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
   3325  1.26  riastrad 	    config->num_pipes_active > 1)
   3326  1.26  riastrad 		last_enabled_level = 0;
   3327   1.3  riastrad 
   3328  1.26  riastrad 	/* ILK: FBC WM must be disabled always */
   3329  1.26  riastrad 	merged->fbc_wm_enabled = INTEL_GEN(dev_priv) >= 6;
   3330   1.3  riastrad 
   3331  1.26  riastrad 	/* merge each WM1+ level */
   3332  1.26  riastrad 	for (level = 1; level <= max_level; level++) {
   3333  1.26  riastrad 		struct intel_wm_level *wm = &merged->wm[level];
   3334   1.3  riastrad 
   3335  1.26  riastrad 		ilk_merge_wm_level(dev_priv, level, wm);
   3336   1.3  riastrad 
   3337  1.26  riastrad 		if (level > last_enabled_level)
   3338  1.26  riastrad 			wm->enable = false;
   3339  1.26  riastrad 		else if (!ilk_validate_wm_level(level, max, wm))
   3340  1.26  riastrad 			/* make sure all following levels get disabled */
   3341  1.26  riastrad 			last_enabled_level = level - 1;
   3342   1.3  riastrad 
   3343  1.26  riastrad 		/*
   3344  1.26  riastrad 		 * The spec says it is preferred to disable
   3345  1.26  riastrad 		 * FBC WMs instead of disabling a WM level.
   3346  1.26  riastrad 		 */
   3347  1.26  riastrad 		if (wm->fbc_val > max->fbc) {
   3348  1.26  riastrad 			if (wm->enable)
   3349  1.26  riastrad 				merged->fbc_wm_enabled = false;
   3350  1.26  riastrad 			wm->fbc_val = 0;
   3351  1.10  riastrad 		}
   3352  1.10  riastrad 	}
   3353   1.3  riastrad 
   3354  1.26  riastrad 	/* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
   3355  1.10  riastrad 	/*
   3356  1.26  riastrad 	 * FIXME this is racy. FBC might get enabled later.
   3357  1.26  riastrad 	 * What we should check here is whether FBC can be
   3358  1.26  riastrad 	 * enabled sometime later.
   3359  1.10  riastrad 	 */
   3360  1.26  riastrad 	if (IS_GEN(dev_priv, 5) && !merged->fbc_wm_enabled &&
   3361  1.26  riastrad 	    intel_fbc_is_active(dev_priv)) {
   3362  1.26  riastrad 		for (level = 2; level <= max_level; level++) {
   3363  1.26  riastrad 			struct intel_wm_level *wm = &merged->wm[level];
   3364   1.3  riastrad 
   3365  1.26  riastrad 			wm->enable = false;
   3366  1.26  riastrad 		}
   3367   1.1  riastrad 	}
   3368   1.3  riastrad }
   3369   1.3  riastrad 
   3370  1.26  riastrad static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
   3371   1.3  riastrad {
   3372  1.26  riastrad 	/* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
   3373  1.26  riastrad 	return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
   3374  1.26  riastrad }
   3375  1.10  riastrad 
   3376  1.26  riastrad /* The value we need to program into the WM_LPx latency field */
   3377  1.26  riastrad static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
   3378  1.26  riastrad 				      int level)
   3379  1.26  riastrad {
   3380  1.26  riastrad 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   3381  1.26  riastrad 		return 2 * level;
   3382  1.26  riastrad 	else
   3383  1.26  riastrad 		return dev_priv->wm.pri_latency[level];
   3384   1.1  riastrad }
   3385   1.1  riastrad 
   3386  1.26  riastrad static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
   3387  1.26  riastrad 				   const struct intel_pipe_wm *merged,
   3388  1.26  riastrad 				   enum intel_ddb_partitioning partitioning,
   3389  1.26  riastrad 				   struct ilk_wm_values *results)
   3390   1.1  riastrad {
   3391  1.10  riastrad 	struct intel_crtc *intel_crtc;
   3392  1.26  riastrad 	int level, wm_lp;
   3393  1.26  riastrad 
   3394  1.26  riastrad 	results->enable_fbc_wm = merged->fbc_wm_enabled;
   3395  1.26  riastrad 	results->partitioning = partitioning;
   3396  1.26  riastrad 
   3397  1.26  riastrad 	/* LP1+ register values */
   3398  1.26  riastrad 	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
   3399  1.26  riastrad 		const struct intel_wm_level *r;
   3400   1.1  riastrad 
   3401  1.26  riastrad 		level = ilk_wm_lp_to_level(wm_lp, merged);
   3402   1.1  riastrad 
   3403  1.26  riastrad 		r = &merged->wm[level];
   3404  1.10  riastrad 
   3405  1.26  riastrad 		/*
   3406  1.26  riastrad 		 * Maintain the watermark values even if the level is
   3407  1.26  riastrad 		 * disabled. Doing otherwise could cause underruns.
   3408  1.26  riastrad 		 */
   3409  1.26  riastrad 		results->wm_lp[wm_lp - 1] =
   3410  1.26  riastrad 			(ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
   3411  1.26  riastrad 			(r->pri_val << WM1_LP_SR_SHIFT) |
   3412  1.26  riastrad 			r->cur_val;
   3413  1.10  riastrad 
   3414  1.26  riastrad 		if (r->enable)
   3415  1.26  riastrad 			results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
   3416  1.10  riastrad 
   3417  1.26  riastrad 		if (INTEL_GEN(dev_priv) >= 8)
   3418  1.26  riastrad 			results->wm_lp[wm_lp - 1] |=
   3419  1.26  riastrad 				r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
   3420  1.26  riastrad 		else
   3421  1.26  riastrad 			results->wm_lp[wm_lp - 1] |=
   3422  1.26  riastrad 				r->fbc_val << WM1_LP_FBC_SHIFT;
   3423  1.10  riastrad 
   3424  1.10  riastrad 		/*
   3425  1.26  riastrad 		 * Always set WM1S_LP_EN when spr_val != 0, even if the
   3426  1.26  riastrad 		 * level is disabled. Doing otherwise could cause underruns.
   3427  1.10  riastrad 		 */
   3428  1.26  riastrad 		if (INTEL_GEN(dev_priv) <= 6 && r->spr_val) {
   3429  1.26  riastrad 			WARN_ON(wm_lp != 1);
   3430  1.26  riastrad 			results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
   3431  1.26  riastrad 		} else
   3432  1.26  riastrad 			results->wm_lp_spr[wm_lp - 1] = r->spr_val;
   3433  1.26  riastrad 	}
   3434  1.26  riastrad 
   3435  1.26  riastrad 	/* LP0 register values */
   3436  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
   3437  1.26  riastrad 		enum pipe pipe = intel_crtc->pipe;
   3438  1.26  riastrad 		const struct intel_wm_level *r =
   3439  1.26  riastrad 			&intel_crtc->wm.active.ilk.wm[0];
   3440  1.26  riastrad 
   3441  1.26  riastrad 		if (WARN_ON(!r->enable))
   3442  1.26  riastrad 			continue;
   3443   1.1  riastrad 
   3444  1.26  riastrad 		results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
   3445  1.26  riastrad 
   3446  1.26  riastrad 		results->wm_pipe[pipe] =
   3447  1.26  riastrad 			(r->pri_val << WM0_PIPE_PLANE_SHIFT) |
   3448  1.26  riastrad 			(r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
   3449  1.26  riastrad 			r->cur_val;
   3450   1.3  riastrad 	}
   3451  1.10  riastrad }
   3452   1.1  riastrad 
   3453  1.26  riastrad /* Find the result with the highest level enabled. Check for enable_fbc_wm in
   3454  1.26  riastrad  * case both are at the same level. Prefer r1 in case they're the same. */
   3455  1.26  riastrad static struct intel_pipe_wm *
   3456  1.26  riastrad ilk_find_best_result(struct drm_i915_private *dev_priv,
   3457  1.26  riastrad 		     struct intel_pipe_wm *r1,
   3458  1.26  riastrad 		     struct intel_pipe_wm *r2)
   3459  1.10  riastrad {
   3460  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   3461  1.26  riastrad 	int level1 = 0, level2 = 0;
   3462   1.1  riastrad 
   3463  1.26  riastrad 	for (level = 1; level <= max_level; level++) {
   3464  1.26  riastrad 		if (r1->wm[level].enable)
   3465  1.26  riastrad 			level1 = level;
   3466  1.26  riastrad 		if (r2->wm[level].enable)
   3467  1.26  riastrad 			level2 = level;
   3468  1.26  riastrad 	}
   3469   1.1  riastrad 
   3470  1.26  riastrad 	if (level1 == level2) {
   3471  1.26  riastrad 		if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
   3472  1.26  riastrad 			return r2;
   3473  1.26  riastrad 		else
   3474  1.26  riastrad 			return r1;
   3475  1.26  riastrad 	} else if (level1 > level2) {
   3476  1.26  riastrad 		return r1;
   3477  1.26  riastrad 	} else {
   3478  1.26  riastrad 		return r2;
   3479  1.26  riastrad 	}
   3480   1.3  riastrad }
   3481   1.3  riastrad 
   3482  1.26  riastrad /* dirty bits used to track which watermarks need changes */
   3483  1.26  riastrad #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
   3484  1.26  riastrad #define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
   3485  1.26  riastrad #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
   3486  1.26  riastrad #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
   3487  1.26  riastrad #define WM_DIRTY_FBC (1 << 24)
   3488  1.26  riastrad #define WM_DIRTY_DDB (1 << 25)
   3489  1.26  riastrad 
   3490  1.26  riastrad static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
   3491  1.26  riastrad 					 const struct ilk_wm_values *old,
   3492  1.26  riastrad 					 const struct ilk_wm_values *new)
   3493   1.3  riastrad {
   3494  1.26  riastrad 	unsigned int dirty = 0;
   3495  1.26  riastrad 	enum pipe pipe;
   3496  1.26  riastrad 	int wm_lp;
   3497  1.10  riastrad 
   3498  1.26  riastrad 	for_each_pipe(dev_priv, pipe) {
   3499  1.26  riastrad 		if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
   3500  1.26  riastrad 			dirty |= WM_DIRTY_LINETIME(pipe);
   3501  1.26  riastrad 			/* Must disable LP1+ watermarks too */
   3502  1.26  riastrad 			dirty |= WM_DIRTY_LP_ALL;
   3503  1.26  riastrad 		}
   3504  1.10  riastrad 
   3505  1.26  riastrad 		if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
   3506  1.26  riastrad 			dirty |= WM_DIRTY_PIPE(pipe);
   3507  1.26  riastrad 			/* Must disable LP1+ watermarks too */
   3508  1.26  riastrad 			dirty |= WM_DIRTY_LP_ALL;
   3509  1.26  riastrad 		}
   3510  1.26  riastrad 	}
   3511   1.3  riastrad 
   3512  1.26  riastrad 	if (old->enable_fbc_wm != new->enable_fbc_wm) {
   3513  1.26  riastrad 		dirty |= WM_DIRTY_FBC;
   3514  1.26  riastrad 		/* Must disable LP1+ watermarks too */
   3515  1.26  riastrad 		dirty |= WM_DIRTY_LP_ALL;
   3516  1.26  riastrad 	}
   3517   1.3  riastrad 
   3518  1.26  riastrad 	if (old->partitioning != new->partitioning) {
   3519  1.26  riastrad 		dirty |= WM_DIRTY_DDB;
   3520  1.26  riastrad 		/* Must disable LP1+ watermarks too */
   3521  1.26  riastrad 		dirty |= WM_DIRTY_LP_ALL;
   3522  1.26  riastrad 	}
   3523   1.3  riastrad 
   3524  1.26  riastrad 	/* LP1+ watermarks already deemed dirty, no need to continue */
   3525  1.26  riastrad 	if (dirty & WM_DIRTY_LP_ALL)
   3526  1.26  riastrad 		return dirty;
   3527   1.3  riastrad 
   3528  1.26  riastrad 	/* Find the lowest numbered LP1+ watermark in need of an update... */
   3529  1.26  riastrad 	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
   3530  1.26  riastrad 		if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
   3531  1.26  riastrad 		    old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
   3532  1.26  riastrad 			break;
   3533  1.26  riastrad 	}
   3534   1.3  riastrad 
   3535  1.26  riastrad 	/* ...and mark it and all higher numbered LP1+ watermarks as dirty */
   3536  1.26  riastrad 	for (; wm_lp <= 3; wm_lp++)
   3537  1.26  riastrad 		dirty |= WM_DIRTY_LP(wm_lp);
   3538   1.3  riastrad 
   3539  1.26  riastrad 	return dirty;
   3540   1.3  riastrad }
   3541   1.3  riastrad 
   3542  1.26  riastrad static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
   3543  1.26  riastrad 			       unsigned int dirty)
   3544   1.3  riastrad {
   3545  1.26  riastrad 	struct ilk_wm_values *previous = &dev_priv->wm.hw;
   3546  1.26  riastrad 	bool changed = false;
   3547   1.3  riastrad 
   3548  1.26  riastrad 	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
   3549  1.26  riastrad 		previous->wm_lp[2] &= ~WM1_LP_SR_EN;
   3550  1.26  riastrad 		I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
   3551  1.26  riastrad 		changed = true;
   3552  1.26  riastrad 	}
   3553  1.26  riastrad 	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
   3554  1.26  riastrad 		previous->wm_lp[1] &= ~WM1_LP_SR_EN;
   3555  1.26  riastrad 		I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
   3556  1.26  riastrad 		changed = true;
   3557  1.26  riastrad 	}
   3558  1.26  riastrad 	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
   3559  1.26  riastrad 		previous->wm_lp[0] &= ~WM1_LP_SR_EN;
   3560  1.26  riastrad 		I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
   3561  1.26  riastrad 		changed = true;
   3562  1.26  riastrad 	}
   3563  1.10  riastrad 
   3564  1.10  riastrad 	/*
   3565  1.26  riastrad 	 * Don't touch WM1S_LP_EN here.
   3566  1.26  riastrad 	 * Doing so could cause underruns.
   3567  1.10  riastrad 	 */
   3568   1.3  riastrad 
   3569  1.26  riastrad 	return changed;
   3570   1.3  riastrad }
   3571   1.3  riastrad 
   3572  1.26  riastrad /*
   3573  1.26  riastrad  * The spec says we shouldn't write when we don't need, because every write
   3574  1.26  riastrad  * causes WMs to be re-evaluated, expending some power.
   3575  1.26  riastrad  */
   3576  1.26  riastrad static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
   3577  1.26  riastrad 				struct ilk_wm_values *results)
   3578   1.3  riastrad {
   3579  1.26  riastrad 	struct ilk_wm_values *previous = &dev_priv->wm.hw;
   3580  1.26  riastrad 	unsigned int dirty;
   3581  1.26  riastrad 	u32 val;
   3582  1.10  riastrad 
   3583  1.26  riastrad 	dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
   3584  1.26  riastrad 	if (!dirty)
   3585  1.26  riastrad 		return;
   3586  1.10  riastrad 
   3587  1.26  riastrad 	_ilk_disable_lp_wm(dev_priv, dirty);
   3588   1.3  riastrad 
   3589  1.26  riastrad 	if (dirty & WM_DIRTY_PIPE(PIPE_A))
   3590  1.26  riastrad 		I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
   3591  1.26  riastrad 	if (dirty & WM_DIRTY_PIPE(PIPE_B))
   3592  1.26  riastrad 		I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
   3593  1.26  riastrad 	if (dirty & WM_DIRTY_PIPE(PIPE_C))
   3594  1.26  riastrad 		I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
   3595   1.3  riastrad 
   3596  1.26  riastrad 	if (dirty & WM_DIRTY_LINETIME(PIPE_A))
   3597  1.26  riastrad 		I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
   3598  1.26  riastrad 	if (dirty & WM_DIRTY_LINETIME(PIPE_B))
   3599  1.26  riastrad 		I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
   3600  1.26  riastrad 	if (dirty & WM_DIRTY_LINETIME(PIPE_C))
   3601  1.26  riastrad 		I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
   3602   1.3  riastrad 
   3603  1.26  riastrad 	if (dirty & WM_DIRTY_DDB) {
   3604  1.26  riastrad 		if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
   3605  1.26  riastrad 			val = I915_READ(WM_MISC);
   3606  1.26  riastrad 			if (results->partitioning == INTEL_DDB_PART_1_2)
   3607  1.26  riastrad 				val &= ~WM_MISC_DATA_PARTITION_5_6;
   3608  1.26  riastrad 			else
   3609  1.26  riastrad 				val |= WM_MISC_DATA_PARTITION_5_6;
   3610  1.26  riastrad 			I915_WRITE(WM_MISC, val);
   3611  1.26  riastrad 		} else {
   3612  1.26  riastrad 			val = I915_READ(DISP_ARB_CTL2);
   3613  1.26  riastrad 			if (results->partitioning == INTEL_DDB_PART_1_2)
   3614  1.26  riastrad 				val &= ~DISP_DATA_PARTITION_5_6;
   3615  1.26  riastrad 			else
   3616  1.26  riastrad 				val |= DISP_DATA_PARTITION_5_6;
   3617  1.26  riastrad 			I915_WRITE(DISP_ARB_CTL2, val);
   3618  1.26  riastrad 		}
   3619  1.26  riastrad 	}
   3620   1.1  riastrad 
   3621  1.26  riastrad 	if (dirty & WM_DIRTY_FBC) {
   3622  1.26  riastrad 		val = I915_READ(DISP_ARB_CTL);
   3623  1.26  riastrad 		if (results->enable_fbc_wm)
   3624  1.26  riastrad 			val &= ~DISP_FBC_WM_DIS;
   3625  1.26  riastrad 		else
   3626  1.26  riastrad 			val |= DISP_FBC_WM_DIS;
   3627  1.26  riastrad 		I915_WRITE(DISP_ARB_CTL, val);
   3628  1.26  riastrad 	}
   3629   1.3  riastrad 
   3630  1.26  riastrad 	if (dirty & WM_DIRTY_LP(1) &&
   3631  1.26  riastrad 	    previous->wm_lp_spr[0] != results->wm_lp_spr[0])
   3632  1.26  riastrad 		I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
   3633   1.1  riastrad 
   3634  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 7) {
   3635  1.26  riastrad 		if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
   3636  1.26  riastrad 			I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
   3637  1.26  riastrad 		if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
   3638  1.26  riastrad 			I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
   3639  1.10  riastrad 	}
   3640   1.1  riastrad 
   3641  1.26  riastrad 	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
   3642  1.26  riastrad 		I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
   3643  1.26  riastrad 	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
   3644  1.26  riastrad 		I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
   3645  1.26  riastrad 	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
   3646  1.26  riastrad 		I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
   3647   1.1  riastrad 
   3648  1.26  riastrad 	dev_priv->wm.hw = *results;
   3649  1.26  riastrad }
   3650   1.3  riastrad 
   3651  1.26  riastrad bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
   3652  1.26  riastrad {
   3653  1.26  riastrad 	return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
   3654   1.3  riastrad }
   3655   1.3  riastrad 
   3656  1.26  riastrad static u8 intel_enabled_dbuf_slices_num(struct drm_i915_private *dev_priv)
   3657  1.26  riastrad {
   3658  1.26  riastrad 	u8 enabled_slices;
   3659  1.26  riastrad 
   3660  1.26  riastrad 	/* Slice 1 will always be enabled */
   3661  1.26  riastrad 	enabled_slices = 1;
   3662  1.26  riastrad 
   3663  1.26  riastrad 	/* Gen prior to GEN11 have only one DBuf slice */
   3664  1.26  riastrad 	if (INTEL_GEN(dev_priv) < 11)
   3665  1.26  riastrad 		return enabled_slices;
   3666  1.10  riastrad 
   3667  1.10  riastrad 	/*
   3668  1.26  riastrad 	 * FIXME: for now we'll only ever use 1 slice; pretend that we have
   3669  1.26  riastrad 	 * only that 1 slice enabled until we have a proper way for on-demand
   3670  1.26  riastrad 	 * toggling of the second slice.
   3671  1.10  riastrad 	 */
   3672  1.26  riastrad 	if (0 && I915_READ(DBUF_CTL_S2) & DBUF_POWER_STATE)
   3673  1.26  riastrad 		enabled_slices++;
   3674  1.26  riastrad 
   3675  1.26  riastrad 	return enabled_slices;
   3676  1.26  riastrad }
   3677  1.26  riastrad 
   3678  1.26  riastrad /*
   3679  1.26  riastrad  * FIXME: We still don't have the proper code detect if we need to apply the WA,
   3680  1.26  riastrad  * so assume we'll always need it in order to avoid underruns.
   3681  1.26  riastrad  */
   3682  1.26  riastrad static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
   3683  1.26  riastrad {
   3684  1.26  riastrad 	return IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv);
   3685  1.26  riastrad }
   3686  1.26  riastrad 
   3687  1.26  riastrad static bool
   3688  1.26  riastrad intel_has_sagv(struct drm_i915_private *dev_priv)
   3689  1.26  riastrad {
   3690  1.26  riastrad 	/* HACK! */
   3691  1.26  riastrad 	if (IS_GEN(dev_priv, 12))
   3692  1.26  riastrad 		return false;
   3693   1.3  riastrad 
   3694  1.26  riastrad 	return (IS_GEN9_BC(dev_priv) || INTEL_GEN(dev_priv) >= 10) &&
   3695  1.26  riastrad 		dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
   3696  1.10  riastrad }
   3697   1.3  riastrad 
   3698  1.26  riastrad static void
   3699  1.26  riastrad skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
   3700  1.10  riastrad {
   3701  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 12) {
   3702  1.26  riastrad 		u32 val = 0;
   3703  1.26  riastrad 		int ret;
   3704  1.10  riastrad 
   3705  1.26  riastrad 		ret = sandybridge_pcode_read(dev_priv,
   3706  1.26  riastrad 					     GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
   3707  1.26  riastrad 					     &val, NULL);
   3708  1.26  riastrad 		if (!ret) {
   3709  1.26  riastrad 			dev_priv->sagv_block_time_us = val;
   3710  1.26  riastrad 			return;
   3711  1.10  riastrad 		}
   3712  1.26  riastrad 
   3713  1.26  riastrad 		drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
   3714  1.26  riastrad 	} else if (IS_GEN(dev_priv, 11)) {
   3715  1.26  riastrad 		dev_priv->sagv_block_time_us = 10;
   3716  1.26  riastrad 		return;
   3717  1.26  riastrad 	} else if (IS_GEN(dev_priv, 10)) {
   3718  1.26  riastrad 		dev_priv->sagv_block_time_us = 20;
   3719  1.26  riastrad 		return;
   3720  1.26  riastrad 	} else if (IS_GEN(dev_priv, 9)) {
   3721  1.26  riastrad 		dev_priv->sagv_block_time_us = 30;
   3722  1.26  riastrad 		return;
   3723  1.10  riastrad 	} else {
   3724  1.26  riastrad 		MISSING_CASE(INTEL_GEN(dev_priv));
   3725  1.10  riastrad 	}
   3726  1.26  riastrad 
   3727  1.26  riastrad 	/* Default to an unusable block time */
   3728  1.26  riastrad 	dev_priv->sagv_block_time_us = -1;
   3729   1.3  riastrad }
   3730   1.1  riastrad 
   3731  1.26  riastrad /*
   3732  1.26  riastrad  * SAGV dynamically adjusts the system agent voltage and clock frequencies
   3733  1.26  riastrad  * depending on power and performance requirements. The display engine access
   3734  1.26  riastrad  * to system memory is blocked during the adjustment time. Because of the
   3735  1.26  riastrad  * blocking time, having this enabled can cause full system hangs and/or pipe
   3736  1.26  riastrad  * underruns if we don't meet all of the following requirements:
   3737  1.26  riastrad  *
   3738  1.26  riastrad  *  - <= 1 pipe enabled
   3739  1.26  riastrad  *  - All planes can enable watermarks for latencies >= SAGV engine block time
   3740  1.26  riastrad  *  - We're not using an interlaced display configuration
   3741  1.26  riastrad  */
   3742  1.26  riastrad int
   3743  1.26  riastrad intel_enable_sagv(struct drm_i915_private *dev_priv)
   3744   1.3  riastrad {
   3745  1.26  riastrad 	int ret;
   3746  1.26  riastrad 
   3747  1.26  riastrad 	if (!intel_has_sagv(dev_priv))
   3748  1.26  riastrad 		return 0;
   3749  1.26  riastrad 
   3750  1.26  riastrad 	if (dev_priv->sagv_status == I915_SAGV_ENABLED)
   3751  1.26  riastrad 		return 0;
   3752  1.10  riastrad 
   3753  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
   3754  1.26  riastrad 	ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
   3755  1.26  riastrad 				      GEN9_SAGV_ENABLE);
   3756   1.3  riastrad 
   3757  1.26  riastrad 	/* We don't need to wait for SAGV when enabling */
   3758  1.10  riastrad 
   3759  1.26  riastrad 	/*
   3760  1.26  riastrad 	 * Some skl systems, pre-release machines in particular,
   3761  1.26  riastrad 	 * don't actually have SAGV.
   3762  1.26  riastrad 	 */
   3763  1.26  riastrad 	if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
   3764  1.26  riastrad 		drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
   3765  1.26  riastrad 		dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
   3766  1.26  riastrad 		return 0;
   3767  1.26  riastrad 	} else if (ret < 0) {
   3768  1.26  riastrad 		drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
   3769  1.26  riastrad 		return ret;
   3770  1.10  riastrad 	}
   3771   1.3  riastrad 
   3772  1.26  riastrad 	dev_priv->sagv_status = I915_SAGV_ENABLED;
   3773  1.26  riastrad 	return 0;
   3774  1.26  riastrad }
   3775   1.3  riastrad 
   3776  1.26  riastrad int
   3777  1.26  riastrad intel_disable_sagv(struct drm_i915_private *dev_priv)
   3778  1.26  riastrad {
   3779  1.26  riastrad 	int ret;
   3780   1.1  riastrad 
   3781  1.26  riastrad 	if (!intel_has_sagv(dev_priv))
   3782  1.26  riastrad 		return 0;
   3783   1.3  riastrad 
   3784  1.26  riastrad 	if (dev_priv->sagv_status == I915_SAGV_DISABLED)
   3785  1.26  riastrad 		return 0;
   3786   1.1  riastrad 
   3787  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
   3788  1.26  riastrad 	/* bspec says to keep retrying for at least 1 ms */
   3789  1.26  riastrad 	ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
   3790  1.26  riastrad 				GEN9_SAGV_DISABLE,
   3791  1.26  riastrad 				GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
   3792  1.26  riastrad 				1);
   3793  1.26  riastrad 	/*
   3794  1.26  riastrad 	 * Some skl systems, pre-release machines in particular,
   3795  1.26  riastrad 	 * don't actually have SAGV.
   3796  1.26  riastrad 	 */
   3797  1.26  riastrad 	if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
   3798  1.26  riastrad 		drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
   3799  1.26  riastrad 		dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
   3800  1.26  riastrad 		return 0;
   3801  1.26  riastrad 	} else if (ret < 0) {
   3802  1.26  riastrad 		drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
   3803  1.26  riastrad 		return ret;
   3804  1.10  riastrad 	}
   3805   1.1  riastrad 
   3806  1.26  riastrad 	dev_priv->sagv_status = I915_SAGV_DISABLED;
   3807  1.26  riastrad 	return 0;
   3808   1.3  riastrad }
   3809   1.3  riastrad 
   3810  1.26  riastrad bool intel_can_enable_sagv(struct intel_atomic_state *state)
   3811   1.3  riastrad {
   3812  1.26  riastrad 	struct drm_device *dev = state->base.dev;
   3813  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(dev);
   3814  1.26  riastrad 	struct intel_crtc *crtc;
   3815  1.26  riastrad 	struct intel_plane *plane;
   3816  1.26  riastrad 	struct intel_crtc_state *crtc_state;
   3817  1.26  riastrad 	enum pipe pipe;
   3818  1.26  riastrad 	int level, latency;
   3819  1.26  riastrad 
   3820  1.26  riastrad 	if (!intel_has_sagv(dev_priv))
   3821  1.26  riastrad 		return false;
   3822   1.3  riastrad 
   3823  1.26  riastrad 	/*
   3824  1.26  riastrad 	 * If there are no active CRTCs, no additional checks need be performed
   3825  1.26  riastrad 	 */
   3826  1.26  riastrad 	if (hweight8(state->active_pipes) == 0)
   3827  1.26  riastrad 		return true;
   3828   1.3  riastrad 
   3829  1.26  riastrad 	/*
   3830  1.26  riastrad 	 * SKL+ workaround: bspec recommends we disable SAGV when we have
   3831  1.26  riastrad 	 * more then one pipe enabled
   3832  1.26  riastrad 	 */
   3833  1.26  riastrad 	if (hweight8(state->active_pipes) > 1)
   3834  1.26  riastrad 		return false;
   3835  1.10  riastrad 
   3836  1.26  riastrad 	/* Since we're now guaranteed to only have one active CRTC... */
   3837  1.26  riastrad 	pipe = ffs(state->active_pipes) - 1;
   3838  1.26  riastrad 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
   3839  1.26  riastrad 	crtc_state = to_intel_crtc_state(crtc->base.state);
   3840  1.10  riastrad 
   3841  1.26  riastrad 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
   3842  1.26  riastrad 		return false;
   3843  1.10  riastrad 
   3844  1.26  riastrad 	for_each_intel_plane_on_crtc(dev, crtc, plane) {
   3845  1.26  riastrad 		struct skl_plane_wm *wm =
   3846  1.26  riastrad 			&crtc_state->wm.skl.optimal.planes[plane->id];
   3847  1.10  riastrad 
   3848  1.26  riastrad 		/* Skip this plane if it's not enabled */
   3849  1.26  riastrad 		if (!wm->wm[0].plane_en)
   3850  1.26  riastrad 			continue;
   3851  1.10  riastrad 
   3852  1.26  riastrad 		/* Find the highest enabled wm level for this plane */
   3853  1.26  riastrad 		for (level = ilk_wm_max_level(dev_priv);
   3854  1.26  riastrad 		     !wm->wm[level].plane_en; --level)
   3855  1.26  riastrad 		     { }
   3856  1.26  riastrad 
   3857  1.26  riastrad 		latency = dev_priv->wm.skl_latency[level];
   3858  1.26  riastrad 
   3859  1.26  riastrad 		if (skl_needs_memory_bw_wa(dev_priv) &&
   3860  1.26  riastrad 		    plane->base.state->fb->modifier ==
   3861  1.26  riastrad 		    I915_FORMAT_MOD_X_TILED)
   3862  1.26  riastrad 			latency += 15;
   3863  1.10  riastrad 
   3864  1.10  riastrad 		/*
   3865  1.26  riastrad 		 * If any of the planes on this pipe don't enable wm levels that
   3866  1.26  riastrad 		 * incur memory latencies higher than sagv_block_time_us we
   3867  1.26  riastrad 		 * can't enable SAGV.
   3868  1.10  riastrad 		 */
   3869  1.26  riastrad 		if (latency < dev_priv->sagv_block_time_us)
   3870  1.26  riastrad 			return false;
   3871  1.10  riastrad 	}
   3872  1.26  riastrad 
   3873  1.26  riastrad 	return true;
   3874  1.10  riastrad }
   3875  1.10  riastrad 
   3876  1.26  riastrad static u16 intel_get_ddb_size(struct drm_i915_private *dev_priv,
   3877  1.26  riastrad 			      const struct intel_crtc_state *crtc_state,
   3878  1.26  riastrad 			      const u64 total_data_rate,
   3879  1.26  riastrad 			      const int num_active,
   3880  1.26  riastrad 			      struct skl_ddb_allocation *ddb)
   3881  1.10  riastrad {
   3882  1.26  riastrad 	const struct drm_display_mode *adjusted_mode;
   3883  1.26  riastrad 	u64 total_data_bw;
   3884  1.26  riastrad 	u16 ddb_size = INTEL_INFO(dev_priv)->ddb_size;
   3885   1.3  riastrad 
   3886  1.26  riastrad 	WARN_ON(ddb_size == 0);
   3887   1.3  riastrad 
   3888  1.26  riastrad 	if (INTEL_GEN(dev_priv) < 11)
   3889  1.26  riastrad 		return ddb_size - 4; /* 4 blocks for bypass path allocation */
   3890  1.10  riastrad 
   3891  1.26  riastrad 	adjusted_mode = &crtc_state->hw.adjusted_mode;
   3892  1.26  riastrad 	total_data_bw = total_data_rate * drm_mode_vrefresh(adjusted_mode);
   3893  1.10  riastrad 
   3894  1.26  riastrad 	/*
   3895  1.26  riastrad 	 * 12GB/s is maximum BW supported by single DBuf slice.
   3896  1.26  riastrad 	 *
   3897  1.26  riastrad 	 * FIXME dbuf slice code is broken:
   3898  1.26  riastrad 	 * - must wait for planes to stop using the slice before powering it off
   3899  1.26  riastrad 	 * - plane straddling both slices is illegal in multi-pipe scenarios
   3900  1.26  riastrad 	 * - should validate we stay within the hw bandwidth limits
   3901  1.26  riastrad 	 */
   3902  1.26  riastrad 	if (0 && (num_active > 1 || total_data_bw >= GBps(12))) {
   3903  1.26  riastrad 		ddb->enabled_slices = 2;
   3904  1.26  riastrad 	} else {
   3905  1.26  riastrad 		ddb->enabled_slices = 1;
   3906  1.26  riastrad 		ddb_size /= 2;
   3907  1.26  riastrad 	}
   3908  1.10  riastrad 
   3909  1.26  riastrad 	return ddb_size;
   3910  1.26  riastrad }
   3911  1.10  riastrad 
   3912  1.26  riastrad static void
   3913  1.26  riastrad skl_ddb_get_pipe_allocation_limits(struct drm_i915_private *dev_priv,
   3914  1.26  riastrad 				   const struct intel_crtc_state *crtc_state,
   3915  1.26  riastrad 				   const u64 total_data_rate,
   3916  1.26  riastrad 				   struct skl_ddb_allocation *ddb,
   3917  1.26  riastrad 				   struct skl_ddb_entry *alloc, /* out */
   3918  1.26  riastrad 				   int *num_active /* out */)
   3919  1.26  riastrad {
   3920  1.26  riastrad 	struct drm_atomic_state *state = crtc_state->uapi.state;
   3921  1.26  riastrad 	struct intel_atomic_state *intel_state = to_intel_atomic_state(state);
   3922  1.26  riastrad 	struct drm_crtc *for_crtc = crtc_state->uapi.crtc;
   3923  1.26  riastrad 	const struct intel_crtc *crtc;
   3924  1.26  riastrad 	u32 pipe_width = 0, total_width = 0, width_before_pipe = 0;
   3925  1.26  riastrad 	enum pipe for_pipe = to_intel_crtc(for_crtc)->pipe;
   3926  1.26  riastrad 	u16 ddb_size;
   3927  1.26  riastrad 	u32 i;
   3928  1.10  riastrad 
   3929  1.26  riastrad 	if (WARN_ON(!state) || !crtc_state->hw.active) {
   3930  1.26  riastrad 		alloc->start = 0;
   3931  1.26  riastrad 		alloc->end = 0;
   3932  1.26  riastrad 		*num_active = hweight8(dev_priv->active_pipes);
   3933  1.26  riastrad 		return;
   3934  1.26  riastrad 	}
   3935  1.10  riastrad 
   3936  1.26  riastrad 	if (intel_state->active_pipe_changes)
   3937  1.26  riastrad 		*num_active = hweight8(intel_state->active_pipes);
   3938  1.26  riastrad 	else
   3939  1.26  riastrad 		*num_active = hweight8(dev_priv->active_pipes);
   3940  1.10  riastrad 
   3941  1.26  riastrad 	ddb_size = intel_get_ddb_size(dev_priv, crtc_state, total_data_rate,
   3942  1.26  riastrad 				      *num_active, ddb);
   3943   1.3  riastrad 
   3944  1.26  riastrad 	/*
   3945  1.26  riastrad 	 * If the state doesn't change the active CRTC's or there is no
   3946  1.26  riastrad 	 * modeset request, then there's no need to recalculate;
   3947  1.26  riastrad 	 * the existing pipe allocation limits should remain unchanged.
   3948  1.26  riastrad 	 * Note that we're safe from racing commits since any racing commit
   3949  1.26  riastrad 	 * that changes the active CRTC list or do modeset would need to
   3950  1.26  riastrad 	 * grab _all_ crtc locks, including the one we currently hold.
   3951  1.26  riastrad 	 */
   3952  1.26  riastrad 	if (!intel_state->active_pipe_changes && !intel_state->modeset) {
   3953  1.26  riastrad 		/*
   3954  1.26  riastrad 		 * alloc may be cleared by clear_intel_crtc_state,
   3955  1.26  riastrad 		 * copy from old state to be sure
   3956  1.26  riastrad 		 */
   3957  1.26  riastrad 		*alloc = to_intel_crtc_state(for_crtc->state)->wm.skl.ddb;
   3958  1.26  riastrad 		return;
   3959  1.10  riastrad 	}
   3960   1.3  riastrad 
   3961  1.26  riastrad 	/*
   3962  1.26  riastrad 	 * Watermark/ddb requirement highly depends upon width of the
   3963  1.26  riastrad 	 * framebuffer, So instead of allocating DDB equally among pipes
   3964  1.26  riastrad 	 * distribute DDB based on resolution/width of the display.
   3965  1.26  riastrad 	 */
   3966  1.26  riastrad 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
   3967  1.26  riastrad 		const struct drm_display_mode *adjusted_mode =
   3968  1.26  riastrad 			&crtc_state->hw.adjusted_mode;
   3969  1.26  riastrad 		enum pipe pipe = crtc->pipe;
   3970  1.26  riastrad 		int hdisplay, vdisplay;
   3971   1.3  riastrad 
   3972  1.26  riastrad 		if (!crtc_state->hw.enable)
   3973  1.26  riastrad 			continue;
   3974   1.3  riastrad 
   3975  1.26  riastrad 		drm_mode_get_hv_timing(adjusted_mode, &hdisplay, &vdisplay);
   3976  1.26  riastrad 		total_width += hdisplay;
   3977   1.3  riastrad 
   3978  1.26  riastrad 		if (pipe < for_pipe)
   3979  1.26  riastrad 			width_before_pipe += hdisplay;
   3980  1.26  riastrad 		else if (pipe == for_pipe)
   3981  1.26  riastrad 			pipe_width = hdisplay;
   3982  1.10  riastrad 	}
   3983   1.3  riastrad 
   3984  1.26  riastrad 	alloc->start = ddb_size * width_before_pipe / total_width;
   3985  1.26  riastrad 	alloc->end = ddb_size * (width_before_pipe + pipe_width) / total_width;
   3986  1.26  riastrad }
   3987   1.3  riastrad 
   3988  1.26  riastrad static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
   3989  1.26  riastrad 				 int width, const struct drm_format_info *format,
   3990  1.26  riastrad 				 u64 modifier, unsigned int rotation,
   3991  1.26  riastrad 				 u32 plane_pixel_rate, struct skl_wm_params *wp,
   3992  1.26  riastrad 				 int color_plane);
   3993  1.26  riastrad static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
   3994  1.26  riastrad 				 int level,
   3995  1.26  riastrad 				 const struct skl_wm_params *wp,
   3996  1.26  riastrad 				 const struct skl_wm_level *result_prev,
   3997  1.26  riastrad 				 struct skl_wm_level *result /* out */);
   3998   1.3  riastrad 
   3999  1.26  riastrad static unsigned int
   4000  1.26  riastrad skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
   4001  1.26  riastrad 		      int num_active)
   4002  1.26  riastrad {
   4003  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   4004  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   4005  1.26  riastrad 	struct skl_wm_level wm = {};
   4006  1.26  riastrad 	int ret, min_ddb_alloc = 0;
   4007  1.26  riastrad 	struct skl_wm_params wp;
   4008  1.26  riastrad 
   4009  1.26  riastrad 	ret = skl_compute_wm_params(crtc_state, 256,
   4010  1.26  riastrad 				    drm_format_info(DRM_FORMAT_ARGB8888),
   4011  1.26  riastrad 				    DRM_FORMAT_MOD_LINEAR,
   4012  1.26  riastrad 				    DRM_MODE_ROTATE_0,
   4013  1.26  riastrad 				    crtc_state->pixel_rate, &wp, 0);
   4014  1.26  riastrad 	WARN_ON(ret);
   4015   1.3  riastrad 
   4016  1.26  riastrad 	for (level = 0; level <= max_level; level++) {
   4017  1.26  riastrad 		skl_compute_plane_wm(crtc_state, level, &wp, &wm, &wm);
   4018  1.26  riastrad 		if (wm.min_ddb_alloc == U16_MAX)
   4019  1.26  riastrad 			break;
   4020   1.3  riastrad 
   4021  1.26  riastrad 		min_ddb_alloc = wm.min_ddb_alloc;
   4022  1.10  riastrad 	}
   4023   1.1  riastrad 
   4024  1.26  riastrad 	return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
   4025   1.1  riastrad }
   4026   1.1  riastrad 
   4027  1.26  riastrad static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
   4028  1.26  riastrad 				       struct skl_ddb_entry *entry, u32 reg)
   4029   1.1  riastrad {
   4030   1.1  riastrad 
   4031  1.26  riastrad 	entry->start = reg & DDB_ENTRY_MASK;
   4032  1.26  riastrad 	entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
   4033  1.26  riastrad 
   4034  1.26  riastrad 	if (entry->end)
   4035  1.26  riastrad 		entry->end += 1;
   4036  1.26  riastrad }
   4037   1.1  riastrad 
   4038  1.26  riastrad static void
   4039  1.26  riastrad skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
   4040  1.26  riastrad 			   const enum pipe pipe,
   4041  1.26  riastrad 			   const enum plane_id plane_id,
   4042  1.26  riastrad 			   struct skl_ddb_entry *ddb_y,
   4043  1.26  riastrad 			   struct skl_ddb_entry *ddb_uv)
   4044  1.26  riastrad {
   4045  1.26  riastrad 	u32 val, val2;
   4046  1.26  riastrad 	u32 fourcc = 0;
   4047   1.1  riastrad 
   4048  1.26  riastrad 	/* Cursor doesn't support NV12/planar, so no extra calculation needed */
   4049  1.26  riastrad 	if (plane_id == PLANE_CURSOR) {
   4050  1.26  riastrad 		val = I915_READ(CUR_BUF_CFG(pipe));
   4051  1.26  riastrad 		skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
   4052  1.26  riastrad 		return;
   4053   1.1  riastrad 	}
   4054   1.1  riastrad 
   4055  1.26  riastrad 	val = I915_READ(PLANE_CTL(pipe, plane_id));
   4056   1.1  riastrad 
   4057  1.26  riastrad 	/* No DDB allocated for disabled planes */
   4058  1.26  riastrad 	if (val & PLANE_CTL_ENABLE)
   4059  1.26  riastrad 		fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
   4060  1.26  riastrad 					      val & PLANE_CTL_ORDER_RGBX,
   4061  1.26  riastrad 					      val & PLANE_CTL_ALPHA_MASK);
   4062  1.26  riastrad 
   4063  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 11) {
   4064  1.26  riastrad 		val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
   4065  1.26  riastrad 		skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
   4066  1.26  riastrad 	} else {
   4067  1.26  riastrad 		val = I915_READ(PLANE_BUF_CFG(pipe, plane_id));
   4068  1.26  riastrad 		val2 = I915_READ(PLANE_NV12_BUF_CFG(pipe, plane_id));
   4069   1.3  riastrad 
   4070  1.26  riastrad 		if (fourcc &&
   4071  1.26  riastrad 		    drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
   4072  1.26  riastrad 			swap(val, val2);
   4073   1.3  riastrad 
   4074  1.26  riastrad 		skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
   4075  1.26  riastrad 		skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
   4076  1.26  riastrad 	}
   4077  1.10  riastrad }
   4078   1.1  riastrad 
   4079  1.26  riastrad void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
   4080  1.26  riastrad 			       struct skl_ddb_entry *ddb_y,
   4081  1.26  riastrad 			       struct skl_ddb_entry *ddb_uv)
   4082  1.10  riastrad {
   4083  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   4084  1.26  riastrad 	enum intel_display_power_domain power_domain;
   4085  1.26  riastrad 	enum pipe pipe = crtc->pipe;
   4086  1.26  riastrad 	intel_wakeref_t wakeref;
   4087  1.26  riastrad 	enum plane_id plane_id;
   4088   1.1  riastrad 
   4089  1.26  riastrad 	power_domain = POWER_DOMAIN_PIPE(pipe);
   4090  1.26  riastrad 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
   4091  1.26  riastrad 	if (!wakeref)
   4092  1.26  riastrad 		return;
   4093   1.1  riastrad 
   4094  1.26  riastrad 	for_each_plane_id_on_crtc(crtc, plane_id)
   4095  1.26  riastrad 		skl_ddb_get_hw_plane_state(dev_priv, pipe,
   4096  1.26  riastrad 					   plane_id,
   4097  1.26  riastrad 					   &ddb_y[plane_id],
   4098  1.26  riastrad 					   &ddb_uv[plane_id]);
   4099   1.1  riastrad 
   4100  1.26  riastrad 	intel_display_power_put(dev_priv, power_domain, wakeref);
   4101  1.26  riastrad }
   4102   1.1  riastrad 
   4103  1.26  riastrad void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
   4104  1.26  riastrad 			  struct skl_ddb_allocation *ddb /* out */)
   4105  1.26  riastrad {
   4106  1.26  riastrad 	ddb->enabled_slices = intel_enabled_dbuf_slices_num(dev_priv);
   4107  1.26  riastrad }
   4108   1.1  riastrad 
   4109  1.26  riastrad /*
   4110  1.26  riastrad  * Determines the downscale amount of a plane for the purposes of watermark calculations.
   4111  1.26  riastrad  * The bspec defines downscale amount as:
   4112  1.26  riastrad  *
   4113  1.26  riastrad  * """
   4114  1.26  riastrad  * Horizontal down scale amount = maximum[1, Horizontal source size /
   4115  1.26  riastrad  *                                           Horizontal destination size]
   4116  1.26  riastrad  * Vertical down scale amount = maximum[1, Vertical source size /
   4117  1.26  riastrad  *                                         Vertical destination size]
   4118  1.26  riastrad  * Total down scale amount = Horizontal down scale amount *
   4119  1.26  riastrad  *                           Vertical down scale amount
   4120  1.26  riastrad  * """
   4121  1.26  riastrad  *
   4122  1.26  riastrad  * Return value is provided in 16.16 fixed point form to retain fractional part.
   4123  1.26  riastrad  * Caller should take care of dividing & rounding off the value.
   4124  1.26  riastrad  */
   4125  1.26  riastrad static uint_fixed_16_16_t
   4126  1.26  riastrad skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
   4127  1.26  riastrad 			   const struct intel_plane_state *plane_state)
   4128  1.10  riastrad {
   4129  1.26  riastrad 	u32 src_w, src_h, dst_w, dst_h;
   4130  1.26  riastrad 	uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
   4131  1.26  riastrad 	uint_fixed_16_16_t downscale_h, downscale_w;
   4132   1.3  riastrad 
   4133  1.26  riastrad 	if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
   4134  1.26  riastrad 		return u32_to_fixed16(0);
   4135  1.10  riastrad 
   4136  1.26  riastrad 	/*
   4137  1.26  riastrad 	 * Src coordinates are already rotated by 270 degrees for
   4138  1.26  riastrad 	 * the 90/270 degree plane rotation cases (to match the
   4139  1.26  riastrad 	 * GTT mapping), hence no need to account for rotation here.
   4140  1.26  riastrad 	 *
   4141  1.26  riastrad 	 * n.b., src is 16.16 fixed point, dst is whole integer.
   4142  1.26  riastrad 	 */
   4143  1.26  riastrad 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
   4144  1.26  riastrad 	src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
   4145  1.26  riastrad 	dst_w = drm_rect_width(&plane_state->uapi.dst);
   4146  1.26  riastrad 	dst_h = drm_rect_height(&plane_state->uapi.dst);
   4147  1.26  riastrad 
   4148  1.26  riastrad 	fp_w_ratio = div_fixed16(src_w, dst_w);
   4149  1.26  riastrad 	fp_h_ratio = div_fixed16(src_h, dst_h);
   4150  1.26  riastrad 	downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
   4151  1.26  riastrad 	downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
   4152  1.26  riastrad 
   4153  1.26  riastrad 	return mul_fixed16(downscale_w, downscale_h);
   4154  1.26  riastrad }
   4155  1.26  riastrad 
   4156  1.26  riastrad static u64
   4157  1.26  riastrad skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
   4158  1.26  riastrad 			     const struct intel_plane_state *plane_state,
   4159  1.26  riastrad 			     int color_plane)
   4160  1.26  riastrad {
   4161  1.26  riastrad 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
   4162  1.26  riastrad 	const struct drm_framebuffer *fb = plane_state->hw.fb;
   4163  1.26  riastrad 	u32 data_rate;
   4164  1.26  riastrad 	u32 width = 0, height = 0;
   4165  1.26  riastrad 	uint_fixed_16_16_t down_scale_amount;
   4166  1.26  riastrad 	u64 rate;
   4167   1.1  riastrad 
   4168  1.26  riastrad 	if (!plane_state->uapi.visible)
   4169  1.26  riastrad 		return 0;
   4170   1.1  riastrad 
   4171  1.26  riastrad 	if (plane->id == PLANE_CURSOR)
   4172  1.26  riastrad 		return 0;
   4173   1.1  riastrad 
   4174  1.26  riastrad 	if (color_plane == 1 &&
   4175  1.26  riastrad 	    !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
   4176  1.26  riastrad 		return 0;
   4177   1.3  riastrad 
   4178   1.1  riastrad 	/*
   4179  1.26  riastrad 	 * Src coordinates are already rotated by 270 degrees for
   4180  1.26  riastrad 	 * the 90/270 degree plane rotation cases (to match the
   4181  1.26  riastrad 	 * GTT mapping), hence no need to account for rotation here.
   4182   1.1  riastrad 	 */
   4183  1.26  riastrad 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
   4184  1.26  riastrad 	height = drm_rect_height(&plane_state->uapi.src) >> 16;
   4185   1.1  riastrad 
   4186  1.26  riastrad 	/* UV plane does 1/2 pixel sub-sampling */
   4187  1.26  riastrad 	if (color_plane == 1) {
   4188  1.26  riastrad 		width /= 2;
   4189  1.26  riastrad 		height /= 2;
   4190  1.26  riastrad 	}
   4191   1.1  riastrad 
   4192  1.26  riastrad 	data_rate = width * height;
   4193   1.3  riastrad 
   4194  1.26  riastrad 	down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
   4195   1.1  riastrad 
   4196  1.26  riastrad 	rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
   4197   1.1  riastrad 
   4198  1.26  riastrad 	rate *= fb->format->cpp[color_plane];
   4199  1.26  riastrad 	return rate;
   4200   1.1  riastrad }
   4201   1.1  riastrad 
   4202  1.26  riastrad static u64
   4203  1.26  riastrad skl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
   4204  1.26  riastrad 				 u64 *plane_data_rate,
   4205  1.26  riastrad 				 u64 *uv_plane_data_rate)
   4206   1.1  riastrad {
   4207  1.26  riastrad 	struct drm_atomic_state *state = crtc_state->uapi.state;
   4208  1.26  riastrad 	struct intel_plane *plane;
   4209  1.26  riastrad 	const struct intel_plane_state *plane_state;
   4210  1.26  riastrad 	u64 total_data_rate = 0;
   4211  1.26  riastrad 
   4212  1.26  riastrad 	if (WARN_ON(!state))
   4213  1.26  riastrad 		return 0;
   4214  1.10  riastrad 
   4215  1.26  riastrad 	/* Calculate and cache data rate for each plane */
   4216  1.26  riastrad 	intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
   4217  1.26  riastrad 		enum plane_id plane_id = plane->id;
   4218  1.26  riastrad 		u64 rate;
   4219  1.26  riastrad 
   4220  1.26  riastrad 		/* packed/y */
   4221  1.26  riastrad 		rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
   4222  1.26  riastrad 		plane_data_rate[plane_id] = rate;
   4223  1.26  riastrad 		total_data_rate += rate;
   4224  1.26  riastrad 
   4225  1.26  riastrad 		/* uv-plane */
   4226  1.26  riastrad 		rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
   4227  1.26  riastrad 		uv_plane_data_rate[plane_id] = rate;
   4228  1.26  riastrad 		total_data_rate += rate;
   4229  1.26  riastrad 	}
   4230  1.10  riastrad 
   4231  1.26  riastrad 	return total_data_rate;
   4232  1.26  riastrad }
   4233  1.10  riastrad 
   4234  1.26  riastrad static u64
   4235  1.26  riastrad icl_get_total_relative_data_rate(struct intel_crtc_state *crtc_state,
   4236  1.26  riastrad 				 u64 *plane_data_rate)
   4237  1.26  riastrad {
   4238  1.26  riastrad 	struct intel_plane *plane;
   4239  1.26  riastrad 	const struct intel_plane_state *plane_state;
   4240  1.26  riastrad 	u64 total_data_rate = 0;
   4241   1.3  riastrad 
   4242  1.26  riastrad 	if (WARN_ON(!crtc_state->uapi.state))
   4243  1.26  riastrad 		return 0;
   4244   1.3  riastrad 
   4245  1.26  riastrad 	/* Calculate and cache data rate for each plane */
   4246  1.26  riastrad 	intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
   4247  1.26  riastrad 		enum plane_id plane_id = plane->id;
   4248  1.26  riastrad 		u64 rate;
   4249  1.26  riastrad 
   4250  1.26  riastrad 		if (!plane_state->planar_linked_plane) {
   4251  1.26  riastrad 			rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
   4252  1.26  riastrad 			plane_data_rate[plane_id] = rate;
   4253  1.26  riastrad 			total_data_rate += rate;
   4254  1.26  riastrad 		} else {
   4255  1.26  riastrad 			enum plane_id y_plane_id;
   4256   1.1  riastrad 
   4257  1.26  riastrad 			/*
   4258  1.26  riastrad 			 * The slave plane might not iterate in
   4259  1.26  riastrad 			 * intel_atomic_crtc_state_for_each_plane_state(),
   4260  1.26  riastrad 			 * and needs the master plane state which may be
   4261  1.26  riastrad 			 * NULL if we try get_new_plane_state(), so we
   4262  1.26  riastrad 			 * always calculate from the master.
   4263  1.26  riastrad 			 */
   4264  1.26  riastrad 			if (plane_state->planar_slave)
   4265  1.26  riastrad 				continue;
   4266   1.1  riastrad 
   4267  1.26  riastrad 			/* Y plane rate is calculated on the slave */
   4268  1.26  riastrad 			rate = skl_plane_relative_data_rate(crtc_state, plane_state, 0);
   4269  1.26  riastrad 			y_plane_id = plane_state->planar_linked_plane->id;
   4270  1.26  riastrad 			plane_data_rate[y_plane_id] = rate;
   4271  1.26  riastrad 			total_data_rate += rate;
   4272  1.26  riastrad 
   4273  1.26  riastrad 			rate = skl_plane_relative_data_rate(crtc_state, plane_state, 1);
   4274  1.26  riastrad 			plane_data_rate[plane_id] = rate;
   4275  1.26  riastrad 			total_data_rate += rate;
   4276  1.26  riastrad 		}
   4277  1.10  riastrad 	}
   4278   1.1  riastrad 
   4279  1.26  riastrad 	return total_data_rate;
   4280   1.1  riastrad }
   4281   1.1  riastrad 
   4282  1.26  riastrad static int
   4283  1.26  riastrad skl_allocate_pipe_ddb(struct intel_crtc_state *crtc_state,
   4284  1.26  riastrad 		      struct skl_ddb_allocation *ddb /* out */)
   4285   1.1  riastrad {
   4286  1.26  riastrad 	struct drm_atomic_state *state = crtc_state->uapi.state;
   4287  1.26  riastrad 	struct drm_crtc *crtc = crtc_state->uapi.crtc;
   4288  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->dev);
   4289  1.26  riastrad 	struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
   4290  1.26  riastrad 	struct skl_ddb_entry *alloc = &crtc_state->wm.skl.ddb;
   4291  1.26  riastrad 	u16 alloc_size, start = 0;
   4292  1.26  riastrad 	u16 total[I915_MAX_PLANES] = {};
   4293  1.26  riastrad 	u16 uv_total[I915_MAX_PLANES] = {};
   4294  1.26  riastrad 	u64 total_data_rate;
   4295  1.26  riastrad 	enum plane_id plane_id;
   4296  1.26  riastrad 	int num_active;
   4297  1.26  riastrad 	u64 plane_data_rate[I915_MAX_PLANES] = {};
   4298  1.26  riastrad 	u64 uv_plane_data_rate[I915_MAX_PLANES] = {};
   4299  1.26  riastrad 	u32 blocks;
   4300  1.26  riastrad 	int level;
   4301   1.3  riastrad 
   4302  1.26  riastrad 	/* Clear the partitioning for disabled planes. */
   4303  1.26  riastrad 	memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
   4304  1.26  riastrad 	memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
   4305   1.1  riastrad 
   4306  1.26  riastrad 	if (WARN_ON(!state))
   4307  1.26  riastrad 		return 0;
   4308   1.3  riastrad 
   4309  1.26  riastrad 	if (!crtc_state->hw.active) {
   4310  1.26  riastrad 		alloc->start = alloc->end = 0;
   4311  1.26  riastrad 		return 0;
   4312   1.3  riastrad 	}
   4313   1.1  riastrad 
   4314  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 11)
   4315  1.26  riastrad 		total_data_rate =
   4316  1.26  riastrad 			icl_get_total_relative_data_rate(crtc_state,
   4317  1.26  riastrad 							 plane_data_rate);
   4318  1.26  riastrad 	else
   4319  1.26  riastrad 		total_data_rate =
   4320  1.26  riastrad 			skl_get_total_relative_data_rate(crtc_state,
   4321  1.26  riastrad 							 plane_data_rate,
   4322  1.26  riastrad 							 uv_plane_data_rate);
   4323   1.1  riastrad 
   4324  1.10  riastrad 
   4325  1.26  riastrad 	skl_ddb_get_pipe_allocation_limits(dev_priv, crtc_state, total_data_rate,
   4326  1.26  riastrad 					   ddb, alloc, &num_active);
   4327  1.26  riastrad 	alloc_size = skl_ddb_entry_size(alloc);
   4328  1.26  riastrad 	if (alloc_size == 0)
   4329  1.26  riastrad 		return 0;
   4330  1.10  riastrad 
   4331  1.26  riastrad 	/* Allocate fixed number of blocks for cursor. */
   4332  1.26  riastrad 	total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
   4333  1.26  riastrad 	alloc_size -= total[PLANE_CURSOR];
   4334  1.26  riastrad 	crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
   4335  1.26  riastrad 		alloc->end - total[PLANE_CURSOR];
   4336  1.26  riastrad 	crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
   4337  1.10  riastrad 
   4338  1.26  riastrad 	if (total_data_rate == 0)
   4339  1.26  riastrad 		return 0;
   4340  1.10  riastrad 
   4341  1.26  riastrad 	/*
   4342  1.26  riastrad 	 * Find the highest watermark level for which we can satisfy the block
   4343  1.26  riastrad 	 * requirement of active planes.
   4344  1.26  riastrad 	 */
   4345  1.26  riastrad 	for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
   4346  1.26  riastrad 		blocks = 0;
   4347  1.26  riastrad 		for_each_plane_id_on_crtc(intel_crtc, plane_id) {
   4348  1.26  riastrad 			const struct skl_plane_wm *wm =
   4349  1.26  riastrad 				&crtc_state->wm.skl.optimal.planes[plane_id];
   4350  1.26  riastrad 
   4351  1.26  riastrad 			if (plane_id == PLANE_CURSOR) {
   4352  1.26  riastrad 				if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
   4353  1.26  riastrad 					WARN_ON(wm->wm[level].min_ddb_alloc != U16_MAX);
   4354  1.26  riastrad 					blocks = U32_MAX;
   4355  1.26  riastrad 					break;
   4356  1.26  riastrad 				}
   4357  1.26  riastrad 				continue;
   4358  1.26  riastrad 			}
   4359  1.10  riastrad 
   4360  1.26  riastrad 			blocks += wm->wm[level].min_ddb_alloc;
   4361  1.26  riastrad 			blocks += wm->uv_wm[level].min_ddb_alloc;
   4362  1.26  riastrad 		}
   4363  1.10  riastrad 
   4364  1.26  riastrad 		if (blocks <= alloc_size) {
   4365  1.26  riastrad 			alloc_size -= blocks;
   4366  1.26  riastrad 			break;
   4367  1.26  riastrad 		}
   4368  1.26  riastrad 	}
   4369  1.10  riastrad 
   4370  1.26  riastrad 	if (level < 0) {
   4371  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   4372  1.26  riastrad 			    "Requested display configuration exceeds system DDB limitations");
   4373  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
   4374  1.26  riastrad 			    blocks, alloc_size);
   4375  1.26  riastrad 		return -EINVAL;
   4376  1.26  riastrad 	}
   4377  1.10  riastrad 
   4378  1.26  riastrad 	/*
   4379  1.26  riastrad 	 * Grant each plane the blocks it requires at the highest achievable
   4380  1.26  riastrad 	 * watermark level, plus an extra share of the leftover blocks
   4381  1.26  riastrad 	 * proportional to its relative data rate.
   4382  1.26  riastrad 	 */
   4383  1.26  riastrad 	for_each_plane_id_on_crtc(intel_crtc, plane_id) {
   4384  1.26  riastrad 		const struct skl_plane_wm *wm =
   4385  1.26  riastrad 			&crtc_state->wm.skl.optimal.planes[plane_id];
   4386  1.26  riastrad 		u64 rate;
   4387  1.26  riastrad 		u16 extra;
   4388  1.10  riastrad 
   4389  1.26  riastrad 		if (plane_id == PLANE_CURSOR)
   4390  1.26  riastrad 			continue;
   4391   1.1  riastrad 
   4392  1.26  riastrad 		/*
   4393  1.26  riastrad 		 * We've accounted for all active planes; remaining planes are
   4394  1.26  riastrad 		 * all disabled.
   4395  1.26  riastrad 		 */
   4396  1.26  riastrad 		if (total_data_rate == 0)
   4397  1.26  riastrad 			break;
   4398   1.3  riastrad 
   4399  1.26  riastrad 		rate = plane_data_rate[plane_id];
   4400  1.26  riastrad 		extra = min_t(u16, alloc_size,
   4401  1.26  riastrad 			      DIV64_U64_ROUND_UP(alloc_size * rate,
   4402  1.26  riastrad 						 total_data_rate));
   4403  1.26  riastrad 		total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
   4404  1.26  riastrad 		alloc_size -= extra;
   4405  1.26  riastrad 		total_data_rate -= rate;
   4406  1.10  riastrad 
   4407  1.26  riastrad 		if (total_data_rate == 0)
   4408  1.26  riastrad 			break;
   4409   1.1  riastrad 
   4410  1.26  riastrad 		rate = uv_plane_data_rate[plane_id];
   4411  1.26  riastrad 		extra = min_t(u16, alloc_size,
   4412  1.26  riastrad 			      DIV64_U64_ROUND_UP(alloc_size * rate,
   4413  1.26  riastrad 						 total_data_rate));
   4414  1.26  riastrad 		uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
   4415  1.26  riastrad 		alloc_size -= extra;
   4416  1.26  riastrad 		total_data_rate -= rate;
   4417   1.3  riastrad 	}
   4418  1.26  riastrad 	WARN_ON(alloc_size != 0 || total_data_rate != 0);
   4419   1.3  riastrad 
   4420  1.26  riastrad 	/* Set the actual DDB start/end points for each plane */
   4421  1.26  riastrad 	start = alloc->start;
   4422  1.26  riastrad 	for_each_plane_id_on_crtc(intel_crtc, plane_id) {
   4423  1.26  riastrad 		struct skl_ddb_entry *plane_alloc =
   4424  1.26  riastrad 			&crtc_state->wm.skl.plane_ddb_y[plane_id];
   4425  1.26  riastrad 		struct skl_ddb_entry *uv_plane_alloc =
   4426  1.26  riastrad 			&crtc_state->wm.skl.plane_ddb_uv[plane_id];
   4427   1.3  riastrad 
   4428  1.26  riastrad 		if (plane_id == PLANE_CURSOR)
   4429  1.26  riastrad 			continue;
   4430   1.3  riastrad 
   4431  1.26  riastrad 		/* Gen11+ uses a separate plane for UV watermarks */
   4432  1.26  riastrad 		WARN_ON(INTEL_GEN(dev_priv) >= 11 && uv_total[plane_id]);
   4433   1.3  riastrad 
   4434  1.26  riastrad 		/* Leave disabled planes at (0,0) */
   4435  1.26  riastrad 		if (total[plane_id]) {
   4436  1.26  riastrad 			plane_alloc->start = start;
   4437  1.26  riastrad 			start += total[plane_id];
   4438  1.26  riastrad 			plane_alloc->end = start;
   4439  1.26  riastrad 		}
   4440   1.3  riastrad 
   4441  1.26  riastrad 		if (uv_total[plane_id]) {
   4442  1.26  riastrad 			uv_plane_alloc->start = start;
   4443  1.26  riastrad 			start += uv_total[plane_id];
   4444  1.26  riastrad 			uv_plane_alloc->end = start;
   4445  1.26  riastrad 		}
   4446  1.10  riastrad 	}
   4447   1.3  riastrad 
   4448  1.26  riastrad 	/*
   4449  1.26  riastrad 	 * When we calculated watermark values we didn't know how high
   4450  1.26  riastrad 	 * of a level we'd actually be able to hit, so we just marked
   4451  1.26  riastrad 	 * all levels as "enabled."  Go back now and disable the ones
   4452  1.26  riastrad 	 * that aren't actually possible.
   4453  1.26  riastrad 	 */
   4454  1.26  riastrad 	for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
   4455  1.26  riastrad 		for_each_plane_id_on_crtc(intel_crtc, plane_id) {
   4456  1.26  riastrad 			struct skl_plane_wm *wm =
   4457  1.26  riastrad 				&crtc_state->wm.skl.optimal.planes[plane_id];
   4458   1.3  riastrad 
   4459  1.26  riastrad 			/*
   4460  1.26  riastrad 			 * We only disable the watermarks for each plane if
   4461  1.26  riastrad 			 * they exceed the ddb allocation of said plane. This
   4462  1.26  riastrad 			 * is done so that we don't end up touching cursor
   4463  1.26  riastrad 			 * watermarks needlessly when some other plane reduces
   4464  1.26  riastrad 			 * our max possible watermark level.
   4465  1.26  riastrad 			 *
   4466  1.26  riastrad 			 * Bspec has this to say about the PLANE_WM enable bit:
   4467  1.26  riastrad 			 * "All the watermarks at this level for all enabled
   4468  1.26  riastrad 			 *  planes must be enabled before the level will be used."
   4469  1.26  riastrad 			 * So this is actually safe to do.
   4470  1.26  riastrad 			 */
   4471  1.26  riastrad 			if (wm->wm[level].min_ddb_alloc > total[plane_id] ||
   4472  1.26  riastrad 			    wm->uv_wm[level].min_ddb_alloc > uv_total[plane_id])
   4473  1.26  riastrad 				memset(&wm->wm[level], 0, sizeof(wm->wm[level]));
   4474   1.3  riastrad 
   4475  1.26  riastrad 			/*
   4476  1.26  riastrad 			 * Wa_1408961008:icl, ehl
   4477  1.26  riastrad 			 * Underruns with WM1+ disabled
   4478  1.26  riastrad 			 */
   4479  1.26  riastrad 			if (IS_GEN(dev_priv, 11) &&
   4480  1.26  riastrad 			    level == 1 && wm->wm[0].plane_en) {
   4481  1.26  riastrad 				wm->wm[level].plane_res_b = wm->wm[0].plane_res_b;
   4482  1.26  riastrad 				wm->wm[level].plane_res_l = wm->wm[0].plane_res_l;
   4483  1.26  riastrad 				wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
   4484  1.26  riastrad 			}
   4485  1.26  riastrad 		}
   4486  1.10  riastrad 	}
   4487   1.3  riastrad 
   4488  1.26  riastrad 	/*
   4489  1.26  riastrad 	 * Go back and disable the transition watermark if it turns out we
   4490  1.26  riastrad 	 * don't have enough DDB blocks for it.
   4491  1.26  riastrad 	 */
   4492  1.26  riastrad 	for_each_plane_id_on_crtc(intel_crtc, plane_id) {
   4493  1.26  riastrad 		struct skl_plane_wm *wm =
   4494  1.26  riastrad 			&crtc_state->wm.skl.optimal.planes[plane_id];
   4495   1.3  riastrad 
   4496  1.26  riastrad 		if (wm->trans_wm.plane_res_b >= total[plane_id])
   4497  1.26  riastrad 			memset(&wm->trans_wm, 0, sizeof(wm->trans_wm));
   4498  1.10  riastrad 	}
   4499   1.3  riastrad 
   4500  1.26  riastrad 	return 0;
   4501  1.10  riastrad }
   4502  1.10  riastrad 
   4503  1.26  riastrad /*
   4504  1.26  riastrad  * The max latency should be 257 (max the punit can code is 255 and we add 2us
   4505  1.26  riastrad  * for the read latency) and cpp should always be <= 8, so that
   4506  1.26  riastrad  * should allow pixel_rate up to ~2 GHz which seems sufficient since max
   4507  1.26  riastrad  * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
   4508  1.26  riastrad */
   4509  1.26  riastrad static uint_fixed_16_16_t
   4510  1.26  riastrad skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
   4511  1.26  riastrad 	       u8 cpp, u32 latency, u32 dbuf_block_size)
   4512  1.10  riastrad {
   4513  1.26  riastrad 	u32 wm_intermediate_val;
   4514  1.26  riastrad 	uint_fixed_16_16_t ret;
   4515  1.26  riastrad 
   4516  1.26  riastrad 	if (latency == 0)
   4517  1.26  riastrad 		return FP_16_16_MAX;
   4518   1.3  riastrad 
   4519  1.26  riastrad 	wm_intermediate_val = latency * pixel_rate * cpp;
   4520  1.26  riastrad 	ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
   4521   1.3  riastrad 
   4522  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 10)
   4523  1.26  riastrad 		ret = add_fixed16_u32(ret, 1);
   4524   1.3  riastrad 
   4525  1.26  riastrad 	return ret;
   4526  1.10  riastrad }
   4527   1.3  riastrad 
   4528  1.26  riastrad static uint_fixed_16_16_t
   4529  1.26  riastrad skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
   4530  1.26  riastrad 	       uint_fixed_16_16_t plane_blocks_per_line)
   4531  1.10  riastrad {
   4532  1.26  riastrad 	u32 wm_intermediate_val;
   4533  1.26  riastrad 	uint_fixed_16_16_t ret;
   4534   1.3  riastrad 
   4535  1.26  riastrad 	if (latency == 0)
   4536  1.26  riastrad 		return FP_16_16_MAX;
   4537  1.20      maya 
   4538  1.26  riastrad 	wm_intermediate_val = latency * pixel_rate;
   4539  1.26  riastrad 	wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
   4540  1.26  riastrad 					   pipe_htotal * 1000);
   4541  1.26  riastrad 	ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
   4542  1.26  riastrad 	return ret;
   4543  1.20      maya }
   4544  1.20      maya 
   4545  1.26  riastrad static uint_fixed_16_16_t
   4546  1.26  riastrad intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
   4547  1.20      maya {
   4548  1.26  riastrad 	u32 pixel_rate;
   4549  1.26  riastrad 	u32 crtc_htotal;
   4550  1.26  riastrad 	uint_fixed_16_16_t linetime_us;
   4551  1.20      maya 
   4552  1.26  riastrad 	if (!crtc_state->hw.active)
   4553  1.26  riastrad 		return u32_to_fixed16(0);
   4554   1.3  riastrad 
   4555  1.26  riastrad 	pixel_rate = crtc_state->pixel_rate;
   4556   1.3  riastrad 
   4557  1.26  riastrad 	if (WARN_ON(pixel_rate == 0))
   4558  1.26  riastrad 		return u32_to_fixed16(0);
   4559  1.10  riastrad 
   4560  1.26  riastrad 	crtc_htotal = crtc_state->hw.adjusted_mode.crtc_htotal;
   4561  1.26  riastrad 	linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
   4562   1.3  riastrad 
   4563  1.26  riastrad 	return linetime_us;
   4564   1.3  riastrad }
   4565   1.3  riastrad 
   4566  1.26  riastrad static u32
   4567  1.26  riastrad skl_adjusted_plane_pixel_rate(const struct intel_crtc_state *crtc_state,
   4568  1.26  riastrad 			      const struct intel_plane_state *plane_state)
   4569   1.3  riastrad {
   4570  1.26  riastrad 	u64 adjusted_pixel_rate;
   4571  1.26  riastrad 	uint_fixed_16_16_t downscale_amount;
   4572   1.3  riastrad 
   4573  1.26  riastrad 	/* Shouldn't reach here on disabled planes... */
   4574  1.26  riastrad 	if (WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state)))
   4575  1.26  riastrad 		return 0;
   4576   1.3  riastrad 
   4577  1.26  riastrad 	/*
   4578  1.26  riastrad 	 * Adjusted plane pixel rate is just the pipe's adjusted pixel rate
   4579  1.26  riastrad 	 * with additional adjustments for plane-specific scaling.
   4580  1.26  riastrad 	 */
   4581  1.26  riastrad 	adjusted_pixel_rate = crtc_state->pixel_rate;
   4582  1.26  riastrad 	downscale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
   4583  1.10  riastrad 
   4584  1.26  riastrad 	return mul_round_up_u32_fixed16(adjusted_pixel_rate,
   4585  1.26  riastrad 					    downscale_amount);
   4586  1.10  riastrad }
   4587   1.3  riastrad 
   4588  1.26  riastrad static int
   4589  1.26  riastrad skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
   4590  1.26  riastrad 		      int width, const struct drm_format_info *format,
   4591  1.26  riastrad 		      u64 modifier, unsigned int rotation,
   4592  1.26  riastrad 		      u32 plane_pixel_rate, struct skl_wm_params *wp,
   4593  1.26  riastrad 		      int color_plane)
   4594  1.10  riastrad {
   4595  1.26  riastrad 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
   4596  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   4597  1.26  riastrad 	u32 interm_pbpl;
   4598  1.10  riastrad 
   4599  1.26  riastrad 	/* only planar format has two planes */
   4600  1.26  riastrad 	if (color_plane == 1 &&
   4601  1.26  riastrad 	    !intel_format_info_is_yuv_semiplanar(format, modifier)) {
   4602  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   4603  1.26  riastrad 			    "Non planar format have single plane\n");
   4604  1.26  riastrad 		return -EINVAL;
   4605  1.26  riastrad 	}
   4606  1.26  riastrad 
   4607  1.26  riastrad 	wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
   4608  1.26  riastrad 		      modifier == I915_FORMAT_MOD_Yf_TILED ||
   4609  1.26  riastrad 		      modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
   4610  1.26  riastrad 		      modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
   4611  1.26  riastrad 	wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
   4612  1.26  riastrad 	wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
   4613  1.26  riastrad 			 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
   4614  1.26  riastrad 	wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
   4615  1.26  riastrad 
   4616  1.26  riastrad 	wp->width = width;
   4617  1.26  riastrad 	if (color_plane == 1 && wp->is_planar)
   4618  1.26  riastrad 		wp->width /= 2;
   4619  1.26  riastrad 
   4620  1.26  riastrad 	wp->cpp = format->cpp[color_plane];
   4621  1.26  riastrad 	wp->plane_pixel_rate = plane_pixel_rate;
   4622  1.26  riastrad 
   4623  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 11 &&
   4624  1.26  riastrad 	    modifier == I915_FORMAT_MOD_Yf_TILED  && wp->cpp == 1)
   4625  1.26  riastrad 		wp->dbuf_block_size = 256;
   4626  1.10  riastrad 	else
   4627  1.26  riastrad 		wp->dbuf_block_size = 512;
   4628   1.3  riastrad 
   4629  1.26  riastrad 	if (drm_rotation_90_or_270(rotation)) {
   4630  1.26  riastrad 		switch (wp->cpp) {
   4631  1.26  riastrad 		case 1:
   4632  1.26  riastrad 			wp->y_min_scanlines = 16;
   4633  1.26  riastrad 			break;
   4634  1.26  riastrad 		case 2:
   4635  1.26  riastrad 			wp->y_min_scanlines = 8;
   4636  1.26  riastrad 			break;
   4637  1.26  riastrad 		case 4:
   4638  1.26  riastrad 			wp->y_min_scanlines = 4;
   4639  1.26  riastrad 			break;
   4640  1.26  riastrad 		default:
   4641  1.26  riastrad 			MISSING_CASE(wp->cpp);
   4642  1.26  riastrad 			return -EINVAL;
   4643  1.26  riastrad 		}
   4644  1.26  riastrad 	} else {
   4645  1.26  riastrad 		wp->y_min_scanlines = 4;
   4646  1.26  riastrad 	}
   4647   1.3  riastrad 
   4648  1.26  riastrad 	if (skl_needs_memory_bw_wa(dev_priv))
   4649  1.26  riastrad 		wp->y_min_scanlines *= 2;
   4650   1.3  riastrad 
   4651  1.26  riastrad 	wp->plane_bytes_per_line = wp->width * wp->cpp;
   4652  1.26  riastrad 	if (wp->y_tiled) {
   4653  1.26  riastrad 		interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
   4654  1.26  riastrad 					   wp->y_min_scanlines,
   4655  1.26  riastrad 					   wp->dbuf_block_size);
   4656  1.26  riastrad 
   4657  1.26  riastrad 		if (INTEL_GEN(dev_priv) >= 10)
   4658  1.26  riastrad 			interm_pbpl++;
   4659  1.26  riastrad 
   4660  1.26  riastrad 		wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
   4661  1.26  riastrad 							wp->y_min_scanlines);
   4662  1.26  riastrad 	} else if (wp->x_tiled && IS_GEN(dev_priv, 9)) {
   4663  1.26  riastrad 		interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
   4664  1.26  riastrad 					   wp->dbuf_block_size);
   4665  1.26  riastrad 		wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
   4666  1.26  riastrad 	} else {
   4667  1.26  riastrad 		interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
   4668  1.26  riastrad 					   wp->dbuf_block_size) + 1;
   4669  1.26  riastrad 		wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
   4670   1.3  riastrad 	}
   4671   1.3  riastrad 
   4672  1.26  riastrad 	wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
   4673  1.26  riastrad 					     wp->plane_blocks_per_line);
   4674  1.10  riastrad 
   4675  1.26  riastrad 	wp->linetime_us = fixed16_to_u32_round_up(
   4676  1.26  riastrad 					intel_get_linetime_us(crtc_state));
   4677  1.10  riastrad 
   4678  1.26  riastrad 	return 0;
   4679   1.3  riastrad }
   4680   1.3  riastrad 
   4681  1.26  riastrad static int
   4682  1.26  riastrad skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
   4683  1.26  riastrad 			    const struct intel_plane_state *plane_state,
   4684  1.26  riastrad 			    struct skl_wm_params *wp, int color_plane)
   4685   1.3  riastrad {
   4686  1.26  riastrad 	const struct drm_framebuffer *fb = plane_state->hw.fb;
   4687  1.26  riastrad 	int width;
   4688   1.3  riastrad 
   4689  1.26  riastrad 	/*
   4690  1.26  riastrad 	 * Src coordinates are already rotated by 270 degrees for
   4691  1.26  riastrad 	 * the 90/270 degree plane rotation cases (to match the
   4692  1.26  riastrad 	 * GTT mapping), hence no need to account for rotation here.
   4693  1.26  riastrad 	 */
   4694  1.26  riastrad 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
   4695  1.10  riastrad 
   4696  1.26  riastrad 	return skl_compute_wm_params(crtc_state, width,
   4697  1.26  riastrad 				     fb->format, fb->modifier,
   4698  1.26  riastrad 				     plane_state->hw.rotation,
   4699  1.26  riastrad 				     skl_adjusted_plane_pixel_rate(crtc_state, plane_state),
   4700  1.26  riastrad 				     wp, color_plane);
   4701  1.10  riastrad }
   4702  1.10  riastrad 
   4703  1.26  riastrad static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
   4704  1.10  riastrad {
   4705  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
   4706  1.26  riastrad 		return true;
   4707   1.1  riastrad 
   4708  1.26  riastrad 	/* The number of lines are ignored for the level 0 watermark. */
   4709  1.26  riastrad 	return level > 0;
   4710  1.26  riastrad }
   4711  1.10  riastrad 
   4712  1.26  riastrad static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
   4713  1.26  riastrad 				 int level,
   4714  1.26  riastrad 				 const struct skl_wm_params *wp,
   4715  1.26  riastrad 				 const struct skl_wm_level *result_prev,
   4716  1.26  riastrad 				 struct skl_wm_level *result /* out */)
   4717  1.26  riastrad {
   4718  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   4719  1.26  riastrad 	u32 latency = dev_priv->wm.skl_latency[level];
   4720  1.26  riastrad 	uint_fixed_16_16_t method1, method2;
   4721  1.26  riastrad 	uint_fixed_16_16_t selected_result;
   4722  1.26  riastrad 	u32 res_blocks, res_lines, min_ddb_alloc = 0;
   4723  1.26  riastrad 
   4724  1.26  riastrad 	if (latency == 0) {
   4725  1.26  riastrad 		/* reject it */
   4726  1.26  riastrad 		result->min_ddb_alloc = U16_MAX;
   4727   1.1  riastrad 		return;
   4728   1.1  riastrad 	}
   4729   1.1  riastrad 
   4730  1.26  riastrad 	/*
   4731  1.26  riastrad 	 * WaIncreaseLatencyIPCEnabled: kbl,cfl
   4732  1.26  riastrad 	 * Display WA #1141: kbl,cfl
   4733  1.26  riastrad 	 */
   4734  1.26  riastrad 	if ((IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv)) ||
   4735  1.26  riastrad 	    dev_priv->ipc_enabled)
   4736  1.26  riastrad 		latency += 4;
   4737  1.26  riastrad 
   4738  1.26  riastrad 	if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
   4739  1.26  riastrad 		latency += 15;
   4740  1.26  riastrad 
   4741  1.26  riastrad 	method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
   4742  1.26  riastrad 				 wp->cpp, latency, wp->dbuf_block_size);
   4743  1.26  riastrad 	method2 = skl_wm_method2(wp->plane_pixel_rate,
   4744  1.26  riastrad 				 crtc_state->hw.adjusted_mode.crtc_htotal,
   4745  1.26  riastrad 				 latency,
   4746  1.26  riastrad 				 wp->plane_blocks_per_line);
   4747  1.10  riastrad 
   4748  1.26  riastrad 	if (wp->y_tiled) {
   4749  1.26  riastrad 		selected_result = max_fixed16(method2, wp->y_tile_minimum);
   4750  1.26  riastrad 	} else {
   4751  1.26  riastrad 		if ((wp->cpp * crtc_state->hw.adjusted_mode.crtc_htotal /
   4752  1.26  riastrad 		     wp->dbuf_block_size < 1) &&
   4753  1.26  riastrad 		     (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
   4754  1.26  riastrad 			selected_result = method2;
   4755  1.26  riastrad 		} else if (latency >= wp->linetime_us) {
   4756  1.26  riastrad 			if (IS_GEN(dev_priv, 9) &&
   4757  1.26  riastrad 			    !IS_GEMINILAKE(dev_priv))
   4758  1.26  riastrad 				selected_result = min_fixed16(method1, method2);
   4759  1.26  riastrad 			else
   4760  1.26  riastrad 				selected_result = method2;
   4761  1.26  riastrad 		} else {
   4762  1.26  riastrad 			selected_result = method1;
   4763  1.26  riastrad 		}
   4764  1.26  riastrad 	}
   4765  1.10  riastrad 
   4766  1.26  riastrad 	res_blocks = fixed16_to_u32_round_up(selected_result) + 1;
   4767  1.26  riastrad 	res_lines = div_round_up_fixed16(selected_result,
   4768  1.26  riastrad 					 wp->plane_blocks_per_line);
   4769  1.26  riastrad 
   4770  1.26  riastrad 	if (IS_GEN9_BC(dev_priv) || IS_BROXTON(dev_priv)) {
   4771  1.26  riastrad 		/* Display WA #1125: skl,bxt,kbl */
   4772  1.26  riastrad 		if (level == 0 && wp->rc_surface)
   4773  1.26  riastrad 			res_blocks +=
   4774  1.26  riastrad 				fixed16_to_u32_round_up(wp->y_tile_minimum);
   4775  1.26  riastrad 
   4776  1.26  riastrad 		/* Display WA #1126: skl,bxt,kbl */
   4777  1.26  riastrad 		if (level >= 1 && level <= 7) {
   4778  1.26  riastrad 			if (wp->y_tiled) {
   4779  1.26  riastrad 				res_blocks +=
   4780  1.26  riastrad 				    fixed16_to_u32_round_up(wp->y_tile_minimum);
   4781  1.26  riastrad 				res_lines += wp->y_min_scanlines;
   4782  1.26  riastrad 			} else {
   4783  1.26  riastrad 				res_blocks++;
   4784  1.26  riastrad 			}
   4785  1.10  riastrad 
   4786  1.26  riastrad 			/*
   4787  1.26  riastrad 			 * Make sure result blocks for higher latency levels are
   4788  1.26  riastrad 			 * atleast as high as level below the current level.
   4789  1.26  riastrad 			 * Assumption in DDB algorithm optimization for special
   4790  1.26  riastrad 			 * cases. Also covers Display WA #1125 for RC.
   4791  1.26  riastrad 			 */
   4792  1.26  riastrad 			if (result_prev->plane_res_b > res_blocks)
   4793  1.26  riastrad 				res_blocks = result_prev->plane_res_b;
   4794  1.26  riastrad 		}
   4795  1.26  riastrad 	}
   4796   1.3  riastrad 
   4797  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 11) {
   4798  1.26  riastrad 		if (wp->y_tiled) {
   4799  1.26  riastrad 			int extra_lines;
   4800   1.1  riastrad 
   4801  1.26  riastrad 			if (res_lines % wp->y_min_scanlines == 0)
   4802  1.26  riastrad 				extra_lines = wp->y_min_scanlines;
   4803  1.26  riastrad 			else
   4804  1.26  riastrad 				extra_lines = wp->y_min_scanlines * 2 -
   4805  1.26  riastrad 					res_lines % wp->y_min_scanlines;
   4806   1.1  riastrad 
   4807  1.26  riastrad 			min_ddb_alloc = mul_round_up_u32_fixed16(res_lines + extra_lines,
   4808  1.26  riastrad 								 wp->plane_blocks_per_line);
   4809  1.26  riastrad 		} else {
   4810  1.26  riastrad 			min_ddb_alloc = res_blocks +
   4811  1.26  riastrad 				DIV_ROUND_UP(res_blocks, 10);
   4812  1.26  riastrad 		}
   4813  1.26  riastrad 	}
   4814   1.1  riastrad 
   4815  1.26  riastrad 	if (!skl_wm_has_lines(dev_priv, level))
   4816  1.26  riastrad 		res_lines = 0;
   4817   1.1  riastrad 
   4818  1.26  riastrad 	if (res_lines > 31) {
   4819  1.26  riastrad 		/* reject it */
   4820  1.26  riastrad 		result->min_ddb_alloc = U16_MAX;
   4821  1.26  riastrad 		return;
   4822  1.26  riastrad 	}
   4823   1.1  riastrad 
   4824  1.26  riastrad 	/*
   4825  1.26  riastrad 	 * If res_lines is valid, assume we can use this watermark level
   4826  1.26  riastrad 	 * for now.  We'll come back and disable it after we calculate the
   4827  1.26  riastrad 	 * DDB allocation if it turns out we don't actually have enough
   4828  1.26  riastrad 	 * blocks to satisfy it.
   4829  1.26  riastrad 	 */
   4830  1.26  riastrad 	result->plane_res_b = res_blocks;
   4831  1.26  riastrad 	result->plane_res_l = res_lines;
   4832  1.26  riastrad 	/* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
   4833  1.26  riastrad 	result->min_ddb_alloc = max(min_ddb_alloc, res_blocks) + 1;
   4834  1.26  riastrad 	result->plane_en = true;
   4835  1.26  riastrad }
   4836   1.1  riastrad 
   4837  1.26  riastrad static void
   4838  1.26  riastrad skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
   4839  1.26  riastrad 		      const struct skl_wm_params *wm_params,
   4840  1.26  riastrad 		      struct skl_wm_level *levels)
   4841  1.26  riastrad {
   4842  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   4843  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   4844  1.26  riastrad 	struct skl_wm_level *result_prev = &levels[0];
   4845   1.1  riastrad 
   4846  1.26  riastrad 	for (level = 0; level <= max_level; level++) {
   4847  1.26  riastrad 		struct skl_wm_level *result = &levels[level];
   4848  1.10  riastrad 
   4849  1.26  riastrad 		skl_compute_plane_wm(crtc_state, level, wm_params,
   4850  1.26  riastrad 				     result_prev, result);
   4851   1.1  riastrad 
   4852  1.26  riastrad 		result_prev = result;
   4853  1.26  riastrad 	}
   4854  1.10  riastrad }
   4855   1.1  riastrad 
   4856  1.26  riastrad static u32
   4857  1.26  riastrad skl_compute_linetime_wm(const struct intel_crtc_state *crtc_state)
   4858  1.10  riastrad {
   4859  1.26  riastrad 	struct drm_atomic_state *state = crtc_state->uapi.state;
   4860  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->dev);
   4861  1.26  riastrad 	uint_fixed_16_16_t linetime_us;
   4862  1.26  riastrad 	u32 linetime_wm;
   4863   1.1  riastrad 
   4864  1.26  riastrad 	linetime_us = intel_get_linetime_us(crtc_state);
   4865  1.26  riastrad 	linetime_wm = fixed16_to_u32_round_up(mul_u32_fixed16(8, linetime_us));
   4866   1.1  riastrad 
   4867  1.26  riastrad 	/* Display WA #1135: BXT:ALL GLK:ALL */
   4868  1.26  riastrad 	if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
   4869  1.26  riastrad 		linetime_wm /= 2;
   4870   1.1  riastrad 
   4871  1.26  riastrad 	return linetime_wm;
   4872  1.26  riastrad }
   4873   1.1  riastrad 
   4874  1.26  riastrad static void skl_compute_transition_wm(const struct intel_crtc_state *crtc_state,
   4875  1.26  riastrad 				      const struct skl_wm_params *wp,
   4876  1.26  riastrad 				      struct skl_plane_wm *wm)
   4877  1.26  riastrad {
   4878  1.26  riastrad 	struct drm_device *dev = crtc_state->uapi.crtc->dev;
   4879  1.26  riastrad 	const struct drm_i915_private *dev_priv = to_i915(dev);
   4880  1.26  riastrad 	u16 trans_min, trans_y_tile_min;
   4881  1.26  riastrad 	const u16 trans_amount = 10; /* This is configurable amount */
   4882  1.26  riastrad 	u16 wm0_sel_res_b, trans_offset_b, res_blocks;
   4883   1.1  riastrad 
   4884  1.26  riastrad 	/* Transition WM are not recommended by HW team for GEN9 */
   4885  1.26  riastrad 	if (INTEL_GEN(dev_priv) <= 9)
   4886  1.26  riastrad 		return;
   4887   1.1  riastrad 
   4888  1.26  riastrad 	/* Transition WM don't make any sense if ipc is disabled */
   4889  1.26  riastrad 	if (!dev_priv->ipc_enabled)
   4890  1.26  riastrad 		return;
   4891   1.1  riastrad 
   4892  1.26  riastrad 	trans_min = 14;
   4893  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 11)
   4894  1.26  riastrad 		trans_min = 4;
   4895  1.26  riastrad 
   4896  1.26  riastrad 	trans_offset_b = trans_min + trans_amount;
   4897  1.26  riastrad 
   4898  1.26  riastrad 	/*
   4899  1.26  riastrad 	 * The spec asks for Selected Result Blocks for wm0 (the real value),
   4900  1.26  riastrad 	 * not Result Blocks (the integer value). Pay attention to the capital
   4901  1.26  riastrad 	 * letters. The value wm_l0->plane_res_b is actually Result Blocks, but
   4902  1.26  riastrad 	 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
   4903  1.26  riastrad 	 * and since we later will have to get the ceiling of the sum in the
   4904  1.26  riastrad 	 * transition watermarks calculation, we can just pretend Selected
   4905  1.26  riastrad 	 * Result Blocks is Result Blocks minus 1 and it should work for the
   4906  1.26  riastrad 	 * current platforms.
   4907  1.26  riastrad 	 */
   4908  1.26  riastrad 	wm0_sel_res_b = wm->wm[0].plane_res_b - 1;
   4909  1.26  riastrad 
   4910  1.26  riastrad 	if (wp->y_tiled) {
   4911  1.26  riastrad 		trans_y_tile_min =
   4912  1.26  riastrad 			(u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
   4913  1.26  riastrad 		res_blocks = max(wm0_sel_res_b, trans_y_tile_min) +
   4914  1.26  riastrad 				trans_offset_b;
   4915  1.26  riastrad 	} else {
   4916  1.26  riastrad 		res_blocks = wm0_sel_res_b + trans_offset_b;
   4917   1.1  riastrad 
   4918  1.26  riastrad 		/* WA BUG:1938466 add one block for non y-tile planes */
   4919  1.26  riastrad 		if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_A0))
   4920  1.26  riastrad 			res_blocks += 1;
   4921   1.1  riastrad 
   4922   1.1  riastrad 	}
   4923   1.1  riastrad 
   4924  1.26  riastrad 	/*
   4925  1.26  riastrad 	 * Just assume we can enable the transition watermark.  After
   4926  1.26  riastrad 	 * computing the DDB we'll come back and disable it if that
   4927  1.26  riastrad 	 * assumption turns out to be false.
   4928  1.26  riastrad 	 */
   4929  1.26  riastrad 	wm->trans_wm.plane_res_b = res_blocks + 1;
   4930  1.26  riastrad 	wm->trans_wm.plane_en = true;
   4931  1.26  riastrad }
   4932   1.1  riastrad 
   4933  1.26  riastrad static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
   4934  1.26  riastrad 				     const struct intel_plane_state *plane_state,
   4935  1.26  riastrad 				     enum plane_id plane_id, int color_plane)
   4936  1.26  riastrad {
   4937  1.26  riastrad 	struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
   4938  1.26  riastrad 	struct skl_wm_params wm_params;
   4939  1.26  riastrad 	int ret;
   4940   1.1  riastrad 
   4941  1.26  riastrad 	ret = skl_compute_plane_wm_params(crtc_state, plane_state,
   4942  1.26  riastrad 					  &wm_params, color_plane);
   4943  1.26  riastrad 	if (ret)
   4944  1.26  riastrad 		return ret;
   4945   1.3  riastrad 
   4946  1.26  riastrad 	skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
   4947  1.26  riastrad 	skl_compute_transition_wm(crtc_state, &wm_params, wm);
   4948   1.1  riastrad 
   4949  1.26  riastrad 	return 0;
   4950  1.26  riastrad }
   4951   1.1  riastrad 
   4952  1.26  riastrad static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
   4953  1.26  riastrad 				 const struct intel_plane_state *plane_state,
   4954  1.26  riastrad 				 enum plane_id plane_id)
   4955  1.26  riastrad {
   4956  1.26  riastrad 	struct skl_plane_wm *wm = &crtc_state->wm.skl.optimal.planes[plane_id];
   4957  1.26  riastrad 	struct skl_wm_params wm_params;
   4958  1.26  riastrad 	int ret;
   4959   1.1  riastrad 
   4960  1.26  riastrad 	wm->is_planar = true;
   4961   1.1  riastrad 
   4962  1.26  riastrad 	/* uv plane watermarks must also be validated for NV12/Planar */
   4963  1.26  riastrad 	ret = skl_compute_plane_wm_params(crtc_state, plane_state,
   4964  1.26  riastrad 					  &wm_params, 1);
   4965  1.26  riastrad 	if (ret)
   4966  1.26  riastrad 		return ret;
   4967   1.1  riastrad 
   4968  1.26  riastrad 	skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
   4969   1.1  riastrad 
   4970  1.26  riastrad 	return 0;
   4971  1.26  riastrad }
   4972   1.1  riastrad 
   4973  1.26  riastrad static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
   4974  1.26  riastrad 			      const struct intel_plane_state *plane_state)
   4975  1.26  riastrad {
   4976  1.26  riastrad 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
   4977  1.26  riastrad 	const struct drm_framebuffer *fb = plane_state->hw.fb;
   4978  1.26  riastrad 	enum plane_id plane_id = plane->id;
   4979  1.26  riastrad 	int ret;
   4980   1.1  riastrad 
   4981  1.26  riastrad 	if (!intel_wm_plane_visible(crtc_state, plane_state))
   4982  1.26  riastrad 		return 0;
   4983   1.1  riastrad 
   4984  1.26  riastrad 	ret = skl_build_plane_wm_single(crtc_state, plane_state,
   4985  1.26  riastrad 					plane_id, 0);
   4986  1.10  riastrad 	if (ret)
   4987  1.26  riastrad 		return ret;
   4988   1.1  riastrad 
   4989  1.26  riastrad 	if (fb->format->is_yuv && fb->format->num_planes > 1) {
   4990  1.26  riastrad 		ret = skl_build_plane_wm_uv(crtc_state, plane_state,
   4991  1.26  riastrad 					    plane_id);
   4992  1.10  riastrad 		if (ret)
   4993  1.26  riastrad 			return ret;
   4994  1.10  riastrad 	}
   4995   1.1  riastrad 
   4996  1.26  riastrad 	return 0;
   4997   1.1  riastrad }
   4998   1.1  riastrad 
   4999  1.26  riastrad static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
   5000  1.26  riastrad 			      const struct intel_plane_state *plane_state)
   5001   1.1  riastrad {
   5002  1.26  riastrad 	enum plane_id plane_id = to_intel_plane(plane_state->uapi.plane)->id;
   5003  1.26  riastrad 	int ret;
   5004   1.1  riastrad 
   5005  1.26  riastrad 	/* Watermarks calculated in master */
   5006  1.26  riastrad 	if (plane_state->planar_slave)
   5007  1.26  riastrad 		return 0;
   5008   1.1  riastrad 
   5009  1.26  riastrad 	if (plane_state->planar_linked_plane) {
   5010  1.26  riastrad 		const struct drm_framebuffer *fb = plane_state->hw.fb;
   5011  1.26  riastrad 		enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
   5012  1.26  riastrad 
   5013  1.26  riastrad 		WARN_ON(!intel_wm_plane_visible(crtc_state, plane_state));
   5014  1.26  riastrad 		WARN_ON(!fb->format->is_yuv ||
   5015  1.26  riastrad 			fb->format->num_planes == 1);
   5016   1.1  riastrad 
   5017  1.26  riastrad 		ret = skl_build_plane_wm_single(crtc_state, plane_state,
   5018  1.26  riastrad 						y_plane_id, 0);
   5019  1.26  riastrad 		if (ret)
   5020  1.26  riastrad 			return ret;
   5021   1.1  riastrad 
   5022  1.26  riastrad 		ret = skl_build_plane_wm_single(crtc_state, plane_state,
   5023  1.26  riastrad 						plane_id, 1);
   5024  1.26  riastrad 		if (ret)
   5025  1.26  riastrad 			return ret;
   5026  1.26  riastrad 	} else if (intel_wm_plane_visible(crtc_state, plane_state)) {
   5027  1.26  riastrad 		ret = skl_build_plane_wm_single(crtc_state, plane_state,
   5028  1.26  riastrad 						plane_id, 0);
   5029  1.26  riastrad 		if (ret)
   5030  1.26  riastrad 			return ret;
   5031  1.10  riastrad 	}
   5032   1.1  riastrad 
   5033  1.26  riastrad 	return 0;
   5034   1.1  riastrad }
   5035   1.1  riastrad 
   5036  1.26  riastrad static int skl_build_pipe_wm(struct intel_crtc_state *crtc_state)
   5037   1.1  riastrad {
   5038  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
   5039  1.26  riastrad 	struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
   5040  1.26  riastrad 	struct intel_plane *plane;
   5041  1.26  riastrad 	const struct intel_plane_state *plane_state;
   5042  1.26  riastrad 	int ret;
   5043  1.26  riastrad 
   5044  1.26  riastrad 	/*
   5045  1.26  riastrad 	 * We'll only calculate watermarks for planes that are actually
   5046  1.26  riastrad 	 * enabled, so make sure all other planes are set as disabled.
   5047  1.26  riastrad 	 */
   5048  1.26  riastrad 	memset(pipe_wm->planes, 0, sizeof(pipe_wm->planes));
   5049  1.10  riastrad 
   5050  1.26  riastrad 	intel_atomic_crtc_state_for_each_plane_state(plane, plane_state,
   5051  1.26  riastrad 						     crtc_state) {
   5052   1.1  riastrad 
   5053  1.26  riastrad 		if (INTEL_GEN(dev_priv) >= 11)
   5054  1.26  riastrad 			ret = icl_build_plane_wm(crtc_state, plane_state);
   5055  1.26  riastrad 		else
   5056  1.26  riastrad 			ret = skl_build_plane_wm(crtc_state, plane_state);
   5057  1.26  riastrad 		if (ret)
   5058  1.26  riastrad 			return ret;
   5059   1.1  riastrad 	}
   5060   1.1  riastrad 
   5061  1.26  riastrad 	pipe_wm->linetime = skl_compute_linetime_wm(crtc_state);
   5062   1.1  riastrad 
   5063  1.26  riastrad 	return 0;
   5064   1.1  riastrad }
   5065   1.1  riastrad 
   5066  1.26  riastrad static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
   5067  1.26  riastrad 				i915_reg_t reg,
   5068  1.26  riastrad 				const struct skl_ddb_entry *entry)
   5069   1.1  riastrad {
   5070  1.26  riastrad 	if (entry->end)
   5071  1.26  riastrad 		I915_WRITE_FW(reg, (entry->end - 1) << 16 | entry->start);
   5072  1.26  riastrad 	else
   5073  1.26  riastrad 		I915_WRITE_FW(reg, 0);
   5074  1.10  riastrad }
   5075   1.1  riastrad 
   5076  1.26  riastrad static void skl_write_wm_level(struct drm_i915_private *dev_priv,
   5077  1.26  riastrad 			       i915_reg_t reg,
   5078  1.26  riastrad 			       const struct skl_wm_level *level)
   5079  1.10  riastrad {
   5080  1.26  riastrad 	u32 val = 0;
   5081   1.1  riastrad 
   5082  1.26  riastrad 	if (level->plane_en)
   5083  1.26  riastrad 		val |= PLANE_WM_EN;
   5084  1.26  riastrad 	if (level->ignore_lines)
   5085  1.26  riastrad 		val |= PLANE_WM_IGNORE_LINES;
   5086  1.26  riastrad 	val |= level->plane_res_b;
   5087  1.26  riastrad 	val |= level->plane_res_l << PLANE_WM_LINES_SHIFT;
   5088   1.1  riastrad 
   5089  1.26  riastrad 	I915_WRITE_FW(reg, val);
   5090   1.1  riastrad }
   5091   1.1  riastrad 
   5092  1.26  riastrad void skl_write_plane_wm(struct intel_plane *plane,
   5093  1.26  riastrad 			const struct intel_crtc_state *crtc_state)
   5094   1.1  riastrad {
   5095  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
   5096  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   5097  1.26  riastrad 	enum plane_id plane_id = plane->id;
   5098  1.26  riastrad 	enum pipe pipe = plane->pipe;
   5099  1.26  riastrad 	const struct skl_plane_wm *wm =
   5100  1.26  riastrad 		&crtc_state->wm.skl.optimal.planes[plane_id];
   5101  1.26  riastrad 	const struct skl_ddb_entry *ddb_y =
   5102  1.26  riastrad 		&crtc_state->wm.skl.plane_ddb_y[plane_id];
   5103  1.26  riastrad 	const struct skl_ddb_entry *ddb_uv =
   5104  1.26  riastrad 		&crtc_state->wm.skl.plane_ddb_uv[plane_id];
   5105  1.26  riastrad 
   5106  1.26  riastrad 	for (level = 0; level <= max_level; level++) {
   5107  1.26  riastrad 		skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
   5108  1.26  riastrad 				   &wm->wm[level]);
   5109  1.26  riastrad 	}
   5110  1.26  riastrad 	skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
   5111  1.26  riastrad 			   &wm->trans_wm);
   5112   1.1  riastrad 
   5113  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 11) {
   5114  1.26  riastrad 		skl_ddb_entry_write(dev_priv,
   5115  1.26  riastrad 				    PLANE_BUF_CFG(pipe, plane_id), ddb_y);
   5116  1.26  riastrad 		return;
   5117  1.26  riastrad 	}
   5118   1.1  riastrad 
   5119  1.26  riastrad 	if (wm->is_planar)
   5120  1.26  riastrad 		swap(ddb_y, ddb_uv);
   5121   1.1  riastrad 
   5122  1.26  riastrad 	skl_ddb_entry_write(dev_priv,
   5123  1.26  riastrad 			    PLANE_BUF_CFG(pipe, plane_id), ddb_y);
   5124  1.26  riastrad 	skl_ddb_entry_write(dev_priv,
   5125  1.26  riastrad 			    PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
   5126   1.1  riastrad }
   5127   1.1  riastrad 
   5128  1.26  riastrad void skl_write_cursor_wm(struct intel_plane *plane,
   5129  1.26  riastrad 			 const struct intel_crtc_state *crtc_state)
   5130   1.1  riastrad {
   5131  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
   5132  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   5133  1.26  riastrad 	enum plane_id plane_id = plane->id;
   5134  1.26  riastrad 	enum pipe pipe = plane->pipe;
   5135  1.26  riastrad 	const struct skl_plane_wm *wm =
   5136  1.26  riastrad 		&crtc_state->wm.skl.optimal.planes[plane_id];
   5137  1.26  riastrad 	const struct skl_ddb_entry *ddb =
   5138  1.26  riastrad 		&crtc_state->wm.skl.plane_ddb_y[plane_id];
   5139  1.10  riastrad 
   5140  1.26  riastrad 	for (level = 0; level <= max_level; level++) {
   5141  1.26  riastrad 		skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
   5142  1.26  riastrad 				   &wm->wm[level]);
   5143  1.26  riastrad 	}
   5144  1.26  riastrad 	skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe), &wm->trans_wm);
   5145  1.10  riastrad 
   5146  1.26  riastrad 	skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
   5147  1.26  riastrad }
   5148   1.1  riastrad 
   5149  1.26  riastrad bool skl_wm_level_equals(const struct skl_wm_level *l1,
   5150  1.26  riastrad 			 const struct skl_wm_level *l2)
   5151  1.26  riastrad {
   5152  1.26  riastrad 	return l1->plane_en == l2->plane_en &&
   5153  1.26  riastrad 		l1->ignore_lines == l2->ignore_lines &&
   5154  1.26  riastrad 		l1->plane_res_l == l2->plane_res_l &&
   5155  1.26  riastrad 		l1->plane_res_b == l2->plane_res_b;
   5156  1.10  riastrad }
   5157   1.1  riastrad 
   5158  1.26  riastrad static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
   5159  1.26  riastrad 				const struct skl_plane_wm *wm1,
   5160  1.26  riastrad 				const struct skl_plane_wm *wm2)
   5161  1.10  riastrad {
   5162  1.26  riastrad 	int level, max_level = ilk_wm_max_level(dev_priv);
   5163   1.1  riastrad 
   5164  1.26  riastrad 	for (level = 0; level <= max_level; level++) {
   5165  1.26  riastrad 		if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]) ||
   5166  1.26  riastrad 		    !skl_wm_level_equals(&wm1->uv_wm[level], &wm2->uv_wm[level]))
   5167  1.26  riastrad 			return false;
   5168  1.26  riastrad 	}
   5169   1.1  riastrad 
   5170  1.26  riastrad 	return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm);
   5171  1.10  riastrad }
   5172   1.1  riastrad 
   5173  1.26  riastrad static bool skl_pipe_wm_equals(struct intel_crtc *crtc,
   5174  1.26  riastrad 			       const struct skl_pipe_wm *wm1,
   5175  1.26  riastrad 			       const struct skl_pipe_wm *wm2)
   5176  1.10  riastrad {
   5177  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   5178  1.26  riastrad 	enum plane_id plane_id;
   5179  1.26  riastrad 
   5180  1.26  riastrad 	for_each_plane_id_on_crtc(crtc, plane_id) {
   5181  1.26  riastrad 		if (!skl_plane_wm_equals(dev_priv,
   5182  1.26  riastrad 					 &wm1->planes[plane_id],
   5183  1.26  riastrad 					 &wm2->planes[plane_id]))
   5184  1.26  riastrad 			return false;
   5185  1.26  riastrad 	}
   5186  1.26  riastrad 
   5187  1.26  riastrad 	return wm1->linetime == wm2->linetime;
   5188   1.1  riastrad }
   5189   1.1  riastrad 
   5190  1.26  riastrad static inline bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
   5191  1.26  riastrad 					   const struct skl_ddb_entry *b)
   5192   1.1  riastrad {
   5193  1.26  riastrad 	return a->start < b->end && b->start < a->end;
   5194   1.1  riastrad }
   5195   1.1  riastrad 
   5196  1.26  riastrad bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
   5197  1.26  riastrad 				 const struct skl_ddb_entry *entries,
   5198  1.26  riastrad 				 int num_entries, int ignore_idx)
   5199  1.26  riastrad {
   5200  1.26  riastrad 	int i;
   5201  1.10  riastrad 
   5202  1.26  riastrad 	for (i = 0; i < num_entries; i++) {
   5203  1.26  riastrad 		if (i != ignore_idx &&
   5204  1.26  riastrad 		    skl_ddb_entries_overlap(ddb, &entries[i]))
   5205  1.26  riastrad 			return true;
   5206  1.26  riastrad 	}
   5207   1.1  riastrad 
   5208  1.26  riastrad 	return false;
   5209   1.1  riastrad }
   5210   1.1  riastrad 
   5211  1.26  riastrad static int
   5212  1.26  riastrad skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
   5213  1.26  riastrad 			    struct intel_crtc_state *new_crtc_state)
   5214   1.1  riastrad {
   5215  1.26  riastrad 	struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
   5216  1.26  riastrad 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
   5217  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   5218  1.26  riastrad 	struct intel_plane *plane;
   5219  1.10  riastrad 
   5220  1.26  riastrad 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
   5221  1.26  riastrad 		struct intel_plane_state *plane_state;
   5222  1.26  riastrad 		enum plane_id plane_id = plane->id;
   5223  1.26  riastrad 
   5224  1.26  riastrad 		if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
   5225  1.26  riastrad 					&new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
   5226  1.26  riastrad 		    skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
   5227  1.26  riastrad 					&new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
   5228  1.26  riastrad 			continue;
   5229  1.10  riastrad 
   5230  1.26  riastrad 		plane_state = intel_atomic_get_plane_state(state, plane);
   5231  1.26  riastrad 		if (IS_ERR(plane_state))
   5232  1.26  riastrad 			return PTR_ERR(plane_state);
   5233  1.10  riastrad 
   5234  1.26  riastrad 		new_crtc_state->update_planes |= BIT(plane_id);
   5235  1.10  riastrad 	}
   5236  1.10  riastrad 
   5237  1.26  riastrad 	return 0;
   5238   1.1  riastrad }
   5239   1.3  riastrad 
   5240  1.26  riastrad static int
   5241  1.26  riastrad skl_compute_ddb(struct intel_atomic_state *state)
   5242   1.1  riastrad {
   5243  1.26  riastrad 	const struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   5244  1.26  riastrad 	struct skl_ddb_allocation *ddb = &state->wm_results.ddb;
   5245  1.26  riastrad 	struct intel_crtc_state *old_crtc_state;
   5246  1.26  riastrad 	struct intel_crtc_state *new_crtc_state;
   5247  1.26  riastrad 	struct intel_crtc *crtc;
   5248  1.26  riastrad 	int ret, i;
   5249   1.1  riastrad 
   5250  1.26  riastrad 	memcpy(ddb, &dev_priv->wm.skl_hw.ddb, sizeof(*ddb));
   5251   1.1  riastrad 
   5252  1.26  riastrad 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
   5253  1.26  riastrad 					    new_crtc_state, i) {
   5254  1.26  riastrad 		ret = skl_allocate_pipe_ddb(new_crtc_state, ddb);
   5255  1.26  riastrad 		if (ret)
   5256  1.26  riastrad 			return ret;
   5257   1.1  riastrad 
   5258  1.26  riastrad 		ret = skl_ddb_add_affected_planes(old_crtc_state,
   5259  1.26  riastrad 						  new_crtc_state);
   5260  1.26  riastrad 		if (ret)
   5261  1.26  riastrad 			return ret;
   5262  1.10  riastrad 	}
   5263   1.1  riastrad 
   5264  1.26  riastrad 	return 0;
   5265  1.26  riastrad }
   5266   1.1  riastrad 
   5267  1.26  riastrad static char enast(bool enable)
   5268  1.26  riastrad {
   5269  1.26  riastrad 	return enable ? '*' : ' ';
   5270   1.1  riastrad }
   5271   1.1  riastrad 
   5272  1.26  riastrad static void
   5273  1.26  riastrad skl_print_wm_changes(struct intel_atomic_state *state)
   5274   1.3  riastrad {
   5275  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   5276  1.26  riastrad 	const struct intel_crtc_state *old_crtc_state;
   5277  1.26  riastrad 	const struct intel_crtc_state *new_crtc_state;
   5278  1.26  riastrad 	struct intel_plane *plane;
   5279  1.26  riastrad 	struct intel_crtc *crtc;
   5280  1.26  riastrad 	int i;
   5281  1.10  riastrad 
   5282  1.26  riastrad 	if (!drm_debug_enabled(DRM_UT_KMS))
   5283  1.10  riastrad 		return;
   5284   1.3  riastrad 
   5285  1.26  riastrad 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
   5286  1.26  riastrad 					    new_crtc_state, i) {
   5287  1.26  riastrad 		const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
   5288  1.26  riastrad 
   5289  1.26  riastrad 		old_pipe_wm = &old_crtc_state->wm.skl.optimal;
   5290  1.26  riastrad 		new_pipe_wm = &new_crtc_state->wm.skl.optimal;
   5291  1.26  riastrad 
   5292  1.26  riastrad 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
   5293  1.26  riastrad 			enum plane_id plane_id = plane->id;
   5294  1.26  riastrad 			const struct skl_ddb_entry *old, *new;
   5295  1.26  riastrad 
   5296  1.26  riastrad 			old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
   5297  1.26  riastrad 			new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
   5298  1.26  riastrad 
   5299  1.26  riastrad 			if (skl_ddb_entry_equal(old, new))
   5300  1.26  riastrad 				continue;
   5301  1.26  riastrad 
   5302  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   5303  1.26  riastrad 				    "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
   5304  1.26  riastrad 				    plane->base.base.id, plane->base.name,
   5305  1.26  riastrad 				    old->start, old->end, new->start, new->end,
   5306  1.26  riastrad 				    skl_ddb_entry_size(old), skl_ddb_entry_size(new));
   5307  1.26  riastrad 		}
   5308  1.26  riastrad 
   5309  1.26  riastrad 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
   5310  1.26  riastrad 			enum plane_id plane_id = plane->id;
   5311  1.26  riastrad 			const struct skl_plane_wm *old_wm, *new_wm;
   5312  1.26  riastrad 
   5313  1.26  riastrad 			old_wm = &old_pipe_wm->planes[plane_id];
   5314  1.26  riastrad 			new_wm = &new_pipe_wm->planes[plane_id];
   5315  1.26  riastrad 
   5316  1.26  riastrad 			if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
   5317  1.26  riastrad 				continue;
   5318  1.26  riastrad 
   5319  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   5320  1.26  riastrad 				    "[PLANE:%d:%s]   level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm"
   5321  1.26  riastrad 				    " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm\n",
   5322  1.26  riastrad 				    plane->base.base.id, plane->base.name,
   5323  1.26  riastrad 				    enast(old_wm->wm[0].plane_en), enast(old_wm->wm[1].plane_en),
   5324  1.26  riastrad 				    enast(old_wm->wm[2].plane_en), enast(old_wm->wm[3].plane_en),
   5325  1.26  riastrad 				    enast(old_wm->wm[4].plane_en), enast(old_wm->wm[5].plane_en),
   5326  1.26  riastrad 				    enast(old_wm->wm[6].plane_en), enast(old_wm->wm[7].plane_en),
   5327  1.26  riastrad 				    enast(old_wm->trans_wm.plane_en),
   5328  1.26  riastrad 				    enast(new_wm->wm[0].plane_en), enast(new_wm->wm[1].plane_en),
   5329  1.26  riastrad 				    enast(new_wm->wm[2].plane_en), enast(new_wm->wm[3].plane_en),
   5330  1.26  riastrad 				    enast(new_wm->wm[4].plane_en), enast(new_wm->wm[5].plane_en),
   5331  1.26  riastrad 				    enast(new_wm->wm[6].plane_en), enast(new_wm->wm[7].plane_en),
   5332  1.26  riastrad 				    enast(new_wm->trans_wm.plane_en));
   5333  1.26  riastrad 
   5334  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   5335  1.26  riastrad 				    "[PLANE:%d:%s]   lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d"
   5336  1.26  riastrad 				      " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d\n",
   5337  1.26  riastrad 				    plane->base.base.id, plane->base.name,
   5338  1.26  riastrad 				    enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].plane_res_l,
   5339  1.26  riastrad 				    enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].plane_res_l,
   5340  1.26  riastrad 				    enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].plane_res_l,
   5341  1.26  riastrad 				    enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].plane_res_l,
   5342  1.26  riastrad 				    enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].plane_res_l,
   5343  1.26  riastrad 				    enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].plane_res_l,
   5344  1.26  riastrad 				    enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].plane_res_l,
   5345  1.26  riastrad 				    enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].plane_res_l,
   5346  1.26  riastrad 				    enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.plane_res_l,
   5347  1.26  riastrad 
   5348  1.26  riastrad 				    enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].plane_res_l,
   5349  1.26  riastrad 				    enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].plane_res_l,
   5350  1.26  riastrad 				    enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].plane_res_l,
   5351  1.26  riastrad 				    enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].plane_res_l,
   5352  1.26  riastrad 				    enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].plane_res_l,
   5353  1.26  riastrad 				    enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].plane_res_l,
   5354  1.26  riastrad 				    enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].plane_res_l,
   5355  1.26  riastrad 				    enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].plane_res_l,
   5356  1.26  riastrad 				    enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.plane_res_l);
   5357  1.26  riastrad 
   5358  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   5359  1.26  riastrad 				    "[PLANE:%d:%s]  blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
   5360  1.26  riastrad 				    " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
   5361  1.26  riastrad 				    plane->base.base.id, plane->base.name,
   5362  1.26  riastrad 				    old_wm->wm[0].plane_res_b, old_wm->wm[1].plane_res_b,
   5363  1.26  riastrad 				    old_wm->wm[2].plane_res_b, old_wm->wm[3].plane_res_b,
   5364  1.26  riastrad 				    old_wm->wm[4].plane_res_b, old_wm->wm[5].plane_res_b,
   5365  1.26  riastrad 				    old_wm->wm[6].plane_res_b, old_wm->wm[7].plane_res_b,
   5366  1.26  riastrad 				    old_wm->trans_wm.plane_res_b,
   5367  1.26  riastrad 				    new_wm->wm[0].plane_res_b, new_wm->wm[1].plane_res_b,
   5368  1.26  riastrad 				    new_wm->wm[2].plane_res_b, new_wm->wm[3].plane_res_b,
   5369  1.26  riastrad 				    new_wm->wm[4].plane_res_b, new_wm->wm[5].plane_res_b,
   5370  1.26  riastrad 				    new_wm->wm[6].plane_res_b, new_wm->wm[7].plane_res_b,
   5371  1.26  riastrad 				    new_wm->trans_wm.plane_res_b);
   5372  1.26  riastrad 
   5373  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   5374  1.26  riastrad 				    "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d"
   5375  1.26  riastrad 				    " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d\n",
   5376  1.26  riastrad 				    plane->base.base.id, plane->base.name,
   5377  1.26  riastrad 				    old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
   5378  1.26  riastrad 				    old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
   5379  1.26  riastrad 				    old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
   5380  1.26  riastrad 				    old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
   5381  1.26  riastrad 				    old_wm->trans_wm.min_ddb_alloc,
   5382  1.26  riastrad 				    new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
   5383  1.26  riastrad 				    new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
   5384  1.26  riastrad 				    new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
   5385  1.26  riastrad 				    new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
   5386  1.26  riastrad 				    new_wm->trans_wm.min_ddb_alloc);
   5387  1.26  riastrad 		}
   5388  1.26  riastrad 	}
   5389   1.3  riastrad }
   5390   1.3  riastrad 
   5391  1.26  riastrad static int intel_add_all_pipes(struct intel_atomic_state *state)
   5392   1.1  riastrad {
   5393  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   5394  1.26  riastrad 	struct intel_crtc *crtc;
   5395  1.10  riastrad 
   5396  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   5397  1.26  riastrad 		struct intel_crtc_state *crtc_state;
   5398   1.1  riastrad 
   5399  1.26  riastrad 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
   5400  1.26  riastrad 		if (IS_ERR(crtc_state))
   5401  1.26  riastrad 			return PTR_ERR(crtc_state);
   5402   1.1  riastrad 	}
   5403  1.10  riastrad 
   5404  1.26  riastrad 	return 0;
   5405  1.26  riastrad }
   5406  1.10  riastrad 
   5407  1.26  riastrad static int
   5408  1.26  riastrad skl_ddb_add_affected_pipes(struct intel_atomic_state *state)
   5409  1.26  riastrad {
   5410  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
   5411  1.26  riastrad 	int ret;
   5412  1.10  riastrad 
   5413  1.26  riastrad 	/*
   5414  1.26  riastrad 	 * If this is our first atomic update following hardware readout,
   5415  1.26  riastrad 	 * we can't trust the DDB that the BIOS programmed for us.  Let's
   5416  1.26  riastrad 	 * pretend that all pipes switched active status so that we'll
   5417  1.26  riastrad 	 * ensure a full DDB recompute.
   5418  1.26  riastrad 	 */
   5419  1.26  riastrad 	if (dev_priv->wm.distrust_bios_wm) {
   5420  1.26  riastrad 		ret = drm_modeset_lock(&dev_priv->drm.mode_config.connection_mutex,
   5421  1.26  riastrad 				       state->base.acquire_ctx);
   5422  1.26  riastrad 		if (ret)
   5423  1.26  riastrad 			return ret;
   5424  1.10  riastrad 
   5425  1.26  riastrad 		state->active_pipe_changes = INTEL_INFO(dev_priv)->pipe_mask;
   5426  1.10  riastrad 
   5427  1.26  riastrad 		/*
   5428  1.26  riastrad 		 * We usually only initialize state->active_pipes if we
   5429  1.26  riastrad 		 * we're doing a modeset; make sure this field is always
   5430  1.26  riastrad 		 * initialized during the sanitization process that happens
   5431  1.26  riastrad 		 * on the first commit too.
   5432  1.26  riastrad 		 */
   5433  1.26  riastrad 		if (!state->modeset)
   5434  1.26  riastrad 			state->active_pipes = dev_priv->active_pipes;
   5435  1.26  riastrad 	}
   5436   1.1  riastrad 
   5437  1.26  riastrad 	/*
   5438  1.26  riastrad 	 * If the modeset changes which CRTC's are active, we need to
   5439  1.26  riastrad 	 * recompute the DDB allocation for *all* active pipes, even
   5440  1.26  riastrad 	 * those that weren't otherwise being modified in any way by this
   5441  1.26  riastrad 	 * atomic commit.  Due to the shrinking of the per-pipe allocations
   5442  1.26  riastrad 	 * when new active CRTC's are added, it's possible for a pipe that
   5443  1.26  riastrad 	 * we were already using and aren't changing at all here to suddenly
   5444  1.26  riastrad 	 * become invalid if its DDB needs exceeds its new allocation.
   5445  1.26  riastrad 	 *
   5446  1.26  riastrad 	 * Note that if we wind up doing a full DDB recompute, we can't let
   5447  1.26  riastrad 	 * any other display updates race with this transaction, so we need
   5448  1.26  riastrad 	 * to grab the lock on *all* CRTC's.
   5449  1.26  riastrad 	 */
   5450  1.26  riastrad 	if (state->active_pipe_changes || state->modeset) {
   5451  1.26  riastrad 		state->wm_results.dirty_pipes = INTEL_INFO(dev_priv)->pipe_mask;
   5452   1.1  riastrad 
   5453  1.26  riastrad 		ret = intel_add_all_pipes(state);
   5454  1.26  riastrad 		if (ret)
   5455  1.26  riastrad 			return ret;
   5456  1.26  riastrad 	}
   5457   1.3  riastrad 
   5458  1.26  riastrad 	return 0;
   5459   1.1  riastrad }
   5460   1.1  riastrad 
   5461  1.26  riastrad /*
   5462  1.26  riastrad  * To make sure the cursor watermark registers are always consistent
   5463  1.26  riastrad  * with our computed state the following scenario needs special
   5464  1.26  riastrad  * treatment:
   5465  1.26  riastrad  *
   5466  1.26  riastrad  * 1. enable cursor
   5467  1.26  riastrad  * 2. move cursor entirely offscreen
   5468  1.26  riastrad  * 3. disable cursor
   5469  1.26  riastrad  *
   5470  1.26  riastrad  * Step 2. does call .disable_plane() but does not zero the watermarks
   5471  1.26  riastrad  * (since we consider an offscreen cursor still active for the purposes
   5472  1.26  riastrad  * of watermarks). Step 3. would not normally call .disable_plane()
   5473  1.26  riastrad  * because the actual plane visibility isn't changing, and we don't
   5474  1.26  riastrad  * deallocate the cursor ddb until the pipe gets disabled. So we must
   5475  1.26  riastrad  * force step 3. to call .disable_plane() to update the watermark
   5476  1.26  riastrad  * registers properly.
   5477  1.26  riastrad  *
   5478  1.26  riastrad  * Other planes do not suffer from this issues as their watermarks are
   5479  1.26  riastrad  * calculated based on the actual plane visibility. The only time this
   5480  1.26  riastrad  * can trigger for the other planes is during the initial readout as the
   5481  1.26  riastrad  * default value of the watermarks registers is not zero.
   5482  1.26  riastrad  */
   5483  1.26  riastrad static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
   5484  1.26  riastrad 				      struct intel_crtc *crtc)
   5485   1.1  riastrad {
   5486  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   5487  1.26  riastrad 	const struct intel_crtc_state *old_crtc_state =
   5488  1.26  riastrad 		intel_atomic_get_old_crtc_state(state, crtc);
   5489  1.26  riastrad 	struct intel_crtc_state *new_crtc_state =
   5490  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   5491  1.26  riastrad 	struct intel_plane *plane;
   5492  1.10  riastrad 
   5493  1.26  riastrad 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
   5494  1.26  riastrad 		struct intel_plane_state *plane_state;
   5495  1.26  riastrad 		enum plane_id plane_id = plane->id;
   5496  1.10  riastrad 
   5497  1.26  riastrad 		/*
   5498  1.26  riastrad 		 * Force a full wm update for every plane on modeset.
   5499  1.26  riastrad 		 * Required because the reset value of the wm registers
   5500  1.26  riastrad 		 * is non-zero, whereas we want all disabled planes to
   5501  1.26  riastrad 		 * have zero watermarks. So if we turn off the relevant
   5502  1.26  riastrad 		 * power well the hardware state will go out of sync
   5503  1.26  riastrad 		 * with the software state.
   5504  1.26  riastrad 		 */
   5505  1.26  riastrad 		if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
   5506  1.26  riastrad 		    skl_plane_wm_equals(dev_priv,
   5507  1.26  riastrad 					&old_crtc_state->wm.skl.optimal.planes[plane_id],
   5508  1.26  riastrad 					&new_crtc_state->wm.skl.optimal.planes[plane_id]))
   5509  1.26  riastrad 			continue;
   5510  1.10  riastrad 
   5511  1.26  riastrad 		plane_state = intel_atomic_get_plane_state(state, plane);
   5512  1.26  riastrad 		if (IS_ERR(plane_state))
   5513  1.26  riastrad 			return PTR_ERR(plane_state);
   5514   1.1  riastrad 
   5515  1.26  riastrad 		new_crtc_state->update_planes |= BIT(plane_id);
   5516   1.1  riastrad 	}
   5517  1.10  riastrad 
   5518  1.26  riastrad 	return 0;
   5519  1.26  riastrad }
   5520  1.10  riastrad 
   5521  1.26  riastrad static int
   5522  1.26  riastrad skl_compute_wm(struct intel_atomic_state *state)
   5523  1.26  riastrad {
   5524  1.26  riastrad 	struct intel_crtc *crtc;
   5525  1.26  riastrad 	struct intel_crtc_state *new_crtc_state;
   5526  1.26  riastrad 	struct intel_crtc_state *old_crtc_state;
   5527  1.26  riastrad 	struct skl_ddb_values *results = &state->wm_results;
   5528  1.26  riastrad 	int ret, i;
   5529  1.10  riastrad 
   5530  1.26  riastrad 	/* Clear all dirty flags */
   5531  1.26  riastrad 	results->dirty_pipes = 0;
   5532  1.10  riastrad 
   5533  1.26  riastrad 	ret = skl_ddb_add_affected_pipes(state);
   5534  1.26  riastrad 	if (ret)
   5535  1.26  riastrad 		return ret;
   5536  1.10  riastrad 
   5537  1.26  riastrad 	/*
   5538  1.26  riastrad 	 * Calculate WM's for all pipes that are part of this transaction.
   5539  1.26  riastrad 	 * Note that skl_ddb_add_affected_pipes may have added more CRTC's that
   5540  1.26  riastrad 	 * weren't otherwise being modified (and set bits in dirty_pipes) if
   5541  1.26  riastrad 	 * pipe allocations had to change.
   5542  1.26  riastrad 	 */
   5543  1.26  riastrad 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
   5544  1.26  riastrad 					    new_crtc_state, i) {
   5545  1.26  riastrad 		ret = skl_build_pipe_wm(new_crtc_state);
   5546  1.26  riastrad 		if (ret)
   5547  1.26  riastrad 			return ret;
   5548  1.10  riastrad 
   5549  1.26  riastrad 		ret = skl_wm_add_affected_planes(state, crtc);
   5550  1.26  riastrad 		if (ret)
   5551  1.26  riastrad 			return ret;
   5552  1.10  riastrad 
   5553  1.26  riastrad 		if (!skl_pipe_wm_equals(crtc,
   5554  1.26  riastrad 					&old_crtc_state->wm.skl.optimal,
   5555  1.26  riastrad 					&new_crtc_state->wm.skl.optimal))
   5556  1.26  riastrad 			results->dirty_pipes |= BIT(crtc->pipe);
   5557  1.26  riastrad 	}
   5558  1.10  riastrad 
   5559  1.26  riastrad 	ret = skl_compute_ddb(state);
   5560  1.26  riastrad 	if (ret)
   5561  1.26  riastrad 		return ret;
   5562  1.10  riastrad 
   5563  1.26  riastrad 	skl_print_wm_changes(state);
   5564   1.1  riastrad 
   5565  1.26  riastrad 	return 0;
   5566   1.1  riastrad }
   5567   1.1  riastrad 
   5568  1.26  riastrad static void skl_atomic_update_crtc_wm(struct intel_atomic_state *state,
   5569  1.26  riastrad 				      struct intel_crtc *crtc)
   5570   1.3  riastrad {
   5571  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   5572  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   5573  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   5574  1.26  riastrad 	const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
   5575  1.26  riastrad 	enum pipe pipe = crtc->pipe;
   5576  1.10  riastrad 
   5577  1.26  riastrad 	if ((state->wm_results.dirty_pipes & BIT(crtc->pipe)) == 0)
   5578  1.26  riastrad 		return;
   5579   1.3  riastrad 
   5580  1.26  riastrad 	I915_WRITE(PIPE_WM_LINETIME(pipe), pipe_wm->linetime);
   5581  1.26  riastrad }
   5582   1.3  riastrad 
   5583  1.26  riastrad static void skl_initial_wm(struct intel_atomic_state *state,
   5584  1.26  riastrad 			   struct intel_crtc *crtc)
   5585  1.26  riastrad {
   5586  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   5587  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   5588  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   5589  1.26  riastrad 	struct skl_ddb_values *results = &state->wm_results;
   5590   1.3  riastrad 
   5591  1.26  riastrad 	if ((results->dirty_pipes & BIT(crtc->pipe)) == 0)
   5592  1.26  riastrad 		return;
   5593  1.10  riastrad 
   5594  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   5595  1.10  riastrad 
   5596  1.26  riastrad 	if (crtc_state->uapi.active_changed)
   5597  1.26  riastrad 		skl_atomic_update_crtc_wm(state, crtc);
   5598  1.10  riastrad 
   5599  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   5600  1.10  riastrad }
   5601  1.10  riastrad 
   5602  1.26  riastrad static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
   5603  1.26  riastrad 				  struct intel_wm_config *config)
   5604  1.10  riastrad {
   5605  1.26  riastrad 	struct intel_crtc *crtc;
   5606  1.10  riastrad 
   5607  1.26  riastrad 	/* Compute the currently _active_ config */
   5608  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   5609  1.26  riastrad 		const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
   5610  1.10  riastrad 
   5611  1.26  riastrad 		if (!wm->pipe_enabled)
   5612  1.26  riastrad 			continue;
   5613  1.10  riastrad 
   5614  1.26  riastrad 		config->sprites_enabled |= wm->sprites_enabled;
   5615  1.26  riastrad 		config->sprites_scaled |= wm->sprites_scaled;
   5616  1.26  riastrad 		config->num_pipes_active++;
   5617  1.10  riastrad 	}
   5618  1.26  riastrad }
   5619  1.10  riastrad 
   5620  1.26  riastrad static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
   5621  1.26  riastrad {
   5622  1.26  riastrad 	struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
   5623  1.26  riastrad 	struct ilk_wm_maximums max;
   5624  1.26  riastrad 	struct intel_wm_config config = {};
   5625  1.26  riastrad 	struct ilk_wm_values results = {};
   5626  1.26  riastrad 	enum intel_ddb_partitioning partitioning;
   5627  1.10  riastrad 
   5628  1.26  riastrad 	ilk_compute_wm_config(dev_priv, &config);
   5629  1.10  riastrad 
   5630  1.26  riastrad 	ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
   5631  1.26  riastrad 	ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
   5632  1.10  riastrad 
   5633  1.26  riastrad 	/* 5/6 split only in single pipe config on IVB+ */
   5634  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 7 &&
   5635  1.26  riastrad 	    config.num_pipes_active == 1 && config.sprites_enabled) {
   5636  1.26  riastrad 		ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
   5637  1.26  riastrad 		ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
   5638  1.10  riastrad 
   5639  1.26  riastrad 		best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
   5640  1.26  riastrad 	} else {
   5641  1.26  riastrad 		best_lp_wm = &lp_wm_1_2;
   5642  1.26  riastrad 	}
   5643  1.10  riastrad 
   5644  1.26  riastrad 	partitioning = (best_lp_wm == &lp_wm_1_2) ?
   5645  1.26  riastrad 		       INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
   5646  1.10  riastrad 
   5647  1.26  riastrad 	ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
   5648  1.10  riastrad 
   5649  1.26  riastrad 	ilk_write_wm_values(dev_priv, &results);
   5650  1.26  riastrad }
   5651  1.10  riastrad 
   5652  1.26  riastrad static void ilk_initial_watermarks(struct intel_atomic_state *state,
   5653  1.26  riastrad 				   struct intel_crtc *crtc)
   5654  1.26  riastrad {
   5655  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   5656  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   5657  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   5658  1.10  riastrad 
   5659  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   5660  1.26  riastrad 	crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
   5661  1.26  riastrad 	ilk_program_watermarks(dev_priv);
   5662  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   5663  1.26  riastrad }
   5664  1.10  riastrad 
   5665  1.26  riastrad static void ilk_optimize_watermarks(struct intel_atomic_state *state,
   5666  1.26  riastrad 				    struct intel_crtc *crtc)
   5667  1.26  riastrad {
   5668  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   5669  1.26  riastrad 	const struct intel_crtc_state *crtc_state =
   5670  1.26  riastrad 		intel_atomic_get_new_crtc_state(state, crtc);
   5671  1.10  riastrad 
   5672  1.26  riastrad 	if (!crtc_state->wm.need_postvbl_update)
   5673  1.26  riastrad 		return;
   5674  1.10  riastrad 
   5675  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   5676  1.26  riastrad 	crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
   5677  1.26  riastrad 	ilk_program_watermarks(dev_priv);
   5678  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   5679  1.10  riastrad }
   5680  1.10  riastrad 
   5681  1.26  riastrad static inline void skl_wm_level_from_reg_val(u32 val,
   5682  1.26  riastrad 					     struct skl_wm_level *level)
   5683  1.10  riastrad {
   5684  1.26  riastrad 	level->plane_en = val & PLANE_WM_EN;
   5685  1.26  riastrad 	level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
   5686  1.26  riastrad 	level->plane_res_b = val & PLANE_WM_BLOCKS_MASK;
   5687  1.26  riastrad 	level->plane_res_l = (val >> PLANE_WM_LINES_SHIFT) &
   5688  1.26  riastrad 		PLANE_WM_LINES_MASK;
   5689  1.10  riastrad }
   5690  1.10  riastrad 
   5691  1.26  riastrad void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
   5692  1.26  riastrad 			      struct skl_pipe_wm *out)
   5693  1.10  riastrad {
   5694  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   5695  1.26  riastrad 	enum pipe pipe = crtc->pipe;
   5696  1.26  riastrad 	int level, max_level;
   5697  1.26  riastrad 	enum plane_id plane_id;
   5698  1.26  riastrad 	u32 val;
   5699  1.10  riastrad 
   5700  1.26  riastrad 	max_level = ilk_wm_max_level(dev_priv);
   5701  1.10  riastrad 
   5702  1.26  riastrad 	for_each_plane_id_on_crtc(crtc, plane_id) {
   5703  1.26  riastrad 		struct skl_plane_wm *wm = &out->planes[plane_id];
   5704  1.10  riastrad 
   5705  1.26  riastrad 		for (level = 0; level <= max_level; level++) {
   5706  1.26  riastrad 			if (plane_id != PLANE_CURSOR)
   5707  1.26  riastrad 				val = I915_READ(PLANE_WM(pipe, plane_id, level));
   5708  1.26  riastrad 			else
   5709  1.26  riastrad 				val = I915_READ(CUR_WM(pipe, level));
   5710   1.1  riastrad 
   5711  1.26  riastrad 			skl_wm_level_from_reg_val(val, &wm->wm[level]);
   5712  1.26  riastrad 		}
   5713   1.1  riastrad 
   5714  1.26  riastrad 		if (plane_id != PLANE_CURSOR)
   5715  1.26  riastrad 			val = I915_READ(PLANE_WM_TRANS(pipe, plane_id));
   5716  1.26  riastrad 		else
   5717  1.26  riastrad 			val = I915_READ(CUR_WM_TRANS(pipe));
   5718   1.3  riastrad 
   5719  1.26  riastrad 		skl_wm_level_from_reg_val(val, &wm->trans_wm);
   5720  1.10  riastrad 	}
   5721   1.1  riastrad 
   5722  1.26  riastrad 	if (!crtc->active)
   5723  1.26  riastrad 		return;
   5724  1.10  riastrad 
   5725  1.26  riastrad 	out->linetime = I915_READ(PIPE_WM_LINETIME(pipe));
   5726  1.10  riastrad }
   5727  1.10  riastrad 
   5728  1.26  riastrad void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
   5729  1.10  riastrad {
   5730  1.26  riastrad 	struct skl_ddb_values *hw = &dev_priv->wm.skl_hw;
   5731  1.26  riastrad 	struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
   5732  1.26  riastrad 	struct intel_crtc *crtc;
   5733  1.26  riastrad 	struct intel_crtc_state *crtc_state;
   5734  1.10  riastrad 
   5735  1.26  riastrad 	skl_ddb_get_hw_state(dev_priv, ddb);
   5736  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   5737  1.26  riastrad 		crtc_state = to_intel_crtc_state(crtc->base.state);
   5738  1.10  riastrad 
   5739  1.26  riastrad 		skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
   5740  1.10  riastrad 
   5741  1.26  riastrad 		if (crtc->active)
   5742  1.26  riastrad 			hw->dirty_pipes |= BIT(crtc->pipe);
   5743  1.26  riastrad 	}
   5744  1.10  riastrad 
   5745  1.26  riastrad 	if (dev_priv->active_pipes) {
   5746  1.26  riastrad 		/* Fully recompute DDB on first atomic commit */
   5747  1.26  riastrad 		dev_priv->wm.distrust_bios_wm = true;
   5748  1.26  riastrad 	}
   5749  1.10  riastrad }
   5750  1.10  riastrad 
   5751  1.26  riastrad static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
   5752  1.10  riastrad {
   5753  1.26  riastrad 	struct drm_device *dev = crtc->base.dev;
   5754  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(dev);
   5755  1.26  riastrad 	struct ilk_wm_values *hw = &dev_priv->wm.hw;
   5756  1.26  riastrad 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
   5757  1.26  riastrad 	struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
   5758  1.26  riastrad 	enum pipe pipe = crtc->pipe;
   5759  1.26  riastrad 	static const i915_reg_t wm0_pipe_reg[] = {
   5760  1.26  riastrad 		[PIPE_A] = WM0_PIPEA_ILK,
   5761  1.26  riastrad 		[PIPE_B] = WM0_PIPEB_ILK,
   5762  1.26  riastrad 		[PIPE_C] = WM0_PIPEC_IVB,
   5763  1.26  riastrad 	};
   5764  1.26  riastrad 
   5765  1.26  riastrad 	hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
   5766  1.26  riastrad 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   5767  1.26  riastrad 		hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
   5768  1.10  riastrad 
   5769  1.26  riastrad 	memset(active, 0, sizeof(*active));
   5770  1.10  riastrad 
   5771  1.26  riastrad 	active->pipe_enabled = crtc->active;
   5772  1.10  riastrad 
   5773  1.26  riastrad 	if (active->pipe_enabled) {
   5774  1.26  riastrad 		u32 tmp = hw->wm_pipe[pipe];
   5775  1.10  riastrad 
   5776  1.26  riastrad 		/*
   5777  1.26  riastrad 		 * For active pipes LP0 watermark is marked as
   5778  1.26  riastrad 		 * enabled, and LP1+ watermaks as disabled since
   5779  1.26  riastrad 		 * we can't really reverse compute them in case
   5780  1.26  riastrad 		 * multiple pipes are active.
   5781  1.26  riastrad 		 */
   5782  1.26  riastrad 		active->wm[0].enable = true;
   5783  1.26  riastrad 		active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
   5784  1.26  riastrad 		active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
   5785  1.26  riastrad 		active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
   5786  1.26  riastrad 		active->linetime = hw->wm_linetime[pipe];
   5787  1.26  riastrad 	} else {
   5788  1.26  riastrad 		int level, max_level = ilk_wm_max_level(dev_priv);
   5789  1.10  riastrad 
   5790  1.26  riastrad 		/*
   5791  1.26  riastrad 		 * For inactive pipes, all watermark levels
   5792  1.26  riastrad 		 * should be marked as enabled but zeroed,
   5793  1.26  riastrad 		 * which is what we'd compute them to.
   5794  1.26  riastrad 		 */
   5795  1.26  riastrad 		for (level = 0; level <= max_level; level++)
   5796  1.26  riastrad 			active->wm[level].enable = true;
   5797  1.26  riastrad 	}
   5798  1.10  riastrad 
   5799  1.26  riastrad 	crtc->wm.active.ilk = *active;
   5800  1.26  riastrad }
   5801   1.1  riastrad 
   5802  1.26  riastrad #define _FW_WM(value, plane) \
   5803  1.26  riastrad 	(((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
   5804  1.26  riastrad #define _FW_WM_VLV(value, plane) \
   5805  1.26  riastrad 	(((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
   5806   1.1  riastrad 
   5807  1.26  riastrad static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
   5808  1.26  riastrad 			       struct g4x_wm_values *wm)
   5809  1.10  riastrad {
   5810  1.26  riastrad 	u32 tmp;
   5811  1.26  riastrad 
   5812  1.26  riastrad 	tmp = I915_READ(DSPFW1);
   5813  1.26  riastrad 	wm->sr.plane = _FW_WM(tmp, SR);
   5814  1.26  riastrad 	wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
   5815  1.26  riastrad 	wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
   5816  1.26  riastrad 	wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
   5817   1.1  riastrad 
   5818  1.26  riastrad 	tmp = I915_READ(DSPFW2);
   5819  1.26  riastrad 	wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
   5820  1.26  riastrad 	wm->sr.fbc = _FW_WM(tmp, FBC_SR);
   5821  1.26  riastrad 	wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
   5822  1.26  riastrad 	wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
   5823  1.26  riastrad 	wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
   5824  1.26  riastrad 	wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
   5825   1.3  riastrad 
   5826  1.26  riastrad 	tmp = I915_READ(DSPFW3);
   5827  1.26  riastrad 	wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
   5828  1.26  riastrad 	wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
   5829  1.26  riastrad 	wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
   5830  1.26  riastrad 	wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
   5831   1.1  riastrad }
   5832   1.1  riastrad 
   5833  1.26  riastrad static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
   5834  1.26  riastrad 			       struct vlv_wm_values *wm)
   5835   1.1  riastrad {
   5836  1.26  riastrad 	enum pipe pipe;
   5837  1.26  riastrad 	u32 tmp;
   5838  1.10  riastrad 
   5839  1.26  riastrad 	for_each_pipe(dev_priv, pipe) {
   5840  1.26  riastrad 		tmp = I915_READ(VLV_DDL(pipe));
   5841  1.10  riastrad 
   5842  1.26  riastrad 		wm->ddl[pipe].plane[PLANE_PRIMARY] =
   5843  1.26  riastrad 			(tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
   5844  1.26  riastrad 		wm->ddl[pipe].plane[PLANE_CURSOR] =
   5845  1.26  riastrad 			(tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
   5846  1.26  riastrad 		wm->ddl[pipe].plane[PLANE_SPRITE0] =
   5847  1.26  riastrad 			(tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
   5848  1.26  riastrad 		wm->ddl[pipe].plane[PLANE_SPRITE1] =
   5849  1.26  riastrad 			(tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
   5850  1.26  riastrad 	}
   5851  1.10  riastrad 
   5852  1.26  riastrad 	tmp = I915_READ(DSPFW1);
   5853  1.26  riastrad 	wm->sr.plane = _FW_WM(tmp, SR);
   5854  1.26  riastrad 	wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
   5855  1.26  riastrad 	wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
   5856  1.26  riastrad 	wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
   5857  1.10  riastrad 
   5858  1.26  riastrad 	tmp = I915_READ(DSPFW2);
   5859  1.26  riastrad 	wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
   5860  1.26  riastrad 	wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
   5861  1.26  riastrad 	wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
   5862   1.1  riastrad 
   5863  1.26  riastrad 	tmp = I915_READ(DSPFW3);
   5864  1.26  riastrad 	wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
   5865  1.10  riastrad 
   5866  1.26  riastrad 	if (IS_CHERRYVIEW(dev_priv)) {
   5867  1.26  riastrad 		tmp = I915_READ(DSPFW7_CHV);
   5868  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
   5869  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
   5870  1.10  riastrad 
   5871  1.26  riastrad 		tmp = I915_READ(DSPFW8_CHV);
   5872  1.26  riastrad 		wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
   5873  1.26  riastrad 		wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
   5874  1.10  riastrad 
   5875  1.26  riastrad 		tmp = I915_READ(DSPFW9_CHV);
   5876  1.26  riastrad 		wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
   5877  1.26  riastrad 		wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
   5878  1.10  riastrad 
   5879  1.26  riastrad 		tmp = I915_READ(DSPHOWM);
   5880  1.26  riastrad 		wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
   5881  1.26  riastrad 		wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
   5882  1.26  riastrad 		wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
   5883  1.26  riastrad 		wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
   5884  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
   5885  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
   5886  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
   5887  1.26  riastrad 		wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
   5888  1.26  riastrad 		wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
   5889  1.26  riastrad 		wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
   5890  1.26  riastrad 	} else {
   5891  1.26  riastrad 		tmp = I915_READ(DSPFW7);
   5892  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
   5893  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
   5894  1.10  riastrad 
   5895  1.26  riastrad 		tmp = I915_READ(DSPHOWM);
   5896  1.26  riastrad 		wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
   5897  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
   5898  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
   5899  1.26  riastrad 		wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
   5900  1.26  riastrad 		wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
   5901  1.26  riastrad 		wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
   5902  1.26  riastrad 		wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
   5903  1.26  riastrad 	}
   5904  1.26  riastrad }
   5905  1.10  riastrad 
   5906  1.26  riastrad #undef _FW_WM
   5907  1.26  riastrad #undef _FW_WM_VLV
   5908  1.10  riastrad 
   5909  1.26  riastrad void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
   5910  1.10  riastrad {
   5911  1.26  riastrad 	struct g4x_wm_values *wm = &dev_priv->wm.g4x;
   5912  1.26  riastrad 	struct intel_crtc *crtc;
   5913  1.10  riastrad 
   5914  1.26  riastrad 	g4x_read_wm_values(dev_priv, wm);
   5915  1.10  riastrad 
   5916  1.26  riastrad 	wm->cxsr = I915_READ(FW_BLC_SELF) & FW_BLC_SELF_EN;
   5917  1.10  riastrad 
   5918  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   5919  1.26  riastrad 		struct intel_crtc_state *crtc_state =
   5920  1.26  riastrad 			to_intel_crtc_state(crtc->base.state);
   5921  1.26  riastrad 		struct g4x_wm_state *active = &crtc->wm.active.g4x;
   5922  1.26  riastrad 		struct g4x_pipe_wm *raw;
   5923  1.26  riastrad 		enum pipe pipe = crtc->pipe;
   5924  1.26  riastrad 		enum plane_id plane_id;
   5925  1.26  riastrad 		int level, max_level;
   5926  1.10  riastrad 
   5927  1.26  riastrad 		active->cxsr = wm->cxsr;
   5928  1.26  riastrad 		active->hpll_en = wm->hpll_en;
   5929  1.26  riastrad 		active->fbc_en = wm->fbc_en;
   5930  1.10  riastrad 
   5931  1.26  riastrad 		active->sr = wm->sr;
   5932  1.26  riastrad 		active->hpll = wm->hpll;
   5933  1.10  riastrad 
   5934  1.26  riastrad 		for_each_plane_id_on_crtc(crtc, plane_id) {
   5935  1.26  riastrad 			active->wm.plane[plane_id] =
   5936  1.26  riastrad 				wm->pipe[pipe].plane[plane_id];
   5937  1.26  riastrad 		}
   5938  1.10  riastrad 
   5939  1.26  riastrad 		if (wm->cxsr && wm->hpll_en)
   5940  1.26  riastrad 			max_level = G4X_WM_LEVEL_HPLL;
   5941  1.26  riastrad 		else if (wm->cxsr)
   5942  1.26  riastrad 			max_level = G4X_WM_LEVEL_SR;
   5943  1.26  riastrad 		else
   5944  1.26  riastrad 			max_level = G4X_WM_LEVEL_NORMAL;
   5945  1.10  riastrad 
   5946  1.26  riastrad 		level = G4X_WM_LEVEL_NORMAL;
   5947  1.26  riastrad 		raw = &crtc_state->wm.g4x.raw[level];
   5948  1.26  riastrad 		for_each_plane_id_on_crtc(crtc, plane_id)
   5949  1.26  riastrad 			raw->plane[plane_id] = active->wm.plane[plane_id];
   5950  1.26  riastrad 
   5951  1.26  riastrad 		if (++level > max_level)
   5952  1.26  riastrad 			goto out;
   5953  1.26  riastrad 
   5954  1.26  riastrad 		raw = &crtc_state->wm.g4x.raw[level];
   5955  1.26  riastrad 		raw->plane[PLANE_PRIMARY] = active->sr.plane;
   5956  1.26  riastrad 		raw->plane[PLANE_CURSOR] = active->sr.cursor;
   5957  1.26  riastrad 		raw->plane[PLANE_SPRITE0] = 0;
   5958  1.26  riastrad 		raw->fbc = active->sr.fbc;
   5959  1.26  riastrad 
   5960  1.26  riastrad 		if (++level > max_level)
   5961  1.26  riastrad 			goto out;
   5962  1.26  riastrad 
   5963  1.26  riastrad 		raw = &crtc_state->wm.g4x.raw[level];
   5964  1.26  riastrad 		raw->plane[PLANE_PRIMARY] = active->hpll.plane;
   5965  1.26  riastrad 		raw->plane[PLANE_CURSOR] = active->hpll.cursor;
   5966  1.26  riastrad 		raw->plane[PLANE_SPRITE0] = 0;
   5967  1.26  riastrad 		raw->fbc = active->hpll.fbc;
   5968  1.26  riastrad 
   5969  1.26  riastrad 	out:
   5970  1.26  riastrad 		for_each_plane_id_on_crtc(crtc, plane_id)
   5971  1.26  riastrad 			g4x_raw_plane_wm_set(crtc_state, level,
   5972  1.26  riastrad 					     plane_id, USHRT_MAX);
   5973  1.26  riastrad 		g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
   5974  1.26  riastrad 
   5975  1.26  riastrad 		crtc_state->wm.g4x.optimal = *active;
   5976  1.26  riastrad 		crtc_state->wm.g4x.intermediate = *active;
   5977  1.26  riastrad 
   5978  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   5979  1.26  riastrad 			    "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
   5980  1.26  riastrad 			    pipe_name(pipe),
   5981  1.26  riastrad 			    wm->pipe[pipe].plane[PLANE_PRIMARY],
   5982  1.26  riastrad 			    wm->pipe[pipe].plane[PLANE_CURSOR],
   5983  1.26  riastrad 			    wm->pipe[pipe].plane[PLANE_SPRITE0]);
   5984  1.26  riastrad 	}
   5985  1.26  riastrad 
   5986  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   5987  1.26  riastrad 		    "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
   5988  1.26  riastrad 		    wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
   5989  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   5990  1.26  riastrad 		    "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
   5991  1.26  riastrad 		    wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
   5992  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
   5993  1.26  riastrad 		    yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
   5994  1.10  riastrad }
   5995  1.10  riastrad 
   5996  1.26  riastrad void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
   5997  1.10  riastrad {
   5998  1.26  riastrad 	struct intel_plane *plane;
   5999  1.26  riastrad 	struct intel_crtc *crtc;
   6000  1.10  riastrad 
   6001  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   6002  1.10  riastrad 
   6003  1.26  riastrad 	for_each_intel_plane(&dev_priv->drm, plane) {
   6004  1.26  riastrad 		struct intel_crtc *crtc =
   6005  1.26  riastrad 			intel_get_crtc_for_pipe(dev_priv, plane->pipe);
   6006  1.26  riastrad 		struct intel_crtc_state *crtc_state =
   6007  1.26  riastrad 			to_intel_crtc_state(crtc->base.state);
   6008  1.26  riastrad 		struct intel_plane_state *plane_state =
   6009  1.26  riastrad 			to_intel_plane_state(plane->base.state);
   6010  1.26  riastrad 		struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
   6011  1.26  riastrad 		enum plane_id plane_id = plane->id;
   6012  1.26  riastrad 		int level;
   6013  1.10  riastrad 
   6014  1.26  riastrad 		if (plane_state->uapi.visible)
   6015  1.26  riastrad 			continue;
   6016  1.10  riastrad 
   6017  1.26  riastrad 		for (level = 0; level < 3; level++) {
   6018  1.26  riastrad 			struct g4x_pipe_wm *raw =
   6019  1.26  riastrad 				&crtc_state->wm.g4x.raw[level];
   6020  1.26  riastrad 
   6021  1.26  riastrad 			raw->plane[plane_id] = 0;
   6022  1.26  riastrad 			wm_state->wm.plane[plane_id] = 0;
   6023  1.26  riastrad 		}
   6024  1.10  riastrad 
   6025  1.26  riastrad 		if (plane_id == PLANE_PRIMARY) {
   6026  1.26  riastrad 			for (level = 0; level < 3; level++) {
   6027  1.26  riastrad 				struct g4x_pipe_wm *raw =
   6028  1.26  riastrad 					&crtc_state->wm.g4x.raw[level];
   6029  1.26  riastrad 				raw->fbc = 0;
   6030  1.26  riastrad 			}
   6031  1.10  riastrad 
   6032  1.26  riastrad 			wm_state->sr.fbc = 0;
   6033  1.26  riastrad 			wm_state->hpll.fbc = 0;
   6034  1.26  riastrad 			wm_state->fbc_en = false;
   6035  1.26  riastrad 		}
   6036  1.26  riastrad 	}
   6037  1.10  riastrad 
   6038  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   6039  1.26  riastrad 		struct intel_crtc_state *crtc_state =
   6040  1.26  riastrad 			to_intel_crtc_state(crtc->base.state);
   6041  1.10  riastrad 
   6042  1.26  riastrad 		crtc_state->wm.g4x.intermediate =
   6043  1.26  riastrad 			crtc_state->wm.g4x.optimal;
   6044  1.26  riastrad 		crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
   6045  1.26  riastrad 	}
   6046  1.10  riastrad 
   6047  1.26  riastrad 	g4x_program_watermarks(dev_priv);
   6048  1.10  riastrad 
   6049  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   6050  1.10  riastrad }
   6051  1.10  riastrad 
   6052  1.26  riastrad void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
   6053  1.10  riastrad {
   6054  1.26  riastrad 	struct vlv_wm_values *wm = &dev_priv->wm.vlv;
   6055  1.26  riastrad 	struct intel_crtc *crtc;
   6056  1.26  riastrad 	u32 val;
   6057  1.26  riastrad 
   6058  1.26  riastrad 	vlv_read_wm_values(dev_priv, wm);
   6059  1.10  riastrad 
   6060  1.26  riastrad 	wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
   6061  1.26  riastrad 	wm->level = VLV_WM_LEVEL_PM2;
   6062  1.10  riastrad 
   6063  1.26  riastrad 	if (IS_CHERRYVIEW(dev_priv)) {
   6064  1.26  riastrad 		vlv_punit_get(dev_priv);
   6065  1.10  riastrad 
   6066  1.26  riastrad 		val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
   6067  1.26  riastrad 		if (val & DSP_MAXFIFO_PM5_ENABLE)
   6068  1.26  riastrad 			wm->level = VLV_WM_LEVEL_PM5;
   6069  1.10  riastrad 
   6070  1.26  riastrad 		/*
   6071  1.26  riastrad 		 * If DDR DVFS is disabled in the BIOS, Punit
   6072  1.26  riastrad 		 * will never ack the request. So if that happens
   6073  1.26  riastrad 		 * assume we don't have to enable/disable DDR DVFS
   6074  1.26  riastrad 		 * dynamically. To test that just set the REQ_ACK
   6075  1.26  riastrad 		 * bit to poke the Punit, but don't change the
   6076  1.26  riastrad 		 * HIGH/LOW bits so that we don't actually change
   6077  1.26  riastrad 		 * the current state.
   6078  1.26  riastrad 		 */
   6079  1.26  riastrad 		val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
   6080  1.26  riastrad 		val |= FORCE_DDR_FREQ_REQ_ACK;
   6081  1.26  riastrad 		vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
   6082   1.1  riastrad 
   6083  1.26  riastrad 		if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
   6084  1.26  riastrad 			      FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
   6085  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   6086  1.26  riastrad 				    "Punit not acking DDR DVFS request, "
   6087  1.26  riastrad 				    "assuming DDR DVFS is disabled\n");
   6088  1.26  riastrad 			dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
   6089  1.26  riastrad 		} else {
   6090  1.26  riastrad 			val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
   6091  1.26  riastrad 			if ((val & FORCE_DDR_HIGH_FREQ) == 0)
   6092  1.26  riastrad 				wm->level = VLV_WM_LEVEL_DDR_DVFS;
   6093  1.26  riastrad 		}
   6094   1.3  riastrad 
   6095  1.26  riastrad 		vlv_punit_put(dev_priv);
   6096   1.3  riastrad 	}
   6097   1.3  riastrad 
   6098  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   6099  1.26  riastrad 		struct intel_crtc_state *crtc_state =
   6100  1.26  riastrad 			to_intel_crtc_state(crtc->base.state);
   6101  1.26  riastrad 		struct vlv_wm_state *active = &crtc->wm.active.vlv;
   6102  1.26  riastrad 		const struct vlv_fifo_state *fifo_state =
   6103  1.26  riastrad 			&crtc_state->wm.vlv.fifo_state;
   6104  1.26  riastrad 		enum pipe pipe = crtc->pipe;
   6105  1.26  riastrad 		enum plane_id plane_id;
   6106  1.26  riastrad 		int level;
   6107   1.1  riastrad 
   6108  1.26  riastrad 		vlv_get_fifo_size(crtc_state);
   6109   1.1  riastrad 
   6110  1.26  riastrad 		active->num_levels = wm->level + 1;
   6111  1.26  riastrad 		active->cxsr = wm->cxsr;
   6112   1.1  riastrad 
   6113  1.26  riastrad 		for (level = 0; level < active->num_levels; level++) {
   6114  1.26  riastrad 			struct g4x_pipe_wm *raw =
   6115  1.26  riastrad 				&crtc_state->wm.vlv.raw[level];
   6116   1.1  riastrad 
   6117  1.26  riastrad 			active->sr[level].plane = wm->sr.plane;
   6118  1.26  riastrad 			active->sr[level].cursor = wm->sr.cursor;
   6119   1.1  riastrad 
   6120  1.26  riastrad 			for_each_plane_id_on_crtc(crtc, plane_id) {
   6121  1.26  riastrad 				active->wm[level].plane[plane_id] =
   6122  1.26  riastrad 					wm->pipe[pipe].plane[plane_id];
   6123   1.3  riastrad 
   6124  1.26  riastrad 				raw->plane[plane_id] =
   6125  1.26  riastrad 					vlv_invert_wm_value(active->wm[level].plane[plane_id],
   6126  1.26  riastrad 							    fifo_state->plane[plane_id]);
   6127  1.26  riastrad 			}
   6128  1.26  riastrad 		}
   6129   1.1  riastrad 
   6130  1.26  riastrad 		for_each_plane_id_on_crtc(crtc, plane_id)
   6131  1.26  riastrad 			vlv_raw_plane_wm_set(crtc_state, level,
   6132  1.26  riastrad 					     plane_id, USHRT_MAX);
   6133  1.26  riastrad 		vlv_invalidate_wms(crtc, active, level);
   6134   1.1  riastrad 
   6135  1.26  riastrad 		crtc_state->wm.vlv.optimal = *active;
   6136  1.26  riastrad 		crtc_state->wm.vlv.intermediate = *active;
   6137   1.1  riastrad 
   6138  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   6139  1.26  riastrad 			    "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
   6140  1.26  riastrad 			    pipe_name(pipe),
   6141  1.26  riastrad 			    wm->pipe[pipe].plane[PLANE_PRIMARY],
   6142  1.26  riastrad 			    wm->pipe[pipe].plane[PLANE_CURSOR],
   6143  1.26  riastrad 			    wm->pipe[pipe].plane[PLANE_SPRITE0],
   6144  1.26  riastrad 			    wm->pipe[pipe].plane[PLANE_SPRITE1]);
   6145  1.10  riastrad 	}
   6146   1.1  riastrad 
   6147  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   6148  1.26  riastrad 		    "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
   6149  1.26  riastrad 		    wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
   6150   1.1  riastrad }
   6151   1.1  riastrad 
   6152  1.26  riastrad void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
   6153   1.1  riastrad {
   6154  1.26  riastrad 	struct intel_plane *plane;
   6155  1.26  riastrad 	struct intel_crtc *crtc;
   6156   1.1  riastrad 
   6157  1.26  riastrad 	mutex_lock(&dev_priv->wm.wm_mutex);
   6158   1.1  riastrad 
   6159  1.26  riastrad 	for_each_intel_plane(&dev_priv->drm, plane) {
   6160  1.26  riastrad 		struct intel_crtc *crtc =
   6161  1.26  riastrad 			intel_get_crtc_for_pipe(dev_priv, plane->pipe);
   6162  1.26  riastrad 		struct intel_crtc_state *crtc_state =
   6163  1.26  riastrad 			to_intel_crtc_state(crtc->base.state);
   6164  1.26  riastrad 		struct intel_plane_state *plane_state =
   6165  1.26  riastrad 			to_intel_plane_state(plane->base.state);
   6166  1.26  riastrad 		struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
   6167  1.26  riastrad 		const struct vlv_fifo_state *fifo_state =
   6168  1.26  riastrad 			&crtc_state->wm.vlv.fifo_state;
   6169  1.26  riastrad 		enum plane_id plane_id = plane->id;
   6170  1.26  riastrad 		int level;
   6171   1.1  riastrad 
   6172  1.26  riastrad 		if (plane_state->uapi.visible)
   6173  1.26  riastrad 			continue;
   6174   1.3  riastrad 
   6175  1.26  riastrad 		for (level = 0; level < wm_state->num_levels; level++) {
   6176  1.26  riastrad 			struct g4x_pipe_wm *raw =
   6177  1.26  riastrad 				&crtc_state->wm.vlv.raw[level];
   6178   1.1  riastrad 
   6179  1.26  riastrad 			raw->plane[plane_id] = 0;
   6180   1.1  riastrad 
   6181  1.26  riastrad 			wm_state->wm[level].plane[plane_id] =
   6182  1.26  riastrad 				vlv_invert_wm_value(raw->plane[plane_id],
   6183  1.26  riastrad 						    fifo_state->plane[plane_id]);
   6184  1.26  riastrad 		}
   6185  1.26  riastrad 	}
   6186  1.20      maya 
   6187  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc) {
   6188  1.26  riastrad 		struct intel_crtc_state *crtc_state =
   6189  1.26  riastrad 			to_intel_crtc_state(crtc->base.state);
   6190  1.20      maya 
   6191  1.26  riastrad 		crtc_state->wm.vlv.intermediate =
   6192  1.26  riastrad 			crtc_state->wm.vlv.optimal;
   6193  1.26  riastrad 		crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
   6194  1.20      maya 	}
   6195  1.20      maya 
   6196  1.26  riastrad 	vlv_program_watermarks(dev_priv);
   6197  1.20      maya 
   6198  1.26  riastrad 	mutex_unlock(&dev_priv->wm.wm_mutex);
   6199  1.20      maya }
   6200  1.20      maya 
   6201  1.26  riastrad /*
   6202  1.26  riastrad  * FIXME should probably kill this and improve
   6203  1.26  riastrad  * the real watermark readout/sanitation instead
   6204  1.20      maya  */
   6205  1.26  riastrad static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
   6206  1.20      maya {
   6207  1.26  riastrad 	I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
   6208  1.26  riastrad 	I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
   6209  1.26  riastrad 	I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
   6210  1.20      maya 
   6211  1.26  riastrad 	/*
   6212  1.26  riastrad 	 * Don't touch WM1S_LP_EN here.
   6213  1.26  riastrad 	 * Doing so could cause underruns.
   6214  1.26  riastrad 	 */
   6215  1.20      maya }
   6216  1.20      maya 
   6217  1.26  riastrad void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
   6218  1.20      maya {
   6219  1.26  riastrad 	struct ilk_wm_values *hw = &dev_priv->wm.hw;
   6220  1.26  riastrad 	struct intel_crtc *crtc;
   6221  1.20      maya 
   6222  1.26  riastrad 	ilk_init_lp_watermarks(dev_priv);
   6223  1.20      maya 
   6224  1.26  riastrad 	for_each_intel_crtc(&dev_priv->drm, crtc)
   6225  1.26  riastrad 		ilk_pipe_wm_get_hw_state(crtc);
   6226  1.20      maya 
   6227  1.26  riastrad 	hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
   6228  1.26  riastrad 	hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
   6229  1.26  riastrad 	hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
   6230  1.20      maya 
   6231  1.26  riastrad 	hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
   6232  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 7) {
   6233  1.26  riastrad 		hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
   6234  1.26  riastrad 		hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
   6235  1.26  riastrad 	}
   6236  1.20      maya 
   6237  1.26  riastrad 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
   6238  1.26  riastrad 		hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
   6239  1.26  riastrad 			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
   6240  1.26  riastrad 	else if (IS_IVYBRIDGE(dev_priv))
   6241  1.26  riastrad 		hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
   6242  1.26  riastrad 			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
   6243  1.20      maya 
   6244  1.26  riastrad 	hw->enable_fbc_wm =
   6245  1.26  riastrad 		!(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
   6246  1.10  riastrad }
   6247   1.1  riastrad 
   6248  1.10  riastrad /**
   6249  1.26  riastrad  * intel_update_watermarks - update FIFO watermark values based on current modes
   6250  1.26  riastrad  * @crtc: the #intel_crtc on which to compute the WM
   6251  1.26  riastrad  *
   6252  1.26  riastrad  * Calculate watermark values for the various WM regs based on current mode
   6253  1.26  riastrad  * and plane configuration.
   6254  1.26  riastrad  *
   6255  1.26  riastrad  * There are several cases to deal with here:
   6256  1.26  riastrad  *   - normal (i.e. non-self-refresh)
   6257  1.26  riastrad  *   - self-refresh (SR) mode
   6258  1.26  riastrad  *   - lines are large relative to FIFO size (buffer can hold up to 2)
   6259  1.26  riastrad  *   - lines are small relative to FIFO size (buffer can hold more than 2
   6260  1.26  riastrad  *     lines), so need to account for TLB latency
   6261  1.26  riastrad  *
   6262  1.26  riastrad  *   The normal calculation is:
   6263  1.26  riastrad  *     watermark = dotclock * bytes per pixel * latency
   6264  1.26  riastrad  *   where latency is platform & configuration dependent (we assume pessimal
   6265  1.26  riastrad  *   values here).
   6266  1.26  riastrad  *
   6267  1.26  riastrad  *   The SR calculation is:
   6268  1.26  riastrad  *     watermark = (trunc(latency/line time)+1) * surface width *
   6269  1.26  riastrad  *       bytes per pixel
   6270  1.26  riastrad  *   where
   6271  1.26  riastrad  *     line time = htotal / dotclock
   6272  1.26  riastrad  *     surface width = hdisplay for normal plane and 64 for cursor
   6273  1.26  riastrad  *   and latency is assumed to be high, as above.
   6274  1.10  riastrad  *
   6275  1.26  riastrad  * The final value programmed to the register should always be rounded up,
   6276  1.26  riastrad  * and include an extra 2 entries to account for clock crossings.
   6277  1.26  riastrad  *
   6278  1.26  riastrad  * We don't use the sprite, so we can ignore that.  And on Crestline we have
   6279  1.26  riastrad  * to set the non-SR watermarks to 8.
   6280  1.10  riastrad  */
   6281  1.26  riastrad void intel_update_watermarks(struct intel_crtc *crtc)
   6282  1.20      maya {
   6283  1.26  riastrad 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
   6284  1.20      maya 
   6285  1.26  riastrad 	if (dev_priv->display.update_wm)
   6286  1.26  riastrad 		dev_priv->display.update_wm(crtc);
   6287  1.20      maya }
   6288  1.20      maya 
   6289  1.26  riastrad void intel_enable_ipc(struct drm_i915_private *dev_priv)
   6290  1.20      maya {
   6291  1.26  riastrad 	u32 val;
   6292  1.26  riastrad 
   6293  1.26  riastrad 	if (!HAS_IPC(dev_priv))
   6294  1.20      maya 		return;
   6295  1.20      maya 
   6296  1.26  riastrad 	val = I915_READ(DISP_ARB_CTL2);
   6297  1.26  riastrad 
   6298  1.26  riastrad 	if (dev_priv->ipc_enabled)
   6299  1.26  riastrad 		val |= DISP_IPC_ENABLE;
   6300  1.20      maya 	else
   6301  1.26  riastrad 		val &= ~DISP_IPC_ENABLE;
   6302  1.20      maya 
   6303  1.26  riastrad 	I915_WRITE(DISP_ARB_CTL2, val);
   6304  1.10  riastrad }
   6305   1.3  riastrad 
   6306  1.26  riastrad static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
   6307  1.10  riastrad {
   6308  1.26  riastrad 	/* Display WA #0477 WaDisableIPC: skl */
   6309  1.26  riastrad 	if (IS_SKYLAKE(dev_priv))
   6310  1.26  riastrad 		return false;
   6311   1.3  riastrad 
   6312  1.26  riastrad 	/* Display WA #1141: SKL:all KBL:all CFL */
   6313  1.26  riastrad 	if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv))
   6314  1.26  riastrad 		return dev_priv->dram_info.symmetric_memory;
   6315   1.1  riastrad 
   6316  1.26  riastrad 	return true;
   6317  1.10  riastrad }
   6318   1.3  riastrad 
   6319  1.26  riastrad void intel_init_ipc(struct drm_i915_private *dev_priv)
   6320  1.10  riastrad {
   6321  1.26  riastrad 	if (!HAS_IPC(dev_priv))
   6322  1.10  riastrad 		return;
   6323   1.3  riastrad 
   6324  1.26  riastrad 	dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
   6325   1.1  riastrad 
   6326  1.26  riastrad 	intel_enable_ipc(dev_priv);
   6327  1.10  riastrad }
   6328   1.1  riastrad 
   6329  1.26  riastrad static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
   6330  1.10  riastrad {
   6331  1.10  riastrad 	/*
   6332  1.10  riastrad 	 * On Ibex Peak and Cougar Point, we need to disable clock
   6333  1.10  riastrad 	 * gating for the panel power sequencer or it will fail to
   6334  1.10  riastrad 	 * start up when no ports are active.
   6335  1.10  riastrad 	 */
   6336  1.10  riastrad 	I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
   6337  1.10  riastrad }
   6338   1.1  riastrad 
   6339  1.26  riastrad static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
   6340  1.10  riastrad {
   6341  1.22  riastrad 	enum pipe pipe;
   6342   1.1  riastrad 
   6343  1.10  riastrad 	for_each_pipe(dev_priv, pipe) {
   6344  1.10  riastrad 		I915_WRITE(DSPCNTR(pipe),
   6345  1.10  riastrad 			   I915_READ(DSPCNTR(pipe)) |
   6346  1.10  riastrad 			   DISPPLANE_TRICKLE_FEED_DISABLE);
   6347   1.1  riastrad 
   6348  1.10  riastrad 		I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
   6349  1.10  riastrad 		POSTING_READ(DSPSURF(pipe));
   6350  1.10  riastrad 	}
   6351  1.10  riastrad }
   6352   1.1  riastrad 
   6353  1.26  riastrad static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
   6354  1.10  riastrad {
   6355  1.26  riastrad 	u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
   6356   1.3  riastrad 
   6357   1.3  riastrad 	/*
   6358  1.10  riastrad 	 * Required for FBC
   6359  1.10  riastrad 	 * WaFbcDisableDpfcClockGating:ilk
   6360   1.3  riastrad 	 */
   6361  1.10  riastrad 	dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
   6362  1.10  riastrad 		   ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
   6363  1.10  riastrad 		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
   6364  1.10  riastrad 
   6365  1.10  riastrad 	I915_WRITE(PCH_3DCGDIS0,
   6366  1.10  riastrad 		   MARIUNIT_CLOCK_GATE_DISABLE |
   6367  1.10  riastrad 		   SVSMUNIT_CLOCK_GATE_DISABLE);
   6368  1.10  riastrad 	I915_WRITE(PCH_3DCGDIS1,
   6369  1.10  riastrad 		   VFMUNIT_CLOCK_GATE_DISABLE);
   6370   1.3  riastrad 
   6371   1.3  riastrad 	/*
   6372  1.10  riastrad 	 * According to the spec the following bits should be set in
   6373  1.10  riastrad 	 * order to enable memory self-refresh
   6374  1.10  riastrad 	 * The bit 22/21 of 0x42004
   6375  1.10  riastrad 	 * The bit 5 of 0x42020
   6376  1.10  riastrad 	 * The bit 15 of 0x45000
   6377   1.3  riastrad 	 */
   6378  1.10  riastrad 	I915_WRITE(ILK_DISPLAY_CHICKEN2,
   6379  1.10  riastrad 		   (I915_READ(ILK_DISPLAY_CHICKEN2) |
   6380  1.10  riastrad 		    ILK_DPARB_GATE | ILK_VSDPFD_FULL));
   6381  1.10  riastrad 	dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
   6382  1.10  riastrad 	I915_WRITE(DISP_ARB_CTL,
   6383  1.10  riastrad 		   (I915_READ(DISP_ARB_CTL) |
   6384  1.10  riastrad 		    DISP_FBC_WM_DIS));
   6385  1.10  riastrad 
   6386   1.3  riastrad 	/*
   6387  1.10  riastrad 	 * Based on the document from hardware guys the following bits
   6388  1.10  riastrad 	 * should be set unconditionally in order to enable FBC.
   6389  1.10  riastrad 	 * The bit 22 of 0x42000
   6390  1.10  riastrad 	 * The bit 22 of 0x42004
   6391  1.10  riastrad 	 * The bit 7,8,9 of 0x42020.
   6392   1.3  riastrad 	 */
   6393  1.26  riastrad 	if (IS_IRONLAKE_M(dev_priv)) {
   6394  1.10  riastrad 		/* WaFbcAsynchFlipDisableFbcQueue:ilk */
   6395  1.10  riastrad 		I915_WRITE(ILK_DISPLAY_CHICKEN1,
   6396  1.10  riastrad 			   I915_READ(ILK_DISPLAY_CHICKEN1) |
   6397  1.10  riastrad 			   ILK_FBCQ_DIS);
   6398  1.10  riastrad 		I915_WRITE(ILK_DISPLAY_CHICKEN2,
   6399  1.10  riastrad 			   I915_READ(ILK_DISPLAY_CHICKEN2) |
   6400  1.10  riastrad 			   ILK_DPARB_GATE);
   6401  1.10  riastrad 	}
   6402   1.3  riastrad 
   6403  1.10  riastrad 	I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
   6404   1.3  riastrad 
   6405  1.10  riastrad 	I915_WRITE(ILK_DISPLAY_CHICKEN2,
   6406  1.10  riastrad 		   I915_READ(ILK_DISPLAY_CHICKEN2) |
   6407  1.10  riastrad 		   ILK_ELPIN_409_SELECT);
   6408  1.10  riastrad 	I915_WRITE(_3D_CHICKEN2,
   6409  1.10  riastrad 		   _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
   6410  1.10  riastrad 		   _3D_CHICKEN2_WM_READ_PIPELINED);
   6411   1.3  riastrad 
   6412  1.10  riastrad 	/* WaDisableRenderCachePipelinedFlush:ilk */
   6413   1.3  riastrad 	I915_WRITE(CACHE_MODE_0,
   6414   1.3  riastrad 		   _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
   6415   1.3  riastrad 
   6416  1.10  riastrad 	/* WaDisable_RenderCache_OperationalFlush:ilk */
   6417  1.10  riastrad 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
   6418  1.10  riastrad 
   6419  1.26  riastrad 	g4x_disable_trickle_feed(dev_priv);
   6420  1.10  riastrad 
   6421  1.26  riastrad 	ibx_init_clock_gating(dev_priv);
   6422   1.3  riastrad }
   6423   1.3  riastrad 
   6424  1.26  riastrad static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
   6425   1.3  riastrad {
   6426  1.26  riastrad 	enum pipe pipe;
   6427  1.26  riastrad 	u32 val;
   6428   1.3  riastrad 
   6429  1.10  riastrad 	/*
   6430  1.10  riastrad 	 * On Ibex Peak and Cougar Point, we need to disable clock
   6431  1.10  riastrad 	 * gating for the panel power sequencer or it will fail to
   6432  1.10  riastrad 	 * start up when no ports are active.
   6433  1.10  riastrad 	 */
   6434  1.10  riastrad 	I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
   6435  1.10  riastrad 		   PCH_DPLUNIT_CLOCK_GATE_DISABLE |
   6436  1.10  riastrad 		   PCH_CPUNIT_CLOCK_GATE_DISABLE);
   6437  1.10  riastrad 	I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
   6438  1.10  riastrad 		   DPLS_EDP_PPS_FIX_DIS);
   6439  1.10  riastrad 	/* The below fixes the weird display corruption, a few pixels shifted
   6440  1.10  riastrad 	 * downward, on (only) LVDS of some HP laptops with IVY.
   6441  1.10  riastrad 	 */
   6442  1.10  riastrad 	for_each_pipe(dev_priv, pipe) {
   6443  1.10  riastrad 		val = I915_READ(TRANS_CHICKEN2(pipe));
   6444  1.10  riastrad 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
   6445  1.10  riastrad 		val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
   6446  1.10  riastrad 		if (dev_priv->vbt.fdi_rx_polarity_inverted)
   6447  1.10  riastrad 			val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
   6448  1.10  riastrad 		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
   6449  1.10  riastrad 		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
   6450  1.10  riastrad 		I915_WRITE(TRANS_CHICKEN2(pipe), val);
   6451  1.10  riastrad 	}
   6452  1.10  riastrad 	/* WADP0ClockGatingDisable */
   6453  1.10  riastrad 	for_each_pipe(dev_priv, pipe) {
   6454  1.10  riastrad 		I915_WRITE(TRANS_CHICKEN1(pipe),
   6455  1.10  riastrad 			   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
   6456  1.10  riastrad 	}
   6457   1.3  riastrad }
   6458   1.3  riastrad 
   6459  1.26  riastrad static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
   6460   1.3  riastrad {
   6461  1.26  riastrad 	u32 tmp;
   6462   1.3  riastrad 
   6463  1.10  riastrad 	tmp = I915_READ(MCH_SSKPD);
   6464  1.10  riastrad 	if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
   6465  1.26  riastrad 		drm_dbg_kms(&dev_priv->drm,
   6466  1.26  riastrad 			    "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
   6467  1.26  riastrad 			    tmp);
   6468   1.3  riastrad }
   6469   1.3  riastrad 
   6470  1.26  riastrad static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
   6471   1.3  riastrad {
   6472  1.26  riastrad 	u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
   6473  1.10  riastrad 
   6474  1.10  riastrad 	I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
   6475  1.10  riastrad 
   6476  1.10  riastrad 	I915_WRITE(ILK_DISPLAY_CHICKEN2,
   6477  1.10  riastrad 		   I915_READ(ILK_DISPLAY_CHICKEN2) |
   6478  1.10  riastrad 		   ILK_ELPIN_409_SELECT);
   6479  1.10  riastrad 
   6480  1.10  riastrad 	/* WaDisableHiZPlanesWhenMSAAEnabled:snb */
   6481  1.10  riastrad 	I915_WRITE(_3D_CHICKEN,
   6482  1.10  riastrad 		   _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
   6483   1.3  riastrad 
   6484  1.10  riastrad 	/* WaDisable_RenderCache_OperationalFlush:snb */
   6485  1.10  riastrad 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
   6486   1.3  riastrad 
   6487  1.10  riastrad 	/*
   6488  1.10  riastrad 	 * BSpec recoomends 8x4 when MSAA is used,
   6489  1.10  riastrad 	 * however in practice 16x4 seems fastest.
   6490  1.10  riastrad 	 *
   6491  1.10  riastrad 	 * Note that PS/WM thread counts depend on the WIZ hashing
   6492  1.10  riastrad 	 * disable bit, which we don't touch here, but it's good
   6493  1.10  riastrad 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
   6494  1.10  riastrad 	 */
   6495  1.10  riastrad 	I915_WRITE(GEN6_GT_MODE,
   6496  1.10  riastrad 		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
   6497   1.3  riastrad 
   6498  1.10  riastrad 	I915_WRITE(CACHE_MODE_0,
   6499  1.10  riastrad 		   _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
   6500   1.3  riastrad 
   6501  1.10  riastrad 	I915_WRITE(GEN6_UCGCTL1,
   6502  1.10  riastrad 		   I915_READ(GEN6_UCGCTL1) |
   6503  1.10  riastrad 		   GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
   6504  1.10  riastrad 		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
   6505   1.3  riastrad 
   6506  1.10  riastrad 	/* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
   6507  1.10  riastrad 	 * gating disable must be set.  Failure to set it results in
   6508  1.10  riastrad 	 * flickering pixels due to Z write ordering failures after
   6509  1.10  riastrad 	 * some amount of runtime in the Mesa "fire" demo, and Unigine
   6510  1.10  riastrad 	 * Sanctuary and Tropics, and apparently anything else with
   6511  1.10  riastrad 	 * alpha test or pixel discard.
   6512  1.10  riastrad 	 *
   6513  1.10  riastrad 	 * According to the spec, bit 11 (RCCUNIT) must also be set,
   6514  1.10  riastrad 	 * but we didn't debug actual testcases to find it out.
   6515  1.10  riastrad 	 *
   6516  1.10  riastrad 	 * WaDisableRCCUnitClockGating:snb
   6517  1.10  riastrad 	 * WaDisableRCPBUnitClockGating:snb
   6518  1.10  riastrad 	 */
   6519  1.10  riastrad 	I915_WRITE(GEN6_UCGCTL2,
   6520  1.10  riastrad 		   GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
   6521  1.10  riastrad 		   GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
   6522   1.3  riastrad 
   6523  1.10  riastrad 	/* WaStripsFansDisableFastClipPerformanceFix:snb */
   6524  1.10  riastrad 	I915_WRITE(_3D_CHICKEN3,
   6525  1.10  riastrad 		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
   6526   1.3  riastrad 
   6527  1.10  riastrad 	/*
   6528  1.10  riastrad 	 * Bspec says:
   6529  1.10  riastrad 	 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
   6530  1.10  riastrad 	 * 3DSTATE_SF number of SF output attributes is more than 16."
   6531  1.10  riastrad 	 */
   6532  1.10  riastrad 	I915_WRITE(_3D_CHICKEN3,
   6533  1.10  riastrad 		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
   6534   1.3  riastrad 
   6535  1.10  riastrad 	/*
   6536  1.10  riastrad 	 * According to the spec the following bits should be
   6537  1.10  riastrad 	 * set in order to enable memory self-refresh and fbc:
   6538  1.10  riastrad 	 * The bit21 and bit22 of 0x42000
   6539  1.10  riastrad 	 * The bit21 and bit22 of 0x42004
   6540  1.10  riastrad 	 * The bit5 and bit7 of 0x42020
   6541  1.10  riastrad 	 * The bit14 of 0x70180
   6542  1.10  riastrad 	 * The bit14 of 0x71180
   6543  1.10  riastrad 	 *
   6544  1.10  riastrad 	 * WaFbcAsynchFlipDisableFbcQueue:snb
   6545  1.10  riastrad 	 */
   6546  1.10  riastrad 	I915_WRITE(ILK_DISPLAY_CHICKEN1,
   6547  1.10  riastrad 		   I915_READ(ILK_DISPLAY_CHICKEN1) |
   6548  1.10  riastrad 		   ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
   6549  1.10  riastrad 	I915_WRITE(ILK_DISPLAY_CHICKEN2,
   6550  1.10  riastrad 		   I915_READ(ILK_DISPLAY_CHICKEN2) |
   6551  1.10  riastrad 		   ILK_DPARB_GATE | ILK_VSDPFD_FULL);
   6552  1.10  riastrad 	I915_WRITE(ILK_DSPCLK_GATE_D,
   6553  1.10  riastrad 		   I915_READ(ILK_DSPCLK_GATE_D) |
   6554  1.10  riastrad 		   ILK_DPARBUNIT_CLOCK_GATE_ENABLE  |
   6555  1.10  riastrad 		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
   6556   1.3  riastrad 
   6557  1.26  riastrad 	g4x_disable_trickle_feed(dev_priv);
   6558   1.3  riastrad 
   6559  1.26  riastrad 	cpt_init_clock_gating(dev_priv);
   6560   1.3  riastrad 
   6561  1.26  riastrad 	gen6_check_mch_setup(dev_priv);
   6562   1.3  riastrad }
   6563   1.3  riastrad 
   6564  1.10  riastrad static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
   6565   1.3  riastrad {
   6566  1.26  riastrad 	u32 reg = I915_READ(GEN7_FF_THREAD_MODE);
   6567   1.3  riastrad 
   6568  1.10  riastrad 	/*
   6569  1.10  riastrad 	 * WaVSThreadDispatchOverride:ivb,vlv
   6570  1.10  riastrad 	 *
   6571  1.10  riastrad 	 * This actually overrides the dispatch
   6572  1.10  riastrad 	 * mode for all thread types.
   6573  1.10  riastrad 	 */
   6574  1.10  riastrad 	reg &= ~GEN7_FF_SCHED_MASK;
   6575  1.10  riastrad 	reg |= GEN7_FF_TS_SCHED_HW;
   6576  1.10  riastrad 	reg |= GEN7_FF_VS_SCHED_HW;
   6577  1.10  riastrad 	reg |= GEN7_FF_DS_SCHED_HW;
   6578   1.3  riastrad 
   6579  1.10  riastrad 	I915_WRITE(GEN7_FF_THREAD_MODE, reg);
   6580   1.3  riastrad }
   6581   1.3  riastrad 
   6582  1.26  riastrad static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
   6583   1.3  riastrad {
   6584  1.10  riastrad 	/*
   6585  1.10  riastrad 	 * TODO: this bit should only be enabled when really needed, then
   6586  1.10  riastrad 	 * disabled when not needed anymore in order to save power.
   6587  1.10  riastrad 	 */
   6588  1.26  riastrad 	if (HAS_PCH_LPT_LP(dev_priv))
   6589  1.10  riastrad 		I915_WRITE(SOUTH_DSPCLK_GATE_D,
   6590  1.10  riastrad 			   I915_READ(SOUTH_DSPCLK_GATE_D) |
   6591  1.10  riastrad 			   PCH_LP_PARTITION_LEVEL_DISABLE);
   6592   1.3  riastrad 
   6593  1.10  riastrad 	/* WADPOClockGatingDisable:hsw */
   6594  1.10  riastrad 	I915_WRITE(TRANS_CHICKEN1(PIPE_A),
   6595  1.10  riastrad 		   I915_READ(TRANS_CHICKEN1(PIPE_A)) |
   6596  1.10  riastrad 		   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
   6597   1.3  riastrad }
   6598   1.3  riastrad 
   6599  1.26  riastrad static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
   6600   1.3  riastrad {
   6601  1.26  riastrad 	if (HAS_PCH_LPT_LP(dev_priv)) {
   6602  1.26  riastrad 		u32 val = I915_READ(SOUTH_DSPCLK_GATE_D);
   6603   1.3  riastrad 
   6604  1.10  riastrad 		val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
   6605  1.10  riastrad 		I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
   6606   1.3  riastrad 	}
   6607   1.3  riastrad }
   6608   1.3  riastrad 
   6609  1.26  riastrad static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
   6610  1.26  riastrad 				   int general_prio_credits,
   6611  1.26  riastrad 				   int high_prio_credits)
   6612  1.26  riastrad {
   6613  1.26  riastrad 	u32 misccpctl;
   6614  1.26  riastrad 	u32 val;
   6615  1.26  riastrad 
   6616  1.26  riastrad 	/* WaTempDisableDOPClkGating:bdw */
   6617  1.26  riastrad 	misccpctl = I915_READ(GEN7_MISCCPCTL);
   6618  1.26  riastrad 	I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
   6619  1.26  riastrad 
   6620  1.26  riastrad 	val = I915_READ(GEN8_L3SQCREG1);
   6621  1.26  riastrad 	val &= ~L3_PRIO_CREDITS_MASK;
   6622  1.26  riastrad 	val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
   6623  1.26  riastrad 	val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
   6624  1.26  riastrad 	I915_WRITE(GEN8_L3SQCREG1, val);
   6625  1.26  riastrad 
   6626  1.26  riastrad 	/*
   6627  1.26  riastrad 	 * Wait at least 100 clocks before re-enabling clock gating.
   6628  1.26  riastrad 	 * See the definition of L3SQCREG1 in BSpec.
   6629  1.26  riastrad 	 */
   6630  1.26  riastrad 	POSTING_READ(GEN8_L3SQCREG1);
   6631  1.26  riastrad 	udelay(1);
   6632  1.26  riastrad 	I915_WRITE(GEN7_MISCCPCTL, misccpctl);
   6633  1.26  riastrad }
   6634  1.26  riastrad 
   6635  1.26  riastrad static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
   6636  1.26  riastrad {
   6637  1.26  riastrad 	/* This is not an Wa. Enable to reduce Sampler power */
   6638  1.26  riastrad 	I915_WRITE(GEN10_DFR_RATIO_EN_AND_CHICKEN,
   6639  1.26  riastrad 		   I915_READ(GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
   6640  1.26  riastrad 
   6641  1.26  riastrad 	/* WaEnable32PlaneMode:icl */
   6642  1.26  riastrad 	I915_WRITE(GEN9_CSFE_CHICKEN1_RCS,
   6643  1.26  riastrad 		   _MASKED_BIT_ENABLE(GEN11_ENABLE_32_PLANE_MODE));
   6644  1.26  riastrad 
   6645  1.26  riastrad 	/*
   6646  1.26  riastrad 	 * Wa_1408615072:icl,ehl  (vsunit)
   6647  1.26  riastrad 	 * Wa_1407596294:icl,ehl  (hsunit)
   6648  1.26  riastrad 	 */
   6649  1.26  riastrad 	intel_uncore_rmw(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE,
   6650  1.26  riastrad 			 0, VSUNIT_CLKGATE_DIS | HSUNIT_CLKGATE_DIS);
   6651  1.26  riastrad 
   6652  1.26  riastrad 	/* Wa_1407352427:icl,ehl */
   6653  1.26  riastrad 	intel_uncore_rmw(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE2,
   6654  1.26  riastrad 			 0, PSDUNIT_CLKGATE_DIS);
   6655  1.26  riastrad }
   6656  1.26  riastrad 
   6657  1.26  riastrad static void tgl_init_clock_gating(struct drm_i915_private *dev_priv)
   6658  1.26  riastrad {
   6659  1.26  riastrad 	u32 vd_pg_enable = 0;
   6660  1.26  riastrad 	unsigned int i;
   6661  1.26  riastrad 
   6662  1.26  riastrad 	/* Wa_1408615072:tgl */
   6663  1.26  riastrad 	intel_uncore_rmw(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE2,
   6664  1.26  riastrad 			 0, VSUNIT_CLKGATE_DIS_TGL);
   6665  1.26  riastrad 
   6666  1.26  riastrad 	/* This is not a WA. Enable VD HCP & MFX_ENC powergate */
   6667  1.26  riastrad 	for (i = 0; i < I915_MAX_VCS; i++) {
   6668  1.26  riastrad 		if (HAS_ENGINE(dev_priv, _VCS(i)))
   6669  1.26  riastrad 			vd_pg_enable |= VDN_HCP_POWERGATE_ENABLE(i) |
   6670  1.26  riastrad 					VDN_MFX_POWERGATE_ENABLE(i);
   6671  1.26  riastrad 	}
   6672  1.26  riastrad 
   6673  1.26  riastrad 	I915_WRITE(POWERGATE_ENABLE,
   6674  1.26  riastrad 		   I915_READ(POWERGATE_ENABLE) | vd_pg_enable);
   6675  1.26  riastrad }
   6676  1.26  riastrad 
   6677  1.26  riastrad static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
   6678  1.26  riastrad {
   6679  1.26  riastrad 	if (!HAS_PCH_CNP(dev_priv))
   6680  1.26  riastrad 		return;
   6681  1.26  riastrad 
   6682  1.26  riastrad 	/* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
   6683  1.26  riastrad 	I915_WRITE(SOUTH_DSPCLK_GATE_D, I915_READ(SOUTH_DSPCLK_GATE_D) |
   6684  1.26  riastrad 		   CNP_PWM_CGE_GATING_DISABLE);
   6685  1.26  riastrad }
   6686  1.26  riastrad 
   6687  1.26  riastrad static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
   6688  1.26  riastrad {
   6689  1.26  riastrad 	u32 val;
   6690  1.26  riastrad 	cnp_init_clock_gating(dev_priv);
   6691  1.26  riastrad 
   6692  1.26  riastrad 	/* This is not an Wa. Enable for better image quality */
   6693  1.26  riastrad 	I915_WRITE(_3D_CHICKEN3,
   6694  1.26  riastrad 		   _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
   6695  1.26  riastrad 
   6696  1.26  riastrad 	/* WaEnableChickenDCPR:cnl */
   6697  1.26  riastrad 	I915_WRITE(GEN8_CHICKEN_DCPR_1,
   6698  1.26  riastrad 		   I915_READ(GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
   6699  1.26  riastrad 
   6700  1.26  riastrad 	/* WaFbcWakeMemOn:cnl */
   6701  1.26  riastrad 	I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
   6702  1.26  riastrad 		   DISP_FBC_MEMORY_WAKE);
   6703  1.26  riastrad 
   6704  1.26  riastrad 	val = I915_READ(SLICE_UNIT_LEVEL_CLKGATE);
   6705  1.26  riastrad 	/* ReadHitWriteOnlyDisable:cnl */
   6706  1.26  riastrad 	val |= RCCUNIT_CLKGATE_DIS;
   6707  1.26  riastrad 	/* WaSarbUnitClockGatingDisable:cnl (pre-prod) */
   6708  1.26  riastrad 	if (IS_CNL_REVID(dev_priv, CNL_REVID_A0, CNL_REVID_B0))
   6709  1.26  riastrad 		val |= SARBUNIT_CLKGATE_DIS;
   6710  1.26  riastrad 	I915_WRITE(SLICE_UNIT_LEVEL_CLKGATE, val);
   6711  1.26  riastrad 
   6712  1.26  riastrad 	/* Wa_2201832410:cnl */
   6713  1.26  riastrad 	val = I915_READ(SUBSLICE_UNIT_LEVEL_CLKGATE);
   6714  1.26  riastrad 	val |= GWUNIT_CLKGATE_DIS;
   6715  1.26  riastrad 	I915_WRITE(SUBSLICE_UNIT_LEVEL_CLKGATE, val);
   6716  1.26  riastrad 
   6717  1.26  riastrad 	/* WaDisableVFclkgate:cnl */
   6718  1.26  riastrad 	/* WaVFUnitClockGatingDisable:cnl */
   6719  1.26  riastrad 	val = I915_READ(UNSLICE_UNIT_LEVEL_CLKGATE);
   6720  1.26  riastrad 	val |= VFUNIT_CLKGATE_DIS;
   6721  1.26  riastrad 	I915_WRITE(UNSLICE_UNIT_LEVEL_CLKGATE, val);
   6722  1.26  riastrad }
   6723  1.26  riastrad 
   6724  1.26  riastrad static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
   6725  1.26  riastrad {
   6726  1.26  riastrad 	cnp_init_clock_gating(dev_priv);
   6727  1.26  riastrad 	gen9_init_clock_gating(dev_priv);
   6728  1.26  riastrad 
   6729  1.26  riastrad 	/* WaFbcNukeOnHostModify:cfl */
   6730  1.26  riastrad 	I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
   6731  1.26  riastrad 		   ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
   6732  1.26  riastrad }
   6733  1.26  riastrad 
   6734  1.26  riastrad static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
   6735  1.26  riastrad {
   6736  1.26  riastrad 	gen9_init_clock_gating(dev_priv);
   6737  1.26  riastrad 
   6738  1.26  riastrad 	/* WaDisableSDEUnitClockGating:kbl */
   6739  1.26  riastrad 	if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
   6740  1.26  riastrad 		I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
   6741  1.26  riastrad 			   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
   6742  1.26  riastrad 
   6743  1.26  riastrad 	/* WaDisableGamClockGating:kbl */
   6744  1.26  riastrad 	if (IS_KBL_REVID(dev_priv, 0, KBL_REVID_B0))
   6745  1.26  riastrad 		I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
   6746  1.26  riastrad 			   GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
   6747  1.26  riastrad 
   6748  1.26  riastrad 	/* WaFbcNukeOnHostModify:kbl */
   6749  1.26  riastrad 	I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
   6750  1.26  riastrad 		   ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
   6751  1.26  riastrad }
   6752  1.26  riastrad 
   6753  1.26  riastrad static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
   6754  1.26  riastrad {
   6755  1.26  riastrad 	gen9_init_clock_gating(dev_priv);
   6756  1.26  riastrad 
   6757  1.26  riastrad 	/* WAC6entrylatency:skl */
   6758  1.26  riastrad 	I915_WRITE(FBC_LLC_READ_CTRL, I915_READ(FBC_LLC_READ_CTRL) |
   6759  1.26  riastrad 		   FBC_LLC_FULLY_OPEN);
   6760  1.26  riastrad 
   6761  1.26  riastrad 	/* WaFbcNukeOnHostModify:skl */
   6762  1.26  riastrad 	I915_WRITE(ILK_DPFC_CHICKEN, I915_READ(ILK_DPFC_CHICKEN) |
   6763  1.26  riastrad 		   ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
   6764  1.26  riastrad }
   6765  1.26  riastrad 
   6766  1.26  riastrad static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
   6767   1.3  riastrad {
   6768  1.22  riastrad 	enum pipe pipe;
   6769   1.3  riastrad 
   6770  1.10  riastrad 	/* WaSwitchSolVfFArbitrationPriority:bdw */
   6771  1.10  riastrad 	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
   6772   1.3  riastrad 
   6773  1.10  riastrad 	/* WaPsrDPAMaskVBlankInSRD:bdw */
   6774  1.10  riastrad 	I915_WRITE(CHICKEN_PAR1_1,
   6775  1.10  riastrad 		   I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
   6776   1.3  riastrad 
   6777  1.10  riastrad 	/* WaPsrDPRSUnmaskVBlankInSRD:bdw */
   6778  1.10  riastrad 	for_each_pipe(dev_priv, pipe) {
   6779  1.10  riastrad 		I915_WRITE(CHICKEN_PIPESL_1(pipe),
   6780  1.10  riastrad 			   I915_READ(CHICKEN_PIPESL_1(pipe)) |
   6781  1.10  riastrad 			   BDW_DPRS_MASK_VBLANK_SRD);
   6782  1.10  riastrad 	}
   6783   1.3  riastrad 
   6784  1.10  riastrad 	/* WaVSRefCountFullforceMissDisable:bdw */
   6785  1.10  riastrad 	/* WaDSRefCountFullforceMissDisable:bdw */
   6786  1.10  riastrad 	I915_WRITE(GEN7_FF_THREAD_MODE,
   6787  1.10  riastrad 		   I915_READ(GEN7_FF_THREAD_MODE) &
   6788  1.10  riastrad 		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
   6789   1.3  riastrad 
   6790  1.10  riastrad 	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
   6791  1.10  riastrad 		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
   6792   1.3  riastrad 
   6793  1.10  riastrad 	/* WaDisableSDEUnitClockGating:bdw */
   6794  1.10  riastrad 	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
   6795  1.10  riastrad 		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
   6796   1.3  riastrad 
   6797  1.26  riastrad 	/* WaProgramL3SqcReg1Default:bdw */
   6798  1.26  riastrad 	gen8_set_l3sqc_credits(dev_priv, 30, 2);
   6799  1.26  riastrad 
   6800  1.26  riastrad 	/* WaKVMNotificationOnConfigChange:bdw */
   6801  1.26  riastrad 	I915_WRITE(CHICKEN_PAR2_1, I915_READ(CHICKEN_PAR2_1)
   6802  1.26  riastrad 		   | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
   6803  1.26  riastrad 
   6804  1.26  riastrad 	lpt_init_clock_gating(dev_priv);
   6805   1.3  riastrad 
   6806  1.26  riastrad 	/* WaDisableDopClockGating:bdw
   6807  1.26  riastrad 	 *
   6808  1.26  riastrad 	 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
   6809  1.26  riastrad 	 * clock gating.
   6810   1.3  riastrad 	 */
   6811  1.26  riastrad 	I915_WRITE(GEN6_UCGCTL1,
   6812  1.26  riastrad 		   I915_READ(GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
   6813   1.3  riastrad }
   6814   1.3  riastrad 
   6815  1.26  riastrad static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
   6816   1.3  riastrad {
   6817  1.10  riastrad 	/* L3 caching of data atomics doesn't work -- disable it. */
   6818  1.10  riastrad 	I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
   6819  1.10  riastrad 	I915_WRITE(HSW_ROW_CHICKEN3,
   6820  1.10  riastrad 		   _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
   6821   1.3  riastrad 
   6822  1.10  riastrad 	/* This is required by WaCatErrorRejectionIssue:hsw */
   6823  1.10  riastrad 	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
   6824  1.10  riastrad 			I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
   6825  1.10  riastrad 			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
   6826   1.3  riastrad 
   6827  1.10  riastrad 	/* WaVSRefCountFullforceMissDisable:hsw */
   6828  1.10  riastrad 	I915_WRITE(GEN7_FF_THREAD_MODE,
   6829  1.10  riastrad 		   I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
   6830   1.3  riastrad 
   6831  1.10  riastrad 	/* WaDisable_RenderCache_OperationalFlush:hsw */
   6832  1.10  riastrad 	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
   6833   1.3  riastrad 
   6834  1.10  riastrad 	/* enable HiZ Raw Stall Optimization */
   6835  1.10  riastrad 	I915_WRITE(CACHE_MODE_0_GEN7,
   6836  1.10  riastrad 		   _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
   6837   1.3  riastrad 
   6838  1.10  riastrad 	/* WaDisable4x2SubspanOptimization:hsw */
   6839  1.10  riastrad 	I915_WRITE(CACHE_MODE_1,
   6840  1.10  riastrad 		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
   6841   1.3  riastrad 
   6842  1.10  riastrad 	/*
   6843  1.10  riastrad 	 * BSpec recommends 8x4 when MSAA is used,
   6844  1.10  riastrad 	 * however in practice 16x4 seems fastest.
   6845  1.10  riastrad 	 *
   6846  1.10  riastrad 	 * Note that PS/WM thread counts depend on the WIZ hashing
   6847  1.10  riastrad 	 * disable bit, which we don't touch here, but it's good
   6848  1.10  riastrad 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
   6849  1.10  riastrad 	 */
   6850  1.10  riastrad 	I915_WRITE(GEN7_GT_MODE,
   6851  1.10  riastrad 		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
   6852   1.3  riastrad 
   6853  1.10  riastrad 	/* WaSampleCChickenBitEnable:hsw */
   6854  1.10  riastrad 	I915_WRITE(HALF_SLICE_CHICKEN3,
   6855  1.10  riastrad 		   _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
   6856   1.3  riastrad 
   6857  1.10  riastrad 	/* WaSwitchSolVfFArbitrationPriority:hsw */
   6858  1.10  riastrad 	I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
   6859   1.3  riastrad 
   6860  1.26  riastrad 	lpt_init_clock_gating(dev_priv);
   6861   1.3  riastrad }
   6862   1.3  riastrad 
   6863  1.26  riastrad static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
   6864   1.3  riastrad {
   6865  1.26  riastrad 	u32 snpcr;
   6866   1.3  riastrad 
   6867  1.10  riastrad 	I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
   6868   1.3  riastrad 
   6869  1.10  riastrad 	/* WaDisableEarlyCull:ivb */
   6870  1.10  riastrad 	I915_WRITE(_3D_CHICKEN3,
   6871  1.10  riastrad 		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
   6872   1.3  riastrad 
   6873  1.10  riastrad 	/* WaDisableBackToBackFlipFix:ivb */
   6874  1.10  riastrad 	I915_WRITE(IVB_CHICKEN3,
   6875  1.10  riastrad 		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
   6876  1.10  riastrad 		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
   6877   1.3  riastrad 
   6878  1.10  riastrad 	/* WaDisablePSDDualDispatchEnable:ivb */
   6879  1.26  riastrad 	if (IS_IVB_GT1(dev_priv))
   6880  1.10  riastrad 		I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
   6881  1.10  riastrad 			   _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
   6882   1.3  riastrad 
   6883  1.10  riastrad 	/* WaDisable_RenderCache_OperationalFlush:ivb */
   6884  1.10  riastrad 	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
   6885   1.3  riastrad 
   6886  1.10  riastrad 	/* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
   6887  1.10  riastrad 	I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
   6888  1.10  riastrad 		   GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
   6889   1.3  riastrad 
   6890  1.10  riastrad 	/* WaApplyL3ControlAndL3ChickenMode:ivb */
   6891  1.10  riastrad 	I915_WRITE(GEN7_L3CNTLREG1,
   6892  1.10  riastrad 			GEN7_WA_FOR_GEN7_L3_CONTROL);
   6893  1.10  riastrad 	I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
   6894  1.10  riastrad 		   GEN7_WA_L3_CHICKEN_MODE);
   6895  1.26  riastrad 	if (IS_IVB_GT1(dev_priv))
   6896  1.10  riastrad 		I915_WRITE(GEN7_ROW_CHICKEN2,
   6897  1.10  riastrad 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
   6898  1.10  riastrad 	else {
   6899  1.10  riastrad 		/* must write both registers */
   6900  1.10  riastrad 		I915_WRITE(GEN7_ROW_CHICKEN2,
   6901  1.10  riastrad 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
   6902  1.10  riastrad 		I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
   6903  1.10  riastrad 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
   6904  1.10  riastrad 	}
   6905   1.3  riastrad 
   6906  1.10  riastrad 	/* WaForceL3Serialization:ivb */
   6907  1.10  riastrad 	I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
   6908  1.10  riastrad 		   ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
   6909   1.3  riastrad 
   6910   1.3  riastrad 	/*
   6911  1.10  riastrad 	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
   6912  1.10  riastrad 	 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
   6913   1.3  riastrad 	 */
   6914  1.10  riastrad 	I915_WRITE(GEN6_UCGCTL2,
   6915  1.10  riastrad 		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
   6916  1.10  riastrad 
   6917  1.10  riastrad 	/* This is required by WaCatErrorRejectionIssue:ivb */
   6918  1.10  riastrad 	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
   6919  1.10  riastrad 			I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
   6920  1.10  riastrad 			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
   6921   1.3  riastrad 
   6922  1.26  riastrad 	g4x_disable_trickle_feed(dev_priv);
   6923  1.10  riastrad 
   6924  1.10  riastrad 	gen7_setup_fixed_func_scheduler(dev_priv);
   6925   1.3  riastrad 
   6926  1.10  riastrad 	if (0) { /* causes HiZ corruption on ivb:gt1 */
   6927  1.10  riastrad 		/* enable HiZ Raw Stall Optimization */
   6928  1.10  riastrad 		I915_WRITE(CACHE_MODE_0_GEN7,
   6929  1.10  riastrad 			   _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
   6930  1.10  riastrad 	}
   6931   1.3  riastrad 
   6932  1.10  riastrad 	/* WaDisable4x2SubspanOptimization:ivb */
   6933  1.10  riastrad 	I915_WRITE(CACHE_MODE_1,
   6934  1.10  riastrad 		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
   6935   1.3  riastrad 
   6936  1.10  riastrad 	/*
   6937  1.10  riastrad 	 * BSpec recommends 8x4 when MSAA is used,
   6938  1.10  riastrad 	 * however in practice 16x4 seems fastest.
   6939  1.10  riastrad 	 *
   6940  1.10  riastrad 	 * Note that PS/WM thread counts depend on the WIZ hashing
   6941  1.10  riastrad 	 * disable bit, which we don't touch here, but it's good
   6942  1.10  riastrad 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
   6943  1.10  riastrad 	 */
   6944  1.10  riastrad 	I915_WRITE(GEN7_GT_MODE,
   6945  1.10  riastrad 		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
   6946   1.3  riastrad 
   6947  1.10  riastrad 	snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
   6948  1.10  riastrad 	snpcr &= ~GEN6_MBC_SNPCR_MASK;
   6949  1.10  riastrad 	snpcr |= GEN6_MBC_SNPCR_MED;
   6950  1.10  riastrad 	I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
   6951   1.3  riastrad 
   6952  1.26  riastrad 	if (!HAS_PCH_NOP(dev_priv))
   6953  1.26  riastrad 		cpt_init_clock_gating(dev_priv);
   6954   1.3  riastrad 
   6955  1.26  riastrad 	gen6_check_mch_setup(dev_priv);
   6956   1.3  riastrad }
   6957   1.3  riastrad 
   6958  1.26  riastrad static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
   6959   1.3  riastrad {
   6960  1.10  riastrad 	/* WaDisableEarlyCull:vlv */
   6961  1.10  riastrad 	I915_WRITE(_3D_CHICKEN3,
   6962  1.10  riastrad 		   _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
   6963   1.3  riastrad 
   6964  1.10  riastrad 	/* WaDisableBackToBackFlipFix:vlv */
   6965  1.10  riastrad 	I915_WRITE(IVB_CHICKEN3,
   6966  1.10  riastrad 		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
   6967  1.10  riastrad 		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
   6968   1.3  riastrad 
   6969  1.10  riastrad 	/* WaPsdDispatchEnable:vlv */
   6970  1.10  riastrad 	/* WaDisablePSDDualDispatchEnable:vlv */
   6971  1.10  riastrad 	I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
   6972  1.10  riastrad 		   _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
   6973  1.10  riastrad 				      GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
   6974   1.3  riastrad 
   6975  1.10  riastrad 	/* WaDisable_RenderCache_OperationalFlush:vlv */
   6976  1.10  riastrad 	I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
   6977   1.3  riastrad 
   6978  1.10  riastrad 	/* WaForceL3Serialization:vlv */
   6979  1.10  riastrad 	I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
   6980  1.10  riastrad 		   ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
   6981   1.3  riastrad 
   6982  1.10  riastrad 	/* WaDisableDopClockGating:vlv */
   6983  1.10  riastrad 	I915_WRITE(GEN7_ROW_CHICKEN2,
   6984  1.10  riastrad 		   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
   6985   1.3  riastrad 
   6986  1.10  riastrad 	/* This is required by WaCatErrorRejectionIssue:vlv */
   6987  1.10  riastrad 	I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
   6988  1.10  riastrad 		   I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
   6989  1.10  riastrad 		   GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
   6990   1.3  riastrad 
   6991  1.10  riastrad 	gen7_setup_fixed_func_scheduler(dev_priv);
   6992   1.3  riastrad 
   6993  1.10  riastrad 	/*
   6994  1.10  riastrad 	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
   6995  1.10  riastrad 	 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
   6996  1.10  riastrad 	 */
   6997  1.10  riastrad 	I915_WRITE(GEN6_UCGCTL2,
   6998  1.10  riastrad 		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
   6999   1.3  riastrad 
   7000  1.10  riastrad 	/* WaDisableL3Bank2xClockGate:vlv
   7001  1.10  riastrad 	 * Disabling L3 clock gating- MMIO 940c[25] = 1
   7002  1.10  riastrad 	 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
   7003  1.10  riastrad 	I915_WRITE(GEN7_UCGCTL4,
   7004  1.10  riastrad 		   I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
   7005   1.3  riastrad 
   7006  1.10  riastrad 	/*
   7007  1.10  riastrad 	 * BSpec says this must be set, even though
   7008  1.10  riastrad 	 * WaDisable4x2SubspanOptimization isn't listed for VLV.
   7009  1.10  riastrad 	 */
   7010  1.10  riastrad 	I915_WRITE(CACHE_MODE_1,
   7011  1.10  riastrad 		   _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
   7012   1.3  riastrad 
   7013  1.10  riastrad 	/*
   7014  1.10  riastrad 	 * BSpec recommends 8x4 when MSAA is used,
   7015  1.10  riastrad 	 * however in practice 16x4 seems fastest.
   7016  1.10  riastrad 	 *
   7017  1.10  riastrad 	 * Note that PS/WM thread counts depend on the WIZ hashing
   7018  1.10  riastrad 	 * disable bit, which we don't touch here, but it's good
   7019  1.10  riastrad 	 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
   7020  1.10  riastrad 	 */
   7021  1.10  riastrad 	I915_WRITE(GEN7_GT_MODE,
   7022  1.10  riastrad 		   _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
   7023   1.3  riastrad 
   7024  1.10  riastrad 	/*
   7025  1.10  riastrad 	 * WaIncreaseL3CreditsForVLVB0:vlv
   7026  1.10  riastrad 	 * This is the hardware default actually.
   7027  1.10  riastrad 	 */
   7028  1.10  riastrad 	I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
   7029   1.3  riastrad 
   7030  1.10  riastrad 	/*
   7031  1.10  riastrad 	 * WaDisableVLVClockGating_VBIIssue:vlv
   7032  1.10  riastrad 	 * Disable clock gating on th GCFG unit to prevent a delay
   7033  1.10  riastrad 	 * in the reporting of vblank events.
   7034  1.10  riastrad 	 */
   7035  1.10  riastrad 	I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
   7036   1.3  riastrad }
   7037   1.3  riastrad 
   7038  1.26  riastrad static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
   7039   1.3  riastrad {
   7040  1.10  riastrad 	/* WaVSRefCountFullforceMissDisable:chv */
   7041  1.10  riastrad 	/* WaDSRefCountFullforceMissDisable:chv */
   7042  1.10  riastrad 	I915_WRITE(GEN7_FF_THREAD_MODE,
   7043  1.10  riastrad 		   I915_READ(GEN7_FF_THREAD_MODE) &
   7044  1.10  riastrad 		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
   7045   1.3  riastrad 
   7046  1.10  riastrad 	/* WaDisableSemaphoreAndSyncFlipWait:chv */
   7047  1.10  riastrad 	I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
   7048  1.10  riastrad 		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
   7049   1.3  riastrad 
   7050  1.10  riastrad 	/* WaDisableCSUnitClockGating:chv */
   7051  1.10  riastrad 	I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
   7052  1.10  riastrad 		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
   7053   1.3  riastrad 
   7054  1.10  riastrad 	/* WaDisableSDEUnitClockGating:chv */
   7055  1.10  riastrad 	I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
   7056  1.10  riastrad 		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
   7057   1.3  riastrad 
   7058  1.10  riastrad 	/*
   7059  1.26  riastrad 	 * WaProgramL3SqcReg1Default:chv
   7060  1.26  riastrad 	 * See gfxspecs/Related Documents/Performance Guide/
   7061  1.26  riastrad 	 * LSQC Setting Recommendations.
   7062  1.10  riastrad 	 */
   7063  1.26  riastrad 	gen8_set_l3sqc_credits(dev_priv, 38, 2);
   7064  1.10  riastrad }
   7065   1.3  riastrad 
   7066  1.26  riastrad static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
   7067  1.10  riastrad {
   7068  1.26  riastrad 	u32 dspclk_gate;
   7069   1.3  riastrad 
   7070  1.10  riastrad 	I915_WRITE(RENCLK_GATE_D1, 0);
   7071  1.10  riastrad 	I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
   7072  1.10  riastrad 		   GS_UNIT_CLOCK_GATE_DISABLE |
   7073  1.10  riastrad 		   CL_UNIT_CLOCK_GATE_DISABLE);
   7074  1.10  riastrad 	I915_WRITE(RAMCLK_GATE_D, 0);
   7075  1.10  riastrad 	dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
   7076  1.10  riastrad 		OVRUNIT_CLOCK_GATE_DISABLE |
   7077  1.10  riastrad 		OVCUNIT_CLOCK_GATE_DISABLE;
   7078  1.26  riastrad 	if (IS_GM45(dev_priv))
   7079  1.10  riastrad 		dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
   7080  1.10  riastrad 	I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
   7081   1.3  riastrad 
   7082  1.10  riastrad 	/* WaDisableRenderCachePipelinedFlush */
   7083  1.10  riastrad 	I915_WRITE(CACHE_MODE_0,
   7084  1.10  riastrad 		   _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
   7085   1.3  riastrad 
   7086  1.10  riastrad 	/* WaDisable_RenderCache_OperationalFlush:g4x */
   7087  1.10  riastrad 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
   7088   1.1  riastrad 
   7089  1.26  riastrad 	g4x_disable_trickle_feed(dev_priv);
   7090  1.10  riastrad }
   7091   1.1  riastrad 
   7092  1.26  riastrad static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
   7093   1.3  riastrad {
   7094  1.26  riastrad 	struct intel_uncore *uncore = &dev_priv->uncore;
   7095   1.1  riastrad 
   7096  1.26  riastrad 	intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
   7097  1.26  riastrad 	intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
   7098  1.26  riastrad 	intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
   7099  1.26  riastrad 	intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
   7100  1.26  riastrad 	intel_uncore_write16(uncore, DEUC, 0);
   7101  1.26  riastrad 	intel_uncore_write(uncore,
   7102  1.26  riastrad 			   MI_ARB_STATE,
   7103  1.26  riastrad 			   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
   7104   1.1  riastrad 
   7105  1.10  riastrad 	/* WaDisable_RenderCache_OperationalFlush:gen4 */
   7106  1.26  riastrad 	intel_uncore_write(uncore,
   7107  1.26  riastrad 			   CACHE_MODE_0,
   7108  1.26  riastrad 			   _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
   7109   1.3  riastrad }
   7110   1.1  riastrad 
   7111  1.26  riastrad static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
   7112   1.3  riastrad {
   7113  1.10  riastrad 	I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
   7114  1.10  riastrad 		   I965_RCC_CLOCK_GATE_DISABLE |
   7115  1.10  riastrad 		   I965_RCPB_CLOCK_GATE_DISABLE |
   7116  1.10  riastrad 		   I965_ISC_CLOCK_GATE_DISABLE |
   7117  1.10  riastrad 		   I965_FBC_CLOCK_GATE_DISABLE);
   7118  1.10  riastrad 	I915_WRITE(RENCLK_GATE_D2, 0);
   7119  1.10  riastrad 	I915_WRITE(MI_ARB_STATE,
   7120  1.10  riastrad 		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
   7121   1.4  riastrad 
   7122  1.10  riastrad 	/* WaDisable_RenderCache_OperationalFlush:gen4 */
   7123  1.10  riastrad 	I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
   7124   1.1  riastrad }
   7125   1.1  riastrad 
   7126  1.26  riastrad static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
   7127   1.1  riastrad {
   7128  1.10  riastrad 	u32 dstate = I915_READ(D_STATE);
   7129  1.10  riastrad 
   7130  1.10  riastrad 	dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
   7131  1.10  riastrad 		DSTATE_DOT_CLOCK_GATING;
   7132  1.10  riastrad 	I915_WRITE(D_STATE, dstate);
   7133  1.10  riastrad 
   7134  1.26  riastrad 	if (IS_PINEVIEW(dev_priv))
   7135  1.10  riastrad 		I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
   7136  1.10  riastrad 
   7137  1.10  riastrad 	/* IIR "flip pending" means done if this bit is set */
   7138  1.10  riastrad 	I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
   7139   1.1  riastrad 
   7140  1.10  riastrad 	/* interrupts should cause a wake up from C3 */
   7141  1.10  riastrad 	I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
   7142   1.1  riastrad 
   7143  1.10  riastrad 	/* On GEN3 we really need to make sure the ARB C3 LP bit is set */
   7144  1.10  riastrad 	I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
   7145   1.1  riastrad 
   7146  1.10  riastrad 	I915_WRITE(MI_ARB_STATE,
   7147  1.10  riastrad 		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
   7148   1.1  riastrad }
   7149   1.1  riastrad 
   7150  1.26  riastrad static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
   7151   1.1  riastrad {
   7152  1.10  riastrad 	I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
   7153   1.1  riastrad 
   7154  1.10  riastrad 	/* interrupts should cause a wake up from C3 */
   7155  1.10  riastrad 	I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
   7156  1.10  riastrad 		   _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
   7157   1.1  riastrad 
   7158  1.10  riastrad 	I915_WRITE(MEM_MODE,
   7159  1.10  riastrad 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
   7160   1.1  riastrad }
   7161   1.1  riastrad 
   7162  1.26  riastrad static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
   7163   1.1  riastrad {
   7164  1.10  riastrad 	I915_WRITE(MEM_MODE,
   7165  1.10  riastrad 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
   7166  1.10  riastrad 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
   7167   1.1  riastrad }
   7168   1.1  riastrad 
   7169  1.26  riastrad void intel_init_clock_gating(struct drm_i915_private *dev_priv)
   7170  1.26  riastrad {
   7171  1.26  riastrad 	dev_priv->display.init_clock_gating(dev_priv);
   7172  1.26  riastrad }
   7173  1.26  riastrad 
   7174  1.26  riastrad void intel_suspend_hw(struct drm_i915_private *dev_priv)
   7175   1.1  riastrad {
   7176  1.26  riastrad 	if (HAS_PCH_LPT(dev_priv))
   7177  1.26  riastrad 		lpt_suspend_hw(dev_priv);
   7178  1.26  riastrad }
   7179   1.1  riastrad 
   7180  1.26  riastrad static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
   7181  1.26  riastrad {
   7182  1.26  riastrad 	drm_dbg_kms(&dev_priv->drm,
   7183  1.26  riastrad 		    "No clock gating settings or workarounds applied.\n");
   7184   1.1  riastrad }
   7185   1.1  riastrad 
   7186  1.26  riastrad /**
   7187  1.26  riastrad  * intel_init_clock_gating_hooks - setup the clock gating hooks
   7188  1.26  riastrad  * @dev_priv: device private
   7189  1.26  riastrad  *
   7190  1.26  riastrad  * Setup the hooks that configure which clocks of a given platform can be
   7191  1.26  riastrad  * gated and also apply various GT and display specific workarounds for these
   7192  1.26  riastrad  * platforms. Note that some GT specific workarounds are applied separately
   7193  1.26  riastrad  * when GPU contexts or batchbuffers start their execution.
   7194  1.26  riastrad  */
   7195  1.26  riastrad void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
   7196   1.1  riastrad {
   7197  1.26  riastrad 	if (IS_GEN(dev_priv, 12))
   7198  1.26  riastrad 		dev_priv->display.init_clock_gating = tgl_init_clock_gating;
   7199  1.26  riastrad 	else if (IS_GEN(dev_priv, 11))
   7200  1.26  riastrad 		dev_priv->display.init_clock_gating = icl_init_clock_gating;
   7201  1.26  riastrad 	else if (IS_CANNONLAKE(dev_priv))
   7202  1.26  riastrad 		dev_priv->display.init_clock_gating = cnl_init_clock_gating;
   7203  1.26  riastrad 	else if (IS_COFFEELAKE(dev_priv))
   7204  1.26  riastrad 		dev_priv->display.init_clock_gating = cfl_init_clock_gating;
   7205  1.26  riastrad 	else if (IS_SKYLAKE(dev_priv))
   7206  1.26  riastrad 		dev_priv->display.init_clock_gating = skl_init_clock_gating;
   7207  1.26  riastrad 	else if (IS_KABYLAKE(dev_priv))
   7208  1.26  riastrad 		dev_priv->display.init_clock_gating = kbl_init_clock_gating;
   7209  1.26  riastrad 	else if (IS_BROXTON(dev_priv))
   7210  1.26  riastrad 		dev_priv->display.init_clock_gating = bxt_init_clock_gating;
   7211  1.26  riastrad 	else if (IS_GEMINILAKE(dev_priv))
   7212  1.26  riastrad 		dev_priv->display.init_clock_gating = glk_init_clock_gating;
   7213  1.26  riastrad 	else if (IS_BROADWELL(dev_priv))
   7214  1.26  riastrad 		dev_priv->display.init_clock_gating = bdw_init_clock_gating;
   7215  1.26  riastrad 	else if (IS_CHERRYVIEW(dev_priv))
   7216  1.26  riastrad 		dev_priv->display.init_clock_gating = chv_init_clock_gating;
   7217  1.26  riastrad 	else if (IS_HASWELL(dev_priv))
   7218  1.26  riastrad 		dev_priv->display.init_clock_gating = hsw_init_clock_gating;
   7219  1.26  riastrad 	else if (IS_IVYBRIDGE(dev_priv))
   7220  1.26  riastrad 		dev_priv->display.init_clock_gating = ivb_init_clock_gating;
   7221  1.26  riastrad 	else if (IS_VALLEYVIEW(dev_priv))
   7222  1.26  riastrad 		dev_priv->display.init_clock_gating = vlv_init_clock_gating;
   7223  1.26  riastrad 	else if (IS_GEN(dev_priv, 6))
   7224  1.26  riastrad 		dev_priv->display.init_clock_gating = gen6_init_clock_gating;
   7225  1.26  riastrad 	else if (IS_GEN(dev_priv, 5))
   7226  1.26  riastrad 		dev_priv->display.init_clock_gating = ilk_init_clock_gating;
   7227  1.26  riastrad 	else if (IS_G4X(dev_priv))
   7228  1.26  riastrad 		dev_priv->display.init_clock_gating = g4x_init_clock_gating;
   7229  1.26  riastrad 	else if (IS_I965GM(dev_priv))
   7230  1.26  riastrad 		dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
   7231  1.26  riastrad 	else if (IS_I965G(dev_priv))
   7232  1.26  riastrad 		dev_priv->display.init_clock_gating = i965g_init_clock_gating;
   7233  1.26  riastrad 	else if (IS_GEN(dev_priv, 3))
   7234  1.26  riastrad 		dev_priv->display.init_clock_gating = gen3_init_clock_gating;
   7235  1.26  riastrad 	else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
   7236  1.26  riastrad 		dev_priv->display.init_clock_gating = i85x_init_clock_gating;
   7237  1.26  riastrad 	else if (IS_GEN(dev_priv, 2))
   7238  1.26  riastrad 		dev_priv->display.init_clock_gating = i830_init_clock_gating;
   7239  1.26  riastrad 	else {
   7240  1.26  riastrad 		MISSING_CASE(INTEL_DEVID(dev_priv));
   7241  1.26  riastrad 		dev_priv->display.init_clock_gating = nop_init_clock_gating;
   7242  1.26  riastrad 	}
   7243   1.1  riastrad }
   7244   1.1  riastrad 
   7245   1.1  riastrad /* Set up chip specific power management-related functions */
   7246  1.26  riastrad void intel_init_pm(struct drm_i915_private *dev_priv)
   7247   1.1  riastrad {
   7248  1.26  riastrad 	/* For cxsr */
   7249  1.26  riastrad 	if (IS_PINEVIEW(dev_priv))
   7250  1.26  riastrad 		pnv_get_mem_freq(dev_priv);
   7251  1.26  riastrad 	else if (IS_GEN(dev_priv, 5))
   7252  1.26  riastrad 		ilk_get_mem_freq(dev_priv);
   7253   1.1  riastrad 
   7254  1.26  riastrad 	if (intel_has_sagv(dev_priv))
   7255  1.26  riastrad 		skl_setup_sagv_block_time(dev_priv);
   7256   1.1  riastrad 
   7257   1.1  riastrad 	/* For FIFO watermark updates */
   7258  1.26  riastrad 	if (INTEL_GEN(dev_priv) >= 9) {
   7259  1.26  riastrad 		skl_setup_wm_latency(dev_priv);
   7260  1.26  riastrad 		dev_priv->display.initial_watermarks = skl_initial_wm;
   7261  1.26  riastrad 		dev_priv->display.atomic_update_watermarks = skl_atomic_update_crtc_wm;
   7262  1.26  riastrad 		dev_priv->display.compute_global_watermarks = skl_compute_wm;
   7263  1.26  riastrad 	} else if (HAS_PCH_SPLIT(dev_priv)) {
   7264  1.26  riastrad 		ilk_setup_wm_latency(dev_priv);
   7265  1.10  riastrad 
   7266  1.26  riastrad 		if ((IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[1] &&
   7267   1.3  riastrad 		     dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
   7268  1.26  riastrad 		    (!IS_GEN(dev_priv, 5) && dev_priv->wm.pri_latency[0] &&
   7269   1.3  riastrad 		     dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
   7270  1.26  riastrad 			dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
   7271  1.26  riastrad 			dev_priv->display.compute_intermediate_wm =
   7272  1.26  riastrad 				ilk_compute_intermediate_wm;
   7273  1.26  riastrad 			dev_priv->display.initial_watermarks =
   7274  1.26  riastrad 				ilk_initial_watermarks;
   7275  1.26  riastrad 			dev_priv->display.optimize_watermarks =
   7276  1.26  riastrad 				ilk_optimize_watermarks;
   7277   1.3  riastrad 		} else {
   7278  1.26  riastrad 			drm_dbg_kms(&dev_priv->drm,
   7279  1.26  riastrad 				    "Failed to read display plane latency. "
   7280  1.26  riastrad 				    "Disable CxSR\n");
   7281  1.26  riastrad 		}
   7282  1.26  riastrad 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
   7283  1.26  riastrad 		vlv_setup_wm_latency(dev_priv);
   7284  1.26  riastrad 		dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
   7285  1.26  riastrad 		dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
   7286  1.26  riastrad 		dev_priv->display.initial_watermarks = vlv_initial_watermarks;
   7287  1.26  riastrad 		dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
   7288  1.26  riastrad 		dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
   7289  1.26  riastrad 	} else if (IS_G4X(dev_priv)) {
   7290  1.26  riastrad 		g4x_setup_wm_latency(dev_priv);
   7291  1.26  riastrad 		dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
   7292  1.26  riastrad 		dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
   7293  1.26  riastrad 		dev_priv->display.initial_watermarks = g4x_initial_watermarks;
   7294  1.26  riastrad 		dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
   7295  1.26  riastrad 	} else if (IS_PINEVIEW(dev_priv)) {
   7296  1.26  riastrad 		if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
   7297   1.1  riastrad 					    dev_priv->is_ddr3,
   7298   1.1  riastrad 					    dev_priv->fsb_freq,
   7299   1.1  riastrad 					    dev_priv->mem_freq)) {
   7300  1.26  riastrad 			drm_info(&dev_priv->drm,
   7301  1.26  riastrad 				 "failed to find known CxSR latency "
   7302   1.1  riastrad 				 "(found ddr%s fsb freq %d, mem freq %d), "
   7303   1.1  riastrad 				 "disabling CxSR\n",
   7304   1.1  riastrad 				 (dev_priv->is_ddr3 == 1) ? "3" : "2",
   7305   1.1  riastrad 				 dev_priv->fsb_freq, dev_priv->mem_freq);
   7306   1.1  riastrad 			/* Disable CxSR and never update its watermark again */
   7307  1.10  riastrad 			intel_set_memory_cxsr(dev_priv, false);
   7308   1.1  riastrad 			dev_priv->display.update_wm = NULL;
   7309   1.1  riastrad 		} else
   7310  1.26  riastrad 			dev_priv->display.update_wm = pnv_update_wm;
   7311  1.26  riastrad 	} else if (IS_GEN(dev_priv, 4)) {
   7312   1.1  riastrad 		dev_priv->display.update_wm = i965_update_wm;
   7313  1.26  riastrad 	} else if (IS_GEN(dev_priv, 3)) {
   7314   1.1  riastrad 		dev_priv->display.update_wm = i9xx_update_wm;
   7315   1.1  riastrad 		dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
   7316  1.26  riastrad 	} else if (IS_GEN(dev_priv, 2)) {
   7317  1.26  riastrad 		if (INTEL_NUM_PIPES(dev_priv) == 1) {
   7318   1.3  riastrad 			dev_priv->display.update_wm = i845_update_wm;
   7319   1.1  riastrad 			dev_priv->display.get_fifo_size = i845_get_fifo_size;
   7320   1.3  riastrad 		} else {
   7321   1.3  riastrad 			dev_priv->display.update_wm = i9xx_update_wm;
   7322   1.1  riastrad 			dev_priv->display.get_fifo_size = i830_get_fifo_size;
   7323   1.1  riastrad 		}
   7324   1.3  riastrad 	} else {
   7325  1.26  riastrad 		drm_err(&dev_priv->drm,
   7326  1.26  riastrad 			"unexpected fall-through in %s\n", __func__);
   7327   1.3  riastrad 	}
   7328  1.10  riastrad }
   7329  1.10  riastrad 
   7330  1.26  riastrad void intel_pm_setup(struct drm_i915_private *dev_priv)
   7331   1.3  riastrad {
   7332  1.26  riastrad 	dev_priv->runtime_pm.suspended = false;
   7333  1.26  riastrad 	atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
   7334   1.3  riastrad }
   7335