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