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