Loading...
1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
28#include <linux/cpufreq.h>
29#include "i915_drv.h"
30#include "intel_drv.h"
31#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
33
34/**
35 * DOC: RC6
36 *
37 * RC6 is a special power stage which allows the GPU to enter an very
38 * low-voltage mode when idle, using down to 0V while at this stage. This
39 * stage is entered automatically when the GPU is idle when RC6 support is
40 * enabled, and as soon as new workload arises GPU wakes up automatically as well.
41 *
42 * There are different RC6 modes available in Intel GPU, which differentiate
43 * among each other with the latency required to enter and leave RC6 and
44 * voltage consumed by the GPU in different states.
45 *
46 * The combination of the following flags define which states GPU is allowed
47 * to enter, while RC6 is the normal RC6 state, RC6p is the deep RC6, and
48 * RC6pp is deepest RC6. Their support by hardware varies according to the
49 * GPU, BIOS, chipset and platform. RC6 is usually the safest one and the one
50 * which brings the most power savings; deeper states save more power, but
51 * require higher latency to switch to and wake up.
52 */
53#define INTEL_RC6_ENABLE (1<<0)
54#define INTEL_RC6p_ENABLE (1<<1)
55#define INTEL_RC6pp_ENABLE (1<<2)
56
57static void bxt_init_clock_gating(struct drm_device *dev)
58{
59 struct drm_i915_private *dev_priv = dev->dev_private;
60
61 /* WaDisableSDEUnitClockGating:bxt */
62 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
63 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
64
65 /*
66 * FIXME:
67 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
68 */
69 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
70 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
71
72 /*
73 * Wa: Backlight PWM may stop in the asserted state, causing backlight
74 * to stay fully on.
75 */
76 if (IS_BXT_REVID(dev_priv, BXT_REVID_B0, REVID_FOREVER))
77 I915_WRITE(GEN9_CLKGATE_DIS_0, I915_READ(GEN9_CLKGATE_DIS_0) |
78 PWM1_GATING_DIS | PWM2_GATING_DIS);
79}
80
81static void i915_pineview_get_mem_freq(struct drm_device *dev)
82{
83 struct drm_i915_private *dev_priv = dev->dev_private;
84 u32 tmp;
85
86 tmp = I915_READ(CLKCFG);
87
88 switch (tmp & CLKCFG_FSB_MASK) {
89 case CLKCFG_FSB_533:
90 dev_priv->fsb_freq = 533; /* 133*4 */
91 break;
92 case CLKCFG_FSB_800:
93 dev_priv->fsb_freq = 800; /* 200*4 */
94 break;
95 case CLKCFG_FSB_667:
96 dev_priv->fsb_freq = 667; /* 167*4 */
97 break;
98 case CLKCFG_FSB_400:
99 dev_priv->fsb_freq = 400; /* 100*4 */
100 break;
101 }
102
103 switch (tmp & CLKCFG_MEM_MASK) {
104 case CLKCFG_MEM_533:
105 dev_priv->mem_freq = 533;
106 break;
107 case CLKCFG_MEM_667:
108 dev_priv->mem_freq = 667;
109 break;
110 case CLKCFG_MEM_800:
111 dev_priv->mem_freq = 800;
112 break;
113 }
114
115 /* detect pineview DDR3 setting */
116 tmp = I915_READ(CSHRDDR3CTL);
117 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
118}
119
120static void i915_ironlake_get_mem_freq(struct drm_device *dev)
121{
122 struct drm_i915_private *dev_priv = dev->dev_private;
123 u16 ddrpll, csipll;
124
125 ddrpll = I915_READ16(DDRMPLL1);
126 csipll = I915_READ16(CSIPLL0);
127
128 switch (ddrpll & 0xff) {
129 case 0xc:
130 dev_priv->mem_freq = 800;
131 break;
132 case 0x10:
133 dev_priv->mem_freq = 1066;
134 break;
135 case 0x14:
136 dev_priv->mem_freq = 1333;
137 break;
138 case 0x18:
139 dev_priv->mem_freq = 1600;
140 break;
141 default:
142 DRM_DEBUG_DRIVER("unknown memory frequency 0x%02x\n",
143 ddrpll & 0xff);
144 dev_priv->mem_freq = 0;
145 break;
146 }
147
148 dev_priv->ips.r_t = dev_priv->mem_freq;
149
150 switch (csipll & 0x3ff) {
151 case 0x00c:
152 dev_priv->fsb_freq = 3200;
153 break;
154 case 0x00e:
155 dev_priv->fsb_freq = 3733;
156 break;
157 case 0x010:
158 dev_priv->fsb_freq = 4266;
159 break;
160 case 0x012:
161 dev_priv->fsb_freq = 4800;
162 break;
163 case 0x014:
164 dev_priv->fsb_freq = 5333;
165 break;
166 case 0x016:
167 dev_priv->fsb_freq = 5866;
168 break;
169 case 0x018:
170 dev_priv->fsb_freq = 6400;
171 break;
172 default:
173 DRM_DEBUG_DRIVER("unknown fsb frequency 0x%04x\n",
174 csipll & 0x3ff);
175 dev_priv->fsb_freq = 0;
176 break;
177 }
178
179 if (dev_priv->fsb_freq == 3200) {
180 dev_priv->ips.c_m = 0;
181 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
182 dev_priv->ips.c_m = 1;
183 } else {
184 dev_priv->ips.c_m = 2;
185 }
186}
187
188static const struct cxsr_latency cxsr_latency_table[] = {
189 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
190 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
191 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
192 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
193 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
194
195 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
196 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
197 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
198 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
199 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
200
201 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
202 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
203 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
204 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
205 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
206
207 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
208 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
209 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
210 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
211 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
212
213 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
214 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
215 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
216 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
217 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
218
219 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
220 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
221 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
222 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
223 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
224};
225
226static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
227 int is_ddr3,
228 int fsb,
229 int mem)
230{
231 const struct cxsr_latency *latency;
232 int i;
233
234 if (fsb == 0 || mem == 0)
235 return NULL;
236
237 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
238 latency = &cxsr_latency_table[i];
239 if (is_desktop == latency->is_desktop &&
240 is_ddr3 == latency->is_ddr3 &&
241 fsb == latency->fsb_freq && mem == latency->mem_freq)
242 return latency;
243 }
244
245 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
246
247 return NULL;
248}
249
250static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
251{
252 u32 val;
253
254 mutex_lock(&dev_priv->rps.hw_lock);
255
256 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
257 if (enable)
258 val &= ~FORCE_DDR_HIGH_FREQ;
259 else
260 val |= FORCE_DDR_HIGH_FREQ;
261 val &= ~FORCE_DDR_LOW_FREQ;
262 val |= FORCE_DDR_FREQ_REQ_ACK;
263 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
264
265 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
266 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
267 DRM_ERROR("timed out waiting for Punit DDR DVFS request\n");
268
269 mutex_unlock(&dev_priv->rps.hw_lock);
270}
271
272static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
273{
274 u32 val;
275
276 mutex_lock(&dev_priv->rps.hw_lock);
277
278 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
279 if (enable)
280 val |= DSP_MAXFIFO_PM5_ENABLE;
281 else
282 val &= ~DSP_MAXFIFO_PM5_ENABLE;
283 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
284
285 mutex_unlock(&dev_priv->rps.hw_lock);
286}
287
288#define FW_WM(value, plane) \
289 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
290
291void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
292{
293 struct drm_device *dev = dev_priv->dev;
294 u32 val;
295
296 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
297 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
298 POSTING_READ(FW_BLC_SELF_VLV);
299 dev_priv->wm.vlv.cxsr = enable;
300 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
301 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
302 POSTING_READ(FW_BLC_SELF);
303 } else if (IS_PINEVIEW(dev)) {
304 val = I915_READ(DSPFW3) & ~PINEVIEW_SELF_REFRESH_EN;
305 val |= enable ? PINEVIEW_SELF_REFRESH_EN : 0;
306 I915_WRITE(DSPFW3, val);
307 POSTING_READ(DSPFW3);
308 } else if (IS_I945G(dev) || IS_I945GM(dev)) {
309 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
310 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
311 I915_WRITE(FW_BLC_SELF, val);
312 POSTING_READ(FW_BLC_SELF);
313 } else if (IS_I915GM(dev)) {
314 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
315 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
316 I915_WRITE(INSTPM, val);
317 POSTING_READ(INSTPM);
318 } else {
319 return;
320 }
321
322 DRM_DEBUG_KMS("memory self-refresh is %s\n",
323 enable ? "enabled" : "disabled");
324}
325
326
327/*
328 * Latency for FIFO fetches is dependent on several factors:
329 * - memory configuration (speed, channels)
330 * - chipset
331 * - current MCH state
332 * It can be fairly high in some situations, so here we assume a fairly
333 * pessimal value. It's a tradeoff between extra memory fetches (if we
334 * set this value too high, the FIFO will fetch frequently to stay full)
335 * and power consumption (set it too low to save power and we might see
336 * FIFO underruns and display "flicker").
337 *
338 * A value of 5us seems to be a good balance; safe for very low end
339 * platforms but not overly aggressive on lower latency configs.
340 */
341static const int pessimal_latency_ns = 5000;
342
343#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
344 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
345
346static int vlv_get_fifo_size(struct drm_device *dev,
347 enum pipe pipe, int plane)
348{
349 struct drm_i915_private *dev_priv = dev->dev_private;
350 int sprite0_start, sprite1_start, size;
351
352 switch (pipe) {
353 uint32_t dsparb, dsparb2, dsparb3;
354 case PIPE_A:
355 dsparb = I915_READ(DSPARB);
356 dsparb2 = I915_READ(DSPARB2);
357 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
358 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
359 break;
360 case PIPE_B:
361 dsparb = I915_READ(DSPARB);
362 dsparb2 = I915_READ(DSPARB2);
363 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
364 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
365 break;
366 case PIPE_C:
367 dsparb2 = I915_READ(DSPARB2);
368 dsparb3 = I915_READ(DSPARB3);
369 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
370 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
371 break;
372 default:
373 return 0;
374 }
375
376 switch (plane) {
377 case 0:
378 size = sprite0_start;
379 break;
380 case 1:
381 size = sprite1_start - sprite0_start;
382 break;
383 case 2:
384 size = 512 - 1 - sprite1_start;
385 break;
386 default:
387 return 0;
388 }
389
390 DRM_DEBUG_KMS("Pipe %c %s %c FIFO size: %d\n",
391 pipe_name(pipe), plane == 0 ? "primary" : "sprite",
392 plane == 0 ? plane_name(pipe) : sprite_name(pipe, plane - 1),
393 size);
394
395 return size;
396}
397
398static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
399{
400 struct drm_i915_private *dev_priv = dev->dev_private;
401 uint32_t dsparb = I915_READ(DSPARB);
402 int size;
403
404 size = dsparb & 0x7f;
405 if (plane)
406 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
407
408 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
409 plane ? "B" : "A", size);
410
411 return size;
412}
413
414static int i830_get_fifo_size(struct drm_device *dev, int plane)
415{
416 struct drm_i915_private *dev_priv = dev->dev_private;
417 uint32_t dsparb = I915_READ(DSPARB);
418 int size;
419
420 size = dsparb & 0x1ff;
421 if (plane)
422 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
423 size >>= 1; /* Convert to cachelines */
424
425 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
426 plane ? "B" : "A", size);
427
428 return size;
429}
430
431static int i845_get_fifo_size(struct drm_device *dev, int plane)
432{
433 struct drm_i915_private *dev_priv = dev->dev_private;
434 uint32_t dsparb = I915_READ(DSPARB);
435 int size;
436
437 size = dsparb & 0x7f;
438 size >>= 2; /* Convert to cachelines */
439
440 DRM_DEBUG_KMS("FIFO size - (0x%08x) %s: %d\n", dsparb,
441 plane ? "B" : "A",
442 size);
443
444 return size;
445}
446
447/* Pineview has different values for various configs */
448static const struct intel_watermark_params pineview_display_wm = {
449 .fifo_size = PINEVIEW_DISPLAY_FIFO,
450 .max_wm = PINEVIEW_MAX_WM,
451 .default_wm = PINEVIEW_DFT_WM,
452 .guard_size = PINEVIEW_GUARD_WM,
453 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
454};
455static const struct intel_watermark_params pineview_display_hplloff_wm = {
456 .fifo_size = PINEVIEW_DISPLAY_FIFO,
457 .max_wm = PINEVIEW_MAX_WM,
458 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
459 .guard_size = PINEVIEW_GUARD_WM,
460 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
461};
462static const struct intel_watermark_params pineview_cursor_wm = {
463 .fifo_size = PINEVIEW_CURSOR_FIFO,
464 .max_wm = PINEVIEW_CURSOR_MAX_WM,
465 .default_wm = PINEVIEW_CURSOR_DFT_WM,
466 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
467 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
468};
469static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
470 .fifo_size = PINEVIEW_CURSOR_FIFO,
471 .max_wm = PINEVIEW_CURSOR_MAX_WM,
472 .default_wm = PINEVIEW_CURSOR_DFT_WM,
473 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
474 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
475};
476static const struct intel_watermark_params g4x_wm_info = {
477 .fifo_size = G4X_FIFO_SIZE,
478 .max_wm = G4X_MAX_WM,
479 .default_wm = G4X_MAX_WM,
480 .guard_size = 2,
481 .cacheline_size = G4X_FIFO_LINE_SIZE,
482};
483static const struct intel_watermark_params g4x_cursor_wm_info = {
484 .fifo_size = I965_CURSOR_FIFO,
485 .max_wm = I965_CURSOR_MAX_WM,
486 .default_wm = I965_CURSOR_DFT_WM,
487 .guard_size = 2,
488 .cacheline_size = G4X_FIFO_LINE_SIZE,
489};
490static const struct intel_watermark_params valleyview_wm_info = {
491 .fifo_size = VALLEYVIEW_FIFO_SIZE,
492 .max_wm = VALLEYVIEW_MAX_WM,
493 .default_wm = VALLEYVIEW_MAX_WM,
494 .guard_size = 2,
495 .cacheline_size = G4X_FIFO_LINE_SIZE,
496};
497static const struct intel_watermark_params valleyview_cursor_wm_info = {
498 .fifo_size = I965_CURSOR_FIFO,
499 .max_wm = VALLEYVIEW_CURSOR_MAX_WM,
500 .default_wm = I965_CURSOR_DFT_WM,
501 .guard_size = 2,
502 .cacheline_size = G4X_FIFO_LINE_SIZE,
503};
504static const struct intel_watermark_params i965_cursor_wm_info = {
505 .fifo_size = I965_CURSOR_FIFO,
506 .max_wm = I965_CURSOR_MAX_WM,
507 .default_wm = I965_CURSOR_DFT_WM,
508 .guard_size = 2,
509 .cacheline_size = I915_FIFO_LINE_SIZE,
510};
511static const struct intel_watermark_params i945_wm_info = {
512 .fifo_size = I945_FIFO_SIZE,
513 .max_wm = I915_MAX_WM,
514 .default_wm = 1,
515 .guard_size = 2,
516 .cacheline_size = I915_FIFO_LINE_SIZE,
517};
518static const struct intel_watermark_params i915_wm_info = {
519 .fifo_size = I915_FIFO_SIZE,
520 .max_wm = I915_MAX_WM,
521 .default_wm = 1,
522 .guard_size = 2,
523 .cacheline_size = I915_FIFO_LINE_SIZE,
524};
525static const struct intel_watermark_params i830_a_wm_info = {
526 .fifo_size = I855GM_FIFO_SIZE,
527 .max_wm = I915_MAX_WM,
528 .default_wm = 1,
529 .guard_size = 2,
530 .cacheline_size = I830_FIFO_LINE_SIZE,
531};
532static const struct intel_watermark_params i830_bc_wm_info = {
533 .fifo_size = I855GM_FIFO_SIZE,
534 .max_wm = I915_MAX_WM/2,
535 .default_wm = 1,
536 .guard_size = 2,
537 .cacheline_size = I830_FIFO_LINE_SIZE,
538};
539static const struct intel_watermark_params i845_wm_info = {
540 .fifo_size = I830_FIFO_SIZE,
541 .max_wm = I915_MAX_WM,
542 .default_wm = 1,
543 .guard_size = 2,
544 .cacheline_size = I830_FIFO_LINE_SIZE,
545};
546
547/**
548 * intel_calculate_wm - calculate watermark level
549 * @clock_in_khz: pixel clock
550 * @wm: chip FIFO params
551 * @cpp: bytes per pixel
552 * @latency_ns: memory latency for the platform
553 *
554 * Calculate the watermark level (the level at which the display plane will
555 * start fetching from memory again). Each chip has a different display
556 * FIFO size and allocation, so the caller needs to figure that out and pass
557 * in the correct intel_watermark_params structure.
558 *
559 * As the pixel clock runs, the FIFO will be drained at a rate that depends
560 * on the pixel size. When it reaches the watermark level, it'll start
561 * fetching FIFO line sized based chunks from memory until the FIFO fills
562 * past the watermark point. If the FIFO drains completely, a FIFO underrun
563 * will occur, and a display engine hang could result.
564 */
565static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
566 const struct intel_watermark_params *wm,
567 int fifo_size, int cpp,
568 unsigned long latency_ns)
569{
570 long entries_required, wm_size;
571
572 /*
573 * Note: we need to make sure we don't overflow for various clock &
574 * latency values.
575 * clocks go from a few thousand to several hundred thousand.
576 * latency is usually a few thousand
577 */
578 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
579 1000;
580 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
581
582 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
583
584 wm_size = fifo_size - (entries_required + wm->guard_size);
585
586 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
587
588 /* Don't promote wm_size to unsigned... */
589 if (wm_size > (long)wm->max_wm)
590 wm_size = wm->max_wm;
591 if (wm_size <= 0)
592 wm_size = wm->default_wm;
593
594 /*
595 * Bspec seems to indicate that the value shouldn't be lower than
596 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
597 * Lets go for 8 which is the burst size since certain platforms
598 * already use a hardcoded 8 (which is what the spec says should be
599 * done).
600 */
601 if (wm_size <= 8)
602 wm_size = 8;
603
604 return wm_size;
605}
606
607static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
608{
609 struct drm_crtc *crtc, *enabled = NULL;
610
611 for_each_crtc(dev, crtc) {
612 if (intel_crtc_active(crtc)) {
613 if (enabled)
614 return NULL;
615 enabled = crtc;
616 }
617 }
618
619 return enabled;
620}
621
622static void pineview_update_wm(struct drm_crtc *unused_crtc)
623{
624 struct drm_device *dev = unused_crtc->dev;
625 struct drm_i915_private *dev_priv = dev->dev_private;
626 struct drm_crtc *crtc;
627 const struct cxsr_latency *latency;
628 u32 reg;
629 unsigned long wm;
630
631 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
632 dev_priv->fsb_freq, dev_priv->mem_freq);
633 if (!latency) {
634 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
635 intel_set_memory_cxsr(dev_priv, false);
636 return;
637 }
638
639 crtc = single_enabled_crtc(dev);
640 if (crtc) {
641 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
642 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
643 int clock = adjusted_mode->crtc_clock;
644
645 /* Display SR */
646 wm = intel_calculate_wm(clock, &pineview_display_wm,
647 pineview_display_wm.fifo_size,
648 cpp, latency->display_sr);
649 reg = I915_READ(DSPFW1);
650 reg &= ~DSPFW_SR_MASK;
651 reg |= FW_WM(wm, SR);
652 I915_WRITE(DSPFW1, reg);
653 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
654
655 /* cursor SR */
656 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
657 pineview_display_wm.fifo_size,
658 cpp, latency->cursor_sr);
659 reg = I915_READ(DSPFW3);
660 reg &= ~DSPFW_CURSOR_SR_MASK;
661 reg |= FW_WM(wm, CURSOR_SR);
662 I915_WRITE(DSPFW3, reg);
663
664 /* Display HPLL off SR */
665 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
666 pineview_display_hplloff_wm.fifo_size,
667 cpp, latency->display_hpll_disable);
668 reg = I915_READ(DSPFW3);
669 reg &= ~DSPFW_HPLL_SR_MASK;
670 reg |= FW_WM(wm, HPLL_SR);
671 I915_WRITE(DSPFW3, reg);
672
673 /* cursor HPLL off SR */
674 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
675 pineview_display_hplloff_wm.fifo_size,
676 cpp, latency->cursor_hpll_disable);
677 reg = I915_READ(DSPFW3);
678 reg &= ~DSPFW_HPLL_CURSOR_MASK;
679 reg |= FW_WM(wm, HPLL_CURSOR);
680 I915_WRITE(DSPFW3, reg);
681 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
682
683 intel_set_memory_cxsr(dev_priv, true);
684 } else {
685 intel_set_memory_cxsr(dev_priv, false);
686 }
687}
688
689static bool g4x_compute_wm0(struct drm_device *dev,
690 int plane,
691 const struct intel_watermark_params *display,
692 int display_latency_ns,
693 const struct intel_watermark_params *cursor,
694 int cursor_latency_ns,
695 int *plane_wm,
696 int *cursor_wm)
697{
698 struct drm_crtc *crtc;
699 const struct drm_display_mode *adjusted_mode;
700 int htotal, hdisplay, clock, cpp;
701 int line_time_us, line_count;
702 int entries, tlb_miss;
703
704 crtc = intel_get_crtc_for_plane(dev, plane);
705 if (!intel_crtc_active(crtc)) {
706 *cursor_wm = cursor->guard_size;
707 *plane_wm = display->guard_size;
708 return false;
709 }
710
711 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
712 clock = adjusted_mode->crtc_clock;
713 htotal = adjusted_mode->crtc_htotal;
714 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
715 cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
716
717 /* Use the small buffer method to calculate plane watermark */
718 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
719 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
720 if (tlb_miss > 0)
721 entries += tlb_miss;
722 entries = DIV_ROUND_UP(entries, display->cacheline_size);
723 *plane_wm = entries + display->guard_size;
724 if (*plane_wm > (int)display->max_wm)
725 *plane_wm = display->max_wm;
726
727 /* Use the large buffer method to calculate cursor watermark */
728 line_time_us = max(htotal * 1000 / clock, 1);
729 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
730 entries = line_count * crtc->cursor->state->crtc_w * cpp;
731 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
732 if (tlb_miss > 0)
733 entries += tlb_miss;
734 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
735 *cursor_wm = entries + cursor->guard_size;
736 if (*cursor_wm > (int)cursor->max_wm)
737 *cursor_wm = (int)cursor->max_wm;
738
739 return true;
740}
741
742/*
743 * Check the wm result.
744 *
745 * If any calculated watermark values is larger than the maximum value that
746 * can be programmed into the associated watermark register, that watermark
747 * must be disabled.
748 */
749static bool g4x_check_srwm(struct drm_device *dev,
750 int display_wm, int cursor_wm,
751 const struct intel_watermark_params *display,
752 const struct intel_watermark_params *cursor)
753{
754 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
755 display_wm, cursor_wm);
756
757 if (display_wm > display->max_wm) {
758 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
759 display_wm, display->max_wm);
760 return false;
761 }
762
763 if (cursor_wm > cursor->max_wm) {
764 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
765 cursor_wm, cursor->max_wm);
766 return false;
767 }
768
769 if (!(display_wm || cursor_wm)) {
770 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
771 return false;
772 }
773
774 return true;
775}
776
777static bool g4x_compute_srwm(struct drm_device *dev,
778 int plane,
779 int latency_ns,
780 const struct intel_watermark_params *display,
781 const struct intel_watermark_params *cursor,
782 int *display_wm, int *cursor_wm)
783{
784 struct drm_crtc *crtc;
785 const struct drm_display_mode *adjusted_mode;
786 int hdisplay, htotal, cpp, clock;
787 unsigned long line_time_us;
788 int line_count, line_size;
789 int small, large;
790 int entries;
791
792 if (!latency_ns) {
793 *display_wm = *cursor_wm = 0;
794 return false;
795 }
796
797 crtc = intel_get_crtc_for_plane(dev, plane);
798 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
799 clock = adjusted_mode->crtc_clock;
800 htotal = adjusted_mode->crtc_htotal;
801 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
802 cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
803
804 line_time_us = max(htotal * 1000 / clock, 1);
805 line_count = (latency_ns / line_time_us + 1000) / 1000;
806 line_size = hdisplay * cpp;
807
808 /* Use the minimum of the small and large buffer method for primary */
809 small = ((clock * cpp / 1000) * latency_ns) / 1000;
810 large = line_count * line_size;
811
812 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
813 *display_wm = entries + display->guard_size;
814
815 /* calculate the self-refresh watermark for display cursor */
816 entries = line_count * cpp * crtc->cursor->state->crtc_w;
817 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
818 *cursor_wm = entries + cursor->guard_size;
819
820 return g4x_check_srwm(dev,
821 *display_wm, *cursor_wm,
822 display, cursor);
823}
824
825#define FW_WM_VLV(value, plane) \
826 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
827
828static void vlv_write_wm_values(struct intel_crtc *crtc,
829 const struct vlv_wm_values *wm)
830{
831 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
832 enum pipe pipe = crtc->pipe;
833
834 I915_WRITE(VLV_DDL(pipe),
835 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
836 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
837 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
838 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
839
840 I915_WRITE(DSPFW1,
841 FW_WM(wm->sr.plane, SR) |
842 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
843 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
844 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
845 I915_WRITE(DSPFW2,
846 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
847 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
848 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
849 I915_WRITE(DSPFW3,
850 FW_WM(wm->sr.cursor, CURSOR_SR));
851
852 if (IS_CHERRYVIEW(dev_priv)) {
853 I915_WRITE(DSPFW7_CHV,
854 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
855 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
856 I915_WRITE(DSPFW8_CHV,
857 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
858 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
859 I915_WRITE(DSPFW9_CHV,
860 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
861 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
862 I915_WRITE(DSPHOWM,
863 FW_WM(wm->sr.plane >> 9, SR_HI) |
864 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
865 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
866 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
867 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
868 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
869 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
870 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
871 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
872 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
873 } else {
874 I915_WRITE(DSPFW7,
875 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
876 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
877 I915_WRITE(DSPHOWM,
878 FW_WM(wm->sr.plane >> 9, SR_HI) |
879 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
880 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
881 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
882 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
883 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
884 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
885 }
886
887 /* zero (unused) WM1 watermarks */
888 I915_WRITE(DSPFW4, 0);
889 I915_WRITE(DSPFW5, 0);
890 I915_WRITE(DSPFW6, 0);
891 I915_WRITE(DSPHOWM1, 0);
892
893 POSTING_READ(DSPFW1);
894}
895
896#undef FW_WM_VLV
897
898enum vlv_wm_level {
899 VLV_WM_LEVEL_PM2,
900 VLV_WM_LEVEL_PM5,
901 VLV_WM_LEVEL_DDR_DVFS,
902};
903
904/* latency must be in 0.1us units. */
905static unsigned int vlv_wm_method2(unsigned int pixel_rate,
906 unsigned int pipe_htotal,
907 unsigned int horiz_pixels,
908 unsigned int cpp,
909 unsigned int latency)
910{
911 unsigned int ret;
912
913 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
914 ret = (ret + 1) * horiz_pixels * cpp;
915 ret = DIV_ROUND_UP(ret, 64);
916
917 return ret;
918}
919
920static void vlv_setup_wm_latency(struct drm_device *dev)
921{
922 struct drm_i915_private *dev_priv = dev->dev_private;
923
924 /* all latencies in usec */
925 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
926
927 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
928
929 if (IS_CHERRYVIEW(dev_priv)) {
930 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
931 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
932
933 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
934 }
935}
936
937static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
938 struct intel_crtc *crtc,
939 const struct intel_plane_state *state,
940 int level)
941{
942 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
943 int clock, htotal, cpp, width, wm;
944
945 if (dev_priv->wm.pri_latency[level] == 0)
946 return USHRT_MAX;
947
948 if (!state->visible)
949 return 0;
950
951 cpp = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
952 clock = crtc->config->base.adjusted_mode.crtc_clock;
953 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
954 width = crtc->config->pipe_src_w;
955 if (WARN_ON(htotal == 0))
956 htotal = 1;
957
958 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
959 /*
960 * FIXME the formula gives values that are
961 * too big for the cursor FIFO, and hence we
962 * would never be able to use cursors. For
963 * now just hardcode the watermark.
964 */
965 wm = 63;
966 } else {
967 wm = vlv_wm_method2(clock, htotal, width, cpp,
968 dev_priv->wm.pri_latency[level] * 10);
969 }
970
971 return min_t(int, wm, USHRT_MAX);
972}
973
974static void vlv_compute_fifo(struct intel_crtc *crtc)
975{
976 struct drm_device *dev = crtc->base.dev;
977 struct vlv_wm_state *wm_state = &crtc->wm_state;
978 struct intel_plane *plane;
979 unsigned int total_rate = 0;
980 const int fifo_size = 512 - 1;
981 int fifo_extra, fifo_left = fifo_size;
982
983 for_each_intel_plane_on_crtc(dev, crtc, plane) {
984 struct intel_plane_state *state =
985 to_intel_plane_state(plane->base.state);
986
987 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
988 continue;
989
990 if (state->visible) {
991 wm_state->num_active_planes++;
992 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
993 }
994 }
995
996 for_each_intel_plane_on_crtc(dev, crtc, plane) {
997 struct intel_plane_state *state =
998 to_intel_plane_state(plane->base.state);
999 unsigned int rate;
1000
1001 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1002 plane->wm.fifo_size = 63;
1003 continue;
1004 }
1005
1006 if (!state->visible) {
1007 plane->wm.fifo_size = 0;
1008 continue;
1009 }
1010
1011 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
1012 plane->wm.fifo_size = fifo_size * rate / total_rate;
1013 fifo_left -= plane->wm.fifo_size;
1014 }
1015
1016 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1017
1018 /* spread the remainder evenly */
1019 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1020 int plane_extra;
1021
1022 if (fifo_left == 0)
1023 break;
1024
1025 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1026 continue;
1027
1028 /* give it all to the first plane if none are active */
1029 if (plane->wm.fifo_size == 0 &&
1030 wm_state->num_active_planes)
1031 continue;
1032
1033 plane_extra = min(fifo_extra, fifo_left);
1034 plane->wm.fifo_size += plane_extra;
1035 fifo_left -= plane_extra;
1036 }
1037
1038 WARN_ON(fifo_left != 0);
1039}
1040
1041static void vlv_invert_wms(struct intel_crtc *crtc)
1042{
1043 struct vlv_wm_state *wm_state = &crtc->wm_state;
1044 int level;
1045
1046 for (level = 0; level < wm_state->num_levels; level++) {
1047 struct drm_device *dev = crtc->base.dev;
1048 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1049 struct intel_plane *plane;
1050
1051 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1052 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1053
1054 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1055 switch (plane->base.type) {
1056 int sprite;
1057 case DRM_PLANE_TYPE_CURSOR:
1058 wm_state->wm[level].cursor = plane->wm.fifo_size -
1059 wm_state->wm[level].cursor;
1060 break;
1061 case DRM_PLANE_TYPE_PRIMARY:
1062 wm_state->wm[level].primary = plane->wm.fifo_size -
1063 wm_state->wm[level].primary;
1064 break;
1065 case DRM_PLANE_TYPE_OVERLAY:
1066 sprite = plane->plane;
1067 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1068 wm_state->wm[level].sprite[sprite];
1069 break;
1070 }
1071 }
1072 }
1073}
1074
1075static void vlv_compute_wm(struct intel_crtc *crtc)
1076{
1077 struct drm_device *dev = crtc->base.dev;
1078 struct vlv_wm_state *wm_state = &crtc->wm_state;
1079 struct intel_plane *plane;
1080 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1081 int level;
1082
1083 memset(wm_state, 0, sizeof(*wm_state));
1084
1085 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
1086 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
1087
1088 wm_state->num_active_planes = 0;
1089
1090 vlv_compute_fifo(crtc);
1091
1092 if (wm_state->num_active_planes != 1)
1093 wm_state->cxsr = false;
1094
1095 if (wm_state->cxsr) {
1096 for (level = 0; level < wm_state->num_levels; level++) {
1097 wm_state->sr[level].plane = sr_fifo_size;
1098 wm_state->sr[level].cursor = 63;
1099 }
1100 }
1101
1102 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1103 struct intel_plane_state *state =
1104 to_intel_plane_state(plane->base.state);
1105
1106 if (!state->visible)
1107 continue;
1108
1109 /* normal watermarks */
1110 for (level = 0; level < wm_state->num_levels; level++) {
1111 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1112 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1113
1114 /* hack */
1115 if (WARN_ON(level == 0 && wm > max_wm))
1116 wm = max_wm;
1117
1118 if (wm > plane->wm.fifo_size)
1119 break;
1120
1121 switch (plane->base.type) {
1122 int sprite;
1123 case DRM_PLANE_TYPE_CURSOR:
1124 wm_state->wm[level].cursor = wm;
1125 break;
1126 case DRM_PLANE_TYPE_PRIMARY:
1127 wm_state->wm[level].primary = wm;
1128 break;
1129 case DRM_PLANE_TYPE_OVERLAY:
1130 sprite = plane->plane;
1131 wm_state->wm[level].sprite[sprite] = wm;
1132 break;
1133 }
1134 }
1135
1136 wm_state->num_levels = level;
1137
1138 if (!wm_state->cxsr)
1139 continue;
1140
1141 /* maxfifo watermarks */
1142 switch (plane->base.type) {
1143 int sprite, level;
1144 case DRM_PLANE_TYPE_CURSOR:
1145 for (level = 0; level < wm_state->num_levels; level++)
1146 wm_state->sr[level].cursor =
1147 wm_state->wm[level].cursor;
1148 break;
1149 case DRM_PLANE_TYPE_PRIMARY:
1150 for (level = 0; level < wm_state->num_levels; level++)
1151 wm_state->sr[level].plane =
1152 min(wm_state->sr[level].plane,
1153 wm_state->wm[level].primary);
1154 break;
1155 case DRM_PLANE_TYPE_OVERLAY:
1156 sprite = plane->plane;
1157 for (level = 0; level < wm_state->num_levels; level++)
1158 wm_state->sr[level].plane =
1159 min(wm_state->sr[level].plane,
1160 wm_state->wm[level].sprite[sprite]);
1161 break;
1162 }
1163 }
1164
1165 /* clear any (partially) filled invalid levels */
1166 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
1167 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1168 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1169 }
1170
1171 vlv_invert_wms(crtc);
1172}
1173
1174#define VLV_FIFO(plane, value) \
1175 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1176
1177static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1178{
1179 struct drm_device *dev = crtc->base.dev;
1180 struct drm_i915_private *dev_priv = to_i915(dev);
1181 struct intel_plane *plane;
1182 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1183
1184 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1185 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1186 WARN_ON(plane->wm.fifo_size != 63);
1187 continue;
1188 }
1189
1190 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1191 sprite0_start = plane->wm.fifo_size;
1192 else if (plane->plane == 0)
1193 sprite1_start = sprite0_start + plane->wm.fifo_size;
1194 else
1195 fifo_size = sprite1_start + plane->wm.fifo_size;
1196 }
1197
1198 WARN_ON(fifo_size != 512 - 1);
1199
1200 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1201 pipe_name(crtc->pipe), sprite0_start,
1202 sprite1_start, fifo_size);
1203
1204 switch (crtc->pipe) {
1205 uint32_t dsparb, dsparb2, dsparb3;
1206 case PIPE_A:
1207 dsparb = I915_READ(DSPARB);
1208 dsparb2 = I915_READ(DSPARB2);
1209
1210 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1211 VLV_FIFO(SPRITEB, 0xff));
1212 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1213 VLV_FIFO(SPRITEB, sprite1_start));
1214
1215 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1216 VLV_FIFO(SPRITEB_HI, 0x1));
1217 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1218 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1219
1220 I915_WRITE(DSPARB, dsparb);
1221 I915_WRITE(DSPARB2, dsparb2);
1222 break;
1223 case PIPE_B:
1224 dsparb = I915_READ(DSPARB);
1225 dsparb2 = I915_READ(DSPARB2);
1226
1227 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1228 VLV_FIFO(SPRITED, 0xff));
1229 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1230 VLV_FIFO(SPRITED, sprite1_start));
1231
1232 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1233 VLV_FIFO(SPRITED_HI, 0xff));
1234 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1235 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1236
1237 I915_WRITE(DSPARB, dsparb);
1238 I915_WRITE(DSPARB2, dsparb2);
1239 break;
1240 case PIPE_C:
1241 dsparb3 = I915_READ(DSPARB3);
1242 dsparb2 = I915_READ(DSPARB2);
1243
1244 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1245 VLV_FIFO(SPRITEF, 0xff));
1246 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1247 VLV_FIFO(SPRITEF, sprite1_start));
1248
1249 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1250 VLV_FIFO(SPRITEF_HI, 0xff));
1251 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1252 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1253
1254 I915_WRITE(DSPARB3, dsparb3);
1255 I915_WRITE(DSPARB2, dsparb2);
1256 break;
1257 default:
1258 break;
1259 }
1260}
1261
1262#undef VLV_FIFO
1263
1264static void vlv_merge_wm(struct drm_device *dev,
1265 struct vlv_wm_values *wm)
1266{
1267 struct intel_crtc *crtc;
1268 int num_active_crtcs = 0;
1269
1270 wm->level = to_i915(dev)->wm.max_level;
1271 wm->cxsr = true;
1272
1273 for_each_intel_crtc(dev, crtc) {
1274 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1275
1276 if (!crtc->active)
1277 continue;
1278
1279 if (!wm_state->cxsr)
1280 wm->cxsr = false;
1281
1282 num_active_crtcs++;
1283 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1284 }
1285
1286 if (num_active_crtcs != 1)
1287 wm->cxsr = false;
1288
1289 if (num_active_crtcs > 1)
1290 wm->level = VLV_WM_LEVEL_PM2;
1291
1292 for_each_intel_crtc(dev, crtc) {
1293 struct vlv_wm_state *wm_state = &crtc->wm_state;
1294 enum pipe pipe = crtc->pipe;
1295
1296 if (!crtc->active)
1297 continue;
1298
1299 wm->pipe[pipe] = wm_state->wm[wm->level];
1300 if (wm->cxsr)
1301 wm->sr = wm_state->sr[wm->level];
1302
1303 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1304 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1305 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1306 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1307 }
1308}
1309
1310static void vlv_update_wm(struct drm_crtc *crtc)
1311{
1312 struct drm_device *dev = crtc->dev;
1313 struct drm_i915_private *dev_priv = dev->dev_private;
1314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1315 enum pipe pipe = intel_crtc->pipe;
1316 struct vlv_wm_values wm = {};
1317
1318 vlv_compute_wm(intel_crtc);
1319 vlv_merge_wm(dev, &wm);
1320
1321 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1322 /* FIXME should be part of crtc atomic commit */
1323 vlv_pipe_set_fifo_size(intel_crtc);
1324 return;
1325 }
1326
1327 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1328 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1329 chv_set_memory_dvfs(dev_priv, false);
1330
1331 if (wm.level < VLV_WM_LEVEL_PM5 &&
1332 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1333 chv_set_memory_pm5(dev_priv, false);
1334
1335 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
1336 intel_set_memory_cxsr(dev_priv, false);
1337
1338 /* FIXME should be part of crtc atomic commit */
1339 vlv_pipe_set_fifo_size(intel_crtc);
1340
1341 vlv_write_wm_values(intel_crtc, &wm);
1342
1343 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1344 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1345 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1346 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1347 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1348
1349 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
1350 intel_set_memory_cxsr(dev_priv, true);
1351
1352 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1353 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1354 chv_set_memory_pm5(dev_priv, true);
1355
1356 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1357 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1358 chv_set_memory_dvfs(dev_priv, true);
1359
1360 dev_priv->wm.vlv = wm;
1361}
1362
1363#define single_plane_enabled(mask) is_power_of_2(mask)
1364
1365static void g4x_update_wm(struct drm_crtc *crtc)
1366{
1367 struct drm_device *dev = crtc->dev;
1368 static const int sr_latency_ns = 12000;
1369 struct drm_i915_private *dev_priv = dev->dev_private;
1370 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1371 int plane_sr, cursor_sr;
1372 unsigned int enabled = 0;
1373 bool cxsr_enabled;
1374
1375 if (g4x_compute_wm0(dev, PIPE_A,
1376 &g4x_wm_info, pessimal_latency_ns,
1377 &g4x_cursor_wm_info, pessimal_latency_ns,
1378 &planea_wm, &cursora_wm))
1379 enabled |= 1 << PIPE_A;
1380
1381 if (g4x_compute_wm0(dev, PIPE_B,
1382 &g4x_wm_info, pessimal_latency_ns,
1383 &g4x_cursor_wm_info, pessimal_latency_ns,
1384 &planeb_wm, &cursorb_wm))
1385 enabled |= 1 << PIPE_B;
1386
1387 if (single_plane_enabled(enabled) &&
1388 g4x_compute_srwm(dev, ffs(enabled) - 1,
1389 sr_latency_ns,
1390 &g4x_wm_info,
1391 &g4x_cursor_wm_info,
1392 &plane_sr, &cursor_sr)) {
1393 cxsr_enabled = true;
1394 } else {
1395 cxsr_enabled = false;
1396 intel_set_memory_cxsr(dev_priv, false);
1397 plane_sr = cursor_sr = 0;
1398 }
1399
1400 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1401 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
1402 planea_wm, cursora_wm,
1403 planeb_wm, cursorb_wm,
1404 plane_sr, cursor_sr);
1405
1406 I915_WRITE(DSPFW1,
1407 FW_WM(plane_sr, SR) |
1408 FW_WM(cursorb_wm, CURSORB) |
1409 FW_WM(planeb_wm, PLANEB) |
1410 FW_WM(planea_wm, PLANEA));
1411 I915_WRITE(DSPFW2,
1412 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
1413 FW_WM(cursora_wm, CURSORA));
1414 /* HPLL off in SR has some issues on G4x... disable it */
1415 I915_WRITE(DSPFW3,
1416 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
1417 FW_WM(cursor_sr, CURSOR_SR));
1418
1419 if (cxsr_enabled)
1420 intel_set_memory_cxsr(dev_priv, true);
1421}
1422
1423static void i965_update_wm(struct drm_crtc *unused_crtc)
1424{
1425 struct drm_device *dev = unused_crtc->dev;
1426 struct drm_i915_private *dev_priv = dev->dev_private;
1427 struct drm_crtc *crtc;
1428 int srwm = 1;
1429 int cursor_sr = 16;
1430 bool cxsr_enabled;
1431
1432 /* Calc sr entries for one plane configs */
1433 crtc = single_enabled_crtc(dev);
1434 if (crtc) {
1435 /* self-refresh has much higher latency */
1436 static const int sr_latency_ns = 12000;
1437 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1438 int clock = adjusted_mode->crtc_clock;
1439 int htotal = adjusted_mode->crtc_htotal;
1440 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
1441 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
1442 unsigned long line_time_us;
1443 int entries;
1444
1445 line_time_us = max(htotal * 1000 / clock, 1);
1446
1447 /* Use ns/us then divide to preserve precision */
1448 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1449 cpp * hdisplay;
1450 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1451 srwm = I965_FIFO_SIZE - entries;
1452 if (srwm < 0)
1453 srwm = 1;
1454 srwm &= 0x1ff;
1455 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1456 entries, srwm);
1457
1458 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1459 cpp * crtc->cursor->state->crtc_w;
1460 entries = DIV_ROUND_UP(entries,
1461 i965_cursor_wm_info.cacheline_size);
1462 cursor_sr = i965_cursor_wm_info.fifo_size -
1463 (entries + i965_cursor_wm_info.guard_size);
1464
1465 if (cursor_sr > i965_cursor_wm_info.max_wm)
1466 cursor_sr = i965_cursor_wm_info.max_wm;
1467
1468 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1469 "cursor %d\n", srwm, cursor_sr);
1470
1471 cxsr_enabled = true;
1472 } else {
1473 cxsr_enabled = false;
1474 /* Turn off self refresh if both pipes are enabled */
1475 intel_set_memory_cxsr(dev_priv, false);
1476 }
1477
1478 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1479 srwm);
1480
1481 /* 965 has limitations... */
1482 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1483 FW_WM(8, CURSORB) |
1484 FW_WM(8, PLANEB) |
1485 FW_WM(8, PLANEA));
1486 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1487 FW_WM(8, PLANEC_OLD));
1488 /* update cursor SR watermark */
1489 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
1490
1491 if (cxsr_enabled)
1492 intel_set_memory_cxsr(dev_priv, true);
1493}
1494
1495#undef FW_WM
1496
1497static void i9xx_update_wm(struct drm_crtc *unused_crtc)
1498{
1499 struct drm_device *dev = unused_crtc->dev;
1500 struct drm_i915_private *dev_priv = dev->dev_private;
1501 const struct intel_watermark_params *wm_info;
1502 uint32_t fwater_lo;
1503 uint32_t fwater_hi;
1504 int cwm, srwm = 1;
1505 int fifo_size;
1506 int planea_wm, planeb_wm;
1507 struct drm_crtc *crtc, *enabled = NULL;
1508
1509 if (IS_I945GM(dev))
1510 wm_info = &i945_wm_info;
1511 else if (!IS_GEN2(dev))
1512 wm_info = &i915_wm_info;
1513 else
1514 wm_info = &i830_a_wm_info;
1515
1516 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1517 crtc = intel_get_crtc_for_plane(dev, 0);
1518 if (intel_crtc_active(crtc)) {
1519 const struct drm_display_mode *adjusted_mode;
1520 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
1521 if (IS_GEN2(dev))
1522 cpp = 4;
1523
1524 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1525 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1526 wm_info, fifo_size, cpp,
1527 pessimal_latency_ns);
1528 enabled = crtc;
1529 } else {
1530 planea_wm = fifo_size - wm_info->guard_size;
1531 if (planea_wm > (long)wm_info->max_wm)
1532 planea_wm = wm_info->max_wm;
1533 }
1534
1535 if (IS_GEN2(dev))
1536 wm_info = &i830_bc_wm_info;
1537
1538 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1539 crtc = intel_get_crtc_for_plane(dev, 1);
1540 if (intel_crtc_active(crtc)) {
1541 const struct drm_display_mode *adjusted_mode;
1542 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
1543 if (IS_GEN2(dev))
1544 cpp = 4;
1545
1546 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1547 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1548 wm_info, fifo_size, cpp,
1549 pessimal_latency_ns);
1550 if (enabled == NULL)
1551 enabled = crtc;
1552 else
1553 enabled = NULL;
1554 } else {
1555 planeb_wm = fifo_size - wm_info->guard_size;
1556 if (planeb_wm > (long)wm_info->max_wm)
1557 planeb_wm = wm_info->max_wm;
1558 }
1559
1560 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1561
1562 if (IS_I915GM(dev) && enabled) {
1563 struct drm_i915_gem_object *obj;
1564
1565 obj = intel_fb_obj(enabled->primary->state->fb);
1566
1567 /* self-refresh seems busted with untiled */
1568 if (obj->tiling_mode == I915_TILING_NONE)
1569 enabled = NULL;
1570 }
1571
1572 /*
1573 * Overlay gets an aggressive default since video jitter is bad.
1574 */
1575 cwm = 2;
1576
1577 /* Play safe and disable self-refresh before adjusting watermarks. */
1578 intel_set_memory_cxsr(dev_priv, false);
1579
1580 /* Calc sr entries for one plane configs */
1581 if (HAS_FW_BLC(dev) && enabled) {
1582 /* self-refresh has much higher latency */
1583 static const int sr_latency_ns = 6000;
1584 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(enabled)->config->base.adjusted_mode;
1585 int clock = adjusted_mode->crtc_clock;
1586 int htotal = adjusted_mode->crtc_htotal;
1587 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
1588 int cpp = drm_format_plane_cpp(enabled->primary->state->fb->pixel_format, 0);
1589 unsigned long line_time_us;
1590 int entries;
1591
1592 line_time_us = max(htotal * 1000 / clock, 1);
1593
1594 /* Use ns/us then divide to preserve precision */
1595 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
1596 cpp * hdisplay;
1597 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1598 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1599 srwm = wm_info->fifo_size - entries;
1600 if (srwm < 0)
1601 srwm = 1;
1602
1603 if (IS_I945G(dev) || IS_I945GM(dev))
1604 I915_WRITE(FW_BLC_SELF,
1605 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1606 else if (IS_I915GM(dev))
1607 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1608 }
1609
1610 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1611 planea_wm, planeb_wm, cwm, srwm);
1612
1613 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1614 fwater_hi = (cwm & 0x1f);
1615
1616 /* Set request length to 8 cachelines per fetch */
1617 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1618 fwater_hi = fwater_hi | (1 << 8);
1619
1620 I915_WRITE(FW_BLC, fwater_lo);
1621 I915_WRITE(FW_BLC2, fwater_hi);
1622
1623 if (enabled)
1624 intel_set_memory_cxsr(dev_priv, true);
1625}
1626
1627static void i845_update_wm(struct drm_crtc *unused_crtc)
1628{
1629 struct drm_device *dev = unused_crtc->dev;
1630 struct drm_i915_private *dev_priv = dev->dev_private;
1631 struct drm_crtc *crtc;
1632 const struct drm_display_mode *adjusted_mode;
1633 uint32_t fwater_lo;
1634 int planea_wm;
1635
1636 crtc = single_enabled_crtc(dev);
1637 if (crtc == NULL)
1638 return;
1639
1640 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
1641 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
1642 &i845_wm_info,
1643 dev_priv->display.get_fifo_size(dev, 0),
1644 4, pessimal_latency_ns);
1645 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1646 fwater_lo |= (3<<8) | planea_wm;
1647
1648 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1649
1650 I915_WRITE(FW_BLC, fwater_lo);
1651}
1652
1653uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
1654{
1655 uint32_t pixel_rate;
1656
1657 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
1658
1659 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1660 * adjust the pixel_rate here. */
1661
1662 if (pipe_config->pch_pfit.enabled) {
1663 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
1664 uint32_t pfit_size = pipe_config->pch_pfit.size;
1665
1666 pipe_w = pipe_config->pipe_src_w;
1667 pipe_h = pipe_config->pipe_src_h;
1668
1669 pfit_w = (pfit_size >> 16) & 0xFFFF;
1670 pfit_h = pfit_size & 0xFFFF;
1671 if (pipe_w < pfit_w)
1672 pipe_w = pfit_w;
1673 if (pipe_h < pfit_h)
1674 pipe_h = pfit_h;
1675
1676 if (WARN_ON(!pfit_w || !pfit_h))
1677 return pixel_rate;
1678
1679 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1680 pfit_w * pfit_h);
1681 }
1682
1683 return pixel_rate;
1684}
1685
1686/* latency must be in 0.1us units. */
1687static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
1688{
1689 uint64_t ret;
1690
1691 if (WARN(latency == 0, "Latency value missing\n"))
1692 return UINT_MAX;
1693
1694 ret = (uint64_t) pixel_rate * cpp * latency;
1695 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1696
1697 return ret;
1698}
1699
1700/* latency must be in 0.1us units. */
1701static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
1702 uint32_t horiz_pixels, uint8_t cpp,
1703 uint32_t latency)
1704{
1705 uint32_t ret;
1706
1707 if (WARN(latency == 0, "Latency value missing\n"))
1708 return UINT_MAX;
1709 if (WARN_ON(!pipe_htotal))
1710 return UINT_MAX;
1711
1712 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
1713 ret = (ret + 1) * horiz_pixels * cpp;
1714 ret = DIV_ROUND_UP(ret, 64) + 2;
1715 return ret;
1716}
1717
1718static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
1719 uint8_t cpp)
1720{
1721 /*
1722 * Neither of these should be possible since this function shouldn't be
1723 * called if the CRTC is off or the plane is invisible. But let's be
1724 * extra paranoid to avoid a potential divide-by-zero if we screw up
1725 * elsewhere in the driver.
1726 */
1727 if (WARN_ON(!cpp))
1728 return 0;
1729 if (WARN_ON(!horiz_pixels))
1730 return 0;
1731
1732 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
1733}
1734
1735struct ilk_wm_maximums {
1736 uint16_t pri;
1737 uint16_t spr;
1738 uint16_t cur;
1739 uint16_t fbc;
1740};
1741
1742/*
1743 * For both WM_PIPE and WM_LP.
1744 * mem_value must be in 0.1us units.
1745 */
1746static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
1747 const struct intel_plane_state *pstate,
1748 uint32_t mem_value,
1749 bool is_lp)
1750{
1751 int cpp = pstate->base.fb ?
1752 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
1753 uint32_t method1, method2;
1754
1755 if (!cstate->base.active || !pstate->visible)
1756 return 0;
1757
1758 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
1759
1760 if (!is_lp)
1761 return method1;
1762
1763 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1764 cstate->base.adjusted_mode.crtc_htotal,
1765 drm_rect_width(&pstate->dst),
1766 cpp, mem_value);
1767
1768 return min(method1, method2);
1769}
1770
1771/*
1772 * For both WM_PIPE and WM_LP.
1773 * mem_value must be in 0.1us units.
1774 */
1775static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
1776 const struct intel_plane_state *pstate,
1777 uint32_t mem_value)
1778{
1779 int cpp = pstate->base.fb ?
1780 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
1781 uint32_t method1, method2;
1782
1783 if (!cstate->base.active || !pstate->visible)
1784 return 0;
1785
1786 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
1787 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1788 cstate->base.adjusted_mode.crtc_htotal,
1789 drm_rect_width(&pstate->dst),
1790 cpp, mem_value);
1791 return min(method1, method2);
1792}
1793
1794/*
1795 * For both WM_PIPE and WM_LP.
1796 * mem_value must be in 0.1us units.
1797 */
1798static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
1799 const struct intel_plane_state *pstate,
1800 uint32_t mem_value)
1801{
1802 /*
1803 * We treat the cursor plane as always-on for the purposes of watermark
1804 * calculation. Until we have two-stage watermark programming merged,
1805 * this is necessary to avoid flickering.
1806 */
1807 int cpp = 4;
1808 int width = pstate->visible ? pstate->base.crtc_w : 64;
1809
1810 if (!cstate->base.active)
1811 return 0;
1812
1813 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1814 cstate->base.adjusted_mode.crtc_htotal,
1815 width, cpp, mem_value);
1816}
1817
1818/* Only for WM_LP. */
1819static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
1820 const struct intel_plane_state *pstate,
1821 uint32_t pri_val)
1822{
1823 int cpp = pstate->base.fb ?
1824 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
1825
1826 if (!cstate->base.active || !pstate->visible)
1827 return 0;
1828
1829 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->dst), cpp);
1830}
1831
1832static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1833{
1834 if (INTEL_INFO(dev)->gen >= 8)
1835 return 3072;
1836 else if (INTEL_INFO(dev)->gen >= 7)
1837 return 768;
1838 else
1839 return 512;
1840}
1841
1842static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1843 int level, bool is_sprite)
1844{
1845 if (INTEL_INFO(dev)->gen >= 8)
1846 /* BDW primary/sprite plane watermarks */
1847 return level == 0 ? 255 : 2047;
1848 else if (INTEL_INFO(dev)->gen >= 7)
1849 /* IVB/HSW primary/sprite plane watermarks */
1850 return level == 0 ? 127 : 1023;
1851 else if (!is_sprite)
1852 /* ILK/SNB primary plane watermarks */
1853 return level == 0 ? 127 : 511;
1854 else
1855 /* ILK/SNB sprite plane watermarks */
1856 return level == 0 ? 63 : 255;
1857}
1858
1859static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1860 int level)
1861{
1862 if (INTEL_INFO(dev)->gen >= 7)
1863 return level == 0 ? 63 : 255;
1864 else
1865 return level == 0 ? 31 : 63;
1866}
1867
1868static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1869{
1870 if (INTEL_INFO(dev)->gen >= 8)
1871 return 31;
1872 else
1873 return 15;
1874}
1875
1876/* Calculate the maximum primary/sprite plane watermark */
1877static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1878 int level,
1879 const struct intel_wm_config *config,
1880 enum intel_ddb_partitioning ddb_partitioning,
1881 bool is_sprite)
1882{
1883 unsigned int fifo_size = ilk_display_fifo_size(dev);
1884
1885 /* if sprites aren't enabled, sprites get nothing */
1886 if (is_sprite && !config->sprites_enabled)
1887 return 0;
1888
1889 /* HSW allows LP1+ watermarks even with multiple pipes */
1890 if (level == 0 || config->num_pipes_active > 1) {
1891 fifo_size /= INTEL_INFO(dev)->num_pipes;
1892
1893 /*
1894 * For some reason the non self refresh
1895 * FIFO size is only half of the self
1896 * refresh FIFO size on ILK/SNB.
1897 */
1898 if (INTEL_INFO(dev)->gen <= 6)
1899 fifo_size /= 2;
1900 }
1901
1902 if (config->sprites_enabled) {
1903 /* level 0 is always calculated with 1:1 split */
1904 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1905 if (is_sprite)
1906 fifo_size *= 5;
1907 fifo_size /= 6;
1908 } else {
1909 fifo_size /= 2;
1910 }
1911 }
1912
1913 /* clamp to max that the registers can hold */
1914 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
1915}
1916
1917/* Calculate the maximum cursor plane watermark */
1918static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
1919 int level,
1920 const struct intel_wm_config *config)
1921{
1922 /* HSW LP1+ watermarks w/ multiple pipes */
1923 if (level > 0 && config->num_pipes_active > 1)
1924 return 64;
1925
1926 /* otherwise just report max that registers can hold */
1927 return ilk_cursor_wm_reg_max(dev, level);
1928}
1929
1930static void ilk_compute_wm_maximums(const struct drm_device *dev,
1931 int level,
1932 const struct intel_wm_config *config,
1933 enum intel_ddb_partitioning ddb_partitioning,
1934 struct ilk_wm_maximums *max)
1935{
1936 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1937 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1938 max->cur = ilk_cursor_wm_max(dev, level, config);
1939 max->fbc = ilk_fbc_wm_reg_max(dev);
1940}
1941
1942static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1943 int level,
1944 struct ilk_wm_maximums *max)
1945{
1946 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1947 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1948 max->cur = ilk_cursor_wm_reg_max(dev, level);
1949 max->fbc = ilk_fbc_wm_reg_max(dev);
1950}
1951
1952static bool ilk_validate_wm_level(int level,
1953 const struct ilk_wm_maximums *max,
1954 struct intel_wm_level *result)
1955{
1956 bool ret;
1957
1958 /* already determined to be invalid? */
1959 if (!result->enable)
1960 return false;
1961
1962 result->enable = result->pri_val <= max->pri &&
1963 result->spr_val <= max->spr &&
1964 result->cur_val <= max->cur;
1965
1966 ret = result->enable;
1967
1968 /*
1969 * HACK until we can pre-compute everything,
1970 * and thus fail gracefully if LP0 watermarks
1971 * are exceeded...
1972 */
1973 if (level == 0 && !result->enable) {
1974 if (result->pri_val > max->pri)
1975 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1976 level, result->pri_val, max->pri);
1977 if (result->spr_val > max->spr)
1978 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1979 level, result->spr_val, max->spr);
1980 if (result->cur_val > max->cur)
1981 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1982 level, result->cur_val, max->cur);
1983
1984 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1985 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1986 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1987 result->enable = true;
1988 }
1989
1990 return ret;
1991}
1992
1993static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
1994 const struct intel_crtc *intel_crtc,
1995 int level,
1996 struct intel_crtc_state *cstate,
1997 struct intel_plane_state *pristate,
1998 struct intel_plane_state *sprstate,
1999 struct intel_plane_state *curstate,
2000 struct intel_wm_level *result)
2001{
2002 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
2003 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
2004 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
2005
2006 /* WM1+ latency values stored in 0.5us units */
2007 if (level > 0) {
2008 pri_latency *= 5;
2009 spr_latency *= 5;
2010 cur_latency *= 5;
2011 }
2012
2013 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2014 pri_latency, level);
2015 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2016 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2017 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2018 result->enable = true;
2019}
2020
2021static uint32_t
2022hsw_compute_linetime_wm(struct drm_device *dev,
2023 struct intel_crtc_state *cstate)
2024{
2025 struct drm_i915_private *dev_priv = dev->dev_private;
2026 const struct drm_display_mode *adjusted_mode =
2027 &cstate->base.adjusted_mode;
2028 u32 linetime, ips_linetime;
2029
2030 if (!cstate->base.active)
2031 return 0;
2032 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2033 return 0;
2034 if (WARN_ON(dev_priv->cdclk_freq == 0))
2035 return 0;
2036
2037 /* The WM are computed with base on how long it takes to fill a single
2038 * row at the given clock rate, multiplied by 8.
2039 * */
2040 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2041 adjusted_mode->crtc_clock);
2042 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2043 dev_priv->cdclk_freq);
2044
2045 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2046 PIPE_WM_LINETIME_TIME(linetime);
2047}
2048
2049static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
2050{
2051 struct drm_i915_private *dev_priv = dev->dev_private;
2052
2053 if (IS_GEN9(dev)) {
2054 uint32_t val;
2055 int ret, i;
2056 int level, max_level = ilk_wm_max_level(dev);
2057
2058 /* read the first set of memory latencies[0:3] */
2059 val = 0; /* data0 to be programmed to 0 for first set */
2060 mutex_lock(&dev_priv->rps.hw_lock);
2061 ret = sandybridge_pcode_read(dev_priv,
2062 GEN9_PCODE_READ_MEM_LATENCY,
2063 &val);
2064 mutex_unlock(&dev_priv->rps.hw_lock);
2065
2066 if (ret) {
2067 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2068 return;
2069 }
2070
2071 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2072 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2073 GEN9_MEM_LATENCY_LEVEL_MASK;
2074 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2075 GEN9_MEM_LATENCY_LEVEL_MASK;
2076 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2077 GEN9_MEM_LATENCY_LEVEL_MASK;
2078
2079 /* read the second set of memory latencies[4:7] */
2080 val = 1; /* data0 to be programmed to 1 for second set */
2081 mutex_lock(&dev_priv->rps.hw_lock);
2082 ret = sandybridge_pcode_read(dev_priv,
2083 GEN9_PCODE_READ_MEM_LATENCY,
2084 &val);
2085 mutex_unlock(&dev_priv->rps.hw_lock);
2086 if (ret) {
2087 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2088 return;
2089 }
2090
2091 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2092 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2093 GEN9_MEM_LATENCY_LEVEL_MASK;
2094 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2095 GEN9_MEM_LATENCY_LEVEL_MASK;
2096 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2097 GEN9_MEM_LATENCY_LEVEL_MASK;
2098
2099 /*
2100 * WaWmMemoryReadLatency:skl
2101 *
2102 * punit doesn't take into account the read latency so we need
2103 * to add 2us to the various latency levels we retrieve from
2104 * the punit.
2105 * - W0 is a bit special in that it's the only level that
2106 * can't be disabled if we want to have display working, so
2107 * we always add 2us there.
2108 * - For levels >=1, punit returns 0us latency when they are
2109 * disabled, so we respect that and don't add 2us then
2110 *
2111 * Additionally, if a level n (n > 1) has a 0us latency, all
2112 * levels m (m >= n) need to be disabled. We make sure to
2113 * sanitize the values out of the punit to satisfy this
2114 * requirement.
2115 */
2116 wm[0] += 2;
2117 for (level = 1; level <= max_level; level++)
2118 if (wm[level] != 0)
2119 wm[level] += 2;
2120 else {
2121 for (i = level + 1; i <= max_level; i++)
2122 wm[i] = 0;
2123
2124 break;
2125 }
2126 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2127 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2128
2129 wm[0] = (sskpd >> 56) & 0xFF;
2130 if (wm[0] == 0)
2131 wm[0] = sskpd & 0xF;
2132 wm[1] = (sskpd >> 4) & 0xFF;
2133 wm[2] = (sskpd >> 12) & 0xFF;
2134 wm[3] = (sskpd >> 20) & 0x1FF;
2135 wm[4] = (sskpd >> 32) & 0x1FF;
2136 } else if (INTEL_INFO(dev)->gen >= 6) {
2137 uint32_t sskpd = I915_READ(MCH_SSKPD);
2138
2139 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2140 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2141 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2142 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
2143 } else if (INTEL_INFO(dev)->gen >= 5) {
2144 uint32_t mltr = I915_READ(MLTR_ILK);
2145
2146 /* ILK primary LP0 latency is 700 ns */
2147 wm[0] = 7;
2148 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2149 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
2150 }
2151}
2152
2153static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2154{
2155 /* ILK sprite LP0 latency is 1300 ns */
2156 if (INTEL_INFO(dev)->gen == 5)
2157 wm[0] = 13;
2158}
2159
2160static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2161{
2162 /* ILK cursor LP0 latency is 1300 ns */
2163 if (INTEL_INFO(dev)->gen == 5)
2164 wm[0] = 13;
2165
2166 /* WaDoubleCursorLP3Latency:ivb */
2167 if (IS_IVYBRIDGE(dev))
2168 wm[3] *= 2;
2169}
2170
2171int ilk_wm_max_level(const struct drm_device *dev)
2172{
2173 /* how many WM levels are we expecting */
2174 if (INTEL_INFO(dev)->gen >= 9)
2175 return 7;
2176 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2177 return 4;
2178 else if (INTEL_INFO(dev)->gen >= 6)
2179 return 3;
2180 else
2181 return 2;
2182}
2183
2184static void intel_print_wm_latency(struct drm_device *dev,
2185 const char *name,
2186 const uint16_t wm[8])
2187{
2188 int level, max_level = ilk_wm_max_level(dev);
2189
2190 for (level = 0; level <= max_level; level++) {
2191 unsigned int latency = wm[level];
2192
2193 if (latency == 0) {
2194 DRM_ERROR("%s WM%d latency not provided\n",
2195 name, level);
2196 continue;
2197 }
2198
2199 /*
2200 * - latencies are in us on gen9.
2201 * - before then, WM1+ latency values are in 0.5us units
2202 */
2203 if (IS_GEN9(dev))
2204 latency *= 10;
2205 else if (level > 0)
2206 latency *= 5;
2207
2208 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2209 name, level, wm[level],
2210 latency / 10, latency % 10);
2211 }
2212}
2213
2214static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2215 uint16_t wm[5], uint16_t min)
2216{
2217 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2218
2219 if (wm[0] >= min)
2220 return false;
2221
2222 wm[0] = max(wm[0], min);
2223 for (level = 1; level <= max_level; level++)
2224 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2225
2226 return true;
2227}
2228
2229static void snb_wm_latency_quirk(struct drm_device *dev)
2230{
2231 struct drm_i915_private *dev_priv = dev->dev_private;
2232 bool changed;
2233
2234 /*
2235 * The BIOS provided WM memory latency values are often
2236 * inadequate for high resolution displays. Adjust them.
2237 */
2238 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2239 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2240 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2241
2242 if (!changed)
2243 return;
2244
2245 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2246 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2247 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2248 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2249}
2250
2251static void ilk_setup_wm_latency(struct drm_device *dev)
2252{
2253 struct drm_i915_private *dev_priv = dev->dev_private;
2254
2255 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2256
2257 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2258 sizeof(dev_priv->wm.pri_latency));
2259 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2260 sizeof(dev_priv->wm.pri_latency));
2261
2262 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2263 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
2264
2265 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2266 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2267 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2268
2269 if (IS_GEN6(dev))
2270 snb_wm_latency_quirk(dev);
2271}
2272
2273static void skl_setup_wm_latency(struct drm_device *dev)
2274{
2275 struct drm_i915_private *dev_priv = dev->dev_private;
2276
2277 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2278 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2279}
2280
2281/* Compute new watermarks for the pipe */
2282static int ilk_compute_pipe_wm(struct intel_crtc *intel_crtc,
2283 struct drm_atomic_state *state)
2284{
2285 struct intel_pipe_wm *pipe_wm;
2286 struct drm_device *dev = intel_crtc->base.dev;
2287 const struct drm_i915_private *dev_priv = dev->dev_private;
2288 struct intel_crtc_state *cstate = NULL;
2289 struct intel_plane *intel_plane;
2290 struct drm_plane_state *ps;
2291 struct intel_plane_state *pristate = NULL;
2292 struct intel_plane_state *sprstate = NULL;
2293 struct intel_plane_state *curstate = NULL;
2294 int level, max_level = ilk_wm_max_level(dev);
2295 /* LP0 watermark maximums depend on this pipe alone */
2296 struct intel_wm_config config = {
2297 .num_pipes_active = 1,
2298 };
2299 struct ilk_wm_maximums max;
2300
2301 cstate = intel_atomic_get_crtc_state(state, intel_crtc);
2302 if (IS_ERR(cstate))
2303 return PTR_ERR(cstate);
2304
2305 pipe_wm = &cstate->wm.optimal.ilk;
2306 memset(pipe_wm, 0, sizeof(*pipe_wm));
2307
2308 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2309 ps = drm_atomic_get_plane_state(state,
2310 &intel_plane->base);
2311 if (IS_ERR(ps))
2312 return PTR_ERR(ps);
2313
2314 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
2315 pristate = to_intel_plane_state(ps);
2316 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
2317 sprstate = to_intel_plane_state(ps);
2318 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
2319 curstate = to_intel_plane_state(ps);
2320 }
2321
2322 config.sprites_enabled = sprstate->visible;
2323 config.sprites_scaled = sprstate->visible &&
2324 (drm_rect_width(&sprstate->dst) != drm_rect_width(&sprstate->src) >> 16 ||
2325 drm_rect_height(&sprstate->dst) != drm_rect_height(&sprstate->src) >> 16);
2326
2327 pipe_wm->pipe_enabled = cstate->base.active;
2328 pipe_wm->sprites_enabled = config.sprites_enabled;
2329 pipe_wm->sprites_scaled = config.sprites_scaled;
2330
2331 /* ILK/SNB: LP2+ watermarks only w/o sprites */
2332 if (INTEL_INFO(dev)->gen <= 6 && sprstate->visible)
2333 max_level = 1;
2334
2335 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
2336 if (config.sprites_scaled)
2337 max_level = 0;
2338
2339 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
2340 pristate, sprstate, curstate, &pipe_wm->wm[0]);
2341
2342 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2343 pipe_wm->linetime = hsw_compute_linetime_wm(dev, cstate);
2344
2345 /* LP0 watermarks always use 1/2 DDB partitioning */
2346 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2347
2348 /* At least LP0 must be valid */
2349 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0]))
2350 return -EINVAL;
2351
2352 ilk_compute_wm_reg_maximums(dev, 1, &max);
2353
2354 for (level = 1; level <= max_level; level++) {
2355 struct intel_wm_level wm = {};
2356
2357 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
2358 pristate, sprstate, curstate, &wm);
2359
2360 /*
2361 * Disable any watermark level that exceeds the
2362 * register maximums since such watermarks are
2363 * always invalid.
2364 */
2365 if (!ilk_validate_wm_level(level, &max, &wm))
2366 break;
2367
2368 pipe_wm->wm[level] = wm;
2369 }
2370
2371 return 0;
2372}
2373
2374/*
2375 * Merge the watermarks from all active pipes for a specific level.
2376 */
2377static void ilk_merge_wm_level(struct drm_device *dev,
2378 int level,
2379 struct intel_wm_level *ret_wm)
2380{
2381 const struct intel_crtc *intel_crtc;
2382
2383 ret_wm->enable = true;
2384
2385 for_each_intel_crtc(dev, intel_crtc) {
2386 const struct intel_crtc_state *cstate =
2387 to_intel_crtc_state(intel_crtc->base.state);
2388 const struct intel_pipe_wm *active = &cstate->wm.optimal.ilk;
2389 const struct intel_wm_level *wm = &active->wm[level];
2390
2391 if (!active->pipe_enabled)
2392 continue;
2393
2394 /*
2395 * The watermark values may have been used in the past,
2396 * so we must maintain them in the registers for some
2397 * time even if the level is now disabled.
2398 */
2399 if (!wm->enable)
2400 ret_wm->enable = false;
2401
2402 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2403 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2404 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2405 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2406 }
2407}
2408
2409/*
2410 * Merge all low power watermarks for all active pipes.
2411 */
2412static void ilk_wm_merge(struct drm_device *dev,
2413 const struct intel_wm_config *config,
2414 const struct ilk_wm_maximums *max,
2415 struct intel_pipe_wm *merged)
2416{
2417 struct drm_i915_private *dev_priv = dev->dev_private;
2418 int level, max_level = ilk_wm_max_level(dev);
2419 int last_enabled_level = max_level;
2420
2421 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2422 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2423 config->num_pipes_active > 1)
2424 return;
2425
2426 /* ILK: FBC WM must be disabled always */
2427 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
2428
2429 /* merge each WM1+ level */
2430 for (level = 1; level <= max_level; level++) {
2431 struct intel_wm_level *wm = &merged->wm[level];
2432
2433 ilk_merge_wm_level(dev, level, wm);
2434
2435 if (level > last_enabled_level)
2436 wm->enable = false;
2437 else if (!ilk_validate_wm_level(level, max, wm))
2438 /* make sure all following levels get disabled */
2439 last_enabled_level = level - 1;
2440
2441 /*
2442 * The spec says it is preferred to disable
2443 * FBC WMs instead of disabling a WM level.
2444 */
2445 if (wm->fbc_val > max->fbc) {
2446 if (wm->enable)
2447 merged->fbc_wm_enabled = false;
2448 wm->fbc_val = 0;
2449 }
2450 }
2451
2452 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2453 /*
2454 * FIXME this is racy. FBC might get enabled later.
2455 * What we should check here is whether FBC can be
2456 * enabled sometime later.
2457 */
2458 if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
2459 intel_fbc_is_active(dev_priv)) {
2460 for (level = 2; level <= max_level; level++) {
2461 struct intel_wm_level *wm = &merged->wm[level];
2462
2463 wm->enable = false;
2464 }
2465 }
2466}
2467
2468static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2469{
2470 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2471 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2472}
2473
2474/* The value we need to program into the WM_LPx latency field */
2475static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2476{
2477 struct drm_i915_private *dev_priv = dev->dev_private;
2478
2479 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2480 return 2 * level;
2481 else
2482 return dev_priv->wm.pri_latency[level];
2483}
2484
2485static void ilk_compute_wm_results(struct drm_device *dev,
2486 const struct intel_pipe_wm *merged,
2487 enum intel_ddb_partitioning partitioning,
2488 struct ilk_wm_values *results)
2489{
2490 struct intel_crtc *intel_crtc;
2491 int level, wm_lp;
2492
2493 results->enable_fbc_wm = merged->fbc_wm_enabled;
2494 results->partitioning = partitioning;
2495
2496 /* LP1+ register values */
2497 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2498 const struct intel_wm_level *r;
2499
2500 level = ilk_wm_lp_to_level(wm_lp, merged);
2501
2502 r = &merged->wm[level];
2503
2504 /*
2505 * Maintain the watermark values even if the level is
2506 * disabled. Doing otherwise could cause underruns.
2507 */
2508 results->wm_lp[wm_lp - 1] =
2509 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
2510 (r->pri_val << WM1_LP_SR_SHIFT) |
2511 r->cur_val;
2512
2513 if (r->enable)
2514 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2515
2516 if (INTEL_INFO(dev)->gen >= 8)
2517 results->wm_lp[wm_lp - 1] |=
2518 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2519 else
2520 results->wm_lp[wm_lp - 1] |=
2521 r->fbc_val << WM1_LP_FBC_SHIFT;
2522
2523 /*
2524 * Always set WM1S_LP_EN when spr_val != 0, even if the
2525 * level is disabled. Doing otherwise could cause underruns.
2526 */
2527 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2528 WARN_ON(wm_lp != 1);
2529 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2530 } else
2531 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
2532 }
2533
2534 /* LP0 register values */
2535 for_each_intel_crtc(dev, intel_crtc) {
2536 const struct intel_crtc_state *cstate =
2537 to_intel_crtc_state(intel_crtc->base.state);
2538 enum pipe pipe = intel_crtc->pipe;
2539 const struct intel_wm_level *r = &cstate->wm.optimal.ilk.wm[0];
2540
2541 if (WARN_ON(!r->enable))
2542 continue;
2543
2544 results->wm_linetime[pipe] = cstate->wm.optimal.ilk.linetime;
2545
2546 results->wm_pipe[pipe] =
2547 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2548 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2549 r->cur_val;
2550 }
2551}
2552
2553/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2554 * case both are at the same level. Prefer r1 in case they're the same. */
2555static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
2556 struct intel_pipe_wm *r1,
2557 struct intel_pipe_wm *r2)
2558{
2559 int level, max_level = ilk_wm_max_level(dev);
2560 int level1 = 0, level2 = 0;
2561
2562 for (level = 1; level <= max_level; level++) {
2563 if (r1->wm[level].enable)
2564 level1 = level;
2565 if (r2->wm[level].enable)
2566 level2 = level;
2567 }
2568
2569 if (level1 == level2) {
2570 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
2571 return r2;
2572 else
2573 return r1;
2574 } else if (level1 > level2) {
2575 return r1;
2576 } else {
2577 return r2;
2578 }
2579}
2580
2581/* dirty bits used to track which watermarks need changes */
2582#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2583#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2584#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2585#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2586#define WM_DIRTY_FBC (1 << 24)
2587#define WM_DIRTY_DDB (1 << 25)
2588
2589static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
2590 const struct ilk_wm_values *old,
2591 const struct ilk_wm_values *new)
2592{
2593 unsigned int dirty = 0;
2594 enum pipe pipe;
2595 int wm_lp;
2596
2597 for_each_pipe(dev_priv, pipe) {
2598 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2599 dirty |= WM_DIRTY_LINETIME(pipe);
2600 /* Must disable LP1+ watermarks too */
2601 dirty |= WM_DIRTY_LP_ALL;
2602 }
2603
2604 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2605 dirty |= WM_DIRTY_PIPE(pipe);
2606 /* Must disable LP1+ watermarks too */
2607 dirty |= WM_DIRTY_LP_ALL;
2608 }
2609 }
2610
2611 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2612 dirty |= WM_DIRTY_FBC;
2613 /* Must disable LP1+ watermarks too */
2614 dirty |= WM_DIRTY_LP_ALL;
2615 }
2616
2617 if (old->partitioning != new->partitioning) {
2618 dirty |= WM_DIRTY_DDB;
2619 /* Must disable LP1+ watermarks too */
2620 dirty |= WM_DIRTY_LP_ALL;
2621 }
2622
2623 /* LP1+ watermarks already deemed dirty, no need to continue */
2624 if (dirty & WM_DIRTY_LP_ALL)
2625 return dirty;
2626
2627 /* Find the lowest numbered LP1+ watermark in need of an update... */
2628 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2629 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2630 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2631 break;
2632 }
2633
2634 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2635 for (; wm_lp <= 3; wm_lp++)
2636 dirty |= WM_DIRTY_LP(wm_lp);
2637
2638 return dirty;
2639}
2640
2641static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2642 unsigned int dirty)
2643{
2644 struct ilk_wm_values *previous = &dev_priv->wm.hw;
2645 bool changed = false;
2646
2647 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2648 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2649 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
2650 changed = true;
2651 }
2652 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2653 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2654 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
2655 changed = true;
2656 }
2657 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2658 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2659 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
2660 changed = true;
2661 }
2662
2663 /*
2664 * Don't touch WM1S_LP_EN here.
2665 * Doing so could cause underruns.
2666 */
2667
2668 return changed;
2669}
2670
2671/*
2672 * The spec says we shouldn't write when we don't need, because every write
2673 * causes WMs to be re-evaluated, expending some power.
2674 */
2675static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2676 struct ilk_wm_values *results)
2677{
2678 struct drm_device *dev = dev_priv->dev;
2679 struct ilk_wm_values *previous = &dev_priv->wm.hw;
2680 unsigned int dirty;
2681 uint32_t val;
2682
2683 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
2684 if (!dirty)
2685 return;
2686
2687 _ilk_disable_lp_wm(dev_priv, dirty);
2688
2689 if (dirty & WM_DIRTY_PIPE(PIPE_A))
2690 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
2691 if (dirty & WM_DIRTY_PIPE(PIPE_B))
2692 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
2693 if (dirty & WM_DIRTY_PIPE(PIPE_C))
2694 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2695
2696 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
2697 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
2698 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
2699 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
2700 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
2701 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2702
2703 if (dirty & WM_DIRTY_DDB) {
2704 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
2705 val = I915_READ(WM_MISC);
2706 if (results->partitioning == INTEL_DDB_PART_1_2)
2707 val &= ~WM_MISC_DATA_PARTITION_5_6;
2708 else
2709 val |= WM_MISC_DATA_PARTITION_5_6;
2710 I915_WRITE(WM_MISC, val);
2711 } else {
2712 val = I915_READ(DISP_ARB_CTL2);
2713 if (results->partitioning == INTEL_DDB_PART_1_2)
2714 val &= ~DISP_DATA_PARTITION_5_6;
2715 else
2716 val |= DISP_DATA_PARTITION_5_6;
2717 I915_WRITE(DISP_ARB_CTL2, val);
2718 }
2719 }
2720
2721 if (dirty & WM_DIRTY_FBC) {
2722 val = I915_READ(DISP_ARB_CTL);
2723 if (results->enable_fbc_wm)
2724 val &= ~DISP_FBC_WM_DIS;
2725 else
2726 val |= DISP_FBC_WM_DIS;
2727 I915_WRITE(DISP_ARB_CTL, val);
2728 }
2729
2730 if (dirty & WM_DIRTY_LP(1) &&
2731 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2732 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2733
2734 if (INTEL_INFO(dev)->gen >= 7) {
2735 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2736 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2737 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2738 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2739 }
2740
2741 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
2742 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
2743 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
2744 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
2745 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
2746 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
2747
2748 dev_priv->wm.hw = *results;
2749}
2750
2751static bool ilk_disable_lp_wm(struct drm_device *dev)
2752{
2753 struct drm_i915_private *dev_priv = dev->dev_private;
2754
2755 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2756}
2757
2758/*
2759 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2760 * different active planes.
2761 */
2762
2763#define SKL_DDB_SIZE 896 /* in blocks */
2764#define BXT_DDB_SIZE 512
2765
2766/*
2767 * Return the index of a plane in the SKL DDB and wm result arrays. Primary
2768 * plane is always in slot 0, cursor is always in slot I915_MAX_PLANES-1, and
2769 * other universal planes are in indices 1..n. Note that this may leave unused
2770 * indices between the top "sprite" plane and the cursor.
2771 */
2772static int
2773skl_wm_plane_id(const struct intel_plane *plane)
2774{
2775 switch (plane->base.type) {
2776 case DRM_PLANE_TYPE_PRIMARY:
2777 return 0;
2778 case DRM_PLANE_TYPE_CURSOR:
2779 return PLANE_CURSOR;
2780 case DRM_PLANE_TYPE_OVERLAY:
2781 return plane->plane + 1;
2782 default:
2783 MISSING_CASE(plane->base.type);
2784 return plane->plane;
2785 }
2786}
2787
2788static void
2789skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
2790 const struct intel_crtc_state *cstate,
2791 const struct intel_wm_config *config,
2792 struct skl_ddb_entry *alloc /* out */)
2793{
2794 struct drm_crtc *for_crtc = cstate->base.crtc;
2795 struct drm_crtc *crtc;
2796 unsigned int pipe_size, ddb_size;
2797 int nth_active_pipe;
2798
2799 if (!cstate->base.active) {
2800 alloc->start = 0;
2801 alloc->end = 0;
2802 return;
2803 }
2804
2805 if (IS_BROXTON(dev))
2806 ddb_size = BXT_DDB_SIZE;
2807 else
2808 ddb_size = SKL_DDB_SIZE;
2809
2810 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2811
2812 nth_active_pipe = 0;
2813 for_each_crtc(dev, crtc) {
2814 if (!to_intel_crtc(crtc)->active)
2815 continue;
2816
2817 if (crtc == for_crtc)
2818 break;
2819
2820 nth_active_pipe++;
2821 }
2822
2823 pipe_size = ddb_size / config->num_pipes_active;
2824 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
2825 alloc->end = alloc->start + pipe_size;
2826}
2827
2828static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2829{
2830 if (config->num_pipes_active == 1)
2831 return 32;
2832
2833 return 8;
2834}
2835
2836static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2837{
2838 entry->start = reg & 0x3ff;
2839 entry->end = (reg >> 16) & 0x3ff;
2840 if (entry->end)
2841 entry->end += 1;
2842}
2843
2844void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2845 struct skl_ddb_allocation *ddb /* out */)
2846{
2847 enum pipe pipe;
2848 int plane;
2849 u32 val;
2850
2851 memset(ddb, 0, sizeof(*ddb));
2852
2853 for_each_pipe(dev_priv, pipe) {
2854 enum intel_display_power_domain power_domain;
2855
2856 power_domain = POWER_DOMAIN_PIPE(pipe);
2857 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
2858 continue;
2859
2860 for_each_plane(dev_priv, pipe, plane) {
2861 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2862 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2863 val);
2864 }
2865
2866 val = I915_READ(CUR_BUF_CFG(pipe));
2867 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
2868 val);
2869
2870 intel_display_power_put(dev_priv, power_domain);
2871 }
2872}
2873
2874static unsigned int
2875skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
2876 const struct drm_plane_state *pstate,
2877 int y)
2878{
2879 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
2880 struct drm_framebuffer *fb = pstate->fb;
2881 uint32_t width = 0, height = 0;
2882
2883 width = drm_rect_width(&intel_pstate->src) >> 16;
2884 height = drm_rect_height(&intel_pstate->src) >> 16;
2885
2886 if (intel_rotation_90_or_270(pstate->rotation))
2887 swap(width, height);
2888
2889 /* for planar format */
2890 if (fb->pixel_format == DRM_FORMAT_NV12) {
2891 if (y) /* y-plane data rate */
2892 return width * height *
2893 drm_format_plane_cpp(fb->pixel_format, 0);
2894 else /* uv-plane data rate */
2895 return (width / 2) * (height / 2) *
2896 drm_format_plane_cpp(fb->pixel_format, 1);
2897 }
2898
2899 /* for packed formats */
2900 return width * height * drm_format_plane_cpp(fb->pixel_format, 0);
2901}
2902
2903/*
2904 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2905 * a 8192x4096@32bpp framebuffer:
2906 * 3 * 4096 * 8192 * 4 < 2^32
2907 */
2908static unsigned int
2909skl_get_total_relative_data_rate(const struct intel_crtc_state *cstate)
2910{
2911 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
2912 struct drm_device *dev = intel_crtc->base.dev;
2913 const struct intel_plane *intel_plane;
2914 unsigned int total_data_rate = 0;
2915
2916 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2917 const struct drm_plane_state *pstate = intel_plane->base.state;
2918
2919 if (pstate->fb == NULL)
2920 continue;
2921
2922 if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
2923 continue;
2924
2925 /* packed/uv */
2926 total_data_rate += skl_plane_relative_data_rate(cstate,
2927 pstate,
2928 0);
2929
2930 if (pstate->fb->pixel_format == DRM_FORMAT_NV12)
2931 /* y-plane */
2932 total_data_rate += skl_plane_relative_data_rate(cstate,
2933 pstate,
2934 1);
2935 }
2936
2937 return total_data_rate;
2938}
2939
2940static void
2941skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
2942 struct skl_ddb_allocation *ddb /* out */)
2943{
2944 struct drm_crtc *crtc = cstate->base.crtc;
2945 struct drm_device *dev = crtc->dev;
2946 struct drm_i915_private *dev_priv = to_i915(dev);
2947 struct intel_wm_config *config = &dev_priv->wm.config;
2948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2949 struct intel_plane *intel_plane;
2950 enum pipe pipe = intel_crtc->pipe;
2951 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
2952 uint16_t alloc_size, start, cursor_blocks;
2953 uint16_t minimum[I915_MAX_PLANES];
2954 uint16_t y_minimum[I915_MAX_PLANES];
2955 unsigned int total_data_rate;
2956
2957 skl_ddb_get_pipe_allocation_limits(dev, cstate, config, alloc);
2958 alloc_size = skl_ddb_entry_size(alloc);
2959 if (alloc_size == 0) {
2960 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
2961 memset(&ddb->plane[pipe][PLANE_CURSOR], 0,
2962 sizeof(ddb->plane[pipe][PLANE_CURSOR]));
2963 return;
2964 }
2965
2966 cursor_blocks = skl_cursor_allocation(config);
2967 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
2968 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
2969
2970 alloc_size -= cursor_blocks;
2971 alloc->end -= cursor_blocks;
2972
2973 /* 1. Allocate the mininum required blocks for each active plane */
2974 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2975 struct drm_plane *plane = &intel_plane->base;
2976 struct drm_framebuffer *fb = plane->state->fb;
2977 int id = skl_wm_plane_id(intel_plane);
2978
2979 if (!to_intel_plane_state(plane->state)->visible)
2980 continue;
2981
2982 if (plane->type == DRM_PLANE_TYPE_CURSOR)
2983 continue;
2984
2985 minimum[id] = 8;
2986 alloc_size -= minimum[id];
2987 y_minimum[id] = (fb->pixel_format == DRM_FORMAT_NV12) ? 8 : 0;
2988 alloc_size -= y_minimum[id];
2989 }
2990
2991 /*
2992 * 2. Distribute the remaining space in proportion to the amount of
2993 * data each plane needs to fetch from memory.
2994 *
2995 * FIXME: we may not allocate every single block here.
2996 */
2997 total_data_rate = skl_get_total_relative_data_rate(cstate);
2998
2999 start = alloc->start;
3000 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3001 struct drm_plane *plane = &intel_plane->base;
3002 struct drm_plane_state *pstate = intel_plane->base.state;
3003 unsigned int data_rate, y_data_rate;
3004 uint16_t plane_blocks, y_plane_blocks = 0;
3005 int id = skl_wm_plane_id(intel_plane);
3006
3007 if (!to_intel_plane_state(pstate)->visible)
3008 continue;
3009 if (plane->type == DRM_PLANE_TYPE_CURSOR)
3010 continue;
3011
3012 data_rate = skl_plane_relative_data_rate(cstate, pstate, 0);
3013
3014 /*
3015 * allocation for (packed formats) or (uv-plane part of planar format):
3016 * promote the expression to 64 bits to avoid overflowing, the
3017 * result is < available as data_rate / total_data_rate < 1
3018 */
3019 plane_blocks = minimum[id];
3020 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3021 total_data_rate);
3022
3023 ddb->plane[pipe][id].start = start;
3024 ddb->plane[pipe][id].end = start + plane_blocks;
3025
3026 start += plane_blocks;
3027
3028 /*
3029 * allocation for y_plane part of planar format:
3030 */
3031 if (pstate->fb->pixel_format == DRM_FORMAT_NV12) {
3032 y_data_rate = skl_plane_relative_data_rate(cstate,
3033 pstate,
3034 1);
3035 y_plane_blocks = y_minimum[id];
3036 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3037 total_data_rate);
3038
3039 ddb->y_plane[pipe][id].start = start;
3040 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
3041
3042 start += y_plane_blocks;
3043 }
3044
3045 }
3046
3047}
3048
3049static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
3050{
3051 /* TODO: Take into account the scalers once we support them */
3052 return config->base.adjusted_mode.crtc_clock;
3053}
3054
3055/*
3056 * The max latency should be 257 (max the punit can code is 255 and we add 2us
3057 * for the read latency) and cpp should always be <= 8, so that
3058 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3059 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3060*/
3061static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
3062{
3063 uint32_t wm_intermediate_val, ret;
3064
3065 if (latency == 0)
3066 return UINT_MAX;
3067
3068 wm_intermediate_val = latency * pixel_rate * cpp / 512;
3069 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3070
3071 return ret;
3072}
3073
3074static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
3075 uint32_t horiz_pixels, uint8_t cpp,
3076 uint64_t tiling, uint32_t latency)
3077{
3078 uint32_t ret;
3079 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3080 uint32_t wm_intermediate_val;
3081
3082 if (latency == 0)
3083 return UINT_MAX;
3084
3085 plane_bytes_per_line = horiz_pixels * cpp;
3086
3087 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3088 tiling == I915_FORMAT_MOD_Yf_TILED) {
3089 plane_bytes_per_line *= 4;
3090 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3091 plane_blocks_per_line /= 4;
3092 } else {
3093 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3094 }
3095
3096 wm_intermediate_val = latency * pixel_rate;
3097 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
3098 plane_blocks_per_line;
3099
3100 return ret;
3101}
3102
3103static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3104 const struct intel_crtc *intel_crtc)
3105{
3106 struct drm_device *dev = intel_crtc->base.dev;
3107 struct drm_i915_private *dev_priv = dev->dev_private;
3108 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
3109
3110 /*
3111 * If ddb allocation of pipes changed, it may require recalculation of
3112 * watermarks
3113 */
3114 if (memcmp(new_ddb->pipe, cur_ddb->pipe, sizeof(new_ddb->pipe)))
3115 return true;
3116
3117 return false;
3118}
3119
3120static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
3121 struct intel_crtc_state *cstate,
3122 struct intel_plane *intel_plane,
3123 uint16_t ddb_allocation,
3124 int level,
3125 uint16_t *out_blocks, /* out */
3126 uint8_t *out_lines /* out */)
3127{
3128 struct drm_plane *plane = &intel_plane->base;
3129 struct drm_framebuffer *fb = plane->state->fb;
3130 struct intel_plane_state *intel_pstate =
3131 to_intel_plane_state(plane->state);
3132 uint32_t latency = dev_priv->wm.skl_latency[level];
3133 uint32_t method1, method2;
3134 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3135 uint32_t res_blocks, res_lines;
3136 uint32_t selected_result;
3137 uint8_t cpp;
3138 uint32_t width = 0, height = 0;
3139
3140 if (latency == 0 || !cstate->base.active || !intel_pstate->visible)
3141 return false;
3142
3143 width = drm_rect_width(&intel_pstate->src) >> 16;
3144 height = drm_rect_height(&intel_pstate->src) >> 16;
3145
3146 if (intel_rotation_90_or_270(plane->state->rotation))
3147 swap(width, height);
3148
3149 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
3150 method1 = skl_wm_method1(skl_pipe_pixel_rate(cstate),
3151 cpp, latency);
3152 method2 = skl_wm_method2(skl_pipe_pixel_rate(cstate),
3153 cstate->base.adjusted_mode.crtc_htotal,
3154 width,
3155 cpp,
3156 fb->modifier[0],
3157 latency);
3158
3159 plane_bytes_per_line = width * cpp;
3160 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3161
3162 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3163 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
3164 uint32_t min_scanlines = 4;
3165 uint32_t y_tile_minimum;
3166 if (intel_rotation_90_or_270(plane->state->rotation)) {
3167 int cpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
3168 drm_format_plane_cpp(fb->pixel_format, 1) :
3169 drm_format_plane_cpp(fb->pixel_format, 0);
3170
3171 switch (cpp) {
3172 case 1:
3173 min_scanlines = 16;
3174 break;
3175 case 2:
3176 min_scanlines = 8;
3177 break;
3178 case 8:
3179 WARN(1, "Unsupported pixel depth for rotation");
3180 }
3181 }
3182 y_tile_minimum = plane_blocks_per_line * min_scanlines;
3183 selected_result = max(method2, y_tile_minimum);
3184 } else {
3185 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3186 selected_result = min(method1, method2);
3187 else
3188 selected_result = method1;
3189 }
3190
3191 res_blocks = selected_result + 1;
3192 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
3193
3194 if (level >= 1 && level <= 7) {
3195 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3196 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED)
3197 res_lines += 4;
3198 else
3199 res_blocks++;
3200 }
3201
3202 if (res_blocks >= ddb_allocation || res_lines > 31)
3203 return false;
3204
3205 *out_blocks = res_blocks;
3206 *out_lines = res_lines;
3207
3208 return true;
3209}
3210
3211static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3212 struct skl_ddb_allocation *ddb,
3213 struct intel_crtc_state *cstate,
3214 int level,
3215 struct skl_wm_level *result)
3216{
3217 struct drm_device *dev = dev_priv->dev;
3218 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
3219 struct intel_plane *intel_plane;
3220 uint16_t ddb_blocks;
3221 enum pipe pipe = intel_crtc->pipe;
3222
3223 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3224 int i = skl_wm_plane_id(intel_plane);
3225
3226 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3227
3228 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
3229 cstate,
3230 intel_plane,
3231 ddb_blocks,
3232 level,
3233 &result->plane_res_b[i],
3234 &result->plane_res_l[i]);
3235 }
3236}
3237
3238static uint32_t
3239skl_compute_linetime_wm(struct intel_crtc_state *cstate)
3240{
3241 if (!cstate->base.active)
3242 return 0;
3243
3244 if (WARN_ON(skl_pipe_pixel_rate(cstate) == 0))
3245 return 0;
3246
3247 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
3248 skl_pipe_pixel_rate(cstate));
3249}
3250
3251static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
3252 struct skl_wm_level *trans_wm /* out */)
3253{
3254 struct drm_crtc *crtc = cstate->base.crtc;
3255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3256 struct intel_plane *intel_plane;
3257
3258 if (!cstate->base.active)
3259 return;
3260
3261 /* Until we know more, just disable transition WMs */
3262 for_each_intel_plane_on_crtc(crtc->dev, intel_crtc, intel_plane) {
3263 int i = skl_wm_plane_id(intel_plane);
3264
3265 trans_wm->plane_en[i] = false;
3266 }
3267}
3268
3269static void skl_compute_pipe_wm(struct intel_crtc_state *cstate,
3270 struct skl_ddb_allocation *ddb,
3271 struct skl_pipe_wm *pipe_wm)
3272{
3273 struct drm_device *dev = cstate->base.crtc->dev;
3274 const struct drm_i915_private *dev_priv = dev->dev_private;
3275 int level, max_level = ilk_wm_max_level(dev);
3276
3277 for (level = 0; level <= max_level; level++) {
3278 skl_compute_wm_level(dev_priv, ddb, cstate,
3279 level, &pipe_wm->wm[level]);
3280 }
3281 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
3282
3283 skl_compute_transition_wm(cstate, &pipe_wm->trans_wm);
3284}
3285
3286static void skl_compute_wm_results(struct drm_device *dev,
3287 struct skl_pipe_wm *p_wm,
3288 struct skl_wm_values *r,
3289 struct intel_crtc *intel_crtc)
3290{
3291 int level, max_level = ilk_wm_max_level(dev);
3292 enum pipe pipe = intel_crtc->pipe;
3293 uint32_t temp;
3294 int i;
3295
3296 for (level = 0; level <= max_level; level++) {
3297 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3298 temp = 0;
3299
3300 temp |= p_wm->wm[level].plane_res_l[i] <<
3301 PLANE_WM_LINES_SHIFT;
3302 temp |= p_wm->wm[level].plane_res_b[i];
3303 if (p_wm->wm[level].plane_en[i])
3304 temp |= PLANE_WM_EN;
3305
3306 r->plane[pipe][i][level] = temp;
3307 }
3308
3309 temp = 0;
3310
3311 temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3312 temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
3313
3314 if (p_wm->wm[level].plane_en[PLANE_CURSOR])
3315 temp |= PLANE_WM_EN;
3316
3317 r->plane[pipe][PLANE_CURSOR][level] = temp;
3318
3319 }
3320
3321 /* transition WMs */
3322 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3323 temp = 0;
3324 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3325 temp |= p_wm->trans_wm.plane_res_b[i];
3326 if (p_wm->trans_wm.plane_en[i])
3327 temp |= PLANE_WM_EN;
3328
3329 r->plane_trans[pipe][i] = temp;
3330 }
3331
3332 temp = 0;
3333 temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3334 temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3335 if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
3336 temp |= PLANE_WM_EN;
3337
3338 r->plane_trans[pipe][PLANE_CURSOR] = temp;
3339
3340 r->wm_linetime[pipe] = p_wm->linetime;
3341}
3342
3343static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3344 i915_reg_t reg,
3345 const struct skl_ddb_entry *entry)
3346{
3347 if (entry->end)
3348 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3349 else
3350 I915_WRITE(reg, 0);
3351}
3352
3353static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3354 const struct skl_wm_values *new)
3355{
3356 struct drm_device *dev = dev_priv->dev;
3357 struct intel_crtc *crtc;
3358
3359 for_each_intel_crtc(dev, crtc) {
3360 int i, level, max_level = ilk_wm_max_level(dev);
3361 enum pipe pipe = crtc->pipe;
3362
3363 if (!new->dirty[pipe])
3364 continue;
3365
3366 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
3367
3368 for (level = 0; level <= max_level; level++) {
3369 for (i = 0; i < intel_num_planes(crtc); i++)
3370 I915_WRITE(PLANE_WM(pipe, i, level),
3371 new->plane[pipe][i][level]);
3372 I915_WRITE(CUR_WM(pipe, level),
3373 new->plane[pipe][PLANE_CURSOR][level]);
3374 }
3375 for (i = 0; i < intel_num_planes(crtc); i++)
3376 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3377 new->plane_trans[pipe][i]);
3378 I915_WRITE(CUR_WM_TRANS(pipe),
3379 new->plane_trans[pipe][PLANE_CURSOR]);
3380
3381 for (i = 0; i < intel_num_planes(crtc); i++) {
3382 skl_ddb_entry_write(dev_priv,
3383 PLANE_BUF_CFG(pipe, i),
3384 &new->ddb.plane[pipe][i]);
3385 skl_ddb_entry_write(dev_priv,
3386 PLANE_NV12_BUF_CFG(pipe, i),
3387 &new->ddb.y_plane[pipe][i]);
3388 }
3389
3390 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
3391 &new->ddb.plane[pipe][PLANE_CURSOR]);
3392 }
3393}
3394
3395/*
3396 * When setting up a new DDB allocation arrangement, we need to correctly
3397 * sequence the times at which the new allocations for the pipes are taken into
3398 * account or we'll have pipes fetching from space previously allocated to
3399 * another pipe.
3400 *
3401 * Roughly the sequence looks like:
3402 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3403 * overlapping with a previous light-up pipe (another way to put it is:
3404 * pipes with their new allocation strickly included into their old ones).
3405 * 2. re-allocate the other pipes that get their allocation reduced
3406 * 3. allocate the pipes having their allocation increased
3407 *
3408 * Steps 1. and 2. are here to take care of the following case:
3409 * - Initially DDB looks like this:
3410 * | B | C |
3411 * - enable pipe A.
3412 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3413 * allocation
3414 * | A | B | C |
3415 *
3416 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3417 */
3418
3419static void
3420skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
3421{
3422 int plane;
3423
3424 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3425
3426 for_each_plane(dev_priv, pipe, plane) {
3427 I915_WRITE(PLANE_SURF(pipe, plane),
3428 I915_READ(PLANE_SURF(pipe, plane)));
3429 }
3430 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3431}
3432
3433static bool
3434skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3435 const struct skl_ddb_allocation *new,
3436 enum pipe pipe)
3437{
3438 uint16_t old_size, new_size;
3439
3440 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3441 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3442
3443 return old_size != new_size &&
3444 new->pipe[pipe].start >= old->pipe[pipe].start &&
3445 new->pipe[pipe].end <= old->pipe[pipe].end;
3446}
3447
3448static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3449 struct skl_wm_values *new_values)
3450{
3451 struct drm_device *dev = dev_priv->dev;
3452 struct skl_ddb_allocation *cur_ddb, *new_ddb;
3453 bool reallocated[I915_MAX_PIPES] = {};
3454 struct intel_crtc *crtc;
3455 enum pipe pipe;
3456
3457 new_ddb = &new_values->ddb;
3458 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3459
3460 /*
3461 * First pass: flush the pipes with the new allocation contained into
3462 * the old space.
3463 *
3464 * We'll wait for the vblank on those pipes to ensure we can safely
3465 * re-allocate the freed space without this pipe fetching from it.
3466 */
3467 for_each_intel_crtc(dev, crtc) {
3468 if (!crtc->active)
3469 continue;
3470
3471 pipe = crtc->pipe;
3472
3473 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3474 continue;
3475
3476 skl_wm_flush_pipe(dev_priv, pipe, 1);
3477 intel_wait_for_vblank(dev, pipe);
3478
3479 reallocated[pipe] = true;
3480 }
3481
3482
3483 /*
3484 * Second pass: flush the pipes that are having their allocation
3485 * reduced, but overlapping with a previous allocation.
3486 *
3487 * Here as well we need to wait for the vblank to make sure the freed
3488 * space is not used anymore.
3489 */
3490 for_each_intel_crtc(dev, crtc) {
3491 if (!crtc->active)
3492 continue;
3493
3494 pipe = crtc->pipe;
3495
3496 if (reallocated[pipe])
3497 continue;
3498
3499 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3500 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
3501 skl_wm_flush_pipe(dev_priv, pipe, 2);
3502 intel_wait_for_vblank(dev, pipe);
3503 reallocated[pipe] = true;
3504 }
3505 }
3506
3507 /*
3508 * Third pass: flush the pipes that got more space allocated.
3509 *
3510 * We don't need to actively wait for the update here, next vblank
3511 * will just get more DDB space with the correct WM values.
3512 */
3513 for_each_intel_crtc(dev, crtc) {
3514 if (!crtc->active)
3515 continue;
3516
3517 pipe = crtc->pipe;
3518
3519 /*
3520 * At this point, only the pipes more space than before are
3521 * left to re-allocate.
3522 */
3523 if (reallocated[pipe])
3524 continue;
3525
3526 skl_wm_flush_pipe(dev_priv, pipe, 3);
3527 }
3528}
3529
3530static bool skl_update_pipe_wm(struct drm_crtc *crtc,
3531 struct skl_ddb_allocation *ddb, /* out */
3532 struct skl_pipe_wm *pipe_wm /* out */)
3533{
3534 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3535 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3536
3537 skl_allocate_pipe_ddb(cstate, ddb);
3538 skl_compute_pipe_wm(cstate, ddb, pipe_wm);
3539
3540 if (!memcmp(&intel_crtc->wm.active.skl, pipe_wm, sizeof(*pipe_wm)))
3541 return false;
3542
3543 intel_crtc->wm.active.skl = *pipe_wm;
3544
3545 return true;
3546}
3547
3548static void skl_update_other_pipe_wm(struct drm_device *dev,
3549 struct drm_crtc *crtc,
3550 struct skl_wm_values *r)
3551{
3552 struct intel_crtc *intel_crtc;
3553 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3554
3555 /*
3556 * If the WM update hasn't changed the allocation for this_crtc (the
3557 * crtc we are currently computing the new WM values for), other
3558 * enabled crtcs will keep the same allocation and we don't need to
3559 * recompute anything for them.
3560 */
3561 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3562 return;
3563
3564 /*
3565 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3566 * other active pipes need new DDB allocation and WM values.
3567 */
3568 for_each_intel_crtc(dev, intel_crtc) {
3569 struct skl_pipe_wm pipe_wm = {};
3570 bool wm_changed;
3571
3572 if (this_crtc->pipe == intel_crtc->pipe)
3573 continue;
3574
3575 if (!intel_crtc->active)
3576 continue;
3577
3578 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
3579 &r->ddb, &pipe_wm);
3580
3581 /*
3582 * If we end up re-computing the other pipe WM values, it's
3583 * because it was really needed, so we expect the WM values to
3584 * be different.
3585 */
3586 WARN_ON(!wm_changed);
3587
3588 skl_compute_wm_results(dev, &pipe_wm, r, intel_crtc);
3589 r->dirty[intel_crtc->pipe] = true;
3590 }
3591}
3592
3593static void skl_clear_wm(struct skl_wm_values *watermarks, enum pipe pipe)
3594{
3595 watermarks->wm_linetime[pipe] = 0;
3596 memset(watermarks->plane[pipe], 0,
3597 sizeof(uint32_t) * 8 * I915_MAX_PLANES);
3598 memset(watermarks->plane_trans[pipe],
3599 0, sizeof(uint32_t) * I915_MAX_PLANES);
3600 watermarks->plane_trans[pipe][PLANE_CURSOR] = 0;
3601
3602 /* Clear ddb entries for pipe */
3603 memset(&watermarks->ddb.pipe[pipe], 0, sizeof(struct skl_ddb_entry));
3604 memset(&watermarks->ddb.plane[pipe], 0,
3605 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3606 memset(&watermarks->ddb.y_plane[pipe], 0,
3607 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3608 memset(&watermarks->ddb.plane[pipe][PLANE_CURSOR], 0,
3609 sizeof(struct skl_ddb_entry));
3610
3611}
3612
3613static void skl_update_wm(struct drm_crtc *crtc)
3614{
3615 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3616 struct drm_device *dev = crtc->dev;
3617 struct drm_i915_private *dev_priv = dev->dev_private;
3618 struct skl_wm_values *results = &dev_priv->wm.skl_results;
3619 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3620 struct skl_pipe_wm *pipe_wm = &cstate->wm.optimal.skl;
3621
3622
3623 /* Clear all dirty flags */
3624 memset(results->dirty, 0, sizeof(bool) * I915_MAX_PIPES);
3625
3626 skl_clear_wm(results, intel_crtc->pipe);
3627
3628 if (!skl_update_pipe_wm(crtc, &results->ddb, pipe_wm))
3629 return;
3630
3631 skl_compute_wm_results(dev, pipe_wm, results, intel_crtc);
3632 results->dirty[intel_crtc->pipe] = true;
3633
3634 skl_update_other_pipe_wm(dev, crtc, results);
3635 skl_write_wm_values(dev_priv, results);
3636 skl_flush_wm_values(dev_priv, results);
3637
3638 /* store the new configuration */
3639 dev_priv->wm.skl_hw = *results;
3640}
3641
3642static void ilk_compute_wm_config(struct drm_device *dev,
3643 struct intel_wm_config *config)
3644{
3645 struct intel_crtc *crtc;
3646
3647 /* Compute the currently _active_ config */
3648 for_each_intel_crtc(dev, crtc) {
3649 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
3650
3651 if (!wm->pipe_enabled)
3652 continue;
3653
3654 config->sprites_enabled |= wm->sprites_enabled;
3655 config->sprites_scaled |= wm->sprites_scaled;
3656 config->num_pipes_active++;
3657 }
3658}
3659
3660static void ilk_program_watermarks(struct intel_crtc_state *cstate)
3661{
3662 struct drm_crtc *crtc = cstate->base.crtc;
3663 struct drm_device *dev = crtc->dev;
3664 struct drm_i915_private *dev_priv = to_i915(dev);
3665 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
3666 struct ilk_wm_maximums max;
3667 struct intel_wm_config config = {};
3668 struct ilk_wm_values results = {};
3669 enum intel_ddb_partitioning partitioning;
3670
3671 ilk_compute_wm_config(dev, &config);
3672
3673 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3674 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
3675
3676 /* 5/6 split only in single pipe config on IVB+ */
3677 if (INTEL_INFO(dev)->gen >= 7 &&
3678 config.num_pipes_active == 1 && config.sprites_enabled) {
3679 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3680 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
3681
3682 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
3683 } else {
3684 best_lp_wm = &lp_wm_1_2;
3685 }
3686
3687 partitioning = (best_lp_wm == &lp_wm_1_2) ?
3688 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
3689
3690 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
3691
3692 ilk_write_wm_values(dev_priv, &results);
3693}
3694
3695static void ilk_update_wm(struct drm_crtc *crtc)
3696{
3697 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3698 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3699
3700 WARN_ON(cstate->base.active != intel_crtc->active);
3701
3702 /*
3703 * IVB workaround: must disable low power watermarks for at least
3704 * one frame before enabling scaling. LP watermarks can be re-enabled
3705 * when scaling is disabled.
3706 *
3707 * WaCxSRDisabledForSpriteScaling:ivb
3708 */
3709 if (cstate->disable_lp_wm) {
3710 ilk_disable_lp_wm(crtc->dev);
3711 intel_wait_for_vblank(crtc->dev, intel_crtc->pipe);
3712 }
3713
3714 intel_crtc->wm.active.ilk = cstate->wm.optimal.ilk;
3715
3716 ilk_program_watermarks(cstate);
3717}
3718
3719static void skl_pipe_wm_active_state(uint32_t val,
3720 struct skl_pipe_wm *active,
3721 bool is_transwm,
3722 bool is_cursor,
3723 int i,
3724 int level)
3725{
3726 bool is_enabled = (val & PLANE_WM_EN) != 0;
3727
3728 if (!is_transwm) {
3729 if (!is_cursor) {
3730 active->wm[level].plane_en[i] = is_enabled;
3731 active->wm[level].plane_res_b[i] =
3732 val & PLANE_WM_BLOCKS_MASK;
3733 active->wm[level].plane_res_l[i] =
3734 (val >> PLANE_WM_LINES_SHIFT) &
3735 PLANE_WM_LINES_MASK;
3736 } else {
3737 active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
3738 active->wm[level].plane_res_b[PLANE_CURSOR] =
3739 val & PLANE_WM_BLOCKS_MASK;
3740 active->wm[level].plane_res_l[PLANE_CURSOR] =
3741 (val >> PLANE_WM_LINES_SHIFT) &
3742 PLANE_WM_LINES_MASK;
3743 }
3744 } else {
3745 if (!is_cursor) {
3746 active->trans_wm.plane_en[i] = is_enabled;
3747 active->trans_wm.plane_res_b[i] =
3748 val & PLANE_WM_BLOCKS_MASK;
3749 active->trans_wm.plane_res_l[i] =
3750 (val >> PLANE_WM_LINES_SHIFT) &
3751 PLANE_WM_LINES_MASK;
3752 } else {
3753 active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
3754 active->trans_wm.plane_res_b[PLANE_CURSOR] =
3755 val & PLANE_WM_BLOCKS_MASK;
3756 active->trans_wm.plane_res_l[PLANE_CURSOR] =
3757 (val >> PLANE_WM_LINES_SHIFT) &
3758 PLANE_WM_LINES_MASK;
3759 }
3760 }
3761}
3762
3763static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3764{
3765 struct drm_device *dev = crtc->dev;
3766 struct drm_i915_private *dev_priv = dev->dev_private;
3767 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3768 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3769 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3770 struct skl_pipe_wm *active = &cstate->wm.optimal.skl;
3771 enum pipe pipe = intel_crtc->pipe;
3772 int level, i, max_level;
3773 uint32_t temp;
3774
3775 max_level = ilk_wm_max_level(dev);
3776
3777 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3778
3779 for (level = 0; level <= max_level; level++) {
3780 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3781 hw->plane[pipe][i][level] =
3782 I915_READ(PLANE_WM(pipe, i, level));
3783 hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
3784 }
3785
3786 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3787 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
3788 hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
3789
3790 if (!intel_crtc->active)
3791 return;
3792
3793 hw->dirty[pipe] = true;
3794
3795 active->linetime = hw->wm_linetime[pipe];
3796
3797 for (level = 0; level <= max_level; level++) {
3798 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3799 temp = hw->plane[pipe][i][level];
3800 skl_pipe_wm_active_state(temp, active, false,
3801 false, i, level);
3802 }
3803 temp = hw->plane[pipe][PLANE_CURSOR][level];
3804 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3805 }
3806
3807 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3808 temp = hw->plane_trans[pipe][i];
3809 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3810 }
3811
3812 temp = hw->plane_trans[pipe][PLANE_CURSOR];
3813 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
3814
3815 intel_crtc->wm.active.skl = *active;
3816}
3817
3818void skl_wm_get_hw_state(struct drm_device *dev)
3819{
3820 struct drm_i915_private *dev_priv = dev->dev_private;
3821 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
3822 struct drm_crtc *crtc;
3823
3824 skl_ddb_get_hw_state(dev_priv, ddb);
3825 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3826 skl_pipe_wm_get_hw_state(crtc);
3827}
3828
3829static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3830{
3831 struct drm_device *dev = crtc->dev;
3832 struct drm_i915_private *dev_priv = dev->dev_private;
3833 struct ilk_wm_values *hw = &dev_priv->wm.hw;
3834 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3835 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3836 struct intel_pipe_wm *active = &cstate->wm.optimal.ilk;
3837 enum pipe pipe = intel_crtc->pipe;
3838 static const i915_reg_t wm0_pipe_reg[] = {
3839 [PIPE_A] = WM0_PIPEA_ILK,
3840 [PIPE_B] = WM0_PIPEB_ILK,
3841 [PIPE_C] = WM0_PIPEC_IVB,
3842 };
3843
3844 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
3845 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
3846 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3847
3848 active->pipe_enabled = intel_crtc->active;
3849
3850 if (active->pipe_enabled) {
3851 u32 tmp = hw->wm_pipe[pipe];
3852
3853 /*
3854 * For active pipes LP0 watermark is marked as
3855 * enabled, and LP1+ watermaks as disabled since
3856 * we can't really reverse compute them in case
3857 * multiple pipes are active.
3858 */
3859 active->wm[0].enable = true;
3860 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3861 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3862 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3863 active->linetime = hw->wm_linetime[pipe];
3864 } else {
3865 int level, max_level = ilk_wm_max_level(dev);
3866
3867 /*
3868 * For inactive pipes, all watermark levels
3869 * should be marked as enabled but zeroed,
3870 * which is what we'd compute them to.
3871 */
3872 for (level = 0; level <= max_level; level++)
3873 active->wm[level].enable = true;
3874 }
3875
3876 intel_crtc->wm.active.ilk = *active;
3877}
3878
3879#define _FW_WM(value, plane) \
3880 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3881#define _FW_WM_VLV(value, plane) \
3882 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3883
3884static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
3885 struct vlv_wm_values *wm)
3886{
3887 enum pipe pipe;
3888 uint32_t tmp;
3889
3890 for_each_pipe(dev_priv, pipe) {
3891 tmp = I915_READ(VLV_DDL(pipe));
3892
3893 wm->ddl[pipe].primary =
3894 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3895 wm->ddl[pipe].cursor =
3896 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3897 wm->ddl[pipe].sprite[0] =
3898 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3899 wm->ddl[pipe].sprite[1] =
3900 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3901 }
3902
3903 tmp = I915_READ(DSPFW1);
3904 wm->sr.plane = _FW_WM(tmp, SR);
3905 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
3906 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
3907 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
3908
3909 tmp = I915_READ(DSPFW2);
3910 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
3911 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
3912 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
3913
3914 tmp = I915_READ(DSPFW3);
3915 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3916
3917 if (IS_CHERRYVIEW(dev_priv)) {
3918 tmp = I915_READ(DSPFW7_CHV);
3919 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3920 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3921
3922 tmp = I915_READ(DSPFW8_CHV);
3923 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
3924 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
3925
3926 tmp = I915_READ(DSPFW9_CHV);
3927 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
3928 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
3929
3930 tmp = I915_READ(DSPHOWM);
3931 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3932 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
3933 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
3934 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
3935 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3936 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3937 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
3938 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3939 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3940 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
3941 } else {
3942 tmp = I915_READ(DSPFW7);
3943 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3944 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3945
3946 tmp = I915_READ(DSPHOWM);
3947 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3948 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3949 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3950 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
3951 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3952 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3953 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
3954 }
3955}
3956
3957#undef _FW_WM
3958#undef _FW_WM_VLV
3959
3960void vlv_wm_get_hw_state(struct drm_device *dev)
3961{
3962 struct drm_i915_private *dev_priv = to_i915(dev);
3963 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
3964 struct intel_plane *plane;
3965 enum pipe pipe;
3966 u32 val;
3967
3968 vlv_read_wm_values(dev_priv, wm);
3969
3970 for_each_intel_plane(dev, plane) {
3971 switch (plane->base.type) {
3972 int sprite;
3973 case DRM_PLANE_TYPE_CURSOR:
3974 plane->wm.fifo_size = 63;
3975 break;
3976 case DRM_PLANE_TYPE_PRIMARY:
3977 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
3978 break;
3979 case DRM_PLANE_TYPE_OVERLAY:
3980 sprite = plane->plane;
3981 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
3982 break;
3983 }
3984 }
3985
3986 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
3987 wm->level = VLV_WM_LEVEL_PM2;
3988
3989 if (IS_CHERRYVIEW(dev_priv)) {
3990 mutex_lock(&dev_priv->rps.hw_lock);
3991
3992 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
3993 if (val & DSP_MAXFIFO_PM5_ENABLE)
3994 wm->level = VLV_WM_LEVEL_PM5;
3995
3996 /*
3997 * If DDR DVFS is disabled in the BIOS, Punit
3998 * will never ack the request. So if that happens
3999 * assume we don't have to enable/disable DDR DVFS
4000 * dynamically. To test that just set the REQ_ACK
4001 * bit to poke the Punit, but don't change the
4002 * HIGH/LOW bits so that we don't actually change
4003 * the current state.
4004 */
4005 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4006 val |= FORCE_DDR_FREQ_REQ_ACK;
4007 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4008
4009 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4010 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4011 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4012 "assuming DDR DVFS is disabled\n");
4013 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4014 } else {
4015 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4016 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4017 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4018 }
4019
4020 mutex_unlock(&dev_priv->rps.hw_lock);
4021 }
4022
4023 for_each_pipe(dev_priv, pipe)
4024 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4025 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4026 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4027
4028 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4029 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4030}
4031
4032void ilk_wm_get_hw_state(struct drm_device *dev)
4033{
4034 struct drm_i915_private *dev_priv = dev->dev_private;
4035 struct ilk_wm_values *hw = &dev_priv->wm.hw;
4036 struct drm_crtc *crtc;
4037
4038 for_each_crtc(dev, crtc)
4039 ilk_pipe_wm_get_hw_state(crtc);
4040
4041 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4042 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4043 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4044
4045 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
4046 if (INTEL_INFO(dev)->gen >= 7) {
4047 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4048 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4049 }
4050
4051 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4052 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4053 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4054 else if (IS_IVYBRIDGE(dev))
4055 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4056 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4057
4058 hw->enable_fbc_wm =
4059 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4060}
4061
4062/**
4063 * intel_update_watermarks - update FIFO watermark values based on current modes
4064 *
4065 * Calculate watermark values for the various WM regs based on current mode
4066 * and plane configuration.
4067 *
4068 * There are several cases to deal with here:
4069 * - normal (i.e. non-self-refresh)
4070 * - self-refresh (SR) mode
4071 * - lines are large relative to FIFO size (buffer can hold up to 2)
4072 * - lines are small relative to FIFO size (buffer can hold more than 2
4073 * lines), so need to account for TLB latency
4074 *
4075 * The normal calculation is:
4076 * watermark = dotclock * bytes per pixel * latency
4077 * where latency is platform & configuration dependent (we assume pessimal
4078 * values here).
4079 *
4080 * The SR calculation is:
4081 * watermark = (trunc(latency/line time)+1) * surface width *
4082 * bytes per pixel
4083 * where
4084 * line time = htotal / dotclock
4085 * surface width = hdisplay for normal plane and 64 for cursor
4086 * and latency is assumed to be high, as above.
4087 *
4088 * The final value programmed to the register should always be rounded up,
4089 * and include an extra 2 entries to account for clock crossings.
4090 *
4091 * We don't use the sprite, so we can ignore that. And on Crestline we have
4092 * to set the non-SR watermarks to 8.
4093 */
4094void intel_update_watermarks(struct drm_crtc *crtc)
4095{
4096 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
4097
4098 if (dev_priv->display.update_wm)
4099 dev_priv->display.update_wm(crtc);
4100}
4101
4102/*
4103 * Lock protecting IPS related data structures
4104 */
4105DEFINE_SPINLOCK(mchdev_lock);
4106
4107/* Global for IPS driver to get at the current i915 device. Protected by
4108 * mchdev_lock. */
4109static struct drm_i915_private *i915_mch_dev;
4110
4111bool ironlake_set_drps(struct drm_device *dev, u8 val)
4112{
4113 struct drm_i915_private *dev_priv = dev->dev_private;
4114 u16 rgvswctl;
4115
4116 assert_spin_locked(&mchdev_lock);
4117
4118 rgvswctl = I915_READ16(MEMSWCTL);
4119 if (rgvswctl & MEMCTL_CMD_STS) {
4120 DRM_DEBUG("gpu busy, RCS change rejected\n");
4121 return false; /* still busy with another command */
4122 }
4123
4124 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4125 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4126 I915_WRITE16(MEMSWCTL, rgvswctl);
4127 POSTING_READ16(MEMSWCTL);
4128
4129 rgvswctl |= MEMCTL_CMD_STS;
4130 I915_WRITE16(MEMSWCTL, rgvswctl);
4131
4132 return true;
4133}
4134
4135static void ironlake_enable_drps(struct drm_device *dev)
4136{
4137 struct drm_i915_private *dev_priv = dev->dev_private;
4138 u32 rgvmodectl;
4139 u8 fmax, fmin, fstart, vstart;
4140
4141 spin_lock_irq(&mchdev_lock);
4142
4143 rgvmodectl = I915_READ(MEMMODECTL);
4144
4145 /* Enable temp reporting */
4146 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4147 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4148
4149 /* 100ms RC evaluation intervals */
4150 I915_WRITE(RCUPEI, 100000);
4151 I915_WRITE(RCDNEI, 100000);
4152
4153 /* Set max/min thresholds to 90ms and 80ms respectively */
4154 I915_WRITE(RCBMAXAVG, 90000);
4155 I915_WRITE(RCBMINAVG, 80000);
4156
4157 I915_WRITE(MEMIHYST, 1);
4158
4159 /* Set up min, max, and cur for interrupt handling */
4160 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4161 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4162 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4163 MEMMODE_FSTART_SHIFT;
4164
4165 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
4166 PXVFREQ_PX_SHIFT;
4167
4168 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4169 dev_priv->ips.fstart = fstart;
4170
4171 dev_priv->ips.max_delay = fstart;
4172 dev_priv->ips.min_delay = fmin;
4173 dev_priv->ips.cur_delay = fstart;
4174
4175 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4176 fmax, fmin, fstart);
4177
4178 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4179
4180 /*
4181 * Interrupts will be enabled in ironlake_irq_postinstall
4182 */
4183
4184 I915_WRITE(VIDSTART, vstart);
4185 POSTING_READ(VIDSTART);
4186
4187 rgvmodectl |= MEMMODE_SWMODE_EN;
4188 I915_WRITE(MEMMODECTL, rgvmodectl);
4189
4190 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
4191 DRM_ERROR("stuck trying to change perf mode\n");
4192 mdelay(1);
4193
4194 ironlake_set_drps(dev, fstart);
4195
4196 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4197 I915_READ(DDREC) + I915_READ(CSIEC);
4198 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
4199 dev_priv->ips.last_count2 = I915_READ(GFXEC);
4200 dev_priv->ips.last_time2 = ktime_get_raw_ns();
4201
4202 spin_unlock_irq(&mchdev_lock);
4203}
4204
4205static void ironlake_disable_drps(struct drm_device *dev)
4206{
4207 struct drm_i915_private *dev_priv = dev->dev_private;
4208 u16 rgvswctl;
4209
4210 spin_lock_irq(&mchdev_lock);
4211
4212 rgvswctl = I915_READ16(MEMSWCTL);
4213
4214 /* Ack interrupts, disable EFC interrupt */
4215 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4216 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4217 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4218 I915_WRITE(DEIIR, DE_PCU_EVENT);
4219 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4220
4221 /* Go back to the starting frequency */
4222 ironlake_set_drps(dev, dev_priv->ips.fstart);
4223 mdelay(1);
4224 rgvswctl |= MEMCTL_CMD_STS;
4225 I915_WRITE(MEMSWCTL, rgvswctl);
4226 mdelay(1);
4227
4228 spin_unlock_irq(&mchdev_lock);
4229}
4230
4231/* There's a funny hw issue where the hw returns all 0 when reading from
4232 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4233 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4234 * all limits and the gpu stuck at whatever frequency it is at atm).
4235 */
4236static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
4237{
4238 u32 limits;
4239
4240 /* Only set the down limit when we've reached the lowest level to avoid
4241 * getting more interrupts, otherwise leave this clear. This prevents a
4242 * race in the hw when coming out of rc6: There's a tiny window where
4243 * the hw runs at the minimal clock before selecting the desired
4244 * frequency, if the down threshold expires in that window we will not
4245 * receive a down interrupt. */
4246 if (IS_GEN9(dev_priv->dev)) {
4247 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4248 if (val <= dev_priv->rps.min_freq_softlimit)
4249 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4250 } else {
4251 limits = dev_priv->rps.max_freq_softlimit << 24;
4252 if (val <= dev_priv->rps.min_freq_softlimit)
4253 limits |= dev_priv->rps.min_freq_softlimit << 16;
4254 }
4255
4256 return limits;
4257}
4258
4259static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4260{
4261 int new_power;
4262 u32 threshold_up = 0, threshold_down = 0; /* in % */
4263 u32 ei_up = 0, ei_down = 0;
4264
4265 new_power = dev_priv->rps.power;
4266 switch (dev_priv->rps.power) {
4267 case LOW_POWER:
4268 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
4269 new_power = BETWEEN;
4270 break;
4271
4272 case BETWEEN:
4273 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
4274 new_power = LOW_POWER;
4275 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
4276 new_power = HIGH_POWER;
4277 break;
4278
4279 case HIGH_POWER:
4280 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
4281 new_power = BETWEEN;
4282 break;
4283 }
4284 /* Max/min bins are special */
4285 if (val <= dev_priv->rps.min_freq_softlimit)
4286 new_power = LOW_POWER;
4287 if (val >= dev_priv->rps.max_freq_softlimit)
4288 new_power = HIGH_POWER;
4289 if (new_power == dev_priv->rps.power)
4290 return;
4291
4292 /* Note the units here are not exactly 1us, but 1280ns. */
4293 switch (new_power) {
4294 case LOW_POWER:
4295 /* Upclock if more than 95% busy over 16ms */
4296 ei_up = 16000;
4297 threshold_up = 95;
4298
4299 /* Downclock if less than 85% busy over 32ms */
4300 ei_down = 32000;
4301 threshold_down = 85;
4302 break;
4303
4304 case BETWEEN:
4305 /* Upclock if more than 90% busy over 13ms */
4306 ei_up = 13000;
4307 threshold_up = 90;
4308
4309 /* Downclock if less than 75% busy over 32ms */
4310 ei_down = 32000;
4311 threshold_down = 75;
4312 break;
4313
4314 case HIGH_POWER:
4315 /* Upclock if more than 85% busy over 10ms */
4316 ei_up = 10000;
4317 threshold_up = 85;
4318
4319 /* Downclock if less than 60% busy over 32ms */
4320 ei_down = 32000;
4321 threshold_down = 60;
4322 break;
4323 }
4324
4325 I915_WRITE(GEN6_RP_UP_EI,
4326 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4327 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4328 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4329
4330 I915_WRITE(GEN6_RP_DOWN_EI,
4331 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4332 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4333 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4334
4335 I915_WRITE(GEN6_RP_CONTROL,
4336 GEN6_RP_MEDIA_TURBO |
4337 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4338 GEN6_RP_MEDIA_IS_GFX |
4339 GEN6_RP_ENABLE |
4340 GEN6_RP_UP_BUSY_AVG |
4341 GEN6_RP_DOWN_IDLE_AVG);
4342
4343 dev_priv->rps.power = new_power;
4344 dev_priv->rps.up_threshold = threshold_up;
4345 dev_priv->rps.down_threshold = threshold_down;
4346 dev_priv->rps.last_adj = 0;
4347}
4348
4349static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4350{
4351 u32 mask = 0;
4352
4353 if (val > dev_priv->rps.min_freq_softlimit)
4354 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
4355 if (val < dev_priv->rps.max_freq_softlimit)
4356 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
4357
4358 mask &= dev_priv->pm_rps_events;
4359
4360 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
4361}
4362
4363/* gen6_set_rps is called to update the frequency request, but should also be
4364 * called when the range (min_delay and max_delay) is modified so that we can
4365 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
4366static void gen6_set_rps(struct drm_device *dev, u8 val)
4367{
4368 struct drm_i915_private *dev_priv = dev->dev_private;
4369
4370 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
4371 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1))
4372 return;
4373
4374 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4375 WARN_ON(val > dev_priv->rps.max_freq);
4376 WARN_ON(val < dev_priv->rps.min_freq);
4377
4378 /* min/max delay may still have been modified so be sure to
4379 * write the limits value.
4380 */
4381 if (val != dev_priv->rps.cur_freq) {
4382 gen6_set_rps_thresholds(dev_priv, val);
4383
4384 if (IS_GEN9(dev))
4385 I915_WRITE(GEN6_RPNSWREQ,
4386 GEN9_FREQUENCY(val));
4387 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4388 I915_WRITE(GEN6_RPNSWREQ,
4389 HSW_FREQUENCY(val));
4390 else
4391 I915_WRITE(GEN6_RPNSWREQ,
4392 GEN6_FREQUENCY(val) |
4393 GEN6_OFFSET(0) |
4394 GEN6_AGGRESSIVE_TURBO);
4395 }
4396
4397 /* Make sure we continue to get interrupts
4398 * until we hit the minimum or maximum frequencies.
4399 */
4400 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
4401 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4402
4403 POSTING_READ(GEN6_RPNSWREQ);
4404
4405 dev_priv->rps.cur_freq = val;
4406 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4407}
4408
4409static void valleyview_set_rps(struct drm_device *dev, u8 val)
4410{
4411 struct drm_i915_private *dev_priv = dev->dev_private;
4412
4413 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4414 WARN_ON(val > dev_priv->rps.max_freq);
4415 WARN_ON(val < dev_priv->rps.min_freq);
4416
4417 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4418 "Odd GPU freq value\n"))
4419 val &= ~1;
4420
4421 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4422
4423 if (val != dev_priv->rps.cur_freq) {
4424 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
4425 if (!IS_CHERRYVIEW(dev_priv))
4426 gen6_set_rps_thresholds(dev_priv, val);
4427 }
4428
4429 dev_priv->rps.cur_freq = val;
4430 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4431}
4432
4433/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
4434 *
4435 * * If Gfx is Idle, then
4436 * 1. Forcewake Media well.
4437 * 2. Request idle freq.
4438 * 3. Release Forcewake of Media well.
4439*/
4440static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4441{
4442 u32 val = dev_priv->rps.idle_freq;
4443
4444 if (dev_priv->rps.cur_freq <= val)
4445 return;
4446
4447 /* Wake up the media well, as that takes a lot less
4448 * power than the Render well. */
4449 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
4450 valleyview_set_rps(dev_priv->dev, val);
4451 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
4452}
4453
4454void gen6_rps_busy(struct drm_i915_private *dev_priv)
4455{
4456 mutex_lock(&dev_priv->rps.hw_lock);
4457 if (dev_priv->rps.enabled) {
4458 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4459 gen6_rps_reset_ei(dev_priv);
4460 I915_WRITE(GEN6_PMINTRMSK,
4461 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4462 }
4463 mutex_unlock(&dev_priv->rps.hw_lock);
4464}
4465
4466void gen6_rps_idle(struct drm_i915_private *dev_priv)
4467{
4468 struct drm_device *dev = dev_priv->dev;
4469
4470 mutex_lock(&dev_priv->rps.hw_lock);
4471 if (dev_priv->rps.enabled) {
4472 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
4473 vlv_set_rps_idle(dev_priv);
4474 else
4475 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4476 dev_priv->rps.last_adj = 0;
4477 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
4478 }
4479 mutex_unlock(&dev_priv->rps.hw_lock);
4480
4481 spin_lock(&dev_priv->rps.client_lock);
4482 while (!list_empty(&dev_priv->rps.clients))
4483 list_del_init(dev_priv->rps.clients.next);
4484 spin_unlock(&dev_priv->rps.client_lock);
4485}
4486
4487void gen6_rps_boost(struct drm_i915_private *dev_priv,
4488 struct intel_rps_client *rps,
4489 unsigned long submitted)
4490{
4491 /* This is intentionally racy! We peek at the state here, then
4492 * validate inside the RPS worker.
4493 */
4494 if (!(dev_priv->mm.busy &&
4495 dev_priv->rps.enabled &&
4496 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4497 return;
4498
4499 /* Force a RPS boost (and don't count it against the client) if
4500 * the GPU is severely congested.
4501 */
4502 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
4503 rps = NULL;
4504
4505 spin_lock(&dev_priv->rps.client_lock);
4506 if (rps == NULL || list_empty(&rps->link)) {
4507 spin_lock_irq(&dev_priv->irq_lock);
4508 if (dev_priv->rps.interrupts_enabled) {
4509 dev_priv->rps.client_boost = true;
4510 queue_work(dev_priv->wq, &dev_priv->rps.work);
4511 }
4512 spin_unlock_irq(&dev_priv->irq_lock);
4513
4514 if (rps != NULL) {
4515 list_add(&rps->link, &dev_priv->rps.clients);
4516 rps->boosts++;
4517 } else
4518 dev_priv->rps.boosts++;
4519 }
4520 spin_unlock(&dev_priv->rps.client_lock);
4521}
4522
4523void intel_set_rps(struct drm_device *dev, u8 val)
4524{
4525 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
4526 valleyview_set_rps(dev, val);
4527 else
4528 gen6_set_rps(dev, val);
4529}
4530
4531static void gen9_disable_rps(struct drm_device *dev)
4532{
4533 struct drm_i915_private *dev_priv = dev->dev_private;
4534
4535 I915_WRITE(GEN6_RC_CONTROL, 0);
4536 I915_WRITE(GEN9_PG_ENABLE, 0);
4537}
4538
4539static void gen6_disable_rps(struct drm_device *dev)
4540{
4541 struct drm_i915_private *dev_priv = dev->dev_private;
4542
4543 I915_WRITE(GEN6_RC_CONTROL, 0);
4544 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
4545}
4546
4547static void cherryview_disable_rps(struct drm_device *dev)
4548{
4549 struct drm_i915_private *dev_priv = dev->dev_private;
4550
4551 I915_WRITE(GEN6_RC_CONTROL, 0);
4552}
4553
4554static void valleyview_disable_rps(struct drm_device *dev)
4555{
4556 struct drm_i915_private *dev_priv = dev->dev_private;
4557
4558 /* we're doing forcewake before Disabling RC6,
4559 * This what the BIOS expects when going into suspend */
4560 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4561
4562 I915_WRITE(GEN6_RC_CONTROL, 0);
4563
4564 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4565}
4566
4567static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4568{
4569 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
4570 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4571 mode = GEN6_RC_CTL_RC6_ENABLE;
4572 else
4573 mode = 0;
4574 }
4575 if (HAS_RC6p(dev))
4576 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
4577 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
4578 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
4579 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
4580
4581 else
4582 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
4583 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
4584}
4585
4586static bool bxt_check_bios_rc6_setup(const struct drm_device *dev)
4587{
4588 struct drm_i915_private *dev_priv = dev->dev_private;
4589 bool enable_rc6 = true;
4590 unsigned long rc6_ctx_base;
4591
4592 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
4593 DRM_DEBUG_KMS("RC6 Base location not set properly.\n");
4594 enable_rc6 = false;
4595 }
4596
4597 /*
4598 * The exact context size is not known for BXT, so assume a page size
4599 * for this check.
4600 */
4601 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
4602 if (!((rc6_ctx_base >= dev_priv->gtt.stolen_reserved_base) &&
4603 (rc6_ctx_base + PAGE_SIZE <= dev_priv->gtt.stolen_reserved_base +
4604 dev_priv->gtt.stolen_reserved_size))) {
4605 DRM_DEBUG_KMS("RC6 Base address not as expected.\n");
4606 enable_rc6 = false;
4607 }
4608
4609 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
4610 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
4611 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
4612 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
4613 DRM_DEBUG_KMS("Engine Idle wait time not set properly.\n");
4614 enable_rc6 = false;
4615 }
4616
4617 if (!(I915_READ(GEN6_RC_CONTROL) & (GEN6_RC_CTL_RC6_ENABLE |
4618 GEN6_RC_CTL_HW_ENABLE)) &&
4619 ((I915_READ(GEN6_RC_CONTROL) & GEN6_RC_CTL_HW_ENABLE) ||
4620 !(I915_READ(GEN6_RC_STATE) & RC6_STATE))) {
4621 DRM_DEBUG_KMS("HW/SW RC6 is not enabled by BIOS.\n");
4622 enable_rc6 = false;
4623 }
4624
4625 return enable_rc6;
4626}
4627
4628int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
4629{
4630 /* No RC6 before Ironlake and code is gone for ilk. */
4631 if (INTEL_INFO(dev)->gen < 6)
4632 return 0;
4633
4634 if (!enable_rc6)
4635 return 0;
4636
4637 if (IS_BROXTON(dev) && !bxt_check_bios_rc6_setup(dev)) {
4638 DRM_INFO("RC6 disabled by BIOS\n");
4639 return 0;
4640 }
4641
4642 /* Respect the kernel parameter if it is set */
4643 if (enable_rc6 >= 0) {
4644 int mask;
4645
4646 if (HAS_RC6p(dev))
4647 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4648 INTEL_RC6pp_ENABLE;
4649 else
4650 mask = INTEL_RC6_ENABLE;
4651
4652 if ((enable_rc6 & mask) != enable_rc6)
4653 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4654 enable_rc6 & mask, enable_rc6, mask);
4655
4656 return enable_rc6 & mask;
4657 }
4658
4659 if (IS_IVYBRIDGE(dev))
4660 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
4661
4662 return INTEL_RC6_ENABLE;
4663}
4664
4665int intel_enable_rc6(const struct drm_device *dev)
4666{
4667 return i915.enable_rc6;
4668}
4669
4670static void gen6_init_rps_frequencies(struct drm_device *dev)
4671{
4672 struct drm_i915_private *dev_priv = dev->dev_private;
4673 uint32_t rp_state_cap;
4674 u32 ddcc_status = 0;
4675 int ret;
4676
4677 /* All of these values are in units of 50MHz */
4678 dev_priv->rps.cur_freq = 0;
4679 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
4680 if (IS_BROXTON(dev)) {
4681 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
4682 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
4683 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4684 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
4685 } else {
4686 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4687 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4688 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4689 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4690 }
4691
4692 /* hw_max = RP0 until we check for overclocking */
4693 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4694
4695 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
4696 if (IS_HASWELL(dev) || IS_BROADWELL(dev) ||
4697 IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
4698 ret = sandybridge_pcode_read(dev_priv,
4699 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4700 &ddcc_status);
4701 if (0 == ret)
4702 dev_priv->rps.efficient_freq =
4703 clamp_t(u8,
4704 ((ddcc_status >> 8) & 0xff),
4705 dev_priv->rps.min_freq,
4706 dev_priv->rps.max_freq);
4707 }
4708
4709 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
4710 /* Store the frequency values in 16.66 MHZ units, which is
4711 the natural hardware unit for SKL */
4712 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4713 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4714 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4715 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
4716 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
4717 }
4718
4719 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4720
4721 /* Preserve min/max settings in case of re-init */
4722 if (dev_priv->rps.max_freq_softlimit == 0)
4723 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4724
4725 if (dev_priv->rps.min_freq_softlimit == 0) {
4726 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4727 dev_priv->rps.min_freq_softlimit =
4728 max_t(int, dev_priv->rps.efficient_freq,
4729 intel_freq_opcode(dev_priv, 450));
4730 else
4731 dev_priv->rps.min_freq_softlimit =
4732 dev_priv->rps.min_freq;
4733 }
4734}
4735
4736/* See the Gen9_GT_PM_Programming_Guide doc for the below */
4737static void gen9_enable_rps(struct drm_device *dev)
4738{
4739 struct drm_i915_private *dev_priv = dev->dev_private;
4740
4741 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4742
4743 gen6_init_rps_frequencies(dev);
4744
4745 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
4746 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
4747 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4748 return;
4749 }
4750
4751 /* Program defaults and thresholds for RPS*/
4752 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4753 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
4754
4755 /* 1 second timeout*/
4756 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4757 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4758
4759 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
4760
4761 /* Leaning on the below call to gen6_set_rps to program/setup the
4762 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4763 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4764 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4765 gen6_set_rps(dev_priv->dev, dev_priv->rps.min_freq_softlimit);
4766
4767 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4768}
4769
4770static void gen9_enable_rc6(struct drm_device *dev)
4771{
4772 struct drm_i915_private *dev_priv = dev->dev_private;
4773 struct intel_engine_cs *ring;
4774 uint32_t rc6_mask = 0;
4775 int unused;
4776
4777 /* 1a: Software RC state - RC0 */
4778 I915_WRITE(GEN6_RC_STATE, 0);
4779
4780 /* 1b: Get forcewake during program sequence. Although the driver
4781 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4782 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4783
4784 /* 2a: Disable RC states. */
4785 I915_WRITE(GEN6_RC_CONTROL, 0);
4786
4787 /* 2b: Program RC6 thresholds.*/
4788
4789 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
4790 if (IS_SKYLAKE(dev))
4791 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
4792 else
4793 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
4794 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4795 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4796 for_each_ring(ring, dev_priv, unused)
4797 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4798
4799 if (HAS_GUC_UCODE(dev))
4800 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
4801
4802 I915_WRITE(GEN6_RC_SLEEP, 0);
4803
4804 /* 2c: Program Coarse Power Gating Policies. */
4805 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4806 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4807
4808 /* 3a: Enable RC6 */
4809 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4810 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4811 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
4812 /* WaRsUseTimeoutMode */
4813 if (IS_SKL_REVID(dev, 0, SKL_REVID_D0) ||
4814 IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
4815 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
4816 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4817 GEN7_RC_CTL_TO_MODE |
4818 rc6_mask);
4819 } else {
4820 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
4821 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4822 GEN6_RC_CTL_EI_MODE(1) |
4823 rc6_mask);
4824 }
4825
4826 /*
4827 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
4828 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
4829 */
4830 if (NEEDS_WaRsDisableCoarsePowerGating(dev))
4831 I915_WRITE(GEN9_PG_ENABLE, 0);
4832 else
4833 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4834 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
4835
4836 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4837
4838}
4839
4840static void gen8_enable_rps(struct drm_device *dev)
4841{
4842 struct drm_i915_private *dev_priv = dev->dev_private;
4843 struct intel_engine_cs *ring;
4844 uint32_t rc6_mask = 0;
4845 int unused;
4846
4847 /* 1a: Software RC state - RC0 */
4848 I915_WRITE(GEN6_RC_STATE, 0);
4849
4850 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4851 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
4852 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4853
4854 /* 2a: Disable RC states. */
4855 I915_WRITE(GEN6_RC_CONTROL, 0);
4856
4857 /* Initialize rps frequencies */
4858 gen6_init_rps_frequencies(dev);
4859
4860 /* 2b: Program RC6 thresholds.*/
4861 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4862 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4863 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
4864 for_each_ring(ring, dev_priv, unused)
4865 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4866 I915_WRITE(GEN6_RC_SLEEP, 0);
4867 if (IS_BROADWELL(dev))
4868 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4869 else
4870 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
4871
4872 /* 3: Enable RC6 */
4873 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4874 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
4875 intel_print_rc6_info(dev, rc6_mask);
4876 if (IS_BROADWELL(dev))
4877 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4878 GEN7_RC_CTL_TO_MODE |
4879 rc6_mask);
4880 else
4881 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4882 GEN6_RC_CTL_EI_MODE(1) |
4883 rc6_mask);
4884
4885 /* 4 Program defaults and thresholds for RPS*/
4886 I915_WRITE(GEN6_RPNSWREQ,
4887 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4888 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4889 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4890 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4891 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
4892
4893 /* Docs recommend 900MHz, and 300 MHz respectively */
4894 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4895 dev_priv->rps.max_freq_softlimit << 24 |
4896 dev_priv->rps.min_freq_softlimit << 16);
4897
4898 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4899 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4900 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4901 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
4902
4903 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4904
4905 /* 5: Enable RPS */
4906 I915_WRITE(GEN6_RP_CONTROL,
4907 GEN6_RP_MEDIA_TURBO |
4908 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4909 GEN6_RP_MEDIA_IS_GFX |
4910 GEN6_RP_ENABLE |
4911 GEN6_RP_UP_BUSY_AVG |
4912 GEN6_RP_DOWN_IDLE_AVG);
4913
4914 /* 6: Ring frequency + overclocking (our driver does this later */
4915
4916 dev_priv->rps.power = HIGH_POWER; /* force a reset */
4917 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
4918
4919 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4920}
4921
4922static void gen6_enable_rps(struct drm_device *dev)
4923{
4924 struct drm_i915_private *dev_priv = dev->dev_private;
4925 struct intel_engine_cs *ring;
4926 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
4927 u32 gtfifodbg;
4928 int rc6_mode;
4929 int i, ret;
4930
4931 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
4932
4933 /* Here begins a magic sequence of register writes to enable
4934 * auto-downclocking.
4935 *
4936 * Perhaps there might be some value in exposing these to
4937 * userspace...
4938 */
4939 I915_WRITE(GEN6_RC_STATE, 0);
4940
4941 /* Clear the DBG now so we don't confuse earlier errors */
4942 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
4943 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
4944 I915_WRITE(GTFIFODBG, gtfifodbg);
4945 }
4946
4947 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4948
4949 /* Initialize rps frequencies */
4950 gen6_init_rps_frequencies(dev);
4951
4952 /* disable the counters and set deterministic thresholds */
4953 I915_WRITE(GEN6_RC_CONTROL, 0);
4954
4955 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
4956 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
4957 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
4958 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
4959 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
4960
4961 for_each_ring(ring, dev_priv, i)
4962 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
4963
4964 I915_WRITE(GEN6_RC_SLEEP, 0);
4965 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
4966 if (IS_IVYBRIDGE(dev))
4967 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
4968 else
4969 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
4970 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
4971 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
4972
4973 /* Check if we are enabling RC6 */
4974 rc6_mode = intel_enable_rc6(dev_priv->dev);
4975 if (rc6_mode & INTEL_RC6_ENABLE)
4976 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
4977
4978 /* We don't use those on Haswell */
4979 if (!IS_HASWELL(dev)) {
4980 if (rc6_mode & INTEL_RC6p_ENABLE)
4981 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
4982
4983 if (rc6_mode & INTEL_RC6pp_ENABLE)
4984 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
4985 }
4986
4987 intel_print_rc6_info(dev, rc6_mask);
4988
4989 I915_WRITE(GEN6_RC_CONTROL,
4990 rc6_mask |
4991 GEN6_RC_CTL_EI_MODE(1) |
4992 GEN6_RC_CTL_HW_ENABLE);
4993
4994 /* Power down if completely idle for over 50ms */
4995 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
4996 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
4997
4998 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
4999 if (ret)
5000 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
5001
5002 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5003 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
5004 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
5005 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
5006 (pcu_mbox & 0xff) * 50);
5007 dev_priv->rps.max_freq = pcu_mbox & 0xff;
5008 }
5009
5010 dev_priv->rps.power = HIGH_POWER; /* force a reset */
5011 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
5012
5013 rc6vids = 0;
5014 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
5015 if (IS_GEN6(dev) && ret) {
5016 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
5017 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
5018 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5019 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5020 rc6vids &= 0xffff00;
5021 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5022 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5023 if (ret)
5024 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5025 }
5026
5027 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5028}
5029
5030static void __gen6_update_ring_freq(struct drm_device *dev)
5031{
5032 struct drm_i915_private *dev_priv = dev->dev_private;
5033 int min_freq = 15;
5034 unsigned int gpu_freq;
5035 unsigned int max_ia_freq, min_ring_freq;
5036 unsigned int max_gpu_freq, min_gpu_freq;
5037 int scaling_factor = 180;
5038 struct cpufreq_policy *policy;
5039
5040 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5041
5042 policy = cpufreq_cpu_get(0);
5043 if (policy) {
5044 max_ia_freq = policy->cpuinfo.max_freq;
5045 cpufreq_cpu_put(policy);
5046 } else {
5047 /*
5048 * Default to measured freq if none found, PCU will ensure we
5049 * don't go over
5050 */
5051 max_ia_freq = tsc_khz;
5052 }
5053
5054 /* Convert from kHz to MHz */
5055 max_ia_freq /= 1000;
5056
5057 min_ring_freq = I915_READ(DCLK) & 0xf;
5058 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5059 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
5060
5061 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
5062 /* Convert GT frequency to 50 HZ units */
5063 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5064 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5065 } else {
5066 min_gpu_freq = dev_priv->rps.min_freq;
5067 max_gpu_freq = dev_priv->rps.max_freq;
5068 }
5069
5070 /*
5071 * For each potential GPU frequency, load a ring frequency we'd like
5072 * to use for memory access. We do this by specifying the IA frequency
5073 * the PCU should use as a reference to determine the ring frequency.
5074 */
5075 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5076 int diff = max_gpu_freq - gpu_freq;
5077 unsigned int ia_freq = 0, ring_freq = 0;
5078
5079 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
5080 /*
5081 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5082 * No floor required for ring frequency on SKL.
5083 */
5084 ring_freq = gpu_freq;
5085 } else if (INTEL_INFO(dev)->gen >= 8) {
5086 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5087 ring_freq = max(min_ring_freq, gpu_freq);
5088 } else if (IS_HASWELL(dev)) {
5089 ring_freq = mult_frac(gpu_freq, 5, 4);
5090 ring_freq = max(min_ring_freq, ring_freq);
5091 /* leave ia_freq as the default, chosen by cpufreq */
5092 } else {
5093 /* On older processors, there is no separate ring
5094 * clock domain, so in order to boost the bandwidth
5095 * of the ring, we need to upclock the CPU (ia_freq).
5096 *
5097 * For GPU frequencies less than 750MHz,
5098 * just use the lowest ring freq.
5099 */
5100 if (gpu_freq < min_freq)
5101 ia_freq = 800;
5102 else
5103 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5104 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5105 }
5106
5107 sandybridge_pcode_write(dev_priv,
5108 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
5109 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5110 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5111 gpu_freq);
5112 }
5113}
5114
5115void gen6_update_ring_freq(struct drm_device *dev)
5116{
5117 struct drm_i915_private *dev_priv = dev->dev_private;
5118
5119 if (!HAS_CORE_RING_FREQ(dev))
5120 return;
5121
5122 mutex_lock(&dev_priv->rps.hw_lock);
5123 __gen6_update_ring_freq(dev);
5124 mutex_unlock(&dev_priv->rps.hw_lock);
5125}
5126
5127static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
5128{
5129 struct drm_device *dev = dev_priv->dev;
5130 u32 val, rp0;
5131
5132 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5133
5134 switch (INTEL_INFO(dev)->eu_total) {
5135 case 8:
5136 /* (2 * 4) config */
5137 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5138 break;
5139 case 12:
5140 /* (2 * 6) config */
5141 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5142 break;
5143 case 16:
5144 /* (2 * 8) config */
5145 default:
5146 /* Setting (2 * 8) Min RP0 for any other combination */
5147 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5148 break;
5149 }
5150
5151 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5152
5153 return rp0;
5154}
5155
5156static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5157{
5158 u32 val, rpe;
5159
5160 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5161 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5162
5163 return rpe;
5164}
5165
5166static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5167{
5168 u32 val, rp1;
5169
5170 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5171 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5172
5173 return rp1;
5174}
5175
5176static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5177{
5178 u32 val, rp1;
5179
5180 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5181
5182 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5183
5184 return rp1;
5185}
5186
5187static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
5188{
5189 u32 val, rp0;
5190
5191 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5192
5193 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5194 /* Clamp to max */
5195 rp0 = min_t(u32, rp0, 0xea);
5196
5197 return rp0;
5198}
5199
5200static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5201{
5202 u32 val, rpe;
5203
5204 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
5205 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
5206 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
5207 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5208
5209 return rpe;
5210}
5211
5212static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
5213{
5214 u32 val;
5215
5216 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5217 /*
5218 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5219 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5220 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5221 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5222 * to make sure it matches what Punit accepts.
5223 */
5224 return max_t(u32, val, 0xc0);
5225}
5226
5227/* Check that the pctx buffer wasn't move under us. */
5228static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5229{
5230 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5231
5232 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5233 dev_priv->vlv_pctx->stolen->start);
5234}
5235
5236
5237/* Check that the pcbr address is not empty. */
5238static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5239{
5240 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5241
5242 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5243}
5244
5245static void cherryview_setup_pctx(struct drm_device *dev)
5246{
5247 struct drm_i915_private *dev_priv = dev->dev_private;
5248 unsigned long pctx_paddr, paddr;
5249 struct i915_gtt *gtt = &dev_priv->gtt;
5250 u32 pcbr;
5251 int pctx_size = 32*1024;
5252
5253 pcbr = I915_READ(VLV_PCBR);
5254 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
5255 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5256 paddr = (dev_priv->mm.stolen_base +
5257 (gtt->stolen_size - pctx_size));
5258
5259 pctx_paddr = (paddr & (~4095));
5260 I915_WRITE(VLV_PCBR, pctx_paddr);
5261 }
5262
5263 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5264}
5265
5266static void valleyview_setup_pctx(struct drm_device *dev)
5267{
5268 struct drm_i915_private *dev_priv = dev->dev_private;
5269 struct drm_i915_gem_object *pctx;
5270 unsigned long pctx_paddr;
5271 u32 pcbr;
5272 int pctx_size = 24*1024;
5273
5274 mutex_lock(&dev->struct_mutex);
5275
5276 pcbr = I915_READ(VLV_PCBR);
5277 if (pcbr) {
5278 /* BIOS set it up already, grab the pre-alloc'd space */
5279 int pcbr_offset;
5280
5281 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5282 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5283 pcbr_offset,
5284 I915_GTT_OFFSET_NONE,
5285 pctx_size);
5286 goto out;
5287 }
5288
5289 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5290
5291 /*
5292 * From the Gunit register HAS:
5293 * The Gfx driver is expected to program this register and ensure
5294 * proper allocation within Gfx stolen memory. For example, this
5295 * register should be programmed such than the PCBR range does not
5296 * overlap with other ranges, such as the frame buffer, protected
5297 * memory, or any other relevant ranges.
5298 */
5299 pctx = i915_gem_object_create_stolen(dev, pctx_size);
5300 if (!pctx) {
5301 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
5302 goto out;
5303 }
5304
5305 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5306 I915_WRITE(VLV_PCBR, pctx_paddr);
5307
5308out:
5309 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
5310 dev_priv->vlv_pctx = pctx;
5311 mutex_unlock(&dev->struct_mutex);
5312}
5313
5314static void valleyview_cleanup_pctx(struct drm_device *dev)
5315{
5316 struct drm_i915_private *dev_priv = dev->dev_private;
5317
5318 if (WARN_ON(!dev_priv->vlv_pctx))
5319 return;
5320
5321 drm_gem_object_unreference_unlocked(&dev_priv->vlv_pctx->base);
5322 dev_priv->vlv_pctx = NULL;
5323}
5324
5325static void valleyview_init_gt_powersave(struct drm_device *dev)
5326{
5327 struct drm_i915_private *dev_priv = dev->dev_private;
5328 u32 val;
5329
5330 valleyview_setup_pctx(dev);
5331
5332 mutex_lock(&dev_priv->rps.hw_lock);
5333
5334 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5335 switch ((val >> 6) & 3) {
5336 case 0:
5337 case 1:
5338 dev_priv->mem_freq = 800;
5339 break;
5340 case 2:
5341 dev_priv->mem_freq = 1066;
5342 break;
5343 case 3:
5344 dev_priv->mem_freq = 1333;
5345 break;
5346 }
5347 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5348
5349 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5350 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5351 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5352 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5353 dev_priv->rps.max_freq);
5354
5355 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5356 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5357 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5358 dev_priv->rps.efficient_freq);
5359
5360 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5361 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
5362 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5363 dev_priv->rps.rp1_freq);
5364
5365 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5366 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5367 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5368 dev_priv->rps.min_freq);
5369
5370 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5371
5372 /* Preserve min/max settings in case of re-init */
5373 if (dev_priv->rps.max_freq_softlimit == 0)
5374 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5375
5376 if (dev_priv->rps.min_freq_softlimit == 0)
5377 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5378
5379 mutex_unlock(&dev_priv->rps.hw_lock);
5380}
5381
5382static void cherryview_init_gt_powersave(struct drm_device *dev)
5383{
5384 struct drm_i915_private *dev_priv = dev->dev_private;
5385 u32 val;
5386
5387 cherryview_setup_pctx(dev);
5388
5389 mutex_lock(&dev_priv->rps.hw_lock);
5390
5391 mutex_lock(&dev_priv->sb_lock);
5392 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
5393 mutex_unlock(&dev_priv->sb_lock);
5394
5395 switch ((val >> 2) & 0x7) {
5396 case 3:
5397 dev_priv->mem_freq = 2000;
5398 break;
5399 default:
5400 dev_priv->mem_freq = 1600;
5401 break;
5402 }
5403 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
5404
5405 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5406 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5407 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
5408 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
5409 dev_priv->rps.max_freq);
5410
5411 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5412 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
5413 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5414 dev_priv->rps.efficient_freq);
5415
5416 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5417 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
5418 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
5419 dev_priv->rps.rp1_freq);
5420
5421 /* PUnit validated range is only [RPe, RP0] */
5422 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
5423 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
5424 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
5425 dev_priv->rps.min_freq);
5426
5427 WARN_ONCE((dev_priv->rps.max_freq |
5428 dev_priv->rps.efficient_freq |
5429 dev_priv->rps.rp1_freq |
5430 dev_priv->rps.min_freq) & 1,
5431 "Odd GPU freq values\n");
5432
5433 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5434
5435 /* Preserve min/max settings in case of re-init */
5436 if (dev_priv->rps.max_freq_softlimit == 0)
5437 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5438
5439 if (dev_priv->rps.min_freq_softlimit == 0)
5440 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5441
5442 mutex_unlock(&dev_priv->rps.hw_lock);
5443}
5444
5445static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5446{
5447 valleyview_cleanup_pctx(dev);
5448}
5449
5450static void cherryview_enable_rps(struct drm_device *dev)
5451{
5452 struct drm_i915_private *dev_priv = dev->dev_private;
5453 struct intel_engine_cs *ring;
5454 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
5455 int i;
5456
5457 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5458
5459 gtfifodbg = I915_READ(GTFIFODBG);
5460 if (gtfifodbg) {
5461 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5462 gtfifodbg);
5463 I915_WRITE(GTFIFODBG, gtfifodbg);
5464 }
5465
5466 cherryview_check_pctx(dev_priv);
5467
5468 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5469 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
5470 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5471
5472 /* Disable RC states. */
5473 I915_WRITE(GEN6_RC_CONTROL, 0);
5474
5475 /* 2a: Program RC6 thresholds.*/
5476 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5477 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5478 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5479
5480 for_each_ring(ring, dev_priv, i)
5481 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5482 I915_WRITE(GEN6_RC_SLEEP, 0);
5483
5484 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5485 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
5486
5487 /* allows RC6 residency counter to work */
5488 I915_WRITE(VLV_COUNTER_CONTROL,
5489 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5490 VLV_MEDIA_RC6_COUNT_EN |
5491 VLV_RENDER_RC6_COUNT_EN));
5492
5493 /* For now we assume BIOS is allocating and populating the PCBR */
5494 pcbr = I915_READ(VLV_PCBR);
5495
5496 /* 3: Enable RC6 */
5497 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5498 (pcbr >> VLV_PCBR_ADDR_SHIFT))
5499 rc6_mode = GEN7_RC_CTL_TO_MODE;
5500
5501 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5502
5503 /* 4 Program defaults and thresholds for RPS*/
5504 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
5505 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5506 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5507 I915_WRITE(GEN6_RP_UP_EI, 66000);
5508 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5509
5510 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5511
5512 /* 5: Enable RPS */
5513 I915_WRITE(GEN6_RP_CONTROL,
5514 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5515 GEN6_RP_MEDIA_IS_GFX |
5516 GEN6_RP_ENABLE |
5517 GEN6_RP_UP_BUSY_AVG |
5518 GEN6_RP_DOWN_IDLE_AVG);
5519
5520 /* Setting Fixed Bias */
5521 val = VLV_OVERRIDE_EN |
5522 VLV_SOC_TDP_EN |
5523 CHV_BIAS_CPU_50_SOC_50;
5524 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5525
5526 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5527
5528 /* RPS code assumes GPLL is used */
5529 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5530
5531 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
5532 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5533
5534 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5535 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5536 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5537 dev_priv->rps.cur_freq);
5538
5539 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5540 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5541 dev_priv->rps.efficient_freq);
5542
5543 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5544
5545 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5546}
5547
5548static void valleyview_enable_rps(struct drm_device *dev)
5549{
5550 struct drm_i915_private *dev_priv = dev->dev_private;
5551 struct intel_engine_cs *ring;
5552 u32 gtfifodbg, val, rc6_mode = 0;
5553 int i;
5554
5555 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5556
5557 valleyview_check_pctx(dev_priv);
5558
5559 if ((gtfifodbg = I915_READ(GTFIFODBG))) {
5560 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5561 gtfifodbg);
5562 I915_WRITE(GTFIFODBG, gtfifodbg);
5563 }
5564
5565 /* If VLV, Forcewake all wells, else re-direct to regular path */
5566 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
5567
5568 /* Disable RC states. */
5569 I915_WRITE(GEN6_RC_CONTROL, 0);
5570
5571 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
5572 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5573 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5574 I915_WRITE(GEN6_RP_UP_EI, 66000);
5575 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5576
5577 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5578
5579 I915_WRITE(GEN6_RP_CONTROL,
5580 GEN6_RP_MEDIA_TURBO |
5581 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5582 GEN6_RP_MEDIA_IS_GFX |
5583 GEN6_RP_ENABLE |
5584 GEN6_RP_UP_BUSY_AVG |
5585 GEN6_RP_DOWN_IDLE_CONT);
5586
5587 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5588 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5589 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5590
5591 for_each_ring(ring, dev_priv, i)
5592 I915_WRITE(RING_MAX_IDLE(ring->mmio_base), 10);
5593
5594 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
5595
5596 /* allows RC6 residency counter to work */
5597 I915_WRITE(VLV_COUNTER_CONTROL,
5598 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5599 VLV_RENDER_RC0_COUNT_EN |
5600 VLV_MEDIA_RC6_COUNT_EN |
5601 VLV_RENDER_RC6_COUNT_EN));
5602
5603 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
5604 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
5605
5606 intel_print_rc6_info(dev, rc6_mode);
5607
5608 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5609
5610 /* Setting Fixed Bias */
5611 val = VLV_OVERRIDE_EN |
5612 VLV_SOC_TDP_EN |
5613 VLV_BIAS_CPU_125_SOC_875;
5614 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5615
5616 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5617
5618 /* RPS code assumes GPLL is used */
5619 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5620
5621 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
5622 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5623
5624 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5625 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
5626 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
5627 dev_priv->rps.cur_freq);
5628
5629 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5630 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
5631 dev_priv->rps.efficient_freq);
5632
5633 valleyview_set_rps(dev_priv->dev, dev_priv->rps.efficient_freq);
5634
5635 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
5636}
5637
5638static unsigned long intel_pxfreq(u32 vidfreq)
5639{
5640 unsigned long freq;
5641 int div = (vidfreq & 0x3f0000) >> 16;
5642 int post = (vidfreq & 0x3000) >> 12;
5643 int pre = (vidfreq & 0x7);
5644
5645 if (!pre)
5646 return 0;
5647
5648 freq = ((div * 133333) / ((1<<post) * pre));
5649
5650 return freq;
5651}
5652
5653static const struct cparams {
5654 u16 i;
5655 u16 t;
5656 u16 m;
5657 u16 c;
5658} cparams[] = {
5659 { 1, 1333, 301, 28664 },
5660 { 1, 1066, 294, 24460 },
5661 { 1, 800, 294, 25192 },
5662 { 0, 1333, 276, 27605 },
5663 { 0, 1066, 276, 27605 },
5664 { 0, 800, 231, 23784 },
5665};
5666
5667static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
5668{
5669 u64 total_count, diff, ret;
5670 u32 count1, count2, count3, m = 0, c = 0;
5671 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5672 int i;
5673
5674 assert_spin_locked(&mchdev_lock);
5675
5676 diff1 = now - dev_priv->ips.last_time1;
5677
5678 /* Prevent division-by-zero if we are asking too fast.
5679 * Also, we don't get interesting results if we are polling
5680 * faster than once in 10ms, so just return the saved value
5681 * in such cases.
5682 */
5683 if (diff1 <= 10)
5684 return dev_priv->ips.chipset_power;
5685
5686 count1 = I915_READ(DMIEC);
5687 count2 = I915_READ(DDREC);
5688 count3 = I915_READ(CSIEC);
5689
5690 total_count = count1 + count2 + count3;
5691
5692 /* FIXME: handle per-counter overflow */
5693 if (total_count < dev_priv->ips.last_count1) {
5694 diff = ~0UL - dev_priv->ips.last_count1;
5695 diff += total_count;
5696 } else {
5697 diff = total_count - dev_priv->ips.last_count1;
5698 }
5699
5700 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
5701 if (cparams[i].i == dev_priv->ips.c_m &&
5702 cparams[i].t == dev_priv->ips.r_t) {
5703 m = cparams[i].m;
5704 c = cparams[i].c;
5705 break;
5706 }
5707 }
5708
5709 diff = div_u64(diff, diff1);
5710 ret = ((m * diff) + c);
5711 ret = div_u64(ret, 10);
5712
5713 dev_priv->ips.last_count1 = total_count;
5714 dev_priv->ips.last_time1 = now;
5715
5716 dev_priv->ips.chipset_power = ret;
5717
5718 return ret;
5719}
5720
5721unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5722{
5723 struct drm_device *dev = dev_priv->dev;
5724 unsigned long val;
5725
5726 if (INTEL_INFO(dev)->gen != 5)
5727 return 0;
5728
5729 spin_lock_irq(&mchdev_lock);
5730
5731 val = __i915_chipset_val(dev_priv);
5732
5733 spin_unlock_irq(&mchdev_lock);
5734
5735 return val;
5736}
5737
5738unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5739{
5740 unsigned long m, x, b;
5741 u32 tsfs;
5742
5743 tsfs = I915_READ(TSFS);
5744
5745 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5746 x = I915_READ8(TR1);
5747
5748 b = tsfs & TSFS_INTR_MASK;
5749
5750 return ((m * x) / 127) - b;
5751}
5752
5753static int _pxvid_to_vd(u8 pxvid)
5754{
5755 if (pxvid == 0)
5756 return 0;
5757
5758 if (pxvid >= 8 && pxvid < 31)
5759 pxvid = 31;
5760
5761 return (pxvid + 2) * 125;
5762}
5763
5764static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
5765{
5766 struct drm_device *dev = dev_priv->dev;
5767 const int vd = _pxvid_to_vd(pxvid);
5768 const int vm = vd - 1125;
5769
5770 if (INTEL_INFO(dev)->is_mobile)
5771 return vm > 0 ? vm : 0;
5772
5773 return vd;
5774}
5775
5776static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
5777{
5778 u64 now, diff, diffms;
5779 u32 count;
5780
5781 assert_spin_locked(&mchdev_lock);
5782
5783 now = ktime_get_raw_ns();
5784 diffms = now - dev_priv->ips.last_time2;
5785 do_div(diffms, NSEC_PER_MSEC);
5786
5787 /* Don't divide by 0 */
5788 if (!diffms)
5789 return;
5790
5791 count = I915_READ(GFXEC);
5792
5793 if (count < dev_priv->ips.last_count2) {
5794 diff = ~0UL - dev_priv->ips.last_count2;
5795 diff += count;
5796 } else {
5797 diff = count - dev_priv->ips.last_count2;
5798 }
5799
5800 dev_priv->ips.last_count2 = count;
5801 dev_priv->ips.last_time2 = now;
5802
5803 /* More magic constants... */
5804 diff = diff * 1181;
5805 diff = div_u64(diff, diffms * 10);
5806 dev_priv->ips.gfx_power = diff;
5807}
5808
5809void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5810{
5811 struct drm_device *dev = dev_priv->dev;
5812
5813 if (INTEL_INFO(dev)->gen != 5)
5814 return;
5815
5816 spin_lock_irq(&mchdev_lock);
5817
5818 __i915_update_gfx_val(dev_priv);
5819
5820 spin_unlock_irq(&mchdev_lock);
5821}
5822
5823static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
5824{
5825 unsigned long t, corr, state1, corr2, state2;
5826 u32 pxvid, ext_v;
5827
5828 assert_spin_locked(&mchdev_lock);
5829
5830 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
5831 pxvid = (pxvid >> 24) & 0x7f;
5832 ext_v = pvid_to_extvid(dev_priv, pxvid);
5833
5834 state1 = ext_v;
5835
5836 t = i915_mch_val(dev_priv);
5837
5838 /* Revel in the empirically derived constants */
5839
5840 /* Correction factor in 1/100000 units */
5841 if (t > 80)
5842 corr = ((t * 2349) + 135940);
5843 else if (t >= 50)
5844 corr = ((t * 964) + 29317);
5845 else /* < 50 */
5846 corr = ((t * 301) + 1004);
5847
5848 corr = corr * ((150142 * state1) / 10000 - 78642);
5849 corr /= 100000;
5850 corr2 = (corr * dev_priv->ips.corr);
5851
5852 state2 = (corr2 * state1) / 10000;
5853 state2 /= 100; /* convert to mW */
5854
5855 __i915_update_gfx_val(dev_priv);
5856
5857 return dev_priv->ips.gfx_power + state2;
5858}
5859
5860unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5861{
5862 struct drm_device *dev = dev_priv->dev;
5863 unsigned long val;
5864
5865 if (INTEL_INFO(dev)->gen != 5)
5866 return 0;
5867
5868 spin_lock_irq(&mchdev_lock);
5869
5870 val = __i915_gfx_val(dev_priv);
5871
5872 spin_unlock_irq(&mchdev_lock);
5873
5874 return val;
5875}
5876
5877/**
5878 * i915_read_mch_val - return value for IPS use
5879 *
5880 * Calculate and return a value for the IPS driver to use when deciding whether
5881 * we have thermal and power headroom to increase CPU or GPU power budget.
5882 */
5883unsigned long i915_read_mch_val(void)
5884{
5885 struct drm_i915_private *dev_priv;
5886 unsigned long chipset_val, graphics_val, ret = 0;
5887
5888 spin_lock_irq(&mchdev_lock);
5889 if (!i915_mch_dev)
5890 goto out_unlock;
5891 dev_priv = i915_mch_dev;
5892
5893 chipset_val = __i915_chipset_val(dev_priv);
5894 graphics_val = __i915_gfx_val(dev_priv);
5895
5896 ret = chipset_val + graphics_val;
5897
5898out_unlock:
5899 spin_unlock_irq(&mchdev_lock);
5900
5901 return ret;
5902}
5903EXPORT_SYMBOL_GPL(i915_read_mch_val);
5904
5905/**
5906 * i915_gpu_raise - raise GPU frequency limit
5907 *
5908 * Raise the limit; IPS indicates we have thermal headroom.
5909 */
5910bool i915_gpu_raise(void)
5911{
5912 struct drm_i915_private *dev_priv;
5913 bool ret = true;
5914
5915 spin_lock_irq(&mchdev_lock);
5916 if (!i915_mch_dev) {
5917 ret = false;
5918 goto out_unlock;
5919 }
5920 dev_priv = i915_mch_dev;
5921
5922 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
5923 dev_priv->ips.max_delay--;
5924
5925out_unlock:
5926 spin_unlock_irq(&mchdev_lock);
5927
5928 return ret;
5929}
5930EXPORT_SYMBOL_GPL(i915_gpu_raise);
5931
5932/**
5933 * i915_gpu_lower - lower GPU frequency limit
5934 *
5935 * IPS indicates we're close to a thermal limit, so throttle back the GPU
5936 * frequency maximum.
5937 */
5938bool i915_gpu_lower(void)
5939{
5940 struct drm_i915_private *dev_priv;
5941 bool ret = true;
5942
5943 spin_lock_irq(&mchdev_lock);
5944 if (!i915_mch_dev) {
5945 ret = false;
5946 goto out_unlock;
5947 }
5948 dev_priv = i915_mch_dev;
5949
5950 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
5951 dev_priv->ips.max_delay++;
5952
5953out_unlock:
5954 spin_unlock_irq(&mchdev_lock);
5955
5956 return ret;
5957}
5958EXPORT_SYMBOL_GPL(i915_gpu_lower);
5959
5960/**
5961 * i915_gpu_busy - indicate GPU business to IPS
5962 *
5963 * Tell the IPS driver whether or not the GPU is busy.
5964 */
5965bool i915_gpu_busy(void)
5966{
5967 struct drm_i915_private *dev_priv;
5968 struct intel_engine_cs *ring;
5969 bool ret = false;
5970 int i;
5971
5972 spin_lock_irq(&mchdev_lock);
5973 if (!i915_mch_dev)
5974 goto out_unlock;
5975 dev_priv = i915_mch_dev;
5976
5977 for_each_ring(ring, dev_priv, i)
5978 ret |= !list_empty(&ring->request_list);
5979
5980out_unlock:
5981 spin_unlock_irq(&mchdev_lock);
5982
5983 return ret;
5984}
5985EXPORT_SYMBOL_GPL(i915_gpu_busy);
5986
5987/**
5988 * i915_gpu_turbo_disable - disable graphics turbo
5989 *
5990 * Disable graphics turbo by resetting the max frequency and setting the
5991 * current frequency to the default.
5992 */
5993bool i915_gpu_turbo_disable(void)
5994{
5995 struct drm_i915_private *dev_priv;
5996 bool ret = true;
5997
5998 spin_lock_irq(&mchdev_lock);
5999 if (!i915_mch_dev) {
6000 ret = false;
6001 goto out_unlock;
6002 }
6003 dev_priv = i915_mch_dev;
6004
6005 dev_priv->ips.max_delay = dev_priv->ips.fstart;
6006
6007 if (!ironlake_set_drps(dev_priv->dev, dev_priv->ips.fstart))
6008 ret = false;
6009
6010out_unlock:
6011 spin_unlock_irq(&mchdev_lock);
6012
6013 return ret;
6014}
6015EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6016
6017/**
6018 * Tells the intel_ips driver that the i915 driver is now loaded, if
6019 * IPS got loaded first.
6020 *
6021 * This awkward dance is so that neither module has to depend on the
6022 * other in order for IPS to do the appropriate communication of
6023 * GPU turbo limits to i915.
6024 */
6025static void
6026ips_ping_for_i915_load(void)
6027{
6028 void (*link)(void);
6029
6030 link = symbol_get(ips_link_to_i915_driver);
6031 if (link) {
6032 link();
6033 symbol_put(ips_link_to_i915_driver);
6034 }
6035}
6036
6037void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6038{
6039 /* We only register the i915 ips part with intel-ips once everything is
6040 * set up, to avoid intel-ips sneaking in and reading bogus values. */
6041 spin_lock_irq(&mchdev_lock);
6042 i915_mch_dev = dev_priv;
6043 spin_unlock_irq(&mchdev_lock);
6044
6045 ips_ping_for_i915_load();
6046}
6047
6048void intel_gpu_ips_teardown(void)
6049{
6050 spin_lock_irq(&mchdev_lock);
6051 i915_mch_dev = NULL;
6052 spin_unlock_irq(&mchdev_lock);
6053}
6054
6055static void intel_init_emon(struct drm_device *dev)
6056{
6057 struct drm_i915_private *dev_priv = dev->dev_private;
6058 u32 lcfuse;
6059 u8 pxw[16];
6060 int i;
6061
6062 /* Disable to program */
6063 I915_WRITE(ECR, 0);
6064 POSTING_READ(ECR);
6065
6066 /* Program energy weights for various events */
6067 I915_WRITE(SDEW, 0x15040d00);
6068 I915_WRITE(CSIEW0, 0x007f0000);
6069 I915_WRITE(CSIEW1, 0x1e220004);
6070 I915_WRITE(CSIEW2, 0x04000004);
6071
6072 for (i = 0; i < 5; i++)
6073 I915_WRITE(PEW(i), 0);
6074 for (i = 0; i < 3; i++)
6075 I915_WRITE(DEW(i), 0);
6076
6077 /* Program P-state weights to account for frequency power adjustment */
6078 for (i = 0; i < 16; i++) {
6079 u32 pxvidfreq = I915_READ(PXVFREQ(i));
6080 unsigned long freq = intel_pxfreq(pxvidfreq);
6081 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6082 PXVFREQ_PX_SHIFT;
6083 unsigned long val;
6084
6085 val = vid * vid;
6086 val *= (freq / 1000);
6087 val *= 255;
6088 val /= (127*127*900);
6089 if (val > 0xff)
6090 DRM_ERROR("bad pxval: %ld\n", val);
6091 pxw[i] = val;
6092 }
6093 /* Render standby states get 0 weight */
6094 pxw[14] = 0;
6095 pxw[15] = 0;
6096
6097 for (i = 0; i < 4; i++) {
6098 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6099 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
6100 I915_WRITE(PXW(i), val);
6101 }
6102
6103 /* Adjust magic regs to magic values (more experimental results) */
6104 I915_WRITE(OGW0, 0);
6105 I915_WRITE(OGW1, 0);
6106 I915_WRITE(EG0, 0x00007f00);
6107 I915_WRITE(EG1, 0x0000000e);
6108 I915_WRITE(EG2, 0x000e0000);
6109 I915_WRITE(EG3, 0x68000300);
6110 I915_WRITE(EG4, 0x42000000);
6111 I915_WRITE(EG5, 0x00140031);
6112 I915_WRITE(EG6, 0);
6113 I915_WRITE(EG7, 0);
6114
6115 for (i = 0; i < 8; i++)
6116 I915_WRITE(PXWL(i), 0);
6117
6118 /* Enable PMON + select events */
6119 I915_WRITE(ECR, 0x80000019);
6120
6121 lcfuse = I915_READ(LCFUSE02);
6122
6123 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
6124}
6125
6126void intel_init_gt_powersave(struct drm_device *dev)
6127{
6128 struct drm_i915_private *dev_priv = dev->dev_private;
6129
6130 /*
6131 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6132 * requirement.
6133 */
6134 if (!i915.enable_rc6) {
6135 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6136 intel_runtime_pm_get(dev_priv);
6137 }
6138
6139 if (IS_CHERRYVIEW(dev))
6140 cherryview_init_gt_powersave(dev);
6141 else if (IS_VALLEYVIEW(dev))
6142 valleyview_init_gt_powersave(dev);
6143}
6144
6145void intel_cleanup_gt_powersave(struct drm_device *dev)
6146{
6147 struct drm_i915_private *dev_priv = dev->dev_private;
6148
6149 if (IS_CHERRYVIEW(dev))
6150 return;
6151 else if (IS_VALLEYVIEW(dev))
6152 valleyview_cleanup_gt_powersave(dev);
6153
6154 if (!i915.enable_rc6)
6155 intel_runtime_pm_put(dev_priv);
6156}
6157
6158static void gen6_suspend_rps(struct drm_device *dev)
6159{
6160 struct drm_i915_private *dev_priv = dev->dev_private;
6161
6162 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6163
6164 gen6_disable_rps_interrupts(dev);
6165}
6166
6167/**
6168 * intel_suspend_gt_powersave - suspend PM work and helper threads
6169 * @dev: drm device
6170 *
6171 * We don't want to disable RC6 or other features here, we just want
6172 * to make sure any work we've queued has finished and won't bother
6173 * us while we're suspended.
6174 */
6175void intel_suspend_gt_powersave(struct drm_device *dev)
6176{
6177 struct drm_i915_private *dev_priv = dev->dev_private;
6178
6179 if (INTEL_INFO(dev)->gen < 6)
6180 return;
6181
6182 gen6_suspend_rps(dev);
6183
6184 /* Force GPU to min freq during suspend */
6185 gen6_rps_idle(dev_priv);
6186}
6187
6188void intel_disable_gt_powersave(struct drm_device *dev)
6189{
6190 struct drm_i915_private *dev_priv = dev->dev_private;
6191
6192 if (IS_IRONLAKE_M(dev)) {
6193 ironlake_disable_drps(dev);
6194 } else if (INTEL_INFO(dev)->gen >= 6) {
6195 intel_suspend_gt_powersave(dev);
6196
6197 mutex_lock(&dev_priv->rps.hw_lock);
6198 if (INTEL_INFO(dev)->gen >= 9)
6199 gen9_disable_rps(dev);
6200 else if (IS_CHERRYVIEW(dev))
6201 cherryview_disable_rps(dev);
6202 else if (IS_VALLEYVIEW(dev))
6203 valleyview_disable_rps(dev);
6204 else
6205 gen6_disable_rps(dev);
6206
6207 dev_priv->rps.enabled = false;
6208 mutex_unlock(&dev_priv->rps.hw_lock);
6209 }
6210}
6211
6212static void intel_gen6_powersave_work(struct work_struct *work)
6213{
6214 struct drm_i915_private *dev_priv =
6215 container_of(work, struct drm_i915_private,
6216 rps.delayed_resume_work.work);
6217 struct drm_device *dev = dev_priv->dev;
6218
6219 mutex_lock(&dev_priv->rps.hw_lock);
6220
6221 gen6_reset_rps_interrupts(dev);
6222
6223 if (IS_CHERRYVIEW(dev)) {
6224 cherryview_enable_rps(dev);
6225 } else if (IS_VALLEYVIEW(dev)) {
6226 valleyview_enable_rps(dev);
6227 } else if (INTEL_INFO(dev)->gen >= 9) {
6228 gen9_enable_rc6(dev);
6229 gen9_enable_rps(dev);
6230 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
6231 __gen6_update_ring_freq(dev);
6232 } else if (IS_BROADWELL(dev)) {
6233 gen8_enable_rps(dev);
6234 __gen6_update_ring_freq(dev);
6235 } else {
6236 gen6_enable_rps(dev);
6237 __gen6_update_ring_freq(dev);
6238 }
6239
6240 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6241 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6242
6243 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6244 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6245
6246 dev_priv->rps.enabled = true;
6247
6248 gen6_enable_rps_interrupts(dev);
6249
6250 mutex_unlock(&dev_priv->rps.hw_lock);
6251
6252 intel_runtime_pm_put(dev_priv);
6253}
6254
6255void intel_enable_gt_powersave(struct drm_device *dev)
6256{
6257 struct drm_i915_private *dev_priv = dev->dev_private;
6258
6259 /* Powersaving is controlled by the host when inside a VM */
6260 if (intel_vgpu_active(dev))
6261 return;
6262
6263 if (IS_IRONLAKE_M(dev)) {
6264 ironlake_enable_drps(dev);
6265 mutex_lock(&dev->struct_mutex);
6266 intel_init_emon(dev);
6267 mutex_unlock(&dev->struct_mutex);
6268 } else if (INTEL_INFO(dev)->gen >= 6) {
6269 /*
6270 * PCU communication is slow and this doesn't need to be
6271 * done at any specific time, so do this out of our fast path
6272 * to make resume and init faster.
6273 *
6274 * We depend on the HW RC6 power context save/restore
6275 * mechanism when entering D3 through runtime PM suspend. So
6276 * disable RPM until RPS/RC6 is properly setup. We can only
6277 * get here via the driver load/system resume/runtime resume
6278 * paths, so the _noresume version is enough (and in case of
6279 * runtime resume it's necessary).
6280 */
6281 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6282 round_jiffies_up_relative(HZ)))
6283 intel_runtime_pm_get_noresume(dev_priv);
6284 }
6285}
6286
6287void intel_reset_gt_powersave(struct drm_device *dev)
6288{
6289 struct drm_i915_private *dev_priv = dev->dev_private;
6290
6291 if (INTEL_INFO(dev)->gen < 6)
6292 return;
6293
6294 gen6_suspend_rps(dev);
6295 dev_priv->rps.enabled = false;
6296}
6297
6298static void ibx_init_clock_gating(struct drm_device *dev)
6299{
6300 struct drm_i915_private *dev_priv = dev->dev_private;
6301
6302 /*
6303 * On Ibex Peak and Cougar Point, we need to disable clock
6304 * gating for the panel power sequencer or it will fail to
6305 * start up when no ports are active.
6306 */
6307 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6308}
6309
6310static void g4x_disable_trickle_feed(struct drm_device *dev)
6311{
6312 struct drm_i915_private *dev_priv = dev->dev_private;
6313 enum pipe pipe;
6314
6315 for_each_pipe(dev_priv, pipe) {
6316 I915_WRITE(DSPCNTR(pipe),
6317 I915_READ(DSPCNTR(pipe)) |
6318 DISPPLANE_TRICKLE_FEED_DISABLE);
6319
6320 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6321 POSTING_READ(DSPSURF(pipe));
6322 }
6323}
6324
6325static void ilk_init_lp_watermarks(struct drm_device *dev)
6326{
6327 struct drm_i915_private *dev_priv = dev->dev_private;
6328
6329 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6330 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6331 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6332
6333 /*
6334 * Don't touch WM1S_LP_EN here.
6335 * Doing so could cause underruns.
6336 */
6337}
6338
6339static void ironlake_init_clock_gating(struct drm_device *dev)
6340{
6341 struct drm_i915_private *dev_priv = dev->dev_private;
6342 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6343
6344 /*
6345 * Required for FBC
6346 * WaFbcDisableDpfcClockGating:ilk
6347 */
6348 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6349 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6350 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
6351
6352 I915_WRITE(PCH_3DCGDIS0,
6353 MARIUNIT_CLOCK_GATE_DISABLE |
6354 SVSMUNIT_CLOCK_GATE_DISABLE);
6355 I915_WRITE(PCH_3DCGDIS1,
6356 VFMUNIT_CLOCK_GATE_DISABLE);
6357
6358 /*
6359 * According to the spec the following bits should be set in
6360 * order to enable memory self-refresh
6361 * The bit 22/21 of 0x42004
6362 * The bit 5 of 0x42020
6363 * The bit 15 of 0x45000
6364 */
6365 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6366 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6367 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
6368 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
6369 I915_WRITE(DISP_ARB_CTL,
6370 (I915_READ(DISP_ARB_CTL) |
6371 DISP_FBC_WM_DIS));
6372
6373 ilk_init_lp_watermarks(dev);
6374
6375 /*
6376 * Based on the document from hardware guys the following bits
6377 * should be set unconditionally in order to enable FBC.
6378 * The bit 22 of 0x42000
6379 * The bit 22 of 0x42004
6380 * The bit 7,8,9 of 0x42020.
6381 */
6382 if (IS_IRONLAKE_M(dev)) {
6383 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
6384 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6385 I915_READ(ILK_DISPLAY_CHICKEN1) |
6386 ILK_FBCQ_DIS);
6387 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6388 I915_READ(ILK_DISPLAY_CHICKEN2) |
6389 ILK_DPARB_GATE);
6390 }
6391
6392 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6393
6394 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6395 I915_READ(ILK_DISPLAY_CHICKEN2) |
6396 ILK_ELPIN_409_SELECT);
6397 I915_WRITE(_3D_CHICKEN2,
6398 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6399 _3D_CHICKEN2_WM_READ_PIPELINED);
6400
6401 /* WaDisableRenderCachePipelinedFlush:ilk */
6402 I915_WRITE(CACHE_MODE_0,
6403 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6404
6405 /* WaDisable_RenderCache_OperationalFlush:ilk */
6406 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6407
6408 g4x_disable_trickle_feed(dev);
6409
6410 ibx_init_clock_gating(dev);
6411}
6412
6413static void cpt_init_clock_gating(struct drm_device *dev)
6414{
6415 struct drm_i915_private *dev_priv = dev->dev_private;
6416 int pipe;
6417 uint32_t val;
6418
6419 /*
6420 * On Ibex Peak and Cougar Point, we need to disable clock
6421 * gating for the panel power sequencer or it will fail to
6422 * start up when no ports are active.
6423 */
6424 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6425 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6426 PCH_CPUNIT_CLOCK_GATE_DISABLE);
6427 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6428 DPLS_EDP_PPS_FIX_DIS);
6429 /* The below fixes the weird display corruption, a few pixels shifted
6430 * downward, on (only) LVDS of some HP laptops with IVY.
6431 */
6432 for_each_pipe(dev_priv, pipe) {
6433 val = I915_READ(TRANS_CHICKEN2(pipe));
6434 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6435 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6436 if (dev_priv->vbt.fdi_rx_polarity_inverted)
6437 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
6438 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6439 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6440 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
6441 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6442 }
6443 /* WADP0ClockGatingDisable */
6444 for_each_pipe(dev_priv, pipe) {
6445 I915_WRITE(TRANS_CHICKEN1(pipe),
6446 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6447 }
6448}
6449
6450static void gen6_check_mch_setup(struct drm_device *dev)
6451{
6452 struct drm_i915_private *dev_priv = dev->dev_private;
6453 uint32_t tmp;
6454
6455 tmp = I915_READ(MCH_SSKPD);
6456 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6457 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6458 tmp);
6459}
6460
6461static void gen6_init_clock_gating(struct drm_device *dev)
6462{
6463 struct drm_i915_private *dev_priv = dev->dev_private;
6464 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6465
6466 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6467
6468 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6469 I915_READ(ILK_DISPLAY_CHICKEN2) |
6470 ILK_ELPIN_409_SELECT);
6471
6472 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
6473 I915_WRITE(_3D_CHICKEN,
6474 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6475
6476 /* WaDisable_RenderCache_OperationalFlush:snb */
6477 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6478
6479 /*
6480 * BSpec recoomends 8x4 when MSAA is used,
6481 * however in practice 16x4 seems fastest.
6482 *
6483 * Note that PS/WM thread counts depend on the WIZ hashing
6484 * disable bit, which we don't touch here, but it's good
6485 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6486 */
6487 I915_WRITE(GEN6_GT_MODE,
6488 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6489
6490 ilk_init_lp_watermarks(dev);
6491
6492 I915_WRITE(CACHE_MODE_0,
6493 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
6494
6495 I915_WRITE(GEN6_UCGCTL1,
6496 I915_READ(GEN6_UCGCTL1) |
6497 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6498 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6499
6500 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6501 * gating disable must be set. Failure to set it results in
6502 * flickering pixels due to Z write ordering failures after
6503 * some amount of runtime in the Mesa "fire" demo, and Unigine
6504 * Sanctuary and Tropics, and apparently anything else with
6505 * alpha test or pixel discard.
6506 *
6507 * According to the spec, bit 11 (RCCUNIT) must also be set,
6508 * but we didn't debug actual testcases to find it out.
6509 *
6510 * WaDisableRCCUnitClockGating:snb
6511 * WaDisableRCPBUnitClockGating:snb
6512 */
6513 I915_WRITE(GEN6_UCGCTL2,
6514 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6515 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6516
6517 /* WaStripsFansDisableFastClipPerformanceFix:snb */
6518 I915_WRITE(_3D_CHICKEN3,
6519 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
6520
6521 /*
6522 * Bspec says:
6523 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6524 * 3DSTATE_SF number of SF output attributes is more than 16."
6525 */
6526 I915_WRITE(_3D_CHICKEN3,
6527 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6528
6529 /*
6530 * According to the spec the following bits should be
6531 * set in order to enable memory self-refresh and fbc:
6532 * The bit21 and bit22 of 0x42000
6533 * The bit21 and bit22 of 0x42004
6534 * The bit5 and bit7 of 0x42020
6535 * The bit14 of 0x70180
6536 * The bit14 of 0x71180
6537 *
6538 * WaFbcAsynchFlipDisableFbcQueue:snb
6539 */
6540 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6541 I915_READ(ILK_DISPLAY_CHICKEN1) |
6542 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6543 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6544 I915_READ(ILK_DISPLAY_CHICKEN2) |
6545 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
6546 I915_WRITE(ILK_DSPCLK_GATE_D,
6547 I915_READ(ILK_DSPCLK_GATE_D) |
6548 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6549 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
6550
6551 g4x_disable_trickle_feed(dev);
6552
6553 cpt_init_clock_gating(dev);
6554
6555 gen6_check_mch_setup(dev);
6556}
6557
6558static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6559{
6560 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6561
6562 /*
6563 * WaVSThreadDispatchOverride:ivb,vlv
6564 *
6565 * This actually overrides the dispatch
6566 * mode for all thread types.
6567 */
6568 reg &= ~GEN7_FF_SCHED_MASK;
6569 reg |= GEN7_FF_TS_SCHED_HW;
6570 reg |= GEN7_FF_VS_SCHED_HW;
6571 reg |= GEN7_FF_DS_SCHED_HW;
6572
6573 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6574}
6575
6576static void lpt_init_clock_gating(struct drm_device *dev)
6577{
6578 struct drm_i915_private *dev_priv = dev->dev_private;
6579
6580 /*
6581 * TODO: this bit should only be enabled when really needed, then
6582 * disabled when not needed anymore in order to save power.
6583 */
6584 if (HAS_PCH_LPT_LP(dev))
6585 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6586 I915_READ(SOUTH_DSPCLK_GATE_D) |
6587 PCH_LP_PARTITION_LEVEL_DISABLE);
6588
6589 /* WADPOClockGatingDisable:hsw */
6590 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6591 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
6592 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6593}
6594
6595static void lpt_suspend_hw(struct drm_device *dev)
6596{
6597 struct drm_i915_private *dev_priv = dev->dev_private;
6598
6599 if (HAS_PCH_LPT_LP(dev)) {
6600 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6601
6602 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6603 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6604 }
6605}
6606
6607static void broadwell_init_clock_gating(struct drm_device *dev)
6608{
6609 struct drm_i915_private *dev_priv = dev->dev_private;
6610 enum pipe pipe;
6611 uint32_t misccpctl;
6612
6613 ilk_init_lp_watermarks(dev);
6614
6615 /* WaSwitchSolVfFArbitrationPriority:bdw */
6616 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6617
6618 /* WaPsrDPAMaskVBlankInSRD:bdw */
6619 I915_WRITE(CHICKEN_PAR1_1,
6620 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6621
6622 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
6623 for_each_pipe(dev_priv, pipe) {
6624 I915_WRITE(CHICKEN_PIPESL_1(pipe),
6625 I915_READ(CHICKEN_PIPESL_1(pipe)) |
6626 BDW_DPRS_MASK_VBLANK_SRD);
6627 }
6628
6629 /* WaVSRefCountFullforceMissDisable:bdw */
6630 /* WaDSRefCountFullforceMissDisable:bdw */
6631 I915_WRITE(GEN7_FF_THREAD_MODE,
6632 I915_READ(GEN7_FF_THREAD_MODE) &
6633 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6634
6635 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6636 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6637
6638 /* WaDisableSDEUnitClockGating:bdw */
6639 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6640 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6641
6642 /*
6643 * WaProgramL3SqcReg1Default:bdw
6644 * WaTempDisableDOPClkGating:bdw
6645 */
6646 misccpctl = I915_READ(GEN7_MISCCPCTL);
6647 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6648 I915_WRITE(GEN8_L3SQCREG1, BDW_WA_L3SQCREG1_DEFAULT);
6649 /*
6650 * Wait at least 100 clocks before re-enabling clock gating. See
6651 * the definition of L3SQCREG1 in BSpec.
6652 */
6653 POSTING_READ(GEN8_L3SQCREG1);
6654 udelay(1);
6655 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6656
6657 /*
6658 * WaGttCachingOffByDefault:bdw
6659 * GTT cache may not work with big pages, so if those
6660 * are ever enabled GTT cache may need to be disabled.
6661 */
6662 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6663
6664 lpt_init_clock_gating(dev);
6665}
6666
6667static void haswell_init_clock_gating(struct drm_device *dev)
6668{
6669 struct drm_i915_private *dev_priv = dev->dev_private;
6670
6671 ilk_init_lp_watermarks(dev);
6672
6673 /* L3 caching of data atomics doesn't work -- disable it. */
6674 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6675 I915_WRITE(HSW_ROW_CHICKEN3,
6676 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6677
6678 /* This is required by WaCatErrorRejectionIssue:hsw */
6679 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6680 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6681 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6682
6683 /* WaVSRefCountFullforceMissDisable:hsw */
6684 I915_WRITE(GEN7_FF_THREAD_MODE,
6685 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
6686
6687 /* WaDisable_RenderCache_OperationalFlush:hsw */
6688 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6689
6690 /* enable HiZ Raw Stall Optimization */
6691 I915_WRITE(CACHE_MODE_0_GEN7,
6692 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6693
6694 /* WaDisable4x2SubspanOptimization:hsw */
6695 I915_WRITE(CACHE_MODE_1,
6696 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6697
6698 /*
6699 * BSpec recommends 8x4 when MSAA is used,
6700 * however in practice 16x4 seems fastest.
6701 *
6702 * Note that PS/WM thread counts depend on the WIZ hashing
6703 * disable bit, which we don't touch here, but it's good
6704 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6705 */
6706 I915_WRITE(GEN7_GT_MODE,
6707 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6708
6709 /* WaSampleCChickenBitEnable:hsw */
6710 I915_WRITE(HALF_SLICE_CHICKEN3,
6711 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6712
6713 /* WaSwitchSolVfFArbitrationPriority:hsw */
6714 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6715
6716 /* WaRsPkgCStateDisplayPMReq:hsw */
6717 I915_WRITE(CHICKEN_PAR1_1,
6718 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
6719
6720 lpt_init_clock_gating(dev);
6721}
6722
6723static void ivybridge_init_clock_gating(struct drm_device *dev)
6724{
6725 struct drm_i915_private *dev_priv = dev->dev_private;
6726 uint32_t snpcr;
6727
6728 ilk_init_lp_watermarks(dev);
6729
6730 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
6731
6732 /* WaDisableEarlyCull:ivb */
6733 I915_WRITE(_3D_CHICKEN3,
6734 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6735
6736 /* WaDisableBackToBackFlipFix:ivb */
6737 I915_WRITE(IVB_CHICKEN3,
6738 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6739 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6740
6741 /* WaDisablePSDDualDispatchEnable:ivb */
6742 if (IS_IVB_GT1(dev))
6743 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6744 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6745
6746 /* WaDisable_RenderCache_OperationalFlush:ivb */
6747 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6748
6749 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
6750 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6751 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6752
6753 /* WaApplyL3ControlAndL3ChickenMode:ivb */
6754 I915_WRITE(GEN7_L3CNTLREG1,
6755 GEN7_WA_FOR_GEN7_L3_CONTROL);
6756 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
6757 GEN7_WA_L3_CHICKEN_MODE);
6758 if (IS_IVB_GT1(dev))
6759 I915_WRITE(GEN7_ROW_CHICKEN2,
6760 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6761 else {
6762 /* must write both registers */
6763 I915_WRITE(GEN7_ROW_CHICKEN2,
6764 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6765 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6766 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6767 }
6768
6769 /* WaForceL3Serialization:ivb */
6770 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6771 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6772
6773 /*
6774 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6775 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
6776 */
6777 I915_WRITE(GEN6_UCGCTL2,
6778 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6779
6780 /* This is required by WaCatErrorRejectionIssue:ivb */
6781 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6782 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6783 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6784
6785 g4x_disable_trickle_feed(dev);
6786
6787 gen7_setup_fixed_func_scheduler(dev_priv);
6788
6789 if (0) { /* causes HiZ corruption on ivb:gt1 */
6790 /* enable HiZ Raw Stall Optimization */
6791 I915_WRITE(CACHE_MODE_0_GEN7,
6792 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6793 }
6794
6795 /* WaDisable4x2SubspanOptimization:ivb */
6796 I915_WRITE(CACHE_MODE_1,
6797 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6798
6799 /*
6800 * BSpec recommends 8x4 when MSAA is used,
6801 * however in practice 16x4 seems fastest.
6802 *
6803 * Note that PS/WM thread counts depend on the WIZ hashing
6804 * disable bit, which we don't touch here, but it's good
6805 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6806 */
6807 I915_WRITE(GEN7_GT_MODE,
6808 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6809
6810 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6811 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6812 snpcr |= GEN6_MBC_SNPCR_MED;
6813 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
6814
6815 if (!HAS_PCH_NOP(dev))
6816 cpt_init_clock_gating(dev);
6817
6818 gen6_check_mch_setup(dev);
6819}
6820
6821static void vlv_init_display_clock_gating(struct drm_i915_private *dev_priv)
6822{
6823 I915_WRITE(DSPCLK_GATE_D, VRHUNIT_CLOCK_GATE_DISABLE);
6824
6825 /*
6826 * Disable trickle feed and enable pnd deadline calculation
6827 */
6828 I915_WRITE(MI_ARB_VLV, MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE);
6829 I915_WRITE(CBR1_VLV, 0);
6830}
6831
6832static void valleyview_init_clock_gating(struct drm_device *dev)
6833{
6834 struct drm_i915_private *dev_priv = dev->dev_private;
6835
6836 vlv_init_display_clock_gating(dev_priv);
6837
6838 /* WaDisableEarlyCull:vlv */
6839 I915_WRITE(_3D_CHICKEN3,
6840 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6841
6842 /* WaDisableBackToBackFlipFix:vlv */
6843 I915_WRITE(IVB_CHICKEN3,
6844 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6845 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6846
6847 /* WaPsdDispatchEnable:vlv */
6848 /* WaDisablePSDDualDispatchEnable:vlv */
6849 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6850 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6851 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
6852
6853 /* WaDisable_RenderCache_OperationalFlush:vlv */
6854 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6855
6856 /* WaForceL3Serialization:vlv */
6857 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6858 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6859
6860 /* WaDisableDopClockGating:vlv */
6861 I915_WRITE(GEN7_ROW_CHICKEN2,
6862 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6863
6864 /* This is required by WaCatErrorRejectionIssue:vlv */
6865 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6866 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6867 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6868
6869 gen7_setup_fixed_func_scheduler(dev_priv);
6870
6871 /*
6872 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
6873 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
6874 */
6875 I915_WRITE(GEN6_UCGCTL2,
6876 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
6877
6878 /* WaDisableL3Bank2xClockGate:vlv
6879 * Disabling L3 clock gating- MMIO 940c[25] = 1
6880 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6881 I915_WRITE(GEN7_UCGCTL4,
6882 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
6883
6884 /*
6885 * BSpec says this must be set, even though
6886 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6887 */
6888 I915_WRITE(CACHE_MODE_1,
6889 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
6890
6891 /*
6892 * BSpec recommends 8x4 when MSAA is used,
6893 * however in practice 16x4 seems fastest.
6894 *
6895 * Note that PS/WM thread counts depend on the WIZ hashing
6896 * disable bit, which we don't touch here, but it's good
6897 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6898 */
6899 I915_WRITE(GEN7_GT_MODE,
6900 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6901
6902 /*
6903 * WaIncreaseL3CreditsForVLVB0:vlv
6904 * This is the hardware default actually.
6905 */
6906 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6907
6908 /*
6909 * WaDisableVLVClockGating_VBIIssue:vlv
6910 * Disable clock gating on th GCFG unit to prevent a delay
6911 * in the reporting of vblank events.
6912 */
6913 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
6914}
6915
6916static void cherryview_init_clock_gating(struct drm_device *dev)
6917{
6918 struct drm_i915_private *dev_priv = dev->dev_private;
6919
6920 vlv_init_display_clock_gating(dev_priv);
6921
6922 /* WaVSRefCountFullforceMissDisable:chv */
6923 /* WaDSRefCountFullforceMissDisable:chv */
6924 I915_WRITE(GEN7_FF_THREAD_MODE,
6925 I915_READ(GEN7_FF_THREAD_MODE) &
6926 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
6927
6928 /* WaDisableSemaphoreAndSyncFlipWait:chv */
6929 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6930 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
6931
6932 /* WaDisableCSUnitClockGating:chv */
6933 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
6934 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6935
6936 /* WaDisableSDEUnitClockGating:chv */
6937 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6938 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6939
6940 /*
6941 * GTT cache may not work with big pages, so if those
6942 * are ever enabled GTT cache may need to be disabled.
6943 */
6944 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6945}
6946
6947static void g4x_init_clock_gating(struct drm_device *dev)
6948{
6949 struct drm_i915_private *dev_priv = dev->dev_private;
6950 uint32_t dspclk_gate;
6951
6952 I915_WRITE(RENCLK_GATE_D1, 0);
6953 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
6954 GS_UNIT_CLOCK_GATE_DISABLE |
6955 CL_UNIT_CLOCK_GATE_DISABLE);
6956 I915_WRITE(RAMCLK_GATE_D, 0);
6957 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
6958 OVRUNIT_CLOCK_GATE_DISABLE |
6959 OVCUNIT_CLOCK_GATE_DISABLE;
6960 if (IS_GM45(dev))
6961 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
6962 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
6963
6964 /* WaDisableRenderCachePipelinedFlush */
6965 I915_WRITE(CACHE_MODE_0,
6966 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
6967
6968 /* WaDisable_RenderCache_OperationalFlush:g4x */
6969 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6970
6971 g4x_disable_trickle_feed(dev);
6972}
6973
6974static void crestline_init_clock_gating(struct drm_device *dev)
6975{
6976 struct drm_i915_private *dev_priv = dev->dev_private;
6977
6978 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
6979 I915_WRITE(RENCLK_GATE_D2, 0);
6980 I915_WRITE(DSPCLK_GATE_D, 0);
6981 I915_WRITE(RAMCLK_GATE_D, 0);
6982 I915_WRITE16(DEUC, 0);
6983 I915_WRITE(MI_ARB_STATE,
6984 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6985
6986 /* WaDisable_RenderCache_OperationalFlush:gen4 */
6987 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6988}
6989
6990static void broadwater_init_clock_gating(struct drm_device *dev)
6991{
6992 struct drm_i915_private *dev_priv = dev->dev_private;
6993
6994 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
6995 I965_RCC_CLOCK_GATE_DISABLE |
6996 I965_RCPB_CLOCK_GATE_DISABLE |
6997 I965_ISC_CLOCK_GATE_DISABLE |
6998 I965_FBC_CLOCK_GATE_DISABLE);
6999 I915_WRITE(RENCLK_GATE_D2, 0);
7000 I915_WRITE(MI_ARB_STATE,
7001 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7002
7003 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7004 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7005}
7006
7007static void gen3_init_clock_gating(struct drm_device *dev)
7008{
7009 struct drm_i915_private *dev_priv = dev->dev_private;
7010 u32 dstate = I915_READ(D_STATE);
7011
7012 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7013 DSTATE_DOT_CLOCK_GATING;
7014 I915_WRITE(D_STATE, dstate);
7015
7016 if (IS_PINEVIEW(dev))
7017 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
7018
7019 /* IIR "flip pending" means done if this bit is set */
7020 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
7021
7022 /* interrupts should cause a wake up from C3 */
7023 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
7024
7025 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7026 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
7027
7028 I915_WRITE(MI_ARB_STATE,
7029 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7030}
7031
7032static void i85x_init_clock_gating(struct drm_device *dev)
7033{
7034 struct drm_i915_private *dev_priv = dev->dev_private;
7035
7036 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
7037
7038 /* interrupts should cause a wake up from C3 */
7039 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7040 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
7041
7042 I915_WRITE(MEM_MODE,
7043 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
7044}
7045
7046static void i830_init_clock_gating(struct drm_device *dev)
7047{
7048 struct drm_i915_private *dev_priv = dev->dev_private;
7049
7050 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
7051
7052 I915_WRITE(MEM_MODE,
7053 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7054 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
7055}
7056
7057void intel_init_clock_gating(struct drm_device *dev)
7058{
7059 struct drm_i915_private *dev_priv = dev->dev_private;
7060
7061 if (dev_priv->display.init_clock_gating)
7062 dev_priv->display.init_clock_gating(dev);
7063}
7064
7065void intel_suspend_hw(struct drm_device *dev)
7066{
7067 if (HAS_PCH_LPT(dev))
7068 lpt_suspend_hw(dev);
7069}
7070
7071/* Set up chip specific power management-related functions */
7072void intel_init_pm(struct drm_device *dev)
7073{
7074 struct drm_i915_private *dev_priv = dev->dev_private;
7075
7076 intel_fbc_init(dev_priv);
7077
7078 /* For cxsr */
7079 if (IS_PINEVIEW(dev))
7080 i915_pineview_get_mem_freq(dev);
7081 else if (IS_GEN5(dev))
7082 i915_ironlake_get_mem_freq(dev);
7083
7084 /* For FIFO watermark updates */
7085 if (INTEL_INFO(dev)->gen >= 9) {
7086 skl_setup_wm_latency(dev);
7087
7088 if (IS_BROXTON(dev))
7089 dev_priv->display.init_clock_gating =
7090 bxt_init_clock_gating;
7091 dev_priv->display.update_wm = skl_update_wm;
7092 } else if (HAS_PCH_SPLIT(dev)) {
7093 ilk_setup_wm_latency(dev);
7094
7095 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7096 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7097 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7098 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7099 dev_priv->display.update_wm = ilk_update_wm;
7100 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
7101 dev_priv->display.program_watermarks = ilk_program_watermarks;
7102 } else {
7103 DRM_DEBUG_KMS("Failed to read display plane latency. "
7104 "Disable CxSR\n");
7105 }
7106
7107 if (IS_GEN5(dev))
7108 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7109 else if (IS_GEN6(dev))
7110 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7111 else if (IS_IVYBRIDGE(dev))
7112 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7113 else if (IS_HASWELL(dev))
7114 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7115 else if (INTEL_INFO(dev)->gen == 8)
7116 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7117 } else if (IS_CHERRYVIEW(dev)) {
7118 vlv_setup_wm_latency(dev);
7119
7120 dev_priv->display.update_wm = vlv_update_wm;
7121 dev_priv->display.init_clock_gating =
7122 cherryview_init_clock_gating;
7123 } else if (IS_VALLEYVIEW(dev)) {
7124 vlv_setup_wm_latency(dev);
7125
7126 dev_priv->display.update_wm = vlv_update_wm;
7127 dev_priv->display.init_clock_gating =
7128 valleyview_init_clock_gating;
7129 } else if (IS_PINEVIEW(dev)) {
7130 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7131 dev_priv->is_ddr3,
7132 dev_priv->fsb_freq,
7133 dev_priv->mem_freq)) {
7134 DRM_INFO("failed to find known CxSR latency "
7135 "(found ddr%s fsb freq %d, mem freq %d), "
7136 "disabling CxSR\n",
7137 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7138 dev_priv->fsb_freq, dev_priv->mem_freq);
7139 /* Disable CxSR and never update its watermark again */
7140 intel_set_memory_cxsr(dev_priv, false);
7141 dev_priv->display.update_wm = NULL;
7142 } else
7143 dev_priv->display.update_wm = pineview_update_wm;
7144 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7145 } else if (IS_G4X(dev)) {
7146 dev_priv->display.update_wm = g4x_update_wm;
7147 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7148 } else if (IS_GEN4(dev)) {
7149 dev_priv->display.update_wm = i965_update_wm;
7150 if (IS_CRESTLINE(dev))
7151 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7152 else if (IS_BROADWATER(dev))
7153 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7154 } else if (IS_GEN3(dev)) {
7155 dev_priv->display.update_wm = i9xx_update_wm;
7156 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7157 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7158 } else if (IS_GEN2(dev)) {
7159 if (INTEL_INFO(dev)->num_pipes == 1) {
7160 dev_priv->display.update_wm = i845_update_wm;
7161 dev_priv->display.get_fifo_size = i845_get_fifo_size;
7162 } else {
7163 dev_priv->display.update_wm = i9xx_update_wm;
7164 dev_priv->display.get_fifo_size = i830_get_fifo_size;
7165 }
7166
7167 if (IS_I85X(dev) || IS_I865G(dev))
7168 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7169 else
7170 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7171 } else {
7172 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
7173 }
7174}
7175
7176int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
7177{
7178 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7179
7180 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7181 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7182 return -EAGAIN;
7183 }
7184
7185 I915_WRITE(GEN6_PCODE_DATA, *val);
7186 I915_WRITE(GEN6_PCODE_DATA1, 0);
7187 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7188
7189 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7190 500)) {
7191 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7192 return -ETIMEDOUT;
7193 }
7194
7195 *val = I915_READ(GEN6_PCODE_DATA);
7196 I915_WRITE(GEN6_PCODE_DATA, 0);
7197
7198 return 0;
7199}
7200
7201int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
7202{
7203 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
7204
7205 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7206 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7207 return -EAGAIN;
7208 }
7209
7210 I915_WRITE(GEN6_PCODE_DATA, val);
7211 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7212
7213 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7214 500)) {
7215 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7216 return -ETIMEDOUT;
7217 }
7218
7219 I915_WRITE(GEN6_PCODE_DATA, 0);
7220
7221 return 0;
7222}
7223
7224static int vlv_gpu_freq_div(unsigned int czclk_freq)
7225{
7226 switch (czclk_freq) {
7227 case 200:
7228 return 10;
7229 case 267:
7230 return 12;
7231 case 320:
7232 case 333:
7233 return 16;
7234 case 400:
7235 return 20;
7236 default:
7237 return -1;
7238 }
7239}
7240
7241static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7242{
7243 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7244
7245 div = vlv_gpu_freq_div(czclk_freq);
7246 if (div < 0)
7247 return div;
7248
7249 return DIV_ROUND_CLOSEST(czclk_freq * (val + 6 - 0xbd), div);
7250}
7251
7252static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
7253{
7254 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7255
7256 mul = vlv_gpu_freq_div(czclk_freq);
7257 if (mul < 0)
7258 return mul;
7259
7260 return DIV_ROUND_CLOSEST(mul * val, czclk_freq) + 0xbd - 6;
7261}
7262
7263static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
7264{
7265 int div, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7266
7267 div = vlv_gpu_freq_div(czclk_freq);
7268 if (div < 0)
7269 return div;
7270 div /= 2;
7271
7272 return DIV_ROUND_CLOSEST(czclk_freq * val, 2 * div) / 2;
7273}
7274
7275static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
7276{
7277 int mul, czclk_freq = DIV_ROUND_CLOSEST(dev_priv->czclk_freq, 1000);
7278
7279 mul = vlv_gpu_freq_div(czclk_freq);
7280 if (mul < 0)
7281 return mul;
7282 mul /= 2;
7283
7284 /* CHV needs even values */
7285 return DIV_ROUND_CLOSEST(val * 2 * mul, czclk_freq) * 2;
7286}
7287
7288int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
7289{
7290 if (IS_GEN9(dev_priv->dev))
7291 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7292 GEN9_FREQ_SCALER);
7293 else if (IS_CHERRYVIEW(dev_priv->dev))
7294 return chv_gpu_freq(dev_priv, val);
7295 else if (IS_VALLEYVIEW(dev_priv->dev))
7296 return byt_gpu_freq(dev_priv, val);
7297 else
7298 return val * GT_FREQUENCY_MULTIPLIER;
7299}
7300
7301int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7302{
7303 if (IS_GEN9(dev_priv->dev))
7304 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7305 GT_FREQUENCY_MULTIPLIER);
7306 else if (IS_CHERRYVIEW(dev_priv->dev))
7307 return chv_freq_opcode(dev_priv, val);
7308 else if (IS_VALLEYVIEW(dev_priv->dev))
7309 return byt_freq_opcode(dev_priv, val);
7310 else
7311 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
7312}
7313
7314struct request_boost {
7315 struct work_struct work;
7316 struct drm_i915_gem_request *req;
7317};
7318
7319static void __intel_rps_boost_work(struct work_struct *work)
7320{
7321 struct request_boost *boost = container_of(work, struct request_boost, work);
7322 struct drm_i915_gem_request *req = boost->req;
7323
7324 if (!i915_gem_request_completed(req, true))
7325 gen6_rps_boost(to_i915(req->ring->dev), NULL,
7326 req->emitted_jiffies);
7327
7328 i915_gem_request_unreference__unlocked(req);
7329 kfree(boost);
7330}
7331
7332void intel_queue_rps_boost_for_request(struct drm_device *dev,
7333 struct drm_i915_gem_request *req)
7334{
7335 struct request_boost *boost;
7336
7337 if (req == NULL || INTEL_INFO(dev)->gen < 6)
7338 return;
7339
7340 if (i915_gem_request_completed(req, true))
7341 return;
7342
7343 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7344 if (boost == NULL)
7345 return;
7346
7347 i915_gem_request_reference(req);
7348 boost->req = req;
7349
7350 INIT_WORK(&boost->work, __intel_rps_boost_work);
7351 queue_work(to_i915(dev)->wq, &boost->work);
7352}
7353
7354void intel_pm_setup(struct drm_device *dev)
7355{
7356 struct drm_i915_private *dev_priv = dev->dev_private;
7357
7358 mutex_init(&dev_priv->rps.hw_lock);
7359 spin_lock_init(&dev_priv->rps.client_lock);
7360
7361 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7362 intel_gen6_powersave_work);
7363 INIT_LIST_HEAD(&dev_priv->rps.clients);
7364 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7365 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
7366
7367 dev_priv->pm.suspended = false;
7368 atomic_set(&dev_priv->pm.wakeref_count, 0);
7369 atomic_set(&dev_priv->pm.atomic_seq, 0);
7370}
1/*
2 * Copyright © 2012 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eugeni Dodonov <eugeni.dodonov@intel.com>
25 *
26 */
27
28#include <linux/module.h>
29#include <linux/pm_runtime.h>
30
31#include <drm/drm_atomic_helper.h>
32#include <drm/drm_fourcc.h>
33#include <drm/drm_plane_helper.h>
34
35#include "display/intel_atomic.h"
36#include "display/intel_atomic_plane.h"
37#include "display/intel_bw.h"
38#include "display/intel_de.h"
39#include "display/intel_display_types.h"
40#include "display/intel_fbc.h"
41#include "display/intel_sprite.h"
42#include "display/skl_universal_plane.h"
43
44#include "gt/intel_llc.h"
45
46#include "i915_drv.h"
47#include "i915_fixed.h"
48#include "i915_irq.h"
49#include "i915_trace.h"
50#include "intel_pm.h"
51#include "intel_sideband.h"
52#include "../../../platform/x86/intel_ips.h"
53
54/* Stores plane specific WM parameters */
55struct skl_wm_params {
56 bool x_tiled, y_tiled;
57 bool rc_surface;
58 bool is_planar;
59 u32 width;
60 u8 cpp;
61 u32 plane_pixel_rate;
62 u32 y_min_scanlines;
63 u32 plane_bytes_per_line;
64 uint_fixed_16_16_t plane_blocks_per_line;
65 uint_fixed_16_16_t y_tile_minimum;
66 u32 linetime_us;
67 u32 dbuf_block_size;
68};
69
70/* used in computing the new watermarks state */
71struct intel_wm_config {
72 unsigned int num_pipes_active;
73 bool sprites_enabled;
74 bool sprites_scaled;
75};
76
77static void gen9_init_clock_gating(struct drm_i915_private *dev_priv)
78{
79 enum pipe pipe;
80
81 if (HAS_LLC(dev_priv)) {
82 /*
83 * WaCompressedResourceDisplayNewHashMode:skl,kbl
84 * Display WA #0390: skl,kbl
85 *
86 * Must match Sampler, Pixel Back End, and Media. See
87 * WaCompressedResourceSamplerPbeMediaNewHashMode.
88 */
89 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
90 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) |
91 SKL_DE_COMPRESSED_HASH_MODE);
92 }
93
94 for_each_pipe(dev_priv, pipe) {
95 /*
96 * "Plane N strech max must be programmed to 11b (x1)
97 * when Async flips are enabled on that plane."
98 */
99 if (!IS_GEMINILAKE(dev_priv) && intel_vtd_active())
100 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
101 SKL_PLANE1_STRETCH_MAX_MASK, SKL_PLANE1_STRETCH_MAX_X1);
102 }
103
104 /* See Bspec note for PSR2_CTL bit 31, Wa#828:skl,bxt,kbl,cfl */
105 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
106 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | SKL_EDP_PSR_FIX_RDWRAP);
107
108 /* WaEnableChickenDCPR:skl,bxt,kbl,glk,cfl */
109 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
110 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
111
112 /*
113 * WaFbcWakeMemOn:skl,bxt,kbl,glk,cfl
114 * Display WA #0859: skl,bxt,kbl,glk,cfl
115 */
116 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
117 DISP_FBC_MEMORY_WAKE);
118}
119
120static void bxt_init_clock_gating(struct drm_i915_private *dev_priv)
121{
122 gen9_init_clock_gating(dev_priv);
123
124 /* WaDisableSDEUnitClockGating:bxt */
125 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
126 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
127
128 /*
129 * FIXME:
130 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
131 */
132 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
133 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
134
135 /*
136 * Wa: Backlight PWM may stop in the asserted state, causing backlight
137 * to stay fully on.
138 */
139 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_0, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_0) |
140 PWM1_GATING_DIS | PWM2_GATING_DIS);
141
142 /*
143 * Lower the display internal timeout.
144 * This is needed to avoid any hard hangs when DSI port PLL
145 * is off and a MMIO access is attempted by any privilege
146 * application, using batch buffers or any other means.
147 */
148 intel_uncore_write(&dev_priv->uncore, RM_TIMEOUT, MMIO_TIMEOUT_US(950));
149
150 /*
151 * WaFbcTurnOffFbcWatermark:bxt
152 * Display WA #0562: bxt
153 */
154 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
155 DISP_FBC_WM_DIS);
156
157 /*
158 * WaFbcHighMemBwCorruptionAvoidance:bxt
159 * Display WA #0883: bxt
160 */
161 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
162 ILK_DPFC_DISABLE_DUMMY0);
163}
164
165static void glk_init_clock_gating(struct drm_i915_private *dev_priv)
166{
167 gen9_init_clock_gating(dev_priv);
168
169 /*
170 * WaDisablePWMClockGating:glk
171 * Backlight PWM may stop in the asserted state, causing backlight
172 * to stay fully on.
173 */
174 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_0, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_0) |
175 PWM1_GATING_DIS | PWM2_GATING_DIS);
176}
177
178static void pnv_get_mem_freq(struct drm_i915_private *dev_priv)
179{
180 u32 tmp;
181
182 tmp = intel_uncore_read(&dev_priv->uncore, CLKCFG);
183
184 switch (tmp & CLKCFG_FSB_MASK) {
185 case CLKCFG_FSB_533:
186 dev_priv->fsb_freq = 533; /* 133*4 */
187 break;
188 case CLKCFG_FSB_800:
189 dev_priv->fsb_freq = 800; /* 200*4 */
190 break;
191 case CLKCFG_FSB_667:
192 dev_priv->fsb_freq = 667; /* 167*4 */
193 break;
194 case CLKCFG_FSB_400:
195 dev_priv->fsb_freq = 400; /* 100*4 */
196 break;
197 }
198
199 switch (tmp & CLKCFG_MEM_MASK) {
200 case CLKCFG_MEM_533:
201 dev_priv->mem_freq = 533;
202 break;
203 case CLKCFG_MEM_667:
204 dev_priv->mem_freq = 667;
205 break;
206 case CLKCFG_MEM_800:
207 dev_priv->mem_freq = 800;
208 break;
209 }
210
211 /* detect pineview DDR3 setting */
212 tmp = intel_uncore_read(&dev_priv->uncore, CSHRDDR3CTL);
213 dev_priv->is_ddr3 = (tmp & CSHRDDR3CTL_DDR3) ? 1 : 0;
214}
215
216static void ilk_get_mem_freq(struct drm_i915_private *dev_priv)
217{
218 u16 ddrpll, csipll;
219
220 ddrpll = intel_uncore_read16(&dev_priv->uncore, DDRMPLL1);
221 csipll = intel_uncore_read16(&dev_priv->uncore, CSIPLL0);
222
223 switch (ddrpll & 0xff) {
224 case 0xc:
225 dev_priv->mem_freq = 800;
226 break;
227 case 0x10:
228 dev_priv->mem_freq = 1066;
229 break;
230 case 0x14:
231 dev_priv->mem_freq = 1333;
232 break;
233 case 0x18:
234 dev_priv->mem_freq = 1600;
235 break;
236 default:
237 drm_dbg(&dev_priv->drm, "unknown memory frequency 0x%02x\n",
238 ddrpll & 0xff);
239 dev_priv->mem_freq = 0;
240 break;
241 }
242
243 switch (csipll & 0x3ff) {
244 case 0x00c:
245 dev_priv->fsb_freq = 3200;
246 break;
247 case 0x00e:
248 dev_priv->fsb_freq = 3733;
249 break;
250 case 0x010:
251 dev_priv->fsb_freq = 4266;
252 break;
253 case 0x012:
254 dev_priv->fsb_freq = 4800;
255 break;
256 case 0x014:
257 dev_priv->fsb_freq = 5333;
258 break;
259 case 0x016:
260 dev_priv->fsb_freq = 5866;
261 break;
262 case 0x018:
263 dev_priv->fsb_freq = 6400;
264 break;
265 default:
266 drm_dbg(&dev_priv->drm, "unknown fsb frequency 0x%04x\n",
267 csipll & 0x3ff);
268 dev_priv->fsb_freq = 0;
269 break;
270 }
271}
272
273static const struct cxsr_latency cxsr_latency_table[] = {
274 {1, 0, 800, 400, 3382, 33382, 3983, 33983}, /* DDR2-400 SC */
275 {1, 0, 800, 667, 3354, 33354, 3807, 33807}, /* DDR2-667 SC */
276 {1, 0, 800, 800, 3347, 33347, 3763, 33763}, /* DDR2-800 SC */
277 {1, 1, 800, 667, 6420, 36420, 6873, 36873}, /* DDR3-667 SC */
278 {1, 1, 800, 800, 5902, 35902, 6318, 36318}, /* DDR3-800 SC */
279
280 {1, 0, 667, 400, 3400, 33400, 4021, 34021}, /* DDR2-400 SC */
281 {1, 0, 667, 667, 3372, 33372, 3845, 33845}, /* DDR2-667 SC */
282 {1, 0, 667, 800, 3386, 33386, 3822, 33822}, /* DDR2-800 SC */
283 {1, 1, 667, 667, 6438, 36438, 6911, 36911}, /* DDR3-667 SC */
284 {1, 1, 667, 800, 5941, 35941, 6377, 36377}, /* DDR3-800 SC */
285
286 {1, 0, 400, 400, 3472, 33472, 4173, 34173}, /* DDR2-400 SC */
287 {1, 0, 400, 667, 3443, 33443, 3996, 33996}, /* DDR2-667 SC */
288 {1, 0, 400, 800, 3430, 33430, 3946, 33946}, /* DDR2-800 SC */
289 {1, 1, 400, 667, 6509, 36509, 7062, 37062}, /* DDR3-667 SC */
290 {1, 1, 400, 800, 5985, 35985, 6501, 36501}, /* DDR3-800 SC */
291
292 {0, 0, 800, 400, 3438, 33438, 4065, 34065}, /* DDR2-400 SC */
293 {0, 0, 800, 667, 3410, 33410, 3889, 33889}, /* DDR2-667 SC */
294 {0, 0, 800, 800, 3403, 33403, 3845, 33845}, /* DDR2-800 SC */
295 {0, 1, 800, 667, 6476, 36476, 6955, 36955}, /* DDR3-667 SC */
296 {0, 1, 800, 800, 5958, 35958, 6400, 36400}, /* DDR3-800 SC */
297
298 {0, 0, 667, 400, 3456, 33456, 4103, 34106}, /* DDR2-400 SC */
299 {0, 0, 667, 667, 3428, 33428, 3927, 33927}, /* DDR2-667 SC */
300 {0, 0, 667, 800, 3443, 33443, 3905, 33905}, /* DDR2-800 SC */
301 {0, 1, 667, 667, 6494, 36494, 6993, 36993}, /* DDR3-667 SC */
302 {0, 1, 667, 800, 5998, 35998, 6460, 36460}, /* DDR3-800 SC */
303
304 {0, 0, 400, 400, 3528, 33528, 4255, 34255}, /* DDR2-400 SC */
305 {0, 0, 400, 667, 3500, 33500, 4079, 34079}, /* DDR2-667 SC */
306 {0, 0, 400, 800, 3487, 33487, 4029, 34029}, /* DDR2-800 SC */
307 {0, 1, 400, 667, 6566, 36566, 7145, 37145}, /* DDR3-667 SC */
308 {0, 1, 400, 800, 6042, 36042, 6584, 36584}, /* DDR3-800 SC */
309};
310
311static const struct cxsr_latency *intel_get_cxsr_latency(bool is_desktop,
312 bool is_ddr3,
313 int fsb,
314 int mem)
315{
316 const struct cxsr_latency *latency;
317 int i;
318
319 if (fsb == 0 || mem == 0)
320 return NULL;
321
322 for (i = 0; i < ARRAY_SIZE(cxsr_latency_table); i++) {
323 latency = &cxsr_latency_table[i];
324 if (is_desktop == latency->is_desktop &&
325 is_ddr3 == latency->is_ddr3 &&
326 fsb == latency->fsb_freq && mem == latency->mem_freq)
327 return latency;
328 }
329
330 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
331
332 return NULL;
333}
334
335static void chv_set_memory_dvfs(struct drm_i915_private *dev_priv, bool enable)
336{
337 u32 val;
338
339 vlv_punit_get(dev_priv);
340
341 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
342 if (enable)
343 val &= ~FORCE_DDR_HIGH_FREQ;
344 else
345 val |= FORCE_DDR_HIGH_FREQ;
346 val &= ~FORCE_DDR_LOW_FREQ;
347 val |= FORCE_DDR_FREQ_REQ_ACK;
348 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
349
350 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
351 FORCE_DDR_FREQ_REQ_ACK) == 0, 3))
352 drm_err(&dev_priv->drm,
353 "timed out waiting for Punit DDR DVFS request\n");
354
355 vlv_punit_put(dev_priv);
356}
357
358static void chv_set_memory_pm5(struct drm_i915_private *dev_priv, bool enable)
359{
360 u32 val;
361
362 vlv_punit_get(dev_priv);
363
364 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
365 if (enable)
366 val |= DSP_MAXFIFO_PM5_ENABLE;
367 else
368 val &= ~DSP_MAXFIFO_PM5_ENABLE;
369 vlv_punit_write(dev_priv, PUNIT_REG_DSPSSPM, val);
370
371 vlv_punit_put(dev_priv);
372}
373
374#define FW_WM(value, plane) \
375 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
376
377static bool _intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
378{
379 bool was_enabled;
380 u32 val;
381
382 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
383 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
384 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
385 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF_VLV);
386 } else if (IS_G4X(dev_priv) || IS_I965GM(dev_priv)) {
387 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
388 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
389 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
390 } else if (IS_PINEVIEW(dev_priv)) {
391 val = intel_uncore_read(&dev_priv->uncore, DSPFW3);
392 was_enabled = val & PINEVIEW_SELF_REFRESH_EN;
393 if (enable)
394 val |= PINEVIEW_SELF_REFRESH_EN;
395 else
396 val &= ~PINEVIEW_SELF_REFRESH_EN;
397 intel_uncore_write(&dev_priv->uncore, DSPFW3, val);
398 intel_uncore_posting_read(&dev_priv->uncore, DSPFW3);
399 } else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv)) {
400 was_enabled = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
401 val = enable ? _MASKED_BIT_ENABLE(FW_BLC_SELF_EN) :
402 _MASKED_BIT_DISABLE(FW_BLC_SELF_EN);
403 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, val);
404 intel_uncore_posting_read(&dev_priv->uncore, FW_BLC_SELF);
405 } else if (IS_I915GM(dev_priv)) {
406 /*
407 * FIXME can't find a bit like this for 915G, and
408 * and yet it does have the related watermark in
409 * FW_BLC_SELF. What's going on?
410 */
411 was_enabled = intel_uncore_read(&dev_priv->uncore, INSTPM) & INSTPM_SELF_EN;
412 val = enable ? _MASKED_BIT_ENABLE(INSTPM_SELF_EN) :
413 _MASKED_BIT_DISABLE(INSTPM_SELF_EN);
414 intel_uncore_write(&dev_priv->uncore, INSTPM, val);
415 intel_uncore_posting_read(&dev_priv->uncore, INSTPM);
416 } else {
417 return false;
418 }
419
420 trace_intel_memory_cxsr(dev_priv, was_enabled, enable);
421
422 drm_dbg_kms(&dev_priv->drm, "memory self-refresh is %s (was %s)\n",
423 enableddisabled(enable),
424 enableddisabled(was_enabled));
425
426 return was_enabled;
427}
428
429/**
430 * intel_set_memory_cxsr - Configure CxSR state
431 * @dev_priv: i915 device
432 * @enable: Allow vs. disallow CxSR
433 *
434 * Allow or disallow the system to enter a special CxSR
435 * (C-state self refresh) state. What typically happens in CxSR mode
436 * is that several display FIFOs may get combined into a single larger
437 * FIFO for a particular plane (so called max FIFO mode) to allow the
438 * system to defer memory fetches longer, and the memory will enter
439 * self refresh.
440 *
441 * Note that enabling CxSR does not guarantee that the system enter
442 * this special mode, nor does it guarantee that the system stays
443 * in that mode once entered. So this just allows/disallows the system
444 * to autonomously utilize the CxSR mode. Other factors such as core
445 * C-states will affect when/if the system actually enters/exits the
446 * CxSR mode.
447 *
448 * Note that on VLV/CHV this actually only controls the max FIFO mode,
449 * and the system is free to enter/exit memory self refresh at any time
450 * even when the use of CxSR has been disallowed.
451 *
452 * While the system is actually in the CxSR/max FIFO mode, some plane
453 * control registers will not get latched on vblank. Thus in order to
454 * guarantee the system will respond to changes in the plane registers
455 * we must always disallow CxSR prior to making changes to those registers.
456 * Unfortunately the system will re-evaluate the CxSR conditions at
457 * frame start which happens after vblank start (which is when the plane
458 * registers would get latched), so we can't proceed with the plane update
459 * during the same frame where we disallowed CxSR.
460 *
461 * Certain platforms also have a deeper HPLL SR mode. Fortunately the
462 * HPLL SR mode depends on CxSR itself, so we don't have to hand hold
463 * the hardware w.r.t. HPLL SR when writing to plane registers.
464 * Disallowing just CxSR is sufficient.
465 */
466bool intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
467{
468 bool ret;
469
470 mutex_lock(&dev_priv->wm.wm_mutex);
471 ret = _intel_set_memory_cxsr(dev_priv, enable);
472 if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
473 dev_priv->wm.vlv.cxsr = enable;
474 else if (IS_G4X(dev_priv))
475 dev_priv->wm.g4x.cxsr = enable;
476 mutex_unlock(&dev_priv->wm.wm_mutex);
477
478 return ret;
479}
480
481/*
482 * Latency for FIFO fetches is dependent on several factors:
483 * - memory configuration (speed, channels)
484 * - chipset
485 * - current MCH state
486 * It can be fairly high in some situations, so here we assume a fairly
487 * pessimal value. It's a tradeoff between extra memory fetches (if we
488 * set this value too high, the FIFO will fetch frequently to stay full)
489 * and power consumption (set it too low to save power and we might see
490 * FIFO underruns and display "flicker").
491 *
492 * A value of 5us seems to be a good balance; safe for very low end
493 * platforms but not overly aggressive on lower latency configs.
494 */
495static const int pessimal_latency_ns = 5000;
496
497#define VLV_FIFO_START(dsparb, dsparb2, lo_shift, hi_shift) \
498 ((((dsparb) >> (lo_shift)) & 0xff) | ((((dsparb2) >> (hi_shift)) & 0x1) << 8))
499
500static void vlv_get_fifo_size(struct intel_crtc_state *crtc_state)
501{
502 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
503 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
504 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
505 enum pipe pipe = crtc->pipe;
506 int sprite0_start, sprite1_start;
507 u32 dsparb, dsparb2, dsparb3;
508
509 switch (pipe) {
510 case PIPE_A:
511 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
512 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
513 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 0, 0);
514 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 8, 4);
515 break;
516 case PIPE_B:
517 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
518 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
519 sprite0_start = VLV_FIFO_START(dsparb, dsparb2, 16, 8);
520 sprite1_start = VLV_FIFO_START(dsparb, dsparb2, 24, 12);
521 break;
522 case PIPE_C:
523 dsparb2 = intel_uncore_read(&dev_priv->uncore, DSPARB2);
524 dsparb3 = intel_uncore_read(&dev_priv->uncore, DSPARB3);
525 sprite0_start = VLV_FIFO_START(dsparb3, dsparb2, 0, 16);
526 sprite1_start = VLV_FIFO_START(dsparb3, dsparb2, 8, 20);
527 break;
528 default:
529 MISSING_CASE(pipe);
530 return;
531 }
532
533 fifo_state->plane[PLANE_PRIMARY] = sprite0_start;
534 fifo_state->plane[PLANE_SPRITE0] = sprite1_start - sprite0_start;
535 fifo_state->plane[PLANE_SPRITE1] = 511 - sprite1_start;
536 fifo_state->plane[PLANE_CURSOR] = 63;
537}
538
539static int i9xx_get_fifo_size(struct drm_i915_private *dev_priv,
540 enum i9xx_plane_id i9xx_plane)
541{
542 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
543 int size;
544
545 size = dsparb & 0x7f;
546 if (i9xx_plane == PLANE_B)
547 size = ((dsparb >> DSPARB_CSTART_SHIFT) & 0x7f) - size;
548
549 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
550 dsparb, plane_name(i9xx_plane), size);
551
552 return size;
553}
554
555static int i830_get_fifo_size(struct drm_i915_private *dev_priv,
556 enum i9xx_plane_id i9xx_plane)
557{
558 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
559 int size;
560
561 size = dsparb & 0x1ff;
562 if (i9xx_plane == PLANE_B)
563 size = ((dsparb >> DSPARB_BEND_SHIFT) & 0x1ff) - size;
564 size >>= 1; /* Convert to cachelines */
565
566 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
567 dsparb, plane_name(i9xx_plane), size);
568
569 return size;
570}
571
572static int i845_get_fifo_size(struct drm_i915_private *dev_priv,
573 enum i9xx_plane_id i9xx_plane)
574{
575 u32 dsparb = intel_uncore_read(&dev_priv->uncore, DSPARB);
576 int size;
577
578 size = dsparb & 0x7f;
579 size >>= 2; /* Convert to cachelines */
580
581 drm_dbg_kms(&dev_priv->drm, "FIFO size - (0x%08x) %c: %d\n",
582 dsparb, plane_name(i9xx_plane), size);
583
584 return size;
585}
586
587/* Pineview has different values for various configs */
588static const struct intel_watermark_params pnv_display_wm = {
589 .fifo_size = PINEVIEW_DISPLAY_FIFO,
590 .max_wm = PINEVIEW_MAX_WM,
591 .default_wm = PINEVIEW_DFT_WM,
592 .guard_size = PINEVIEW_GUARD_WM,
593 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
594};
595
596static const struct intel_watermark_params pnv_display_hplloff_wm = {
597 .fifo_size = PINEVIEW_DISPLAY_FIFO,
598 .max_wm = PINEVIEW_MAX_WM,
599 .default_wm = PINEVIEW_DFT_HPLLOFF_WM,
600 .guard_size = PINEVIEW_GUARD_WM,
601 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
602};
603
604static const struct intel_watermark_params pnv_cursor_wm = {
605 .fifo_size = PINEVIEW_CURSOR_FIFO,
606 .max_wm = PINEVIEW_CURSOR_MAX_WM,
607 .default_wm = PINEVIEW_CURSOR_DFT_WM,
608 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
609 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
610};
611
612static const struct intel_watermark_params pnv_cursor_hplloff_wm = {
613 .fifo_size = PINEVIEW_CURSOR_FIFO,
614 .max_wm = PINEVIEW_CURSOR_MAX_WM,
615 .default_wm = PINEVIEW_CURSOR_DFT_WM,
616 .guard_size = PINEVIEW_CURSOR_GUARD_WM,
617 .cacheline_size = PINEVIEW_FIFO_LINE_SIZE,
618};
619
620static const struct intel_watermark_params i965_cursor_wm_info = {
621 .fifo_size = I965_CURSOR_FIFO,
622 .max_wm = I965_CURSOR_MAX_WM,
623 .default_wm = I965_CURSOR_DFT_WM,
624 .guard_size = 2,
625 .cacheline_size = I915_FIFO_LINE_SIZE,
626};
627
628static const struct intel_watermark_params i945_wm_info = {
629 .fifo_size = I945_FIFO_SIZE,
630 .max_wm = I915_MAX_WM,
631 .default_wm = 1,
632 .guard_size = 2,
633 .cacheline_size = I915_FIFO_LINE_SIZE,
634};
635
636static const struct intel_watermark_params i915_wm_info = {
637 .fifo_size = I915_FIFO_SIZE,
638 .max_wm = I915_MAX_WM,
639 .default_wm = 1,
640 .guard_size = 2,
641 .cacheline_size = I915_FIFO_LINE_SIZE,
642};
643
644static const struct intel_watermark_params i830_a_wm_info = {
645 .fifo_size = I855GM_FIFO_SIZE,
646 .max_wm = I915_MAX_WM,
647 .default_wm = 1,
648 .guard_size = 2,
649 .cacheline_size = I830_FIFO_LINE_SIZE,
650};
651
652static const struct intel_watermark_params i830_bc_wm_info = {
653 .fifo_size = I855GM_FIFO_SIZE,
654 .max_wm = I915_MAX_WM/2,
655 .default_wm = 1,
656 .guard_size = 2,
657 .cacheline_size = I830_FIFO_LINE_SIZE,
658};
659
660static const struct intel_watermark_params i845_wm_info = {
661 .fifo_size = I830_FIFO_SIZE,
662 .max_wm = I915_MAX_WM,
663 .default_wm = 1,
664 .guard_size = 2,
665 .cacheline_size = I830_FIFO_LINE_SIZE,
666};
667
668/**
669 * intel_wm_method1 - Method 1 / "small buffer" watermark formula
670 * @pixel_rate: Pipe pixel rate in kHz
671 * @cpp: Plane bytes per pixel
672 * @latency: Memory wakeup latency in 0.1us units
673 *
674 * Compute the watermark using the method 1 or "small buffer"
675 * formula. The caller may additonally add extra cachelines
676 * to account for TLB misses and clock crossings.
677 *
678 * This method is concerned with the short term drain rate
679 * of the FIFO, ie. it does not account for blanking periods
680 * which would effectively reduce the average drain rate across
681 * a longer period. The name "small" refers to the fact the
682 * FIFO is relatively small compared to the amount of data
683 * fetched.
684 *
685 * The FIFO level vs. time graph might look something like:
686 *
687 * |\ |\
688 * | \ | \
689 * __---__---__ (- plane active, _ blanking)
690 * -> time
691 *
692 * or perhaps like this:
693 *
694 * |\|\ |\|\
695 * __----__----__ (- plane active, _ blanking)
696 * -> time
697 *
698 * Returns:
699 * The watermark in bytes
700 */
701static unsigned int intel_wm_method1(unsigned int pixel_rate,
702 unsigned int cpp,
703 unsigned int latency)
704{
705 u64 ret;
706
707 ret = mul_u32_u32(pixel_rate, cpp * latency);
708 ret = DIV_ROUND_UP_ULL(ret, 10000);
709
710 return ret;
711}
712
713/**
714 * intel_wm_method2 - Method 2 / "large buffer" watermark formula
715 * @pixel_rate: Pipe pixel rate in kHz
716 * @htotal: Pipe horizontal total
717 * @width: Plane width in pixels
718 * @cpp: Plane bytes per pixel
719 * @latency: Memory wakeup latency in 0.1us units
720 *
721 * Compute the watermark using the method 2 or "large buffer"
722 * formula. The caller may additonally add extra cachelines
723 * to account for TLB misses and clock crossings.
724 *
725 * This method is concerned with the long term drain rate
726 * of the FIFO, ie. it does account for blanking periods
727 * which effectively reduce the average drain rate across
728 * a longer period. The name "large" refers to the fact the
729 * FIFO is relatively large compared to the amount of data
730 * fetched.
731 *
732 * The FIFO level vs. time graph might look something like:
733 *
734 * |\___ |\___
735 * | \___ | \___
736 * | \ | \
737 * __ --__--__--__--__--__--__ (- plane active, _ blanking)
738 * -> time
739 *
740 * Returns:
741 * The watermark in bytes
742 */
743static unsigned int intel_wm_method2(unsigned int pixel_rate,
744 unsigned int htotal,
745 unsigned int width,
746 unsigned int cpp,
747 unsigned int latency)
748{
749 unsigned int ret;
750
751 /*
752 * FIXME remove once all users are computing
753 * watermarks in the correct place.
754 */
755 if (WARN_ON_ONCE(htotal == 0))
756 htotal = 1;
757
758 ret = (latency * pixel_rate) / (htotal * 10000);
759 ret = (ret + 1) * width * cpp;
760
761 return ret;
762}
763
764/**
765 * intel_calculate_wm - calculate watermark level
766 * @pixel_rate: pixel clock
767 * @wm: chip FIFO params
768 * @fifo_size: size of the FIFO buffer
769 * @cpp: bytes per pixel
770 * @latency_ns: memory latency for the platform
771 *
772 * Calculate the watermark level (the level at which the display plane will
773 * start fetching from memory again). Each chip has a different display
774 * FIFO size and allocation, so the caller needs to figure that out and pass
775 * in the correct intel_watermark_params structure.
776 *
777 * As the pixel clock runs, the FIFO will be drained at a rate that depends
778 * on the pixel size. When it reaches the watermark level, it'll start
779 * fetching FIFO line sized based chunks from memory until the FIFO fills
780 * past the watermark point. If the FIFO drains completely, a FIFO underrun
781 * will occur, and a display engine hang could result.
782 */
783static unsigned int intel_calculate_wm(int pixel_rate,
784 const struct intel_watermark_params *wm,
785 int fifo_size, int cpp,
786 unsigned int latency_ns)
787{
788 int entries, wm_size;
789
790 /*
791 * Note: we need to make sure we don't overflow for various clock &
792 * latency values.
793 * clocks go from a few thousand to several hundred thousand.
794 * latency is usually a few thousand
795 */
796 entries = intel_wm_method1(pixel_rate, cpp,
797 latency_ns / 100);
798 entries = DIV_ROUND_UP(entries, wm->cacheline_size) +
799 wm->guard_size;
800 DRM_DEBUG_KMS("FIFO entries required for mode: %d\n", entries);
801
802 wm_size = fifo_size - entries;
803 DRM_DEBUG_KMS("FIFO watermark level: %d\n", wm_size);
804
805 /* Don't promote wm_size to unsigned... */
806 if (wm_size > wm->max_wm)
807 wm_size = wm->max_wm;
808 if (wm_size <= 0)
809 wm_size = wm->default_wm;
810
811 /*
812 * Bspec seems to indicate that the value shouldn't be lower than
813 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
814 * Lets go for 8 which is the burst size since certain platforms
815 * already use a hardcoded 8 (which is what the spec says should be
816 * done).
817 */
818 if (wm_size <= 8)
819 wm_size = 8;
820
821 return wm_size;
822}
823
824static bool is_disabling(int old, int new, int threshold)
825{
826 return old >= threshold && new < threshold;
827}
828
829static bool is_enabling(int old, int new, int threshold)
830{
831 return old < threshold && new >= threshold;
832}
833
834static int intel_wm_num_levels(struct drm_i915_private *dev_priv)
835{
836 return dev_priv->wm.max_level + 1;
837}
838
839static bool intel_wm_plane_visible(const struct intel_crtc_state *crtc_state,
840 const struct intel_plane_state *plane_state)
841{
842 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
843
844 /* FIXME check the 'enable' instead */
845 if (!crtc_state->hw.active)
846 return false;
847
848 /*
849 * Treat cursor with fb as always visible since cursor updates
850 * can happen faster than the vrefresh rate, and the current
851 * watermark code doesn't handle that correctly. Cursor updates
852 * which set/clear the fb or change the cursor size are going
853 * to get throttled by intel_legacy_cursor_update() to work
854 * around this problem with the watermark code.
855 */
856 if (plane->id == PLANE_CURSOR)
857 return plane_state->hw.fb != NULL;
858 else
859 return plane_state->uapi.visible;
860}
861
862static bool intel_crtc_active(struct intel_crtc *crtc)
863{
864 /* Be paranoid as we can arrive here with only partial
865 * state retrieved from the hardware during setup.
866 *
867 * We can ditch the adjusted_mode.crtc_clock check as soon
868 * as Haswell has gained clock readout/fastboot support.
869 *
870 * We can ditch the crtc->primary->state->fb check as soon as we can
871 * properly reconstruct framebuffers.
872 *
873 * FIXME: The intel_crtc->active here should be switched to
874 * crtc->state->active once we have proper CRTC states wired up
875 * for atomic.
876 */
877 return crtc->active && crtc->base.primary->state->fb &&
878 crtc->config->hw.adjusted_mode.crtc_clock;
879}
880
881static struct intel_crtc *single_enabled_crtc(struct drm_i915_private *dev_priv)
882{
883 struct intel_crtc *crtc, *enabled = NULL;
884
885 for_each_intel_crtc(&dev_priv->drm, crtc) {
886 if (intel_crtc_active(crtc)) {
887 if (enabled)
888 return NULL;
889 enabled = crtc;
890 }
891 }
892
893 return enabled;
894}
895
896static void pnv_update_wm(struct intel_crtc *unused_crtc)
897{
898 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
899 struct intel_crtc *crtc;
900 const struct cxsr_latency *latency;
901 u32 reg;
902 unsigned int wm;
903
904 latency = intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
905 dev_priv->is_ddr3,
906 dev_priv->fsb_freq,
907 dev_priv->mem_freq);
908 if (!latency) {
909 drm_dbg_kms(&dev_priv->drm,
910 "Unknown FSB/MEM found, disable CxSR\n");
911 intel_set_memory_cxsr(dev_priv, false);
912 return;
913 }
914
915 crtc = single_enabled_crtc(dev_priv);
916 if (crtc) {
917 const struct drm_display_mode *pipe_mode =
918 &crtc->config->hw.pipe_mode;
919 const struct drm_framebuffer *fb =
920 crtc->base.primary->state->fb;
921 int cpp = fb->format->cpp[0];
922 int clock = pipe_mode->crtc_clock;
923
924 /* Display SR */
925 wm = intel_calculate_wm(clock, &pnv_display_wm,
926 pnv_display_wm.fifo_size,
927 cpp, latency->display_sr);
928 reg = intel_uncore_read(&dev_priv->uncore, DSPFW1);
929 reg &= ~DSPFW_SR_MASK;
930 reg |= FW_WM(wm, SR);
931 intel_uncore_write(&dev_priv->uncore, DSPFW1, reg);
932 drm_dbg_kms(&dev_priv->drm, "DSPFW1 register is %x\n", reg);
933
934 /* cursor SR */
935 wm = intel_calculate_wm(clock, &pnv_cursor_wm,
936 pnv_display_wm.fifo_size,
937 4, latency->cursor_sr);
938 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
939 reg &= ~DSPFW_CURSOR_SR_MASK;
940 reg |= FW_WM(wm, CURSOR_SR);
941 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
942
943 /* Display HPLL off SR */
944 wm = intel_calculate_wm(clock, &pnv_display_hplloff_wm,
945 pnv_display_hplloff_wm.fifo_size,
946 cpp, latency->display_hpll_disable);
947 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
948 reg &= ~DSPFW_HPLL_SR_MASK;
949 reg |= FW_WM(wm, HPLL_SR);
950 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
951
952 /* cursor HPLL off SR */
953 wm = intel_calculate_wm(clock, &pnv_cursor_hplloff_wm,
954 pnv_display_hplloff_wm.fifo_size,
955 4, latency->cursor_hpll_disable);
956 reg = intel_uncore_read(&dev_priv->uncore, DSPFW3);
957 reg &= ~DSPFW_HPLL_CURSOR_MASK;
958 reg |= FW_WM(wm, HPLL_CURSOR);
959 intel_uncore_write(&dev_priv->uncore, DSPFW3, reg);
960 drm_dbg_kms(&dev_priv->drm, "DSPFW3 register is %x\n", reg);
961
962 intel_set_memory_cxsr(dev_priv, true);
963 } else {
964 intel_set_memory_cxsr(dev_priv, false);
965 }
966}
967
968/*
969 * Documentation says:
970 * "If the line size is small, the TLB fetches can get in the way of the
971 * data fetches, causing some lag in the pixel data return which is not
972 * accounted for in the above formulas. The following adjustment only
973 * needs to be applied if eight whole lines fit in the buffer at once.
974 * The WM is adjusted upwards by the difference between the FIFO size
975 * and the size of 8 whole lines. This adjustment is always performed
976 * in the actual pixel depth regardless of whether FBC is enabled or not."
977 */
978static unsigned int g4x_tlb_miss_wa(int fifo_size, int width, int cpp)
979{
980 int tlb_miss = fifo_size * 64 - width * cpp * 8;
981
982 return max(0, tlb_miss);
983}
984
985static void g4x_write_wm_values(struct drm_i915_private *dev_priv,
986 const struct g4x_wm_values *wm)
987{
988 enum pipe pipe;
989
990 for_each_pipe(dev_priv, pipe)
991 trace_g4x_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
992
993 intel_uncore_write(&dev_priv->uncore, DSPFW1,
994 FW_WM(wm->sr.plane, SR) |
995 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
996 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
997 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
998 intel_uncore_write(&dev_priv->uncore, DSPFW2,
999 (wm->fbc_en ? DSPFW_FBC_SR_EN : 0) |
1000 FW_WM(wm->sr.fbc, FBC_SR) |
1001 FW_WM(wm->hpll.fbc, FBC_HPLL_SR) |
1002 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEB) |
1003 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1004 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
1005 intel_uncore_write(&dev_priv->uncore, DSPFW3,
1006 (wm->hpll_en ? DSPFW_HPLL_SR_EN : 0) |
1007 FW_WM(wm->sr.cursor, CURSOR_SR) |
1008 FW_WM(wm->hpll.cursor, HPLL_CURSOR) |
1009 FW_WM(wm->hpll.plane, HPLL_SR));
1010
1011 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
1012}
1013
1014#define FW_WM_VLV(value, plane) \
1015 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
1016
1017static void vlv_write_wm_values(struct drm_i915_private *dev_priv,
1018 const struct vlv_wm_values *wm)
1019{
1020 enum pipe pipe;
1021
1022 for_each_pipe(dev_priv, pipe) {
1023 trace_vlv_wm(intel_get_crtc_for_pipe(dev_priv, pipe), wm);
1024
1025 intel_uncore_write(&dev_priv->uncore, VLV_DDL(pipe),
1026 (wm->ddl[pipe].plane[PLANE_CURSOR] << DDL_CURSOR_SHIFT) |
1027 (wm->ddl[pipe].plane[PLANE_SPRITE1] << DDL_SPRITE_SHIFT(1)) |
1028 (wm->ddl[pipe].plane[PLANE_SPRITE0] << DDL_SPRITE_SHIFT(0)) |
1029 (wm->ddl[pipe].plane[PLANE_PRIMARY] << DDL_PLANE_SHIFT));
1030 }
1031
1032 /*
1033 * Zero the (unused) WM1 watermarks, and also clear all the
1034 * high order bits so that there are no out of bounds values
1035 * present in the registers during the reprogramming.
1036 */
1037 intel_uncore_write(&dev_priv->uncore, DSPHOWM, 0);
1038 intel_uncore_write(&dev_priv->uncore, DSPHOWM1, 0);
1039 intel_uncore_write(&dev_priv->uncore, DSPFW4, 0);
1040 intel_uncore_write(&dev_priv->uncore, DSPFW5, 0);
1041 intel_uncore_write(&dev_priv->uncore, DSPFW6, 0);
1042
1043 intel_uncore_write(&dev_priv->uncore, DSPFW1,
1044 FW_WM(wm->sr.plane, SR) |
1045 FW_WM(wm->pipe[PIPE_B].plane[PLANE_CURSOR], CURSORB) |
1046 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_PRIMARY], PLANEB) |
1047 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_PRIMARY], PLANEA));
1048 intel_uncore_write(&dev_priv->uncore, DSPFW2,
1049 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE1], SPRITEB) |
1050 FW_WM(wm->pipe[PIPE_A].plane[PLANE_CURSOR], CURSORA) |
1051 FW_WM_VLV(wm->pipe[PIPE_A].plane[PLANE_SPRITE0], SPRITEA));
1052 intel_uncore_write(&dev_priv->uncore, DSPFW3,
1053 FW_WM(wm->sr.cursor, CURSOR_SR));
1054
1055 if (IS_CHERRYVIEW(dev_priv)) {
1056 intel_uncore_write(&dev_priv->uncore, DSPFW7_CHV,
1057 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1058 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
1059 intel_uncore_write(&dev_priv->uncore, DSPFW8_CHV,
1060 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE1], SPRITEF) |
1061 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_SPRITE0], SPRITEE));
1062 intel_uncore_write(&dev_priv->uncore, DSPFW9_CHV,
1063 FW_WM_VLV(wm->pipe[PIPE_C].plane[PLANE_PRIMARY], PLANEC) |
1064 FW_WM(wm->pipe[PIPE_C].plane[PLANE_CURSOR], CURSORC));
1065 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
1066 FW_WM(wm->sr.plane >> 9, SR_HI) |
1067 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE1] >> 8, SPRITEF_HI) |
1068 FW_WM(wm->pipe[PIPE_C].plane[PLANE_SPRITE0] >> 8, SPRITEE_HI) |
1069 FW_WM(wm->pipe[PIPE_C].plane[PLANE_PRIMARY] >> 8, PLANEC_HI) |
1070 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1071 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1072 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1073 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1074 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1075 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
1076 } else {
1077 intel_uncore_write(&dev_priv->uncore, DSPFW7,
1078 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE1], SPRITED) |
1079 FW_WM_VLV(wm->pipe[PIPE_B].plane[PLANE_SPRITE0], SPRITEC));
1080 intel_uncore_write(&dev_priv->uncore, DSPHOWM,
1081 FW_WM(wm->sr.plane >> 9, SR_HI) |
1082 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE1] >> 8, SPRITED_HI) |
1083 FW_WM(wm->pipe[PIPE_B].plane[PLANE_SPRITE0] >> 8, SPRITEC_HI) |
1084 FW_WM(wm->pipe[PIPE_B].plane[PLANE_PRIMARY] >> 8, PLANEB_HI) |
1085 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE1] >> 8, SPRITEB_HI) |
1086 FW_WM(wm->pipe[PIPE_A].plane[PLANE_SPRITE0] >> 8, SPRITEA_HI) |
1087 FW_WM(wm->pipe[PIPE_A].plane[PLANE_PRIMARY] >> 8, PLANEA_HI));
1088 }
1089
1090 intel_uncore_posting_read(&dev_priv->uncore, DSPFW1);
1091}
1092
1093#undef FW_WM_VLV
1094
1095static void g4x_setup_wm_latency(struct drm_i915_private *dev_priv)
1096{
1097 /* all latencies in usec */
1098 dev_priv->wm.pri_latency[G4X_WM_LEVEL_NORMAL] = 5;
1099 dev_priv->wm.pri_latency[G4X_WM_LEVEL_SR] = 12;
1100 dev_priv->wm.pri_latency[G4X_WM_LEVEL_HPLL] = 35;
1101
1102 dev_priv->wm.max_level = G4X_WM_LEVEL_HPLL;
1103}
1104
1105static int g4x_plane_fifo_size(enum plane_id plane_id, int level)
1106{
1107 /*
1108 * DSPCNTR[13] supposedly controls whether the
1109 * primary plane can use the FIFO space otherwise
1110 * reserved for the sprite plane. It's not 100% clear
1111 * what the actual FIFO size is, but it looks like we
1112 * can happily set both primary and sprite watermarks
1113 * up to 127 cachelines. So that would seem to mean
1114 * that either DSPCNTR[13] doesn't do anything, or that
1115 * the total FIFO is >= 256 cachelines in size. Either
1116 * way, we don't seem to have to worry about this
1117 * repartitioning as the maximum watermark value the
1118 * register can hold for each plane is lower than the
1119 * minimum FIFO size.
1120 */
1121 switch (plane_id) {
1122 case PLANE_CURSOR:
1123 return 63;
1124 case PLANE_PRIMARY:
1125 return level == G4X_WM_LEVEL_NORMAL ? 127 : 511;
1126 case PLANE_SPRITE0:
1127 return level == G4X_WM_LEVEL_NORMAL ? 127 : 0;
1128 default:
1129 MISSING_CASE(plane_id);
1130 return 0;
1131 }
1132}
1133
1134static int g4x_fbc_fifo_size(int level)
1135{
1136 switch (level) {
1137 case G4X_WM_LEVEL_SR:
1138 return 7;
1139 case G4X_WM_LEVEL_HPLL:
1140 return 15;
1141 default:
1142 MISSING_CASE(level);
1143 return 0;
1144 }
1145}
1146
1147static u16 g4x_compute_wm(const struct intel_crtc_state *crtc_state,
1148 const struct intel_plane_state *plane_state,
1149 int level)
1150{
1151 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1152 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1153 const struct drm_display_mode *pipe_mode =
1154 &crtc_state->hw.pipe_mode;
1155 unsigned int latency = dev_priv->wm.pri_latency[level] * 10;
1156 unsigned int clock, htotal, cpp, width, wm;
1157
1158 if (latency == 0)
1159 return USHRT_MAX;
1160
1161 if (!intel_wm_plane_visible(crtc_state, plane_state))
1162 return 0;
1163
1164 cpp = plane_state->hw.fb->format->cpp[0];
1165
1166 /*
1167 * Not 100% sure which way ELK should go here as the
1168 * spec only says CL/CTG should assume 32bpp and BW
1169 * doesn't need to. But as these things followed the
1170 * mobile vs. desktop lines on gen3 as well, let's
1171 * assume ELK doesn't need this.
1172 *
1173 * The spec also fails to list such a restriction for
1174 * the HPLL watermark, which seems a little strange.
1175 * Let's use 32bpp for the HPLL watermark as well.
1176 */
1177 if (IS_GM45(dev_priv) && plane->id == PLANE_PRIMARY &&
1178 level != G4X_WM_LEVEL_NORMAL)
1179 cpp = max(cpp, 4u);
1180
1181 clock = pipe_mode->crtc_clock;
1182 htotal = pipe_mode->crtc_htotal;
1183
1184 width = drm_rect_width(&plane_state->uapi.dst);
1185
1186 if (plane->id == PLANE_CURSOR) {
1187 wm = intel_wm_method2(clock, htotal, width, cpp, latency);
1188 } else if (plane->id == PLANE_PRIMARY &&
1189 level == G4X_WM_LEVEL_NORMAL) {
1190 wm = intel_wm_method1(clock, cpp, latency);
1191 } else {
1192 unsigned int small, large;
1193
1194 small = intel_wm_method1(clock, cpp, latency);
1195 large = intel_wm_method2(clock, htotal, width, cpp, latency);
1196
1197 wm = min(small, large);
1198 }
1199
1200 wm += g4x_tlb_miss_wa(g4x_plane_fifo_size(plane->id, level),
1201 width, cpp);
1202
1203 wm = DIV_ROUND_UP(wm, 64) + 2;
1204
1205 return min_t(unsigned int, wm, USHRT_MAX);
1206}
1207
1208static bool g4x_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1209 int level, enum plane_id plane_id, u16 value)
1210{
1211 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1212 bool dirty = false;
1213
1214 for (; level < intel_wm_num_levels(dev_priv); level++) {
1215 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1216
1217 dirty |= raw->plane[plane_id] != value;
1218 raw->plane[plane_id] = value;
1219 }
1220
1221 return dirty;
1222}
1223
1224static bool g4x_raw_fbc_wm_set(struct intel_crtc_state *crtc_state,
1225 int level, u16 value)
1226{
1227 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1228 bool dirty = false;
1229
1230 /* NORMAL level doesn't have an FBC watermark */
1231 level = max(level, G4X_WM_LEVEL_SR);
1232
1233 for (; level < intel_wm_num_levels(dev_priv); level++) {
1234 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1235
1236 dirty |= raw->fbc != value;
1237 raw->fbc = value;
1238 }
1239
1240 return dirty;
1241}
1242
1243static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
1244 const struct intel_plane_state *plane_state,
1245 u32 pri_val);
1246
1247static bool g4x_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1248 const struct intel_plane_state *plane_state)
1249{
1250 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1251 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1252 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1253 enum plane_id plane_id = plane->id;
1254 bool dirty = false;
1255 int level;
1256
1257 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1258 dirty |= g4x_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1259 if (plane_id == PLANE_PRIMARY)
1260 dirty |= g4x_raw_fbc_wm_set(crtc_state, 0, 0);
1261 goto out;
1262 }
1263
1264 for (level = 0; level < num_levels; level++) {
1265 struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1266 int wm, max_wm;
1267
1268 wm = g4x_compute_wm(crtc_state, plane_state, level);
1269 max_wm = g4x_plane_fifo_size(plane_id, level);
1270
1271 if (wm > max_wm)
1272 break;
1273
1274 dirty |= raw->plane[plane_id] != wm;
1275 raw->plane[plane_id] = wm;
1276
1277 if (plane_id != PLANE_PRIMARY ||
1278 level == G4X_WM_LEVEL_NORMAL)
1279 continue;
1280
1281 wm = ilk_compute_fbc_wm(crtc_state, plane_state,
1282 raw->plane[plane_id]);
1283 max_wm = g4x_fbc_fifo_size(level);
1284
1285 /*
1286 * FBC wm is not mandatory as we
1287 * can always just disable its use.
1288 */
1289 if (wm > max_wm)
1290 wm = USHRT_MAX;
1291
1292 dirty |= raw->fbc != wm;
1293 raw->fbc = wm;
1294 }
1295
1296 /* mark watermarks as invalid */
1297 dirty |= g4x_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1298
1299 if (plane_id == PLANE_PRIMARY)
1300 dirty |= g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
1301
1302 out:
1303 if (dirty) {
1304 drm_dbg_kms(&dev_priv->drm,
1305 "%s watermarks: normal=%d, SR=%d, HPLL=%d\n",
1306 plane->base.name,
1307 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_NORMAL].plane[plane_id],
1308 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].plane[plane_id],
1309 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].plane[plane_id]);
1310
1311 if (plane_id == PLANE_PRIMARY)
1312 drm_dbg_kms(&dev_priv->drm,
1313 "FBC watermarks: SR=%d, HPLL=%d\n",
1314 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_SR].fbc,
1315 crtc_state->wm.g4x.raw[G4X_WM_LEVEL_HPLL].fbc);
1316 }
1317
1318 return dirty;
1319}
1320
1321static bool g4x_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1322 enum plane_id plane_id, int level)
1323{
1324 const struct g4x_pipe_wm *raw = &crtc_state->wm.g4x.raw[level];
1325
1326 return raw->plane[plane_id] <= g4x_plane_fifo_size(plane_id, level);
1327}
1328
1329static bool g4x_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state,
1330 int level)
1331{
1332 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1333
1334 if (level > dev_priv->wm.max_level)
1335 return false;
1336
1337 return g4x_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1338 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1339 g4x_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1340}
1341
1342/* mark all levels starting from 'level' as invalid */
1343static void g4x_invalidate_wms(struct intel_crtc *crtc,
1344 struct g4x_wm_state *wm_state, int level)
1345{
1346 if (level <= G4X_WM_LEVEL_NORMAL) {
1347 enum plane_id plane_id;
1348
1349 for_each_plane_id_on_crtc(crtc, plane_id)
1350 wm_state->wm.plane[plane_id] = USHRT_MAX;
1351 }
1352
1353 if (level <= G4X_WM_LEVEL_SR) {
1354 wm_state->cxsr = false;
1355 wm_state->sr.cursor = USHRT_MAX;
1356 wm_state->sr.plane = USHRT_MAX;
1357 wm_state->sr.fbc = USHRT_MAX;
1358 }
1359
1360 if (level <= G4X_WM_LEVEL_HPLL) {
1361 wm_state->hpll_en = false;
1362 wm_state->hpll.cursor = USHRT_MAX;
1363 wm_state->hpll.plane = USHRT_MAX;
1364 wm_state->hpll.fbc = USHRT_MAX;
1365 }
1366}
1367
1368static bool g4x_compute_fbc_en(const struct g4x_wm_state *wm_state,
1369 int level)
1370{
1371 if (level < G4X_WM_LEVEL_SR)
1372 return false;
1373
1374 if (level >= G4X_WM_LEVEL_SR &&
1375 wm_state->sr.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_SR))
1376 return false;
1377
1378 if (level >= G4X_WM_LEVEL_HPLL &&
1379 wm_state->hpll.fbc > g4x_fbc_fifo_size(G4X_WM_LEVEL_HPLL))
1380 return false;
1381
1382 return true;
1383}
1384
1385static int g4x_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1386{
1387 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1388 struct intel_atomic_state *state =
1389 to_intel_atomic_state(crtc_state->uapi.state);
1390 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
1391 int num_active_planes = hweight8(crtc_state->active_planes &
1392 ~BIT(PLANE_CURSOR));
1393 const struct g4x_pipe_wm *raw;
1394 const struct intel_plane_state *old_plane_state;
1395 const struct intel_plane_state *new_plane_state;
1396 struct intel_plane *plane;
1397 enum plane_id plane_id;
1398 int i, level;
1399 unsigned int dirty = 0;
1400
1401 for_each_oldnew_intel_plane_in_state(state, plane,
1402 old_plane_state,
1403 new_plane_state, i) {
1404 if (new_plane_state->hw.crtc != &crtc->base &&
1405 old_plane_state->hw.crtc != &crtc->base)
1406 continue;
1407
1408 if (g4x_raw_plane_wm_compute(crtc_state, new_plane_state))
1409 dirty |= BIT(plane->id);
1410 }
1411
1412 if (!dirty)
1413 return 0;
1414
1415 level = G4X_WM_LEVEL_NORMAL;
1416 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1417 goto out;
1418
1419 raw = &crtc_state->wm.g4x.raw[level];
1420 for_each_plane_id_on_crtc(crtc, plane_id)
1421 wm_state->wm.plane[plane_id] = raw->plane[plane_id];
1422
1423 level = G4X_WM_LEVEL_SR;
1424 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1425 goto out;
1426
1427 raw = &crtc_state->wm.g4x.raw[level];
1428 wm_state->sr.plane = raw->plane[PLANE_PRIMARY];
1429 wm_state->sr.cursor = raw->plane[PLANE_CURSOR];
1430 wm_state->sr.fbc = raw->fbc;
1431
1432 wm_state->cxsr = num_active_planes == BIT(PLANE_PRIMARY);
1433
1434 level = G4X_WM_LEVEL_HPLL;
1435 if (!g4x_raw_crtc_wm_is_valid(crtc_state, level))
1436 goto out;
1437
1438 raw = &crtc_state->wm.g4x.raw[level];
1439 wm_state->hpll.plane = raw->plane[PLANE_PRIMARY];
1440 wm_state->hpll.cursor = raw->plane[PLANE_CURSOR];
1441 wm_state->hpll.fbc = raw->fbc;
1442
1443 wm_state->hpll_en = wm_state->cxsr;
1444
1445 level++;
1446
1447 out:
1448 if (level == G4X_WM_LEVEL_NORMAL)
1449 return -EINVAL;
1450
1451 /* invalidate the higher levels */
1452 g4x_invalidate_wms(crtc, wm_state, level);
1453
1454 /*
1455 * Determine if the FBC watermark(s) can be used. IF
1456 * this isn't the case we prefer to disable the FBC
1457 * watermark(s) rather than disable the SR/HPLL
1458 * level(s) entirely. 'level-1' is the highest valid
1459 * level here.
1460 */
1461 wm_state->fbc_en = g4x_compute_fbc_en(wm_state, level - 1);
1462
1463 return 0;
1464}
1465
1466static int g4x_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
1467{
1468 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
1469 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1470 struct g4x_wm_state *intermediate = &new_crtc_state->wm.g4x.intermediate;
1471 const struct g4x_wm_state *optimal = &new_crtc_state->wm.g4x.optimal;
1472 struct intel_atomic_state *intel_state =
1473 to_intel_atomic_state(new_crtc_state->uapi.state);
1474 const struct intel_crtc_state *old_crtc_state =
1475 intel_atomic_get_old_crtc_state(intel_state, crtc);
1476 const struct g4x_wm_state *active = &old_crtc_state->wm.g4x.optimal;
1477 enum plane_id plane_id;
1478
1479 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
1480 *intermediate = *optimal;
1481
1482 intermediate->cxsr = false;
1483 intermediate->hpll_en = false;
1484 goto out;
1485 }
1486
1487 intermediate->cxsr = optimal->cxsr && active->cxsr &&
1488 !new_crtc_state->disable_cxsr;
1489 intermediate->hpll_en = optimal->hpll_en && active->hpll_en &&
1490 !new_crtc_state->disable_cxsr;
1491 intermediate->fbc_en = optimal->fbc_en && active->fbc_en;
1492
1493 for_each_plane_id_on_crtc(crtc, plane_id) {
1494 intermediate->wm.plane[plane_id] =
1495 max(optimal->wm.plane[plane_id],
1496 active->wm.plane[plane_id]);
1497
1498 drm_WARN_ON(&dev_priv->drm, intermediate->wm.plane[plane_id] >
1499 g4x_plane_fifo_size(plane_id, G4X_WM_LEVEL_NORMAL));
1500 }
1501
1502 intermediate->sr.plane = max(optimal->sr.plane,
1503 active->sr.plane);
1504 intermediate->sr.cursor = max(optimal->sr.cursor,
1505 active->sr.cursor);
1506 intermediate->sr.fbc = max(optimal->sr.fbc,
1507 active->sr.fbc);
1508
1509 intermediate->hpll.plane = max(optimal->hpll.plane,
1510 active->hpll.plane);
1511 intermediate->hpll.cursor = max(optimal->hpll.cursor,
1512 active->hpll.cursor);
1513 intermediate->hpll.fbc = max(optimal->hpll.fbc,
1514 active->hpll.fbc);
1515
1516 drm_WARN_ON(&dev_priv->drm,
1517 (intermediate->sr.plane >
1518 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_SR) ||
1519 intermediate->sr.cursor >
1520 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_SR)) &&
1521 intermediate->cxsr);
1522 drm_WARN_ON(&dev_priv->drm,
1523 (intermediate->sr.plane >
1524 g4x_plane_fifo_size(PLANE_PRIMARY, G4X_WM_LEVEL_HPLL) ||
1525 intermediate->sr.cursor >
1526 g4x_plane_fifo_size(PLANE_CURSOR, G4X_WM_LEVEL_HPLL)) &&
1527 intermediate->hpll_en);
1528
1529 drm_WARN_ON(&dev_priv->drm,
1530 intermediate->sr.fbc > g4x_fbc_fifo_size(1) &&
1531 intermediate->fbc_en && intermediate->cxsr);
1532 drm_WARN_ON(&dev_priv->drm,
1533 intermediate->hpll.fbc > g4x_fbc_fifo_size(2) &&
1534 intermediate->fbc_en && intermediate->hpll_en);
1535
1536out:
1537 /*
1538 * If our intermediate WM are identical to the final WM, then we can
1539 * omit the post-vblank programming; only update if it's different.
1540 */
1541 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
1542 new_crtc_state->wm.need_postvbl_update = true;
1543
1544 return 0;
1545}
1546
1547static void g4x_merge_wm(struct drm_i915_private *dev_priv,
1548 struct g4x_wm_values *wm)
1549{
1550 struct intel_crtc *crtc;
1551 int num_active_pipes = 0;
1552
1553 wm->cxsr = true;
1554 wm->hpll_en = true;
1555 wm->fbc_en = true;
1556
1557 for_each_intel_crtc(&dev_priv->drm, crtc) {
1558 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1559
1560 if (!crtc->active)
1561 continue;
1562
1563 if (!wm_state->cxsr)
1564 wm->cxsr = false;
1565 if (!wm_state->hpll_en)
1566 wm->hpll_en = false;
1567 if (!wm_state->fbc_en)
1568 wm->fbc_en = false;
1569
1570 num_active_pipes++;
1571 }
1572
1573 if (num_active_pipes != 1) {
1574 wm->cxsr = false;
1575 wm->hpll_en = false;
1576 wm->fbc_en = false;
1577 }
1578
1579 for_each_intel_crtc(&dev_priv->drm, crtc) {
1580 const struct g4x_wm_state *wm_state = &crtc->wm.active.g4x;
1581 enum pipe pipe = crtc->pipe;
1582
1583 wm->pipe[pipe] = wm_state->wm;
1584 if (crtc->active && wm->cxsr)
1585 wm->sr = wm_state->sr;
1586 if (crtc->active && wm->hpll_en)
1587 wm->hpll = wm_state->hpll;
1588 }
1589}
1590
1591static void g4x_program_watermarks(struct drm_i915_private *dev_priv)
1592{
1593 struct g4x_wm_values *old_wm = &dev_priv->wm.g4x;
1594 struct g4x_wm_values new_wm = {};
1595
1596 g4x_merge_wm(dev_priv, &new_wm);
1597
1598 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
1599 return;
1600
1601 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
1602 _intel_set_memory_cxsr(dev_priv, false);
1603
1604 g4x_write_wm_values(dev_priv, &new_wm);
1605
1606 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
1607 _intel_set_memory_cxsr(dev_priv, true);
1608
1609 *old_wm = new_wm;
1610}
1611
1612static void g4x_initial_watermarks(struct intel_atomic_state *state,
1613 struct intel_crtc *crtc)
1614{
1615 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1616 const struct intel_crtc_state *crtc_state =
1617 intel_atomic_get_new_crtc_state(state, crtc);
1618
1619 mutex_lock(&dev_priv->wm.wm_mutex);
1620 crtc->wm.active.g4x = crtc_state->wm.g4x.intermediate;
1621 g4x_program_watermarks(dev_priv);
1622 mutex_unlock(&dev_priv->wm.wm_mutex);
1623}
1624
1625static void g4x_optimize_watermarks(struct intel_atomic_state *state,
1626 struct intel_crtc *crtc)
1627{
1628 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1629 const struct intel_crtc_state *crtc_state =
1630 intel_atomic_get_new_crtc_state(state, crtc);
1631
1632 if (!crtc_state->wm.need_postvbl_update)
1633 return;
1634
1635 mutex_lock(&dev_priv->wm.wm_mutex);
1636 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
1637 g4x_program_watermarks(dev_priv);
1638 mutex_unlock(&dev_priv->wm.wm_mutex);
1639}
1640
1641/* latency must be in 0.1us units. */
1642static unsigned int vlv_wm_method2(unsigned int pixel_rate,
1643 unsigned int htotal,
1644 unsigned int width,
1645 unsigned int cpp,
1646 unsigned int latency)
1647{
1648 unsigned int ret;
1649
1650 ret = intel_wm_method2(pixel_rate, htotal,
1651 width, cpp, latency);
1652 ret = DIV_ROUND_UP(ret, 64);
1653
1654 return ret;
1655}
1656
1657static void vlv_setup_wm_latency(struct drm_i915_private *dev_priv)
1658{
1659 /* all latencies in usec */
1660 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
1661
1662 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
1663
1664 if (IS_CHERRYVIEW(dev_priv)) {
1665 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
1666 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
1667
1668 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
1669 }
1670}
1671
1672static u16 vlv_compute_wm_level(const struct intel_crtc_state *crtc_state,
1673 const struct intel_plane_state *plane_state,
1674 int level)
1675{
1676 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1677 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1678 const struct drm_display_mode *pipe_mode =
1679 &crtc_state->hw.pipe_mode;
1680 unsigned int clock, htotal, cpp, width, wm;
1681
1682 if (dev_priv->wm.pri_latency[level] == 0)
1683 return USHRT_MAX;
1684
1685 if (!intel_wm_plane_visible(crtc_state, plane_state))
1686 return 0;
1687
1688 cpp = plane_state->hw.fb->format->cpp[0];
1689 clock = pipe_mode->crtc_clock;
1690 htotal = pipe_mode->crtc_htotal;
1691 width = crtc_state->pipe_src_w;
1692
1693 if (plane->id == PLANE_CURSOR) {
1694 /*
1695 * FIXME the formula gives values that are
1696 * too big for the cursor FIFO, and hence we
1697 * would never be able to use cursors. For
1698 * now just hardcode the watermark.
1699 */
1700 wm = 63;
1701 } else {
1702 wm = vlv_wm_method2(clock, htotal, width, cpp,
1703 dev_priv->wm.pri_latency[level] * 10);
1704 }
1705
1706 return min_t(unsigned int, wm, USHRT_MAX);
1707}
1708
1709static bool vlv_need_sprite0_fifo_workaround(unsigned int active_planes)
1710{
1711 return (active_planes & (BIT(PLANE_SPRITE0) |
1712 BIT(PLANE_SPRITE1))) == BIT(PLANE_SPRITE1);
1713}
1714
1715static int vlv_compute_fifo(struct intel_crtc_state *crtc_state)
1716{
1717 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1718 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1719 const struct g4x_pipe_wm *raw =
1720 &crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2];
1721 struct vlv_fifo_state *fifo_state = &crtc_state->wm.vlv.fifo_state;
1722 unsigned int active_planes = crtc_state->active_planes & ~BIT(PLANE_CURSOR);
1723 int num_active_planes = hweight8(active_planes);
1724 const int fifo_size = 511;
1725 int fifo_extra, fifo_left = fifo_size;
1726 int sprite0_fifo_extra = 0;
1727 unsigned int total_rate;
1728 enum plane_id plane_id;
1729
1730 /*
1731 * When enabling sprite0 after sprite1 has already been enabled
1732 * we tend to get an underrun unless sprite0 already has some
1733 * FIFO space allcoated. Hence we always allocate at least one
1734 * cacheline for sprite0 whenever sprite1 is enabled.
1735 *
1736 * All other plane enable sequences appear immune to this problem.
1737 */
1738 if (vlv_need_sprite0_fifo_workaround(active_planes))
1739 sprite0_fifo_extra = 1;
1740
1741 total_rate = raw->plane[PLANE_PRIMARY] +
1742 raw->plane[PLANE_SPRITE0] +
1743 raw->plane[PLANE_SPRITE1] +
1744 sprite0_fifo_extra;
1745
1746 if (total_rate > fifo_size)
1747 return -EINVAL;
1748
1749 if (total_rate == 0)
1750 total_rate = 1;
1751
1752 for_each_plane_id_on_crtc(crtc, plane_id) {
1753 unsigned int rate;
1754
1755 if ((active_planes & BIT(plane_id)) == 0) {
1756 fifo_state->plane[plane_id] = 0;
1757 continue;
1758 }
1759
1760 rate = raw->plane[plane_id];
1761 fifo_state->plane[plane_id] = fifo_size * rate / total_rate;
1762 fifo_left -= fifo_state->plane[plane_id];
1763 }
1764
1765 fifo_state->plane[PLANE_SPRITE0] += sprite0_fifo_extra;
1766 fifo_left -= sprite0_fifo_extra;
1767
1768 fifo_state->plane[PLANE_CURSOR] = 63;
1769
1770 fifo_extra = DIV_ROUND_UP(fifo_left, num_active_planes ?: 1);
1771
1772 /* spread the remainder evenly */
1773 for_each_plane_id_on_crtc(crtc, plane_id) {
1774 int plane_extra;
1775
1776 if (fifo_left == 0)
1777 break;
1778
1779 if ((active_planes & BIT(plane_id)) == 0)
1780 continue;
1781
1782 plane_extra = min(fifo_extra, fifo_left);
1783 fifo_state->plane[plane_id] += plane_extra;
1784 fifo_left -= plane_extra;
1785 }
1786
1787 drm_WARN_ON(&dev_priv->drm, active_planes != 0 && fifo_left != 0);
1788
1789 /* give it all to the first plane if none are active */
1790 if (active_planes == 0) {
1791 drm_WARN_ON(&dev_priv->drm, fifo_left != fifo_size);
1792 fifo_state->plane[PLANE_PRIMARY] = fifo_left;
1793 }
1794
1795 return 0;
1796}
1797
1798/* mark all levels starting from 'level' as invalid */
1799static void vlv_invalidate_wms(struct intel_crtc *crtc,
1800 struct vlv_wm_state *wm_state, int level)
1801{
1802 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1803
1804 for (; level < intel_wm_num_levels(dev_priv); level++) {
1805 enum plane_id plane_id;
1806
1807 for_each_plane_id_on_crtc(crtc, plane_id)
1808 wm_state->wm[level].plane[plane_id] = USHRT_MAX;
1809
1810 wm_state->sr[level].cursor = USHRT_MAX;
1811 wm_state->sr[level].plane = USHRT_MAX;
1812 }
1813}
1814
1815static u16 vlv_invert_wm_value(u16 wm, u16 fifo_size)
1816{
1817 if (wm > fifo_size)
1818 return USHRT_MAX;
1819 else
1820 return fifo_size - wm;
1821}
1822
1823/*
1824 * Starting from 'level' set all higher
1825 * levels to 'value' in the "raw" watermarks.
1826 */
1827static bool vlv_raw_plane_wm_set(struct intel_crtc_state *crtc_state,
1828 int level, enum plane_id plane_id, u16 value)
1829{
1830 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1831 int num_levels = intel_wm_num_levels(dev_priv);
1832 bool dirty = false;
1833
1834 for (; level < num_levels; level++) {
1835 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1836
1837 dirty |= raw->plane[plane_id] != value;
1838 raw->plane[plane_id] = value;
1839 }
1840
1841 return dirty;
1842}
1843
1844static bool vlv_raw_plane_wm_compute(struct intel_crtc_state *crtc_state,
1845 const struct intel_plane_state *plane_state)
1846{
1847 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1848 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1849 enum plane_id plane_id = plane->id;
1850 int num_levels = intel_wm_num_levels(to_i915(plane->base.dev));
1851 int level;
1852 bool dirty = false;
1853
1854 if (!intel_wm_plane_visible(crtc_state, plane_state)) {
1855 dirty |= vlv_raw_plane_wm_set(crtc_state, 0, plane_id, 0);
1856 goto out;
1857 }
1858
1859 for (level = 0; level < num_levels; level++) {
1860 struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1861 int wm = vlv_compute_wm_level(crtc_state, plane_state, level);
1862 int max_wm = plane_id == PLANE_CURSOR ? 63 : 511;
1863
1864 if (wm > max_wm)
1865 break;
1866
1867 dirty |= raw->plane[plane_id] != wm;
1868 raw->plane[plane_id] = wm;
1869 }
1870
1871 /* mark all higher levels as invalid */
1872 dirty |= vlv_raw_plane_wm_set(crtc_state, level, plane_id, USHRT_MAX);
1873
1874out:
1875 if (dirty)
1876 drm_dbg_kms(&dev_priv->drm,
1877 "%s watermarks: PM2=%d, PM5=%d, DDR DVFS=%d\n",
1878 plane->base.name,
1879 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM2].plane[plane_id],
1880 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_PM5].plane[plane_id],
1881 crtc_state->wm.vlv.raw[VLV_WM_LEVEL_DDR_DVFS].plane[plane_id]);
1882
1883 return dirty;
1884}
1885
1886static bool vlv_raw_plane_wm_is_valid(const struct intel_crtc_state *crtc_state,
1887 enum plane_id plane_id, int level)
1888{
1889 const struct g4x_pipe_wm *raw =
1890 &crtc_state->wm.vlv.raw[level];
1891 const struct vlv_fifo_state *fifo_state =
1892 &crtc_state->wm.vlv.fifo_state;
1893
1894 return raw->plane[plane_id] <= fifo_state->plane[plane_id];
1895}
1896
1897static bool vlv_raw_crtc_wm_is_valid(const struct intel_crtc_state *crtc_state, int level)
1898{
1899 return vlv_raw_plane_wm_is_valid(crtc_state, PLANE_PRIMARY, level) &&
1900 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE0, level) &&
1901 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_SPRITE1, level) &&
1902 vlv_raw_plane_wm_is_valid(crtc_state, PLANE_CURSOR, level);
1903}
1904
1905static int vlv_compute_pipe_wm(struct intel_crtc_state *crtc_state)
1906{
1907 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1908 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1909 struct intel_atomic_state *state =
1910 to_intel_atomic_state(crtc_state->uapi.state);
1911 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
1912 const struct vlv_fifo_state *fifo_state =
1913 &crtc_state->wm.vlv.fifo_state;
1914 int num_active_planes = hweight8(crtc_state->active_planes &
1915 ~BIT(PLANE_CURSOR));
1916 bool needs_modeset = drm_atomic_crtc_needs_modeset(&crtc_state->uapi);
1917 const struct intel_plane_state *old_plane_state;
1918 const struct intel_plane_state *new_plane_state;
1919 struct intel_plane *plane;
1920 enum plane_id plane_id;
1921 int level, ret, i;
1922 unsigned int dirty = 0;
1923
1924 for_each_oldnew_intel_plane_in_state(state, plane,
1925 old_plane_state,
1926 new_plane_state, i) {
1927 if (new_plane_state->hw.crtc != &crtc->base &&
1928 old_plane_state->hw.crtc != &crtc->base)
1929 continue;
1930
1931 if (vlv_raw_plane_wm_compute(crtc_state, new_plane_state))
1932 dirty |= BIT(plane->id);
1933 }
1934
1935 /*
1936 * DSPARB registers may have been reset due to the
1937 * power well being turned off. Make sure we restore
1938 * them to a consistent state even if no primary/sprite
1939 * planes are initially active.
1940 */
1941 if (needs_modeset)
1942 crtc_state->fifo_changed = true;
1943
1944 if (!dirty)
1945 return 0;
1946
1947 /* cursor changes don't warrant a FIFO recompute */
1948 if (dirty & ~BIT(PLANE_CURSOR)) {
1949 const struct intel_crtc_state *old_crtc_state =
1950 intel_atomic_get_old_crtc_state(state, crtc);
1951 const struct vlv_fifo_state *old_fifo_state =
1952 &old_crtc_state->wm.vlv.fifo_state;
1953
1954 ret = vlv_compute_fifo(crtc_state);
1955 if (ret)
1956 return ret;
1957
1958 if (needs_modeset ||
1959 memcmp(old_fifo_state, fifo_state,
1960 sizeof(*fifo_state)) != 0)
1961 crtc_state->fifo_changed = true;
1962 }
1963
1964 /* initially allow all levels */
1965 wm_state->num_levels = intel_wm_num_levels(dev_priv);
1966 /*
1967 * Note that enabling cxsr with no primary/sprite planes
1968 * enabled can wedge the pipe. Hence we only allow cxsr
1969 * with exactly one enabled primary/sprite plane.
1970 */
1971 wm_state->cxsr = crtc->pipe != PIPE_C && num_active_planes == 1;
1972
1973 for (level = 0; level < wm_state->num_levels; level++) {
1974 const struct g4x_pipe_wm *raw = &crtc_state->wm.vlv.raw[level];
1975 const int sr_fifo_size = INTEL_NUM_PIPES(dev_priv) * 512 - 1;
1976
1977 if (!vlv_raw_crtc_wm_is_valid(crtc_state, level))
1978 break;
1979
1980 for_each_plane_id_on_crtc(crtc, plane_id) {
1981 wm_state->wm[level].plane[plane_id] =
1982 vlv_invert_wm_value(raw->plane[plane_id],
1983 fifo_state->plane[plane_id]);
1984 }
1985
1986 wm_state->sr[level].plane =
1987 vlv_invert_wm_value(max3(raw->plane[PLANE_PRIMARY],
1988 raw->plane[PLANE_SPRITE0],
1989 raw->plane[PLANE_SPRITE1]),
1990 sr_fifo_size);
1991
1992 wm_state->sr[level].cursor =
1993 vlv_invert_wm_value(raw->plane[PLANE_CURSOR],
1994 63);
1995 }
1996
1997 if (level == 0)
1998 return -EINVAL;
1999
2000 /* limit to only levels we can actually handle */
2001 wm_state->num_levels = level;
2002
2003 /* invalidate the higher levels */
2004 vlv_invalidate_wms(crtc, wm_state, level);
2005
2006 return 0;
2007}
2008
2009#define VLV_FIFO(plane, value) \
2010 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
2011
2012static void vlv_atomic_update_fifo(struct intel_atomic_state *state,
2013 struct intel_crtc *crtc)
2014{
2015 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2016 struct intel_uncore *uncore = &dev_priv->uncore;
2017 const struct intel_crtc_state *crtc_state =
2018 intel_atomic_get_new_crtc_state(state, crtc);
2019 const struct vlv_fifo_state *fifo_state =
2020 &crtc_state->wm.vlv.fifo_state;
2021 int sprite0_start, sprite1_start, fifo_size;
2022 u32 dsparb, dsparb2, dsparb3;
2023
2024 if (!crtc_state->fifo_changed)
2025 return;
2026
2027 sprite0_start = fifo_state->plane[PLANE_PRIMARY];
2028 sprite1_start = fifo_state->plane[PLANE_SPRITE0] + sprite0_start;
2029 fifo_size = fifo_state->plane[PLANE_SPRITE1] + sprite1_start;
2030
2031 drm_WARN_ON(&dev_priv->drm, fifo_state->plane[PLANE_CURSOR] != 63);
2032 drm_WARN_ON(&dev_priv->drm, fifo_size != 511);
2033
2034 trace_vlv_fifo_size(crtc, sprite0_start, sprite1_start, fifo_size);
2035
2036 /*
2037 * uncore.lock serves a double purpose here. It allows us to
2038 * use the less expensive I915_{READ,WRITE}_FW() functions, and
2039 * it protects the DSPARB registers from getting clobbered by
2040 * parallel updates from multiple pipes.
2041 *
2042 * intel_pipe_update_start() has already disabled interrupts
2043 * for us, so a plain spin_lock() is sufficient here.
2044 */
2045 spin_lock(&uncore->lock);
2046
2047 switch (crtc->pipe) {
2048 case PIPE_A:
2049 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2050 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2051
2052 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
2053 VLV_FIFO(SPRITEB, 0xff));
2054 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
2055 VLV_FIFO(SPRITEB, sprite1_start));
2056
2057 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
2058 VLV_FIFO(SPRITEB_HI, 0x1));
2059 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
2060 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
2061
2062 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2063 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2064 break;
2065 case PIPE_B:
2066 dsparb = intel_uncore_read_fw(uncore, DSPARB);
2067 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2068
2069 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
2070 VLV_FIFO(SPRITED, 0xff));
2071 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
2072 VLV_FIFO(SPRITED, sprite1_start));
2073
2074 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
2075 VLV_FIFO(SPRITED_HI, 0xff));
2076 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
2077 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
2078
2079 intel_uncore_write_fw(uncore, DSPARB, dsparb);
2080 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2081 break;
2082 case PIPE_C:
2083 dsparb3 = intel_uncore_read_fw(uncore, DSPARB3);
2084 dsparb2 = intel_uncore_read_fw(uncore, DSPARB2);
2085
2086 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
2087 VLV_FIFO(SPRITEF, 0xff));
2088 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
2089 VLV_FIFO(SPRITEF, sprite1_start));
2090
2091 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
2092 VLV_FIFO(SPRITEF_HI, 0xff));
2093 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
2094 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
2095
2096 intel_uncore_write_fw(uncore, DSPARB3, dsparb3);
2097 intel_uncore_write_fw(uncore, DSPARB2, dsparb2);
2098 break;
2099 default:
2100 break;
2101 }
2102
2103 intel_uncore_posting_read_fw(uncore, DSPARB);
2104
2105 spin_unlock(&uncore->lock);
2106}
2107
2108#undef VLV_FIFO
2109
2110static int vlv_compute_intermediate_wm(struct intel_crtc_state *new_crtc_state)
2111{
2112 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
2113 struct vlv_wm_state *intermediate = &new_crtc_state->wm.vlv.intermediate;
2114 const struct vlv_wm_state *optimal = &new_crtc_state->wm.vlv.optimal;
2115 struct intel_atomic_state *intel_state =
2116 to_intel_atomic_state(new_crtc_state->uapi.state);
2117 const struct intel_crtc_state *old_crtc_state =
2118 intel_atomic_get_old_crtc_state(intel_state, crtc);
2119 const struct vlv_wm_state *active = &old_crtc_state->wm.vlv.optimal;
2120 int level;
2121
2122 if (!new_crtc_state->hw.active || drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi)) {
2123 *intermediate = *optimal;
2124
2125 intermediate->cxsr = false;
2126 goto out;
2127 }
2128
2129 intermediate->num_levels = min(optimal->num_levels, active->num_levels);
2130 intermediate->cxsr = optimal->cxsr && active->cxsr &&
2131 !new_crtc_state->disable_cxsr;
2132
2133 for (level = 0; level < intermediate->num_levels; level++) {
2134 enum plane_id plane_id;
2135
2136 for_each_plane_id_on_crtc(crtc, plane_id) {
2137 intermediate->wm[level].plane[plane_id] =
2138 min(optimal->wm[level].plane[plane_id],
2139 active->wm[level].plane[plane_id]);
2140 }
2141
2142 intermediate->sr[level].plane = min(optimal->sr[level].plane,
2143 active->sr[level].plane);
2144 intermediate->sr[level].cursor = min(optimal->sr[level].cursor,
2145 active->sr[level].cursor);
2146 }
2147
2148 vlv_invalidate_wms(crtc, intermediate, level);
2149
2150out:
2151 /*
2152 * If our intermediate WM are identical to the final WM, then we can
2153 * omit the post-vblank programming; only update if it's different.
2154 */
2155 if (memcmp(intermediate, optimal, sizeof(*intermediate)) != 0)
2156 new_crtc_state->wm.need_postvbl_update = true;
2157
2158 return 0;
2159}
2160
2161static void vlv_merge_wm(struct drm_i915_private *dev_priv,
2162 struct vlv_wm_values *wm)
2163{
2164 struct intel_crtc *crtc;
2165 int num_active_pipes = 0;
2166
2167 wm->level = dev_priv->wm.max_level;
2168 wm->cxsr = true;
2169
2170 for_each_intel_crtc(&dev_priv->drm, crtc) {
2171 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
2172
2173 if (!crtc->active)
2174 continue;
2175
2176 if (!wm_state->cxsr)
2177 wm->cxsr = false;
2178
2179 num_active_pipes++;
2180 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
2181 }
2182
2183 if (num_active_pipes != 1)
2184 wm->cxsr = false;
2185
2186 if (num_active_pipes > 1)
2187 wm->level = VLV_WM_LEVEL_PM2;
2188
2189 for_each_intel_crtc(&dev_priv->drm, crtc) {
2190 const struct vlv_wm_state *wm_state = &crtc->wm.active.vlv;
2191 enum pipe pipe = crtc->pipe;
2192
2193 wm->pipe[pipe] = wm_state->wm[wm->level];
2194 if (crtc->active && wm->cxsr)
2195 wm->sr = wm_state->sr[wm->level];
2196
2197 wm->ddl[pipe].plane[PLANE_PRIMARY] = DDL_PRECISION_HIGH | 2;
2198 wm->ddl[pipe].plane[PLANE_SPRITE0] = DDL_PRECISION_HIGH | 2;
2199 wm->ddl[pipe].plane[PLANE_SPRITE1] = DDL_PRECISION_HIGH | 2;
2200 wm->ddl[pipe].plane[PLANE_CURSOR] = DDL_PRECISION_HIGH | 2;
2201 }
2202}
2203
2204static void vlv_program_watermarks(struct drm_i915_private *dev_priv)
2205{
2206 struct vlv_wm_values *old_wm = &dev_priv->wm.vlv;
2207 struct vlv_wm_values new_wm = {};
2208
2209 vlv_merge_wm(dev_priv, &new_wm);
2210
2211 if (memcmp(old_wm, &new_wm, sizeof(new_wm)) == 0)
2212 return;
2213
2214 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
2215 chv_set_memory_dvfs(dev_priv, false);
2216
2217 if (is_disabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
2218 chv_set_memory_pm5(dev_priv, false);
2219
2220 if (is_disabling(old_wm->cxsr, new_wm.cxsr, true))
2221 _intel_set_memory_cxsr(dev_priv, false);
2222
2223 vlv_write_wm_values(dev_priv, &new_wm);
2224
2225 if (is_enabling(old_wm->cxsr, new_wm.cxsr, true))
2226 _intel_set_memory_cxsr(dev_priv, true);
2227
2228 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_PM5))
2229 chv_set_memory_pm5(dev_priv, true);
2230
2231 if (is_enabling(old_wm->level, new_wm.level, VLV_WM_LEVEL_DDR_DVFS))
2232 chv_set_memory_dvfs(dev_priv, true);
2233
2234 *old_wm = new_wm;
2235}
2236
2237static void vlv_initial_watermarks(struct intel_atomic_state *state,
2238 struct intel_crtc *crtc)
2239{
2240 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2241 const struct intel_crtc_state *crtc_state =
2242 intel_atomic_get_new_crtc_state(state, crtc);
2243
2244 mutex_lock(&dev_priv->wm.wm_mutex);
2245 crtc->wm.active.vlv = crtc_state->wm.vlv.intermediate;
2246 vlv_program_watermarks(dev_priv);
2247 mutex_unlock(&dev_priv->wm.wm_mutex);
2248}
2249
2250static void vlv_optimize_watermarks(struct intel_atomic_state *state,
2251 struct intel_crtc *crtc)
2252{
2253 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2254 const struct intel_crtc_state *crtc_state =
2255 intel_atomic_get_new_crtc_state(state, crtc);
2256
2257 if (!crtc_state->wm.need_postvbl_update)
2258 return;
2259
2260 mutex_lock(&dev_priv->wm.wm_mutex);
2261 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
2262 vlv_program_watermarks(dev_priv);
2263 mutex_unlock(&dev_priv->wm.wm_mutex);
2264}
2265
2266static void i965_update_wm(struct intel_crtc *unused_crtc)
2267{
2268 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
2269 struct intel_crtc *crtc;
2270 int srwm = 1;
2271 int cursor_sr = 16;
2272 bool cxsr_enabled;
2273
2274 /* Calc sr entries for one plane configs */
2275 crtc = single_enabled_crtc(dev_priv);
2276 if (crtc) {
2277 /* self-refresh has much higher latency */
2278 static const int sr_latency_ns = 12000;
2279 const struct drm_display_mode *pipe_mode =
2280 &crtc->config->hw.pipe_mode;
2281 const struct drm_framebuffer *fb =
2282 crtc->base.primary->state->fb;
2283 int clock = pipe_mode->crtc_clock;
2284 int htotal = pipe_mode->crtc_htotal;
2285 int hdisplay = crtc->config->pipe_src_w;
2286 int cpp = fb->format->cpp[0];
2287 int entries;
2288
2289 entries = intel_wm_method2(clock, htotal,
2290 hdisplay, cpp, sr_latency_ns / 100);
2291 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
2292 srwm = I965_FIFO_SIZE - entries;
2293 if (srwm < 0)
2294 srwm = 1;
2295 srwm &= 0x1ff;
2296 drm_dbg_kms(&dev_priv->drm,
2297 "self-refresh entries: %d, wm: %d\n",
2298 entries, srwm);
2299
2300 entries = intel_wm_method2(clock, htotal,
2301 crtc->base.cursor->state->crtc_w, 4,
2302 sr_latency_ns / 100);
2303 entries = DIV_ROUND_UP(entries,
2304 i965_cursor_wm_info.cacheline_size) +
2305 i965_cursor_wm_info.guard_size;
2306
2307 cursor_sr = i965_cursor_wm_info.fifo_size - entries;
2308 if (cursor_sr > i965_cursor_wm_info.max_wm)
2309 cursor_sr = i965_cursor_wm_info.max_wm;
2310
2311 drm_dbg_kms(&dev_priv->drm,
2312 "self-refresh watermark: display plane %d "
2313 "cursor %d\n", srwm, cursor_sr);
2314
2315 cxsr_enabled = true;
2316 } else {
2317 cxsr_enabled = false;
2318 /* Turn off self refresh if both pipes are enabled */
2319 intel_set_memory_cxsr(dev_priv, false);
2320 }
2321
2322 drm_dbg_kms(&dev_priv->drm,
2323 "Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
2324 srwm);
2325
2326 /* 965 has limitations... */
2327 intel_uncore_write(&dev_priv->uncore, DSPFW1, FW_WM(srwm, SR) |
2328 FW_WM(8, CURSORB) |
2329 FW_WM(8, PLANEB) |
2330 FW_WM(8, PLANEA));
2331 intel_uncore_write(&dev_priv->uncore, DSPFW2, FW_WM(8, CURSORA) |
2332 FW_WM(8, PLANEC_OLD));
2333 /* update cursor SR watermark */
2334 intel_uncore_write(&dev_priv->uncore, DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
2335
2336 if (cxsr_enabled)
2337 intel_set_memory_cxsr(dev_priv, true);
2338}
2339
2340#undef FW_WM
2341
2342static void i9xx_update_wm(struct intel_crtc *unused_crtc)
2343{
2344 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
2345 const struct intel_watermark_params *wm_info;
2346 u32 fwater_lo;
2347 u32 fwater_hi;
2348 int cwm, srwm = 1;
2349 int fifo_size;
2350 int planea_wm, planeb_wm;
2351 struct intel_crtc *crtc, *enabled = NULL;
2352
2353 if (IS_I945GM(dev_priv))
2354 wm_info = &i945_wm_info;
2355 else if (DISPLAY_VER(dev_priv) != 2)
2356 wm_info = &i915_wm_info;
2357 else
2358 wm_info = &i830_a_wm_info;
2359
2360 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_A);
2361 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_A);
2362 if (intel_crtc_active(crtc)) {
2363 const struct drm_display_mode *pipe_mode =
2364 &crtc->config->hw.pipe_mode;
2365 const struct drm_framebuffer *fb =
2366 crtc->base.primary->state->fb;
2367 int cpp;
2368
2369 if (DISPLAY_VER(dev_priv) == 2)
2370 cpp = 4;
2371 else
2372 cpp = fb->format->cpp[0];
2373
2374 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
2375 wm_info, fifo_size, cpp,
2376 pessimal_latency_ns);
2377 enabled = crtc;
2378 } else {
2379 planea_wm = fifo_size - wm_info->guard_size;
2380 if (planea_wm > (long)wm_info->max_wm)
2381 planea_wm = wm_info->max_wm;
2382 }
2383
2384 if (DISPLAY_VER(dev_priv) == 2)
2385 wm_info = &i830_bc_wm_info;
2386
2387 fifo_size = dev_priv->display.get_fifo_size(dev_priv, PLANE_B);
2388 crtc = intel_get_crtc_for_plane(dev_priv, PLANE_B);
2389 if (intel_crtc_active(crtc)) {
2390 const struct drm_display_mode *pipe_mode =
2391 &crtc->config->hw.pipe_mode;
2392 const struct drm_framebuffer *fb =
2393 crtc->base.primary->state->fb;
2394 int cpp;
2395
2396 if (DISPLAY_VER(dev_priv) == 2)
2397 cpp = 4;
2398 else
2399 cpp = fb->format->cpp[0];
2400
2401 planeb_wm = intel_calculate_wm(pipe_mode->crtc_clock,
2402 wm_info, fifo_size, cpp,
2403 pessimal_latency_ns);
2404 if (enabled == NULL)
2405 enabled = crtc;
2406 else
2407 enabled = NULL;
2408 } else {
2409 planeb_wm = fifo_size - wm_info->guard_size;
2410 if (planeb_wm > (long)wm_info->max_wm)
2411 planeb_wm = wm_info->max_wm;
2412 }
2413
2414 drm_dbg_kms(&dev_priv->drm,
2415 "FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
2416
2417 if (IS_I915GM(dev_priv) && enabled) {
2418 struct drm_i915_gem_object *obj;
2419
2420 obj = intel_fb_obj(enabled->base.primary->state->fb);
2421
2422 /* self-refresh seems busted with untiled */
2423 if (!i915_gem_object_is_tiled(obj))
2424 enabled = NULL;
2425 }
2426
2427 /*
2428 * Overlay gets an aggressive default since video jitter is bad.
2429 */
2430 cwm = 2;
2431
2432 /* Play safe and disable self-refresh before adjusting watermarks. */
2433 intel_set_memory_cxsr(dev_priv, false);
2434
2435 /* Calc sr entries for one plane configs */
2436 if (HAS_FW_BLC(dev_priv) && enabled) {
2437 /* self-refresh has much higher latency */
2438 static const int sr_latency_ns = 6000;
2439 const struct drm_display_mode *pipe_mode =
2440 &enabled->config->hw.pipe_mode;
2441 const struct drm_framebuffer *fb =
2442 enabled->base.primary->state->fb;
2443 int clock = pipe_mode->crtc_clock;
2444 int htotal = pipe_mode->crtc_htotal;
2445 int hdisplay = enabled->config->pipe_src_w;
2446 int cpp;
2447 int entries;
2448
2449 if (IS_I915GM(dev_priv) || IS_I945GM(dev_priv))
2450 cpp = 4;
2451 else
2452 cpp = fb->format->cpp[0];
2453
2454 entries = intel_wm_method2(clock, htotal, hdisplay, cpp,
2455 sr_latency_ns / 100);
2456 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
2457 drm_dbg_kms(&dev_priv->drm,
2458 "self-refresh entries: %d\n", entries);
2459 srwm = wm_info->fifo_size - entries;
2460 if (srwm < 0)
2461 srwm = 1;
2462
2463 if (IS_I945G(dev_priv) || IS_I945GM(dev_priv))
2464 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF,
2465 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
2466 else
2467 intel_uncore_write(&dev_priv->uncore, FW_BLC_SELF, srwm & 0x3f);
2468 }
2469
2470 drm_dbg_kms(&dev_priv->drm,
2471 "Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
2472 planea_wm, planeb_wm, cwm, srwm);
2473
2474 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
2475 fwater_hi = (cwm & 0x1f);
2476
2477 /* Set request length to 8 cachelines per fetch */
2478 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
2479 fwater_hi = fwater_hi | (1 << 8);
2480
2481 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2482 intel_uncore_write(&dev_priv->uncore, FW_BLC2, fwater_hi);
2483
2484 if (enabled)
2485 intel_set_memory_cxsr(dev_priv, true);
2486}
2487
2488static void i845_update_wm(struct intel_crtc *unused_crtc)
2489{
2490 struct drm_i915_private *dev_priv = to_i915(unused_crtc->base.dev);
2491 struct intel_crtc *crtc;
2492 const struct drm_display_mode *pipe_mode;
2493 u32 fwater_lo;
2494 int planea_wm;
2495
2496 crtc = single_enabled_crtc(dev_priv);
2497 if (crtc == NULL)
2498 return;
2499
2500 pipe_mode = &crtc->config->hw.pipe_mode;
2501 planea_wm = intel_calculate_wm(pipe_mode->crtc_clock,
2502 &i845_wm_info,
2503 dev_priv->display.get_fifo_size(dev_priv, PLANE_A),
2504 4, pessimal_latency_ns);
2505 fwater_lo = intel_uncore_read(&dev_priv->uncore, FW_BLC) & ~0xfff;
2506 fwater_lo |= (3<<8) | planea_wm;
2507
2508 drm_dbg_kms(&dev_priv->drm,
2509 "Setting FIFO watermarks - A: %d\n", planea_wm);
2510
2511 intel_uncore_write(&dev_priv->uncore, FW_BLC, fwater_lo);
2512}
2513
2514/* latency must be in 0.1us units. */
2515static unsigned int ilk_wm_method1(unsigned int pixel_rate,
2516 unsigned int cpp,
2517 unsigned int latency)
2518{
2519 unsigned int ret;
2520
2521 ret = intel_wm_method1(pixel_rate, cpp, latency);
2522 ret = DIV_ROUND_UP(ret, 64) + 2;
2523
2524 return ret;
2525}
2526
2527/* latency must be in 0.1us units. */
2528static unsigned int ilk_wm_method2(unsigned int pixel_rate,
2529 unsigned int htotal,
2530 unsigned int width,
2531 unsigned int cpp,
2532 unsigned int latency)
2533{
2534 unsigned int ret;
2535
2536 ret = intel_wm_method2(pixel_rate, htotal,
2537 width, cpp, latency);
2538 ret = DIV_ROUND_UP(ret, 64) + 2;
2539
2540 return ret;
2541}
2542
2543static u32 ilk_wm_fbc(u32 pri_val, u32 horiz_pixels, u8 cpp)
2544{
2545 /*
2546 * Neither of these should be possible since this function shouldn't be
2547 * called if the CRTC is off or the plane is invisible. But let's be
2548 * extra paranoid to avoid a potential divide-by-zero if we screw up
2549 * elsewhere in the driver.
2550 */
2551 if (WARN_ON(!cpp))
2552 return 0;
2553 if (WARN_ON(!horiz_pixels))
2554 return 0;
2555
2556 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
2557}
2558
2559struct ilk_wm_maximums {
2560 u16 pri;
2561 u16 spr;
2562 u16 cur;
2563 u16 fbc;
2564};
2565
2566/*
2567 * For both WM_PIPE and WM_LP.
2568 * mem_value must be in 0.1us units.
2569 */
2570static u32 ilk_compute_pri_wm(const struct intel_crtc_state *crtc_state,
2571 const struct intel_plane_state *plane_state,
2572 u32 mem_value, bool is_lp)
2573{
2574 u32 method1, method2;
2575 int cpp;
2576
2577 if (mem_value == 0)
2578 return U32_MAX;
2579
2580 if (!intel_wm_plane_visible(crtc_state, plane_state))
2581 return 0;
2582
2583 cpp = plane_state->hw.fb->format->cpp[0];
2584
2585 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2586
2587 if (!is_lp)
2588 return method1;
2589
2590 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2591 crtc_state->hw.pipe_mode.crtc_htotal,
2592 drm_rect_width(&plane_state->uapi.dst),
2593 cpp, mem_value);
2594
2595 return min(method1, method2);
2596}
2597
2598/*
2599 * For both WM_PIPE and WM_LP.
2600 * mem_value must be in 0.1us units.
2601 */
2602static u32 ilk_compute_spr_wm(const struct intel_crtc_state *crtc_state,
2603 const struct intel_plane_state *plane_state,
2604 u32 mem_value)
2605{
2606 u32 method1, method2;
2607 int cpp;
2608
2609 if (mem_value == 0)
2610 return U32_MAX;
2611
2612 if (!intel_wm_plane_visible(crtc_state, plane_state))
2613 return 0;
2614
2615 cpp = plane_state->hw.fb->format->cpp[0];
2616
2617 method1 = ilk_wm_method1(crtc_state->pixel_rate, cpp, mem_value);
2618 method2 = ilk_wm_method2(crtc_state->pixel_rate,
2619 crtc_state->hw.pipe_mode.crtc_htotal,
2620 drm_rect_width(&plane_state->uapi.dst),
2621 cpp, mem_value);
2622 return min(method1, method2);
2623}
2624
2625/*
2626 * For both WM_PIPE and WM_LP.
2627 * mem_value must be in 0.1us units.
2628 */
2629static u32 ilk_compute_cur_wm(const struct intel_crtc_state *crtc_state,
2630 const struct intel_plane_state *plane_state,
2631 u32 mem_value)
2632{
2633 int cpp;
2634
2635 if (mem_value == 0)
2636 return U32_MAX;
2637
2638 if (!intel_wm_plane_visible(crtc_state, plane_state))
2639 return 0;
2640
2641 cpp = plane_state->hw.fb->format->cpp[0];
2642
2643 return ilk_wm_method2(crtc_state->pixel_rate,
2644 crtc_state->hw.pipe_mode.crtc_htotal,
2645 drm_rect_width(&plane_state->uapi.dst),
2646 cpp, mem_value);
2647}
2648
2649/* Only for WM_LP. */
2650static u32 ilk_compute_fbc_wm(const struct intel_crtc_state *crtc_state,
2651 const struct intel_plane_state *plane_state,
2652 u32 pri_val)
2653{
2654 int cpp;
2655
2656 if (!intel_wm_plane_visible(crtc_state, plane_state))
2657 return 0;
2658
2659 cpp = plane_state->hw.fb->format->cpp[0];
2660
2661 return ilk_wm_fbc(pri_val, drm_rect_width(&plane_state->uapi.dst),
2662 cpp);
2663}
2664
2665static unsigned int
2666ilk_display_fifo_size(const struct drm_i915_private *dev_priv)
2667{
2668 if (DISPLAY_VER(dev_priv) >= 8)
2669 return 3072;
2670 else if (DISPLAY_VER(dev_priv) >= 7)
2671 return 768;
2672 else
2673 return 512;
2674}
2675
2676static unsigned int
2677ilk_plane_wm_reg_max(const struct drm_i915_private *dev_priv,
2678 int level, bool is_sprite)
2679{
2680 if (DISPLAY_VER(dev_priv) >= 8)
2681 /* BDW primary/sprite plane watermarks */
2682 return level == 0 ? 255 : 2047;
2683 else if (DISPLAY_VER(dev_priv) >= 7)
2684 /* IVB/HSW primary/sprite plane watermarks */
2685 return level == 0 ? 127 : 1023;
2686 else if (!is_sprite)
2687 /* ILK/SNB primary plane watermarks */
2688 return level == 0 ? 127 : 511;
2689 else
2690 /* ILK/SNB sprite plane watermarks */
2691 return level == 0 ? 63 : 255;
2692}
2693
2694static unsigned int
2695ilk_cursor_wm_reg_max(const struct drm_i915_private *dev_priv, int level)
2696{
2697 if (DISPLAY_VER(dev_priv) >= 7)
2698 return level == 0 ? 63 : 255;
2699 else
2700 return level == 0 ? 31 : 63;
2701}
2702
2703static unsigned int ilk_fbc_wm_reg_max(const struct drm_i915_private *dev_priv)
2704{
2705 if (DISPLAY_VER(dev_priv) >= 8)
2706 return 31;
2707 else
2708 return 15;
2709}
2710
2711/* Calculate the maximum primary/sprite plane watermark */
2712static unsigned int ilk_plane_wm_max(const struct drm_i915_private *dev_priv,
2713 int level,
2714 const struct intel_wm_config *config,
2715 enum intel_ddb_partitioning ddb_partitioning,
2716 bool is_sprite)
2717{
2718 unsigned int fifo_size = ilk_display_fifo_size(dev_priv);
2719
2720 /* if sprites aren't enabled, sprites get nothing */
2721 if (is_sprite && !config->sprites_enabled)
2722 return 0;
2723
2724 /* HSW allows LP1+ watermarks even with multiple pipes */
2725 if (level == 0 || config->num_pipes_active > 1) {
2726 fifo_size /= INTEL_NUM_PIPES(dev_priv);
2727
2728 /*
2729 * For some reason the non self refresh
2730 * FIFO size is only half of the self
2731 * refresh FIFO size on ILK/SNB.
2732 */
2733 if (DISPLAY_VER(dev_priv) <= 6)
2734 fifo_size /= 2;
2735 }
2736
2737 if (config->sprites_enabled) {
2738 /* level 0 is always calculated with 1:1 split */
2739 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
2740 if (is_sprite)
2741 fifo_size *= 5;
2742 fifo_size /= 6;
2743 } else {
2744 fifo_size /= 2;
2745 }
2746 }
2747
2748 /* clamp to max that the registers can hold */
2749 return min(fifo_size, ilk_plane_wm_reg_max(dev_priv, level, is_sprite));
2750}
2751
2752/* Calculate the maximum cursor plane watermark */
2753static unsigned int ilk_cursor_wm_max(const struct drm_i915_private *dev_priv,
2754 int level,
2755 const struct intel_wm_config *config)
2756{
2757 /* HSW LP1+ watermarks w/ multiple pipes */
2758 if (level > 0 && config->num_pipes_active > 1)
2759 return 64;
2760
2761 /* otherwise just report max that registers can hold */
2762 return ilk_cursor_wm_reg_max(dev_priv, level);
2763}
2764
2765static void ilk_compute_wm_maximums(const struct drm_i915_private *dev_priv,
2766 int level,
2767 const struct intel_wm_config *config,
2768 enum intel_ddb_partitioning ddb_partitioning,
2769 struct ilk_wm_maximums *max)
2770{
2771 max->pri = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, false);
2772 max->spr = ilk_plane_wm_max(dev_priv, level, config, ddb_partitioning, true);
2773 max->cur = ilk_cursor_wm_max(dev_priv, level, config);
2774 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
2775}
2776
2777static void ilk_compute_wm_reg_maximums(const struct drm_i915_private *dev_priv,
2778 int level,
2779 struct ilk_wm_maximums *max)
2780{
2781 max->pri = ilk_plane_wm_reg_max(dev_priv, level, false);
2782 max->spr = ilk_plane_wm_reg_max(dev_priv, level, true);
2783 max->cur = ilk_cursor_wm_reg_max(dev_priv, level);
2784 max->fbc = ilk_fbc_wm_reg_max(dev_priv);
2785}
2786
2787static bool ilk_validate_wm_level(int level,
2788 const struct ilk_wm_maximums *max,
2789 struct intel_wm_level *result)
2790{
2791 bool ret;
2792
2793 /* already determined to be invalid? */
2794 if (!result->enable)
2795 return false;
2796
2797 result->enable = result->pri_val <= max->pri &&
2798 result->spr_val <= max->spr &&
2799 result->cur_val <= max->cur;
2800
2801 ret = result->enable;
2802
2803 /*
2804 * HACK until we can pre-compute everything,
2805 * and thus fail gracefully if LP0 watermarks
2806 * are exceeded...
2807 */
2808 if (level == 0 && !result->enable) {
2809 if (result->pri_val > max->pri)
2810 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
2811 level, result->pri_val, max->pri);
2812 if (result->spr_val > max->spr)
2813 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
2814 level, result->spr_val, max->spr);
2815 if (result->cur_val > max->cur)
2816 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
2817 level, result->cur_val, max->cur);
2818
2819 result->pri_val = min_t(u32, result->pri_val, max->pri);
2820 result->spr_val = min_t(u32, result->spr_val, max->spr);
2821 result->cur_val = min_t(u32, result->cur_val, max->cur);
2822 result->enable = true;
2823 }
2824
2825 return ret;
2826}
2827
2828static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
2829 const struct intel_crtc *crtc,
2830 int level,
2831 struct intel_crtc_state *crtc_state,
2832 const struct intel_plane_state *pristate,
2833 const struct intel_plane_state *sprstate,
2834 const struct intel_plane_state *curstate,
2835 struct intel_wm_level *result)
2836{
2837 u16 pri_latency = dev_priv->wm.pri_latency[level];
2838 u16 spr_latency = dev_priv->wm.spr_latency[level];
2839 u16 cur_latency = dev_priv->wm.cur_latency[level];
2840
2841 /* WM1+ latency values stored in 0.5us units */
2842 if (level > 0) {
2843 pri_latency *= 5;
2844 spr_latency *= 5;
2845 cur_latency *= 5;
2846 }
2847
2848 if (pristate) {
2849 result->pri_val = ilk_compute_pri_wm(crtc_state, pristate,
2850 pri_latency, level);
2851 result->fbc_val = ilk_compute_fbc_wm(crtc_state, pristate, result->pri_val);
2852 }
2853
2854 if (sprstate)
2855 result->spr_val = ilk_compute_spr_wm(crtc_state, sprstate, spr_latency);
2856
2857 if (curstate)
2858 result->cur_val = ilk_compute_cur_wm(crtc_state, curstate, cur_latency);
2859
2860 result->enable = true;
2861}
2862
2863static void intel_read_wm_latency(struct drm_i915_private *dev_priv,
2864 u16 wm[8])
2865{
2866 struct intel_uncore *uncore = &dev_priv->uncore;
2867
2868 if (DISPLAY_VER(dev_priv) >= 9) {
2869 u32 val;
2870 int ret, i;
2871 int level, max_level = ilk_wm_max_level(dev_priv);
2872
2873 /* read the first set of memory latencies[0:3] */
2874 val = 0; /* data0 to be programmed to 0 for first set */
2875 ret = sandybridge_pcode_read(dev_priv,
2876 GEN9_PCODE_READ_MEM_LATENCY,
2877 &val, NULL);
2878
2879 if (ret) {
2880 drm_err(&dev_priv->drm,
2881 "SKL Mailbox read error = %d\n", ret);
2882 return;
2883 }
2884
2885 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2886 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2887 GEN9_MEM_LATENCY_LEVEL_MASK;
2888 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2889 GEN9_MEM_LATENCY_LEVEL_MASK;
2890 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2891 GEN9_MEM_LATENCY_LEVEL_MASK;
2892
2893 /* read the second set of memory latencies[4:7] */
2894 val = 1; /* data0 to be programmed to 1 for second set */
2895 ret = sandybridge_pcode_read(dev_priv,
2896 GEN9_PCODE_READ_MEM_LATENCY,
2897 &val, NULL);
2898 if (ret) {
2899 drm_err(&dev_priv->drm,
2900 "SKL Mailbox read error = %d\n", ret);
2901 return;
2902 }
2903
2904 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2905 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2906 GEN9_MEM_LATENCY_LEVEL_MASK;
2907 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2908 GEN9_MEM_LATENCY_LEVEL_MASK;
2909 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2910 GEN9_MEM_LATENCY_LEVEL_MASK;
2911
2912 /*
2913 * If a level n (n > 1) has a 0us latency, all levels m (m >= n)
2914 * need to be disabled. We make sure to sanitize the values out
2915 * of the punit to satisfy this requirement.
2916 */
2917 for (level = 1; level <= max_level; level++) {
2918 if (wm[level] == 0) {
2919 for (i = level + 1; i <= max_level; i++)
2920 wm[i] = 0;
2921 break;
2922 }
2923 }
2924
2925 /*
2926 * WaWmMemoryReadLatency:skl+,glk
2927 *
2928 * punit doesn't take into account the read latency so we need
2929 * to add 2us to the various latency levels we retrieve from the
2930 * punit when level 0 response data us 0us.
2931 */
2932 if (wm[0] == 0) {
2933 wm[0] += 2;
2934 for (level = 1; level <= max_level; level++) {
2935 if (wm[level] == 0)
2936 break;
2937 wm[level] += 2;
2938 }
2939 }
2940
2941 /*
2942 * WA Level-0 adjustment for 16GB DIMMs: SKL+
2943 * If we could not get dimm info enable this WA to prevent from
2944 * any underrun. If not able to get Dimm info assume 16GB dimm
2945 * to avoid any underrun.
2946 */
2947 if (dev_priv->dram_info.wm_lv_0_adjust_needed)
2948 wm[0] += 1;
2949
2950 } else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
2951 u64 sskpd = intel_uncore_read64(uncore, MCH_SSKPD);
2952
2953 wm[0] = (sskpd >> 56) & 0xFF;
2954 if (wm[0] == 0)
2955 wm[0] = sskpd & 0xF;
2956 wm[1] = (sskpd >> 4) & 0xFF;
2957 wm[2] = (sskpd >> 12) & 0xFF;
2958 wm[3] = (sskpd >> 20) & 0x1FF;
2959 wm[4] = (sskpd >> 32) & 0x1FF;
2960 } else if (DISPLAY_VER(dev_priv) >= 6) {
2961 u32 sskpd = intel_uncore_read(uncore, MCH_SSKPD);
2962
2963 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2964 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2965 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2966 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
2967 } else if (DISPLAY_VER(dev_priv) >= 5) {
2968 u32 mltr = intel_uncore_read(uncore, MLTR_ILK);
2969
2970 /* ILK primary LP0 latency is 700 ns */
2971 wm[0] = 7;
2972 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2973 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
2974 } else {
2975 MISSING_CASE(INTEL_DEVID(dev_priv));
2976 }
2977}
2978
2979static void intel_fixup_spr_wm_latency(struct drm_i915_private *dev_priv,
2980 u16 wm[5])
2981{
2982 /* ILK sprite LP0 latency is 1300 ns */
2983 if (DISPLAY_VER(dev_priv) == 5)
2984 wm[0] = 13;
2985}
2986
2987static void intel_fixup_cur_wm_latency(struct drm_i915_private *dev_priv,
2988 u16 wm[5])
2989{
2990 /* ILK cursor LP0 latency is 1300 ns */
2991 if (DISPLAY_VER(dev_priv) == 5)
2992 wm[0] = 13;
2993}
2994
2995int ilk_wm_max_level(const struct drm_i915_private *dev_priv)
2996{
2997 /* how many WM levels are we expecting */
2998 if (HAS_HW_SAGV_WM(dev_priv))
2999 return 5;
3000 else if (DISPLAY_VER(dev_priv) >= 9)
3001 return 7;
3002 else if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3003 return 4;
3004 else if (DISPLAY_VER(dev_priv) >= 6)
3005 return 3;
3006 else
3007 return 2;
3008}
3009
3010static void intel_print_wm_latency(struct drm_i915_private *dev_priv,
3011 const char *name,
3012 const u16 wm[])
3013{
3014 int level, max_level = ilk_wm_max_level(dev_priv);
3015
3016 for (level = 0; level <= max_level; level++) {
3017 unsigned int latency = wm[level];
3018
3019 if (latency == 0) {
3020 drm_dbg_kms(&dev_priv->drm,
3021 "%s WM%d latency not provided\n",
3022 name, level);
3023 continue;
3024 }
3025
3026 /*
3027 * - latencies are in us on gen9.
3028 * - before then, WM1+ latency values are in 0.5us units
3029 */
3030 if (DISPLAY_VER(dev_priv) >= 9)
3031 latency *= 10;
3032 else if (level > 0)
3033 latency *= 5;
3034
3035 drm_dbg_kms(&dev_priv->drm,
3036 "%s WM%d latency %u (%u.%u usec)\n", name, level,
3037 wm[level], latency / 10, latency % 10);
3038 }
3039}
3040
3041static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
3042 u16 wm[5], u16 min)
3043{
3044 int level, max_level = ilk_wm_max_level(dev_priv);
3045
3046 if (wm[0] >= min)
3047 return false;
3048
3049 wm[0] = max(wm[0], min);
3050 for (level = 1; level <= max_level; level++)
3051 wm[level] = max_t(u16, wm[level], DIV_ROUND_UP(min, 5));
3052
3053 return true;
3054}
3055
3056static void snb_wm_latency_quirk(struct drm_i915_private *dev_priv)
3057{
3058 bool changed;
3059
3060 /*
3061 * The BIOS provided WM memory latency values are often
3062 * inadequate for high resolution displays. Adjust them.
3063 */
3064 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
3065 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
3066 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
3067
3068 if (!changed)
3069 return;
3070
3071 drm_dbg_kms(&dev_priv->drm,
3072 "WM latency values increased to avoid potential underruns\n");
3073 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3074 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3075 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3076}
3077
3078static void snb_wm_lp3_irq_quirk(struct drm_i915_private *dev_priv)
3079{
3080 /*
3081 * On some SNB machines (Thinkpad X220 Tablet at least)
3082 * LP3 usage can cause vblank interrupts to be lost.
3083 * The DEIIR bit will go high but it looks like the CPU
3084 * never gets interrupted.
3085 *
3086 * It's not clear whether other interrupt source could
3087 * be affected or if this is somehow limited to vblank
3088 * interrupts only. To play it safe we disable LP3
3089 * watermarks entirely.
3090 */
3091 if (dev_priv->wm.pri_latency[3] == 0 &&
3092 dev_priv->wm.spr_latency[3] == 0 &&
3093 dev_priv->wm.cur_latency[3] == 0)
3094 return;
3095
3096 dev_priv->wm.pri_latency[3] = 0;
3097 dev_priv->wm.spr_latency[3] = 0;
3098 dev_priv->wm.cur_latency[3] = 0;
3099
3100 drm_dbg_kms(&dev_priv->drm,
3101 "LP3 watermarks disabled due to potential for lost interrupts\n");
3102 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3103 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3104 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3105}
3106
3107static void ilk_setup_wm_latency(struct drm_i915_private *dev_priv)
3108{
3109 intel_read_wm_latency(dev_priv, dev_priv->wm.pri_latency);
3110
3111 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
3112 sizeof(dev_priv->wm.pri_latency));
3113 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
3114 sizeof(dev_priv->wm.pri_latency));
3115
3116 intel_fixup_spr_wm_latency(dev_priv, dev_priv->wm.spr_latency);
3117 intel_fixup_cur_wm_latency(dev_priv, dev_priv->wm.cur_latency);
3118
3119 intel_print_wm_latency(dev_priv, "Primary", dev_priv->wm.pri_latency);
3120 intel_print_wm_latency(dev_priv, "Sprite", dev_priv->wm.spr_latency);
3121 intel_print_wm_latency(dev_priv, "Cursor", dev_priv->wm.cur_latency);
3122
3123 if (DISPLAY_VER(dev_priv) == 6) {
3124 snb_wm_latency_quirk(dev_priv);
3125 snb_wm_lp3_irq_quirk(dev_priv);
3126 }
3127}
3128
3129static void skl_setup_wm_latency(struct drm_i915_private *dev_priv)
3130{
3131 intel_read_wm_latency(dev_priv, dev_priv->wm.skl_latency);
3132 intel_print_wm_latency(dev_priv, "Gen9 Plane", dev_priv->wm.skl_latency);
3133}
3134
3135static bool ilk_validate_pipe_wm(const struct drm_i915_private *dev_priv,
3136 struct intel_pipe_wm *pipe_wm)
3137{
3138 /* LP0 watermark maximums depend on this pipe alone */
3139 const struct intel_wm_config config = {
3140 .num_pipes_active = 1,
3141 .sprites_enabled = pipe_wm->sprites_enabled,
3142 .sprites_scaled = pipe_wm->sprites_scaled,
3143 };
3144 struct ilk_wm_maximums max;
3145
3146 /* LP0 watermarks always use 1/2 DDB partitioning */
3147 ilk_compute_wm_maximums(dev_priv, 0, &config, INTEL_DDB_PART_1_2, &max);
3148
3149 /* At least LP0 must be valid */
3150 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
3151 drm_dbg_kms(&dev_priv->drm, "LP0 watermark invalid\n");
3152 return false;
3153 }
3154
3155 return true;
3156}
3157
3158/* Compute new watermarks for the pipe */
3159static int ilk_compute_pipe_wm(struct intel_crtc_state *crtc_state)
3160{
3161 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3162 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3163 struct intel_pipe_wm *pipe_wm;
3164 struct intel_plane *plane;
3165 const struct intel_plane_state *plane_state;
3166 const struct intel_plane_state *pristate = NULL;
3167 const struct intel_plane_state *sprstate = NULL;
3168 const struct intel_plane_state *curstate = NULL;
3169 int level, max_level = ilk_wm_max_level(dev_priv), usable_level;
3170 struct ilk_wm_maximums max;
3171
3172 pipe_wm = &crtc_state->wm.ilk.optimal;
3173
3174 intel_atomic_crtc_state_for_each_plane_state(plane, plane_state, crtc_state) {
3175 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
3176 pristate = plane_state;
3177 else if (plane->base.type == DRM_PLANE_TYPE_OVERLAY)
3178 sprstate = plane_state;
3179 else if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
3180 curstate = plane_state;
3181 }
3182
3183 pipe_wm->pipe_enabled = crtc_state->hw.active;
3184 if (sprstate) {
3185 pipe_wm->sprites_enabled = sprstate->uapi.visible;
3186 pipe_wm->sprites_scaled = sprstate->uapi.visible &&
3187 (drm_rect_width(&sprstate->uapi.dst) != drm_rect_width(&sprstate->uapi.src) >> 16 ||
3188 drm_rect_height(&sprstate->uapi.dst) != drm_rect_height(&sprstate->uapi.src) >> 16);
3189 }
3190
3191 usable_level = max_level;
3192
3193 /* ILK/SNB: LP2+ watermarks only w/o sprites */
3194 if (DISPLAY_VER(dev_priv) <= 6 && pipe_wm->sprites_enabled)
3195 usable_level = 1;
3196
3197 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
3198 if (pipe_wm->sprites_scaled)
3199 usable_level = 0;
3200
3201 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
3202 ilk_compute_wm_level(dev_priv, crtc, 0, crtc_state,
3203 pristate, sprstate, curstate, &pipe_wm->wm[0]);
3204
3205 if (!ilk_validate_pipe_wm(dev_priv, pipe_wm))
3206 return -EINVAL;
3207
3208 ilk_compute_wm_reg_maximums(dev_priv, 1, &max);
3209
3210 for (level = 1; level <= usable_level; level++) {
3211 struct intel_wm_level *wm = &pipe_wm->wm[level];
3212
3213 ilk_compute_wm_level(dev_priv, crtc, level, crtc_state,
3214 pristate, sprstate, curstate, wm);
3215
3216 /*
3217 * Disable any watermark level that exceeds the
3218 * register maximums since such watermarks are
3219 * always invalid.
3220 */
3221 if (!ilk_validate_wm_level(level, &max, wm)) {
3222 memset(wm, 0, sizeof(*wm));
3223 break;
3224 }
3225 }
3226
3227 return 0;
3228}
3229
3230/*
3231 * Build a set of 'intermediate' watermark values that satisfy both the old
3232 * state and the new state. These can be programmed to the hardware
3233 * immediately.
3234 */
3235static int ilk_compute_intermediate_wm(struct intel_crtc_state *newstate)
3236{
3237 struct intel_crtc *intel_crtc = to_intel_crtc(newstate->uapi.crtc);
3238 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3239 struct intel_pipe_wm *a = &newstate->wm.ilk.intermediate;
3240 struct intel_atomic_state *intel_state =
3241 to_intel_atomic_state(newstate->uapi.state);
3242 const struct intel_crtc_state *oldstate =
3243 intel_atomic_get_old_crtc_state(intel_state, intel_crtc);
3244 const struct intel_pipe_wm *b = &oldstate->wm.ilk.optimal;
3245 int level, max_level = ilk_wm_max_level(dev_priv);
3246
3247 /*
3248 * Start with the final, target watermarks, then combine with the
3249 * currently active watermarks to get values that are safe both before
3250 * and after the vblank.
3251 */
3252 *a = newstate->wm.ilk.optimal;
3253 if (!newstate->hw.active || drm_atomic_crtc_needs_modeset(&newstate->uapi) ||
3254 intel_state->skip_intermediate_wm)
3255 return 0;
3256
3257 a->pipe_enabled |= b->pipe_enabled;
3258 a->sprites_enabled |= b->sprites_enabled;
3259 a->sprites_scaled |= b->sprites_scaled;
3260
3261 for (level = 0; level <= max_level; level++) {
3262 struct intel_wm_level *a_wm = &a->wm[level];
3263 const struct intel_wm_level *b_wm = &b->wm[level];
3264
3265 a_wm->enable &= b_wm->enable;
3266 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
3267 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
3268 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
3269 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
3270 }
3271
3272 /*
3273 * We need to make sure that these merged watermark values are
3274 * actually a valid configuration themselves. If they're not,
3275 * there's no safe way to transition from the old state to
3276 * the new state, so we need to fail the atomic transaction.
3277 */
3278 if (!ilk_validate_pipe_wm(dev_priv, a))
3279 return -EINVAL;
3280
3281 /*
3282 * If our intermediate WM are identical to the final WM, then we can
3283 * omit the post-vblank programming; only update if it's different.
3284 */
3285 if (memcmp(a, &newstate->wm.ilk.optimal, sizeof(*a)) != 0)
3286 newstate->wm.need_postvbl_update = true;
3287
3288 return 0;
3289}
3290
3291/*
3292 * Merge the watermarks from all active pipes for a specific level.
3293 */
3294static void ilk_merge_wm_level(struct drm_i915_private *dev_priv,
3295 int level,
3296 struct intel_wm_level *ret_wm)
3297{
3298 const struct intel_crtc *intel_crtc;
3299
3300 ret_wm->enable = true;
3301
3302 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
3303 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
3304 const struct intel_wm_level *wm = &active->wm[level];
3305
3306 if (!active->pipe_enabled)
3307 continue;
3308
3309 /*
3310 * The watermark values may have been used in the past,
3311 * so we must maintain them in the registers for some
3312 * time even if the level is now disabled.
3313 */
3314 if (!wm->enable)
3315 ret_wm->enable = false;
3316
3317 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
3318 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
3319 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
3320 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
3321 }
3322}
3323
3324/*
3325 * Merge all low power watermarks for all active pipes.
3326 */
3327static void ilk_wm_merge(struct drm_i915_private *dev_priv,
3328 const struct intel_wm_config *config,
3329 const struct ilk_wm_maximums *max,
3330 struct intel_pipe_wm *merged)
3331{
3332 int level, max_level = ilk_wm_max_level(dev_priv);
3333 int last_enabled_level = max_level;
3334
3335 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
3336 if ((DISPLAY_VER(dev_priv) <= 6 || IS_IVYBRIDGE(dev_priv)) &&
3337 config->num_pipes_active > 1)
3338 last_enabled_level = 0;
3339
3340 /* ILK: FBC WM must be disabled always */
3341 merged->fbc_wm_enabled = DISPLAY_VER(dev_priv) >= 6;
3342
3343 /* merge each WM1+ level */
3344 for (level = 1; level <= max_level; level++) {
3345 struct intel_wm_level *wm = &merged->wm[level];
3346
3347 ilk_merge_wm_level(dev_priv, level, wm);
3348
3349 if (level > last_enabled_level)
3350 wm->enable = false;
3351 else if (!ilk_validate_wm_level(level, max, wm))
3352 /* make sure all following levels get disabled */
3353 last_enabled_level = level - 1;
3354
3355 /*
3356 * The spec says it is preferred to disable
3357 * FBC WMs instead of disabling a WM level.
3358 */
3359 if (wm->fbc_val > max->fbc) {
3360 if (wm->enable)
3361 merged->fbc_wm_enabled = false;
3362 wm->fbc_val = 0;
3363 }
3364 }
3365
3366 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
3367 /*
3368 * FIXME this is racy. FBC might get enabled later.
3369 * What we should check here is whether FBC can be
3370 * enabled sometime later.
3371 */
3372 if (DISPLAY_VER(dev_priv) == 5 && !merged->fbc_wm_enabled &&
3373 intel_fbc_is_active(dev_priv)) {
3374 for (level = 2; level <= max_level; level++) {
3375 struct intel_wm_level *wm = &merged->wm[level];
3376
3377 wm->enable = false;
3378 }
3379 }
3380}
3381
3382static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
3383{
3384 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
3385 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
3386}
3387
3388/* The value we need to program into the WM_LPx latency field */
3389static unsigned int ilk_wm_lp_latency(struct drm_i915_private *dev_priv,
3390 int level)
3391{
3392 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
3393 return 2 * level;
3394 else
3395 return dev_priv->wm.pri_latency[level];
3396}
3397
3398static void ilk_compute_wm_results(struct drm_i915_private *dev_priv,
3399 const struct intel_pipe_wm *merged,
3400 enum intel_ddb_partitioning partitioning,
3401 struct ilk_wm_values *results)
3402{
3403 struct intel_crtc *intel_crtc;
3404 int level, wm_lp;
3405
3406 results->enable_fbc_wm = merged->fbc_wm_enabled;
3407 results->partitioning = partitioning;
3408
3409 /* LP1+ register values */
3410 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3411 const struct intel_wm_level *r;
3412
3413 level = ilk_wm_lp_to_level(wm_lp, merged);
3414
3415 r = &merged->wm[level];
3416
3417 /*
3418 * Maintain the watermark values even if the level is
3419 * disabled. Doing otherwise could cause underruns.
3420 */
3421 results->wm_lp[wm_lp - 1] =
3422 (ilk_wm_lp_latency(dev_priv, level) << WM1_LP_LATENCY_SHIFT) |
3423 (r->pri_val << WM1_LP_SR_SHIFT) |
3424 r->cur_val;
3425
3426 if (r->enable)
3427 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
3428
3429 if (DISPLAY_VER(dev_priv) >= 8)
3430 results->wm_lp[wm_lp - 1] |=
3431 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
3432 else
3433 results->wm_lp[wm_lp - 1] |=
3434 r->fbc_val << WM1_LP_FBC_SHIFT;
3435
3436 /*
3437 * Always set WM1S_LP_EN when spr_val != 0, even if the
3438 * level is disabled. Doing otherwise could cause underruns.
3439 */
3440 if (DISPLAY_VER(dev_priv) <= 6 && r->spr_val) {
3441 drm_WARN_ON(&dev_priv->drm, wm_lp != 1);
3442 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
3443 } else
3444 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
3445 }
3446
3447 /* LP0 register values */
3448 for_each_intel_crtc(&dev_priv->drm, intel_crtc) {
3449 enum pipe pipe = intel_crtc->pipe;
3450 const struct intel_pipe_wm *pipe_wm = &intel_crtc->wm.active.ilk;
3451 const struct intel_wm_level *r = &pipe_wm->wm[0];
3452
3453 if (drm_WARN_ON(&dev_priv->drm, !r->enable))
3454 continue;
3455
3456 results->wm_pipe[pipe] =
3457 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
3458 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
3459 r->cur_val;
3460 }
3461}
3462
3463/* Find the result with the highest level enabled. Check for enable_fbc_wm in
3464 * case both are at the same level. Prefer r1 in case they're the same. */
3465static struct intel_pipe_wm *
3466ilk_find_best_result(struct drm_i915_private *dev_priv,
3467 struct intel_pipe_wm *r1,
3468 struct intel_pipe_wm *r2)
3469{
3470 int level, max_level = ilk_wm_max_level(dev_priv);
3471 int level1 = 0, level2 = 0;
3472
3473 for (level = 1; level <= max_level; level++) {
3474 if (r1->wm[level].enable)
3475 level1 = level;
3476 if (r2->wm[level].enable)
3477 level2 = level;
3478 }
3479
3480 if (level1 == level2) {
3481 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
3482 return r2;
3483 else
3484 return r1;
3485 } else if (level1 > level2) {
3486 return r1;
3487 } else {
3488 return r2;
3489 }
3490}
3491
3492/* dirty bits used to track which watermarks need changes */
3493#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
3494#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
3495#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
3496#define WM_DIRTY_FBC (1 << 24)
3497#define WM_DIRTY_DDB (1 << 25)
3498
3499static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
3500 const struct ilk_wm_values *old,
3501 const struct ilk_wm_values *new)
3502{
3503 unsigned int dirty = 0;
3504 enum pipe pipe;
3505 int wm_lp;
3506
3507 for_each_pipe(dev_priv, pipe) {
3508 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
3509 dirty |= WM_DIRTY_PIPE(pipe);
3510 /* Must disable LP1+ watermarks too */
3511 dirty |= WM_DIRTY_LP_ALL;
3512 }
3513 }
3514
3515 if (old->enable_fbc_wm != new->enable_fbc_wm) {
3516 dirty |= WM_DIRTY_FBC;
3517 /* Must disable LP1+ watermarks too */
3518 dirty |= WM_DIRTY_LP_ALL;
3519 }
3520
3521 if (old->partitioning != new->partitioning) {
3522 dirty |= WM_DIRTY_DDB;
3523 /* Must disable LP1+ watermarks too */
3524 dirty |= WM_DIRTY_LP_ALL;
3525 }
3526
3527 /* LP1+ watermarks already deemed dirty, no need to continue */
3528 if (dirty & WM_DIRTY_LP_ALL)
3529 return dirty;
3530
3531 /* Find the lowest numbered LP1+ watermark in need of an update... */
3532 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
3533 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
3534 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
3535 break;
3536 }
3537
3538 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
3539 for (; wm_lp <= 3; wm_lp++)
3540 dirty |= WM_DIRTY_LP(wm_lp);
3541
3542 return dirty;
3543}
3544
3545static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
3546 unsigned int dirty)
3547{
3548 struct ilk_wm_values *previous = &dev_priv->wm.hw;
3549 bool changed = false;
3550
3551 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
3552 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
3553 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, previous->wm_lp[2]);
3554 changed = true;
3555 }
3556 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
3557 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
3558 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, previous->wm_lp[1]);
3559 changed = true;
3560 }
3561 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
3562 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
3563 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, previous->wm_lp[0]);
3564 changed = true;
3565 }
3566
3567 /*
3568 * Don't touch WM1S_LP_EN here.
3569 * Doing so could cause underruns.
3570 */
3571
3572 return changed;
3573}
3574
3575/*
3576 * The spec says we shouldn't write when we don't need, because every write
3577 * causes WMs to be re-evaluated, expending some power.
3578 */
3579static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
3580 struct ilk_wm_values *results)
3581{
3582 struct ilk_wm_values *previous = &dev_priv->wm.hw;
3583 unsigned int dirty;
3584 u32 val;
3585
3586 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
3587 if (!dirty)
3588 return;
3589
3590 _ilk_disable_lp_wm(dev_priv, dirty);
3591
3592 if (dirty & WM_DIRTY_PIPE(PIPE_A))
3593 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_A), results->wm_pipe[0]);
3594 if (dirty & WM_DIRTY_PIPE(PIPE_B))
3595 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_B), results->wm_pipe[1]);
3596 if (dirty & WM_DIRTY_PIPE(PIPE_C))
3597 intel_uncore_write(&dev_priv->uncore, WM0_PIPE_ILK(PIPE_C), results->wm_pipe[2]);
3598
3599 if (dirty & WM_DIRTY_DDB) {
3600 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv)) {
3601 val = intel_uncore_read(&dev_priv->uncore, WM_MISC);
3602 if (results->partitioning == INTEL_DDB_PART_1_2)
3603 val &= ~WM_MISC_DATA_PARTITION_5_6;
3604 else
3605 val |= WM_MISC_DATA_PARTITION_5_6;
3606 intel_uncore_write(&dev_priv->uncore, WM_MISC, val);
3607 } else {
3608 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
3609 if (results->partitioning == INTEL_DDB_PART_1_2)
3610 val &= ~DISP_DATA_PARTITION_5_6;
3611 else
3612 val |= DISP_DATA_PARTITION_5_6;
3613 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
3614 }
3615 }
3616
3617 if (dirty & WM_DIRTY_FBC) {
3618 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL);
3619 if (results->enable_fbc_wm)
3620 val &= ~DISP_FBC_WM_DIS;
3621 else
3622 val |= DISP_FBC_WM_DIS;
3623 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, val);
3624 }
3625
3626 if (dirty & WM_DIRTY_LP(1) &&
3627 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
3628 intel_uncore_write(&dev_priv->uncore, WM1S_LP_ILK, results->wm_lp_spr[0]);
3629
3630 if (DISPLAY_VER(dev_priv) >= 7) {
3631 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
3632 intel_uncore_write(&dev_priv->uncore, WM2S_LP_IVB, results->wm_lp_spr[1]);
3633 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
3634 intel_uncore_write(&dev_priv->uncore, WM3S_LP_IVB, results->wm_lp_spr[2]);
3635 }
3636
3637 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
3638 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, results->wm_lp[0]);
3639 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
3640 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, results->wm_lp[1]);
3641 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
3642 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, results->wm_lp[2]);
3643
3644 dev_priv->wm.hw = *results;
3645}
3646
3647bool ilk_disable_lp_wm(struct drm_i915_private *dev_priv)
3648{
3649 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
3650}
3651
3652u8 intel_enabled_dbuf_slices_mask(struct drm_i915_private *dev_priv)
3653{
3654 u8 enabled_slices = 0;
3655 enum dbuf_slice slice;
3656
3657 for_each_dbuf_slice(dev_priv, slice) {
3658 if (intel_uncore_read(&dev_priv->uncore,
3659 DBUF_CTL_S(slice)) & DBUF_POWER_STATE)
3660 enabled_slices |= BIT(slice);
3661 }
3662
3663 return enabled_slices;
3664}
3665
3666/*
3667 * FIXME: We still don't have the proper code detect if we need to apply the WA,
3668 * so assume we'll always need it in order to avoid underruns.
3669 */
3670static bool skl_needs_memory_bw_wa(struct drm_i915_private *dev_priv)
3671{
3672 return DISPLAY_VER(dev_priv) == 9;
3673}
3674
3675static bool
3676intel_has_sagv(struct drm_i915_private *dev_priv)
3677{
3678 return DISPLAY_VER(dev_priv) >= 9 && !IS_LP(dev_priv) &&
3679 dev_priv->sagv_status != I915_SAGV_NOT_CONTROLLED;
3680}
3681
3682static void
3683skl_setup_sagv_block_time(struct drm_i915_private *dev_priv)
3684{
3685 if (DISPLAY_VER(dev_priv) >= 12) {
3686 u32 val = 0;
3687 int ret;
3688
3689 ret = sandybridge_pcode_read(dev_priv,
3690 GEN12_PCODE_READ_SAGV_BLOCK_TIME_US,
3691 &val, NULL);
3692 if (!ret) {
3693 dev_priv->sagv_block_time_us = val;
3694 return;
3695 }
3696
3697 drm_dbg(&dev_priv->drm, "Couldn't read SAGV block time!\n");
3698 } else if (DISPLAY_VER(dev_priv) == 11) {
3699 dev_priv->sagv_block_time_us = 10;
3700 return;
3701 } else if (DISPLAY_VER(dev_priv) == 10) {
3702 dev_priv->sagv_block_time_us = 20;
3703 return;
3704 } else if (DISPLAY_VER(dev_priv) == 9) {
3705 dev_priv->sagv_block_time_us = 30;
3706 return;
3707 } else {
3708 MISSING_CASE(DISPLAY_VER(dev_priv));
3709 }
3710
3711 /* Default to an unusable block time */
3712 dev_priv->sagv_block_time_us = -1;
3713}
3714
3715/*
3716 * SAGV dynamically adjusts the system agent voltage and clock frequencies
3717 * depending on power and performance requirements. The display engine access
3718 * to system memory is blocked during the adjustment time. Because of the
3719 * blocking time, having this enabled can cause full system hangs and/or pipe
3720 * underruns if we don't meet all of the following requirements:
3721 *
3722 * - <= 1 pipe enabled
3723 * - All planes can enable watermarks for latencies >= SAGV engine block time
3724 * - We're not using an interlaced display configuration
3725 */
3726static int
3727intel_enable_sagv(struct drm_i915_private *dev_priv)
3728{
3729 int ret;
3730
3731 if (!intel_has_sagv(dev_priv))
3732 return 0;
3733
3734 if (dev_priv->sagv_status == I915_SAGV_ENABLED)
3735 return 0;
3736
3737 drm_dbg_kms(&dev_priv->drm, "Enabling SAGV\n");
3738 ret = sandybridge_pcode_write(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3739 GEN9_SAGV_ENABLE);
3740
3741 /* We don't need to wait for SAGV when enabling */
3742
3743 /*
3744 * Some skl systems, pre-release machines in particular,
3745 * don't actually have SAGV.
3746 */
3747 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
3748 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
3749 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
3750 return 0;
3751 } else if (ret < 0) {
3752 drm_err(&dev_priv->drm, "Failed to enable SAGV\n");
3753 return ret;
3754 }
3755
3756 dev_priv->sagv_status = I915_SAGV_ENABLED;
3757 return 0;
3758}
3759
3760static int
3761intel_disable_sagv(struct drm_i915_private *dev_priv)
3762{
3763 int ret;
3764
3765 if (!intel_has_sagv(dev_priv))
3766 return 0;
3767
3768 if (dev_priv->sagv_status == I915_SAGV_DISABLED)
3769 return 0;
3770
3771 drm_dbg_kms(&dev_priv->drm, "Disabling SAGV\n");
3772 /* bspec says to keep retrying for at least 1 ms */
3773 ret = skl_pcode_request(dev_priv, GEN9_PCODE_SAGV_CONTROL,
3774 GEN9_SAGV_DISABLE,
3775 GEN9_SAGV_IS_DISABLED, GEN9_SAGV_IS_DISABLED,
3776 1);
3777 /*
3778 * Some skl systems, pre-release machines in particular,
3779 * don't actually have SAGV.
3780 */
3781 if (IS_SKYLAKE(dev_priv) && ret == -ENXIO) {
3782 drm_dbg(&dev_priv->drm, "No SAGV found on system, ignoring\n");
3783 dev_priv->sagv_status = I915_SAGV_NOT_CONTROLLED;
3784 return 0;
3785 } else if (ret < 0) {
3786 drm_err(&dev_priv->drm, "Failed to disable SAGV (%d)\n", ret);
3787 return ret;
3788 }
3789
3790 dev_priv->sagv_status = I915_SAGV_DISABLED;
3791 return 0;
3792}
3793
3794void intel_sagv_pre_plane_update(struct intel_atomic_state *state)
3795{
3796 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3797 const struct intel_bw_state *new_bw_state;
3798 const struct intel_bw_state *old_bw_state;
3799 u32 new_mask = 0;
3800
3801 /*
3802 * Just return if we can't control SAGV or don't have it.
3803 * This is different from situation when we have SAGV but just can't
3804 * afford it due to DBuf limitation - in case if SAGV is completely
3805 * disabled in a BIOS, we are not even allowed to send a PCode request,
3806 * as it will throw an error. So have to check it here.
3807 */
3808 if (!intel_has_sagv(dev_priv))
3809 return;
3810
3811 new_bw_state = intel_atomic_get_new_bw_state(state);
3812 if (!new_bw_state)
3813 return;
3814
3815 if (DISPLAY_VER(dev_priv) < 11 && !intel_can_enable_sagv(dev_priv, new_bw_state)) {
3816 intel_disable_sagv(dev_priv);
3817 return;
3818 }
3819
3820 old_bw_state = intel_atomic_get_old_bw_state(state);
3821 /*
3822 * Nothing to mask
3823 */
3824 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3825 return;
3826
3827 new_mask = old_bw_state->qgv_points_mask | new_bw_state->qgv_points_mask;
3828
3829 /*
3830 * If new mask is zero - means there is nothing to mask,
3831 * we can only unmask, which should be done in unmask.
3832 */
3833 if (!new_mask)
3834 return;
3835
3836 /*
3837 * Restrict required qgv points before updating the configuration.
3838 * According to BSpec we can't mask and unmask qgv points at the same
3839 * time. Also masking should be done before updating the configuration
3840 * and unmasking afterwards.
3841 */
3842 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
3843}
3844
3845void intel_sagv_post_plane_update(struct intel_atomic_state *state)
3846{
3847 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3848 const struct intel_bw_state *new_bw_state;
3849 const struct intel_bw_state *old_bw_state;
3850 u32 new_mask = 0;
3851
3852 /*
3853 * Just return if we can't control SAGV or don't have it.
3854 * This is different from situation when we have SAGV but just can't
3855 * afford it due to DBuf limitation - in case if SAGV is completely
3856 * disabled in a BIOS, we are not even allowed to send a PCode request,
3857 * as it will throw an error. So have to check it here.
3858 */
3859 if (!intel_has_sagv(dev_priv))
3860 return;
3861
3862 new_bw_state = intel_atomic_get_new_bw_state(state);
3863 if (!new_bw_state)
3864 return;
3865
3866 if (DISPLAY_VER(dev_priv) < 11 && intel_can_enable_sagv(dev_priv, new_bw_state)) {
3867 intel_enable_sagv(dev_priv);
3868 return;
3869 }
3870
3871 old_bw_state = intel_atomic_get_old_bw_state(state);
3872 /*
3873 * Nothing to unmask
3874 */
3875 if (new_bw_state->qgv_points_mask == old_bw_state->qgv_points_mask)
3876 return;
3877
3878 new_mask = new_bw_state->qgv_points_mask;
3879
3880 /*
3881 * Allow required qgv points after updating the configuration.
3882 * According to BSpec we can't mask and unmask qgv points at the same
3883 * time. Also masking should be done before updating the configuration
3884 * and unmasking afterwards.
3885 */
3886 icl_pcode_restrict_qgv_points(dev_priv, new_mask);
3887}
3888
3889static bool skl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3890{
3891 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3892 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3893 enum plane_id plane_id;
3894 int max_level = INT_MAX;
3895
3896 if (!intel_has_sagv(dev_priv))
3897 return false;
3898
3899 if (!crtc_state->hw.active)
3900 return true;
3901
3902 if (crtc_state->hw.pipe_mode.flags & DRM_MODE_FLAG_INTERLACE)
3903 return false;
3904
3905 for_each_plane_id_on_crtc(crtc, plane_id) {
3906 const struct skl_plane_wm *wm =
3907 &crtc_state->wm.skl.optimal.planes[plane_id];
3908 int level;
3909
3910 /* Skip this plane if it's not enabled */
3911 if (!wm->wm[0].enable)
3912 continue;
3913
3914 /* Find the highest enabled wm level for this plane */
3915 for (level = ilk_wm_max_level(dev_priv);
3916 !wm->wm[level].enable; --level)
3917 { }
3918
3919 /* Highest common enabled wm level for all planes */
3920 max_level = min(level, max_level);
3921 }
3922
3923 /* No enabled planes? */
3924 if (max_level == INT_MAX)
3925 return true;
3926
3927 for_each_plane_id_on_crtc(crtc, plane_id) {
3928 const struct skl_plane_wm *wm =
3929 &crtc_state->wm.skl.optimal.planes[plane_id];
3930
3931 /*
3932 * All enabled planes must have enabled a common wm level that
3933 * can tolerate memory latencies higher than sagv_block_time_us
3934 */
3935 if (wm->wm[0].enable && !wm->wm[max_level].can_sagv)
3936 return false;
3937 }
3938
3939 return true;
3940}
3941
3942static bool tgl_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3943{
3944 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3945 enum plane_id plane_id;
3946
3947 if (!crtc_state->hw.active)
3948 return true;
3949
3950 for_each_plane_id_on_crtc(crtc, plane_id) {
3951 const struct skl_plane_wm *wm =
3952 &crtc_state->wm.skl.optimal.planes[plane_id];
3953
3954 if (wm->wm[0].enable && !wm->sagv.wm0.enable)
3955 return false;
3956 }
3957
3958 return true;
3959}
3960
3961static bool intel_crtc_can_enable_sagv(const struct intel_crtc_state *crtc_state)
3962{
3963 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3964 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3965
3966 if (DISPLAY_VER(dev_priv) >= 12)
3967 return tgl_crtc_can_enable_sagv(crtc_state);
3968 else
3969 return skl_crtc_can_enable_sagv(crtc_state);
3970}
3971
3972bool intel_can_enable_sagv(struct drm_i915_private *dev_priv,
3973 const struct intel_bw_state *bw_state)
3974{
3975 if (DISPLAY_VER(dev_priv) < 11 &&
3976 bw_state->active_pipes && !is_power_of_2(bw_state->active_pipes))
3977 return false;
3978
3979 return bw_state->pipe_sagv_reject == 0;
3980}
3981
3982static int intel_compute_sagv_mask(struct intel_atomic_state *state)
3983{
3984 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3985 int ret;
3986 struct intel_crtc *crtc;
3987 struct intel_crtc_state *new_crtc_state;
3988 struct intel_bw_state *new_bw_state = NULL;
3989 const struct intel_bw_state *old_bw_state = NULL;
3990 int i;
3991
3992 for_each_new_intel_crtc_in_state(state, crtc,
3993 new_crtc_state, i) {
3994 new_bw_state = intel_atomic_get_bw_state(state);
3995 if (IS_ERR(new_bw_state))
3996 return PTR_ERR(new_bw_state);
3997
3998 old_bw_state = intel_atomic_get_old_bw_state(state);
3999
4000 if (intel_crtc_can_enable_sagv(new_crtc_state))
4001 new_bw_state->pipe_sagv_reject &= ~BIT(crtc->pipe);
4002 else
4003 new_bw_state->pipe_sagv_reject |= BIT(crtc->pipe);
4004 }
4005
4006 if (!new_bw_state)
4007 return 0;
4008
4009 new_bw_state->active_pipes =
4010 intel_calc_active_pipes(state, old_bw_state->active_pipes);
4011
4012 if (new_bw_state->active_pipes != old_bw_state->active_pipes) {
4013 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4014 if (ret)
4015 return ret;
4016 }
4017
4018 for_each_new_intel_crtc_in_state(state, crtc,
4019 new_crtc_state, i) {
4020 struct skl_pipe_wm *pipe_wm = &new_crtc_state->wm.skl.optimal;
4021
4022 /*
4023 * We store use_sagv_wm in the crtc state rather than relying on
4024 * that bw state since we have no convenient way to get at the
4025 * latter from the plane commit hooks (especially in the legacy
4026 * cursor case)
4027 */
4028 pipe_wm->use_sagv_wm = !HAS_HW_SAGV_WM(dev_priv) &&
4029 DISPLAY_VER(dev_priv) >= 12 &&
4030 intel_can_enable_sagv(dev_priv, new_bw_state);
4031 }
4032
4033 if (intel_can_enable_sagv(dev_priv, new_bw_state) !=
4034 intel_can_enable_sagv(dev_priv, old_bw_state)) {
4035 ret = intel_atomic_serialize_global_state(&new_bw_state->base);
4036 if (ret)
4037 return ret;
4038 } else if (new_bw_state->pipe_sagv_reject != old_bw_state->pipe_sagv_reject) {
4039 ret = intel_atomic_lock_global_state(&new_bw_state->base);
4040 if (ret)
4041 return ret;
4042 }
4043
4044 return 0;
4045}
4046
4047static int intel_dbuf_slice_size(struct drm_i915_private *dev_priv)
4048{
4049 return INTEL_INFO(dev_priv)->dbuf.size /
4050 hweight8(INTEL_INFO(dev_priv)->dbuf.slice_mask);
4051}
4052
4053static void
4054skl_ddb_entry_for_slices(struct drm_i915_private *dev_priv, u8 slice_mask,
4055 struct skl_ddb_entry *ddb)
4056{
4057 int slice_size = intel_dbuf_slice_size(dev_priv);
4058
4059 if (!slice_mask) {
4060 ddb->start = 0;
4061 ddb->end = 0;
4062 return;
4063 }
4064
4065 ddb->start = (ffs(slice_mask) - 1) * slice_size;
4066 ddb->end = fls(slice_mask) * slice_size;
4067
4068 WARN_ON(ddb->start >= ddb->end);
4069 WARN_ON(ddb->end > INTEL_INFO(dev_priv)->dbuf.size);
4070}
4071
4072static unsigned int mbus_ddb_offset(struct drm_i915_private *i915, u8 slice_mask)
4073{
4074 struct skl_ddb_entry ddb;
4075
4076 if (slice_mask & (BIT(DBUF_S1) | BIT(DBUF_S2)))
4077 slice_mask = BIT(DBUF_S1);
4078 else if (slice_mask & (BIT(DBUF_S3) | BIT(DBUF_S4)))
4079 slice_mask = BIT(DBUF_S3);
4080
4081 skl_ddb_entry_for_slices(i915, slice_mask, &ddb);
4082
4083 return ddb.start;
4084}
4085
4086u32 skl_ddb_dbuf_slice_mask(struct drm_i915_private *dev_priv,
4087 const struct skl_ddb_entry *entry)
4088{
4089 int slice_size = intel_dbuf_slice_size(dev_priv);
4090 enum dbuf_slice start_slice, end_slice;
4091 u8 slice_mask = 0;
4092
4093 if (!skl_ddb_entry_size(entry))
4094 return 0;
4095
4096 start_slice = entry->start / slice_size;
4097 end_slice = (entry->end - 1) / slice_size;
4098
4099 /*
4100 * Per plane DDB entry can in a really worst case be on multiple slices
4101 * but single entry is anyway contigious.
4102 */
4103 while (start_slice <= end_slice) {
4104 slice_mask |= BIT(start_slice);
4105 start_slice++;
4106 }
4107
4108 return slice_mask;
4109}
4110
4111static unsigned int intel_crtc_ddb_weight(const struct intel_crtc_state *crtc_state)
4112{
4113 const struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4114 int hdisplay, vdisplay;
4115
4116 if (!crtc_state->hw.active)
4117 return 0;
4118
4119 /*
4120 * Watermark/ddb requirement highly depends upon width of the
4121 * framebuffer, So instead of allocating DDB equally among pipes
4122 * distribute DDB based on resolution/width of the display.
4123 */
4124 drm_mode_get_hv_timing(pipe_mode, &hdisplay, &vdisplay);
4125
4126 return hdisplay;
4127}
4128
4129static void intel_crtc_dbuf_weights(const struct intel_dbuf_state *dbuf_state,
4130 enum pipe for_pipe,
4131 unsigned int *weight_start,
4132 unsigned int *weight_end,
4133 unsigned int *weight_total)
4134{
4135 struct drm_i915_private *dev_priv =
4136 to_i915(dbuf_state->base.state->base.dev);
4137 enum pipe pipe;
4138
4139 *weight_start = 0;
4140 *weight_end = 0;
4141 *weight_total = 0;
4142
4143 for_each_pipe(dev_priv, pipe) {
4144 int weight = dbuf_state->weight[pipe];
4145
4146 /*
4147 * Do not account pipes using other slice sets
4148 * luckily as of current BSpec slice sets do not partially
4149 * intersect(pipes share either same one slice or same slice set
4150 * i.e no partial intersection), so it is enough to check for
4151 * equality for now.
4152 */
4153 if (dbuf_state->slices[pipe] != dbuf_state->slices[for_pipe])
4154 continue;
4155
4156 *weight_total += weight;
4157 if (pipe < for_pipe) {
4158 *weight_start += weight;
4159 *weight_end += weight;
4160 } else if (pipe == for_pipe) {
4161 *weight_end += weight;
4162 }
4163 }
4164}
4165
4166static int
4167skl_crtc_allocate_ddb(struct intel_atomic_state *state, struct intel_crtc *crtc)
4168{
4169 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4170 unsigned int weight_total, weight_start, weight_end;
4171 const struct intel_dbuf_state *old_dbuf_state =
4172 intel_atomic_get_old_dbuf_state(state);
4173 struct intel_dbuf_state *new_dbuf_state =
4174 intel_atomic_get_new_dbuf_state(state);
4175 struct intel_crtc_state *crtc_state;
4176 struct skl_ddb_entry ddb_slices;
4177 enum pipe pipe = crtc->pipe;
4178 unsigned int mbus_offset = 0;
4179 u32 ddb_range_size;
4180 u32 dbuf_slice_mask;
4181 u32 start, end;
4182 int ret;
4183
4184 if (new_dbuf_state->weight[pipe] == 0) {
4185 new_dbuf_state->ddb[pipe].start = 0;
4186 new_dbuf_state->ddb[pipe].end = 0;
4187 goto out;
4188 }
4189
4190 dbuf_slice_mask = new_dbuf_state->slices[pipe];
4191
4192 skl_ddb_entry_for_slices(dev_priv, dbuf_slice_mask, &ddb_slices);
4193 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_slice_mask);
4194 ddb_range_size = skl_ddb_entry_size(&ddb_slices);
4195
4196 intel_crtc_dbuf_weights(new_dbuf_state, pipe,
4197 &weight_start, &weight_end, &weight_total);
4198
4199 start = ddb_range_size * weight_start / weight_total;
4200 end = ddb_range_size * weight_end / weight_total;
4201
4202 new_dbuf_state->ddb[pipe].start = ddb_slices.start - mbus_offset + start;
4203 new_dbuf_state->ddb[pipe].end = ddb_slices.start - mbus_offset + end;
4204out:
4205 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe] &&
4206 skl_ddb_entry_equal(&old_dbuf_state->ddb[pipe],
4207 &new_dbuf_state->ddb[pipe]))
4208 return 0;
4209
4210 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
4211 if (ret)
4212 return ret;
4213
4214 crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
4215 if (IS_ERR(crtc_state))
4216 return PTR_ERR(crtc_state);
4217
4218 /*
4219 * Used for checking overlaps, so we need absolute
4220 * offsets instead of MBUS relative offsets.
4221 */
4222 crtc_state->wm.skl.ddb.start = mbus_offset + new_dbuf_state->ddb[pipe].start;
4223 crtc_state->wm.skl.ddb.end = mbus_offset + new_dbuf_state->ddb[pipe].end;
4224
4225 drm_dbg_kms(&dev_priv->drm,
4226 "[CRTC:%d:%s] dbuf slices 0x%x -> 0x%x, ddb (%d - %d) -> (%d - %d), active pipes 0x%x -> 0x%x\n",
4227 crtc->base.base.id, crtc->base.name,
4228 old_dbuf_state->slices[pipe], new_dbuf_state->slices[pipe],
4229 old_dbuf_state->ddb[pipe].start, old_dbuf_state->ddb[pipe].end,
4230 new_dbuf_state->ddb[pipe].start, new_dbuf_state->ddb[pipe].end,
4231 old_dbuf_state->active_pipes, new_dbuf_state->active_pipes);
4232
4233 return 0;
4234}
4235
4236static int skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
4237 int width, const struct drm_format_info *format,
4238 u64 modifier, unsigned int rotation,
4239 u32 plane_pixel_rate, struct skl_wm_params *wp,
4240 int color_plane);
4241static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
4242 int level,
4243 unsigned int latency,
4244 const struct skl_wm_params *wp,
4245 const struct skl_wm_level *result_prev,
4246 struct skl_wm_level *result /* out */);
4247
4248static unsigned int
4249skl_cursor_allocation(const struct intel_crtc_state *crtc_state,
4250 int num_active)
4251{
4252 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4253 int level, max_level = ilk_wm_max_level(dev_priv);
4254 struct skl_wm_level wm = {};
4255 int ret, min_ddb_alloc = 0;
4256 struct skl_wm_params wp;
4257
4258 ret = skl_compute_wm_params(crtc_state, 256,
4259 drm_format_info(DRM_FORMAT_ARGB8888),
4260 DRM_FORMAT_MOD_LINEAR,
4261 DRM_MODE_ROTATE_0,
4262 crtc_state->pixel_rate, &wp, 0);
4263 drm_WARN_ON(&dev_priv->drm, ret);
4264
4265 for (level = 0; level <= max_level; level++) {
4266 unsigned int latency = dev_priv->wm.skl_latency[level];
4267
4268 skl_compute_plane_wm(crtc_state, level, latency, &wp, &wm, &wm);
4269 if (wm.min_ddb_alloc == U16_MAX)
4270 break;
4271
4272 min_ddb_alloc = wm.min_ddb_alloc;
4273 }
4274
4275 return max(num_active == 1 ? 32 : 8, min_ddb_alloc);
4276}
4277
4278static void skl_ddb_entry_init_from_hw(struct drm_i915_private *dev_priv,
4279 struct skl_ddb_entry *entry, u32 reg)
4280{
4281 entry->start = reg & DDB_ENTRY_MASK;
4282 entry->end = (reg >> DDB_ENTRY_END_SHIFT) & DDB_ENTRY_MASK;
4283
4284 if (entry->end)
4285 entry->end += 1;
4286}
4287
4288static void
4289skl_ddb_get_hw_plane_state(struct drm_i915_private *dev_priv,
4290 const enum pipe pipe,
4291 const enum plane_id plane_id,
4292 struct skl_ddb_entry *ddb_y,
4293 struct skl_ddb_entry *ddb_uv)
4294{
4295 u32 val, val2;
4296 u32 fourcc = 0;
4297
4298 /* Cursor doesn't support NV12/planar, so no extra calculation needed */
4299 if (plane_id == PLANE_CURSOR) {
4300 val = intel_uncore_read(&dev_priv->uncore, CUR_BUF_CFG(pipe));
4301 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4302 return;
4303 }
4304
4305 val = intel_uncore_read(&dev_priv->uncore, PLANE_CTL(pipe, plane_id));
4306
4307 /* No DDB allocated for disabled planes */
4308 if (val & PLANE_CTL_ENABLE)
4309 fourcc = skl_format_to_fourcc(val & PLANE_CTL_FORMAT_MASK,
4310 val & PLANE_CTL_ORDER_RGBX,
4311 val & PLANE_CTL_ALPHA_MASK);
4312
4313 if (DISPLAY_VER(dev_priv) >= 11) {
4314 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4315 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4316 } else {
4317 val = intel_uncore_read(&dev_priv->uncore, PLANE_BUF_CFG(pipe, plane_id));
4318 val2 = intel_uncore_read(&dev_priv->uncore, PLANE_NV12_BUF_CFG(pipe, plane_id));
4319
4320 if (fourcc &&
4321 drm_format_info_is_yuv_semiplanar(drm_format_info(fourcc)))
4322 swap(val, val2);
4323
4324 skl_ddb_entry_init_from_hw(dev_priv, ddb_y, val);
4325 skl_ddb_entry_init_from_hw(dev_priv, ddb_uv, val2);
4326 }
4327}
4328
4329void skl_pipe_ddb_get_hw_state(struct intel_crtc *crtc,
4330 struct skl_ddb_entry *ddb_y,
4331 struct skl_ddb_entry *ddb_uv)
4332{
4333 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4334 enum intel_display_power_domain power_domain;
4335 enum pipe pipe = crtc->pipe;
4336 intel_wakeref_t wakeref;
4337 enum plane_id plane_id;
4338
4339 power_domain = POWER_DOMAIN_PIPE(pipe);
4340 wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4341 if (!wakeref)
4342 return;
4343
4344 for_each_plane_id_on_crtc(crtc, plane_id)
4345 skl_ddb_get_hw_plane_state(dev_priv, pipe,
4346 plane_id,
4347 &ddb_y[plane_id],
4348 &ddb_uv[plane_id]);
4349
4350 intel_display_power_put(dev_priv, power_domain, wakeref);
4351}
4352
4353/*
4354 * Determines the downscale amount of a plane for the purposes of watermark calculations.
4355 * The bspec defines downscale amount as:
4356 *
4357 * """
4358 * Horizontal down scale amount = maximum[1, Horizontal source size /
4359 * Horizontal destination size]
4360 * Vertical down scale amount = maximum[1, Vertical source size /
4361 * Vertical destination size]
4362 * Total down scale amount = Horizontal down scale amount *
4363 * Vertical down scale amount
4364 * """
4365 *
4366 * Return value is provided in 16.16 fixed point form to retain fractional part.
4367 * Caller should take care of dividing & rounding off the value.
4368 */
4369static uint_fixed_16_16_t
4370skl_plane_downscale_amount(const struct intel_crtc_state *crtc_state,
4371 const struct intel_plane_state *plane_state)
4372{
4373 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4374 u32 src_w, src_h, dst_w, dst_h;
4375 uint_fixed_16_16_t fp_w_ratio, fp_h_ratio;
4376 uint_fixed_16_16_t downscale_h, downscale_w;
4377
4378 if (drm_WARN_ON(&dev_priv->drm,
4379 !intel_wm_plane_visible(crtc_state, plane_state)))
4380 return u32_to_fixed16(0);
4381
4382 /*
4383 * Src coordinates are already rotated by 270 degrees for
4384 * the 90/270 degree plane rotation cases (to match the
4385 * GTT mapping), hence no need to account for rotation here.
4386 *
4387 * n.b., src is 16.16 fixed point, dst is whole integer.
4388 */
4389 src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
4390 src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
4391 dst_w = drm_rect_width(&plane_state->uapi.dst);
4392 dst_h = drm_rect_height(&plane_state->uapi.dst);
4393
4394 fp_w_ratio = div_fixed16(src_w, dst_w);
4395 fp_h_ratio = div_fixed16(src_h, dst_h);
4396 downscale_w = max_fixed16(fp_w_ratio, u32_to_fixed16(1));
4397 downscale_h = max_fixed16(fp_h_ratio, u32_to_fixed16(1));
4398
4399 return mul_fixed16(downscale_w, downscale_h);
4400}
4401
4402struct dbuf_slice_conf_entry {
4403 u8 active_pipes;
4404 u8 dbuf_mask[I915_MAX_PIPES];
4405 bool join_mbus;
4406};
4407
4408/*
4409 * Table taken from Bspec 12716
4410 * Pipes do have some preferred DBuf slice affinity,
4411 * plus there are some hardcoded requirements on how
4412 * those should be distributed for multipipe scenarios.
4413 * For more DBuf slices algorithm can get even more messy
4414 * and less readable, so decided to use a table almost
4415 * as is from BSpec itself - that way it is at least easier
4416 * to compare, change and check.
4417 */
4418static const struct dbuf_slice_conf_entry icl_allowed_dbufs[] =
4419/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4420{
4421 {
4422 .active_pipes = BIT(PIPE_A),
4423 .dbuf_mask = {
4424 [PIPE_A] = BIT(DBUF_S1),
4425 },
4426 },
4427 {
4428 .active_pipes = BIT(PIPE_B),
4429 .dbuf_mask = {
4430 [PIPE_B] = BIT(DBUF_S1),
4431 },
4432 },
4433 {
4434 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4435 .dbuf_mask = {
4436 [PIPE_A] = BIT(DBUF_S1),
4437 [PIPE_B] = BIT(DBUF_S2),
4438 },
4439 },
4440 {
4441 .active_pipes = BIT(PIPE_C),
4442 .dbuf_mask = {
4443 [PIPE_C] = BIT(DBUF_S2),
4444 },
4445 },
4446 {
4447 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4448 .dbuf_mask = {
4449 [PIPE_A] = BIT(DBUF_S1),
4450 [PIPE_C] = BIT(DBUF_S2),
4451 },
4452 },
4453 {
4454 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4455 .dbuf_mask = {
4456 [PIPE_B] = BIT(DBUF_S1),
4457 [PIPE_C] = BIT(DBUF_S2),
4458 },
4459 },
4460 {
4461 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4462 .dbuf_mask = {
4463 [PIPE_A] = BIT(DBUF_S1),
4464 [PIPE_B] = BIT(DBUF_S1),
4465 [PIPE_C] = BIT(DBUF_S2),
4466 },
4467 },
4468 {}
4469};
4470
4471/*
4472 * Table taken from Bspec 49255
4473 * Pipes do have some preferred DBuf slice affinity,
4474 * plus there are some hardcoded requirements on how
4475 * those should be distributed for multipipe scenarios.
4476 * For more DBuf slices algorithm can get even more messy
4477 * and less readable, so decided to use a table almost
4478 * as is from BSpec itself - that way it is at least easier
4479 * to compare, change and check.
4480 */
4481static const struct dbuf_slice_conf_entry tgl_allowed_dbufs[] =
4482/* Autogenerated with igt/tools/intel_dbuf_map tool: */
4483{
4484 {
4485 .active_pipes = BIT(PIPE_A),
4486 .dbuf_mask = {
4487 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4488 },
4489 },
4490 {
4491 .active_pipes = BIT(PIPE_B),
4492 .dbuf_mask = {
4493 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2),
4494 },
4495 },
4496 {
4497 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4498 .dbuf_mask = {
4499 [PIPE_A] = BIT(DBUF_S2),
4500 [PIPE_B] = BIT(DBUF_S1),
4501 },
4502 },
4503 {
4504 .active_pipes = BIT(PIPE_C),
4505 .dbuf_mask = {
4506 [PIPE_C] = BIT(DBUF_S2) | BIT(DBUF_S1),
4507 },
4508 },
4509 {
4510 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4511 .dbuf_mask = {
4512 [PIPE_A] = BIT(DBUF_S1),
4513 [PIPE_C] = BIT(DBUF_S2),
4514 },
4515 },
4516 {
4517 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4518 .dbuf_mask = {
4519 [PIPE_B] = BIT(DBUF_S1),
4520 [PIPE_C] = BIT(DBUF_S2),
4521 },
4522 },
4523 {
4524 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4525 .dbuf_mask = {
4526 [PIPE_A] = BIT(DBUF_S1),
4527 [PIPE_B] = BIT(DBUF_S1),
4528 [PIPE_C] = BIT(DBUF_S2),
4529 },
4530 },
4531 {
4532 .active_pipes = BIT(PIPE_D),
4533 .dbuf_mask = {
4534 [PIPE_D] = BIT(DBUF_S2) | BIT(DBUF_S1),
4535 },
4536 },
4537 {
4538 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4539 .dbuf_mask = {
4540 [PIPE_A] = BIT(DBUF_S1),
4541 [PIPE_D] = BIT(DBUF_S2),
4542 },
4543 },
4544 {
4545 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4546 .dbuf_mask = {
4547 [PIPE_B] = BIT(DBUF_S1),
4548 [PIPE_D] = BIT(DBUF_S2),
4549 },
4550 },
4551 {
4552 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4553 .dbuf_mask = {
4554 [PIPE_A] = BIT(DBUF_S1),
4555 [PIPE_B] = BIT(DBUF_S1),
4556 [PIPE_D] = BIT(DBUF_S2),
4557 },
4558 },
4559 {
4560 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4561 .dbuf_mask = {
4562 [PIPE_C] = BIT(DBUF_S1),
4563 [PIPE_D] = BIT(DBUF_S2),
4564 },
4565 },
4566 {
4567 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4568 .dbuf_mask = {
4569 [PIPE_A] = BIT(DBUF_S1),
4570 [PIPE_C] = BIT(DBUF_S2),
4571 [PIPE_D] = BIT(DBUF_S2),
4572 },
4573 },
4574 {
4575 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4576 .dbuf_mask = {
4577 [PIPE_B] = BIT(DBUF_S1),
4578 [PIPE_C] = BIT(DBUF_S2),
4579 [PIPE_D] = BIT(DBUF_S2),
4580 },
4581 },
4582 {
4583 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4584 .dbuf_mask = {
4585 [PIPE_A] = BIT(DBUF_S1),
4586 [PIPE_B] = BIT(DBUF_S1),
4587 [PIPE_C] = BIT(DBUF_S2),
4588 [PIPE_D] = BIT(DBUF_S2),
4589 },
4590 },
4591 {}
4592};
4593
4594static const struct dbuf_slice_conf_entry adlp_allowed_dbufs[] = {
4595 {
4596 .active_pipes = BIT(PIPE_A),
4597 .dbuf_mask = {
4598 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
4599 },
4600 .join_mbus = true,
4601 },
4602 {
4603 .active_pipes = BIT(PIPE_B),
4604 .dbuf_mask = {
4605 [PIPE_B] = BIT(DBUF_S1) | BIT(DBUF_S2) | BIT(DBUF_S3) | BIT(DBUF_S4),
4606 },
4607 .join_mbus = true,
4608 },
4609 {
4610 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B),
4611 .dbuf_mask = {
4612 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4613 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4614 },
4615 },
4616 {
4617 .active_pipes = BIT(PIPE_C),
4618 .dbuf_mask = {
4619 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4620 },
4621 },
4622 {
4623 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C),
4624 .dbuf_mask = {
4625 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4626 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4627 },
4628 },
4629 {
4630 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C),
4631 .dbuf_mask = {
4632 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4633 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4634 },
4635 },
4636 {
4637 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C),
4638 .dbuf_mask = {
4639 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4640 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4641 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4642 },
4643 },
4644 {
4645 .active_pipes = BIT(PIPE_D),
4646 .dbuf_mask = {
4647 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4648 },
4649 },
4650 {
4651 .active_pipes = BIT(PIPE_A) | BIT(PIPE_D),
4652 .dbuf_mask = {
4653 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4654 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4655 },
4656 },
4657 {
4658 .active_pipes = BIT(PIPE_B) | BIT(PIPE_D),
4659 .dbuf_mask = {
4660 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4661 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4662 },
4663 },
4664 {
4665 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_D),
4666 .dbuf_mask = {
4667 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4668 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4669 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4670 },
4671 },
4672 {
4673 .active_pipes = BIT(PIPE_C) | BIT(PIPE_D),
4674 .dbuf_mask = {
4675 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4676 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4677 },
4678 },
4679 {
4680 .active_pipes = BIT(PIPE_A) | BIT(PIPE_C) | BIT(PIPE_D),
4681 .dbuf_mask = {
4682 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4683 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4684 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4685 },
4686 },
4687 {
4688 .active_pipes = BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4689 .dbuf_mask = {
4690 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4691 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4692 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4693 },
4694 },
4695 {
4696 .active_pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D),
4697 .dbuf_mask = {
4698 [PIPE_A] = BIT(DBUF_S1) | BIT(DBUF_S2),
4699 [PIPE_B] = BIT(DBUF_S3) | BIT(DBUF_S4),
4700 [PIPE_C] = BIT(DBUF_S3) | BIT(DBUF_S4),
4701 [PIPE_D] = BIT(DBUF_S1) | BIT(DBUF_S2),
4702 },
4703 },
4704 {}
4705
4706};
4707
4708static bool check_mbus_joined(u8 active_pipes,
4709 const struct dbuf_slice_conf_entry *dbuf_slices)
4710{
4711 int i;
4712
4713 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
4714 if (dbuf_slices[i].active_pipes == active_pipes)
4715 return dbuf_slices[i].join_mbus;
4716 }
4717 return false;
4718}
4719
4720static bool adlp_check_mbus_joined(u8 active_pipes)
4721{
4722 return check_mbus_joined(active_pipes, adlp_allowed_dbufs);
4723}
4724
4725static u8 compute_dbuf_slices(enum pipe pipe, u8 active_pipes,
4726 const struct dbuf_slice_conf_entry *dbuf_slices)
4727{
4728 int i;
4729
4730 for (i = 0; i < dbuf_slices[i].active_pipes; i++) {
4731 if (dbuf_slices[i].active_pipes == active_pipes)
4732 return dbuf_slices[i].dbuf_mask[pipe];
4733 }
4734 return 0;
4735}
4736
4737/*
4738 * This function finds an entry with same enabled pipe configuration and
4739 * returns correspondent DBuf slice mask as stated in BSpec for particular
4740 * platform.
4741 */
4742static u8 icl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
4743{
4744 /*
4745 * FIXME: For ICL this is still a bit unclear as prev BSpec revision
4746 * required calculating "pipe ratio" in order to determine
4747 * if one or two slices can be used for single pipe configurations
4748 * as additional constraint to the existing table.
4749 * However based on recent info, it should be not "pipe ratio"
4750 * but rather ratio between pixel_rate and cdclk with additional
4751 * constants, so for now we are using only table until this is
4752 * clarified. Also this is the reason why crtc_state param is
4753 * still here - we will need it once those additional constraints
4754 * pop up.
4755 */
4756 return compute_dbuf_slices(pipe, active_pipes, icl_allowed_dbufs);
4757}
4758
4759static u8 tgl_compute_dbuf_slices(enum pipe pipe, u8 active_pipes)
4760{
4761 return compute_dbuf_slices(pipe, active_pipes, tgl_allowed_dbufs);
4762}
4763
4764static u32 adlp_compute_dbuf_slices(enum pipe pipe, u32 active_pipes)
4765{
4766 return compute_dbuf_slices(pipe, active_pipes, adlp_allowed_dbufs);
4767}
4768
4769static u8 skl_compute_dbuf_slices(struct intel_crtc *crtc, u8 active_pipes)
4770{
4771 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4772 enum pipe pipe = crtc->pipe;
4773
4774 if (IS_ALDERLAKE_P(dev_priv))
4775 return adlp_compute_dbuf_slices(pipe, active_pipes);
4776 else if (DISPLAY_VER(dev_priv) == 12)
4777 return tgl_compute_dbuf_slices(pipe, active_pipes);
4778 else if (DISPLAY_VER(dev_priv) == 11)
4779 return icl_compute_dbuf_slices(pipe, active_pipes);
4780 /*
4781 * For anything else just return one slice yet.
4782 * Should be extended for other platforms.
4783 */
4784 return active_pipes & BIT(pipe) ? BIT(DBUF_S1) : 0;
4785}
4786
4787static u64
4788skl_plane_relative_data_rate(const struct intel_crtc_state *crtc_state,
4789 const struct intel_plane_state *plane_state,
4790 int color_plane)
4791{
4792 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
4793 const struct drm_framebuffer *fb = plane_state->hw.fb;
4794 u32 data_rate;
4795 u32 width = 0, height = 0;
4796 uint_fixed_16_16_t down_scale_amount;
4797 u64 rate;
4798
4799 if (!plane_state->uapi.visible)
4800 return 0;
4801
4802 if (plane->id == PLANE_CURSOR)
4803 return 0;
4804
4805 if (color_plane == 1 &&
4806 !intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier))
4807 return 0;
4808
4809 /*
4810 * Src coordinates are already rotated by 270 degrees for
4811 * the 90/270 degree plane rotation cases (to match the
4812 * GTT mapping), hence no need to account for rotation here.
4813 */
4814 width = drm_rect_width(&plane_state->uapi.src) >> 16;
4815 height = drm_rect_height(&plane_state->uapi.src) >> 16;
4816
4817 /* UV plane does 1/2 pixel sub-sampling */
4818 if (color_plane == 1) {
4819 width /= 2;
4820 height /= 2;
4821 }
4822
4823 data_rate = width * height;
4824
4825 down_scale_amount = skl_plane_downscale_amount(crtc_state, plane_state);
4826
4827 rate = mul_round_up_u32_fixed16(data_rate, down_scale_amount);
4828
4829 rate *= fb->format->cpp[color_plane];
4830 return rate;
4831}
4832
4833static u64
4834skl_get_total_relative_data_rate(struct intel_atomic_state *state,
4835 struct intel_crtc *crtc)
4836{
4837 struct intel_crtc_state *crtc_state =
4838 intel_atomic_get_new_crtc_state(state, crtc);
4839 const struct intel_plane_state *plane_state;
4840 struct intel_plane *plane;
4841 u64 total_data_rate = 0;
4842 enum plane_id plane_id;
4843 int i;
4844
4845 /* Calculate and cache data rate for each plane */
4846 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4847 if (plane->pipe != crtc->pipe)
4848 continue;
4849
4850 plane_id = plane->id;
4851
4852 /* packed/y */
4853 crtc_state->plane_data_rate[plane_id] =
4854 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
4855
4856 /* uv-plane */
4857 crtc_state->uv_plane_data_rate[plane_id] =
4858 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4859 }
4860
4861 for_each_plane_id_on_crtc(crtc, plane_id) {
4862 total_data_rate += crtc_state->plane_data_rate[plane_id];
4863 total_data_rate += crtc_state->uv_plane_data_rate[plane_id];
4864 }
4865
4866 return total_data_rate;
4867}
4868
4869static u64
4870icl_get_total_relative_data_rate(struct intel_atomic_state *state,
4871 struct intel_crtc *crtc)
4872{
4873 struct intel_crtc_state *crtc_state =
4874 intel_atomic_get_new_crtc_state(state, crtc);
4875 const struct intel_plane_state *plane_state;
4876 struct intel_plane *plane;
4877 u64 total_data_rate = 0;
4878 enum plane_id plane_id;
4879 int i;
4880
4881 /* Calculate and cache data rate for each plane */
4882 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4883 if (plane->pipe != crtc->pipe)
4884 continue;
4885
4886 plane_id = plane->id;
4887
4888 if (!plane_state->planar_linked_plane) {
4889 crtc_state->plane_data_rate[plane_id] =
4890 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
4891 } else {
4892 enum plane_id y_plane_id;
4893
4894 /*
4895 * The slave plane might not iterate in
4896 * intel_atomic_crtc_state_for_each_plane_state(),
4897 * and needs the master plane state which may be
4898 * NULL if we try get_new_plane_state(), so we
4899 * always calculate from the master.
4900 */
4901 if (plane_state->planar_slave)
4902 continue;
4903
4904 /* Y plane rate is calculated on the slave */
4905 y_plane_id = plane_state->planar_linked_plane->id;
4906 crtc_state->plane_data_rate[y_plane_id] =
4907 skl_plane_relative_data_rate(crtc_state, plane_state, 0);
4908
4909 crtc_state->plane_data_rate[plane_id] =
4910 skl_plane_relative_data_rate(crtc_state, plane_state, 1);
4911 }
4912 }
4913
4914 for_each_plane_id_on_crtc(crtc, plane_id)
4915 total_data_rate += crtc_state->plane_data_rate[plane_id];
4916
4917 return total_data_rate;
4918}
4919
4920const struct skl_wm_level *
4921skl_plane_wm_level(const struct skl_pipe_wm *pipe_wm,
4922 enum plane_id plane_id,
4923 int level)
4924{
4925 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4926
4927 if (level == 0 && pipe_wm->use_sagv_wm)
4928 return &wm->sagv.wm0;
4929
4930 return &wm->wm[level];
4931}
4932
4933const struct skl_wm_level *
4934skl_plane_trans_wm(const struct skl_pipe_wm *pipe_wm,
4935 enum plane_id plane_id)
4936{
4937 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
4938
4939 if (pipe_wm->use_sagv_wm)
4940 return &wm->sagv.trans_wm;
4941
4942 return &wm->trans_wm;
4943}
4944
4945/*
4946 * We only disable the watermarks for each plane if
4947 * they exceed the ddb allocation of said plane. This
4948 * is done so that we don't end up touching cursor
4949 * watermarks needlessly when some other plane reduces
4950 * our max possible watermark level.
4951 *
4952 * Bspec has this to say about the PLANE_WM enable bit:
4953 * "All the watermarks at this level for all enabled
4954 * planes must be enabled before the level will be used."
4955 * So this is actually safe to do.
4956 */
4957static void
4958skl_check_wm_level(struct skl_wm_level *wm, u64 total)
4959{
4960 if (wm->min_ddb_alloc > total)
4961 memset(wm, 0, sizeof(*wm));
4962}
4963
4964static void
4965skl_check_nv12_wm_level(struct skl_wm_level *wm, struct skl_wm_level *uv_wm,
4966 u64 total, u64 uv_total)
4967{
4968 if (wm->min_ddb_alloc > total ||
4969 uv_wm->min_ddb_alloc > uv_total) {
4970 memset(wm, 0, sizeof(*wm));
4971 memset(uv_wm, 0, sizeof(*uv_wm));
4972 }
4973}
4974
4975static int
4976skl_allocate_plane_ddb(struct intel_atomic_state *state,
4977 struct intel_crtc *crtc)
4978{
4979 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4980 struct intel_crtc_state *crtc_state =
4981 intel_atomic_get_new_crtc_state(state, crtc);
4982 const struct intel_dbuf_state *dbuf_state =
4983 intel_atomic_get_new_dbuf_state(state);
4984 const struct skl_ddb_entry *alloc = &dbuf_state->ddb[crtc->pipe];
4985 int num_active = hweight8(dbuf_state->active_pipes);
4986 u16 alloc_size, start = 0;
4987 u16 total[I915_MAX_PLANES] = {};
4988 u16 uv_total[I915_MAX_PLANES] = {};
4989 u64 total_data_rate;
4990 enum plane_id plane_id;
4991 u32 blocks;
4992 int level;
4993
4994 /* Clear the partitioning for disabled planes. */
4995 memset(crtc_state->wm.skl.plane_ddb_y, 0, sizeof(crtc_state->wm.skl.plane_ddb_y));
4996 memset(crtc_state->wm.skl.plane_ddb_uv, 0, sizeof(crtc_state->wm.skl.plane_ddb_uv));
4997
4998 if (!crtc_state->hw.active)
4999 return 0;
5000
5001 if (DISPLAY_VER(dev_priv) >= 11)
5002 total_data_rate =
5003 icl_get_total_relative_data_rate(state, crtc);
5004 else
5005 total_data_rate =
5006 skl_get_total_relative_data_rate(state, crtc);
5007
5008 alloc_size = skl_ddb_entry_size(alloc);
5009 if (alloc_size == 0)
5010 return 0;
5011
5012 /* Allocate fixed number of blocks for cursor. */
5013 total[PLANE_CURSOR] = skl_cursor_allocation(crtc_state, num_active);
5014 alloc_size -= total[PLANE_CURSOR];
5015 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].start =
5016 alloc->end - total[PLANE_CURSOR];
5017 crtc_state->wm.skl.plane_ddb_y[PLANE_CURSOR].end = alloc->end;
5018
5019 if (total_data_rate == 0)
5020 return 0;
5021
5022 /*
5023 * Find the highest watermark level for which we can satisfy the block
5024 * requirement of active planes.
5025 */
5026 for (level = ilk_wm_max_level(dev_priv); level >= 0; level--) {
5027 blocks = 0;
5028 for_each_plane_id_on_crtc(crtc, plane_id) {
5029 const struct skl_plane_wm *wm =
5030 &crtc_state->wm.skl.optimal.planes[plane_id];
5031
5032 if (plane_id == PLANE_CURSOR) {
5033 if (wm->wm[level].min_ddb_alloc > total[PLANE_CURSOR]) {
5034 drm_WARN_ON(&dev_priv->drm,
5035 wm->wm[level].min_ddb_alloc != U16_MAX);
5036 blocks = U32_MAX;
5037 break;
5038 }
5039 continue;
5040 }
5041
5042 blocks += wm->wm[level].min_ddb_alloc;
5043 blocks += wm->uv_wm[level].min_ddb_alloc;
5044 }
5045
5046 if (blocks <= alloc_size) {
5047 alloc_size -= blocks;
5048 break;
5049 }
5050 }
5051
5052 if (level < 0) {
5053 drm_dbg_kms(&dev_priv->drm,
5054 "Requested display configuration exceeds system DDB limitations");
5055 drm_dbg_kms(&dev_priv->drm, "minimum required %d/%d\n",
5056 blocks, alloc_size);
5057 return -EINVAL;
5058 }
5059
5060 /*
5061 * Grant each plane the blocks it requires at the highest achievable
5062 * watermark level, plus an extra share of the leftover blocks
5063 * proportional to its relative data rate.
5064 */
5065 for_each_plane_id_on_crtc(crtc, plane_id) {
5066 const struct skl_plane_wm *wm =
5067 &crtc_state->wm.skl.optimal.planes[plane_id];
5068 u64 rate;
5069 u16 extra;
5070
5071 if (plane_id == PLANE_CURSOR)
5072 continue;
5073
5074 /*
5075 * We've accounted for all active planes; remaining planes are
5076 * all disabled.
5077 */
5078 if (total_data_rate == 0)
5079 break;
5080
5081 rate = crtc_state->plane_data_rate[plane_id];
5082 extra = min_t(u16, alloc_size,
5083 DIV64_U64_ROUND_UP(alloc_size * rate,
5084 total_data_rate));
5085 total[plane_id] = wm->wm[level].min_ddb_alloc + extra;
5086 alloc_size -= extra;
5087 total_data_rate -= rate;
5088
5089 if (total_data_rate == 0)
5090 break;
5091
5092 rate = crtc_state->uv_plane_data_rate[plane_id];
5093 extra = min_t(u16, alloc_size,
5094 DIV64_U64_ROUND_UP(alloc_size * rate,
5095 total_data_rate));
5096 uv_total[plane_id] = wm->uv_wm[level].min_ddb_alloc + extra;
5097 alloc_size -= extra;
5098 total_data_rate -= rate;
5099 }
5100 drm_WARN_ON(&dev_priv->drm, alloc_size != 0 || total_data_rate != 0);
5101
5102 /* Set the actual DDB start/end points for each plane */
5103 start = alloc->start;
5104 for_each_plane_id_on_crtc(crtc, plane_id) {
5105 struct skl_ddb_entry *plane_alloc =
5106 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5107 struct skl_ddb_entry *uv_plane_alloc =
5108 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
5109
5110 if (plane_id == PLANE_CURSOR)
5111 continue;
5112
5113 /* Gen11+ uses a separate plane for UV watermarks */
5114 drm_WARN_ON(&dev_priv->drm,
5115 DISPLAY_VER(dev_priv) >= 11 && uv_total[plane_id]);
5116
5117 /* Leave disabled planes at (0,0) */
5118 if (total[plane_id]) {
5119 plane_alloc->start = start;
5120 start += total[plane_id];
5121 plane_alloc->end = start;
5122 }
5123
5124 if (uv_total[plane_id]) {
5125 uv_plane_alloc->start = start;
5126 start += uv_total[plane_id];
5127 uv_plane_alloc->end = start;
5128 }
5129 }
5130
5131 /*
5132 * When we calculated watermark values we didn't know how high
5133 * of a level we'd actually be able to hit, so we just marked
5134 * all levels as "enabled." Go back now and disable the ones
5135 * that aren't actually possible.
5136 */
5137 for (level++; level <= ilk_wm_max_level(dev_priv); level++) {
5138 for_each_plane_id_on_crtc(crtc, plane_id) {
5139 struct skl_plane_wm *wm =
5140 &crtc_state->wm.skl.optimal.planes[plane_id];
5141
5142 skl_check_nv12_wm_level(&wm->wm[level], &wm->uv_wm[level],
5143 total[plane_id], uv_total[plane_id]);
5144
5145 /*
5146 * Wa_1408961008:icl, ehl
5147 * Underruns with WM1+ disabled
5148 */
5149 if (DISPLAY_VER(dev_priv) == 11 &&
5150 level == 1 && wm->wm[0].enable) {
5151 wm->wm[level].blocks = wm->wm[0].blocks;
5152 wm->wm[level].lines = wm->wm[0].lines;
5153 wm->wm[level].ignore_lines = wm->wm[0].ignore_lines;
5154 }
5155 }
5156 }
5157
5158 /*
5159 * Go back and disable the transition and SAGV watermarks
5160 * if it turns out we don't have enough DDB blocks for them.
5161 */
5162 for_each_plane_id_on_crtc(crtc, plane_id) {
5163 struct skl_plane_wm *wm =
5164 &crtc_state->wm.skl.optimal.planes[plane_id];
5165
5166 skl_check_wm_level(&wm->trans_wm, total[plane_id]);
5167 skl_check_wm_level(&wm->sagv.wm0, total[plane_id]);
5168 skl_check_wm_level(&wm->sagv.trans_wm, total[plane_id]);
5169 }
5170
5171 return 0;
5172}
5173
5174/*
5175 * The max latency should be 257 (max the punit can code is 255 and we add 2us
5176 * for the read latency) and cpp should always be <= 8, so that
5177 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
5178 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
5179*/
5180static uint_fixed_16_16_t
5181skl_wm_method1(const struct drm_i915_private *dev_priv, u32 pixel_rate,
5182 u8 cpp, u32 latency, u32 dbuf_block_size)
5183{
5184 u32 wm_intermediate_val;
5185 uint_fixed_16_16_t ret;
5186
5187 if (latency == 0)
5188 return FP_16_16_MAX;
5189
5190 wm_intermediate_val = latency * pixel_rate * cpp;
5191 ret = div_fixed16(wm_intermediate_val, 1000 * dbuf_block_size);
5192
5193 if (DISPLAY_VER(dev_priv) >= 10)
5194 ret = add_fixed16_u32(ret, 1);
5195
5196 return ret;
5197}
5198
5199static uint_fixed_16_16_t
5200skl_wm_method2(u32 pixel_rate, u32 pipe_htotal, u32 latency,
5201 uint_fixed_16_16_t plane_blocks_per_line)
5202{
5203 u32 wm_intermediate_val;
5204 uint_fixed_16_16_t ret;
5205
5206 if (latency == 0)
5207 return FP_16_16_MAX;
5208
5209 wm_intermediate_val = latency * pixel_rate;
5210 wm_intermediate_val = DIV_ROUND_UP(wm_intermediate_val,
5211 pipe_htotal * 1000);
5212 ret = mul_u32_fixed16(wm_intermediate_val, plane_blocks_per_line);
5213 return ret;
5214}
5215
5216static uint_fixed_16_16_t
5217intel_get_linetime_us(const struct intel_crtc_state *crtc_state)
5218{
5219 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5220 u32 pixel_rate;
5221 u32 crtc_htotal;
5222 uint_fixed_16_16_t linetime_us;
5223
5224 if (!crtc_state->hw.active)
5225 return u32_to_fixed16(0);
5226
5227 pixel_rate = crtc_state->pixel_rate;
5228
5229 if (drm_WARN_ON(&dev_priv->drm, pixel_rate == 0))
5230 return u32_to_fixed16(0);
5231
5232 crtc_htotal = crtc_state->hw.pipe_mode.crtc_htotal;
5233 linetime_us = div_fixed16(crtc_htotal * 1000, pixel_rate);
5234
5235 return linetime_us;
5236}
5237
5238static int
5239skl_compute_wm_params(const struct intel_crtc_state *crtc_state,
5240 int width, const struct drm_format_info *format,
5241 u64 modifier, unsigned int rotation,
5242 u32 plane_pixel_rate, struct skl_wm_params *wp,
5243 int color_plane)
5244{
5245 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5246 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5247 u32 interm_pbpl;
5248
5249 /* only planar format has two planes */
5250 if (color_plane == 1 &&
5251 !intel_format_info_is_yuv_semiplanar(format, modifier)) {
5252 drm_dbg_kms(&dev_priv->drm,
5253 "Non planar format have single plane\n");
5254 return -EINVAL;
5255 }
5256
5257 wp->y_tiled = modifier == I915_FORMAT_MOD_Y_TILED ||
5258 modifier == I915_FORMAT_MOD_Yf_TILED ||
5259 modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5260 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5261 wp->x_tiled = modifier == I915_FORMAT_MOD_X_TILED;
5262 wp->rc_surface = modifier == I915_FORMAT_MOD_Y_TILED_CCS ||
5263 modifier == I915_FORMAT_MOD_Yf_TILED_CCS;
5264 wp->is_planar = intel_format_info_is_yuv_semiplanar(format, modifier);
5265
5266 wp->width = width;
5267 if (color_plane == 1 && wp->is_planar)
5268 wp->width /= 2;
5269
5270 wp->cpp = format->cpp[color_plane];
5271 wp->plane_pixel_rate = plane_pixel_rate;
5272
5273 if (DISPLAY_VER(dev_priv) >= 11 &&
5274 modifier == I915_FORMAT_MOD_Yf_TILED && wp->cpp == 1)
5275 wp->dbuf_block_size = 256;
5276 else
5277 wp->dbuf_block_size = 512;
5278
5279 if (drm_rotation_90_or_270(rotation)) {
5280 switch (wp->cpp) {
5281 case 1:
5282 wp->y_min_scanlines = 16;
5283 break;
5284 case 2:
5285 wp->y_min_scanlines = 8;
5286 break;
5287 case 4:
5288 wp->y_min_scanlines = 4;
5289 break;
5290 default:
5291 MISSING_CASE(wp->cpp);
5292 return -EINVAL;
5293 }
5294 } else {
5295 wp->y_min_scanlines = 4;
5296 }
5297
5298 if (skl_needs_memory_bw_wa(dev_priv))
5299 wp->y_min_scanlines *= 2;
5300
5301 wp->plane_bytes_per_line = wp->width * wp->cpp;
5302 if (wp->y_tiled) {
5303 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line *
5304 wp->y_min_scanlines,
5305 wp->dbuf_block_size);
5306
5307 if (DISPLAY_VER(dev_priv) >= 10)
5308 interm_pbpl++;
5309
5310 wp->plane_blocks_per_line = div_fixed16(interm_pbpl,
5311 wp->y_min_scanlines);
5312 } else {
5313 interm_pbpl = DIV_ROUND_UP(wp->plane_bytes_per_line,
5314 wp->dbuf_block_size);
5315
5316 if (!wp->x_tiled || DISPLAY_VER(dev_priv) >= 10)
5317 interm_pbpl++;
5318
5319 wp->plane_blocks_per_line = u32_to_fixed16(interm_pbpl);
5320 }
5321
5322 wp->y_tile_minimum = mul_u32_fixed16(wp->y_min_scanlines,
5323 wp->plane_blocks_per_line);
5324
5325 wp->linetime_us = fixed16_to_u32_round_up(
5326 intel_get_linetime_us(crtc_state));
5327
5328 return 0;
5329}
5330
5331static int
5332skl_compute_plane_wm_params(const struct intel_crtc_state *crtc_state,
5333 const struct intel_plane_state *plane_state,
5334 struct skl_wm_params *wp, int color_plane)
5335{
5336 const struct drm_framebuffer *fb = plane_state->hw.fb;
5337 int width;
5338
5339 /*
5340 * Src coordinates are already rotated by 270 degrees for
5341 * the 90/270 degree plane rotation cases (to match the
5342 * GTT mapping), hence no need to account for rotation here.
5343 */
5344 width = drm_rect_width(&plane_state->uapi.src) >> 16;
5345
5346 return skl_compute_wm_params(crtc_state, width,
5347 fb->format, fb->modifier,
5348 plane_state->hw.rotation,
5349 intel_plane_pixel_rate(crtc_state, plane_state),
5350 wp, color_plane);
5351}
5352
5353static bool skl_wm_has_lines(struct drm_i915_private *dev_priv, int level)
5354{
5355 if (DISPLAY_VER(dev_priv) >= 10)
5356 return true;
5357
5358 /* The number of lines are ignored for the level 0 watermark. */
5359 return level > 0;
5360}
5361
5362static int skl_wm_max_lines(struct drm_i915_private *dev_priv)
5363{
5364 if (DISPLAY_VER(dev_priv) >= 13)
5365 return 255;
5366 else
5367 return 31;
5368}
5369
5370static void skl_compute_plane_wm(const struct intel_crtc_state *crtc_state,
5371 int level,
5372 unsigned int latency,
5373 const struct skl_wm_params *wp,
5374 const struct skl_wm_level *result_prev,
5375 struct skl_wm_level *result /* out */)
5376{
5377 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5378 uint_fixed_16_16_t method1, method2;
5379 uint_fixed_16_16_t selected_result;
5380 u32 blocks, lines, min_ddb_alloc = 0;
5381
5382 if (latency == 0) {
5383 /* reject it */
5384 result->min_ddb_alloc = U16_MAX;
5385 return;
5386 }
5387
5388 /*
5389 * WaIncreaseLatencyIPCEnabled: kbl,cfl
5390 * Display WA #1141: kbl,cfl
5391 */
5392 if ((IS_KABYLAKE(dev_priv) ||
5393 IS_COFFEELAKE(dev_priv) ||
5394 IS_COMETLAKE(dev_priv)) &&
5395 dev_priv->ipc_enabled)
5396 latency += 4;
5397
5398 if (skl_needs_memory_bw_wa(dev_priv) && wp->x_tiled)
5399 latency += 15;
5400
5401 method1 = skl_wm_method1(dev_priv, wp->plane_pixel_rate,
5402 wp->cpp, latency, wp->dbuf_block_size);
5403 method2 = skl_wm_method2(wp->plane_pixel_rate,
5404 crtc_state->hw.pipe_mode.crtc_htotal,
5405 latency,
5406 wp->plane_blocks_per_line);
5407
5408 if (wp->y_tiled) {
5409 selected_result = max_fixed16(method2, wp->y_tile_minimum);
5410 } else {
5411 if ((wp->cpp * crtc_state->hw.pipe_mode.crtc_htotal /
5412 wp->dbuf_block_size < 1) &&
5413 (wp->plane_bytes_per_line / wp->dbuf_block_size < 1)) {
5414 selected_result = method2;
5415 } else if (latency >= wp->linetime_us) {
5416 if (DISPLAY_VER(dev_priv) == 9)
5417 selected_result = min_fixed16(method1, method2);
5418 else
5419 selected_result = method2;
5420 } else {
5421 selected_result = method1;
5422 }
5423 }
5424
5425 blocks = fixed16_to_u32_round_up(selected_result) + 1;
5426 lines = div_round_up_fixed16(selected_result,
5427 wp->plane_blocks_per_line);
5428
5429 if (DISPLAY_VER(dev_priv) == 9) {
5430 /* Display WA #1125: skl,bxt,kbl */
5431 if (level == 0 && wp->rc_surface)
5432 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5433
5434 /* Display WA #1126: skl,bxt,kbl */
5435 if (level >= 1 && level <= 7) {
5436 if (wp->y_tiled) {
5437 blocks += fixed16_to_u32_round_up(wp->y_tile_minimum);
5438 lines += wp->y_min_scanlines;
5439 } else {
5440 blocks++;
5441 }
5442
5443 /*
5444 * Make sure result blocks for higher latency levels are
5445 * atleast as high as level below the current level.
5446 * Assumption in DDB algorithm optimization for special
5447 * cases. Also covers Display WA #1125 for RC.
5448 */
5449 if (result_prev->blocks > blocks)
5450 blocks = result_prev->blocks;
5451 }
5452 }
5453
5454 if (DISPLAY_VER(dev_priv) >= 11) {
5455 if (wp->y_tiled) {
5456 int extra_lines;
5457
5458 if (lines % wp->y_min_scanlines == 0)
5459 extra_lines = wp->y_min_scanlines;
5460 else
5461 extra_lines = wp->y_min_scanlines * 2 -
5462 lines % wp->y_min_scanlines;
5463
5464 min_ddb_alloc = mul_round_up_u32_fixed16(lines + extra_lines,
5465 wp->plane_blocks_per_line);
5466 } else {
5467 min_ddb_alloc = blocks + DIV_ROUND_UP(blocks, 10);
5468 }
5469 }
5470
5471 if (!skl_wm_has_lines(dev_priv, level))
5472 lines = 0;
5473
5474 if (lines > skl_wm_max_lines(dev_priv)) {
5475 /* reject it */
5476 result->min_ddb_alloc = U16_MAX;
5477 return;
5478 }
5479
5480 /*
5481 * If lines is valid, assume we can use this watermark level
5482 * for now. We'll come back and disable it after we calculate the
5483 * DDB allocation if it turns out we don't actually have enough
5484 * blocks to satisfy it.
5485 */
5486 result->blocks = blocks;
5487 result->lines = lines;
5488 /* Bspec says: value >= plane ddb allocation -> invalid, hence the +1 here */
5489 result->min_ddb_alloc = max(min_ddb_alloc, blocks) + 1;
5490 result->enable = true;
5491
5492 if (DISPLAY_VER(dev_priv) < 12)
5493 result->can_sagv = latency >= dev_priv->sagv_block_time_us;
5494}
5495
5496static void
5497skl_compute_wm_levels(const struct intel_crtc_state *crtc_state,
5498 const struct skl_wm_params *wm_params,
5499 struct skl_wm_level *levels)
5500{
5501 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5502 int level, max_level = ilk_wm_max_level(dev_priv);
5503 struct skl_wm_level *result_prev = &levels[0];
5504
5505 for (level = 0; level <= max_level; level++) {
5506 struct skl_wm_level *result = &levels[level];
5507 unsigned int latency = dev_priv->wm.skl_latency[level];
5508
5509 skl_compute_plane_wm(crtc_state, level, latency,
5510 wm_params, result_prev, result);
5511
5512 result_prev = result;
5513 }
5514}
5515
5516static void tgl_compute_sagv_wm(const struct intel_crtc_state *crtc_state,
5517 const struct skl_wm_params *wm_params,
5518 struct skl_plane_wm *plane_wm)
5519{
5520 struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5521 struct skl_wm_level *sagv_wm = &plane_wm->sagv.wm0;
5522 struct skl_wm_level *levels = plane_wm->wm;
5523 unsigned int latency = dev_priv->wm.skl_latency[0] + dev_priv->sagv_block_time_us;
5524
5525 skl_compute_plane_wm(crtc_state, 0, latency,
5526 wm_params, &levels[0],
5527 sagv_wm);
5528}
5529
5530static void skl_compute_transition_wm(struct drm_i915_private *dev_priv,
5531 struct skl_wm_level *trans_wm,
5532 const struct skl_wm_level *wm0,
5533 const struct skl_wm_params *wp)
5534{
5535 u16 trans_min, trans_amount, trans_y_tile_min;
5536 u16 wm0_blocks, trans_offset, blocks;
5537
5538 /* Transition WM don't make any sense if ipc is disabled */
5539 if (!dev_priv->ipc_enabled)
5540 return;
5541
5542 /*
5543 * WaDisableTWM:skl,kbl,cfl,bxt
5544 * Transition WM are not recommended by HW team for GEN9
5545 */
5546 if (DISPLAY_VER(dev_priv) == 9)
5547 return;
5548
5549 if (DISPLAY_VER(dev_priv) >= 11)
5550 trans_min = 4;
5551 else
5552 trans_min = 14;
5553
5554 /* Display WA #1140: glk,cnl */
5555 if (DISPLAY_VER(dev_priv) == 10)
5556 trans_amount = 0;
5557 else
5558 trans_amount = 10; /* This is configurable amount */
5559
5560 trans_offset = trans_min + trans_amount;
5561
5562 /*
5563 * The spec asks for Selected Result Blocks for wm0 (the real value),
5564 * not Result Blocks (the integer value). Pay attention to the capital
5565 * letters. The value wm_l0->blocks is actually Result Blocks, but
5566 * since Result Blocks is the ceiling of Selected Result Blocks plus 1,
5567 * and since we later will have to get the ceiling of the sum in the
5568 * transition watermarks calculation, we can just pretend Selected
5569 * Result Blocks is Result Blocks minus 1 and it should work for the
5570 * current platforms.
5571 */
5572 wm0_blocks = wm0->blocks - 1;
5573
5574 if (wp->y_tiled) {
5575 trans_y_tile_min =
5576 (u16)mul_round_up_u32_fixed16(2, wp->y_tile_minimum);
5577 blocks = max(wm0_blocks, trans_y_tile_min) + trans_offset;
5578 } else {
5579 blocks = wm0_blocks + trans_offset;
5580 }
5581 blocks++;
5582
5583 /*
5584 * Just assume we can enable the transition watermark. After
5585 * computing the DDB we'll come back and disable it if that
5586 * assumption turns out to be false.
5587 */
5588 trans_wm->blocks = blocks;
5589 trans_wm->min_ddb_alloc = max_t(u16, wm0->min_ddb_alloc, blocks + 1);
5590 trans_wm->enable = true;
5591}
5592
5593static int skl_build_plane_wm_single(struct intel_crtc_state *crtc_state,
5594 const struct intel_plane_state *plane_state,
5595 enum plane_id plane_id, int color_plane)
5596{
5597 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5598 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5599 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5600 struct skl_wm_params wm_params;
5601 int ret;
5602
5603 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
5604 &wm_params, color_plane);
5605 if (ret)
5606 return ret;
5607
5608 skl_compute_wm_levels(crtc_state, &wm_params, wm->wm);
5609
5610 skl_compute_transition_wm(dev_priv, &wm->trans_wm,
5611 &wm->wm[0], &wm_params);
5612
5613 if (DISPLAY_VER(dev_priv) >= 12) {
5614 tgl_compute_sagv_wm(crtc_state, &wm_params, wm);
5615
5616 skl_compute_transition_wm(dev_priv, &wm->sagv.trans_wm,
5617 &wm->sagv.wm0, &wm_params);
5618 }
5619
5620 return 0;
5621}
5622
5623static int skl_build_plane_wm_uv(struct intel_crtc_state *crtc_state,
5624 const struct intel_plane_state *plane_state,
5625 enum plane_id plane_id)
5626{
5627 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5628 struct skl_wm_params wm_params;
5629 int ret;
5630
5631 wm->is_planar = true;
5632
5633 /* uv plane watermarks must also be validated for NV12/Planar */
5634 ret = skl_compute_plane_wm_params(crtc_state, plane_state,
5635 &wm_params, 1);
5636 if (ret)
5637 return ret;
5638
5639 skl_compute_wm_levels(crtc_state, &wm_params, wm->uv_wm);
5640
5641 return 0;
5642}
5643
5644static int skl_build_plane_wm(struct intel_crtc_state *crtc_state,
5645 const struct intel_plane_state *plane_state)
5646{
5647 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5648 enum plane_id plane_id = plane->id;
5649 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5650 const struct drm_framebuffer *fb = plane_state->hw.fb;
5651 int ret;
5652
5653 memset(wm, 0, sizeof(*wm));
5654
5655 if (!intel_wm_plane_visible(crtc_state, plane_state))
5656 return 0;
5657
5658 ret = skl_build_plane_wm_single(crtc_state, plane_state,
5659 plane_id, 0);
5660 if (ret)
5661 return ret;
5662
5663 if (fb->format->is_yuv && fb->format->num_planes > 1) {
5664 ret = skl_build_plane_wm_uv(crtc_state, plane_state,
5665 plane_id);
5666 if (ret)
5667 return ret;
5668 }
5669
5670 return 0;
5671}
5672
5673static int icl_build_plane_wm(struct intel_crtc_state *crtc_state,
5674 const struct intel_plane_state *plane_state)
5675{
5676 struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
5677 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5678 enum plane_id plane_id = plane->id;
5679 struct skl_plane_wm *wm = &crtc_state->wm.skl.raw.planes[plane_id];
5680 int ret;
5681
5682 /* Watermarks calculated in master */
5683 if (plane_state->planar_slave)
5684 return 0;
5685
5686 memset(wm, 0, sizeof(*wm));
5687
5688 if (plane_state->planar_linked_plane) {
5689 const struct drm_framebuffer *fb = plane_state->hw.fb;
5690 enum plane_id y_plane_id = plane_state->planar_linked_plane->id;
5691
5692 drm_WARN_ON(&dev_priv->drm,
5693 !intel_wm_plane_visible(crtc_state, plane_state));
5694 drm_WARN_ON(&dev_priv->drm, !fb->format->is_yuv ||
5695 fb->format->num_planes == 1);
5696
5697 ret = skl_build_plane_wm_single(crtc_state, plane_state,
5698 y_plane_id, 0);
5699 if (ret)
5700 return ret;
5701
5702 ret = skl_build_plane_wm_single(crtc_state, plane_state,
5703 plane_id, 1);
5704 if (ret)
5705 return ret;
5706 } else if (intel_wm_plane_visible(crtc_state, plane_state)) {
5707 ret = skl_build_plane_wm_single(crtc_state, plane_state,
5708 plane_id, 0);
5709 if (ret)
5710 return ret;
5711 }
5712
5713 return 0;
5714}
5715
5716static int skl_build_pipe_wm(struct intel_atomic_state *state,
5717 struct intel_crtc *crtc)
5718{
5719 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5720 struct intel_crtc_state *crtc_state =
5721 intel_atomic_get_new_crtc_state(state, crtc);
5722 const struct intel_plane_state *plane_state;
5723 struct intel_plane *plane;
5724 int ret, i;
5725
5726 for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5727 /*
5728 * FIXME should perhaps check {old,new}_plane_crtc->hw.crtc
5729 * instead but we don't populate that correctly for NV12 Y
5730 * planes so for now hack this.
5731 */
5732 if (plane->pipe != crtc->pipe)
5733 continue;
5734
5735 if (DISPLAY_VER(dev_priv) >= 11)
5736 ret = icl_build_plane_wm(crtc_state, plane_state);
5737 else
5738 ret = skl_build_plane_wm(crtc_state, plane_state);
5739 if (ret)
5740 return ret;
5741 }
5742
5743 crtc_state->wm.skl.optimal = crtc_state->wm.skl.raw;
5744
5745 return 0;
5746}
5747
5748static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
5749 i915_reg_t reg,
5750 const struct skl_ddb_entry *entry)
5751{
5752 if (entry->end)
5753 intel_de_write_fw(dev_priv, reg,
5754 (entry->end - 1) << 16 | entry->start);
5755 else
5756 intel_de_write_fw(dev_priv, reg, 0);
5757}
5758
5759static void skl_write_wm_level(struct drm_i915_private *dev_priv,
5760 i915_reg_t reg,
5761 const struct skl_wm_level *level)
5762{
5763 u32 val = 0;
5764
5765 if (level->enable)
5766 val |= PLANE_WM_EN;
5767 if (level->ignore_lines)
5768 val |= PLANE_WM_IGNORE_LINES;
5769 val |= level->blocks;
5770 val |= REG_FIELD_PREP(PLANE_WM_LINES_MASK, level->lines);
5771
5772 intel_de_write_fw(dev_priv, reg, val);
5773}
5774
5775void skl_write_plane_wm(struct intel_plane *plane,
5776 const struct intel_crtc_state *crtc_state)
5777{
5778 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5779 int level, max_level = ilk_wm_max_level(dev_priv);
5780 enum plane_id plane_id = plane->id;
5781 enum pipe pipe = plane->pipe;
5782 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5783 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5784 const struct skl_ddb_entry *ddb_y =
5785 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5786 const struct skl_ddb_entry *ddb_uv =
5787 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
5788
5789 for (level = 0; level <= max_level; level++)
5790 skl_write_wm_level(dev_priv, PLANE_WM(pipe, plane_id, level),
5791 skl_plane_wm_level(pipe_wm, plane_id, level));
5792
5793 skl_write_wm_level(dev_priv, PLANE_WM_TRANS(pipe, plane_id),
5794 skl_plane_trans_wm(pipe_wm, plane_id));
5795
5796 if (HAS_HW_SAGV_WM(dev_priv)) {
5797 skl_write_wm_level(dev_priv, PLANE_WM_SAGV(pipe, plane_id),
5798 &wm->sagv.wm0);
5799 skl_write_wm_level(dev_priv, PLANE_WM_SAGV_TRANS(pipe, plane_id),
5800 &wm->sagv.trans_wm);
5801 }
5802
5803 if (DISPLAY_VER(dev_priv) >= 11) {
5804 skl_ddb_entry_write(dev_priv,
5805 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5806 return;
5807 }
5808
5809 if (wm->is_planar)
5810 swap(ddb_y, ddb_uv);
5811
5812 skl_ddb_entry_write(dev_priv,
5813 PLANE_BUF_CFG(pipe, plane_id), ddb_y);
5814 skl_ddb_entry_write(dev_priv,
5815 PLANE_NV12_BUF_CFG(pipe, plane_id), ddb_uv);
5816}
5817
5818void skl_write_cursor_wm(struct intel_plane *plane,
5819 const struct intel_crtc_state *crtc_state)
5820{
5821 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
5822 int level, max_level = ilk_wm_max_level(dev_priv);
5823 enum plane_id plane_id = plane->id;
5824 enum pipe pipe = plane->pipe;
5825 const struct skl_pipe_wm *pipe_wm = &crtc_state->wm.skl.optimal;
5826 const struct skl_ddb_entry *ddb =
5827 &crtc_state->wm.skl.plane_ddb_y[plane_id];
5828
5829 for (level = 0; level <= max_level; level++)
5830 skl_write_wm_level(dev_priv, CUR_WM(pipe, level),
5831 skl_plane_wm_level(pipe_wm, plane_id, level));
5832
5833 skl_write_wm_level(dev_priv, CUR_WM_TRANS(pipe),
5834 skl_plane_trans_wm(pipe_wm, plane_id));
5835
5836 if (HAS_HW_SAGV_WM(dev_priv)) {
5837 const struct skl_plane_wm *wm = &pipe_wm->planes[plane_id];
5838
5839 skl_write_wm_level(dev_priv, CUR_WM_SAGV(pipe),
5840 &wm->sagv.wm0);
5841 skl_write_wm_level(dev_priv, CUR_WM_SAGV_TRANS(pipe),
5842 &wm->sagv.trans_wm);
5843 }
5844
5845 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe), ddb);
5846}
5847
5848bool skl_wm_level_equals(const struct skl_wm_level *l1,
5849 const struct skl_wm_level *l2)
5850{
5851 return l1->enable == l2->enable &&
5852 l1->ignore_lines == l2->ignore_lines &&
5853 l1->lines == l2->lines &&
5854 l1->blocks == l2->blocks;
5855}
5856
5857static bool skl_plane_wm_equals(struct drm_i915_private *dev_priv,
5858 const struct skl_plane_wm *wm1,
5859 const struct skl_plane_wm *wm2)
5860{
5861 int level, max_level = ilk_wm_max_level(dev_priv);
5862
5863 for (level = 0; level <= max_level; level++) {
5864 /*
5865 * We don't check uv_wm as the hardware doesn't actually
5866 * use it. It only gets used for calculating the required
5867 * ddb allocation.
5868 */
5869 if (!skl_wm_level_equals(&wm1->wm[level], &wm2->wm[level]))
5870 return false;
5871 }
5872
5873 return skl_wm_level_equals(&wm1->trans_wm, &wm2->trans_wm) &&
5874 skl_wm_level_equals(&wm1->sagv.wm0, &wm2->sagv.wm0) &&
5875 skl_wm_level_equals(&wm1->sagv.trans_wm, &wm2->sagv.trans_wm);
5876}
5877
5878static bool skl_ddb_entries_overlap(const struct skl_ddb_entry *a,
5879 const struct skl_ddb_entry *b)
5880{
5881 return a->start < b->end && b->start < a->end;
5882}
5883
5884static void skl_ddb_entry_union(struct skl_ddb_entry *a,
5885 const struct skl_ddb_entry *b)
5886{
5887 if (a->end && b->end) {
5888 a->start = min(a->start, b->start);
5889 a->end = max(a->end, b->end);
5890 } else if (b->end) {
5891 a->start = b->start;
5892 a->end = b->end;
5893 }
5894}
5895
5896bool skl_ddb_allocation_overlaps(const struct skl_ddb_entry *ddb,
5897 const struct skl_ddb_entry *entries,
5898 int num_entries, int ignore_idx)
5899{
5900 int i;
5901
5902 for (i = 0; i < num_entries; i++) {
5903 if (i != ignore_idx &&
5904 skl_ddb_entries_overlap(ddb, &entries[i]))
5905 return true;
5906 }
5907
5908 return false;
5909}
5910
5911static int
5912skl_ddb_add_affected_planes(const struct intel_crtc_state *old_crtc_state,
5913 struct intel_crtc_state *new_crtc_state)
5914{
5915 struct intel_atomic_state *state = to_intel_atomic_state(new_crtc_state->uapi.state);
5916 struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
5917 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5918 struct intel_plane *plane;
5919
5920 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5921 struct intel_plane_state *plane_state;
5922 enum plane_id plane_id = plane->id;
5923
5924 if (skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_y[plane_id],
5925 &new_crtc_state->wm.skl.plane_ddb_y[plane_id]) &&
5926 skl_ddb_entry_equal(&old_crtc_state->wm.skl.plane_ddb_uv[plane_id],
5927 &new_crtc_state->wm.skl.plane_ddb_uv[plane_id]))
5928 continue;
5929
5930 plane_state = intel_atomic_get_plane_state(state, plane);
5931 if (IS_ERR(plane_state))
5932 return PTR_ERR(plane_state);
5933
5934 new_crtc_state->update_planes |= BIT(plane_id);
5935 }
5936
5937 return 0;
5938}
5939
5940static u8 intel_dbuf_enabled_slices(const struct intel_dbuf_state *dbuf_state)
5941{
5942 struct drm_i915_private *dev_priv = to_i915(dbuf_state->base.state->base.dev);
5943 u8 enabled_slices;
5944 enum pipe pipe;
5945
5946 /*
5947 * FIXME: For now we always enable slice S1 as per
5948 * the Bspec display initialization sequence.
5949 */
5950 enabled_slices = BIT(DBUF_S1);
5951
5952 for_each_pipe(dev_priv, pipe)
5953 enabled_slices |= dbuf_state->slices[pipe];
5954
5955 return enabled_slices;
5956}
5957
5958static int
5959skl_compute_ddb(struct intel_atomic_state *state)
5960{
5961 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5962 const struct intel_dbuf_state *old_dbuf_state;
5963 struct intel_dbuf_state *new_dbuf_state = NULL;
5964 const struct intel_crtc_state *old_crtc_state;
5965 struct intel_crtc_state *new_crtc_state;
5966 struct intel_crtc *crtc;
5967 int ret, i;
5968
5969 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
5970 new_dbuf_state = intel_atomic_get_dbuf_state(state);
5971 if (IS_ERR(new_dbuf_state))
5972 return PTR_ERR(new_dbuf_state);
5973
5974 old_dbuf_state = intel_atomic_get_old_dbuf_state(state);
5975 break;
5976 }
5977
5978 if (!new_dbuf_state)
5979 return 0;
5980
5981 new_dbuf_state->active_pipes =
5982 intel_calc_active_pipes(state, old_dbuf_state->active_pipes);
5983
5984 if (old_dbuf_state->active_pipes != new_dbuf_state->active_pipes) {
5985 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
5986 if (ret)
5987 return ret;
5988 }
5989
5990 for_each_intel_crtc(&dev_priv->drm, crtc) {
5991 enum pipe pipe = crtc->pipe;
5992
5993 new_dbuf_state->slices[pipe] =
5994 skl_compute_dbuf_slices(crtc, new_dbuf_state->active_pipes);
5995
5996 if (old_dbuf_state->slices[pipe] == new_dbuf_state->slices[pipe])
5997 continue;
5998
5999 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6000 if (ret)
6001 return ret;
6002 }
6003
6004 new_dbuf_state->enabled_slices = intel_dbuf_enabled_slices(new_dbuf_state);
6005
6006 if (IS_ALDERLAKE_P(dev_priv))
6007 new_dbuf_state->joined_mbus = adlp_check_mbus_joined(new_dbuf_state->active_pipes);
6008
6009 if (old_dbuf_state->enabled_slices != new_dbuf_state->enabled_slices ||
6010 old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6011 ret = intel_atomic_serialize_global_state(&new_dbuf_state->base);
6012 if (ret)
6013 return ret;
6014
6015 if (old_dbuf_state->joined_mbus != new_dbuf_state->joined_mbus) {
6016 /* TODO: Implement vblank synchronized MBUS joining changes */
6017 ret = intel_modeset_all_pipes(state);
6018 if (ret)
6019 return ret;
6020 }
6021
6022 drm_dbg_kms(&dev_priv->drm,
6023 "Enabled dbuf slices 0x%x -> 0x%x (total dbuf slices 0x%x), mbus joined? %s->%s\n",
6024 old_dbuf_state->enabled_slices,
6025 new_dbuf_state->enabled_slices,
6026 INTEL_INFO(dev_priv)->dbuf.slice_mask,
6027 yesno(old_dbuf_state->joined_mbus),
6028 yesno(new_dbuf_state->joined_mbus));
6029 }
6030
6031 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6032 enum pipe pipe = crtc->pipe;
6033
6034 new_dbuf_state->weight[pipe] = intel_crtc_ddb_weight(new_crtc_state);
6035
6036 if (old_dbuf_state->weight[pipe] == new_dbuf_state->weight[pipe])
6037 continue;
6038
6039 ret = intel_atomic_lock_global_state(&new_dbuf_state->base);
6040 if (ret)
6041 return ret;
6042 }
6043
6044 for_each_intel_crtc(&dev_priv->drm, crtc) {
6045 ret = skl_crtc_allocate_ddb(state, crtc);
6046 if (ret)
6047 return ret;
6048 }
6049
6050 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6051 new_crtc_state, i) {
6052 ret = skl_allocate_plane_ddb(state, crtc);
6053 if (ret)
6054 return ret;
6055
6056 ret = skl_ddb_add_affected_planes(old_crtc_state,
6057 new_crtc_state);
6058 if (ret)
6059 return ret;
6060 }
6061
6062 return 0;
6063}
6064
6065static char enast(bool enable)
6066{
6067 return enable ? '*' : ' ';
6068}
6069
6070static void
6071skl_print_wm_changes(struct intel_atomic_state *state)
6072{
6073 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6074 const struct intel_crtc_state *old_crtc_state;
6075 const struct intel_crtc_state *new_crtc_state;
6076 struct intel_plane *plane;
6077 struct intel_crtc *crtc;
6078 int i;
6079
6080 if (!drm_debug_enabled(DRM_UT_KMS))
6081 return;
6082
6083 for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6084 new_crtc_state, i) {
6085 const struct skl_pipe_wm *old_pipe_wm, *new_pipe_wm;
6086
6087 old_pipe_wm = &old_crtc_state->wm.skl.optimal;
6088 new_pipe_wm = &new_crtc_state->wm.skl.optimal;
6089
6090 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6091 enum plane_id plane_id = plane->id;
6092 const struct skl_ddb_entry *old, *new;
6093
6094 old = &old_crtc_state->wm.skl.plane_ddb_y[plane_id];
6095 new = &new_crtc_state->wm.skl.plane_ddb_y[plane_id];
6096
6097 if (skl_ddb_entry_equal(old, new))
6098 continue;
6099
6100 drm_dbg_kms(&dev_priv->drm,
6101 "[PLANE:%d:%s] ddb (%4d - %4d) -> (%4d - %4d), size %4d -> %4d\n",
6102 plane->base.base.id, plane->base.name,
6103 old->start, old->end, new->start, new->end,
6104 skl_ddb_entry_size(old), skl_ddb_entry_size(new));
6105 }
6106
6107 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6108 enum plane_id plane_id = plane->id;
6109 const struct skl_plane_wm *old_wm, *new_wm;
6110
6111 old_wm = &old_pipe_wm->planes[plane_id];
6112 new_wm = &new_pipe_wm->planes[plane_id];
6113
6114 if (skl_plane_wm_equals(dev_priv, old_wm, new_wm))
6115 continue;
6116
6117 drm_dbg_kms(&dev_priv->drm,
6118 "[PLANE:%d:%s] level %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm"
6119 " -> %cwm0,%cwm1,%cwm2,%cwm3,%cwm4,%cwm5,%cwm6,%cwm7,%ctwm,%cswm,%cstwm\n",
6120 plane->base.base.id, plane->base.name,
6121 enast(old_wm->wm[0].enable), enast(old_wm->wm[1].enable),
6122 enast(old_wm->wm[2].enable), enast(old_wm->wm[3].enable),
6123 enast(old_wm->wm[4].enable), enast(old_wm->wm[5].enable),
6124 enast(old_wm->wm[6].enable), enast(old_wm->wm[7].enable),
6125 enast(old_wm->trans_wm.enable),
6126 enast(old_wm->sagv.wm0.enable),
6127 enast(old_wm->sagv.trans_wm.enable),
6128 enast(new_wm->wm[0].enable), enast(new_wm->wm[1].enable),
6129 enast(new_wm->wm[2].enable), enast(new_wm->wm[3].enable),
6130 enast(new_wm->wm[4].enable), enast(new_wm->wm[5].enable),
6131 enast(new_wm->wm[6].enable), enast(new_wm->wm[7].enable),
6132 enast(new_wm->trans_wm.enable),
6133 enast(new_wm->sagv.wm0.enable),
6134 enast(new_wm->sagv.trans_wm.enable));
6135
6136 drm_dbg_kms(&dev_priv->drm,
6137 "[PLANE:%d:%s] lines %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d"
6138 " -> %c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%3d,%c%4d\n",
6139 plane->base.base.id, plane->base.name,
6140 enast(old_wm->wm[0].ignore_lines), old_wm->wm[0].lines,
6141 enast(old_wm->wm[1].ignore_lines), old_wm->wm[1].lines,
6142 enast(old_wm->wm[2].ignore_lines), old_wm->wm[2].lines,
6143 enast(old_wm->wm[3].ignore_lines), old_wm->wm[3].lines,
6144 enast(old_wm->wm[4].ignore_lines), old_wm->wm[4].lines,
6145 enast(old_wm->wm[5].ignore_lines), old_wm->wm[5].lines,
6146 enast(old_wm->wm[6].ignore_lines), old_wm->wm[6].lines,
6147 enast(old_wm->wm[7].ignore_lines), old_wm->wm[7].lines,
6148 enast(old_wm->trans_wm.ignore_lines), old_wm->trans_wm.lines,
6149 enast(old_wm->sagv.wm0.ignore_lines), old_wm->sagv.wm0.lines,
6150 enast(old_wm->sagv.trans_wm.ignore_lines), old_wm->sagv.trans_wm.lines,
6151 enast(new_wm->wm[0].ignore_lines), new_wm->wm[0].lines,
6152 enast(new_wm->wm[1].ignore_lines), new_wm->wm[1].lines,
6153 enast(new_wm->wm[2].ignore_lines), new_wm->wm[2].lines,
6154 enast(new_wm->wm[3].ignore_lines), new_wm->wm[3].lines,
6155 enast(new_wm->wm[4].ignore_lines), new_wm->wm[4].lines,
6156 enast(new_wm->wm[5].ignore_lines), new_wm->wm[5].lines,
6157 enast(new_wm->wm[6].ignore_lines), new_wm->wm[6].lines,
6158 enast(new_wm->wm[7].ignore_lines), new_wm->wm[7].lines,
6159 enast(new_wm->trans_wm.ignore_lines), new_wm->trans_wm.lines,
6160 enast(new_wm->sagv.wm0.ignore_lines), new_wm->sagv.wm0.lines,
6161 enast(new_wm->sagv.trans_wm.ignore_lines), new_wm->sagv.trans_wm.lines);
6162
6163 drm_dbg_kms(&dev_priv->drm,
6164 "[PLANE:%d:%s] blocks %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6165 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
6166 plane->base.base.id, plane->base.name,
6167 old_wm->wm[0].blocks, old_wm->wm[1].blocks,
6168 old_wm->wm[2].blocks, old_wm->wm[3].blocks,
6169 old_wm->wm[4].blocks, old_wm->wm[5].blocks,
6170 old_wm->wm[6].blocks, old_wm->wm[7].blocks,
6171 old_wm->trans_wm.blocks,
6172 old_wm->sagv.wm0.blocks,
6173 old_wm->sagv.trans_wm.blocks,
6174 new_wm->wm[0].blocks, new_wm->wm[1].blocks,
6175 new_wm->wm[2].blocks, new_wm->wm[3].blocks,
6176 new_wm->wm[4].blocks, new_wm->wm[5].blocks,
6177 new_wm->wm[6].blocks, new_wm->wm[7].blocks,
6178 new_wm->trans_wm.blocks,
6179 new_wm->sagv.wm0.blocks,
6180 new_wm->sagv.trans_wm.blocks);
6181
6182 drm_dbg_kms(&dev_priv->drm,
6183 "[PLANE:%d:%s] min_ddb %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d"
6184 " -> %4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%4d,%5d\n",
6185 plane->base.base.id, plane->base.name,
6186 old_wm->wm[0].min_ddb_alloc, old_wm->wm[1].min_ddb_alloc,
6187 old_wm->wm[2].min_ddb_alloc, old_wm->wm[3].min_ddb_alloc,
6188 old_wm->wm[4].min_ddb_alloc, old_wm->wm[5].min_ddb_alloc,
6189 old_wm->wm[6].min_ddb_alloc, old_wm->wm[7].min_ddb_alloc,
6190 old_wm->trans_wm.min_ddb_alloc,
6191 old_wm->sagv.wm0.min_ddb_alloc,
6192 old_wm->sagv.trans_wm.min_ddb_alloc,
6193 new_wm->wm[0].min_ddb_alloc, new_wm->wm[1].min_ddb_alloc,
6194 new_wm->wm[2].min_ddb_alloc, new_wm->wm[3].min_ddb_alloc,
6195 new_wm->wm[4].min_ddb_alloc, new_wm->wm[5].min_ddb_alloc,
6196 new_wm->wm[6].min_ddb_alloc, new_wm->wm[7].min_ddb_alloc,
6197 new_wm->trans_wm.min_ddb_alloc,
6198 new_wm->sagv.wm0.min_ddb_alloc,
6199 new_wm->sagv.trans_wm.min_ddb_alloc);
6200 }
6201 }
6202}
6203
6204static bool skl_plane_selected_wm_equals(struct intel_plane *plane,
6205 const struct skl_pipe_wm *old_pipe_wm,
6206 const struct skl_pipe_wm *new_pipe_wm)
6207{
6208 struct drm_i915_private *i915 = to_i915(plane->base.dev);
6209 int level, max_level = ilk_wm_max_level(i915);
6210
6211 for (level = 0; level <= max_level; level++) {
6212 /*
6213 * We don't check uv_wm as the hardware doesn't actually
6214 * use it. It only gets used for calculating the required
6215 * ddb allocation.
6216 */
6217 if (!skl_wm_level_equals(skl_plane_wm_level(old_pipe_wm, plane->id, level),
6218 skl_plane_wm_level(new_pipe_wm, plane->id, level)))
6219 return false;
6220 }
6221
6222 if (HAS_HW_SAGV_WM(i915)) {
6223 const struct skl_plane_wm *old_wm = &old_pipe_wm->planes[plane->id];
6224 const struct skl_plane_wm *new_wm = &new_pipe_wm->planes[plane->id];
6225
6226 if (!skl_wm_level_equals(&old_wm->sagv.wm0, &new_wm->sagv.wm0) ||
6227 !skl_wm_level_equals(&old_wm->sagv.trans_wm, &new_wm->sagv.trans_wm))
6228 return false;
6229 }
6230
6231 return skl_wm_level_equals(skl_plane_trans_wm(old_pipe_wm, plane->id),
6232 skl_plane_trans_wm(new_pipe_wm, plane->id));
6233}
6234
6235/*
6236 * To make sure the cursor watermark registers are always consistent
6237 * with our computed state the following scenario needs special
6238 * treatment:
6239 *
6240 * 1. enable cursor
6241 * 2. move cursor entirely offscreen
6242 * 3. disable cursor
6243 *
6244 * Step 2. does call .disable_plane() but does not zero the watermarks
6245 * (since we consider an offscreen cursor still active for the purposes
6246 * of watermarks). Step 3. would not normally call .disable_plane()
6247 * because the actual plane visibility isn't changing, and we don't
6248 * deallocate the cursor ddb until the pipe gets disabled. So we must
6249 * force step 3. to call .disable_plane() to update the watermark
6250 * registers properly.
6251 *
6252 * Other planes do not suffer from this issues as their watermarks are
6253 * calculated based on the actual plane visibility. The only time this
6254 * can trigger for the other planes is during the initial readout as the
6255 * default value of the watermarks registers is not zero.
6256 */
6257static int skl_wm_add_affected_planes(struct intel_atomic_state *state,
6258 struct intel_crtc *crtc)
6259{
6260 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6261 const struct intel_crtc_state *old_crtc_state =
6262 intel_atomic_get_old_crtc_state(state, crtc);
6263 struct intel_crtc_state *new_crtc_state =
6264 intel_atomic_get_new_crtc_state(state, crtc);
6265 struct intel_plane *plane;
6266
6267 for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6268 struct intel_plane_state *plane_state;
6269 enum plane_id plane_id = plane->id;
6270
6271 /*
6272 * Force a full wm update for every plane on modeset.
6273 * Required because the reset value of the wm registers
6274 * is non-zero, whereas we want all disabled planes to
6275 * have zero watermarks. So if we turn off the relevant
6276 * power well the hardware state will go out of sync
6277 * with the software state.
6278 */
6279 if (!drm_atomic_crtc_needs_modeset(&new_crtc_state->uapi) &&
6280 skl_plane_selected_wm_equals(plane,
6281 &old_crtc_state->wm.skl.optimal,
6282 &new_crtc_state->wm.skl.optimal))
6283 continue;
6284
6285 plane_state = intel_atomic_get_plane_state(state, plane);
6286 if (IS_ERR(plane_state))
6287 return PTR_ERR(plane_state);
6288
6289 new_crtc_state->update_planes |= BIT(plane_id);
6290 }
6291
6292 return 0;
6293}
6294
6295static int
6296skl_compute_wm(struct intel_atomic_state *state)
6297{
6298 struct intel_crtc *crtc;
6299 struct intel_crtc_state *new_crtc_state;
6300 int ret, i;
6301
6302 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6303 ret = skl_build_pipe_wm(state, crtc);
6304 if (ret)
6305 return ret;
6306 }
6307
6308 ret = skl_compute_ddb(state);
6309 if (ret)
6310 return ret;
6311
6312 ret = intel_compute_sagv_mask(state);
6313 if (ret)
6314 return ret;
6315
6316 /*
6317 * skl_compute_ddb() will have adjusted the final watermarks
6318 * based on how much ddb is available. Now we can actually
6319 * check if the final watermarks changed.
6320 */
6321 for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6322 ret = skl_wm_add_affected_planes(state, crtc);
6323 if (ret)
6324 return ret;
6325 }
6326
6327 skl_print_wm_changes(state);
6328
6329 return 0;
6330}
6331
6332static void ilk_compute_wm_config(struct drm_i915_private *dev_priv,
6333 struct intel_wm_config *config)
6334{
6335 struct intel_crtc *crtc;
6336
6337 /* Compute the currently _active_ config */
6338 for_each_intel_crtc(&dev_priv->drm, crtc) {
6339 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
6340
6341 if (!wm->pipe_enabled)
6342 continue;
6343
6344 config->sprites_enabled |= wm->sprites_enabled;
6345 config->sprites_scaled |= wm->sprites_scaled;
6346 config->num_pipes_active++;
6347 }
6348}
6349
6350static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
6351{
6352 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
6353 struct ilk_wm_maximums max;
6354 struct intel_wm_config config = {};
6355 struct ilk_wm_values results = {};
6356 enum intel_ddb_partitioning partitioning;
6357
6358 ilk_compute_wm_config(dev_priv, &config);
6359
6360 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_1_2, &max);
6361 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_1_2);
6362
6363 /* 5/6 split only in single pipe config on IVB+ */
6364 if (DISPLAY_VER(dev_priv) >= 7 &&
6365 config.num_pipes_active == 1 && config.sprites_enabled) {
6366 ilk_compute_wm_maximums(dev_priv, 1, &config, INTEL_DDB_PART_5_6, &max);
6367 ilk_wm_merge(dev_priv, &config, &max, &lp_wm_5_6);
6368
6369 best_lp_wm = ilk_find_best_result(dev_priv, &lp_wm_1_2, &lp_wm_5_6);
6370 } else {
6371 best_lp_wm = &lp_wm_1_2;
6372 }
6373
6374 partitioning = (best_lp_wm == &lp_wm_1_2) ?
6375 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
6376
6377 ilk_compute_wm_results(dev_priv, best_lp_wm, partitioning, &results);
6378
6379 ilk_write_wm_values(dev_priv, &results);
6380}
6381
6382static void ilk_initial_watermarks(struct intel_atomic_state *state,
6383 struct intel_crtc *crtc)
6384{
6385 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6386 const struct intel_crtc_state *crtc_state =
6387 intel_atomic_get_new_crtc_state(state, crtc);
6388
6389 mutex_lock(&dev_priv->wm.wm_mutex);
6390 crtc->wm.active.ilk = crtc_state->wm.ilk.intermediate;
6391 ilk_program_watermarks(dev_priv);
6392 mutex_unlock(&dev_priv->wm.wm_mutex);
6393}
6394
6395static void ilk_optimize_watermarks(struct intel_atomic_state *state,
6396 struct intel_crtc *crtc)
6397{
6398 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6399 const struct intel_crtc_state *crtc_state =
6400 intel_atomic_get_new_crtc_state(state, crtc);
6401
6402 if (!crtc_state->wm.need_postvbl_update)
6403 return;
6404
6405 mutex_lock(&dev_priv->wm.wm_mutex);
6406 crtc->wm.active.ilk = crtc_state->wm.ilk.optimal;
6407 ilk_program_watermarks(dev_priv);
6408 mutex_unlock(&dev_priv->wm.wm_mutex);
6409}
6410
6411static void skl_wm_level_from_reg_val(u32 val, struct skl_wm_level *level)
6412{
6413 level->enable = val & PLANE_WM_EN;
6414 level->ignore_lines = val & PLANE_WM_IGNORE_LINES;
6415 level->blocks = val & PLANE_WM_BLOCKS_MASK;
6416 level->lines = REG_FIELD_GET(PLANE_WM_LINES_MASK, val);
6417}
6418
6419void skl_pipe_wm_get_hw_state(struct intel_crtc *crtc,
6420 struct skl_pipe_wm *out)
6421{
6422 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6423 enum pipe pipe = crtc->pipe;
6424 int level, max_level;
6425 enum plane_id plane_id;
6426 u32 val;
6427
6428 max_level = ilk_wm_max_level(dev_priv);
6429
6430 for_each_plane_id_on_crtc(crtc, plane_id) {
6431 struct skl_plane_wm *wm = &out->planes[plane_id];
6432
6433 for (level = 0; level <= max_level; level++) {
6434 if (plane_id != PLANE_CURSOR)
6435 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM(pipe, plane_id, level));
6436 else
6437 val = intel_uncore_read(&dev_priv->uncore, CUR_WM(pipe, level));
6438
6439 skl_wm_level_from_reg_val(val, &wm->wm[level]);
6440 }
6441
6442 if (plane_id != PLANE_CURSOR)
6443 val = intel_uncore_read(&dev_priv->uncore, PLANE_WM_TRANS(pipe, plane_id));
6444 else
6445 val = intel_uncore_read(&dev_priv->uncore, CUR_WM_TRANS(pipe));
6446
6447 skl_wm_level_from_reg_val(val, &wm->trans_wm);
6448
6449 if (HAS_HW_SAGV_WM(dev_priv)) {
6450 if (plane_id != PLANE_CURSOR)
6451 val = intel_uncore_read(&dev_priv->uncore,
6452 PLANE_WM_SAGV(pipe, plane_id));
6453 else
6454 val = intel_uncore_read(&dev_priv->uncore,
6455 CUR_WM_SAGV(pipe));
6456
6457 skl_wm_level_from_reg_val(val, &wm->sagv.wm0);
6458
6459 if (plane_id != PLANE_CURSOR)
6460 val = intel_uncore_read(&dev_priv->uncore,
6461 PLANE_WM_SAGV_TRANS(pipe, plane_id));
6462 else
6463 val = intel_uncore_read(&dev_priv->uncore,
6464 CUR_WM_SAGV_TRANS(pipe));
6465
6466 skl_wm_level_from_reg_val(val, &wm->sagv.trans_wm);
6467 } else if (DISPLAY_VER(dev_priv) >= 12) {
6468 wm->sagv.wm0 = wm->wm[0];
6469 wm->sagv.trans_wm = wm->trans_wm;
6470 }
6471 }
6472}
6473
6474void skl_wm_get_hw_state(struct drm_i915_private *dev_priv)
6475{
6476 struct intel_dbuf_state *dbuf_state =
6477 to_intel_dbuf_state(dev_priv->dbuf.obj.state);
6478 struct intel_crtc *crtc;
6479
6480 if (IS_ALDERLAKE_P(dev_priv))
6481 dbuf_state->joined_mbus = intel_de_read(dev_priv, MBUS_CTL) & MBUS_JOIN;
6482
6483 for_each_intel_crtc(&dev_priv->drm, crtc) {
6484 struct intel_crtc_state *crtc_state =
6485 to_intel_crtc_state(crtc->base.state);
6486 enum pipe pipe = crtc->pipe;
6487 unsigned int mbus_offset;
6488 enum plane_id plane_id;
6489
6490 skl_pipe_wm_get_hw_state(crtc, &crtc_state->wm.skl.optimal);
6491 crtc_state->wm.skl.raw = crtc_state->wm.skl.optimal;
6492
6493 memset(&dbuf_state->ddb[pipe], 0, sizeof(dbuf_state->ddb[pipe]));
6494
6495 for_each_plane_id_on_crtc(crtc, plane_id) {
6496 struct skl_ddb_entry *ddb_y =
6497 &crtc_state->wm.skl.plane_ddb_y[plane_id];
6498 struct skl_ddb_entry *ddb_uv =
6499 &crtc_state->wm.skl.plane_ddb_uv[plane_id];
6500
6501 skl_ddb_get_hw_plane_state(dev_priv, crtc->pipe,
6502 plane_id, ddb_y, ddb_uv);
6503
6504 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_y);
6505 skl_ddb_entry_union(&dbuf_state->ddb[pipe], ddb_uv);
6506 }
6507
6508 dbuf_state->slices[pipe] =
6509 skl_compute_dbuf_slices(crtc, dbuf_state->active_pipes);
6510
6511 dbuf_state->weight[pipe] = intel_crtc_ddb_weight(crtc_state);
6512
6513 /*
6514 * Used for checking overlaps, so we need absolute
6515 * offsets instead of MBUS relative offsets.
6516 */
6517 mbus_offset = mbus_ddb_offset(dev_priv, dbuf_state->slices[pipe]);
6518 crtc_state->wm.skl.ddb.start = mbus_offset + dbuf_state->ddb[pipe].start;
6519 crtc_state->wm.skl.ddb.end = mbus_offset + dbuf_state->ddb[pipe].end;
6520
6521 drm_dbg_kms(&dev_priv->drm,
6522 "[CRTC:%d:%s] dbuf slices 0x%x, ddb (%d - %d), active pipes 0x%x, mbus joined: %s\n",
6523 crtc->base.base.id, crtc->base.name,
6524 dbuf_state->slices[pipe], dbuf_state->ddb[pipe].start,
6525 dbuf_state->ddb[pipe].end, dbuf_state->active_pipes,
6526 yesno(dbuf_state->joined_mbus));
6527 }
6528
6529 dbuf_state->enabled_slices = dev_priv->dbuf.enabled_slices;
6530}
6531
6532static void ilk_pipe_wm_get_hw_state(struct intel_crtc *crtc)
6533{
6534 struct drm_device *dev = crtc->base.dev;
6535 struct drm_i915_private *dev_priv = to_i915(dev);
6536 struct ilk_wm_values *hw = &dev_priv->wm.hw;
6537 struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
6538 struct intel_pipe_wm *active = &crtc_state->wm.ilk.optimal;
6539 enum pipe pipe = crtc->pipe;
6540
6541 hw->wm_pipe[pipe] = intel_uncore_read(&dev_priv->uncore, WM0_PIPE_ILK(pipe));
6542
6543 memset(active, 0, sizeof(*active));
6544
6545 active->pipe_enabled = crtc->active;
6546
6547 if (active->pipe_enabled) {
6548 u32 tmp = hw->wm_pipe[pipe];
6549
6550 /*
6551 * For active pipes LP0 watermark is marked as
6552 * enabled, and LP1+ watermaks as disabled since
6553 * we can't really reverse compute them in case
6554 * multiple pipes are active.
6555 */
6556 active->wm[0].enable = true;
6557 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
6558 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
6559 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
6560 } else {
6561 int level, max_level = ilk_wm_max_level(dev_priv);
6562
6563 /*
6564 * For inactive pipes, all watermark levels
6565 * should be marked as enabled but zeroed,
6566 * which is what we'd compute them to.
6567 */
6568 for (level = 0; level <= max_level; level++)
6569 active->wm[level].enable = true;
6570 }
6571
6572 crtc->wm.active.ilk = *active;
6573}
6574
6575#define _FW_WM(value, plane) \
6576 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
6577#define _FW_WM_VLV(value, plane) \
6578 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
6579
6580static void g4x_read_wm_values(struct drm_i915_private *dev_priv,
6581 struct g4x_wm_values *wm)
6582{
6583 u32 tmp;
6584
6585 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
6586 wm->sr.plane = _FW_WM(tmp, SR);
6587 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6588 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEB);
6589 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM(tmp, PLANEA);
6590
6591 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
6592 wm->fbc_en = tmp & DSPFW_FBC_SR_EN;
6593 wm->sr.fbc = _FW_WM(tmp, FBC_SR);
6594 wm->hpll.fbc = _FW_WM(tmp, FBC_HPLL_SR);
6595 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEB);
6596 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6597 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM(tmp, SPRITEA);
6598
6599 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
6600 wm->hpll_en = tmp & DSPFW_HPLL_SR_EN;
6601 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6602 wm->hpll.cursor = _FW_WM(tmp, HPLL_CURSOR);
6603 wm->hpll.plane = _FW_WM(tmp, HPLL_SR);
6604}
6605
6606static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
6607 struct vlv_wm_values *wm)
6608{
6609 enum pipe pipe;
6610 u32 tmp;
6611
6612 for_each_pipe(dev_priv, pipe) {
6613 tmp = intel_uncore_read(&dev_priv->uncore, VLV_DDL(pipe));
6614
6615 wm->ddl[pipe].plane[PLANE_PRIMARY] =
6616 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6617 wm->ddl[pipe].plane[PLANE_CURSOR] =
6618 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6619 wm->ddl[pipe].plane[PLANE_SPRITE0] =
6620 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6621 wm->ddl[pipe].plane[PLANE_SPRITE1] =
6622 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
6623 }
6624
6625 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW1);
6626 wm->sr.plane = _FW_WM(tmp, SR);
6627 wm->pipe[PIPE_B].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORB);
6628 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEB);
6629 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEA);
6630
6631 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW2);
6632 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEB);
6633 wm->pipe[PIPE_A].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORA);
6634 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEA);
6635
6636 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW3);
6637 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
6638
6639 if (IS_CHERRYVIEW(dev_priv)) {
6640 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7_CHV);
6641 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6642 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
6643
6644 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW8_CHV);
6645 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITEF);
6646 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEE);
6647
6648 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW9_CHV);
6649 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] = _FW_WM_VLV(tmp, PLANEC);
6650 wm->pipe[PIPE_C].plane[PLANE_CURSOR] = _FW_WM(tmp, CURSORC);
6651
6652 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
6653 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
6654 wm->pipe[PIPE_C].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
6655 wm->pipe[PIPE_C].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
6656 wm->pipe[PIPE_C].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEC_HI) << 8;
6657 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6658 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6659 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6660 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6661 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6662 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
6663 } else {
6664 tmp = intel_uncore_read(&dev_priv->uncore, DSPFW7);
6665 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] = _FW_WM_VLV(tmp, SPRITED);
6666 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] = _FW_WM_VLV(tmp, SPRITEC);
6667
6668 tmp = intel_uncore_read(&dev_priv->uncore, DSPHOWM);
6669 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
6670 wm->pipe[PIPE_B].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITED_HI) << 8;
6671 wm->pipe[PIPE_B].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
6672 wm->pipe[PIPE_B].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEB_HI) << 8;
6673 wm->pipe[PIPE_A].plane[PLANE_SPRITE1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
6674 wm->pipe[PIPE_A].plane[PLANE_SPRITE0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
6675 wm->pipe[PIPE_A].plane[PLANE_PRIMARY] |= _FW_WM(tmp, PLANEA_HI) << 8;
6676 }
6677}
6678
6679#undef _FW_WM
6680#undef _FW_WM_VLV
6681
6682void g4x_wm_get_hw_state(struct drm_i915_private *dev_priv)
6683{
6684 struct g4x_wm_values *wm = &dev_priv->wm.g4x;
6685 struct intel_crtc *crtc;
6686
6687 g4x_read_wm_values(dev_priv, wm);
6688
6689 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF) & FW_BLC_SELF_EN;
6690
6691 for_each_intel_crtc(&dev_priv->drm, crtc) {
6692 struct intel_crtc_state *crtc_state =
6693 to_intel_crtc_state(crtc->base.state);
6694 struct g4x_wm_state *active = &crtc->wm.active.g4x;
6695 struct g4x_pipe_wm *raw;
6696 enum pipe pipe = crtc->pipe;
6697 enum plane_id plane_id;
6698 int level, max_level;
6699
6700 active->cxsr = wm->cxsr;
6701 active->hpll_en = wm->hpll_en;
6702 active->fbc_en = wm->fbc_en;
6703
6704 active->sr = wm->sr;
6705 active->hpll = wm->hpll;
6706
6707 for_each_plane_id_on_crtc(crtc, plane_id) {
6708 active->wm.plane[plane_id] =
6709 wm->pipe[pipe].plane[plane_id];
6710 }
6711
6712 if (wm->cxsr && wm->hpll_en)
6713 max_level = G4X_WM_LEVEL_HPLL;
6714 else if (wm->cxsr)
6715 max_level = G4X_WM_LEVEL_SR;
6716 else
6717 max_level = G4X_WM_LEVEL_NORMAL;
6718
6719 level = G4X_WM_LEVEL_NORMAL;
6720 raw = &crtc_state->wm.g4x.raw[level];
6721 for_each_plane_id_on_crtc(crtc, plane_id)
6722 raw->plane[plane_id] = active->wm.plane[plane_id];
6723
6724 if (++level > max_level)
6725 goto out;
6726
6727 raw = &crtc_state->wm.g4x.raw[level];
6728 raw->plane[PLANE_PRIMARY] = active->sr.plane;
6729 raw->plane[PLANE_CURSOR] = active->sr.cursor;
6730 raw->plane[PLANE_SPRITE0] = 0;
6731 raw->fbc = active->sr.fbc;
6732
6733 if (++level > max_level)
6734 goto out;
6735
6736 raw = &crtc_state->wm.g4x.raw[level];
6737 raw->plane[PLANE_PRIMARY] = active->hpll.plane;
6738 raw->plane[PLANE_CURSOR] = active->hpll.cursor;
6739 raw->plane[PLANE_SPRITE0] = 0;
6740 raw->fbc = active->hpll.fbc;
6741
6742 out:
6743 for_each_plane_id_on_crtc(crtc, plane_id)
6744 g4x_raw_plane_wm_set(crtc_state, level,
6745 plane_id, USHRT_MAX);
6746 g4x_raw_fbc_wm_set(crtc_state, level, USHRT_MAX);
6747
6748 crtc_state->wm.g4x.optimal = *active;
6749 crtc_state->wm.g4x.intermediate = *active;
6750
6751 drm_dbg_kms(&dev_priv->drm,
6752 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite=%d\n",
6753 pipe_name(pipe),
6754 wm->pipe[pipe].plane[PLANE_PRIMARY],
6755 wm->pipe[pipe].plane[PLANE_CURSOR],
6756 wm->pipe[pipe].plane[PLANE_SPRITE0]);
6757 }
6758
6759 drm_dbg_kms(&dev_priv->drm,
6760 "Initial SR watermarks: plane=%d, cursor=%d fbc=%d\n",
6761 wm->sr.plane, wm->sr.cursor, wm->sr.fbc);
6762 drm_dbg_kms(&dev_priv->drm,
6763 "Initial HPLL watermarks: plane=%d, SR cursor=%d fbc=%d\n",
6764 wm->hpll.plane, wm->hpll.cursor, wm->hpll.fbc);
6765 drm_dbg_kms(&dev_priv->drm, "Initial SR=%s HPLL=%s FBC=%s\n",
6766 yesno(wm->cxsr), yesno(wm->hpll_en), yesno(wm->fbc_en));
6767}
6768
6769void g4x_wm_sanitize(struct drm_i915_private *dev_priv)
6770{
6771 struct intel_plane *plane;
6772 struct intel_crtc *crtc;
6773
6774 mutex_lock(&dev_priv->wm.wm_mutex);
6775
6776 for_each_intel_plane(&dev_priv->drm, plane) {
6777 struct intel_crtc *crtc =
6778 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6779 struct intel_crtc_state *crtc_state =
6780 to_intel_crtc_state(crtc->base.state);
6781 struct intel_plane_state *plane_state =
6782 to_intel_plane_state(plane->base.state);
6783 struct g4x_wm_state *wm_state = &crtc_state->wm.g4x.optimal;
6784 enum plane_id plane_id = plane->id;
6785 int level;
6786
6787 if (plane_state->uapi.visible)
6788 continue;
6789
6790 for (level = 0; level < 3; level++) {
6791 struct g4x_pipe_wm *raw =
6792 &crtc_state->wm.g4x.raw[level];
6793
6794 raw->plane[plane_id] = 0;
6795 wm_state->wm.plane[plane_id] = 0;
6796 }
6797
6798 if (plane_id == PLANE_PRIMARY) {
6799 for (level = 0; level < 3; level++) {
6800 struct g4x_pipe_wm *raw =
6801 &crtc_state->wm.g4x.raw[level];
6802 raw->fbc = 0;
6803 }
6804
6805 wm_state->sr.fbc = 0;
6806 wm_state->hpll.fbc = 0;
6807 wm_state->fbc_en = false;
6808 }
6809 }
6810
6811 for_each_intel_crtc(&dev_priv->drm, crtc) {
6812 struct intel_crtc_state *crtc_state =
6813 to_intel_crtc_state(crtc->base.state);
6814
6815 crtc_state->wm.g4x.intermediate =
6816 crtc_state->wm.g4x.optimal;
6817 crtc->wm.active.g4x = crtc_state->wm.g4x.optimal;
6818 }
6819
6820 g4x_program_watermarks(dev_priv);
6821
6822 mutex_unlock(&dev_priv->wm.wm_mutex);
6823}
6824
6825void vlv_wm_get_hw_state(struct drm_i915_private *dev_priv)
6826{
6827 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
6828 struct intel_crtc *crtc;
6829 u32 val;
6830
6831 vlv_read_wm_values(dev_priv, wm);
6832
6833 wm->cxsr = intel_uncore_read(&dev_priv->uncore, FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
6834 wm->level = VLV_WM_LEVEL_PM2;
6835
6836 if (IS_CHERRYVIEW(dev_priv)) {
6837 vlv_punit_get(dev_priv);
6838
6839 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPSSPM);
6840 if (val & DSP_MAXFIFO_PM5_ENABLE)
6841 wm->level = VLV_WM_LEVEL_PM5;
6842
6843 /*
6844 * If DDR DVFS is disabled in the BIOS, Punit
6845 * will never ack the request. So if that happens
6846 * assume we don't have to enable/disable DDR DVFS
6847 * dynamically. To test that just set the REQ_ACK
6848 * bit to poke the Punit, but don't change the
6849 * HIGH/LOW bits so that we don't actually change
6850 * the current state.
6851 */
6852 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6853 val |= FORCE_DDR_FREQ_REQ_ACK;
6854 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
6855
6856 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
6857 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
6858 drm_dbg_kms(&dev_priv->drm,
6859 "Punit not acking DDR DVFS request, "
6860 "assuming DDR DVFS is disabled\n");
6861 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
6862 } else {
6863 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
6864 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
6865 wm->level = VLV_WM_LEVEL_DDR_DVFS;
6866 }
6867
6868 vlv_punit_put(dev_priv);
6869 }
6870
6871 for_each_intel_crtc(&dev_priv->drm, crtc) {
6872 struct intel_crtc_state *crtc_state =
6873 to_intel_crtc_state(crtc->base.state);
6874 struct vlv_wm_state *active = &crtc->wm.active.vlv;
6875 const struct vlv_fifo_state *fifo_state =
6876 &crtc_state->wm.vlv.fifo_state;
6877 enum pipe pipe = crtc->pipe;
6878 enum plane_id plane_id;
6879 int level;
6880
6881 vlv_get_fifo_size(crtc_state);
6882
6883 active->num_levels = wm->level + 1;
6884 active->cxsr = wm->cxsr;
6885
6886 for (level = 0; level < active->num_levels; level++) {
6887 struct g4x_pipe_wm *raw =
6888 &crtc_state->wm.vlv.raw[level];
6889
6890 active->sr[level].plane = wm->sr.plane;
6891 active->sr[level].cursor = wm->sr.cursor;
6892
6893 for_each_plane_id_on_crtc(crtc, plane_id) {
6894 active->wm[level].plane[plane_id] =
6895 wm->pipe[pipe].plane[plane_id];
6896
6897 raw->plane[plane_id] =
6898 vlv_invert_wm_value(active->wm[level].plane[plane_id],
6899 fifo_state->plane[plane_id]);
6900 }
6901 }
6902
6903 for_each_plane_id_on_crtc(crtc, plane_id)
6904 vlv_raw_plane_wm_set(crtc_state, level,
6905 plane_id, USHRT_MAX);
6906 vlv_invalidate_wms(crtc, active, level);
6907
6908 crtc_state->wm.vlv.optimal = *active;
6909 crtc_state->wm.vlv.intermediate = *active;
6910
6911 drm_dbg_kms(&dev_priv->drm,
6912 "Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
6913 pipe_name(pipe),
6914 wm->pipe[pipe].plane[PLANE_PRIMARY],
6915 wm->pipe[pipe].plane[PLANE_CURSOR],
6916 wm->pipe[pipe].plane[PLANE_SPRITE0],
6917 wm->pipe[pipe].plane[PLANE_SPRITE1]);
6918 }
6919
6920 drm_dbg_kms(&dev_priv->drm,
6921 "Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
6922 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
6923}
6924
6925void vlv_wm_sanitize(struct drm_i915_private *dev_priv)
6926{
6927 struct intel_plane *plane;
6928 struct intel_crtc *crtc;
6929
6930 mutex_lock(&dev_priv->wm.wm_mutex);
6931
6932 for_each_intel_plane(&dev_priv->drm, plane) {
6933 struct intel_crtc *crtc =
6934 intel_get_crtc_for_pipe(dev_priv, plane->pipe);
6935 struct intel_crtc_state *crtc_state =
6936 to_intel_crtc_state(crtc->base.state);
6937 struct intel_plane_state *plane_state =
6938 to_intel_plane_state(plane->base.state);
6939 struct vlv_wm_state *wm_state = &crtc_state->wm.vlv.optimal;
6940 const struct vlv_fifo_state *fifo_state =
6941 &crtc_state->wm.vlv.fifo_state;
6942 enum plane_id plane_id = plane->id;
6943 int level;
6944
6945 if (plane_state->uapi.visible)
6946 continue;
6947
6948 for (level = 0; level < wm_state->num_levels; level++) {
6949 struct g4x_pipe_wm *raw =
6950 &crtc_state->wm.vlv.raw[level];
6951
6952 raw->plane[plane_id] = 0;
6953
6954 wm_state->wm[level].plane[plane_id] =
6955 vlv_invert_wm_value(raw->plane[plane_id],
6956 fifo_state->plane[plane_id]);
6957 }
6958 }
6959
6960 for_each_intel_crtc(&dev_priv->drm, crtc) {
6961 struct intel_crtc_state *crtc_state =
6962 to_intel_crtc_state(crtc->base.state);
6963
6964 crtc_state->wm.vlv.intermediate =
6965 crtc_state->wm.vlv.optimal;
6966 crtc->wm.active.vlv = crtc_state->wm.vlv.optimal;
6967 }
6968
6969 vlv_program_watermarks(dev_priv);
6970
6971 mutex_unlock(&dev_priv->wm.wm_mutex);
6972}
6973
6974/*
6975 * FIXME should probably kill this and improve
6976 * the real watermark readout/sanitation instead
6977 */
6978static void ilk_init_lp_watermarks(struct drm_i915_private *dev_priv)
6979{
6980 intel_uncore_write(&dev_priv->uncore, WM3_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK) & ~WM1_LP_SR_EN);
6981 intel_uncore_write(&dev_priv->uncore, WM2_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK) & ~WM1_LP_SR_EN);
6982 intel_uncore_write(&dev_priv->uncore, WM1_LP_ILK, intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK) & ~WM1_LP_SR_EN);
6983
6984 /*
6985 * Don't touch WM1S_LP_EN here.
6986 * Doing so could cause underruns.
6987 */
6988}
6989
6990void ilk_wm_get_hw_state(struct drm_i915_private *dev_priv)
6991{
6992 struct ilk_wm_values *hw = &dev_priv->wm.hw;
6993 struct intel_crtc *crtc;
6994
6995 ilk_init_lp_watermarks(dev_priv);
6996
6997 for_each_intel_crtc(&dev_priv->drm, crtc)
6998 ilk_pipe_wm_get_hw_state(crtc);
6999
7000 hw->wm_lp[0] = intel_uncore_read(&dev_priv->uncore, WM1_LP_ILK);
7001 hw->wm_lp[1] = intel_uncore_read(&dev_priv->uncore, WM2_LP_ILK);
7002 hw->wm_lp[2] = intel_uncore_read(&dev_priv->uncore, WM3_LP_ILK);
7003
7004 hw->wm_lp_spr[0] = intel_uncore_read(&dev_priv->uncore, WM1S_LP_ILK);
7005 if (DISPLAY_VER(dev_priv) >= 7) {
7006 hw->wm_lp_spr[1] = intel_uncore_read(&dev_priv->uncore, WM2S_LP_IVB);
7007 hw->wm_lp_spr[2] = intel_uncore_read(&dev_priv->uncore, WM3S_LP_IVB);
7008 }
7009
7010 if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
7011 hw->partitioning = (intel_uncore_read(&dev_priv->uncore, WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
7012 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
7013 else if (IS_IVYBRIDGE(dev_priv))
7014 hw->partitioning = (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
7015 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
7016
7017 hw->enable_fbc_wm =
7018 !(intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) & DISP_FBC_WM_DIS);
7019}
7020
7021/**
7022 * intel_update_watermarks - update FIFO watermark values based on current modes
7023 * @crtc: the #intel_crtc on which to compute the WM
7024 *
7025 * Calculate watermark values for the various WM regs based on current mode
7026 * and plane configuration.
7027 *
7028 * There are several cases to deal with here:
7029 * - normal (i.e. non-self-refresh)
7030 * - self-refresh (SR) mode
7031 * - lines are large relative to FIFO size (buffer can hold up to 2)
7032 * - lines are small relative to FIFO size (buffer can hold more than 2
7033 * lines), so need to account for TLB latency
7034 *
7035 * The normal calculation is:
7036 * watermark = dotclock * bytes per pixel * latency
7037 * where latency is platform & configuration dependent (we assume pessimal
7038 * values here).
7039 *
7040 * The SR calculation is:
7041 * watermark = (trunc(latency/line time)+1) * surface width *
7042 * bytes per pixel
7043 * where
7044 * line time = htotal / dotclock
7045 * surface width = hdisplay for normal plane and 64 for cursor
7046 * and latency is assumed to be high, as above.
7047 *
7048 * The final value programmed to the register should always be rounded up,
7049 * and include an extra 2 entries to account for clock crossings.
7050 *
7051 * We don't use the sprite, so we can ignore that. And on Crestline we have
7052 * to set the non-SR watermarks to 8.
7053 */
7054void intel_update_watermarks(struct intel_crtc *crtc)
7055{
7056 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7057
7058 if (dev_priv->display.update_wm)
7059 dev_priv->display.update_wm(crtc);
7060}
7061
7062void intel_enable_ipc(struct drm_i915_private *dev_priv)
7063{
7064 u32 val;
7065
7066 if (!HAS_IPC(dev_priv))
7067 return;
7068
7069 val = intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL2);
7070
7071 if (dev_priv->ipc_enabled)
7072 val |= DISP_IPC_ENABLE;
7073 else
7074 val &= ~DISP_IPC_ENABLE;
7075
7076 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL2, val);
7077}
7078
7079static bool intel_can_enable_ipc(struct drm_i915_private *dev_priv)
7080{
7081 /* Display WA #0477 WaDisableIPC: skl */
7082 if (IS_SKYLAKE(dev_priv))
7083 return false;
7084
7085 /* Display WA #1141: SKL:all KBL:all CFL */
7086 if (IS_KABYLAKE(dev_priv) ||
7087 IS_COFFEELAKE(dev_priv) ||
7088 IS_COMETLAKE(dev_priv))
7089 return dev_priv->dram_info.symmetric_memory;
7090
7091 return true;
7092}
7093
7094void intel_init_ipc(struct drm_i915_private *dev_priv)
7095{
7096 if (!HAS_IPC(dev_priv))
7097 return;
7098
7099 dev_priv->ipc_enabled = intel_can_enable_ipc(dev_priv);
7100
7101 intel_enable_ipc(dev_priv);
7102}
7103
7104static void ibx_init_clock_gating(struct drm_i915_private *dev_priv)
7105{
7106 /*
7107 * On Ibex Peak and Cougar Point, we need to disable clock
7108 * gating for the panel power sequencer or it will fail to
7109 * start up when no ports are active.
7110 */
7111 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
7112}
7113
7114static void g4x_disable_trickle_feed(struct drm_i915_private *dev_priv)
7115{
7116 enum pipe pipe;
7117
7118 for_each_pipe(dev_priv, pipe) {
7119 intel_uncore_write(&dev_priv->uncore, DSPCNTR(pipe),
7120 intel_uncore_read(&dev_priv->uncore, DSPCNTR(pipe)) |
7121 DISPPLANE_TRICKLE_FEED_DISABLE);
7122
7123 intel_uncore_write(&dev_priv->uncore, DSPSURF(pipe), intel_uncore_read(&dev_priv->uncore, DSPSURF(pipe)));
7124 intel_uncore_posting_read(&dev_priv->uncore, DSPSURF(pipe));
7125 }
7126}
7127
7128static void ilk_init_clock_gating(struct drm_i915_private *dev_priv)
7129{
7130 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
7131
7132 /*
7133 * Required for FBC
7134 * WaFbcDisableDpfcClockGating:ilk
7135 */
7136 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
7137 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
7138 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
7139
7140 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS0,
7141 MARIUNIT_CLOCK_GATE_DISABLE |
7142 SVSMUNIT_CLOCK_GATE_DISABLE);
7143 intel_uncore_write(&dev_priv->uncore, PCH_3DCGDIS1,
7144 VFMUNIT_CLOCK_GATE_DISABLE);
7145
7146 /*
7147 * According to the spec the following bits should be set in
7148 * order to enable memory self-refresh
7149 * The bit 22/21 of 0x42004
7150 * The bit 5 of 0x42020
7151 * The bit 15 of 0x45000
7152 */
7153 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7154 (intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7155 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
7156 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
7157 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL,
7158 (intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7159 DISP_FBC_WM_DIS));
7160
7161 /*
7162 * Based on the document from hardware guys the following bits
7163 * should be set unconditionally in order to enable FBC.
7164 * The bit 22 of 0x42000
7165 * The bit 22 of 0x42004
7166 * The bit 7,8,9 of 0x42020.
7167 */
7168 if (IS_IRONLAKE_M(dev_priv)) {
7169 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
7170 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7171 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
7172 ILK_FBCQ_DIS);
7173 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7174 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7175 ILK_DPARB_GATE);
7176 }
7177
7178 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
7179
7180 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7181 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7182 ILK_ELPIN_409_SELECT);
7183
7184 g4x_disable_trickle_feed(dev_priv);
7185
7186 ibx_init_clock_gating(dev_priv);
7187}
7188
7189static void cpt_init_clock_gating(struct drm_i915_private *dev_priv)
7190{
7191 enum pipe pipe;
7192 u32 val;
7193
7194 /*
7195 * On Ibex Peak and Cougar Point, we need to disable clock
7196 * gating for the panel power sequencer or it will fail to
7197 * start up when no ports are active.
7198 */
7199 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
7200 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
7201 PCH_CPUNIT_CLOCK_GATE_DISABLE);
7202 intel_uncore_write(&dev_priv->uncore, SOUTH_CHICKEN2, intel_uncore_read(&dev_priv->uncore, SOUTH_CHICKEN2) |
7203 DPLS_EDP_PPS_FIX_DIS);
7204 /* The below fixes the weird display corruption, a few pixels shifted
7205 * downward, on (only) LVDS of some HP laptops with IVY.
7206 */
7207 for_each_pipe(dev_priv, pipe) {
7208 val = intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN2(pipe));
7209 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
7210 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
7211 if (dev_priv->vbt.fdi_rx_polarity_inverted)
7212 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
7213 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
7214 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
7215 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN2(pipe), val);
7216 }
7217 /* WADP0ClockGatingDisable */
7218 for_each_pipe(dev_priv, pipe) {
7219 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(pipe),
7220 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7221 }
7222}
7223
7224static void gen6_check_mch_setup(struct drm_i915_private *dev_priv)
7225{
7226 u32 tmp;
7227
7228 tmp = intel_uncore_read(&dev_priv->uncore, MCH_SSKPD);
7229 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
7230 drm_dbg_kms(&dev_priv->drm,
7231 "Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
7232 tmp);
7233}
7234
7235static void gen6_init_clock_gating(struct drm_i915_private *dev_priv)
7236{
7237 u32 dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
7238
7239 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, dspclk_gate);
7240
7241 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7242 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7243 ILK_ELPIN_409_SELECT);
7244
7245 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7246 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
7247 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
7248 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7249
7250 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
7251 * gating disable must be set. Failure to set it results in
7252 * flickering pixels due to Z write ordering failures after
7253 * some amount of runtime in the Mesa "fire" demo, and Unigine
7254 * Sanctuary and Tropics, and apparently anything else with
7255 * alpha test or pixel discard.
7256 *
7257 * According to the spec, bit 11 (RCCUNIT) must also be set,
7258 * but we didn't debug actual testcases to find it out.
7259 *
7260 * WaDisableRCCUnitClockGating:snb
7261 * WaDisableRCPBUnitClockGating:snb
7262 */
7263 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
7264 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
7265 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
7266
7267 /*
7268 * According to the spec the following bits should be
7269 * set in order to enable memory self-refresh and fbc:
7270 * The bit21 and bit22 of 0x42000
7271 * The bit21 and bit22 of 0x42004
7272 * The bit5 and bit7 of 0x42020
7273 * The bit14 of 0x70180
7274 * The bit14 of 0x71180
7275 *
7276 * WaFbcAsynchFlipDisableFbcQueue:snb
7277 */
7278 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7279 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
7280 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
7281 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2,
7282 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN2) |
7283 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
7284 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D,
7285 intel_uncore_read(&dev_priv->uncore, ILK_DSPCLK_GATE_D) |
7286 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
7287 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
7288
7289 g4x_disable_trickle_feed(dev_priv);
7290
7291 cpt_init_clock_gating(dev_priv);
7292
7293 gen6_check_mch_setup(dev_priv);
7294}
7295
7296static void lpt_init_clock_gating(struct drm_i915_private *dev_priv)
7297{
7298 /*
7299 * TODO: this bit should only be enabled when really needed, then
7300 * disabled when not needed anymore in order to save power.
7301 */
7302 if (HAS_PCH_LPT_LP(dev_priv))
7303 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D,
7304 intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
7305 PCH_LP_PARTITION_LEVEL_DISABLE);
7306
7307 /* WADPOClockGatingDisable:hsw */
7308 intel_uncore_write(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A),
7309 intel_uncore_read(&dev_priv->uncore, TRANS_CHICKEN1(PIPE_A)) |
7310 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
7311}
7312
7313static void lpt_suspend_hw(struct drm_i915_private *dev_priv)
7314{
7315 if (HAS_PCH_LPT_LP(dev_priv)) {
7316 u32 val = intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D);
7317
7318 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7319 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, val);
7320 }
7321}
7322
7323static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
7324 int general_prio_credits,
7325 int high_prio_credits)
7326{
7327 u32 misccpctl;
7328 u32 val;
7329
7330 /* WaTempDisableDOPClkGating:bdw */
7331 misccpctl = intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL);
7332 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
7333
7334 val = intel_uncore_read(&dev_priv->uncore, GEN8_L3SQCREG1);
7335 val &= ~L3_PRIO_CREDITS_MASK;
7336 val |= L3_GENERAL_PRIO_CREDITS(general_prio_credits);
7337 val |= L3_HIGH_PRIO_CREDITS(high_prio_credits);
7338 intel_uncore_write(&dev_priv->uncore, GEN8_L3SQCREG1, val);
7339
7340 /*
7341 * Wait at least 100 clocks before re-enabling clock gating.
7342 * See the definition of L3SQCREG1 in BSpec.
7343 */
7344 intel_uncore_posting_read(&dev_priv->uncore, GEN8_L3SQCREG1);
7345 udelay(1);
7346 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, misccpctl);
7347}
7348
7349static void icl_init_clock_gating(struct drm_i915_private *dev_priv)
7350{
7351 /* Wa_1409120013:icl,ehl */
7352 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
7353 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7354
7355 /* This is not an Wa. Enable to reduce Sampler power */
7356 intel_uncore_write(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN,
7357 intel_uncore_read(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN) & ~DFR_DISABLE);
7358
7359 /*Wa_14010594013:icl, ehl */
7360 intel_uncore_rmw(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7361 0, CNL_DELAY_PMRSP);
7362}
7363
7364static void gen12lp_init_clock_gating(struct drm_i915_private *dev_priv)
7365{
7366 /* Wa_1409120013:tgl,rkl,adl_s,dg1 */
7367 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN,
7368 ILK_DPFC_CHICKEN_COMP_DUMMY_PIXEL);
7369
7370 /* Wa_1409825376:tgl (pre-prod)*/
7371 if (IS_TGL_DISPLAY_STEP(dev_priv, STEP_A0, STEP_B1))
7372 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_3, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_3) |
7373 TGL_VRH_GATING_DIS);
7374
7375 /* Wa_14011059788:tgl,rkl,adl_s,dg1 */
7376 intel_uncore_rmw(&dev_priv->uncore, GEN10_DFR_RATIO_EN_AND_CHICKEN,
7377 0, DFR_DISABLE);
7378
7379 /* Wa_14013723622:tgl,rkl,dg1,adl-s */
7380 if (DISPLAY_VER(dev_priv) == 12)
7381 intel_uncore_rmw(&dev_priv->uncore, CLKREQ_POLICY,
7382 CLKREQ_POLICY_MEM_UP_OVRD, 0);
7383}
7384
7385static void adlp_init_clock_gating(struct drm_i915_private *dev_priv)
7386{
7387 gen12lp_init_clock_gating(dev_priv);
7388
7389 /* Wa_22011091694:adlp */
7390 intel_de_rmw(dev_priv, GEN9_CLKGATE_DIS_5, 0, DPCE_GATING_DIS);
7391}
7392
7393static void dg1_init_clock_gating(struct drm_i915_private *dev_priv)
7394{
7395 gen12lp_init_clock_gating(dev_priv);
7396
7397 /* Wa_1409836686:dg1[a0] */
7398 if (IS_DG1_REVID(dev_priv, DG1_REVID_A0, DG1_REVID_A0))
7399 intel_uncore_write(&dev_priv->uncore, GEN9_CLKGATE_DIS_3, intel_uncore_read(&dev_priv->uncore, GEN9_CLKGATE_DIS_3) |
7400 DPT_GATING_DIS);
7401}
7402
7403static void cnp_init_clock_gating(struct drm_i915_private *dev_priv)
7404{
7405 if (!HAS_PCH_CNP(dev_priv))
7406 return;
7407
7408 /* Display WA #1181 WaSouthDisplayDisablePWMCGEGating: cnp */
7409 intel_uncore_write(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D, intel_uncore_read(&dev_priv->uncore, SOUTH_DSPCLK_GATE_D) |
7410 CNP_PWM_CGE_GATING_DISABLE);
7411}
7412
7413static void cnl_init_clock_gating(struct drm_i915_private *dev_priv)
7414{
7415 u32 val;
7416 cnp_init_clock_gating(dev_priv);
7417
7418 /* This is not an Wa. Enable for better image quality */
7419 intel_uncore_write(&dev_priv->uncore, _3D_CHICKEN3,
7420 _MASKED_BIT_ENABLE(_3D_CHICKEN3_AA_LINE_QUALITY_FIX_ENABLE));
7421
7422 /* WaEnableChickenDCPR:cnl */
7423 intel_uncore_write(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1,
7424 intel_uncore_read(&dev_priv->uncore, GEN8_CHICKEN_DCPR_1) | MASK_WAKEMEM);
7425
7426 /*
7427 * WaFbcWakeMemOn:cnl
7428 * Display WA #0859: cnl
7429 */
7430 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7431 DISP_FBC_MEMORY_WAKE);
7432
7433 val = intel_uncore_read(&dev_priv->uncore, SLICE_UNIT_LEVEL_CLKGATE);
7434 /* ReadHitWriteOnlyDisable:cnl */
7435 val |= RCCUNIT_CLKGATE_DIS;
7436 intel_uncore_write(&dev_priv->uncore, SLICE_UNIT_LEVEL_CLKGATE, val);
7437
7438 /* Wa_2201832410:cnl */
7439 val = intel_uncore_read(&dev_priv->uncore, SUBSLICE_UNIT_LEVEL_CLKGATE);
7440 val |= GWUNIT_CLKGATE_DIS;
7441 intel_uncore_write(&dev_priv->uncore, SUBSLICE_UNIT_LEVEL_CLKGATE, val);
7442
7443 /* WaDisableVFclkgate:cnl */
7444 /* WaVFUnitClockGatingDisable:cnl */
7445 val = intel_uncore_read(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE);
7446 val |= VFUNIT_CLKGATE_DIS;
7447 intel_uncore_write(&dev_priv->uncore, UNSLICE_UNIT_LEVEL_CLKGATE, val);
7448}
7449
7450static void cfl_init_clock_gating(struct drm_i915_private *dev_priv)
7451{
7452 cnp_init_clock_gating(dev_priv);
7453 gen9_init_clock_gating(dev_priv);
7454
7455 /* WAC6entrylatency:cfl */
7456 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
7457 FBC_LLC_FULLY_OPEN);
7458
7459 /*
7460 * WaFbcTurnOffFbcWatermark:cfl
7461 * Display WA #0562: cfl
7462 */
7463 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7464 DISP_FBC_WM_DIS);
7465
7466 /*
7467 * WaFbcNukeOnHostModify:cfl
7468 * Display WA #0873: cfl
7469 */
7470 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
7471 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7472}
7473
7474static void kbl_init_clock_gating(struct drm_i915_private *dev_priv)
7475{
7476 gen9_init_clock_gating(dev_priv);
7477
7478 /* WAC6entrylatency:kbl */
7479 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
7480 FBC_LLC_FULLY_OPEN);
7481
7482 /* WaDisableSDEUnitClockGating:kbl */
7483 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_B0))
7484 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
7485 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
7486
7487 /* WaDisableGamClockGating:kbl */
7488 if (IS_KBL_GT_STEP(dev_priv, 0, STEP_B0))
7489 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
7490 GEN6_GAMUNIT_CLOCK_GATE_DISABLE);
7491
7492 /*
7493 * WaFbcTurnOffFbcWatermark:kbl
7494 * Display WA #0562: kbl
7495 */
7496 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7497 DISP_FBC_WM_DIS);
7498
7499 /*
7500 * WaFbcNukeOnHostModify:kbl
7501 * Display WA #0873: kbl
7502 */
7503 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
7504 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7505}
7506
7507static void skl_init_clock_gating(struct drm_i915_private *dev_priv)
7508{
7509 gen9_init_clock_gating(dev_priv);
7510
7511 /* WaDisableDopClockGating:skl */
7512 intel_uncore_write(&dev_priv->uncore, GEN7_MISCCPCTL, intel_uncore_read(&dev_priv->uncore, GEN7_MISCCPCTL) &
7513 ~GEN7_DOP_CLOCK_GATE_ENABLE);
7514
7515 /* WAC6entrylatency:skl */
7516 intel_uncore_write(&dev_priv->uncore, FBC_LLC_READ_CTRL, intel_uncore_read(&dev_priv->uncore, FBC_LLC_READ_CTRL) |
7517 FBC_LLC_FULLY_OPEN);
7518
7519 /*
7520 * WaFbcTurnOffFbcWatermark:skl
7521 * Display WA #0562: skl
7522 */
7523 intel_uncore_write(&dev_priv->uncore, DISP_ARB_CTL, intel_uncore_read(&dev_priv->uncore, DISP_ARB_CTL) |
7524 DISP_FBC_WM_DIS);
7525
7526 /*
7527 * WaFbcNukeOnHostModify:skl
7528 * Display WA #0873: skl
7529 */
7530 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
7531 ILK_DPFC_NUKE_ON_ANY_MODIFICATION);
7532
7533 /*
7534 * WaFbcHighMemBwCorruptionAvoidance:skl
7535 * Display WA #0883: skl
7536 */
7537 intel_uncore_write(&dev_priv->uncore, ILK_DPFC_CHICKEN, intel_uncore_read(&dev_priv->uncore, ILK_DPFC_CHICKEN) |
7538 ILK_DPFC_DISABLE_DUMMY0);
7539}
7540
7541static void bdw_init_clock_gating(struct drm_i915_private *dev_priv)
7542{
7543 enum pipe pipe;
7544
7545 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
7546 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7547 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
7548 HSW_FBCQ_DIS);
7549
7550 /* WaSwitchSolVfFArbitrationPriority:bdw */
7551 intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7552
7553 /* WaPsrDPAMaskVBlankInSRD:bdw */
7554 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR1_1,
7555 intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
7556
7557 for_each_pipe(dev_priv, pipe) {
7558 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
7559 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7560 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe)) |
7561 BDW_DPRS_MASK_VBLANK_SRD);
7562
7563 /* Undocumented but fixes async flip + VT-d corruption */
7564 if (intel_vtd_active())
7565 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7566 HSW_PRI_STRETCH_MAX_MASK, HSW_PRI_STRETCH_MAX_X1);
7567 }
7568
7569 /* WaVSRefCountFullforceMissDisable:bdw */
7570 /* WaDSRefCountFullforceMissDisable:bdw */
7571 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7572 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
7573 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
7574
7575 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
7576 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
7577
7578 /* WaDisableSDEUnitClockGating:bdw */
7579 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
7580 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
7581
7582 /* WaProgramL3SqcReg1Default:bdw */
7583 gen8_set_l3sqc_credits(dev_priv, 30, 2);
7584
7585 /* WaKVMNotificationOnConfigChange:bdw */
7586 intel_uncore_write(&dev_priv->uncore, CHICKEN_PAR2_1, intel_uncore_read(&dev_priv->uncore, CHICKEN_PAR2_1)
7587 | KVM_CONFIG_CHANGE_NOTIFICATION_SELECT);
7588
7589 lpt_init_clock_gating(dev_priv);
7590
7591 /* WaDisableDopClockGating:bdw
7592 *
7593 * Also see the CHICKEN2 write in bdw_init_workarounds() to disable DOP
7594 * clock gating.
7595 */
7596 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1,
7597 intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) | GEN6_EU_TCUNIT_CLOCK_GATE_DISABLE);
7598}
7599
7600static void hsw_init_clock_gating(struct drm_i915_private *dev_priv)
7601{
7602 enum pipe pipe;
7603
7604 /* WaFbcAsynchFlipDisableFbcQueue:hsw,bdw */
7605 intel_uncore_write(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A),
7606 intel_uncore_read(&dev_priv->uncore, CHICKEN_PIPESL_1(PIPE_A)) |
7607 HSW_FBCQ_DIS);
7608
7609 for_each_pipe(dev_priv, pipe) {
7610 /* Undocumented but fixes async flip + VT-d corruption */
7611 if (intel_vtd_active())
7612 intel_uncore_rmw(&dev_priv->uncore, CHICKEN_PIPESL_1(pipe),
7613 HSW_PRI_STRETCH_MAX_MASK, HSW_PRI_STRETCH_MAX_X1);
7614 }
7615
7616 /* This is required by WaCatErrorRejectionIssue:hsw */
7617 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7618 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7619 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7620
7621 /* WaSwitchSolVfFArbitrationPriority:hsw */
7622 intel_uncore_write(&dev_priv->uncore, GAM_ECOCHK, intel_uncore_read(&dev_priv->uncore, GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
7623
7624 lpt_init_clock_gating(dev_priv);
7625}
7626
7627static void ivb_init_clock_gating(struct drm_i915_private *dev_priv)
7628{
7629 u32 snpcr;
7630
7631 intel_uncore_write(&dev_priv->uncore, ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
7632
7633 /* WaFbcAsynchFlipDisableFbcQueue:ivb */
7634 intel_uncore_write(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1,
7635 intel_uncore_read(&dev_priv->uncore, ILK_DISPLAY_CHICKEN1) |
7636 ILK_FBCQ_DIS);
7637
7638 /* WaDisableBackToBackFlipFix:ivb */
7639 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
7640 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7641 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7642
7643 if (IS_IVB_GT1(dev_priv))
7644 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
7645 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7646 else {
7647 /* must write both registers */
7648 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
7649 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7650 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2_GT2,
7651 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7652 }
7653
7654 /*
7655 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
7656 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
7657 */
7658 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
7659 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
7660
7661 /* This is required by WaCatErrorRejectionIssue:ivb */
7662 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7663 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7664 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7665
7666 g4x_disable_trickle_feed(dev_priv);
7667
7668 snpcr = intel_uncore_read(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR);
7669 snpcr &= ~GEN6_MBC_SNPCR_MASK;
7670 snpcr |= GEN6_MBC_SNPCR_MED;
7671 intel_uncore_write(&dev_priv->uncore, GEN6_MBCUNIT_SNPCR, snpcr);
7672
7673 if (!HAS_PCH_NOP(dev_priv))
7674 cpt_init_clock_gating(dev_priv);
7675
7676 gen6_check_mch_setup(dev_priv);
7677}
7678
7679static void vlv_init_clock_gating(struct drm_i915_private *dev_priv)
7680{
7681 /* WaDisableBackToBackFlipFix:vlv */
7682 intel_uncore_write(&dev_priv->uncore, IVB_CHICKEN3,
7683 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
7684 CHICKEN3_DGMG_DONE_FIX_DISABLE);
7685
7686 /* WaDisableDopClockGating:vlv */
7687 intel_uncore_write(&dev_priv->uncore, GEN7_ROW_CHICKEN2,
7688 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
7689
7690 /* This is required by WaCatErrorRejectionIssue:vlv */
7691 intel_uncore_write(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
7692 intel_uncore_read(&dev_priv->uncore, GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
7693 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
7694
7695 /*
7696 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
7697 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
7698 */
7699 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL2,
7700 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
7701
7702 /* WaDisableL3Bank2xClockGate:vlv
7703 * Disabling L3 clock gating- MMIO 940c[25] = 1
7704 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
7705 intel_uncore_write(&dev_priv->uncore, GEN7_UCGCTL4,
7706 intel_uncore_read(&dev_priv->uncore, GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
7707
7708 /*
7709 * WaDisableVLVClockGating_VBIIssue:vlv
7710 * Disable clock gating on th GCFG unit to prevent a delay
7711 * in the reporting of vblank events.
7712 */
7713 intel_uncore_write(&dev_priv->uncore, VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
7714}
7715
7716static void chv_init_clock_gating(struct drm_i915_private *dev_priv)
7717{
7718 /* WaVSRefCountFullforceMissDisable:chv */
7719 /* WaDSRefCountFullforceMissDisable:chv */
7720 intel_uncore_write(&dev_priv->uncore, GEN7_FF_THREAD_MODE,
7721 intel_uncore_read(&dev_priv->uncore, GEN7_FF_THREAD_MODE) &
7722 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
7723
7724 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7725 intel_uncore_write(&dev_priv->uncore, GEN6_RC_SLEEP_PSMI_CONTROL,
7726 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
7727
7728 /* WaDisableCSUnitClockGating:chv */
7729 intel_uncore_write(&dev_priv->uncore, GEN6_UCGCTL1, intel_uncore_read(&dev_priv->uncore, GEN6_UCGCTL1) |
7730 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
7731
7732 /* WaDisableSDEUnitClockGating:chv */
7733 intel_uncore_write(&dev_priv->uncore, GEN8_UCGCTL6, intel_uncore_read(&dev_priv->uncore, GEN8_UCGCTL6) |
7734 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
7735
7736 /*
7737 * WaProgramL3SqcReg1Default:chv
7738 * See gfxspecs/Related Documents/Performance Guide/
7739 * LSQC Setting Recommendations.
7740 */
7741 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7742}
7743
7744static void g4x_init_clock_gating(struct drm_i915_private *dev_priv)
7745{
7746 u32 dspclk_gate;
7747
7748 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, 0);
7749 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7750 GS_UNIT_CLOCK_GATE_DISABLE |
7751 CL_UNIT_CLOCK_GATE_DISABLE);
7752 intel_uncore_write(&dev_priv->uncore, RAMCLK_GATE_D, 0);
7753 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7754 OVRUNIT_CLOCK_GATE_DISABLE |
7755 OVCUNIT_CLOCK_GATE_DISABLE;
7756 if (IS_GM45(dev_priv))
7757 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7758 intel_uncore_write(&dev_priv->uncore, DSPCLK_GATE_D, dspclk_gate);
7759
7760 g4x_disable_trickle_feed(dev_priv);
7761}
7762
7763static void i965gm_init_clock_gating(struct drm_i915_private *dev_priv)
7764{
7765 struct intel_uncore *uncore = &dev_priv->uncore;
7766
7767 intel_uncore_write(uncore, RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7768 intel_uncore_write(uncore, RENCLK_GATE_D2, 0);
7769 intel_uncore_write(uncore, DSPCLK_GATE_D, 0);
7770 intel_uncore_write(uncore, RAMCLK_GATE_D, 0);
7771 intel_uncore_write16(uncore, DEUC, 0);
7772 intel_uncore_write(uncore,
7773 MI_ARB_STATE,
7774 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7775}
7776
7777static void i965g_init_clock_gating(struct drm_i915_private *dev_priv)
7778{
7779 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7780 I965_RCC_CLOCK_GATE_DISABLE |
7781 I965_RCPB_CLOCK_GATE_DISABLE |
7782 I965_ISC_CLOCK_GATE_DISABLE |
7783 I965_FBC_CLOCK_GATE_DISABLE);
7784 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D2, 0);
7785 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
7786 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7787}
7788
7789static void gen3_init_clock_gating(struct drm_i915_private *dev_priv)
7790{
7791 u32 dstate = intel_uncore_read(&dev_priv->uncore, D_STATE);
7792
7793 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7794 DSTATE_DOT_CLOCK_GATING;
7795 intel_uncore_write(&dev_priv->uncore, D_STATE, dstate);
7796
7797 if (IS_PINEVIEW(dev_priv))
7798 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
7799
7800 /* IIR "flip pending" means done if this bit is set */
7801 intel_uncore_write(&dev_priv->uncore, ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
7802
7803 /* interrupts should cause a wake up from C3 */
7804 intel_uncore_write(&dev_priv->uncore, INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
7805
7806 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7807 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
7808
7809 intel_uncore_write(&dev_priv->uncore, MI_ARB_STATE,
7810 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
7811}
7812
7813static void i85x_init_clock_gating(struct drm_i915_private *dev_priv)
7814{
7815 intel_uncore_write(&dev_priv->uncore, RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
7816
7817 /* interrupts should cause a wake up from C3 */
7818 intel_uncore_write(&dev_priv->uncore, MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7819 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
7820
7821 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
7822 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
7823
7824 /*
7825 * Have FBC ignore 3D activity since we use software
7826 * render tracking, and otherwise a pure 3D workload
7827 * (even if it just renders a single frame and then does
7828 * abosultely nothing) would not allow FBC to recompress
7829 * until a 2D blit occurs.
7830 */
7831 intel_uncore_write(&dev_priv->uncore, SCPD0,
7832 _MASKED_BIT_ENABLE(SCPD_FBC_IGNORE_3D));
7833}
7834
7835static void i830_init_clock_gating(struct drm_i915_private *dev_priv)
7836{
7837 intel_uncore_write(&dev_priv->uncore, MEM_MODE,
7838 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7839 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
7840}
7841
7842void intel_init_clock_gating(struct drm_i915_private *dev_priv)
7843{
7844 dev_priv->display.init_clock_gating(dev_priv);
7845}
7846
7847void intel_suspend_hw(struct drm_i915_private *dev_priv)
7848{
7849 if (HAS_PCH_LPT(dev_priv))
7850 lpt_suspend_hw(dev_priv);
7851}
7852
7853static void nop_init_clock_gating(struct drm_i915_private *dev_priv)
7854{
7855 drm_dbg_kms(&dev_priv->drm,
7856 "No clock gating settings or workarounds applied.\n");
7857}
7858
7859/**
7860 * intel_init_clock_gating_hooks - setup the clock gating hooks
7861 * @dev_priv: device private
7862 *
7863 * Setup the hooks that configure which clocks of a given platform can be
7864 * gated and also apply various GT and display specific workarounds for these
7865 * platforms. Note that some GT specific workarounds are applied separately
7866 * when GPU contexts or batchbuffers start their execution.
7867 */
7868void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7869{
7870 if (IS_ALDERLAKE_P(dev_priv))
7871 dev_priv->display.init_clock_gating = adlp_init_clock_gating;
7872 else if (IS_DG1(dev_priv))
7873 dev_priv->display.init_clock_gating = dg1_init_clock_gating;
7874 else if (GRAPHICS_VER(dev_priv) == 12)
7875 dev_priv->display.init_clock_gating = gen12lp_init_clock_gating;
7876 else if (GRAPHICS_VER(dev_priv) == 11)
7877 dev_priv->display.init_clock_gating = icl_init_clock_gating;
7878 else if (IS_CANNONLAKE(dev_priv))
7879 dev_priv->display.init_clock_gating = cnl_init_clock_gating;
7880 else if (IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv))
7881 dev_priv->display.init_clock_gating = cfl_init_clock_gating;
7882 else if (IS_SKYLAKE(dev_priv))
7883 dev_priv->display.init_clock_gating = skl_init_clock_gating;
7884 else if (IS_KABYLAKE(dev_priv))
7885 dev_priv->display.init_clock_gating = kbl_init_clock_gating;
7886 else if (IS_BROXTON(dev_priv))
7887 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
7888 else if (IS_GEMINILAKE(dev_priv))
7889 dev_priv->display.init_clock_gating = glk_init_clock_gating;
7890 else if (IS_BROADWELL(dev_priv))
7891 dev_priv->display.init_clock_gating = bdw_init_clock_gating;
7892 else if (IS_CHERRYVIEW(dev_priv))
7893 dev_priv->display.init_clock_gating = chv_init_clock_gating;
7894 else if (IS_HASWELL(dev_priv))
7895 dev_priv->display.init_clock_gating = hsw_init_clock_gating;
7896 else if (IS_IVYBRIDGE(dev_priv))
7897 dev_priv->display.init_clock_gating = ivb_init_clock_gating;
7898 else if (IS_VALLEYVIEW(dev_priv))
7899 dev_priv->display.init_clock_gating = vlv_init_clock_gating;
7900 else if (GRAPHICS_VER(dev_priv) == 6)
7901 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7902 else if (GRAPHICS_VER(dev_priv) == 5)
7903 dev_priv->display.init_clock_gating = ilk_init_clock_gating;
7904 else if (IS_G4X(dev_priv))
7905 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7906 else if (IS_I965GM(dev_priv))
7907 dev_priv->display.init_clock_gating = i965gm_init_clock_gating;
7908 else if (IS_I965G(dev_priv))
7909 dev_priv->display.init_clock_gating = i965g_init_clock_gating;
7910 else if (GRAPHICS_VER(dev_priv) == 3)
7911 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7912 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7913 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7914 else if (GRAPHICS_VER(dev_priv) == 2)
7915 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7916 else {
7917 MISSING_CASE(INTEL_DEVID(dev_priv));
7918 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7919 }
7920}
7921
7922/* Set up chip specific power management-related functions */
7923void intel_init_pm(struct drm_i915_private *dev_priv)
7924{
7925 /* For cxsr */
7926 if (IS_PINEVIEW(dev_priv))
7927 pnv_get_mem_freq(dev_priv);
7928 else if (GRAPHICS_VER(dev_priv) == 5)
7929 ilk_get_mem_freq(dev_priv);
7930
7931 if (intel_has_sagv(dev_priv))
7932 skl_setup_sagv_block_time(dev_priv);
7933
7934 /* For FIFO watermark updates */
7935 if (DISPLAY_VER(dev_priv) >= 9) {
7936 skl_setup_wm_latency(dev_priv);
7937 dev_priv->display.compute_global_watermarks = skl_compute_wm;
7938 } else if (HAS_PCH_SPLIT(dev_priv)) {
7939 ilk_setup_wm_latency(dev_priv);
7940
7941 if ((DISPLAY_VER(dev_priv) == 5 && dev_priv->wm.pri_latency[1] &&
7942 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7943 (DISPLAY_VER(dev_priv) != 5 && dev_priv->wm.pri_latency[0] &&
7944 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
7945 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
7946 dev_priv->display.compute_intermediate_wm =
7947 ilk_compute_intermediate_wm;
7948 dev_priv->display.initial_watermarks =
7949 ilk_initial_watermarks;
7950 dev_priv->display.optimize_watermarks =
7951 ilk_optimize_watermarks;
7952 } else {
7953 drm_dbg_kms(&dev_priv->drm,
7954 "Failed to read display plane latency. "
7955 "Disable CxSR\n");
7956 }
7957 } else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
7958 vlv_setup_wm_latency(dev_priv);
7959 dev_priv->display.compute_pipe_wm = vlv_compute_pipe_wm;
7960 dev_priv->display.compute_intermediate_wm = vlv_compute_intermediate_wm;
7961 dev_priv->display.initial_watermarks = vlv_initial_watermarks;
7962 dev_priv->display.optimize_watermarks = vlv_optimize_watermarks;
7963 dev_priv->display.atomic_update_watermarks = vlv_atomic_update_fifo;
7964 } else if (IS_G4X(dev_priv)) {
7965 g4x_setup_wm_latency(dev_priv);
7966 dev_priv->display.compute_pipe_wm = g4x_compute_pipe_wm;
7967 dev_priv->display.compute_intermediate_wm = g4x_compute_intermediate_wm;
7968 dev_priv->display.initial_watermarks = g4x_initial_watermarks;
7969 dev_priv->display.optimize_watermarks = g4x_optimize_watermarks;
7970 } else if (IS_PINEVIEW(dev_priv)) {
7971 if (!intel_get_cxsr_latency(!IS_MOBILE(dev_priv),
7972 dev_priv->is_ddr3,
7973 dev_priv->fsb_freq,
7974 dev_priv->mem_freq)) {
7975 drm_info(&dev_priv->drm,
7976 "failed to find known CxSR latency "
7977 "(found ddr%s fsb freq %d, mem freq %d), "
7978 "disabling CxSR\n",
7979 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7980 dev_priv->fsb_freq, dev_priv->mem_freq);
7981 /* Disable CxSR and never update its watermark again */
7982 intel_set_memory_cxsr(dev_priv, false);
7983 dev_priv->display.update_wm = NULL;
7984 } else
7985 dev_priv->display.update_wm = pnv_update_wm;
7986 } else if (DISPLAY_VER(dev_priv) == 4) {
7987 dev_priv->display.update_wm = i965_update_wm;
7988 } else if (DISPLAY_VER(dev_priv) == 3) {
7989 dev_priv->display.update_wm = i9xx_update_wm;
7990 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
7991 } else if (DISPLAY_VER(dev_priv) == 2) {
7992 if (INTEL_NUM_PIPES(dev_priv) == 1) {
7993 dev_priv->display.update_wm = i845_update_wm;
7994 dev_priv->display.get_fifo_size = i845_get_fifo_size;
7995 } else {
7996 dev_priv->display.update_wm = i9xx_update_wm;
7997 dev_priv->display.get_fifo_size = i830_get_fifo_size;
7998 }
7999 } else {
8000 drm_err(&dev_priv->drm,
8001 "unexpected fall-through in %s\n", __func__);
8002 }
8003}
8004
8005void intel_pm_setup(struct drm_i915_private *dev_priv)
8006{
8007 dev_priv->runtime_pm.suspended = false;
8008 atomic_set(&dev_priv->runtime_pm.wakeref_count, 0);
8009}
8010
8011static struct intel_global_state *intel_dbuf_duplicate_state(struct intel_global_obj *obj)
8012{
8013 struct intel_dbuf_state *dbuf_state;
8014
8015 dbuf_state = kmemdup(obj->state, sizeof(*dbuf_state), GFP_KERNEL);
8016 if (!dbuf_state)
8017 return NULL;
8018
8019 return &dbuf_state->base;
8020}
8021
8022static void intel_dbuf_destroy_state(struct intel_global_obj *obj,
8023 struct intel_global_state *state)
8024{
8025 kfree(state);
8026}
8027
8028static const struct intel_global_state_funcs intel_dbuf_funcs = {
8029 .atomic_duplicate_state = intel_dbuf_duplicate_state,
8030 .atomic_destroy_state = intel_dbuf_destroy_state,
8031};
8032
8033struct intel_dbuf_state *
8034intel_atomic_get_dbuf_state(struct intel_atomic_state *state)
8035{
8036 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8037 struct intel_global_state *dbuf_state;
8038
8039 dbuf_state = intel_atomic_get_global_obj_state(state, &dev_priv->dbuf.obj);
8040 if (IS_ERR(dbuf_state))
8041 return ERR_CAST(dbuf_state);
8042
8043 return to_intel_dbuf_state(dbuf_state);
8044}
8045
8046int intel_dbuf_init(struct drm_i915_private *dev_priv)
8047{
8048 struct intel_dbuf_state *dbuf_state;
8049
8050 dbuf_state = kzalloc(sizeof(*dbuf_state), GFP_KERNEL);
8051 if (!dbuf_state)
8052 return -ENOMEM;
8053
8054 intel_atomic_global_obj_init(dev_priv, &dev_priv->dbuf.obj,
8055 &dbuf_state->base, &intel_dbuf_funcs);
8056
8057 return 0;
8058}
8059
8060/*
8061 * Configure MBUS_CTL and all DBUF_CTL_S of each slice to join_mbus state before
8062 * update the request state of all DBUS slices.
8063 */
8064static void update_mbus_pre_enable(struct intel_atomic_state *state)
8065{
8066 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8067 u32 mbus_ctl, dbuf_min_tracker_val;
8068 enum dbuf_slice slice;
8069 const struct intel_dbuf_state *dbuf_state =
8070 intel_atomic_get_new_dbuf_state(state);
8071
8072 if (!IS_ALDERLAKE_P(dev_priv))
8073 return;
8074
8075 /*
8076 * TODO: Implement vblank synchronized MBUS joining changes.
8077 * Must be properly coordinated with dbuf reprogramming.
8078 */
8079 if (dbuf_state->joined_mbus) {
8080 mbus_ctl = MBUS_HASHING_MODE_1x4 | MBUS_JOIN |
8081 MBUS_JOIN_PIPE_SELECT_NONE;
8082 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(3);
8083 } else {
8084 mbus_ctl = MBUS_HASHING_MODE_2x2 |
8085 MBUS_JOIN_PIPE_SELECT_NONE;
8086 dbuf_min_tracker_val = DBUF_MIN_TRACKER_STATE_SERVICE(1);
8087 }
8088
8089 intel_de_rmw(dev_priv, MBUS_CTL,
8090 MBUS_HASHING_MODE_MASK | MBUS_JOIN |
8091 MBUS_JOIN_PIPE_SELECT_MASK, mbus_ctl);
8092
8093 for_each_dbuf_slice(dev_priv, slice)
8094 intel_de_rmw(dev_priv, DBUF_CTL_S(slice),
8095 DBUF_MIN_TRACKER_STATE_SERVICE_MASK,
8096 dbuf_min_tracker_val);
8097}
8098
8099void intel_dbuf_pre_plane_update(struct intel_atomic_state *state)
8100{
8101 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8102 const struct intel_dbuf_state *new_dbuf_state =
8103 intel_atomic_get_new_dbuf_state(state);
8104 const struct intel_dbuf_state *old_dbuf_state =
8105 intel_atomic_get_old_dbuf_state(state);
8106
8107 if (!new_dbuf_state ||
8108 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8109 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
8110 return;
8111
8112 WARN_ON(!new_dbuf_state->base.changed);
8113
8114 update_mbus_pre_enable(state);
8115 gen9_dbuf_slices_update(dev_priv,
8116 old_dbuf_state->enabled_slices |
8117 new_dbuf_state->enabled_slices);
8118}
8119
8120void intel_dbuf_post_plane_update(struct intel_atomic_state *state)
8121{
8122 struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8123 const struct intel_dbuf_state *new_dbuf_state =
8124 intel_atomic_get_new_dbuf_state(state);
8125 const struct intel_dbuf_state *old_dbuf_state =
8126 intel_atomic_get_old_dbuf_state(state);
8127
8128 if (!new_dbuf_state ||
8129 ((new_dbuf_state->enabled_slices == old_dbuf_state->enabled_slices)
8130 && (new_dbuf_state->joined_mbus == old_dbuf_state->joined_mbus)))
8131 return;
8132
8133 WARN_ON(!new_dbuf_state->base.changed);
8134
8135 gen9_dbuf_slices_update(dev_priv,
8136 new_dbuf_state->enabled_slices);
8137}