xref: /linux/drivers/gpu/drm/i915/display/i9xx_wm.c (revision e47a324d6f07c9ef252cfce1f14cfa5110cbed99)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2023 Intel Corporation
4  */
5 
6 #include "i915_drv.h"
7 #include "i915_reg.h"
8 #include "i9xx_wm.h"
9 #include "i9xx_wm_regs.h"
10 #include "intel_atomic.h"
11 #include "intel_bo.h"
12 #include "intel_de.h"
13 #include "intel_display.h"
14 #include "intel_display_trace.h"
15 #include "intel_fb.h"
16 #include "intel_mchbar_regs.h"
17 #include "intel_wm.h"
18 #include "skl_watermark.h"
19 #include "vlv_sideband.h"
20 
21 struct intel_watermark_params {
22 	u16 fifo_size;
23 	u16 max_wm;
24 	u8 default_wm;
25 	u8 guard_size;
26 	u8 cacheline_size;
27 };
28 
29 /* used in computing the new watermarks state */
30 struct intel_wm_config {
31 	unsigned int num_pipes_active;
32 	bool sprites_enabled;
33 	bool sprites_scaled;
34 };
35 
36 struct cxsr_latency {
37 	bool is_desktop : 1;
38 	bool is_ddr3 : 1;
39 	u16 fsb_freq;
40 	u16 mem_freq;
41 	u16 display_sr;
42 	u16 display_hpll_disable;
43 	u16 cursor_sr;
44 	u16 cursor_hpll_disable;
45 };
46 
47 static const struct cxsr_latency cxsr_latency_table[] = {
48 	{1, 0, 800, 400, 3382, 33382, 3983, 33983},    /* DDR2-400 SC */
49 	{1, 0, 800, 667, 3354, 33354, 3807, 33807},    /* DDR2-667 SC */
50 	{1, 0, 800, 800, 3347, 33347, 3763, 33763},    /* DDR2-800 SC */
51 	{1, 1, 800, 667, 6420, 36420, 6873, 36873},    /* DDR3-667 SC */
52 	{1, 1, 800, 800, 5902, 35902, 6318, 36318},    /* DDR3-800 SC */
53 
54 	{1, 0, 667, 400, 3400, 33400, 4021, 34021},    /* DDR2-400 SC */
55 	{1, 0, 667, 667, 3372, 33372, 3845, 33845},    /* DDR2-667 SC */
56 	{1, 0, 667, 800, 3386, 33386, 3822, 33822},    /* DDR2-800 SC */
57 	{1, 1, 667, 667, 6438, 36438, 6911, 36911},    /* DDR3-667 SC */
58 	{1, 1, 667, 800, 5941, 35941, 6377, 36377},    /* DDR3-800 SC */
59 
60 	{1, 0, 400, 400, 3472, 33472, 4173, 34173},    /* DDR2-400 SC */
61 	{1, 0, 400, 667, 3443, 33443, 3996, 33996},    /* DDR2-667 SC */
62 	{1, 0, 400, 800, 3430, 33430, 3946, 33946},    /* DDR2-800 SC */
63 	{1, 1, 400, 667, 6509, 36509, 7062, 37062},    /* DDR3-667 SC */
64 	{1, 1, 400, 800, 5985, 35985, 6501, 36501},    /* DDR3-800 SC */
65 
66 	{0, 0, 800, 400, 3438, 33438, 4065, 34065},    /* DDR2-400 SC */
67 	{0, 0, 800, 667, 3410, 33410, 3889, 33889},    /* DDR2-667 SC */
68 	{0, 0, 800, 800, 3403, 33403, 3845, 33845},    /* DDR2-800 SC */
69 	{0, 1, 800, 667, 6476, 36476, 6955, 36955},    /* DDR3-667 SC */
70 	{0, 1, 800, 800, 5958, 35958, 6400, 36400},    /* DDR3-800 SC */
71 
72 	{0, 0, 667, 400, 3456, 33456, 4103, 34106},    /* DDR2-400 SC */
73 	{0, 0, 667, 667, 3428, 33428, 3927, 33927},    /* DDR2-667 SC */
74 	{0, 0, 667, 800, 3443, 33443, 3905, 33905},    /* DDR2-800 SC */
75 	{0, 1, 667, 667, 6494, 36494, 6993, 36993},    /* DDR3-667 SC */
76 	{0, 1, 667, 800, 5998, 35998, 6460, 36460},    /* DDR3-800 SC */
77 
78 	{0, 0, 400, 400, 3528, 33528, 4255, 34255},    /* DDR2-400 SC */
79 	{0, 0, 400, 667, 3500, 33500, 4079, 34079},    /* DDR2-667 SC */
80 	{0, 0, 400, 800, 3487, 33487, 4029, 34029},    /* DDR2-800 SC */
81 	{0, 1, 400, 667, 6566, 36566, 7145, 37145},    /* DDR3-667 SC */
82 	{0, 1, 400, 800, 6042, 36042, 6584, 36584},    /* DDR3-800 SC */
83 };
84 
85 static const struct cxsr_latency *pnv_get_cxsr_latency(struct intel_display *display)
86 {
87 	struct drm_i915_private *i915 = to_i915(display->drm);
88 	int i;
89 
90 	for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
91 		const struct cxsr_latency *latency = &cxsr_latency_table[i];
92 		bool is_desktop = !display->platform.mobile;
93 
94 		if (is_desktop == latency->is_desktop &&
95 		    i915->is_ddr3 == latency->is_ddr3 &&
96 		    DIV_ROUND_CLOSEST(i915->fsb_freq, 1000) == latency->fsb_freq &&
97 		    DIV_ROUND_CLOSEST(i915->mem_freq, 1000) == latency->mem_freq)
98 			return latency;
99 	}
100 
101 	drm_dbg_kms(display->drm,
102 		    "Could not find CxSR latency for DDR%s, FSB %u kHz, MEM %u kHz\n",
103 		    i915->is_ddr3 ? "3" : "2", i915->fsb_freq, i915->mem_freq);
104 
105 	return NULL;
106 }
107 
108 static void chv_set_memory_dvfs(struct intel_display *display, bool enable)
109 {
110 	struct drm_i915_private *dev_priv = to_i915(display->drm);
111 	u32 val;
112 
113 	vlv_punit_get(dev_priv);
114 
115 	val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
116 	if (enable)
117 		val &= ~FORCE_DDR_HIGH_FREQ;
118 	else
119 		val |= FORCE_DDR_HIGH_FREQ;
120 	val &= ~FORCE_DDR_LOW_FREQ;
121 	val |= FORCE_DDR_FREQ_REQ_ACK;
122 	vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
123 
124 	if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
125 		      FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
126 		drm_err(display->drm,
127 			"timed out waiting for Punit DDR DVFS request\n");
128 
129 	vlv_punit_put(dev_priv);
130 }
131 
132 static void chv_set_memory_pm5(struct intel_display *display, bool enable)
133 {
134 	struct drm_i915_private *dev_priv = to_i915(display->drm);
135 	u32 val;
136 
137 	vlv_punit_get(dev_priv);
138 
139 	val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
140 	if (enable)
141 		val |= DSP_MAXFIFO_PM5_ENABLE;
142 	else
143 		val &= ~DSP_MAXFIFO_PM5_ENABLE;
144 	vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
145 
146 	vlv_punit_put(dev_priv);
147 }
148 
149 #define FW_WM(value, plane) \
150 	(((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
151 
152 static bool _intel_set_memory_cxsr(struct intel_display *display, bool enable)
153 {
154 	bool was_enabled;
155 	u32 val;
156 
157 	if (display->platform.valleyview || display->platform.cherryview) {
158 		was_enabled = intel_de_read(display, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
159 		intel_de_write(display, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
160 		intel_de_posting_read(display, FW_BLC_SELF_VLV);
161 	} else if (display->platform.g4x || display->platform.i965gm) {
162 		was_enabled = intel_de_read(display, FW_BLC_SELF) & FW_BLC_SELF_EN;
163 		intel_de_write(display, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
164 		intel_de_posting_read(display, FW_BLC_SELF);
165 	} else if (display->platform.pineview) {
166 		val = intel_de_read(display, DSPFW3(display));
167 		was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
168 		if (enable)
169 			val |= PINEVIEW_SELF_REFRESH_EN;
170 		else
171 			val &= ~PINEVIEW_SELF_REFRESH_EN;
172 		intel_de_write(display, DSPFW3(display), val);
173 		intel_de_posting_read(display, DSPFW3(display));
174 	} else if (display->platform.i945g || display->platform.i945gm) {
175 		was_enabled = intel_de_read(display, FW_BLC_SELF) & FW_BLC_SELF_EN;
176 		val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
177 			       _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
178 		intel_de_write(display, FW_BLC_SELF, val);
179 		intel_de_posting_read(display, FW_BLC_SELF);
180 	} else if (display->platform.i915gm) {
181 		/*
182 		 * FIXME can't find a bit like this for 915G, and
183 		 * yet it does have the related watermark in
184 		 * FW_BLC_SELF. What's going on?
185 		 */
186 		was_enabled = intel_de_read(display, INSTPM) & INSTPM_SELF_EN;
187 		val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
188 			       _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
189 		intel_de_write(display, INSTPM, val);
190 		intel_de_posting_read(display, INSTPM);
191 	} else {
192 		return false;
193 	}
194 
195 	trace_intel_memory_cxsr(display, was_enabled, enable);
196 
197 	drm_dbg_kms(display->drm, "memory self-refresh is %s (was %s)\n",
198 		    str_enabled_disabled(enable),
199 		    str_enabled_disabled(was_enabled));
200 
201 	return was_enabled;
202 }
203 
204 /**
205  * intel_set_memory_cxsr - Configure CxSR state
206  * @display: display device
207  * @enable: Allow vs. disallow CxSR
208  *
209  * Allow or disallow the system to enter a special CxSR
210  * (C-state self refresh) state. What typically happens in CxSR mode
211  * is that several display FIFOs may get combined into a single larger
212  * FIFO for a particular plane (so called max FIFO mode) to allow the
213  * system to defer memory fetches longer, and the memory will enter
214  * self refresh.
215  *
216  * Note that enabling CxSR does not guarantee that the system enter
217  * this special mode, nor does it guarantee that the system stays
218  * in that mode once entered. So this just allows/disallows the system
219  * to autonomously utilize the CxSR mode. Other factors such as core
220  * C-states will affect when/if the system actually enters/exits the
221  * CxSR mode.
222  *
223  * Note that on VLV/CHV this actually only controls the max FIFO mode,
224  * and the system is free to enter/exit memory self refresh at any time
225  * even when the use of CxSR has been disallowed.
226  *
227  * While the system is actually in the CxSR/max FIFO mode, some plane
228  * control registers will not get latched on vblank. Thus in order to
229  * guarantee the system will respond to changes in the plane registers
230  * we must always disallow CxSR prior to making changes to those registers.
231  * Unfortunately the system will re-evaluate the CxSR conditions at
232  * frame start which happens after vblank start (which is when the plane
233  * registers would get latched), so we can't proceed with the plane update
234  * during the same frame where we disallowed CxSR.
235  *
236  * Certain platforms also have a deeper HPLL SR mode. Fortunately the
237  * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
238  * the hardware w.r.t. HPLL SR when writing to plane registers.
239  * Disallowing just CxSR is sufficient.
240  */
241 bool intel_set_memory_cxsr(struct intel_display *display, bool enable)
242 {
243 	bool ret;
244 
245 	mutex_lock(&display->wm.wm_mutex);
246 	ret = _intel_set_memory_cxsr(display, enable);
247 	if (display->platform.valleyview || display->platform.cherryview)
248 		display->wm.vlv.cxsr = enable;
249 	else if (display->platform.g4x)
250 		display->wm.g4x.cxsr = enable;
251 	mutex_unlock(&display->wm.wm_mutex);
252 
253 	return ret;
254 }
255 
256 /*
257  * Latency for FIFO fetches is dependent on several factors:
258  *   - memory configuration (speed, channels)
259  *   - chipset
260  *   - current MCH state
261  * It can be fairly high in some situations, so here we assume a fairly
262  * pessimal value.  It's a tradeoff between extra memory fetches (if we
263  * set this value too high, the FIFO will fetch frequently to stay full)
264  * and power consumption (set it too low to save power and we might see
265  * FIFO underruns and display "flicker").
266  *
267  * A value of 5us seems to be a good balance; safe for very low end
268  * platforms but not overly aggressive on lower latency configs.
269  */
270 static const int pessimal_latency_ns = 5000;
271 
272 #define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
273 	((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
274 
275 static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
276 {
277 	struct intel_display *display = to_intel_display(crtc_state);
278 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
279 	struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
280 	enum pipe pipe = crtc->pipe;
281 	int sprite0_start, sprite1_start;
282 	u32 dsparb, dsparb2, dsparb3;
283 
284 	switch (pipe) {
285 	case PIPE_A:
286 		dsparb = intel_de_read(display, DSPARB(display));
287 		dsparb2 = intel_de_read(display, DSPARB2);
288 		sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
289 		sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
290 		break;
291 	case PIPE_B:
292 		dsparb = intel_de_read(display, DSPARB(display));
293 		dsparb2 = intel_de_read(display, DSPARB2);
294 		sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
295 		sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
296 		break;
297 	case PIPE_C:
298 		dsparb2 = intel_de_read(display, DSPARB2);
299 		dsparb3 = intel_de_read(display, DSPARB3);
300 		sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
301 		sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
302 		break;
303 	default:
304 		MISSING_CASE(pipe);
305 		return;
306 	}
307 
308 	fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
309 	fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
310 	fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
311 	fifo_state->plane[PLANE_CURSOR] = 63;
312 }
313 
314 static int i9xx_get_fifo_size(struct intel_display *display,
315 			      enum i9xx_plane_id i9xx_plane)
316 {
317 	u32 dsparb = intel_de_read(display, DSPARB(display));
318 	int size;
319 
320 	size = dsparb & 0x7f;
321 	if (i9xx_plane == PLANE_B)
322 		size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
323 
324 	drm_dbg_kms(display->drm, "FIFO size - (0x%08x) %c: %d\n",
325 		    dsparb, plane_name(i9xx_plane), size);
326 
327 	return size;
328 }
329 
330 static int i830_get_fifo_size(struct intel_display *display,
331 			      enum i9xx_plane_id i9xx_plane)
332 {
333 	u32 dsparb = intel_de_read(display, DSPARB(display));
334 	int size;
335 
336 	size = dsparb & 0x1ff;
337 	if (i9xx_plane == PLANE_B)
338 		size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
339 	size >>= 1; /* Convert to cachelines */
340 
341 	drm_dbg_kms(display->drm, "FIFO size - (0x%08x) %c: %d\n",
342 		    dsparb, plane_name(i9xx_plane), size);
343 
344 	return size;
345 }
346 
347 static int i845_get_fifo_size(struct intel_display *display,
348 			      enum i9xx_plane_id i9xx_plane)
349 {
350 	u32 dsparb = intel_de_read(display, DSPARB(display));
351 	int size;
352 
353 	size = dsparb & 0x7f;
354 	size >>= 2; /* Convert to cachelines */
355 
356 	drm_dbg_kms(display->drm, "FIFO size - (0x%08x) %c: %d\n",
357 		    dsparb, plane_name(i9xx_plane), size);
358 
359 	return size;
360 }
361 
362 /* Pineview has different values for various configs */
363 static const struct intel_watermark_params pnv_display_wm = {
364 	.fifo_size = PINEVIEW_DISPLAY_FIFO,
365 	.max_wm = PINEVIEW_MAX_WM,
366 	.default_wm = PINEVIEW_DFT_WM,
367 	.guard_size = PINEVIEW_GUARD_WM,
368 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
369 };
370 
371 static const struct intel_watermark_params pnv_display_hplloff_wm = {
372 	.fifo_size = PINEVIEW_DISPLAY_FIFO,
373 	.max_wm = PINEVIEW_MAX_WM,
374 	.default_wm = PINEVIEW_DFT_HPLLOFF_WM,
375 	.guard_size = PINEVIEW_GUARD_WM,
376 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
377 };
378 
379 static const struct intel_watermark_params pnv_cursor_wm = {
380 	.fifo_size = PINEVIEW_CURSOR_FIFO,
381 	.max_wm = PINEVIEW_CURSOR_MAX_WM,
382 	.default_wm = PINEVIEW_CURSOR_DFT_WM,
383 	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
384 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
385 };
386 
387 static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
388 	.fifo_size = PINEVIEW_CURSOR_FIFO,
389 	.max_wm = PINEVIEW_CURSOR_MAX_WM,
390 	.default_wm = PINEVIEW_CURSOR_DFT_WM,
391 	.guard_size = PINEVIEW_CURSOR_GUARD_WM,
392 	.cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
393 };
394 
395 static const struct intel_watermark_params i965_cursor_wm_info = {
396 	.fifo_size = I965_CURSOR_FIFO,
397 	.max_wm = I965_CURSOR_MAX_WM,
398 	.default_wm = I965_CURSOR_DFT_WM,
399 	.guard_size = 2,
400 	.cacheline_size = I915_FIFO_LINE_SIZE,
401 };
402 
403 static const struct intel_watermark_params i945_wm_info = {
404 	.fifo_size = I945_FIFO_SIZE,
405 	.max_wm = I915_MAX_WM,
406 	.default_wm = 1,
407 	.guard_size = 2,
408 	.cacheline_size = I915_FIFO_LINE_SIZE,
409 };
410 
411 static const struct intel_watermark_params i915_wm_info = {
412 	.fifo_size = I915_FIFO_SIZE,
413 	.max_wm = I915_MAX_WM,
414 	.default_wm = 1,
415 	.guard_size = 2,
416 	.cacheline_size = I915_FIFO_LINE_SIZE,
417 };
418 
419 static const struct intel_watermark_params i830_a_wm_info = {
420 	.fifo_size = I855GM_FIFO_SIZE,
421 	.max_wm = I915_MAX_WM,
422 	.default_wm = 1,
423 	.guard_size = 2,
424 	.cacheline_size = I830_FIFO_LINE_SIZE,
425 };
426 
427 static const struct intel_watermark_params i830_bc_wm_info = {
428 	.fifo_size = I855GM_FIFO_SIZE,
429 	.max_wm = I915_MAX_WM / 2,
430 	.default_wm = 1,
431 	.guard_size = 2,
432 	.cacheline_size = I830_FIFO_LINE_SIZE,
433 };
434 
435 static const struct intel_watermark_params i845_wm_info = {
436 	.fifo_size = I830_FIFO_SIZE,
437 	.max_wm = I915_MAX_WM,
438 	.default_wm = 1,
439 	.guard_size = 2,
440 	.cacheline_size = I830_FIFO_LINE_SIZE,
441 };
442 
443 /**
444  * intel_wm_method1 - Method 1 / "small buffer" watermark formula
445  * @pixel_rate: Pipe pixel rate in kHz
446  * @cpp: Plane bytes per pixel
447  * @latency: Memory wakeup latency in 0.1us units
448  *
449  * Compute the watermark using the method 1 or "small buffer"
450  * formula. The caller may additionally add extra cachelines
451  * to account for TLB misses and clock crossings.
452  *
453  * This method is concerned with the short term drain rate
454  * of the FIFO, ie. it does not account for blanking periods
455  * which would effectively reduce the average drain rate across
456  * a longer period. The name "small" refers to the fact the
457  * FIFO is relatively small compared to the amount of data
458  * fetched.
459  *
460  * The FIFO level vs. time graph might look something like:
461  *
462  *   |\   |\
463  *   | \  | \
464  * __---__---__ (- plane active, _ blanking)
465  * -> time
466  *
467  * or perhaps like this:
468  *
469  *   |\|\  |\|\
470  * __----__----__ (- plane active, _ blanking)
471  * -> time
472  *
473  * Returns:
474  * The watermark in bytes
475  */
476 static unsigned int intel_wm_method1(unsigned int pixel_rate,
477 				     unsigned int cpp,
478 				     unsigned int latency)
479 {
480 	u64 ret;
481 
482 	ret = mul_u32_u32(pixel_rate, cpp * latency);
483 	ret = DIV_ROUND_UP_ULL(ret, 10000);
484 
485 	return ret;
486 }
487 
488 /**
489  * intel_wm_method2 - Method 2 / "large buffer" watermark formula
490  * @pixel_rate: Pipe pixel rate in kHz
491  * @htotal: Pipe horizontal total
492  * @width: Plane width in pixels
493  * @cpp: Plane bytes per pixel
494  * @latency: Memory wakeup latency in 0.1us units
495  *
496  * Compute the watermark using the method 2 or "large buffer"
497  * formula. The caller may additionally add extra cachelines
498  * to account for TLB misses and clock crossings.
499  *
500  * This method is concerned with the long term drain rate
501  * of the FIFO, ie. it does account for blanking periods
502  * which effectively reduce the average drain rate across
503  * a longer period. The name "large" refers to the fact the
504  * FIFO is relatively large compared to the amount of data
505  * fetched.
506  *
507  * The FIFO level vs. time graph might look something like:
508  *
509  *    |\___       |\___
510  *    |    \___   |    \___
511  *    |        \  |        \
512  * __ --__--__--__--__--__--__ (- plane active, _ blanking)
513  * -> time
514  *
515  * Returns:
516  * The watermark in bytes
517  */
518 static unsigned int intel_wm_method2(unsigned int pixel_rate,
519 				     unsigned int htotal,
520 				     unsigned int width,
521 				     unsigned int cpp,
522 				     unsigned int latency)
523 {
524 	unsigned int ret;
525 
526 	/*
527 	 * FIXME remove once all users are computing
528 	 * watermarks in the correct place.
529 	 */
530 	if (WARN_ON_ONCE(htotal == 0))
531 		htotal = 1;
532 
533 	ret = (latency * pixel_rate) / (htotal * 10000);
534 	ret = (ret + 1) * width * cpp;
535 
536 	return ret;
537 }
538 
539 /**
540  * intel_calculate_wm - calculate watermark level
541  * @display: display device
542  * @pixel_rate: pixel clock
543  * @wm: chip FIFO params
544  * @fifo_size: size of the FIFO buffer
545  * @cpp: bytes per pixel
546  * @latency_ns: memory latency for the platform
547  *
548  * Calculate the watermark level (the level at which the display plane will
549  * start fetching from memory again).  Each chip has a different display
550  * FIFO size and allocation, so the caller needs to figure that out and pass
551  * in the correct intel_watermark_params structure.
552  *
553  * As the pixel clock runs, the FIFO will be drained at a rate that depends
554  * on the pixel size.  When it reaches the watermark level, it'll start
555  * fetching FIFO line sized based chunks from memory until the FIFO fills
556  * past the watermark point.  If the FIFO drains completely, a FIFO underrun
557  * will occur, and a display engine hang could result.
558  */
559 static unsigned int intel_calculate_wm(struct intel_display *display,
560 				       int pixel_rate,
561 				       const struct intel_watermark_params *wm,
562 				       int fifo_size, int cpp,
563 				       unsigned int latency_ns)
564 {
565 	int entries, wm_size;
566 
567 	/*
568 	 * Note: we need to make sure we don't overflow for various clock &
569 	 * latency values.
570 	 * clocks go from a few thousand to several hundred thousand.
571 	 * latency is usually a few thousand
572 	 */
573 	entries = intel_wm_method1(pixel_rate, cpp,
574 				   latency_ns / 100);
575 	entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
576 		wm->guard_size;
577 	drm_dbg_kms(display->drm, "FIFO entries required for mode: %d\n", entries);
578 
579 	wm_size = fifo_size - entries;
580 	drm_dbg_kms(display->drm, "FIFO watermark level: %d\n", wm_size);
581 
582 	/* Don't promote wm_size to unsigned... */
583 	if (wm_size > wm->max_wm)
584 		wm_size = wm->max_wm;
585 	if (wm_size <= 0)
586 		wm_size = wm->default_wm;
587 
588 	/*
589 	 * Bspec seems to indicate that the value shouldn't be lower than
590 	 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
591 	 * Lets go for 8 which is the burst size since certain platforms
592 	 * already use a hardcoded 8 (which is what the spec says should be
593 	 * done).
594 	 */
595 	if (wm_size <= 8)
596 		wm_size = 8;
597 
598 	return wm_size;
599 }
600 
601 static bool is_disabling(int old, int new, int threshold)
602 {
603 	return old >= threshold && new < threshold;
604 }
605 
606 static bool is_enabling(int old, int new, int threshold)
607 {
608 	return old < threshold && new >= threshold;
609 }
610 
611 static bool intel_crtc_active(struct intel_crtc *crtc)
612 {
613 	/* Be paranoid as we can arrive here with only partial
614 	 * state retrieved from the hardware during setup.
615 	 *
616 	 * We can ditch the adjusted_mode.crtc_clock check as soon
617 	 * as Haswell has gained clock readout/fastboot support.
618 	 *
619 	 * We can ditch the crtc->primary->state->fb check as soon as we can
620 	 * properly reconstruct framebuffers.
621 	 *
622 	 * FIXME: The intel_crtc->active here should be switched to
623 	 * crtc->state->active once we have proper CRTC states wired up
624 	 * for atomic.
625 	 */
626 	return crtc->active && crtc->base.primary->state->fb &&
627 		crtc->config->hw.adjusted_mode.crtc_clock;
628 }
629 
630 static struct intel_crtc *single_enabled_crtc(struct intel_display *display)
631 {
632 	struct intel_crtc *crtc, *enabled = NULL;
633 
634 	for_each_intel_crtc(display->drm, crtc) {
635 		if (intel_crtc_active(crtc)) {
636 			if (enabled)
637 				return NULL;
638 			enabled = crtc;
639 		}
640 	}
641 
642 	return enabled;
643 }
644 
645 static void pnv_update_wm(struct intel_display *display)
646 {
647 	struct intel_crtc *crtc;
648 	const struct cxsr_latency *latency;
649 	u32 reg;
650 	unsigned int wm;
651 
652 	latency = pnv_get_cxsr_latency(display);
653 	if (!latency) {
654 		drm_dbg_kms(display->drm, "Unknown FSB/MEM, disabling CxSR\n");
655 		intel_set_memory_cxsr(display, false);
656 		return;
657 	}
658 
659 	crtc = single_enabled_crtc(display);
660 	if (crtc) {
661 		const struct drm_framebuffer *fb =
662 			crtc->base.primary->state->fb;
663 		int pixel_rate = crtc->config->pixel_rate;
664 		int cpp = fb->format->cpp[0];
665 
666 		/* Display SR */
667 		wm = intel_calculate_wm(display, pixel_rate,
668 					&pnv_display_wm,
669 					pnv_display_wm.fifo_size,
670 					cpp, latency->display_sr);
671 		reg = intel_de_read(display, DSPFW1(display));
672 		reg &= ~DSPFW_SR_MASK;
673 		reg |= FW_WM(wm, SR);
674 		intel_de_write(display, DSPFW1(display), reg);
675 		drm_dbg_kms(display->drm, "DSPFW1 register is %x\n", reg);
676 
677 		/* cursor SR */
678 		wm = intel_calculate_wm(display, pixel_rate,
679 					&pnv_cursor_wm,
680 					pnv_display_wm.fifo_size,
681 					4, latency->cursor_sr);
682 		intel_de_rmw(display, DSPFW3(display),
683 			     DSPFW_CURSOR_SR_MASK, FW_WM(wm, CURSOR_SR));
684 
685 		/* Display HPLL off SR */
686 		wm = intel_calculate_wm(display, pixel_rate,
687 					&pnv_display_hplloff_wm,
688 					pnv_display_hplloff_wm.fifo_size,
689 					cpp, latency->display_hpll_disable);
690 		intel_de_rmw(display, DSPFW3(display),
691 			     DSPFW_HPLL_SR_MASK, FW_WM(wm, HPLL_SR));
692 
693 		/* cursor HPLL off SR */
694 		wm = intel_calculate_wm(display, pixel_rate,
695 					&pnv_cursor_hplloff_wm,
696 					pnv_display_hplloff_wm.fifo_size,
697 					4, latency->cursor_hpll_disable);
698 		reg = intel_de_read(display, DSPFW3(display));
699 		reg &= ~DSPFW_HPLL_CURSOR_MASK;
700 		reg |= FW_WM(wm, HPLL_CURSOR);
701 		intel_de_write(display, DSPFW3(display), reg);
702 		drm_dbg_kms(display->drm, "DSPFW3 register is %x\n", reg);
703 
704 		intel_set_memory_cxsr(display, true);
705 	} else {
706 		intel_set_memory_cxsr(display, false);
707 	}
708 }
709 
710 static bool i9xx_wm_need_update(const struct intel_plane_state *old_plane_state,
711 				const struct intel_plane_state *new_plane_state)
712 {
713 	/* Update watermarks on tiling or size changes. */
714 	if (old_plane_state->uapi.visible != new_plane_state->uapi.visible)
715 		return true;
716 
717 	if (!old_plane_state->hw.fb || !new_plane_state->hw.fb)
718 		return false;
719 
720 	if (old_plane_state->hw.fb->modifier != new_plane_state->hw.fb->modifier ||
721 	    old_plane_state->hw.rotation != new_plane_state->hw.rotation ||
722 	    drm_rect_width(&old_plane_state->uapi.src) != drm_rect_width(&new_plane_state->uapi.src) ||
723 	    drm_rect_height(&old_plane_state->uapi.src) != drm_rect_height(&new_plane_state->uapi.src) ||
724 	    drm_rect_width(&old_plane_state->uapi.dst) != drm_rect_width(&new_plane_state->uapi.dst) ||
725 	    drm_rect_height(&old_plane_state->uapi.dst) != drm_rect_height(&new_plane_state->uapi.dst))
726 		return true;
727 
728 	return false;
729 }
730 
731 static void i9xx_wm_compute(struct intel_crtc_state *new_crtc_state,
732 			    const struct intel_plane_state *old_plane_state,
733 			    const struct intel_plane_state *new_plane_state)
734 {
735 	bool turn_off, turn_on, visible, was_visible, mode_changed;
736 
737 	mode_changed = intel_crtc_needs_modeset(new_crtc_state);
738 	was_visible = old_plane_state->uapi.visible;
739 	visible = new_plane_state->uapi.visible;
740 
741 	if (!was_visible && !visible)
742 		return;
743 
744 	turn_off = was_visible && (!visible || mode_changed);
745 	turn_on = visible && (!was_visible || mode_changed);
746 
747 	/* FIXME nuke when all wm code is atomic */
748 	if (turn_on) {
749 		new_crtc_state->update_wm_pre = true;
750 	} else if (turn_off) {
751 		new_crtc_state->update_wm_post = true;
752 	} else if (i9xx_wm_need_update(old_plane_state, new_plane_state)) {
753 		/* FIXME bollocks */
754 		new_crtc_state->update_wm_pre = true;
755 		new_crtc_state->update_wm_post = true;
756 	}
757 }
758 
759 static int i9xx_compute_watermarks(struct intel_atomic_state *state,
760 				   struct intel_crtc *crtc)
761 {
762 	struct intel_crtc_state *new_crtc_state =
763 		intel_atomic_get_new_crtc_state(state, crtc);
764 	const struct intel_plane_state *old_plane_state;
765 	const struct intel_plane_state *new_plane_state;
766 	struct intel_plane *plane;
767 	int i;
768 
769 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
770 					     new_plane_state, i) {
771 		if (plane->pipe != crtc->pipe)
772 			continue;
773 
774 		i9xx_wm_compute(new_crtc_state, old_plane_state, new_plane_state);
775 	}
776 
777 	return 0;
778 }
779 
780 /*
781  * Documentation says:
782  * "If the line size is small, the TLB fetches can get in the way of the
783  *  data fetches, causing some lag in the pixel data return which is not
784  *  accounted for in the above formulas. The following adjustment only
785  *  needs to be applied if eight whole lines fit in the buffer at once.
786  *  The WM is adjusted upwards by the difference between the FIFO size
787  *  and the size of 8 whole lines. This adjustment is always performed
788  *  in the actual pixel depth regardless of whether FBC is enabled or not."
789  */
790 static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
791 {
792 	int tlb_miss = fifo_size * 64 - width * cpp * 8;
793 
794 	return max(0, tlb_miss);
795 }
796 
797 static void g4x_write_wm_values(struct intel_display *display,
798 				const struct g4x_wm_values *wm)
799 {
800 	enum pipe pipe;
801 
802 	for_each_pipe(display, pipe)
803 		trace_g4x_wm(intel_crtc_for_pipe(display, pipe), wm);
804 
805 	intel_de_write(display, DSPFW1(display),
806 		       FW_WM(wm->sr.plane, SR) |
807 		       FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
808 		       FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
809 		       FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
810 	intel_de_write(display, DSPFW2(display),
811 		       (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
812 		       FW_WM(wm->sr.fbc, FBC_SR) |
813 		       FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
814 		       FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
815 		       FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
816 		       FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
817 	intel_de_write(display, DSPFW3(display),
818 		       (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
819 		       FW_WM(wm->sr.cursor, CURSOR_SR) |
820 		       FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
821 		       FW_WM(wm->hpll.plane, HPLL_SR));
822 
823 	intel_de_posting_read(display, DSPFW1(display));
824 }
825 
826 #define FW_WM_VLV(value, plane) \
827 	(((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
828 
829 static void vlv_write_wm_values(struct intel_display *display,
830 				const struct vlv_wm_values *wm)
831 {
832 	enum pipe pipe;
833 
834 	for_each_pipe(display, pipe) {
835 		trace_vlv_wm(intel_crtc_for_pipe(display, pipe), wm);
836 
837 		intel_de_write(display, VLV_DDL(pipe),
838 			       (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
839 			       (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
840 			       (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
841 			       (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
842 	}
843 
844 	/*
845 	 * Zero the (unused) WM1 watermarks, and also clear all the
846 	 * high order bits so that there are no out of bounds values
847 	 * present in the registers during the reprogramming.
848 	 */
849 	intel_de_write(display, DSPHOWM, 0);
850 	intel_de_write(display, DSPHOWM1, 0);
851 	intel_de_write(display, DSPFW4, 0);
852 	intel_de_write(display, DSPFW5, 0);
853 	intel_de_write(display, DSPFW6, 0);
854 
855 	intel_de_write(display, DSPFW1(display),
856 		       FW_WM(wm->sr.plane, SR) |
857 		       FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
858 		       FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
859 		       FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
860 	intel_de_write(display, DSPFW2(display),
861 		       FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
862 		       FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
863 		       FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
864 	intel_de_write(display, DSPFW3(display),
865 		       FW_WM(wm->sr.cursor, CURSOR_SR));
866 
867 	if (display->platform.cherryview) {
868 		intel_de_write(display, DSPFW7_CHV,
869 			       FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
870 			       FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
871 		intel_de_write(display, DSPFW8_CHV,
872 			       FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
873 			       FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
874 		intel_de_write(display, DSPFW9_CHV,
875 			       FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
876 			       FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
877 		intel_de_write(display, DSPHOWM,
878 			       FW_WM(wm->sr.plane >> 9, SR_HI) |
879 			       FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
880 			       FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
881 			       FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
882 			       FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
883 			       FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
884 			       FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
885 			       FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
886 			       FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
887 			       FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
888 	} else {
889 		intel_de_write(display, DSPFW7,
890 			       FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
891 			       FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
892 		intel_de_write(display, DSPHOWM,
893 			       FW_WM(wm->sr.plane >> 9, SR_HI) |
894 			       FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
895 			       FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
896 			       FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
897 			       FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
898 			       FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
899 			       FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
900 	}
901 
902 	intel_de_posting_read(display, DSPFW1(display));
903 }
904 
905 #undef FW_WM_VLV
906 
907 static void g4x_setup_wm_latency(struct intel_display *display)
908 {
909 	/* all latencies in usec */
910 	display->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
911 	display->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
912 	display->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
913 
914 	display->wm.num_levels = G4X_WM_LEVEL_HPLL + 1;
915 }
916 
917 static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
918 {
919 	/*
920 	 * DSPCNTR[13] supposedly controls whether the
921 	 * primary plane can use the FIFO space otherwise
922 	 * reserved for the sprite plane. It's not 100% clear
923 	 * what the actual FIFO size is, but it looks like we
924 	 * can happily set both primary and sprite watermarks
925 	 * up to 127 cachelines. So that would seem to mean
926 	 * that either DSPCNTR[13] doesn't do anything, or that
927 	 * the total FIFO is >= 256 cachelines in size. Either
928 	 * way, we don't seem to have to worry about this
929 	 * repartitioning as the maximum watermark value the
930 	 * register can hold for each plane is lower than the
931 	 * minimum FIFO size.
932 	 */
933 	switch (plane_id) {
934 	case PLANE_CURSOR:
935 		return 63;
936 	case PLANE_PRIMARY:
937 		return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
938 	case PLANE_SPRITE0:
939 		return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
940 	default:
941 		MISSING_CASE(plane_id);
942 		return 0;
943 	}
944 }
945 
946 static int g4x_fbc_fifo_size(int level)
947 {
948 	switch (level) {
949 	case G4X_WM_LEVEL_SR:
950 		return 7;
951 	case G4X_WM_LEVEL_HPLL:
952 		return 15;
953 	default:
954 		MISSING_CASE(level);
955 		return 0;
956 	}
957 }
958 
959 static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
960 			  const struct intel_plane_state *plane_state,
961 			  int level)
962 {
963 	struct intel_display *display = to_intel_display(plane_state);
964 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
965 	const struct drm_display_mode *pipe_mode =
966 		&crtc_state->hw.pipe_mode;
967 	unsigned int latency = display->wm.pri_latency[level] * 10;
968 	unsigned int pixel_rate, htotal, cpp, width, wm;
969 
970 	if (latency == 0)
971 		return USHRT_MAX;
972 
973 	if (!intel_wm_plane_visible(crtc_state, plane_state))
974 		return 0;
975 
976 	cpp = plane_state->hw.fb->format->cpp[0];
977 
978 	/*
979 	 * WaUse32BppForSRWM:ctg,elk
980 	 *
981 	 * The spec fails to list this restriction for the
982 	 * HPLL watermark, which seems a little strange.
983 	 * Let's use 32bpp for the HPLL watermark as well.
984 	 */
985 	if (plane->id == PLANE_PRIMARY &&
986 	    level != G4X_WM_LEVEL_NORMAL)
987 		cpp = max(cpp, 4u);
988 
989 	pixel_rate = crtc_state->pixel_rate;
990 	htotal = pipe_mode->crtc_htotal;
991 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
992 
993 	if (plane->id == PLANE_CURSOR) {
994 		wm = intel_wm_method2(pixel_rate, htotal, width, cpp, latency);
995 	} else if (plane->id == PLANE_PRIMARY &&
996 		   level == G4X_WM_LEVEL_NORMAL) {
997 		wm = intel_wm_method1(pixel_rate, cpp, latency);
998 	} else {
999 		unsigned int small, large;
1000 
1001 		small = intel_wm_method1(pixel_rate, cpp, latency);
1002 		large = intel_wm_method2(pixel_rate, htotal, width, cpp, latency);
1003 
1004 		wm = min(small, large);
1005 	}
1006 
1007 	wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1008 			      width, cpp);
1009 
1010 	wm = DIV_ROUND_UP(wm, 64) + 2;
1011 
1012 	return min_t(unsigned int, wm, USHRT_MAX);
1013 }
1014 
1015 static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1016 				 int level, enum plane_id plane_id, u16 value)
1017 {
1018 	struct intel_display *display = to_intel_display(crtc_state);
1019 	bool dirty = false;
1020 
1021 	for (; level < display->wm.num_levels; level++) {
1022 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1023 
1024 		dirty |= raw->plane[plane_id] != value;
1025 		raw->plane[plane_id] = value;
1026 	}
1027 
1028 	return dirty;
1029 }
1030 
1031 static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1032 			       int level, u16 value)
1033 {
1034 	struct intel_display *display = to_intel_display(crtc_state);
1035 	bool dirty = false;
1036 
1037 	/* NORMAL level doesn't have an FBC watermark */
1038 	level = max(level, G4X_WM_LEVEL_SR);
1039 
1040 	for (; level < display->wm.num_levels; level++) {
1041 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1042 
1043 		dirty |= raw->fbc != value;
1044 		raw->fbc = value;
1045 	}
1046 
1047 	return dirty;
1048 }
1049 
1050 static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1051 			      const struct intel_plane_state *plane_state,
1052 			      u32 pri_val);
1053 
1054 static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1055 				     const struct intel_plane_state *plane_state)
1056 {
1057 	struct intel_display *display = to_intel_display(crtc_state);
1058 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1059 	enum plane_id plane_id = plane->id;
1060 	bool dirty = false;
1061 	int level;
1062 
1063 	if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1064 		dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1065 		if (plane_id == PLANE_PRIMARY)
1066 			dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1067 		goto out;
1068 	}
1069 
1070 	for (level = 0; level < display->wm.num_levels; level++) {
1071 		struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1072 		int wm, max_wm;
1073 
1074 		wm = g4x_compute_wm(crtc_state, plane_state, level);
1075 		max_wm = g4x_plane_fifo_size(plane_id, level);
1076 
1077 		if (wm > max_wm)
1078 			break;
1079 
1080 		dirty |= raw->plane[plane_id] != wm;
1081 		raw->plane[plane_id] = wm;
1082 
1083 		if (plane_id != PLANE_PRIMARY ||
1084 		    level == G4X_WM_LEVEL_NORMAL)
1085 			continue;
1086 
1087 		wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1088 					raw->plane[plane_id]);
1089 		max_wm = g4x_fbc_fifo_size(level);
1090 
1091 		/*
1092 		 * FBC wm is not mandatory as we
1093 		 * can always just disable its use.
1094 		 */
1095 		if (wm > max_wm)
1096 			wm = USHRT_MAX;
1097 
1098 		dirty |= raw->fbc != wm;
1099 		raw->fbc = wm;
1100 	}
1101 
1102 	/* mark watermarks as invalid */
1103 	dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1104 
1105 	if (plane_id == PLANE_PRIMARY)
1106 		dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1107 
1108  out:
1109 	if (dirty) {
1110 		drm_dbg_kms(display->drm,
1111 			    "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1112 			    plane->base.name,
1113 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1114 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1115 			    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1116 
1117 		if (plane_id == PLANE_PRIMARY)
1118 			drm_dbg_kms(display->drm,
1119 				    "FBC watermarks: SR=%d, HPLL=%d\n",
1120 				    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1121 				    crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1122 	}
1123 
1124 	return dirty;
1125 }
1126 
1127 static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1128 				      enum plane_id plane_id, int level)
1129 {
1130 	const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1131 
1132 	return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1133 }
1134 
1135 static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1136 				     int level)
1137 {
1138 	struct intel_display *display = to_intel_display(crtc_state);
1139 
1140 	if (level >= display->wm.num_levels)
1141 		return false;
1142 
1143 	return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1144 		g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1145 		g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1146 }
1147 
1148 /* mark all levels starting from 'level' as invalid */
1149 static void g4x_invalidate_wms(struct intel_crtc *crtc,
1150 			       struct g4x_wm_state *wm_state, int level)
1151 {
1152 	if (level <= G4X_WM_LEVEL_NORMAL) {
1153 		enum plane_id plane_id;
1154 
1155 		for_each_plane_id_on_crtc(crtc, plane_id)
1156 			wm_state->wm.plane[plane_id] = USHRT_MAX;
1157 	}
1158 
1159 	if (level <= G4X_WM_LEVEL_SR) {
1160 		wm_state->cxsr = false;
1161 		wm_state->sr.cursor = USHRT_MAX;
1162 		wm_state->sr.plane = USHRT_MAX;
1163 		wm_state->sr.fbc = USHRT_MAX;
1164 	}
1165 
1166 	if (level <= G4X_WM_LEVEL_HPLL) {
1167 		wm_state->hpll_en = false;
1168 		wm_state->hpll.cursor = USHRT_MAX;
1169 		wm_state->hpll.plane = USHRT_MAX;
1170 		wm_state->hpll.fbc = USHRT_MAX;
1171 	}
1172 }
1173 
1174 static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1175 			       int level)
1176 {
1177 	if (level < G4X_WM_LEVEL_SR)
1178 		return false;
1179 
1180 	if (level >= G4X_WM_LEVEL_SR &&
1181 	    wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1182 		return false;
1183 
1184 	if (level >= G4X_WM_LEVEL_HPLL &&
1185 	    wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1186 		return false;
1187 
1188 	return true;
1189 }
1190 
1191 static int _g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1192 {
1193 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1194 	struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1195 	u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1196 	const struct g4x_pipe_wm *raw;
1197 	enum plane_id plane_id;
1198 	int level;
1199 
1200 	level = G4X_WM_LEVEL_NORMAL;
1201 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1202 		goto out;
1203 
1204 	raw = &crtc_state->wm.g4x.raw[level];
1205 	for_each_plane_id_on_crtc(crtc, plane_id)
1206 		wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1207 
1208 	level = G4X_WM_LEVEL_SR;
1209 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1210 		goto out;
1211 
1212 	raw = &crtc_state->wm.g4x.raw[level];
1213 	wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1214 	wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1215 	wm_state->sr.fbc = raw->fbc;
1216 
1217 	wm_state->cxsr = active_planes == BIT(PLANE_PRIMARY);
1218 
1219 	level = G4X_WM_LEVEL_HPLL;
1220 	if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1221 		goto out;
1222 
1223 	raw = &crtc_state->wm.g4x.raw[level];
1224 	wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1225 	wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1226 	wm_state->hpll.fbc = raw->fbc;
1227 
1228 	wm_state->hpll_en = wm_state->cxsr;
1229 
1230 	level++;
1231 
1232  out:
1233 	if (level == G4X_WM_LEVEL_NORMAL)
1234 		return -EINVAL;
1235 
1236 	/* invalidate the higher levels */
1237 	g4x_invalidate_wms(crtc, wm_state, level);
1238 
1239 	/*
1240 	 * Determine if the FBC watermark(s) can be used. IF
1241 	 * this isn't the case we prefer to disable the FBC
1242 	 * watermark(s) rather than disable the SR/HPLL
1243 	 * level(s) entirely. 'level-1' is the highest valid
1244 	 * level here.
1245 	 */
1246 	wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
1247 
1248 	return 0;
1249 }
1250 
1251 static int g4x_compute_pipe_wm(struct intel_atomic_state *state,
1252 			       struct intel_crtc *crtc)
1253 {
1254 	struct intel_crtc_state *crtc_state =
1255 		intel_atomic_get_new_crtc_state(state, crtc);
1256 	const struct intel_plane_state *old_plane_state;
1257 	const struct intel_plane_state *new_plane_state;
1258 	struct intel_plane *plane;
1259 	unsigned int dirty = 0;
1260 	int i;
1261 
1262 	for_each_oldnew_intel_plane_in_state(state, plane,
1263 					     old_plane_state,
1264 					     new_plane_state, i) {
1265 		if (new_plane_state->hw.crtc != &crtc->base &&
1266 		    old_plane_state->hw.crtc != &crtc->base)
1267 			continue;
1268 
1269 		if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
1270 			dirty |= BIT(plane->id);
1271 	}
1272 
1273 	if (!dirty)
1274 		return 0;
1275 
1276 	return _g4x_compute_pipe_wm(crtc_state);
1277 }
1278 
1279 static int g4x_compute_intermediate_wm(struct intel_atomic_state *state,
1280 				       struct intel_crtc *crtc)
1281 {
1282 	struct intel_display *display = to_intel_display(state);
1283 	struct intel_crtc_state *new_crtc_state =
1284 		intel_atomic_get_new_crtc_state(state, crtc);
1285 	const struct intel_crtc_state *old_crtc_state =
1286 		intel_atomic_get_old_crtc_state(state, crtc);
1287 	struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1288 	const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1289 	const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
1290 	enum plane_id plane_id;
1291 
1292 	if (!new_crtc_state->hw.active ||
1293 	    intel_crtc_needs_modeset(new_crtc_state)) {
1294 		*intermediate = *optimal;
1295 
1296 		intermediate->cxsr = false;
1297 		intermediate->hpll_en = false;
1298 		goto out;
1299 	}
1300 
1301 	intermediate->cxsr = optimal->cxsr && active->cxsr &&
1302 		!new_crtc_state->disable_cxsr;
1303 	intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1304 		!new_crtc_state->disable_cxsr;
1305 	intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1306 
1307 	for_each_plane_id_on_crtc(crtc, plane_id) {
1308 		intermediate->wm.plane[plane_id] =
1309 			max(optimal->wm.plane[plane_id],
1310 			    active->wm.plane[plane_id]);
1311 
1312 		drm_WARN_ON(display->drm, intermediate->wm.plane[plane_id] >
1313 			    g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1314 	}
1315 
1316 	intermediate->sr.plane = max(optimal->sr.plane,
1317 				     active->sr.plane);
1318 	intermediate->sr.cursor = max(optimal->sr.cursor,
1319 				      active->sr.cursor);
1320 	intermediate->sr.fbc = max(optimal->sr.fbc,
1321 				   active->sr.fbc);
1322 
1323 	intermediate->hpll.plane = max(optimal->hpll.plane,
1324 				       active->hpll.plane);
1325 	intermediate->hpll.cursor = max(optimal->hpll.cursor,
1326 					active->hpll.cursor);
1327 	intermediate->hpll.fbc = max(optimal->hpll.fbc,
1328 				     active->hpll.fbc);
1329 
1330 	drm_WARN_ON(display->drm,
1331 		    (intermediate->sr.plane >
1332 		     g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1333 		     intermediate->sr.cursor >
1334 		     g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1335 		    intermediate->cxsr);
1336 	drm_WARN_ON(display->drm,
1337 		    (intermediate->sr.plane >
1338 		     g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1339 		     intermediate->sr.cursor >
1340 		     g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1341 		    intermediate->hpll_en);
1342 
1343 	drm_WARN_ON(display->drm,
1344 		    intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1345 		    intermediate->fbc_en && intermediate->cxsr);
1346 	drm_WARN_ON(display->drm,
1347 		    intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1348 		    intermediate->fbc_en && intermediate->hpll_en);
1349 
1350 out:
1351 	/*
1352 	 * If our intermediate WM are identical to the final WM, then we can
1353 	 * omit the post-vblank programming; only update if it's different.
1354 	 */
1355 	if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1356 		new_crtc_state->wm.need_postvbl_update = true;
1357 
1358 	return 0;
1359 }
1360 
1361 static int g4x_compute_watermarks(struct intel_atomic_state *state,
1362 				  struct intel_crtc *crtc)
1363 {
1364 	int ret;
1365 
1366 	ret = g4x_compute_pipe_wm(state, crtc);
1367 	if (ret)
1368 		return ret;
1369 
1370 	ret = g4x_compute_intermediate_wm(state, crtc);
1371 	if (ret)
1372 		return ret;
1373 
1374 	return 0;
1375 }
1376 
1377 static void g4x_merge_wm(struct intel_display *display,
1378 			 struct g4x_wm_values *wm)
1379 {
1380 	struct intel_crtc *crtc;
1381 	int num_active_pipes = 0;
1382 
1383 	wm->cxsr = true;
1384 	wm->hpll_en = true;
1385 	wm->fbc_en = true;
1386 
1387 	for_each_intel_crtc(display->drm, crtc) {
1388 		const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1389 
1390 		if (!crtc->active)
1391 			continue;
1392 
1393 		if (!wm_state->cxsr)
1394 			wm->cxsr = false;
1395 		if (!wm_state->hpll_en)
1396 			wm->hpll_en = false;
1397 		if (!wm_state->fbc_en)
1398 			wm->fbc_en = false;
1399 
1400 		num_active_pipes++;
1401 	}
1402 
1403 	if (num_active_pipes != 1) {
1404 		wm->cxsr = false;
1405 		wm->hpll_en = false;
1406 		wm->fbc_en = false;
1407 	}
1408 
1409 	for_each_intel_crtc(display->drm, crtc) {
1410 		const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1411 		enum pipe pipe = crtc->pipe;
1412 
1413 		wm->pipe[pipe] = wm_state->wm;
1414 		if (crtc->active && wm->cxsr)
1415 			wm->sr = wm_state->sr;
1416 		if (crtc->active && wm->hpll_en)
1417 			wm->hpll = wm_state->hpll;
1418 	}
1419 }
1420 
1421 static void g4x_program_watermarks(struct intel_display *display)
1422 {
1423 	struct g4x_wm_values *old_wm = &display->wm.g4x;
1424 	struct g4x_wm_values new_wm = {};
1425 
1426 	g4x_merge_wm(display, &new_wm);
1427 
1428 	if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1429 		return;
1430 
1431 	if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1432 		_intel_set_memory_cxsr(display, false);
1433 
1434 	g4x_write_wm_values(display, &new_wm);
1435 
1436 	if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1437 		_intel_set_memory_cxsr(display, true);
1438 
1439 	*old_wm = new_wm;
1440 }
1441 
1442 static void g4x_initial_watermarks(struct intel_atomic_state *state,
1443 				   struct intel_crtc *crtc)
1444 {
1445 	struct intel_display *display = to_intel_display(crtc);
1446 	const struct intel_crtc_state *crtc_state =
1447 		intel_atomic_get_new_crtc_state(state, crtc);
1448 
1449 	mutex_lock(&display->wm.wm_mutex);
1450 	crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1451 	g4x_program_watermarks(display);
1452 	mutex_unlock(&display->wm.wm_mutex);
1453 }
1454 
1455 static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1456 				    struct intel_crtc *crtc)
1457 {
1458 	struct intel_display *display = to_intel_display(crtc);
1459 	const struct intel_crtc_state *crtc_state =
1460 		intel_atomic_get_new_crtc_state(state, crtc);
1461 
1462 	if (!crtc_state->wm.need_postvbl_update)
1463 		return;
1464 
1465 	mutex_lock(&display->wm.wm_mutex);
1466 	crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1467 	g4x_program_watermarks(display);
1468 	mutex_unlock(&display->wm.wm_mutex);
1469 }
1470 
1471 /* latency must be in 0.1us units. */
1472 static unsigned int vlv_wm_method2(unsigned int pixel_rate,
1473 				   unsigned int htotal,
1474 				   unsigned int width,
1475 				   unsigned int cpp,
1476 				   unsigned int latency)
1477 {
1478 	unsigned int ret;
1479 
1480 	ret = intel_wm_method2(pixel_rate, htotal,
1481 			       width, cpp, latency);
1482 	ret = DIV_ROUND_UP(ret, 64);
1483 
1484 	return ret;
1485 }
1486 
1487 static void vlv_setup_wm_latency(struct intel_display *display)
1488 {
1489 	/* all latencies in usec */
1490 	display->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1491 
1492 	display->wm.num_levels = VLV_WM_LEVEL_PM2 + 1;
1493 
1494 	if (display->platform.cherryview) {
1495 		display->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1496 		display->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
1497 
1498 		display->wm.num_levels = VLV_WM_LEVEL_DDR_DVFS + 1;
1499 	}
1500 }
1501 
1502 static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1503 				const struct intel_plane_state *plane_state,
1504 				int level)
1505 {
1506 	struct intel_display *display = to_intel_display(plane_state);
1507 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1508 	const struct drm_display_mode *pipe_mode =
1509 		&crtc_state->hw.pipe_mode;
1510 	unsigned int pixel_rate, htotal, cpp, width, wm;
1511 
1512 	if (display->wm.pri_latency[level] == 0)
1513 		return USHRT_MAX;
1514 
1515 	if (!intel_wm_plane_visible(crtc_state, plane_state))
1516 		return 0;
1517 
1518 	cpp = plane_state->hw.fb->format->cpp[0];
1519 	pixel_rate = crtc_state->pixel_rate;
1520 	htotal = pipe_mode->crtc_htotal;
1521 	width = drm_rect_width(&plane_state->uapi.src) >> 16;
1522 
1523 	if (plane->id == PLANE_CURSOR) {
1524 		/*
1525 		 * FIXME the formula gives values that are
1526 		 * too big for the cursor FIFO, and hence we
1527 		 * would never be able to use cursors. For
1528 		 * now just hardcode the watermark.
1529 		 */
1530 		wm = 63;
1531 	} else {
1532 		wm = vlv_wm_method2(pixel_rate, htotal, width, cpp,
1533 				    display->wm.pri_latency[level] * 10);
1534 	}
1535 
1536 	return min_t(unsigned int, wm, USHRT_MAX);
1537 }
1538 
1539 static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1540 {
1541 	return (active_planes & (BIT(PLANE_SPRITE0) |
1542 				 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1543 }
1544 
1545 static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
1546 {
1547 	struct intel_display *display = to_intel_display(crtc_state);
1548 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1549 	const struct g4x_pipe_wm *raw =
1550 		&crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
1551 	struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
1552 	u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1553 	int num_active_planes = hweight8(active_planes);
1554 	const int fifo_size = 511;
1555 	int fifo_extra, fifo_left = fifo_size;
1556 	int sprite0_fifo_extra = 0;
1557 	unsigned int total_rate;
1558 	enum plane_id plane_id;
1559 
1560 	/*
1561 	 * When enabling sprite0 after sprite1 has already been enabled
1562 	 * we tend to get an underrun unless sprite0 already has some
1563 	 * FIFO space allocated. Hence we always allocate at least one
1564 	 * cacheline for sprite0 whenever sprite1 is enabled.
1565 	 *
1566 	 * All other plane enable sequences appear immune to this problem.
1567 	 */
1568 	if (vlv_need_sprite0_fifo_workaround(active_planes))
1569 		sprite0_fifo_extra = 1;
1570 
1571 	total_rate = raw->plane[PLANE_PRIMARY] +
1572 		raw->plane[PLANE_SPRITE0] +
1573 		raw->plane[PLANE_SPRITE1] +
1574 		sprite0_fifo_extra;
1575 
1576 	if (total_rate > fifo_size)
1577 		return -EINVAL;
1578 
1579 	if (total_rate == 0)
1580 		total_rate = 1;
1581 
1582 	for_each_plane_id_on_crtc(crtc, plane_id) {
1583 		unsigned int rate;
1584 
1585 		if ((active_planes & BIT(plane_id)) == 0) {
1586 			fifo_state->plane[plane_id] = 0;
1587 			continue;
1588 		}
1589 
1590 		rate = raw->plane[plane_id];
1591 		fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1592 		fifo_left -= fifo_state->plane[plane_id];
1593 	}
1594 
1595 	fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1596 	fifo_left -= sprite0_fifo_extra;
1597 
1598 	fifo_state->plane[PLANE_CURSOR] = 63;
1599 
1600 	fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
1601 
1602 	/* spread the remainder evenly */
1603 	for_each_plane_id_on_crtc(crtc, plane_id) {
1604 		int plane_extra;
1605 
1606 		if (fifo_left == 0)
1607 			break;
1608 
1609 		if ((active_planes & BIT(plane_id)) == 0)
1610 			continue;
1611 
1612 		plane_extra = min(fifo_extra, fifo_left);
1613 		fifo_state->plane[plane_id] += plane_extra;
1614 		fifo_left -= plane_extra;
1615 	}
1616 
1617 	drm_WARN_ON(display->drm, active_planes != 0 && fifo_left != 0);
1618 
1619 	/* give it all to the first plane if none are active */
1620 	if (active_planes == 0) {
1621 		drm_WARN_ON(display->drm, fifo_left != fifo_size);
1622 		fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1623 	}
1624 
1625 	return 0;
1626 }
1627 
1628 /* mark all levels starting from 'level' as invalid */
1629 static void vlv_invalidate_wms(struct intel_crtc *crtc,
1630 			       struct vlv_wm_state *wm_state, int level)
1631 {
1632 	struct intel_display *display = to_intel_display(crtc);
1633 
1634 	for (; level < display->wm.num_levels; level++) {
1635 		enum plane_id plane_id;
1636 
1637 		for_each_plane_id_on_crtc(crtc, plane_id)
1638 			wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1639 
1640 		wm_state->sr[level].cursor = USHRT_MAX;
1641 		wm_state->sr[level].plane = USHRT_MAX;
1642 	}
1643 }
1644 
1645 static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1646 {
1647 	if (wm > fifo_size)
1648 		return USHRT_MAX;
1649 	else
1650 		return fifo_size - wm;
1651 }
1652 
1653 /*
1654  * Starting from 'level' set all higher
1655  * levels to 'value' in the "raw" watermarks.
1656  */
1657 static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1658 				 int level, enum plane_id plane_id, u16 value)
1659 {
1660 	struct intel_display *display = to_intel_display(crtc_state);
1661 	bool dirty = false;
1662 
1663 	for (; level < display->wm.num_levels; level++) {
1664 		struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1665 
1666 		dirty |= raw->plane[plane_id] != value;
1667 		raw->plane[plane_id] = value;
1668 	}
1669 
1670 	return dirty;
1671 }
1672 
1673 static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1674 				     const struct intel_plane_state *plane_state)
1675 {
1676 	struct intel_display *display = to_intel_display(crtc_state);
1677 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1678 	enum plane_id plane_id = plane->id;
1679 	int level;
1680 	bool dirty = false;
1681 
1682 	if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1683 		dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1684 		goto out;
1685 	}
1686 
1687 	for (level = 0; level < display->wm.num_levels; level++) {
1688 		struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1689 		int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1690 		int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1691 
1692 		if (wm > max_wm)
1693 			break;
1694 
1695 		dirty |= raw->plane[plane_id] != wm;
1696 		raw->plane[plane_id] = wm;
1697 	}
1698 
1699 	/* mark all higher levels as invalid */
1700 	dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1701 
1702 out:
1703 	if (dirty)
1704 		drm_dbg_kms(display->drm,
1705 			    "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1706 			    plane->base.name,
1707 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1708 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1709 			    crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1710 
1711 	return dirty;
1712 }
1713 
1714 static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1715 				      enum plane_id plane_id, int level)
1716 {
1717 	const struct g4x_pipe_wm *raw =
1718 		&crtc_state->wm.vlv.raw[level];
1719 	const struct vlv_fifo_state *fifo_state =
1720 		&crtc_state->wm.vlv.fifo_state;
1721 
1722 	return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1723 }
1724 
1725 static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
1726 {
1727 	return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1728 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1729 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1730 		vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1731 }
1732 
1733 static int _vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1734 {
1735 	struct intel_display *display = to_intel_display(crtc_state);
1736 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1737 	struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
1738 	const struct vlv_fifo_state *fifo_state =
1739 		&crtc_state->wm.vlv.fifo_state;
1740 	u8 active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1741 	int num_active_planes = hweight8(active_planes);
1742 	enum plane_id plane_id;
1743 	int level;
1744 
1745 	/* initially allow all levels */
1746 	wm_state->num_levels = display->wm.num_levels;
1747 	/*
1748 	 * Note that enabling cxsr with no primary/sprite planes
1749 	 * enabled can wedge the pipe. Hence we only allow cxsr
1750 	 * with exactly one enabled primary/sprite plane.
1751 	 */
1752 	wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
1753 
1754 	for (level = 0; level < wm_state->num_levels; level++) {
1755 		const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1756 		const int sr_fifo_size = INTEL_NUM_PIPES(display) * 512 - 1;
1757 
1758 		if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
1759 			break;
1760 
1761 		for_each_plane_id_on_crtc(crtc, plane_id) {
1762 			wm_state->wm[level].plane[plane_id] =
1763 				vlv_invert_wm_value(raw->plane[plane_id],
1764 						    fifo_state->plane[plane_id]);
1765 		}
1766 
1767 		wm_state->sr[level].plane =
1768 			vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
1769 						 raw->plane[PLANE_SPRITE0],
1770 						 raw->plane[PLANE_SPRITE1]),
1771 					    sr_fifo_size);
1772 
1773 		wm_state->sr[level].cursor =
1774 			vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1775 					    63);
1776 	}
1777 
1778 	if (level == 0)
1779 		return -EINVAL;
1780 
1781 	/* limit to only levels we can actually handle */
1782 	wm_state->num_levels = level;
1783 
1784 	/* invalidate the higher levels */
1785 	vlv_invalidate_wms(crtc, wm_state, level);
1786 
1787 	return 0;
1788 }
1789 
1790 static int vlv_compute_pipe_wm(struct intel_atomic_state *state,
1791 			       struct intel_crtc *crtc)
1792 {
1793 	struct intel_crtc_state *crtc_state =
1794 		intel_atomic_get_new_crtc_state(state, crtc);
1795 	const struct intel_plane_state *old_plane_state;
1796 	const struct intel_plane_state *new_plane_state;
1797 	struct intel_plane *plane;
1798 	unsigned int dirty = 0;
1799 	int i;
1800 
1801 	for_each_oldnew_intel_plane_in_state(state, plane,
1802 					     old_plane_state,
1803 					     new_plane_state, i) {
1804 		if (new_plane_state->hw.crtc != &crtc->base &&
1805 		    old_plane_state->hw.crtc != &crtc->base)
1806 			continue;
1807 
1808 		if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
1809 			dirty |= BIT(plane->id);
1810 	}
1811 
1812 	/*
1813 	 * DSPARB registers may have been reset due to the
1814 	 * power well being turned off. Make sure we restore
1815 	 * them to a consistent state even if no primary/sprite
1816 	 * planes are initially active. We also force a FIFO
1817 	 * recomputation so that we are sure to sanitize the
1818 	 * FIFO setting we took over from the BIOS even if there
1819 	 * are no active planes on the crtc.
1820 	 */
1821 	if (intel_crtc_needs_modeset(crtc_state))
1822 		dirty = ~0;
1823 
1824 	if (!dirty)
1825 		return 0;
1826 
1827 	/* cursor changes don't warrant a FIFO recompute */
1828 	if (dirty & ~BIT(PLANE_CURSOR)) {
1829 		const struct intel_crtc_state *old_crtc_state =
1830 			intel_atomic_get_old_crtc_state(state, crtc);
1831 		const struct vlv_fifo_state *old_fifo_state =
1832 			&old_crtc_state->wm.vlv.fifo_state;
1833 		const struct vlv_fifo_state *new_fifo_state =
1834 			&crtc_state->wm.vlv.fifo_state;
1835 		int ret;
1836 
1837 		ret = vlv_compute_fifo(crtc_state);
1838 		if (ret)
1839 			return ret;
1840 
1841 		if (intel_crtc_needs_modeset(crtc_state) ||
1842 		    memcmp(old_fifo_state, new_fifo_state,
1843 			   sizeof(*new_fifo_state)) != 0)
1844 			crtc_state->fifo_changed = true;
1845 	}
1846 
1847 	return _vlv_compute_pipe_wm(crtc_state);
1848 }
1849 
1850 #define VLV_FIFO(plane, value) \
1851 	(((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1852 
1853 static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
1854 				   struct intel_crtc *crtc)
1855 {
1856 	struct intel_display *display = to_intel_display(crtc);
1857 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1858 	struct intel_uncore *uncore = &dev_priv->uncore;
1859 	const struct intel_crtc_state *crtc_state =
1860 		intel_atomic_get_new_crtc_state(state, crtc);
1861 	const struct vlv_fifo_state *fifo_state =
1862 		&crtc_state->wm.vlv.fifo_state;
1863 	int sprite0_start, sprite1_start, fifo_size;
1864 	u32 dsparb, dsparb2, dsparb3;
1865 
1866 	if (!crtc_state->fifo_changed)
1867 		return;
1868 
1869 	sprite0_start = fifo_state->plane[PLANE_PRIMARY];
1870 	sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
1871 	fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
1872 
1873 	drm_WARN_ON(display->drm, fifo_state->plane[PLANE_CURSOR] != 63);
1874 	drm_WARN_ON(display->drm, fifo_size != 511);
1875 
1876 	trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
1877 
1878 	/*
1879 	 * uncore.lock serves a double purpose here. It allows us to
1880 	 * use the less expensive I915_{READ,WRITE}_FW() functions, and
1881 	 * it protects the DSPARB registers from getting clobbered by
1882 	 * parallel updates from multiple pipes.
1883 	 *
1884 	 * intel_pipe_update_start() has already disabled interrupts
1885 	 * for us, so a plain spin_lock() is sufficient here.
1886 	 */
1887 	spin_lock(&uncore->lock);
1888 
1889 	switch (crtc->pipe) {
1890 	case PIPE_A:
1891 		dsparb = intel_de_read_fw(display, DSPARB(display));
1892 		dsparb2 = intel_de_read_fw(display, DSPARB2);
1893 
1894 		dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1895 			    VLV_FIFO(SPRITEB, 0xff));
1896 		dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1897 			   VLV_FIFO(SPRITEB, sprite1_start));
1898 
1899 		dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1900 			     VLV_FIFO(SPRITEB_HI, 0x1));
1901 		dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1902 			   VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1903 
1904 		intel_de_write_fw(display, DSPARB(display), dsparb);
1905 		intel_de_write_fw(display, DSPARB2, dsparb2);
1906 		break;
1907 	case PIPE_B:
1908 		dsparb = intel_de_read_fw(display, DSPARB(display));
1909 		dsparb2 = intel_de_read_fw(display, DSPARB2);
1910 
1911 		dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1912 			    VLV_FIFO(SPRITED, 0xff));
1913 		dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1914 			   VLV_FIFO(SPRITED, sprite1_start));
1915 
1916 		dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1917 			     VLV_FIFO(SPRITED_HI, 0xff));
1918 		dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1919 			   VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1920 
1921 		intel_de_write_fw(display, DSPARB(display), dsparb);
1922 		intel_de_write_fw(display, DSPARB2, dsparb2);
1923 		break;
1924 	case PIPE_C:
1925 		dsparb3 = intel_de_read_fw(display, DSPARB3);
1926 		dsparb2 = intel_de_read_fw(display, DSPARB2);
1927 
1928 		dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1929 			     VLV_FIFO(SPRITEF, 0xff));
1930 		dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1931 			    VLV_FIFO(SPRITEF, sprite1_start));
1932 
1933 		dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1934 			     VLV_FIFO(SPRITEF_HI, 0xff));
1935 		dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1936 			   VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1937 
1938 		intel_de_write_fw(display, DSPARB3, dsparb3);
1939 		intel_de_write_fw(display, DSPARB2, dsparb2);
1940 		break;
1941 	default:
1942 		break;
1943 	}
1944 
1945 	intel_de_read_fw(display, DSPARB(display));
1946 
1947 	spin_unlock(&uncore->lock);
1948 }
1949 
1950 #undef VLV_FIFO
1951 
1952 static int vlv_compute_intermediate_wm(struct intel_atomic_state *state,
1953 				       struct intel_crtc *crtc)
1954 {
1955 	struct intel_crtc_state *new_crtc_state =
1956 		intel_atomic_get_new_crtc_state(state, crtc);
1957 	const struct intel_crtc_state *old_crtc_state =
1958 		intel_atomic_get_old_crtc_state(state, crtc);
1959 	struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
1960 	const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
1961 	const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
1962 	int level;
1963 
1964 	if (!new_crtc_state->hw.active ||
1965 	    intel_crtc_needs_modeset(new_crtc_state)) {
1966 		*intermediate = *optimal;
1967 
1968 		intermediate->cxsr = false;
1969 		goto out;
1970 	}
1971 
1972 	intermediate->num_levels = min(optimal->num_levels, active->num_levels);
1973 	intermediate->cxsr = optimal->cxsr && active->cxsr &&
1974 		!new_crtc_state->disable_cxsr;
1975 
1976 	for (level = 0; level < intermediate->num_levels; level++) {
1977 		enum plane_id plane_id;
1978 
1979 		for_each_plane_id_on_crtc(crtc, plane_id) {
1980 			intermediate->wm[level].plane[plane_id] =
1981 				min(optimal->wm[level].plane[plane_id],
1982 				    active->wm[level].plane[plane_id]);
1983 		}
1984 
1985 		intermediate->sr[level].plane = min(optimal->sr[level].plane,
1986 						    active->sr[level].plane);
1987 		intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
1988 						     active->sr[level].cursor);
1989 	}
1990 
1991 	vlv_invalidate_wms(crtc, intermediate, level);
1992 
1993 out:
1994 	/*
1995 	 * If our intermediate WM are identical to the final WM, then we can
1996 	 * omit the post-vblank programming; only update if it's different.
1997 	 */
1998 	if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1999 		new_crtc_state->wm.need_postvbl_update = true;
2000 
2001 	return 0;
2002 }
2003 
2004 static int vlv_compute_watermarks(struct intel_atomic_state *state,
2005 				  struct intel_crtc *crtc)
2006 {
2007 	int ret;
2008 
2009 	ret = vlv_compute_pipe_wm(state, crtc);
2010 	if (ret)
2011 		return ret;
2012 
2013 	ret = vlv_compute_intermediate_wm(state, crtc);
2014 	if (ret)
2015 		return ret;
2016 
2017 	return 0;
2018 }
2019 
2020 static void vlv_merge_wm(struct intel_display *display,
2021 			 struct vlv_wm_values *wm)
2022 {
2023 	struct intel_crtc *crtc;
2024 	int num_active_pipes = 0;
2025 
2026 	wm->level = display->wm.num_levels - 1;
2027 	wm->cxsr = true;
2028 
2029 	for_each_intel_crtc(display->drm, crtc) {
2030 		const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
2031 
2032 		if (!crtc->active)
2033 			continue;
2034 
2035 		if (!wm_state->cxsr)
2036 			wm->cxsr = false;
2037 
2038 		num_active_pipes++;
2039 		wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2040 	}
2041 
2042 	if (num_active_pipes != 1)
2043 		wm->cxsr = false;
2044 
2045 	if (num_active_pipes > 1)
2046 		wm->level = VLV_WM_LEVEL_PM2;
2047 
2048 	for_each_intel_crtc(display->drm, crtc) {
2049 		const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
2050 		enum pipe pipe = crtc->pipe;
2051 
2052 		wm->pipe[pipe] = wm_state->wm[wm->level];
2053 		if (crtc->active && wm->cxsr)
2054 			wm->sr = wm_state->sr[wm->level];
2055 
2056 		wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2057 		wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2058 		wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2059 		wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
2060 	}
2061 }
2062 
2063 static void vlv_program_watermarks(struct intel_display *display)
2064 {
2065 	struct vlv_wm_values *old_wm = &display->wm.vlv;
2066 	struct vlv_wm_values new_wm = {};
2067 
2068 	vlv_merge_wm(display, &new_wm);
2069 
2070 	if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
2071 		return;
2072 
2073 	if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
2074 		chv_set_memory_dvfs(display, false);
2075 
2076 	if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
2077 		chv_set_memory_pm5(display, false);
2078 
2079 	if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
2080 		_intel_set_memory_cxsr(display, false);
2081 
2082 	vlv_write_wm_values(display, &new_wm);
2083 
2084 	if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
2085 		_intel_set_memory_cxsr(display, true);
2086 
2087 	if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
2088 		chv_set_memory_pm5(display, true);
2089 
2090 	if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
2091 		chv_set_memory_dvfs(display, true);
2092 
2093 	*old_wm = new_wm;
2094 }
2095 
2096 static void vlv_initial_watermarks(struct intel_atomic_state *state,
2097 				   struct intel_crtc *crtc)
2098 {
2099 	struct intel_display *display = to_intel_display(crtc);
2100 	const struct intel_crtc_state *crtc_state =
2101 		intel_atomic_get_new_crtc_state(state, crtc);
2102 
2103 	mutex_lock(&display->wm.wm_mutex);
2104 	crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2105 	vlv_program_watermarks(display);
2106 	mutex_unlock(&display->wm.wm_mutex);
2107 }
2108 
2109 static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2110 				    struct intel_crtc *crtc)
2111 {
2112 	struct intel_display *display = to_intel_display(crtc);
2113 	const struct intel_crtc_state *crtc_state =
2114 		intel_atomic_get_new_crtc_state(state, crtc);
2115 
2116 	if (!crtc_state->wm.need_postvbl_update)
2117 		return;
2118 
2119 	mutex_lock(&display->wm.wm_mutex);
2120 	crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
2121 	vlv_program_watermarks(display);
2122 	mutex_unlock(&display->wm.wm_mutex);
2123 }
2124 
2125 static void i965_update_wm(struct intel_display *display)
2126 {
2127 	struct intel_crtc *crtc;
2128 	int srwm = 1;
2129 	int cursor_sr = 16;
2130 	bool cxsr_enabled;
2131 
2132 	/* Calc sr entries for one plane configs */
2133 	crtc = single_enabled_crtc(display);
2134 	if (crtc) {
2135 		/* self-refresh has much higher latency */
2136 		static const int sr_latency_ns = 12000;
2137 		const struct drm_display_mode *pipe_mode =
2138 			&crtc->config->hw.pipe_mode;
2139 		const struct drm_framebuffer *fb =
2140 			crtc->base.primary->state->fb;
2141 		int pixel_rate = crtc->config->pixel_rate;
2142 		int htotal = pipe_mode->crtc_htotal;
2143 		int width = drm_rect_width(&crtc->base.primary->state->src) >> 16;
2144 		int cpp = fb->format->cpp[0];
2145 		int entries;
2146 
2147 		entries = intel_wm_method2(pixel_rate, htotal,
2148 					   width, cpp, sr_latency_ns / 100);
2149 		entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2150 		srwm = I965_FIFO_SIZE - entries;
2151 		if (srwm < 0)
2152 			srwm = 1;
2153 		srwm &= 0x1ff;
2154 		drm_dbg_kms(display->drm,
2155 			    "self-refresh entries: %d, wm: %d\n",
2156 			    entries, srwm);
2157 
2158 		entries = intel_wm_method2(pixel_rate, htotal,
2159 					   crtc->base.cursor->state->crtc_w, 4,
2160 					   sr_latency_ns / 100);
2161 		entries = DIV_ROUND_UP(entries,
2162 				       i965_cursor_wm_info.cacheline_size) +
2163 			i965_cursor_wm_info.guard_size;
2164 
2165 		cursor_sr = i965_cursor_wm_info.fifo_size - entries;
2166 		if (cursor_sr > i965_cursor_wm_info.max_wm)
2167 			cursor_sr = i965_cursor_wm_info.max_wm;
2168 
2169 		drm_dbg_kms(display->drm,
2170 			    "self-refresh watermark: display plane %d "
2171 			    "cursor %d\n", srwm, cursor_sr);
2172 
2173 		cxsr_enabled = true;
2174 	} else {
2175 		cxsr_enabled = false;
2176 		/* Turn off self refresh if both pipes are enabled */
2177 		intel_set_memory_cxsr(display, false);
2178 	}
2179 
2180 	drm_dbg_kms(display->drm,
2181 		    "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2182 		    srwm);
2183 
2184 	/* 965 has limitations... */
2185 	intel_de_write(display, DSPFW1(display),
2186 		       FW_WM(srwm, SR) |
2187 		       FW_WM(8, CURSORB) |
2188 		       FW_WM(8, PLANEB) |
2189 		       FW_WM(8, PLANEA));
2190 	intel_de_write(display, DSPFW2(display),
2191 		       FW_WM(8, CURSORA) |
2192 		       FW_WM(8, PLANEC_OLD));
2193 	/* update cursor SR watermark */
2194 	intel_de_write(display, DSPFW3(display),
2195 		       FW_WM(cursor_sr, CURSOR_SR));
2196 
2197 	if (cxsr_enabled)
2198 		intel_set_memory_cxsr(display, true);
2199 }
2200 
2201 #undef FW_WM
2202 
2203 static struct intel_crtc *intel_crtc_for_plane(struct intel_display *display,
2204 					       enum i9xx_plane_id i9xx_plane)
2205 {
2206 	struct intel_plane *plane;
2207 
2208 	for_each_intel_plane(display->drm, plane) {
2209 		if (plane->id == PLANE_PRIMARY &&
2210 		    plane->i9xx_plane == i9xx_plane)
2211 			return intel_crtc_for_pipe(display, plane->pipe);
2212 	}
2213 
2214 	return NULL;
2215 }
2216 
2217 static void i9xx_update_wm(struct intel_display *display)
2218 {
2219 	const struct intel_watermark_params *wm_info;
2220 	u32 fwater_lo;
2221 	u32 fwater_hi;
2222 	int cwm, srwm = 1;
2223 	int fifo_size;
2224 	int planea_wm, planeb_wm;
2225 	struct intel_crtc *crtc;
2226 
2227 	if (display->platform.i945gm)
2228 		wm_info = &i945_wm_info;
2229 	else if (DISPLAY_VER(display) != 2)
2230 		wm_info = &i915_wm_info;
2231 	else
2232 		wm_info = &i830_a_wm_info;
2233 
2234 	if (DISPLAY_VER(display) == 2)
2235 		fifo_size = i830_get_fifo_size(display, PLANE_A);
2236 	else
2237 		fifo_size = i9xx_get_fifo_size(display, PLANE_A);
2238 	crtc = intel_crtc_for_plane(display, PLANE_A);
2239 	if (intel_crtc_active(crtc)) {
2240 		const struct drm_framebuffer *fb =
2241 			crtc->base.primary->state->fb;
2242 		int cpp;
2243 
2244 		if (DISPLAY_VER(display) == 2)
2245 			cpp = 4;
2246 		else
2247 			cpp = fb->format->cpp[0];
2248 
2249 		planea_wm = intel_calculate_wm(display, crtc->config->pixel_rate,
2250 					       wm_info, fifo_size, cpp,
2251 					       pessimal_latency_ns);
2252 	} else {
2253 		planea_wm = fifo_size - wm_info->guard_size;
2254 		if (planea_wm > (long)wm_info->max_wm)
2255 			planea_wm = wm_info->max_wm;
2256 	}
2257 
2258 	if (DISPLAY_VER(display) == 2)
2259 		wm_info = &i830_bc_wm_info;
2260 
2261 	if (DISPLAY_VER(display) == 2)
2262 		fifo_size = i830_get_fifo_size(display, PLANE_B);
2263 	else
2264 		fifo_size = i9xx_get_fifo_size(display, PLANE_B);
2265 	crtc = intel_crtc_for_plane(display, PLANE_B);
2266 	if (intel_crtc_active(crtc)) {
2267 		const struct drm_framebuffer *fb =
2268 			crtc->base.primary->state->fb;
2269 		int cpp;
2270 
2271 		if (DISPLAY_VER(display) == 2)
2272 			cpp = 4;
2273 		else
2274 			cpp = fb->format->cpp[0];
2275 
2276 		planeb_wm = intel_calculate_wm(display, crtc->config->pixel_rate,
2277 					       wm_info, fifo_size, cpp,
2278 					       pessimal_latency_ns);
2279 	} else {
2280 		planeb_wm = fifo_size - wm_info->guard_size;
2281 		if (planeb_wm > (long)wm_info->max_wm)
2282 			planeb_wm = wm_info->max_wm;
2283 	}
2284 
2285 	drm_dbg_kms(display->drm,
2286 		    "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2287 
2288 	crtc = single_enabled_crtc(display);
2289 	if (display->platform.i915gm && crtc) {
2290 		struct drm_gem_object *obj;
2291 
2292 		obj = intel_fb_bo(crtc->base.primary->state->fb);
2293 
2294 		/* self-refresh seems busted with untiled */
2295 		if (!intel_bo_is_tiled(obj))
2296 			crtc = NULL;
2297 	}
2298 
2299 	/*
2300 	 * Overlay gets an aggressive default since video jitter is bad.
2301 	 */
2302 	cwm = 2;
2303 
2304 	/* Play safe and disable self-refresh before adjusting watermarks. */
2305 	intel_set_memory_cxsr(display, false);
2306 
2307 	/* Calc sr entries for one plane configs */
2308 	if (HAS_FW_BLC(display) && crtc) {
2309 		/* self-refresh has much higher latency */
2310 		static const int sr_latency_ns = 6000;
2311 		const struct drm_display_mode *pipe_mode =
2312 			&crtc->config->hw.pipe_mode;
2313 		const struct drm_framebuffer *fb =
2314 			crtc->base.primary->state->fb;
2315 		int pixel_rate = crtc->config->pixel_rate;
2316 		int htotal = pipe_mode->crtc_htotal;
2317 		int width = drm_rect_width(&crtc->base.primary->state->src) >> 16;
2318 		int cpp;
2319 		int entries;
2320 
2321 		if (display->platform.i915gm || display->platform.i945gm)
2322 			cpp = 4;
2323 		else
2324 			cpp = fb->format->cpp[0];
2325 
2326 		entries = intel_wm_method2(pixel_rate, htotal, width, cpp,
2327 					   sr_latency_ns / 100);
2328 		entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2329 		drm_dbg_kms(display->drm,
2330 			    "self-refresh entries: %d\n", entries);
2331 		srwm = wm_info->fifo_size - entries;
2332 		if (srwm < 0)
2333 			srwm = 1;
2334 
2335 		if (display->platform.i945g || display->platform.i945gm)
2336 			intel_de_write(display, FW_BLC_SELF,
2337 				       FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
2338 		else
2339 			intel_de_write(display, FW_BLC_SELF, srwm & 0x3f);
2340 	}
2341 
2342 	drm_dbg_kms(display->drm,
2343 		    "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2344 		     planea_wm, planeb_wm, cwm, srwm);
2345 
2346 	fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2347 	fwater_hi = (cwm & 0x1f);
2348 
2349 	/* Set request length to 8 cachelines per fetch */
2350 	fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2351 	fwater_hi = fwater_hi | (1 << 8);
2352 
2353 	intel_de_write(display, FW_BLC, fwater_lo);
2354 	intel_de_write(display, FW_BLC2, fwater_hi);
2355 
2356 	if (crtc)
2357 		intel_set_memory_cxsr(display, true);
2358 }
2359 
2360 static void i845_update_wm(struct intel_display *display)
2361 {
2362 	struct intel_crtc *crtc;
2363 	u32 fwater_lo;
2364 	int planea_wm;
2365 
2366 	crtc = single_enabled_crtc(display);
2367 	if (crtc == NULL)
2368 		return;
2369 
2370 	planea_wm = intel_calculate_wm(display, crtc->config->pixel_rate,
2371 				       &i845_wm_info,
2372 				       i845_get_fifo_size(display, PLANE_A),
2373 				       4, pessimal_latency_ns);
2374 	fwater_lo = intel_de_read(display, FW_BLC) & ~0xfff;
2375 	fwater_lo |= (3<<8) | planea_wm;
2376 
2377 	drm_dbg_kms(display->drm,
2378 		    "Setting FIFO watermarks - A: %d\n", planea_wm);
2379 
2380 	intel_de_write(display, FW_BLC, fwater_lo);
2381 }
2382 
2383 /* latency must be in 0.1us units. */
2384 static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2385 				   unsigned int cpp,
2386 				   unsigned int latency)
2387 {
2388 	unsigned int ret;
2389 
2390 	ret = intel_wm_method1(pixel_rate, cpp, latency);
2391 	ret = DIV_ROUND_UP(ret, 64) + 2;
2392 
2393 	return ret;
2394 }
2395 
2396 /* latency must be in 0.1us units. */
2397 static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2398 				   unsigned int htotal,
2399 				   unsigned int width,
2400 				   unsigned int cpp,
2401 				   unsigned int latency)
2402 {
2403 	unsigned int ret;
2404 
2405 	ret = intel_wm_method2(pixel_rate, htotal,
2406 			       width, cpp, latency);
2407 	ret = DIV_ROUND_UP(ret, 64) + 2;
2408 
2409 	return ret;
2410 }
2411 
2412 static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
2413 {
2414 	/*
2415 	 * Neither of these should be possible since this function shouldn't be
2416 	 * called if the CRTC is off or the plane is invisible.  But let's be
2417 	 * extra paranoid to avoid a potential divide-by-zero if we screw up
2418 	 * elsewhere in the driver.
2419 	 */
2420 	if (WARN_ON(!cpp))
2421 		return 0;
2422 	if (WARN_ON(!horiz_pixels))
2423 		return 0;
2424 
2425 	return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
2426 }
2427 
2428 struct ilk_wm_maximums {
2429 	u16 pri;
2430 	u16 spr;
2431 	u16 cur;
2432 	u16 fbc;
2433 };
2434 
2435 /*
2436  * For both WM_PIPE and WM_LP.
2437  * mem_value must be in 0.1us units.
2438  */
2439 static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2440 			      const struct intel_plane_state *plane_state,
2441 			      u32 mem_value, bool is_lp)
2442 {
2443 	u32 method1, method2;
2444 	int cpp;
2445 
2446 	if (mem_value == 0)
2447 		return U32_MAX;
2448 
2449 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2450 		return 0;
2451 
2452 	cpp = plane_state->hw.fb->format->cpp[0];
2453 
2454 	method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2455 
2456 	if (!is_lp)
2457 		return method1;
2458 
2459 	method2 = ilk_wm_method2(crtc_state->pixel_rate,
2460 				 crtc_state->hw.pipe_mode.crtc_htotal,
2461 				 drm_rect_width(&plane_state->uapi.src) >> 16,
2462 				 cpp, mem_value);
2463 
2464 	return min(method1, method2);
2465 }
2466 
2467 /*
2468  * For both WM_PIPE and WM_LP.
2469  * mem_value must be in 0.1us units.
2470  */
2471 static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2472 			      const struct intel_plane_state *plane_state,
2473 			      u32 mem_value)
2474 {
2475 	u32 method1, method2;
2476 	int cpp;
2477 
2478 	if (mem_value == 0)
2479 		return U32_MAX;
2480 
2481 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2482 		return 0;
2483 
2484 	cpp = plane_state->hw.fb->format->cpp[0];
2485 
2486 	method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2487 	method2 = ilk_wm_method2(crtc_state->pixel_rate,
2488 				 crtc_state->hw.pipe_mode.crtc_htotal,
2489 				 drm_rect_width(&plane_state->uapi.src) >> 16,
2490 				 cpp, mem_value);
2491 	return min(method1, method2);
2492 }
2493 
2494 /*
2495  * For both WM_PIPE and WM_LP.
2496  * mem_value must be in 0.1us units.
2497  */
2498 static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2499 			      const struct intel_plane_state *plane_state,
2500 			      u32 mem_value)
2501 {
2502 	int cpp;
2503 
2504 	if (mem_value == 0)
2505 		return U32_MAX;
2506 
2507 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2508 		return 0;
2509 
2510 	cpp = plane_state->hw.fb->format->cpp[0];
2511 
2512 	return ilk_wm_method2(crtc_state->pixel_rate,
2513 			      crtc_state->hw.pipe_mode.crtc_htotal,
2514 			      drm_rect_width(&plane_state->uapi.src) >> 16,
2515 			      cpp, mem_value);
2516 }
2517 
2518 /* Only for WM_LP. */
2519 static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2520 			      const struct intel_plane_state *plane_state,
2521 			      u32 pri_val)
2522 {
2523 	int cpp;
2524 
2525 	if (!intel_wm_plane_visible(crtc_state, plane_state))
2526 		return 0;
2527 
2528 	cpp = plane_state->hw.fb->format->cpp[0];
2529 
2530 	return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.src) >> 16,
2531 			  cpp);
2532 }
2533 
2534 static unsigned int
2535 ilk_display_fifo_size(struct intel_display *display)
2536 {
2537 	if (DISPLAY_VER(display) >= 8)
2538 		return 3072;
2539 	else if (DISPLAY_VER(display) >= 7)
2540 		return 768;
2541 	else
2542 		return 512;
2543 }
2544 
2545 static unsigned int
2546 ilk_plane_wm_reg_max(struct intel_display *display,
2547 		     int level, bool is_sprite)
2548 {
2549 	if (DISPLAY_VER(display) >= 8)
2550 		/* BDW primary/sprite plane watermarks */
2551 		return level == 0 ? 255 : 2047;
2552 	else if (DISPLAY_VER(display) >= 7)
2553 		/* IVB/HSW primary/sprite plane watermarks */
2554 		return level == 0 ? 127 : 1023;
2555 	else if (!is_sprite)
2556 		/* ILK/SNB primary plane watermarks */
2557 		return level == 0 ? 127 : 511;
2558 	else
2559 		/* ILK/SNB sprite plane watermarks */
2560 		return level == 0 ? 63 : 255;
2561 }
2562 
2563 static unsigned int
2564 ilk_cursor_wm_reg_max(struct intel_display *display, int level)
2565 {
2566 	if (DISPLAY_VER(display) >= 7)
2567 		return level == 0 ? 63 : 255;
2568 	else
2569 		return level == 0 ? 31 : 63;
2570 }
2571 
2572 static unsigned int ilk_fbc_wm_reg_max(struct intel_display *display)
2573 {
2574 	if (DISPLAY_VER(display) >= 8)
2575 		return 31;
2576 	else
2577 		return 15;
2578 }
2579 
2580 /* Calculate the maximum primary/sprite plane watermark */
2581 static unsigned int ilk_plane_wm_max(struct intel_display *display,
2582 				     int level,
2583 				     const struct intel_wm_config *config,
2584 				     enum intel_ddb_partitioning ddb_partitioning,
2585 				     bool is_sprite)
2586 {
2587 	unsigned int fifo_size = ilk_display_fifo_size(display);
2588 
2589 	/* if sprites aren't enabled, sprites get nothing */
2590 	if (is_sprite && !config->sprites_enabled)
2591 		return 0;
2592 
2593 	/* HSW allows LP1+ watermarks even with multiple pipes */
2594 	if (level == 0 || config->num_pipes_active > 1) {
2595 		fifo_size /= INTEL_NUM_PIPES(display);
2596 
2597 		/*
2598 		 * For some reason the non self refresh
2599 		 * FIFO size is only half of the self
2600 		 * refresh FIFO size on ILK/SNB.
2601 		 */
2602 		if (DISPLAY_VER(display) < 7)
2603 			fifo_size /= 2;
2604 	}
2605 
2606 	if (config->sprites_enabled) {
2607 		/* level 0 is always calculated with 1:1 split */
2608 		if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2609 			if (is_sprite)
2610 				fifo_size *= 5;
2611 			fifo_size /= 6;
2612 		} else {
2613 			fifo_size /= 2;
2614 		}
2615 	}
2616 
2617 	/* clamp to max that the registers can hold */
2618 	return min(fifo_size, ilk_plane_wm_reg_max(display, level, is_sprite));
2619 }
2620 
2621 /* Calculate the maximum cursor plane watermark */
2622 static unsigned int ilk_cursor_wm_max(struct intel_display *display,
2623 				      int level,
2624 				      const struct intel_wm_config *config)
2625 {
2626 	/* HSW LP1+ watermarks w/ multiple pipes */
2627 	if (level > 0 && config->num_pipes_active > 1)
2628 		return 64;
2629 
2630 	/* otherwise just report max that registers can hold */
2631 	return ilk_cursor_wm_reg_max(display, level);
2632 }
2633 
2634 static void ilk_compute_wm_maximums(struct intel_display *display,
2635 				    int level,
2636 				    const struct intel_wm_config *config,
2637 				    enum intel_ddb_partitioning ddb_partitioning,
2638 				    struct ilk_wm_maximums *max)
2639 {
2640 	max->pri = ilk_plane_wm_max(display, level, config, ddb_partitioning, false);
2641 	max->spr = ilk_plane_wm_max(display, level, config, ddb_partitioning, true);
2642 	max->cur = ilk_cursor_wm_max(display, level, config);
2643 	max->fbc = ilk_fbc_wm_reg_max(display);
2644 }
2645 
2646 static void ilk_compute_wm_reg_maximums(struct intel_display *display,
2647 					int level,
2648 					struct ilk_wm_maximums *max)
2649 {
2650 	max->pri = ilk_plane_wm_reg_max(display, level, false);
2651 	max->spr = ilk_plane_wm_reg_max(display, level, true);
2652 	max->cur = ilk_cursor_wm_reg_max(display, level);
2653 	max->fbc = ilk_fbc_wm_reg_max(display);
2654 }
2655 
2656 static bool ilk_validate_wm_level(struct intel_display *display,
2657 				  int level,
2658 				  const struct ilk_wm_maximums *max,
2659 				  struct intel_wm_level *result)
2660 {
2661 	bool ret;
2662 
2663 	/* already determined to be invalid? */
2664 	if (!result->enable)
2665 		return false;
2666 
2667 	result->enable = result->pri_val <= max->pri &&
2668 			 result->spr_val <= max->spr &&
2669 			 result->cur_val <= max->cur;
2670 
2671 	ret = result->enable;
2672 
2673 	/*
2674 	 * HACK until we can pre-compute everything,
2675 	 * and thus fail gracefully if LP0 watermarks
2676 	 * are exceeded...
2677 	 */
2678 	if (level == 0 && !result->enable) {
2679 		if (result->pri_val > max->pri)
2680 			drm_dbg_kms(display->drm,
2681 				    "Primary WM%d too large %u (max %u)\n",
2682 				    level, result->pri_val, max->pri);
2683 		if (result->spr_val > max->spr)
2684 			drm_dbg_kms(display->drm,
2685 				    "Sprite WM%d too large %u (max %u)\n",
2686 				    level, result->spr_val, max->spr);
2687 		if (result->cur_val > max->cur)
2688 			drm_dbg_kms(display->drm,
2689 				    "Cursor WM%d too large %u (max %u)\n",
2690 				    level, result->cur_val, max->cur);
2691 
2692 		result->pri_val = min_t(u32, result->pri_val, max->pri);
2693 		result->spr_val = min_t(u32, result->spr_val, max->spr);
2694 		result->cur_val = min_t(u32, result->cur_val, max->cur);
2695 		result->enable = true;
2696 	}
2697 
2698 	return ret;
2699 }
2700 
2701 static void ilk_compute_wm_level(struct intel_display *display,
2702 				 const struct intel_crtc *crtc,
2703 				 int level,
2704 				 struct intel_crtc_state *crtc_state,
2705 				 const struct intel_plane_state *pristate,
2706 				 const struct intel_plane_state *sprstate,
2707 				 const struct intel_plane_state *curstate,
2708 				 struct intel_wm_level *result)
2709 {
2710 	u16 pri_latency = display->wm.pri_latency[level];
2711 	u16 spr_latency = display->wm.spr_latency[level];
2712 	u16 cur_latency = display->wm.cur_latency[level];
2713 
2714 	/* WM1+ latency values stored in 0.5us units */
2715 	if (level > 0) {
2716 		pri_latency *= 5;
2717 		spr_latency *= 5;
2718 		cur_latency *= 5;
2719 	}
2720 
2721 	if (pristate) {
2722 		result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
2723 						     pri_latency, level);
2724 		result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
2725 	}
2726 
2727 	if (sprstate)
2728 		result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
2729 
2730 	if (curstate)
2731 		result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
2732 
2733 	result->enable = true;
2734 }
2735 
2736 static void hsw_read_wm_latency(struct intel_display *display, u16 wm[])
2737 {
2738 	struct drm_i915_private *i915 = to_i915(display->drm);
2739 	u64 sskpd;
2740 
2741 	display->wm.num_levels = 5;
2742 
2743 	sskpd = intel_uncore_read64(&i915->uncore, MCH_SSKPD);
2744 
2745 	wm[0] = REG_FIELD_GET64(SSKPD_NEW_WM0_MASK_HSW, sskpd);
2746 	if (wm[0] == 0)
2747 		wm[0] = REG_FIELD_GET64(SSKPD_OLD_WM0_MASK_HSW, sskpd);
2748 	wm[1] = REG_FIELD_GET64(SSKPD_WM1_MASK_HSW, sskpd);
2749 	wm[2] = REG_FIELD_GET64(SSKPD_WM2_MASK_HSW, sskpd);
2750 	wm[3] = REG_FIELD_GET64(SSKPD_WM3_MASK_HSW, sskpd);
2751 	wm[4] = REG_FIELD_GET64(SSKPD_WM4_MASK_HSW, sskpd);
2752 }
2753 
2754 static void snb_read_wm_latency(struct intel_display *display, u16 wm[])
2755 {
2756 	struct drm_i915_private *i915 = to_i915(display->drm);
2757 	u32 sskpd;
2758 
2759 	display->wm.num_levels = 4;
2760 
2761 	sskpd = intel_uncore_read(&i915->uncore, MCH_SSKPD);
2762 
2763 	wm[0] = REG_FIELD_GET(SSKPD_WM0_MASK_SNB, sskpd);
2764 	wm[1] = REG_FIELD_GET(SSKPD_WM1_MASK_SNB, sskpd);
2765 	wm[2] = REG_FIELD_GET(SSKPD_WM2_MASK_SNB, sskpd);
2766 	wm[3] = REG_FIELD_GET(SSKPD_WM3_MASK_SNB, sskpd);
2767 }
2768 
2769 static void ilk_read_wm_latency(struct intel_display *display, u16 wm[])
2770 {
2771 	struct drm_i915_private *i915 = to_i915(display->drm);
2772 	u32 mltr;
2773 
2774 	display->wm.num_levels = 3;
2775 
2776 	mltr = intel_uncore_read(&i915->uncore, MLTR_ILK);
2777 
2778 	/* ILK primary LP0 latency is 700 ns */
2779 	wm[0] = 7;
2780 	wm[1] = REG_FIELD_GET(MLTR_WM1_MASK, mltr);
2781 	wm[2] = REG_FIELD_GET(MLTR_WM2_MASK, mltr);
2782 }
2783 
2784 static void intel_fixup_spr_wm_latency(struct intel_display *display, u16 wm[5])
2785 {
2786 	/* ILK sprite LP0 latency is 1300 ns */
2787 	if (DISPLAY_VER(display) == 5)
2788 		wm[0] = 13;
2789 }
2790 
2791 static void intel_fixup_cur_wm_latency(struct intel_display *display, u16 wm[5])
2792 {
2793 	/* ILK cursor LP0 latency is 1300 ns */
2794 	if (DISPLAY_VER(display) == 5)
2795 		wm[0] = 13;
2796 }
2797 
2798 static bool ilk_increase_wm_latency(struct intel_display *display, u16 wm[5], u16 min)
2799 {
2800 	int level;
2801 
2802 	if (wm[0] >= min)
2803 		return false;
2804 
2805 	wm[0] = max(wm[0], min);
2806 	for (level = 1; level < display->wm.num_levels; level++)
2807 		wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
2808 
2809 	return true;
2810 }
2811 
2812 static void snb_wm_latency_quirk(struct intel_display *display)
2813 {
2814 	bool changed;
2815 
2816 	/*
2817 	 * The BIOS provided WM memory latency values are often
2818 	 * inadequate for high resolution displays. Adjust them.
2819 	 */
2820 	changed = ilk_increase_wm_latency(display, display->wm.pri_latency, 12);
2821 	changed |= ilk_increase_wm_latency(display, display->wm.spr_latency, 12);
2822 	changed |= ilk_increase_wm_latency(display, display->wm.cur_latency, 12);
2823 
2824 	if (!changed)
2825 		return;
2826 
2827 	drm_dbg_kms(display->drm,
2828 		    "WM latency values increased to avoid potential underruns\n");
2829 	intel_print_wm_latency(display, "Primary", display->wm.pri_latency);
2830 	intel_print_wm_latency(display, "Sprite", display->wm.spr_latency);
2831 	intel_print_wm_latency(display, "Cursor", display->wm.cur_latency);
2832 }
2833 
2834 static void snb_wm_lp3_irq_quirk(struct intel_display *display)
2835 {
2836 	/*
2837 	 * On some SNB machines (Thinkpad X220 Tablet at least)
2838 	 * LP3 usage can cause vblank interrupts to be lost.
2839 	 * The DEIIR bit will go high but it looks like the CPU
2840 	 * never gets interrupted.
2841 	 *
2842 	 * It's not clear whether other interrupt source could
2843 	 * be affected or if this is somehow limited to vblank
2844 	 * interrupts only. To play it safe we disable LP3
2845 	 * watermarks entirely.
2846 	 */
2847 	if (display->wm.pri_latency[3] == 0 &&
2848 	    display->wm.spr_latency[3] == 0 &&
2849 	    display->wm.cur_latency[3] == 0)
2850 		return;
2851 
2852 	display->wm.pri_latency[3] = 0;
2853 	display->wm.spr_latency[3] = 0;
2854 	display->wm.cur_latency[3] = 0;
2855 
2856 	drm_dbg_kms(display->drm,
2857 		    "LP3 watermarks disabled due to potential for lost interrupts\n");
2858 	intel_print_wm_latency(display, "Primary", display->wm.pri_latency);
2859 	intel_print_wm_latency(display, "Sprite", display->wm.spr_latency);
2860 	intel_print_wm_latency(display, "Cursor", display->wm.cur_latency);
2861 }
2862 
2863 static void ilk_setup_wm_latency(struct intel_display *display)
2864 {
2865 	if (display->platform.broadwell || display->platform.haswell)
2866 		hsw_read_wm_latency(display, display->wm.pri_latency);
2867 	else if (DISPLAY_VER(display) >= 6)
2868 		snb_read_wm_latency(display, display->wm.pri_latency);
2869 	else
2870 		ilk_read_wm_latency(display, display->wm.pri_latency);
2871 
2872 	memcpy(display->wm.spr_latency, display->wm.pri_latency,
2873 	       sizeof(display->wm.pri_latency));
2874 	memcpy(display->wm.cur_latency, display->wm.pri_latency,
2875 	       sizeof(display->wm.pri_latency));
2876 
2877 	intel_fixup_spr_wm_latency(display, display->wm.spr_latency);
2878 	intel_fixup_cur_wm_latency(display, display->wm.cur_latency);
2879 
2880 	intel_print_wm_latency(display, "Primary", display->wm.pri_latency);
2881 	intel_print_wm_latency(display, "Sprite", display->wm.spr_latency);
2882 	intel_print_wm_latency(display, "Cursor", display->wm.cur_latency);
2883 
2884 	if (DISPLAY_VER(display) == 6) {
2885 		snb_wm_latency_quirk(display);
2886 		snb_wm_lp3_irq_quirk(display);
2887 	}
2888 }
2889 
2890 static bool ilk_validate_pipe_wm(struct intel_display *display,
2891 				 struct intel_pipe_wm *pipe_wm)
2892 {
2893 	/* LP0 watermark maximums depend on this pipe alone */
2894 	const struct intel_wm_config config = {
2895 		.num_pipes_active = 1,
2896 		.sprites_enabled = pipe_wm->sprites_enabled,
2897 		.sprites_scaled = pipe_wm->sprites_scaled,
2898 	};
2899 	struct ilk_wm_maximums max;
2900 
2901 	/* LP0 watermarks always use 1/2 DDB partitioning */
2902 	ilk_compute_wm_maximums(display, 0, &config, INTEL_DDB_PART_1_2, &max);
2903 
2904 	/* At least LP0 must be valid */
2905 	if (!ilk_validate_wm_level(display, 0, &max, &pipe_wm->wm[0])) {
2906 		drm_dbg_kms(display->drm, "LP0 watermark invalid\n");
2907 		return false;
2908 	}
2909 
2910 	return true;
2911 }
2912 
2913 /* Compute new watermarks for the pipe */
2914 static int ilk_compute_pipe_wm(struct intel_atomic_state *state,
2915 			       struct intel_crtc *crtc)
2916 {
2917 	struct intel_display *display = to_intel_display(state);
2918 	struct intel_crtc_state *crtc_state =
2919 		intel_atomic_get_new_crtc_state(state, crtc);
2920 	struct intel_pipe_wm *pipe_wm;
2921 	struct intel_plane *plane;
2922 	const struct intel_plane_state *plane_state;
2923 	const struct intel_plane_state *pristate = NULL;
2924 	const struct intel_plane_state *sprstate = NULL;
2925 	const struct intel_plane_state *curstate = NULL;
2926 	struct ilk_wm_maximums max;
2927 	int level, usable_level;
2928 
2929 	pipe_wm = &crtc_state->wm.ilk.optimal;
2930 
2931 	intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
2932 		if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
2933 			pristate = plane_state;
2934 		else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
2935 			sprstate = plane_state;
2936 		else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
2937 			curstate = plane_state;
2938 	}
2939 
2940 	pipe_wm->pipe_enabled = crtc_state->hw.active;
2941 	pipe_wm->sprites_enabled = crtc_state->active_planes & BIT(PLANE_SPRITE0);
2942 	pipe_wm->sprites_scaled = crtc_state->scaled_planes & BIT(PLANE_SPRITE0);
2943 
2944 	usable_level = display->wm.num_levels - 1;
2945 
2946 	/* ILK/SNB: LP2+ watermarks only w/o sprites */
2947 	if (DISPLAY_VER(display) < 7 && pipe_wm->sprites_enabled)
2948 		usable_level = 1;
2949 
2950 	/* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2951 	if (pipe_wm->sprites_scaled)
2952 		usable_level = 0;
2953 
2954 	memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2955 	ilk_compute_wm_level(display, crtc, 0, crtc_state,
2956 			     pristate, sprstate, curstate, &pipe_wm->wm[0]);
2957 
2958 	if (!ilk_validate_pipe_wm(display, pipe_wm))
2959 		return -EINVAL;
2960 
2961 	ilk_compute_wm_reg_maximums(display, 1, &max);
2962 
2963 	for (level = 1; level <= usable_level; level++) {
2964 		struct intel_wm_level *wm = &pipe_wm->wm[level];
2965 
2966 		ilk_compute_wm_level(display, crtc, level, crtc_state,
2967 				     pristate, sprstate, curstate, wm);
2968 
2969 		/*
2970 		 * Disable any watermark level that exceeds the
2971 		 * register maximums since such watermarks are
2972 		 * always invalid.
2973 		 */
2974 		if (!ilk_validate_wm_level(display, level, &max, wm)) {
2975 			memset(wm, 0, sizeof(*wm));
2976 			break;
2977 		}
2978 	}
2979 
2980 	return 0;
2981 }
2982 
2983 /*
2984  * Build a set of 'intermediate' watermark values that satisfy both the old
2985  * state and the new state.  These can be programmed to the hardware
2986  * immediately.
2987  */
2988 static int ilk_compute_intermediate_wm(struct intel_atomic_state *state,
2989 				       struct intel_crtc *crtc)
2990 {
2991 	struct intel_display *display = to_intel_display(crtc);
2992 	struct intel_crtc_state *new_crtc_state =
2993 		intel_atomic_get_new_crtc_state(state, crtc);
2994 	const struct intel_crtc_state *old_crtc_state =
2995 		intel_atomic_get_old_crtc_state(state, crtc);
2996 	struct intel_pipe_wm *intermediate = &new_crtc_state->wm.ilk.intermediate;
2997 	const struct intel_pipe_wm *optimal = &new_crtc_state->wm.ilk.optimal;
2998 	const struct intel_pipe_wm *active = &old_crtc_state->wm.ilk.optimal;
2999 	int level;
3000 
3001 	/*
3002 	 * Start with the final, target watermarks, then combine with the
3003 	 * currently active watermarks to get values that are safe both before
3004 	 * and after the vblank.
3005 	 */
3006 	*intermediate = *optimal;
3007 	if (!new_crtc_state->hw.active ||
3008 	    intel_crtc_needs_modeset(new_crtc_state) ||
3009 	    state->skip_intermediate_wm)
3010 		return 0;
3011 
3012 	intermediate->pipe_enabled |= active->pipe_enabled;
3013 	intermediate->sprites_enabled |= active->sprites_enabled;
3014 	intermediate->sprites_scaled |= active->sprites_scaled;
3015 
3016 	for (level = 0; level < display->wm.num_levels; level++) {
3017 		struct intel_wm_level *intermediate_wm = &intermediate->wm[level];
3018 		const struct intel_wm_level *active_wm = &active->wm[level];
3019 
3020 		intermediate_wm->enable &= active_wm->enable;
3021 		intermediate_wm->pri_val = max(intermediate_wm->pri_val,
3022 					       active_wm->pri_val);
3023 		intermediate_wm->spr_val = max(intermediate_wm->spr_val,
3024 					       active_wm->spr_val);
3025 		intermediate_wm->cur_val = max(intermediate_wm->cur_val,
3026 					       active_wm->cur_val);
3027 		intermediate_wm->fbc_val = max(intermediate_wm->fbc_val,
3028 					       active_wm->fbc_val);
3029 	}
3030 
3031 	/*
3032 	 * We need to make sure that these merged watermark values are
3033 	 * actually a valid configuration themselves.  If they're not,
3034 	 * there's no safe way to transition from the old state to
3035 	 * the new state, so we need to fail the atomic transaction.
3036 	 */
3037 	if (!ilk_validate_pipe_wm(display, intermediate))
3038 		return -EINVAL;
3039 
3040 	/*
3041 	 * If our intermediate WM are identical to the final WM, then we can
3042 	 * omit the post-vblank programming; only update if it's different.
3043 	 */
3044 	if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
3045 		new_crtc_state->wm.need_postvbl_update = true;
3046 
3047 	return 0;
3048 }
3049 
3050 static int ilk_compute_watermarks(struct intel_atomic_state *state,
3051 				  struct intel_crtc *crtc)
3052 {
3053 	int ret;
3054 
3055 	ret = ilk_compute_pipe_wm(state, crtc);
3056 	if (ret)
3057 		return ret;
3058 
3059 	ret = ilk_compute_intermediate_wm(state, crtc);
3060 	if (ret)
3061 		return ret;
3062 
3063 	return 0;
3064 }
3065 
3066 /*
3067  * Merge the watermarks from all active pipes for a specific level.
3068  */
3069 static void ilk_merge_wm_level(struct intel_display *display,
3070 			       int level,
3071 			       struct intel_wm_level *ret_wm)
3072 {
3073 	const struct intel_crtc *crtc;
3074 
3075 	ret_wm->enable = true;
3076 
3077 	for_each_intel_crtc(display->drm, crtc) {
3078 		const struct intel_pipe_wm *active = &crtc->wm.active.ilk;
3079 		const struct intel_wm_level *wm = &active->wm[level];
3080 
3081 		if (!active->pipe_enabled)
3082 			continue;
3083 
3084 		/*
3085 		 * The watermark values may have been used in the past,
3086 		 * so we must maintain them in the registers for some
3087 		 * time even if the level is now disabled.
3088 		 */
3089 		if (!wm->enable)
3090 			ret_wm->enable = false;
3091 
3092 		ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3093 		ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3094 		ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3095 		ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3096 	}
3097 }
3098 
3099 /*
3100  * Merge all low power watermarks for all active pipes.
3101  */
3102 static void ilk_wm_merge(struct intel_display *display,
3103 			 const struct intel_wm_config *config,
3104 			 const struct ilk_wm_maximums *max,
3105 			 struct intel_pipe_wm *merged)
3106 {
3107 	int level, num_levels = display->wm.num_levels;
3108 	int last_enabled_level = num_levels - 1;
3109 
3110 	/* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
3111 	if ((DISPLAY_VER(display) < 7 || display->platform.ivybridge) &&
3112 	    config->num_pipes_active > 1)
3113 		last_enabled_level = 0;
3114 
3115 	/* ILK: FBC WM must be disabled always */
3116 	merged->fbc_wm_enabled = DISPLAY_VER(display) >= 6;
3117 
3118 	/* merge each WM1+ level */
3119 	for (level = 1; level < num_levels; level++) {
3120 		struct intel_wm_level *wm = &merged->wm[level];
3121 
3122 		ilk_merge_wm_level(display, level, wm);
3123 
3124 		if (level > last_enabled_level)
3125 			wm->enable = false;
3126 		else if (!ilk_validate_wm_level(display, level, max, wm))
3127 			/* make sure all following levels get disabled */
3128 			last_enabled_level = level - 1;
3129 
3130 		/*
3131 		 * The spec says it is preferred to disable
3132 		 * FBC WMs instead of disabling a WM level.
3133 		 */
3134 		if (wm->fbc_val > max->fbc) {
3135 			if (wm->enable)
3136 				merged->fbc_wm_enabled = false;
3137 			wm->fbc_val = 0;
3138 		}
3139 	}
3140 
3141 	/* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3142 	if (DISPLAY_VER(display) == 5 && HAS_FBC(display) &&
3143 	    display->params.enable_fbc && !merged->fbc_wm_enabled) {
3144 		for (level = 2; level < num_levels; level++) {
3145 			struct intel_wm_level *wm = &merged->wm[level];
3146 
3147 			wm->enable = false;
3148 		}
3149 	}
3150 }
3151 
3152 static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3153 {
3154 	/* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3155 	return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3156 }
3157 
3158 /* The value we need to program into the WM_LPx latency field */
3159 static unsigned int ilk_wm_lp_latency(struct intel_display *display,
3160 				      int level)
3161 {
3162 	if (display->platform.haswell || display->platform.broadwell)
3163 		return 2 * level;
3164 	else
3165 		return display->wm.pri_latency[level];
3166 }
3167 
3168 static void ilk_compute_wm_results(struct intel_display *display,
3169 				   const struct intel_pipe_wm *merged,
3170 				   enum intel_ddb_partitioning partitioning,
3171 				   struct ilk_wm_values *results)
3172 {
3173 	struct intel_crtc *crtc;
3174 	int level, wm_lp;
3175 
3176 	results->enable_fbc_wm = merged->fbc_wm_enabled;
3177 	results->partitioning = partitioning;
3178 
3179 	/* LP1+ register values */
3180 	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3181 		const struct intel_wm_level *r;
3182 
3183 		level = ilk_wm_lp_to_level(wm_lp, merged);
3184 
3185 		r = &merged->wm[level];
3186 
3187 		/*
3188 		 * Maintain the watermark values even if the level is
3189 		 * disabled. Doing otherwise could cause underruns.
3190 		 */
3191 		results->wm_lp[wm_lp - 1] =
3192 			WM_LP_LATENCY(ilk_wm_lp_latency(display, level)) |
3193 			WM_LP_PRIMARY(r->pri_val) |
3194 			WM_LP_CURSOR(r->cur_val);
3195 
3196 		if (r->enable)
3197 			results->wm_lp[wm_lp - 1] |= WM_LP_ENABLE;
3198 
3199 		if (DISPLAY_VER(display) >= 8)
3200 			results->wm_lp[wm_lp - 1] |= WM_LP_FBC_BDW(r->fbc_val);
3201 		else
3202 			results->wm_lp[wm_lp - 1] |= WM_LP_FBC_ILK(r->fbc_val);
3203 
3204 		results->wm_lp_spr[wm_lp - 1] = WM_LP_SPRITE(r->spr_val);
3205 
3206 		/*
3207 		 * Always set WM_LP_SPRITE_EN when spr_val != 0, even if the
3208 		 * level is disabled. Doing otherwise could cause underruns.
3209 		 */
3210 		if (DISPLAY_VER(display) < 7 && r->spr_val) {
3211 			drm_WARN_ON(display->drm, wm_lp != 1);
3212 			results->wm_lp_spr[wm_lp - 1] |= WM_LP_SPRITE_ENABLE;
3213 		}
3214 	}
3215 
3216 	/* LP0 register values */
3217 	for_each_intel_crtc(display->drm, crtc) {
3218 		enum pipe pipe = crtc->pipe;
3219 		const struct intel_pipe_wm *pipe_wm = &crtc->wm.active.ilk;
3220 		const struct intel_wm_level *r = &pipe_wm->wm[0];
3221 
3222 		if (drm_WARN_ON(display->drm, !r->enable))
3223 			continue;
3224 
3225 		results->wm_pipe[pipe] =
3226 			WM0_PIPE_PRIMARY(r->pri_val) |
3227 			WM0_PIPE_SPRITE(r->spr_val) |
3228 			WM0_PIPE_CURSOR(r->cur_val);
3229 	}
3230 }
3231 
3232 /*
3233  * Find the result with the highest level enabled. Check for enable_fbc_wm in
3234  * case both are at the same level. Prefer r1 in case they're the same.
3235  */
3236 static struct intel_pipe_wm *
3237 ilk_find_best_result(struct intel_display *display,
3238 		     struct intel_pipe_wm *r1,
3239 		     struct intel_pipe_wm *r2)
3240 {
3241 	int level, level1 = 0, level2 = 0;
3242 
3243 	for (level = 1; level < display->wm.num_levels; level++) {
3244 		if (r1->wm[level].enable)
3245 			level1 = level;
3246 		if (r2->wm[level].enable)
3247 			level2 = level;
3248 	}
3249 
3250 	if (level1 == level2) {
3251 		if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
3252 			return r2;
3253 		else
3254 			return r1;
3255 	} else if (level1 > level2) {
3256 		return r1;
3257 	} else {
3258 		return r2;
3259 	}
3260 }
3261 
3262 /* dirty bits used to track which watermarks need changes */
3263 #define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3264 #define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3265 #define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3266 #define WM_DIRTY_FBC (1 << 24)
3267 #define WM_DIRTY_DDB (1 << 25)
3268 
3269 static unsigned int ilk_compute_wm_dirty(struct intel_display *display,
3270 					 const struct ilk_wm_values *old,
3271 					 const struct ilk_wm_values *new)
3272 {
3273 	unsigned int dirty = 0;
3274 	enum pipe pipe;
3275 	int wm_lp;
3276 
3277 	for_each_pipe(display, pipe) {
3278 		if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3279 			dirty |= WM_DIRTY_PIPE(pipe);
3280 			/* Must disable LP1+ watermarks too */
3281 			dirty |= WM_DIRTY_LP_ALL;
3282 		}
3283 	}
3284 
3285 	if (old->enable_fbc_wm != new->enable_fbc_wm) {
3286 		dirty |= WM_DIRTY_FBC;
3287 		/* Must disable LP1+ watermarks too */
3288 		dirty |= WM_DIRTY_LP_ALL;
3289 	}
3290 
3291 	if (old->partitioning != new->partitioning) {
3292 		dirty |= WM_DIRTY_DDB;
3293 		/* Must disable LP1+ watermarks too */
3294 		dirty |= WM_DIRTY_LP_ALL;
3295 	}
3296 
3297 	/* LP1+ watermarks already deemed dirty, no need to continue */
3298 	if (dirty & WM_DIRTY_LP_ALL)
3299 		return dirty;
3300 
3301 	/* Find the lowest numbered LP1+ watermark in need of an update... */
3302 	for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3303 		if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3304 		    old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3305 			break;
3306 	}
3307 
3308 	/* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3309 	for (; wm_lp <= 3; wm_lp++)
3310 		dirty |= WM_DIRTY_LP(wm_lp);
3311 
3312 	return dirty;
3313 }
3314 
3315 static bool _ilk_disable_lp_wm(struct intel_display *display,
3316 			       unsigned int dirty)
3317 {
3318 	struct ilk_wm_values *previous = &display->wm.hw;
3319 	bool changed = false;
3320 
3321 	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM_LP_ENABLE) {
3322 		previous->wm_lp[2] &= ~WM_LP_ENABLE;
3323 		intel_de_write(display, WM3_LP_ILK, previous->wm_lp[2]);
3324 		changed = true;
3325 	}
3326 	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM_LP_ENABLE) {
3327 		previous->wm_lp[1] &= ~WM_LP_ENABLE;
3328 		intel_de_write(display, WM2_LP_ILK, previous->wm_lp[1]);
3329 		changed = true;
3330 	}
3331 	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM_LP_ENABLE) {
3332 		previous->wm_lp[0] &= ~WM_LP_ENABLE;
3333 		intel_de_write(display, WM1_LP_ILK, previous->wm_lp[0]);
3334 		changed = true;
3335 	}
3336 
3337 	/*
3338 	 * Don't touch WM_LP_SPRITE_ENABLE here.
3339 	 * Doing so could cause underruns.
3340 	 */
3341 
3342 	return changed;
3343 }
3344 
3345 /*
3346  * The spec says we shouldn't write when we don't need, because every write
3347  * causes WMs to be re-evaluated, expending some power.
3348  */
3349 static void ilk_write_wm_values(struct intel_display *display,
3350 				struct ilk_wm_values *results)
3351 {
3352 	struct ilk_wm_values *previous = &display->wm.hw;
3353 	unsigned int dirty;
3354 
3355 	dirty = ilk_compute_wm_dirty(display, previous, results);
3356 	if (!dirty)
3357 		return;
3358 
3359 	_ilk_disable_lp_wm(display, dirty);
3360 
3361 	if (dirty & WM_DIRTY_PIPE(PIPE_A))
3362 		intel_de_write(display, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
3363 	if (dirty & WM_DIRTY_PIPE(PIPE_B))
3364 		intel_de_write(display, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
3365 	if (dirty & WM_DIRTY_PIPE(PIPE_C))
3366 		intel_de_write(display, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
3367 
3368 	if (dirty & WM_DIRTY_DDB) {
3369 		if (display->platform.haswell || display->platform.broadwell)
3370 			intel_de_rmw(display, WM_MISC, WM_MISC_DATA_PARTITION_5_6,
3371 				     results->partitioning == INTEL_DDB_PART_1_2 ? 0 :
3372 				     WM_MISC_DATA_PARTITION_5_6);
3373 		else
3374 			intel_de_rmw(display, DISP_ARB_CTL2, DISP_DATA_PARTITION_5_6,
3375 				     results->partitioning == INTEL_DDB_PART_1_2 ? 0 :
3376 				     DISP_DATA_PARTITION_5_6);
3377 	}
3378 
3379 	if (dirty & WM_DIRTY_FBC)
3380 		intel_de_rmw(display, DISP_ARB_CTL, DISP_FBC_WM_DIS,
3381 			     results->enable_fbc_wm ? 0 : DISP_FBC_WM_DIS);
3382 
3383 	if (dirty & WM_DIRTY_LP(1) &&
3384 	    previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3385 		intel_de_write(display, WM1S_LP_ILK, results->wm_lp_spr[0]);
3386 
3387 	if (DISPLAY_VER(display) >= 7) {
3388 		if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3389 			intel_de_write(display, WM2S_LP_IVB, results->wm_lp_spr[1]);
3390 		if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3391 			intel_de_write(display, WM3S_LP_IVB, results->wm_lp_spr[2]);
3392 	}
3393 
3394 	if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
3395 		intel_de_write(display, WM1_LP_ILK, results->wm_lp[0]);
3396 	if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
3397 		intel_de_write(display, WM2_LP_ILK, results->wm_lp[1]);
3398 	if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
3399 		intel_de_write(display, WM3_LP_ILK, results->wm_lp[2]);
3400 
3401 	display->wm.hw = *results;
3402 }
3403 
3404 bool ilk_disable_cxsr(struct intel_display *display)
3405 {
3406 	return _ilk_disable_lp_wm(display, WM_DIRTY_LP_ALL);
3407 }
3408 
3409 static void ilk_compute_wm_config(struct intel_display *display,
3410 				  struct intel_wm_config *config)
3411 {
3412 	struct intel_crtc *crtc;
3413 
3414 	/* Compute the currently _active_ config */
3415 	for_each_intel_crtc(display->drm, crtc) {
3416 		const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
3417 
3418 		if (!wm->pipe_enabled)
3419 			continue;
3420 
3421 		config->sprites_enabled |= wm->sprites_enabled;
3422 		config->sprites_scaled |= wm->sprites_scaled;
3423 		config->num_pipes_active++;
3424 	}
3425 }
3426 
3427 static void ilk_program_watermarks(struct intel_display *display)
3428 {
3429 	struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
3430 	struct ilk_wm_maximums max;
3431 	struct intel_wm_config config = {};
3432 	struct ilk_wm_values results = {};
3433 	enum intel_ddb_partitioning partitioning;
3434 
3435 	ilk_compute_wm_config(display, &config);
3436 
3437 	ilk_compute_wm_maximums(display, 1, &config, INTEL_DDB_PART_1_2, &max);
3438 	ilk_wm_merge(display, &config, &max, &lp_wm_1_2);
3439 
3440 	/* 5/6 split only in single pipe config on IVB+ */
3441 	if (DISPLAY_VER(display) >= 7 &&
3442 	    config.num_pipes_active == 1 && config.sprites_enabled) {
3443 		ilk_compute_wm_maximums(display, 1, &config, INTEL_DDB_PART_5_6, &max);
3444 		ilk_wm_merge(display, &config, &max, &lp_wm_5_6);
3445 
3446 		best_lp_wm = ilk_find_best_result(display, &lp_wm_1_2, &lp_wm_5_6);
3447 	} else {
3448 		best_lp_wm = &lp_wm_1_2;
3449 	}
3450 
3451 	partitioning = (best_lp_wm == &lp_wm_1_2) ?
3452 		       INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
3453 
3454 	ilk_compute_wm_results(display, best_lp_wm, partitioning, &results);
3455 
3456 	ilk_write_wm_values(display, &results);
3457 }
3458 
3459 static void ilk_initial_watermarks(struct intel_atomic_state *state,
3460 				   struct intel_crtc *crtc)
3461 {
3462 	struct intel_display *display = to_intel_display(crtc);
3463 	const struct intel_crtc_state *crtc_state =
3464 		intel_atomic_get_new_crtc_state(state, crtc);
3465 
3466 	mutex_lock(&display->wm.wm_mutex);
3467 	crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
3468 	ilk_program_watermarks(display);
3469 	mutex_unlock(&display->wm.wm_mutex);
3470 }
3471 
3472 static void ilk_optimize_watermarks(struct intel_atomic_state *state,
3473 				    struct intel_crtc *crtc)
3474 {
3475 	struct intel_display *display = to_intel_display(crtc);
3476 	const struct intel_crtc_state *crtc_state =
3477 		intel_atomic_get_new_crtc_state(state, crtc);
3478 
3479 	if (!crtc_state->wm.need_postvbl_update)
3480 		return;
3481 
3482 	mutex_lock(&display->wm.wm_mutex);
3483 	crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
3484 	ilk_program_watermarks(display);
3485 	mutex_unlock(&display->wm.wm_mutex);
3486 }
3487 
3488 static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
3489 {
3490 	struct intel_display *display = to_intel_display(crtc);
3491 	struct ilk_wm_values *hw = &display->wm.hw;
3492 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
3493 	struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
3494 	enum pipe pipe = crtc->pipe;
3495 
3496 	hw->wm_pipe[pipe] = intel_de_read(display, WM0_PIPE_ILK(pipe));
3497 
3498 	memset(active, 0, sizeof(*active));
3499 
3500 	active->pipe_enabled = crtc->active;
3501 
3502 	if (active->pipe_enabled) {
3503 		u32 tmp = hw->wm_pipe[pipe];
3504 
3505 		/*
3506 		 * For active pipes LP0 watermark is marked as
3507 		 * enabled, and LP1+ watermaks as disabled since
3508 		 * we can't really reverse compute them in case
3509 		 * multiple pipes are active.
3510 		 */
3511 		active->wm[0].enable = true;
3512 		active->wm[0].pri_val = REG_FIELD_GET(WM0_PIPE_PRIMARY_MASK, tmp);
3513 		active->wm[0].spr_val = REG_FIELD_GET(WM0_PIPE_SPRITE_MASK, tmp);
3514 		active->wm[0].cur_val = REG_FIELD_GET(WM0_PIPE_CURSOR_MASK, tmp);
3515 	} else {
3516 		int level;
3517 
3518 		/*
3519 		 * For inactive pipes, all watermark levels
3520 		 * should be marked as enabled but zeroed,
3521 		 * which is what we'd compute them to.
3522 		 */
3523 		for (level = 0; level < display->wm.num_levels; level++)
3524 			active->wm[level].enable = true;
3525 	}
3526 
3527 	crtc->wm.active.ilk = *active;
3528 }
3529 
3530 static int ilk_sanitize_watermarks_add_affected(struct drm_atomic_state *state)
3531 {
3532 	struct drm_plane *plane;
3533 	struct intel_crtc *crtc;
3534 
3535 	for_each_intel_crtc(state->dev, crtc) {
3536 		struct intel_crtc_state *crtc_state;
3537 
3538 		crtc_state = intel_atomic_get_crtc_state(state, crtc);
3539 		if (IS_ERR(crtc_state))
3540 			return PTR_ERR(crtc_state);
3541 
3542 		if (crtc_state->hw.active) {
3543 			/*
3544 			 * Preserve the inherited flag to avoid
3545 			 * taking the full modeset path.
3546 			 */
3547 			crtc_state->inherited = true;
3548 		}
3549 	}
3550 
3551 	drm_for_each_plane(plane, state->dev) {
3552 		struct drm_plane_state *plane_state;
3553 
3554 		plane_state = drm_atomic_get_plane_state(state, plane);
3555 		if (IS_ERR(plane_state))
3556 			return PTR_ERR(plane_state);
3557 	}
3558 
3559 	return 0;
3560 }
3561 
3562 /*
3563  * Calculate what we think the watermarks should be for the state we've read
3564  * out of the hardware and then immediately program those watermarks so that
3565  * we ensure the hardware settings match our internal state.
3566  *
3567  * We can calculate what we think WM's should be by creating a duplicate of the
3568  * current state (which was constructed during hardware readout) and running it
3569  * through the atomic check code to calculate new watermark values in the
3570  * state object.
3571  */
3572 void ilk_wm_sanitize(struct intel_display *display)
3573 {
3574 	struct drm_atomic_state *state;
3575 	struct intel_atomic_state *intel_state;
3576 	struct intel_crtc *crtc;
3577 	struct intel_crtc_state *crtc_state;
3578 	struct drm_modeset_acquire_ctx ctx;
3579 	int ret;
3580 	int i;
3581 
3582 	/* Only supported on platforms that use atomic watermark design */
3583 	if (!display->funcs.wm->optimize_watermarks)
3584 		return;
3585 
3586 	if (drm_WARN_ON(display->drm, DISPLAY_VER(display) >= 9))
3587 		return;
3588 
3589 	state = drm_atomic_state_alloc(display->drm);
3590 	if (drm_WARN_ON(display->drm, !state))
3591 		return;
3592 
3593 	intel_state = to_intel_atomic_state(state);
3594 
3595 	drm_modeset_acquire_init(&ctx, 0);
3596 
3597 	state->acquire_ctx = &ctx;
3598 	to_intel_atomic_state(state)->internal = true;
3599 
3600 retry:
3601 	/*
3602 	 * Hardware readout is the only time we don't want to calculate
3603 	 * intermediate watermarks (since we don't trust the current
3604 	 * watermarks).
3605 	 */
3606 	if (!HAS_GMCH(display))
3607 		intel_state->skip_intermediate_wm = true;
3608 
3609 	ret = ilk_sanitize_watermarks_add_affected(state);
3610 	if (ret)
3611 		goto fail;
3612 
3613 	ret = intel_atomic_check(display->drm, state);
3614 	if (ret)
3615 		goto fail;
3616 
3617 	/* Write calculated watermark values back */
3618 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
3619 		crtc_state->wm.need_postvbl_update = true;
3620 		intel_optimize_watermarks(intel_state, crtc);
3621 
3622 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
3623 	}
3624 
3625 fail:
3626 	if (ret == -EDEADLK) {
3627 		drm_atomic_state_clear(state);
3628 		drm_modeset_backoff(&ctx);
3629 		goto retry;
3630 	}
3631 
3632 	/*
3633 	 * If we fail here, it means that the hardware appears to be
3634 	 * programmed in a way that shouldn't be possible, given our
3635 	 * understanding of watermark requirements.  This might mean a
3636 	 * mistake in the hardware readout code or a mistake in the
3637 	 * watermark calculations for a given platform.  Raise a WARN
3638 	 * so that this is noticeable.
3639 	 *
3640 	 * If this actually happens, we'll have to just leave the
3641 	 * BIOS-programmed watermarks untouched and hope for the best.
3642 	 */
3643 	drm_WARN(display->drm, ret,
3644 		 "Could not determine valid watermarks for inherited state\n");
3645 
3646 	drm_atomic_state_put(state);
3647 
3648 	drm_modeset_drop_locks(&ctx);
3649 	drm_modeset_acquire_fini(&ctx);
3650 }
3651 
3652 #define _FW_WM(value, plane) \
3653 	(((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3654 #define _FW_WM_VLV(value, plane) \
3655 	(((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3656 
3657 static void g4x_read_wm_values(struct intel_display *display,
3658 			       struct g4x_wm_values *wm)
3659 {
3660 	u32 tmp;
3661 
3662 	tmp = intel_de_read(display, DSPFW1(display));
3663 	wm->sr.plane = _FW_WM(tmp, SR);
3664 	wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
3665 	wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
3666 	wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
3667 
3668 	tmp = intel_de_read(display, DSPFW2(display));
3669 	wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
3670 	wm->sr.fbc = _FW_WM(tmp, FBC_SR);
3671 	wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
3672 	wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
3673 	wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
3674 	wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
3675 
3676 	tmp = intel_de_read(display, DSPFW3(display));
3677 	wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
3678 	wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3679 	wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
3680 	wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
3681 }
3682 
3683 static void vlv_read_wm_values(struct intel_display *display,
3684 			       struct vlv_wm_values *wm)
3685 {
3686 	enum pipe pipe;
3687 	u32 tmp;
3688 
3689 	for_each_pipe(display, pipe) {
3690 		tmp = intel_de_read(display, VLV_DDL(pipe));
3691 
3692 		wm->ddl[pipe].plane[PLANE_PRIMARY] =
3693 			(tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3694 		wm->ddl[pipe].plane[PLANE_CURSOR] =
3695 			(tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3696 		wm->ddl[pipe].plane[PLANE_SPRITE0] =
3697 			(tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3698 		wm->ddl[pipe].plane[PLANE_SPRITE1] =
3699 			(tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3700 	}
3701 
3702 	tmp = intel_de_read(display, DSPFW1(display));
3703 	wm->sr.plane = _FW_WM(tmp, SR);
3704 	wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
3705 	wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
3706 	wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
3707 
3708 	tmp = intel_de_read(display, DSPFW2(display));
3709 	wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
3710 	wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
3711 	wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
3712 
3713 	tmp = intel_de_read(display, DSPFW3(display));
3714 	wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3715 
3716 	if (display->platform.cherryview) {
3717 		tmp = intel_de_read(display, DSPFW7_CHV);
3718 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
3719 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
3720 
3721 		tmp = intel_de_read(display, DSPFW8_CHV);
3722 		wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
3723 		wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
3724 
3725 		tmp = intel_de_read(display, DSPFW9_CHV);
3726 		wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
3727 		wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
3728 
3729 		tmp = intel_de_read(display, DSPHOWM);
3730 		wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3731 		wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
3732 		wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
3733 		wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
3734 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3735 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3736 		wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
3737 		wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3738 		wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3739 		wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
3740 	} else {
3741 		tmp = intel_de_read(display, DSPFW7);
3742 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
3743 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
3744 
3745 		tmp = intel_de_read(display, DSPHOWM);
3746 		wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3747 		wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3748 		wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3749 		wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
3750 		wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3751 		wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3752 		wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
3753 	}
3754 }
3755 
3756 #undef _FW_WM
3757 #undef _FW_WM_VLV
3758 
3759 static void g4x_wm_get_hw_state(struct intel_display *display)
3760 {
3761 	struct g4x_wm_values *wm = &display->wm.g4x;
3762 	struct intel_crtc *crtc;
3763 
3764 	g4x_read_wm_values(display, wm);
3765 
3766 	wm->cxsr = intel_de_read(display, FW_BLC_SELF) & FW_BLC_SELF_EN;
3767 
3768 	for_each_intel_crtc(display->drm, crtc) {
3769 		struct intel_crtc_state *crtc_state =
3770 			to_intel_crtc_state(crtc->base.state);
3771 		struct g4x_wm_state *active = &crtc->wm.active.g4x;
3772 		struct g4x_pipe_wm *raw;
3773 		enum pipe pipe = crtc->pipe;
3774 		enum plane_id plane_id;
3775 		int level, max_level;
3776 
3777 		active->cxsr = wm->cxsr;
3778 		active->hpll_en = wm->hpll_en;
3779 		active->fbc_en = wm->fbc_en;
3780 
3781 		active->sr = wm->sr;
3782 		active->hpll = wm->hpll;
3783 
3784 		for_each_plane_id_on_crtc(crtc, plane_id) {
3785 			active->wm.plane[plane_id] =
3786 				wm->pipe[pipe].plane[plane_id];
3787 		}
3788 
3789 		if (wm->cxsr && wm->hpll_en)
3790 			max_level = G4X_WM_LEVEL_HPLL;
3791 		else if (wm->cxsr)
3792 			max_level = G4X_WM_LEVEL_SR;
3793 		else
3794 			max_level = G4X_WM_LEVEL_NORMAL;
3795 
3796 		level = G4X_WM_LEVEL_NORMAL;
3797 		raw = &crtc_state->wm.g4x.raw[level];
3798 		for_each_plane_id_on_crtc(crtc, plane_id)
3799 			raw->plane[plane_id] = active->wm.plane[plane_id];
3800 
3801 		level = G4X_WM_LEVEL_SR;
3802 		if (level > max_level)
3803 			goto out;
3804 
3805 		raw = &crtc_state->wm.g4x.raw[level];
3806 		raw->plane[PLANE_PRIMARY] = active->sr.plane;
3807 		raw->plane[PLANE_CURSOR] = active->sr.cursor;
3808 		raw->plane[PLANE_SPRITE0] = 0;
3809 		raw->fbc = active->sr.fbc;
3810 
3811 		level = G4X_WM_LEVEL_HPLL;
3812 		if (level > max_level)
3813 			goto out;
3814 
3815 		raw = &crtc_state->wm.g4x.raw[level];
3816 		raw->plane[PLANE_PRIMARY] = active->hpll.plane;
3817 		raw->plane[PLANE_CURSOR] = active->hpll.cursor;
3818 		raw->plane[PLANE_SPRITE0] = 0;
3819 		raw->fbc = active->hpll.fbc;
3820 
3821 		level++;
3822 	out:
3823 		for_each_plane_id_on_crtc(crtc, plane_id)
3824 			g4x_raw_plane_wm_set(crtc_state, level,
3825 					     plane_id, USHRT_MAX);
3826 		g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
3827 
3828 		g4x_invalidate_wms(crtc, active, level);
3829 
3830 		crtc_state->wm.g4x.optimal = *active;
3831 		crtc_state->wm.g4x.intermediate = *active;
3832 
3833 		drm_dbg_kms(display->drm,
3834 			    "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
3835 			    pipe_name(pipe),
3836 			    wm->pipe[pipe].plane[PLANE_PRIMARY],
3837 			    wm->pipe[pipe].plane[PLANE_CURSOR],
3838 			    wm->pipe[pipe].plane[PLANE_SPRITE0]);
3839 	}
3840 
3841 	drm_dbg_kms(display->drm,
3842 		    "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
3843 		    wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
3844 	drm_dbg_kms(display->drm,
3845 		    "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
3846 		    wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
3847 	drm_dbg_kms(display->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
3848 		    str_yes_no(wm->cxsr), str_yes_no(wm->hpll_en),
3849 		    str_yes_no(wm->fbc_en));
3850 }
3851 
3852 static void g4x_wm_sanitize(struct intel_display *display)
3853 {
3854 	struct intel_plane *plane;
3855 	struct intel_crtc *crtc;
3856 
3857 	mutex_lock(&display->wm.wm_mutex);
3858 
3859 	for_each_intel_plane(display->drm, plane) {
3860 		struct intel_crtc *crtc =
3861 			intel_crtc_for_pipe(display, plane->pipe);
3862 		struct intel_crtc_state *crtc_state =
3863 			to_intel_crtc_state(crtc->base.state);
3864 		struct intel_plane_state *plane_state =
3865 			to_intel_plane_state(plane->base.state);
3866 		enum plane_id plane_id = plane->id;
3867 		int level;
3868 
3869 		if (plane_state->uapi.visible)
3870 			continue;
3871 
3872 		for (level = 0; level < display->wm.num_levels; level++) {
3873 			struct g4x_pipe_wm *raw =
3874 				&crtc_state->wm.g4x.raw[level];
3875 
3876 			raw->plane[plane_id] = 0;
3877 
3878 			if (plane_id == PLANE_PRIMARY)
3879 				raw->fbc = 0;
3880 		}
3881 	}
3882 
3883 	for_each_intel_crtc(display->drm, crtc) {
3884 		struct intel_crtc_state *crtc_state =
3885 			to_intel_crtc_state(crtc->base.state);
3886 		int ret;
3887 
3888 		ret = _g4x_compute_pipe_wm(crtc_state);
3889 		drm_WARN_ON(display->drm, ret);
3890 
3891 		crtc_state->wm.g4x.intermediate =
3892 			crtc_state->wm.g4x.optimal;
3893 		crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
3894 	}
3895 
3896 	g4x_program_watermarks(display);
3897 
3898 	mutex_unlock(&display->wm.wm_mutex);
3899 }
3900 
3901 static void vlv_wm_get_hw_state(struct intel_display *display)
3902 {
3903 	struct drm_i915_private *dev_priv = to_i915(display->drm);
3904 	struct vlv_wm_values *wm = &display->wm.vlv;
3905 	struct intel_crtc *crtc;
3906 	u32 val;
3907 
3908 	vlv_read_wm_values(display, wm);
3909 
3910 	wm->cxsr = intel_de_read(display, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
3911 	wm->level = VLV_WM_LEVEL_PM2;
3912 
3913 	if (display->platform.cherryview) {
3914 		vlv_punit_get(dev_priv);
3915 
3916 		val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
3917 		if (val & DSP_MAXFIFO_PM5_ENABLE)
3918 			wm->level = VLV_WM_LEVEL_PM5;
3919 
3920 		/*
3921 		 * If DDR DVFS is disabled in the BIOS, Punit
3922 		 * will never ack the request. So if that happens
3923 		 * assume we don't have to enable/disable DDR DVFS
3924 		 * dynamically. To test that just set the REQ_ACK
3925 		 * bit to poke the Punit, but don't change the
3926 		 * HIGH/LOW bits so that we don't actually change
3927 		 * the current state.
3928 		 */
3929 		val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
3930 		val |= FORCE_DDR_FREQ_REQ_ACK;
3931 		vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
3932 
3933 		if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
3934 			      FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
3935 			drm_dbg_kms(display->drm,
3936 				    "Punit not acking DDR DVFS request, "
3937 				    "assuming DDR DVFS is disabled\n");
3938 			display->wm.num_levels = VLV_WM_LEVEL_PM5 + 1;
3939 		} else {
3940 			val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
3941 			if ((val & FORCE_DDR_HIGH_FREQ) == 0)
3942 				wm->level = VLV_WM_LEVEL_DDR_DVFS;
3943 		}
3944 
3945 		vlv_punit_put(dev_priv);
3946 	}
3947 
3948 	for_each_intel_crtc(display->drm, crtc) {
3949 		struct intel_crtc_state *crtc_state =
3950 			to_intel_crtc_state(crtc->base.state);
3951 		struct vlv_wm_state *active = &crtc->wm.active.vlv;
3952 		const struct vlv_fifo_state *fifo_state =
3953 			&crtc_state->wm.vlv.fifo_state;
3954 		enum pipe pipe = crtc->pipe;
3955 		enum plane_id plane_id;
3956 		int level;
3957 
3958 		vlv_get_fifo_size(crtc_state);
3959 
3960 		active->num_levels = wm->level + 1;
3961 		active->cxsr = wm->cxsr;
3962 
3963 		for (level = 0; level < active->num_levels; level++) {
3964 			struct g4x_pipe_wm *raw =
3965 				&crtc_state->wm.vlv.raw[level];
3966 
3967 			active->sr[level].plane = wm->sr.plane;
3968 			active->sr[level].cursor = wm->sr.cursor;
3969 
3970 			for_each_plane_id_on_crtc(crtc, plane_id) {
3971 				active->wm[level].plane[plane_id] =
3972 					wm->pipe[pipe].plane[plane_id];
3973 
3974 				raw->plane[plane_id] =
3975 					vlv_invert_wm_value(active->wm[level].plane[plane_id],
3976 							    fifo_state->plane[plane_id]);
3977 			}
3978 		}
3979 
3980 		for_each_plane_id_on_crtc(crtc, plane_id)
3981 			vlv_raw_plane_wm_set(crtc_state, level,
3982 					     plane_id, USHRT_MAX);
3983 		vlv_invalidate_wms(crtc, active, level);
3984 
3985 		crtc_state->wm.vlv.optimal = *active;
3986 		crtc_state->wm.vlv.intermediate = *active;
3987 
3988 		drm_dbg_kms(display->drm,
3989 			    "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
3990 			    pipe_name(pipe),
3991 			    wm->pipe[pipe].plane[PLANE_PRIMARY],
3992 			    wm->pipe[pipe].plane[PLANE_CURSOR],
3993 			    wm->pipe[pipe].plane[PLANE_SPRITE0],
3994 			    wm->pipe[pipe].plane[PLANE_SPRITE1]);
3995 	}
3996 
3997 	drm_dbg_kms(display->drm,
3998 		    "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
3999 		    wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4000 }
4001 
4002 static void vlv_wm_sanitize(struct intel_display *display)
4003 {
4004 	struct intel_plane *plane;
4005 	struct intel_crtc *crtc;
4006 
4007 	mutex_lock(&display->wm.wm_mutex);
4008 
4009 	for_each_intel_plane(display->drm, plane) {
4010 		struct intel_crtc *crtc =
4011 			intel_crtc_for_pipe(display, plane->pipe);
4012 		struct intel_crtc_state *crtc_state =
4013 			to_intel_crtc_state(crtc->base.state);
4014 		struct intel_plane_state *plane_state =
4015 			to_intel_plane_state(plane->base.state);
4016 		enum plane_id plane_id = plane->id;
4017 		int level;
4018 
4019 		if (plane_state->uapi.visible)
4020 			continue;
4021 
4022 		for (level = 0; level < display->wm.num_levels; level++) {
4023 			struct g4x_pipe_wm *raw =
4024 				&crtc_state->wm.vlv.raw[level];
4025 
4026 			raw->plane[plane_id] = 0;
4027 		}
4028 	}
4029 
4030 	for_each_intel_crtc(display->drm, crtc) {
4031 		struct intel_crtc_state *crtc_state =
4032 			to_intel_crtc_state(crtc->base.state);
4033 		int ret;
4034 
4035 		ret = _vlv_compute_pipe_wm(crtc_state);
4036 		drm_WARN_ON(display->drm, ret);
4037 
4038 		crtc_state->wm.vlv.intermediate =
4039 			crtc_state->wm.vlv.optimal;
4040 		crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
4041 	}
4042 
4043 	vlv_program_watermarks(display);
4044 
4045 	mutex_unlock(&display->wm.wm_mutex);
4046 }
4047 
4048 /*
4049  * FIXME should probably kill this and improve
4050  * the real watermark readout/sanitation instead
4051  */
4052 static void ilk_init_lp_watermarks(struct intel_display *display)
4053 {
4054 	intel_de_rmw(display, WM3_LP_ILK, WM_LP_ENABLE, 0);
4055 	intel_de_rmw(display, WM2_LP_ILK, WM_LP_ENABLE, 0);
4056 	intel_de_rmw(display, WM1_LP_ILK, WM_LP_ENABLE, 0);
4057 
4058 	/*
4059 	 * Don't touch WM_LP_SPRITE_ENABLE here.
4060 	 * Doing so could cause underruns.
4061 	 */
4062 }
4063 
4064 static void ilk_wm_get_hw_state(struct intel_display *display)
4065 {
4066 	struct ilk_wm_values *hw = &display->wm.hw;
4067 	struct intel_crtc *crtc;
4068 
4069 	ilk_init_lp_watermarks(display);
4070 
4071 	for_each_intel_crtc(display->drm, crtc)
4072 		ilk_pipe_wm_get_hw_state(crtc);
4073 
4074 	hw->wm_lp[0] = intel_de_read(display, WM1_LP_ILK);
4075 	hw->wm_lp[1] = intel_de_read(display, WM2_LP_ILK);
4076 	hw->wm_lp[2] = intel_de_read(display, WM3_LP_ILK);
4077 
4078 	hw->wm_lp_spr[0] = intel_de_read(display, WM1S_LP_ILK);
4079 	if (DISPLAY_VER(display) >= 7) {
4080 		hw->wm_lp_spr[1] = intel_de_read(display, WM2S_LP_IVB);
4081 		hw->wm_lp_spr[2] = intel_de_read(display, WM3S_LP_IVB);
4082 	}
4083 
4084 	if (display->platform.haswell || display->platform.broadwell)
4085 		hw->partitioning = (intel_de_read(display, WM_MISC) &
4086 				    WM_MISC_DATA_PARTITION_5_6) ?
4087 			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4088 	else if (display->platform.ivybridge)
4089 		hw->partitioning = (intel_de_read(display, DISP_ARB_CTL2) &
4090 				    DISP_DATA_PARTITION_5_6) ?
4091 			INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4092 
4093 	hw->enable_fbc_wm =
4094 		!(intel_de_read(display, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4095 }
4096 
4097 static const struct intel_wm_funcs ilk_wm_funcs = {
4098 	.compute_watermarks = ilk_compute_watermarks,
4099 	.initial_watermarks = ilk_initial_watermarks,
4100 	.optimize_watermarks = ilk_optimize_watermarks,
4101 	.get_hw_state = ilk_wm_get_hw_state,
4102 };
4103 
4104 static const struct intel_wm_funcs vlv_wm_funcs = {
4105 	.compute_watermarks = vlv_compute_watermarks,
4106 	.initial_watermarks = vlv_initial_watermarks,
4107 	.optimize_watermarks = vlv_optimize_watermarks,
4108 	.atomic_update_watermarks = vlv_atomic_update_fifo,
4109 	.get_hw_state = vlv_wm_get_hw_state,
4110 	.sanitize = vlv_wm_sanitize,
4111 };
4112 
4113 static const struct intel_wm_funcs g4x_wm_funcs = {
4114 	.compute_watermarks = g4x_compute_watermarks,
4115 	.initial_watermarks = g4x_initial_watermarks,
4116 	.optimize_watermarks = g4x_optimize_watermarks,
4117 	.get_hw_state = g4x_wm_get_hw_state,
4118 	.sanitize = g4x_wm_sanitize,
4119 };
4120 
4121 static const struct intel_wm_funcs pnv_wm_funcs = {
4122 	.compute_watermarks = i9xx_compute_watermarks,
4123 	.update_wm = pnv_update_wm,
4124 };
4125 
4126 static const struct intel_wm_funcs i965_wm_funcs = {
4127 	.compute_watermarks = i9xx_compute_watermarks,
4128 	.update_wm = i965_update_wm,
4129 };
4130 
4131 static const struct intel_wm_funcs i9xx_wm_funcs = {
4132 	.compute_watermarks = i9xx_compute_watermarks,
4133 	.update_wm = i9xx_update_wm,
4134 };
4135 
4136 static const struct intel_wm_funcs i845_wm_funcs = {
4137 	.compute_watermarks = i9xx_compute_watermarks,
4138 	.update_wm = i845_update_wm,
4139 };
4140 
4141 static const struct intel_wm_funcs nop_funcs = {
4142 };
4143 
4144 void i9xx_wm_init(struct intel_display *display)
4145 {
4146 	/* For FIFO watermark updates */
4147 	if (HAS_PCH_SPLIT(display)) {
4148 		ilk_setup_wm_latency(display);
4149 		display->funcs.wm = &ilk_wm_funcs;
4150 	} else if (display->platform.valleyview || display->platform.cherryview) {
4151 		vlv_setup_wm_latency(display);
4152 		display->funcs.wm = &vlv_wm_funcs;
4153 	} else if (display->platform.g4x) {
4154 		g4x_setup_wm_latency(display);
4155 		display->funcs.wm = &g4x_wm_funcs;
4156 	} else if (display->platform.pineview) {
4157 		if (!pnv_get_cxsr_latency(display)) {
4158 			drm_info(display->drm, "Unknown FSB/MEM, disabling CxSR\n");
4159 			/* Disable CxSR and never update its watermark again */
4160 			intel_set_memory_cxsr(display, false);
4161 			display->funcs.wm = &nop_funcs;
4162 		} else {
4163 			display->funcs.wm = &pnv_wm_funcs;
4164 		}
4165 	} else if (DISPLAY_VER(display) == 4) {
4166 		display->funcs.wm = &i965_wm_funcs;
4167 	} else if (DISPLAY_VER(display) == 3) {
4168 		display->funcs.wm = &i9xx_wm_funcs;
4169 	} else if (DISPLAY_VER(display) == 2) {
4170 		if (INTEL_NUM_PIPES(display) == 1)
4171 			display->funcs.wm = &i845_wm_funcs;
4172 		else
4173 			display->funcs.wm = &i9xx_wm_funcs;
4174 	} else {
4175 		drm_err(display->drm,
4176 			"unexpected fall-through in %s\n", __func__);
4177 		display->funcs.wm = &nop_funcs;
4178 	}
4179 }
4180