]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/gpu/drm/i915/intel_pm.c
drm/i915/execlists: Refactor common engine setup
[mirror_ubuntu-bionic-kernel.git] / drivers / gpu / drm / i915 / intel_pm.c
CommitLineData
85208be0
ED
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
2b4e57bd 28#include <linux/cpufreq.h>
85208be0
ED
29#include "i915_drv.h"
30#include "intel_drv.h"
eb48eb00
DV
31#include "../../../platform/x86/intel_ips.h"
32#include <linux/module.h>
85208be0 33
dc39fff7 34/**
18afd443
JN
35 * DOC: RC6
36 *
dc39fff7
BW
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
a82abe43
ID
57static void bxt_init_clock_gating(struct drm_device *dev)
58{
32608ca2
ID
59 struct drm_i915_private *dev_priv = dev->dev_private;
60
a7546159
NH
61 /* WaDisableSDEUnitClockGating:bxt */
62 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
63 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
64
32608ca2
ID
65 /*
66 * FIXME:
868434c5 67 * GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ applies on 3x6 GT SKUs only.
32608ca2 68 */
32608ca2 69 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
868434c5 70 GEN8_HDCUNIT_CLOCK_GATE_DISABLE_HDCREQ);
d965e7ac
ID
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);
a82abe43
ID
79}
80
c921aba8
DV
81static void i915_pineview_get_mem_freq(struct drm_device *dev)
82{
50227e1c 83 struct drm_i915_private *dev_priv = dev->dev_private;
c921aba8
DV
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{
50227e1c 122 struct drm_i915_private *dev_priv = dev->dev_private;
c921aba8
DV
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
20e4d407 148 dev_priv->ips.r_t = dev_priv->mem_freq;
c921aba8
DV
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) {
20e4d407 180 dev_priv->ips.c_m = 0;
c921aba8 181 } else if (dev_priv->fsb_freq > 3200 && dev_priv->fsb_freq <= 4800) {
20e4d407 182 dev_priv->ips.c_m = 1;
c921aba8 183 } else {
20e4d407 184 dev_priv->ips.c_m = 2;
c921aba8
DV
185 }
186}
187
b445e3b0
ED
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
63c62275 226static const struct cxsr_latency *intel_get_cxsr_latency(int is_desktop,
b445e3b0
ED
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
fc1ac8de
VS
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
cfb41411
VS
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
f4998963
VS
288#define FW_WM(value, plane) \
289 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK)
290
5209b1f4 291void intel_set_memory_cxsr(struct drm_i915_private *dev_priv, bool enable)
b445e3b0 292{
5209b1f4
ID
293 struct drm_device *dev = dev_priv->dev;
294 u32 val;
b445e3b0 295
666a4537 296 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
5209b1f4 297 I915_WRITE(FW_BLC_SELF_VLV, enable ? FW_CSPWRDWNEN : 0);
a7a6c498 298 POSTING_READ(FW_BLC_SELF_VLV);
852eb00d 299 dev_priv->wm.vlv.cxsr = enable;
5209b1f4
ID
300 } else if (IS_G4X(dev) || IS_CRESTLINE(dev)) {
301 I915_WRITE(FW_BLC_SELF, enable ? FW_BLC_SELF_EN : 0);
a7a6c498 302 POSTING_READ(FW_BLC_SELF);
5209b1f4
ID
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);
a7a6c498 307 POSTING_READ(DSPFW3);
5209b1f4
ID
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);
a7a6c498 312 POSTING_READ(FW_BLC_SELF);
5209b1f4
ID
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);
a7a6c498 317 POSTING_READ(INSTPM);
5209b1f4
ID
318 } else {
319 return;
320 }
b445e3b0 321
5209b1f4
ID
322 DRM_DEBUG_KMS("memory self-refresh is %s\n",
323 enable ? "enabled" : "disabled");
b445e3b0
ED
324}
325
fc1ac8de 326
b445e3b0
ED
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 */
5aef6003 341static const int pessimal_latency_ns = 5000;
b445e3b0 342
b5004720
VS
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
1fa61106 398static int i9xx_get_fifo_size(struct drm_device *dev, int plane)
b445e3b0
ED
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
feb56b93 414static int i830_get_fifo_size(struct drm_device *dev, int plane)
b445e3b0
ED
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
1fa61106 431static int i845_get_fifo_size(struct drm_device *dev, int plane)
b445e3b0
ED
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
b445e3b0
ED
447/* Pineview has different values for various configs */
448static const struct intel_watermark_params pineview_display_wm = {
e0f0273e
VS
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,
b445e3b0
ED
454};
455static const struct intel_watermark_params pineview_display_hplloff_wm = {
e0f0273e
VS
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,
b445e3b0
ED
461};
462static const struct intel_watermark_params pineview_cursor_wm = {
e0f0273e
VS
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,
b445e3b0
ED
468};
469static const struct intel_watermark_params pineview_cursor_hplloff_wm = {
e0f0273e
VS
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,
b445e3b0
ED
475};
476static const struct intel_watermark_params g4x_wm_info = {
e0f0273e
VS
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,
b445e3b0
ED
482};
483static const struct intel_watermark_params g4x_cursor_wm_info = {
e0f0273e
VS
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,
b445e3b0 489};
b445e3b0 490static const struct intel_watermark_params i965_cursor_wm_info = {
e0f0273e
VS
491 .fifo_size = I965_CURSOR_FIFO,
492 .max_wm = I965_CURSOR_MAX_WM,
493 .default_wm = I965_CURSOR_DFT_WM,
494 .guard_size = 2,
495 .cacheline_size = I915_FIFO_LINE_SIZE,
b445e3b0
ED
496};
497static const struct intel_watermark_params i945_wm_info = {
e0f0273e
VS
498 .fifo_size = I945_FIFO_SIZE,
499 .max_wm = I915_MAX_WM,
500 .default_wm = 1,
501 .guard_size = 2,
502 .cacheline_size = I915_FIFO_LINE_SIZE,
b445e3b0
ED
503};
504static const struct intel_watermark_params i915_wm_info = {
e0f0273e
VS
505 .fifo_size = I915_FIFO_SIZE,
506 .max_wm = I915_MAX_WM,
507 .default_wm = 1,
508 .guard_size = 2,
509 .cacheline_size = I915_FIFO_LINE_SIZE,
b445e3b0 510};
9d539105 511static const struct intel_watermark_params i830_a_wm_info = {
e0f0273e
VS
512 .fifo_size = I855GM_FIFO_SIZE,
513 .max_wm = I915_MAX_WM,
514 .default_wm = 1,
515 .guard_size = 2,
516 .cacheline_size = I830_FIFO_LINE_SIZE,
b445e3b0 517};
9d539105
VS
518static const struct intel_watermark_params i830_bc_wm_info = {
519 .fifo_size = I855GM_FIFO_SIZE,
520 .max_wm = I915_MAX_WM/2,
521 .default_wm = 1,
522 .guard_size = 2,
523 .cacheline_size = I830_FIFO_LINE_SIZE,
524};
feb56b93 525static const struct intel_watermark_params i845_wm_info = {
e0f0273e
VS
526 .fifo_size = I830_FIFO_SIZE,
527 .max_wm = I915_MAX_WM,
528 .default_wm = 1,
529 .guard_size = 2,
530 .cacheline_size = I830_FIFO_LINE_SIZE,
b445e3b0
ED
531};
532
b445e3b0
ED
533/**
534 * intel_calculate_wm - calculate watermark level
535 * @clock_in_khz: pixel clock
536 * @wm: chip FIFO params
ac484963 537 * @cpp: bytes per pixel
b445e3b0
ED
538 * @latency_ns: memory latency for the platform
539 *
540 * Calculate the watermark level (the level at which the display plane will
541 * start fetching from memory again). Each chip has a different display
542 * FIFO size and allocation, so the caller needs to figure that out and pass
543 * in the correct intel_watermark_params structure.
544 *
545 * As the pixel clock runs, the FIFO will be drained at a rate that depends
546 * on the pixel size. When it reaches the watermark level, it'll start
547 * fetching FIFO line sized based chunks from memory until the FIFO fills
548 * past the watermark point. If the FIFO drains completely, a FIFO underrun
549 * will occur, and a display engine hang could result.
550 */
551static unsigned long intel_calculate_wm(unsigned long clock_in_khz,
552 const struct intel_watermark_params *wm,
ac484963 553 int fifo_size, int cpp,
b445e3b0
ED
554 unsigned long latency_ns)
555{
556 long entries_required, wm_size;
557
558 /*
559 * Note: we need to make sure we don't overflow for various clock &
560 * latency values.
561 * clocks go from a few thousand to several hundred thousand.
562 * latency is usually a few thousand
563 */
ac484963 564 entries_required = ((clock_in_khz / 1000) * cpp * latency_ns) /
b445e3b0
ED
565 1000;
566 entries_required = DIV_ROUND_UP(entries_required, wm->cacheline_size);
567
568 DRM_DEBUG_KMS("FIFO entries required for mode: %ld\n", entries_required);
569
570 wm_size = fifo_size - (entries_required + wm->guard_size);
571
572 DRM_DEBUG_KMS("FIFO watermark level: %ld\n", wm_size);
573
574 /* Don't promote wm_size to unsigned... */
575 if (wm_size > (long)wm->max_wm)
576 wm_size = wm->max_wm;
577 if (wm_size <= 0)
578 wm_size = wm->default_wm;
d6feb196
VS
579
580 /*
581 * Bspec seems to indicate that the value shouldn't be lower than
582 * 'burst size + 1'. Certainly 830 is quite unhappy with low values.
583 * Lets go for 8 which is the burst size since certain platforms
584 * already use a hardcoded 8 (which is what the spec says should be
585 * done).
586 */
587 if (wm_size <= 8)
588 wm_size = 8;
589
b445e3b0
ED
590 return wm_size;
591}
592
593static struct drm_crtc *single_enabled_crtc(struct drm_device *dev)
594{
595 struct drm_crtc *crtc, *enabled = NULL;
596
70e1e0ec 597 for_each_crtc(dev, crtc) {
3490ea5d 598 if (intel_crtc_active(crtc)) {
b445e3b0
ED
599 if (enabled)
600 return NULL;
601 enabled = crtc;
602 }
603 }
604
605 return enabled;
606}
607
46ba614c 608static void pineview_update_wm(struct drm_crtc *unused_crtc)
b445e3b0 609{
46ba614c 610 struct drm_device *dev = unused_crtc->dev;
b445e3b0
ED
611 struct drm_i915_private *dev_priv = dev->dev_private;
612 struct drm_crtc *crtc;
613 const struct cxsr_latency *latency;
614 u32 reg;
615 unsigned long wm;
616
617 latency = intel_get_cxsr_latency(IS_PINEVIEW_G(dev), dev_priv->is_ddr3,
618 dev_priv->fsb_freq, dev_priv->mem_freq);
619 if (!latency) {
620 DRM_DEBUG_KMS("Unknown FSB/MEM found, disable CxSR\n");
5209b1f4 621 intel_set_memory_cxsr(dev_priv, false);
b445e3b0
ED
622 return;
623 }
624
625 crtc = single_enabled_crtc(dev);
626 if (crtc) {
7c5f93b0 627 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
ac484963 628 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
7c5f93b0 629 int clock = adjusted_mode->crtc_clock;
b445e3b0
ED
630
631 /* Display SR */
632 wm = intel_calculate_wm(clock, &pineview_display_wm,
633 pineview_display_wm.fifo_size,
ac484963 634 cpp, latency->display_sr);
b445e3b0
ED
635 reg = I915_READ(DSPFW1);
636 reg &= ~DSPFW_SR_MASK;
f4998963 637 reg |= FW_WM(wm, SR);
b445e3b0
ED
638 I915_WRITE(DSPFW1, reg);
639 DRM_DEBUG_KMS("DSPFW1 register is %x\n", reg);
640
641 /* cursor SR */
642 wm = intel_calculate_wm(clock, &pineview_cursor_wm,
643 pineview_display_wm.fifo_size,
ac484963 644 cpp, latency->cursor_sr);
b445e3b0
ED
645 reg = I915_READ(DSPFW3);
646 reg &= ~DSPFW_CURSOR_SR_MASK;
f4998963 647 reg |= FW_WM(wm, CURSOR_SR);
b445e3b0
ED
648 I915_WRITE(DSPFW3, reg);
649
650 /* Display HPLL off SR */
651 wm = intel_calculate_wm(clock, &pineview_display_hplloff_wm,
652 pineview_display_hplloff_wm.fifo_size,
ac484963 653 cpp, latency->display_hpll_disable);
b445e3b0
ED
654 reg = I915_READ(DSPFW3);
655 reg &= ~DSPFW_HPLL_SR_MASK;
f4998963 656 reg |= FW_WM(wm, HPLL_SR);
b445e3b0
ED
657 I915_WRITE(DSPFW3, reg);
658
659 /* cursor HPLL off SR */
660 wm = intel_calculate_wm(clock, &pineview_cursor_hplloff_wm,
661 pineview_display_hplloff_wm.fifo_size,
ac484963 662 cpp, latency->cursor_hpll_disable);
b445e3b0
ED
663 reg = I915_READ(DSPFW3);
664 reg &= ~DSPFW_HPLL_CURSOR_MASK;
f4998963 665 reg |= FW_WM(wm, HPLL_CURSOR);
b445e3b0
ED
666 I915_WRITE(DSPFW3, reg);
667 DRM_DEBUG_KMS("DSPFW3 register is %x\n", reg);
668
5209b1f4 669 intel_set_memory_cxsr(dev_priv, true);
b445e3b0 670 } else {
5209b1f4 671 intel_set_memory_cxsr(dev_priv, false);
b445e3b0
ED
672 }
673}
674
675static bool g4x_compute_wm0(struct drm_device *dev,
676 int plane,
677 const struct intel_watermark_params *display,
678 int display_latency_ns,
679 const struct intel_watermark_params *cursor,
680 int cursor_latency_ns,
681 int *plane_wm,
682 int *cursor_wm)
683{
684 struct drm_crtc *crtc;
4fe8590a 685 const struct drm_display_mode *adjusted_mode;
ac484963 686 int htotal, hdisplay, clock, cpp;
b445e3b0
ED
687 int line_time_us, line_count;
688 int entries, tlb_miss;
689
690 crtc = intel_get_crtc_for_plane(dev, plane);
3490ea5d 691 if (!intel_crtc_active(crtc)) {
b445e3b0
ED
692 *cursor_wm = cursor->guard_size;
693 *plane_wm = display->guard_size;
694 return false;
695 }
696
6e3c9717 697 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
241bfc38 698 clock = adjusted_mode->crtc_clock;
fec8cba3 699 htotal = adjusted_mode->crtc_htotal;
6e3c9717 700 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
ac484963 701 cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
b445e3b0
ED
702
703 /* Use the small buffer method to calculate plane watermark */
ac484963 704 entries = ((clock * cpp / 1000) * display_latency_ns) / 1000;
b445e3b0
ED
705 tlb_miss = display->fifo_size*display->cacheline_size - hdisplay * 8;
706 if (tlb_miss > 0)
707 entries += tlb_miss;
708 entries = DIV_ROUND_UP(entries, display->cacheline_size);
709 *plane_wm = entries + display->guard_size;
710 if (*plane_wm > (int)display->max_wm)
711 *plane_wm = display->max_wm;
712
713 /* Use the large buffer method to calculate cursor watermark */
922044c9 714 line_time_us = max(htotal * 1000 / clock, 1);
b445e3b0 715 line_count = (cursor_latency_ns / line_time_us + 1000) / 1000;
ac484963 716 entries = line_count * crtc->cursor->state->crtc_w * cpp;
b445e3b0
ED
717 tlb_miss = cursor->fifo_size*cursor->cacheline_size - hdisplay * 8;
718 if (tlb_miss > 0)
719 entries += tlb_miss;
720 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
721 *cursor_wm = entries + cursor->guard_size;
722 if (*cursor_wm > (int)cursor->max_wm)
723 *cursor_wm = (int)cursor->max_wm;
724
725 return true;
726}
727
728/*
729 * Check the wm result.
730 *
731 * If any calculated watermark values is larger than the maximum value that
732 * can be programmed into the associated watermark register, that watermark
733 * must be disabled.
734 */
735static bool g4x_check_srwm(struct drm_device *dev,
736 int display_wm, int cursor_wm,
737 const struct intel_watermark_params *display,
738 const struct intel_watermark_params *cursor)
739{
740 DRM_DEBUG_KMS("SR watermark: display plane %d, cursor %d\n",
741 display_wm, cursor_wm);
742
743 if (display_wm > display->max_wm) {
744 DRM_DEBUG_KMS("display watermark is too large(%d/%ld), disabling\n",
745 display_wm, display->max_wm);
746 return false;
747 }
748
749 if (cursor_wm > cursor->max_wm) {
750 DRM_DEBUG_KMS("cursor watermark is too large(%d/%ld), disabling\n",
751 cursor_wm, cursor->max_wm);
752 return false;
753 }
754
755 if (!(display_wm || cursor_wm)) {
756 DRM_DEBUG_KMS("SR latency is 0, disabling\n");
757 return false;
758 }
759
760 return true;
761}
762
763static bool g4x_compute_srwm(struct drm_device *dev,
764 int plane,
765 int latency_ns,
766 const struct intel_watermark_params *display,
767 const struct intel_watermark_params *cursor,
768 int *display_wm, int *cursor_wm)
769{
770 struct drm_crtc *crtc;
4fe8590a 771 const struct drm_display_mode *adjusted_mode;
ac484963 772 int hdisplay, htotal, cpp, clock;
b445e3b0
ED
773 unsigned long line_time_us;
774 int line_count, line_size;
775 int small, large;
776 int entries;
777
778 if (!latency_ns) {
779 *display_wm = *cursor_wm = 0;
780 return false;
781 }
782
783 crtc = intel_get_crtc_for_plane(dev, plane);
6e3c9717 784 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
241bfc38 785 clock = adjusted_mode->crtc_clock;
fec8cba3 786 htotal = adjusted_mode->crtc_htotal;
6e3c9717 787 hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
ac484963 788 cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
b445e3b0 789
922044c9 790 line_time_us = max(htotal * 1000 / clock, 1);
b445e3b0 791 line_count = (latency_ns / line_time_us + 1000) / 1000;
ac484963 792 line_size = hdisplay * cpp;
b445e3b0
ED
793
794 /* Use the minimum of the small and large buffer method for primary */
ac484963 795 small = ((clock * cpp / 1000) * latency_ns) / 1000;
b445e3b0
ED
796 large = line_count * line_size;
797
798 entries = DIV_ROUND_UP(min(small, large), display->cacheline_size);
799 *display_wm = entries + display->guard_size;
800
801 /* calculate the self-refresh watermark for display cursor */
ac484963 802 entries = line_count * cpp * crtc->cursor->state->crtc_w;
b445e3b0
ED
803 entries = DIV_ROUND_UP(entries, cursor->cacheline_size);
804 *cursor_wm = entries + cursor->guard_size;
805
806 return g4x_check_srwm(dev,
807 *display_wm, *cursor_wm,
808 display, cursor);
809}
810
15665979
VS
811#define FW_WM_VLV(value, plane) \
812 (((value) << DSPFW_ ## plane ## _SHIFT) & DSPFW_ ## plane ## _MASK_VLV)
813
0018fda1
VS
814static void vlv_write_wm_values(struct intel_crtc *crtc,
815 const struct vlv_wm_values *wm)
816{
817 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
818 enum pipe pipe = crtc->pipe;
819
820 I915_WRITE(VLV_DDL(pipe),
821 (wm->ddl[pipe].cursor << DDL_CURSOR_SHIFT) |
822 (wm->ddl[pipe].sprite[1] << DDL_SPRITE_SHIFT(1)) |
823 (wm->ddl[pipe].sprite[0] << DDL_SPRITE_SHIFT(0)) |
824 (wm->ddl[pipe].primary << DDL_PLANE_SHIFT));
825
ae80152d 826 I915_WRITE(DSPFW1,
15665979
VS
827 FW_WM(wm->sr.plane, SR) |
828 FW_WM(wm->pipe[PIPE_B].cursor, CURSORB) |
829 FW_WM_VLV(wm->pipe[PIPE_B].primary, PLANEB) |
830 FW_WM_VLV(wm->pipe[PIPE_A].primary, PLANEA));
ae80152d 831 I915_WRITE(DSPFW2,
15665979
VS
832 FW_WM_VLV(wm->pipe[PIPE_A].sprite[1], SPRITEB) |
833 FW_WM(wm->pipe[PIPE_A].cursor, CURSORA) |
834 FW_WM_VLV(wm->pipe[PIPE_A].sprite[0], SPRITEA));
ae80152d 835 I915_WRITE(DSPFW3,
15665979 836 FW_WM(wm->sr.cursor, CURSOR_SR));
ae80152d
VS
837
838 if (IS_CHERRYVIEW(dev_priv)) {
839 I915_WRITE(DSPFW7_CHV,
15665979
VS
840 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
841 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
ae80152d 842 I915_WRITE(DSPFW8_CHV,
15665979
VS
843 FW_WM_VLV(wm->pipe[PIPE_C].sprite[1], SPRITEF) |
844 FW_WM_VLV(wm->pipe[PIPE_C].sprite[0], SPRITEE));
ae80152d 845 I915_WRITE(DSPFW9_CHV,
15665979
VS
846 FW_WM_VLV(wm->pipe[PIPE_C].primary, PLANEC) |
847 FW_WM(wm->pipe[PIPE_C].cursor, CURSORC));
ae80152d 848 I915_WRITE(DSPHOWM,
15665979
VS
849 FW_WM(wm->sr.plane >> 9, SR_HI) |
850 FW_WM(wm->pipe[PIPE_C].sprite[1] >> 8, SPRITEF_HI) |
851 FW_WM(wm->pipe[PIPE_C].sprite[0] >> 8, SPRITEE_HI) |
852 FW_WM(wm->pipe[PIPE_C].primary >> 8, PLANEC_HI) |
853 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
854 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
855 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
856 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
857 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
858 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
ae80152d
VS
859 } else {
860 I915_WRITE(DSPFW7,
15665979
VS
861 FW_WM_VLV(wm->pipe[PIPE_B].sprite[1], SPRITED) |
862 FW_WM_VLV(wm->pipe[PIPE_B].sprite[0], SPRITEC));
ae80152d 863 I915_WRITE(DSPHOWM,
15665979
VS
864 FW_WM(wm->sr.plane >> 9, SR_HI) |
865 FW_WM(wm->pipe[PIPE_B].sprite[1] >> 8, SPRITED_HI) |
866 FW_WM(wm->pipe[PIPE_B].sprite[0] >> 8, SPRITEC_HI) |
867 FW_WM(wm->pipe[PIPE_B].primary >> 8, PLANEB_HI) |
868 FW_WM(wm->pipe[PIPE_A].sprite[1] >> 8, SPRITEB_HI) |
869 FW_WM(wm->pipe[PIPE_A].sprite[0] >> 8, SPRITEA_HI) |
870 FW_WM(wm->pipe[PIPE_A].primary >> 8, PLANEA_HI));
ae80152d
VS
871 }
872
2cb389b7
VS
873 /* zero (unused) WM1 watermarks */
874 I915_WRITE(DSPFW4, 0);
875 I915_WRITE(DSPFW5, 0);
876 I915_WRITE(DSPFW6, 0);
877 I915_WRITE(DSPHOWM1, 0);
878
ae80152d 879 POSTING_READ(DSPFW1);
0018fda1
VS
880}
881
15665979
VS
882#undef FW_WM_VLV
883
6eb1a681
VS
884enum vlv_wm_level {
885 VLV_WM_LEVEL_PM2,
886 VLV_WM_LEVEL_PM5,
887 VLV_WM_LEVEL_DDR_DVFS,
6eb1a681
VS
888};
889
262cd2e1
VS
890/* latency must be in 0.1us units. */
891static unsigned int vlv_wm_method2(unsigned int pixel_rate,
892 unsigned int pipe_htotal,
893 unsigned int horiz_pixels,
ac484963 894 unsigned int cpp,
262cd2e1
VS
895 unsigned int latency)
896{
897 unsigned int ret;
898
899 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
ac484963 900 ret = (ret + 1) * horiz_pixels * cpp;
262cd2e1
VS
901 ret = DIV_ROUND_UP(ret, 64);
902
903 return ret;
904}
905
906static void vlv_setup_wm_latency(struct drm_device *dev)
907{
908 struct drm_i915_private *dev_priv = dev->dev_private;
909
910 /* all latencies in usec */
911 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM2] = 3;
912
58590c14
VS
913 dev_priv->wm.max_level = VLV_WM_LEVEL_PM2;
914
262cd2e1
VS
915 if (IS_CHERRYVIEW(dev_priv)) {
916 dev_priv->wm.pri_latency[VLV_WM_LEVEL_PM5] = 12;
917 dev_priv->wm.pri_latency[VLV_WM_LEVEL_DDR_DVFS] = 33;
58590c14
VS
918
919 dev_priv->wm.max_level = VLV_WM_LEVEL_DDR_DVFS;
262cd2e1
VS
920 }
921}
922
923static uint16_t vlv_compute_wm_level(struct intel_plane *plane,
924 struct intel_crtc *crtc,
925 const struct intel_plane_state *state,
926 int level)
927{
928 struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
ac484963 929 int clock, htotal, cpp, width, wm;
262cd2e1
VS
930
931 if (dev_priv->wm.pri_latency[level] == 0)
932 return USHRT_MAX;
933
934 if (!state->visible)
935 return 0;
936
ac484963 937 cpp = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
262cd2e1
VS
938 clock = crtc->config->base.adjusted_mode.crtc_clock;
939 htotal = crtc->config->base.adjusted_mode.crtc_htotal;
940 width = crtc->config->pipe_src_w;
941 if (WARN_ON(htotal == 0))
942 htotal = 1;
943
944 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
945 /*
946 * FIXME the formula gives values that are
947 * too big for the cursor FIFO, and hence we
948 * would never be able to use cursors. For
949 * now just hardcode the watermark.
950 */
951 wm = 63;
952 } else {
ac484963 953 wm = vlv_wm_method2(clock, htotal, width, cpp,
262cd2e1
VS
954 dev_priv->wm.pri_latency[level] * 10);
955 }
956
957 return min_t(int, wm, USHRT_MAX);
958}
959
54f1b6e1
VS
960static void vlv_compute_fifo(struct intel_crtc *crtc)
961{
962 struct drm_device *dev = crtc->base.dev;
963 struct vlv_wm_state *wm_state = &crtc->wm_state;
964 struct intel_plane *plane;
965 unsigned int total_rate = 0;
966 const int fifo_size = 512 - 1;
967 int fifo_extra, fifo_left = fifo_size;
968
969 for_each_intel_plane_on_crtc(dev, crtc, plane) {
970 struct intel_plane_state *state =
971 to_intel_plane_state(plane->base.state);
972
973 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
974 continue;
975
976 if (state->visible) {
977 wm_state->num_active_planes++;
978 total_rate += drm_format_plane_cpp(state->base.fb->pixel_format, 0);
979 }
980 }
981
982 for_each_intel_plane_on_crtc(dev, crtc, plane) {
983 struct intel_plane_state *state =
984 to_intel_plane_state(plane->base.state);
985 unsigned int rate;
986
987 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
988 plane->wm.fifo_size = 63;
989 continue;
990 }
991
992 if (!state->visible) {
993 plane->wm.fifo_size = 0;
994 continue;
995 }
996
997 rate = drm_format_plane_cpp(state->base.fb->pixel_format, 0);
998 plane->wm.fifo_size = fifo_size * rate / total_rate;
999 fifo_left -= plane->wm.fifo_size;
1000 }
1001
1002 fifo_extra = DIV_ROUND_UP(fifo_left, wm_state->num_active_planes ?: 1);
1003
1004 /* spread the remainder evenly */
1005 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1006 int plane_extra;
1007
1008 if (fifo_left == 0)
1009 break;
1010
1011 if (plane->base.type == DRM_PLANE_TYPE_CURSOR)
1012 continue;
1013
1014 /* give it all to the first plane if none are active */
1015 if (plane->wm.fifo_size == 0 &&
1016 wm_state->num_active_planes)
1017 continue;
1018
1019 plane_extra = min(fifo_extra, fifo_left);
1020 plane->wm.fifo_size += plane_extra;
1021 fifo_left -= plane_extra;
1022 }
1023
1024 WARN_ON(fifo_left != 0);
1025}
1026
262cd2e1
VS
1027static void vlv_invert_wms(struct intel_crtc *crtc)
1028{
1029 struct vlv_wm_state *wm_state = &crtc->wm_state;
1030 int level;
1031
1032 for (level = 0; level < wm_state->num_levels; level++) {
1033 struct drm_device *dev = crtc->base.dev;
1034 const int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1035 struct intel_plane *plane;
1036
1037 wm_state->sr[level].plane = sr_fifo_size - wm_state->sr[level].plane;
1038 wm_state->sr[level].cursor = 63 - wm_state->sr[level].cursor;
1039
1040 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1041 switch (plane->base.type) {
1042 int sprite;
1043 case DRM_PLANE_TYPE_CURSOR:
1044 wm_state->wm[level].cursor = plane->wm.fifo_size -
1045 wm_state->wm[level].cursor;
1046 break;
1047 case DRM_PLANE_TYPE_PRIMARY:
1048 wm_state->wm[level].primary = plane->wm.fifo_size -
1049 wm_state->wm[level].primary;
1050 break;
1051 case DRM_PLANE_TYPE_OVERLAY:
1052 sprite = plane->plane;
1053 wm_state->wm[level].sprite[sprite] = plane->wm.fifo_size -
1054 wm_state->wm[level].sprite[sprite];
1055 break;
1056 }
1057 }
1058 }
1059}
1060
26e1fe4f 1061static void vlv_compute_wm(struct intel_crtc *crtc)
262cd2e1
VS
1062{
1063 struct drm_device *dev = crtc->base.dev;
1064 struct vlv_wm_state *wm_state = &crtc->wm_state;
1065 struct intel_plane *plane;
1066 int sr_fifo_size = INTEL_INFO(dev)->num_pipes * 512 - 1;
1067 int level;
1068
1069 memset(wm_state, 0, sizeof(*wm_state));
1070
852eb00d 1071 wm_state->cxsr = crtc->pipe != PIPE_C && crtc->wm.cxsr_allowed;
58590c14 1072 wm_state->num_levels = to_i915(dev)->wm.max_level + 1;
262cd2e1
VS
1073
1074 wm_state->num_active_planes = 0;
262cd2e1 1075
54f1b6e1 1076 vlv_compute_fifo(crtc);
262cd2e1
VS
1077
1078 if (wm_state->num_active_planes != 1)
1079 wm_state->cxsr = false;
1080
1081 if (wm_state->cxsr) {
1082 for (level = 0; level < wm_state->num_levels; level++) {
1083 wm_state->sr[level].plane = sr_fifo_size;
1084 wm_state->sr[level].cursor = 63;
1085 }
1086 }
1087
1088 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1089 struct intel_plane_state *state =
1090 to_intel_plane_state(plane->base.state);
1091
1092 if (!state->visible)
1093 continue;
1094
1095 /* normal watermarks */
1096 for (level = 0; level < wm_state->num_levels; level++) {
1097 int wm = vlv_compute_wm_level(plane, crtc, state, level);
1098 int max_wm = plane->base.type == DRM_PLANE_TYPE_CURSOR ? 63 : 511;
1099
1100 /* hack */
1101 if (WARN_ON(level == 0 && wm > max_wm))
1102 wm = max_wm;
1103
1104 if (wm > plane->wm.fifo_size)
1105 break;
1106
1107 switch (plane->base.type) {
1108 int sprite;
1109 case DRM_PLANE_TYPE_CURSOR:
1110 wm_state->wm[level].cursor = wm;
1111 break;
1112 case DRM_PLANE_TYPE_PRIMARY:
1113 wm_state->wm[level].primary = wm;
1114 break;
1115 case DRM_PLANE_TYPE_OVERLAY:
1116 sprite = plane->plane;
1117 wm_state->wm[level].sprite[sprite] = wm;
1118 break;
1119 }
1120 }
1121
1122 wm_state->num_levels = level;
1123
1124 if (!wm_state->cxsr)
1125 continue;
1126
1127 /* maxfifo watermarks */
1128 switch (plane->base.type) {
1129 int sprite, level;
1130 case DRM_PLANE_TYPE_CURSOR:
1131 for (level = 0; level < wm_state->num_levels; level++)
1132 wm_state->sr[level].cursor =
5a37ed0a 1133 wm_state->wm[level].cursor;
262cd2e1
VS
1134 break;
1135 case DRM_PLANE_TYPE_PRIMARY:
1136 for (level = 0; level < wm_state->num_levels; level++)
1137 wm_state->sr[level].plane =
1138 min(wm_state->sr[level].plane,
1139 wm_state->wm[level].primary);
1140 break;
1141 case DRM_PLANE_TYPE_OVERLAY:
1142 sprite = plane->plane;
1143 for (level = 0; level < wm_state->num_levels; level++)
1144 wm_state->sr[level].plane =
1145 min(wm_state->sr[level].plane,
1146 wm_state->wm[level].sprite[sprite]);
1147 break;
1148 }
1149 }
1150
1151 /* clear any (partially) filled invalid levels */
58590c14 1152 for (level = wm_state->num_levels; level < to_i915(dev)->wm.max_level + 1; level++) {
262cd2e1
VS
1153 memset(&wm_state->wm[level], 0, sizeof(wm_state->wm[level]));
1154 memset(&wm_state->sr[level], 0, sizeof(wm_state->sr[level]));
1155 }
1156
1157 vlv_invert_wms(crtc);
1158}
1159
54f1b6e1
VS
1160#define VLV_FIFO(plane, value) \
1161 (((value) << DSPARB_ ## plane ## _SHIFT_VLV) & DSPARB_ ## plane ## _MASK_VLV)
1162
1163static void vlv_pipe_set_fifo_size(struct intel_crtc *crtc)
1164{
1165 struct drm_device *dev = crtc->base.dev;
1166 struct drm_i915_private *dev_priv = to_i915(dev);
1167 struct intel_plane *plane;
1168 int sprite0_start = 0, sprite1_start = 0, fifo_size = 0;
1169
1170 for_each_intel_plane_on_crtc(dev, crtc, plane) {
1171 if (plane->base.type == DRM_PLANE_TYPE_CURSOR) {
1172 WARN_ON(plane->wm.fifo_size != 63);
1173 continue;
1174 }
1175
1176 if (plane->base.type == DRM_PLANE_TYPE_PRIMARY)
1177 sprite0_start = plane->wm.fifo_size;
1178 else if (plane->plane == 0)
1179 sprite1_start = sprite0_start + plane->wm.fifo_size;
1180 else
1181 fifo_size = sprite1_start + plane->wm.fifo_size;
1182 }
1183
1184 WARN_ON(fifo_size != 512 - 1);
1185
1186 DRM_DEBUG_KMS("Pipe %c FIFO split %d / %d / %d\n",
1187 pipe_name(crtc->pipe), sprite0_start,
1188 sprite1_start, fifo_size);
1189
1190 switch (crtc->pipe) {
1191 uint32_t dsparb, dsparb2, dsparb3;
1192 case PIPE_A:
1193 dsparb = I915_READ(DSPARB);
1194 dsparb2 = I915_READ(DSPARB2);
1195
1196 dsparb &= ~(VLV_FIFO(SPRITEA, 0xff) |
1197 VLV_FIFO(SPRITEB, 0xff));
1198 dsparb |= (VLV_FIFO(SPRITEA, sprite0_start) |
1199 VLV_FIFO(SPRITEB, sprite1_start));
1200
1201 dsparb2 &= ~(VLV_FIFO(SPRITEA_HI, 0x1) |
1202 VLV_FIFO(SPRITEB_HI, 0x1));
1203 dsparb2 |= (VLV_FIFO(SPRITEA_HI, sprite0_start >> 8) |
1204 VLV_FIFO(SPRITEB_HI, sprite1_start >> 8));
1205
1206 I915_WRITE(DSPARB, dsparb);
1207 I915_WRITE(DSPARB2, dsparb2);
1208 break;
1209 case PIPE_B:
1210 dsparb = I915_READ(DSPARB);
1211 dsparb2 = I915_READ(DSPARB2);
1212
1213 dsparb &= ~(VLV_FIFO(SPRITEC, 0xff) |
1214 VLV_FIFO(SPRITED, 0xff));
1215 dsparb |= (VLV_FIFO(SPRITEC, sprite0_start) |
1216 VLV_FIFO(SPRITED, sprite1_start));
1217
1218 dsparb2 &= ~(VLV_FIFO(SPRITEC_HI, 0xff) |
1219 VLV_FIFO(SPRITED_HI, 0xff));
1220 dsparb2 |= (VLV_FIFO(SPRITEC_HI, sprite0_start >> 8) |
1221 VLV_FIFO(SPRITED_HI, sprite1_start >> 8));
1222
1223 I915_WRITE(DSPARB, dsparb);
1224 I915_WRITE(DSPARB2, dsparb2);
1225 break;
1226 case PIPE_C:
1227 dsparb3 = I915_READ(DSPARB3);
1228 dsparb2 = I915_READ(DSPARB2);
1229
1230 dsparb3 &= ~(VLV_FIFO(SPRITEE, 0xff) |
1231 VLV_FIFO(SPRITEF, 0xff));
1232 dsparb3 |= (VLV_FIFO(SPRITEE, sprite0_start) |
1233 VLV_FIFO(SPRITEF, sprite1_start));
1234
1235 dsparb2 &= ~(VLV_FIFO(SPRITEE_HI, 0xff) |
1236 VLV_FIFO(SPRITEF_HI, 0xff));
1237 dsparb2 |= (VLV_FIFO(SPRITEE_HI, sprite0_start >> 8) |
1238 VLV_FIFO(SPRITEF_HI, sprite1_start >> 8));
1239
1240 I915_WRITE(DSPARB3, dsparb3);
1241 I915_WRITE(DSPARB2, dsparb2);
1242 break;
1243 default:
1244 break;
1245 }
1246}
1247
1248#undef VLV_FIFO
1249
262cd2e1
VS
1250static void vlv_merge_wm(struct drm_device *dev,
1251 struct vlv_wm_values *wm)
1252{
1253 struct intel_crtc *crtc;
1254 int num_active_crtcs = 0;
1255
58590c14 1256 wm->level = to_i915(dev)->wm.max_level;
262cd2e1
VS
1257 wm->cxsr = true;
1258
1259 for_each_intel_crtc(dev, crtc) {
1260 const struct vlv_wm_state *wm_state = &crtc->wm_state;
1261
1262 if (!crtc->active)
1263 continue;
1264
1265 if (!wm_state->cxsr)
1266 wm->cxsr = false;
1267
1268 num_active_crtcs++;
1269 wm->level = min_t(int, wm->level, wm_state->num_levels - 1);
1270 }
1271
1272 if (num_active_crtcs != 1)
1273 wm->cxsr = false;
1274
6f9c784b
VS
1275 if (num_active_crtcs > 1)
1276 wm->level = VLV_WM_LEVEL_PM2;
1277
262cd2e1
VS
1278 for_each_intel_crtc(dev, crtc) {
1279 struct vlv_wm_state *wm_state = &crtc->wm_state;
1280 enum pipe pipe = crtc->pipe;
1281
1282 if (!crtc->active)
1283 continue;
1284
1285 wm->pipe[pipe] = wm_state->wm[wm->level];
1286 if (wm->cxsr)
1287 wm->sr = wm_state->sr[wm->level];
1288
1289 wm->ddl[pipe].primary = DDL_PRECISION_HIGH | 2;
1290 wm->ddl[pipe].sprite[0] = DDL_PRECISION_HIGH | 2;
1291 wm->ddl[pipe].sprite[1] = DDL_PRECISION_HIGH | 2;
1292 wm->ddl[pipe].cursor = DDL_PRECISION_HIGH | 2;
1293 }
1294}
1295
1296static void vlv_update_wm(struct drm_crtc *crtc)
1297{
1298 struct drm_device *dev = crtc->dev;
1299 struct drm_i915_private *dev_priv = dev->dev_private;
1300 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1301 enum pipe pipe = intel_crtc->pipe;
1302 struct vlv_wm_values wm = {};
1303
26e1fe4f 1304 vlv_compute_wm(intel_crtc);
262cd2e1
VS
1305 vlv_merge_wm(dev, &wm);
1306
54f1b6e1
VS
1307 if (memcmp(&dev_priv->wm.vlv, &wm, sizeof(wm)) == 0) {
1308 /* FIXME should be part of crtc atomic commit */
1309 vlv_pipe_set_fifo_size(intel_crtc);
262cd2e1 1310 return;
54f1b6e1 1311 }
262cd2e1
VS
1312
1313 if (wm.level < VLV_WM_LEVEL_DDR_DVFS &&
1314 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_DDR_DVFS)
1315 chv_set_memory_dvfs(dev_priv, false);
1316
1317 if (wm.level < VLV_WM_LEVEL_PM5 &&
1318 dev_priv->wm.vlv.level >= VLV_WM_LEVEL_PM5)
1319 chv_set_memory_pm5(dev_priv, false);
1320
852eb00d 1321 if (!wm.cxsr && dev_priv->wm.vlv.cxsr)
262cd2e1 1322 intel_set_memory_cxsr(dev_priv, false);
262cd2e1 1323
54f1b6e1
VS
1324 /* FIXME should be part of crtc atomic commit */
1325 vlv_pipe_set_fifo_size(intel_crtc);
1326
262cd2e1
VS
1327 vlv_write_wm_values(intel_crtc, &wm);
1328
1329 DRM_DEBUG_KMS("Setting FIFO watermarks - %c: plane=%d, cursor=%d, "
1330 "sprite0=%d, sprite1=%d, SR: plane=%d, cursor=%d level=%d cxsr=%d\n",
1331 pipe_name(pipe), wm.pipe[pipe].primary, wm.pipe[pipe].cursor,
1332 wm.pipe[pipe].sprite[0], wm.pipe[pipe].sprite[1],
1333 wm.sr.plane, wm.sr.cursor, wm.level, wm.cxsr);
1334
852eb00d 1335 if (wm.cxsr && !dev_priv->wm.vlv.cxsr)
262cd2e1 1336 intel_set_memory_cxsr(dev_priv, true);
262cd2e1
VS
1337
1338 if (wm.level >= VLV_WM_LEVEL_PM5 &&
1339 dev_priv->wm.vlv.level < VLV_WM_LEVEL_PM5)
1340 chv_set_memory_pm5(dev_priv, true);
1341
1342 if (wm.level >= VLV_WM_LEVEL_DDR_DVFS &&
1343 dev_priv->wm.vlv.level < VLV_WM_LEVEL_DDR_DVFS)
1344 chv_set_memory_dvfs(dev_priv, true);
1345
1346 dev_priv->wm.vlv = wm;
3c2777fd
VS
1347}
1348
ae80152d
VS
1349#define single_plane_enabled(mask) is_power_of_2(mask)
1350
46ba614c 1351static void g4x_update_wm(struct drm_crtc *crtc)
b445e3b0 1352{
46ba614c 1353 struct drm_device *dev = crtc->dev;
b445e3b0
ED
1354 static const int sr_latency_ns = 12000;
1355 struct drm_i915_private *dev_priv = dev->dev_private;
1356 int planea_wm, planeb_wm, cursora_wm, cursorb_wm;
1357 int plane_sr, cursor_sr;
1358 unsigned int enabled = 0;
9858425c 1359 bool cxsr_enabled;
b445e3b0 1360
51cea1f4 1361 if (g4x_compute_wm0(dev, PIPE_A,
5aef6003
CW
1362 &g4x_wm_info, pessimal_latency_ns,
1363 &g4x_cursor_wm_info, pessimal_latency_ns,
b445e3b0 1364 &planea_wm, &cursora_wm))
51cea1f4 1365 enabled |= 1 << PIPE_A;
b445e3b0 1366
51cea1f4 1367 if (g4x_compute_wm0(dev, PIPE_B,
5aef6003
CW
1368 &g4x_wm_info, pessimal_latency_ns,
1369 &g4x_cursor_wm_info, pessimal_latency_ns,
b445e3b0 1370 &planeb_wm, &cursorb_wm))
51cea1f4 1371 enabled |= 1 << PIPE_B;
b445e3b0 1372
b445e3b0
ED
1373 if (single_plane_enabled(enabled) &&
1374 g4x_compute_srwm(dev, ffs(enabled) - 1,
1375 sr_latency_ns,
1376 &g4x_wm_info,
1377 &g4x_cursor_wm_info,
52bd02d8 1378 &plane_sr, &cursor_sr)) {
9858425c 1379 cxsr_enabled = true;
52bd02d8 1380 } else {
9858425c 1381 cxsr_enabled = false;
5209b1f4 1382 intel_set_memory_cxsr(dev_priv, false);
52bd02d8
CW
1383 plane_sr = cursor_sr = 0;
1384 }
b445e3b0 1385
a5043453
VS
1386 DRM_DEBUG_KMS("Setting FIFO watermarks - A: plane=%d, cursor=%d, "
1387 "B: plane=%d, cursor=%d, SR: plane=%d, cursor=%d\n",
b445e3b0
ED
1388 planea_wm, cursora_wm,
1389 planeb_wm, cursorb_wm,
1390 plane_sr, cursor_sr);
1391
1392 I915_WRITE(DSPFW1,
f4998963
VS
1393 FW_WM(plane_sr, SR) |
1394 FW_WM(cursorb_wm, CURSORB) |
1395 FW_WM(planeb_wm, PLANEB) |
1396 FW_WM(planea_wm, PLANEA));
b445e3b0 1397 I915_WRITE(DSPFW2,
8c919b28 1398 (I915_READ(DSPFW2) & ~DSPFW_CURSORA_MASK) |
f4998963 1399 FW_WM(cursora_wm, CURSORA));
b445e3b0
ED
1400 /* HPLL off in SR has some issues on G4x... disable it */
1401 I915_WRITE(DSPFW3,
8c919b28 1402 (I915_READ(DSPFW3) & ~(DSPFW_HPLL_SR_EN | DSPFW_CURSOR_SR_MASK)) |
f4998963 1403 FW_WM(cursor_sr, CURSOR_SR));
9858425c
ID
1404
1405 if (cxsr_enabled)
1406 intel_set_memory_cxsr(dev_priv, true);
b445e3b0
ED
1407}
1408
46ba614c 1409static void i965_update_wm(struct drm_crtc *unused_crtc)
b445e3b0 1410{
46ba614c 1411 struct drm_device *dev = unused_crtc->dev;
b445e3b0
ED
1412 struct drm_i915_private *dev_priv = dev->dev_private;
1413 struct drm_crtc *crtc;
1414 int srwm = 1;
1415 int cursor_sr = 16;
9858425c 1416 bool cxsr_enabled;
b445e3b0
ED
1417
1418 /* Calc sr entries for one plane configs */
1419 crtc = single_enabled_crtc(dev);
1420 if (crtc) {
1421 /* self-refresh has much higher latency */
1422 static const int sr_latency_ns = 12000;
124abe07 1423 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
241bfc38 1424 int clock = adjusted_mode->crtc_clock;
fec8cba3 1425 int htotal = adjusted_mode->crtc_htotal;
6e3c9717 1426 int hdisplay = to_intel_crtc(crtc)->config->pipe_src_w;
ac484963 1427 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
b445e3b0
ED
1428 unsigned long line_time_us;
1429 int entries;
1430
922044c9 1431 line_time_us = max(htotal * 1000 / clock, 1);
b445e3b0
ED
1432
1433 /* Use ns/us then divide to preserve precision */
1434 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
ac484963 1435 cpp * hdisplay;
b445e3b0
ED
1436 entries = DIV_ROUND_UP(entries, I915_FIFO_LINE_SIZE);
1437 srwm = I965_FIFO_SIZE - entries;
1438 if (srwm < 0)
1439 srwm = 1;
1440 srwm &= 0x1ff;
1441 DRM_DEBUG_KMS("self-refresh entries: %d, wm: %d\n",
1442 entries, srwm);
1443
1444 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
ac484963 1445 cpp * crtc->cursor->state->crtc_w;
b445e3b0
ED
1446 entries = DIV_ROUND_UP(entries,
1447 i965_cursor_wm_info.cacheline_size);
1448 cursor_sr = i965_cursor_wm_info.fifo_size -
1449 (entries + i965_cursor_wm_info.guard_size);
1450
1451 if (cursor_sr > i965_cursor_wm_info.max_wm)
1452 cursor_sr = i965_cursor_wm_info.max_wm;
1453
1454 DRM_DEBUG_KMS("self-refresh watermark: display plane %d "
1455 "cursor %d\n", srwm, cursor_sr);
1456
9858425c 1457 cxsr_enabled = true;
b445e3b0 1458 } else {
9858425c 1459 cxsr_enabled = false;
b445e3b0 1460 /* Turn off self refresh if both pipes are enabled */
5209b1f4 1461 intel_set_memory_cxsr(dev_priv, false);
b445e3b0
ED
1462 }
1463
1464 DRM_DEBUG_KMS("Setting FIFO watermarks - A: 8, B: 8, C: 8, SR %d\n",
1465 srwm);
1466
1467 /* 965 has limitations... */
f4998963
VS
1468 I915_WRITE(DSPFW1, FW_WM(srwm, SR) |
1469 FW_WM(8, CURSORB) |
1470 FW_WM(8, PLANEB) |
1471 FW_WM(8, PLANEA));
1472 I915_WRITE(DSPFW2, FW_WM(8, CURSORA) |
1473 FW_WM(8, PLANEC_OLD));
b445e3b0 1474 /* update cursor SR watermark */
f4998963 1475 I915_WRITE(DSPFW3, FW_WM(cursor_sr, CURSOR_SR));
9858425c
ID
1476
1477 if (cxsr_enabled)
1478 intel_set_memory_cxsr(dev_priv, true);
b445e3b0
ED
1479}
1480
f4998963
VS
1481#undef FW_WM
1482
46ba614c 1483static void i9xx_update_wm(struct drm_crtc *unused_crtc)
b445e3b0 1484{
46ba614c 1485 struct drm_device *dev = unused_crtc->dev;
b445e3b0
ED
1486 struct drm_i915_private *dev_priv = dev->dev_private;
1487 const struct intel_watermark_params *wm_info;
1488 uint32_t fwater_lo;
1489 uint32_t fwater_hi;
1490 int cwm, srwm = 1;
1491 int fifo_size;
1492 int planea_wm, planeb_wm;
1493 struct drm_crtc *crtc, *enabled = NULL;
1494
1495 if (IS_I945GM(dev))
1496 wm_info = &i945_wm_info;
1497 else if (!IS_GEN2(dev))
1498 wm_info = &i915_wm_info;
1499 else
9d539105 1500 wm_info = &i830_a_wm_info;
b445e3b0
ED
1501
1502 fifo_size = dev_priv->display.get_fifo_size(dev, 0);
1503 crtc = intel_get_crtc_for_plane(dev, 0);
3490ea5d 1504 if (intel_crtc_active(crtc)) {
241bfc38 1505 const struct drm_display_mode *adjusted_mode;
ac484963 1506 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
b9e0bda3
CW
1507 if (IS_GEN2(dev))
1508 cpp = 4;
1509
6e3c9717 1510 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
241bfc38 1511 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
b9e0bda3 1512 wm_info, fifo_size, cpp,
5aef6003 1513 pessimal_latency_ns);
b445e3b0 1514 enabled = crtc;
9d539105 1515 } else {
b445e3b0 1516 planea_wm = fifo_size - wm_info->guard_size;
9d539105
VS
1517 if (planea_wm > (long)wm_info->max_wm)
1518 planea_wm = wm_info->max_wm;
1519 }
1520
1521 if (IS_GEN2(dev))
1522 wm_info = &i830_bc_wm_info;
b445e3b0
ED
1523
1524 fifo_size = dev_priv->display.get_fifo_size(dev, 1);
1525 crtc = intel_get_crtc_for_plane(dev, 1);
3490ea5d 1526 if (intel_crtc_active(crtc)) {
241bfc38 1527 const struct drm_display_mode *adjusted_mode;
ac484963 1528 int cpp = drm_format_plane_cpp(crtc->primary->state->fb->pixel_format, 0);
b9e0bda3
CW
1529 if (IS_GEN2(dev))
1530 cpp = 4;
1531
6e3c9717 1532 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
241bfc38 1533 planeb_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
b9e0bda3 1534 wm_info, fifo_size, cpp,
5aef6003 1535 pessimal_latency_ns);
b445e3b0
ED
1536 if (enabled == NULL)
1537 enabled = crtc;
1538 else
1539 enabled = NULL;
9d539105 1540 } else {
b445e3b0 1541 planeb_wm = fifo_size - wm_info->guard_size;
9d539105
VS
1542 if (planeb_wm > (long)wm_info->max_wm)
1543 planeb_wm = wm_info->max_wm;
1544 }
b445e3b0
ED
1545
1546 DRM_DEBUG_KMS("FIFO watermarks - A: %d, B: %d\n", planea_wm, planeb_wm);
1547
2ab1bc9d 1548 if (IS_I915GM(dev) && enabled) {
2ff8fde1 1549 struct drm_i915_gem_object *obj;
2ab1bc9d 1550
59bea882 1551 obj = intel_fb_obj(enabled->primary->state->fb);
2ab1bc9d
DV
1552
1553 /* self-refresh seems busted with untiled */
2ff8fde1 1554 if (obj->tiling_mode == I915_TILING_NONE)
2ab1bc9d
DV
1555 enabled = NULL;
1556 }
1557
b445e3b0
ED
1558 /*
1559 * Overlay gets an aggressive default since video jitter is bad.
1560 */
1561 cwm = 2;
1562
1563 /* Play safe and disable self-refresh before adjusting watermarks. */
5209b1f4 1564 intel_set_memory_cxsr(dev_priv, false);
b445e3b0
ED
1565
1566 /* Calc sr entries for one plane configs */
1567 if (HAS_FW_BLC(dev) && enabled) {
1568 /* self-refresh has much higher latency */
1569 static const int sr_latency_ns = 6000;
124abe07 1570 const struct drm_display_mode *adjusted_mode = &to_intel_crtc(enabled)->config->base.adjusted_mode;
241bfc38 1571 int clock = adjusted_mode->crtc_clock;
fec8cba3 1572 int htotal = adjusted_mode->crtc_htotal;
6e3c9717 1573 int hdisplay = to_intel_crtc(enabled)->config->pipe_src_w;
ac484963 1574 int cpp = drm_format_plane_cpp(enabled->primary->state->fb->pixel_format, 0);
b445e3b0
ED
1575 unsigned long line_time_us;
1576 int entries;
1577
922044c9 1578 line_time_us = max(htotal * 1000 / clock, 1);
b445e3b0
ED
1579
1580 /* Use ns/us then divide to preserve precision */
1581 entries = (((sr_latency_ns / line_time_us) + 1000) / 1000) *
ac484963 1582 cpp * hdisplay;
b445e3b0
ED
1583 entries = DIV_ROUND_UP(entries, wm_info->cacheline_size);
1584 DRM_DEBUG_KMS("self-refresh entries: %d\n", entries);
1585 srwm = wm_info->fifo_size - entries;
1586 if (srwm < 0)
1587 srwm = 1;
1588
1589 if (IS_I945G(dev) || IS_I945GM(dev))
1590 I915_WRITE(FW_BLC_SELF,
1591 FW_BLC_SELF_FIFO_MASK | (srwm & 0xff));
1592 else if (IS_I915GM(dev))
1593 I915_WRITE(FW_BLC_SELF, srwm & 0x3f);
1594 }
1595
1596 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d, B: %d, C: %d, SR %d\n",
1597 planea_wm, planeb_wm, cwm, srwm);
1598
1599 fwater_lo = ((planeb_wm & 0x3f) << 16) | (planea_wm & 0x3f);
1600 fwater_hi = (cwm & 0x1f);
1601
1602 /* Set request length to 8 cachelines per fetch */
1603 fwater_lo = fwater_lo | (1 << 24) | (1 << 8);
1604 fwater_hi = fwater_hi | (1 << 8);
1605
1606 I915_WRITE(FW_BLC, fwater_lo);
1607 I915_WRITE(FW_BLC2, fwater_hi);
1608
5209b1f4
ID
1609 if (enabled)
1610 intel_set_memory_cxsr(dev_priv, true);
b445e3b0
ED
1611}
1612
feb56b93 1613static void i845_update_wm(struct drm_crtc *unused_crtc)
b445e3b0 1614{
46ba614c 1615 struct drm_device *dev = unused_crtc->dev;
b445e3b0
ED
1616 struct drm_i915_private *dev_priv = dev->dev_private;
1617 struct drm_crtc *crtc;
241bfc38 1618 const struct drm_display_mode *adjusted_mode;
b445e3b0
ED
1619 uint32_t fwater_lo;
1620 int planea_wm;
1621
1622 crtc = single_enabled_crtc(dev);
1623 if (crtc == NULL)
1624 return;
1625
6e3c9717 1626 adjusted_mode = &to_intel_crtc(crtc)->config->base.adjusted_mode;
241bfc38 1627 planea_wm = intel_calculate_wm(adjusted_mode->crtc_clock,
feb56b93 1628 &i845_wm_info,
b445e3b0 1629 dev_priv->display.get_fifo_size(dev, 0),
5aef6003 1630 4, pessimal_latency_ns);
b445e3b0
ED
1631 fwater_lo = I915_READ(FW_BLC) & ~0xfff;
1632 fwater_lo |= (3<<8) | planea_wm;
1633
1634 DRM_DEBUG_KMS("Setting FIFO watermarks - A: %d\n", planea_wm);
1635
1636 I915_WRITE(FW_BLC, fwater_lo);
1637}
1638
8cfb3407 1639uint32_t ilk_pipe_pixel_rate(const struct intel_crtc_state *pipe_config)
801bcfff 1640{
fd4daa9c 1641 uint32_t pixel_rate;
801bcfff 1642
8cfb3407 1643 pixel_rate = pipe_config->base.adjusted_mode.crtc_clock;
801bcfff
PZ
1644
1645 /* We only use IF-ID interlacing. If we ever use PF-ID we'll need to
1646 * adjust the pixel_rate here. */
1647
8cfb3407 1648 if (pipe_config->pch_pfit.enabled) {
801bcfff 1649 uint64_t pipe_w, pipe_h, pfit_w, pfit_h;
8cfb3407
VS
1650 uint32_t pfit_size = pipe_config->pch_pfit.size;
1651
1652 pipe_w = pipe_config->pipe_src_w;
1653 pipe_h = pipe_config->pipe_src_h;
801bcfff 1654
801bcfff
PZ
1655 pfit_w = (pfit_size >> 16) & 0xFFFF;
1656 pfit_h = pfit_size & 0xFFFF;
1657 if (pipe_w < pfit_w)
1658 pipe_w = pfit_w;
1659 if (pipe_h < pfit_h)
1660 pipe_h = pfit_h;
1661
15126882
MR
1662 if (WARN_ON(!pfit_w || !pfit_h))
1663 return pixel_rate;
1664
801bcfff
PZ
1665 pixel_rate = div_u64((uint64_t) pixel_rate * pipe_w * pipe_h,
1666 pfit_w * pfit_h);
1667 }
1668
1669 return pixel_rate;
1670}
1671
37126462 1672/* latency must be in 0.1us units. */
ac484963 1673static uint32_t ilk_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
801bcfff
PZ
1674{
1675 uint64_t ret;
1676
3312ba65
VS
1677 if (WARN(latency == 0, "Latency value missing\n"))
1678 return UINT_MAX;
1679
ac484963 1680 ret = (uint64_t) pixel_rate * cpp * latency;
801bcfff
PZ
1681 ret = DIV_ROUND_UP_ULL(ret, 64 * 10000) + 2;
1682
1683 return ret;
1684}
1685
37126462 1686/* latency must be in 0.1us units. */
23297044 1687static uint32_t ilk_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
ac484963 1688 uint32_t horiz_pixels, uint8_t cpp,
801bcfff
PZ
1689 uint32_t latency)
1690{
1691 uint32_t ret;
1692
3312ba65
VS
1693 if (WARN(latency == 0, "Latency value missing\n"))
1694 return UINT_MAX;
15126882
MR
1695 if (WARN_ON(!pipe_htotal))
1696 return UINT_MAX;
3312ba65 1697
801bcfff 1698 ret = (latency * pixel_rate) / (pipe_htotal * 10000);
ac484963 1699 ret = (ret + 1) * horiz_pixels * cpp;
801bcfff
PZ
1700 ret = DIV_ROUND_UP(ret, 64) + 2;
1701 return ret;
1702}
1703
23297044 1704static uint32_t ilk_wm_fbc(uint32_t pri_val, uint32_t horiz_pixels,
ac484963 1705 uint8_t cpp)
cca32e9a 1706{
15126882
MR
1707 /*
1708 * Neither of these should be possible since this function shouldn't be
1709 * called if the CRTC is off or the plane is invisible. But let's be
1710 * extra paranoid to avoid a potential divide-by-zero if we screw up
1711 * elsewhere in the driver.
1712 */
ac484963 1713 if (WARN_ON(!cpp))
15126882
MR
1714 return 0;
1715 if (WARN_ON(!horiz_pixels))
1716 return 0;
1717
ac484963 1718 return DIV_ROUND_UP(pri_val * 64, horiz_pixels * cpp) + 2;
cca32e9a
PZ
1719}
1720
820c1980 1721struct ilk_wm_maximums {
cca32e9a
PZ
1722 uint16_t pri;
1723 uint16_t spr;
1724 uint16_t cur;
1725 uint16_t fbc;
1726};
1727
37126462
VS
1728/*
1729 * For both WM_PIPE and WM_LP.
1730 * mem_value must be in 0.1us units.
1731 */
7221fc33 1732static uint32_t ilk_compute_pri_wm(const struct intel_crtc_state *cstate,
43d59eda 1733 const struct intel_plane_state *pstate,
cca32e9a
PZ
1734 uint32_t mem_value,
1735 bool is_lp)
801bcfff 1736{
ac484963
VS
1737 int cpp = pstate->base.fb ?
1738 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
cca32e9a
PZ
1739 uint32_t method1, method2;
1740
7221fc33 1741 if (!cstate->base.active || !pstate->visible)
801bcfff
PZ
1742 return 0;
1743
ac484963 1744 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
cca32e9a
PZ
1745
1746 if (!is_lp)
1747 return method1;
1748
7221fc33
MR
1749 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1750 cstate->base.adjusted_mode.crtc_htotal,
43d59eda 1751 drm_rect_width(&pstate->dst),
ac484963 1752 cpp, mem_value);
cca32e9a
PZ
1753
1754 return min(method1, method2);
801bcfff
PZ
1755}
1756
37126462
VS
1757/*
1758 * For both WM_PIPE and WM_LP.
1759 * mem_value must be in 0.1us units.
1760 */
7221fc33 1761static uint32_t ilk_compute_spr_wm(const struct intel_crtc_state *cstate,
43d59eda 1762 const struct intel_plane_state *pstate,
801bcfff
PZ
1763 uint32_t mem_value)
1764{
ac484963
VS
1765 int cpp = pstate->base.fb ?
1766 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
801bcfff
PZ
1767 uint32_t method1, method2;
1768
7221fc33 1769 if (!cstate->base.active || !pstate->visible)
801bcfff
PZ
1770 return 0;
1771
ac484963 1772 method1 = ilk_wm_method1(ilk_pipe_pixel_rate(cstate), cpp, mem_value);
7221fc33
MR
1773 method2 = ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1774 cstate->base.adjusted_mode.crtc_htotal,
43d59eda 1775 drm_rect_width(&pstate->dst),
ac484963 1776 cpp, mem_value);
801bcfff
PZ
1777 return min(method1, method2);
1778}
1779
37126462
VS
1780/*
1781 * For both WM_PIPE and WM_LP.
1782 * mem_value must be in 0.1us units.
1783 */
7221fc33 1784static uint32_t ilk_compute_cur_wm(const struct intel_crtc_state *cstate,
43d59eda 1785 const struct intel_plane_state *pstate,
801bcfff
PZ
1786 uint32_t mem_value)
1787{
b2435692
MR
1788 /*
1789 * We treat the cursor plane as always-on for the purposes of watermark
1790 * calculation. Until we have two-stage watermark programming merged,
1791 * this is necessary to avoid flickering.
1792 */
1793 int cpp = 4;
1794 int width = pstate->visible ? pstate->base.crtc_w : 64;
43d59eda 1795
b2435692 1796 if (!cstate->base.active)
801bcfff
PZ
1797 return 0;
1798
7221fc33
MR
1799 return ilk_wm_method2(ilk_pipe_pixel_rate(cstate),
1800 cstate->base.adjusted_mode.crtc_htotal,
b2435692 1801 width, cpp, mem_value);
801bcfff
PZ
1802}
1803
cca32e9a 1804/* Only for WM_LP. */
7221fc33 1805static uint32_t ilk_compute_fbc_wm(const struct intel_crtc_state *cstate,
43d59eda 1806 const struct intel_plane_state *pstate,
1fda9882 1807 uint32_t pri_val)
cca32e9a 1808{
ac484963
VS
1809 int cpp = pstate->base.fb ?
1810 drm_format_plane_cpp(pstate->base.fb->pixel_format, 0) : 0;
43d59eda 1811
7221fc33 1812 if (!cstate->base.active || !pstate->visible)
cca32e9a
PZ
1813 return 0;
1814
ac484963 1815 return ilk_wm_fbc(pri_val, drm_rect_width(&pstate->dst), cpp);
cca32e9a
PZ
1816}
1817
158ae64f
VS
1818static unsigned int ilk_display_fifo_size(const struct drm_device *dev)
1819{
416f4727
VS
1820 if (INTEL_INFO(dev)->gen >= 8)
1821 return 3072;
1822 else if (INTEL_INFO(dev)->gen >= 7)
158ae64f
VS
1823 return 768;
1824 else
1825 return 512;
1826}
1827
4e975081
VS
1828static unsigned int ilk_plane_wm_reg_max(const struct drm_device *dev,
1829 int level, bool is_sprite)
1830{
1831 if (INTEL_INFO(dev)->gen >= 8)
1832 /* BDW primary/sprite plane watermarks */
1833 return level == 0 ? 255 : 2047;
1834 else if (INTEL_INFO(dev)->gen >= 7)
1835 /* IVB/HSW primary/sprite plane watermarks */
1836 return level == 0 ? 127 : 1023;
1837 else if (!is_sprite)
1838 /* ILK/SNB primary plane watermarks */
1839 return level == 0 ? 127 : 511;
1840 else
1841 /* ILK/SNB sprite plane watermarks */
1842 return level == 0 ? 63 : 255;
1843}
1844
1845static unsigned int ilk_cursor_wm_reg_max(const struct drm_device *dev,
1846 int level)
1847{
1848 if (INTEL_INFO(dev)->gen >= 7)
1849 return level == 0 ? 63 : 255;
1850 else
1851 return level == 0 ? 31 : 63;
1852}
1853
1854static unsigned int ilk_fbc_wm_reg_max(const struct drm_device *dev)
1855{
1856 if (INTEL_INFO(dev)->gen >= 8)
1857 return 31;
1858 else
1859 return 15;
1860}
1861
158ae64f
VS
1862/* Calculate the maximum primary/sprite plane watermark */
1863static unsigned int ilk_plane_wm_max(const struct drm_device *dev,
1864 int level,
240264f4 1865 const struct intel_wm_config *config,
158ae64f
VS
1866 enum intel_ddb_partitioning ddb_partitioning,
1867 bool is_sprite)
1868{
1869 unsigned int fifo_size = ilk_display_fifo_size(dev);
158ae64f
VS
1870
1871 /* if sprites aren't enabled, sprites get nothing */
240264f4 1872 if (is_sprite && !config->sprites_enabled)
158ae64f
VS
1873 return 0;
1874
1875 /* HSW allows LP1+ watermarks even with multiple pipes */
240264f4 1876 if (level == 0 || config->num_pipes_active > 1) {
158ae64f
VS
1877 fifo_size /= INTEL_INFO(dev)->num_pipes;
1878
1879 /*
1880 * For some reason the non self refresh
1881 * FIFO size is only half of the self
1882 * refresh FIFO size on ILK/SNB.
1883 */
1884 if (INTEL_INFO(dev)->gen <= 6)
1885 fifo_size /= 2;
1886 }
1887
240264f4 1888 if (config->sprites_enabled) {
158ae64f
VS
1889 /* level 0 is always calculated with 1:1 split */
1890 if (level > 0 && ddb_partitioning == INTEL_DDB_PART_5_6) {
1891 if (is_sprite)
1892 fifo_size *= 5;
1893 fifo_size /= 6;
1894 } else {
1895 fifo_size /= 2;
1896 }
1897 }
1898
1899 /* clamp to max that the registers can hold */
4e975081 1900 return min(fifo_size, ilk_plane_wm_reg_max(dev, level, is_sprite));
158ae64f
VS
1901}
1902
1903/* Calculate the maximum cursor plane watermark */
1904static unsigned int ilk_cursor_wm_max(const struct drm_device *dev,
240264f4
VS
1905 int level,
1906 const struct intel_wm_config *config)
158ae64f
VS
1907{
1908 /* HSW LP1+ watermarks w/ multiple pipes */
240264f4 1909 if (level > 0 && config->num_pipes_active > 1)
158ae64f
VS
1910 return 64;
1911
1912 /* otherwise just report max that registers can hold */
4e975081 1913 return ilk_cursor_wm_reg_max(dev, level);
158ae64f
VS
1914}
1915
d34ff9c6 1916static void ilk_compute_wm_maximums(const struct drm_device *dev,
34982fe1
VS
1917 int level,
1918 const struct intel_wm_config *config,
1919 enum intel_ddb_partitioning ddb_partitioning,
820c1980 1920 struct ilk_wm_maximums *max)
158ae64f 1921{
240264f4
VS
1922 max->pri = ilk_plane_wm_max(dev, level, config, ddb_partitioning, false);
1923 max->spr = ilk_plane_wm_max(dev, level, config, ddb_partitioning, true);
1924 max->cur = ilk_cursor_wm_max(dev, level, config);
4e975081 1925 max->fbc = ilk_fbc_wm_reg_max(dev);
158ae64f
VS
1926}
1927
a3cb4048
VS
1928static void ilk_compute_wm_reg_maximums(struct drm_device *dev,
1929 int level,
1930 struct ilk_wm_maximums *max)
1931{
1932 max->pri = ilk_plane_wm_reg_max(dev, level, false);
1933 max->spr = ilk_plane_wm_reg_max(dev, level, true);
1934 max->cur = ilk_cursor_wm_reg_max(dev, level);
1935 max->fbc = ilk_fbc_wm_reg_max(dev);
1936}
1937
d9395655 1938static bool ilk_validate_wm_level(int level,
820c1980 1939 const struct ilk_wm_maximums *max,
d9395655 1940 struct intel_wm_level *result)
a9786a11
VS
1941{
1942 bool ret;
1943
1944 /* already determined to be invalid? */
1945 if (!result->enable)
1946 return false;
1947
1948 result->enable = result->pri_val <= max->pri &&
1949 result->spr_val <= max->spr &&
1950 result->cur_val <= max->cur;
1951
1952 ret = result->enable;
1953
1954 /*
1955 * HACK until we can pre-compute everything,
1956 * and thus fail gracefully if LP0 watermarks
1957 * are exceeded...
1958 */
1959 if (level == 0 && !result->enable) {
1960 if (result->pri_val > max->pri)
1961 DRM_DEBUG_KMS("Primary WM%d too large %u (max %u)\n",
1962 level, result->pri_val, max->pri);
1963 if (result->spr_val > max->spr)
1964 DRM_DEBUG_KMS("Sprite WM%d too large %u (max %u)\n",
1965 level, result->spr_val, max->spr);
1966 if (result->cur_val > max->cur)
1967 DRM_DEBUG_KMS("Cursor WM%d too large %u (max %u)\n",
1968 level, result->cur_val, max->cur);
1969
1970 result->pri_val = min_t(uint32_t, result->pri_val, max->pri);
1971 result->spr_val = min_t(uint32_t, result->spr_val, max->spr);
1972 result->cur_val = min_t(uint32_t, result->cur_val, max->cur);
1973 result->enable = true;
1974 }
1975
a9786a11
VS
1976 return ret;
1977}
1978
d34ff9c6 1979static void ilk_compute_wm_level(const struct drm_i915_private *dev_priv,
43d59eda 1980 const struct intel_crtc *intel_crtc,
6f5ddd17 1981 int level,
7221fc33 1982 struct intel_crtc_state *cstate,
86c8bbbe
MR
1983 struct intel_plane_state *pristate,
1984 struct intel_plane_state *sprstate,
1985 struct intel_plane_state *curstate,
1fd527cc 1986 struct intel_wm_level *result)
6f5ddd17
VS
1987{
1988 uint16_t pri_latency = dev_priv->wm.pri_latency[level];
1989 uint16_t spr_latency = dev_priv->wm.spr_latency[level];
1990 uint16_t cur_latency = dev_priv->wm.cur_latency[level];
1991
1992 /* WM1+ latency values stored in 0.5us units */
1993 if (level > 0) {
1994 pri_latency *= 5;
1995 spr_latency *= 5;
1996 cur_latency *= 5;
1997 }
1998
e3bddded
ML
1999 if (pristate) {
2000 result->pri_val = ilk_compute_pri_wm(cstate, pristate,
2001 pri_latency, level);
2002 result->fbc_val = ilk_compute_fbc_wm(cstate, pristate, result->pri_val);
2003 }
2004
2005 if (sprstate)
2006 result->spr_val = ilk_compute_spr_wm(cstate, sprstate, spr_latency);
2007
2008 if (curstate)
2009 result->cur_val = ilk_compute_cur_wm(cstate, curstate, cur_latency);
2010
6f5ddd17
VS
2011 result->enable = true;
2012}
2013
801bcfff 2014static uint32_t
ee91a159
MR
2015hsw_compute_linetime_wm(struct drm_device *dev,
2016 struct intel_crtc_state *cstate)
1f8eeabf
ED
2017{
2018 struct drm_i915_private *dev_priv = dev->dev_private;
ee91a159
MR
2019 const struct drm_display_mode *adjusted_mode =
2020 &cstate->base.adjusted_mode;
85a02deb 2021 u32 linetime, ips_linetime;
1f8eeabf 2022
ee91a159
MR
2023 if (!cstate->base.active)
2024 return 0;
2025 if (WARN_ON(adjusted_mode->crtc_clock == 0))
2026 return 0;
2027 if (WARN_ON(dev_priv->cdclk_freq == 0))
801bcfff 2028 return 0;
1011d8c4 2029
1f8eeabf
ED
2030 /* The WM are computed with base on how long it takes to fill a single
2031 * row at the given clock rate, multiplied by 8.
2032 * */
124abe07
VS
2033 linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
2034 adjusted_mode->crtc_clock);
2035 ips_linetime = DIV_ROUND_CLOSEST(adjusted_mode->crtc_htotal * 1000 * 8,
05024da3 2036 dev_priv->cdclk_freq);
1f8eeabf 2037
801bcfff
PZ
2038 return PIPE_WM_LINETIME_IPS_LINETIME(ips_linetime) |
2039 PIPE_WM_LINETIME_TIME(linetime);
1f8eeabf
ED
2040}
2041
2af30a5c 2042static void intel_read_wm_latency(struct drm_device *dev, uint16_t wm[8])
12b134df
VS
2043{
2044 struct drm_i915_private *dev_priv = dev->dev_private;
2045
2af30a5c
PB
2046 if (IS_GEN9(dev)) {
2047 uint32_t val;
4f947386 2048 int ret, i;
367294be 2049 int level, max_level = ilk_wm_max_level(dev);
2af30a5c
PB
2050
2051 /* read the first set of memory latencies[0:3] */
2052 val = 0; /* data0 to be programmed to 0 for first set */
2053 mutex_lock(&dev_priv->rps.hw_lock);
2054 ret = sandybridge_pcode_read(dev_priv,
2055 GEN9_PCODE_READ_MEM_LATENCY,
2056 &val);
2057 mutex_unlock(&dev_priv->rps.hw_lock);
2058
2059 if (ret) {
2060 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2061 return;
2062 }
2063
2064 wm[0] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2065 wm[1] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2066 GEN9_MEM_LATENCY_LEVEL_MASK;
2067 wm[2] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2068 GEN9_MEM_LATENCY_LEVEL_MASK;
2069 wm[3] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2070 GEN9_MEM_LATENCY_LEVEL_MASK;
2071
2072 /* read the second set of memory latencies[4:7] */
2073 val = 1; /* data0 to be programmed to 1 for second set */
2074 mutex_lock(&dev_priv->rps.hw_lock);
2075 ret = sandybridge_pcode_read(dev_priv,
2076 GEN9_PCODE_READ_MEM_LATENCY,
2077 &val);
2078 mutex_unlock(&dev_priv->rps.hw_lock);
2079 if (ret) {
2080 DRM_ERROR("SKL Mailbox read error = %d\n", ret);
2081 return;
2082 }
2083
2084 wm[4] = val & GEN9_MEM_LATENCY_LEVEL_MASK;
2085 wm[5] = (val >> GEN9_MEM_LATENCY_LEVEL_1_5_SHIFT) &
2086 GEN9_MEM_LATENCY_LEVEL_MASK;
2087 wm[6] = (val >> GEN9_MEM_LATENCY_LEVEL_2_6_SHIFT) &
2088 GEN9_MEM_LATENCY_LEVEL_MASK;
2089 wm[7] = (val >> GEN9_MEM_LATENCY_LEVEL_3_7_SHIFT) &
2090 GEN9_MEM_LATENCY_LEVEL_MASK;
2091
367294be 2092 /*
6f97235b
DL
2093 * WaWmMemoryReadLatency:skl
2094 *
367294be
VK
2095 * punit doesn't take into account the read latency so we need
2096 * to add 2us to the various latency levels we retrieve from
2097 * the punit.
2098 * - W0 is a bit special in that it's the only level that
2099 * can't be disabled if we want to have display working, so
2100 * we always add 2us there.
2101 * - For levels >=1, punit returns 0us latency when they are
2102 * disabled, so we respect that and don't add 2us then
4f947386
VK
2103 *
2104 * Additionally, if a level n (n > 1) has a 0us latency, all
2105 * levels m (m >= n) need to be disabled. We make sure to
2106 * sanitize the values out of the punit to satisfy this
2107 * requirement.
367294be
VK
2108 */
2109 wm[0] += 2;
2110 for (level = 1; level <= max_level; level++)
2111 if (wm[level] != 0)
2112 wm[level] += 2;
4f947386
VK
2113 else {
2114 for (i = level + 1; i <= max_level; i++)
2115 wm[i] = 0;
367294be 2116
4f947386
VK
2117 break;
2118 }
2af30a5c 2119 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
12b134df
VS
2120 uint64_t sskpd = I915_READ64(MCH_SSKPD);
2121
2122 wm[0] = (sskpd >> 56) & 0xFF;
2123 if (wm[0] == 0)
2124 wm[0] = sskpd & 0xF;
e5d5019e
VS
2125 wm[1] = (sskpd >> 4) & 0xFF;
2126 wm[2] = (sskpd >> 12) & 0xFF;
2127 wm[3] = (sskpd >> 20) & 0x1FF;
2128 wm[4] = (sskpd >> 32) & 0x1FF;
63cf9a13
VS
2129 } else if (INTEL_INFO(dev)->gen >= 6) {
2130 uint32_t sskpd = I915_READ(MCH_SSKPD);
2131
2132 wm[0] = (sskpd >> SSKPD_WM0_SHIFT) & SSKPD_WM_MASK;
2133 wm[1] = (sskpd >> SSKPD_WM1_SHIFT) & SSKPD_WM_MASK;
2134 wm[2] = (sskpd >> SSKPD_WM2_SHIFT) & SSKPD_WM_MASK;
2135 wm[3] = (sskpd >> SSKPD_WM3_SHIFT) & SSKPD_WM_MASK;
3a88d0ac
VS
2136 } else if (INTEL_INFO(dev)->gen >= 5) {
2137 uint32_t mltr = I915_READ(MLTR_ILK);
2138
2139 /* ILK primary LP0 latency is 700 ns */
2140 wm[0] = 7;
2141 wm[1] = (mltr >> MLTR_WM1_SHIFT) & ILK_SRLT_MASK;
2142 wm[2] = (mltr >> MLTR_WM2_SHIFT) & ILK_SRLT_MASK;
12b134df
VS
2143 }
2144}
2145
53615a5e
VS
2146static void intel_fixup_spr_wm_latency(struct drm_device *dev, uint16_t wm[5])
2147{
2148 /* ILK sprite LP0 latency is 1300 ns */
2149 if (INTEL_INFO(dev)->gen == 5)
2150 wm[0] = 13;
2151}
2152
2153static void intel_fixup_cur_wm_latency(struct drm_device *dev, uint16_t wm[5])
2154{
2155 /* ILK cursor LP0 latency is 1300 ns */
2156 if (INTEL_INFO(dev)->gen == 5)
2157 wm[0] = 13;
2158
2159 /* WaDoubleCursorLP3Latency:ivb */
2160 if (IS_IVYBRIDGE(dev))
2161 wm[3] *= 2;
2162}
2163
546c81fd 2164int ilk_wm_max_level(const struct drm_device *dev)
26ec971e 2165{
26ec971e 2166 /* how many WM levels are we expecting */
b6e742f6 2167 if (INTEL_INFO(dev)->gen >= 9)
2af30a5c
PB
2168 return 7;
2169 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ad0d6dc4 2170 return 4;
26ec971e 2171 else if (INTEL_INFO(dev)->gen >= 6)
ad0d6dc4 2172 return 3;
26ec971e 2173 else
ad0d6dc4
VS
2174 return 2;
2175}
7526ed79 2176
ad0d6dc4
VS
2177static void intel_print_wm_latency(struct drm_device *dev,
2178 const char *name,
2af30a5c 2179 const uint16_t wm[8])
ad0d6dc4
VS
2180{
2181 int level, max_level = ilk_wm_max_level(dev);
26ec971e
VS
2182
2183 for (level = 0; level <= max_level; level++) {
2184 unsigned int latency = wm[level];
2185
2186 if (latency == 0) {
2187 DRM_ERROR("%s WM%d latency not provided\n",
2188 name, level);
2189 continue;
2190 }
2191
2af30a5c
PB
2192 /*
2193 * - latencies are in us on gen9.
2194 * - before then, WM1+ latency values are in 0.5us units
2195 */
2196 if (IS_GEN9(dev))
2197 latency *= 10;
2198 else if (level > 0)
26ec971e
VS
2199 latency *= 5;
2200
2201 DRM_DEBUG_KMS("%s WM%d latency %u (%u.%u usec)\n",
2202 name, level, wm[level],
2203 latency / 10, latency % 10);
2204 }
2205}
2206
e95a2f75
VS
2207static bool ilk_increase_wm_latency(struct drm_i915_private *dev_priv,
2208 uint16_t wm[5], uint16_t min)
2209{
2210 int level, max_level = ilk_wm_max_level(dev_priv->dev);
2211
2212 if (wm[0] >= min)
2213 return false;
2214
2215 wm[0] = max(wm[0], min);
2216 for (level = 1; level <= max_level; level++)
2217 wm[level] = max_t(uint16_t, wm[level], DIV_ROUND_UP(min, 5));
2218
2219 return true;
2220}
2221
2222static void snb_wm_latency_quirk(struct drm_device *dev)
2223{
2224 struct drm_i915_private *dev_priv = dev->dev_private;
2225 bool changed;
2226
2227 /*
2228 * The BIOS provided WM memory latency values are often
2229 * inadequate for high resolution displays. Adjust them.
2230 */
2231 changed = ilk_increase_wm_latency(dev_priv, dev_priv->wm.pri_latency, 12) |
2232 ilk_increase_wm_latency(dev_priv, dev_priv->wm.spr_latency, 12) |
2233 ilk_increase_wm_latency(dev_priv, dev_priv->wm.cur_latency, 12);
2234
2235 if (!changed)
2236 return;
2237
2238 DRM_DEBUG_KMS("WM latency values increased to avoid potential underruns\n");
2239 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2240 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2241 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
2242}
2243
fa50ad61 2244static void ilk_setup_wm_latency(struct drm_device *dev)
53615a5e
VS
2245{
2246 struct drm_i915_private *dev_priv = dev->dev_private;
2247
2248 intel_read_wm_latency(dev, dev_priv->wm.pri_latency);
2249
2250 memcpy(dev_priv->wm.spr_latency, dev_priv->wm.pri_latency,
2251 sizeof(dev_priv->wm.pri_latency));
2252 memcpy(dev_priv->wm.cur_latency, dev_priv->wm.pri_latency,
2253 sizeof(dev_priv->wm.pri_latency));
2254
2255 intel_fixup_spr_wm_latency(dev, dev_priv->wm.spr_latency);
2256 intel_fixup_cur_wm_latency(dev, dev_priv->wm.cur_latency);
26ec971e
VS
2257
2258 intel_print_wm_latency(dev, "Primary", dev_priv->wm.pri_latency);
2259 intel_print_wm_latency(dev, "Sprite", dev_priv->wm.spr_latency);
2260 intel_print_wm_latency(dev, "Cursor", dev_priv->wm.cur_latency);
e95a2f75
VS
2261
2262 if (IS_GEN6(dev))
2263 snb_wm_latency_quirk(dev);
53615a5e
VS
2264}
2265
2af30a5c
PB
2266static void skl_setup_wm_latency(struct drm_device *dev)
2267{
2268 struct drm_i915_private *dev_priv = dev->dev_private;
2269
2270 intel_read_wm_latency(dev, dev_priv->wm.skl_latency);
2271 intel_print_wm_latency(dev, "Gen9 Plane", dev_priv->wm.skl_latency);
2272}
2273
ed4a6a7c
MR
2274static bool ilk_validate_pipe_wm(struct drm_device *dev,
2275 struct intel_pipe_wm *pipe_wm)
2276{
2277 /* LP0 watermark maximums depend on this pipe alone */
2278 const struct intel_wm_config config = {
2279 .num_pipes_active = 1,
2280 .sprites_enabled = pipe_wm->sprites_enabled,
2281 .sprites_scaled = pipe_wm->sprites_scaled,
2282 };
2283 struct ilk_wm_maximums max;
2284
2285 /* LP0 watermarks always use 1/2 DDB partitioning */
2286 ilk_compute_wm_maximums(dev, 0, &config, INTEL_DDB_PART_1_2, &max);
2287
2288 /* At least LP0 must be valid */
2289 if (!ilk_validate_wm_level(0, &max, &pipe_wm->wm[0])) {
2290 DRM_DEBUG_KMS("LP0 watermark invalid\n");
2291 return false;
2292 }
2293
2294 return true;
2295}
2296
0b2ae6d7 2297/* Compute new watermarks for the pipe */
e3bddded 2298static int ilk_compute_pipe_wm(struct intel_crtc_state *cstate)
0b2ae6d7 2299{
e3bddded
ML
2300 struct drm_atomic_state *state = cstate->base.state;
2301 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
86c8bbbe 2302 struct intel_pipe_wm *pipe_wm;
e3bddded 2303 struct drm_device *dev = state->dev;
d34ff9c6 2304 const struct drm_i915_private *dev_priv = dev->dev_private;
43d59eda 2305 struct intel_plane *intel_plane;
86c8bbbe 2306 struct intel_plane_state *pristate = NULL;
43d59eda 2307 struct intel_plane_state *sprstate = NULL;
86c8bbbe 2308 struct intel_plane_state *curstate = NULL;
d81f04c5 2309 int level, max_level = ilk_wm_max_level(dev), usable_level;
820c1980 2310 struct ilk_wm_maximums max;
0b2ae6d7 2311
86c8bbbe
MR
2312 pipe_wm = &cstate->wm.optimal.ilk;
2313
43d59eda 2314 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
e3bddded
ML
2315 struct intel_plane_state *ps;
2316
2317 ps = intel_atomic_get_existing_plane_state(state,
2318 intel_plane);
2319 if (!ps)
2320 continue;
86c8bbbe
MR
2321
2322 if (intel_plane->base.type == DRM_PLANE_TYPE_PRIMARY)
e3bddded 2323 pristate = ps;
86c8bbbe 2324 else if (intel_plane->base.type == DRM_PLANE_TYPE_OVERLAY)
e3bddded 2325 sprstate = ps;
86c8bbbe 2326 else if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
e3bddded 2327 curstate = ps;
43d59eda
MR
2328 }
2329
ed4a6a7c 2330 pipe_wm->pipe_enabled = cstate->base.active;
e3bddded
ML
2331 if (sprstate) {
2332 pipe_wm->sprites_enabled = sprstate->visible;
2333 pipe_wm->sprites_scaled = sprstate->visible &&
2334 (drm_rect_width(&sprstate->dst) != drm_rect_width(&sprstate->src) >> 16 ||
2335 drm_rect_height(&sprstate->dst) != drm_rect_height(&sprstate->src) >> 16);
2336 }
2337
d81f04c5
ML
2338 usable_level = max_level;
2339
7b39a0b7 2340 /* ILK/SNB: LP2+ watermarks only w/o sprites */
e3bddded 2341 if (INTEL_INFO(dev)->gen <= 6 && pipe_wm->sprites_enabled)
d81f04c5 2342 usable_level = 1;
7b39a0b7
VS
2343
2344 /* ILK/SNB/IVB: LP1+ watermarks only w/o scaling */
ed4a6a7c 2345 if (pipe_wm->sprites_scaled)
d81f04c5 2346 usable_level = 0;
7b39a0b7 2347
86c8bbbe 2348 ilk_compute_wm_level(dev_priv, intel_crtc, 0, cstate,
71f0a626
ML
2349 pristate, sprstate, curstate, &pipe_wm->raw_wm[0]);
2350
2351 memset(&pipe_wm->wm, 0, sizeof(pipe_wm->wm));
2352 pipe_wm->wm[0] = pipe_wm->raw_wm[0];
0b2ae6d7 2353
a42a5719 2354 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ee91a159 2355 pipe_wm->linetime = hsw_compute_linetime_wm(dev, cstate);
0b2ae6d7 2356
ed4a6a7c 2357 if (!ilk_validate_pipe_wm(dev, pipe_wm))
1a426d61 2358 return -EINVAL;
a3cb4048
VS
2359
2360 ilk_compute_wm_reg_maximums(dev, 1, &max);
2361
2362 for (level = 1; level <= max_level; level++) {
71f0a626 2363 struct intel_wm_level *wm = &pipe_wm->raw_wm[level];
a3cb4048 2364
86c8bbbe 2365 ilk_compute_wm_level(dev_priv, intel_crtc, level, cstate,
d81f04c5 2366 pristate, sprstate, curstate, wm);
a3cb4048
VS
2367
2368 /*
2369 * Disable any watermark level that exceeds the
2370 * register maximums since such watermarks are
2371 * always invalid.
2372 */
71f0a626
ML
2373 if (level > usable_level)
2374 continue;
2375
2376 if (ilk_validate_wm_level(level, &max, wm))
2377 pipe_wm->wm[level] = *wm;
2378 else
d81f04c5 2379 usable_level = level;
a3cb4048
VS
2380 }
2381
86c8bbbe 2382 return 0;
0b2ae6d7
VS
2383}
2384
ed4a6a7c
MR
2385/*
2386 * Build a set of 'intermediate' watermark values that satisfy both the old
2387 * state and the new state. These can be programmed to the hardware
2388 * immediately.
2389 */
2390static int ilk_compute_intermediate_wm(struct drm_device *dev,
2391 struct intel_crtc *intel_crtc,
2392 struct intel_crtc_state *newstate)
2393{
2394 struct intel_pipe_wm *a = &newstate->wm.intermediate;
2395 struct intel_pipe_wm *b = &intel_crtc->wm.active.ilk;
2396 int level, max_level = ilk_wm_max_level(dev);
2397
2398 /*
2399 * Start with the final, target watermarks, then combine with the
2400 * currently active watermarks to get values that are safe both before
2401 * and after the vblank.
2402 */
2403 *a = newstate->wm.optimal.ilk;
2404 a->pipe_enabled |= b->pipe_enabled;
2405 a->sprites_enabled |= b->sprites_enabled;
2406 a->sprites_scaled |= b->sprites_scaled;
2407
2408 for (level = 0; level <= max_level; level++) {
2409 struct intel_wm_level *a_wm = &a->wm[level];
2410 const struct intel_wm_level *b_wm = &b->wm[level];
2411
2412 a_wm->enable &= b_wm->enable;
2413 a_wm->pri_val = max(a_wm->pri_val, b_wm->pri_val);
2414 a_wm->spr_val = max(a_wm->spr_val, b_wm->spr_val);
2415 a_wm->cur_val = max(a_wm->cur_val, b_wm->cur_val);
2416 a_wm->fbc_val = max(a_wm->fbc_val, b_wm->fbc_val);
2417 }
2418
2419 /*
2420 * We need to make sure that these merged watermark values are
2421 * actually a valid configuration themselves. If they're not,
2422 * there's no safe way to transition from the old state to
2423 * the new state, so we need to fail the atomic transaction.
2424 */
2425 if (!ilk_validate_pipe_wm(dev, a))
2426 return -EINVAL;
2427
2428 /*
2429 * If our intermediate WM are identical to the final WM, then we can
2430 * omit the post-vblank programming; only update if it's different.
2431 */
2432 if (memcmp(a, &newstate->wm.optimal.ilk, sizeof(*a)) == 0)
2433 newstate->wm.need_postvbl_update = false;
2434
2435 return 0;
2436}
2437
0b2ae6d7
VS
2438/*
2439 * Merge the watermarks from all active pipes for a specific level.
2440 */
2441static void ilk_merge_wm_level(struct drm_device *dev,
2442 int level,
2443 struct intel_wm_level *ret_wm)
2444{
2445 const struct intel_crtc *intel_crtc;
2446
d52fea5b
VS
2447 ret_wm->enable = true;
2448
d3fcc808 2449 for_each_intel_crtc(dev, intel_crtc) {
ed4a6a7c 2450 const struct intel_pipe_wm *active = &intel_crtc->wm.active.ilk;
fe392efd
VS
2451 const struct intel_wm_level *wm = &active->wm[level];
2452
2453 if (!active->pipe_enabled)
2454 continue;
0b2ae6d7 2455
d52fea5b
VS
2456 /*
2457 * The watermark values may have been used in the past,
2458 * so we must maintain them in the registers for some
2459 * time even if the level is now disabled.
2460 */
0b2ae6d7 2461 if (!wm->enable)
d52fea5b 2462 ret_wm->enable = false;
0b2ae6d7
VS
2463
2464 ret_wm->pri_val = max(ret_wm->pri_val, wm->pri_val);
2465 ret_wm->spr_val = max(ret_wm->spr_val, wm->spr_val);
2466 ret_wm->cur_val = max(ret_wm->cur_val, wm->cur_val);
2467 ret_wm->fbc_val = max(ret_wm->fbc_val, wm->fbc_val);
2468 }
0b2ae6d7
VS
2469}
2470
2471/*
2472 * Merge all low power watermarks for all active pipes.
2473 */
2474static void ilk_wm_merge(struct drm_device *dev,
0ba22e26 2475 const struct intel_wm_config *config,
820c1980 2476 const struct ilk_wm_maximums *max,
0b2ae6d7
VS
2477 struct intel_pipe_wm *merged)
2478{
7733b49b 2479 struct drm_i915_private *dev_priv = dev->dev_private;
0b2ae6d7 2480 int level, max_level = ilk_wm_max_level(dev);
d52fea5b 2481 int last_enabled_level = max_level;
0b2ae6d7 2482
0ba22e26
VS
2483 /* ILK/SNB/IVB: LP1+ watermarks only w/ single pipe */
2484 if ((INTEL_INFO(dev)->gen <= 6 || IS_IVYBRIDGE(dev)) &&
2485 config->num_pipes_active > 1)
1204d5ba 2486 last_enabled_level = 0;
0ba22e26 2487
6c8b6c28
VS
2488 /* ILK: FBC WM must be disabled always */
2489 merged->fbc_wm_enabled = INTEL_INFO(dev)->gen >= 6;
0b2ae6d7
VS
2490
2491 /* merge each WM1+ level */
2492 for (level = 1; level <= max_level; level++) {
2493 struct intel_wm_level *wm = &merged->wm[level];
2494
2495 ilk_merge_wm_level(dev, level, wm);
2496
d52fea5b
VS
2497 if (level > last_enabled_level)
2498 wm->enable = false;
2499 else if (!ilk_validate_wm_level(level, max, wm))
2500 /* make sure all following levels get disabled */
2501 last_enabled_level = level - 1;
0b2ae6d7
VS
2502
2503 /*
2504 * The spec says it is preferred to disable
2505 * FBC WMs instead of disabling a WM level.
2506 */
2507 if (wm->fbc_val > max->fbc) {
d52fea5b
VS
2508 if (wm->enable)
2509 merged->fbc_wm_enabled = false;
0b2ae6d7
VS
2510 wm->fbc_val = 0;
2511 }
2512 }
6c8b6c28
VS
2513
2514 /* ILK: LP2+ must be disabled when FBC WM is disabled but FBC enabled */
2515 /*
2516 * FIXME this is racy. FBC might get enabled later.
2517 * What we should check here is whether FBC can be
2518 * enabled sometime later.
2519 */
7733b49b 2520 if (IS_GEN5(dev) && !merged->fbc_wm_enabled &&
0e631adc 2521 intel_fbc_is_active(dev_priv)) {
6c8b6c28
VS
2522 for (level = 2; level <= max_level; level++) {
2523 struct intel_wm_level *wm = &merged->wm[level];
2524
2525 wm->enable = false;
2526 }
2527 }
0b2ae6d7
VS
2528}
2529
b380ca3c
VS
2530static int ilk_wm_lp_to_level(int wm_lp, const struct intel_pipe_wm *pipe_wm)
2531{
2532 /* LP1,LP2,LP3 levels are either 1,2,3 or 1,3,4 */
2533 return wm_lp + (wm_lp >= 2 && pipe_wm->wm[4].enable);
2534}
2535
a68d68ee
VS
2536/* The value we need to program into the WM_LPx latency field */
2537static unsigned int ilk_wm_lp_latency(struct drm_device *dev, int level)
2538{
2539 struct drm_i915_private *dev_priv = dev->dev_private;
2540
a42a5719 2541 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
a68d68ee
VS
2542 return 2 * level;
2543 else
2544 return dev_priv->wm.pri_latency[level];
2545}
2546
820c1980 2547static void ilk_compute_wm_results(struct drm_device *dev,
0362c781 2548 const struct intel_pipe_wm *merged,
609cedef 2549 enum intel_ddb_partitioning partitioning,
820c1980 2550 struct ilk_wm_values *results)
801bcfff 2551{
0b2ae6d7
VS
2552 struct intel_crtc *intel_crtc;
2553 int level, wm_lp;
cca32e9a 2554
0362c781 2555 results->enable_fbc_wm = merged->fbc_wm_enabled;
609cedef 2556 results->partitioning = partitioning;
cca32e9a 2557
0b2ae6d7 2558 /* LP1+ register values */
cca32e9a 2559 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
1fd527cc 2560 const struct intel_wm_level *r;
801bcfff 2561
b380ca3c 2562 level = ilk_wm_lp_to_level(wm_lp, merged);
0b2ae6d7 2563
0362c781 2564 r = &merged->wm[level];
cca32e9a 2565
d52fea5b
VS
2566 /*
2567 * Maintain the watermark values even if the level is
2568 * disabled. Doing otherwise could cause underruns.
2569 */
2570 results->wm_lp[wm_lp - 1] =
a68d68ee 2571 (ilk_wm_lp_latency(dev, level) << WM1_LP_LATENCY_SHIFT) |
416f4727
VS
2572 (r->pri_val << WM1_LP_SR_SHIFT) |
2573 r->cur_val;
2574
d52fea5b
VS
2575 if (r->enable)
2576 results->wm_lp[wm_lp - 1] |= WM1_LP_SR_EN;
2577
416f4727
VS
2578 if (INTEL_INFO(dev)->gen >= 8)
2579 results->wm_lp[wm_lp - 1] |=
2580 r->fbc_val << WM1_LP_FBC_SHIFT_BDW;
2581 else
2582 results->wm_lp[wm_lp - 1] |=
2583 r->fbc_val << WM1_LP_FBC_SHIFT;
2584
d52fea5b
VS
2585 /*
2586 * Always set WM1S_LP_EN when spr_val != 0, even if the
2587 * level is disabled. Doing otherwise could cause underruns.
2588 */
6cef2b8a
VS
2589 if (INTEL_INFO(dev)->gen <= 6 && r->spr_val) {
2590 WARN_ON(wm_lp != 1);
2591 results->wm_lp_spr[wm_lp - 1] = WM1S_LP_EN | r->spr_val;
2592 } else
2593 results->wm_lp_spr[wm_lp - 1] = r->spr_val;
cca32e9a 2594 }
801bcfff 2595
0b2ae6d7 2596 /* LP0 register values */
d3fcc808 2597 for_each_intel_crtc(dev, intel_crtc) {
0b2ae6d7 2598 enum pipe pipe = intel_crtc->pipe;
ed4a6a7c
MR
2599 const struct intel_wm_level *r =
2600 &intel_crtc->wm.active.ilk.wm[0];
0b2ae6d7
VS
2601
2602 if (WARN_ON(!r->enable))
2603 continue;
2604
ed4a6a7c 2605 results->wm_linetime[pipe] = intel_crtc->wm.active.ilk.linetime;
1011d8c4 2606
0b2ae6d7
VS
2607 results->wm_pipe[pipe] =
2608 (r->pri_val << WM0_PIPE_PLANE_SHIFT) |
2609 (r->spr_val << WM0_PIPE_SPRITE_SHIFT) |
2610 r->cur_val;
801bcfff
PZ
2611 }
2612}
2613
861f3389
PZ
2614/* Find the result with the highest level enabled. Check for enable_fbc_wm in
2615 * case both are at the same level. Prefer r1 in case they're the same. */
820c1980 2616static struct intel_pipe_wm *ilk_find_best_result(struct drm_device *dev,
198a1e9b
VS
2617 struct intel_pipe_wm *r1,
2618 struct intel_pipe_wm *r2)
861f3389 2619{
198a1e9b
VS
2620 int level, max_level = ilk_wm_max_level(dev);
2621 int level1 = 0, level2 = 0;
861f3389 2622
198a1e9b
VS
2623 for (level = 1; level <= max_level; level++) {
2624 if (r1->wm[level].enable)
2625 level1 = level;
2626 if (r2->wm[level].enable)
2627 level2 = level;
861f3389
PZ
2628 }
2629
198a1e9b
VS
2630 if (level1 == level2) {
2631 if (r2->fbc_wm_enabled && !r1->fbc_wm_enabled)
861f3389
PZ
2632 return r2;
2633 else
2634 return r1;
198a1e9b 2635 } else if (level1 > level2) {
861f3389
PZ
2636 return r1;
2637 } else {
2638 return r2;
2639 }
2640}
2641
49a687c4
VS
2642/* dirty bits used to track which watermarks need changes */
2643#define WM_DIRTY_PIPE(pipe) (1 << (pipe))
2644#define WM_DIRTY_LINETIME(pipe) (1 << (8 + (pipe)))
2645#define WM_DIRTY_LP(wm_lp) (1 << (15 + (wm_lp)))
2646#define WM_DIRTY_LP_ALL (WM_DIRTY_LP(1) | WM_DIRTY_LP(2) | WM_DIRTY_LP(3))
2647#define WM_DIRTY_FBC (1 << 24)
2648#define WM_DIRTY_DDB (1 << 25)
2649
055e393f 2650static unsigned int ilk_compute_wm_dirty(struct drm_i915_private *dev_priv,
820c1980
ID
2651 const struct ilk_wm_values *old,
2652 const struct ilk_wm_values *new)
49a687c4
VS
2653{
2654 unsigned int dirty = 0;
2655 enum pipe pipe;
2656 int wm_lp;
2657
055e393f 2658 for_each_pipe(dev_priv, pipe) {
49a687c4
VS
2659 if (old->wm_linetime[pipe] != new->wm_linetime[pipe]) {
2660 dirty |= WM_DIRTY_LINETIME(pipe);
2661 /* Must disable LP1+ watermarks too */
2662 dirty |= WM_DIRTY_LP_ALL;
2663 }
2664
2665 if (old->wm_pipe[pipe] != new->wm_pipe[pipe]) {
2666 dirty |= WM_DIRTY_PIPE(pipe);
2667 /* Must disable LP1+ watermarks too */
2668 dirty |= WM_DIRTY_LP_ALL;
2669 }
2670 }
2671
2672 if (old->enable_fbc_wm != new->enable_fbc_wm) {
2673 dirty |= WM_DIRTY_FBC;
2674 /* Must disable LP1+ watermarks too */
2675 dirty |= WM_DIRTY_LP_ALL;
2676 }
2677
2678 if (old->partitioning != new->partitioning) {
2679 dirty |= WM_DIRTY_DDB;
2680 /* Must disable LP1+ watermarks too */
2681 dirty |= WM_DIRTY_LP_ALL;
2682 }
2683
2684 /* LP1+ watermarks already deemed dirty, no need to continue */
2685 if (dirty & WM_DIRTY_LP_ALL)
2686 return dirty;
2687
2688 /* Find the lowest numbered LP1+ watermark in need of an update... */
2689 for (wm_lp = 1; wm_lp <= 3; wm_lp++) {
2690 if (old->wm_lp[wm_lp - 1] != new->wm_lp[wm_lp - 1] ||
2691 old->wm_lp_spr[wm_lp - 1] != new->wm_lp_spr[wm_lp - 1])
2692 break;
2693 }
2694
2695 /* ...and mark it and all higher numbered LP1+ watermarks as dirty */
2696 for (; wm_lp <= 3; wm_lp++)
2697 dirty |= WM_DIRTY_LP(wm_lp);
2698
2699 return dirty;
2700}
2701
8553c18e
VS
2702static bool _ilk_disable_lp_wm(struct drm_i915_private *dev_priv,
2703 unsigned int dirty)
801bcfff 2704{
820c1980 2705 struct ilk_wm_values *previous = &dev_priv->wm.hw;
8553c18e 2706 bool changed = false;
801bcfff 2707
facd619b
VS
2708 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] & WM1_LP_SR_EN) {
2709 previous->wm_lp[2] &= ~WM1_LP_SR_EN;
2710 I915_WRITE(WM3_LP_ILK, previous->wm_lp[2]);
8553c18e 2711 changed = true;
facd619b
VS
2712 }
2713 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] & WM1_LP_SR_EN) {
2714 previous->wm_lp[1] &= ~WM1_LP_SR_EN;
2715 I915_WRITE(WM2_LP_ILK, previous->wm_lp[1]);
8553c18e 2716 changed = true;
facd619b
VS
2717 }
2718 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] & WM1_LP_SR_EN) {
2719 previous->wm_lp[0] &= ~WM1_LP_SR_EN;
2720 I915_WRITE(WM1_LP_ILK, previous->wm_lp[0]);
8553c18e 2721 changed = true;
facd619b 2722 }
801bcfff 2723
facd619b
VS
2724 /*
2725 * Don't touch WM1S_LP_EN here.
2726 * Doing so could cause underruns.
2727 */
6cef2b8a 2728
8553c18e
VS
2729 return changed;
2730}
2731
2732/*
2733 * The spec says we shouldn't write when we don't need, because every write
2734 * causes WMs to be re-evaluated, expending some power.
2735 */
820c1980
ID
2736static void ilk_write_wm_values(struct drm_i915_private *dev_priv,
2737 struct ilk_wm_values *results)
8553c18e
VS
2738{
2739 struct drm_device *dev = dev_priv->dev;
820c1980 2740 struct ilk_wm_values *previous = &dev_priv->wm.hw;
8553c18e
VS
2741 unsigned int dirty;
2742 uint32_t val;
2743
055e393f 2744 dirty = ilk_compute_wm_dirty(dev_priv, previous, results);
8553c18e
VS
2745 if (!dirty)
2746 return;
2747
2748 _ilk_disable_lp_wm(dev_priv, dirty);
2749
49a687c4 2750 if (dirty & WM_DIRTY_PIPE(PIPE_A))
801bcfff 2751 I915_WRITE(WM0_PIPEA_ILK, results->wm_pipe[0]);
49a687c4 2752 if (dirty & WM_DIRTY_PIPE(PIPE_B))
801bcfff 2753 I915_WRITE(WM0_PIPEB_ILK, results->wm_pipe[1]);
49a687c4 2754 if (dirty & WM_DIRTY_PIPE(PIPE_C))
801bcfff
PZ
2755 I915_WRITE(WM0_PIPEC_IVB, results->wm_pipe[2]);
2756
49a687c4 2757 if (dirty & WM_DIRTY_LINETIME(PIPE_A))
801bcfff 2758 I915_WRITE(PIPE_WM_LINETIME(PIPE_A), results->wm_linetime[0]);
49a687c4 2759 if (dirty & WM_DIRTY_LINETIME(PIPE_B))
801bcfff 2760 I915_WRITE(PIPE_WM_LINETIME(PIPE_B), results->wm_linetime[1]);
49a687c4 2761 if (dirty & WM_DIRTY_LINETIME(PIPE_C))
801bcfff
PZ
2762 I915_WRITE(PIPE_WM_LINETIME(PIPE_C), results->wm_linetime[2]);
2763
49a687c4 2764 if (dirty & WM_DIRTY_DDB) {
a42a5719 2765 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
ac9545fd
VS
2766 val = I915_READ(WM_MISC);
2767 if (results->partitioning == INTEL_DDB_PART_1_2)
2768 val &= ~WM_MISC_DATA_PARTITION_5_6;
2769 else
2770 val |= WM_MISC_DATA_PARTITION_5_6;
2771 I915_WRITE(WM_MISC, val);
2772 } else {
2773 val = I915_READ(DISP_ARB_CTL2);
2774 if (results->partitioning == INTEL_DDB_PART_1_2)
2775 val &= ~DISP_DATA_PARTITION_5_6;
2776 else
2777 val |= DISP_DATA_PARTITION_5_6;
2778 I915_WRITE(DISP_ARB_CTL2, val);
2779 }
1011d8c4
PZ
2780 }
2781
49a687c4 2782 if (dirty & WM_DIRTY_FBC) {
cca32e9a
PZ
2783 val = I915_READ(DISP_ARB_CTL);
2784 if (results->enable_fbc_wm)
2785 val &= ~DISP_FBC_WM_DIS;
2786 else
2787 val |= DISP_FBC_WM_DIS;
2788 I915_WRITE(DISP_ARB_CTL, val);
2789 }
2790
954911eb
ID
2791 if (dirty & WM_DIRTY_LP(1) &&
2792 previous->wm_lp_spr[0] != results->wm_lp_spr[0])
2793 I915_WRITE(WM1S_LP_ILK, results->wm_lp_spr[0]);
2794
2795 if (INTEL_INFO(dev)->gen >= 7) {
6cef2b8a
VS
2796 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp_spr[1] != results->wm_lp_spr[1])
2797 I915_WRITE(WM2S_LP_IVB, results->wm_lp_spr[1]);
2798 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp_spr[2] != results->wm_lp_spr[2])
2799 I915_WRITE(WM3S_LP_IVB, results->wm_lp_spr[2]);
2800 }
801bcfff 2801
facd619b 2802 if (dirty & WM_DIRTY_LP(1) && previous->wm_lp[0] != results->wm_lp[0])
801bcfff 2803 I915_WRITE(WM1_LP_ILK, results->wm_lp[0]);
facd619b 2804 if (dirty & WM_DIRTY_LP(2) && previous->wm_lp[1] != results->wm_lp[1])
801bcfff 2805 I915_WRITE(WM2_LP_ILK, results->wm_lp[1]);
facd619b 2806 if (dirty & WM_DIRTY_LP(3) && previous->wm_lp[2] != results->wm_lp[2])
801bcfff 2807 I915_WRITE(WM3_LP_ILK, results->wm_lp[2]);
609cedef
VS
2808
2809 dev_priv->wm.hw = *results;
801bcfff
PZ
2810}
2811
ed4a6a7c 2812bool ilk_disable_lp_wm(struct drm_device *dev)
8553c18e
VS
2813{
2814 struct drm_i915_private *dev_priv = dev->dev_private;
2815
2816 return _ilk_disable_lp_wm(dev_priv, WM_DIRTY_LP_ALL);
2817}
2818
b9cec075
DL
2819/*
2820 * On gen9, we need to allocate Display Data Buffer (DDB) portions to the
2821 * different active planes.
2822 */
2823
2824#define SKL_DDB_SIZE 896 /* in blocks */
43d735a6 2825#define BXT_DDB_SIZE 512
b9cec075 2826
024c9045
MR
2827/*
2828 * Return the index of a plane in the SKL DDB and wm result arrays. Primary
2829 * plane is always in slot 0, cursor is always in slot I915_MAX_PLANES-1, and
2830 * other universal planes are in indices 1..n. Note that this may leave unused
2831 * indices between the top "sprite" plane and the cursor.
2832 */
2833static int
2834skl_wm_plane_id(const struct intel_plane *plane)
2835{
2836 switch (plane->base.type) {
2837 case DRM_PLANE_TYPE_PRIMARY:
2838 return 0;
2839 case DRM_PLANE_TYPE_CURSOR:
2840 return PLANE_CURSOR;
2841 case DRM_PLANE_TYPE_OVERLAY:
2842 return plane->plane + 1;
2843 default:
2844 MISSING_CASE(plane->base.type);
2845 return plane->plane;
2846 }
2847}
2848
b9cec075
DL
2849static void
2850skl_ddb_get_pipe_allocation_limits(struct drm_device *dev,
024c9045 2851 const struct intel_crtc_state *cstate,
b9cec075 2852 const struct intel_wm_config *config,
b9cec075
DL
2853 struct skl_ddb_entry *alloc /* out */)
2854{
024c9045 2855 struct drm_crtc *for_crtc = cstate->base.crtc;
b9cec075
DL
2856 struct drm_crtc *crtc;
2857 unsigned int pipe_size, ddb_size;
2858 int nth_active_pipe;
2859
024c9045 2860 if (!cstate->base.active) {
b9cec075
DL
2861 alloc->start = 0;
2862 alloc->end = 0;
2863 return;
2864 }
2865
43d735a6
DL
2866 if (IS_BROXTON(dev))
2867 ddb_size = BXT_DDB_SIZE;
2868 else
2869 ddb_size = SKL_DDB_SIZE;
b9cec075
DL
2870
2871 ddb_size -= 4; /* 4 blocks for bypass path allocation */
2872
2873 nth_active_pipe = 0;
2874 for_each_crtc(dev, crtc) {
3ef00284 2875 if (!to_intel_crtc(crtc)->active)
b9cec075
DL
2876 continue;
2877
2878 if (crtc == for_crtc)
2879 break;
2880
2881 nth_active_pipe++;
2882 }
2883
2884 pipe_size = ddb_size / config->num_pipes_active;
2885 alloc->start = nth_active_pipe * ddb_size / config->num_pipes_active;
16160e3d 2886 alloc->end = alloc->start + pipe_size;
b9cec075
DL
2887}
2888
2889static unsigned int skl_cursor_allocation(const struct intel_wm_config *config)
2890{
2891 if (config->num_pipes_active == 1)
2892 return 32;
2893
2894 return 8;
2895}
2896
a269c583
DL
2897static void skl_ddb_entry_init_from_hw(struct skl_ddb_entry *entry, u32 reg)
2898{
2899 entry->start = reg & 0x3ff;
2900 entry->end = (reg >> 16) & 0x3ff;
16160e3d
DL
2901 if (entry->end)
2902 entry->end += 1;
a269c583
DL
2903}
2904
08db6652
DL
2905void skl_ddb_get_hw_state(struct drm_i915_private *dev_priv,
2906 struct skl_ddb_allocation *ddb /* out */)
a269c583 2907{
a269c583
DL
2908 enum pipe pipe;
2909 int plane;
2910 u32 val;
2911
b10f1b20
ML
2912 memset(ddb, 0, sizeof(*ddb));
2913
a269c583 2914 for_each_pipe(dev_priv, pipe) {
4d800030
ID
2915 enum intel_display_power_domain power_domain;
2916
2917 power_domain = POWER_DOMAIN_PIPE(pipe);
2918 if (!intel_display_power_get_if_enabled(dev_priv, power_domain))
b10f1b20
ML
2919 continue;
2920
dd740780 2921 for_each_plane(dev_priv, pipe, plane) {
a269c583
DL
2922 val = I915_READ(PLANE_BUF_CFG(pipe, plane));
2923 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][plane],
2924 val);
2925 }
2926
2927 val = I915_READ(CUR_BUF_CFG(pipe));
4969d33e
MR
2928 skl_ddb_entry_init_from_hw(&ddb->plane[pipe][PLANE_CURSOR],
2929 val);
4d800030
ID
2930
2931 intel_display_power_put(dev_priv, power_domain);
a269c583
DL
2932 }
2933}
2934
b9cec075 2935static unsigned int
024c9045
MR
2936skl_plane_relative_data_rate(const struct intel_crtc_state *cstate,
2937 const struct drm_plane_state *pstate,
2938 int y)
b9cec075 2939{
a280f7dd 2940 struct intel_plane_state *intel_pstate = to_intel_plane_state(pstate);
024c9045 2941 struct drm_framebuffer *fb = pstate->fb;
a280f7dd
KM
2942 uint32_t width = 0, height = 0;
2943
2944 width = drm_rect_width(&intel_pstate->src) >> 16;
2945 height = drm_rect_height(&intel_pstate->src) >> 16;
2946
2947 if (intel_rotation_90_or_270(pstate->rotation))
2948 swap(width, height);
2cd601c6
CK
2949
2950 /* for planar format */
024c9045 2951 if (fb->pixel_format == DRM_FORMAT_NV12) {
2cd601c6 2952 if (y) /* y-plane data rate */
a280f7dd 2953 return width * height *
024c9045 2954 drm_format_plane_cpp(fb->pixel_format, 0);
2cd601c6 2955 else /* uv-plane data rate */
a280f7dd 2956 return (width / 2) * (height / 2) *
024c9045 2957 drm_format_plane_cpp(fb->pixel_format, 1);
2cd601c6
CK
2958 }
2959
2960 /* for packed formats */
a280f7dd 2961 return width * height * drm_format_plane_cpp(fb->pixel_format, 0);
b9cec075
DL
2962}
2963
2964/*
2965 * We don't overflow 32 bits. Worst case is 3 planes enabled, each fetching
2966 * a 8192x4096@32bpp framebuffer:
2967 * 3 * 4096 * 8192 * 4 < 2^32
2968 */
2969static unsigned int
024c9045 2970skl_get_total_relative_data_rate(const struct intel_crtc_state *cstate)
b9cec075 2971{
024c9045
MR
2972 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
2973 struct drm_device *dev = intel_crtc->base.dev;
2974 const struct intel_plane *intel_plane;
b9cec075 2975 unsigned int total_data_rate = 0;
b9cec075 2976
024c9045
MR
2977 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
2978 const struct drm_plane_state *pstate = intel_plane->base.state;
b9cec075 2979
024c9045 2980 if (pstate->fb == NULL)
b9cec075
DL
2981 continue;
2982
024c9045
MR
2983 if (intel_plane->base.type == DRM_PLANE_TYPE_CURSOR)
2984 continue;
2985
2986 /* packed/uv */
2987 total_data_rate += skl_plane_relative_data_rate(cstate,
2988 pstate,
2989 0);
2990
2991 if (pstate->fb->pixel_format == DRM_FORMAT_NV12)
2992 /* y-plane */
2993 total_data_rate += skl_plane_relative_data_rate(cstate,
2994 pstate,
2995 1);
b9cec075
DL
2996 }
2997
2998 return total_data_rate;
2999}
3000
3001static void
024c9045 3002skl_allocate_pipe_ddb(struct intel_crtc_state *cstate,
b9cec075
DL
3003 struct skl_ddb_allocation *ddb /* out */)
3004{
024c9045 3005 struct drm_crtc *crtc = cstate->base.crtc;
b9cec075 3006 struct drm_device *dev = crtc->dev;
aa363136
MR
3007 struct drm_i915_private *dev_priv = to_i915(dev);
3008 struct intel_wm_config *config = &dev_priv->wm.config;
b9cec075 3009 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
024c9045 3010 struct intel_plane *intel_plane;
b9cec075 3011 enum pipe pipe = intel_crtc->pipe;
34bb56af 3012 struct skl_ddb_entry *alloc = &ddb->pipe[pipe];
b9cec075 3013 uint16_t alloc_size, start, cursor_blocks;
80958155 3014 uint16_t minimum[I915_MAX_PLANES];
2cd601c6 3015 uint16_t y_minimum[I915_MAX_PLANES];
b9cec075 3016 unsigned int total_data_rate;
b9cec075 3017
024c9045 3018 skl_ddb_get_pipe_allocation_limits(dev, cstate, config, alloc);
34bb56af 3019 alloc_size = skl_ddb_entry_size(alloc);
b9cec075
DL
3020 if (alloc_size == 0) {
3021 memset(ddb->plane[pipe], 0, sizeof(ddb->plane[pipe]));
4969d33e
MR
3022 memset(&ddb->plane[pipe][PLANE_CURSOR], 0,
3023 sizeof(ddb->plane[pipe][PLANE_CURSOR]));
b9cec075
DL
3024 return;
3025 }
3026
3027 cursor_blocks = skl_cursor_allocation(config);
4969d33e
MR
3028 ddb->plane[pipe][PLANE_CURSOR].start = alloc->end - cursor_blocks;
3029 ddb->plane[pipe][PLANE_CURSOR].end = alloc->end;
b9cec075
DL
3030
3031 alloc_size -= cursor_blocks;
34bb56af 3032 alloc->end -= cursor_blocks;
b9cec075 3033
80958155 3034 /* 1. Allocate the mininum required blocks for each active plane */
024c9045
MR
3035 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3036 struct drm_plane *plane = &intel_plane->base;
3037 struct drm_framebuffer *fb = plane->state->fb;
3038 int id = skl_wm_plane_id(intel_plane);
80958155 3039
a280f7dd 3040 if (!to_intel_plane_state(plane->state)->visible)
024c9045 3041 continue;
a280f7dd 3042
024c9045 3043 if (plane->type == DRM_PLANE_TYPE_CURSOR)
80958155
DL
3044 continue;
3045
024c9045
MR
3046 minimum[id] = 8;
3047 alloc_size -= minimum[id];
3048 y_minimum[id] = (fb->pixel_format == DRM_FORMAT_NV12) ? 8 : 0;
3049 alloc_size -= y_minimum[id];
80958155
DL
3050 }
3051
b9cec075 3052 /*
80958155
DL
3053 * 2. Distribute the remaining space in proportion to the amount of
3054 * data each plane needs to fetch from memory.
b9cec075
DL
3055 *
3056 * FIXME: we may not allocate every single block here.
3057 */
024c9045 3058 total_data_rate = skl_get_total_relative_data_rate(cstate);
b9cec075 3059
34bb56af 3060 start = alloc->start;
024c9045
MR
3061 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3062 struct drm_plane *plane = &intel_plane->base;
3063 struct drm_plane_state *pstate = intel_plane->base.state;
2cd601c6
CK
3064 unsigned int data_rate, y_data_rate;
3065 uint16_t plane_blocks, y_plane_blocks = 0;
024c9045 3066 int id = skl_wm_plane_id(intel_plane);
b9cec075 3067
a280f7dd 3068 if (!to_intel_plane_state(pstate)->visible)
024c9045
MR
3069 continue;
3070 if (plane->type == DRM_PLANE_TYPE_CURSOR)
b9cec075
DL
3071 continue;
3072
024c9045 3073 data_rate = skl_plane_relative_data_rate(cstate, pstate, 0);
b9cec075
DL
3074
3075 /*
2cd601c6 3076 * allocation for (packed formats) or (uv-plane part of planar format):
b9cec075
DL
3077 * promote the expression to 64 bits to avoid overflowing, the
3078 * result is < available as data_rate / total_data_rate < 1
3079 */
024c9045 3080 plane_blocks = minimum[id];
80958155
DL
3081 plane_blocks += div_u64((uint64_t)alloc_size * data_rate,
3082 total_data_rate);
b9cec075 3083
024c9045
MR
3084 ddb->plane[pipe][id].start = start;
3085 ddb->plane[pipe][id].end = start + plane_blocks;
b9cec075
DL
3086
3087 start += plane_blocks;
2cd601c6
CK
3088
3089 /*
3090 * allocation for y_plane part of planar format:
3091 */
024c9045
MR
3092 if (pstate->fb->pixel_format == DRM_FORMAT_NV12) {
3093 y_data_rate = skl_plane_relative_data_rate(cstate,
3094 pstate,
3095 1);
3096 y_plane_blocks = y_minimum[id];
2cd601c6
CK
3097 y_plane_blocks += div_u64((uint64_t)alloc_size * y_data_rate,
3098 total_data_rate);
3099
024c9045
MR
3100 ddb->y_plane[pipe][id].start = start;
3101 ddb->y_plane[pipe][id].end = start + y_plane_blocks;
2cd601c6
CK
3102
3103 start += y_plane_blocks;
3104 }
3105
b9cec075
DL
3106 }
3107
3108}
3109
5cec258b 3110static uint32_t skl_pipe_pixel_rate(const struct intel_crtc_state *config)
2d41c0b5
PB
3111{
3112 /* TODO: Take into account the scalers once we support them */
2d112de7 3113 return config->base.adjusted_mode.crtc_clock;
2d41c0b5
PB
3114}
3115
3116/*
3117 * The max latency should be 257 (max the punit can code is 255 and we add 2us
ac484963 3118 * for the read latency) and cpp should always be <= 8, so that
2d41c0b5
PB
3119 * should allow pixel_rate up to ~2 GHz which seems sufficient since max
3120 * 2xcdclk is 1350 MHz and the pixel rate should never exceed that.
3121*/
ac484963 3122static uint32_t skl_wm_method1(uint32_t pixel_rate, uint8_t cpp, uint32_t latency)
2d41c0b5
PB
3123{
3124 uint32_t wm_intermediate_val, ret;
3125
3126 if (latency == 0)
3127 return UINT_MAX;
3128
ac484963 3129 wm_intermediate_val = latency * pixel_rate * cpp / 512;
2d41c0b5
PB
3130 ret = DIV_ROUND_UP(wm_intermediate_val, 1000);
3131
3132 return ret;
3133}
3134
3135static uint32_t skl_wm_method2(uint32_t pixel_rate, uint32_t pipe_htotal,
ac484963 3136 uint32_t horiz_pixels, uint8_t cpp,
0fda6568 3137 uint64_t tiling, uint32_t latency)
2d41c0b5 3138{
d4c2aa60
TU
3139 uint32_t ret;
3140 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3141 uint32_t wm_intermediate_val;
2d41c0b5
PB
3142
3143 if (latency == 0)
3144 return UINT_MAX;
3145
ac484963 3146 plane_bytes_per_line = horiz_pixels * cpp;
0fda6568
TU
3147
3148 if (tiling == I915_FORMAT_MOD_Y_TILED ||
3149 tiling == I915_FORMAT_MOD_Yf_TILED) {
3150 plane_bytes_per_line *= 4;
3151 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3152 plane_blocks_per_line /= 4;
3153 } else {
3154 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
3155 }
3156
2d41c0b5
PB
3157 wm_intermediate_val = latency * pixel_rate;
3158 ret = DIV_ROUND_UP(wm_intermediate_val, pipe_htotal * 1000) *
d4c2aa60 3159 plane_blocks_per_line;
2d41c0b5
PB
3160
3161 return ret;
3162}
3163
2d41c0b5
PB
3164static bool skl_ddb_allocation_changed(const struct skl_ddb_allocation *new_ddb,
3165 const struct intel_crtc *intel_crtc)
3166{
3167 struct drm_device *dev = intel_crtc->base.dev;
3168 struct drm_i915_private *dev_priv = dev->dev_private;
3169 const struct skl_ddb_allocation *cur_ddb = &dev_priv->wm.skl_hw.ddb;
2d41c0b5 3170
e6d90023
KM
3171 /*
3172 * If ddb allocation of pipes changed, it may require recalculation of
3173 * watermarks
3174 */
3175 if (memcmp(new_ddb->pipe, cur_ddb->pipe, sizeof(new_ddb->pipe)))
2d41c0b5
PB
3176 return true;
3177
3178 return false;
3179}
3180
d4c2aa60 3181static bool skl_compute_plane_wm(const struct drm_i915_private *dev_priv,
024c9045
MR
3182 struct intel_crtc_state *cstate,
3183 struct intel_plane *intel_plane,
afb024aa 3184 uint16_t ddb_allocation,
d4c2aa60 3185 int level,
afb024aa
DL
3186 uint16_t *out_blocks, /* out */
3187 uint8_t *out_lines /* out */)
2d41c0b5 3188{
024c9045
MR
3189 struct drm_plane *plane = &intel_plane->base;
3190 struct drm_framebuffer *fb = plane->state->fb;
a280f7dd
KM
3191 struct intel_plane_state *intel_pstate =
3192 to_intel_plane_state(plane->state);
d4c2aa60
TU
3193 uint32_t latency = dev_priv->wm.skl_latency[level];
3194 uint32_t method1, method2;
3195 uint32_t plane_bytes_per_line, plane_blocks_per_line;
3196 uint32_t res_blocks, res_lines;
3197 uint32_t selected_result;
ac484963 3198 uint8_t cpp;
a280f7dd 3199 uint32_t width = 0, height = 0;
2d41c0b5 3200
a280f7dd 3201 if (latency == 0 || !cstate->base.active || !intel_pstate->visible)
2d41c0b5
PB
3202 return false;
3203
a280f7dd
KM
3204 width = drm_rect_width(&intel_pstate->src) >> 16;
3205 height = drm_rect_height(&intel_pstate->src) >> 16;
3206
3207 if (intel_rotation_90_or_270(plane->state->rotation))
3208 swap(width, height);
3209
ac484963 3210 cpp = drm_format_plane_cpp(fb->pixel_format, 0);
024c9045 3211 method1 = skl_wm_method1(skl_pipe_pixel_rate(cstate),
ac484963 3212 cpp, latency);
024c9045
MR
3213 method2 = skl_wm_method2(skl_pipe_pixel_rate(cstate),
3214 cstate->base.adjusted_mode.crtc_htotal,
a280f7dd
KM
3215 width,
3216 cpp,
3217 fb->modifier[0],
d4c2aa60 3218 latency);
2d41c0b5 3219
a280f7dd 3220 plane_bytes_per_line = width * cpp;
d4c2aa60 3221 plane_blocks_per_line = DIV_ROUND_UP(plane_bytes_per_line, 512);
2d41c0b5 3222
024c9045
MR
3223 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3224 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED) {
1fc0a8f7
TU
3225 uint32_t min_scanlines = 4;
3226 uint32_t y_tile_minimum;
024c9045 3227 if (intel_rotation_90_or_270(plane->state->rotation)) {
ac484963 3228 int cpp = (fb->pixel_format == DRM_FORMAT_NV12) ?
024c9045
MR
3229 drm_format_plane_cpp(fb->pixel_format, 1) :
3230 drm_format_plane_cpp(fb->pixel_format, 0);
3231
ac484963 3232 switch (cpp) {
1fc0a8f7
TU
3233 case 1:
3234 min_scanlines = 16;
3235 break;
3236 case 2:
3237 min_scanlines = 8;
3238 break;
3239 case 8:
3240 WARN(1, "Unsupported pixel depth for rotation");
2f0b5790 3241 }
1fc0a8f7
TU
3242 }
3243 y_tile_minimum = plane_blocks_per_line * min_scanlines;
0fda6568
TU
3244 selected_result = max(method2, y_tile_minimum);
3245 } else {
3246 if ((ddb_allocation / plane_blocks_per_line) >= 1)
3247 selected_result = min(method1, method2);
3248 else
3249 selected_result = method1;
3250 }
2d41c0b5 3251
d4c2aa60
TU
3252 res_blocks = selected_result + 1;
3253 res_lines = DIV_ROUND_UP(selected_result, plane_blocks_per_line);
e6d66171 3254
0fda6568 3255 if (level >= 1 && level <= 7) {
024c9045
MR
3256 if (fb->modifier[0] == I915_FORMAT_MOD_Y_TILED ||
3257 fb->modifier[0] == I915_FORMAT_MOD_Yf_TILED)
0fda6568
TU
3258 res_lines += 4;
3259 else
3260 res_blocks++;
3261 }
e6d66171 3262
d4c2aa60 3263 if (res_blocks >= ddb_allocation || res_lines > 31)
e6d66171
DL
3264 return false;
3265
3266 *out_blocks = res_blocks;
3267 *out_lines = res_lines;
2d41c0b5
PB
3268
3269 return true;
3270}
3271
3272static void skl_compute_wm_level(const struct drm_i915_private *dev_priv,
3273 struct skl_ddb_allocation *ddb,
024c9045 3274 struct intel_crtc_state *cstate,
2d41c0b5 3275 int level,
2d41c0b5
PB
3276 struct skl_wm_level *result)
3277{
024c9045
MR
3278 struct drm_device *dev = dev_priv->dev;
3279 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
3280 struct intel_plane *intel_plane;
2d41c0b5 3281 uint16_t ddb_blocks;
024c9045
MR
3282 enum pipe pipe = intel_crtc->pipe;
3283
3284 for_each_intel_plane_on_crtc(dev, intel_crtc, intel_plane) {
3285 int i = skl_wm_plane_id(intel_plane);
2d41c0b5 3286
2d41c0b5
PB
3287 ddb_blocks = skl_ddb_entry_size(&ddb->plane[pipe][i]);
3288
d4c2aa60 3289 result->plane_en[i] = skl_compute_plane_wm(dev_priv,
024c9045
MR
3290 cstate,
3291 intel_plane,
2d41c0b5 3292 ddb_blocks,
d4c2aa60 3293 level,
2d41c0b5
PB
3294 &result->plane_res_b[i],
3295 &result->plane_res_l[i]);
3296 }
2d41c0b5
PB
3297}
3298
407b50f3 3299static uint32_t
024c9045 3300skl_compute_linetime_wm(struct intel_crtc_state *cstate)
407b50f3 3301{
024c9045 3302 if (!cstate->base.active)
407b50f3
DL
3303 return 0;
3304
024c9045 3305 if (WARN_ON(skl_pipe_pixel_rate(cstate) == 0))
661abfc0 3306 return 0;
407b50f3 3307
024c9045
MR
3308 return DIV_ROUND_UP(8 * cstate->base.adjusted_mode.crtc_htotal * 1000,
3309 skl_pipe_pixel_rate(cstate));
407b50f3
DL
3310}
3311
024c9045 3312static void skl_compute_transition_wm(struct intel_crtc_state *cstate,
9414f563 3313 struct skl_wm_level *trans_wm /* out */)
407b50f3 3314{
024c9045 3315 struct drm_crtc *crtc = cstate->base.crtc;
9414f563 3316 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
024c9045 3317 struct intel_plane *intel_plane;
9414f563 3318
024c9045 3319 if (!cstate->base.active)
407b50f3 3320 return;
9414f563
DL
3321
3322 /* Until we know more, just disable transition WMs */
024c9045
MR
3323 for_each_intel_plane_on_crtc(crtc->dev, intel_crtc, intel_plane) {
3324 int i = skl_wm_plane_id(intel_plane);
3325
9414f563 3326 trans_wm->plane_en[i] = false;
024c9045 3327 }
407b50f3
DL
3328}
3329
024c9045 3330static void skl_compute_pipe_wm(struct intel_crtc_state *cstate,
2d41c0b5 3331 struct skl_ddb_allocation *ddb,
2d41c0b5
PB
3332 struct skl_pipe_wm *pipe_wm)
3333{
024c9045 3334 struct drm_device *dev = cstate->base.crtc->dev;
2d41c0b5 3335 const struct drm_i915_private *dev_priv = dev->dev_private;
2d41c0b5
PB
3336 int level, max_level = ilk_wm_max_level(dev);
3337
3338 for (level = 0; level <= max_level; level++) {
024c9045
MR
3339 skl_compute_wm_level(dev_priv, ddb, cstate,
3340 level, &pipe_wm->wm[level]);
2d41c0b5 3341 }
024c9045 3342 pipe_wm->linetime = skl_compute_linetime_wm(cstate);
2d41c0b5 3343
024c9045 3344 skl_compute_transition_wm(cstate, &pipe_wm->trans_wm);
2d41c0b5
PB
3345}
3346
3347static void skl_compute_wm_results(struct drm_device *dev,
2d41c0b5
PB
3348 struct skl_pipe_wm *p_wm,
3349 struct skl_wm_values *r,
3350 struct intel_crtc *intel_crtc)
3351{
3352 int level, max_level = ilk_wm_max_level(dev);
3353 enum pipe pipe = intel_crtc->pipe;
9414f563
DL
3354 uint32_t temp;
3355 int i;
2d41c0b5
PB
3356
3357 for (level = 0; level <= max_level; level++) {
2d41c0b5
PB
3358 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3359 temp = 0;
2d41c0b5
PB
3360
3361 temp |= p_wm->wm[level].plane_res_l[i] <<
3362 PLANE_WM_LINES_SHIFT;
3363 temp |= p_wm->wm[level].plane_res_b[i];
3364 if (p_wm->wm[level].plane_en[i])
3365 temp |= PLANE_WM_EN;
3366
3367 r->plane[pipe][i][level] = temp;
2d41c0b5
PB
3368 }
3369
3370 temp = 0;
2d41c0b5 3371
4969d33e
MR
3372 temp |= p_wm->wm[level].plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3373 temp |= p_wm->wm[level].plane_res_b[PLANE_CURSOR];
2d41c0b5 3374
4969d33e 3375 if (p_wm->wm[level].plane_en[PLANE_CURSOR])
2d41c0b5
PB
3376 temp |= PLANE_WM_EN;
3377
4969d33e 3378 r->plane[pipe][PLANE_CURSOR][level] = temp;
2d41c0b5
PB
3379
3380 }
3381
9414f563
DL
3382 /* transition WMs */
3383 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3384 temp = 0;
3385 temp |= p_wm->trans_wm.plane_res_l[i] << PLANE_WM_LINES_SHIFT;
3386 temp |= p_wm->trans_wm.plane_res_b[i];
3387 if (p_wm->trans_wm.plane_en[i])
3388 temp |= PLANE_WM_EN;
3389
3390 r->plane_trans[pipe][i] = temp;
3391 }
3392
3393 temp = 0;
4969d33e
MR
3394 temp |= p_wm->trans_wm.plane_res_l[PLANE_CURSOR] << PLANE_WM_LINES_SHIFT;
3395 temp |= p_wm->trans_wm.plane_res_b[PLANE_CURSOR];
3396 if (p_wm->trans_wm.plane_en[PLANE_CURSOR])
9414f563
DL
3397 temp |= PLANE_WM_EN;
3398
4969d33e 3399 r->plane_trans[pipe][PLANE_CURSOR] = temp;
9414f563 3400
2d41c0b5
PB
3401 r->wm_linetime[pipe] = p_wm->linetime;
3402}
3403
f0f59a00
VS
3404static void skl_ddb_entry_write(struct drm_i915_private *dev_priv,
3405 i915_reg_t reg,
16160e3d
DL
3406 const struct skl_ddb_entry *entry)
3407{
3408 if (entry->end)
3409 I915_WRITE(reg, (entry->end - 1) << 16 | entry->start);
3410 else
3411 I915_WRITE(reg, 0);
3412}
3413
2d41c0b5
PB
3414static void skl_write_wm_values(struct drm_i915_private *dev_priv,
3415 const struct skl_wm_values *new)
3416{
3417 struct drm_device *dev = dev_priv->dev;
3418 struct intel_crtc *crtc;
3419
19c8054c 3420 for_each_intel_crtc(dev, crtc) {
2d41c0b5
PB
3421 int i, level, max_level = ilk_wm_max_level(dev);
3422 enum pipe pipe = crtc->pipe;
3423
5d374d96
DL
3424 if (!new->dirty[pipe])
3425 continue;
8211bd5b 3426
5d374d96 3427 I915_WRITE(PIPE_WM_LINETIME(pipe), new->wm_linetime[pipe]);
8211bd5b 3428
5d374d96
DL
3429 for (level = 0; level <= max_level; level++) {
3430 for (i = 0; i < intel_num_planes(crtc); i++)
3431 I915_WRITE(PLANE_WM(pipe, i, level),
3432 new->plane[pipe][i][level]);
3433 I915_WRITE(CUR_WM(pipe, level),
4969d33e 3434 new->plane[pipe][PLANE_CURSOR][level]);
2d41c0b5 3435 }
5d374d96
DL
3436 for (i = 0; i < intel_num_planes(crtc); i++)
3437 I915_WRITE(PLANE_WM_TRANS(pipe, i),
3438 new->plane_trans[pipe][i]);
4969d33e
MR
3439 I915_WRITE(CUR_WM_TRANS(pipe),
3440 new->plane_trans[pipe][PLANE_CURSOR]);
5d374d96 3441
2cd601c6 3442 for (i = 0; i < intel_num_planes(crtc); i++) {
5d374d96
DL
3443 skl_ddb_entry_write(dev_priv,
3444 PLANE_BUF_CFG(pipe, i),
3445 &new->ddb.plane[pipe][i]);
2cd601c6
CK
3446 skl_ddb_entry_write(dev_priv,
3447 PLANE_NV12_BUF_CFG(pipe, i),
3448 &new->ddb.y_plane[pipe][i]);
3449 }
5d374d96
DL
3450
3451 skl_ddb_entry_write(dev_priv, CUR_BUF_CFG(pipe),
4969d33e 3452 &new->ddb.plane[pipe][PLANE_CURSOR]);
2d41c0b5 3453 }
2d41c0b5
PB
3454}
3455
0e8fb7ba
DL
3456/*
3457 * When setting up a new DDB allocation arrangement, we need to correctly
3458 * sequence the times at which the new allocations for the pipes are taken into
3459 * account or we'll have pipes fetching from space previously allocated to
3460 * another pipe.
3461 *
3462 * Roughly the sequence looks like:
3463 * 1. re-allocate the pipe(s) with the allocation being reduced and not
3464 * overlapping with a previous light-up pipe (another way to put it is:
3465 * pipes with their new allocation strickly included into their old ones).
3466 * 2. re-allocate the other pipes that get their allocation reduced
3467 * 3. allocate the pipes having their allocation increased
3468 *
3469 * Steps 1. and 2. are here to take care of the following case:
3470 * - Initially DDB looks like this:
3471 * | B | C |
3472 * - enable pipe A.
3473 * - pipe B has a reduced DDB allocation that overlaps with the old pipe C
3474 * allocation
3475 * | A | B | C |
3476 *
3477 * We need to sequence the re-allocation: C, B, A (and not B, C, A).
3478 */
3479
d21b795c
DL
3480static void
3481skl_wm_flush_pipe(struct drm_i915_private *dev_priv, enum pipe pipe, int pass)
0e8fb7ba 3482{
0e8fb7ba
DL
3483 int plane;
3484
d21b795c
DL
3485 DRM_DEBUG_KMS("flush pipe %c (pass %d)\n", pipe_name(pipe), pass);
3486
dd740780 3487 for_each_plane(dev_priv, pipe, plane) {
0e8fb7ba
DL
3488 I915_WRITE(PLANE_SURF(pipe, plane),
3489 I915_READ(PLANE_SURF(pipe, plane)));
3490 }
3491 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3492}
3493
3494static bool
3495skl_ddb_allocation_included(const struct skl_ddb_allocation *old,
3496 const struct skl_ddb_allocation *new,
3497 enum pipe pipe)
3498{
3499 uint16_t old_size, new_size;
3500
3501 old_size = skl_ddb_entry_size(&old->pipe[pipe]);
3502 new_size = skl_ddb_entry_size(&new->pipe[pipe]);
3503
3504 return old_size != new_size &&
3505 new->pipe[pipe].start >= old->pipe[pipe].start &&
3506 new->pipe[pipe].end <= old->pipe[pipe].end;
3507}
3508
3509static void skl_flush_wm_values(struct drm_i915_private *dev_priv,
3510 struct skl_wm_values *new_values)
3511{
3512 struct drm_device *dev = dev_priv->dev;
3513 struct skl_ddb_allocation *cur_ddb, *new_ddb;
c929cb45 3514 bool reallocated[I915_MAX_PIPES] = {};
0e8fb7ba
DL
3515 struct intel_crtc *crtc;
3516 enum pipe pipe;
3517
3518 new_ddb = &new_values->ddb;
3519 cur_ddb = &dev_priv->wm.skl_hw.ddb;
3520
3521 /*
3522 * First pass: flush the pipes with the new allocation contained into
3523 * the old space.
3524 *
3525 * We'll wait for the vblank on those pipes to ensure we can safely
3526 * re-allocate the freed space without this pipe fetching from it.
3527 */
3528 for_each_intel_crtc(dev, crtc) {
3529 if (!crtc->active)
3530 continue;
3531
3532 pipe = crtc->pipe;
3533
3534 if (!skl_ddb_allocation_included(cur_ddb, new_ddb, pipe))
3535 continue;
3536
d21b795c 3537 skl_wm_flush_pipe(dev_priv, pipe, 1);
0e8fb7ba
DL
3538 intel_wait_for_vblank(dev, pipe);
3539
3540 reallocated[pipe] = true;
3541 }
3542
3543
3544 /*
3545 * Second pass: flush the pipes that are having their allocation
3546 * reduced, but overlapping with a previous allocation.
3547 *
3548 * Here as well we need to wait for the vblank to make sure the freed
3549 * space is not used anymore.
3550 */
3551 for_each_intel_crtc(dev, crtc) {
3552 if (!crtc->active)
3553 continue;
3554
3555 pipe = crtc->pipe;
3556
3557 if (reallocated[pipe])
3558 continue;
3559
3560 if (skl_ddb_entry_size(&new_ddb->pipe[pipe]) <
3561 skl_ddb_entry_size(&cur_ddb->pipe[pipe])) {
d21b795c 3562 skl_wm_flush_pipe(dev_priv, pipe, 2);
0e8fb7ba 3563 intel_wait_for_vblank(dev, pipe);
d9d8e6b3 3564 reallocated[pipe] = true;
0e8fb7ba 3565 }
0e8fb7ba
DL
3566 }
3567
3568 /*
3569 * Third pass: flush the pipes that got more space allocated.
3570 *
3571 * We don't need to actively wait for the update here, next vblank
3572 * will just get more DDB space with the correct WM values.
3573 */
3574 for_each_intel_crtc(dev, crtc) {
3575 if (!crtc->active)
3576 continue;
3577
3578 pipe = crtc->pipe;
3579
3580 /*
3581 * At this point, only the pipes more space than before are
3582 * left to re-allocate.
3583 */
3584 if (reallocated[pipe])
3585 continue;
3586
d21b795c 3587 skl_wm_flush_pipe(dev_priv, pipe, 3);
0e8fb7ba
DL
3588 }
3589}
3590
2d41c0b5 3591static bool skl_update_pipe_wm(struct drm_crtc *crtc,
2d41c0b5
PB
3592 struct skl_ddb_allocation *ddb, /* out */
3593 struct skl_pipe_wm *pipe_wm /* out */)
3594{
3595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
024c9045 3596 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
2d41c0b5 3597
aa363136 3598 skl_allocate_pipe_ddb(cstate, ddb);
024c9045 3599 skl_compute_pipe_wm(cstate, ddb, pipe_wm);
2d41c0b5 3600
4e0963c7 3601 if (!memcmp(&intel_crtc->wm.active.skl, pipe_wm, sizeof(*pipe_wm)))
2d41c0b5
PB
3602 return false;
3603
4e0963c7 3604 intel_crtc->wm.active.skl = *pipe_wm;
2cd601c6 3605
2d41c0b5
PB
3606 return true;
3607}
3608
3609static void skl_update_other_pipe_wm(struct drm_device *dev,
3610 struct drm_crtc *crtc,
2d41c0b5
PB
3611 struct skl_wm_values *r)
3612{
3613 struct intel_crtc *intel_crtc;
3614 struct intel_crtc *this_crtc = to_intel_crtc(crtc);
3615
3616 /*
3617 * If the WM update hasn't changed the allocation for this_crtc (the
3618 * crtc we are currently computing the new WM values for), other
3619 * enabled crtcs will keep the same allocation and we don't need to
3620 * recompute anything for them.
3621 */
3622 if (!skl_ddb_allocation_changed(&r->ddb, this_crtc))
3623 return;
3624
3625 /*
3626 * Otherwise, because of this_crtc being freshly enabled/disabled, the
3627 * other active pipes need new DDB allocation and WM values.
3628 */
19c8054c 3629 for_each_intel_crtc(dev, intel_crtc) {
2d41c0b5
PB
3630 struct skl_pipe_wm pipe_wm = {};
3631 bool wm_changed;
3632
3633 if (this_crtc->pipe == intel_crtc->pipe)
3634 continue;
3635
3636 if (!intel_crtc->active)
3637 continue;
3638
aa363136 3639 wm_changed = skl_update_pipe_wm(&intel_crtc->base,
2d41c0b5
PB
3640 &r->ddb, &pipe_wm);
3641
3642 /*
3643 * If we end up re-computing the other pipe WM values, it's
3644 * because it was really needed, so we expect the WM values to
3645 * be different.
3646 */
3647 WARN_ON(!wm_changed);
3648
024c9045 3649 skl_compute_wm_results(dev, &pipe_wm, r, intel_crtc);
2d41c0b5
PB
3650 r->dirty[intel_crtc->pipe] = true;
3651 }
3652}
3653
adda50b8
BP
3654static void skl_clear_wm(struct skl_wm_values *watermarks, enum pipe pipe)
3655{
3656 watermarks->wm_linetime[pipe] = 0;
3657 memset(watermarks->plane[pipe], 0,
3658 sizeof(uint32_t) * 8 * I915_MAX_PLANES);
adda50b8
BP
3659 memset(watermarks->plane_trans[pipe],
3660 0, sizeof(uint32_t) * I915_MAX_PLANES);
4969d33e 3661 watermarks->plane_trans[pipe][PLANE_CURSOR] = 0;
adda50b8
BP
3662
3663 /* Clear ddb entries for pipe */
3664 memset(&watermarks->ddb.pipe[pipe], 0, sizeof(struct skl_ddb_entry));
3665 memset(&watermarks->ddb.plane[pipe], 0,
3666 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
3667 memset(&watermarks->ddb.y_plane[pipe], 0,
3668 sizeof(struct skl_ddb_entry) * I915_MAX_PLANES);
4969d33e
MR
3669 memset(&watermarks->ddb.plane[pipe][PLANE_CURSOR], 0,
3670 sizeof(struct skl_ddb_entry));
adda50b8
BP
3671
3672}
3673
2d41c0b5
PB
3674static void skl_update_wm(struct drm_crtc *crtc)
3675{
3676 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3677 struct drm_device *dev = crtc->dev;
3678 struct drm_i915_private *dev_priv = dev->dev_private;
2d41c0b5 3679 struct skl_wm_values *results = &dev_priv->wm.skl_results;
4e0963c7
MR
3680 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3681 struct skl_pipe_wm *pipe_wm = &cstate->wm.optimal.skl;
2d41c0b5 3682
adda50b8
BP
3683
3684 /* Clear all dirty flags */
3685 memset(results->dirty, 0, sizeof(bool) * I915_MAX_PIPES);
3686
3687 skl_clear_wm(results, intel_crtc->pipe);
2d41c0b5 3688
aa363136 3689 if (!skl_update_pipe_wm(crtc, &results->ddb, pipe_wm))
2d41c0b5
PB
3690 return;
3691
4e0963c7 3692 skl_compute_wm_results(dev, pipe_wm, results, intel_crtc);
2d41c0b5
PB
3693 results->dirty[intel_crtc->pipe] = true;
3694
aa363136 3695 skl_update_other_pipe_wm(dev, crtc, results);
2d41c0b5 3696 skl_write_wm_values(dev_priv, results);
0e8fb7ba 3697 skl_flush_wm_values(dev_priv, results);
53b0deb4
DL
3698
3699 /* store the new configuration */
3700 dev_priv->wm.skl_hw = *results;
2d41c0b5
PB
3701}
3702
d890565c
VS
3703static void ilk_compute_wm_config(struct drm_device *dev,
3704 struct intel_wm_config *config)
3705{
3706 struct intel_crtc *crtc;
3707
3708 /* Compute the currently _active_ config */
3709 for_each_intel_crtc(dev, crtc) {
3710 const struct intel_pipe_wm *wm = &crtc->wm.active.ilk;
3711
3712 if (!wm->pipe_enabled)
3713 continue;
3714
3715 config->sprites_enabled |= wm->sprites_enabled;
3716 config->sprites_scaled |= wm->sprites_scaled;
3717 config->num_pipes_active++;
3718 }
3719}
3720
ed4a6a7c 3721static void ilk_program_watermarks(struct drm_i915_private *dev_priv)
801bcfff 3722{
ed4a6a7c 3723 struct drm_device *dev = dev_priv->dev;
b9d5c839 3724 struct intel_pipe_wm lp_wm_1_2 = {}, lp_wm_5_6 = {}, *best_lp_wm;
820c1980 3725 struct ilk_wm_maximums max;
d890565c 3726 struct intel_wm_config config = {};
820c1980 3727 struct ilk_wm_values results = {};
77c122bc 3728 enum intel_ddb_partitioning partitioning;
261a27d1 3729
d890565c
VS
3730 ilk_compute_wm_config(dev, &config);
3731
3732 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_1_2, &max);
3733 ilk_wm_merge(dev, &config, &max, &lp_wm_1_2);
a485bfb8
VS
3734
3735 /* 5/6 split only in single pipe config on IVB+ */
ec98c8d1 3736 if (INTEL_INFO(dev)->gen >= 7 &&
d890565c
VS
3737 config.num_pipes_active == 1 && config.sprites_enabled) {
3738 ilk_compute_wm_maximums(dev, 1, &config, INTEL_DDB_PART_5_6, &max);
3739 ilk_wm_merge(dev, &config, &max, &lp_wm_5_6);
0362c781 3740
820c1980 3741 best_lp_wm = ilk_find_best_result(dev, &lp_wm_1_2, &lp_wm_5_6);
861f3389 3742 } else {
198a1e9b 3743 best_lp_wm = &lp_wm_1_2;
861f3389
PZ
3744 }
3745
198a1e9b 3746 partitioning = (best_lp_wm == &lp_wm_1_2) ?
77c122bc 3747 INTEL_DDB_PART_1_2 : INTEL_DDB_PART_5_6;
801bcfff 3748
820c1980 3749 ilk_compute_wm_results(dev, best_lp_wm, partitioning, &results);
609cedef 3750
820c1980 3751 ilk_write_wm_values(dev_priv, &results);
1011d8c4
PZ
3752}
3753
ed4a6a7c 3754static void ilk_initial_watermarks(struct intel_crtc_state *cstate)
b9d5c839 3755{
ed4a6a7c
MR
3756 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
3757 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
b9d5c839 3758
ed4a6a7c
MR
3759 mutex_lock(&dev_priv->wm.wm_mutex);
3760 intel_crtc->wm.active.ilk = cstate->wm.intermediate;
3761 ilk_program_watermarks(dev_priv);
3762 mutex_unlock(&dev_priv->wm.wm_mutex);
3763}
bf220452 3764
ed4a6a7c
MR
3765static void ilk_optimize_watermarks(struct intel_crtc_state *cstate)
3766{
3767 struct drm_i915_private *dev_priv = to_i915(cstate->base.crtc->dev);
3768 struct intel_crtc *intel_crtc = to_intel_crtc(cstate->base.crtc);
bf220452 3769
ed4a6a7c
MR
3770 mutex_lock(&dev_priv->wm.wm_mutex);
3771 if (cstate->wm.need_postvbl_update) {
3772 intel_crtc->wm.active.ilk = cstate->wm.optimal.ilk;
3773 ilk_program_watermarks(dev_priv);
3774 }
3775 mutex_unlock(&dev_priv->wm.wm_mutex);
b9d5c839
VS
3776}
3777
3078999f
PB
3778static void skl_pipe_wm_active_state(uint32_t val,
3779 struct skl_pipe_wm *active,
3780 bool is_transwm,
3781 bool is_cursor,
3782 int i,
3783 int level)
3784{
3785 bool is_enabled = (val & PLANE_WM_EN) != 0;
3786
3787 if (!is_transwm) {
3788 if (!is_cursor) {
3789 active->wm[level].plane_en[i] = is_enabled;
3790 active->wm[level].plane_res_b[i] =
3791 val & PLANE_WM_BLOCKS_MASK;
3792 active->wm[level].plane_res_l[i] =
3793 (val >> PLANE_WM_LINES_SHIFT) &
3794 PLANE_WM_LINES_MASK;
3795 } else {
4969d33e
MR
3796 active->wm[level].plane_en[PLANE_CURSOR] = is_enabled;
3797 active->wm[level].plane_res_b[PLANE_CURSOR] =
3078999f 3798 val & PLANE_WM_BLOCKS_MASK;
4969d33e 3799 active->wm[level].plane_res_l[PLANE_CURSOR] =
3078999f
PB
3800 (val >> PLANE_WM_LINES_SHIFT) &
3801 PLANE_WM_LINES_MASK;
3802 }
3803 } else {
3804 if (!is_cursor) {
3805 active->trans_wm.plane_en[i] = is_enabled;
3806 active->trans_wm.plane_res_b[i] =
3807 val & PLANE_WM_BLOCKS_MASK;
3808 active->trans_wm.plane_res_l[i] =
3809 (val >> PLANE_WM_LINES_SHIFT) &
3810 PLANE_WM_LINES_MASK;
3811 } else {
4969d33e
MR
3812 active->trans_wm.plane_en[PLANE_CURSOR] = is_enabled;
3813 active->trans_wm.plane_res_b[PLANE_CURSOR] =
3078999f 3814 val & PLANE_WM_BLOCKS_MASK;
4969d33e 3815 active->trans_wm.plane_res_l[PLANE_CURSOR] =
3078999f
PB
3816 (val >> PLANE_WM_LINES_SHIFT) &
3817 PLANE_WM_LINES_MASK;
3818 }
3819 }
3820}
3821
3822static void skl_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3823{
3824 struct drm_device *dev = crtc->dev;
3825 struct drm_i915_private *dev_priv = dev->dev_private;
3826 struct skl_wm_values *hw = &dev_priv->wm.skl_hw;
3827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4e0963c7
MR
3828 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3829 struct skl_pipe_wm *active = &cstate->wm.optimal.skl;
3078999f
PB
3830 enum pipe pipe = intel_crtc->pipe;
3831 int level, i, max_level;
3832 uint32_t temp;
3833
3834 max_level = ilk_wm_max_level(dev);
3835
3836 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
3837
3838 for (level = 0; level <= max_level; level++) {
3839 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3840 hw->plane[pipe][i][level] =
3841 I915_READ(PLANE_WM(pipe, i, level));
4969d33e 3842 hw->plane[pipe][PLANE_CURSOR][level] = I915_READ(CUR_WM(pipe, level));
3078999f
PB
3843 }
3844
3845 for (i = 0; i < intel_num_planes(intel_crtc); i++)
3846 hw->plane_trans[pipe][i] = I915_READ(PLANE_WM_TRANS(pipe, i));
4969d33e 3847 hw->plane_trans[pipe][PLANE_CURSOR] = I915_READ(CUR_WM_TRANS(pipe));
3078999f 3848
3ef00284 3849 if (!intel_crtc->active)
3078999f
PB
3850 return;
3851
3852 hw->dirty[pipe] = true;
3853
3854 active->linetime = hw->wm_linetime[pipe];
3855
3856 for (level = 0; level <= max_level; level++) {
3857 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3858 temp = hw->plane[pipe][i][level];
3859 skl_pipe_wm_active_state(temp, active, false,
3860 false, i, level);
3861 }
4969d33e 3862 temp = hw->plane[pipe][PLANE_CURSOR][level];
3078999f
PB
3863 skl_pipe_wm_active_state(temp, active, false, true, i, level);
3864 }
3865
3866 for (i = 0; i < intel_num_planes(intel_crtc); i++) {
3867 temp = hw->plane_trans[pipe][i];
3868 skl_pipe_wm_active_state(temp, active, true, false, i, 0);
3869 }
3870
4969d33e 3871 temp = hw->plane_trans[pipe][PLANE_CURSOR];
3078999f 3872 skl_pipe_wm_active_state(temp, active, true, true, i, 0);
4e0963c7
MR
3873
3874 intel_crtc->wm.active.skl = *active;
3078999f
PB
3875}
3876
3877void skl_wm_get_hw_state(struct drm_device *dev)
3878{
a269c583
DL
3879 struct drm_i915_private *dev_priv = dev->dev_private;
3880 struct skl_ddb_allocation *ddb = &dev_priv->wm.skl_hw.ddb;
3078999f
PB
3881 struct drm_crtc *crtc;
3882
a269c583 3883 skl_ddb_get_hw_state(dev_priv, ddb);
3078999f
PB
3884 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
3885 skl_pipe_wm_get_hw_state(crtc);
3886}
3887
243e6a44
VS
3888static void ilk_pipe_wm_get_hw_state(struct drm_crtc *crtc)
3889{
3890 struct drm_device *dev = crtc->dev;
3891 struct drm_i915_private *dev_priv = dev->dev_private;
820c1980 3892 struct ilk_wm_values *hw = &dev_priv->wm.hw;
243e6a44 3893 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4e0963c7
MR
3894 struct intel_crtc_state *cstate = to_intel_crtc_state(crtc->state);
3895 struct intel_pipe_wm *active = &cstate->wm.optimal.ilk;
243e6a44 3896 enum pipe pipe = intel_crtc->pipe;
f0f59a00 3897 static const i915_reg_t wm0_pipe_reg[] = {
243e6a44
VS
3898 [PIPE_A] = WM0_PIPEA_ILK,
3899 [PIPE_B] = WM0_PIPEB_ILK,
3900 [PIPE_C] = WM0_PIPEC_IVB,
3901 };
3902
3903 hw->wm_pipe[pipe] = I915_READ(wm0_pipe_reg[pipe]);
a42a5719 3904 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ce0e0713 3905 hw->wm_linetime[pipe] = I915_READ(PIPE_WM_LINETIME(pipe));
243e6a44 3906
3ef00284 3907 active->pipe_enabled = intel_crtc->active;
2a44b76b
VS
3908
3909 if (active->pipe_enabled) {
243e6a44
VS
3910 u32 tmp = hw->wm_pipe[pipe];
3911
3912 /*
3913 * For active pipes LP0 watermark is marked as
3914 * enabled, and LP1+ watermaks as disabled since
3915 * we can't really reverse compute them in case
3916 * multiple pipes are active.
3917 */
3918 active->wm[0].enable = true;
3919 active->wm[0].pri_val = (tmp & WM0_PIPE_PLANE_MASK) >> WM0_PIPE_PLANE_SHIFT;
3920 active->wm[0].spr_val = (tmp & WM0_PIPE_SPRITE_MASK) >> WM0_PIPE_SPRITE_SHIFT;
3921 active->wm[0].cur_val = tmp & WM0_PIPE_CURSOR_MASK;
3922 active->linetime = hw->wm_linetime[pipe];
3923 } else {
3924 int level, max_level = ilk_wm_max_level(dev);
3925
3926 /*
3927 * For inactive pipes, all watermark levels
3928 * should be marked as enabled but zeroed,
3929 * which is what we'd compute them to.
3930 */
3931 for (level = 0; level <= max_level; level++)
3932 active->wm[level].enable = true;
3933 }
4e0963c7
MR
3934
3935 intel_crtc->wm.active.ilk = *active;
243e6a44
VS
3936}
3937
6eb1a681
VS
3938#define _FW_WM(value, plane) \
3939 (((value) & DSPFW_ ## plane ## _MASK) >> DSPFW_ ## plane ## _SHIFT)
3940#define _FW_WM_VLV(value, plane) \
3941 (((value) & DSPFW_ ## plane ## _MASK_VLV) >> DSPFW_ ## plane ## _SHIFT)
3942
3943static void vlv_read_wm_values(struct drm_i915_private *dev_priv,
3944 struct vlv_wm_values *wm)
3945{
3946 enum pipe pipe;
3947 uint32_t tmp;
3948
3949 for_each_pipe(dev_priv, pipe) {
3950 tmp = I915_READ(VLV_DDL(pipe));
3951
3952 wm->ddl[pipe].primary =
3953 (tmp >> DDL_PLANE_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3954 wm->ddl[pipe].cursor =
3955 (tmp >> DDL_CURSOR_SHIFT) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3956 wm->ddl[pipe].sprite[0] =
3957 (tmp >> DDL_SPRITE_SHIFT(0)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3958 wm->ddl[pipe].sprite[1] =
3959 (tmp >> DDL_SPRITE_SHIFT(1)) & (DDL_PRECISION_HIGH | DRAIN_LATENCY_MASK);
3960 }
3961
3962 tmp = I915_READ(DSPFW1);
3963 wm->sr.plane = _FW_WM(tmp, SR);
3964 wm->pipe[PIPE_B].cursor = _FW_WM(tmp, CURSORB);
3965 wm->pipe[PIPE_B].primary = _FW_WM_VLV(tmp, PLANEB);
3966 wm->pipe[PIPE_A].primary = _FW_WM_VLV(tmp, PLANEA);
3967
3968 tmp = I915_READ(DSPFW2);
3969 wm->pipe[PIPE_A].sprite[1] = _FW_WM_VLV(tmp, SPRITEB);
3970 wm->pipe[PIPE_A].cursor = _FW_WM(tmp, CURSORA);
3971 wm->pipe[PIPE_A].sprite[0] = _FW_WM_VLV(tmp, SPRITEA);
3972
3973 tmp = I915_READ(DSPFW3);
3974 wm->sr.cursor = _FW_WM(tmp, CURSOR_SR);
3975
3976 if (IS_CHERRYVIEW(dev_priv)) {
3977 tmp = I915_READ(DSPFW7_CHV);
3978 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
3979 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
3980
3981 tmp = I915_READ(DSPFW8_CHV);
3982 wm->pipe[PIPE_C].sprite[1] = _FW_WM_VLV(tmp, SPRITEF);
3983 wm->pipe[PIPE_C].sprite[0] = _FW_WM_VLV(tmp, SPRITEE);
3984
3985 tmp = I915_READ(DSPFW9_CHV);
3986 wm->pipe[PIPE_C].primary = _FW_WM_VLV(tmp, PLANEC);
3987 wm->pipe[PIPE_C].cursor = _FW_WM(tmp, CURSORC);
3988
3989 tmp = I915_READ(DSPHOWM);
3990 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
3991 wm->pipe[PIPE_C].sprite[1] |= _FW_WM(tmp, SPRITEF_HI) << 8;
3992 wm->pipe[PIPE_C].sprite[0] |= _FW_WM(tmp, SPRITEE_HI) << 8;
3993 wm->pipe[PIPE_C].primary |= _FW_WM(tmp, PLANEC_HI) << 8;
3994 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
3995 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
3996 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
3997 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
3998 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
3999 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4000 } else {
4001 tmp = I915_READ(DSPFW7);
4002 wm->pipe[PIPE_B].sprite[1] = _FW_WM_VLV(tmp, SPRITED);
4003 wm->pipe[PIPE_B].sprite[0] = _FW_WM_VLV(tmp, SPRITEC);
4004
4005 tmp = I915_READ(DSPHOWM);
4006 wm->sr.plane |= _FW_WM(tmp, SR_HI) << 9;
4007 wm->pipe[PIPE_B].sprite[1] |= _FW_WM(tmp, SPRITED_HI) << 8;
4008 wm->pipe[PIPE_B].sprite[0] |= _FW_WM(tmp, SPRITEC_HI) << 8;
4009 wm->pipe[PIPE_B].primary |= _FW_WM(tmp, PLANEB_HI) << 8;
4010 wm->pipe[PIPE_A].sprite[1] |= _FW_WM(tmp, SPRITEB_HI) << 8;
4011 wm->pipe[PIPE_A].sprite[0] |= _FW_WM(tmp, SPRITEA_HI) << 8;
4012 wm->pipe[PIPE_A].primary |= _FW_WM(tmp, PLANEA_HI) << 8;
4013 }
4014}
4015
4016#undef _FW_WM
4017#undef _FW_WM_VLV
4018
4019void vlv_wm_get_hw_state(struct drm_device *dev)
4020{
4021 struct drm_i915_private *dev_priv = to_i915(dev);
4022 struct vlv_wm_values *wm = &dev_priv->wm.vlv;
4023 struct intel_plane *plane;
4024 enum pipe pipe;
4025 u32 val;
4026
4027 vlv_read_wm_values(dev_priv, wm);
4028
4029 for_each_intel_plane(dev, plane) {
4030 switch (plane->base.type) {
4031 int sprite;
4032 case DRM_PLANE_TYPE_CURSOR:
4033 plane->wm.fifo_size = 63;
4034 break;
4035 case DRM_PLANE_TYPE_PRIMARY:
4036 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, 0);
4037 break;
4038 case DRM_PLANE_TYPE_OVERLAY:
4039 sprite = plane->plane;
4040 plane->wm.fifo_size = vlv_get_fifo_size(dev, plane->pipe, sprite + 1);
4041 break;
4042 }
4043 }
4044
4045 wm->cxsr = I915_READ(FW_BLC_SELF_VLV) & FW_CSPWRDWNEN;
4046 wm->level = VLV_WM_LEVEL_PM2;
4047
4048 if (IS_CHERRYVIEW(dev_priv)) {
4049 mutex_lock(&dev_priv->rps.hw_lock);
4050
4051 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4052 if (val & DSP_MAXFIFO_PM5_ENABLE)
4053 wm->level = VLV_WM_LEVEL_PM5;
4054
58590c14
VS
4055 /*
4056 * If DDR DVFS is disabled in the BIOS, Punit
4057 * will never ack the request. So if that happens
4058 * assume we don't have to enable/disable DDR DVFS
4059 * dynamically. To test that just set the REQ_ACK
4060 * bit to poke the Punit, but don't change the
4061 * HIGH/LOW bits so that we don't actually change
4062 * the current state.
4063 */
6eb1a681 4064 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
58590c14
VS
4065 val |= FORCE_DDR_FREQ_REQ_ACK;
4066 vlv_punit_write(dev_priv, PUNIT_REG_DDR_SETUP2, val);
4067
4068 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2) &
4069 FORCE_DDR_FREQ_REQ_ACK) == 0, 3)) {
4070 DRM_DEBUG_KMS("Punit not acking DDR DVFS request, "
4071 "assuming DDR DVFS is disabled\n");
4072 dev_priv->wm.max_level = VLV_WM_LEVEL_PM5;
4073 } else {
4074 val = vlv_punit_read(dev_priv, PUNIT_REG_DDR_SETUP2);
4075 if ((val & FORCE_DDR_HIGH_FREQ) == 0)
4076 wm->level = VLV_WM_LEVEL_DDR_DVFS;
4077 }
6eb1a681
VS
4078
4079 mutex_unlock(&dev_priv->rps.hw_lock);
4080 }
4081
4082 for_each_pipe(dev_priv, pipe)
4083 DRM_DEBUG_KMS("Initial watermarks: pipe %c, plane=%d, cursor=%d, sprite0=%d, sprite1=%d\n",
4084 pipe_name(pipe), wm->pipe[pipe].primary, wm->pipe[pipe].cursor,
4085 wm->pipe[pipe].sprite[0], wm->pipe[pipe].sprite[1]);
4086
4087 DRM_DEBUG_KMS("Initial watermarks: SR plane=%d, SR cursor=%d level=%d cxsr=%d\n",
4088 wm->sr.plane, wm->sr.cursor, wm->level, wm->cxsr);
4089}
4090
243e6a44
VS
4091void ilk_wm_get_hw_state(struct drm_device *dev)
4092{
4093 struct drm_i915_private *dev_priv = dev->dev_private;
820c1980 4094 struct ilk_wm_values *hw = &dev_priv->wm.hw;
243e6a44
VS
4095 struct drm_crtc *crtc;
4096
70e1e0ec 4097 for_each_crtc(dev, crtc)
243e6a44
VS
4098 ilk_pipe_wm_get_hw_state(crtc);
4099
4100 hw->wm_lp[0] = I915_READ(WM1_LP_ILK);
4101 hw->wm_lp[1] = I915_READ(WM2_LP_ILK);
4102 hw->wm_lp[2] = I915_READ(WM3_LP_ILK);
4103
4104 hw->wm_lp_spr[0] = I915_READ(WM1S_LP_ILK);
cfa7698b
VS
4105 if (INTEL_INFO(dev)->gen >= 7) {
4106 hw->wm_lp_spr[1] = I915_READ(WM2S_LP_IVB);
4107 hw->wm_lp_spr[2] = I915_READ(WM3S_LP_IVB);
4108 }
243e6a44 4109
a42a5719 4110 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
ac9545fd
VS
4111 hw->partitioning = (I915_READ(WM_MISC) & WM_MISC_DATA_PARTITION_5_6) ?
4112 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
4113 else if (IS_IVYBRIDGE(dev))
4114 hw->partitioning = (I915_READ(DISP_ARB_CTL2) & DISP_DATA_PARTITION_5_6) ?
4115 INTEL_DDB_PART_5_6 : INTEL_DDB_PART_1_2;
243e6a44
VS
4116
4117 hw->enable_fbc_wm =
4118 !(I915_READ(DISP_ARB_CTL) & DISP_FBC_WM_DIS);
4119}
4120
b445e3b0
ED
4121/**
4122 * intel_update_watermarks - update FIFO watermark values based on current modes
4123 *
4124 * Calculate watermark values for the various WM regs based on current mode
4125 * and plane configuration.
4126 *
4127 * There are several cases to deal with here:
4128 * - normal (i.e. non-self-refresh)
4129 * - self-refresh (SR) mode
4130 * - lines are large relative to FIFO size (buffer can hold up to 2)
4131 * - lines are small relative to FIFO size (buffer can hold more than 2
4132 * lines), so need to account for TLB latency
4133 *
4134 * The normal calculation is:
4135 * watermark = dotclock * bytes per pixel * latency
4136 * where latency is platform & configuration dependent (we assume pessimal
4137 * values here).
4138 *
4139 * The SR calculation is:
4140 * watermark = (trunc(latency/line time)+1) * surface width *
4141 * bytes per pixel
4142 * where
4143 * line time = htotal / dotclock
4144 * surface width = hdisplay for normal plane and 64 for cursor
4145 * and latency is assumed to be high, as above.
4146 *
4147 * The final value programmed to the register should always be rounded up,
4148 * and include an extra 2 entries to account for clock crossings.
4149 *
4150 * We don't use the sprite, so we can ignore that. And on Crestline we have
4151 * to set the non-SR watermarks to 8.
4152 */
46ba614c 4153void intel_update_watermarks(struct drm_crtc *crtc)
b445e3b0 4154{
46ba614c 4155 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
b445e3b0
ED
4156
4157 if (dev_priv->display.update_wm)
46ba614c 4158 dev_priv->display.update_wm(crtc);
b445e3b0
ED
4159}
4160
e2828914 4161/*
9270388e 4162 * Lock protecting IPS related data structures
9270388e
DV
4163 */
4164DEFINE_SPINLOCK(mchdev_lock);
4165
4166/* Global for IPS driver to get at the current i915 device. Protected by
4167 * mchdev_lock. */
4168static struct drm_i915_private *i915_mch_dev;
4169
91d14251 4170bool ironlake_set_drps(struct drm_i915_private *dev_priv, u8 val)
2b4e57bd 4171{
2b4e57bd
ED
4172 u16 rgvswctl;
4173
9270388e
DV
4174 assert_spin_locked(&mchdev_lock);
4175
2b4e57bd
ED
4176 rgvswctl = I915_READ16(MEMSWCTL);
4177 if (rgvswctl & MEMCTL_CMD_STS) {
4178 DRM_DEBUG("gpu busy, RCS change rejected\n");
4179 return false; /* still busy with another command */
4180 }
4181
4182 rgvswctl = (MEMCTL_CMD_CHFREQ << MEMCTL_CMD_SHIFT) |
4183 (val << MEMCTL_FREQ_SHIFT) | MEMCTL_SFCAVM;
4184 I915_WRITE16(MEMSWCTL, rgvswctl);
4185 POSTING_READ16(MEMSWCTL);
4186
4187 rgvswctl |= MEMCTL_CMD_STS;
4188 I915_WRITE16(MEMSWCTL, rgvswctl);
4189
4190 return true;
4191}
4192
91d14251 4193static void ironlake_enable_drps(struct drm_i915_private *dev_priv)
2b4e57bd 4194{
84f1b20f 4195 u32 rgvmodectl;
2b4e57bd
ED
4196 u8 fmax, fmin, fstart, vstart;
4197
9270388e
DV
4198 spin_lock_irq(&mchdev_lock);
4199
84f1b20f
TU
4200 rgvmodectl = I915_READ(MEMMODECTL);
4201
2b4e57bd
ED
4202 /* Enable temp reporting */
4203 I915_WRITE16(PMMISC, I915_READ(PMMISC) | MCPPCE_EN);
4204 I915_WRITE16(TSC1, I915_READ(TSC1) | TSE);
4205
4206 /* 100ms RC evaluation intervals */
4207 I915_WRITE(RCUPEI, 100000);
4208 I915_WRITE(RCDNEI, 100000);
4209
4210 /* Set max/min thresholds to 90ms and 80ms respectively */
4211 I915_WRITE(RCBMAXAVG, 90000);
4212 I915_WRITE(RCBMINAVG, 80000);
4213
4214 I915_WRITE(MEMIHYST, 1);
4215
4216 /* Set up min, max, and cur for interrupt handling */
4217 fmax = (rgvmodectl & MEMMODE_FMAX_MASK) >> MEMMODE_FMAX_SHIFT;
4218 fmin = (rgvmodectl & MEMMODE_FMIN_MASK);
4219 fstart = (rgvmodectl & MEMMODE_FSTART_MASK) >>
4220 MEMMODE_FSTART_SHIFT;
4221
616847e7 4222 vstart = (I915_READ(PXVFREQ(fstart)) & PXVFREQ_PX_MASK) >>
2b4e57bd
ED
4223 PXVFREQ_PX_SHIFT;
4224
20e4d407
DV
4225 dev_priv->ips.fmax = fmax; /* IPS callback will increase this */
4226 dev_priv->ips.fstart = fstart;
2b4e57bd 4227
20e4d407
DV
4228 dev_priv->ips.max_delay = fstart;
4229 dev_priv->ips.min_delay = fmin;
4230 dev_priv->ips.cur_delay = fstart;
2b4e57bd
ED
4231
4232 DRM_DEBUG_DRIVER("fmax: %d, fmin: %d, fstart: %d\n",
4233 fmax, fmin, fstart);
4234
4235 I915_WRITE(MEMINTREN, MEMINT_CX_SUPR_EN | MEMINT_EVAL_CHG_EN);
4236
4237 /*
4238 * Interrupts will be enabled in ironlake_irq_postinstall
4239 */
4240
4241 I915_WRITE(VIDSTART, vstart);
4242 POSTING_READ(VIDSTART);
4243
4244 rgvmodectl |= MEMMODE_SWMODE_EN;
4245 I915_WRITE(MEMMODECTL, rgvmodectl);
4246
9270388e 4247 if (wait_for_atomic((I915_READ(MEMSWCTL) & MEMCTL_CMD_STS) == 0, 10))
2b4e57bd 4248 DRM_ERROR("stuck trying to change perf mode\n");
dd92d8de 4249 mdelay(1);
2b4e57bd 4250
91d14251 4251 ironlake_set_drps(dev_priv, fstart);
2b4e57bd 4252
7d81c3e0
VS
4253 dev_priv->ips.last_count1 = I915_READ(DMIEC) +
4254 I915_READ(DDREC) + I915_READ(CSIEC);
20e4d407 4255 dev_priv->ips.last_time1 = jiffies_to_msecs(jiffies);
7d81c3e0 4256 dev_priv->ips.last_count2 = I915_READ(GFXEC);
5ed0bdf2 4257 dev_priv->ips.last_time2 = ktime_get_raw_ns();
9270388e
DV
4258
4259 spin_unlock_irq(&mchdev_lock);
2b4e57bd
ED
4260}
4261
91d14251 4262static void ironlake_disable_drps(struct drm_i915_private *dev_priv)
2b4e57bd 4263{
9270388e
DV
4264 u16 rgvswctl;
4265
4266 spin_lock_irq(&mchdev_lock);
4267
4268 rgvswctl = I915_READ16(MEMSWCTL);
2b4e57bd
ED
4269
4270 /* Ack interrupts, disable EFC interrupt */
4271 I915_WRITE(MEMINTREN, I915_READ(MEMINTREN) & ~MEMINT_EVAL_CHG_EN);
4272 I915_WRITE(MEMINTRSTS, MEMINT_EVAL_CHG);
4273 I915_WRITE(DEIER, I915_READ(DEIER) & ~DE_PCU_EVENT);
4274 I915_WRITE(DEIIR, DE_PCU_EVENT);
4275 I915_WRITE(DEIMR, I915_READ(DEIMR) | DE_PCU_EVENT);
4276
4277 /* Go back to the starting frequency */
91d14251 4278 ironlake_set_drps(dev_priv, dev_priv->ips.fstart);
dd92d8de 4279 mdelay(1);
2b4e57bd
ED
4280 rgvswctl |= MEMCTL_CMD_STS;
4281 I915_WRITE(MEMSWCTL, rgvswctl);
dd92d8de 4282 mdelay(1);
2b4e57bd 4283
9270388e 4284 spin_unlock_irq(&mchdev_lock);
2b4e57bd
ED
4285}
4286
acbe9475
DV
4287/* There's a funny hw issue where the hw returns all 0 when reading from
4288 * GEN6_RP_INTERRUPT_LIMITS. Hence we always need to compute the desired value
4289 * ourselves, instead of doing a rmw cycle (which might result in us clearing
4290 * all limits and the gpu stuck at whatever frequency it is at atm).
4291 */
74ef1173 4292static u32 intel_rps_limits(struct drm_i915_private *dev_priv, u8 val)
2b4e57bd 4293{
7b9e0ae6 4294 u32 limits;
2b4e57bd 4295
20b46e59
DV
4296 /* Only set the down limit when we've reached the lowest level to avoid
4297 * getting more interrupts, otherwise leave this clear. This prevents a
4298 * race in the hw when coming out of rc6: There's a tiny window where
4299 * the hw runs at the minimal clock before selecting the desired
4300 * frequency, if the down threshold expires in that window we will not
4301 * receive a down interrupt. */
2d1fe073 4302 if (IS_GEN9(dev_priv)) {
74ef1173
AG
4303 limits = (dev_priv->rps.max_freq_softlimit) << 23;
4304 if (val <= dev_priv->rps.min_freq_softlimit)
4305 limits |= (dev_priv->rps.min_freq_softlimit) << 14;
4306 } else {
4307 limits = dev_priv->rps.max_freq_softlimit << 24;
4308 if (val <= dev_priv->rps.min_freq_softlimit)
4309 limits |= dev_priv->rps.min_freq_softlimit << 16;
4310 }
20b46e59
DV
4311
4312 return limits;
4313}
4314
dd75fdc8
CW
4315static void gen6_set_rps_thresholds(struct drm_i915_private *dev_priv, u8 val)
4316{
4317 int new_power;
8a586437
AG
4318 u32 threshold_up = 0, threshold_down = 0; /* in % */
4319 u32 ei_up = 0, ei_down = 0;
dd75fdc8
CW
4320
4321 new_power = dev_priv->rps.power;
4322 switch (dev_priv->rps.power) {
4323 case LOW_POWER:
b39fb297 4324 if (val > dev_priv->rps.efficient_freq + 1 && val > dev_priv->rps.cur_freq)
dd75fdc8
CW
4325 new_power = BETWEEN;
4326 break;
4327
4328 case BETWEEN:
b39fb297 4329 if (val <= dev_priv->rps.efficient_freq && val < dev_priv->rps.cur_freq)
dd75fdc8 4330 new_power = LOW_POWER;
b39fb297 4331 else if (val >= dev_priv->rps.rp0_freq && val > dev_priv->rps.cur_freq)
dd75fdc8
CW
4332 new_power = HIGH_POWER;
4333 break;
4334
4335 case HIGH_POWER:
b39fb297 4336 if (val < (dev_priv->rps.rp1_freq + dev_priv->rps.rp0_freq) >> 1 && val < dev_priv->rps.cur_freq)
dd75fdc8
CW
4337 new_power = BETWEEN;
4338 break;
4339 }
4340 /* Max/min bins are special */
aed242ff 4341 if (val <= dev_priv->rps.min_freq_softlimit)
dd75fdc8 4342 new_power = LOW_POWER;
aed242ff 4343 if (val >= dev_priv->rps.max_freq_softlimit)
dd75fdc8
CW
4344 new_power = HIGH_POWER;
4345 if (new_power == dev_priv->rps.power)
4346 return;
4347
4348 /* Note the units here are not exactly 1us, but 1280ns. */
4349 switch (new_power) {
4350 case LOW_POWER:
4351 /* Upclock if more than 95% busy over 16ms */
8a586437
AG
4352 ei_up = 16000;
4353 threshold_up = 95;
dd75fdc8
CW
4354
4355 /* Downclock if less than 85% busy over 32ms */
8a586437
AG
4356 ei_down = 32000;
4357 threshold_down = 85;
dd75fdc8
CW
4358 break;
4359
4360 case BETWEEN:
4361 /* Upclock if more than 90% busy over 13ms */
8a586437
AG
4362 ei_up = 13000;
4363 threshold_up = 90;
dd75fdc8
CW
4364
4365 /* Downclock if less than 75% busy over 32ms */
8a586437
AG
4366 ei_down = 32000;
4367 threshold_down = 75;
dd75fdc8
CW
4368 break;
4369
4370 case HIGH_POWER:
4371 /* Upclock if more than 85% busy over 10ms */
8a586437
AG
4372 ei_up = 10000;
4373 threshold_up = 85;
dd75fdc8
CW
4374
4375 /* Downclock if less than 60% busy over 32ms */
8a586437
AG
4376 ei_down = 32000;
4377 threshold_down = 60;
dd75fdc8
CW
4378 break;
4379 }
4380
8a586437
AG
4381 I915_WRITE(GEN6_RP_UP_EI,
4382 GT_INTERVAL_FROM_US(dev_priv, ei_up));
4383 I915_WRITE(GEN6_RP_UP_THRESHOLD,
4384 GT_INTERVAL_FROM_US(dev_priv, (ei_up * threshold_up / 100)));
4385
4386 I915_WRITE(GEN6_RP_DOWN_EI,
4387 GT_INTERVAL_FROM_US(dev_priv, ei_down));
4388 I915_WRITE(GEN6_RP_DOWN_THRESHOLD,
4389 GT_INTERVAL_FROM_US(dev_priv, (ei_down * threshold_down / 100)));
4390
4391 I915_WRITE(GEN6_RP_CONTROL,
4392 GEN6_RP_MEDIA_TURBO |
4393 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4394 GEN6_RP_MEDIA_IS_GFX |
4395 GEN6_RP_ENABLE |
4396 GEN6_RP_UP_BUSY_AVG |
4397 GEN6_RP_DOWN_IDLE_AVG);
4398
dd75fdc8 4399 dev_priv->rps.power = new_power;
8fb55197
CW
4400 dev_priv->rps.up_threshold = threshold_up;
4401 dev_priv->rps.down_threshold = threshold_down;
dd75fdc8
CW
4402 dev_priv->rps.last_adj = 0;
4403}
4404
2876ce73
CW
4405static u32 gen6_rps_pm_mask(struct drm_i915_private *dev_priv, u8 val)
4406{
4407 u32 mask = 0;
4408
4409 if (val > dev_priv->rps.min_freq_softlimit)
6f4b12f8 4410 mask |= GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_DOWN_THRESHOLD | GEN6_PM_RP_DOWN_TIMEOUT;
2876ce73 4411 if (val < dev_priv->rps.max_freq_softlimit)
6f4b12f8 4412 mask |= GEN6_PM_RP_UP_EI_EXPIRED | GEN6_PM_RP_UP_THRESHOLD;
2876ce73 4413
7b3c29f6
CW
4414 mask &= dev_priv->pm_rps_events;
4415
59d02a1f 4416 return gen6_sanitize_rps_pm_mask(dev_priv, ~mask);
2876ce73
CW
4417}
4418
b8a5ff8d
JM
4419/* gen6_set_rps is called to update the frequency request, but should also be
4420 * called when the range (min_delay and max_delay) is modified so that we can
4421 * update the GEN6_RP_INTERRUPT_LIMITS register accordingly. */
ffe02b40 4422static void gen6_set_rps(struct drm_device *dev, u8 val)
20b46e59
DV
4423{
4424 struct drm_i915_private *dev_priv = dev->dev_private;
7b9e0ae6 4425
23eafea6 4426 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
e87a005d 4427 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1))
23eafea6
SAK
4428 return;
4429
4fc688ce 4430 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
aed242ff
CW
4431 WARN_ON(val > dev_priv->rps.max_freq);
4432 WARN_ON(val < dev_priv->rps.min_freq);
004777cb 4433
eb64cad1
CW
4434 /* min/max delay may still have been modified so be sure to
4435 * write the limits value.
4436 */
4437 if (val != dev_priv->rps.cur_freq) {
4438 gen6_set_rps_thresholds(dev_priv, val);
b8a5ff8d 4439
5704195c
AG
4440 if (IS_GEN9(dev))
4441 I915_WRITE(GEN6_RPNSWREQ,
4442 GEN9_FREQUENCY(val));
4443 else if (IS_HASWELL(dev) || IS_BROADWELL(dev))
eb64cad1
CW
4444 I915_WRITE(GEN6_RPNSWREQ,
4445 HSW_FREQUENCY(val));
4446 else
4447 I915_WRITE(GEN6_RPNSWREQ,
4448 GEN6_FREQUENCY(val) |
4449 GEN6_OFFSET(0) |
4450 GEN6_AGGRESSIVE_TURBO);
b8a5ff8d 4451 }
7b9e0ae6 4452
7b9e0ae6
CW
4453 /* Make sure we continue to get interrupts
4454 * until we hit the minimum or maximum frequencies.
4455 */
74ef1173 4456 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS, intel_rps_limits(dev_priv, val));
2876ce73 4457 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
7b9e0ae6 4458
d5570a72
BW
4459 POSTING_READ(GEN6_RPNSWREQ);
4460
b39fb297 4461 dev_priv->rps.cur_freq = val;
0f94592e 4462 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
2b4e57bd
ED
4463}
4464
ffe02b40
VS
4465static void valleyview_set_rps(struct drm_device *dev, u8 val)
4466{
4467 struct drm_i915_private *dev_priv = dev->dev_private;
4468
4469 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
aed242ff
CW
4470 WARN_ON(val > dev_priv->rps.max_freq);
4471 WARN_ON(val < dev_priv->rps.min_freq);
ffe02b40
VS
4472
4473 if (WARN_ONCE(IS_CHERRYVIEW(dev) && (val & 1),
4474 "Odd GPU freq value\n"))
4475 val &= ~1;
4476
cd25dd5b
D
4477 I915_WRITE(GEN6_PMINTRMSK, gen6_rps_pm_mask(dev_priv, val));
4478
8fb55197 4479 if (val != dev_priv->rps.cur_freq) {
ffe02b40 4480 vlv_punit_write(dev_priv, PUNIT_REG_GPU_FREQ_REQ, val);
8fb55197
CW
4481 if (!IS_CHERRYVIEW(dev_priv))
4482 gen6_set_rps_thresholds(dev_priv, val);
4483 }
ffe02b40 4484
ffe02b40
VS
4485 dev_priv->rps.cur_freq = val;
4486 trace_intel_gpu_freq_change(intel_gpu_freq(dev_priv, val));
4487}
4488
a7f6e231 4489/* vlv_set_rps_idle: Set the frequency to idle, if Gfx clocks are down
76c3552f
D
4490 *
4491 * * If Gfx is Idle, then
a7f6e231
D
4492 * 1. Forcewake Media well.
4493 * 2. Request idle freq.
4494 * 3. Release Forcewake of Media well.
76c3552f
D
4495*/
4496static void vlv_set_rps_idle(struct drm_i915_private *dev_priv)
4497{
aed242ff 4498 u32 val = dev_priv->rps.idle_freq;
5549d25f 4499
aed242ff 4500 if (dev_priv->rps.cur_freq <= val)
76c3552f
D
4501 return;
4502
a7f6e231
D
4503 /* Wake up the media well, as that takes a lot less
4504 * power than the Render well. */
4505 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_MEDIA);
4506 valleyview_set_rps(dev_priv->dev, val);
4507 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_MEDIA);
76c3552f
D
4508}
4509
43cf3bf0
CW
4510void gen6_rps_busy(struct drm_i915_private *dev_priv)
4511{
4512 mutex_lock(&dev_priv->rps.hw_lock);
4513 if (dev_priv->rps.enabled) {
4514 if (dev_priv->pm_rps_events & (GEN6_PM_RP_DOWN_EI_EXPIRED | GEN6_PM_RP_UP_EI_EXPIRED))
4515 gen6_rps_reset_ei(dev_priv);
4516 I915_WRITE(GEN6_PMINTRMSK,
4517 gen6_rps_pm_mask(dev_priv, dev_priv->rps.cur_freq));
4518 }
4519 mutex_unlock(&dev_priv->rps.hw_lock);
4520}
4521
b29c19b6
CW
4522void gen6_rps_idle(struct drm_i915_private *dev_priv)
4523{
691bb717
DL
4524 struct drm_device *dev = dev_priv->dev;
4525
b29c19b6 4526 mutex_lock(&dev_priv->rps.hw_lock);
c0951f0c 4527 if (dev_priv->rps.enabled) {
666a4537 4528 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
76c3552f 4529 vlv_set_rps_idle(dev_priv);
7526ed79 4530 else
aed242ff 4531 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
c0951f0c 4532 dev_priv->rps.last_adj = 0;
43cf3bf0 4533 I915_WRITE(GEN6_PMINTRMSK, 0xffffffff);
c0951f0c 4534 }
8d3afd7d 4535 mutex_unlock(&dev_priv->rps.hw_lock);
1854d5ca 4536
8d3afd7d 4537 spin_lock(&dev_priv->rps.client_lock);
1854d5ca
CW
4538 while (!list_empty(&dev_priv->rps.clients))
4539 list_del_init(dev_priv->rps.clients.next);
8d3afd7d 4540 spin_unlock(&dev_priv->rps.client_lock);
b29c19b6
CW
4541}
4542
1854d5ca 4543void gen6_rps_boost(struct drm_i915_private *dev_priv,
e61b9958
CW
4544 struct intel_rps_client *rps,
4545 unsigned long submitted)
b29c19b6 4546{
8d3afd7d
CW
4547 /* This is intentionally racy! We peek at the state here, then
4548 * validate inside the RPS worker.
4549 */
4550 if (!(dev_priv->mm.busy &&
4551 dev_priv->rps.enabled &&
4552 dev_priv->rps.cur_freq < dev_priv->rps.max_freq_softlimit))
4553 return;
43cf3bf0 4554
e61b9958
CW
4555 /* Force a RPS boost (and don't count it against the client) if
4556 * the GPU is severely congested.
4557 */
d0bc54f2 4558 if (rps && time_after(jiffies, submitted + DRM_I915_THROTTLE_JIFFIES))
e61b9958
CW
4559 rps = NULL;
4560
8d3afd7d
CW
4561 spin_lock(&dev_priv->rps.client_lock);
4562 if (rps == NULL || list_empty(&rps->link)) {
4563 spin_lock_irq(&dev_priv->irq_lock);
4564 if (dev_priv->rps.interrupts_enabled) {
4565 dev_priv->rps.client_boost = true;
4566 queue_work(dev_priv->wq, &dev_priv->rps.work);
4567 }
4568 spin_unlock_irq(&dev_priv->irq_lock);
1854d5ca 4569
2e1b8730
CW
4570 if (rps != NULL) {
4571 list_add(&rps->link, &dev_priv->rps.clients);
4572 rps->boosts++;
1854d5ca
CW
4573 } else
4574 dev_priv->rps.boosts++;
c0951f0c 4575 }
8d3afd7d 4576 spin_unlock(&dev_priv->rps.client_lock);
b29c19b6
CW
4577}
4578
ffe02b40 4579void intel_set_rps(struct drm_device *dev, u8 val)
0a073b84 4580{
666a4537 4581 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev))
ffe02b40
VS
4582 valleyview_set_rps(dev, val);
4583 else
4584 gen6_set_rps(dev, val);
0a073b84
JB
4585}
4586
2030d684 4587static void gen9_disable_rc6(struct drm_device *dev)
20e49366
ZW
4588{
4589 struct drm_i915_private *dev_priv = dev->dev_private;
4590
4591 I915_WRITE(GEN6_RC_CONTROL, 0);
38c23527 4592 I915_WRITE(GEN9_PG_ENABLE, 0);
20e49366
ZW
4593}
4594
2030d684
AG
4595static void gen9_disable_rps(struct drm_device *dev)
4596{
4597 struct drm_i915_private *dev_priv = dev->dev_private;
4598
4599 I915_WRITE(GEN6_RP_CONTROL, 0);
4600}
4601
44fc7d5c 4602static void gen6_disable_rps(struct drm_device *dev)
d20d4f0c
JB
4603{
4604 struct drm_i915_private *dev_priv = dev->dev_private;
4605
4606 I915_WRITE(GEN6_RC_CONTROL, 0);
44fc7d5c 4607 I915_WRITE(GEN6_RPNSWREQ, 1 << 31);
2030d684 4608 I915_WRITE(GEN6_RP_CONTROL, 0);
44fc7d5c
DV
4609}
4610
38807746
D
4611static void cherryview_disable_rps(struct drm_device *dev)
4612{
4613 struct drm_i915_private *dev_priv = dev->dev_private;
4614
4615 I915_WRITE(GEN6_RC_CONTROL, 0);
4616}
4617
44fc7d5c
DV
4618static void valleyview_disable_rps(struct drm_device *dev)
4619{
4620 struct drm_i915_private *dev_priv = dev->dev_private;
4621
98a2e5f9
D
4622 /* we're doing forcewake before Disabling RC6,
4623 * This what the BIOS expects when going into suspend */
59bad947 4624 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
98a2e5f9 4625
44fc7d5c 4626 I915_WRITE(GEN6_RC_CONTROL, 0);
d20d4f0c 4627
59bad947 4628 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
d20d4f0c
JB
4629}
4630
dc39fff7
BW
4631static void intel_print_rc6_info(struct drm_device *dev, u32 mode)
4632{
666a4537 4633 if (IS_VALLEYVIEW(dev) || IS_CHERRYVIEW(dev)) {
91ca689a
ID
4634 if (mode & (GEN7_RC_CTL_TO_MODE | GEN6_RC_CTL_EI_MODE(1)))
4635 mode = GEN6_RC_CTL_RC6_ENABLE;
4636 else
4637 mode = 0;
4638 }
58abf1da
RV
4639 if (HAS_RC6p(dev))
4640 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s RC6p %s RC6pp %s\n",
87ad3212
JN
4641 onoff(mode & GEN6_RC_CTL_RC6_ENABLE),
4642 onoff(mode & GEN6_RC_CTL_RC6p_ENABLE),
4643 onoff(mode & GEN6_RC_CTL_RC6pp_ENABLE));
58abf1da
RV
4644
4645 else
4646 DRM_DEBUG_KMS("Enabling RC6 states: RC6 %s\n",
87ad3212 4647 onoff(mode & GEN6_RC_CTL_RC6_ENABLE));
dc39fff7
BW
4648}
4649
274008e8
SAK
4650static bool bxt_check_bios_rc6_setup(const struct drm_device *dev)
4651{
72e96d64
JL
4652 struct drm_i915_private *dev_priv = to_i915(dev);
4653 struct i915_ggtt *ggtt = &dev_priv->ggtt;
274008e8
SAK
4654 bool enable_rc6 = true;
4655 unsigned long rc6_ctx_base;
4656
4657 if (!(I915_READ(RC6_LOCATION) & RC6_CTX_IN_DRAM)) {
4658 DRM_DEBUG_KMS("RC6 Base location not set properly.\n");
4659 enable_rc6 = false;
4660 }
4661
4662 /*
4663 * The exact context size is not known for BXT, so assume a page size
4664 * for this check.
4665 */
4666 rc6_ctx_base = I915_READ(RC6_CTX_BASE) & RC6_CTX_BASE_MASK;
72e96d64
JL
4667 if (!((rc6_ctx_base >= ggtt->stolen_reserved_base) &&
4668 (rc6_ctx_base + PAGE_SIZE <= ggtt->stolen_reserved_base +
4669 ggtt->stolen_reserved_size))) {
274008e8
SAK
4670 DRM_DEBUG_KMS("RC6 Base address not as expected.\n");
4671 enable_rc6 = false;
4672 }
4673
4674 if (!(((I915_READ(PWRCTX_MAXCNT_RCSUNIT) & IDLE_TIME_MASK) > 1) &&
4675 ((I915_READ(PWRCTX_MAXCNT_VCSUNIT0) & IDLE_TIME_MASK) > 1) &&
4676 ((I915_READ(PWRCTX_MAXCNT_BCSUNIT) & IDLE_TIME_MASK) > 1) &&
4677 ((I915_READ(PWRCTX_MAXCNT_VECSUNIT) & IDLE_TIME_MASK) > 1))) {
4678 DRM_DEBUG_KMS("Engine Idle wait time not set properly.\n");
4679 enable_rc6 = false;
4680 }
4681
4682 if (!(I915_READ(GEN6_RC_CONTROL) & (GEN6_RC_CTL_RC6_ENABLE |
4683 GEN6_RC_CTL_HW_ENABLE)) &&
4684 ((I915_READ(GEN6_RC_CONTROL) & GEN6_RC_CTL_HW_ENABLE) ||
4685 !(I915_READ(GEN6_RC_STATE) & RC6_STATE))) {
4686 DRM_DEBUG_KMS("HW/SW RC6 is not enabled by BIOS.\n");
4687 enable_rc6 = false;
4688 }
4689
4690 return enable_rc6;
4691}
4692
4693int sanitize_rc6_option(const struct drm_device *dev, int enable_rc6)
2b4e57bd 4694{
e7d66d89
DV
4695 /* No RC6 before Ironlake and code is gone for ilk. */
4696 if (INTEL_INFO(dev)->gen < 6)
e6069ca8
ID
4697 return 0;
4698
274008e8
SAK
4699 if (!enable_rc6)
4700 return 0;
4701
4702 if (IS_BROXTON(dev) && !bxt_check_bios_rc6_setup(dev)) {
4703 DRM_INFO("RC6 disabled by BIOS\n");
4704 return 0;
4705 }
4706
456470eb 4707 /* Respect the kernel parameter if it is set */
e6069ca8
ID
4708 if (enable_rc6 >= 0) {
4709 int mask;
4710
58abf1da 4711 if (HAS_RC6p(dev))
e6069ca8
ID
4712 mask = INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE |
4713 INTEL_RC6pp_ENABLE;
4714 else
4715 mask = INTEL_RC6_ENABLE;
4716
4717 if ((enable_rc6 & mask) != enable_rc6)
8dfd1f04
DV
4718 DRM_DEBUG_KMS("Adjusting RC6 mask to %d (requested %d, valid %d)\n",
4719 enable_rc6 & mask, enable_rc6, mask);
e6069ca8
ID
4720
4721 return enable_rc6 & mask;
4722 }
2b4e57bd 4723
8bade1ad 4724 if (IS_IVYBRIDGE(dev))
cca84a1f 4725 return (INTEL_RC6_ENABLE | INTEL_RC6p_ENABLE);
8bade1ad
BW
4726
4727 return INTEL_RC6_ENABLE;
2b4e57bd
ED
4728}
4729
e6069ca8
ID
4730int intel_enable_rc6(const struct drm_device *dev)
4731{
4732 return i915.enable_rc6;
4733}
4734
93ee2920 4735static void gen6_init_rps_frequencies(struct drm_device *dev)
3280e8b0 4736{
93ee2920
TR
4737 struct drm_i915_private *dev_priv = dev->dev_private;
4738 uint32_t rp_state_cap;
4739 u32 ddcc_status = 0;
4740 int ret;
4741
3280e8b0
BW
4742 /* All of these values are in units of 50MHz */
4743 dev_priv->rps.cur_freq = 0;
93ee2920 4744 /* static values from HW: RP0 > RP1 > RPn (min_freq) */
35040562
BP
4745 if (IS_BROXTON(dev)) {
4746 rp_state_cap = I915_READ(BXT_RP_STATE_CAP);
4747 dev_priv->rps.rp0_freq = (rp_state_cap >> 16) & 0xff;
4748 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4749 dev_priv->rps.min_freq = (rp_state_cap >> 0) & 0xff;
4750 } else {
4751 rp_state_cap = I915_READ(GEN6_RP_STATE_CAP);
4752 dev_priv->rps.rp0_freq = (rp_state_cap >> 0) & 0xff;
4753 dev_priv->rps.rp1_freq = (rp_state_cap >> 8) & 0xff;
4754 dev_priv->rps.min_freq = (rp_state_cap >> 16) & 0xff;
4755 }
4756
3280e8b0
BW
4757 /* hw_max = RP0 until we check for overclocking */
4758 dev_priv->rps.max_freq = dev_priv->rps.rp0_freq;
4759
93ee2920 4760 dev_priv->rps.efficient_freq = dev_priv->rps.rp1_freq;
ef11bdb3
RV
4761 if (IS_HASWELL(dev) || IS_BROADWELL(dev) ||
4762 IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
93ee2920
TR
4763 ret = sandybridge_pcode_read(dev_priv,
4764 HSW_PCODE_DYNAMIC_DUTY_CYCLE_CONTROL,
4765 &ddcc_status);
4766 if (0 == ret)
4767 dev_priv->rps.efficient_freq =
46efa4ab
TR
4768 clamp_t(u8,
4769 ((ddcc_status >> 8) & 0xff),
4770 dev_priv->rps.min_freq,
4771 dev_priv->rps.max_freq);
93ee2920
TR
4772 }
4773
ef11bdb3 4774 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
c5e0688c
AG
4775 /* Store the frequency values in 16.66 MHZ units, which is
4776 the natural hardware unit for SKL */
4777 dev_priv->rps.rp0_freq *= GEN9_FREQ_SCALER;
4778 dev_priv->rps.rp1_freq *= GEN9_FREQ_SCALER;
4779 dev_priv->rps.min_freq *= GEN9_FREQ_SCALER;
4780 dev_priv->rps.max_freq *= GEN9_FREQ_SCALER;
4781 dev_priv->rps.efficient_freq *= GEN9_FREQ_SCALER;
4782 }
4783
aed242ff
CW
4784 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
4785
3280e8b0
BW
4786 /* Preserve min/max settings in case of re-init */
4787 if (dev_priv->rps.max_freq_softlimit == 0)
4788 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
4789
93ee2920
TR
4790 if (dev_priv->rps.min_freq_softlimit == 0) {
4791 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
4792 dev_priv->rps.min_freq_softlimit =
813b5e69
VS
4793 max_t(int, dev_priv->rps.efficient_freq,
4794 intel_freq_opcode(dev_priv, 450));
93ee2920
TR
4795 else
4796 dev_priv->rps.min_freq_softlimit =
4797 dev_priv->rps.min_freq;
4798 }
3280e8b0
BW
4799}
4800
b6fef0ef 4801/* See the Gen9_GT_PM_Programming_Guide doc for the below */
20e49366 4802static void gen9_enable_rps(struct drm_device *dev)
b6fef0ef
JB
4803{
4804 struct drm_i915_private *dev_priv = dev->dev_private;
4805
4806 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
4807
ba1c554c
DL
4808 gen6_init_rps_frequencies(dev);
4809
23eafea6 4810 /* WaGsvDisableTurbo: Workaround to disable turbo on BXT A* */
e87a005d 4811 if (IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
2030d684
AG
4812 /*
4813 * BIOS could leave the Hw Turbo enabled, so need to explicitly
4814 * clear out the Control register just to avoid inconsitency
4815 * with debugfs interface, which will show Turbo as enabled
4816 * only and that is not expected by the User after adding the
4817 * WaGsvDisableTurbo. Apart from this there is no problem even
4818 * if the Turbo is left enabled in the Control register, as the
4819 * Up/Down interrupts would remain masked.
4820 */
4821 gen9_disable_rps(dev);
23eafea6
SAK
4822 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4823 return;
4824 }
4825
0beb059a
AG
4826 /* Program defaults and thresholds for RPS*/
4827 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4828 GEN9_FREQUENCY(dev_priv->rps.rp1_freq));
4829
4830 /* 1 second timeout*/
4831 I915_WRITE(GEN6_RP_DOWN_TIMEOUT,
4832 GT_INTERVAL_FROM_US(dev_priv, 1000000));
4833
b6fef0ef 4834 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 0xa);
b6fef0ef 4835
0beb059a
AG
4836 /* Leaning on the below call to gen6_set_rps to program/setup the
4837 * Up/Down EI & threshold registers, as well as the RP_CONTROL,
4838 * RP_INTERRUPT_LIMITS & RPNSWREQ registers */
4839 dev_priv->rps.power = HIGH_POWER; /* force a reset */
5fd9f523 4840 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
b6fef0ef
JB
4841
4842 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
4843}
4844
4845static void gen9_enable_rc6(struct drm_device *dev)
20e49366
ZW
4846{
4847 struct drm_i915_private *dev_priv = dev->dev_private;
e2f80391 4848 struct intel_engine_cs *engine;
20e49366 4849 uint32_t rc6_mask = 0;
20e49366
ZW
4850
4851 /* 1a: Software RC state - RC0 */
4852 I915_WRITE(GEN6_RC_STATE, 0);
4853
4854 /* 1b: Get forcewake during program sequence. Although the driver
4855 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
59bad947 4856 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
20e49366
ZW
4857
4858 /* 2a: Disable RC states. */
4859 I915_WRITE(GEN6_RC_CONTROL, 0);
4860
4861 /* 2b: Program RC6 thresholds.*/
63a4dec2
SAK
4862
4863 /* WaRsDoubleRc6WrlWithCoarsePowerGating: Doubling WRL only when CPG is enabled */
e7674b8c 4864 if (IS_SKYLAKE(dev))
63a4dec2
SAK
4865 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 108 << 16);
4866 else
4867 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 54 << 16);
20e49366
ZW
4868 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4869 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
b4ac5afc 4870 for_each_engine(engine, dev_priv)
e2f80391 4871 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
97c322e7
SAK
4872
4873 if (HAS_GUC_UCODE(dev))
4874 I915_WRITE(GUC_MAX_IDLE_COUNT, 0xA);
4875
20e49366 4876 I915_WRITE(GEN6_RC_SLEEP, 0);
20e49366 4877
38c23527
ZW
4878 /* 2c: Program Coarse Power Gating Policies. */
4879 I915_WRITE(GEN9_MEDIA_PG_IDLE_HYSTERESIS, 25);
4880 I915_WRITE(GEN9_RENDER_PG_IDLE_HYSTERESIS, 25);
4881
20e49366
ZW
4882 /* 3a: Enable RC6 */
4883 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4884 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
87ad3212 4885 DRM_INFO("RC6 %s\n", onoff(rc6_mask & GEN6_RC_CTL_RC6_ENABLE));
3e7732a0 4886 /* WaRsUseTimeoutMode */
e87a005d 4887 if (IS_SKL_REVID(dev, 0, SKL_REVID_D0) ||
cbdc12a9 4888 IS_BXT_REVID(dev, 0, BXT_REVID_A1)) {
3e7732a0 4889 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us */
e3429cd2
SAK
4890 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4891 GEN7_RC_CTL_TO_MODE |
4892 rc6_mask);
3e7732a0
SAK
4893 } else {
4894 I915_WRITE(GEN6_RC6_THRESHOLD, 37500); /* 37.5/125ms per EI */
e3429cd2
SAK
4895 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4896 GEN6_RC_CTL_EI_MODE(1) |
4897 rc6_mask);
3e7732a0 4898 }
20e49366 4899
cb07bae0
SK
4900 /*
4901 * 3b: Enable Coarse Power Gating only when RC6 is enabled.
f2d2fe95 4902 * WaRsDisableCoarsePowerGating:skl,bxt - Render/Media PG need to be disabled with RC6.
cb07bae0 4903 */
06e668ac 4904 if (NEEDS_WaRsDisableCoarsePowerGating(dev))
f2d2fe95
SAK
4905 I915_WRITE(GEN9_PG_ENABLE, 0);
4906 else
4907 I915_WRITE(GEN9_PG_ENABLE, (rc6_mask & GEN6_RC_CTL_RC6_ENABLE) ?
4908 (GEN9_RENDER_PG_ENABLE | GEN9_MEDIA_PG_ENABLE) : 0);
38c23527 4909
59bad947 4910 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
20e49366
ZW
4911
4912}
4913
6edee7f3
BW
4914static void gen8_enable_rps(struct drm_device *dev)
4915{
4916 struct drm_i915_private *dev_priv = dev->dev_private;
e2f80391 4917 struct intel_engine_cs *engine;
93ee2920 4918 uint32_t rc6_mask = 0;
6edee7f3
BW
4919
4920 /* 1a: Software RC state - RC0 */
4921 I915_WRITE(GEN6_RC_STATE, 0);
4922
4923 /* 1c & 1d: Get forcewake during program sequence. Although the driver
4924 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
59bad947 4925 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
6edee7f3
BW
4926
4927 /* 2a: Disable RC states. */
4928 I915_WRITE(GEN6_RC_CONTROL, 0);
4929
93ee2920
TR
4930 /* Initialize rps frequencies */
4931 gen6_init_rps_frequencies(dev);
6edee7f3
BW
4932
4933 /* 2b: Program RC6 thresholds.*/
4934 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
4935 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
4936 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
b4ac5afc 4937 for_each_engine(engine, dev_priv)
e2f80391 4938 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
6edee7f3 4939 I915_WRITE(GEN6_RC_SLEEP, 0);
0d68b25e
TR
4940 if (IS_BROADWELL(dev))
4941 I915_WRITE(GEN6_RC6_THRESHOLD, 625); /* 800us/1.28 for TO */
4942 else
4943 I915_WRITE(GEN6_RC6_THRESHOLD, 50000); /* 50/125ms per EI */
6edee7f3
BW
4944
4945 /* 3: Enable RC6 */
4946 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
4947 rc6_mask = GEN6_RC_CTL_RC6_ENABLE;
abbf9d2c 4948 intel_print_rc6_info(dev, rc6_mask);
0d68b25e
TR
4949 if (IS_BROADWELL(dev))
4950 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4951 GEN7_RC_CTL_TO_MODE |
4952 rc6_mask);
4953 else
4954 I915_WRITE(GEN6_RC_CONTROL, GEN6_RC_CTL_HW_ENABLE |
4955 GEN6_RC_CTL_EI_MODE(1) |
4956 rc6_mask);
6edee7f3
BW
4957
4958 /* 4 Program defaults and thresholds for RPS*/
f9bdc585
BW
4959 I915_WRITE(GEN6_RPNSWREQ,
4960 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
4961 I915_WRITE(GEN6_RC_VIDEO_FREQ,
4962 HSW_FREQUENCY(dev_priv->rps.rp1_freq));
7526ed79
DV
4963 /* NB: Docs say 1s, and 1000000 - which aren't equivalent */
4964 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 100000000 / 128); /* 1 second timeout */
4965
4966 /* Docs recommend 900MHz, and 300 MHz respectively */
4967 I915_WRITE(GEN6_RP_INTERRUPT_LIMITS,
4968 dev_priv->rps.max_freq_softlimit << 24 |
4969 dev_priv->rps.min_freq_softlimit << 16);
4970
4971 I915_WRITE(GEN6_RP_UP_THRESHOLD, 7600000 / 128); /* 76ms busyness per EI, 90% */
4972 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 31300000 / 128); /* 313ms busyness per EI, 70%*/
4973 I915_WRITE(GEN6_RP_UP_EI, 66000); /* 84.48ms, XXX: random? */
4974 I915_WRITE(GEN6_RP_DOWN_EI, 350000); /* 448ms, XXX: random? */
4975
4976 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
6edee7f3
BW
4977
4978 /* 5: Enable RPS */
7526ed79
DV
4979 I915_WRITE(GEN6_RP_CONTROL,
4980 GEN6_RP_MEDIA_TURBO |
4981 GEN6_RP_MEDIA_HW_NORMAL_MODE |
4982 GEN6_RP_MEDIA_IS_GFX |
4983 GEN6_RP_ENABLE |
4984 GEN6_RP_UP_BUSY_AVG |
4985 GEN6_RP_DOWN_IDLE_AVG);
4986
4987 /* 6: Ring frequency + overclocking (our driver does this later */
4988
c7f3153a 4989 dev_priv->rps.power = HIGH_POWER; /* force a reset */
aed242ff 4990 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
7526ed79 4991
59bad947 4992 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
6edee7f3
BW
4993}
4994
79f5b2c7 4995static void gen6_enable_rps(struct drm_device *dev)
2b4e57bd 4996{
79f5b2c7 4997 struct drm_i915_private *dev_priv = dev->dev_private;
e2f80391 4998 struct intel_engine_cs *engine;
d060c169 4999 u32 rc6vids, pcu_mbox = 0, rc6_mask = 0;
2b4e57bd 5000 u32 gtfifodbg;
2b4e57bd 5001 int rc6_mode;
b4ac5afc 5002 int ret;
2b4e57bd 5003
4fc688ce 5004 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
79f5b2c7 5005
2b4e57bd
ED
5006 /* Here begins a magic sequence of register writes to enable
5007 * auto-downclocking.
5008 *
5009 * Perhaps there might be some value in exposing these to
5010 * userspace...
5011 */
5012 I915_WRITE(GEN6_RC_STATE, 0);
2b4e57bd
ED
5013
5014 /* Clear the DBG now so we don't confuse earlier errors */
297b32ec
VS
5015 gtfifodbg = I915_READ(GTFIFODBG);
5016 if (gtfifodbg) {
2b4e57bd
ED
5017 DRM_ERROR("GT fifo had a previous error %x\n", gtfifodbg);
5018 I915_WRITE(GTFIFODBG, gtfifodbg);
5019 }
5020
59bad947 5021 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
2b4e57bd 5022
93ee2920
TR
5023 /* Initialize rps frequencies */
5024 gen6_init_rps_frequencies(dev);
dd0a1aa1 5025
2b4e57bd
ED
5026 /* disable the counters and set deterministic thresholds */
5027 I915_WRITE(GEN6_RC_CONTROL, 0);
5028
5029 I915_WRITE(GEN6_RC1_WAKE_RATE_LIMIT, 1000 << 16);
5030 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16 | 30);
5031 I915_WRITE(GEN6_RC6pp_WAKE_RATE_LIMIT, 30);
5032 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5033 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5034
b4ac5afc 5035 for_each_engine(engine, dev_priv)
e2f80391 5036 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
2b4e57bd
ED
5037
5038 I915_WRITE(GEN6_RC_SLEEP, 0);
5039 I915_WRITE(GEN6_RC1e_THRESHOLD, 1000);
29c78f60 5040 if (IS_IVYBRIDGE(dev))
351aa566
SM
5041 I915_WRITE(GEN6_RC6_THRESHOLD, 125000);
5042 else
5043 I915_WRITE(GEN6_RC6_THRESHOLD, 50000);
0920a487 5044 I915_WRITE(GEN6_RC6p_THRESHOLD, 150000);
2b4e57bd
ED
5045 I915_WRITE(GEN6_RC6pp_THRESHOLD, 64000); /* unused */
5046
5a7dc92a 5047 /* Check if we are enabling RC6 */
2b4e57bd
ED
5048 rc6_mode = intel_enable_rc6(dev_priv->dev);
5049 if (rc6_mode & INTEL_RC6_ENABLE)
5050 rc6_mask |= GEN6_RC_CTL_RC6_ENABLE;
5051
5a7dc92a
ED
5052 /* We don't use those on Haswell */
5053 if (!IS_HASWELL(dev)) {
5054 if (rc6_mode & INTEL_RC6p_ENABLE)
5055 rc6_mask |= GEN6_RC_CTL_RC6p_ENABLE;
2b4e57bd 5056
5a7dc92a
ED
5057 if (rc6_mode & INTEL_RC6pp_ENABLE)
5058 rc6_mask |= GEN6_RC_CTL_RC6pp_ENABLE;
5059 }
2b4e57bd 5060
dc39fff7 5061 intel_print_rc6_info(dev, rc6_mask);
2b4e57bd
ED
5062
5063 I915_WRITE(GEN6_RC_CONTROL,
5064 rc6_mask |
5065 GEN6_RC_CTL_EI_MODE(1) |
5066 GEN6_RC_CTL_HW_ENABLE);
5067
dd75fdc8
CW
5068 /* Power down if completely idle for over 50ms */
5069 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 50000);
2b4e57bd 5070 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
2b4e57bd 5071
42c0526c 5072 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_MIN_FREQ_TABLE, 0);
d060c169 5073 if (ret)
42c0526c 5074 DRM_DEBUG_DRIVER("Failed to set the min frequency\n");
d060c169
BW
5075
5076 ret = sandybridge_pcode_read(dev_priv, GEN6_READ_OC_PARAMS, &pcu_mbox);
5077 if (!ret && (pcu_mbox & (1<<31))) { /* OC supported */
5078 DRM_DEBUG_DRIVER("Overclocking supported. Max: %dMHz, Overclock max: %dMHz\n",
b39fb297 5079 (dev_priv->rps.max_freq_softlimit & 0xff) * 50,
d060c169 5080 (pcu_mbox & 0xff) * 50);
b39fb297 5081 dev_priv->rps.max_freq = pcu_mbox & 0xff;
2b4e57bd
ED
5082 }
5083
dd75fdc8 5084 dev_priv->rps.power = HIGH_POWER; /* force a reset */
aed242ff 5085 gen6_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
2b4e57bd 5086
31643d54
BW
5087 rc6vids = 0;
5088 ret = sandybridge_pcode_read(dev_priv, GEN6_PCODE_READ_RC6VIDS, &rc6vids);
5089 if (IS_GEN6(dev) && ret) {
5090 DRM_DEBUG_DRIVER("Couldn't check for BIOS workaround\n");
5091 } else if (IS_GEN6(dev) && (GEN6_DECODE_RC6_VID(rc6vids & 0xff) < 450)) {
5092 DRM_DEBUG_DRIVER("You should update your BIOS. Correcting minimum rc6 voltage (%dmV->%dmV)\n",
5093 GEN6_DECODE_RC6_VID(rc6vids & 0xff), 450);
5094 rc6vids &= 0xffff00;
5095 rc6vids |= GEN6_ENCODE_RC6_VID(450);
5096 ret = sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_RC6VIDS, rc6vids);
5097 if (ret)
5098 DRM_ERROR("Couldn't fix incorrect rc6 voltage\n");
5099 }
5100
59bad947 5101 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
2b4e57bd
ED
5102}
5103
c2bc2fc5 5104static void __gen6_update_ring_freq(struct drm_device *dev)
2b4e57bd 5105{
79f5b2c7 5106 struct drm_i915_private *dev_priv = dev->dev_private;
2b4e57bd 5107 int min_freq = 15;
3ebecd07
CW
5108 unsigned int gpu_freq;
5109 unsigned int max_ia_freq, min_ring_freq;
4c8c7743 5110 unsigned int max_gpu_freq, min_gpu_freq;
2b4e57bd 5111 int scaling_factor = 180;
eda79642 5112 struct cpufreq_policy *policy;
2b4e57bd 5113
4fc688ce 5114 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
79f5b2c7 5115
eda79642
BW
5116 policy = cpufreq_cpu_get(0);
5117 if (policy) {
5118 max_ia_freq = policy->cpuinfo.max_freq;
5119 cpufreq_cpu_put(policy);
5120 } else {
5121 /*
5122 * Default to measured freq if none found, PCU will ensure we
5123 * don't go over
5124 */
2b4e57bd 5125 max_ia_freq = tsc_khz;
eda79642 5126 }
2b4e57bd
ED
5127
5128 /* Convert from kHz to MHz */
5129 max_ia_freq /= 1000;
5130
153b4b95 5131 min_ring_freq = I915_READ(DCLK) & 0xf;
f6aca45c
BW
5132 /* convert DDR frequency from units of 266.6MHz to bandwidth */
5133 min_ring_freq = mult_frac(min_ring_freq, 8, 3);
3ebecd07 5134
ef11bdb3 5135 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
4c8c7743
AG
5136 /* Convert GT frequency to 50 HZ units */
5137 min_gpu_freq = dev_priv->rps.min_freq / GEN9_FREQ_SCALER;
5138 max_gpu_freq = dev_priv->rps.max_freq / GEN9_FREQ_SCALER;
5139 } else {
5140 min_gpu_freq = dev_priv->rps.min_freq;
5141 max_gpu_freq = dev_priv->rps.max_freq;
5142 }
5143
2b4e57bd
ED
5144 /*
5145 * For each potential GPU frequency, load a ring frequency we'd like
5146 * to use for memory access. We do this by specifying the IA frequency
5147 * the PCU should use as a reference to determine the ring frequency.
5148 */
4c8c7743
AG
5149 for (gpu_freq = max_gpu_freq; gpu_freq >= min_gpu_freq; gpu_freq--) {
5150 int diff = max_gpu_freq - gpu_freq;
3ebecd07
CW
5151 unsigned int ia_freq = 0, ring_freq = 0;
5152
ef11bdb3 5153 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev)) {
4c8c7743
AG
5154 /*
5155 * ring_freq = 2 * GT. ring_freq is in 100MHz units
5156 * No floor required for ring frequency on SKL.
5157 */
5158 ring_freq = gpu_freq;
5159 } else if (INTEL_INFO(dev)->gen >= 8) {
46c764d4
BW
5160 /* max(2 * GT, DDR). NB: GT is 50MHz units */
5161 ring_freq = max(min_ring_freq, gpu_freq);
5162 } else if (IS_HASWELL(dev)) {
f6aca45c 5163 ring_freq = mult_frac(gpu_freq, 5, 4);
3ebecd07
CW
5164 ring_freq = max(min_ring_freq, ring_freq);
5165 /* leave ia_freq as the default, chosen by cpufreq */
5166 } else {
5167 /* On older processors, there is no separate ring
5168 * clock domain, so in order to boost the bandwidth
5169 * of the ring, we need to upclock the CPU (ia_freq).
5170 *
5171 * For GPU frequencies less than 750MHz,
5172 * just use the lowest ring freq.
5173 */
5174 if (gpu_freq < min_freq)
5175 ia_freq = 800;
5176 else
5177 ia_freq = max_ia_freq - ((diff * scaling_factor) / 2);
5178 ia_freq = DIV_ROUND_CLOSEST(ia_freq, 100);
5179 }
2b4e57bd 5180
42c0526c
BW
5181 sandybridge_pcode_write(dev_priv,
5182 GEN6_PCODE_WRITE_MIN_FREQ_TABLE,
3ebecd07
CW
5183 ia_freq << GEN6_PCODE_FREQ_IA_RATIO_SHIFT |
5184 ring_freq << GEN6_PCODE_FREQ_RING_RATIO_SHIFT |
5185 gpu_freq);
2b4e57bd 5186 }
2b4e57bd
ED
5187}
5188
c2bc2fc5
ID
5189void gen6_update_ring_freq(struct drm_device *dev)
5190{
5191 struct drm_i915_private *dev_priv = dev->dev_private;
5192
97d3308a 5193 if (!HAS_CORE_RING_FREQ(dev))
c2bc2fc5
ID
5194 return;
5195
5196 mutex_lock(&dev_priv->rps.hw_lock);
5197 __gen6_update_ring_freq(dev);
5198 mutex_unlock(&dev_priv->rps.hw_lock);
5199}
5200
03af2045 5201static int cherryview_rps_max_freq(struct drm_i915_private *dev_priv)
2b6b3a09 5202{
095acd5f 5203 struct drm_device *dev = dev_priv->dev;
2b6b3a09
D
5204 u32 val, rp0;
5205
5b5929cb 5206 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
2b6b3a09 5207
5b5929cb
JN
5208 switch (INTEL_INFO(dev)->eu_total) {
5209 case 8:
5210 /* (2 * 4) config */
5211 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS4EU_FUSE_SHIFT);
5212 break;
5213 case 12:
5214 /* (2 * 6) config */
5215 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS6EU_FUSE_SHIFT);
5216 break;
5217 case 16:
5218 /* (2 * 8) config */
5219 default:
5220 /* Setting (2 * 8) Min RP0 for any other combination */
5221 rp0 = (val >> FB_GFX_FMAX_AT_VMAX_2SS8EU_FUSE_SHIFT);
5222 break;
095acd5f 5223 }
5b5929cb
JN
5224
5225 rp0 = (rp0 & FB_GFX_FREQ_FUSE_MASK);
5226
2b6b3a09
D
5227 return rp0;
5228}
5229
5230static int cherryview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5231{
5232 u32 val, rpe;
5233
5234 val = vlv_punit_read(dev_priv, PUNIT_GPU_DUTYCYCLE_REG);
5235 rpe = (val >> PUNIT_GPU_DUTYCYCLE_RPE_FREQ_SHIFT) & PUNIT_GPU_DUTYCYCLE_RPE_FREQ_MASK;
5236
5237 return rpe;
5238}
5239
7707df4a
D
5240static int cherryview_rps_guar_freq(struct drm_i915_private *dev_priv)
5241{
5242 u32 val, rp1;
5243
5b5929cb
JN
5244 val = vlv_punit_read(dev_priv, FB_GFX_FMAX_AT_VMAX_FUSE);
5245 rp1 = (val & FB_GFX_FREQ_FUSE_MASK);
5246
7707df4a
D
5247 return rp1;
5248}
5249
f8f2b001
D
5250static int valleyview_rps_guar_freq(struct drm_i915_private *dev_priv)
5251{
5252 u32 val, rp1;
5253
5254 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
5255
5256 rp1 = (val & FB_GFX_FGUARANTEED_FREQ_FUSE_MASK) >> FB_GFX_FGUARANTEED_FREQ_FUSE_SHIFT;
5257
5258 return rp1;
5259}
5260
03af2045 5261static int valleyview_rps_max_freq(struct drm_i915_private *dev_priv)
0a073b84
JB
5262{
5263 u32 val, rp0;
5264
64936258 5265 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FREQ_FUSE);
0a073b84
JB
5266
5267 rp0 = (val & FB_GFX_MAX_FREQ_FUSE_MASK) >> FB_GFX_MAX_FREQ_FUSE_SHIFT;
5268 /* Clamp to max */
5269 rp0 = min_t(u32, rp0, 0xea);
5270
5271 return rp0;
5272}
5273
5274static int valleyview_rps_rpe_freq(struct drm_i915_private *dev_priv)
5275{
5276 u32 val, rpe;
5277
64936258 5278 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_LO);
0a073b84 5279 rpe = (val & FB_FMAX_VMIN_FREQ_LO_MASK) >> FB_FMAX_VMIN_FREQ_LO_SHIFT;
64936258 5280 val = vlv_nc_read(dev_priv, IOSF_NC_FB_GFX_FMAX_FUSE_HI);
0a073b84
JB
5281 rpe |= (val & FB_FMAX_VMIN_FREQ_HI_MASK) << 5;
5282
5283 return rpe;
5284}
5285
03af2045 5286static int valleyview_rps_min_freq(struct drm_i915_private *dev_priv)
0a073b84 5287{
36146035
ID
5288 u32 val;
5289
5290 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_LFM) & 0xff;
5291 /*
5292 * According to the BYT Punit GPU turbo HAS 1.1.6.3 the minimum value
5293 * for the minimum frequency in GPLL mode is 0xc1. Contrary to this on
5294 * a BYT-M B0 the above register contains 0xbf. Moreover when setting
5295 * a frequency Punit will not allow values below 0xc0. Clamp it 0xc0
5296 * to make sure it matches what Punit accepts.
5297 */
5298 return max_t(u32, val, 0xc0);
0a073b84
JB
5299}
5300
ae48434c
ID
5301/* Check that the pctx buffer wasn't move under us. */
5302static void valleyview_check_pctx(struct drm_i915_private *dev_priv)
5303{
5304 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5305
5306 WARN_ON(pctx_addr != dev_priv->mm.stolen_base +
5307 dev_priv->vlv_pctx->stolen->start);
5308}
5309
38807746
D
5310
5311/* Check that the pcbr address is not empty. */
5312static void cherryview_check_pctx(struct drm_i915_private *dev_priv)
5313{
5314 unsigned long pctx_addr = I915_READ(VLV_PCBR) & ~4095;
5315
5316 WARN_ON((pctx_addr >> VLV_PCBR_ADDR_SHIFT) == 0);
5317}
5318
5319static void cherryview_setup_pctx(struct drm_device *dev)
5320{
72e96d64 5321 struct drm_i915_private *dev_priv = to_i915(dev);
62106b4f 5322 struct i915_ggtt *ggtt = &dev_priv->ggtt;
72e96d64 5323 unsigned long pctx_paddr, paddr;
38807746
D
5324 u32 pcbr;
5325 int pctx_size = 32*1024;
5326
38807746
D
5327 pcbr = I915_READ(VLV_PCBR);
5328 if ((pcbr >> VLV_PCBR_ADDR_SHIFT) == 0) {
ce611ef8 5329 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
38807746 5330 paddr = (dev_priv->mm.stolen_base +
62106b4f 5331 (ggtt->stolen_size - pctx_size));
38807746
D
5332
5333 pctx_paddr = (paddr & (~4095));
5334 I915_WRITE(VLV_PCBR, pctx_paddr);
5335 }
ce611ef8
VS
5336
5337 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
38807746
D
5338}
5339
c9cddffc
JB
5340static void valleyview_setup_pctx(struct drm_device *dev)
5341{
5342 struct drm_i915_private *dev_priv = dev->dev_private;
5343 struct drm_i915_gem_object *pctx;
5344 unsigned long pctx_paddr;
5345 u32 pcbr;
5346 int pctx_size = 24*1024;
5347
ee504898 5348 mutex_lock(&dev->struct_mutex);
17b0c1f7 5349
c9cddffc
JB
5350 pcbr = I915_READ(VLV_PCBR);
5351 if (pcbr) {
5352 /* BIOS set it up already, grab the pre-alloc'd space */
5353 int pcbr_offset;
5354
5355 pcbr_offset = (pcbr & (~4095)) - dev_priv->mm.stolen_base;
5356 pctx = i915_gem_object_create_stolen_for_preallocated(dev_priv->dev,
5357 pcbr_offset,
190d6cd5 5358 I915_GTT_OFFSET_NONE,
c9cddffc
JB
5359 pctx_size);
5360 goto out;
5361 }
5362
ce611ef8
VS
5363 DRM_DEBUG_DRIVER("BIOS didn't set up PCBR, fixing up\n");
5364
c9cddffc
JB
5365 /*
5366 * From the Gunit register HAS:
5367 * The Gfx driver is expected to program this register and ensure
5368 * proper allocation within Gfx stolen memory. For example, this
5369 * register should be programmed such than the PCBR range does not
5370 * overlap with other ranges, such as the frame buffer, protected
5371 * memory, or any other relevant ranges.
5372 */
5373 pctx = i915_gem_object_create_stolen(dev, pctx_size);
5374 if (!pctx) {
5375 DRM_DEBUG("not enough stolen space for PCTX, disabling\n");
ee504898 5376 goto out;
c9cddffc
JB
5377 }
5378
5379 pctx_paddr = dev_priv->mm.stolen_base + pctx->stolen->start;
5380 I915_WRITE(VLV_PCBR, pctx_paddr);
5381
5382out:
ce611ef8 5383 DRM_DEBUG_DRIVER("PCBR: 0x%08x\n", I915_READ(VLV_PCBR));
c9cddffc 5384 dev_priv->vlv_pctx = pctx;
ee504898 5385 mutex_unlock(&dev->struct_mutex);
c9cddffc
JB
5386}
5387
ae48434c
ID
5388static void valleyview_cleanup_pctx(struct drm_device *dev)
5389{
5390 struct drm_i915_private *dev_priv = dev->dev_private;
5391
5392 if (WARN_ON(!dev_priv->vlv_pctx))
5393 return;
5394
ee504898 5395 drm_gem_object_unreference_unlocked(&dev_priv->vlv_pctx->base);
ae48434c
ID
5396 dev_priv->vlv_pctx = NULL;
5397}
5398
c30fec65
VS
5399static void vlv_init_gpll_ref_freq(struct drm_i915_private *dev_priv)
5400{
5401 dev_priv->rps.gpll_ref_freq =
5402 vlv_get_cck_clock(dev_priv, "GPLL ref",
5403 CCK_GPLL_CLOCK_CONTROL,
5404 dev_priv->czclk_freq);
5405
5406 DRM_DEBUG_DRIVER("GPLL reference freq: %d kHz\n",
5407 dev_priv->rps.gpll_ref_freq);
5408}
5409
4e80519e
ID
5410static void valleyview_init_gt_powersave(struct drm_device *dev)
5411{
5412 struct drm_i915_private *dev_priv = dev->dev_private;
2bb25c17 5413 u32 val;
4e80519e
ID
5414
5415 valleyview_setup_pctx(dev);
5416
c30fec65
VS
5417 vlv_init_gpll_ref_freq(dev_priv);
5418
4e80519e
ID
5419 mutex_lock(&dev_priv->rps.hw_lock);
5420
2bb25c17
VS
5421 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5422 switch ((val >> 6) & 3) {
5423 case 0:
5424 case 1:
5425 dev_priv->mem_freq = 800;
5426 break;
5427 case 2:
5428 dev_priv->mem_freq = 1066;
5429 break;
5430 case 3:
5431 dev_priv->mem_freq = 1333;
5432 break;
5433 }
80b83b62 5434 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
2bb25c17 5435
4e80519e
ID
5436 dev_priv->rps.max_freq = valleyview_rps_max_freq(dev_priv);
5437 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5438 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
7c59a9c1 5439 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
4e80519e
ID
5440 dev_priv->rps.max_freq);
5441
5442 dev_priv->rps.efficient_freq = valleyview_rps_rpe_freq(dev_priv);
5443 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
7c59a9c1 5444 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
4e80519e
ID
5445 dev_priv->rps.efficient_freq);
5446
f8f2b001
D
5447 dev_priv->rps.rp1_freq = valleyview_rps_guar_freq(dev_priv);
5448 DRM_DEBUG_DRIVER("RP1(Guar Freq) GPU freq: %d MHz (%u)\n",
7c59a9c1 5449 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
f8f2b001
D
5450 dev_priv->rps.rp1_freq);
5451
4e80519e
ID
5452 dev_priv->rps.min_freq = valleyview_rps_min_freq(dev_priv);
5453 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
7c59a9c1 5454 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
4e80519e
ID
5455 dev_priv->rps.min_freq);
5456
aed242ff
CW
5457 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5458
4e80519e
ID
5459 /* Preserve min/max settings in case of re-init */
5460 if (dev_priv->rps.max_freq_softlimit == 0)
5461 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5462
5463 if (dev_priv->rps.min_freq_softlimit == 0)
5464 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5465
5466 mutex_unlock(&dev_priv->rps.hw_lock);
5467}
5468
38807746
D
5469static void cherryview_init_gt_powersave(struct drm_device *dev)
5470{
2b6b3a09 5471 struct drm_i915_private *dev_priv = dev->dev_private;
2bb25c17 5472 u32 val;
2b6b3a09 5473
38807746 5474 cherryview_setup_pctx(dev);
2b6b3a09 5475
c30fec65
VS
5476 vlv_init_gpll_ref_freq(dev_priv);
5477
2b6b3a09
D
5478 mutex_lock(&dev_priv->rps.hw_lock);
5479
a580516d 5480 mutex_lock(&dev_priv->sb_lock);
c6e8f39d 5481 val = vlv_cck_read(dev_priv, CCK_FUSE_REG);
a580516d 5482 mutex_unlock(&dev_priv->sb_lock);
c6e8f39d 5483
2bb25c17 5484 switch ((val >> 2) & 0x7) {
2bb25c17 5485 case 3:
2bb25c17
VS
5486 dev_priv->mem_freq = 2000;
5487 break;
bfa7df01 5488 default:
2bb25c17
VS
5489 dev_priv->mem_freq = 1600;
5490 break;
5491 }
80b83b62 5492 DRM_DEBUG_DRIVER("DDR speed: %d MHz\n", dev_priv->mem_freq);
2bb25c17 5493
2b6b3a09
D
5494 dev_priv->rps.max_freq = cherryview_rps_max_freq(dev_priv);
5495 dev_priv->rps.rp0_freq = dev_priv->rps.max_freq;
5496 DRM_DEBUG_DRIVER("max GPU freq: %d MHz (%u)\n",
7c59a9c1 5497 intel_gpu_freq(dev_priv, dev_priv->rps.max_freq),
2b6b3a09
D
5498 dev_priv->rps.max_freq);
5499
5500 dev_priv->rps.efficient_freq = cherryview_rps_rpe_freq(dev_priv);
5501 DRM_DEBUG_DRIVER("RPe GPU freq: %d MHz (%u)\n",
7c59a9c1 5502 intel_gpu_freq(dev_priv, dev_priv->rps.efficient_freq),
2b6b3a09
D
5503 dev_priv->rps.efficient_freq);
5504
7707df4a
D
5505 dev_priv->rps.rp1_freq = cherryview_rps_guar_freq(dev_priv);
5506 DRM_DEBUG_DRIVER("RP1(Guar) GPU freq: %d MHz (%u)\n",
7c59a9c1 5507 intel_gpu_freq(dev_priv, dev_priv->rps.rp1_freq),
7707df4a
D
5508 dev_priv->rps.rp1_freq);
5509
5b7c91b7
D
5510 /* PUnit validated range is only [RPe, RP0] */
5511 dev_priv->rps.min_freq = dev_priv->rps.efficient_freq;
2b6b3a09 5512 DRM_DEBUG_DRIVER("min GPU freq: %d MHz (%u)\n",
7c59a9c1 5513 intel_gpu_freq(dev_priv, dev_priv->rps.min_freq),
2b6b3a09
D
5514 dev_priv->rps.min_freq);
5515
1c14762d
VS
5516 WARN_ONCE((dev_priv->rps.max_freq |
5517 dev_priv->rps.efficient_freq |
5518 dev_priv->rps.rp1_freq |
5519 dev_priv->rps.min_freq) & 1,
5520 "Odd GPU freq values\n");
5521
aed242ff
CW
5522 dev_priv->rps.idle_freq = dev_priv->rps.min_freq;
5523
2b6b3a09
D
5524 /* Preserve min/max settings in case of re-init */
5525 if (dev_priv->rps.max_freq_softlimit == 0)
5526 dev_priv->rps.max_freq_softlimit = dev_priv->rps.max_freq;
5527
5528 if (dev_priv->rps.min_freq_softlimit == 0)
5529 dev_priv->rps.min_freq_softlimit = dev_priv->rps.min_freq;
5530
5531 mutex_unlock(&dev_priv->rps.hw_lock);
38807746
D
5532}
5533
4e80519e
ID
5534static void valleyview_cleanup_gt_powersave(struct drm_device *dev)
5535{
5536 valleyview_cleanup_pctx(dev);
5537}
5538
38807746
D
5539static void cherryview_enable_rps(struct drm_device *dev)
5540{
5541 struct drm_i915_private *dev_priv = dev->dev_private;
e2f80391 5542 struct intel_engine_cs *engine;
2b6b3a09 5543 u32 gtfifodbg, val, rc6_mode = 0, pcbr;
38807746
D
5544
5545 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5546
297b32ec
VS
5547 gtfifodbg = I915_READ(GTFIFODBG) & ~(GT_FIFO_SBDEDICATE_FREE_ENTRY_CHV |
5548 GT_FIFO_FREE_ENTRIES_CHV);
38807746
D
5549 if (gtfifodbg) {
5550 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5551 gtfifodbg);
5552 I915_WRITE(GTFIFODBG, gtfifodbg);
5553 }
5554
5555 cherryview_check_pctx(dev_priv);
5556
5557 /* 1a & 1b: Get forcewake during program sequence. Although the driver
5558 * hasn't enabled a state yet where we need forcewake, BIOS may have.*/
59bad947 5559 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
38807746 5560
160614a2
VS
5561 /* Disable RC states. */
5562 I915_WRITE(GEN6_RC_CONTROL, 0);
5563
38807746
D
5564 /* 2a: Program RC6 thresholds.*/
5565 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 40 << 16);
5566 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000); /* 12500 * 1280ns */
5567 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25); /* 25 * 1280ns */
5568
b4ac5afc 5569 for_each_engine(engine, dev_priv)
e2f80391 5570 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
38807746
D
5571 I915_WRITE(GEN6_RC_SLEEP, 0);
5572
f4f71c7d
D
5573 /* TO threshold set to 500 us ( 0x186 * 1.28 us) */
5574 I915_WRITE(GEN6_RC6_THRESHOLD, 0x186);
38807746
D
5575
5576 /* allows RC6 residency counter to work */
5577 I915_WRITE(VLV_COUNTER_CONTROL,
5578 _MASKED_BIT_ENABLE(VLV_COUNT_RANGE_HIGH |
5579 VLV_MEDIA_RC6_COUNT_EN |
5580 VLV_RENDER_RC6_COUNT_EN));
5581
5582 /* For now we assume BIOS is allocating and populating the PCBR */
5583 pcbr = I915_READ(VLV_PCBR);
5584
38807746
D
5585 /* 3: Enable RC6 */
5586 if ((intel_enable_rc6(dev) & INTEL_RC6_ENABLE) &&
5587 (pcbr >> VLV_PCBR_ADDR_SHIFT))
af5a75a3 5588 rc6_mode = GEN7_RC_CTL_TO_MODE;
38807746
D
5589
5590 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
5591
2b6b3a09 5592 /* 4 Program defaults and thresholds for RPS*/
3cbdb48f 5593 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
2b6b3a09
D
5594 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5595 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5596 I915_WRITE(GEN6_RP_UP_EI, 66000);
5597 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5598
5599 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5600
5601 /* 5: Enable RPS */
5602 I915_WRITE(GEN6_RP_CONTROL,
5603 GEN6_RP_MEDIA_HW_NORMAL_MODE |
eb973a5e 5604 GEN6_RP_MEDIA_IS_GFX |
2b6b3a09
D
5605 GEN6_RP_ENABLE |
5606 GEN6_RP_UP_BUSY_AVG |
5607 GEN6_RP_DOWN_IDLE_AVG);
5608
3ef62342
D
5609 /* Setting Fixed Bias */
5610 val = VLV_OVERRIDE_EN |
5611 VLV_SOC_TDP_EN |
5612 CHV_BIAS_CPU_50_SOC_50;
5613 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5614
2b6b3a09
D
5615 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
5616
8d40c3ae
VS
5617 /* RPS code assumes GPLL is used */
5618 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5619
742f491d 5620 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
2b6b3a09
D
5621 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5622
5623 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
5624 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
7c59a9c1 5625 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
2b6b3a09
D
5626 dev_priv->rps.cur_freq);
5627
5628 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5fd9f523
VS
5629 intel_gpu_freq(dev_priv, dev_priv->rps.idle_freq),
5630 dev_priv->rps.idle_freq);
2b6b3a09 5631
5fd9f523 5632 valleyview_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
2b6b3a09 5633
59bad947 5634 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
38807746
D
5635}
5636
0a073b84
JB
5637static void valleyview_enable_rps(struct drm_device *dev)
5638{
5639 struct drm_i915_private *dev_priv = dev->dev_private;
e2f80391 5640 struct intel_engine_cs *engine;
2a5913a8 5641 u32 gtfifodbg, val, rc6_mode = 0;
0a073b84
JB
5642
5643 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
5644
ae48434c
ID
5645 valleyview_check_pctx(dev_priv);
5646
297b32ec
VS
5647 gtfifodbg = I915_READ(GTFIFODBG);
5648 if (gtfifodbg) {
f7d85c1e
JB
5649 DRM_DEBUG_DRIVER("GT fifo had a previous error %x\n",
5650 gtfifodbg);
0a073b84
JB
5651 I915_WRITE(GTFIFODBG, gtfifodbg);
5652 }
5653
c8d9a590 5654 /* If VLV, Forcewake all wells, else re-direct to regular path */
59bad947 5655 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
0a073b84 5656
160614a2
VS
5657 /* Disable RC states. */
5658 I915_WRITE(GEN6_RC_CONTROL, 0);
5659
cad725fe 5660 I915_WRITE(GEN6_RP_DOWN_TIMEOUT, 1000000);
0a073b84
JB
5661 I915_WRITE(GEN6_RP_UP_THRESHOLD, 59400);
5662 I915_WRITE(GEN6_RP_DOWN_THRESHOLD, 245000);
5663 I915_WRITE(GEN6_RP_UP_EI, 66000);
5664 I915_WRITE(GEN6_RP_DOWN_EI, 350000);
5665
5666 I915_WRITE(GEN6_RP_IDLE_HYSTERSIS, 10);
5667
5668 I915_WRITE(GEN6_RP_CONTROL,
5669 GEN6_RP_MEDIA_TURBO |
5670 GEN6_RP_MEDIA_HW_NORMAL_MODE |
5671 GEN6_RP_MEDIA_IS_GFX |
5672 GEN6_RP_ENABLE |
5673 GEN6_RP_UP_BUSY_AVG |
5674 GEN6_RP_DOWN_IDLE_CONT);
5675
5676 I915_WRITE(GEN6_RC6_WAKE_RATE_LIMIT, 0x00280000);
5677 I915_WRITE(GEN6_RC_EVALUATION_INTERVAL, 125000);
5678 I915_WRITE(GEN6_RC_IDLE_HYSTERSIS, 25);
5679
b4ac5afc 5680 for_each_engine(engine, dev_priv)
e2f80391 5681 I915_WRITE(RING_MAX_IDLE(engine->mmio_base), 10);
0a073b84 5682
2f0aa304 5683 I915_WRITE(GEN6_RC6_THRESHOLD, 0x557);
0a073b84
JB
5684
5685 /* allows RC6 residency counter to work */
49798eb2 5686 I915_WRITE(VLV_COUNTER_CONTROL,
31685c25
D
5687 _MASKED_BIT_ENABLE(VLV_MEDIA_RC0_COUNT_EN |
5688 VLV_RENDER_RC0_COUNT_EN |
49798eb2
JB
5689 VLV_MEDIA_RC6_COUNT_EN |
5690 VLV_RENDER_RC6_COUNT_EN));
31685c25 5691
a2b23fe0 5692 if (intel_enable_rc6(dev) & INTEL_RC6_ENABLE)
6b88f295 5693 rc6_mode = GEN7_RC_CTL_TO_MODE | VLV_RC_CTL_CTX_RST_PARALLEL;
dc39fff7
BW
5694
5695 intel_print_rc6_info(dev, rc6_mode);
5696
a2b23fe0 5697 I915_WRITE(GEN6_RC_CONTROL, rc6_mode);
0a073b84 5698
3ef62342
D
5699 /* Setting Fixed Bias */
5700 val = VLV_OVERRIDE_EN |
5701 VLV_SOC_TDP_EN |
5702 VLV_BIAS_CPU_125_SOC_875;
5703 vlv_punit_write(dev_priv, VLV_TURBO_SOC_OVERRIDE, val);
5704
64936258 5705 val = vlv_punit_read(dev_priv, PUNIT_REG_GPU_FREQ_STS);
0a073b84 5706
8d40c3ae
VS
5707 /* RPS code assumes GPLL is used */
5708 WARN_ONCE((val & GPLLENABLE) == 0, "GPLL not enabled\n");
5709
742f491d 5710 DRM_DEBUG_DRIVER("GPLL enabled? %s\n", yesno(val & GPLLENABLE));
0a073b84
JB
5711 DRM_DEBUG_DRIVER("GPU status: 0x%08x\n", val);
5712
b39fb297 5713 dev_priv->rps.cur_freq = (val >> 8) & 0xff;
73008b98 5714 DRM_DEBUG_DRIVER("current GPU freq: %d MHz (%u)\n",
7c59a9c1 5715 intel_gpu_freq(dev_priv, dev_priv->rps.cur_freq),
b39fb297 5716 dev_priv->rps.cur_freq);
0a073b84 5717
73008b98 5718 DRM_DEBUG_DRIVER("setting GPU freq to %d MHz (%u)\n",
5fd9f523
VS
5719 intel_gpu_freq(dev_priv, dev_priv->rps.idle_freq),
5720 dev_priv->rps.idle_freq);
0a073b84 5721
5fd9f523 5722 valleyview_set_rps(dev_priv->dev, dev_priv->rps.idle_freq);
0a073b84 5723
59bad947 5724 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
0a073b84
JB
5725}
5726
dde18883
ED
5727static unsigned long intel_pxfreq(u32 vidfreq)
5728{
5729 unsigned long freq;
5730 int div = (vidfreq & 0x3f0000) >> 16;
5731 int post = (vidfreq & 0x3000) >> 12;
5732 int pre = (vidfreq & 0x7);
5733
5734 if (!pre)
5735 return 0;
5736
5737 freq = ((div * 133333) / ((1<<post) * pre));
5738
5739 return freq;
5740}
5741
eb48eb00
DV
5742static const struct cparams {
5743 u16 i;
5744 u16 t;
5745 u16 m;
5746 u16 c;
5747} cparams[] = {
5748 { 1, 1333, 301, 28664 },
5749 { 1, 1066, 294, 24460 },
5750 { 1, 800, 294, 25192 },
5751 { 0, 1333, 276, 27605 },
5752 { 0, 1066, 276, 27605 },
5753 { 0, 800, 231, 23784 },
5754};
5755
f531dcb2 5756static unsigned long __i915_chipset_val(struct drm_i915_private *dev_priv)
eb48eb00
DV
5757{
5758 u64 total_count, diff, ret;
5759 u32 count1, count2, count3, m = 0, c = 0;
5760 unsigned long now = jiffies_to_msecs(jiffies), diff1;
5761 int i;
5762
02d71956
DV
5763 assert_spin_locked(&mchdev_lock);
5764
20e4d407 5765 diff1 = now - dev_priv->ips.last_time1;
eb48eb00
DV
5766
5767 /* Prevent division-by-zero if we are asking too fast.
5768 * Also, we don't get interesting results if we are polling
5769 * faster than once in 10ms, so just return the saved value
5770 * in such cases.
5771 */
5772 if (diff1 <= 10)
20e4d407 5773 return dev_priv->ips.chipset_power;
eb48eb00
DV
5774
5775 count1 = I915_READ(DMIEC);
5776 count2 = I915_READ(DDREC);
5777 count3 = I915_READ(CSIEC);
5778
5779 total_count = count1 + count2 + count3;
5780
5781 /* FIXME: handle per-counter overflow */
20e4d407
DV
5782 if (total_count < dev_priv->ips.last_count1) {
5783 diff = ~0UL - dev_priv->ips.last_count1;
eb48eb00
DV
5784 diff += total_count;
5785 } else {
20e4d407 5786 diff = total_count - dev_priv->ips.last_count1;
eb48eb00
DV
5787 }
5788
5789 for (i = 0; i < ARRAY_SIZE(cparams); i++) {
20e4d407
DV
5790 if (cparams[i].i == dev_priv->ips.c_m &&
5791 cparams[i].t == dev_priv->ips.r_t) {
eb48eb00
DV
5792 m = cparams[i].m;
5793 c = cparams[i].c;
5794 break;
5795 }
5796 }
5797
5798 diff = div_u64(diff, diff1);
5799 ret = ((m * diff) + c);
5800 ret = div_u64(ret, 10);
5801
20e4d407
DV
5802 dev_priv->ips.last_count1 = total_count;
5803 dev_priv->ips.last_time1 = now;
eb48eb00 5804
20e4d407 5805 dev_priv->ips.chipset_power = ret;
eb48eb00
DV
5806
5807 return ret;
5808}
5809
f531dcb2
CW
5810unsigned long i915_chipset_val(struct drm_i915_private *dev_priv)
5811{
3d13ef2e 5812 struct drm_device *dev = dev_priv->dev;
f531dcb2
CW
5813 unsigned long val;
5814
3d13ef2e 5815 if (INTEL_INFO(dev)->gen != 5)
f531dcb2
CW
5816 return 0;
5817
5818 spin_lock_irq(&mchdev_lock);
5819
5820 val = __i915_chipset_val(dev_priv);
5821
5822 spin_unlock_irq(&mchdev_lock);
5823
5824 return val;
5825}
5826
eb48eb00
DV
5827unsigned long i915_mch_val(struct drm_i915_private *dev_priv)
5828{
5829 unsigned long m, x, b;
5830 u32 tsfs;
5831
5832 tsfs = I915_READ(TSFS);
5833
5834 m = ((tsfs & TSFS_SLOPE_MASK) >> TSFS_SLOPE_SHIFT);
5835 x = I915_READ8(TR1);
5836
5837 b = tsfs & TSFS_INTR_MASK;
5838
5839 return ((m * x) / 127) - b;
5840}
5841
d972d6ee
MK
5842static int _pxvid_to_vd(u8 pxvid)
5843{
5844 if (pxvid == 0)
5845 return 0;
5846
5847 if (pxvid >= 8 && pxvid < 31)
5848 pxvid = 31;
5849
5850 return (pxvid + 2) * 125;
5851}
5852
5853static u32 pvid_to_extvid(struct drm_i915_private *dev_priv, u8 pxvid)
eb48eb00 5854{
3d13ef2e 5855 struct drm_device *dev = dev_priv->dev;
d972d6ee
MK
5856 const int vd = _pxvid_to_vd(pxvid);
5857 const int vm = vd - 1125;
5858
3d13ef2e 5859 if (INTEL_INFO(dev)->is_mobile)
d972d6ee
MK
5860 return vm > 0 ? vm : 0;
5861
5862 return vd;
eb48eb00
DV
5863}
5864
02d71956 5865static void __i915_update_gfx_val(struct drm_i915_private *dev_priv)
eb48eb00 5866{
5ed0bdf2 5867 u64 now, diff, diffms;
eb48eb00
DV
5868 u32 count;
5869
02d71956 5870 assert_spin_locked(&mchdev_lock);
eb48eb00 5871
5ed0bdf2
TG
5872 now = ktime_get_raw_ns();
5873 diffms = now - dev_priv->ips.last_time2;
5874 do_div(diffms, NSEC_PER_MSEC);
eb48eb00
DV
5875
5876 /* Don't divide by 0 */
eb48eb00
DV
5877 if (!diffms)
5878 return;
5879
5880 count = I915_READ(GFXEC);
5881
20e4d407
DV
5882 if (count < dev_priv->ips.last_count2) {
5883 diff = ~0UL - dev_priv->ips.last_count2;
eb48eb00
DV
5884 diff += count;
5885 } else {
20e4d407 5886 diff = count - dev_priv->ips.last_count2;
eb48eb00
DV
5887 }
5888
20e4d407
DV
5889 dev_priv->ips.last_count2 = count;
5890 dev_priv->ips.last_time2 = now;
eb48eb00
DV
5891
5892 /* More magic constants... */
5893 diff = diff * 1181;
5894 diff = div_u64(diff, diffms * 10);
20e4d407 5895 dev_priv->ips.gfx_power = diff;
eb48eb00
DV
5896}
5897
02d71956
DV
5898void i915_update_gfx_val(struct drm_i915_private *dev_priv)
5899{
3d13ef2e
DL
5900 struct drm_device *dev = dev_priv->dev;
5901
5902 if (INTEL_INFO(dev)->gen != 5)
02d71956
DV
5903 return;
5904
9270388e 5905 spin_lock_irq(&mchdev_lock);
02d71956
DV
5906
5907 __i915_update_gfx_val(dev_priv);
5908
9270388e 5909 spin_unlock_irq(&mchdev_lock);
02d71956
DV
5910}
5911
f531dcb2 5912static unsigned long __i915_gfx_val(struct drm_i915_private *dev_priv)
eb48eb00
DV
5913{
5914 unsigned long t, corr, state1, corr2, state2;
5915 u32 pxvid, ext_v;
5916
02d71956
DV
5917 assert_spin_locked(&mchdev_lock);
5918
616847e7 5919 pxvid = I915_READ(PXVFREQ(dev_priv->rps.cur_freq));
eb48eb00
DV
5920 pxvid = (pxvid >> 24) & 0x7f;
5921 ext_v = pvid_to_extvid(dev_priv, pxvid);
5922
5923 state1 = ext_v;
5924
5925 t = i915_mch_val(dev_priv);
5926
5927 /* Revel in the empirically derived constants */
5928
5929 /* Correction factor in 1/100000 units */
5930 if (t > 80)
5931 corr = ((t * 2349) + 135940);
5932 else if (t >= 50)
5933 corr = ((t * 964) + 29317);
5934 else /* < 50 */
5935 corr = ((t * 301) + 1004);
5936
5937 corr = corr * ((150142 * state1) / 10000 - 78642);
5938 corr /= 100000;
20e4d407 5939 corr2 = (corr * dev_priv->ips.corr);
eb48eb00
DV
5940
5941 state2 = (corr2 * state1) / 10000;
5942 state2 /= 100; /* convert to mW */
5943
02d71956 5944 __i915_update_gfx_val(dev_priv);
eb48eb00 5945
20e4d407 5946 return dev_priv->ips.gfx_power + state2;
eb48eb00
DV
5947}
5948
f531dcb2
CW
5949unsigned long i915_gfx_val(struct drm_i915_private *dev_priv)
5950{
3d13ef2e 5951 struct drm_device *dev = dev_priv->dev;
f531dcb2
CW
5952 unsigned long val;
5953
3d13ef2e 5954 if (INTEL_INFO(dev)->gen != 5)
f531dcb2
CW
5955 return 0;
5956
5957 spin_lock_irq(&mchdev_lock);
5958
5959 val = __i915_gfx_val(dev_priv);
5960
5961 spin_unlock_irq(&mchdev_lock);
5962
5963 return val;
5964}
5965
eb48eb00
DV
5966/**
5967 * i915_read_mch_val - return value for IPS use
5968 *
5969 * Calculate and return a value for the IPS driver to use when deciding whether
5970 * we have thermal and power headroom to increase CPU or GPU power budget.
5971 */
5972unsigned long i915_read_mch_val(void)
5973{
5974 struct drm_i915_private *dev_priv;
5975 unsigned long chipset_val, graphics_val, ret = 0;
5976
9270388e 5977 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
5978 if (!i915_mch_dev)
5979 goto out_unlock;
5980 dev_priv = i915_mch_dev;
5981
f531dcb2
CW
5982 chipset_val = __i915_chipset_val(dev_priv);
5983 graphics_val = __i915_gfx_val(dev_priv);
eb48eb00
DV
5984
5985 ret = chipset_val + graphics_val;
5986
5987out_unlock:
9270388e 5988 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
5989
5990 return ret;
5991}
5992EXPORT_SYMBOL_GPL(i915_read_mch_val);
5993
5994/**
5995 * i915_gpu_raise - raise GPU frequency limit
5996 *
5997 * Raise the limit; IPS indicates we have thermal headroom.
5998 */
5999bool i915_gpu_raise(void)
6000{
6001 struct drm_i915_private *dev_priv;
6002 bool ret = true;
6003
9270388e 6004 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
6005 if (!i915_mch_dev) {
6006 ret = false;
6007 goto out_unlock;
6008 }
6009 dev_priv = i915_mch_dev;
6010
20e4d407
DV
6011 if (dev_priv->ips.max_delay > dev_priv->ips.fmax)
6012 dev_priv->ips.max_delay--;
eb48eb00
DV
6013
6014out_unlock:
9270388e 6015 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
6016
6017 return ret;
6018}
6019EXPORT_SYMBOL_GPL(i915_gpu_raise);
6020
6021/**
6022 * i915_gpu_lower - lower GPU frequency limit
6023 *
6024 * IPS indicates we're close to a thermal limit, so throttle back the GPU
6025 * frequency maximum.
6026 */
6027bool i915_gpu_lower(void)
6028{
6029 struct drm_i915_private *dev_priv;
6030 bool ret = true;
6031
9270388e 6032 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
6033 if (!i915_mch_dev) {
6034 ret = false;
6035 goto out_unlock;
6036 }
6037 dev_priv = i915_mch_dev;
6038
20e4d407
DV
6039 if (dev_priv->ips.max_delay < dev_priv->ips.min_delay)
6040 dev_priv->ips.max_delay++;
eb48eb00
DV
6041
6042out_unlock:
9270388e 6043 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
6044
6045 return ret;
6046}
6047EXPORT_SYMBOL_GPL(i915_gpu_lower);
6048
6049/**
6050 * i915_gpu_busy - indicate GPU business to IPS
6051 *
6052 * Tell the IPS driver whether or not the GPU is busy.
6053 */
6054bool i915_gpu_busy(void)
6055{
6056 struct drm_i915_private *dev_priv;
e2f80391 6057 struct intel_engine_cs *engine;
eb48eb00
DV
6058 bool ret = false;
6059
9270388e 6060 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
6061 if (!i915_mch_dev)
6062 goto out_unlock;
6063 dev_priv = i915_mch_dev;
6064
b4ac5afc 6065 for_each_engine(engine, dev_priv)
e2f80391 6066 ret |= !list_empty(&engine->request_list);
eb48eb00
DV
6067
6068out_unlock:
9270388e 6069 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
6070
6071 return ret;
6072}
6073EXPORT_SYMBOL_GPL(i915_gpu_busy);
6074
6075/**
6076 * i915_gpu_turbo_disable - disable graphics turbo
6077 *
6078 * Disable graphics turbo by resetting the max frequency and setting the
6079 * current frequency to the default.
6080 */
6081bool i915_gpu_turbo_disable(void)
6082{
6083 struct drm_i915_private *dev_priv;
6084 bool ret = true;
6085
9270388e 6086 spin_lock_irq(&mchdev_lock);
eb48eb00
DV
6087 if (!i915_mch_dev) {
6088 ret = false;
6089 goto out_unlock;
6090 }
6091 dev_priv = i915_mch_dev;
6092
20e4d407 6093 dev_priv->ips.max_delay = dev_priv->ips.fstart;
eb48eb00 6094
91d14251 6095 if (!ironlake_set_drps(dev_priv, dev_priv->ips.fstart))
eb48eb00
DV
6096 ret = false;
6097
6098out_unlock:
9270388e 6099 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
6100
6101 return ret;
6102}
6103EXPORT_SYMBOL_GPL(i915_gpu_turbo_disable);
6104
6105/**
6106 * Tells the intel_ips driver that the i915 driver is now loaded, if
6107 * IPS got loaded first.
6108 *
6109 * This awkward dance is so that neither module has to depend on the
6110 * other in order for IPS to do the appropriate communication of
6111 * GPU turbo limits to i915.
6112 */
6113static void
6114ips_ping_for_i915_load(void)
6115{
6116 void (*link)(void);
6117
6118 link = symbol_get(ips_link_to_i915_driver);
6119 if (link) {
6120 link();
6121 symbol_put(ips_link_to_i915_driver);
6122 }
6123}
6124
6125void intel_gpu_ips_init(struct drm_i915_private *dev_priv)
6126{
02d71956
DV
6127 /* We only register the i915 ips part with intel-ips once everything is
6128 * set up, to avoid intel-ips sneaking in and reading bogus values. */
9270388e 6129 spin_lock_irq(&mchdev_lock);
eb48eb00 6130 i915_mch_dev = dev_priv;
9270388e 6131 spin_unlock_irq(&mchdev_lock);
eb48eb00
DV
6132
6133 ips_ping_for_i915_load();
6134}
6135
6136void intel_gpu_ips_teardown(void)
6137{
9270388e 6138 spin_lock_irq(&mchdev_lock);
eb48eb00 6139 i915_mch_dev = NULL;
9270388e 6140 spin_unlock_irq(&mchdev_lock);
eb48eb00 6141}
76c3552f 6142
8090c6b9 6143static void intel_init_emon(struct drm_device *dev)
dde18883
ED
6144{
6145 struct drm_i915_private *dev_priv = dev->dev_private;
6146 u32 lcfuse;
6147 u8 pxw[16];
6148 int i;
6149
6150 /* Disable to program */
6151 I915_WRITE(ECR, 0);
6152 POSTING_READ(ECR);
6153
6154 /* Program energy weights for various events */
6155 I915_WRITE(SDEW, 0x15040d00);
6156 I915_WRITE(CSIEW0, 0x007f0000);
6157 I915_WRITE(CSIEW1, 0x1e220004);
6158 I915_WRITE(CSIEW2, 0x04000004);
6159
6160 for (i = 0; i < 5; i++)
616847e7 6161 I915_WRITE(PEW(i), 0);
dde18883 6162 for (i = 0; i < 3; i++)
616847e7 6163 I915_WRITE(DEW(i), 0);
dde18883
ED
6164
6165 /* Program P-state weights to account for frequency power adjustment */
6166 for (i = 0; i < 16; i++) {
616847e7 6167 u32 pxvidfreq = I915_READ(PXVFREQ(i));
dde18883
ED
6168 unsigned long freq = intel_pxfreq(pxvidfreq);
6169 unsigned long vid = (pxvidfreq & PXVFREQ_PX_MASK) >>
6170 PXVFREQ_PX_SHIFT;
6171 unsigned long val;
6172
6173 val = vid * vid;
6174 val *= (freq / 1000);
6175 val *= 255;
6176 val /= (127*127*900);
6177 if (val > 0xff)
6178 DRM_ERROR("bad pxval: %ld\n", val);
6179 pxw[i] = val;
6180 }
6181 /* Render standby states get 0 weight */
6182 pxw[14] = 0;
6183 pxw[15] = 0;
6184
6185 for (i = 0; i < 4; i++) {
6186 u32 val = (pxw[i*4] << 24) | (pxw[(i*4)+1] << 16) |
6187 (pxw[(i*4)+2] << 8) | (pxw[(i*4)+3]);
616847e7 6188 I915_WRITE(PXW(i), val);
dde18883
ED
6189 }
6190
6191 /* Adjust magic regs to magic values (more experimental results) */
6192 I915_WRITE(OGW0, 0);
6193 I915_WRITE(OGW1, 0);
6194 I915_WRITE(EG0, 0x00007f00);
6195 I915_WRITE(EG1, 0x0000000e);
6196 I915_WRITE(EG2, 0x000e0000);
6197 I915_WRITE(EG3, 0x68000300);
6198 I915_WRITE(EG4, 0x42000000);
6199 I915_WRITE(EG5, 0x00140031);
6200 I915_WRITE(EG6, 0);
6201 I915_WRITE(EG7, 0);
6202
6203 for (i = 0; i < 8; i++)
616847e7 6204 I915_WRITE(PXWL(i), 0);
dde18883
ED
6205
6206 /* Enable PMON + select events */
6207 I915_WRITE(ECR, 0x80000019);
6208
6209 lcfuse = I915_READ(LCFUSE02);
6210
20e4d407 6211 dev_priv->ips.corr = (lcfuse & LCFUSE_HIV_MASK);
dde18883
ED
6212}
6213
ae48434c
ID
6214void intel_init_gt_powersave(struct drm_device *dev)
6215{
b268c699
ID
6216 struct drm_i915_private *dev_priv = dev->dev_private;
6217
b268c699
ID
6218 /*
6219 * RPM depends on RC6 to save restore the GT HW context, so make RC6 a
6220 * requirement.
6221 */
6222 if (!i915.enable_rc6) {
6223 DRM_INFO("RC6 disabled, disabling runtime PM support\n");
6224 intel_runtime_pm_get(dev_priv);
6225 }
e6069ca8 6226
38807746
D
6227 if (IS_CHERRYVIEW(dev))
6228 cherryview_init_gt_powersave(dev);
6229 else if (IS_VALLEYVIEW(dev))
4e80519e 6230 valleyview_init_gt_powersave(dev);
ae48434c
ID
6231}
6232
6233void intel_cleanup_gt_powersave(struct drm_device *dev)
6234{
b268c699
ID
6235 struct drm_i915_private *dev_priv = dev->dev_private;
6236
38807746
D
6237 if (IS_CHERRYVIEW(dev))
6238 return;
6239 else if (IS_VALLEYVIEW(dev))
4e80519e 6240 valleyview_cleanup_gt_powersave(dev);
b268c699
ID
6241
6242 if (!i915.enable_rc6)
6243 intel_runtime_pm_put(dev_priv);
ae48434c
ID
6244}
6245
91d14251 6246static void gen6_suspend_rps(struct drm_i915_private *dev_priv)
dbea3cea 6247{
dbea3cea
ID
6248 flush_delayed_work(&dev_priv->rps.delayed_resume_work);
6249
91d14251 6250 gen6_disable_rps_interrupts(dev_priv);
dbea3cea
ID
6251}
6252
156c7ca0
JB
6253/**
6254 * intel_suspend_gt_powersave - suspend PM work and helper threads
6255 * @dev: drm device
6256 *
6257 * We don't want to disable RC6 or other features here, we just want
6258 * to make sure any work we've queued has finished and won't bother
6259 * us while we're suspended.
6260 */
6261void intel_suspend_gt_powersave(struct drm_device *dev)
6262{
6263 struct drm_i915_private *dev_priv = dev->dev_private;
6264
91d14251 6265 if (INTEL_GEN(dev_priv) < 6)
d4d70aa5
ID
6266 return;
6267
91d14251 6268 gen6_suspend_rps(dev_priv);
b47adc17
D
6269
6270 /* Force GPU to min freq during suspend */
6271 gen6_rps_idle(dev_priv);
156c7ca0
JB
6272}
6273
8090c6b9
DV
6274void intel_disable_gt_powersave(struct drm_device *dev)
6275{
1a01ab3b
JB
6276 struct drm_i915_private *dev_priv = dev->dev_private;
6277
930ebb46 6278 if (IS_IRONLAKE_M(dev)) {
91d14251 6279 ironlake_disable_drps(dev_priv);
38807746 6280 } else if (INTEL_INFO(dev)->gen >= 6) {
10d8d366 6281 intel_suspend_gt_powersave(dev);
e494837a 6282
4fc688ce 6283 mutex_lock(&dev_priv->rps.hw_lock);
2030d684
AG
6284 if (INTEL_INFO(dev)->gen >= 9) {
6285 gen9_disable_rc6(dev);
20e49366 6286 gen9_disable_rps(dev);
2030d684 6287 } else if (IS_CHERRYVIEW(dev))
38807746
D
6288 cherryview_disable_rps(dev);
6289 else if (IS_VALLEYVIEW(dev))
d20d4f0c
JB
6290 valleyview_disable_rps(dev);
6291 else
6292 gen6_disable_rps(dev);
e534770a 6293
c0951f0c 6294 dev_priv->rps.enabled = false;
4fc688ce 6295 mutex_unlock(&dev_priv->rps.hw_lock);
930ebb46 6296 }
8090c6b9
DV
6297}
6298
1a01ab3b
JB
6299static void intel_gen6_powersave_work(struct work_struct *work)
6300{
6301 struct drm_i915_private *dev_priv =
6302 container_of(work, struct drm_i915_private,
6303 rps.delayed_resume_work.work);
6304 struct drm_device *dev = dev_priv->dev;
6305
4fc688ce 6306 mutex_lock(&dev_priv->rps.hw_lock);
0a073b84 6307
4c2a8897 6308 gen6_reset_rps_interrupts(dev);
3cc134e3 6309
38807746
D
6310 if (IS_CHERRYVIEW(dev)) {
6311 cherryview_enable_rps(dev);
6312 } else if (IS_VALLEYVIEW(dev)) {
0a073b84 6313 valleyview_enable_rps(dev);
20e49366 6314 } else if (INTEL_INFO(dev)->gen >= 9) {
b6fef0ef 6315 gen9_enable_rc6(dev);
20e49366 6316 gen9_enable_rps(dev);
ef11bdb3 6317 if (IS_SKYLAKE(dev) || IS_KABYLAKE(dev))
cc017fb4 6318 __gen6_update_ring_freq(dev);
6edee7f3
BW
6319 } else if (IS_BROADWELL(dev)) {
6320 gen8_enable_rps(dev);
c2bc2fc5 6321 __gen6_update_ring_freq(dev);
0a073b84
JB
6322 } else {
6323 gen6_enable_rps(dev);
c2bc2fc5 6324 __gen6_update_ring_freq(dev);
0a073b84 6325 }
aed242ff
CW
6326
6327 WARN_ON(dev_priv->rps.max_freq < dev_priv->rps.min_freq);
6328 WARN_ON(dev_priv->rps.idle_freq > dev_priv->rps.max_freq);
6329
6330 WARN_ON(dev_priv->rps.efficient_freq < dev_priv->rps.min_freq);
6331 WARN_ON(dev_priv->rps.efficient_freq > dev_priv->rps.max_freq);
6332
c0951f0c 6333 dev_priv->rps.enabled = true;
3cc134e3 6334
91d14251 6335 gen6_enable_rps_interrupts(dev_priv);
3cc134e3 6336
4fc688ce 6337 mutex_unlock(&dev_priv->rps.hw_lock);
c6df39b5
ID
6338
6339 intel_runtime_pm_put(dev_priv);
1a01ab3b
JB
6340}
6341
8090c6b9
DV
6342void intel_enable_gt_powersave(struct drm_device *dev)
6343{
1a01ab3b
JB
6344 struct drm_i915_private *dev_priv = dev->dev_private;
6345
f61018b1
YZ
6346 /* Powersaving is controlled by the host when inside a VM */
6347 if (intel_vgpu_active(dev))
6348 return;
6349
8090c6b9 6350 if (IS_IRONLAKE_M(dev)) {
91d14251 6351 ironlake_enable_drps(dev_priv);
84f1b20f 6352 mutex_lock(&dev->struct_mutex);
8090c6b9 6353 intel_init_emon(dev);
dc1d0136 6354 mutex_unlock(&dev->struct_mutex);
38807746 6355 } else if (INTEL_INFO(dev)->gen >= 6) {
1a01ab3b
JB
6356 /*
6357 * PCU communication is slow and this doesn't need to be
6358 * done at any specific time, so do this out of our fast path
6359 * to make resume and init faster.
c6df39b5
ID
6360 *
6361 * We depend on the HW RC6 power context save/restore
6362 * mechanism when entering D3 through runtime PM suspend. So
6363 * disable RPM until RPS/RC6 is properly setup. We can only
6364 * get here via the driver load/system resume/runtime resume
6365 * paths, so the _noresume version is enough (and in case of
6366 * runtime resume it's necessary).
1a01ab3b 6367 */
c6df39b5
ID
6368 if (schedule_delayed_work(&dev_priv->rps.delayed_resume_work,
6369 round_jiffies_up_relative(HZ)))
6370 intel_runtime_pm_get_noresume(dev_priv);
8090c6b9
DV
6371 }
6372}
6373
c6df39b5
ID
6374void intel_reset_gt_powersave(struct drm_device *dev)
6375{
6376 struct drm_i915_private *dev_priv = dev->dev_private;
6377
dbea3cea
ID
6378 if (INTEL_INFO(dev)->gen < 6)
6379 return;
6380
91d14251 6381 gen6_suspend_rps(dev_priv);
c6df39b5 6382 dev_priv->rps.enabled = false;
c6df39b5
ID
6383}
6384
3107bd48
DV
6385static void ibx_init_clock_gating(struct drm_device *dev)
6386{
6387 struct drm_i915_private *dev_priv = dev->dev_private;
6388
6389 /*
6390 * On Ibex Peak and Cougar Point, we need to disable clock
6391 * gating for the panel power sequencer or it will fail to
6392 * start up when no ports are active.
6393 */
6394 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE);
6395}
6396
0e088b8f
VS
6397static void g4x_disable_trickle_feed(struct drm_device *dev)
6398{
6399 struct drm_i915_private *dev_priv = dev->dev_private;
b12ce1d8 6400 enum pipe pipe;
0e088b8f 6401
055e393f 6402 for_each_pipe(dev_priv, pipe) {
0e088b8f
VS
6403 I915_WRITE(DSPCNTR(pipe),
6404 I915_READ(DSPCNTR(pipe)) |
6405 DISPPLANE_TRICKLE_FEED_DISABLE);
b12ce1d8
VS
6406
6407 I915_WRITE(DSPSURF(pipe), I915_READ(DSPSURF(pipe)));
6408 POSTING_READ(DSPSURF(pipe));
0e088b8f
VS
6409 }
6410}
6411
017636cc
VS
6412static void ilk_init_lp_watermarks(struct drm_device *dev)
6413{
6414 struct drm_i915_private *dev_priv = dev->dev_private;
6415
6416 I915_WRITE(WM3_LP_ILK, I915_READ(WM3_LP_ILK) & ~WM1_LP_SR_EN);
6417 I915_WRITE(WM2_LP_ILK, I915_READ(WM2_LP_ILK) & ~WM1_LP_SR_EN);
6418 I915_WRITE(WM1_LP_ILK, I915_READ(WM1_LP_ILK) & ~WM1_LP_SR_EN);
6419
6420 /*
6421 * Don't touch WM1S_LP_EN here.
6422 * Doing so could cause underruns.
6423 */
6424}
6425
1fa61106 6426static void ironlake_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6427{
6428 struct drm_i915_private *dev_priv = dev->dev_private;
231e54f6 6429 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6f1d69b0 6430
f1e8fa56
DL
6431 /*
6432 * Required for FBC
6433 * WaFbcDisableDpfcClockGating:ilk
6434 */
4d47e4f5
DL
6435 dspclk_gate |= ILK_DPFCRUNIT_CLOCK_GATE_DISABLE |
6436 ILK_DPFCUNIT_CLOCK_GATE_DISABLE |
6437 ILK_DPFDUNIT_CLOCK_GATE_ENABLE;
6f1d69b0
ED
6438
6439 I915_WRITE(PCH_3DCGDIS0,
6440 MARIUNIT_CLOCK_GATE_DISABLE |
6441 SVSMUNIT_CLOCK_GATE_DISABLE);
6442 I915_WRITE(PCH_3DCGDIS1,
6443 VFMUNIT_CLOCK_GATE_DISABLE);
6444
6f1d69b0
ED
6445 /*
6446 * According to the spec the following bits should be set in
6447 * order to enable memory self-refresh
6448 * The bit 22/21 of 0x42004
6449 * The bit 5 of 0x42020
6450 * The bit 15 of 0x45000
6451 */
6452 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6453 (I915_READ(ILK_DISPLAY_CHICKEN2) |
6454 ILK_DPARB_GATE | ILK_VSDPFD_FULL));
4d47e4f5 6455 dspclk_gate |= ILK_DPARBUNIT_CLOCK_GATE_ENABLE;
6f1d69b0
ED
6456 I915_WRITE(DISP_ARB_CTL,
6457 (I915_READ(DISP_ARB_CTL) |
6458 DISP_FBC_WM_DIS));
017636cc
VS
6459
6460 ilk_init_lp_watermarks(dev);
6f1d69b0
ED
6461
6462 /*
6463 * Based on the document from hardware guys the following bits
6464 * should be set unconditionally in order to enable FBC.
6465 * The bit 22 of 0x42000
6466 * The bit 22 of 0x42004
6467 * The bit 7,8,9 of 0x42020.
6468 */
6469 if (IS_IRONLAKE_M(dev)) {
4bb35334 6470 /* WaFbcAsynchFlipDisableFbcQueue:ilk */
6f1d69b0
ED
6471 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6472 I915_READ(ILK_DISPLAY_CHICKEN1) |
6473 ILK_FBCQ_DIS);
6474 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6475 I915_READ(ILK_DISPLAY_CHICKEN2) |
6476 ILK_DPARB_GATE);
6f1d69b0
ED
6477 }
6478
4d47e4f5
DL
6479 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6480
6f1d69b0
ED
6481 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6482 I915_READ(ILK_DISPLAY_CHICKEN2) |
6483 ILK_ELPIN_409_SELECT);
6484 I915_WRITE(_3D_CHICKEN2,
6485 _3D_CHICKEN2_WM_READ_PIPELINED << 16 |
6486 _3D_CHICKEN2_WM_READ_PIPELINED);
4358a374 6487
ecdb4eb7 6488 /* WaDisableRenderCachePipelinedFlush:ilk */
4358a374
DV
6489 I915_WRITE(CACHE_MODE_0,
6490 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
3107bd48 6491
4e04632e
AG
6492 /* WaDisable_RenderCache_OperationalFlush:ilk */
6493 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6494
0e088b8f 6495 g4x_disable_trickle_feed(dev);
bdad2b2f 6496
3107bd48
DV
6497 ibx_init_clock_gating(dev);
6498}
6499
6500static void cpt_init_clock_gating(struct drm_device *dev)
6501{
6502 struct drm_i915_private *dev_priv = dev->dev_private;
6503 int pipe;
3f704fa2 6504 uint32_t val;
3107bd48
DV
6505
6506 /*
6507 * On Ibex Peak and Cougar Point, we need to disable clock
6508 * gating for the panel power sequencer or it will fail to
6509 * start up when no ports are active.
6510 */
cd664078
JB
6511 I915_WRITE(SOUTH_DSPCLK_GATE_D, PCH_DPLSUNIT_CLOCK_GATE_DISABLE |
6512 PCH_DPLUNIT_CLOCK_GATE_DISABLE |
6513 PCH_CPUNIT_CLOCK_GATE_DISABLE);
3107bd48
DV
6514 I915_WRITE(SOUTH_CHICKEN2, I915_READ(SOUTH_CHICKEN2) |
6515 DPLS_EDP_PPS_FIX_DIS);
335c07b7
TI
6516 /* The below fixes the weird display corruption, a few pixels shifted
6517 * downward, on (only) LVDS of some HP laptops with IVY.
6518 */
055e393f 6519 for_each_pipe(dev_priv, pipe) {
dc4bd2d1
PZ
6520 val = I915_READ(TRANS_CHICKEN2(pipe));
6521 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
6522 val &= ~TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
41aa3448 6523 if (dev_priv->vbt.fdi_rx_polarity_inverted)
3f704fa2 6524 val |= TRANS_CHICKEN2_FDI_POLARITY_REVERSED;
dc4bd2d1
PZ
6525 val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
6526 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_COUNTER;
6527 val &= ~TRANS_CHICKEN2_DISABLE_DEEP_COLOR_MODESWITCH;
3f704fa2
PZ
6528 I915_WRITE(TRANS_CHICKEN2(pipe), val);
6529 }
3107bd48 6530 /* WADP0ClockGatingDisable */
055e393f 6531 for_each_pipe(dev_priv, pipe) {
3107bd48
DV
6532 I915_WRITE(TRANS_CHICKEN1(pipe),
6533 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
6534 }
6f1d69b0
ED
6535}
6536
1d7aaa0c
DV
6537static void gen6_check_mch_setup(struct drm_device *dev)
6538{
6539 struct drm_i915_private *dev_priv = dev->dev_private;
6540 uint32_t tmp;
6541
6542 tmp = I915_READ(MCH_SSKPD);
df662a28
DV
6543 if ((tmp & MCH_SSKPD_WM0_MASK) != MCH_SSKPD_WM0_VAL)
6544 DRM_DEBUG_KMS("Wrong MCH_SSKPD value: 0x%08x This can cause underruns.\n",
6545 tmp);
1d7aaa0c
DV
6546}
6547
1fa61106 6548static void gen6_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6549{
6550 struct drm_i915_private *dev_priv = dev->dev_private;
231e54f6 6551 uint32_t dspclk_gate = ILK_VRHUNIT_CLOCK_GATE_DISABLE;
6f1d69b0 6552
231e54f6 6553 I915_WRITE(ILK_DSPCLK_GATE_D, dspclk_gate);
6f1d69b0
ED
6554
6555 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6556 I915_READ(ILK_DISPLAY_CHICKEN2) |
6557 ILK_ELPIN_409_SELECT);
6558
ecdb4eb7 6559 /* WaDisableHiZPlanesWhenMSAAEnabled:snb */
4283908e
DV
6560 I915_WRITE(_3D_CHICKEN,
6561 _MASKED_BIT_ENABLE(_3D_CHICKEN_HIZ_PLANE_DISABLE_MSAA_4X_SNB));
6562
4e04632e
AG
6563 /* WaDisable_RenderCache_OperationalFlush:snb */
6564 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6565
8d85d272
VS
6566 /*
6567 * BSpec recoomends 8x4 when MSAA is used,
6568 * however in practice 16x4 seems fastest.
c5c98a58
VS
6569 *
6570 * Note that PS/WM thread counts depend on the WIZ hashing
6571 * disable bit, which we don't touch here, but it's good
6572 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
8d85d272
VS
6573 */
6574 I915_WRITE(GEN6_GT_MODE,
98533251 6575 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
8d85d272 6576
017636cc 6577 ilk_init_lp_watermarks(dev);
6f1d69b0 6578
6f1d69b0 6579 I915_WRITE(CACHE_MODE_0,
50743298 6580 _MASKED_BIT_DISABLE(CM0_STC_EVICT_DISABLE_LRA_SNB));
6f1d69b0
ED
6581
6582 I915_WRITE(GEN6_UCGCTL1,
6583 I915_READ(GEN6_UCGCTL1) |
6584 GEN6_BLBUNIT_CLOCK_GATE_DISABLE |
6585 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
6586
6587 /* According to the BSpec vol1g, bit 12 (RCPBUNIT) clock
6588 * gating disable must be set. Failure to set it results in
6589 * flickering pixels due to Z write ordering failures after
6590 * some amount of runtime in the Mesa "fire" demo, and Unigine
6591 * Sanctuary and Tropics, and apparently anything else with
6592 * alpha test or pixel discard.
6593 *
6594 * According to the spec, bit 11 (RCCUNIT) must also be set,
6595 * but we didn't debug actual testcases to find it out.
0f846f81 6596 *
ef59318c
VS
6597 * WaDisableRCCUnitClockGating:snb
6598 * WaDisableRCPBUnitClockGating:snb
6f1d69b0
ED
6599 */
6600 I915_WRITE(GEN6_UCGCTL2,
6601 GEN6_RCPBUNIT_CLOCK_GATE_DISABLE |
6602 GEN6_RCCUNIT_CLOCK_GATE_DISABLE);
6603
5eb146dd 6604 /* WaStripsFansDisableFastClipPerformanceFix:snb */
743b57d8
VS
6605 I915_WRITE(_3D_CHICKEN3,
6606 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_FASTCLIP_CULL));
6f1d69b0 6607
e927ecde
VS
6608 /*
6609 * Bspec says:
6610 * "This bit must be set if 3DSTATE_CLIP clip mode is set to normal and
6611 * 3DSTATE_SF number of SF output attributes is more than 16."
6612 */
6613 I915_WRITE(_3D_CHICKEN3,
6614 _MASKED_BIT_ENABLE(_3D_CHICKEN3_SF_DISABLE_PIPELINED_ATTR_FETCH));
6615
6f1d69b0
ED
6616 /*
6617 * According to the spec the following bits should be
6618 * set in order to enable memory self-refresh and fbc:
6619 * The bit21 and bit22 of 0x42000
6620 * The bit21 and bit22 of 0x42004
6621 * The bit5 and bit7 of 0x42020
6622 * The bit14 of 0x70180
6623 * The bit14 of 0x71180
4bb35334
DL
6624 *
6625 * WaFbcAsynchFlipDisableFbcQueue:snb
6f1d69b0
ED
6626 */
6627 I915_WRITE(ILK_DISPLAY_CHICKEN1,
6628 I915_READ(ILK_DISPLAY_CHICKEN1) |
6629 ILK_FBCQ_DIS | ILK_PABSTRETCH_DIS);
6630 I915_WRITE(ILK_DISPLAY_CHICKEN2,
6631 I915_READ(ILK_DISPLAY_CHICKEN2) |
6632 ILK_DPARB_GATE | ILK_VSDPFD_FULL);
231e54f6
DL
6633 I915_WRITE(ILK_DSPCLK_GATE_D,
6634 I915_READ(ILK_DSPCLK_GATE_D) |
6635 ILK_DPARBUNIT_CLOCK_GATE_ENABLE |
6636 ILK_DPFDUNIT_CLOCK_GATE_ENABLE);
6f1d69b0 6637
0e088b8f 6638 g4x_disable_trickle_feed(dev);
f8f2ac9a 6639
3107bd48 6640 cpt_init_clock_gating(dev);
1d7aaa0c
DV
6641
6642 gen6_check_mch_setup(dev);
6f1d69b0
ED
6643}
6644
6645static void gen7_setup_fixed_func_scheduler(struct drm_i915_private *dev_priv)
6646{
6647 uint32_t reg = I915_READ(GEN7_FF_THREAD_MODE);
6648
3aad9059 6649 /*
46680e0a 6650 * WaVSThreadDispatchOverride:ivb,vlv
3aad9059
VS
6651 *
6652 * This actually overrides the dispatch
6653 * mode for all thread types.
6654 */
6f1d69b0
ED
6655 reg &= ~GEN7_FF_SCHED_MASK;
6656 reg |= GEN7_FF_TS_SCHED_HW;
6657 reg |= GEN7_FF_VS_SCHED_HW;
6658 reg |= GEN7_FF_DS_SCHED_HW;
6659
6660 I915_WRITE(GEN7_FF_THREAD_MODE, reg);
6661}
6662
17a303ec
PZ
6663static void lpt_init_clock_gating(struct drm_device *dev)
6664{
6665 struct drm_i915_private *dev_priv = dev->dev_private;
6666
6667 /*
6668 * TODO: this bit should only be enabled when really needed, then
6669 * disabled when not needed anymore in order to save power.
6670 */
c2699524 6671 if (HAS_PCH_LPT_LP(dev))
17a303ec
PZ
6672 I915_WRITE(SOUTH_DSPCLK_GATE_D,
6673 I915_READ(SOUTH_DSPCLK_GATE_D) |
6674 PCH_LP_PARTITION_LEVEL_DISABLE);
0a790cdb
PZ
6675
6676 /* WADPOClockGatingDisable:hsw */
36c0d0cf
VS
6677 I915_WRITE(TRANS_CHICKEN1(PIPE_A),
6678 I915_READ(TRANS_CHICKEN1(PIPE_A)) |
0a790cdb 6679 TRANS_CHICKEN1_DP0UNIT_GC_DISABLE);
17a303ec
PZ
6680}
6681
7d708ee4
ID
6682static void lpt_suspend_hw(struct drm_device *dev)
6683{
6684 struct drm_i915_private *dev_priv = dev->dev_private;
6685
c2699524 6686 if (HAS_PCH_LPT_LP(dev)) {
7d708ee4
ID
6687 uint32_t val = I915_READ(SOUTH_DSPCLK_GATE_D);
6688
6689 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
6690 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
6691 }
6692}
6693
450174fe
ID
6694static void gen8_set_l3sqc_credits(struct drm_i915_private *dev_priv,
6695 int general_prio_credits,
6696 int high_prio_credits)
6697{
6698 u32 misccpctl;
6699
6700 /* WaTempDisableDOPClkGating:bdw */
6701 misccpctl = I915_READ(GEN7_MISCCPCTL);
6702 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
6703
6704 I915_WRITE(GEN8_L3SQCREG1,
6705 L3_GENERAL_PRIO_CREDITS(general_prio_credits) |
6706 L3_HIGH_PRIO_CREDITS(high_prio_credits));
6707
6708 /*
6709 * Wait at least 100 clocks before re-enabling clock gating.
6710 * See the definition of L3SQCREG1 in BSpec.
6711 */
6712 POSTING_READ(GEN8_L3SQCREG1);
6713 udelay(1);
6714 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
6715}
6716
47c2bd97 6717static void broadwell_init_clock_gating(struct drm_device *dev)
1020a5c2
BW
6718{
6719 struct drm_i915_private *dev_priv = dev->dev_private;
07d27e20 6720 enum pipe pipe;
1020a5c2 6721
7ad0dbab 6722 ilk_init_lp_watermarks(dev);
50ed5fbd 6723
ab57fff1 6724 /* WaSwitchSolVfFArbitrationPriority:bdw */
50ed5fbd 6725 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
fe4ab3ce 6726
ab57fff1 6727 /* WaPsrDPAMaskVBlankInSRD:bdw */
fe4ab3ce
BW
6728 I915_WRITE(CHICKEN_PAR1_1,
6729 I915_READ(CHICKEN_PAR1_1) | DPA_MASK_VBLANK_SRD);
6730
ab57fff1 6731 /* WaPsrDPRSUnmaskVBlankInSRD:bdw */
055e393f 6732 for_each_pipe(dev_priv, pipe) {
07d27e20 6733 I915_WRITE(CHICKEN_PIPESL_1(pipe),
c7c65622 6734 I915_READ(CHICKEN_PIPESL_1(pipe)) |
8f670bb1 6735 BDW_DPRS_MASK_VBLANK_SRD);
fe4ab3ce 6736 }
63801f21 6737
ab57fff1
BW
6738 /* WaVSRefCountFullforceMissDisable:bdw */
6739 /* WaDSRefCountFullforceMissDisable:bdw */
6740 I915_WRITE(GEN7_FF_THREAD_MODE,
6741 I915_READ(GEN7_FF_THREAD_MODE) &
6742 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
36075a4c 6743
295e8bb7
VS
6744 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
6745 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
4f1ca9e9
VS
6746
6747 /* WaDisableSDEUnitClockGating:bdw */
6748 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
6749 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
5d708680 6750
450174fe
ID
6751 /* WaProgramL3SqcReg1Default:bdw */
6752 gen8_set_l3sqc_credits(dev_priv, 30, 2);
4d487cff 6753
6d50b065
VS
6754 /*
6755 * WaGttCachingOffByDefault:bdw
6756 * GTT cache may not work with big pages, so if those
6757 * are ever enabled GTT cache may need to be disabled.
6758 */
6759 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
6760
89d6b2b8 6761 lpt_init_clock_gating(dev);
1020a5c2
BW
6762}
6763
cad2a2d7
ED
6764static void haswell_init_clock_gating(struct drm_device *dev)
6765{
6766 struct drm_i915_private *dev_priv = dev->dev_private;
cad2a2d7 6767
017636cc 6768 ilk_init_lp_watermarks(dev);
cad2a2d7 6769
f3fc4884
FJ
6770 /* L3 caching of data atomics doesn't work -- disable it. */
6771 I915_WRITE(HSW_SCRATCH1, HSW_SCRATCH1_L3_DATA_ATOMICS_DISABLE);
6772 I915_WRITE(HSW_ROW_CHICKEN3,
6773 _MASKED_BIT_ENABLE(HSW_ROW_CHICKEN3_L3_GLOBAL_ATOMICS_DISABLE));
6774
ecdb4eb7 6775 /* This is required by WaCatErrorRejectionIssue:hsw */
cad2a2d7
ED
6776 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6777 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6778 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6779
e36ea7ff
VS
6780 /* WaVSRefCountFullforceMissDisable:hsw */
6781 I915_WRITE(GEN7_FF_THREAD_MODE,
6782 I915_READ(GEN7_FF_THREAD_MODE) & ~GEN7_FF_VS_REF_CNT_FFME);
cad2a2d7 6783
4e04632e
AG
6784 /* WaDisable_RenderCache_OperationalFlush:hsw */
6785 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6786
fe27c606
CW
6787 /* enable HiZ Raw Stall Optimization */
6788 I915_WRITE(CACHE_MODE_0_GEN7,
6789 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6790
ecdb4eb7 6791 /* WaDisable4x2SubspanOptimization:hsw */
cad2a2d7
ED
6792 I915_WRITE(CACHE_MODE_1,
6793 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
1544d9d5 6794
a12c4967
VS
6795 /*
6796 * BSpec recommends 8x4 when MSAA is used,
6797 * however in practice 16x4 seems fastest.
c5c98a58
VS
6798 *
6799 * Note that PS/WM thread counts depend on the WIZ hashing
6800 * disable bit, which we don't touch here, but it's good
6801 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
a12c4967
VS
6802 */
6803 I915_WRITE(GEN7_GT_MODE,
98533251 6804 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
a12c4967 6805
94411593
KG
6806 /* WaSampleCChickenBitEnable:hsw */
6807 I915_WRITE(HALF_SLICE_CHICKEN3,
6808 _MASKED_BIT_ENABLE(HSW_SAMPLE_C_PERFORMANCE));
6809
ecdb4eb7 6810 /* WaSwitchSolVfFArbitrationPriority:hsw */
e3dff585
BW
6811 I915_WRITE(GAM_ECOCHK, I915_READ(GAM_ECOCHK) | HSW_ECOCHK_ARB_PRIO_SOL);
6812
90a88643
PZ
6813 /* WaRsPkgCStateDisplayPMReq:hsw */
6814 I915_WRITE(CHICKEN_PAR1_1,
6815 I915_READ(CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
1544d9d5 6816
17a303ec 6817 lpt_init_clock_gating(dev);
cad2a2d7
ED
6818}
6819
1fa61106 6820static void ivybridge_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6821{
6822 struct drm_i915_private *dev_priv = dev->dev_private;
20848223 6823 uint32_t snpcr;
6f1d69b0 6824
017636cc 6825 ilk_init_lp_watermarks(dev);
6f1d69b0 6826
231e54f6 6827 I915_WRITE(ILK_DSPCLK_GATE_D, ILK_VRHUNIT_CLOCK_GATE_DISABLE);
6f1d69b0 6828
ecdb4eb7 6829 /* WaDisableEarlyCull:ivb */
87f8020e
JB
6830 I915_WRITE(_3D_CHICKEN3,
6831 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6832
ecdb4eb7 6833 /* WaDisableBackToBackFlipFix:ivb */
6f1d69b0
ED
6834 I915_WRITE(IVB_CHICKEN3,
6835 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6836 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6837
ecdb4eb7 6838 /* WaDisablePSDDualDispatchEnable:ivb */
12f3382b
JB
6839 if (IS_IVB_GT1(dev))
6840 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
6841 _MASKED_BIT_ENABLE(GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
12f3382b 6842
4e04632e
AG
6843 /* WaDisable_RenderCache_OperationalFlush:ivb */
6844 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6845
ecdb4eb7 6846 /* Apply the WaDisableRHWOOptimizationForRenderHang:ivb workaround. */
6f1d69b0
ED
6847 I915_WRITE(GEN7_COMMON_SLICE_CHICKEN1,
6848 GEN7_CSC1_RHWO_OPT_DISABLE_IN_RCC);
6849
ecdb4eb7 6850 /* WaApplyL3ControlAndL3ChickenMode:ivb */
6f1d69b0
ED
6851 I915_WRITE(GEN7_L3CNTLREG1,
6852 GEN7_WA_FOR_GEN7_L3_CONTROL);
6853 I915_WRITE(GEN7_L3_CHICKEN_MODE_REGISTER,
8ab43976
JB
6854 GEN7_WA_L3_CHICKEN_MODE);
6855 if (IS_IVB_GT1(dev))
6856 I915_WRITE(GEN7_ROW_CHICKEN2,
6857 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
412236c2
VS
6858 else {
6859 /* must write both registers */
6860 I915_WRITE(GEN7_ROW_CHICKEN2,
6861 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
8ab43976
JB
6862 I915_WRITE(GEN7_ROW_CHICKEN2_GT2,
6863 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
412236c2 6864 }
6f1d69b0 6865
ecdb4eb7 6866 /* WaForceL3Serialization:ivb */
61939d97
JB
6867 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6868 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6869
1b80a19a 6870 /*
0f846f81 6871 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
ecdb4eb7 6872 * This implements the WaDisableRCZUnitClockGating:ivb workaround.
0f846f81
JB
6873 */
6874 I915_WRITE(GEN6_UCGCTL2,
28acf3b2 6875 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
0f846f81 6876
ecdb4eb7 6877 /* This is required by WaCatErrorRejectionIssue:ivb */
6f1d69b0
ED
6878 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6879 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6880 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6881
0e088b8f 6882 g4x_disable_trickle_feed(dev);
6f1d69b0
ED
6883
6884 gen7_setup_fixed_func_scheduler(dev_priv);
97e1930f 6885
22721343
CW
6886 if (0) { /* causes HiZ corruption on ivb:gt1 */
6887 /* enable HiZ Raw Stall Optimization */
6888 I915_WRITE(CACHE_MODE_0_GEN7,
6889 _MASKED_BIT_DISABLE(HIZ_RAW_STALL_OPT_DISABLE));
6890 }
116f2b6d 6891
ecdb4eb7 6892 /* WaDisable4x2SubspanOptimization:ivb */
97e1930f
DV
6893 I915_WRITE(CACHE_MODE_1,
6894 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
20848223 6895
a607c1a4
VS
6896 /*
6897 * BSpec recommends 8x4 when MSAA is used,
6898 * however in practice 16x4 seems fastest.
c5c98a58
VS
6899 *
6900 * Note that PS/WM thread counts depend on the WIZ hashing
6901 * disable bit, which we don't touch here, but it's good
6902 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
a607c1a4
VS
6903 */
6904 I915_WRITE(GEN7_GT_MODE,
98533251 6905 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
a607c1a4 6906
20848223
BW
6907 snpcr = I915_READ(GEN6_MBCUNIT_SNPCR);
6908 snpcr &= ~GEN6_MBC_SNPCR_MASK;
6909 snpcr |= GEN6_MBC_SNPCR_MED;
6910 I915_WRITE(GEN6_MBCUNIT_SNPCR, snpcr);
3107bd48 6911
ab5c608b
BW
6912 if (!HAS_PCH_NOP(dev))
6913 cpt_init_clock_gating(dev);
1d7aaa0c
DV
6914
6915 gen6_check_mch_setup(dev);
6f1d69b0
ED
6916}
6917
1fa61106 6918static void valleyview_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
6919{
6920 struct drm_i915_private *dev_priv = dev->dev_private;
6f1d69b0 6921
ecdb4eb7 6922 /* WaDisableEarlyCull:vlv */
87f8020e
JB
6923 I915_WRITE(_3D_CHICKEN3,
6924 _MASKED_BIT_ENABLE(_3D_CHICKEN_SF_DISABLE_OBJEND_CULL));
6925
ecdb4eb7 6926 /* WaDisableBackToBackFlipFix:vlv */
6f1d69b0
ED
6927 I915_WRITE(IVB_CHICKEN3,
6928 CHICKEN3_DGMG_REQ_OUT_FIX_DISABLE |
6929 CHICKEN3_DGMG_DONE_FIX_DISABLE);
6930
fad7d36e 6931 /* WaPsdDispatchEnable:vlv */
ecdb4eb7 6932 /* WaDisablePSDDualDispatchEnable:vlv */
12f3382b 6933 I915_WRITE(GEN7_HALF_SLICE_CHICKEN1,
d3bc0303
JB
6934 _MASKED_BIT_ENABLE(GEN7_MAX_PS_THREAD_DEP |
6935 GEN7_PSD_SINGLE_PORT_DISPATCH_ENABLE));
12f3382b 6936
4e04632e
AG
6937 /* WaDisable_RenderCache_OperationalFlush:vlv */
6938 I915_WRITE(CACHE_MODE_0_GEN7, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6939
ecdb4eb7 6940 /* WaForceL3Serialization:vlv */
61939d97
JB
6941 I915_WRITE(GEN7_L3SQCREG4, I915_READ(GEN7_L3SQCREG4) &
6942 ~L3SQ_URB_READ_CAM_MATCH_DISABLE);
6943
ecdb4eb7 6944 /* WaDisableDopClockGating:vlv */
8ab43976
JB
6945 I915_WRITE(GEN7_ROW_CHICKEN2,
6946 _MASKED_BIT_ENABLE(DOP_CLOCK_GATING_DISABLE));
6947
ecdb4eb7 6948 /* This is required by WaCatErrorRejectionIssue:vlv */
6f1d69b0
ED
6949 I915_WRITE(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG,
6950 I915_READ(GEN7_SQ_CHICKEN_MBCUNIT_CONFIG) |
6951 GEN7_SQ_CHICKEN_MBCUNIT_SQINTMOB);
6952
46680e0a
VS
6953 gen7_setup_fixed_func_scheduler(dev_priv);
6954
3c0edaeb 6955 /*
0f846f81 6956 * According to the spec, bit 13 (RCZUNIT) must be set on IVB.
ecdb4eb7 6957 * This implements the WaDisableRCZUnitClockGating:vlv workaround.
0f846f81
JB
6958 */
6959 I915_WRITE(GEN6_UCGCTL2,
3c0edaeb 6960 GEN6_RCZUNIT_CLOCK_GATE_DISABLE);
0f846f81 6961
c98f5062
AG
6962 /* WaDisableL3Bank2xClockGate:vlv
6963 * Disabling L3 clock gating- MMIO 940c[25] = 1
6964 * Set bit 25, to disable L3_BANK_2x_CLK_GATING */
6965 I915_WRITE(GEN7_UCGCTL4,
6966 I915_READ(GEN7_UCGCTL4) | GEN7_L3BANK2X_CLOCK_GATE_DISABLE);
e3f33d46 6967
afd58e79
VS
6968 /*
6969 * BSpec says this must be set, even though
6970 * WaDisable4x2SubspanOptimization isn't listed for VLV.
6971 */
6b26c86d
DV
6972 I915_WRITE(CACHE_MODE_1,
6973 _MASKED_BIT_ENABLE(PIXEL_SUBSPAN_COLLECT_OPT_DISABLE));
7983117f 6974
da2518f9
VS
6975 /*
6976 * BSpec recommends 8x4 when MSAA is used,
6977 * however in practice 16x4 seems fastest.
6978 *
6979 * Note that PS/WM thread counts depend on the WIZ hashing
6980 * disable bit, which we don't touch here, but it's good
6981 * to keep in mind (see 3DSTATE_PS and 3DSTATE_WM).
6982 */
6983 I915_WRITE(GEN7_GT_MODE,
6984 _MASKED_FIELD(GEN6_WIZ_HASHING_MASK, GEN6_WIZ_HASHING_16x4));
6985
031994ee
VS
6986 /*
6987 * WaIncreaseL3CreditsForVLVB0:vlv
6988 * This is the hardware default actually.
6989 */
6990 I915_WRITE(GEN7_L3SQCREG1, VLV_B0_WA_L3SQCREG1_VALUE);
6991
2d809570 6992 /*
ecdb4eb7 6993 * WaDisableVLVClockGating_VBIIssue:vlv
2d809570
JB
6994 * Disable clock gating on th GCFG unit to prevent a delay
6995 * in the reporting of vblank events.
6996 */
7a0d1eed 6997 I915_WRITE(VLV_GUNIT_CLOCK_GATE, GCFG_DIS);
6f1d69b0
ED
6998}
6999
a4565da8
VS
7000static void cherryview_init_clock_gating(struct drm_device *dev)
7001{
7002 struct drm_i915_private *dev_priv = dev->dev_private;
7003
232ce337
VS
7004 /* WaVSRefCountFullforceMissDisable:chv */
7005 /* WaDSRefCountFullforceMissDisable:chv */
7006 I915_WRITE(GEN7_FF_THREAD_MODE,
7007 I915_READ(GEN7_FF_THREAD_MODE) &
7008 ~(GEN8_FF_DS_REF_CNT_FFME | GEN7_FF_VS_REF_CNT_FFME));
acea6f95
VS
7009
7010 /* WaDisableSemaphoreAndSyncFlipWait:chv */
7011 I915_WRITE(GEN6_RC_SLEEP_PSMI_CONTROL,
7012 _MASKED_BIT_ENABLE(GEN8_RC_SEMA_IDLE_MSG_DISABLE));
0846697c
VS
7013
7014 /* WaDisableCSUnitClockGating:chv */
7015 I915_WRITE(GEN6_UCGCTL1, I915_READ(GEN6_UCGCTL1) |
7016 GEN6_CSUNIT_CLOCK_GATE_DISABLE);
c631780f
VS
7017
7018 /* WaDisableSDEUnitClockGating:chv */
7019 I915_WRITE(GEN8_UCGCTL6, I915_READ(GEN8_UCGCTL6) |
7020 GEN8_SDEUNIT_CLOCK_GATE_DISABLE);
6d50b065 7021
450174fe
ID
7022 /*
7023 * WaProgramL3SqcReg1Default:chv
7024 * See gfxspecs/Related Documents/Performance Guide/
7025 * LSQC Setting Recommendations.
7026 */
7027 gen8_set_l3sqc_credits(dev_priv, 38, 2);
7028
6d50b065
VS
7029 /*
7030 * GTT cache may not work with big pages, so if those
7031 * are ever enabled GTT cache may need to be disabled.
7032 */
7033 I915_WRITE(HSW_GTT_CACHE_EN, GTT_CACHE_EN_ALL);
a4565da8
VS
7034}
7035
1fa61106 7036static void g4x_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
7037{
7038 struct drm_i915_private *dev_priv = dev->dev_private;
7039 uint32_t dspclk_gate;
7040
7041 I915_WRITE(RENCLK_GATE_D1, 0);
7042 I915_WRITE(RENCLK_GATE_D2, VF_UNIT_CLOCK_GATE_DISABLE |
7043 GS_UNIT_CLOCK_GATE_DISABLE |
7044 CL_UNIT_CLOCK_GATE_DISABLE);
7045 I915_WRITE(RAMCLK_GATE_D, 0);
7046 dspclk_gate = VRHUNIT_CLOCK_GATE_DISABLE |
7047 OVRUNIT_CLOCK_GATE_DISABLE |
7048 OVCUNIT_CLOCK_GATE_DISABLE;
7049 if (IS_GM45(dev))
7050 dspclk_gate |= DSSUNIT_CLOCK_GATE_DISABLE;
7051 I915_WRITE(DSPCLK_GATE_D, dspclk_gate);
4358a374
DV
7052
7053 /* WaDisableRenderCachePipelinedFlush */
7054 I915_WRITE(CACHE_MODE_0,
7055 _MASKED_BIT_ENABLE(CM0_PIPELINED_RENDER_FLUSH_DISABLE));
de1aa629 7056
4e04632e
AG
7057 /* WaDisable_RenderCache_OperationalFlush:g4x */
7058 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
7059
0e088b8f 7060 g4x_disable_trickle_feed(dev);
6f1d69b0
ED
7061}
7062
1fa61106 7063static void crestline_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
7064{
7065 struct drm_i915_private *dev_priv = dev->dev_private;
7066
7067 I915_WRITE(RENCLK_GATE_D1, I965_RCC_CLOCK_GATE_DISABLE);
7068 I915_WRITE(RENCLK_GATE_D2, 0);
7069 I915_WRITE(DSPCLK_GATE_D, 0);
7070 I915_WRITE(RAMCLK_GATE_D, 0);
7071 I915_WRITE16(DEUC, 0);
20f94967
VS
7072 I915_WRITE(MI_ARB_STATE,
7073 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
4e04632e
AG
7074
7075 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7076 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6f1d69b0
ED
7077}
7078
1fa61106 7079static void broadwater_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
7080{
7081 struct drm_i915_private *dev_priv = dev->dev_private;
7082
7083 I915_WRITE(RENCLK_GATE_D1, I965_RCZ_CLOCK_GATE_DISABLE |
7084 I965_RCC_CLOCK_GATE_DISABLE |
7085 I965_RCPB_CLOCK_GATE_DISABLE |
7086 I965_ISC_CLOCK_GATE_DISABLE |
7087 I965_FBC_CLOCK_GATE_DISABLE);
7088 I915_WRITE(RENCLK_GATE_D2, 0);
20f94967
VS
7089 I915_WRITE(MI_ARB_STATE,
7090 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
4e04632e
AG
7091
7092 /* WaDisable_RenderCache_OperationalFlush:gen4 */
7093 I915_WRITE(CACHE_MODE_0, _MASKED_BIT_DISABLE(RC_OP_FLUSH_ENABLE));
6f1d69b0
ED
7094}
7095
1fa61106 7096static void gen3_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
7097{
7098 struct drm_i915_private *dev_priv = dev->dev_private;
7099 u32 dstate = I915_READ(D_STATE);
7100
7101 dstate |= DSTATE_PLL_D3_OFF | DSTATE_GFX_CLOCK_GATING |
7102 DSTATE_DOT_CLOCK_GATING;
7103 I915_WRITE(D_STATE, dstate);
13a86b85
CW
7104
7105 if (IS_PINEVIEW(dev))
7106 I915_WRITE(ECOSKPD, _MASKED_BIT_ENABLE(ECO_GATING_CX_ONLY));
974a3b0f
DV
7107
7108 /* IIR "flip pending" means done if this bit is set */
7109 I915_WRITE(ECOSKPD, _MASKED_BIT_DISABLE(ECO_FLIP_DONE));
12fabbcb
VS
7110
7111 /* interrupts should cause a wake up from C3 */
3299254f 7112 I915_WRITE(INSTPM, _MASKED_BIT_ENABLE(INSTPM_AGPBUSY_INT_EN));
dbb42748
VS
7113
7114 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
7115 I915_WRITE(MI_ARB_STATE, _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
1038392b
VS
7116
7117 I915_WRITE(MI_ARB_STATE,
7118 _MASKED_BIT_ENABLE(MI_ARB_DISPLAY_TRICKLE_FEED_DISABLE));
6f1d69b0
ED
7119}
7120
1fa61106 7121static void i85x_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
7122{
7123 struct drm_i915_private *dev_priv = dev->dev_private;
7124
7125 I915_WRITE(RENCLK_GATE_D1, SV_CLOCK_GATE_DISABLE);
54e472ae
VS
7126
7127 /* interrupts should cause a wake up from C3 */
7128 I915_WRITE(MI_STATE, _MASKED_BIT_ENABLE(MI_AGPBUSY_INT_EN) |
7129 _MASKED_BIT_DISABLE(MI_AGPBUSY_830_MODE));
1038392b
VS
7130
7131 I915_WRITE(MEM_MODE,
7132 _MASKED_BIT_ENABLE(MEM_DISPLAY_TRICKLE_FEED_DISABLE));
6f1d69b0
ED
7133}
7134
1fa61106 7135static void i830_init_clock_gating(struct drm_device *dev)
6f1d69b0
ED
7136{
7137 struct drm_i915_private *dev_priv = dev->dev_private;
7138
7139 I915_WRITE(DSPCLK_GATE_D, OVRUNIT_CLOCK_GATE_DISABLE);
1038392b
VS
7140
7141 I915_WRITE(MEM_MODE,
7142 _MASKED_BIT_ENABLE(MEM_DISPLAY_A_TRICKLE_FEED_DISABLE) |
7143 _MASKED_BIT_ENABLE(MEM_DISPLAY_B_TRICKLE_FEED_DISABLE));
6f1d69b0
ED
7144}
7145
6f1d69b0
ED
7146void intel_init_clock_gating(struct drm_device *dev)
7147{
7148 struct drm_i915_private *dev_priv = dev->dev_private;
7149
bb400da9 7150 dev_priv->display.init_clock_gating(dev);
6f1d69b0
ED
7151}
7152
7d708ee4
ID
7153void intel_suspend_hw(struct drm_device *dev)
7154{
7155 if (HAS_PCH_LPT(dev))
7156 lpt_suspend_hw(dev);
7157}
7158
bb400da9
ID
7159static void nop_init_clock_gating(struct drm_device *dev)
7160{
7161 DRM_DEBUG_KMS("No clock gating settings or workarounds applied.\n");
7162}
7163
7164/**
7165 * intel_init_clock_gating_hooks - setup the clock gating hooks
7166 * @dev_priv: device private
7167 *
7168 * Setup the hooks that configure which clocks of a given platform can be
7169 * gated and also apply various GT and display specific workarounds for these
7170 * platforms. Note that some GT specific workarounds are applied separately
7171 * when GPU contexts or batchbuffers start their execution.
7172 */
7173void intel_init_clock_gating_hooks(struct drm_i915_private *dev_priv)
7174{
7175 if (IS_SKYLAKE(dev_priv))
7176 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7177 else if (IS_KABYLAKE(dev_priv))
7178 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7179 else if (IS_BROXTON(dev_priv))
7180 dev_priv->display.init_clock_gating = bxt_init_clock_gating;
7181 else if (IS_BROADWELL(dev_priv))
7182 dev_priv->display.init_clock_gating = broadwell_init_clock_gating;
7183 else if (IS_CHERRYVIEW(dev_priv))
7184 dev_priv->display.init_clock_gating = cherryview_init_clock_gating;
7185 else if (IS_HASWELL(dev_priv))
7186 dev_priv->display.init_clock_gating = haswell_init_clock_gating;
7187 else if (IS_IVYBRIDGE(dev_priv))
7188 dev_priv->display.init_clock_gating = ivybridge_init_clock_gating;
7189 else if (IS_VALLEYVIEW(dev_priv))
7190 dev_priv->display.init_clock_gating = valleyview_init_clock_gating;
7191 else if (IS_GEN6(dev_priv))
7192 dev_priv->display.init_clock_gating = gen6_init_clock_gating;
7193 else if (IS_GEN5(dev_priv))
7194 dev_priv->display.init_clock_gating = ironlake_init_clock_gating;
7195 else if (IS_G4X(dev_priv))
7196 dev_priv->display.init_clock_gating = g4x_init_clock_gating;
7197 else if (IS_CRESTLINE(dev_priv))
7198 dev_priv->display.init_clock_gating = crestline_init_clock_gating;
7199 else if (IS_BROADWATER(dev_priv))
7200 dev_priv->display.init_clock_gating = broadwater_init_clock_gating;
7201 else if (IS_GEN3(dev_priv))
7202 dev_priv->display.init_clock_gating = gen3_init_clock_gating;
7203 else if (IS_I85X(dev_priv) || IS_I865G(dev_priv))
7204 dev_priv->display.init_clock_gating = i85x_init_clock_gating;
7205 else if (IS_GEN2(dev_priv))
7206 dev_priv->display.init_clock_gating = i830_init_clock_gating;
7207 else {
7208 MISSING_CASE(INTEL_DEVID(dev_priv));
7209 dev_priv->display.init_clock_gating = nop_init_clock_gating;
7210 }
7211}
7212
1fa61106
ED
7213/* Set up chip specific power management-related functions */
7214void intel_init_pm(struct drm_device *dev)
7215{
7216 struct drm_i915_private *dev_priv = dev->dev_private;
7217
7ff0ebcc 7218 intel_fbc_init(dev_priv);
1fa61106 7219
c921aba8
DV
7220 /* For cxsr */
7221 if (IS_PINEVIEW(dev))
7222 i915_pineview_get_mem_freq(dev);
7223 else if (IS_GEN5(dev))
7224 i915_ironlake_get_mem_freq(dev);
7225
1fa61106 7226 /* For FIFO watermark updates */
f5ed50cb 7227 if (INTEL_INFO(dev)->gen >= 9) {
2af30a5c 7228 skl_setup_wm_latency(dev);
2d41c0b5 7229 dev_priv->display.update_wm = skl_update_wm;
c83155a6 7230 } else if (HAS_PCH_SPLIT(dev)) {
fa50ad61 7231 ilk_setup_wm_latency(dev);
53615a5e 7232
bd602544
VS
7233 if ((IS_GEN5(dev) && dev_priv->wm.pri_latency[1] &&
7234 dev_priv->wm.spr_latency[1] && dev_priv->wm.cur_latency[1]) ||
7235 (!IS_GEN5(dev) && dev_priv->wm.pri_latency[0] &&
7236 dev_priv->wm.spr_latency[0] && dev_priv->wm.cur_latency[0])) {
86c8bbbe 7237 dev_priv->display.compute_pipe_wm = ilk_compute_pipe_wm;
ed4a6a7c
MR
7238 dev_priv->display.compute_intermediate_wm =
7239 ilk_compute_intermediate_wm;
7240 dev_priv->display.initial_watermarks =
7241 ilk_initial_watermarks;
7242 dev_priv->display.optimize_watermarks =
7243 ilk_optimize_watermarks;
bd602544
VS
7244 } else {
7245 DRM_DEBUG_KMS("Failed to read display plane latency. "
7246 "Disable CxSR\n");
7247 }
a4565da8 7248 } else if (IS_CHERRYVIEW(dev)) {
262cd2e1 7249 vlv_setup_wm_latency(dev);
262cd2e1 7250 dev_priv->display.update_wm = vlv_update_wm;
1fa61106 7251 } else if (IS_VALLEYVIEW(dev)) {
26e1fe4f 7252 vlv_setup_wm_latency(dev);
26e1fe4f 7253 dev_priv->display.update_wm = vlv_update_wm;
1fa61106
ED
7254 } else if (IS_PINEVIEW(dev)) {
7255 if (!intel_get_cxsr_latency(IS_PINEVIEW_G(dev),
7256 dev_priv->is_ddr3,
7257 dev_priv->fsb_freq,
7258 dev_priv->mem_freq)) {
7259 DRM_INFO("failed to find known CxSR latency "
7260 "(found ddr%s fsb freq %d, mem freq %d), "
7261 "disabling CxSR\n",
7262 (dev_priv->is_ddr3 == 1) ? "3" : "2",
7263 dev_priv->fsb_freq, dev_priv->mem_freq);
7264 /* Disable CxSR and never update its watermark again */
5209b1f4 7265 intel_set_memory_cxsr(dev_priv, false);
1fa61106
ED
7266 dev_priv->display.update_wm = NULL;
7267 } else
7268 dev_priv->display.update_wm = pineview_update_wm;
1fa61106
ED
7269 } else if (IS_G4X(dev)) {
7270 dev_priv->display.update_wm = g4x_update_wm;
1fa61106
ED
7271 } else if (IS_GEN4(dev)) {
7272 dev_priv->display.update_wm = i965_update_wm;
1fa61106
ED
7273 } else if (IS_GEN3(dev)) {
7274 dev_priv->display.update_wm = i9xx_update_wm;
7275 dev_priv->display.get_fifo_size = i9xx_get_fifo_size;
feb56b93
DV
7276 } else if (IS_GEN2(dev)) {
7277 if (INTEL_INFO(dev)->num_pipes == 1) {
7278 dev_priv->display.update_wm = i845_update_wm;
1fa61106 7279 dev_priv->display.get_fifo_size = i845_get_fifo_size;
feb56b93
DV
7280 } else {
7281 dev_priv->display.update_wm = i9xx_update_wm;
1fa61106 7282 dev_priv->display.get_fifo_size = i830_get_fifo_size;
feb56b93 7283 }
feb56b93
DV
7284 } else {
7285 DRM_ERROR("unexpected fall-through in intel_init_pm\n");
1fa61106
ED
7286 }
7287}
7288
151a49d0 7289int sandybridge_pcode_read(struct drm_i915_private *dev_priv, u32 mbox, u32 *val)
42c0526c 7290{
4fc688ce 7291 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
42c0526c
BW
7292
7293 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7294 DRM_DEBUG_DRIVER("warning: pcode (read) mailbox access failed\n");
7295 return -EAGAIN;
7296 }
7297
7298 I915_WRITE(GEN6_PCODE_DATA, *val);
dddab346 7299 I915_WRITE(GEN6_PCODE_DATA1, 0);
42c0526c
BW
7300 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7301
7302 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7303 500)) {
7304 DRM_ERROR("timeout waiting for pcode read (%d) to finish\n", mbox);
7305 return -ETIMEDOUT;
7306 }
7307
7308 *val = I915_READ(GEN6_PCODE_DATA);
7309 I915_WRITE(GEN6_PCODE_DATA, 0);
7310
7311 return 0;
7312}
7313
151a49d0 7314int sandybridge_pcode_write(struct drm_i915_private *dev_priv, u32 mbox, u32 val)
42c0526c 7315{
4fc688ce 7316 WARN_ON(!mutex_is_locked(&dev_priv->rps.hw_lock));
42c0526c
BW
7317
7318 if (I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) {
7319 DRM_DEBUG_DRIVER("warning: pcode (write) mailbox access failed\n");
7320 return -EAGAIN;
7321 }
7322
7323 I915_WRITE(GEN6_PCODE_DATA, val);
7324 I915_WRITE(GEN6_PCODE_MAILBOX, GEN6_PCODE_READY | mbox);
7325
7326 if (wait_for((I915_READ(GEN6_PCODE_MAILBOX) & GEN6_PCODE_READY) == 0,
7327 500)) {
7328 DRM_ERROR("timeout waiting for pcode write (%d) to finish\n", mbox);
7329 return -ETIMEDOUT;
7330 }
7331
7332 I915_WRITE(GEN6_PCODE_DATA, 0);
7333
7334 return 0;
7335}
a0e4e199 7336
dd06f88c
VS
7337static int byt_gpu_freq(struct drm_i915_private *dev_priv, int val)
7338{
c30fec65
VS
7339 /*
7340 * N = val - 0xb7
7341 * Slow = Fast = GPLL ref * N
7342 */
7343 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * (val - 0xb7), 1000);
855ba3be
JB
7344}
7345
b55dd647 7346static int byt_freq_opcode(struct drm_i915_private *dev_priv, int val)
855ba3be 7347{
c30fec65 7348 return DIV_ROUND_CLOSEST(1000 * val, dev_priv->rps.gpll_ref_freq) + 0xb7;
855ba3be
JB
7349}
7350
b55dd647 7351static int chv_gpu_freq(struct drm_i915_private *dev_priv, int val)
22b1b2f8 7352{
c30fec65
VS
7353 /*
7354 * N = val / 2
7355 * CU (slow) = CU2x (fast) / 2 = GPLL ref * N / 2
7356 */
7357 return DIV_ROUND_CLOSEST(dev_priv->rps.gpll_ref_freq * val, 2 * 2 * 1000);
22b1b2f8
D
7358}
7359
b55dd647 7360static int chv_freq_opcode(struct drm_i915_private *dev_priv, int val)
22b1b2f8 7361{
1c14762d 7362 /* CHV needs even values */
c30fec65 7363 return DIV_ROUND_CLOSEST(2 * 1000 * val, dev_priv->rps.gpll_ref_freq) * 2;
22b1b2f8
D
7364}
7365
616bc820 7366int intel_gpu_freq(struct drm_i915_private *dev_priv, int val)
22b1b2f8 7367{
2d1fe073 7368 if (IS_GEN9(dev_priv))
500a3d2e
MK
7369 return DIV_ROUND_CLOSEST(val * GT_FREQUENCY_MULTIPLIER,
7370 GEN9_FREQ_SCALER);
2d1fe073 7371 else if (IS_CHERRYVIEW(dev_priv))
616bc820 7372 return chv_gpu_freq(dev_priv, val);
2d1fe073 7373 else if (IS_VALLEYVIEW(dev_priv))
616bc820
VS
7374 return byt_gpu_freq(dev_priv, val);
7375 else
7376 return val * GT_FREQUENCY_MULTIPLIER;
22b1b2f8
D
7377}
7378
616bc820
VS
7379int intel_freq_opcode(struct drm_i915_private *dev_priv, int val)
7380{
2d1fe073 7381 if (IS_GEN9(dev_priv))
500a3d2e
MK
7382 return DIV_ROUND_CLOSEST(val * GEN9_FREQ_SCALER,
7383 GT_FREQUENCY_MULTIPLIER);
2d1fe073 7384 else if (IS_CHERRYVIEW(dev_priv))
616bc820 7385 return chv_freq_opcode(dev_priv, val);
2d1fe073 7386 else if (IS_VALLEYVIEW(dev_priv))
616bc820
VS
7387 return byt_freq_opcode(dev_priv, val);
7388 else
500a3d2e 7389 return DIV_ROUND_CLOSEST(val, GT_FREQUENCY_MULTIPLIER);
616bc820 7390}
22b1b2f8 7391
6ad790c0
CW
7392struct request_boost {
7393 struct work_struct work;
eed29a5b 7394 struct drm_i915_gem_request *req;
6ad790c0
CW
7395};
7396
7397static void __intel_rps_boost_work(struct work_struct *work)
7398{
7399 struct request_boost *boost = container_of(work, struct request_boost, work);
e61b9958 7400 struct drm_i915_gem_request *req = boost->req;
6ad790c0 7401
e61b9958 7402 if (!i915_gem_request_completed(req, true))
4a570db5 7403 gen6_rps_boost(to_i915(req->engine->dev), NULL,
e61b9958 7404 req->emitted_jiffies);
6ad790c0 7405
73db04cf 7406 i915_gem_request_unreference(req);
6ad790c0
CW
7407 kfree(boost);
7408}
7409
91d14251 7410void intel_queue_rps_boost_for_request(struct drm_i915_gem_request *req)
6ad790c0
CW
7411{
7412 struct request_boost *boost;
7413
91d14251 7414 if (req == NULL || INTEL_GEN(req->i915) < 6)
6ad790c0
CW
7415 return;
7416
e61b9958
CW
7417 if (i915_gem_request_completed(req, true))
7418 return;
7419
6ad790c0
CW
7420 boost = kmalloc(sizeof(*boost), GFP_ATOMIC);
7421 if (boost == NULL)
7422 return;
7423
eed29a5b
DV
7424 i915_gem_request_reference(req);
7425 boost->req = req;
6ad790c0
CW
7426
7427 INIT_WORK(&boost->work, __intel_rps_boost_work);
91d14251 7428 queue_work(req->i915->wq, &boost->work);
6ad790c0
CW
7429}
7430
f742a552 7431void intel_pm_setup(struct drm_device *dev)
907b28c5
CW
7432{
7433 struct drm_i915_private *dev_priv = dev->dev_private;
7434
f742a552 7435 mutex_init(&dev_priv->rps.hw_lock);
8d3afd7d 7436 spin_lock_init(&dev_priv->rps.client_lock);
f742a552 7437
907b28c5
CW
7438 INIT_DELAYED_WORK(&dev_priv->rps.delayed_resume_work,
7439 intel_gen6_powersave_work);
1854d5ca 7440 INIT_LIST_HEAD(&dev_priv->rps.clients);
2e1b8730
CW
7441 INIT_LIST_HEAD(&dev_priv->rps.semaphores.link);
7442 INIT_LIST_HEAD(&dev_priv->rps.mmioflips.link);
5d584b2e 7443
33688d95 7444 dev_priv->pm.suspended = false;
1f814dac 7445 atomic_set(&dev_priv->pm.wakeref_count, 0);
2b19efeb 7446 atomic_set(&dev_priv->pm.atomic_seq, 0);
907b28c5 7447}