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