1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eugeni Dodonov <eugeni.dodonov@intel.com>
25  *
26  */
27 
28 #include "display/intel_de.h"
29 #include "display/intel_display_trace.h"
30 #include "display/skl_watermark.h"
31 
32 #include "gt/intel_engine_regs.h"
33 #include "gt/intel_gt_regs.h"
34 
35 #include "i915_drv.h"
36 #include "intel_mchbar_regs.h"
37 #include "intel_pm.h"
38 #include "vlv_sideband.h"
39 
40 struct drm_i915_clock_gating_funcs {
41 	void (*init_clock_gating)(struct drm_i915_private *i915);
42 };
43 
44 /* used in computing the new watermarks state */
45 struct intel_wm_config {
46 	unsigned int num_pipes_active;
47 	bool sprites_enabled;
48 	bool sprites_scaled;
49 };
50 
gen9_init_clock_gating(struct drm_i915_private * dev_priv)51 static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
52 {
53 	if (HAS_LLC(dev_priv)) {
54 		/*
55 		 * WaCompressedResourceDisplayNewHashMode:skl,kbl
56 		 * Display WA #0390: skl,kbl
57 		 *
58 		 * Must match Sampler, Pixel Back End, and Media. See
59 		 * WaCompressedResourceSamplerPbeMediaNewHashMode.
60 		 */
61 		intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
62 			   intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) |
63 			   SKL_DE_COMPRESSED_HASH_MODE);
64 	}
65 
66 	/* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
67 	intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
68 		   intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
69 
70 	/* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
71 	intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
72 		   intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
73 
74 	/*
75 	 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
76 	 * Display WA #0859: skl,bxt,kbl,glk,cfl
77 	 */
78 	intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
79 		   DISP_FBC_MEMORY_WAKE);
80 }
81 
bxt_init_clock_gating(struct drm_i915_private * dev_priv)82 static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
83 {
84 	gen9_init_clock_gating(dev_priv);
85 
86 	/* WaDisableSDEUnitClockGating:bxt */
87 	intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
88 		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
89 
90 	/*
91 	 * FIXME:
92 	 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
93 	 */
94 	intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
95 		   GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
96 
97 	/*
98 	 * Wa: Backlight PWM may stop in the asserted state, causing backlight
99 	 * to stay fully on.
100 	 */
101 	intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_0, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_0) |
102 		   PWM1_GATING_DIS | PWM2_GATING_DIS);
103 
104 	/*
105 	 * Lower the display internal timeout.
106 	 * This is needed to avoid any hard hangs when DSI port PLL
107 	 * is off and a MMIO access is attempted by any privilege
108 	 * application, using batch buffers or any other means.
109 	 */
110 	intel_uncore_write(&dev_priv->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
111 
112 	/*
113 	 * WaFbcTurnOffFbcWatermark:bxt
114 	 * Display WA #0562: bxt
115 	 */
116 	intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
117 		   DISP_FBC_WM_DIS);
118 
119 	/*
120 	 * WaFbcHighMemBwCorruptionAvoidance:bxt
121 	 * Display WA #0883: bxt
122 	 */
123 	intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
124 			   intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
125 			   DPFC_DISABLE_DUMMY0);
126 }
127 
glk_init_clock_gating(struct drm_i915_private * dev_priv)128 static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
129 {
130 	gen9_init_clock_gating(dev_priv);
131 
132 	/*
133 	 * WaDisablePWMClockGating:glk
134 	 * Backlight PWM may stop in the asserted state, causing backlight
135 	 * to stay fully on.
136 	 */
137 	intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_0, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_0) |
138 		   PWM1_GATING_DIS | PWM2_GATING_DIS);
139 }
140 
pnv_get_mem_freq(struct drm_i915_private * dev_priv)141 static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
142 {
143 	u32 tmp;
144 
145 	tmp = intel_uncore_read(&dev_priv->uncore, CLKCFG);
146 
147 	switch (tmp & CLKCFG_FSB_MASK) {
148 	case CLKCFG_FSB_533:
149 		dev_priv->fsb_freq = 533; /* 133*4 */
150 		break;
151 	case CLKCFG_FSB_800:
152 		dev_priv->fsb_freq = 800; /* 200*4 */
153 		break;
154 	case CLKCFG_FSB_667:
155 		dev_priv->fsb_freq =  667; /* 167*4 */
156 		break;
157 	case CLKCFG_FSB_400:
158 		dev_priv->fsb_freq = 400; /* 100*4 */
159 		break;
160 	}
161 
162 	switch (tmp & CLKCFG_MEM_MASK) {
163 	case CLKCFG_MEM_533:
164 		dev_priv->mem_freq = 533;
165 		break;
166 	case CLKCFG_MEM_667:
167 		dev_priv->mem_freq = 667;
168 		break;
169 	case CLKCFG_MEM_800:
170 		dev_priv->mem_freq = 800;
171 		break;
172 	}
173 
174 	/* detect pineview DDR3 setting */
175 	tmp = intel_uncore_read(&dev_priv->uncore, CSHRDDR3CTL);
176 	dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
177 }
178 
ilk_get_mem_freq(struct drm_i915_private * dev_priv)179 static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
180 {
181 	u16 ddrpll, csipll;
182 
183 	ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
184 	csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
185 
186 	switch (ddrpll & 0xff) {
187 	case 0xc:
188 		dev_priv->mem_freq = 800;
189 		break;
190 	case 0x10:
191 		dev_priv->mem_freq = 1066;
192 		break;
193 	case 0x14:
194 		dev_priv->mem_freq = 1333;
195 		break;
196 	case 0x18:
197 		dev_priv->mem_freq = 1600;
198 		break;
199 	default:
200 		drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
201 			ddrpll & 0xff);
202 		dev_priv->mem_freq = 0;
203 		break;
204 	}
205 
206 	switch (csipll & 0x3ff) {
207 	case 0x00c:
208 		dev_priv->fsb_freq = 3200;
209 		break;
210 	case 0x00e:
211 		dev_priv->fsb_freq = 3733;
212 		break;
213 	case 0x010:
214 		dev_priv->fsb_freq = 4266;
215 		break;
216 	case 0x012:
217 		dev_priv->fsb_freq = 4800;
218 		break;
219 	case 0x014:
220 		dev_priv->fsb_freq = 5333;
221 		break;
222 	case 0x016:
223 		dev_priv->fsb_freq = 5866;
224 		break;
225 	case 0x018:
226 		dev_priv->fsb_freq = 6400;
227 		break;
228 	default:
229 		drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
230 			csipll & 0x3ff);
231 		dev_priv->fsb_freq = 0;
232 		break;
233 	}
234 }
235 
236 static const struct cxsr_latency cxsr_latency_table[] = {
237 	{1, 0, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
238 	{1, 0, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
239 	{1, 0, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
240 	{1, 1, 800, 667, 6420, 36420, 6873, 36873},    /* DDR3-667 SC */
241 	{1, 1, 800, 800, 5902, 35902, 6318, 36318},    /* DDR3-800 SC */
242 
243 	{1, 0, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
244 	{1, 0, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
245 	{1, 0, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
246 	{1, 1, 667, 667, 6438, 36438, 6911, 36911},    /* DDR3-667 SC */
247 	{1, 1, 667, 800, 5941, 35941, 6377, 36377},    /* DDR3-800 SC */
248 
249 	{1, 0, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
250 	{1, 0, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
251 	{1, 0, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
252 	{1, 1, 400, 667, 6509, 36509, 7062, 37062},    /* DDR3-667 SC */
253 	{1, 1, 400, 800, 5985, 35985, 6501, 36501},    /* DDR3-800 SC */
254 
255 	{0, 0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
256 	{0, 0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
257 	{0, 0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
258 	{0, 1, 800, 667, 6476, 36476, 6955, 36955},    /* DDR3-667 SC */
259 	{0, 1, 800, 800, 5958, 35958, 6400, 36400},    /* DDR3-800 SC */
260 
261 	{0, 0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
262 	{0, 0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
263 	{0, 0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
264 	{0, 1, 667, 667, 6494, 36494, 6993, 36993},    /* DDR3-667 SC */
265 	{0, 1, 667, 800, 5998, 35998, 6460, 36460},    /* DDR3-800 SC */
266 
267 	{0, 0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
268 	{0, 0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
269 	{0, 0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
270 	{0, 1, 400, 667, 6566, 36566, 7145, 37145},    /* DDR3-667 SC */
271 	{0, 1, 400, 800, 6042, 36042, 6584, 36584},    /* DDR3-800 SC */
272 };
273 
intel_get_cxsr_latency(bool is_desktop,bool is_ddr3,int fsb,int mem)274 static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
275 							 bool is_ddr3,
276 							 int fsb,
277 							 int mem)
278 {
279 	const struct cxsr_latency *latency;
280 	int i;
281 
282 	if (fsb == 0 || mem == 0)
283 		return NULL;
284 
285 	for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
286 		latency = &cxsr_latency_table[i];
287 		if (is_desktop == latency->is_desktop &&
288 		    is_ddr3 == latency->is_ddr3 &&
289 		    fsb == latency->fsb_freq && mem == latency->mem_freq)
290 			return latency;
291 	}
292 
293 	DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
294 
295 	return NULL;
296 }
297 
chv_set_memory_dvfs(struct drm_i915_private * dev_priv,bool enable)298 static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
299 {
300 	u32 val;
301 
302 	vlv_punit_get(dev_priv);
303 
304 	val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
305 	if (enable)
306 		val &= ~FORCE_DDR_HIGH_FREQ;
307 	else
308 		val |= FORCE_DDR_HIGH_FREQ;
309 	val &= ~FORCE_DDR_LOW_FREQ;
310 	val |= FORCE_DDR_FREQ_REQ_ACK;
311 	vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
312 
313 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
314 		      FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
315 		drm_err(&dev_priv->drm,
316 			"timed out waiting for Punit DDR DVFS request\n");
317 
318 	vlv_punit_put(dev_priv);
319 }
320 
chv_set_memory_pm5(struct drm_i915_private * dev_priv,bool enable)321 static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
322 {
323 	u32 val;
324 
325 	vlv_punit_get(dev_priv);
326 
327 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
328 	if (enable)
329 		val |= DSP_MAXFIFO_PM5_ENABLE;
330 	else
331 		val &= ~DSP_MAXFIFO_PM5_ENABLE;
332 	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
333 
334 	vlv_punit_put(dev_priv);
335 }
336 
337 #define FW_WM(value, plane) \
338 	(((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
339 
_intel_set_memory_cxsr(struct drm_i915_private * dev_priv,bool enable)340 static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
341 {
342 	bool was_enabled;
343 	u32 val;
344 
345 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
346 		was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
347 		intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
348 		intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF_VLV);
349 	} else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
350 		was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
351 		intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
352 		intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
353 	} else if (IS_PINEVIEW(dev_priv)) {
354 		val = intel_uncore_read(&dev_priv->uncore, DSPFW3);
355 		was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
356 		if (enable)
357 			val |= PINEVIEW_SELF_REFRESH_EN;
358 		else
359 			val &= ~PINEVIEW_SELF_REFRESH_EN;
360 		intel_uncore_write(&dev_priv->uncore, DSPFW3, val);
361 		intel_uncore_posting_read(&dev_priv->uncore, DSPFW3);
362 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
363 		was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
364 		val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
365 			       _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
366 		intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, val);
367 		intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
368 	} else if (IS_I915GM(dev_priv)) {
369 		/*
370 		 * FIXME can't find a bit like this for 915G, and
371 		 * and yet it does have the related watermark in
372 		 * FW_BLC_SELF. What's going on?
373 		 */
374 		was_enabled = intel_uncore_read(&dev_priv->uncore, INSTPM) & INSTPM_SELF_EN;
375 		val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
376 			       _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
377 		intel_uncore_write(&dev_priv->uncore, INSTPM, val);
378 		intel_uncore_posting_read(&dev_priv->uncore, INSTPM);
379 	} else {
380 		return false;
381 	}
382 
383 	trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
384 
385 	drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
386 		    str_enabled_disabled(enable),
387 		    str_enabled_disabled(was_enabled));
388 
389 	return was_enabled;
390 }
391 
392 /**
393  * intel_set_memory_cxsr - Configure CxSR state
394  * @dev_priv: i915 device
395  * @enable: Allow vs. disallow CxSR
396  *
397  * Allow or disallow the system to enter a special CxSR
398  * (C-state self refresh) state. What typically happens in CxSR mode
399  * is that several display FIFOs may get combined into a single larger
400  * FIFO for a particular plane (so called max FIFO mode) to allow the
401  * system to defer memory fetches longer, and the memory will enter
402  * self refresh.
403  *
404  * Note that enabling CxSR does not guarantee that the system enter
405  * this special mode, nor does it guarantee that the system stays
406  * in that mode once entered. So this just allows/disallows the system
407  * to autonomously utilize the CxSR mode. Other factors such as core
408  * C-states will affect when/if the system actually enters/exits the
409  * CxSR mode.
410  *
411  * Note that on VLV/CHV this actually only controls the max FIFO mode,
412  * and the system is free to enter/exit memory self refresh at any time
413  * even when the use of CxSR has been disallowed.
414  *
415  * While the system is actually in the CxSR/max FIFO mode, some plane
416  * control registers will not get latched on vblank. Thus in order to
417  * guarantee the system will respond to changes in the plane registers
418  * we must always disallow CxSR prior to making changes to those registers.
419  * Unfortunately the system will re-evaluate the CxSR conditions at
420  * frame start which happens after vblank start (which is when the plane
421  * registers would get latched), so we can't proceed with the plane update
422  * during the same frame where we disallowed CxSR.
423  *
424  * Certain platforms also have a deeper HPLL SR mode. Fortunately the
425  * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
426  * the hardware w.r.t. HPLL SR when writing to plane registers.
427  * Disallowing just CxSR is sufficient.
428  */
intel_set_memory_cxsr(struct drm_i915_private * dev_priv,bool enable)429 bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
430 {
431 	bool ret;
432 
433 	mutex_lock(&dev_priv->display.wm.wm_mutex);
434 	ret = _intel_set_memory_cxsr(dev_priv, enable);
435 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
436 		dev_priv->display.wm.vlv.cxsr = enable;
437 	else if (IS_G4X(dev_priv))
438 		dev_priv->display.wm.g4x.cxsr = enable;
439 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
440 
441 	return ret;
442 }
443 
444 /*
445  * Latency for FIFO fetches is dependent on several factors:
446  *   - memory configuration (speed, channels)
447  *   - chipset
448  *   - current MCH state
449  * It can be fairly high in some situations, so here we assume a fairly
450  * pessimal value.  It's a tradeoff between extra memory fetches (if we
451  * set this value too high, the FIFO will fetch frequently to stay full)
452  * and power consumption (set it too low to save power and we might see
453  * FIFO underruns and display "flicker").
454  *
455  * A value of 5us seems to be a good balance; safe for very low end
456  * platforms but not overly aggressive on lower latency configs.
457  */
458 static const int pessimal_latency_ns = 5000;
459 
460 #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
461 	((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
462 
vlv_get_fifo_size(struct intel_crtc_state * crtc_state)463 static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
464 {
465 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
466 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
467 	struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
468 	enum pipe pipe = crtc->pipe;
469 	int sprite0_start, sprite1_start;
470 	u32 dsparb, dsparb2, dsparb3;
471 
472 	switch (pipe) {
473 	case PIPE_A:
474 		dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
475 		dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
476 		sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
477 		sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
478 		break;
479 	case PIPE_B:
480 		dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
481 		dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
482 		sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
483 		sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
484 		break;
485 	case PIPE_C:
486 		dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
487 		dsparb3 = intel_uncore_read(&dev_priv->uncore, DSPARB3);
488 		sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
489 		sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
490 		break;
491 	default:
492 		MISSING_CASE(pipe);
493 		return;
494 	}
495 
496 	fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
497 	fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
498 	fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
499 	fifo_state->plane[PLANE_CURSOR] = 63;
500 }
501 
i9xx_get_fifo_size(struct drm_i915_private * dev_priv,enum i9xx_plane_id i9xx_plane)502 static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
503 			      enum i9xx_plane_id i9xx_plane)
504 {
505 	u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
506 	int size;
507 
508 	size = dsparb & 0x7f;
509 	if (i9xx_plane == PLANE_B)
510 		size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
511 
512 	drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
513 		    dsparb, plane_name(i9xx_plane), size);
514 
515 	return size;
516 }
517 
i830_get_fifo_size(struct drm_i915_private * dev_priv,enum i9xx_plane_id i9xx_plane)518 static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
519 			      enum i9xx_plane_id i9xx_plane)
520 {
521 	u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
522 	int size;
523 
524 	size = dsparb & 0x1ff;
525 	if (i9xx_plane == PLANE_B)
526 		size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
527 	size >>= 1; /* Convert to cachelines */
528 
529 	drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
530 		    dsparb, plane_name(i9xx_plane), size);
531 
532 	return size;
533 }
534 
i845_get_fifo_size(struct drm_i915_private * dev_priv,enum i9xx_plane_id i9xx_plane)535 static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
536 			      enum i9xx_plane_id i9xx_plane)
537 {
538 	u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
539 	int size;
540 
541 	size = dsparb & 0x7f;
542 	size >>= 2; /* Convert to cachelines */
543 
544 	drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
545 		    dsparb, plane_name(i9xx_plane), size);
546 
547 	return size;
548 }
549 
550 /* Pineview has different values for various configs */
551 static const struct intel_watermark_params pnv_display_wm = {
552 	.fifo_size = PINEVIEW_DISPLAY_FIFO,
553 	.max_wm = PINEVIEW_MAX_WM,
554 	.default_wm = PINEVIEW_DFT_WM,
555 	.guard_size = PINEVIEW_GUARD_WM,
556 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
557 };
558 
559 static const struct intel_watermark_params pnv_display_hplloff_wm = {
560 	.fifo_size = PINEVIEW_DISPLAY_FIFO,
561 	.max_wm = PINEVIEW_MAX_WM,
562 	.default_wm = PINEVIEW_DFT_HPLLOFF_WM,
563 	.guard_size = PINEVIEW_GUARD_WM,
564 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
565 };
566 
567 static const struct intel_watermark_params pnv_cursor_wm = {
568 	.fifo_size = PINEVIEW_CURSOR_FIFO,
569 	.max_wm = PINEVIEW_CURSOR_MAX_WM,
570 	.default_wm = PINEVIEW_CURSOR_DFT_WM,
571 	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
572 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
573 };
574 
575 static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
576 	.fifo_size = PINEVIEW_CURSOR_FIFO,
577 	.max_wm = PINEVIEW_CURSOR_MAX_WM,
578 	.default_wm = PINEVIEW_CURSOR_DFT_WM,
579 	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
580 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
581 };
582 
583 static const struct intel_watermark_params i965_cursor_wm_info = {
584 	.fifo_size = I965_CURSOR_FIFO,
585 	.max_wm = I965_CURSOR_MAX_WM,
586 	.default_wm = I965_CURSOR_DFT_WM,
587 	.guard_size = 2,
588 	.cacheline_size = I915_FIFO_LINE_SIZE,
589 };
590 
591 static const struct intel_watermark_params i945_wm_info = {
592 	.fifo_size = I945_FIFO_SIZE,
593 	.max_wm = I915_MAX_WM,
594 	.default_wm = 1,
595 	.guard_size = 2,
596 	.cacheline_size = I915_FIFO_LINE_SIZE,
597 };
598 
599 static const struct intel_watermark_params i915_wm_info = {
600 	.fifo_size = I915_FIFO_SIZE,
601 	.max_wm = I915_MAX_WM,
602 	.default_wm = 1,
603 	.guard_size = 2,
604 	.cacheline_size = I915_FIFO_LINE_SIZE,
605 };
606 
607 static const struct intel_watermark_params i830_a_wm_info = {
608 	.fifo_size = I855GM_FIFO_SIZE,
609 	.max_wm = I915_MAX_WM,
610 	.default_wm = 1,
611 	.guard_size = 2,
612 	.cacheline_size = I830_FIFO_LINE_SIZE,
613 };
614 
615 static const struct intel_watermark_params i830_bc_wm_info = {
616 	.fifo_size = I855GM_FIFO_SIZE,
617 	.max_wm = I915_MAX_WM/2,
618 	.default_wm = 1,
619 	.guard_size = 2,
620 	.cacheline_size = I830_FIFO_LINE_SIZE,
621 };
622 
623 static const struct intel_watermark_params i845_wm_info = {
624 	.fifo_size = I830_FIFO_SIZE,
625 	.max_wm = I915_MAX_WM,
626 	.default_wm = 1,
627 	.guard_size = 2,
628 	.cacheline_size = I830_FIFO_LINE_SIZE,
629 };
630 
631 /**
632  * intel_wm_method1 - Method 1 / "small buffer" watermark formula
633  * @pixel_rate: Pipe pixel rate in kHz
634  * @cpp: Plane bytes per pixel
635  * @latency: Memory wakeup latency in 0.1us units
636  *
637  * Compute the watermark using the method 1 or "small buffer"
638  * formula. The caller may additonally add extra cachelines
639  * to account for TLB misses and clock crossings.
640  *
641  * This method is concerned with the short term drain rate
642  * of the FIFO, ie. it does not account for blanking periods
643  * which would effectively reduce the average drain rate across
644  * a longer period. The name "small" refers to the fact the
645  * FIFO is relatively small compared to the amount of data
646  * fetched.
647  *
648  * The FIFO level vs. time graph might look something like:
649  *
650  *   |\   |\
651  *   | \  | \
652  * __---__---__ (- plane active, _ blanking)
653  * -> time
654  *
655  * or perhaps like this:
656  *
657  *   |\|\  |\|\
658  * __----__----__ (- plane active, _ blanking)
659  * -> time
660  *
661  * Returns:
662  * The watermark in bytes
663  */
intel_wm_method1(unsigned int pixel_rate,unsigned int cpp,unsigned int latency)664 static unsigned int intel_wm_method1(unsigned int pixel_rate,
665 				     unsigned int cpp,
666 				     unsigned int latency)
667 {
668 	u64 ret;
669 
670 	ret = mul_u32_u32(pixel_rate, cpp * latency);
671 	ret = DIV_ROUND_UP_ULL(ret, 10000);
672 
673 	return ret;
674 }
675 
676 /**
677  * intel_wm_method2 - Method 2 / "large buffer" watermark formula
678  * @pixel_rate: Pipe pixel rate in kHz
679  * @htotal: Pipe horizontal total
680  * @width: Plane width in pixels
681  * @cpp: Plane bytes per pixel
682  * @latency: Memory wakeup latency in 0.1us units
683  *
684  * Compute the watermark using the method 2 or "large buffer"
685  * formula. The caller may additonally add extra cachelines
686  * to account for TLB misses and clock crossings.
687  *
688  * This method is concerned with the long term drain rate
689  * of the FIFO, ie. it does account for blanking periods
690  * which effectively reduce the average drain rate across
691  * a longer period. The name "large" refers to the fact the
692  * FIFO is relatively large compared to the amount of data
693  * fetched.
694  *
695  * The FIFO level vs. time graph might look something like:
696  *
697  *    |\___       |\___
698  *    |    \___   |    \___
699  *    |        \  |        \
700  * __ --__--__--__--__--__--__ (- plane active, _ blanking)
701  * -> time
702  *
703  * Returns:
704  * The watermark in bytes
705  */
intel_wm_method2(unsigned int pixel_rate,unsigned int htotal,unsigned int width,unsigned int cpp,unsigned int latency)706 static unsigned int intel_wm_method2(unsigned int pixel_rate,
707 				     unsigned int htotal,
708 				     unsigned int width,
709 				     unsigned int cpp,
710 				     unsigned int latency)
711 {
712 	unsigned int ret;
713 
714 	/*
715 	 * FIXME remove once all users are computing
716 	 * watermarks in the correct place.
717 	 */
718 	if (WARN_ON_ONCE(htotal == 0))
719 		htotal = 1;
720 
721 	ret = (latency * pixel_rate) / (htotal * 10000);
722 	ret = (ret + 1) * width * cpp;
723 
724 	return ret;
725 }
726 
727 /**
728  * intel_calculate_wm - calculate watermark level
729  * @pixel_rate: pixel clock
730  * @wm: chip FIFO params
731  * @fifo_size: size of the FIFO buffer
732  * @cpp: bytes per pixel
733  * @latency_ns: memory latency for the platform
734  *
735  * Calculate the watermark level (the level at which the display plane will
736  * start fetching from memory again).  Each chip has a different display
737  * FIFO size and allocation, so the caller needs to figure that out and pass
738  * in the correct intel_watermark_params structure.
739  *
740  * As the pixel clock runs, the FIFO will be drained at a rate that depends
741  * on the pixel size.  When it reaches the watermark level, it'll start
742  * fetching FIFO line sized based chunks from memory until the FIFO fills
743  * past the watermark point.  If the FIFO drains completely, a FIFO underrun
744  * will occur, and a display engine hang could result.
745  */
intel_calculate_wm(int pixel_rate,const struct intel_watermark_params * wm,int fifo_size,int cpp,unsigned int latency_ns)746 static unsigned int intel_calculate_wm(int pixel_rate,
747 				       const struct intel_watermark_params *wm,
748 				       int fifo_size, int cpp,
749 				       unsigned int latency_ns)
750 {
751 	int entries, wm_size;
752 
753 	/*
754 	 * Note: we need to make sure we don't overflow for various clock &
755 	 * latency values.
756 	 * clocks go from a few thousand to several hundred thousand.
757 	 * latency is usually a few thousand
758 	 */
759 	entries = intel_wm_method1(pixel_rate, cpp,
760 				   latency_ns / 100);
761 	entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
762 		wm->guard_size;
763 	DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
764 
765 	wm_size = fifo_size - entries;
766 	DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
767 
768 	/* Don't promote wm_size to unsigned... */
769 	if (wm_size > wm->max_wm)
770 		wm_size = wm->max_wm;
771 	if (wm_size <= 0)
772 		wm_size = wm->default_wm;
773 
774 	/*
775 	 * Bspec seems to indicate that the value shouldn't be lower than
776 	 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
777 	 * Lets go for 8 which is the burst size since certain platforms
778 	 * already use a hardcoded 8 (which is what the spec says should be
779 	 * done).
780 	 */
781 	if (wm_size <= 8)
782 		wm_size = 8;
783 
784 	return wm_size;
785 }
786 
is_disabling(int old,int new,int threshold)787 static bool is_disabling(int old, int new, int threshold)
788 {
789 	return old >= threshold && new < threshold;
790 }
791 
is_enabling(int old,int new,int threshold)792 static bool is_enabling(int old, int new, int threshold)
793 {
794 	return old < threshold && new >= threshold;
795 }
796 
intel_wm_num_levels(struct drm_i915_private * dev_priv)797 static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
798 {
799 	return dev_priv->display.wm.max_level + 1;
800 }
801 
intel_wm_plane_visible(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)802 bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
803 			    const struct intel_plane_state *plane_state)
804 {
805 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
806 
807 	/* FIXME check the 'enable' instead */
808 	if (!crtc_state->hw.active)
809 		return false;
810 
811 	/*
812 	 * Treat cursor with fb as always visible since cursor updates
813 	 * can happen faster than the vrefresh rate, and the current
814 	 * watermark code doesn't handle that correctly. Cursor updates
815 	 * which set/clear the fb or change the cursor size are going
816 	 * to get throttled by intel_legacy_cursor_update() to work
817 	 * around this problem with the watermark code.
818 	 */
819 	if (plane->id == PLANE_CURSOR)
820 		return plane_state->hw.fb != NULL;
821 	else
822 		return plane_state->uapi.visible;
823 }
824 
intel_crtc_active(struct intel_crtc * crtc)825 static bool intel_crtc_active(struct intel_crtc *crtc)
826 {
827 	/* Be paranoid as we can arrive here with only partial
828 	 * state retrieved from the hardware during setup.
829 	 *
830 	 * We can ditch the adjusted_mode.crtc_clock check as soon
831 	 * as Haswell has gained clock readout/fastboot support.
832 	 *
833 	 * We can ditch the crtc->primary->state->fb check as soon as we can
834 	 * properly reconstruct framebuffers.
835 	 *
836 	 * FIXME: The intel_crtc->active here should be switched to
837 	 * crtc->state->active once we have proper CRTC states wired up
838 	 * for atomic.
839 	 */
840 	return crtc && crtc->active && crtc->base.primary->state->fb &&
841 		crtc->config->hw.adjusted_mode.crtc_clock;
842 }
843 
single_enabled_crtc(struct drm_i915_private * dev_priv)844 static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
845 {
846 	struct intel_crtc *crtc, *enabled = NULL;
847 
848 	for_each_intel_crtc(&dev_priv->drm, crtc) {
849 		if (intel_crtc_active(crtc)) {
850 			if (enabled)
851 				return NULL;
852 			enabled = crtc;
853 		}
854 	}
855 
856 	return enabled;
857 }
858 
pnv_update_wm(struct drm_i915_private * dev_priv)859 static void pnv_update_wm(struct drm_i915_private *dev_priv)
860 {
861 	struct intel_crtc *crtc;
862 	const struct cxsr_latency *latency;
863 	u32 reg;
864 	unsigned int wm;
865 
866 	latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
867 					 dev_priv->is_ddr3,
868 					 dev_priv->fsb_freq,
869 					 dev_priv->mem_freq);
870 	if (!latency) {
871 		drm_dbg_kms(&dev_priv->drm,
872 			    "Unknown FSB/MEM found, disable CxSR\n");
873 		intel_set_memory_cxsr(dev_priv, false);
874 		return;
875 	}
876 
877 	crtc = single_enabled_crtc(dev_priv);
878 	if (crtc) {
879 		const struct drm_framebuffer *fb =
880 			crtc->base.primary->state->fb;
881 		int pixel_rate = crtc->config->pixel_rate;
882 		int cpp = fb->format->cpp[0];
883 
884 		/* Display SR */
885 		wm = intel_calculate_wm(pixel_rate, &pnv_display_wm,
886 					pnv_display_wm.fifo_size,
887 					cpp, latency->display_sr);
888 		reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
889 		reg &= ~DSPFW_SR_MASK;
890 		reg |= FW_WM(wm, SR);
891 		intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
892 		drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
893 
894 		/* cursor SR */
895 		wm = intel_calculate_wm(pixel_rate, &pnv_cursor_wm,
896 					pnv_display_wm.fifo_size,
897 					4, latency->cursor_sr);
898 		reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
899 		reg &= ~DSPFW_CURSOR_SR_MASK;
900 		reg |= FW_WM(wm, CURSOR_SR);
901 		intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
902 
903 		/* Display HPLL off SR */
904 		wm = intel_calculate_wm(pixel_rate, &pnv_display_hplloff_wm,
905 					pnv_display_hplloff_wm.fifo_size,
906 					cpp, latency->display_hpll_disable);
907 		reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
908 		reg &= ~DSPFW_HPLL_SR_MASK;
909 		reg |= FW_WM(wm, HPLL_SR);
910 		intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
911 
912 		/* cursor HPLL off SR */
913 		wm = intel_calculate_wm(pixel_rate, &pnv_cursor_hplloff_wm,
914 					pnv_display_hplloff_wm.fifo_size,
915 					4, latency->cursor_hpll_disable);
916 		reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
917 		reg &= ~DSPFW_HPLL_CURSOR_MASK;
918 		reg |= FW_WM(wm, HPLL_CURSOR);
919 		intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
920 		drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
921 
922 		intel_set_memory_cxsr(dev_priv, true);
923 	} else {
924 		intel_set_memory_cxsr(dev_priv, false);
925 	}
926 }
927 
928 /*
929  * Documentation says:
930  * "If the line size is small, the TLB fetches can get in the way of the
931  *  data fetches, causing some lag in the pixel data return which is not
932  *  accounted for in the above formulas. The following adjustment only
933  *  needs to be applied if eight whole lines fit in the buffer at once.
934  *  The WM is adjusted upwards by the difference between the FIFO size
935  *  and the size of 8 whole lines. This adjustment is always performed
936  *  in the actual pixel depth regardless of whether FBC is enabled or not."
937  */
g4x_tlb_miss_wa(int fifo_size,int width,int cpp)938 static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
939 {
940 	int tlb_miss = fifo_size * 64 - width * cpp * 8;
941 
942 	return max(0, tlb_miss);
943 }
944 
g4x_write_wm_values(struct drm_i915_private * dev_priv,const struct g4x_wm_values * wm)945 static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
946 				const struct g4x_wm_values *wm)
947 {
948 	enum pipe pipe;
949 
950 	for_each_pipe(dev_priv, pipe)
951 		trace_g4x_wm(intel_crtc_for_pipe(dev_priv, pipe), wm);
952 
953 	intel_uncore_write(&dev_priv->uncore, DSPFW1,
954 		   FW_WM(wm->sr.plane, SR) |
955 		   FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
956 		   FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
957 		   FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
958 	intel_uncore_write(&dev_priv->uncore, DSPFW2,
959 		   (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
960 		   FW_WM(wm->sr.fbc, FBC_SR) |
961 		   FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
962 		   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
963 		   FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
964 		   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
965 	intel_uncore_write(&dev_priv->uncore, DSPFW3,
966 		   (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
967 		   FW_WM(wm->sr.cursor, CURSOR_SR) |
968 		   FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
969 		   FW_WM(wm->hpll.plane, HPLL_SR));
970 
971 	intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
972 }
973 
974 #define FW_WM_VLV(value, plane) \
975 	(((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
976 
vlv_write_wm_values(struct drm_i915_private * dev_priv,const struct vlv_wm_values * wm)977 static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
978 				const struct vlv_wm_values *wm)
979 {
980 	enum pipe pipe;
981 
982 	for_each_pipe(dev_priv, pipe) {
983 		trace_vlv_wm(intel_crtc_for_pipe(dev_priv, pipe), wm);
984 
985 		intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
986 			   (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
987 			   (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
988 			   (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
989 			   (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
990 	}
991 
992 	/*
993 	 * Zero the (unused) WM1 watermarks, and also clear all the
994 	 * high order bits so that there are no out of bounds values
995 	 * present in the registers during the reprogramming.
996 	 */
997 	intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
998 	intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
999 	intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1000 	intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1001 	intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
1002 
1003 	intel_uncore_write(&dev_priv->uncore, DSPFW1,
1004 		   FW_WM(wm->sr.plane, SR) |
1005 		   FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1006 		   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1007 		   FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
1008 	intel_uncore_write(&dev_priv->uncore, DSPFW2,
1009 		   FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1010 		   FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1011 		   FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
1012 	intel_uncore_write(&dev_priv->uncore, DSPFW3,
1013 		   FW_WM(wm->sr.cursor, CURSOR_SR));
1014 
1015 	if (IS_CHERRYVIEW(dev_priv)) {
1016 		intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
1017 			   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1018 			   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
1019 		intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
1020 			   FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1021 			   FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
1022 		intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
1023 			   FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1024 			   FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
1025 		intel_uncore_write(&dev_priv->uncore, DSPHOWM,
1026 			   FW_WM(wm->sr.plane >> 9, SR_HI) |
1027 			   FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1028 			   FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1029 			   FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1030 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1031 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1032 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1033 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1034 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1035 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
1036 	} else {
1037 		intel_uncore_write(&dev_priv->uncore, DSPFW7,
1038 			   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1039 			   FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
1040 		intel_uncore_write(&dev_priv->uncore, DSPHOWM,
1041 			   FW_WM(wm->sr.plane >> 9, SR_HI) |
1042 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1043 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1044 			   FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1045 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1046 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1047 			   FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
1048 	}
1049 
1050 	intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
1051 }
1052 
1053 #undef FW_WM_VLV
1054 
g4x_setup_wm_latency(struct drm_i915_private * dev_priv)1055 static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1056 {
1057 	/* all latencies in usec */
1058 	dev_priv->display.wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1059 	dev_priv->display.wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
1060 	dev_priv->display.wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
1061 
1062 	dev_priv->display.wm.max_level = G4X_WM_LEVEL_HPLL;
1063 }
1064 
g4x_plane_fifo_size(enum plane_id plane_id,int level)1065 static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1066 {
1067 	/*
1068 	 * DSPCNTR[13] supposedly controls whether the
1069 	 * primary plane can use the FIFO space otherwise
1070 	 * reserved for the sprite plane. It's not 100% clear
1071 	 * what the actual FIFO size is, but it looks like we
1072 	 * can happily set both primary and sprite watermarks
1073 	 * up to 127 cachelines. So that would seem to mean
1074 	 * that either DSPCNTR[13] doesn't do anything, or that
1075 	 * the total FIFO is >= 256 cachelines in size. Either
1076 	 * way, we don't seem to have to worry about this
1077 	 * repartitioning as the maximum watermark value the
1078 	 * register can hold for each plane is lower than the
1079 	 * minimum FIFO size.
1080 	 */
1081 	switch (plane_id) {
1082 	case PLANE_CURSOR:
1083 		return 63;
1084 	case PLANE_PRIMARY:
1085 		return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1086 	case PLANE_SPRITE0:
1087 		return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1088 	default:
1089 		MISSING_CASE(plane_id);
1090 		return 0;
1091 	}
1092 }
1093 
g4x_fbc_fifo_size(int level)1094 static int g4x_fbc_fifo_size(int level)
1095 {
1096 	switch (level) {
1097 	case G4X_WM_LEVEL_SR:
1098 		return 7;
1099 	case G4X_WM_LEVEL_HPLL:
1100 		return 15;
1101 	default:
1102 		MISSING_CASE(level);
1103 		return 0;
1104 	}
1105 }
1106 
g4x_compute_wm(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,int level)1107 static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1108 			  const struct intel_plane_state *plane_state,
1109 			  int level)
1110 {
1111 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1112 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1113 	const struct drm_display_mode *pipe_mode =
1114 		&crtc_state->hw.pipe_mode;
1115 	unsigned int latency = dev_priv->display.wm.pri_latency[level] * 10;
1116 	unsigned int pixel_rate, htotal, cpp, width, wm;
1117 
1118 	if (latency == 0)
1119 		return USHRT_MAX;
1120 
1121 	if (!intel_wm_plane_visible(crtc_state, plane_state))
1122 		return 0;
1123 
1124 	cpp = plane_state->hw.fb->format->cpp[0];
1125 
1126 	/*
1127 	 * WaUse32BppForSRWM:ctg,elk
1128 	 *
1129 	 * The spec fails to list this restriction for the
1130 	 * HPLL watermark, which seems a little strange.
1131 	 * Let's use 32bpp for the HPLL watermark as well.
1132 	 */
1133 	if (plane->id == PLANE_PRIMARY &&
1134 	    level != G4X_WM_LEVEL_NORMAL)
1135 		cpp = max(cpp, 4u);
1136 
1137 	pixel_rate = crtc_state->pixel_rate;
1138 	htotal = pipe_mode->crtc_htotal;
1139 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
1140 
1141 	if (plane->id == PLANE_CURSOR) {
1142 		wm = intel_wm_method2(pixel_rate, htotal, width, cpp, latency);
1143 	} else if (plane->id == PLANE_PRIMARY &&
1144 		   level == G4X_WM_LEVEL_NORMAL) {
1145 		wm = intel_wm_method1(pixel_rate, cpp, latency);
1146 	} else {
1147 		unsigned int small, large;
1148 
1149 		small = intel_wm_method1(pixel_rate, cpp, latency);
1150 		large = intel_wm_method2(pixel_rate, htotal, width, cpp, latency);
1151 
1152 		wm = min(small, large);
1153 	}
1154 
1155 	wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1156 			      width, cpp);
1157 
1158 	wm = DIV_ROUND_UP(wm, 64) + 2;
1159 
1160 	return min_t(unsigned int, wm, USHRT_MAX);
1161 }
1162 
g4x_raw_plane_wm_set(struct intel_crtc_state * crtc_state,int level,enum plane_id plane_id,u16 value)1163 static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1164 				 int level, enum plane_id plane_id, u16 value)
1165 {
1166 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1167 	bool dirty = false;
1168 
1169 	for (; level < intel_wm_num_levels(dev_priv); level++) {
1170 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1171 
1172 		dirty |= raw->plane[plane_id] != value;
1173 		raw->plane[plane_id] = value;
1174 	}
1175 
1176 	return dirty;
1177 }
1178 
g4x_raw_fbc_wm_set(struct intel_crtc_state * crtc_state,int level,u16 value)1179 static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1180 			       int level, u16 value)
1181 {
1182 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1183 	bool dirty = false;
1184 
1185 	/* NORMAL level doesn't have an FBC watermark */
1186 	level = max(level, G4X_WM_LEVEL_SR);
1187 
1188 	for (; level < intel_wm_num_levels(dev_priv); level++) {
1189 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1190 
1191 		dirty |= raw->fbc != value;
1192 		raw->fbc = value;
1193 	}
1194 
1195 	return dirty;
1196 }
1197 
1198 static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1199 			      const struct intel_plane_state *plane_state,
1200 			      u32 pri_val);
1201 
g4x_raw_plane_wm_compute(struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)1202 static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1203 				     const struct intel_plane_state *plane_state)
1204 {
1205 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1206 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1207 	int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1208 	enum plane_id plane_id = plane->id;
1209 	bool dirty = false;
1210 	int level;
1211 
1212 	if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1213 		dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1214 		if (plane_id == PLANE_PRIMARY)
1215 			dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1216 		goto out;
1217 	}
1218 
1219 	for (level = 0; level < num_levels; level++) {
1220 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1221 		int wm, max_wm;
1222 
1223 		wm = g4x_compute_wm(crtc_state, plane_state, level);
1224 		max_wm = g4x_plane_fifo_size(plane_id, level);
1225 
1226 		if (wm > max_wm)
1227 			break;
1228 
1229 		dirty |= raw->plane[plane_id] != wm;
1230 		raw->plane[plane_id] = wm;
1231 
1232 		if (plane_id != PLANE_PRIMARY ||
1233 		    level == G4X_WM_LEVEL_NORMAL)
1234 			continue;
1235 
1236 		wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1237 					raw->plane[plane_id]);
1238 		max_wm = g4x_fbc_fifo_size(level);
1239 
1240 		/*
1241 		 * FBC wm is not mandatory as we
1242 		 * can always just disable its use.
1243 		 */
1244 		if (wm > max_wm)
1245 			wm = USHRT_MAX;
1246 
1247 		dirty |= raw->fbc != wm;
1248 		raw->fbc = wm;
1249 	}
1250 
1251 	/* mark watermarks as invalid */
1252 	dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1253 
1254 	if (plane_id == PLANE_PRIMARY)
1255 		dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1256 
1257  out:
1258 	if (dirty) {
1259 		drm_dbg_kms(&dev_priv->drm,
1260 			    "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1261 			    plane->base.name,
1262 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1263 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1264 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1265 
1266 		if (plane_id == PLANE_PRIMARY)
1267 			drm_dbg_kms(&dev_priv->drm,
1268 				    "FBC watermarks: SR=%d, HPLL=%d\n",
1269 				    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1270 				    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1271 	}
1272 
1273 	return dirty;
1274 }
1275 
g4x_raw_plane_wm_is_valid(const struct intel_crtc_state * crtc_state,enum plane_id plane_id,int level)1276 static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1277 				      enum plane_id plane_id, int level)
1278 {
1279 	const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1280 
1281 	return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1282 }
1283 
g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state * crtc_state,int level)1284 static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1285 				     int level)
1286 {
1287 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1288 
1289 	if (level > dev_priv->display.wm.max_level)
1290 		return false;
1291 
1292 	return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1293 		g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1294 		g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1295 }
1296 
1297 /* mark all levels starting from 'level' as invalid */
g4x_invalidate_wms(struct intel_crtc * crtc,struct g4x_wm_state * wm_state,int level)1298 static void g4x_invalidate_wms(struct intel_crtc *crtc,
1299 			       struct g4x_wm_state *wm_state, int level)
1300 {
1301 	if (level <= G4X_WM_LEVEL_NORMAL) {
1302 		enum plane_id plane_id;
1303 
1304 		for_each_plane_id_on_crtc(crtc, plane_id)
1305 			wm_state->wm.plane[plane_id] = USHRT_MAX;
1306 	}
1307 
1308 	if (level <= G4X_WM_LEVEL_SR) {
1309 		wm_state->cxsr = false;
1310 		wm_state->sr.cursor = USHRT_MAX;
1311 		wm_state->sr.plane = USHRT_MAX;
1312 		wm_state->sr.fbc = USHRT_MAX;
1313 	}
1314 
1315 	if (level <= G4X_WM_LEVEL_HPLL) {
1316 		wm_state->hpll_en = false;
1317 		wm_state->hpll.cursor = USHRT_MAX;
1318 		wm_state->hpll.plane = USHRT_MAX;
1319 		wm_state->hpll.fbc = USHRT_MAX;
1320 	}
1321 }
1322 
g4x_compute_fbc_en(const struct g4x_wm_state * wm_state,int level)1323 static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1324 			       int level)
1325 {
1326 	if (level < G4X_WM_LEVEL_SR)
1327 		return false;
1328 
1329 	if (level >= G4X_WM_LEVEL_SR &&
1330 	    wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1331 		return false;
1332 
1333 	if (level >= G4X_WM_LEVEL_HPLL &&
1334 	    wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1335 		return false;
1336 
1337 	return true;
1338 }
1339 
g4x_compute_pipe_wm(struct intel_atomic_state * state,struct intel_crtc * crtc)1340 static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
1341 			       struct intel_crtc *crtc)
1342 {
1343 	struct intel_crtc_state *crtc_state =
1344 		intel_atomic_get_new_crtc_state(state, crtc);
1345 	struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1346 	u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1347 	const struct g4x_pipe_wm *raw;
1348 	const struct intel_plane_state *old_plane_state;
1349 	const struct intel_plane_state *new_plane_state;
1350 	struct intel_plane *plane;
1351 	enum plane_id plane_id;
1352 	int i, level;
1353 	unsigned int dirty = 0;
1354 
1355 	for_each_oldnew_intel_plane_in_state(state, plane,
1356 					     old_plane_state,
1357 					     new_plane_state, i) {
1358 		if (new_plane_state->hw.crtc != &crtc->base &&
1359 		    old_plane_state->hw.crtc != &crtc->base)
1360 			continue;
1361 
1362 		if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
1363 			dirty |= BIT(plane->id);
1364 	}
1365 
1366 	if (!dirty)
1367 		return 0;
1368 
1369 	level = G4X_WM_LEVEL_NORMAL;
1370 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1371 		goto out;
1372 
1373 	raw = &crtc_state->wm.g4x.raw[level];
1374 	for_each_plane_id_on_crtc(crtc, plane_id)
1375 		wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1376 
1377 	level = G4X_WM_LEVEL_SR;
1378 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1379 		goto out;
1380 
1381 	raw = &crtc_state->wm.g4x.raw[level];
1382 	wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1383 	wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1384 	wm_state->sr.fbc = raw->fbc;
1385 
1386 	wm_state->cxsr = active_planes == BIT(PLANE_PRIMARY);
1387 
1388 	level = G4X_WM_LEVEL_HPLL;
1389 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1390 		goto out;
1391 
1392 	raw = &crtc_state->wm.g4x.raw[level];
1393 	wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1394 	wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1395 	wm_state->hpll.fbc = raw->fbc;
1396 
1397 	wm_state->hpll_en = wm_state->cxsr;
1398 
1399 	level++;
1400 
1401  out:
1402 	if (level == G4X_WM_LEVEL_NORMAL)
1403 		return -EINVAL;
1404 
1405 	/* invalidate the higher levels */
1406 	g4x_invalidate_wms(crtc, wm_state, level);
1407 
1408 	/*
1409 	 * Determine if the FBC watermark(s) can be used. IF
1410 	 * this isn't the case we prefer to disable the FBC
1411 	 * watermark(s) rather than disable the SR/HPLL
1412 	 * level(s) entirely. 'level-1' is the highest valid
1413 	 * level here.
1414 	 */
1415 	wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
1416 
1417 	return 0;
1418 }
1419 
g4x_compute_intermediate_wm(struct intel_atomic_state * state,struct intel_crtc * crtc)1420 static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
1421 				       struct intel_crtc *crtc)
1422 {
1423 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1424 	struct intel_crtc_state *new_crtc_state =
1425 		intel_atomic_get_new_crtc_state(state, crtc);
1426 	const struct intel_crtc_state *old_crtc_state =
1427 		intel_atomic_get_old_crtc_state(state, crtc);
1428 	struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1429 	const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1430 	const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
1431 	enum plane_id plane_id;
1432 
1433 	if (!new_crtc_state->hw.active ||
1434 	    drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
1435 		*intermediate = *optimal;
1436 
1437 		intermediate->cxsr = false;
1438 		intermediate->hpll_en = false;
1439 		goto out;
1440 	}
1441 
1442 	intermediate->cxsr = optimal->cxsr && active->cxsr &&
1443 		!new_crtc_state->disable_cxsr;
1444 	intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1445 		!new_crtc_state->disable_cxsr;
1446 	intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1447 
1448 	for_each_plane_id_on_crtc(crtc, plane_id) {
1449 		intermediate->wm.plane[plane_id] =
1450 			max(optimal->wm.plane[plane_id],
1451 			    active->wm.plane[plane_id]);
1452 
1453 		drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1454 			    g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1455 	}
1456 
1457 	intermediate->sr.plane = max(optimal->sr.plane,
1458 				     active->sr.plane);
1459 	intermediate->sr.cursor = max(optimal->sr.cursor,
1460 				      active->sr.cursor);
1461 	intermediate->sr.fbc = max(optimal->sr.fbc,
1462 				   active->sr.fbc);
1463 
1464 	intermediate->hpll.plane = max(optimal->hpll.plane,
1465 				       active->hpll.plane);
1466 	intermediate->hpll.cursor = max(optimal->hpll.cursor,
1467 					active->hpll.cursor);
1468 	intermediate->hpll.fbc = max(optimal->hpll.fbc,
1469 				     active->hpll.fbc);
1470 
1471 	drm_WARN_ON(&dev_priv->drm,
1472 		    (intermediate->sr.plane >
1473 		     g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1474 		     intermediate->sr.cursor >
1475 		     g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1476 		    intermediate->cxsr);
1477 	drm_WARN_ON(&dev_priv->drm,
1478 		    (intermediate->sr.plane >
1479 		     g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1480 		     intermediate->sr.cursor >
1481 		     g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1482 		    intermediate->hpll_en);
1483 
1484 	drm_WARN_ON(&dev_priv->drm,
1485 		    intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1486 		    intermediate->fbc_en && intermediate->cxsr);
1487 	drm_WARN_ON(&dev_priv->drm,
1488 		    intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1489 		    intermediate->fbc_en && intermediate->hpll_en);
1490 
1491 out:
1492 	/*
1493 	 * If our intermediate WM are identical to the final WM, then we can
1494 	 * omit the post-vblank programming; only update if it's different.
1495 	 */
1496 	if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1497 		new_crtc_state->wm.need_postvbl_update = true;
1498 
1499 	return 0;
1500 }
1501 
g4x_merge_wm(struct drm_i915_private * dev_priv,struct g4x_wm_values * wm)1502 static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1503 			 struct g4x_wm_values *wm)
1504 {
1505 	struct intel_crtc *crtc;
1506 	int num_active_pipes = 0;
1507 
1508 	wm->cxsr = true;
1509 	wm->hpll_en = true;
1510 	wm->fbc_en = true;
1511 
1512 	for_each_intel_crtc(&dev_priv->drm, crtc) {
1513 		const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1514 
1515 		if (!crtc->active)
1516 			continue;
1517 
1518 		if (!wm_state->cxsr)
1519 			wm->cxsr = false;
1520 		if (!wm_state->hpll_en)
1521 			wm->hpll_en = false;
1522 		if (!wm_state->fbc_en)
1523 			wm->fbc_en = false;
1524 
1525 		num_active_pipes++;
1526 	}
1527 
1528 	if (num_active_pipes != 1) {
1529 		wm->cxsr = false;
1530 		wm->hpll_en = false;
1531 		wm->fbc_en = false;
1532 	}
1533 
1534 	for_each_intel_crtc(&dev_priv->drm, crtc) {
1535 		const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1536 		enum pipe pipe = crtc->pipe;
1537 
1538 		wm->pipe[pipe] = wm_state->wm;
1539 		if (crtc->active && wm->cxsr)
1540 			wm->sr = wm_state->sr;
1541 		if (crtc->active && wm->hpll_en)
1542 			wm->hpll = wm_state->hpll;
1543 	}
1544 }
1545 
g4x_program_watermarks(struct drm_i915_private * dev_priv)1546 static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1547 {
1548 	struct g4x_wm_values *old_wm = &dev_priv->display.wm.g4x;
1549 	struct g4x_wm_values new_wm = {};
1550 
1551 	g4x_merge_wm(dev_priv, &new_wm);
1552 
1553 	if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1554 		return;
1555 
1556 	if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1557 		_intel_set_memory_cxsr(dev_priv, false);
1558 
1559 	g4x_write_wm_values(dev_priv, &new_wm);
1560 
1561 	if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1562 		_intel_set_memory_cxsr(dev_priv, true);
1563 
1564 	*old_wm = new_wm;
1565 }
1566 
g4x_initial_watermarks(struct intel_atomic_state * state,struct intel_crtc * crtc)1567 static void g4x_initial_watermarks(struct intel_atomic_state *state,
1568 				   struct intel_crtc *crtc)
1569 {
1570 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1571 	const struct intel_crtc_state *crtc_state =
1572 		intel_atomic_get_new_crtc_state(state, crtc);
1573 
1574 	mutex_lock(&dev_priv->display.wm.wm_mutex);
1575 	crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1576 	g4x_program_watermarks(dev_priv);
1577 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
1578 }
1579 
g4x_optimize_watermarks(struct intel_atomic_state * state,struct intel_crtc * crtc)1580 static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1581 				    struct intel_crtc *crtc)
1582 {
1583 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1584 	const struct intel_crtc_state *crtc_state =
1585 		intel_atomic_get_new_crtc_state(state, crtc);
1586 
1587 	if (!crtc_state->wm.need_postvbl_update)
1588 		return;
1589 
1590 	mutex_lock(&dev_priv->display.wm.wm_mutex);
1591 	crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1592 	g4x_program_watermarks(dev_priv);
1593 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
1594 }
1595 
1596 /* latency must be in 0.1us units. */
vlv_wm_method2(unsigned int pixel_rate,unsigned int htotal,unsigned int width,unsigned int cpp,unsigned int latency)1597 static unsigned int vlv_wm_method2(unsigned int pixel_rate,
1598 				   unsigned int htotal,
1599 				   unsigned int width,
1600 				   unsigned int cpp,
1601 				   unsigned int latency)
1602 {
1603 	unsigned int ret;
1604 
1605 	ret = intel_wm_method2(pixel_rate, htotal,
1606 			       width, cpp, latency);
1607 	ret = DIV_ROUND_UP(ret, 64);
1608 
1609 	return ret;
1610 }
1611 
vlv_setup_wm_latency(struct drm_i915_private * dev_priv)1612 static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
1613 {
1614 	/* all latencies in usec */
1615 	dev_priv->display.wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1616 
1617 	dev_priv->display.wm.max_level = VLV_WM_LEVEL_PM2;
1618 
1619 	if (IS_CHERRYVIEW(dev_priv)) {
1620 		dev_priv->display.wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1621 		dev_priv->display.wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
1622 
1623 		dev_priv->display.wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
1624 	}
1625 }
1626 
vlv_compute_wm_level(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,int level)1627 static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1628 				const struct intel_plane_state *plane_state,
1629 				int level)
1630 {
1631 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1632 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1633 	const struct drm_display_mode *pipe_mode =
1634 		&crtc_state->hw.pipe_mode;
1635 	unsigned int pixel_rate, htotal, cpp, width, wm;
1636 
1637 	if (dev_priv->display.wm.pri_latency[level] == 0)
1638 		return USHRT_MAX;
1639 
1640 	if (!intel_wm_plane_visible(crtc_state, plane_state))
1641 		return 0;
1642 
1643 	cpp = plane_state->hw.fb->format->cpp[0];
1644 	pixel_rate = crtc_state->pixel_rate;
1645 	htotal = pipe_mode->crtc_htotal;
1646 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
1647 
1648 	if (plane->id == PLANE_CURSOR) {
1649 		/*
1650 		 * FIXME the formula gives values that are
1651 		 * too big for the cursor FIFO, and hence we
1652 		 * would never be able to use cursors. For
1653 		 * now just hardcode the watermark.
1654 		 */
1655 		wm = 63;
1656 	} else {
1657 		wm = vlv_wm_method2(pixel_rate, htotal, width, cpp,
1658 				    dev_priv->display.wm.pri_latency[level] * 10);
1659 	}
1660 
1661 	return min_t(unsigned int, wm, USHRT_MAX);
1662 }
1663 
vlv_need_sprite0_fifo_workaround(unsigned int active_planes)1664 static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1665 {
1666 	return (active_planes & (BIT(PLANE_SPRITE0) |
1667 				 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1668 }
1669 
vlv_compute_fifo(struct intel_crtc_state * crtc_state)1670 static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
1671 {
1672 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1673 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1674 	const struct g4x_pipe_wm *raw =
1675 		&crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
1676 	struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
1677 	u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1678 	int num_active_planes = hweight8(active_planes);
1679 	const int fifo_size = 511;
1680 	int fifo_extra, fifo_left = fifo_size;
1681 	int sprite0_fifo_extra = 0;
1682 	unsigned int total_rate;
1683 	enum plane_id plane_id;
1684 
1685 	/*
1686 	 * When enabling sprite0 after sprite1 has already been enabled
1687 	 * we tend to get an underrun unless sprite0 already has some
1688 	 * FIFO space allcoated. Hence we always allocate at least one
1689 	 * cacheline for sprite0 whenever sprite1 is enabled.
1690 	 *
1691 	 * All other plane enable sequences appear immune to this problem.
1692 	 */
1693 	if (vlv_need_sprite0_fifo_workaround(active_planes))
1694 		sprite0_fifo_extra = 1;
1695 
1696 	total_rate = raw->plane[PLANE_PRIMARY] +
1697 		raw->plane[PLANE_SPRITE0] +
1698 		raw->plane[PLANE_SPRITE1] +
1699 		sprite0_fifo_extra;
1700 
1701 	if (total_rate > fifo_size)
1702 		return -EINVAL;
1703 
1704 	if (total_rate == 0)
1705 		total_rate = 1;
1706 
1707 	for_each_plane_id_on_crtc(crtc, plane_id) {
1708 		unsigned int rate;
1709 
1710 		if ((active_planes & BIT(plane_id)) == 0) {
1711 			fifo_state->plane[plane_id] = 0;
1712 			continue;
1713 		}
1714 
1715 		rate = raw->plane[plane_id];
1716 		fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1717 		fifo_left -= fifo_state->plane[plane_id];
1718 	}
1719 
1720 	fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1721 	fifo_left -= sprite0_fifo_extra;
1722 
1723 	fifo_state->plane[PLANE_CURSOR] = 63;
1724 
1725 	fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
1726 
1727 	/* spread the remainder evenly */
1728 	for_each_plane_id_on_crtc(crtc, plane_id) {
1729 		int plane_extra;
1730 
1731 		if (fifo_left == 0)
1732 			break;
1733 
1734 		if ((active_planes & BIT(plane_id)) == 0)
1735 			continue;
1736 
1737 		plane_extra = min(fifo_extra, fifo_left);
1738 		fifo_state->plane[plane_id] += plane_extra;
1739 		fifo_left -= plane_extra;
1740 	}
1741 
1742 	drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
1743 
1744 	/* give it all to the first plane if none are active */
1745 	if (active_planes == 0) {
1746 		drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
1747 		fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1748 	}
1749 
1750 	return 0;
1751 }
1752 
1753 /* mark all levels starting from 'level' as invalid */
vlv_invalidate_wms(struct intel_crtc * crtc,struct vlv_wm_state * wm_state,int level)1754 static void vlv_invalidate_wms(struct intel_crtc *crtc,
1755 			       struct vlv_wm_state *wm_state, int level)
1756 {
1757 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1758 
1759 	for (; level < intel_wm_num_levels(dev_priv); level++) {
1760 		enum plane_id plane_id;
1761 
1762 		for_each_plane_id_on_crtc(crtc, plane_id)
1763 			wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1764 
1765 		wm_state->sr[level].cursor = USHRT_MAX;
1766 		wm_state->sr[level].plane = USHRT_MAX;
1767 	}
1768 }
1769 
vlv_invert_wm_value(u16 wm,u16 fifo_size)1770 static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1771 {
1772 	if (wm > fifo_size)
1773 		return USHRT_MAX;
1774 	else
1775 		return fifo_size - wm;
1776 }
1777 
1778 /*
1779  * Starting from 'level' set all higher
1780  * levels to 'value' in the "raw" watermarks.
1781  */
vlv_raw_plane_wm_set(struct intel_crtc_state * crtc_state,int level,enum plane_id plane_id,u16 value)1782 static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1783 				 int level, enum plane_id plane_id, u16 value)
1784 {
1785 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1786 	int num_levels = intel_wm_num_levels(dev_priv);
1787 	bool dirty = false;
1788 
1789 	for (; level < num_levels; level++) {
1790 		struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1791 
1792 		dirty |= raw->plane[plane_id] != value;
1793 		raw->plane[plane_id] = value;
1794 	}
1795 
1796 	return dirty;
1797 }
1798 
vlv_raw_plane_wm_compute(struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state)1799 static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1800 				     const struct intel_plane_state *plane_state)
1801 {
1802 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1803 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1804 	enum plane_id plane_id = plane->id;
1805 	int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1806 	int level;
1807 	bool dirty = false;
1808 
1809 	if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1810 		dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1811 		goto out;
1812 	}
1813 
1814 	for (level = 0; level < num_levels; level++) {
1815 		struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1816 		int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1817 		int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1818 
1819 		if (wm > max_wm)
1820 			break;
1821 
1822 		dirty |= raw->plane[plane_id] != wm;
1823 		raw->plane[plane_id] = wm;
1824 	}
1825 
1826 	/* mark all higher levels as invalid */
1827 	dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1828 
1829 out:
1830 	if (dirty)
1831 		drm_dbg_kms(&dev_priv->drm,
1832 			    "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1833 			    plane->base.name,
1834 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1835 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1836 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1837 
1838 	return dirty;
1839 }
1840 
vlv_raw_plane_wm_is_valid(const struct intel_crtc_state * crtc_state,enum plane_id plane_id,int level)1841 static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1842 				      enum plane_id plane_id, int level)
1843 {
1844 	const struct g4x_pipe_wm *raw =
1845 		&crtc_state->wm.vlv.raw[level];
1846 	const struct vlv_fifo_state *fifo_state =
1847 		&crtc_state->wm.vlv.fifo_state;
1848 
1849 	return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1850 }
1851 
vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state * crtc_state,int level)1852 static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
1853 {
1854 	return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1855 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1856 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1857 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1858 }
1859 
vlv_compute_pipe_wm(struct intel_atomic_state * state,struct intel_crtc * crtc)1860 static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
1861 			       struct intel_crtc *crtc)
1862 {
1863 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1864 	struct intel_crtc_state *crtc_state =
1865 		intel_atomic_get_new_crtc_state(state, crtc);
1866 	struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
1867 	const struct vlv_fifo_state *fifo_state =
1868 		&crtc_state->wm.vlv.fifo_state;
1869 	u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1870 	int num_active_planes = hweight8(active_planes);
1871 	bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
1872 	const struct intel_plane_state *old_plane_state;
1873 	const struct intel_plane_state *new_plane_state;
1874 	struct intel_plane *plane;
1875 	enum plane_id plane_id;
1876 	int level, ret, i;
1877 	unsigned int dirty = 0;
1878 
1879 	for_each_oldnew_intel_plane_in_state(state, plane,
1880 					     old_plane_state,
1881 					     new_plane_state, i) {
1882 		if (new_plane_state->hw.crtc != &crtc->base &&
1883 		    old_plane_state->hw.crtc != &crtc->base)
1884 			continue;
1885 
1886 		if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
1887 			dirty |= BIT(plane->id);
1888 	}
1889 
1890 	/*
1891 	 * DSPARB registers may have been reset due to the
1892 	 * power well being turned off. Make sure we restore
1893 	 * them to a consistent state even if no primary/sprite
1894 	 * planes are initially active.
1895 	 */
1896 	if (needs_modeset)
1897 		crtc_state->fifo_changed = true;
1898 
1899 	if (!dirty)
1900 		return 0;
1901 
1902 	/* cursor changes don't warrant a FIFO recompute */
1903 	if (dirty & ~BIT(PLANE_CURSOR)) {
1904 		const struct intel_crtc_state *old_crtc_state =
1905 			intel_atomic_get_old_crtc_state(state, crtc);
1906 		const struct vlv_fifo_state *old_fifo_state =
1907 			&old_crtc_state->wm.vlv.fifo_state;
1908 
1909 		ret = vlv_compute_fifo(crtc_state);
1910 		if (ret)
1911 			return ret;
1912 
1913 		if (needs_modeset ||
1914 		    memcmp(old_fifo_state, fifo_state,
1915 			   sizeof(*fifo_state)) != 0)
1916 			crtc_state->fifo_changed = true;
1917 	}
1918 
1919 	/* initially allow all levels */
1920 	wm_state->num_levels = intel_wm_num_levels(dev_priv);
1921 	/*
1922 	 * Note that enabling cxsr with no primary/sprite planes
1923 	 * enabled can wedge the pipe. Hence we only allow cxsr
1924 	 * with exactly one enabled primary/sprite plane.
1925 	 */
1926 	wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
1927 
1928 	for (level = 0; level < wm_state->num_levels; level++) {
1929 		const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1930 		const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
1931 
1932 		if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
1933 			break;
1934 
1935 		for_each_plane_id_on_crtc(crtc, plane_id) {
1936 			wm_state->wm[level].plane[plane_id] =
1937 				vlv_invert_wm_value(raw->plane[plane_id],
1938 						    fifo_state->plane[plane_id]);
1939 		}
1940 
1941 		wm_state->sr[level].plane =
1942 			vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
1943 						 raw->plane[PLANE_SPRITE0],
1944 						 raw->plane[PLANE_SPRITE1]),
1945 					    sr_fifo_size);
1946 
1947 		wm_state->sr[level].cursor =
1948 			vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1949 					    63);
1950 	}
1951 
1952 	if (level == 0)
1953 		return -EINVAL;
1954 
1955 	/* limit to only levels we can actually handle */
1956 	wm_state->num_levels = level;
1957 
1958 	/* invalidate the higher levels */
1959 	vlv_invalidate_wms(crtc, wm_state, level);
1960 
1961 	return 0;
1962 }
1963 
1964 #define VLV_FIFO(plane, value) \
1965 	(((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1966 
vlv_atomic_update_fifo(struct intel_atomic_state * state,struct intel_crtc * crtc)1967 static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1968 				   struct intel_crtc *crtc)
1969 {
1970 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1971 	struct intel_uncore *uncore = &dev_priv->uncore;
1972 	const struct intel_crtc_state *crtc_state =
1973 		intel_atomic_get_new_crtc_state(state, crtc);
1974 	const struct vlv_fifo_state *fifo_state =
1975 		&crtc_state->wm.vlv.fifo_state;
1976 	int sprite0_start, sprite1_start, fifo_size;
1977 	u32 dsparb, dsparb2, dsparb3;
1978 
1979 	if (!crtc_state->fifo_changed)
1980 		return;
1981 
1982 	sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1983 	sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1984 	fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
1985 
1986 	drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
1987 	drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
1988 
1989 	trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1990 
1991 	/*
1992 	 * uncore.lock serves a double purpose here. It allows us to
1993 	 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1994 	 * it protects the DSPARB registers from getting clobbered by
1995 	 * parallel updates from multiple pipes.
1996 	 *
1997 	 * intel_pipe_update_start() has already disabled interrupts
1998 	 * for us, so a plain spin_lock() is sufficient here.
1999 	 */
2000 	spin_lock(&uncore->lock);
2001 
2002 	switch (crtc->pipe) {
2003 	case PIPE_A:
2004 		dsparb = intel_uncore_read_fw(uncore, DSPARB);
2005 		dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2006 
2007 		dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2008 			    VLV_FIFO(SPRITEB, 0xff));
2009 		dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2010 			   VLV_FIFO(SPRITEB, sprite1_start));
2011 
2012 		dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2013 			     VLV_FIFO(SPRITEB_HI, 0x1));
2014 		dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2015 			   VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2016 
2017 		intel_uncore_write_fw(uncore, DSPARB, dsparb);
2018 		intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2019 		break;
2020 	case PIPE_B:
2021 		dsparb = intel_uncore_read_fw(uncore, DSPARB);
2022 		dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2023 
2024 		dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2025 			    VLV_FIFO(SPRITED, 0xff));
2026 		dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2027 			   VLV_FIFO(SPRITED, sprite1_start));
2028 
2029 		dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2030 			     VLV_FIFO(SPRITED_HI, 0xff));
2031 		dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2032 			   VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2033 
2034 		intel_uncore_write_fw(uncore, DSPARB, dsparb);
2035 		intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2036 		break;
2037 	case PIPE_C:
2038 		dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2039 		dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2040 
2041 		dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2042 			     VLV_FIFO(SPRITEF, 0xff));
2043 		dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2044 			    VLV_FIFO(SPRITEF, sprite1_start));
2045 
2046 		dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2047 			     VLV_FIFO(SPRITEF_HI, 0xff));
2048 		dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2049 			   VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2050 
2051 		intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2052 		intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2053 		break;
2054 	default:
2055 		break;
2056 	}
2057 
2058 	intel_uncore_posting_read_fw(uncore, DSPARB);
2059 
2060 	spin_unlock(&uncore->lock);
2061 }
2062 
2063 #undef VLV_FIFO
2064 
vlv_compute_intermediate_wm(struct intel_atomic_state * state,struct intel_crtc * crtc)2065 static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
2066 				       struct intel_crtc *crtc)
2067 {
2068 	struct intel_crtc_state *new_crtc_state =
2069 		intel_atomic_get_new_crtc_state(state, crtc);
2070 	const struct intel_crtc_state *old_crtc_state =
2071 		intel_atomic_get_old_crtc_state(state, crtc);
2072 	struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2073 	const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2074 	const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
2075 	int level;
2076 
2077 	if (!new_crtc_state->hw.active ||
2078 	    drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
2079 		*intermediate = *optimal;
2080 
2081 		intermediate->cxsr = false;
2082 		goto out;
2083 	}
2084 
2085 	intermediate->num_levels = min(optimal->num_levels, active->num_levels);
2086 	intermediate->cxsr = optimal->cxsr && active->cxsr &&
2087 		!new_crtc_state->disable_cxsr;
2088 
2089 	for (level = 0; level < intermediate->num_levels; level++) {
2090 		enum plane_id plane_id;
2091 
2092 		for_each_plane_id_on_crtc(crtc, plane_id) {
2093 			intermediate->wm[level].plane[plane_id] =
2094 				min(optimal->wm[level].plane[plane_id],
2095 				    active->wm[level].plane[plane_id]);
2096 		}
2097 
2098 		intermediate->sr[level].plane = min(optimal->sr[level].plane,
2099 						    active->sr[level].plane);
2100 		intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2101 						     active->sr[level].cursor);
2102 	}
2103 
2104 	vlv_invalidate_wms(crtc, intermediate, level);
2105 
2106 out:
2107 	/*
2108 	 * If our intermediate WM are identical to the final WM, then we can
2109 	 * omit the post-vblank programming; only update if it's different.
2110 	 */
2111 	if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2112 		new_crtc_state->wm.need_postvbl_update = true;
2113 
2114 	return 0;
2115 }
2116 
vlv_merge_wm(struct drm_i915_private * dev_priv,struct vlv_wm_values * wm)2117 static void vlv_merge_wm(struct drm_i915_private *dev_priv,
2118 			 struct vlv_wm_values *wm)
2119 {
2120 	struct intel_crtc *crtc;
2121 	int num_active_pipes = 0;
2122 
2123 	wm->level = dev_priv->display.wm.max_level;
2124 	wm->cxsr = true;
2125 
2126 	for_each_intel_crtc(&dev_priv->drm, crtc) {
2127 		const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
2128 
2129 		if (!crtc->active)
2130 			continue;
2131 
2132 		if (!wm_state->cxsr)
2133 			wm->cxsr = false;
2134 
2135 		num_active_pipes++;
2136 		wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2137 	}
2138 
2139 	if (num_active_pipes != 1)
2140 		wm->cxsr = false;
2141 
2142 	if (num_active_pipes > 1)
2143 		wm->level = VLV_WM_LEVEL_PM2;
2144 
2145 	for_each_intel_crtc(&dev_priv->drm, crtc) {
2146 		const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
2147 		enum pipe pipe = crtc->pipe;
2148 
2149 		wm->pipe[pipe] = wm_state->wm[wm->level];
2150 		if (crtc->active && wm->cxsr)
2151 			wm->sr = wm_state->sr[wm->level];
2152 
2153 		wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2154 		wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2155 		wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2156 		wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
2157 	}
2158 }
2159 
vlv_program_watermarks(struct drm_i915_private * dev_priv)2160 static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
2161 {
2162 	struct vlv_wm_values *old_wm = &dev_priv->display.wm.vlv;
2163 	struct vlv_wm_values new_wm = {};
2164 
2165 	vlv_merge_wm(dev_priv, &new_wm);
2166 
2167 	if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
2168 		return;
2169 
2170 	if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
2171 		chv_set_memory_dvfs(dev_priv, false);
2172 
2173 	if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
2174 		chv_set_memory_pm5(dev_priv, false);
2175 
2176 	if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
2177 		_intel_set_memory_cxsr(dev_priv, false);
2178 
2179 	vlv_write_wm_values(dev_priv, &new_wm);
2180 
2181 	if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
2182 		_intel_set_memory_cxsr(dev_priv, true);
2183 
2184 	if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
2185 		chv_set_memory_pm5(dev_priv, true);
2186 
2187 	if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
2188 		chv_set_memory_dvfs(dev_priv, true);
2189 
2190 	*old_wm = new_wm;
2191 }
2192 
vlv_initial_watermarks(struct intel_atomic_state * state,struct intel_crtc * crtc)2193 static void vlv_initial_watermarks(struct intel_atomic_state *state,
2194 				   struct intel_crtc *crtc)
2195 {
2196 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2197 	const struct intel_crtc_state *crtc_state =
2198 		intel_atomic_get_new_crtc_state(state, crtc);
2199 
2200 	mutex_lock(&dev_priv->display.wm.wm_mutex);
2201 	crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2202 	vlv_program_watermarks(dev_priv);
2203 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
2204 }
2205 
vlv_optimize_watermarks(struct intel_atomic_state * state,struct intel_crtc * crtc)2206 static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2207 				    struct intel_crtc *crtc)
2208 {
2209 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2210 	const struct intel_crtc_state *crtc_state =
2211 		intel_atomic_get_new_crtc_state(state, crtc);
2212 
2213 	if (!crtc_state->wm.need_postvbl_update)
2214 		return;
2215 
2216 	mutex_lock(&dev_priv->display.wm.wm_mutex);
2217 	crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
2218 	vlv_program_watermarks(dev_priv);
2219 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
2220 }
2221 
i965_update_wm(struct drm_i915_private * dev_priv)2222 static void i965_update_wm(struct drm_i915_private *dev_priv)
2223 {
2224 	struct intel_crtc *crtc;
2225 	int srwm = 1;
2226 	int cursor_sr = 16;
2227 	bool cxsr_enabled;
2228 
2229 	/* Calc sr entries for one plane configs */
2230 	crtc = single_enabled_crtc(dev_priv);
2231 	if (crtc) {
2232 		/* self-refresh has much higher latency */
2233 		static const int sr_latency_ns = 12000;
2234 		const struct drm_display_mode *pipe_mode =
2235 			&crtc->config->hw.pipe_mode;
2236 		const struct drm_framebuffer *fb =
2237 			crtc->base.primary->state->fb;
2238 		int pixel_rate = crtc->config->pixel_rate;
2239 		int htotal = pipe_mode->crtc_htotal;
2240 		int width = drm_rect_width(&crtc->base.primary->state->src) >> 16;
2241 		int cpp = fb->format->cpp[0];
2242 		int entries;
2243 
2244 		entries = intel_wm_method2(pixel_rate, htotal,
2245 					   width, cpp, sr_latency_ns / 100);
2246 		entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2247 		srwm = I965_FIFO_SIZE - entries;
2248 		if (srwm < 0)
2249 			srwm = 1;
2250 		srwm &= 0x1ff;
2251 		drm_dbg_kms(&dev_priv->drm,
2252 			    "self-refresh entries: %d, wm: %d\n",
2253 			    entries, srwm);
2254 
2255 		entries = intel_wm_method2(pixel_rate, htotal,
2256 					   crtc->base.cursor->state->crtc_w, 4,
2257 					   sr_latency_ns / 100);
2258 		entries = DIV_ROUND_UP(entries,
2259 				       i965_cursor_wm_info.cacheline_size) +
2260 			i965_cursor_wm_info.guard_size;
2261 
2262 		cursor_sr = i965_cursor_wm_info.fifo_size - entries;
2263 		if (cursor_sr > i965_cursor_wm_info.max_wm)
2264 			cursor_sr = i965_cursor_wm_info.max_wm;
2265 
2266 		drm_dbg_kms(&dev_priv->drm,
2267 			    "self-refresh watermark: display plane %d "
2268 			    "cursor %d\n", srwm, cursor_sr);
2269 
2270 		cxsr_enabled = true;
2271 	} else {
2272 		cxsr_enabled = false;
2273 		/* Turn off self refresh if both pipes are enabled */
2274 		intel_set_memory_cxsr(dev_priv, false);
2275 	}
2276 
2277 	drm_dbg_kms(&dev_priv->drm,
2278 		    "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2279 		    srwm);
2280 
2281 	/* 965 has limitations... */
2282 	intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
2283 		   FW_WM(8, CURSORB) |
2284 		   FW_WM(8, PLANEB) |
2285 		   FW_WM(8, PLANEA));
2286 	intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
2287 		   FW_WM(8, PLANEC_OLD));
2288 	/* update cursor SR watermark */
2289 	intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
2290 
2291 	if (cxsr_enabled)
2292 		intel_set_memory_cxsr(dev_priv, true);
2293 }
2294 
2295 #undef FW_WM
2296 
intel_crtc_for_plane(struct drm_i915_private * i915,enum i9xx_plane_id i9xx_plane)2297 static struct intel_crtc *intel_crtc_for_plane(struct drm_i915_private *i915,
2298 					       enum i9xx_plane_id i9xx_plane)
2299 {
2300 	struct intel_plane *plane;
2301 
2302 	for_each_intel_plane(&i915->drm, plane) {
2303 		if (plane->id == PLANE_PRIMARY &&
2304 		    plane->i9xx_plane == i9xx_plane)
2305 			return intel_crtc_for_pipe(i915, plane->pipe);
2306 	}
2307 
2308 	return NULL;
2309 }
2310 
i9xx_update_wm(struct drm_i915_private * dev_priv)2311 static void i9xx_update_wm(struct drm_i915_private *dev_priv)
2312 {
2313 	const struct intel_watermark_params *wm_info;
2314 	u32 fwater_lo;
2315 	u32 fwater_hi;
2316 	int cwm, srwm = 1;
2317 	int fifo_size;
2318 	int planea_wm, planeb_wm;
2319 	struct intel_crtc *crtc;
2320 
2321 	if (IS_I945GM(dev_priv))
2322 		wm_info = &i945_wm_info;
2323 	else if (DISPLAY_VER(dev_priv) != 2)
2324 		wm_info = &i915_wm_info;
2325 	else
2326 		wm_info = &i830_a_wm_info;
2327 
2328 	if (DISPLAY_VER(dev_priv) == 2)
2329 		fifo_size = i830_get_fifo_size(dev_priv, PLANE_A);
2330 	else
2331 		fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_A);
2332 	crtc = intel_crtc_for_plane(dev_priv, PLANE_A);
2333 	if (intel_crtc_active(crtc)) {
2334 		const struct drm_framebuffer *fb =
2335 			crtc->base.primary->state->fb;
2336 		int cpp;
2337 
2338 		if (DISPLAY_VER(dev_priv) == 2)
2339 			cpp = 4;
2340 		else
2341 			cpp = fb->format->cpp[0];
2342 
2343 		planea_wm = intel_calculate_wm(crtc->config->pixel_rate,
2344 					       wm_info, fifo_size, cpp,
2345 					       pessimal_latency_ns);
2346 	} else {
2347 		planea_wm = fifo_size - wm_info->guard_size;
2348 		if (planea_wm > (long)wm_info->max_wm)
2349 			planea_wm = wm_info->max_wm;
2350 	}
2351 
2352 	if (DISPLAY_VER(dev_priv) == 2)
2353 		wm_info = &i830_bc_wm_info;
2354 
2355 	if (DISPLAY_VER(dev_priv) == 2)
2356 		fifo_size = i830_get_fifo_size(dev_priv, PLANE_B);
2357 	else
2358 		fifo_size = i9xx_get_fifo_size(dev_priv, PLANE_B);
2359 	crtc = intel_crtc_for_plane(dev_priv, PLANE_B);
2360 	if (intel_crtc_active(crtc)) {
2361 		const struct drm_framebuffer *fb =
2362 			crtc->base.primary->state->fb;
2363 		int cpp;
2364 
2365 		if (DISPLAY_VER(dev_priv) == 2)
2366 			cpp = 4;
2367 		else
2368 			cpp = fb->format->cpp[0];
2369 
2370 		planeb_wm = intel_calculate_wm(crtc->config->pixel_rate,
2371 					       wm_info, fifo_size, cpp,
2372 					       pessimal_latency_ns);
2373 	} else {
2374 		planeb_wm = fifo_size - wm_info->guard_size;
2375 		if (planeb_wm > (long)wm_info->max_wm)
2376 			planeb_wm = wm_info->max_wm;
2377 	}
2378 
2379 	drm_dbg_kms(&dev_priv->drm,
2380 		    "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2381 
2382 	crtc = single_enabled_crtc(dev_priv);
2383 	if (IS_I915GM(dev_priv) && crtc) {
2384 		struct drm_i915_gem_object *obj;
2385 
2386 		obj = intel_fb_obj(crtc->base.primary->state->fb);
2387 
2388 		/* self-refresh seems busted with untiled */
2389 		if (!i915_gem_object_is_tiled(obj))
2390 			crtc = NULL;
2391 	}
2392 
2393 	/*
2394 	 * Overlay gets an aggressive default since video jitter is bad.
2395 	 */
2396 	cwm = 2;
2397 
2398 	/* Play safe and disable self-refresh before adjusting watermarks. */
2399 	intel_set_memory_cxsr(dev_priv, false);
2400 
2401 	/* Calc sr entries for one plane configs */
2402 	if (HAS_FW_BLC(dev_priv) && crtc) {
2403 		/* self-refresh has much higher latency */
2404 		static const int sr_latency_ns = 6000;
2405 		const struct drm_display_mode *pipe_mode =
2406 			&crtc->config->hw.pipe_mode;
2407 		const struct drm_framebuffer *fb =
2408 			crtc->base.primary->state->fb;
2409 		int pixel_rate = crtc->config->pixel_rate;
2410 		int htotal = pipe_mode->crtc_htotal;
2411 		int width = drm_rect_width(&crtc->base.primary->state->src) >> 16;
2412 		int cpp;
2413 		int entries;
2414 
2415 		if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
2416 			cpp = 4;
2417 		else
2418 			cpp = fb->format->cpp[0];
2419 
2420 		entries = intel_wm_method2(pixel_rate, htotal, width, cpp,
2421 					   sr_latency_ns / 100);
2422 		entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2423 		drm_dbg_kms(&dev_priv->drm,
2424 			    "self-refresh entries: %d\n", entries);
2425 		srwm = wm_info->fifo_size - entries;
2426 		if (srwm < 0)
2427 			srwm = 1;
2428 
2429 		if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
2430 			intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
2431 				   FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
2432 		else
2433 			intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
2434 	}
2435 
2436 	drm_dbg_kms(&dev_priv->drm,
2437 		    "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2438 		     planea_wm, planeb_wm, cwm, srwm);
2439 
2440 	fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2441 	fwater_hi = (cwm & 0x1f);
2442 
2443 	/* Set request length to 8 cachelines per fetch */
2444 	fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2445 	fwater_hi = fwater_hi | (1 << 8);
2446 
2447 	intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2448 	intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
2449 
2450 	if (crtc)
2451 		intel_set_memory_cxsr(dev_priv, true);
2452 }
2453 
i845_update_wm(struct drm_i915_private * dev_priv)2454 static void i845_update_wm(struct drm_i915_private *dev_priv)
2455 {
2456 	struct intel_crtc *crtc;
2457 	u32 fwater_lo;
2458 	int planea_wm;
2459 
2460 	crtc = single_enabled_crtc(dev_priv);
2461 	if (crtc == NULL)
2462 		return;
2463 
2464 	planea_wm = intel_calculate_wm(crtc->config->pixel_rate,
2465 				       &i845_wm_info,
2466 				       i845_get_fifo_size(dev_priv, PLANE_A),
2467 				       4, pessimal_latency_ns);
2468 	fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
2469 	fwater_lo |= (3<<8) | planea_wm;
2470 
2471 	drm_dbg_kms(&dev_priv->drm,
2472 		    "Setting FIFO watermarks - A: %d\n", planea_wm);
2473 
2474 	intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2475 }
2476 
2477 /* latency must be in 0.1us units. */
ilk_wm_method1(unsigned int pixel_rate,unsigned int cpp,unsigned int latency)2478 static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2479 				   unsigned int cpp,
2480 				   unsigned int latency)
2481 {
2482 	unsigned int ret;
2483 
2484 	ret = intel_wm_method1(pixel_rate, cpp, latency);
2485 	ret = DIV_ROUND_UP(ret, 64) + 2;
2486 
2487 	return ret;
2488 }
2489 
2490 /* latency must be in 0.1us units. */
ilk_wm_method2(unsigned int pixel_rate,unsigned int htotal,unsigned int width,unsigned int cpp,unsigned int latency)2491 static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2492 				   unsigned int htotal,
2493 				   unsigned int width,
2494 				   unsigned int cpp,
2495 				   unsigned int latency)
2496 {
2497 	unsigned int ret;
2498 
2499 	ret = intel_wm_method2(pixel_rate, htotal,
2500 			       width, cpp, latency);
2501 	ret = DIV_ROUND_UP(ret, 64) + 2;
2502 
2503 	return ret;
2504 }
2505 
ilk_wm_fbc(u32 pri_val,u32 horiz_pixels,u8 cpp)2506 static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
2507 {
2508 	/*
2509 	 * Neither of these should be possible since this function shouldn't be
2510 	 * called if the CRTC is off or the plane is invisible.  But let's be
2511 	 * extra paranoid to avoid a potential divide-by-zero if we screw up
2512 	 * elsewhere in the driver.
2513 	 */
2514 	if (WARN_ON(!cpp))
2515 		return 0;
2516 	if (WARN_ON(!horiz_pixels))
2517 		return 0;
2518 
2519 	return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
2520 }
2521 
2522 struct ilk_wm_maximums {
2523 	u16 pri;
2524 	u16 spr;
2525 	u16 cur;
2526 	u16 fbc;
2527 };
2528 
2529 /*
2530  * For both WM_PIPE and WM_LP.
2531  * mem_value must be in 0.1us units.
2532  */
ilk_compute_pri_wm(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,u32 mem_value,bool is_lp)2533 static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2534 			      const struct intel_plane_state *plane_state,
2535 			      u32 mem_value, bool is_lp)
2536 {
2537 	u32 method1, method2;
2538 	int cpp;
2539 
2540 	if (mem_value == 0)
2541 		return U32_MAX;
2542 
2543 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2544 		return 0;
2545 
2546 	cpp = plane_state->hw.fb->format->cpp[0];
2547 
2548 	method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2549 
2550 	if (!is_lp)
2551 		return method1;
2552 
2553 	method2 = ilk_wm_method2(crtc_state->pixel_rate,
2554 				 crtc_state->hw.pipe_mode.crtc_htotal,
2555 				 drm_rect_width(&plane_state->uapi.src) >> 16,
2556 				 cpp, mem_value);
2557 
2558 	return min(method1, method2);
2559 }
2560 
2561 /*
2562  * For both WM_PIPE and WM_LP.
2563  * mem_value must be in 0.1us units.
2564  */
ilk_compute_spr_wm(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,u32 mem_value)2565 static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2566 			      const struct intel_plane_state *plane_state,
2567 			      u32 mem_value)
2568 {
2569 	u32 method1, method2;
2570 	int cpp;
2571 
2572 	if (mem_value == 0)
2573 		return U32_MAX;
2574 
2575 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2576 		return 0;
2577 
2578 	cpp = plane_state->hw.fb->format->cpp[0];
2579 
2580 	method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2581 	method2 = ilk_wm_method2(crtc_state->pixel_rate,
2582 				 crtc_state->hw.pipe_mode.crtc_htotal,
2583 				 drm_rect_width(&plane_state->uapi.src) >> 16,
2584 				 cpp, mem_value);
2585 	return min(method1, method2);
2586 }
2587 
2588 /*
2589  * For both WM_PIPE and WM_LP.
2590  * mem_value must be in 0.1us units.
2591  */
ilk_compute_cur_wm(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,u32 mem_value)2592 static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2593 			      const struct intel_plane_state *plane_state,
2594 			      u32 mem_value)
2595 {
2596 	int cpp;
2597 
2598 	if (mem_value == 0)
2599 		return U32_MAX;
2600 
2601 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2602 		return 0;
2603 
2604 	cpp = plane_state->hw.fb->format->cpp[0];
2605 
2606 	return ilk_wm_method2(crtc_state->pixel_rate,
2607 			      crtc_state->hw.pipe_mode.crtc_htotal,
2608 			      drm_rect_width(&plane_state->uapi.src) >> 16,
2609 			      cpp, mem_value);
2610 }
2611 
2612 /* Only for WM_LP. */
ilk_compute_fbc_wm(const struct intel_crtc_state * crtc_state,const struct intel_plane_state * plane_state,u32 pri_val)2613 static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2614 			      const struct intel_plane_state *plane_state,
2615 			      u32 pri_val)
2616 {
2617 	int cpp;
2618 
2619 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2620 		return 0;
2621 
2622 	cpp = plane_state->hw.fb->format->cpp[0];
2623 
2624 	return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.src) >> 16,
2625 			  cpp);
2626 }
2627 
2628 static unsigned int
ilk_display_fifo_size(const struct drm_i915_private * dev_priv)2629 ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
2630 {
2631 	if (DISPLAY_VER(dev_priv) >= 8)
2632 		return 3072;
2633 	else if (DISPLAY_VER(dev_priv) >= 7)
2634 		return 768;
2635 	else
2636 		return 512;
2637 }
2638 
2639 static unsigned int
ilk_plane_wm_reg_max(const struct drm_i915_private * dev_priv,int level,bool is_sprite)2640 ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2641 		     int level, bool is_sprite)
2642 {
2643 	if (DISPLAY_VER(dev_priv) >= 8)
2644 		/* BDW primary/sprite plane watermarks */
2645 		return level == 0 ? 255 : 2047;
2646 	else if (DISPLAY_VER(dev_priv) >= 7)
2647 		/* IVB/HSW primary/sprite plane watermarks */
2648 		return level == 0 ? 127 : 1023;
2649 	else if (!is_sprite)
2650 		/* ILK/SNB primary plane watermarks */
2651 		return level == 0 ? 127 : 511;
2652 	else
2653 		/* ILK/SNB sprite plane watermarks */
2654 		return level == 0 ? 63 : 255;
2655 }
2656 
2657 static unsigned int
ilk_cursor_wm_reg_max(const struct drm_i915_private * dev_priv,int level)2658 ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
2659 {
2660 	if (DISPLAY_VER(dev_priv) >= 7)
2661 		return level == 0 ? 63 : 255;
2662 	else
2663 		return level == 0 ? 31 : 63;
2664 }
2665 
ilk_fbc_wm_reg_max(const struct drm_i915_private * dev_priv)2666 static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
2667 {
2668 	if (DISPLAY_VER(dev_priv) >= 8)
2669 		return 31;
2670 	else
2671 		return 15;
2672 }
2673 
2674 /* Calculate the maximum primary/sprite plane watermark */
ilk_plane_wm_max(const struct drm_i915_private * dev_priv,int level,const struct intel_wm_config * config,enum intel_ddb_partitioning ddb_partitioning,bool is_sprite)2675 static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
2676 				     int level,
2677 				     const struct intel_wm_config *config,
2678 				     enum intel_ddb_partitioning ddb_partitioning,
2679 				     bool is_sprite)
2680 {
2681 	unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
2682 
2683 	/* if sprites aren't enabled, sprites get nothing */
2684 	if (is_sprite && !config->sprites_enabled)
2685 		return 0;
2686 
2687 	/* HSW allows LP1+ watermarks even with multiple pipes */
2688 	if (level == 0 || config->num_pipes_active > 1) {
2689 		fifo_size /= INTEL_NUM_PIPES(dev_priv);
2690 
2691 		/*
2692 		 * For some reason the non self refresh
2693 		 * FIFO size is only half of the self
2694 		 * refresh FIFO size on ILK/SNB.
2695 		 */
2696 		if (DISPLAY_VER(dev_priv) <= 6)
2697 			fifo_size /= 2;
2698 	}
2699 
2700 	if (config->sprites_enabled) {
2701 		/* level 0 is always calculated with 1:1 split */
2702 		if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2703 			if (is_sprite)
2704 				fifo_size *= 5;
2705 			fifo_size /= 6;
2706 		} else {
2707 			fifo_size /= 2;
2708 		}
2709 	}
2710 
2711 	/* clamp to max that the registers can hold */
2712 	return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
2713 }
2714 
2715 /* Calculate the maximum cursor plane watermark */
ilk_cursor_wm_max(const struct drm_i915_private * dev_priv,int level,const struct intel_wm_config * config)2716 static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
2717 				      int level,
2718 				      const struct intel_wm_config *config)
2719 {
2720 	/* HSW LP1+ watermarks w/ multiple pipes */
2721 	if (level > 0 && config->num_pipes_active > 1)
2722 		return 64;
2723 
2724 	/* otherwise just report max that registers can hold */
2725 	return ilk_cursor_wm_reg_max(dev_priv, level);
2726 }
2727 
ilk_compute_wm_maximums(const struct drm_i915_private * dev_priv,int level,const struct intel_wm_config * config,enum intel_ddb_partitioning ddb_partitioning,struct ilk_wm_maximums * max)2728 static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
2729 				    int level,
2730 				    const struct intel_wm_config *config,
2731 				    enum intel_ddb_partitioning ddb_partitioning,
2732 				    struct ilk_wm_maximums *max)
2733 {
2734 	max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2735 	max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2736 	max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2737 	max->fbc = ilk_fbc_wm_reg_max(dev_priv);
2738 }
2739 
ilk_compute_wm_reg_maximums(const struct drm_i915_private * dev_priv,int level,struct ilk_wm_maximums * max)2740 static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
2741 					int level,
2742 					struct ilk_wm_maximums *max)
2743 {
2744 	max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2745 	max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2746 	max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2747 	max->fbc = ilk_fbc_wm_reg_max(dev_priv);
2748 }
2749 
ilk_validate_wm_level(int level,const struct ilk_wm_maximums * max,struct intel_wm_level * result)2750 static bool ilk_validate_wm_level(int level,
2751 				  const struct ilk_wm_maximums *max,
2752 				  struct intel_wm_level *result)
2753 {
2754 	bool ret;
2755 
2756 	/* already determined to be invalid? */
2757 	if (!result->enable)
2758 		return false;
2759 
2760 	result->enable = result->pri_val <= max->pri &&
2761 			 result->spr_val <= max->spr &&
2762 			 result->cur_val <= max->cur;
2763 
2764 	ret = result->enable;
2765 
2766 	/*
2767 	 * HACK until we can pre-compute everything,
2768 	 * and thus fail gracefully if LP0 watermarks
2769 	 * are exceeded...
2770 	 */
2771 	if (level == 0 && !result->enable) {
2772 		if (result->pri_val > max->pri)
2773 			DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2774 				      level, result->pri_val, max->pri);
2775 		if (result->spr_val > max->spr)
2776 			DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2777 				      level, result->spr_val, max->spr);
2778 		if (result->cur_val > max->cur)
2779 			DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2780 				      level, result->cur_val, max->cur);
2781 
2782 		result->pri_val = min_t(u32, result->pri_val, max->pri);
2783 		result->spr_val = min_t(u32, result->spr_val, max->spr);
2784 		result->cur_val = min_t(u32, result->cur_val, max->cur);
2785 		result->enable = true;
2786 	}
2787 
2788 	return ret;
2789 }
2790 
ilk_compute_wm_level(const struct drm_i915_private * dev_priv,const struct intel_crtc * crtc,int level,struct intel_crtc_state * crtc_state,const struct intel_plane_state * pristate,const struct intel_plane_state * sprstate,const struct intel_plane_state * curstate,struct intel_wm_level * result)2791 static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
2792 				 const struct intel_crtc *crtc,
2793 				 int level,
2794 				 struct intel_crtc_state *crtc_state,
2795 				 const struct intel_plane_state *pristate,
2796 				 const struct intel_plane_state *sprstate,
2797 				 const struct intel_plane_state *curstate,
2798 				 struct intel_wm_level *result)
2799 {
2800 	u16 pri_latency = dev_priv->display.wm.pri_latency[level];
2801 	u16 spr_latency = dev_priv->display.wm.spr_latency[level];
2802 	u16 cur_latency = dev_priv->display.wm.cur_latency[level];
2803 
2804 	/* WM1+ latency values stored in 0.5us units */
2805 	if (level > 0) {
2806 		pri_latency *= 5;
2807 		spr_latency *= 5;
2808 		cur_latency *= 5;
2809 	}
2810 
2811 	if (pristate) {
2812 		result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
2813 						     pri_latency, level);
2814 		result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
2815 	}
2816 
2817 	if (sprstate)
2818 		result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
2819 
2820 	if (curstate)
2821 		result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
2822 
2823 	result->enable = true;
2824 }
2825 
hsw_read_wm_latency(struct drm_i915_private * i915,u16 wm[])2826 static void hsw_read_wm_latency(struct drm_i915_private *i915, u16 wm[])
2827 {
2828 	u64 sskpd;
2829 
2830 	sskpd = intel_uncore_read64(&i915->uncore, MCH_SSKPD);
2831 
2832 	wm[0] = REG_FIELD_GET64(SSKPD_NEW_WM0_MASK_HSW, sskpd);
2833 	if (wm[0] == 0)
2834 		wm[0] = REG_FIELD_GET64(SSKPD_OLD_WM0_MASK_HSW, sskpd);
2835 	wm[1] = REG_FIELD_GET64(SSKPD_WM1_MASK_HSW, sskpd);
2836 	wm[2] = REG_FIELD_GET64(SSKPD_WM2_MASK_HSW, sskpd);
2837 	wm[3] = REG_FIELD_GET64(SSKPD_WM3_MASK_HSW, sskpd);
2838 	wm[4] = REG_FIELD_GET64(SSKPD_WM4_MASK_HSW, sskpd);
2839 }
2840 
snb_read_wm_latency(struct drm_i915_private * i915,u16 wm[])2841 static void snb_read_wm_latency(struct drm_i915_private *i915, u16 wm[])
2842 {
2843 	u32 sskpd;
2844 
2845 	sskpd = intel_uncore_read(&i915->uncore, MCH_SSKPD);
2846 
2847 	wm[0] = REG_FIELD_GET(SSKPD_WM0_MASK_SNB, sskpd);
2848 	wm[1] = REG_FIELD_GET(SSKPD_WM1_MASK_SNB, sskpd);
2849 	wm[2] = REG_FIELD_GET(SSKPD_WM2_MASK_SNB, sskpd);
2850 	wm[3] = REG_FIELD_GET(SSKPD_WM3_MASK_SNB, sskpd);
2851 }
2852 
ilk_read_wm_latency(struct drm_i915_private * i915,u16 wm[])2853 static void ilk_read_wm_latency(struct drm_i915_private *i915, u16 wm[])
2854 {
2855 	u32 mltr;
2856 
2857 	mltr = intel_uncore_read(&i915->uncore, MLTR_ILK);
2858 
2859 	/* ILK primary LP0 latency is 700 ns */
2860 	wm[0] = 7;
2861 	wm[1] = REG_FIELD_GET(MLTR_WM1_MASK, mltr);
2862 	wm[2] = REG_FIELD_GET(MLTR_WM2_MASK, mltr);
2863 }
2864 
intel_fixup_spr_wm_latency(struct drm_i915_private * dev_priv,u16 wm[5])2865 static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2866 				       u16 wm[5])
2867 {
2868 	/* ILK sprite LP0 latency is 1300 ns */
2869 	if (DISPLAY_VER(dev_priv) == 5)
2870 		wm[0] = 13;
2871 }
2872 
intel_fixup_cur_wm_latency(struct drm_i915_private * dev_priv,u16 wm[5])2873 static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2874 				       u16 wm[5])
2875 {
2876 	/* ILK cursor LP0 latency is 1300 ns */
2877 	if (DISPLAY_VER(dev_priv) == 5)
2878 		wm[0] = 13;
2879 }
2880 
ilk_wm_max_level(const struct drm_i915_private * dev_priv)2881 int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
2882 {
2883 	/* how many WM levels are we expecting */
2884 	if (HAS_HW_SAGV_WM(dev_priv))
2885 		return 5;
2886 	else if (DISPLAY_VER(dev_priv) >= 9)
2887 		return 7;
2888 	else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
2889 		return 4;
2890 	else if (DISPLAY_VER(dev_priv) >= 6)
2891 		return 3;
2892 	else
2893 		return 2;
2894 }
2895 
intel_print_wm_latency(struct drm_i915_private * dev_priv,const char * name,const u16 wm[])2896 void intel_print_wm_latency(struct drm_i915_private *dev_priv,
2897 			    const char *name, const u16 wm[])
2898 {
2899 	int level, max_level = ilk_wm_max_level(dev_priv);
2900 
2901 	for (level = 0; level <= max_level; level++) {
2902 		unsigned int latency = wm[level];
2903 
2904 		if (latency == 0) {
2905 			drm_dbg_kms(&dev_priv->drm,
2906 				    "%s WM%d latency not provided\n",
2907 				    name, level);
2908 			continue;
2909 		}
2910 
2911 		/*
2912 		 * - latencies are in us on gen9.
2913 		 * - before then, WM1+ latency values are in 0.5us units
2914 		 */
2915 		if (DISPLAY_VER(dev_priv) >= 9)
2916 			latency *= 10;
2917 		else if (level > 0)
2918 			latency *= 5;
2919 
2920 		drm_dbg_kms(&dev_priv->drm,
2921 			    "%s WM%d latency %u (%u.%u usec)\n", name, level,
2922 			    wm[level], latency / 10, latency % 10);
2923 	}
2924 }
2925 
ilk_increase_wm_latency(struct drm_i915_private * dev_priv,u16 wm[5],u16 min)2926 static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2927 				    u16 wm[5], u16 min)
2928 {
2929 	int level, max_level = ilk_wm_max_level(dev_priv);
2930 
2931 	if (wm[0] >= min)
2932 		return false;
2933 
2934 	wm[0] = max(wm[0], min);
2935 	for (level = 1; level <= max_level; level++)
2936 		wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
2937 
2938 	return true;
2939 }
2940 
snb_wm_latency_quirk(struct drm_i915_private * dev_priv)2941 static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
2942 {
2943 	bool changed;
2944 
2945 	/*
2946 	 * The BIOS provided WM memory latency values are often
2947 	 * inadequate for high resolution displays. Adjust them.
2948 	 */
2949 	changed = ilk_increase_wm_latency(dev_priv, dev_priv->display.wm.pri_latency, 12);
2950 	changed |= ilk_increase_wm_latency(dev_priv, dev_priv->display.wm.spr_latency, 12);
2951 	changed |= ilk_increase_wm_latency(dev_priv, dev_priv->display.wm.cur_latency, 12);
2952 
2953 	if (!changed)
2954 		return;
2955 
2956 	drm_dbg_kms(&dev_priv->drm,
2957 		    "WM latency values increased to avoid potential underruns\n");
2958 	intel_print_wm_latency(dev_priv, "Primary", dev_priv->display.wm.pri_latency);
2959 	intel_print_wm_latency(dev_priv, "Sprite", dev_priv->display.wm.spr_latency);
2960 	intel_print_wm_latency(dev_priv, "Cursor", dev_priv->display.wm.cur_latency);
2961 }
2962 
snb_wm_lp3_irq_quirk(struct drm_i915_private * dev_priv)2963 static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
2964 {
2965 	/*
2966 	 * On some SNB machines (Thinkpad X220 Tablet at least)
2967 	 * LP3 usage can cause vblank interrupts to be lost.
2968 	 * The DEIIR bit will go high but it looks like the CPU
2969 	 * never gets interrupted.
2970 	 *
2971 	 * It's not clear whether other interrupt source could
2972 	 * be affected or if this is somehow limited to vblank
2973 	 * interrupts only. To play it safe we disable LP3
2974 	 * watermarks entirely.
2975 	 */
2976 	if (dev_priv->display.wm.pri_latency[3] == 0 &&
2977 	    dev_priv->display.wm.spr_latency[3] == 0 &&
2978 	    dev_priv->display.wm.cur_latency[3] == 0)
2979 		return;
2980 
2981 	dev_priv->display.wm.pri_latency[3] = 0;
2982 	dev_priv->display.wm.spr_latency[3] = 0;
2983 	dev_priv->display.wm.cur_latency[3] = 0;
2984 
2985 	drm_dbg_kms(&dev_priv->drm,
2986 		    "LP3 watermarks disabled due to potential for lost interrupts\n");
2987 	intel_print_wm_latency(dev_priv, "Primary", dev_priv->display.wm.pri_latency);
2988 	intel_print_wm_latency(dev_priv, "Sprite", dev_priv->display.wm.spr_latency);
2989 	intel_print_wm_latency(dev_priv, "Cursor", dev_priv->display.wm.cur_latency);
2990 }
2991 
ilk_setup_wm_latency(struct drm_i915_private * dev_priv)2992 static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
2993 {
2994 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
2995 		hsw_read_wm_latency(dev_priv, dev_priv->display.wm.pri_latency);
2996 	else if (DISPLAY_VER(dev_priv) >= 6)
2997 		snb_read_wm_latency(dev_priv, dev_priv->display.wm.pri_latency);
2998 	else
2999 		ilk_read_wm_latency(dev_priv, dev_priv->display.wm.pri_latency);
3000 
3001 	memcpy(dev_priv->display.wm.spr_latency, dev_priv->display.wm.pri_latency,
3002 	       sizeof(dev_priv->display.wm.pri_latency));
3003 	memcpy(dev_priv->display.wm.cur_latency, dev_priv->display.wm.pri_latency,
3004 	       sizeof(dev_priv->display.wm.pri_latency));
3005 
3006 	intel_fixup_spr_wm_latency(dev_priv, dev_priv->display.wm.spr_latency);
3007 	intel_fixup_cur_wm_latency(dev_priv, dev_priv->display.wm.cur_latency);
3008 
3009 	intel_print_wm_latency(dev_priv, "Primary", dev_priv->display.wm.pri_latency);
3010 	intel_print_wm_latency(dev_priv, "Sprite", dev_priv->display.wm.spr_latency);
3011 	intel_print_wm_latency(dev_priv, "Cursor", dev_priv->display.wm.cur_latency);
3012 
3013 	if (DISPLAY_VER(dev_priv) == 6) {
3014 		snb_wm_latency_quirk(dev_priv);
3015 		snb_wm_lp3_irq_quirk(dev_priv);
3016 	}
3017 }
3018 
ilk_validate_pipe_wm(const struct drm_i915_private * dev_priv,struct intel_pipe_wm * pipe_wm)3019 static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
3020 				 struct intel_pipe_wm *pipe_wm)
3021 {
3022 	/* LP0 watermark maximums depend on this pipe alone */
3023 	const struct intel_wm_config config = {
3024 		.num_pipes_active = 1,
3025 		.sprites_enabled = pipe_wm->sprites_enabled,
3026 		.sprites_scaled = pipe_wm->sprites_scaled,
3027 	};
3028 	struct ilk_wm_maximums max;
3029 
3030 	/* LP0 watermarks always use 1/2 DDB partitioning */
3031 	ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
3032 
3033 	/* At least LP0 must be valid */
3034 	if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3035 		drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
3036 		return false;
3037 	}
3038 
3039 	return true;
3040 }
3041 
3042 /* Compute new watermarks for the pipe */
ilk_compute_pipe_wm(struct intel_atomic_state * state,struct intel_crtc * crtc)3043 static int ilk_compute_pipe_wm(struct intel_atomic_state *state,
3044 			       struct intel_crtc *crtc)
3045 {
3046 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3047 	struct intel_crtc_state *crtc_state =
3048 		intel_atomic_get_new_crtc_state(state, crtc);
3049 	struct intel_pipe_wm *pipe_wm;
3050 	struct intel_plane *plane;
3051 	const struct intel_plane_state *plane_state;
3052 	const struct intel_plane_state *pristate = NULL;
3053 	const struct intel_plane_state *sprstate = NULL;
3054 	const struct intel_plane_state *curstate = NULL;
3055 	int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
3056 	struct ilk_wm_maximums max;
3057 
3058 	pipe_wm = &crtc_state->wm.ilk.optimal;
3059 
3060 	intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3061 		if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3062 			pristate = plane_state;
3063 		else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3064 			sprstate = plane_state;
3065 		else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3066 			curstate = plane_state;
3067 	}
3068 
3069 	pipe_wm->pipe_enabled = crtc_state->hw.active;
3070 	pipe_wm->sprites_enabled = crtc_state->active_planes & BIT(PLANE_SPRITE0);
3071 	pipe_wm->sprites_scaled = crtc_state->scaled_planes & BIT(PLANE_SPRITE0);
3072 
3073 	usable_level = max_level;
3074 
3075 	/* ILK/SNB: LP2+ watermarks only w/o sprites */
3076 	if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
3077 		usable_level = 1;
3078 
3079 	/* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
3080 	if (pipe_wm->sprites_scaled)
3081 		usable_level = 0;
3082 
3083 	memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3084 	ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
3085 			     pristate, sprstate, curstate, &pipe_wm->wm[0]);
3086 
3087 	if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
3088 		return -EINVAL;
3089 
3090 	ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
3091 
3092 	for (level = 1; level <= usable_level; level++) {
3093 		struct intel_wm_level *wm = &pipe_wm->wm[level];
3094 
3095 		ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
3096 				     pristate, sprstate, curstate, wm);
3097 
3098 		/*
3099 		 * Disable any watermark level that exceeds the
3100 		 * register maximums since such watermarks are
3101 		 * always invalid.
3102 		 */
3103 		if (!ilk_validate_wm_level(level, &max, wm)) {
3104 			memset(wm, 0, sizeof(*wm));
3105 			break;
3106 		}
3107 	}
3108 
3109 	return 0;
3110 }
3111 
3112 /*
3113  * Build a set of 'intermediate' watermark values that satisfy both the old
3114  * state and the new state.  These can be programmed to the hardware
3115  * immediately.
3116  */
ilk_compute_intermediate_wm(struct intel_atomic_state * state,struct intel_crtc * crtc)3117 static int ilk_compute_intermediate_wm(struct intel_atomic_state *state,
3118 				       struct intel_crtc *crtc)
3119 {
3120 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3121 	struct intel_crtc_state *new_crtc_state =
3122 		intel_atomic_get_new_crtc_state(state, crtc);
3123 	const struct intel_crtc_state *old_crtc_state =
3124 		intel_atomic_get_old_crtc_state(state, crtc);
3125 	struct intel_pipe_wm *a = &new_crtc_state->wm.ilk.intermediate;
3126 	const struct intel_pipe_wm *b = &old_crtc_state->wm.ilk.optimal;
3127 	int level, max_level = ilk_wm_max_level(dev_priv);
3128 
3129 	/*
3130 	 * Start with the final, target watermarks, then combine with the
3131 	 * currently active watermarks to get values that are safe both before
3132 	 * and after the vblank.
3133 	 */
3134 	*a = new_crtc_state->wm.ilk.optimal;
3135 	if (!new_crtc_state->hw.active ||
3136 	    drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) ||
3137 	    state->skip_intermediate_wm)
3138 		return 0;
3139 
3140 	a->pipe_enabled |= b->pipe_enabled;
3141 	a->sprites_enabled |= b->sprites_enabled;
3142 	a->sprites_scaled |= b->sprites_scaled;
3143 
3144 	for (level = 0; level <= max_level; level++) {
3145 		struct intel_wm_level *a_wm = &a->wm[level];
3146 		const struct intel_wm_level *b_wm = &b->wm[level];
3147 
3148 		a_wm->enable &= b_wm->enable;
3149 		a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3150 		a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3151 		a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3152 		a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3153 	}
3154 
3155 	/*
3156 	 * We need to make sure that these merged watermark values are
3157 	 * actually a valid configuration themselves.  If they're not,
3158 	 * there's no safe way to transition from the old state to
3159 	 * the new state, so we need to fail the atomic transaction.
3160 	 */
3161 	if (!ilk_validate_pipe_wm(dev_priv, a))
3162 		return -EINVAL;
3163 
3164 	/*
3165 	 * If our intermediate WM are identical to the final WM, then we can
3166 	 * omit the post-vblank programming; only update if it's different.
3167 	 */
3168 	if (memcmp(a, &new_crtc_state->wm.ilk.optimal, sizeof(*a)) != 0)
3169 		new_crtc_state->wm.need_postvbl_update = true;
3170 
3171 	return 0;
3172 }
3173 
3174 /*
3175  * Merge the watermarks from all active pipes for a specific level.
3176  */
ilk_merge_wm_level(struct drm_i915_private * dev_priv,int level,struct intel_wm_level * ret_wm)3177 static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
3178 			       int level,
3179 			       struct intel_wm_level *ret_wm)
3180 {
3181 	const struct intel_crtc *crtc;
3182 
3183 	ret_wm->enable = true;
3184 
3185 	for_each_intel_crtc(&dev_priv->drm, crtc) {
3186 		const struct intel_pipe_wm *active = &crtc->wm.active.ilk;
3187 		const struct intel_wm_level *wm = &active->wm[level];
3188 
3189 		if (!active->pipe_enabled)
3190 			continue;
3191 
3192 		/*
3193 		 * The watermark values may have been used in the past,
3194 		 * so we must maintain them in the registers for some
3195 		 * time even if the level is now disabled.
3196 		 */
3197 		if (!wm->enable)
3198 			ret_wm->enable = false;
3199 
3200 		ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3201 		ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3202 		ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3203 		ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3204 	}
3205 }
3206 
3207 /*
3208  * Merge all low power watermarks for all active pipes.
3209  */
ilk_wm_merge(struct drm_i915_private * dev_priv,const struct intel_wm_config * config,const struct ilk_wm_maximums * max,struct intel_pipe_wm * merged)3210 static void ilk_wm_merge(struct drm_i915_private *dev_priv,
3211 			 const struct intel_wm_config *config,
3212 			 const struct ilk_wm_maximums *max,
3213 			 struct intel_pipe_wm *merged)
3214 {
3215 	int level, max_level = ilk_wm_max_level(dev_priv);
3216 	int last_enabled_level = max_level;
3217 
3218 	/* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
3219 	if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
3220 	    config->num_pipes_active > 1)
3221 		last_enabled_level = 0;
3222 
3223 	/* ILK: FBC WM must be disabled always */
3224 	merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
3225 
3226 	/* merge each WM1+ level */
3227 	for (level = 1; level <= max_level; level++) {
3228 		struct intel_wm_level *wm = &merged->wm[level];
3229 
3230 		ilk_merge_wm_level(dev_priv, level, wm);
3231 
3232 		if (level > last_enabled_level)
3233 			wm->enable = false;
3234 		else if (!ilk_validate_wm_level(level, max, wm))
3235 			/* make sure all following levels get disabled */
3236 			last_enabled_level = level - 1;
3237 
3238 		/*
3239 		 * The spec says it is preferred to disable
3240 		 * FBC WMs instead of disabling a WM level.
3241 		 */
3242 		if (wm->fbc_val > max->fbc) {
3243 			if (wm->enable)
3244 				merged->fbc_wm_enabled = false;
3245 			wm->fbc_val = 0;
3246 		}
3247 	}
3248 
3249 	/* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3250 	if (DISPLAY_VER(dev_priv) == 5 && HAS_FBC(dev_priv) &&
3251 	    dev_priv->params.enable_fbc && !merged->fbc_wm_enabled) {
3252 		for (level = 2; level <= max_level; level++) {
3253 			struct intel_wm_level *wm = &merged->wm[level];
3254 
3255 			wm->enable = false;
3256 		}
3257 	}
3258 }
3259 
ilk_wm_lp_to_level(int wm_lp,const struct intel_pipe_wm * pipe_wm)3260 static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3261 {
3262 	/* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3263 	return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3264 }
3265 
3266 /* The value we need to program into the WM_LPx latency field */
ilk_wm_lp_latency(struct drm_i915_private * dev_priv,int level)3267 static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3268 				      int level)
3269 {
3270 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3271 		return 2 * level;
3272 	else
3273 		return dev_priv->display.wm.pri_latency[level];
3274 }
3275 
ilk_compute_wm_results(struct drm_i915_private * dev_priv,const struct intel_pipe_wm * merged,enum intel_ddb_partitioning partitioning,struct ilk_wm_values * results)3276 static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
3277 				   const struct intel_pipe_wm *merged,
3278 				   enum intel_ddb_partitioning partitioning,
3279 				   struct ilk_wm_values *results)
3280 {
3281 	struct intel_crtc *crtc;
3282 	int level, wm_lp;
3283 
3284 	results->enable_fbc_wm = merged->fbc_wm_enabled;
3285 	results->partitioning = partitioning;
3286 
3287 	/* LP1+ register values */
3288 	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3289 		const struct intel_wm_level *r;
3290 
3291 		level = ilk_wm_lp_to_level(wm_lp, merged);
3292 
3293 		r = &merged->wm[level];
3294 
3295 		/*
3296 		 * Maintain the watermark values even if the level is
3297 		 * disabled. Doing otherwise could cause underruns.
3298 		 */
3299 		results->wm_lp[wm_lp - 1] =
3300 			WM_LP_LATENCY(ilk_wm_lp_latency(dev_priv, level)) |
3301 			WM_LP_PRIMARY(r->pri_val) |
3302 			WM_LP_CURSOR(r->cur_val);
3303 
3304 		if (r->enable)
3305 			results->wm_lp[wm_lp - 1] |= WM_LP_ENABLE;
3306 
3307 		if (DISPLAY_VER(dev_priv) >= 8)
3308 			results->wm_lp[wm_lp - 1] |= WM_LP_FBC_BDW(r->fbc_val);
3309 		else
3310 			results->wm_lp[wm_lp - 1] |= WM_LP_FBC_ILK(r->fbc_val);
3311 
3312 		results->wm_lp_spr[wm_lp - 1] = WM_LP_SPRITE(r->spr_val);
3313 
3314 		/*
3315 		 * Always set WM_LP_SPRITE_EN when spr_val != 0, even if the
3316 		 * level is disabled. Doing otherwise could cause underruns.
3317 		 */
3318 		if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
3319 			drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
3320 			results->wm_lp_spr[wm_lp - 1] |= WM_LP_SPRITE_ENABLE;
3321 		}
3322 	}
3323 
3324 	/* LP0 register values */
3325 	for_each_intel_crtc(&dev_priv->drm, crtc) {
3326 		enum pipe pipe = crtc->pipe;
3327 		const struct intel_pipe_wm *pipe_wm = &crtc->wm.active.ilk;
3328 		const struct intel_wm_level *r = &pipe_wm->wm[0];
3329 
3330 		if (drm_WARN_ON(&dev_priv->drm, !r->enable))
3331 			continue;
3332 
3333 		results->wm_pipe[pipe] =
3334 			WM0_PIPE_PRIMARY(r->pri_val) |
3335 			WM0_PIPE_SPRITE(r->spr_val) |
3336 			WM0_PIPE_CURSOR(r->cur_val);
3337 	}
3338 }
3339 
3340 /* Find the result with the highest level enabled. Check for enable_fbc_wm in
3341  * case both are at the same level. Prefer r1 in case they're the same. */
3342 static struct intel_pipe_wm *
ilk_find_best_result(struct drm_i915_private * dev_priv,struct intel_pipe_wm * r1,struct intel_pipe_wm * r2)3343 ilk_find_best_result(struct drm_i915_private *dev_priv,
3344 		     struct intel_pipe_wm *r1,
3345 		     struct intel_pipe_wm *r2)
3346 {
3347 	int level, max_level = ilk_wm_max_level(dev_priv);
3348 	int level1 = 0, level2 = 0;
3349 
3350 	for (level = 1; level <= max_level; level++) {
3351 		if (r1->wm[level].enable)
3352 			level1 = level;
3353 		if (r2->wm[level].enable)
3354 			level2 = level;
3355 	}
3356 
3357 	if (level1 == level2) {
3358 		if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
3359 			return r2;
3360 		else
3361 			return r1;
3362 	} else if (level1 > level2) {
3363 		return r1;
3364 	} else {
3365 		return r2;
3366 	}
3367 }
3368 
3369 /* dirty bits used to track which watermarks need changes */
3370 #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3371 #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3372 #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3373 #define WM_DIRTY_FBC (1 << 24)
3374 #define WM_DIRTY_DDB (1 << 25)
3375 
ilk_compute_wm_dirty(struct drm_i915_private * dev_priv,const struct ilk_wm_values * old,const struct ilk_wm_values * new)3376 static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
3377 					 const struct ilk_wm_values *old,
3378 					 const struct ilk_wm_values *new)
3379 {
3380 	unsigned int dirty = 0;
3381 	enum pipe pipe;
3382 	int wm_lp;
3383 
3384 	for_each_pipe(dev_priv, pipe) {
3385 		if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3386 			dirty |= WM_DIRTY_PIPE(pipe);
3387 			/* Must disable LP1+ watermarks too */
3388 			dirty |= WM_DIRTY_LP_ALL;
3389 		}
3390 	}
3391 
3392 	if (old->enable_fbc_wm != new->enable_fbc_wm) {
3393 		dirty |= WM_DIRTY_FBC;
3394 		/* Must disable LP1+ watermarks too */
3395 		dirty |= WM_DIRTY_LP_ALL;
3396 	}
3397 
3398 	if (old->partitioning != new->partitioning) {
3399 		dirty |= WM_DIRTY_DDB;
3400 		/* Must disable LP1+ watermarks too */
3401 		dirty |= WM_DIRTY_LP_ALL;
3402 	}
3403 
3404 	/* LP1+ watermarks already deemed dirty, no need to continue */
3405 	if (dirty & WM_DIRTY_LP_ALL)
3406 		return dirty;
3407 
3408 	/* Find the lowest numbered LP1+ watermark in need of an update... */
3409 	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3410 		if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3411 		    old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3412 			break;
3413 	}
3414 
3415 	/* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3416 	for (; wm_lp <= 3; wm_lp++)
3417 		dirty |= WM_DIRTY_LP(wm_lp);
3418 
3419 	return dirty;
3420 }
3421 
_ilk_disable_lp_wm(struct drm_i915_private * dev_priv,unsigned int dirty)3422 static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3423 			       unsigned int dirty)
3424 {
3425 	struct ilk_wm_values *previous = &dev_priv->display.wm.hw;
3426 	bool changed = false;
3427 
3428 	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM_LP_ENABLE) {
3429 		previous->wm_lp[2] &= ~WM_LP_ENABLE;
3430 		intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
3431 		changed = true;
3432 	}
3433 	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM_LP_ENABLE) {
3434 		previous->wm_lp[1] &= ~WM_LP_ENABLE;
3435 		intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
3436 		changed = true;
3437 	}
3438 	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM_LP_ENABLE) {
3439 		previous->wm_lp[0] &= ~WM_LP_ENABLE;
3440 		intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
3441 		changed = true;
3442 	}
3443 
3444 	/*
3445 	 * Don't touch WM_LP_SPRITE_ENABLE here.
3446 	 * Doing so could cause underruns.
3447 	 */
3448 
3449 	return changed;
3450 }
3451 
3452 /*
3453  * The spec says we shouldn't write when we don't need, because every write
3454  * causes WMs to be re-evaluated, expending some power.
3455  */
ilk_write_wm_values(struct drm_i915_private * dev_priv,struct ilk_wm_values * results)3456 static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3457 				struct ilk_wm_values *results)
3458 {
3459 	struct ilk_wm_values *previous = &dev_priv->display.wm.hw;
3460 	unsigned int dirty;
3461 	u32 val;
3462 
3463 	dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
3464 	if (!dirty)
3465 		return;
3466 
3467 	_ilk_disable_lp_wm(dev_priv, dirty);
3468 
3469 	if (dirty & WM_DIRTY_PIPE(PIPE_A))
3470 		intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
3471 	if (dirty & WM_DIRTY_PIPE(PIPE_B))
3472 		intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
3473 	if (dirty & WM_DIRTY_PIPE(PIPE_C))
3474 		intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
3475 
3476 	if (dirty & WM_DIRTY_DDB) {
3477 		if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3478 			val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
3479 			if (results->partitioning == INTEL_DDB_PART_1_2)
3480 				val &= ~WM_MISC_DATA_PARTITION_5_6;
3481 			else
3482 				val |= WM_MISC_DATA_PARTITION_5_6;
3483 			intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
3484 		} else {
3485 			val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
3486 			if (results->partitioning == INTEL_DDB_PART_1_2)
3487 				val &= ~DISP_DATA_PARTITION_5_6;
3488 			else
3489 				val |= DISP_DATA_PARTITION_5_6;
3490 			intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
3491 		}
3492 	}
3493 
3494 	if (dirty & WM_DIRTY_FBC) {
3495 		val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
3496 		if (results->enable_fbc_wm)
3497 			val &= ~DISP_FBC_WM_DIS;
3498 		else
3499 			val |= DISP_FBC_WM_DIS;
3500 		intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
3501 	}
3502 
3503 	if (dirty & WM_DIRTY_LP(1) &&
3504 	    previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3505 		intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
3506 
3507 	if (DISPLAY_VER(dev_priv) >= 7) {
3508 		if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3509 			intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
3510 		if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3511 			intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
3512 	}
3513 
3514 	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
3515 		intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
3516 	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
3517 		intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
3518 	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
3519 		intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
3520 
3521 	dev_priv->display.wm.hw = *results;
3522 }
3523 
ilk_disable_lp_wm(struct drm_i915_private * dev_priv)3524 bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
3525 {
3526 	return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3527 }
3528 
ilk_compute_wm_config(struct drm_i915_private * dev_priv,struct intel_wm_config * config)3529 static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
3530 				  struct intel_wm_config *config)
3531 {
3532 	struct intel_crtc *crtc;
3533 
3534 	/* Compute the currently _active_ config */
3535 	for_each_intel_crtc(&dev_priv->drm, crtc) {
3536 		const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
3537 
3538 		if (!wm->pipe_enabled)
3539 			continue;
3540 
3541 		config->sprites_enabled |= wm->sprites_enabled;
3542 		config->sprites_scaled |= wm->sprites_scaled;
3543 		config->num_pipes_active++;
3544 	}
3545 }
3546 
ilk_program_watermarks(struct drm_i915_private * dev_priv)3547 static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
3548 {
3549 	struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
3550 	struct ilk_wm_maximums max;
3551 	struct intel_wm_config config = {};
3552 	struct ilk_wm_values results = {};
3553 	enum intel_ddb_partitioning partitioning;
3554 
3555 	ilk_compute_wm_config(dev_priv, &config);
3556 
3557 	ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
3558 	ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
3559 
3560 	/* 5/6 split only in single pipe config on IVB+ */
3561 	if (DISPLAY_VER(dev_priv) >= 7 &&
3562 	    config.num_pipes_active == 1 && config.sprites_enabled) {
3563 		ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
3564 		ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
3565 
3566 		best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
3567 	} else {
3568 		best_lp_wm = &lp_wm_1_2;
3569 	}
3570 
3571 	partitioning = (best_lp_wm == &lp_wm_1_2) ?
3572 		       INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
3573 
3574 	ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
3575 
3576 	ilk_write_wm_values(dev_priv, &results);
3577 }
3578 
ilk_initial_watermarks(struct intel_atomic_state * state,struct intel_crtc * crtc)3579 static void ilk_initial_watermarks(struct intel_atomic_state *state,
3580 				   struct intel_crtc *crtc)
3581 {
3582 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3583 	const struct intel_crtc_state *crtc_state =
3584 		intel_atomic_get_new_crtc_state(state, crtc);
3585 
3586 	mutex_lock(&dev_priv->display.wm.wm_mutex);
3587 	crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
3588 	ilk_program_watermarks(dev_priv);
3589 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
3590 }
3591 
ilk_optimize_watermarks(struct intel_atomic_state * state,struct intel_crtc * crtc)3592 static void ilk_optimize_watermarks(struct intel_atomic_state *state,
3593 				    struct intel_crtc *crtc)
3594 {
3595 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3596 	const struct intel_crtc_state *crtc_state =
3597 		intel_atomic_get_new_crtc_state(state, crtc);
3598 
3599 	if (!crtc_state->wm.need_postvbl_update)
3600 		return;
3601 
3602 	mutex_lock(&dev_priv->display.wm.wm_mutex);
3603 	crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
3604 	ilk_program_watermarks(dev_priv);
3605 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
3606 }
3607 
ilk_pipe_wm_get_hw_state(struct intel_crtc * crtc)3608 static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
3609 {
3610 	struct drm_device *dev = crtc->base.dev;
3611 	struct drm_i915_private *dev_priv = to_i915(dev);
3612 	struct ilk_wm_values *hw = &dev_priv->display.wm.hw;
3613 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
3614 	struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
3615 	enum pipe pipe = crtc->pipe;
3616 
3617 	hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
3618 
3619 	memset(active, 0, sizeof(*active));
3620 
3621 	active->pipe_enabled = crtc->active;
3622 
3623 	if (active->pipe_enabled) {
3624 		u32 tmp = hw->wm_pipe[pipe];
3625 
3626 		/*
3627 		 * For active pipes LP0 watermark is marked as
3628 		 * enabled, and LP1+ watermaks as disabled since
3629 		 * we can't really reverse compute them in case
3630 		 * multiple pipes are active.
3631 		 */
3632 		active->wm[0].enable = true;
3633 		active->wm[0].pri_val = REG_FIELD_GET(WM0_PIPE_PRIMARY_MASK, tmp);
3634 		active->wm[0].spr_val = REG_FIELD_GET(WM0_PIPE_SPRITE_MASK, tmp);
3635 		active->wm[0].cur_val = REG_FIELD_GET(WM0_PIPE_CURSOR_MASK, tmp);
3636 	} else {
3637 		int level, max_level = ilk_wm_max_level(dev_priv);
3638 
3639 		/*
3640 		 * For inactive pipes, all watermark levels
3641 		 * should be marked as enabled but zeroed,
3642 		 * which is what we'd compute them to.
3643 		 */
3644 		for (level = 0; level <= max_level; level++)
3645 			active->wm[level].enable = true;
3646 	}
3647 
3648 	crtc->wm.active.ilk = *active;
3649 }
3650 
3651 #define _FW_WM(value, plane) \
3652 	(((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3653 #define _FW_WM_VLV(value, plane) \
3654 	(((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3655 
g4x_read_wm_values(struct drm_i915_private * dev_priv,struct g4x_wm_values * wm)3656 static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
3657 			       struct g4x_wm_values *wm)
3658 {
3659 	u32 tmp;
3660 
3661 	tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
3662 	wm->sr.plane = _FW_WM(tmp, SR);
3663 	wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
3664 	wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
3665 	wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
3666 
3667 	tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
3668 	wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
3669 	wm->sr.fbc = _FW_WM(tmp, FBC_SR);
3670 	wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
3671 	wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
3672 	wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
3673 	wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
3674 
3675 	tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
3676 	wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
3677 	wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3678 	wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
3679 	wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
3680 }
3681 
vlv_read_wm_values(struct drm_i915_private * dev_priv,struct vlv_wm_values * wm)3682 static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
3683 			       struct vlv_wm_values *wm)
3684 {
3685 	enum pipe pipe;
3686 	u32 tmp;
3687 
3688 	for_each_pipe(dev_priv, pipe) {
3689 		tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
3690 
3691 		wm->ddl[pipe].plane[PLANE_PRIMARY] =
3692 			(tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3693 		wm->ddl[pipe].plane[PLANE_CURSOR] =
3694 			(tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3695 		wm->ddl[pipe].plane[PLANE_SPRITE0] =
3696 			(tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3697 		wm->ddl[pipe].plane[PLANE_SPRITE1] =
3698 			(tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3699 	}
3700 
3701 	tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
3702 	wm->sr.plane = _FW_WM(tmp, SR);
3703 	wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
3704 	wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
3705 	wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
3706 
3707 	tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
3708 	wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
3709 	wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
3710 	wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
3711 
3712 	tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
3713 	wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3714 
3715 	if (IS_CHERRYVIEW(dev_priv)) {
3716 		tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
3717 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
3718 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
3719 
3720 		tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
3721 		wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
3722 		wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
3723 
3724 		tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
3725 		wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
3726 		wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
3727 
3728 		tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
3729 		wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3730 		wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
3731 		wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
3732 		wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
3733 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3734 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3735 		wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
3736 		wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3737 		wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3738 		wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
3739 	} else {
3740 		tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
3741 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
3742 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
3743 
3744 		tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
3745 		wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3746 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3747 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3748 		wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
3749 		wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3750 		wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3751 		wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
3752 	}
3753 }
3754 
3755 #undef _FW_WM
3756 #undef _FW_WM_VLV
3757 
g4x_wm_get_hw_state(struct drm_i915_private * dev_priv)3758 void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
3759 {
3760 	struct g4x_wm_values *wm = &dev_priv->display.wm.g4x;
3761 	struct intel_crtc *crtc;
3762 
3763 	g4x_read_wm_values(dev_priv, wm);
3764 
3765 	wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
3766 
3767 	for_each_intel_crtc(&dev_priv->drm, crtc) {
3768 		struct intel_crtc_state *crtc_state =
3769 			to_intel_crtc_state(crtc->base.state);
3770 		struct g4x_wm_state *active = &crtc->wm.active.g4x;
3771 		struct g4x_pipe_wm *raw;
3772 		enum pipe pipe = crtc->pipe;
3773 		enum plane_id plane_id;
3774 		int level, max_level;
3775 
3776 		active->cxsr = wm->cxsr;
3777 		active->hpll_en = wm->hpll_en;
3778 		active->fbc_en = wm->fbc_en;
3779 
3780 		active->sr = wm->sr;
3781 		active->hpll = wm->hpll;
3782 
3783 		for_each_plane_id_on_crtc(crtc, plane_id) {
3784 			active->wm.plane[plane_id] =
3785 				wm->pipe[pipe].plane[plane_id];
3786 		}
3787 
3788 		if (wm->cxsr && wm->hpll_en)
3789 			max_level = G4X_WM_LEVEL_HPLL;
3790 		else if (wm->cxsr)
3791 			max_level = G4X_WM_LEVEL_SR;
3792 		else
3793 			max_level = G4X_WM_LEVEL_NORMAL;
3794 
3795 		level = G4X_WM_LEVEL_NORMAL;
3796 		raw = &crtc_state->wm.g4x.raw[level];
3797 		for_each_plane_id_on_crtc(crtc, plane_id)
3798 			raw->plane[plane_id] = active->wm.plane[plane_id];
3799 
3800 		level = G4X_WM_LEVEL_SR;
3801 		if (level > max_level)
3802 			goto out;
3803 
3804 		raw = &crtc_state->wm.g4x.raw[level];
3805 		raw->plane[PLANE_PRIMARY] = active->sr.plane;
3806 		raw->plane[PLANE_CURSOR] = active->sr.cursor;
3807 		raw->plane[PLANE_SPRITE0] = 0;
3808 		raw->fbc = active->sr.fbc;
3809 
3810 		level = G4X_WM_LEVEL_HPLL;
3811 		if (level > max_level)
3812 			goto out;
3813 
3814 		raw = &crtc_state->wm.g4x.raw[level];
3815 		raw->plane[PLANE_PRIMARY] = active->hpll.plane;
3816 		raw->plane[PLANE_CURSOR] = active->hpll.cursor;
3817 		raw->plane[PLANE_SPRITE0] = 0;
3818 		raw->fbc = active->hpll.fbc;
3819 
3820 		level++;
3821 	out:
3822 		for_each_plane_id_on_crtc(crtc, plane_id)
3823 			g4x_raw_plane_wm_set(crtc_state, level,
3824 					     plane_id, USHRT_MAX);
3825 		g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
3826 
3827 		crtc_state->wm.g4x.optimal = *active;
3828 		crtc_state->wm.g4x.intermediate = *active;
3829 
3830 		drm_dbg_kms(&dev_priv->drm,
3831 			    "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
3832 			    pipe_name(pipe),
3833 			    wm->pipe[pipe].plane[PLANE_PRIMARY],
3834 			    wm->pipe[pipe].plane[PLANE_CURSOR],
3835 			    wm->pipe[pipe].plane[PLANE_SPRITE0]);
3836 	}
3837 
3838 	drm_dbg_kms(&dev_priv->drm,
3839 		    "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
3840 		    wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
3841 	drm_dbg_kms(&dev_priv->drm,
3842 		    "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
3843 		    wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
3844 	drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
3845 		    str_yes_no(wm->cxsr), str_yes_no(wm->hpll_en),
3846 		    str_yes_no(wm->fbc_en));
3847 }
3848 
g4x_wm_sanitize(struct drm_i915_private * dev_priv)3849 void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
3850 {
3851 	struct intel_plane *plane;
3852 	struct intel_crtc *crtc;
3853 
3854 	mutex_lock(&dev_priv->display.wm.wm_mutex);
3855 
3856 	for_each_intel_plane(&dev_priv->drm, plane) {
3857 		struct intel_crtc *crtc =
3858 			intel_crtc_for_pipe(dev_priv, plane->pipe);
3859 		struct intel_crtc_state *crtc_state =
3860 			to_intel_crtc_state(crtc->base.state);
3861 		struct intel_plane_state *plane_state =
3862 			to_intel_plane_state(plane->base.state);
3863 		struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
3864 		enum plane_id plane_id = plane->id;
3865 		int level;
3866 
3867 		if (plane_state->uapi.visible)
3868 			continue;
3869 
3870 		for (level = 0; level < 3; level++) {
3871 			struct g4x_pipe_wm *raw =
3872 				&crtc_state->wm.g4x.raw[level];
3873 
3874 			raw->plane[plane_id] = 0;
3875 			wm_state->wm.plane[plane_id] = 0;
3876 		}
3877 
3878 		if (plane_id == PLANE_PRIMARY) {
3879 			for (level = 0; level < 3; level++) {
3880 				struct g4x_pipe_wm *raw =
3881 					&crtc_state->wm.g4x.raw[level];
3882 				raw->fbc = 0;
3883 			}
3884 
3885 			wm_state->sr.fbc = 0;
3886 			wm_state->hpll.fbc = 0;
3887 			wm_state->fbc_en = false;
3888 		}
3889 	}
3890 
3891 	for_each_intel_crtc(&dev_priv->drm, crtc) {
3892 		struct intel_crtc_state *crtc_state =
3893 			to_intel_crtc_state(crtc->base.state);
3894 
3895 		crtc_state->wm.g4x.intermediate =
3896 			crtc_state->wm.g4x.optimal;
3897 		crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
3898 	}
3899 
3900 	g4x_program_watermarks(dev_priv);
3901 
3902 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
3903 }
3904 
vlv_wm_get_hw_state(struct drm_i915_private * dev_priv)3905 void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
3906 {
3907 	struct vlv_wm_values *wm = &dev_priv->display.wm.vlv;
3908 	struct intel_crtc *crtc;
3909 	u32 val;
3910 
3911 	vlv_read_wm_values(dev_priv, wm);
3912 
3913 	wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
3914 	wm->level = VLV_WM_LEVEL_PM2;
3915 
3916 	if (IS_CHERRYVIEW(dev_priv)) {
3917 		vlv_punit_get(dev_priv);
3918 
3919 		val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
3920 		if (val & DSP_MAXFIFO_PM5_ENABLE)
3921 			wm->level = VLV_WM_LEVEL_PM5;
3922 
3923 		/*
3924 		 * If DDR DVFS is disabled in the BIOS, Punit
3925 		 * will never ack the request. So if that happens
3926 		 * assume we don't have to enable/disable DDR DVFS
3927 		 * dynamically. To test that just set the REQ_ACK
3928 		 * bit to poke the Punit, but don't change the
3929 		 * HIGH/LOW bits so that we don't actually change
3930 		 * the current state.
3931 		 */
3932 		val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
3933 		val |= FORCE_DDR_FREQ_REQ_ACK;
3934 		vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
3935 
3936 		if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
3937 			      FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
3938 			drm_dbg_kms(&dev_priv->drm,
3939 				    "Punit not acking DDR DVFS request, "
3940 				    "assuming DDR DVFS is disabled\n");
3941 			dev_priv->display.wm.max_level = VLV_WM_LEVEL_PM5;
3942 		} else {
3943 			val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
3944 			if ((val & FORCE_DDR_HIGH_FREQ) == 0)
3945 				wm->level = VLV_WM_LEVEL_DDR_DVFS;
3946 		}
3947 
3948 		vlv_punit_put(dev_priv);
3949 	}
3950 
3951 	for_each_intel_crtc(&dev_priv->drm, crtc) {
3952 		struct intel_crtc_state *crtc_state =
3953 			to_intel_crtc_state(crtc->base.state);
3954 		struct vlv_wm_state *active = &crtc->wm.active.vlv;
3955 		const struct vlv_fifo_state *fifo_state =
3956 			&crtc_state->wm.vlv.fifo_state;
3957 		enum pipe pipe = crtc->pipe;
3958 		enum plane_id plane_id;
3959 		int level;
3960 
3961 		vlv_get_fifo_size(crtc_state);
3962 
3963 		active->num_levels = wm->level + 1;
3964 		active->cxsr = wm->cxsr;
3965 
3966 		for (level = 0; level < active->num_levels; level++) {
3967 			struct g4x_pipe_wm *raw =
3968 				&crtc_state->wm.vlv.raw[level];
3969 
3970 			active->sr[level].plane = wm->sr.plane;
3971 			active->sr[level].cursor = wm->sr.cursor;
3972 
3973 			for_each_plane_id_on_crtc(crtc, plane_id) {
3974 				active->wm[level].plane[plane_id] =
3975 					wm->pipe[pipe].plane[plane_id];
3976 
3977 				raw->plane[plane_id] =
3978 					vlv_invert_wm_value(active->wm[level].plane[plane_id],
3979 							    fifo_state->plane[plane_id]);
3980 			}
3981 		}
3982 
3983 		for_each_plane_id_on_crtc(crtc, plane_id)
3984 			vlv_raw_plane_wm_set(crtc_state, level,
3985 					     plane_id, USHRT_MAX);
3986 		vlv_invalidate_wms(crtc, active, level);
3987 
3988 		crtc_state->wm.vlv.optimal = *active;
3989 		crtc_state->wm.vlv.intermediate = *active;
3990 
3991 		drm_dbg_kms(&dev_priv->drm,
3992 			    "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
3993 			    pipe_name(pipe),
3994 			    wm->pipe[pipe].plane[PLANE_PRIMARY],
3995 			    wm->pipe[pipe].plane[PLANE_CURSOR],
3996 			    wm->pipe[pipe].plane[PLANE_SPRITE0],
3997 			    wm->pipe[pipe].plane[PLANE_SPRITE1]);
3998 	}
3999 
4000 	drm_dbg_kms(&dev_priv->drm,
4001 		    "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4002 		    wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4003 }
4004 
vlv_wm_sanitize(struct drm_i915_private * dev_priv)4005 void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
4006 {
4007 	struct intel_plane *plane;
4008 	struct intel_crtc *crtc;
4009 
4010 	mutex_lock(&dev_priv->display.wm.wm_mutex);
4011 
4012 	for_each_intel_plane(&dev_priv->drm, plane) {
4013 		struct intel_crtc *crtc =
4014 			intel_crtc_for_pipe(dev_priv, plane->pipe);
4015 		struct intel_crtc_state *crtc_state =
4016 			to_intel_crtc_state(crtc->base.state);
4017 		struct intel_plane_state *plane_state =
4018 			to_intel_plane_state(plane->base.state);
4019 		struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
4020 		const struct vlv_fifo_state *fifo_state =
4021 			&crtc_state->wm.vlv.fifo_state;
4022 		enum plane_id plane_id = plane->id;
4023 		int level;
4024 
4025 		if (plane_state->uapi.visible)
4026 			continue;
4027 
4028 		for (level = 0; level < wm_state->num_levels; level++) {
4029 			struct g4x_pipe_wm *raw =
4030 				&crtc_state->wm.vlv.raw[level];
4031 
4032 			raw->plane[plane_id] = 0;
4033 
4034 			wm_state->wm[level].plane[plane_id] =
4035 				vlv_invert_wm_value(raw->plane[plane_id],
4036 						    fifo_state->plane[plane_id]);
4037 		}
4038 	}
4039 
4040 	for_each_intel_crtc(&dev_priv->drm, crtc) {
4041 		struct intel_crtc_state *crtc_state =
4042 			to_intel_crtc_state(crtc->base.state);
4043 
4044 		crtc_state->wm.vlv.intermediate =
4045 			crtc_state->wm.vlv.optimal;
4046 		crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
4047 	}
4048 
4049 	vlv_program_watermarks(dev_priv);
4050 
4051 	mutex_unlock(&dev_priv->display.wm.wm_mutex);
4052 }
4053 
4054 /*
4055  * FIXME should probably kill this and improve
4056  * the real watermark readout/sanitation instead
4057  */
ilk_init_lp_watermarks(struct drm_i915_private * dev_priv)4058 static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
4059 {
4060 	intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM_LP_ENABLE);
4061 	intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM_LP_ENABLE);
4062 	intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK) & ~WM_LP_ENABLE);
4063 
4064 	/*
4065 	 * Don't touch WM_LP_SPRITE_ENABLE here.
4066 	 * Doing so could cause underruns.
4067 	 */
4068 }
4069 
ilk_wm_get_hw_state(struct drm_i915_private * dev_priv)4070 void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
4071 {
4072 	struct ilk_wm_values *hw = &dev_priv->display.wm.hw;
4073 	struct intel_crtc *crtc;
4074 
4075 	ilk_init_lp_watermarks(dev_priv);
4076 
4077 	for_each_intel_crtc(&dev_priv->drm, crtc)
4078 		ilk_pipe_wm_get_hw_state(crtc);
4079 
4080 	hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
4081 	hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
4082 	hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
4083 
4084 	hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
4085 	if (DISPLAY_VER(dev_priv) >= 7) {
4086 		hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
4087 		hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
4088 	}
4089 
4090 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
4091 		hw->partitioning = (intel_uncore_read(&dev_priv->uncore, WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4092 			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4093 	else if (IS_IVYBRIDGE(dev_priv))
4094 		hw->partitioning = (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4095 			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4096 
4097 	hw->enable_fbc_wm =
4098 		!(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4099 }
4100 
ibx_init_clock_gating(struct drm_i915_private * dev_priv)4101 static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
4102 {
4103 	/*
4104 	 * On Ibex Peak and Cougar Point, we need to disable clock
4105 	 * gating for the panel power sequencer or it will fail to
4106 	 * start up when no ports are active.
4107 	 */
4108 	intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
4109 }
4110 
g4x_disable_trickle_feed(struct drm_i915_private * dev_priv)4111 static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
4112 {
4113 	enum pipe pipe;
4114 
4115 	for_each_pipe(dev_priv, pipe) {
4116 		intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
4117 			   intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
4118 			   DISP_TRICKLE_FEED_DISABLE);
4119 
4120 		intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
4121 		intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
4122 	}
4123 }
4124 
ilk_init_clock_gating(struct drm_i915_private * dev_priv)4125 static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
4126 {
4127 	u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
4128 
4129 	/*
4130 	 * Required for FBC
4131 	 * WaFbcDisableDpfcClockGating:ilk
4132 	 */
4133 	dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
4134 		   ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
4135 		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
4136 
4137 	intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
4138 		   MARIUNIT_CLOCK_GATE_DISABLE |
4139 		   SVSMUNIT_CLOCK_GATE_DISABLE);
4140 	intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
4141 		   VFMUNIT_CLOCK_GATE_DISABLE);
4142 
4143 	/*
4144 	 * According to the spec the following bits should be set in
4145 	 * order to enable memory self-refresh
4146 	 * The bit 22/21 of 0x42004
4147 	 * The bit 5 of 0x42020
4148 	 * The bit 15 of 0x45000
4149 	 */
4150 	intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
4151 		   (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
4152 		    ILK_DPARB_GATE | ILK_VSDPFD_FULL));
4153 	dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
4154 	intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
4155 		   (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
4156 		    DISP_FBC_WM_DIS));
4157 
4158 	/*
4159 	 * Based on the document from hardware guys the following bits
4160 	 * should be set unconditionally in order to enable FBC.
4161 	 * The bit 22 of 0x42000
4162 	 * The bit 22 of 0x42004
4163 	 * The bit 7,8,9 of 0x42020.
4164 	 */
4165 	if (IS_IRONLAKE_M(dev_priv)) {
4166 		/* WaFbcAsynchFlipDisableFbcQueue:ilk */
4167 		intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
4168 			   intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
4169 			   ILK_FBCQ_DIS);
4170 		intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
4171 			   intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
4172 			   ILK_DPARB_GATE);
4173 	}
4174 
4175 	intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
4176 
4177 	intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
4178 		   intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
4179 		   ILK_ELPIN_409_SELECT);
4180 
4181 	g4x_disable_trickle_feed(dev_priv);
4182 
4183 	ibx_init_clock_gating(dev_priv);
4184 }
4185 
cpt_init_clock_gating(struct drm_i915_private * dev_priv)4186 static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
4187 {
4188 	enum pipe pipe;
4189 	u32 val;
4190 
4191 	/*
4192 	 * On Ibex Peak and Cougar Point, we need to disable clock
4193 	 * gating for the panel power sequencer or it will fail to
4194 	 * start up when no ports are active.
4195 	 */
4196 	intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
4197 		   PCH_DPLUNIT_CLOCK_GATE_DISABLE |
4198 		   PCH_CPUNIT_CLOCK_GATE_DISABLE);
4199 	intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
4200 		   DPLS_EDP_PPS_FIX_DIS);
4201 	/* The below fixes the weird display corruption, a few pixels shifted
4202 	 * downward, on (only) LVDS of some HP laptops with IVY.
4203 	 */
4204 	for_each_pipe(dev_priv, pipe) {
4205 		val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
4206 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
4207 		val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
4208 		if (dev_priv->display.vbt.fdi_rx_polarity_inverted)
4209 			val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
4210 		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
4211 		val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
4212 		intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
4213 	}
4214 	/* WADP0ClockGatingDisable */
4215 	for_each_pipe(dev_priv, pipe) {
4216 		intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
4217 			   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
4218 	}
4219 }
4220 
gen6_check_mch_setup(struct drm_i915_private * dev_priv)4221 static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
4222 {
4223 	u32 tmp;
4224 
4225 	tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
4226 	if (REG_FIELD_GET(SSKPD_WM0_MASK_SNB, tmp) != 12)
4227 		drm_dbg_kms(&dev_priv->drm,
4228 			    "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
4229 			    tmp);
4230 }
4231 
gen6_init_clock_gating(struct drm_i915_private * dev_priv)4232 static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
4233 {
4234 	u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
4235 
4236 	intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
4237 
4238 	intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
4239 		   intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
4240 		   ILK_ELPIN_409_SELECT);
4241 
4242 	intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
4243 		   intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
4244 		   GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
4245 		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
4246 
4247 	/* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
4248 	 * gating disable must be set.  Failure to set it results in
4249 	 * flickering pixels due to Z write ordering failures after
4250 	 * some amount of runtime in the Mesa "fire" demo, and Unigine
4251 	 * Sanctuary and Tropics, and apparently anything else with
4252 	 * alpha test or pixel discard.
4253 	 *
4254 	 * According to the spec, bit 11 (RCCUNIT) must also be set,
4255 	 * but we didn't debug actual testcases to find it out.
4256 	 *
4257 	 * WaDisableRCCUnitClockGating:snb
4258 	 * WaDisableRCPBUnitClockGating:snb
4259 	 */
4260 	intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
4261 		   GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
4262 		   GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
4263 
4264 	/*
4265 	 * According to the spec the following bits should be
4266 	 * set in order to enable memory self-refresh and fbc:
4267 	 * The bit21 and bit22 of 0x42000
4268 	 * The bit21 and bit22 of 0x42004
4269 	 * The bit5 and bit7 of 0x42020
4270 	 * The bit14 of 0x70180
4271 	 * The bit14 of 0x71180
4272 	 *
4273 	 * WaFbcAsynchFlipDisableFbcQueue:snb
4274 	 */
4275 	intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
4276 		   intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
4277 		   ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
4278 	intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
4279 		   intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
4280 		   ILK_DPARB_GATE | ILK_VSDPFD_FULL);
4281 	intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
4282 		   intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
4283 		   ILK_DPARBUNIT_CLOCK_GATE_ENABLE  |
4284 		   ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
4285 
4286 	g4x_disable_trickle_feed(dev_priv);
4287 
4288 	cpt_init_clock_gating(dev_priv);
4289 
4290 	gen6_check_mch_setup(dev_priv);
4291 }
4292 
lpt_init_clock_gating(struct drm_i915_private * dev_priv)4293 static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
4294 {
4295 	/*
4296 	 * TODO: this bit should only be enabled when really needed, then
4297 	 * disabled when not needed anymore in order to save power.
4298 	 */
4299 	if (HAS_PCH_LPT_LP(dev_priv))
4300 		intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
4301 			   intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
4302 			   PCH_LP_PARTITION_LEVEL_DISABLE);
4303 
4304 	/* WADPOClockGatingDisable:hsw */
4305 	intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
4306 		   intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
4307 		   TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
4308 }
4309 
lpt_suspend_hw(struct drm_i915_private * dev_priv)4310 static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
4311 {
4312 	if (HAS_PCH_LPT_LP(dev_priv)) {
4313 		u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
4314 
4315 		val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
4316 		intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
4317 	}
4318 }
4319 
gen8_set_l3sqc_credits(struct drm_i915_private * dev_priv,int general_prio_credits,int high_prio_credits)4320 static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
4321 				   int general_prio_credits,
4322 				   int high_prio_credits)
4323 {
4324 	u32 misccpctl;
4325 	u32 val;
4326 
4327 	/* WaTempDisableDOPClkGating:bdw */
4328 	misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
4329 	intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
4330 
4331 	val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
4332 	val &= ~L3_PRIO_CREDITS_MASK;
4333 	val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
4334 	val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
4335 	intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
4336 
4337 	/*
4338 	 * Wait at least 100 clocks before re-enabling clock gating.
4339 	 * See the definition of L3SQCREG1 in BSpec.
4340 	 */
4341 	intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
4342 	udelay(1);
4343 	intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
4344 }
4345 
icl_init_clock_gating(struct drm_i915_private * dev_priv)4346 static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
4347 {
4348 	/* Wa_1409120013:icl,ehl */
4349 	intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
4350 			   DPFC_CHICKEN_COMP_DUMMY_PIXEL);
4351 
4352 	/*Wa_14010594013:icl, ehl */
4353 	intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
4354 			 0, ICL_DELAY_PMRSP);
4355 }
4356 
gen12lp_init_clock_gating(struct drm_i915_private * dev_priv)4357 static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
4358 {
4359 	/* Wa_1409120013 */
4360 	if (DISPLAY_VER(dev_priv) == 12)
4361 		intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
4362 				   DPFC_CHICKEN_COMP_DUMMY_PIXEL);
4363 
4364 	/* Wa_1409825376:tgl (pre-prod)*/
4365 	if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_C0))
4366 		intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_3, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_3) |
4367 			   TGL_VRH_GATING_DIS);
4368 
4369 	/* Wa_14013723622:tgl,rkl,dg1,adl-s */
4370 	if (DISPLAY_VER(dev_priv) == 12)
4371 		intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
4372 				 CLKREQ_POLICY_MEM_UP_OVRD, 0);
4373 }
4374 
adlp_init_clock_gating(struct drm_i915_private * dev_priv)4375 static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
4376 {
4377 	gen12lp_init_clock_gating(dev_priv);
4378 
4379 	/* Wa_22011091694:adlp */
4380 	intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
4381 
4382 	/* Bspec/49189 Initialize Sequence */
4383 	intel_de_rmw(dev_priv, GEN8_CHICKEN_DCPR_1, DDI_CLOCK_REG_ACCESS, 0);
4384 }
4385 
dg1_init_clock_gating(struct drm_i915_private * dev_priv)4386 static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
4387 {
4388 	gen12lp_init_clock_gating(dev_priv);
4389 
4390 	/* Wa_1409836686:dg1[a0] */
4391 	if (IS_DG1_GRAPHICS_STEP(dev_priv, STEP_A0, STEP_B0))
4392 		intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_3, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_3) |
4393 			   DPT_GATING_DIS);
4394 }
4395 
xehpsdv_init_clock_gating(struct drm_i915_private * dev_priv)4396 static void xehpsdv_init_clock_gating(struct drm_i915_private *dev_priv)
4397 {
4398 	/* Wa_22010146351:xehpsdv */
4399 	if (IS_XEHPSDV_GRAPHICS_STEP(dev_priv, STEP_A0, STEP_B0))
4400 		intel_uncore_rmw(&dev_priv->uncore, XEHP_CLOCK_GATE_DIS, 0, SGR_DIS);
4401 }
4402 
dg2_init_clock_gating(struct drm_i915_private * i915)4403 static void dg2_init_clock_gating(struct drm_i915_private *i915)
4404 {
4405 	/* Wa_22010954014:dg2 */
4406 	intel_uncore_rmw(&i915->uncore, XEHP_CLOCK_GATE_DIS, 0,
4407 			 SGSI_SIDECLK_DIS);
4408 
4409 	/*
4410 	 * Wa_14010733611:dg2_g10
4411 	 * Wa_22010146351:dg2_g10
4412 	 */
4413 	if (IS_DG2_GRAPHICS_STEP(i915, G10, STEP_A0, STEP_B0))
4414 		intel_uncore_rmw(&i915->uncore, XEHP_CLOCK_GATE_DIS, 0,
4415 				 SGR_DIS | SGGI_DIS);
4416 }
4417 
pvc_init_clock_gating(struct drm_i915_private * dev_priv)4418 static void pvc_init_clock_gating(struct drm_i915_private *dev_priv)
4419 {
4420 	/* Wa_14012385139:pvc */
4421 	if (IS_PVC_BD_STEP(dev_priv, STEP_A0, STEP_B0))
4422 		intel_uncore_rmw(&dev_priv->uncore, XEHP_CLOCK_GATE_DIS, 0, SGR_DIS);
4423 
4424 	/* Wa_22010954014:pvc */
4425 	if (IS_PVC_BD_STEP(dev_priv, STEP_A0, STEP_B0))
4426 		intel_uncore_rmw(&dev_priv->uncore, XEHP_CLOCK_GATE_DIS, 0, SGSI_SIDECLK_DIS);
4427 }
4428 
cnp_init_clock_gating(struct drm_i915_private * dev_priv)4429 static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
4430 {
4431 	if (!HAS_PCH_CNP(dev_priv))
4432 		return;
4433 
4434 	/* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
4435 	intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
4436 		   CNP_PWM_CGE_GATING_DISABLE);
4437 }
4438 
cfl_init_clock_gating(struct drm_i915_private * dev_priv)4439 static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
4440 {
4441 	cnp_init_clock_gating(dev_priv);
4442 	gen9_init_clock_gating(dev_priv);
4443 
4444 	/* WAC6entrylatency:cfl */
4445 	intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
4446 		   FBC_LLC_FULLY_OPEN);
4447 
4448 	/*
4449 	 * WaFbcTurnOffFbcWatermark:cfl
4450 	 * Display WA #0562: cfl
4451 	 */
4452 	intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
4453 		   DISP_FBC_WM_DIS);
4454 
4455 	/*
4456 	 * WaFbcNukeOnHostModify:cfl
4457 	 * Display WA #0873: cfl
4458 	 */
4459 	intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
4460 			   intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
4461 			   DPFC_NUKE_ON_ANY_MODIFICATION);
4462 }
4463 
kbl_init_clock_gating(struct drm_i915_private * dev_priv)4464 static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
4465 {
4466 	gen9_init_clock_gating(dev_priv);
4467 
4468 	/* WAC6entrylatency:kbl */
4469 	intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
4470 		   FBC_LLC_FULLY_OPEN);
4471 
4472 	/* WaDisableSDEUnitClockGating:kbl */
4473 	if (IS_KBL_GRAPHICS_STEP(dev_priv, 0, STEP_C0))
4474 		intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
4475 			   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
4476 
4477 	/* WaDisableGamClockGating:kbl */
4478 	if (IS_KBL_GRAPHICS_STEP(dev_priv, 0, STEP_C0))
4479 		intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
4480 			   GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
4481 
4482 	/*
4483 	 * WaFbcTurnOffFbcWatermark:kbl
4484 	 * Display WA #0562: kbl
4485 	 */
4486 	intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
4487 		   DISP_FBC_WM_DIS);
4488 
4489 	/*
4490 	 * WaFbcNukeOnHostModify:kbl
4491 	 * Display WA #0873: kbl
4492 	 */
4493 	intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
4494 			   intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
4495 			   DPFC_NUKE_ON_ANY_MODIFICATION);
4496 }
4497 
skl_init_clock_gating(struct drm_i915_private * dev_priv)4498 static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
4499 {
4500 	gen9_init_clock_gating(dev_priv);
4501 
4502 	/* WaDisableDopClockGating:skl */
4503 	intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL) &
4504 		   ~GEN7_DOP_CLOCK_GATE_ENABLE);
4505 
4506 	/* WAC6entrylatency:skl */
4507 	intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
4508 		   FBC_LLC_FULLY_OPEN);
4509 
4510 	/*
4511 	 * WaFbcTurnOffFbcWatermark:skl
4512 	 * Display WA #0562: skl
4513 	 */
4514 	intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
4515 		   DISP_FBC_WM_DIS);
4516 
4517 	/*
4518 	 * WaFbcNukeOnHostModify:skl
4519 	 * Display WA #0873: skl
4520 	 */
4521 	intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
4522 			   intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
4523 			   DPFC_NUKE_ON_ANY_MODIFICATION);
4524 
4525 	/*
4526 	 * WaFbcHighMemBwCorruptionAvoidance:skl
4527 	 * Display WA #0883: skl
4528 	 */
4529 	intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A),
4530 			   intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN(INTEL_FBC_A)) |
4531 			   DPFC_DISABLE_DUMMY0);
4532 }
4533 
bdw_init_clock_gating(struct drm_i915_private * dev_priv)4534 static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
4535 {
4536 	enum pipe pipe;
4537 
4538 	/* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
4539 	intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
4540 		   intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
4541 		   HSW_FBCQ_DIS);
4542 
4543 	/* WaSwitchSolVfFArbitrationPriority:bdw */
4544 	intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
4545 
4546 	/* WaPsrDPAMaskVBlankInSRD:bdw */
4547 	intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
4548 		   intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
4549 
4550 	for_each_pipe(dev_priv, pipe) {
4551 		/* WaPsrDPRSUnmaskVBlankInSRD:bdw */
4552 		intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
4553 			   intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
4554 			   BDW_DPRS_MASK_VBLANK_SRD);
4555 	}
4556 
4557 	/* WaVSRefCountFullforceMissDisable:bdw */
4558 	/* WaDSRefCountFullforceMissDisable:bdw */
4559 	intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
4560 		   intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
4561 		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
4562 
4563 	intel_uncore_write(&dev_priv->uncore, RING_PSMI_CTL(RENDER_RING_BASE),
4564 		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
4565 
4566 	/* WaDisableSDEUnitClockGating:bdw */
4567 	intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
4568 		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
4569 
4570 	/* WaProgramL3SqcReg1Default:bdw */
4571 	gen8_set_l3sqc_credits(dev_priv, 30, 2);
4572 
4573 	/* WaKVMNotificationOnConfigChange:bdw */
4574 	intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR2_1, intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR2_1)
4575 		   | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
4576 
4577 	lpt_init_clock_gating(dev_priv);
4578 
4579 	/* WaDisableDopClockGating:bdw
4580 	 *
4581 	 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
4582 	 * clock gating.
4583 	 */
4584 	intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
4585 		   intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
4586 }
4587 
hsw_init_clock_gating(struct drm_i915_private * dev_priv)4588 static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
4589 {
4590 	/* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
4591 	intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
4592 		   intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
4593 		   HSW_FBCQ_DIS);
4594 
4595 	/* This is required by WaCatErrorRejectionIssue:hsw */
4596 	intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4597 		   intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4598 		   GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4599 
4600 	/* WaSwitchSolVfFArbitrationPriority:hsw */
4601 	intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
4602 
4603 	lpt_init_clock_gating(dev_priv);
4604 }
4605 
ivb_init_clock_gating(struct drm_i915_private * dev_priv)4606 static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
4607 {
4608 	u32 snpcr;
4609 
4610 	intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
4611 
4612 	/* WaFbcAsynchFlipDisableFbcQueue:ivb */
4613 	intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
4614 		   intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
4615 		   ILK_FBCQ_DIS);
4616 
4617 	/* WaDisableBackToBackFlipFix:ivb */
4618 	intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
4619 		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4620 		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
4621 
4622 	if (IS_IVB_GT1(dev_priv))
4623 		intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
4624 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4625 	else {
4626 		/* must write both registers */
4627 		intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
4628 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4629 		intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
4630 			   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4631 	}
4632 
4633 	/*
4634 	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
4635 	 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
4636 	 */
4637 	intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
4638 		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
4639 
4640 	/* This is required by WaCatErrorRejectionIssue:ivb */
4641 	intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4642 			intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4643 			GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4644 
4645 	g4x_disable_trickle_feed(dev_priv);
4646 
4647 	snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
4648 	snpcr &= ~GEN6_MBC_SNPCR_MASK;
4649 	snpcr |= GEN6_MBC_SNPCR_MED;
4650 	intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
4651 
4652 	if (!HAS_PCH_NOP(dev_priv))
4653 		cpt_init_clock_gating(dev_priv);
4654 
4655 	gen6_check_mch_setup(dev_priv);
4656 }
4657 
vlv_init_clock_gating(struct drm_i915_private * dev_priv)4658 static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
4659 {
4660 	/* WaDisableBackToBackFlipFix:vlv */
4661 	intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
4662 		   CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
4663 		   CHICKEN3_DGMG_DONE_FIX_DISABLE);
4664 
4665 	/* WaDisableDopClockGating:vlv */
4666 	intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
4667 		   _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
4668 
4669 	/* This is required by WaCatErrorRejectionIssue:vlv */
4670 	intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
4671 		   intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
4672 		   GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
4673 
4674 	/*
4675 	 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
4676 	 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
4677 	 */
4678 	intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
4679 		   GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
4680 
4681 	/* WaDisableL3Bank2xClockGate:vlv
4682 	 * Disabling L3 clock gating- MMIO 940c[25] = 1
4683 	 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
4684 	intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
4685 		   intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
4686 
4687 	/*
4688 	 * WaDisableVLVClockGating_VBIIssue:vlv
4689 	 * Disable clock gating on th GCFG unit to prevent a delay
4690 	 * in the reporting of vblank events.
4691 	 */
4692 	intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
4693 }
4694 
chv_init_clock_gating(struct drm_i915_private * dev_priv)4695 static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
4696 {
4697 	/* WaVSRefCountFullforceMissDisable:chv */
4698 	/* WaDSRefCountFullforceMissDisable:chv */
4699 	intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
4700 		   intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
4701 		   ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
4702 
4703 	/* WaDisableSemaphoreAndSyncFlipWait:chv */
4704 	intel_uncore_write(&dev_priv->uncore, RING_PSMI_CTL(RENDER_RING_BASE),
4705 		   _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
4706 
4707 	/* WaDisableCSUnitClockGating:chv */
4708 	intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
4709 		   GEN6_CSUNIT_CLOCK_GATE_DISABLE);
4710 
4711 	/* WaDisableSDEUnitClockGating:chv */
4712 	intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
4713 		   GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
4714 
4715 	/*
4716 	 * WaProgramL3SqcReg1Default:chv
4717 	 * See gfxspecs/Related Documents/Performance Guide/
4718 	 * LSQC Setting Recommendations.
4719 	 */
4720 	gen8_set_l3sqc_credits(dev_priv, 38, 2);
4721 }
4722 
g4x_init_clock_gating(struct drm_i915_private * dev_priv)4723 static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
4724 {
4725 	u32 dspclk_gate;
4726 
4727 	intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
4728 	intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
4729 		   GS_UNIT_CLOCK_GATE_DISABLE |
4730 		   CL_UNIT_CLOCK_GATE_DISABLE);
4731 	intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
4732 	dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
4733 		OVRUNIT_CLOCK_GATE_DISABLE |
4734 		OVCUNIT_CLOCK_GATE_DISABLE;
4735 	if (IS_GM45(dev_priv))
4736 		dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
4737 	intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D(dev_priv), dspclk_gate);
4738 
4739 	g4x_disable_trickle_feed(dev_priv);
4740 }
4741 
i965gm_init_clock_gating(struct drm_i915_private * dev_priv)4742 static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
4743 {
4744 	struct intel_uncore *uncore = &dev_priv->uncore;
4745 
4746 	intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
4747 	intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
4748 	intel_uncore_write(uncore, DSPCLK_GATE_D(dev_priv), 0);
4749 	intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
4750 	intel_uncore_write16(uncore, DEUC, 0);
4751 	intel_uncore_write(uncore,
4752 			   MI_ARB_STATE,
4753 			   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
4754 }
4755 
i965g_init_clock_gating(struct drm_i915_private * dev_priv)4756 static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
4757 {
4758 	intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
4759 		   I965_RCC_CLOCK_GATE_DISABLE |
4760 		   I965_RCPB_CLOCK_GATE_DISABLE |
4761 		   I965_ISC_CLOCK_GATE_DISABLE |
4762 		   I965_FBC_CLOCK_GATE_DISABLE);
4763 	intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
4764 	intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
4765 		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
4766 }
4767 
gen3_init_clock_gating(struct drm_i915_private * dev_priv)4768 static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
4769 {
4770 	u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
4771 
4772 	dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
4773 		DSTATE_DOT_CLOCK_GATING;
4774 	intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
4775 
4776 	if (IS_PINEVIEW(dev_priv))
4777 		intel_uncore_write(&dev_priv->uncore, ECOSKPD(RENDER_RING_BASE),
4778 				   _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
4779 
4780 	/* IIR "flip pending" means done if this bit is set */
4781 	intel_uncore_write(&dev_priv->uncore, ECOSKPD(RENDER_RING_BASE),
4782 			   _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
4783 
4784 	/* interrupts should cause a wake up from C3 */
4785 	intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
4786 
4787 	/* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4788 	intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
4789 
4790 	intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
4791 		   _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
4792 }
4793 
i85x_init_clock_gating(struct drm_i915_private * dev_priv)4794 static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
4795 {
4796 	intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
4797 
4798 	/* interrupts should cause a wake up from C3 */
4799 	intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
4800 		   _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
4801 
4802 	intel_uncore_write(&dev_priv->uncore, MEM_MODE,
4803 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
4804 
4805 	/*
4806 	 * Have FBC ignore 3D activity since we use software
4807 	 * render tracking, and otherwise a pure 3D workload
4808 	 * (even if it just renders a single frame and then does
4809 	 * abosultely nothing) would not allow FBC to recompress
4810 	 * until a 2D blit occurs.
4811 	 */
4812 	intel_uncore_write(&dev_priv->uncore, SCPD0,
4813 		   _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
4814 }
4815 
i830_init_clock_gating(struct drm_i915_private * dev_priv)4816 static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
4817 {
4818 	intel_uncore_write(&dev_priv->uncore, MEM_MODE,
4819 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
4820 		   _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
4821 }
4822 
intel_init_clock_gating(struct drm_i915_private * dev_priv)4823 void intel_init_clock_gating(struct drm_i915_private *dev_priv)
4824 {
4825 	dev_priv->clock_gating_funcs->init_clock_gating(dev_priv);
4826 }
4827 
intel_suspend_hw(struct drm_i915_private * dev_priv)4828 void intel_suspend_hw(struct drm_i915_private *dev_priv)
4829 {
4830 	if (HAS_PCH_LPT(dev_priv))
4831 		lpt_suspend_hw(dev_priv);
4832 }
4833 
nop_init_clock_gating(struct drm_i915_private * dev_priv)4834 static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
4835 {
4836 	drm_dbg_kms(&dev_priv->drm,
4837 		    "No clock gating settings or workarounds applied.\n");
4838 }
4839 
4840 #define CG_FUNCS(platform)						\
4841 static const struct drm_i915_clock_gating_funcs platform##_clock_gating_funcs = { \
4842 	.init_clock_gating = platform##_init_clock_gating,		\
4843 }
4844 
4845 CG_FUNCS(pvc);
4846 CG_FUNCS(dg2);
4847 CG_FUNCS(xehpsdv);
4848 CG_FUNCS(adlp);
4849 CG_FUNCS(dg1);
4850 CG_FUNCS(gen12lp);
4851 CG_FUNCS(icl);
4852 CG_FUNCS(cfl);
4853 CG_FUNCS(skl);
4854 CG_FUNCS(kbl);
4855 CG_FUNCS(bxt);
4856 CG_FUNCS(glk);
4857 CG_FUNCS(bdw);
4858 CG_FUNCS(chv);
4859 CG_FUNCS(hsw);
4860 CG_FUNCS(ivb);
4861 CG_FUNCS(vlv);
4862 CG_FUNCS(gen6);
4863 CG_FUNCS(ilk);
4864 CG_FUNCS(g4x);
4865 CG_FUNCS(i965gm);
4866 CG_FUNCS(i965g);
4867 CG_FUNCS(gen3);
4868 CG_FUNCS(i85x);
4869 CG_FUNCS(i830);
4870 CG_FUNCS(nop);
4871 #undef CG_FUNCS
4872 
4873 /**
4874  * intel_init_clock_gating_hooks - setup the clock gating hooks
4875  * @dev_priv: device private
4876  *
4877  * Setup the hooks that configure which clocks of a given platform can be
4878  * gated and also apply various GT and display specific workarounds for these
4879  * platforms. Note that some GT specific workarounds are applied separately
4880  * when GPU contexts or batchbuffers start their execution.
4881  */
intel_init_clock_gating_hooks(struct drm_i915_private * dev_priv)4882 void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
4883 {
4884 	if (IS_PONTEVECCHIO(dev_priv))
4885 		dev_priv->clock_gating_funcs = &pvc_clock_gating_funcs;
4886 	else if (IS_DG2(dev_priv))
4887 		dev_priv->clock_gating_funcs = &dg2_clock_gating_funcs;
4888 	else if (IS_XEHPSDV(dev_priv))
4889 		dev_priv->clock_gating_funcs = &xehpsdv_clock_gating_funcs;
4890 	else if (IS_ALDERLAKE_P(dev_priv))
4891 		dev_priv->clock_gating_funcs = &adlp_clock_gating_funcs;
4892 	else if (IS_DG1(dev_priv))
4893 		dev_priv->clock_gating_funcs = &dg1_clock_gating_funcs;
4894 	else if (GRAPHICS_VER(dev_priv) == 12)
4895 		dev_priv->clock_gating_funcs = &gen12lp_clock_gating_funcs;
4896 	else if (GRAPHICS_VER(dev_priv) == 11)
4897 		dev_priv->clock_gating_funcs = &icl_clock_gating_funcs;
4898 	else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
4899 		dev_priv->clock_gating_funcs = &cfl_clock_gating_funcs;
4900 	else if (IS_SKYLAKE(dev_priv))
4901 		dev_priv->clock_gating_funcs = &skl_clock_gating_funcs;
4902 	else if (IS_KABYLAKE(dev_priv))
4903 		dev_priv->clock_gating_funcs = &kbl_clock_gating_funcs;
4904 	else if (IS_BROXTON(dev_priv))
4905 		dev_priv->clock_gating_funcs = &bxt_clock_gating_funcs;
4906 	else if (IS_GEMINILAKE(dev_priv))
4907 		dev_priv->clock_gating_funcs = &glk_clock_gating_funcs;
4908 	else if (IS_BROADWELL(dev_priv))
4909 		dev_priv->clock_gating_funcs = &bdw_clock_gating_funcs;
4910 	else if (IS_CHERRYVIEW(dev_priv))
4911 		dev_priv->clock_gating_funcs = &chv_clock_gating_funcs;
4912 	else if (IS_HASWELL(dev_priv))
4913 		dev_priv->clock_gating_funcs = &hsw_clock_gating_funcs;
4914 	else if (IS_IVYBRIDGE(dev_priv))
4915 		dev_priv->clock_gating_funcs = &ivb_clock_gating_funcs;
4916 	else if (IS_VALLEYVIEW(dev_priv))
4917 		dev_priv->clock_gating_funcs = &vlv_clock_gating_funcs;
4918 	else if (GRAPHICS_VER(dev_priv) == 6)
4919 		dev_priv->clock_gating_funcs = &gen6_clock_gating_funcs;
4920 	else if (GRAPHICS_VER(dev_priv) == 5)
4921 		dev_priv->clock_gating_funcs = &ilk_clock_gating_funcs;
4922 	else if (IS_G4X(dev_priv))
4923 		dev_priv->clock_gating_funcs = &g4x_clock_gating_funcs;
4924 	else if (IS_I965GM(dev_priv))
4925 		dev_priv->clock_gating_funcs = &i965gm_clock_gating_funcs;
4926 	else if (IS_I965G(dev_priv))
4927 		dev_priv->clock_gating_funcs = &i965g_clock_gating_funcs;
4928 	else if (GRAPHICS_VER(dev_priv) == 3)
4929 		dev_priv->clock_gating_funcs = &gen3_clock_gating_funcs;
4930 	else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
4931 		dev_priv->clock_gating_funcs = &i85x_clock_gating_funcs;
4932 	else if (GRAPHICS_VER(dev_priv) == 2)
4933 		dev_priv->clock_gating_funcs = &i830_clock_gating_funcs;
4934 	else {
4935 		MISSING_CASE(INTEL_DEVID(dev_priv));
4936 		dev_priv->clock_gating_funcs = &nop_clock_gating_funcs;
4937 	}
4938 }
4939 
4940 static const struct intel_wm_funcs ilk_wm_funcs = {
4941 	.compute_pipe_wm = ilk_compute_pipe_wm,
4942 	.compute_intermediate_wm = ilk_compute_intermediate_wm,
4943 	.initial_watermarks = ilk_initial_watermarks,
4944 	.optimize_watermarks = ilk_optimize_watermarks,
4945 };
4946 
4947 static const struct intel_wm_funcs vlv_wm_funcs = {
4948 	.compute_pipe_wm = vlv_compute_pipe_wm,
4949 	.compute_intermediate_wm = vlv_compute_intermediate_wm,
4950 	.initial_watermarks = vlv_initial_watermarks,
4951 	.optimize_watermarks = vlv_optimize_watermarks,
4952 	.atomic_update_watermarks = vlv_atomic_update_fifo,
4953 };
4954 
4955 static const struct intel_wm_funcs g4x_wm_funcs = {
4956 	.compute_pipe_wm = g4x_compute_pipe_wm,
4957 	.compute_intermediate_wm = g4x_compute_intermediate_wm,
4958 	.initial_watermarks = g4x_initial_watermarks,
4959 	.optimize_watermarks = g4x_optimize_watermarks,
4960 };
4961 
4962 static const struct intel_wm_funcs pnv_wm_funcs = {
4963 	.update_wm = pnv_update_wm,
4964 };
4965 
4966 static const struct intel_wm_funcs i965_wm_funcs = {
4967 	.update_wm = i965_update_wm,
4968 };
4969 
4970 static const struct intel_wm_funcs i9xx_wm_funcs = {
4971 	.update_wm = i9xx_update_wm,
4972 };
4973 
4974 static const struct intel_wm_funcs i845_wm_funcs = {
4975 	.update_wm = i845_update_wm,
4976 };
4977 
4978 static const struct intel_wm_funcs nop_funcs = {
4979 };
4980 
4981 /* Set up chip specific power management-related functions */
intel_init_pm(struct drm_i915_private * dev_priv)4982 void intel_init_pm(struct drm_i915_private *dev_priv)
4983 {
4984 	if (DISPLAY_VER(dev_priv) >= 9) {
4985 		skl_wm_init(dev_priv);
4986 		return;
4987 	}
4988 
4989 	/* For cxsr */
4990 	if (IS_PINEVIEW(dev_priv))
4991 		pnv_get_mem_freq(dev_priv);
4992 	else if (GRAPHICS_VER(dev_priv) == 5)
4993 		ilk_get_mem_freq(dev_priv);
4994 
4995 	/* For FIFO watermark updates */
4996 	if (HAS_PCH_SPLIT(dev_priv)) {
4997 		ilk_setup_wm_latency(dev_priv);
4998 
4999 		if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->display.wm.pri_latency[1] &&
5000 		     dev_priv->display.wm.spr_latency[1] && dev_priv->display.wm.cur_latency[1]) ||
5001 		    (DISPLAY_VER(dev_priv) != 5 && dev_priv->display.wm.pri_latency[0] &&
5002 		     dev_priv->display.wm.spr_latency[0] && dev_priv->display.wm.cur_latency[0])) {
5003 			dev_priv->display.funcs.wm = &ilk_wm_funcs;
5004 		} else {
5005 			drm_dbg_kms(&dev_priv->drm,
5006 				    "Failed to read display plane latency. "
5007 				    "Disable CxSR\n");
5008 			dev_priv->display.funcs.wm = &nop_funcs;
5009 		}
5010 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
5011 		vlv_setup_wm_latency(dev_priv);
5012 		dev_priv->display.funcs.wm = &vlv_wm_funcs;
5013 	} else if (IS_G4X(dev_priv)) {
5014 		g4x_setup_wm_latency(dev_priv);
5015 		dev_priv->display.funcs.wm = &g4x_wm_funcs;
5016 	} else if (IS_PINEVIEW(dev_priv)) {
5017 		if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
5018 					    dev_priv->is_ddr3,
5019 					    dev_priv->fsb_freq,
5020 					    dev_priv->mem_freq)) {
5021 			drm_info(&dev_priv->drm,
5022 				 "failed to find known CxSR latency "
5023 				 "(found ddr%s fsb freq %d, mem freq %d), "
5024 				 "disabling CxSR\n",
5025 				 (dev_priv->is_ddr3 == 1) ? "3" : "2",
5026 				 dev_priv->fsb_freq, dev_priv->mem_freq);
5027 			/* Disable CxSR and never update its watermark again */
5028 			intel_set_memory_cxsr(dev_priv, false);
5029 			dev_priv->display.funcs.wm = &nop_funcs;
5030 		} else
5031 			dev_priv->display.funcs.wm = &pnv_wm_funcs;
5032 	} else if (DISPLAY_VER(dev_priv) == 4) {
5033 		dev_priv->display.funcs.wm = &i965_wm_funcs;
5034 	} else if (DISPLAY_VER(dev_priv) == 3) {
5035 		dev_priv->display.funcs.wm = &i9xx_wm_funcs;
5036 	} else if (DISPLAY_VER(dev_priv) == 2) {
5037 		if (INTEL_NUM_PIPES(dev_priv) == 1)
5038 			dev_priv->display.funcs.wm = &i845_wm_funcs;
5039 		else
5040 			dev_priv->display.funcs.wm = &i9xx_wm_funcs;
5041 	} else {
5042 		drm_err(&dev_priv->drm,
5043 			"unexpected fall-through in %s\n", __func__);
5044 		dev_priv->display.funcs.wm = &nop_funcs;
5045 	}
5046 }
5047 
intel_pm_setup(struct drm_i915_private * dev_priv)5048 void intel_pm_setup(struct drm_i915_private *dev_priv)
5049 {
5050 	dev_priv->runtime_pm.suspended = false;
5051 	atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
5052 }
5053